blob: d4c27b74d9403412a543180157e99b0fc0893e3a [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;
The Android Open Source Projectc2ad2412009-03-19 23:08:54 -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;
Jean-Baptiste Querua8675f62009-07-29 14:25:07 -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;
Jean-Baptiste Querua8675f62009-07-29 14:25:07 -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;
Jean-Baptiste Querua8675f62009-07-29 14:25:07 -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;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -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;
Jean-Baptiste Querua8675f62009-07-29 14:25:07 -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;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 static final boolean PROFILE_ORIENTATION = false;
143 static final boolean BLUR = true;
Jean-Baptiste Queru843ef362009-05-20 11:28:04 -0700144 static final boolean localLOGV = DEBUG;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 static final int LOG_WM_NO_SURFACE_MEMORY = 31000;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -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;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -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;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -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;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -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;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -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
Jean-Baptiste Querua8675f62009-07-29 14:25:07 -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;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700186
187 static final int INJECT_FAILED = 0;
188 static final int INJECT_SUCCEEDED = 1;
189 static final int INJECT_NO_PERMISSION = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190
191 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;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700195
Jean-Baptiste Queru843ef362009-05-20 11:28:04 -0700196 /** The minimum time between dispatching touch events. */
197 int mMinWaitTimeBetweenTouchEvents = 1000 / 35;
198
199 // Last touch event time
200 long mLastTouchEventTime = 0;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700201
Jean-Baptiste Queru843ef362009-05-20 11:28:04 -0700202 // Last touch event type
203 int mLastTouchEventType = OTHER_EVENT;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700204
Jean-Baptiste Queru843ef362009-05-20 11:28:04 -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
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700212 // Last time we updated battery stats
Jean-Baptiste Queru843ef362009-05-20 11:28:04 -0700213 long mLastBatteryStatsCallTime = 0;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 private static final String SYSTEM_SECURE = "ro.secure";
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -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;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700242
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 final boolean mLimitedAlphaCompositing;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 final WindowManagerPolicy mPolicy = PolicyManager.makeNewWindowManager();
246
247 final IActivityManager mActivityManager;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 final IBatteryStats mBatteryStats;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -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>();
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -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>();
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -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;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 IInputMethodManager mInputMethodManager;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 SurfaceSession mFxSession;
Jean-Baptiste Querua8675f62009-07-29 14:25:07 -0700336 private DimAnimator mDimAnimator = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 Surface mBlurSurface;
338 boolean mBlurShown;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 int mTransactionSequence = 0;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -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 Hackborncc956672009-03-26 00:04:52 -0700350 int mLastRotationFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 ArrayList<IRotationWatcher> mRotationWatchers
352 = new ArrayList<IRotationWatcher>();
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -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;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -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>();
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -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;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -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;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -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;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -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;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 float mWindowAnimationScale = 1.0f;
403 float mTransitionAnimationScale = 1.0f;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -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;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -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();
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700421
Dianne Hackborn9d4de632009-03-24 19:37:25 -0700422 final Configuration mTempConfiguration = new Configuration();
Jean-Baptiste Querua8675f62009-07-29 14:25:07 -0700423 int mScreenLayout = Configuration.SCREENLAYOUT_SIZE_UNDEFINED;
424
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();
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -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 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 return thr.mService;
446 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 static class WMThread extends Thread {
449 WindowManagerService mService;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -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;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -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 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -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);
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 synchronized (this) {
471 mService = s;
472 notifyAll();
473 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -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;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -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 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -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);
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 synchronized (this) {
505 mRunning = true;
506 notifyAll();
507 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -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);
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -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);
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700535
Jean-Baptiste Queruac1e59d2009-08-11 13:12:34 -0700536 int max_events_per_sec = 35;
537 try {
538 max_events_per_sec = Integer.parseInt(SystemProperties
539 .get("windowsmgr.max_events_per_sec"));
540 if (max_events_per_sec < 1) {
541 max_events_per_sec = 35;
542 }
543 } catch (NumberFormatException e) {
544 }
545 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 mQueue = new KeyQ();
548
549 mInputThread = new InputDispatcherThread();
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700550
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 PolicyThread thr = new PolicyThread(mPolicy, this, context, pm);
552 thr.start();
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 synchronized (thr) {
555 while (!thr.mRunning) {
556 try {
557 thr.wait();
558 } catch (InterruptedException e) {
559 }
560 }
561 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700562
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 mInputThread.start();
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 // Add ourself to the Watchdog monitors.
566 Watchdog.getInstance().addMonitor(this);
567 }
568
569 @Override
570 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
571 throws RemoteException {
572 try {
573 return super.onTransact(code, data, reply, flags);
574 } catch (RuntimeException e) {
575 // The window manager only throws security exceptions, so let's
576 // log all others.
577 if (!(e instanceof SecurityException)) {
578 Log.e(TAG, "Window Manager Crash", e);
579 }
580 throw e;
581 }
582 }
583
584 private void placeWindowAfter(Object pos, WindowState window) {
585 final int i = mWindows.indexOf(pos);
586 if (localLOGV || DEBUG_FOCUS) Log.v(
587 TAG, "Adding window " + window + " at "
588 + (i+1) + " of " + mWindows.size() + " (after " + pos + ")");
589 mWindows.add(i+1, window);
590 }
591
592 private void placeWindowBefore(Object pos, WindowState window) {
593 final int i = mWindows.indexOf(pos);
594 if (localLOGV || DEBUG_FOCUS) Log.v(
595 TAG, "Adding window " + window + " at "
596 + i + " of " + mWindows.size() + " (before " + pos + ")");
597 mWindows.add(i, window);
598 }
599
600 //This method finds out the index of a window that has the same app token as
601 //win. used for z ordering the windows in mWindows
602 private int findIdxBasedOnAppTokens(WindowState win) {
603 //use a local variable to cache mWindows
604 ArrayList localmWindows = mWindows;
605 int jmax = localmWindows.size();
606 if(jmax == 0) {
607 return -1;
608 }
609 for(int j = (jmax-1); j >= 0; j--) {
610 WindowState wentry = (WindowState)localmWindows.get(j);
611 if(wentry.mAppToken == win.mAppToken) {
612 return j;
613 }
614 }
615 return -1;
616 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700617
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 private void addWindowToListInOrderLocked(WindowState win, boolean addToToken) {
619 final IWindow client = win.mClient;
620 final WindowToken token = win.mToken;
621 final ArrayList localmWindows = mWindows;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700622
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 final int N = localmWindows.size();
624 final WindowState attached = win.mAttachedWindow;
625 int i;
626 if (attached == null) {
627 int tokenWindowsPos = token.windows.size();
628 if (token.appWindowToken != null) {
629 int index = tokenWindowsPos-1;
630 if (index >= 0) {
631 // If this application has existing windows, we
632 // simply place the new window on top of them... but
633 // keep the starting window on top.
634 if (win.mAttrs.type == TYPE_BASE_APPLICATION) {
635 // Base windows go behind everything else.
636 placeWindowBefore(token.windows.get(0), win);
637 tokenWindowsPos = 0;
638 } else {
639 AppWindowToken atoken = win.mAppToken;
640 if (atoken != null &&
641 token.windows.get(index) == atoken.startingWindow) {
642 placeWindowBefore(token.windows.get(index), win);
643 tokenWindowsPos--;
644 } else {
645 int newIdx = findIdxBasedOnAppTokens(win);
646 if(newIdx != -1) {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700647 //there is a window above this one associated with the same
648 //apptoken note that the window could be a floating window
649 //that was created later or a window at the top of the list of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650 //windows associated with this token.
651 localmWindows.add(newIdx+1, win);
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700652 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653 }
654 }
655 } else {
656 if (localLOGV) Log.v(
657 TAG, "Figuring out where to add app window "
658 + client.asBinder() + " (token=" + token + ")");
659 // Figure out where the window should go, based on the
660 // order of applications.
661 final int NA = mAppTokens.size();
662 Object pos = null;
663 for (i=NA-1; i>=0; i--) {
664 AppWindowToken t = mAppTokens.get(i);
665 if (t == token) {
666 i--;
667 break;
668 }
669 if (t.windows.size() > 0) {
670 pos = t.windows.get(0);
671 }
672 }
673 // We now know the index into the apps. If we found
674 // an app window above, that gives us the position; else
675 // we need to look some more.
676 if (pos != null) {
677 // Move behind any windows attached to this one.
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700678 WindowToken atoken =
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679 mTokenMap.get(((WindowState)pos).mClient.asBinder());
680 if (atoken != null) {
681 final int NC = atoken.windows.size();
682 if (NC > 0) {
683 WindowState bottom = atoken.windows.get(0);
684 if (bottom.mSubLayer < 0) {
685 pos = bottom;
686 }
687 }
688 }
689 placeWindowBefore(pos, win);
690 } else {
691 while (i >= 0) {
692 AppWindowToken t = mAppTokens.get(i);
693 final int NW = t.windows.size();
694 if (NW > 0) {
695 pos = t.windows.get(NW-1);
696 break;
697 }
698 i--;
699 }
700 if (pos != null) {
701 // Move in front of any windows attached to this
702 // one.
703 WindowToken atoken =
704 mTokenMap.get(((WindowState)pos).mClient.asBinder());
705 if (atoken != null) {
706 final int NC = atoken.windows.size();
707 if (NC > 0) {
708 WindowState top = atoken.windows.get(NC-1);
709 if (top.mSubLayer >= 0) {
710 pos = top;
711 }
712 }
713 }
714 placeWindowAfter(pos, win);
715 } else {
716 // Just search for the start of this layer.
717 final int myLayer = win.mBaseLayer;
718 for (i=0; i<N; i++) {
719 WindowState w = (WindowState)localmWindows.get(i);
720 if (w.mBaseLayer > myLayer) {
721 break;
722 }
723 }
724 if (localLOGV || DEBUG_FOCUS) Log.v(
725 TAG, "Adding window " + win + " at "
726 + i + " of " + N);
727 localmWindows.add(i, win);
728 }
729 }
730 }
731 } else {
732 // Figure out where window should go, based on layer.
733 final int myLayer = win.mBaseLayer;
734 for (i=N-1; i>=0; i--) {
735 if (((WindowState)localmWindows.get(i)).mBaseLayer <= myLayer) {
736 i++;
737 break;
738 }
739 }
740 if (i < 0) i = 0;
741 if (localLOGV || DEBUG_FOCUS) Log.v(
742 TAG, "Adding window " + win + " at "
743 + i + " of " + N);
744 localmWindows.add(i, win);
745 }
746 if (addToToken) {
747 token.windows.add(tokenWindowsPos, win);
748 }
749
750 } else {
751 // Figure out this window's ordering relative to the window
752 // it is attached to.
753 final int NA = token.windows.size();
754 final int sublayer = win.mSubLayer;
755 int largestSublayer = Integer.MIN_VALUE;
756 WindowState windowWithLargestSublayer = null;
757 for (i=0; i<NA; i++) {
758 WindowState w = token.windows.get(i);
759 final int wSublayer = w.mSubLayer;
760 if (wSublayer >= largestSublayer) {
761 largestSublayer = wSublayer;
762 windowWithLargestSublayer = w;
763 }
764 if (sublayer < 0) {
765 // For negative sublayers, we go below all windows
766 // in the same sublayer.
767 if (wSublayer >= sublayer) {
768 if (addToToken) {
769 token.windows.add(i, win);
770 }
771 placeWindowBefore(
772 wSublayer >= 0 ? attached : w, win);
773 break;
774 }
775 } else {
776 // For positive sublayers, we go above all windows
777 // in the same sublayer.
778 if (wSublayer > sublayer) {
779 if (addToToken) {
780 token.windows.add(i, win);
781 }
782 placeWindowBefore(w, win);
783 break;
784 }
785 }
786 }
787 if (i >= NA) {
788 if (addToToken) {
789 token.windows.add(win);
790 }
791 if (sublayer < 0) {
792 placeWindowBefore(attached, win);
793 } else {
794 placeWindowAfter(largestSublayer >= 0
795 ? windowWithLargestSublayer
796 : attached,
797 win);
798 }
799 }
800 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700801
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800802 if (win.mAppToken != null && addToToken) {
803 win.mAppToken.allAppWindows.add(win);
804 }
805 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700806
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800807 static boolean canBeImeTarget(WindowState w) {
808 final int fl = w.mAttrs.flags
809 & (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM);
810 if (fl == 0 || fl == (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM)) {
811 return w.isVisibleOrAdding();
812 }
813 return false;
814 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700815
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800816 int findDesiredInputMethodWindowIndexLocked(boolean willMove) {
817 final ArrayList localmWindows = mWindows;
818 final int N = localmWindows.size();
819 WindowState w = null;
820 int i = N;
821 while (i > 0) {
822 i--;
823 w = (WindowState)localmWindows.get(i);
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700824
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800825 //Log.i(TAG, "Checking window @" + i + " " + w + " fl=0x"
826 // + Integer.toHexString(w.mAttrs.flags));
827 if (canBeImeTarget(w)) {
828 //Log.i(TAG, "Putting input method here!");
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700829
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800830 // Yet more tricksyness! If this window is a "starting"
831 // window, we do actually want to be on top of it, but
832 // it is not -really- where input will go. So if the caller
833 // is not actually looking to move the IME, look down below
834 // for a real window to target...
835 if (!willMove
836 && w.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
837 && i > 0) {
838 WindowState wb = (WindowState)localmWindows.get(i-1);
839 if (wb.mAppToken == w.mAppToken && canBeImeTarget(wb)) {
840 i--;
841 w = wb;
842 }
843 }
844 break;
845 }
846 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700847
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800848 mUpcomingInputMethodTarget = w;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700849
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800850 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Desired input method target="
851 + w + " willMove=" + willMove);
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700852
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800853 if (willMove && w != null) {
854 final WindowState curTarget = mInputMethodTarget;
855 if (curTarget != null && curTarget.mAppToken != null) {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800857 // Now some fun for dealing with window animations that
858 // modify the Z order. We need to look at all windows below
859 // the current target that are in this app, finding the highest
860 // visible one in layering.
861 AppWindowToken token = curTarget.mAppToken;
862 WindowState highestTarget = null;
863 int highestPos = 0;
864 if (token.animating || token.animation != null) {
865 int pos = 0;
866 pos = localmWindows.indexOf(curTarget);
867 while (pos >= 0) {
868 WindowState win = (WindowState)localmWindows.get(pos);
869 if (win.mAppToken != token) {
870 break;
871 }
872 if (!win.mRemoved) {
873 if (highestTarget == null || win.mAnimLayer >
874 highestTarget.mAnimLayer) {
875 highestTarget = win;
876 highestPos = pos;
877 }
878 }
879 pos--;
880 }
881 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700882
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800883 if (highestTarget != null) {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700884 if (DEBUG_INPUT_METHOD) Log.v(TAG, "mNextAppTransition="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800885 + mNextAppTransition + " " + highestTarget
886 + " animating=" + highestTarget.isAnimating()
887 + " layer=" + highestTarget.mAnimLayer
888 + " new layer=" + w.mAnimLayer);
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700889
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800890 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
891 // If we are currently setting up for an animation,
892 // hold everything until we can find out what will happen.
893 mInputMethodTargetWaitingAnim = true;
894 mInputMethodTarget = highestTarget;
895 return highestPos + 1;
896 } else if (highestTarget.isAnimating() &&
897 highestTarget.mAnimLayer > w.mAnimLayer) {
898 // If the window we are currently targeting is involved
899 // with an animation, and it is on top of the next target
900 // we will be over, then hold off on moving until
901 // that is done.
902 mInputMethodTarget = highestTarget;
903 return highestPos + 1;
904 }
905 }
906 }
907 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700908
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909 //Log.i(TAG, "Placing input method @" + (i+1));
910 if (w != null) {
911 if (willMove) {
912 RuntimeException e = new RuntimeException();
913 e.fillInStackTrace();
914 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
915 + mInputMethodTarget + " to " + w, e);
916 mInputMethodTarget = w;
917 if (w.mAppToken != null) {
918 setInputMethodAnimLayerAdjustment(w.mAppToken.animLayerAdjustment);
919 } else {
920 setInputMethodAnimLayerAdjustment(0);
921 }
922 }
923 return i+1;
924 }
925 if (willMove) {
926 RuntimeException e = new RuntimeException();
927 e.fillInStackTrace();
928 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
929 + mInputMethodTarget + " to null", e);
930 mInputMethodTarget = null;
931 setInputMethodAnimLayerAdjustment(0);
932 }
933 return -1;
934 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936 void addInputMethodWindowToListLocked(WindowState win) {
937 int pos = findDesiredInputMethodWindowIndexLocked(true);
938 if (pos >= 0) {
939 win.mTargetAppToken = mInputMethodTarget.mAppToken;
940 mWindows.add(pos, win);
941 moveInputMethodDialogsLocked(pos+1);
942 return;
943 }
944 win.mTargetAppToken = null;
945 addWindowToListInOrderLocked(win, true);
946 moveInputMethodDialogsLocked(pos);
947 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700948
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800949 void setInputMethodAnimLayerAdjustment(int adj) {
950 if (DEBUG_LAYERS) Log.v(TAG, "Setting im layer adj to " + adj);
951 mInputMethodAnimLayerAdjustment = adj;
952 WindowState imw = mInputMethodWindow;
953 if (imw != null) {
954 imw.mAnimLayer = imw.mLayer + adj;
955 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
956 + " anim layer: " + imw.mAnimLayer);
957 int wi = imw.mChildWindows.size();
958 while (wi > 0) {
959 wi--;
960 WindowState cw = (WindowState)imw.mChildWindows.get(wi);
961 cw.mAnimLayer = cw.mLayer + adj;
962 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + cw
963 + " anim layer: " + cw.mAnimLayer);
964 }
965 }
966 int di = mInputMethodDialogs.size();
967 while (di > 0) {
968 di --;
969 imw = mInputMethodDialogs.get(di);
970 imw.mAnimLayer = imw.mLayer + adj;
971 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
972 + " anim layer: " + imw.mAnimLayer);
973 }
974 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700975
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800976 private int tmpRemoveWindowLocked(int interestingPos, WindowState win) {
977 int wpos = mWindows.indexOf(win);
978 if (wpos >= 0) {
979 if (wpos < interestingPos) interestingPos--;
980 mWindows.remove(wpos);
981 int NC = win.mChildWindows.size();
982 while (NC > 0) {
983 NC--;
984 WindowState cw = (WindowState)win.mChildWindows.get(NC);
985 int cpos = mWindows.indexOf(cw);
986 if (cpos >= 0) {
987 if (cpos < interestingPos) interestingPos--;
988 mWindows.remove(cpos);
989 }
990 }
991 }
992 return interestingPos;
993 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700994
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800995 private void reAddWindowToListInOrderLocked(WindowState win) {
996 addWindowToListInOrderLocked(win, false);
997 // This is a hack to get all of the child windows added as well
998 // at the right position. Child windows should be rare and
999 // this case should be rare, so it shouldn't be that big a deal.
1000 int wpos = mWindows.indexOf(win);
1001 if (wpos >= 0) {
1002 mWindows.remove(wpos);
1003 reAddWindowLocked(wpos, win);
1004 }
1005 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 void logWindowList(String prefix) {
1008 int N = mWindows.size();
1009 while (N > 0) {
1010 N--;
1011 Log.v(TAG, prefix + "#" + N + ": " + mWindows.get(N));
1012 }
1013 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001014
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 void moveInputMethodDialogsLocked(int pos) {
1016 ArrayList<WindowState> dialogs = mInputMethodDialogs;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001017
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 final int N = dialogs.size();
1019 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Removing " + N + " dialogs w/pos=" + pos);
1020 for (int i=0; i<N; i++) {
1021 pos = tmpRemoveWindowLocked(pos, dialogs.get(i));
1022 }
1023 if (DEBUG_INPUT_METHOD) {
1024 Log.v(TAG, "Window list w/pos=" + pos);
1025 logWindowList(" ");
1026 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001027
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 if (pos >= 0) {
1029 final AppWindowToken targetAppToken = mInputMethodTarget.mAppToken;
1030 if (pos < mWindows.size()) {
1031 WindowState wp = (WindowState)mWindows.get(pos);
1032 if (wp == mInputMethodWindow) {
1033 pos++;
1034 }
1035 }
1036 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Adding " + N + " dialogs at pos=" + pos);
1037 for (int i=0; i<N; i++) {
1038 WindowState win = dialogs.get(i);
1039 win.mTargetAppToken = targetAppToken;
1040 pos = reAddWindowLocked(pos, win);
1041 }
1042 if (DEBUG_INPUT_METHOD) {
1043 Log.v(TAG, "Final window list:");
1044 logWindowList(" ");
1045 }
1046 return;
1047 }
1048 for (int i=0; i<N; i++) {
1049 WindowState win = dialogs.get(i);
1050 win.mTargetAppToken = null;
1051 reAddWindowToListInOrderLocked(win);
1052 if (DEBUG_INPUT_METHOD) {
1053 Log.v(TAG, "No IM target, final list:");
1054 logWindowList(" ");
1055 }
1056 }
1057 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059 boolean moveInputMethodWindowsIfNeededLocked(boolean needAssignLayers) {
1060 final WindowState imWin = mInputMethodWindow;
1061 final int DN = mInputMethodDialogs.size();
1062 if (imWin == null && DN == 0) {
1063 return false;
1064 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001066 int imPos = findDesiredInputMethodWindowIndexLocked(true);
1067 if (imPos >= 0) {
1068 // In this case, the input method windows are to be placed
1069 // immediately above the window they are targeting.
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001071 // First check to see if the input method windows are already
1072 // located here, and contiguous.
1073 final int N = mWindows.size();
1074 WindowState firstImWin = imPos < N
1075 ? (WindowState)mWindows.get(imPos) : null;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077 // Figure out the actual input method window that should be
1078 // at the bottom of their stack.
1079 WindowState baseImWin = imWin != null
1080 ? imWin : mInputMethodDialogs.get(0);
1081 if (baseImWin.mChildWindows.size() > 0) {
1082 WindowState cw = (WindowState)baseImWin.mChildWindows.get(0);
1083 if (cw.mSubLayer < 0) baseImWin = cw;
1084 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001086 if (firstImWin == baseImWin) {
1087 // The windows haven't moved... but are they still contiguous?
1088 // First find the top IM window.
1089 int pos = imPos+1;
1090 while (pos < N) {
1091 if (!((WindowState)mWindows.get(pos)).mIsImWindow) {
1092 break;
1093 }
1094 pos++;
1095 }
1096 pos++;
1097 // Now there should be no more input method windows above.
1098 while (pos < N) {
1099 if (((WindowState)mWindows.get(pos)).mIsImWindow) {
1100 break;
1101 }
1102 pos++;
1103 }
1104 if (pos >= N) {
1105 // All is good!
1106 return false;
1107 }
1108 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110 if (imWin != null) {
1111 if (DEBUG_INPUT_METHOD) {
1112 Log.v(TAG, "Moving IM from " + imPos);
1113 logWindowList(" ");
1114 }
1115 imPos = tmpRemoveWindowLocked(imPos, imWin);
1116 if (DEBUG_INPUT_METHOD) {
1117 Log.v(TAG, "List after moving with new pos " + imPos + ":");
1118 logWindowList(" ");
1119 }
1120 imWin.mTargetAppToken = mInputMethodTarget.mAppToken;
1121 reAddWindowLocked(imPos, imWin);
1122 if (DEBUG_INPUT_METHOD) {
1123 Log.v(TAG, "List after moving IM to " + imPos + ":");
1124 logWindowList(" ");
1125 }
1126 if (DN > 0) moveInputMethodDialogsLocked(imPos+1);
1127 } else {
1128 moveInputMethodDialogsLocked(imPos);
1129 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001131 } else {
1132 // In this case, the input method windows go in a fixed layer,
1133 // because they aren't currently associated with a focus window.
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 if (imWin != null) {
1136 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Moving IM from " + imPos);
1137 tmpRemoveWindowLocked(0, imWin);
1138 imWin.mTargetAppToken = null;
1139 reAddWindowToListInOrderLocked(imWin);
1140 if (DEBUG_INPUT_METHOD) {
1141 Log.v(TAG, "List with no IM target:");
1142 logWindowList(" ");
1143 }
1144 if (DN > 0) moveInputMethodDialogsLocked(-1);;
1145 } else {
1146 moveInputMethodDialogsLocked(-1);;
1147 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001149 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 if (needAssignLayers) {
1152 assignLayersLocked();
1153 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 return true;
1156 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001158 void adjustInputMethodDialogsLocked() {
1159 moveInputMethodDialogsLocked(findDesiredInputMethodWindowIndexLocked(true));
1160 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001162 public int addWindow(Session session, IWindow client,
1163 WindowManager.LayoutParams attrs, int viewVisibility,
1164 Rect outContentInsets) {
1165 int res = mPolicy.checkAddPermission(attrs);
1166 if (res != WindowManagerImpl.ADD_OKAY) {
1167 return res;
1168 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001170 boolean reportNewConfig = false;
1171 WindowState attachedWindow = null;
1172 WindowState win = null;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174 synchronized(mWindowMap) {
1175 // Instantiating a Display requires talking with the simulator,
1176 // so don't do it until we know the system is mostly up and
1177 // running.
1178 if (mDisplay == null) {
1179 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
1180 mDisplay = wm.getDefaultDisplay();
1181 mQueue.setDisplay(mDisplay);
1182 reportNewConfig = true;
1183 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 if (mWindowMap.containsKey(client.asBinder())) {
1186 Log.w(TAG, "Window " + client + " is already added");
1187 return WindowManagerImpl.ADD_DUPLICATE_ADD;
1188 }
1189
1190 if (attrs.type >= FIRST_SUB_WINDOW && attrs.type <= LAST_SUB_WINDOW) {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001191 attachedWindow = windowForClientLocked(null, attrs.token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 if (attachedWindow == null) {
1193 Log.w(TAG, "Attempted to add window with token that is not a window: "
1194 + attrs.token + ". Aborting.");
1195 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1196 }
1197 if (attachedWindow.mAttrs.type >= FIRST_SUB_WINDOW
1198 && attachedWindow.mAttrs.type <= LAST_SUB_WINDOW) {
1199 Log.w(TAG, "Attempted to add window with token that is a sub-window: "
1200 + attrs.token + ". Aborting.");
1201 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1202 }
1203 }
1204
1205 boolean addToken = false;
1206 WindowToken token = mTokenMap.get(attrs.token);
1207 if (token == null) {
1208 if (attrs.type >= FIRST_APPLICATION_WINDOW
1209 && attrs.type <= LAST_APPLICATION_WINDOW) {
1210 Log.w(TAG, "Attempted to add application window with unknown token "
1211 + attrs.token + ". Aborting.");
1212 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1213 }
1214 if (attrs.type == TYPE_INPUT_METHOD) {
1215 Log.w(TAG, "Attempted to add input method window with unknown token "
1216 + attrs.token + ". Aborting.");
1217 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1218 }
1219 token = new WindowToken(attrs.token, -1, false);
1220 addToken = true;
1221 } else if (attrs.type >= FIRST_APPLICATION_WINDOW
1222 && attrs.type <= LAST_APPLICATION_WINDOW) {
1223 AppWindowToken atoken = token.appWindowToken;
1224 if (atoken == null) {
1225 Log.w(TAG, "Attempted to add window with non-application token "
1226 + token + ". Aborting.");
1227 return WindowManagerImpl.ADD_NOT_APP_TOKEN;
1228 } else if (atoken.removed) {
1229 Log.w(TAG, "Attempted to add window with exiting application token "
1230 + token + ". Aborting.");
1231 return WindowManagerImpl.ADD_APP_EXITING;
1232 }
1233 if (attrs.type == TYPE_APPLICATION_STARTING && atoken.firstWindowDrawn) {
1234 // No need for this guy!
1235 if (localLOGV) Log.v(
1236 TAG, "**** NO NEED TO START: " + attrs.getTitle());
1237 return WindowManagerImpl.ADD_STARTING_NOT_NEEDED;
1238 }
1239 } else if (attrs.type == TYPE_INPUT_METHOD) {
1240 if (token.windowType != TYPE_INPUT_METHOD) {
1241 Log.w(TAG, "Attempted to add input method window with bad token "
1242 + attrs.token + ". Aborting.");
1243 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1244 }
1245 }
1246
1247 win = new WindowState(session, client, token,
1248 attachedWindow, attrs, viewVisibility);
1249 if (win.mDeathRecipient == null) {
1250 // Client has apparently died, so there is no reason to
1251 // continue.
1252 Log.w(TAG, "Adding window client " + client.asBinder()
1253 + " that is dead, aborting.");
1254 return WindowManagerImpl.ADD_APP_EXITING;
1255 }
1256
1257 mPolicy.adjustWindowParamsLw(win.mAttrs);
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259 res = mPolicy.prepareAddWindowLw(win, attrs);
1260 if (res != WindowManagerImpl.ADD_OKAY) {
1261 return res;
1262 }
1263
1264 // From now on, no exceptions or errors allowed!
1265
1266 res = WindowManagerImpl.ADD_OKAY;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001267
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268 final long origId = Binder.clearCallingIdentity();
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001270 if (addToken) {
1271 mTokenMap.put(attrs.token, token);
1272 mTokenList.add(token);
1273 }
1274 win.attach();
1275 mWindowMap.put(client.asBinder(), win);
1276
1277 if (attrs.type == TYPE_APPLICATION_STARTING &&
1278 token.appWindowToken != null) {
1279 token.appWindowToken.startingWindow = win;
1280 }
1281
1282 boolean imMayMove = true;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 if (attrs.type == TYPE_INPUT_METHOD) {
1285 mInputMethodWindow = win;
1286 addInputMethodWindowToListLocked(win);
1287 imMayMove = false;
1288 } else if (attrs.type == TYPE_INPUT_METHOD_DIALOG) {
1289 mInputMethodDialogs.add(win);
1290 addWindowToListInOrderLocked(win, true);
1291 adjustInputMethodDialogsLocked();
1292 imMayMove = false;
1293 } else {
1294 addWindowToListInOrderLocked(win, true);
1295 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001297 win.mEnterAnimationPending = true;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001299 mPolicy.getContentInsetHintLw(attrs, outContentInsets);
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001300
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001301 if (mInTouchMode) {
1302 res |= WindowManagerImpl.ADD_FLAG_IN_TOUCH_MODE;
1303 }
1304 if (win == null || win.mAppToken == null || !win.mAppToken.clientHidden) {
1305 res |= WindowManagerImpl.ADD_FLAG_APP_VISIBLE;
1306 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001307
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001308 boolean focusChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 if (win.canReceiveKeys()) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001310 if ((focusChanged=updateFocusedWindowLocked(UPDATE_FOCUS_WILL_ASSIGN_LAYERS))
1311 == true) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001312 imMayMove = false;
1313 }
1314 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316 if (imMayMove) {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001317 moveInputMethodWindowsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001318 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 assignLayersLocked();
1321 // Don't do layout here, the window must call
1322 // relayout to be displayed, so we'll do it there.
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001324 //dump();
1325
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001326 if (focusChanged) {
1327 if (mCurrentFocus != null) {
1328 mKeyWaiter.handleNewWindowLocked(mCurrentFocus);
1329 }
1330 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001331 if (localLOGV) Log.v(
1332 TAG, "New client " + client.asBinder()
1333 + ": window=" + win);
1334 }
1335
1336 // sendNewConfiguration() checks caller permissions so we must call it with
1337 // privilege. updateOrientationFromAppTokens() clears and resets the caller
1338 // identity anyway, so it's safe to just clear & restore around this whole
1339 // block.
1340 final long origId = Binder.clearCallingIdentity();
1341 if (reportNewConfig) {
1342 sendNewConfiguration();
1343 } else {
1344 // Update Orientation after adding a window, only if the window needs to be
1345 // displayed right away
1346 if (win.isVisibleOrAdding()) {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001347 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001348 sendNewConfiguration();
1349 }
1350 }
1351 }
1352 Binder.restoreCallingIdentity(origId);
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001354 return res;
1355 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001357 public void removeWindow(Session session, IWindow client) {
1358 synchronized(mWindowMap) {
1359 WindowState win = windowForClientLocked(session, client);
1360 if (win == null) {
1361 return;
1362 }
1363 removeWindowLocked(session, win);
1364 }
1365 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001367 public void removeWindowLocked(Session session, WindowState win) {
1368
1369 if (localLOGV || DEBUG_FOCUS) Log.v(
1370 TAG, "Remove " + win + " client="
1371 + Integer.toHexString(System.identityHashCode(
1372 win.mClient.asBinder()))
1373 + ", surface=" + win.mSurface);
1374
1375 final long origId = Binder.clearCallingIdentity();
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001377 if (DEBUG_APP_TRANSITIONS) Log.v(
1378 TAG, "Remove " + win + ": mSurface=" + win.mSurface
1379 + " mExiting=" + win.mExiting
1380 + " isAnimating=" + win.isAnimating()
1381 + " app-animation="
1382 + (win.mAppToken != null ? win.mAppToken.animation : null)
1383 + " inPendingTransaction="
1384 + (win.mAppToken != null ? win.mAppToken.inPendingTransaction : false)
1385 + " mDisplayFrozen=" + mDisplayFrozen);
1386 // Visibility of the removed window. Will be used later to update orientation later on.
1387 boolean wasVisible = false;
1388 // First, see if we need to run an animation. If we do, we have
1389 // to hold off on removing the window until the animation is done.
1390 // If the display is frozen, just remove immediately, since the
1391 // animation wouldn't be seen.
1392 if (win.mSurface != null && !mDisplayFrozen) {
1393 // If we are not currently running the exit animation, we
1394 // need to see about starting one.
1395 if (wasVisible=win.isWinVisibleLw()) {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001397 int transit = WindowManagerPolicy.TRANSIT_EXIT;
1398 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
1399 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
1400 }
1401 // Try starting an animation.
1402 if (applyAnimationLocked(win, transit, false)) {
1403 win.mExiting = true;
1404 }
1405 }
1406 if (win.mExiting || win.isAnimating()) {
1407 // The exit animation is running... wait for it!
1408 //Log.i(TAG, "*** Running exit animation...");
1409 win.mExiting = true;
1410 win.mRemoveOnExit = true;
1411 mLayoutNeeded = true;
1412 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
1413 performLayoutAndPlaceSurfacesLocked();
1414 if (win.mAppToken != null) {
1415 win.mAppToken.updateReportedVisibilityLocked();
1416 }
1417 //dump();
1418 Binder.restoreCallingIdentity(origId);
1419 return;
1420 }
1421 }
1422
1423 removeWindowInnerLocked(session, win);
1424 // Removing a visible window will effect the computed orientation
1425 // So just update orientation if needed.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001426 if (wasVisible && computeForcedAppOrientationLocked()
1427 != mForcedAppOrientation) {
1428 mH.sendMessage(mH.obtainMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001429 }
1430 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
1431 Binder.restoreCallingIdentity(origId);
1432 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001434 private void removeWindowInnerLocked(Session session, WindowState win) {
1435 mKeyWaiter.releasePendingPointerLocked(win.mSession);
1436 mKeyWaiter.releasePendingTrackballLocked(win.mSession);
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001438 win.mRemoved = true;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001440 if (mInputMethodTarget == win) {
1441 moveInputMethodWindowsIfNeededLocked(false);
1442 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001444 mPolicy.removeWindowLw(win);
1445 win.removeLocked();
1446
1447 mWindowMap.remove(win.mClient.asBinder());
1448 mWindows.remove(win);
1449
1450 if (mInputMethodWindow == win) {
1451 mInputMethodWindow = null;
1452 } else if (win.mAttrs.type == TYPE_INPUT_METHOD_DIALOG) {
1453 mInputMethodDialogs.remove(win);
1454 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001456 final WindowToken token = win.mToken;
1457 final AppWindowToken atoken = win.mAppToken;
1458 token.windows.remove(win);
1459 if (atoken != null) {
1460 atoken.allAppWindows.remove(win);
1461 }
1462 if (localLOGV) Log.v(
1463 TAG, "**** Removing window " + win + ": count="
1464 + token.windows.size());
1465 if (token.windows.size() == 0) {
1466 if (!token.explicit) {
1467 mTokenMap.remove(token.token);
1468 mTokenList.remove(token);
1469 } else if (atoken != null) {
1470 atoken.firstWindowDrawn = false;
1471 }
1472 }
1473
1474 if (atoken != null) {
1475 if (atoken.startingWindow == win) {
1476 atoken.startingWindow = null;
1477 } else if (atoken.allAppWindows.size() == 0 && atoken.startingData != null) {
1478 // If this is the last window and we had requested a starting
1479 // transition window, well there is no point now.
1480 atoken.startingData = null;
1481 } else if (atoken.allAppWindows.size() == 1 && atoken.startingView != null) {
1482 // If this is the last window except for a starting transition
1483 // window, we need to get rid of the starting transition.
1484 if (DEBUG_STARTING_WINDOW) {
1485 Log.v(TAG, "Schedule remove starting " + token
1486 + ": no more real windows");
1487 }
1488 Message m = mH.obtainMessage(H.REMOVE_STARTING, atoken);
1489 mH.sendMessage(m);
1490 }
1491 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001492
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001493 if (!mInLayout) {
1494 assignLayersLocked();
1495 mLayoutNeeded = true;
1496 performLayoutAndPlaceSurfacesLocked();
1497 if (win.mAppToken != null) {
1498 win.mAppToken.updateReportedVisibilityLocked();
1499 }
1500 }
1501 }
1502
1503 private void setTransparentRegionWindow(Session session, IWindow client, Region region) {
1504 long origId = Binder.clearCallingIdentity();
1505 try {
1506 synchronized (mWindowMap) {
1507 WindowState w = windowForClientLocked(session, client);
1508 if ((w != null) && (w.mSurface != null)) {
1509 Surface.openTransaction();
1510 try {
1511 w.mSurface.setTransparentRegionHint(region);
1512 } finally {
1513 Surface.closeTransaction();
1514 }
1515 }
1516 }
1517 } finally {
1518 Binder.restoreCallingIdentity(origId);
1519 }
1520 }
1521
1522 void setInsetsWindow(Session session, IWindow client,
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001523 int touchableInsets, Rect contentInsets,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001524 Rect visibleInsets) {
1525 long origId = Binder.clearCallingIdentity();
1526 try {
1527 synchronized (mWindowMap) {
1528 WindowState w = windowForClientLocked(session, client);
1529 if (w != null) {
1530 w.mGivenInsetsPending = false;
1531 w.mGivenContentInsets.set(contentInsets);
1532 w.mGivenVisibleInsets.set(visibleInsets);
1533 w.mTouchableInsets = touchableInsets;
1534 mLayoutNeeded = true;
1535 performLayoutAndPlaceSurfacesLocked();
1536 }
1537 }
1538 } finally {
1539 Binder.restoreCallingIdentity(origId);
1540 }
1541 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001543 public void getWindowDisplayFrame(Session session, IWindow client,
1544 Rect outDisplayFrame) {
1545 synchronized(mWindowMap) {
1546 WindowState win = windowForClientLocked(session, client);
1547 if (win == null) {
1548 outDisplayFrame.setEmpty();
1549 return;
1550 }
1551 outDisplayFrame.set(win.mDisplayFrame);
1552 }
1553 }
1554
1555 public int relayoutWindow(Session session, IWindow client,
1556 WindowManager.LayoutParams attrs, int requestedWidth,
1557 int requestedHeight, int viewVisibility, boolean insetsPending,
1558 Rect outFrame, Rect outContentInsets, Rect outVisibleInsets,
1559 Surface outSurface) {
1560 boolean displayed = false;
1561 boolean inTouchMode;
1562 Configuration newConfig = null;
1563 long origId = Binder.clearCallingIdentity();
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001565 synchronized(mWindowMap) {
1566 WindowState win = windowForClientLocked(session, client);
1567 if (win == null) {
1568 return 0;
1569 }
1570 win.mRequestedWidth = requestedWidth;
1571 win.mRequestedHeight = requestedHeight;
1572
1573 if (attrs != null) {
1574 mPolicy.adjustWindowParamsLw(attrs);
1575 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001577 int attrChanges = 0;
1578 int flagChanges = 0;
1579 if (attrs != null) {
1580 flagChanges = win.mAttrs.flags ^= attrs.flags;
1581 attrChanges = win.mAttrs.copyFrom(attrs);
1582 }
1583
1584 if (localLOGV) Log.v(
1585 TAG, "Relayout given client " + client.asBinder()
1586 + " (" + win.mAttrs.getTitle() + ")");
1587
1588
1589 if ((attrChanges & WindowManager.LayoutParams.ALPHA_CHANGED) != 0) {
1590 win.mAlpha = attrs.alpha;
1591 }
1592
1593 final boolean scaledWindow =
1594 ((win.mAttrs.flags & WindowManager.LayoutParams.FLAG_SCALED) != 0);
1595
1596 if (scaledWindow) {
1597 // requested{Width|Height} Surface's physical size
1598 // attrs.{width|height} Size on screen
1599 win.mHScale = (attrs.width != requestedWidth) ?
1600 (attrs.width / (float)requestedWidth) : 1.0f;
1601 win.mVScale = (attrs.height != requestedHeight) ?
1602 (attrs.height / (float)requestedHeight) : 1.0f;
1603 }
1604
1605 boolean imMayMove = (flagChanges&(
1606 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
1607 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)) != 0;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001608
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001609 boolean focusMayChange = win.mViewVisibility != viewVisibility
1610 || ((flagChanges&WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) != 0)
1611 || (!win.mRelayoutCalled);
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001612
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001613 win.mRelayoutCalled = true;
1614 final int oldVisibility = win.mViewVisibility;
1615 win.mViewVisibility = viewVisibility;
1616 if (viewVisibility == View.VISIBLE &&
1617 (win.mAppToken == null || !win.mAppToken.clientHidden)) {
1618 displayed = !win.isVisibleLw();
1619 if (win.mExiting) {
1620 win.mExiting = false;
1621 win.mAnimation = null;
1622 }
1623 if (win.mDestroying) {
1624 win.mDestroying = false;
1625 mDestroySurface.remove(win);
1626 }
1627 if (oldVisibility == View.GONE) {
1628 win.mEnterAnimationPending = true;
1629 }
1630 if (displayed && win.mSurface != null && !win.mDrawPending
1631 && !win.mCommitDrawPending && !mDisplayFrozen) {
1632 applyEnterAnimationLocked(win);
1633 }
1634 if ((attrChanges&WindowManager.LayoutParams.FORMAT_CHANGED) != 0) {
1635 // To change the format, we need to re-build the surface.
1636 win.destroySurfaceLocked();
1637 displayed = true;
1638 }
1639 try {
1640 Surface surface = win.createSurfaceLocked();
1641 if (surface != null) {
1642 outSurface.copyFrom(surface);
1643 } else {
1644 outSurface.clear();
1645 }
1646 } catch (Exception e) {
1647 Log.w(TAG, "Exception thrown when creating surface for client "
1648 + client + " (" + win.mAttrs.getTitle() + ")",
1649 e);
1650 Binder.restoreCallingIdentity(origId);
1651 return 0;
1652 }
1653 if (displayed) {
1654 focusMayChange = true;
1655 }
1656 if (win.mAttrs.type == TYPE_INPUT_METHOD
1657 && mInputMethodWindow == null) {
1658 mInputMethodWindow = win;
1659 imMayMove = true;
1660 }
1661 } else {
1662 win.mEnterAnimationPending = false;
1663 if (win.mSurface != null) {
1664 // If we are not currently running the exit animation, we
1665 // need to see about starting one.
1666 if (!win.mExiting) {
1667 // Try starting an animation; if there isn't one, we
1668 // can destroy the surface right away.
1669 int transit = WindowManagerPolicy.TRANSIT_EXIT;
1670 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
1671 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
1672 }
1673 if (win.isWinVisibleLw() &&
1674 applyAnimationLocked(win, transit, false)) {
1675 win.mExiting = true;
1676 mKeyWaiter.finishedKey(session, client, true,
1677 KeyWaiter.RETURN_NOTHING);
1678 } else if (win.isAnimating()) {
1679 // Currently in a hide animation... turn this into
1680 // an exit.
1681 win.mExiting = true;
1682 } else {
1683 if (mInputMethodWindow == win) {
1684 mInputMethodWindow = null;
1685 }
1686 win.destroySurfaceLocked();
1687 }
1688 }
1689 }
1690 outSurface.clear();
1691 }
1692
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001693 if (focusMayChange) {
1694 //System.out.println("Focus may change: " + win.mAttrs.getTitle());
1695 if (updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001696 imMayMove = false;
1697 }
1698 //System.out.println("Relayout " + win + ": focus=" + mCurrentFocus);
1699 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001700
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001701 // updateFocusedWindowLocked() already assigned layers so we only need to
1702 // reassign them at this point if the IM window state gets shuffled
1703 boolean assignLayers = false;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001704
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001705 if (imMayMove) {
1706 if (moveInputMethodWindowsIfNeededLocked(false)) {
1707 assignLayers = true;
1708 }
1709 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001710
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001711 mLayoutNeeded = true;
1712 win.mGivenInsetsPending = insetsPending;
1713 if (assignLayers) {
1714 assignLayersLocked();
1715 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001716 newConfig = updateOrientationFromAppTokensLocked(null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001717 performLayoutAndPlaceSurfacesLocked();
1718 if (win.mAppToken != null) {
1719 win.mAppToken.updateReportedVisibilityLocked();
1720 }
1721 outFrame.set(win.mFrame);
1722 outContentInsets.set(win.mContentInsets);
1723 outVisibleInsets.set(win.mVisibleInsets);
1724 if (localLOGV) Log.v(
1725 TAG, "Relayout given client " + client.asBinder()
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001726 + ", requestedWidth=" + requestedWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001727 + ", requestedHeight=" + requestedHeight
1728 + ", viewVisibility=" + viewVisibility
1729 + "\nRelayout returning frame=" + outFrame
1730 + ", surface=" + outSurface);
1731
1732 if (localLOGV || DEBUG_FOCUS) Log.v(
1733 TAG, "Relayout of " + win + ": focusMayChange=" + focusMayChange);
1734
1735 inTouchMode = mInTouchMode;
1736 }
1737
1738 if (newConfig != null) {
1739 sendNewConfiguration();
1740 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001741
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001742 Binder.restoreCallingIdentity(origId);
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001743
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001744 return (inTouchMode ? WindowManagerImpl.RELAYOUT_IN_TOUCH_MODE : 0)
1745 | (displayed ? WindowManagerImpl.RELAYOUT_FIRST_TIME : 0);
1746 }
1747
1748 public void finishDrawingWindow(Session session, IWindow client) {
1749 final long origId = Binder.clearCallingIdentity();
1750 synchronized(mWindowMap) {
1751 WindowState win = windowForClientLocked(session, client);
1752 if (win != null && win.finishDrawingLocked()) {
1753 mLayoutNeeded = true;
1754 performLayoutAndPlaceSurfacesLocked();
1755 }
1756 }
1757 Binder.restoreCallingIdentity(origId);
1758 }
1759
1760 private AttributeCache.Entry getCachedAnimations(WindowManager.LayoutParams lp) {
1761 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: params package="
1762 + (lp != null ? lp.packageName : null)
1763 + " resId=0x" + (lp != null ? Integer.toHexString(lp.windowAnimations) : null));
1764 if (lp != null && lp.windowAnimations != 0) {
1765 // If this is a system resource, don't try to load it from the
1766 // application resources. It is nice to avoid loading application
1767 // resources if we can.
1768 String packageName = lp.packageName != null ? lp.packageName : "android";
1769 int resId = lp.windowAnimations;
1770 if ((resId&0xFF000000) == 0x01000000) {
1771 packageName = "android";
1772 }
1773 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: picked package="
1774 + packageName);
1775 return AttributeCache.instance().get(packageName, resId,
1776 com.android.internal.R.styleable.WindowAnimation);
1777 }
1778 return null;
1779 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001781 private void applyEnterAnimationLocked(WindowState win) {
1782 int transit = WindowManagerPolicy.TRANSIT_SHOW;
1783 if (win.mEnterAnimationPending) {
1784 win.mEnterAnimationPending = false;
1785 transit = WindowManagerPolicy.TRANSIT_ENTER;
1786 }
1787
1788 applyAnimationLocked(win, transit, true);
1789 }
1790
1791 private boolean applyAnimationLocked(WindowState win,
1792 int transit, boolean isEntrance) {
1793 if (win.mLocalAnimating && win.mAnimationIsEntrance == isEntrance) {
1794 // If we are trying to apply an animation, but already running
1795 // an animation of the same type, then just leave that one alone.
1796 return true;
1797 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001798
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001799 // Only apply an animation if the display isn't frozen. If it is
1800 // frozen, there is no reason to animate and it can cause strange
1801 // artifacts when we unfreeze the display if some different animation
1802 // is running.
1803 if (!mDisplayFrozen) {
1804 int anim = mPolicy.selectAnimationLw(win, transit);
1805 int attr = -1;
1806 Animation a = null;
1807 if (anim != 0) {
1808 a = AnimationUtils.loadAnimation(mContext, anim);
1809 } else {
1810 switch (transit) {
1811 case WindowManagerPolicy.TRANSIT_ENTER:
1812 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
1813 break;
1814 case WindowManagerPolicy.TRANSIT_EXIT:
1815 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
1816 break;
1817 case WindowManagerPolicy.TRANSIT_SHOW:
1818 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
1819 break;
1820 case WindowManagerPolicy.TRANSIT_HIDE:
1821 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
1822 break;
1823 }
1824 if (attr >= 0) {
1825 a = loadAnimation(win.mAttrs, attr);
1826 }
1827 }
1828 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: win=" + win
1829 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
1830 + " mAnimation=" + win.mAnimation
1831 + " isEntrance=" + isEntrance);
1832 if (a != null) {
1833 if (DEBUG_ANIM) {
1834 RuntimeException e = new RuntimeException();
1835 e.fillInStackTrace();
1836 Log.v(TAG, "Loaded animation " + a + " for " + win, e);
1837 }
1838 win.setAnimation(a);
1839 win.mAnimationIsEntrance = isEntrance;
1840 }
1841 } else {
1842 win.clearAnimation();
1843 }
1844
1845 return win.mAnimation != null;
1846 }
1847
1848 private Animation loadAnimation(WindowManager.LayoutParams lp, int animAttr) {
1849 int anim = 0;
1850 Context context = mContext;
1851 if (animAttr >= 0) {
1852 AttributeCache.Entry ent = getCachedAnimations(lp);
1853 if (ent != null) {
1854 context = ent.context;
1855 anim = ent.array.getResourceId(animAttr, 0);
1856 }
1857 }
1858 if (anim != 0) {
1859 return AnimationUtils.loadAnimation(context, anim);
1860 }
1861 return null;
1862 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001864 private boolean applyAnimationLocked(AppWindowToken wtoken,
1865 WindowManager.LayoutParams lp, int transit, boolean enter) {
1866 // Only apply an animation if the display isn't frozen. If it is
1867 // frozen, there is no reason to animate and it can cause strange
1868 // artifacts when we unfreeze the display if some different animation
1869 // is running.
1870 if (!mDisplayFrozen) {
Jean-Baptiste Querua8675f62009-07-29 14:25:07 -07001871 Animation a;
1872 if (lp != null && (lp.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
1873 a = new FadeInOutAnimation(enter);
1874 if (DEBUG_ANIM) Log.v(TAG,
1875 "applying FadeInOutAnimation for a window in compatibility mode");
1876 } else {
1877 int animAttr = 0;
1878 switch (transit) {
1879 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
1880 animAttr = enter
1881 ? com.android.internal.R.styleable.WindowAnimation_activityOpenEnterAnimation
1882 : com.android.internal.R.styleable.WindowAnimation_activityOpenExitAnimation;
1883 break;
1884 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
1885 animAttr = enter
1886 ? com.android.internal.R.styleable.WindowAnimation_activityCloseEnterAnimation
1887 : com.android.internal.R.styleable.WindowAnimation_activityCloseExitAnimation;
1888 break;
1889 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
1890 animAttr = enter
1891 ? com.android.internal.R.styleable.WindowAnimation_taskOpenEnterAnimation
1892 : com.android.internal.R.styleable.WindowAnimation_taskOpenExitAnimation;
1893 break;
1894 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
1895 animAttr = enter
1896 ? com.android.internal.R.styleable.WindowAnimation_taskCloseEnterAnimation
1897 : com.android.internal.R.styleable.WindowAnimation_taskCloseExitAnimation;
1898 break;
1899 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
1900 animAttr = enter
1901 ? com.android.internal.R.styleable.WindowAnimation_taskToFrontEnterAnimation
1902 : com.android.internal.R.styleable.WindowAnimation_taskToFrontExitAnimation;
1903 break;
1904 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
1905 animAttr = enter
1906 ? com.android.internal.R.styleable.WindowAnimation_taskToBackEnterAnimation
1907 : com.android.internal.R.styleable.WindowAnimation_taskToBackExitAnimation;
1908 break;
1909 }
1910 a = loadAnimation(lp, animAttr);
1911 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: wtoken=" + wtoken
1912 + " anim=" + a
1913 + " animAttr=0x" + Integer.toHexString(animAttr)
1914 + " transit=" + transit);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001915 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001916 if (a != null) {
1917 if (DEBUG_ANIM) {
1918 RuntimeException e = new RuntimeException();
1919 e.fillInStackTrace();
1920 Log.v(TAG, "Loaded animation " + a + " for " + wtoken, e);
1921 }
1922 wtoken.setAnimation(a);
1923 }
1924 } else {
1925 wtoken.clearAnimation();
1926 }
1927
1928 return wtoken.animation != null;
1929 }
1930
1931 // -------------------------------------------------------------
1932 // Application Window Tokens
1933 // -------------------------------------------------------------
1934
1935 public void validateAppTokens(List tokens) {
1936 int v = tokens.size()-1;
1937 int m = mAppTokens.size()-1;
1938 while (v >= 0 && m >= 0) {
1939 AppWindowToken wtoken = mAppTokens.get(m);
1940 if (wtoken.removed) {
1941 m--;
1942 continue;
1943 }
1944 if (tokens.get(v) != wtoken.token) {
1945 Log.w(TAG, "Tokens out of sync: external is " + tokens.get(v)
1946 + " @ " + v + ", internal is " + wtoken.token + " @ " + m);
1947 }
1948 v--;
1949 m--;
1950 }
1951 while (v >= 0) {
1952 Log.w(TAG, "External token not found: " + tokens.get(v) + " @ " + v);
1953 v--;
1954 }
1955 while (m >= 0) {
1956 AppWindowToken wtoken = mAppTokens.get(m);
1957 if (!wtoken.removed) {
1958 Log.w(TAG, "Invalid internal token: " + wtoken.token + " @ " + m);
1959 }
1960 m--;
1961 }
1962 }
1963
1964 boolean checkCallingPermission(String permission, String func) {
1965 // Quick check: if the calling permission is me, it's all okay.
1966 if (Binder.getCallingPid() == Process.myPid()) {
1967 return true;
1968 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001969
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001970 if (mContext.checkCallingPermission(permission)
1971 == PackageManager.PERMISSION_GRANTED) {
1972 return true;
1973 }
1974 String msg = "Permission Denial: " + func + " from pid="
1975 + Binder.getCallingPid()
1976 + ", uid=" + Binder.getCallingUid()
1977 + " requires " + permission;
1978 Log.w(TAG, msg);
1979 return false;
1980 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001981
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001982 AppWindowToken findAppWindowToken(IBinder token) {
1983 WindowToken wtoken = mTokenMap.get(token);
1984 if (wtoken == null) {
1985 return null;
1986 }
1987 return wtoken.appWindowToken;
1988 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001989
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001990 public void addWindowToken(IBinder token, int type) {
1991 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
1992 "addWindowToken()")) {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001993 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001994 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07001995
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001996 synchronized(mWindowMap) {
1997 WindowToken wtoken = mTokenMap.get(token);
1998 if (wtoken != null) {
1999 Log.w(TAG, "Attempted to add existing input method token: " + token);
2000 return;
2001 }
2002 wtoken = new WindowToken(token, type, true);
2003 mTokenMap.put(token, wtoken);
2004 mTokenList.add(wtoken);
2005 }
2006 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002007
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002008 public void removeWindowToken(IBinder token) {
2009 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2010 "removeWindowToken()")) {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002011 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002012 }
2013
2014 final long origId = Binder.clearCallingIdentity();
2015 synchronized(mWindowMap) {
2016 WindowToken wtoken = mTokenMap.remove(token);
2017 mTokenList.remove(wtoken);
2018 if (wtoken != null) {
2019 boolean delayed = false;
2020 if (!wtoken.hidden) {
2021 wtoken.hidden = true;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002023 final int N = wtoken.windows.size();
2024 boolean changed = false;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002025
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002026 for (int i=0; i<N; i++) {
2027 WindowState win = wtoken.windows.get(i);
2028
2029 if (win.isAnimating()) {
2030 delayed = true;
2031 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002032
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002033 if (win.isVisibleNow()) {
2034 applyAnimationLocked(win,
2035 WindowManagerPolicy.TRANSIT_EXIT, false);
2036 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2037 KeyWaiter.RETURN_NOTHING);
2038 changed = true;
2039 }
2040 }
2041
2042 if (changed) {
2043 mLayoutNeeded = true;
2044 performLayoutAndPlaceSurfacesLocked();
2045 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2046 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002048 if (delayed) {
2049 mExitingTokens.add(wtoken);
2050 }
2051 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002052
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002053 } else {
2054 Log.w(TAG, "Attempted to remove non-existing token: " + token);
2055 }
2056 }
2057 Binder.restoreCallingIdentity(origId);
2058 }
2059
2060 public void addAppToken(int addPos, IApplicationToken token,
2061 int groupId, int requestedOrientation, boolean fullscreen) {
2062 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2063 "addAppToken()")) {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002064 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002065 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002067 synchronized(mWindowMap) {
2068 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2069 if (wtoken != null) {
2070 Log.w(TAG, "Attempted to add existing app token: " + token);
2071 return;
2072 }
2073 wtoken = new AppWindowToken(token);
2074 wtoken.groupId = groupId;
2075 wtoken.appFullscreen = fullscreen;
2076 wtoken.requestedOrientation = requestedOrientation;
2077 mAppTokens.add(addPos, wtoken);
Jean-Baptiste Queru843ef362009-05-20 11:28:04 -07002078 if (localLOGV) Log.v(TAG, "Adding new app token: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002079 mTokenMap.put(token.asBinder(), wtoken);
2080 mTokenList.add(wtoken);
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002082 // Application tokens start out hidden.
2083 wtoken.hidden = true;
2084 wtoken.hiddenRequested = true;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002086 //dump();
2087 }
2088 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002090 public void setAppGroupId(IBinder token, int groupId) {
2091 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2092 "setAppStartingIcon()")) {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002093 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002094 }
2095
2096 synchronized(mWindowMap) {
2097 AppWindowToken wtoken = findAppWindowToken(token);
2098 if (wtoken == null) {
2099 Log.w(TAG, "Attempted to set group id of non-existing app token: " + token);
2100 return;
2101 }
2102 wtoken.groupId = groupId;
2103 }
2104 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002106 public int getOrientationFromWindowsLocked() {
2107 int pos = mWindows.size() - 1;
2108 while (pos >= 0) {
2109 WindowState wtoken = (WindowState) mWindows.get(pos);
2110 pos--;
2111 if (wtoken.mAppToken != null) {
2112 // We hit an application window. so the orientation will be determined by the
2113 // app window. No point in continuing further.
2114 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2115 }
2116 if (!wtoken.isVisibleLw()) {
2117 continue;
2118 }
2119 int req = wtoken.mAttrs.screenOrientation;
2120 if((req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) ||
2121 (req == ActivityInfo.SCREEN_ORIENTATION_BEHIND)){
2122 continue;
2123 } else {
2124 return req;
2125 }
2126 }
2127 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2128 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002130 public int getOrientationFromAppTokensLocked() {
2131 int pos = mAppTokens.size() - 1;
2132 int curGroup = 0;
2133 int lastOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Jean-Baptiste Queru843ef362009-05-20 11:28:04 -07002134 boolean findingBehind = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002135 boolean haveGroup = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002136 boolean lastFullscreen = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002137 while (pos >= 0) {
2138 AppWindowToken wtoken = mAppTokens.get(pos);
2139 pos--;
Jean-Baptiste Queru843ef362009-05-20 11:28:04 -07002140 // if we're about to tear down this window and not seek for
2141 // the behind activity, don't use it for orientation
2142 if (!findingBehind
2143 && (!wtoken.hidden && wtoken.hiddenRequested)) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002144 continue;
2145 }
2146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002147 if (!haveGroup) {
2148 // We ignore any hidden applications on the top.
2149 if (wtoken.hiddenRequested || wtoken.willBeHidden) {
2150 continue;
2151 }
2152 haveGroup = true;
2153 curGroup = wtoken.groupId;
2154 lastOrientation = wtoken.requestedOrientation;
2155 } else if (curGroup != wtoken.groupId) {
2156 // If we have hit a new application group, and the bottom
2157 // of the previous group didn't explicitly say to use
The Android Open Source Project4df24232009-03-05 14:34:35 -08002158 // the orientation behind it, and the last app was
2159 // full screen, then we'll stick with the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002160 // user's orientation.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002161 if (lastOrientation != ActivityInfo.SCREEN_ORIENTATION_BEHIND
2162 && lastFullscreen) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002163 return lastOrientation;
2164 }
2165 }
2166 int or = wtoken.requestedOrientation;
Jean-Baptiste Queru843ef362009-05-20 11:28:04 -07002167 // If this application is fullscreen, and didn't explicitly say
2168 // to use the orientation behind it, then just take whatever
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002169 // orientation it has and ignores whatever is under it.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002170 lastFullscreen = wtoken.appFullscreen;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002171 if (lastFullscreen
Jean-Baptiste Queru843ef362009-05-20 11:28:04 -07002172 && or != ActivityInfo.SCREEN_ORIENTATION_BEHIND) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002173 return or;
2174 }
2175 // If this application has requested an explicit orientation,
2176 // then use it.
2177 if (or == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE ||
2178 or == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ||
2179 or == ActivityInfo.SCREEN_ORIENTATION_SENSOR ||
2180 or == ActivityInfo.SCREEN_ORIENTATION_NOSENSOR ||
2181 or == ActivityInfo.SCREEN_ORIENTATION_USER) {
2182 return or;
2183 }
Jean-Baptiste Queru843ef362009-05-20 11:28:04 -07002184 findingBehind |= (or == ActivityInfo.SCREEN_ORIENTATION_BEHIND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002185 }
2186 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2187 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002189 public Configuration updateOrientationFromAppTokens(
The Android Open Source Project10592532009-03-18 17:39:46 -07002190 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002191 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2192 "updateOrientationFromAppTokens()")) {
2193 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
2194 }
2195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002196 Configuration config;
2197 long ident = Binder.clearCallingIdentity();
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002198 config = updateOrientationFromAppTokensUnchecked(currentConfig,
2199 freezeThisOneIfNeeded);
2200 Binder.restoreCallingIdentity(ident);
2201 return config;
2202 }
2203
2204 Configuration updateOrientationFromAppTokensUnchecked(
2205 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
2206 Configuration config;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002207 synchronized(mWindowMap) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002208 config = updateOrientationFromAppTokensLocked(currentConfig, freezeThisOneIfNeeded);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002209 }
2210 if (config != null) {
2211 mLayoutNeeded = true;
2212 performLayoutAndPlaceSurfacesLocked();
2213 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002214 return config;
2215 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002217 /*
2218 * The orientation is computed from non-application windows first. If none of
2219 * the non-application windows specify orientation, the orientation is computed from
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002220 * application tokens.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002221 * @see android.view.IWindowManager#updateOrientationFromAppTokens(
2222 * android.os.IBinder)
2223 */
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002224 Configuration updateOrientationFromAppTokensLocked(
The Android Open Source Project10592532009-03-18 17:39:46 -07002225 Configuration appConfig, IBinder freezeThisOneIfNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002226 boolean changed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002227 long ident = Binder.clearCallingIdentity();
2228 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002229 int req = computeForcedAppOrientationLocked();
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002231 if (req != mForcedAppOrientation) {
2232 changed = true;
2233 mForcedAppOrientation = req;
2234 //send a message to Policy indicating orientation change to take
2235 //action like disabling/enabling sensors etc.,
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002236 mPolicy.setCurrentOrientationLw(req);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002237 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002239 if (changed) {
2240 changed = setRotationUncheckedLocked(
Dianne Hackborncc956672009-03-26 00:04:52 -07002241 WindowManagerPolicy.USE_LAST_ROTATION,
2242 mLastRotationFlags & (~Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002243 if (changed) {
2244 if (freezeThisOneIfNeeded != null) {
2245 AppWindowToken wtoken = findAppWindowToken(
2246 freezeThisOneIfNeeded);
2247 if (wtoken != null) {
2248 startAppFreezingScreenLocked(wtoken,
2249 ActivityInfo.CONFIG_ORIENTATION);
2250 }
2251 }
Dianne Hackborn9d4de632009-03-24 19:37:25 -07002252 return computeNewConfigurationLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002253 }
2254 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002255
2256 // No obvious action we need to take, but if our current
2257 // state mismatches the activity maanager's, update it
2258 if (appConfig != null) {
Dianne Hackborn9d4de632009-03-24 19:37:25 -07002259 mTempConfiguration.setToDefaults();
2260 if (computeNewConfigurationLocked(mTempConfiguration)) {
2261 if (appConfig.diff(mTempConfiguration) != 0) {
2262 Log.i(TAG, "Config changed: " + mTempConfiguration);
2263 return new Configuration(mTempConfiguration);
2264 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002265 }
2266 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002267 } finally {
2268 Binder.restoreCallingIdentity(ident);
2269 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002271 return null;
2272 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002273
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002274 int computeForcedAppOrientationLocked() {
2275 int req = getOrientationFromWindowsLocked();
2276 if (req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
2277 req = getOrientationFromAppTokensLocked();
2278 }
2279 return req;
2280 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002282 public void setAppOrientation(IApplicationToken token, int requestedOrientation) {
2283 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2284 "setAppOrientation()")) {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002285 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002286 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002288 synchronized(mWindowMap) {
2289 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2290 if (wtoken == null) {
2291 Log.w(TAG, "Attempted to set orientation of non-existing app token: " + token);
2292 return;
2293 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002295 wtoken.requestedOrientation = requestedOrientation;
2296 }
2297 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002299 public int getAppOrientation(IApplicationToken token) {
2300 synchronized(mWindowMap) {
2301 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2302 if (wtoken == null) {
2303 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2304 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002306 return wtoken.requestedOrientation;
2307 }
2308 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002310 public void setFocusedApp(IBinder token, boolean moveFocusNow) {
2311 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2312 "setFocusedApp()")) {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002313 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002314 }
2315
2316 synchronized(mWindowMap) {
2317 boolean changed = false;
2318 if (token == null) {
2319 if (DEBUG_FOCUS) Log.v(TAG, "Clearing focused app, was " + mFocusedApp);
2320 changed = mFocusedApp != null;
2321 mFocusedApp = null;
2322 mKeyWaiter.tickle();
2323 } else {
2324 AppWindowToken newFocus = findAppWindowToken(token);
2325 if (newFocus == null) {
2326 Log.w(TAG, "Attempted to set focus to non-existing app token: " + token);
2327 return;
2328 }
2329 changed = mFocusedApp != newFocus;
2330 mFocusedApp = newFocus;
2331 if (DEBUG_FOCUS) Log.v(TAG, "Set focused app to: " + mFocusedApp);
2332 mKeyWaiter.tickle();
2333 }
2334
2335 if (moveFocusNow && changed) {
2336 final long origId = Binder.clearCallingIdentity();
2337 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2338 Binder.restoreCallingIdentity(origId);
2339 }
2340 }
2341 }
2342
2343 public void prepareAppTransition(int transit) {
2344 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2345 "prepareAppTransition()")) {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002346 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002347 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002349 synchronized(mWindowMap) {
2350 if (DEBUG_APP_TRANSITIONS) Log.v(
2351 TAG, "Prepare app transition: transit=" + transit
2352 + " mNextAppTransition=" + mNextAppTransition);
2353 if (!mDisplayFrozen) {
2354 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
2355 mNextAppTransition = transit;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002356 } else if (transit == WindowManagerPolicy.TRANSIT_TASK_OPEN
2357 && mNextAppTransition == WindowManagerPolicy.TRANSIT_TASK_CLOSE) {
2358 // Opening a new task always supersedes a close for the anim.
2359 mNextAppTransition = transit;
2360 } else if (transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN
2361 && mNextAppTransition == WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE) {
2362 // Opening a new activity always supersedes a close for the anim.
2363 mNextAppTransition = transit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002364 }
2365 mAppTransitionReady = false;
2366 mAppTransitionTimeout = false;
2367 mStartingIconInTransition = false;
2368 mSkipAppTransitionAnimation = false;
2369 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
2370 mH.sendMessageDelayed(mH.obtainMessage(H.APP_TRANSITION_TIMEOUT),
2371 5000);
2372 }
2373 }
2374 }
2375
2376 public int getPendingAppTransition() {
2377 return mNextAppTransition;
2378 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002379
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002380 public void executeAppTransition() {
2381 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2382 "executeAppTransition()")) {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002383 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002384 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002386 synchronized(mWindowMap) {
2387 if (DEBUG_APP_TRANSITIONS) Log.v(
2388 TAG, "Execute app transition: mNextAppTransition=" + mNextAppTransition);
2389 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
2390 mAppTransitionReady = true;
2391 final long origId = Binder.clearCallingIdentity();
2392 performLayoutAndPlaceSurfacesLocked();
2393 Binder.restoreCallingIdentity(origId);
2394 }
2395 }
2396 }
2397
2398 public void setAppStartingWindow(IBinder token, String pkg,
2399 int theme, CharSequence nonLocalizedLabel, int labelRes, int icon,
2400 IBinder transferFrom, boolean createIfNeeded) {
2401 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2402 "setAppStartingIcon()")) {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002403 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002404 }
2405
2406 synchronized(mWindowMap) {
2407 if (DEBUG_STARTING_WINDOW) Log.v(
2408 TAG, "setAppStartingIcon: token=" + token + " pkg=" + pkg
2409 + " transferFrom=" + transferFrom);
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002411 AppWindowToken wtoken = findAppWindowToken(token);
2412 if (wtoken == null) {
2413 Log.w(TAG, "Attempted to set icon of non-existing app token: " + token);
2414 return;
2415 }
2416
2417 // If the display is frozen, we won't do anything until the
2418 // actual window is displayed so there is no reason to put in
2419 // the starting window.
2420 if (mDisplayFrozen) {
2421 return;
2422 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002423
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002424 if (wtoken.startingData != null) {
2425 return;
2426 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002428 if (transferFrom != null) {
2429 AppWindowToken ttoken = findAppWindowToken(transferFrom);
2430 if (ttoken != null) {
2431 WindowState startingWindow = ttoken.startingWindow;
2432 if (startingWindow != null) {
2433 if (mStartingIconInTransition) {
2434 // In this case, the starting icon has already
2435 // been displayed, so start letting windows get
2436 // shown immediately without any more transitions.
2437 mSkipAppTransitionAnimation = true;
2438 }
2439 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
2440 "Moving existing starting from " + ttoken
2441 + " to " + wtoken);
2442 final long origId = Binder.clearCallingIdentity();
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002444 // Transfer the starting window over to the new
2445 // token.
2446 wtoken.startingData = ttoken.startingData;
2447 wtoken.startingView = ttoken.startingView;
2448 wtoken.startingWindow = startingWindow;
2449 ttoken.startingData = null;
2450 ttoken.startingView = null;
2451 ttoken.startingWindow = null;
2452 ttoken.startingMoved = true;
2453 startingWindow.mToken = wtoken;
The Android Open Source Projectc2ad2412009-03-19 23:08:54 -07002454 startingWindow.mRootToken = wtoken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002455 startingWindow.mAppToken = wtoken;
2456 mWindows.remove(startingWindow);
2457 ttoken.windows.remove(startingWindow);
2458 ttoken.allAppWindows.remove(startingWindow);
2459 addWindowToListInOrderLocked(startingWindow, true);
2460 wtoken.allAppWindows.add(startingWindow);
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002462 // Propagate other interesting state between the
2463 // tokens. If the old token is displayed, we should
2464 // immediately force the new one to be displayed. If
2465 // it is animating, we need to move that animation to
2466 // the new one.
2467 if (ttoken.allDrawn) {
2468 wtoken.allDrawn = true;
2469 }
2470 if (ttoken.firstWindowDrawn) {
2471 wtoken.firstWindowDrawn = true;
2472 }
2473 if (!ttoken.hidden) {
2474 wtoken.hidden = false;
2475 wtoken.hiddenRequested = false;
2476 wtoken.willBeHidden = false;
2477 }
2478 if (wtoken.clientHidden != ttoken.clientHidden) {
2479 wtoken.clientHidden = ttoken.clientHidden;
2480 wtoken.sendAppVisibilityToClients();
2481 }
2482 if (ttoken.animation != null) {
2483 wtoken.animation = ttoken.animation;
2484 wtoken.animating = ttoken.animating;
2485 wtoken.animLayerAdjustment = ttoken.animLayerAdjustment;
2486 ttoken.animation = null;
2487 ttoken.animLayerAdjustment = 0;
2488 wtoken.updateLayers();
2489 ttoken.updateLayers();
2490 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002492 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002493 mLayoutNeeded = true;
2494 performLayoutAndPlaceSurfacesLocked();
2495 Binder.restoreCallingIdentity(origId);
2496 return;
2497 } else if (ttoken.startingData != null) {
2498 // The previous app was getting ready to show a
2499 // starting window, but hasn't yet done so. Steal it!
2500 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
2501 "Moving pending starting from " + ttoken
2502 + " to " + wtoken);
2503 wtoken.startingData = ttoken.startingData;
2504 ttoken.startingData = null;
2505 ttoken.startingMoved = true;
2506 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
2507 // Note: we really want to do sendMessageAtFrontOfQueue() because we
2508 // want to process the message ASAP, before any other queued
2509 // messages.
2510 mH.sendMessageAtFrontOfQueue(m);
2511 return;
2512 }
2513 }
2514 }
2515
2516 // There is no existing starting window, and the caller doesn't
2517 // want us to create one, so that's it!
2518 if (!createIfNeeded) {
2519 return;
2520 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002522 mStartingIconInTransition = true;
2523 wtoken.startingData = new StartingData(
2524 pkg, theme, nonLocalizedLabel,
2525 labelRes, icon);
2526 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
2527 // Note: we really want to do sendMessageAtFrontOfQueue() because we
2528 // want to process the message ASAP, before any other queued
2529 // messages.
2530 mH.sendMessageAtFrontOfQueue(m);
2531 }
2532 }
2533
2534 public void setAppWillBeHidden(IBinder token) {
2535 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2536 "setAppWillBeHidden()")) {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002537 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002538 }
2539
2540 AppWindowToken wtoken;
2541
2542 synchronized(mWindowMap) {
2543 wtoken = findAppWindowToken(token);
2544 if (wtoken == null) {
2545 Log.w(TAG, "Attempted to set will be hidden of non-existing app token: " + token);
2546 return;
2547 }
2548 wtoken.willBeHidden = true;
2549 }
2550 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002552 boolean setTokenVisibilityLocked(AppWindowToken wtoken, WindowManager.LayoutParams lp,
2553 boolean visible, int transit, boolean performLayout) {
2554 boolean delayed = false;
2555
2556 if (wtoken.clientHidden == visible) {
2557 wtoken.clientHidden = !visible;
2558 wtoken.sendAppVisibilityToClients();
2559 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002560
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002561 wtoken.willBeHidden = false;
2562 if (wtoken.hidden == visible) {
2563 final int N = wtoken.allAppWindows.size();
2564 boolean changed = false;
2565 if (DEBUG_APP_TRANSITIONS) Log.v(
2566 TAG, "Changing app " + wtoken + " hidden=" + wtoken.hidden
2567 + " performLayout=" + performLayout);
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002569 boolean runningAppAnimation = false;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002571 if (transit != WindowManagerPolicy.TRANSIT_NONE) {
2572 if (wtoken.animation == sDummyAnimation) {
2573 wtoken.animation = null;
2574 }
2575 applyAnimationLocked(wtoken, lp, transit, visible);
2576 changed = true;
2577 if (wtoken.animation != null) {
2578 delayed = runningAppAnimation = true;
2579 }
2580 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002581
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002582 for (int i=0; i<N; i++) {
2583 WindowState win = wtoken.allAppWindows.get(i);
2584 if (win == wtoken.startingWindow) {
2585 continue;
2586 }
2587
2588 if (win.isAnimating()) {
2589 delayed = true;
2590 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002591
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002592 //Log.i(TAG, "Window " + win + ": vis=" + win.isVisible());
2593 //win.dump(" ");
2594 if (visible) {
2595 if (!win.isVisibleNow()) {
2596 if (!runningAppAnimation) {
2597 applyAnimationLocked(win,
2598 WindowManagerPolicy.TRANSIT_ENTER, true);
2599 }
2600 changed = true;
2601 }
2602 } else if (win.isVisibleNow()) {
2603 if (!runningAppAnimation) {
2604 applyAnimationLocked(win,
2605 WindowManagerPolicy.TRANSIT_EXIT, false);
2606 }
2607 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2608 KeyWaiter.RETURN_NOTHING);
2609 changed = true;
2610 }
2611 }
2612
2613 wtoken.hidden = wtoken.hiddenRequested = !visible;
2614 if (!visible) {
2615 unsetAppFreezingScreenLocked(wtoken, true, true);
2616 } else {
2617 // If we are being set visible, and the starting window is
2618 // not yet displayed, then make sure it doesn't get displayed.
2619 WindowState swin = wtoken.startingWindow;
2620 if (swin != null && (swin.mDrawPending
2621 || swin.mCommitDrawPending)) {
2622 swin.mPolicyVisibility = false;
2623 swin.mPolicyVisibilityAfterAnim = false;
2624 }
2625 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002626
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002627 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "setTokenVisibilityLocked: " + wtoken
2628 + ": hidden=" + wtoken.hidden + " hiddenRequested="
2629 + wtoken.hiddenRequested);
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002630
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002631 if (changed && performLayout) {
2632 mLayoutNeeded = true;
2633 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002634 performLayoutAndPlaceSurfacesLocked();
2635 }
2636 }
2637
2638 if (wtoken.animation != null) {
2639 delayed = true;
2640 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002641
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002642 return delayed;
2643 }
2644
2645 public void setAppVisibility(IBinder token, boolean visible) {
2646 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2647 "setAppVisibility()")) {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002648 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002649 }
2650
2651 AppWindowToken wtoken;
2652
2653 synchronized(mWindowMap) {
2654 wtoken = findAppWindowToken(token);
2655 if (wtoken == null) {
2656 Log.w(TAG, "Attempted to set visibility of non-existing app token: " + token);
2657 return;
2658 }
2659
2660 if (DEBUG_APP_TRANSITIONS || DEBUG_ORIENTATION) {
2661 RuntimeException e = new RuntimeException();
2662 e.fillInStackTrace();
2663 Log.v(TAG, "setAppVisibility(" + token + ", " + visible
2664 + "): mNextAppTransition=" + mNextAppTransition
2665 + " hidden=" + wtoken.hidden
2666 + " hiddenRequested=" + wtoken.hiddenRequested, e);
2667 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002668
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002669 // If we are preparing an app transition, then delay changing
2670 // the visibility of this token until we execute that transition.
2671 if (!mDisplayFrozen && mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
2672 // Already in requested state, don't do anything more.
2673 if (wtoken.hiddenRequested != visible) {
2674 return;
2675 }
2676 wtoken.hiddenRequested = !visible;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002677
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002678 if (DEBUG_APP_TRANSITIONS) Log.v(
2679 TAG, "Setting dummy animation on: " + wtoken);
2680 wtoken.setDummyAnimation();
2681 mOpeningApps.remove(wtoken);
2682 mClosingApps.remove(wtoken);
2683 wtoken.inPendingTransaction = true;
2684 if (visible) {
2685 mOpeningApps.add(wtoken);
2686 wtoken.allDrawn = false;
2687 wtoken.startingDisplayed = false;
2688 wtoken.startingMoved = false;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002689
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002690 if (wtoken.clientHidden) {
2691 // In the case where we are making an app visible
2692 // but holding off for a transition, we still need
2693 // to tell the client to make its windows visible so
2694 // they get drawn. Otherwise, we will wait on
2695 // performing the transition until all windows have
2696 // been drawn, they never will be, and we are sad.
2697 wtoken.clientHidden = false;
2698 wtoken.sendAppVisibilityToClients();
2699 }
2700 } else {
2701 mClosingApps.add(wtoken);
2702 }
2703 return;
2704 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002706 final long origId = Binder.clearCallingIdentity();
2707 setTokenVisibilityLocked(wtoken, null, visible, WindowManagerPolicy.TRANSIT_NONE, true);
2708 wtoken.updateReportedVisibilityLocked();
2709 Binder.restoreCallingIdentity(origId);
2710 }
2711 }
2712
2713 void unsetAppFreezingScreenLocked(AppWindowToken wtoken,
2714 boolean unfreezeSurfaceNow, boolean force) {
2715 if (wtoken.freezingScreen) {
2716 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + wtoken
2717 + " force=" + force);
2718 final int N = wtoken.allAppWindows.size();
2719 boolean unfrozeWindows = false;
2720 for (int i=0; i<N; i++) {
2721 WindowState w = wtoken.allAppWindows.get(i);
2722 if (w.mAppFreezing) {
2723 w.mAppFreezing = false;
2724 if (w.mSurface != null && !w.mOrientationChanging) {
2725 w.mOrientationChanging = true;
2726 }
2727 unfrozeWindows = true;
2728 }
2729 }
2730 if (force || unfrozeWindows) {
2731 if (DEBUG_ORIENTATION) Log.v(TAG, "No longer freezing: " + wtoken);
2732 wtoken.freezingScreen = false;
2733 mAppsFreezingScreen--;
2734 }
2735 if (unfreezeSurfaceNow) {
2736 if (unfrozeWindows) {
2737 mLayoutNeeded = true;
2738 performLayoutAndPlaceSurfacesLocked();
2739 }
2740 if (mAppsFreezingScreen == 0 && !mWindowsFreezingScreen) {
2741 stopFreezingDisplayLocked();
2742 }
2743 }
2744 }
2745 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002746
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002747 public void startAppFreezingScreenLocked(AppWindowToken wtoken,
2748 int configChanges) {
2749 if (DEBUG_ORIENTATION) {
2750 RuntimeException e = new RuntimeException();
2751 e.fillInStackTrace();
2752 Log.i(TAG, "Set freezing of " + wtoken.appToken
2753 + ": hidden=" + wtoken.hidden + " freezing="
2754 + wtoken.freezingScreen, e);
2755 }
2756 if (!wtoken.hiddenRequested) {
2757 if (!wtoken.freezingScreen) {
2758 wtoken.freezingScreen = true;
2759 mAppsFreezingScreen++;
2760 if (mAppsFreezingScreen == 1) {
2761 startFreezingDisplayLocked();
2762 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
2763 mH.sendMessageDelayed(mH.obtainMessage(H.APP_FREEZE_TIMEOUT),
2764 5000);
2765 }
2766 }
2767 final int N = wtoken.allAppWindows.size();
2768 for (int i=0; i<N; i++) {
2769 WindowState w = wtoken.allAppWindows.get(i);
2770 w.mAppFreezing = true;
2771 }
2772 }
2773 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002775 public void startAppFreezingScreen(IBinder token, int configChanges) {
2776 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2777 "setAppFreezingScreen()")) {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002778 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002779 }
2780
2781 synchronized(mWindowMap) {
2782 if (configChanges == 0 && !mDisplayFrozen) {
2783 if (DEBUG_ORIENTATION) Log.v(TAG, "Skipping set freeze of " + token);
2784 return;
2785 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002786
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002787 AppWindowToken wtoken = findAppWindowToken(token);
2788 if (wtoken == null || wtoken.appToken == null) {
2789 Log.w(TAG, "Attempted to freeze screen with non-existing app token: " + wtoken);
2790 return;
2791 }
2792 final long origId = Binder.clearCallingIdentity();
2793 startAppFreezingScreenLocked(wtoken, configChanges);
2794 Binder.restoreCallingIdentity(origId);
2795 }
2796 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002798 public void stopAppFreezingScreen(IBinder token, boolean force) {
2799 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2800 "setAppFreezingScreen()")) {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002801 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002802 }
2803
2804 synchronized(mWindowMap) {
2805 AppWindowToken wtoken = findAppWindowToken(token);
2806 if (wtoken == null || wtoken.appToken == null) {
2807 return;
2808 }
2809 final long origId = Binder.clearCallingIdentity();
2810 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + token
2811 + ": hidden=" + wtoken.hidden + " freezing=" + wtoken.freezingScreen);
2812 unsetAppFreezingScreenLocked(wtoken, true, force);
2813 Binder.restoreCallingIdentity(origId);
2814 }
2815 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002816
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002817 public void removeAppToken(IBinder token) {
2818 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2819 "removeAppToken()")) {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002820 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002821 }
2822
2823 AppWindowToken wtoken = null;
2824 AppWindowToken startingToken = null;
2825 boolean delayed = false;
2826
2827 final long origId = Binder.clearCallingIdentity();
2828 synchronized(mWindowMap) {
2829 WindowToken basewtoken = mTokenMap.remove(token);
2830 mTokenList.remove(basewtoken);
2831 if (basewtoken != null && (wtoken=basewtoken.appWindowToken) != null) {
2832 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "Removing app token: " + wtoken);
2833 delayed = setTokenVisibilityLocked(wtoken, null, false, WindowManagerPolicy.TRANSIT_NONE, true);
2834 wtoken.inPendingTransaction = false;
2835 mOpeningApps.remove(wtoken);
2836 if (mClosingApps.contains(wtoken)) {
2837 delayed = true;
2838 } else if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
2839 mClosingApps.add(wtoken);
2840 delayed = true;
2841 }
2842 if (DEBUG_APP_TRANSITIONS) Log.v(
2843 TAG, "Removing app " + wtoken + " delayed=" + delayed
2844 + " animation=" + wtoken.animation
2845 + " animating=" + wtoken.animating);
2846 if (delayed) {
2847 // set the token aside because it has an active animation to be finished
2848 mExitingAppTokens.add(wtoken);
2849 }
2850 mAppTokens.remove(wtoken);
2851 wtoken.removed = true;
2852 if (wtoken.startingData != null) {
2853 startingToken = wtoken;
2854 }
2855 unsetAppFreezingScreenLocked(wtoken, true, true);
2856 if (mFocusedApp == wtoken) {
2857 if (DEBUG_FOCUS) Log.v(TAG, "Removing focused app token:" + wtoken);
2858 mFocusedApp = null;
2859 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2860 mKeyWaiter.tickle();
2861 }
2862 } else {
2863 Log.w(TAG, "Attempted to remove non-existing app token: " + token);
2864 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002865
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002866 if (!delayed && wtoken != null) {
2867 wtoken.updateReportedVisibilityLocked();
2868 }
2869 }
2870 Binder.restoreCallingIdentity(origId);
2871
2872 if (startingToken != null) {
2873 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Schedule remove starting "
2874 + startingToken + ": app token removed");
2875 Message m = mH.obtainMessage(H.REMOVE_STARTING, startingToken);
2876 mH.sendMessage(m);
2877 }
2878 }
2879
2880 private boolean tmpRemoveAppWindowsLocked(WindowToken token) {
2881 final int NW = token.windows.size();
2882 for (int i=0; i<NW; i++) {
2883 WindowState win = token.windows.get(i);
2884 mWindows.remove(win);
2885 int j = win.mChildWindows.size();
2886 while (j > 0) {
2887 j--;
2888 mWindows.remove(win.mChildWindows.get(j));
2889 }
2890 }
2891 return NW > 0;
2892 }
2893
2894 void dumpAppTokensLocked() {
2895 for (int i=mAppTokens.size()-1; i>=0; i--) {
2896 Log.v(TAG, " #" + i + ": " + mAppTokens.get(i).token);
2897 }
2898 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002899
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002900 void dumpWindowsLocked() {
2901 for (int i=mWindows.size()-1; i>=0; i--) {
2902 Log.v(TAG, " #" + i + ": " + mWindows.get(i));
2903 }
2904 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002906 private int findWindowOffsetLocked(int tokenPos) {
2907 final int NW = mWindows.size();
2908
2909 if (tokenPos >= mAppTokens.size()) {
2910 int i = NW;
2911 while (i > 0) {
2912 i--;
2913 WindowState win = (WindowState)mWindows.get(i);
2914 if (win.getAppToken() != null) {
2915 return i+1;
2916 }
2917 }
2918 }
2919
2920 while (tokenPos > 0) {
2921 // Find the first app token below the new position that has
2922 // a window displayed.
2923 final AppWindowToken wtoken = mAppTokens.get(tokenPos-1);
2924 if (DEBUG_REORDER) Log.v(TAG, "Looking for lower windows @ "
2925 + tokenPos + " -- " + wtoken.token);
2926 int i = wtoken.windows.size();
2927 while (i > 0) {
2928 i--;
2929 WindowState win = wtoken.windows.get(i);
2930 int j = win.mChildWindows.size();
2931 while (j > 0) {
2932 j--;
2933 WindowState cwin = (WindowState)win.mChildWindows.get(j);
2934 if (cwin.mSubLayer >= 0 ) {
2935 for (int pos=NW-1; pos>=0; pos--) {
2936 if (mWindows.get(pos) == cwin) {
2937 if (DEBUG_REORDER) Log.v(TAG,
2938 "Found child win @" + (pos+1));
2939 return pos+1;
2940 }
2941 }
2942 }
2943 }
2944 for (int pos=NW-1; pos>=0; pos--) {
2945 if (mWindows.get(pos) == win) {
2946 if (DEBUG_REORDER) Log.v(TAG, "Found win @" + (pos+1));
2947 return pos+1;
2948 }
2949 }
2950 }
2951 tokenPos--;
2952 }
2953
2954 return 0;
2955 }
2956
2957 private final int reAddWindowLocked(int index, WindowState win) {
2958 final int NCW = win.mChildWindows.size();
2959 boolean added = false;
2960 for (int j=0; j<NCW; j++) {
2961 WindowState cwin = (WindowState)win.mChildWindows.get(j);
2962 if (!added && cwin.mSubLayer >= 0) {
2963 mWindows.add(index, win);
2964 index++;
2965 added = true;
2966 }
2967 mWindows.add(index, cwin);
2968 index++;
2969 }
2970 if (!added) {
2971 mWindows.add(index, win);
2972 index++;
2973 }
2974 return index;
2975 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002976
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002977 private final int reAddAppWindowsLocked(int index, WindowToken token) {
2978 final int NW = token.windows.size();
2979 for (int i=0; i<NW; i++) {
2980 index = reAddWindowLocked(index, token.windows.get(i));
2981 }
2982 return index;
2983 }
2984
2985 public void moveAppToken(int index, IBinder token) {
2986 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2987 "moveAppToken()")) {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07002988 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002989 }
2990
2991 synchronized(mWindowMap) {
2992 if (DEBUG_REORDER) Log.v(TAG, "Initial app tokens:");
2993 if (DEBUG_REORDER) dumpAppTokensLocked();
2994 final AppWindowToken wtoken = findAppWindowToken(token);
2995 if (wtoken == null || !mAppTokens.remove(wtoken)) {
2996 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
2997 + token + " (" + wtoken + ")");
2998 return;
2999 }
3000 mAppTokens.add(index, wtoken);
3001 if (DEBUG_REORDER) Log.v(TAG, "Moved " + token + " to " + index + ":");
3002 if (DEBUG_REORDER) dumpAppTokensLocked();
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003003
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003004 final long origId = Binder.clearCallingIdentity();
3005 if (DEBUG_REORDER) Log.v(TAG, "Removing windows in " + token + ":");
3006 if (DEBUG_REORDER) dumpWindowsLocked();
3007 if (tmpRemoveAppWindowsLocked(wtoken)) {
3008 if (DEBUG_REORDER) Log.v(TAG, "Adding windows back in:");
3009 if (DEBUG_REORDER) dumpWindowsLocked();
3010 reAddAppWindowsLocked(findWindowOffsetLocked(index), wtoken);
3011 if (DEBUG_REORDER) Log.v(TAG, "Final window list:");
3012 if (DEBUG_REORDER) dumpWindowsLocked();
3013 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003014 mLayoutNeeded = true;
3015 performLayoutAndPlaceSurfacesLocked();
3016 }
3017 Binder.restoreCallingIdentity(origId);
3018 }
3019 }
3020
3021 private void removeAppTokensLocked(List<IBinder> tokens) {
3022 // XXX This should be done more efficiently!
3023 // (take advantage of the fact that both lists should be
3024 // ordered in the same way.)
3025 int N = tokens.size();
3026 for (int i=0; i<N; i++) {
3027 IBinder token = tokens.get(i);
3028 final AppWindowToken wtoken = findAppWindowToken(token);
3029 if (!mAppTokens.remove(wtoken)) {
3030 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3031 + token + " (" + wtoken + ")");
3032 i--;
3033 N--;
3034 }
3035 }
3036 }
3037
3038 private void moveAppWindowsLocked(List<IBinder> tokens, int tokenPos) {
3039 // First remove all of the windows from the list.
3040 final int N = tokens.size();
3041 int i;
3042 for (i=0; i<N; i++) {
3043 WindowToken token = mTokenMap.get(tokens.get(i));
3044 if (token != null) {
3045 tmpRemoveAppWindowsLocked(token);
3046 }
3047 }
3048
3049 // Where to start adding?
3050 int pos = findWindowOffsetLocked(tokenPos);
3051
3052 // And now add them back at the correct place.
3053 for (i=0; i<N; i++) {
3054 WindowToken token = mTokenMap.get(tokens.get(i));
3055 if (token != null) {
3056 pos = reAddAppWindowsLocked(pos, token);
3057 }
3058 }
3059
3060 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003061 mLayoutNeeded = true;
3062 performLayoutAndPlaceSurfacesLocked();
3063
3064 //dump();
3065 }
3066
3067 public void moveAppTokensToTop(List<IBinder> tokens) {
3068 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3069 "moveAppTokensToTop()")) {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003070 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003071 }
3072
3073 final long origId = Binder.clearCallingIdentity();
3074 synchronized(mWindowMap) {
3075 removeAppTokensLocked(tokens);
3076 final int N = tokens.size();
3077 for (int i=0; i<N; i++) {
3078 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3079 if (wt != null) {
3080 mAppTokens.add(wt);
3081 }
3082 }
3083 moveAppWindowsLocked(tokens, mAppTokens.size());
3084 }
3085 Binder.restoreCallingIdentity(origId);
3086 }
3087
3088 public void moveAppTokensToBottom(List<IBinder> tokens) {
3089 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3090 "moveAppTokensToBottom()")) {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003091 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003092 }
3093
3094 final long origId = Binder.clearCallingIdentity();
3095 synchronized(mWindowMap) {
3096 removeAppTokensLocked(tokens);
3097 final int N = tokens.size();
3098 int pos = 0;
3099 for (int i=0; i<N; i++) {
3100 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3101 if (wt != null) {
3102 mAppTokens.add(pos, wt);
3103 pos++;
3104 }
3105 }
3106 moveAppWindowsLocked(tokens, 0);
3107 }
3108 Binder.restoreCallingIdentity(origId);
3109 }
3110
3111 // -------------------------------------------------------------
3112 // Misc IWindowSession methods
3113 // -------------------------------------------------------------
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003115 public void disableKeyguard(IBinder token, String tag) {
3116 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3117 != PackageManager.PERMISSION_GRANTED) {
3118 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3119 }
3120 mKeyguardDisabled.acquire(token, tag);
3121 }
3122
3123 public void reenableKeyguard(IBinder token) {
3124 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3125 != PackageManager.PERMISSION_GRANTED) {
3126 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3127 }
3128 synchronized (mKeyguardDisabled) {
3129 mKeyguardDisabled.release(token);
3130
3131 if (!mKeyguardDisabled.isAcquired()) {
3132 // if we are the last one to reenable the keyguard wait until
3133 // we have actaully finished reenabling until returning
3134 mWaitingUntilKeyguardReenabled = true;
3135 while (mWaitingUntilKeyguardReenabled) {
3136 try {
3137 mKeyguardDisabled.wait();
3138 } catch (InterruptedException e) {
3139 Thread.currentThread().interrupt();
3140 }
3141 }
3142 }
3143 }
3144 }
3145
3146 /**
3147 * @see android.app.KeyguardManager#exitKeyguardSecurely
3148 */
3149 public void exitKeyguardSecurely(final IOnKeyguardExitResult callback) {
3150 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3151 != PackageManager.PERMISSION_GRANTED) {
3152 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3153 }
3154 mPolicy.exitKeyguardSecurely(new WindowManagerPolicy.OnKeyguardExitResult() {
3155 public void onKeyguardExitResult(boolean success) {
3156 try {
3157 callback.onKeyguardExitResult(success);
3158 } catch (RemoteException e) {
3159 // Client has died, we don't care.
3160 }
3161 }
3162 });
3163 }
3164
3165 public boolean inKeyguardRestrictedInputMode() {
3166 return mPolicy.inKeyguardRestrictedKeyInputMode();
3167 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003169 static float fixScale(float scale) {
3170 if (scale < 0) scale = 0;
3171 else if (scale > 20) scale = 20;
3172 return Math.abs(scale);
3173 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003175 public void setAnimationScale(int which, float scale) {
3176 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
3177 "setAnimationScale()")) {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003178 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003179 }
3180
3181 if (scale < 0) scale = 0;
3182 else if (scale > 20) scale = 20;
3183 scale = Math.abs(scale);
3184 switch (which) {
3185 case 0: mWindowAnimationScale = fixScale(scale); break;
3186 case 1: mTransitionAnimationScale = fixScale(scale); break;
3187 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003189 // Persist setting
3190 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
3191 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003193 public void setAnimationScales(float[] scales) {
3194 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
3195 "setAnimationScale()")) {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003196 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003197 }
3198
3199 if (scales != null) {
3200 if (scales.length >= 1) {
3201 mWindowAnimationScale = fixScale(scales[0]);
3202 }
3203 if (scales.length >= 2) {
3204 mTransitionAnimationScale = fixScale(scales[1]);
3205 }
3206 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003208 // Persist setting
3209 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
3210 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003212 public float getAnimationScale(int which) {
3213 switch (which) {
3214 case 0: return mWindowAnimationScale;
3215 case 1: return mTransitionAnimationScale;
3216 }
3217 return 0;
3218 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003220 public float[] getAnimationScales() {
3221 return new float[] { mWindowAnimationScale, mTransitionAnimationScale };
3222 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003224 public int getSwitchState(int sw) {
3225 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3226 "getSwitchState()")) {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003227 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003228 }
3229 return KeyInputQueue.getSwitchState(sw);
3230 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003231
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003232 public int getSwitchStateForDevice(int devid, int sw) {
3233 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3234 "getSwitchStateForDevice()")) {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003235 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003236 }
3237 return KeyInputQueue.getSwitchState(devid, sw);
3238 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003240 public int getScancodeState(int sw) {
3241 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3242 "getScancodeState()")) {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003243 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003244 }
3245 return KeyInputQueue.getScancodeState(sw);
3246 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003248 public int getScancodeStateForDevice(int devid, int sw) {
3249 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3250 "getScancodeStateForDevice()")) {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003251 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003252 }
3253 return KeyInputQueue.getScancodeState(devid, sw);
3254 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003256 public int getKeycodeState(int sw) {
3257 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3258 "getKeycodeState()")) {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003259 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003260 }
3261 return KeyInputQueue.getKeycodeState(sw);
3262 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003264 public int getKeycodeStateForDevice(int devid, int sw) {
3265 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3266 "getKeycodeStateForDevice()")) {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003267 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003268 }
3269 return KeyInputQueue.getKeycodeState(devid, sw);
3270 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003272 public boolean hasKeys(int[] keycodes, boolean[] keyExists) {
3273 return KeyInputQueue.hasKeys(keycodes, keyExists);
3274 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003276 public void enableScreenAfterBoot() {
3277 synchronized(mWindowMap) {
3278 if (mSystemBooted) {
3279 return;
3280 }
3281 mSystemBooted = true;
3282 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003284 performEnableScreen();
3285 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003287 public void enableScreenIfNeededLocked() {
3288 if (mDisplayEnabled) {
3289 return;
3290 }
3291 if (!mSystemBooted) {
3292 return;
3293 }
3294 mH.sendMessage(mH.obtainMessage(H.ENABLE_SCREEN));
3295 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003297 public void performEnableScreen() {
3298 synchronized(mWindowMap) {
3299 if (mDisplayEnabled) {
3300 return;
3301 }
3302 if (!mSystemBooted) {
3303 return;
3304 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003306 // Don't enable the screen until all existing windows
3307 // have been drawn.
3308 final int N = mWindows.size();
3309 for (int i=0; i<N; i++) {
3310 WindowState w = (WindowState)mWindows.get(i);
3311 if (w.isVisibleLw() && !w.isDisplayedLw()) {
3312 return;
3313 }
3314 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003316 mDisplayEnabled = true;
3317 if (false) {
3318 Log.i(TAG, "ENABLING SCREEN!");
3319 StringWriter sw = new StringWriter();
3320 PrintWriter pw = new PrintWriter(sw);
3321 this.dump(null, pw, null);
3322 Log.i(TAG, sw.toString());
3323 }
3324 try {
3325 IBinder surfaceFlinger = ServiceManager.getService("SurfaceFlinger");
3326 if (surfaceFlinger != null) {
3327 //Log.i(TAG, "******* TELLING SURFACE FLINGER WE ARE BOOTED!");
3328 Parcel data = Parcel.obtain();
3329 data.writeInterfaceToken("android.ui.ISurfaceComposer");
3330 surfaceFlinger.transact(IBinder.FIRST_CALL_TRANSACTION,
3331 data, null, 0);
3332 data.recycle();
3333 }
3334 } catch (RemoteException ex) {
3335 Log.e(TAG, "Boot completed: SurfaceFlinger is dead!");
3336 }
3337 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003339 mPolicy.enableScreenAfterBoot();
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003341 // Make sure the last requested orientation has been applied.
Dianne Hackborncc956672009-03-26 00:04:52 -07003342 setRotationUnchecked(WindowManagerPolicy.USE_LAST_ROTATION, false,
3343 mLastRotationFlags | Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003344 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003346 public void setInTouchMode(boolean mode) {
3347 synchronized(mWindowMap) {
3348 mInTouchMode = mode;
3349 }
3350 }
3351
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003352 public void setRotation(int rotation,
Dianne Hackborn02771bc2009-03-25 22:39:21 -07003353 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003354 if (!checkCallingPermission(android.Manifest.permission.SET_ORIENTATION,
Dianne Hackborn02771bc2009-03-25 22:39:21 -07003355 "setRotation()")) {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003356 throw new SecurityException("Requires SET_ORIENTATION permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003357 }
3358
Dianne Hackborn02771bc2009-03-25 22:39:21 -07003359 setRotationUnchecked(rotation, alwaysSendConfiguration, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003360 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003361
Dianne Hackborn02771bc2009-03-25 22:39:21 -07003362 public void setRotationUnchecked(int rotation,
3363 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003364 if(DEBUG_ORIENTATION) Log.v(TAG,
3365 "alwaysSendConfiguration set to "+alwaysSendConfiguration);
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003367 long origId = Binder.clearCallingIdentity();
3368 boolean changed;
3369 synchronized(mWindowMap) {
Dianne Hackborn02771bc2009-03-25 22:39:21 -07003370 changed = setRotationUncheckedLocked(rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003371 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003373 if (changed) {
3374 sendNewConfiguration();
3375 synchronized(mWindowMap) {
3376 mLayoutNeeded = true;
3377 performLayoutAndPlaceSurfacesLocked();
3378 }
3379 } else if (alwaysSendConfiguration) {
3380 //update configuration ignoring orientation change
3381 sendNewConfiguration();
3382 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003384 Binder.restoreCallingIdentity(origId);
3385 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003386
Dianne Hackborn02771bc2009-03-25 22:39:21 -07003387 public boolean setRotationUncheckedLocked(int rotation, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003388 boolean changed;
3389 if (rotation == WindowManagerPolicy.USE_LAST_ROTATION) {
3390 rotation = mRequestedRotation;
3391 } else {
3392 mRequestedRotation = rotation;
Dianne Hackborncc956672009-03-26 00:04:52 -07003393 mLastRotationFlags = animFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003394 }
3395 if (DEBUG_ORIENTATION) Log.v(TAG, "Overwriting rotation value from " + rotation);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003396 rotation = mPolicy.rotationForOrientationLw(mForcedAppOrientation,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003397 mRotation, mDisplayEnabled);
3398 if (DEBUG_ORIENTATION) Log.v(TAG, "new rotation is set to " + rotation);
3399 changed = mDisplayEnabled && mRotation != rotation;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003400
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003401 if (changed) {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003402 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003403 "Rotation changed to " + rotation
3404 + " from " + mRotation
3405 + " (forceApp=" + mForcedAppOrientation
3406 + ", req=" + mRequestedRotation + ")");
3407 mRotation = rotation;
3408 mWindowsFreezingScreen = true;
3409 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
3410 mH.sendMessageDelayed(mH.obtainMessage(H.WINDOW_FREEZE_TIMEOUT),
3411 2000);
3412 startFreezingDisplayLocked();
Dianne Hackborn02771bc2009-03-25 22:39:21 -07003413 Log.i(TAG, "Setting rotation to " + rotation + ", animFlags=" + animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003414 mQueue.setOrientation(rotation);
3415 if (mDisplayEnabled) {
Dianne Hackborncc956672009-03-26 00:04:52 -07003416 Surface.setOrientation(0, rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003417 }
3418 for (int i=mWindows.size()-1; i>=0; i--) {
3419 WindowState w = (WindowState)mWindows.get(i);
3420 if (w.mSurface != null) {
3421 w.mOrientationChanging = true;
3422 }
3423 }
3424 for (int i=mRotationWatchers.size()-1; i>=0; i--) {
3425 try {
3426 mRotationWatchers.get(i).onRotationChanged(rotation);
3427 } catch (RemoteException e) {
3428 }
3429 }
3430 } //end if changed
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003431
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003432 return changed;
3433 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003435 public int getRotation() {
3436 return mRotation;
3437 }
3438
3439 public int watchRotation(IRotationWatcher watcher) {
3440 final IBinder watcherBinder = watcher.asBinder();
3441 IBinder.DeathRecipient dr = new IBinder.DeathRecipient() {
3442 public void binderDied() {
3443 synchronized (mWindowMap) {
3444 for (int i=0; i<mRotationWatchers.size(); i++) {
3445 if (watcherBinder == mRotationWatchers.get(i).asBinder()) {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003446 IRotationWatcher removed = mRotationWatchers.remove(i);
3447 if (removed != null) {
3448 removed.asBinder().unlinkToDeath(this, 0);
3449 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003450 i--;
3451 }
3452 }
3453 }
3454 }
3455 };
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003457 synchronized (mWindowMap) {
3458 try {
3459 watcher.asBinder().linkToDeath(dr, 0);
3460 mRotationWatchers.add(watcher);
3461 } catch (RemoteException e) {
3462 // Client died, no cleanup needed.
3463 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003465 return mRotation;
3466 }
3467 }
3468
3469 /**
3470 * Starts the view server on the specified port.
3471 *
3472 * @param port The port to listener to.
3473 *
3474 * @return True if the server was successfully started, false otherwise.
3475 *
3476 * @see com.android.server.ViewServer
3477 * @see com.android.server.ViewServer#VIEW_SERVER_DEFAULT_PORT
3478 */
3479 public boolean startViewServer(int port) {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003480 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003481 return false;
3482 }
3483
3484 if (!checkCallingPermission(Manifest.permission.DUMP, "startViewServer")) {
3485 return false;
3486 }
3487
3488 if (port < 1024) {
3489 return false;
3490 }
3491
3492 if (mViewServer != null) {
3493 if (!mViewServer.isRunning()) {
3494 try {
3495 return mViewServer.start();
3496 } catch (IOException e) {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003497 Log.w(TAG, "View server did not start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003498 }
3499 }
3500 return false;
3501 }
3502
3503 try {
3504 mViewServer = new ViewServer(this, port);
3505 return mViewServer.start();
3506 } catch (IOException e) {
3507 Log.w(TAG, "View server did not start");
3508 }
3509 return false;
3510 }
3511
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003512 private boolean isSystemSecure() {
3513 return "1".equals(SystemProperties.get(SYSTEM_SECURE, "1")) &&
3514 "0".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
3515 }
3516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003517 /**
3518 * Stops the view server if it exists.
3519 *
3520 * @return True if the server stopped, false if it wasn't started or
3521 * couldn't be stopped.
3522 *
3523 * @see com.android.server.ViewServer
3524 */
3525 public boolean stopViewServer() {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003526 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003527 return false;
3528 }
3529
3530 if (!checkCallingPermission(Manifest.permission.DUMP, "stopViewServer")) {
3531 return false;
3532 }
3533
3534 if (mViewServer != null) {
3535 return mViewServer.stop();
3536 }
3537 return false;
3538 }
3539
3540 /**
3541 * Indicates whether the view server is running.
3542 *
3543 * @return True if the server is running, false otherwise.
3544 *
3545 * @see com.android.server.ViewServer
3546 */
3547 public boolean isViewServerRunning() {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003548 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003549 return false;
3550 }
3551
3552 if (!checkCallingPermission(Manifest.permission.DUMP, "isViewServerRunning")) {
3553 return false;
3554 }
3555
3556 return mViewServer != null && mViewServer.isRunning();
3557 }
3558
3559 /**
3560 * Lists all availble windows in the system. The listing is written in the
3561 * specified Socket's output stream with the following syntax:
3562 * windowHashCodeInHexadecimal windowName
3563 * Each line of the ouput represents a different window.
3564 *
3565 * @param client The remote client to send the listing to.
3566 * @return False if an error occured, true otherwise.
3567 */
3568 boolean viewServerListWindows(Socket client) {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003569 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003570 return false;
3571 }
3572
3573 boolean result = true;
3574
3575 Object[] windows;
3576 synchronized (mWindowMap) {
3577 windows = new Object[mWindows.size()];
3578 //noinspection unchecked
3579 windows = mWindows.toArray(windows);
3580 }
3581
3582 BufferedWriter out = null;
3583
3584 // Any uncaught exception will crash the system process
3585 try {
3586 OutputStream clientStream = client.getOutputStream();
3587 out = new BufferedWriter(new OutputStreamWriter(clientStream), 8 * 1024);
3588
3589 final int count = windows.length;
3590 for (int i = 0; i < count; i++) {
3591 final WindowState w = (WindowState) windows[i];
3592 out.write(Integer.toHexString(System.identityHashCode(w)));
3593 out.write(' ');
3594 out.append(w.mAttrs.getTitle());
3595 out.write('\n');
3596 }
3597
3598 out.write("DONE.\n");
3599 out.flush();
3600 } catch (Exception e) {
3601 result = false;
3602 } finally {
3603 if (out != null) {
3604 try {
3605 out.close();
3606 } catch (IOException e) {
3607 result = false;
3608 }
3609 }
3610 }
3611
3612 return result;
3613 }
3614
3615 /**
3616 * Sends a command to a target window. The result of the command, if any, will be
3617 * written in the output stream of the specified socket.
3618 *
3619 * The parameters must follow this syntax:
3620 * windowHashcode extra
3621 *
3622 * Where XX is the length in characeters of the windowTitle.
3623 *
3624 * The first parameter is the target window. The window with the specified hashcode
3625 * will be the target. If no target can be found, nothing happens. The extra parameters
3626 * will be delivered to the target window and as parameters to the command itself.
3627 *
3628 * @param client The remote client to sent the result, if any, to.
3629 * @param command The command to execute.
3630 * @param parameters The command parameters.
3631 *
3632 * @return True if the command was successfully delivered, false otherwise. This does
3633 * not indicate whether the command itself was successful.
3634 */
3635 boolean viewServerWindowCommand(Socket client, String command, String parameters) {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003636 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003637 return false;
3638 }
3639
3640 boolean success = true;
3641 Parcel data = null;
3642 Parcel reply = null;
3643
3644 // Any uncaught exception will crash the system process
3645 try {
3646 // Find the hashcode of the window
3647 int index = parameters.indexOf(' ');
3648 if (index == -1) {
3649 index = parameters.length();
3650 }
3651 final String code = parameters.substring(0, index);
3652 int hashCode = "ffffffff".equals(code) ? -1 : Integer.parseInt(code, 16);
3653
3654 // Extract the command's parameter after the window description
3655 if (index < parameters.length()) {
3656 parameters = parameters.substring(index + 1);
3657 } else {
3658 parameters = "";
3659 }
3660
3661 final WindowManagerService.WindowState window = findWindow(hashCode);
3662 if (window == null) {
3663 return false;
3664 }
3665
3666 data = Parcel.obtain();
3667 data.writeInterfaceToken("android.view.IWindow");
3668 data.writeString(command);
3669 data.writeString(parameters);
3670 data.writeInt(1);
3671 ParcelFileDescriptor.fromSocket(client).writeToParcel(data, 0);
3672
3673 reply = Parcel.obtain();
3674
3675 final IBinder binder = window.mClient.asBinder();
3676 // TODO: GET THE TRANSACTION CODE IN A SAFER MANNER
3677 binder.transact(IBinder.FIRST_CALL_TRANSACTION, data, reply, 0);
3678
3679 reply.readException();
3680
3681 } catch (Exception e) {
3682 Log.w(TAG, "Could not send command " + command + " with parameters " + parameters, e);
3683 success = false;
3684 } finally {
3685 if (data != null) {
3686 data.recycle();
3687 }
3688 if (reply != null) {
3689 reply.recycle();
3690 }
3691 }
3692
3693 return success;
3694 }
3695
3696 private WindowState findWindow(int hashCode) {
3697 if (hashCode == -1) {
3698 return getFocusedWindow();
3699 }
3700
3701 synchronized (mWindowMap) {
3702 final ArrayList windows = mWindows;
3703 final int count = windows.size();
3704
3705 for (int i = 0; i < count; i++) {
3706 WindowState w = (WindowState) windows.get(i);
3707 if (System.identityHashCode(w) == hashCode) {
3708 return w;
3709 }
3710 }
3711 }
3712
3713 return null;
3714 }
3715
3716 /*
3717 * Instruct the Activity Manager to fetch the current configuration and broadcast
3718 * that to config-changed listeners if appropriate.
3719 */
3720 void sendNewConfiguration() {
3721 try {
3722 mActivityManager.updateConfiguration(null);
3723 } catch (RemoteException e) {
3724 }
3725 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003726
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003727 public Configuration computeNewConfiguration() {
3728 synchronized (mWindowMap) {
Dianne Hackborn9d4de632009-03-24 19:37:25 -07003729 return computeNewConfigurationLocked();
3730 }
3731 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003732
Dianne Hackborn9d4de632009-03-24 19:37:25 -07003733 Configuration computeNewConfigurationLocked() {
3734 Configuration config = new Configuration();
3735 if (!computeNewConfigurationLocked(config)) {
3736 return null;
3737 }
3738 Log.i(TAG, "Config changed: " + config);
3739 long now = SystemClock.uptimeMillis();
3740 //Log.i(TAG, "Config changing, gc pending: " + mFreezeGcPending + ", now " + now);
3741 if (mFreezeGcPending != 0) {
3742 if (now > (mFreezeGcPending+1000)) {
3743 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
3744 mH.removeMessages(H.FORCE_GC);
3745 Runtime.getRuntime().gc();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003746 mFreezeGcPending = now;
3747 }
Dianne Hackborn9d4de632009-03-24 19:37:25 -07003748 } else {
3749 mFreezeGcPending = now;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003750 }
Dianne Hackborn9d4de632009-03-24 19:37:25 -07003751 return config;
3752 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003753
Dianne Hackborn9d4de632009-03-24 19:37:25 -07003754 boolean computeNewConfigurationLocked(Configuration config) {
3755 if (mDisplay == null) {
3756 return false;
3757 }
3758 mQueue.getInputConfiguration(config);
3759 final int dw = mDisplay.getWidth();
3760 final int dh = mDisplay.getHeight();
3761 int orientation = Configuration.ORIENTATION_SQUARE;
3762 if (dw < dh) {
3763 orientation = Configuration.ORIENTATION_PORTRAIT;
3764 } else if (dw > dh) {
3765 orientation = Configuration.ORIENTATION_LANDSCAPE;
3766 }
3767 config.orientation = orientation;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003768
Jean-Baptiste Querua8675f62009-07-29 14:25:07 -07003769 DisplayMetrics dm = new DisplayMetrics();
3770 mDisplay.getMetrics(dm);
3771 CompatibilityInfo.updateCompatibleScreenFrame(dm, orientation, mCompatibleScreenFrame);
3772
3773 if (mScreenLayout == Configuration.SCREENLAYOUT_SIZE_UNDEFINED) {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003774 // Note we only do this once because at this point we don't
3775 // expect the screen to change in this way at runtime, and want
3776 // to avoid all of this computation for every config change.
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003777 int longSize = dw;
3778 int shortSize = dh;
3779 if (longSize < shortSize) {
3780 int tmp = longSize;
3781 longSize = shortSize;
3782 shortSize = tmp;
3783 }
3784 longSize = (int)(longSize/dm.density);
3785 shortSize = (int)(shortSize/dm.density);
Jean-Baptiste Querua8675f62009-07-29 14:25:07 -07003786
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003787 // These semi-magic numbers define our compatibility modes for
3788 // applications with different screens. Don't change unless you
3789 // make sure to test lots and lots of apps!
3790 if (longSize < 470) {
3791 // This is shorter than an HVGA normal density screen (which
3792 // is 480 pixels on its long side).
Jean-Baptiste Querua8675f62009-07-29 14:25:07 -07003793 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_SMALL
3794 | Configuration.SCREENLAYOUT_LONG_NO;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003795 } else {
Jean-Baptiste Querua8675f62009-07-29 14:25:07 -07003796 // Is this a large screen?
3797 if (longSize > 640 && shortSize >= 480) {
3798 // VGA or larger screens at medium density are the point
3799 // at which we consider it to be a large screen.
3800 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_LARGE;
3801 } else {
3802 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_NORMAL;
3803
3804 // If this screen is wider than normal HVGA, or taller
3805 // than FWVGA, then for old apps we want to run in size
3806 // compatibility mode.
3807 if (shortSize > 321 || longSize > 570) {
3808 mScreenLayout |= Configuration.SCREENLAYOUT_COMPAT_NEEDED;
3809 }
3810 }
3811
3812 // Is this a long screen?
3813 if (((longSize*3)/5) >= (shortSize-1)) {
3814 // Anything wider than WVGA (5:3) is considering to be long.
3815 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_YES;
3816 } else {
3817 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_NO;
3818 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003819 }
3820 }
Jean-Baptiste Querua8675f62009-07-29 14:25:07 -07003821 config.screenLayout = mScreenLayout;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003822
Dianne Hackborn9d4de632009-03-24 19:37:25 -07003823 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
3824 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
3825 mPolicy.adjustConfigurationLw(config);
3826 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003827 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003828
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003829 // -------------------------------------------------------------
3830 // Input Events and Focus Management
3831 // -------------------------------------------------------------
3832
3833 private final void wakeupIfNeeded(WindowState targetWin, int eventType) {
Jean-Baptiste Queru843ef362009-05-20 11:28:04 -07003834 long curTime = SystemClock.uptimeMillis();
3835
3836 if (eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT) {
3837 if (mLastTouchEventType == eventType &&
3838 (curTime - mLastUserActivityCallTime) < MIN_TIME_BETWEEN_USERACTIVITIES) {
3839 return;
3840 }
3841 mLastUserActivityCallTime = curTime;
3842 mLastTouchEventType = eventType;
3843 }
3844
3845 if (targetWin == null
3846 || targetWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
3847 mPowerManager.userActivity(curTime, false, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003848 }
3849 }
3850
3851 // tells if it's a cheek event or not -- this function is stateful
3852 private static final int EVENT_NONE = 0;
3853 private static final int EVENT_UNKNOWN = 0;
3854 private static final int EVENT_CHEEK = 0;
3855 private static final int EVENT_IGNORE_DURATION = 300; // ms
3856 private static final float CHEEK_THRESHOLD = 0.6f;
3857 private int mEventState = EVENT_NONE;
3858 private float mEventSize;
The Android Open Source Projectc2ad2412009-03-19 23:08:54 -07003859
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003860 private int eventType(MotionEvent ev) {
3861 float size = ev.getSize();
3862 switch (ev.getAction()) {
3863 case MotionEvent.ACTION_DOWN:
3864 mEventSize = size;
3865 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_EVENT;
3866 case MotionEvent.ACTION_UP:
3867 if (size > mEventSize) mEventSize = size;
The Android Open Source Projectc2ad2412009-03-19 23:08:54 -07003868 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003869 case MotionEvent.ACTION_MOVE:
3870 final int N = ev.getHistorySize();
3871 if (size > mEventSize) mEventSize = size;
3872 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
3873 for (int i=0; i<N; i++) {
3874 size = ev.getHistoricalSize(i);
3875 if (size > mEventSize) mEventSize = size;
3876 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
3877 }
3878 if (ev.getEventTime() < ev.getDownTime() + EVENT_IGNORE_DURATION) {
3879 return TOUCH_EVENT;
3880 } else {
The Android Open Source Projectc2ad2412009-03-19 23:08:54 -07003881 return LONG_TOUCH_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003882 }
3883 default:
3884 // not good
3885 return OTHER_EVENT;
3886 }
3887 }
3888
3889 /**
3890 * @return Returns true if event was dispatched, false if it was dropped for any reason
3891 */
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003892 private int dispatchPointer(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003893 if (DEBUG_INPUT || WindowManagerPolicy.WATCH_POINTER) Log.v(TAG,
3894 "dispatchPointer " + ev);
3895
3896 Object targetObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003897 ev, true, false, pid, uid);
3898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003899 int action = ev.getAction();
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003900
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003901 if (action == MotionEvent.ACTION_UP) {
3902 // let go of our target
3903 mKeyWaiter.mMotionTarget = null;
3904 mPowerManager.logPointerUpEvent();
3905 } else if (action == MotionEvent.ACTION_DOWN) {
3906 mPowerManager.logPointerDownEvent();
3907 }
3908
3909 if (targetObj == null) {
3910 // In this case we are either dropping the event, or have received
3911 // a move or up without a down. It is common to receive move
3912 // events in such a way, since this means the user is moving the
3913 // pointer without actually pressing down. All other cases should
3914 // be atypical, so let's log them.
Jean-Baptiste Queru843ef362009-05-20 11:28:04 -07003915 if (action != MotionEvent.ACTION_MOVE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003916 Log.w(TAG, "No window to dispatch pointer action " + ev.getAction());
3917 }
3918 if (qev != null) {
3919 mQueue.recycleEvent(qev);
3920 }
3921 ev.recycle();
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003922 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003923 }
3924 if (targetObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
3925 if (qev != null) {
3926 mQueue.recycleEvent(qev);
3927 }
3928 ev.recycle();
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003929 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003930 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003931
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003932 WindowState target = (WindowState)targetObj;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003933
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003934 final long eventTime = ev.getEventTime();
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003936 //Log.i(TAG, "Sending " + ev + " to " + target);
3937
3938 if (uid != 0 && uid != target.mSession.mUid) {
3939 if (mContext.checkPermission(
3940 android.Manifest.permission.INJECT_EVENTS, pid, uid)
3941 != PackageManager.PERMISSION_GRANTED) {
3942 Log.w(TAG, "Permission denied: injecting pointer event from pid "
3943 + pid + " uid " + uid + " to window " + target
3944 + " owned by uid " + target.mSession.mUid);
3945 if (qev != null) {
3946 mQueue.recycleEvent(qev);
3947 }
3948 ev.recycle();
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003949 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003950 }
3951 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003952
3953 if ((target.mAttrs.flags &
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003954 WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES) != 0) {
3955 //target wants to ignore fat touch events
3956 boolean cheekPress = mPolicy.isCheekPressedAgainstScreen(ev);
3957 //explicit flag to return without processing event further
3958 boolean returnFlag = false;
3959 if((action == MotionEvent.ACTION_DOWN)) {
3960 mFatTouch = false;
3961 if(cheekPress) {
3962 mFatTouch = true;
3963 returnFlag = true;
3964 }
3965 } else {
3966 if(action == MotionEvent.ACTION_UP) {
3967 if(mFatTouch) {
3968 //earlier even was invalid doesnt matter if current up is cheekpress or not
3969 mFatTouch = false;
3970 returnFlag = true;
3971 } else if(cheekPress) {
3972 //cancel the earlier event
3973 ev.setAction(MotionEvent.ACTION_CANCEL);
3974 action = MotionEvent.ACTION_CANCEL;
3975 }
3976 } else if(action == MotionEvent.ACTION_MOVE) {
3977 if(mFatTouch) {
3978 //two cases here
3979 //an invalid down followed by 0 or moves(valid or invalid)
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003980 //a valid down, invalid move, more moves. want to ignore till up
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003981 returnFlag = true;
3982 } else if(cheekPress) {
3983 //valid down followed by invalid moves
3984 //an invalid move have to cancel earlier action
3985 ev.setAction(MotionEvent.ACTION_CANCEL);
3986 action = MotionEvent.ACTION_CANCEL;
3987 if (DEBUG_INPUT) Log.v(TAG, "Sending cancel for invalid ACTION_MOVE");
3988 //note that the subsequent invalid moves will not get here
3989 mFatTouch = true;
3990 }
3991 }
3992 } //else if action
3993 if(returnFlag) {
3994 //recycle que, ev
3995 if (qev != null) {
3996 mQueue.recycleEvent(qev);
3997 }
3998 ev.recycle();
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07003999 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004000 }
4001 } //end if target
Jean-Baptiste Queru843ef362009-05-20 11:28:04 -07004002
Jean-Baptiste Queruac1e59d2009-08-11 13:12:34 -07004003 // Enable this for testing the "right" value
4004 if (false && action == MotionEvent.ACTION_DOWN) {
Jean-Baptiste Queru843ef362009-05-20 11:28:04 -07004005 int max_events_per_sec = 35;
4006 try {
4007 max_events_per_sec = Integer.parseInt(SystemProperties
4008 .get("windowsmgr.max_events_per_sec"));
4009 if (max_events_per_sec < 1) {
4010 max_events_per_sec = 35;
4011 }
4012 } catch (NumberFormatException e) {
4013 }
4014 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
4015 }
4016
4017 /*
4018 * Throttle events to minimize CPU usage when there's a flood of events
4019 * e.g. constant contact with the screen
4020 */
4021 if (action == MotionEvent.ACTION_MOVE) {
4022 long nextEventTime = mLastTouchEventTime + mMinWaitTimeBetweenTouchEvents;
4023 long now = SystemClock.uptimeMillis();
4024 if (now < nextEventTime) {
4025 try {
4026 Thread.sleep(nextEventTime - now);
4027 } catch (InterruptedException e) {
4028 }
4029 mLastTouchEventTime = nextEventTime;
4030 } else {
4031 mLastTouchEventTime = now;
4032 }
4033 }
4034
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004035 synchronized(mWindowMap) {
4036 if (qev != null && action == MotionEvent.ACTION_MOVE) {
4037 mKeyWaiter.bindTargetWindowLocked(target,
4038 KeyWaiter.RETURN_PENDING_POINTER, qev);
4039 ev = null;
4040 } else {
4041 if (action == MotionEvent.ACTION_DOWN) {
4042 WindowState out = mKeyWaiter.mOutsideTouchTargets;
4043 if (out != null) {
4044 MotionEvent oev = MotionEvent.obtain(ev);
4045 oev.setAction(MotionEvent.ACTION_OUTSIDE);
4046 do {
4047 final Rect frame = out.mFrame;
4048 oev.offsetLocation(-(float)frame.left, -(float)frame.top);
4049 try {
4050 out.mClient.dispatchPointer(oev, eventTime);
4051 } catch (android.os.RemoteException e) {
4052 Log.i(TAG, "WINDOW DIED during outside motion dispatch: " + out);
4053 }
4054 oev.offsetLocation((float)frame.left, (float)frame.top);
4055 out = out.mNextOutsideTouch;
4056 } while (out != null);
4057 mKeyWaiter.mOutsideTouchTargets = null;
4058 }
4059 }
4060 final Rect frame = target.mFrame;
4061 ev.offsetLocation(-(float)frame.left, -(float)frame.top);
4062 mKeyWaiter.bindTargetWindowLocked(target);
4063 }
4064 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004066 // finally offset the event to the target's coordinate system and
4067 // dispatch the event.
4068 try {
4069 if (DEBUG_INPUT || DEBUG_FOCUS || WindowManagerPolicy.WATCH_POINTER) {
4070 Log.v(TAG, "Delivering pointer " + qev + " to " + target);
4071 }
4072 target.mClient.dispatchPointer(ev, eventTime);
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004073 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004074 } catch (android.os.RemoteException e) {
4075 Log.i(TAG, "WINDOW DIED during motion dispatch: " + target);
4076 mKeyWaiter.mMotionTarget = null;
4077 try {
4078 removeWindow(target.mSession, target.mClient);
4079 } catch (java.util.NoSuchElementException ex) {
4080 // This will happen if the window has already been
4081 // removed.
4082 }
4083 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004084 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004085 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004086
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004087 /**
4088 * @return Returns true if event was dispatched, false if it was dropped for any reason
4089 */
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004090 private int dispatchTrackball(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004091 if (DEBUG_INPUT) Log.v(
4092 TAG, "dispatchTrackball [" + ev.getAction() +"] <" + ev.getX() + ", " + ev.getY() + ">");
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004094 Object focusObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004095 ev, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004096 if (focusObj == null) {
4097 Log.w(TAG, "No focus window, dropping trackball: " + ev);
4098 if (qev != null) {
4099 mQueue.recycleEvent(qev);
4100 }
4101 ev.recycle();
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004102 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004103 }
4104 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
4105 if (qev != null) {
4106 mQueue.recycleEvent(qev);
4107 }
4108 ev.recycle();
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004109 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004110 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004112 WindowState focus = (WindowState)focusObj;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004114 if (uid != 0 && uid != focus.mSession.mUid) {
4115 if (mContext.checkPermission(
4116 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4117 != PackageManager.PERMISSION_GRANTED) {
4118 Log.w(TAG, "Permission denied: injecting key event from pid "
4119 + pid + " uid " + uid + " to window " + focus
4120 + " owned by uid " + focus.mSession.mUid);
4121 if (qev != null) {
4122 mQueue.recycleEvent(qev);
4123 }
4124 ev.recycle();
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004125 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004126 }
4127 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004129 final long eventTime = ev.getEventTime();
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004131 synchronized(mWindowMap) {
4132 if (qev != null && ev.getAction() == MotionEvent.ACTION_MOVE) {
4133 mKeyWaiter.bindTargetWindowLocked(focus,
4134 KeyWaiter.RETURN_PENDING_TRACKBALL, qev);
4135 // We don't deliver movement events to the client, we hold
4136 // them and wait for them to call back.
4137 ev = null;
4138 } else {
4139 mKeyWaiter.bindTargetWindowLocked(focus);
4140 }
4141 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004143 try {
4144 focus.mClient.dispatchTrackball(ev, eventTime);
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004145 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004146 } catch (android.os.RemoteException e) {
4147 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
4148 try {
4149 removeWindow(focus.mSession, focus.mClient);
4150 } catch (java.util.NoSuchElementException ex) {
4151 // This will happen if the window has already been
4152 // removed.
4153 }
4154 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004155
4156 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004157 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004159 /**
4160 * @return Returns true if event was dispatched, false if it was dropped for any reason
4161 */
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004162 private int dispatchKey(KeyEvent event, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004163 if (DEBUG_INPUT) Log.v(TAG, "Dispatch key: " + event);
4164
4165 Object focusObj = mKeyWaiter.waitForNextEventTarget(event, null,
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004166 null, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004167 if (focusObj == null) {
4168 Log.w(TAG, "No focus window, dropping: " + event);
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004169 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004170 }
4171 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004172 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004173 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004175 WindowState focus = (WindowState)focusObj;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004177 if (DEBUG_INPUT) Log.v(
4178 TAG, "Dispatching to " + focus + ": " + event);
4179
4180 if (uid != 0 && uid != focus.mSession.mUid) {
4181 if (mContext.checkPermission(
4182 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4183 != PackageManager.PERMISSION_GRANTED) {
4184 Log.w(TAG, "Permission denied: injecting key event from pid "
4185 + pid + " uid " + uid + " to window " + focus
4186 + " owned by uid " + focus.mSession.mUid);
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004187 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004188 }
4189 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004191 synchronized(mWindowMap) {
4192 mKeyWaiter.bindTargetWindowLocked(focus);
4193 }
4194
4195 // NOSHIP extra state logging
4196 mKeyWaiter.recordDispatchState(event, focus);
4197 // END NOSHIP
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004199 try {
4200 if (DEBUG_INPUT || DEBUG_FOCUS) {
4201 Log.v(TAG, "Delivering key " + event.getKeyCode()
4202 + " to " + focus);
4203 }
4204 focus.mClient.dispatchKey(event);
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004205 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004206 } catch (android.os.RemoteException e) {
4207 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
4208 try {
4209 removeWindow(focus.mSession, focus.mClient);
4210 } catch (java.util.NoSuchElementException ex) {
4211 // This will happen if the window has already been
4212 // removed.
4213 }
4214 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004215
4216 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004217 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004219 public void pauseKeyDispatching(IBinder _token) {
4220 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4221 "pauseKeyDispatching()")) {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004222 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004223 }
4224
4225 synchronized (mWindowMap) {
4226 WindowToken token = mTokenMap.get(_token);
4227 if (token != null) {
4228 mKeyWaiter.pauseDispatchingLocked(token);
4229 }
4230 }
4231 }
4232
4233 public void resumeKeyDispatching(IBinder _token) {
4234 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4235 "resumeKeyDispatching()")) {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004236 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004237 }
4238
4239 synchronized (mWindowMap) {
4240 WindowToken token = mTokenMap.get(_token);
4241 if (token != null) {
4242 mKeyWaiter.resumeDispatchingLocked(token);
4243 }
4244 }
4245 }
4246
4247 public void setEventDispatching(boolean enabled) {
4248 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4249 "resumeKeyDispatching()")) {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004250 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004251 }
4252
4253 synchronized (mWindowMap) {
4254 mKeyWaiter.setEventDispatchingLocked(enabled);
4255 }
4256 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004258 /**
4259 * Injects a keystroke event into the UI.
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004260 *
4261 * @param ev A motion event describing the keystroke action. (Be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004262 * {@link SystemClock#uptimeMillis()} as the timebase.)
4263 * @param sync If true, wait for the event to be completed before returning to the caller.
4264 * @return Returns true if event was dispatched, false if it was dropped for any reason
4265 */
4266 public boolean injectKeyEvent(KeyEvent ev, boolean sync) {
4267 long downTime = ev.getDownTime();
4268 long eventTime = ev.getEventTime();
4269
4270 int action = ev.getAction();
4271 int code = ev.getKeyCode();
4272 int repeatCount = ev.getRepeatCount();
4273 int metaState = ev.getMetaState();
4274 int deviceId = ev.getDeviceId();
4275 int scancode = ev.getScanCode();
4276
4277 if (eventTime == 0) eventTime = SystemClock.uptimeMillis();
4278 if (downTime == 0) downTime = eventTime;
4279
4280 KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
The Android Open Source Project10592532009-03-18 17:39:46 -07004281 deviceId, scancode, KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004282
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004283 final int pid = Binder.getCallingPid();
4284 final int uid = Binder.getCallingUid();
4285 final long ident = Binder.clearCallingIdentity();
4286 final int result = dispatchKey(newEvent, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004287 if (sync) {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004288 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004289 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004290 Binder.restoreCallingIdentity(ident);
4291 switch (result) {
4292 case INJECT_NO_PERMISSION:
4293 throw new SecurityException(
4294 "Injecting to another application requires INJECT_EVENT permission");
4295 case INJECT_SUCCEEDED:
4296 return true;
4297 }
4298 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004299 }
4300
4301 /**
4302 * Inject a pointer (touch) event into the UI.
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004303 *
4304 * @param ev A motion event describing the pointer (touch) action. (As noted in
4305 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004306 * {@link SystemClock#uptimeMillis()} as the timebase.)
4307 * @param sync If true, wait for the event to be completed before returning to the caller.
4308 * @return Returns true if event was dispatched, false if it was dropped for any reason
4309 */
4310 public boolean injectPointerEvent(MotionEvent ev, boolean sync) {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004311 final int pid = Binder.getCallingPid();
4312 final int uid = Binder.getCallingUid();
4313 final long ident = Binder.clearCallingIdentity();
4314 final int result = dispatchPointer(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004315 if (sync) {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004316 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004317 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004318 Binder.restoreCallingIdentity(ident);
4319 switch (result) {
4320 case INJECT_NO_PERMISSION:
4321 throw new SecurityException(
4322 "Injecting to another application requires INJECT_EVENT permission");
4323 case INJECT_SUCCEEDED:
4324 return true;
4325 }
4326 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004327 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004329 /**
4330 * Inject a trackball (navigation device) event into the UI.
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004331 *
4332 * @param ev A motion event describing the trackball action. (As noted in
4333 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004334 * {@link SystemClock#uptimeMillis()} as the timebase.)
4335 * @param sync If true, wait for the event to be completed before returning to the caller.
4336 * @return Returns true if event was dispatched, false if it was dropped for any reason
4337 */
4338 public boolean injectTrackballEvent(MotionEvent ev, boolean sync) {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004339 final int pid = Binder.getCallingPid();
4340 final int uid = Binder.getCallingUid();
4341 final long ident = Binder.clearCallingIdentity();
4342 final int result = dispatchTrackball(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004343 if (sync) {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004344 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004345 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004346 Binder.restoreCallingIdentity(ident);
4347 switch (result) {
4348 case INJECT_NO_PERMISSION:
4349 throw new SecurityException(
4350 "Injecting to another application requires INJECT_EVENT permission");
4351 case INJECT_SUCCEEDED:
4352 return true;
4353 }
4354 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004355 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004357 private WindowState getFocusedWindow() {
4358 synchronized (mWindowMap) {
4359 return getFocusedWindowLocked();
4360 }
4361 }
4362
4363 private WindowState getFocusedWindowLocked() {
4364 return mCurrentFocus;
4365 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004367 /**
4368 * This class holds the state for dispatching key events. This state
4369 * is protected by the KeyWaiter instance, NOT by the window lock. You
4370 * can be holding the main window lock while acquire the KeyWaiter lock,
4371 * but not the other way around.
4372 */
4373 final class KeyWaiter {
4374 // NOSHIP debugging
4375 public class DispatchState {
4376 private KeyEvent event;
4377 private WindowState focus;
4378 private long time;
4379 private WindowState lastWin;
4380 private IBinder lastBinder;
4381 private boolean finished;
4382 private boolean gotFirstWindow;
4383 private boolean eventDispatching;
4384 private long timeToSwitch;
4385 private boolean wasFrozen;
4386 private boolean focusPaused;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004387 private WindowState curFocus;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004388
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004389 DispatchState(KeyEvent theEvent, WindowState theFocus) {
4390 focus = theFocus;
4391 event = theEvent;
4392 time = System.currentTimeMillis();
4393 // snapshot KeyWaiter state
4394 lastWin = mLastWin;
4395 lastBinder = mLastBinder;
4396 finished = mFinished;
4397 gotFirstWindow = mGotFirstWindow;
4398 eventDispatching = mEventDispatching;
4399 timeToSwitch = mTimeToSwitch;
4400 wasFrozen = mWasFrozen;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004401 curFocus = mCurrentFocus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004402 // cache the paused state at ctor time as well
4403 if (theFocus == null || theFocus.mToken == null) {
4404 Log.i(TAG, "focus " + theFocus + " mToken is null at event dispatch!");
4405 focusPaused = false;
4406 } else {
4407 focusPaused = theFocus.mToken.paused;
4408 }
4409 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004411 public String toString() {
4412 return "{{" + event + " to " + focus + " @ " + time
4413 + " lw=" + lastWin + " lb=" + lastBinder
4414 + " fin=" + finished + " gfw=" + gotFirstWindow
4415 + " ed=" + eventDispatching + " tts=" + timeToSwitch
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004416 + " wf=" + wasFrozen + " fp=" + focusPaused
4417 + " mcf=" + mCurrentFocus + "}}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004418 }
4419 };
4420 private DispatchState mDispatchState = null;
4421 public void recordDispatchState(KeyEvent theEvent, WindowState theFocus) {
4422 mDispatchState = new DispatchState(theEvent, theFocus);
4423 }
4424 // END NOSHIP
4425
4426 public static final int RETURN_NOTHING = 0;
4427 public static final int RETURN_PENDING_POINTER = 1;
4428 public static final int RETURN_PENDING_TRACKBALL = 2;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004430 final Object SKIP_TARGET_TOKEN = new Object();
4431 final Object CONSUMED_EVENT_TOKEN = new Object();
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004433 private WindowState mLastWin = null;
4434 private IBinder mLastBinder = null;
4435 private boolean mFinished = true;
4436 private boolean mGotFirstWindow = false;
4437 private boolean mEventDispatching = true;
4438 private long mTimeToSwitch = 0;
4439 /* package */ boolean mWasFrozen = false;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004440
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004441 // Target of Motion events
4442 WindowState mMotionTarget;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004444 // Windows above the target who would like to receive an "outside"
4445 // touch event for any down events outside of them.
4446 WindowState mOutsideTouchTargets;
4447
4448 /**
4449 * Wait for the last event dispatch to complete, then find the next
4450 * target that should receive the given event and wait for that one
4451 * to be ready to receive it.
4452 */
4453 Object waitForNextEventTarget(KeyEvent nextKey, QueuedEvent qev,
4454 MotionEvent nextMotion, boolean isPointerEvent,
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004455 boolean failIfTimeout, int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004456 long startTime = SystemClock.uptimeMillis();
4457 long keyDispatchingTimeout = 5 * 1000;
4458 long waitedFor = 0;
4459
4460 while (true) {
4461 // Figure out which window we care about. It is either the
4462 // last window we are waiting to have process the event or,
4463 // if none, then the next window we think the event should go
4464 // to. Note: we retrieve mLastWin outside of the lock, so
4465 // it may change before we lock. Thus we must check it again.
4466 WindowState targetWin = mLastWin;
4467 boolean targetIsNew = targetWin == null;
4468 if (DEBUG_INPUT) Log.v(
4469 TAG, "waitForLastKey: mFinished=" + mFinished +
4470 ", mLastWin=" + mLastWin);
4471 if (targetIsNew) {
4472 Object target = findTargetWindow(nextKey, qev, nextMotion,
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004473 isPointerEvent, callingPid, callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004474 if (target == SKIP_TARGET_TOKEN) {
4475 // The user has pressed a special key, and we are
4476 // dropping all pending events before it.
4477 if (DEBUG_INPUT) Log.v(TAG, "Skipping: " + nextKey
4478 + " " + nextMotion);
4479 return null;
4480 }
4481 if (target == CONSUMED_EVENT_TOKEN) {
4482 if (DEBUG_INPUT) Log.v(TAG, "Consumed: " + nextKey
4483 + " " + nextMotion);
4484 return target;
4485 }
4486 targetWin = (WindowState)target;
4487 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004488
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004489 AppWindowToken targetApp = null;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004491 // Now: is it okay to send the next event to this window?
4492 synchronized (this) {
4493 // First: did we come here based on the last window not
4494 // being null, but it changed by the time we got here?
4495 // If so, try again.
4496 if (!targetIsNew && mLastWin == null) {
4497 continue;
4498 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004499
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004500 // We never dispatch events if not finished with the
4501 // last one, or the display is frozen.
4502 if (mFinished && !mDisplayFrozen) {
4503 // If event dispatching is disabled, then we
4504 // just consume the events.
4505 if (!mEventDispatching) {
4506 if (DEBUG_INPUT) Log.v(TAG,
4507 "Skipping event; dispatching disabled: "
4508 + nextKey + " " + nextMotion);
4509 return null;
4510 }
4511 if (targetWin != null) {
4512 // If this is a new target, and that target is not
4513 // paused or unresponsive, then all looks good to
4514 // handle the event.
4515 if (targetIsNew && !targetWin.mToken.paused) {
4516 return targetWin;
4517 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004518
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004519 // If we didn't find a target window, and there is no
4520 // focused app window, then just eat the events.
4521 } else if (mFocusedApp == null) {
4522 if (DEBUG_INPUT) Log.v(TAG,
4523 "Skipping event; no focused app: "
4524 + nextKey + " " + nextMotion);
4525 return null;
4526 }
4527 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004528
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004529 if (DEBUG_INPUT) Log.v(
4530 TAG, "Waiting for last key in " + mLastBinder
4531 + " target=" + targetWin
4532 + " mFinished=" + mFinished
4533 + " mDisplayFrozen=" + mDisplayFrozen
4534 + " targetIsNew=" + targetIsNew
4535 + " paused="
4536 + (targetWin != null ? targetWin.mToken.paused : false)
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004537 + " mFocusedApp=" + mFocusedApp
4538 + " mCurrentFocus=" + mCurrentFocus);
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004539
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004540 targetApp = targetWin != null
4541 ? targetWin.mAppToken : mFocusedApp;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004543 long curTimeout = keyDispatchingTimeout;
4544 if (mTimeToSwitch != 0) {
4545 long now = SystemClock.uptimeMillis();
4546 if (mTimeToSwitch <= now) {
4547 // If an app switch key has been pressed, and we have
4548 // waited too long for the current app to finish
4549 // processing keys, then wait no more!
4550 doFinishedKeyLocked(true);
4551 continue;
4552 }
4553 long switchTimeout = mTimeToSwitch - now;
4554 if (curTimeout > switchTimeout) {
4555 curTimeout = switchTimeout;
4556 }
4557 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004558
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004559 try {
4560 // after that continue
4561 // processing keys, so we don't get stuck.
4562 if (DEBUG_INPUT) Log.v(
4563 TAG, "Waiting for key dispatch: " + curTimeout);
4564 wait(curTimeout);
4565 if (DEBUG_INPUT) Log.v(TAG, "Finished waiting @"
4566 + SystemClock.uptimeMillis() + " startTime="
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004567 + startTime + " switchTime=" + mTimeToSwitch
4568 + " target=" + targetWin + " mLW=" + mLastWin
4569 + " mLB=" + mLastBinder + " fin=" + mFinished
4570 + " mCurrentFocus=" + mCurrentFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004571 } catch (InterruptedException e) {
4572 }
4573 }
4574
4575 // If we were frozen during configuration change, restart the
4576 // timeout checks from now; otherwise look at whether we timed
4577 // out before awakening.
4578 if (mWasFrozen) {
4579 waitedFor = 0;
4580 mWasFrozen = false;
4581 } else {
4582 waitedFor = SystemClock.uptimeMillis() - startTime;
4583 }
4584
4585 if (waitedFor >= keyDispatchingTimeout && mTimeToSwitch == 0) {
4586 IApplicationToken at = null;
4587 synchronized (this) {
4588 Log.w(TAG, "Key dispatching timed out sending to " +
4589 (targetWin != null ? targetWin.mAttrs.getTitle()
4590 : "<null>"));
4591 // NOSHIP debugging
4592 Log.w(TAG, "Dispatch state: " + mDispatchState);
4593 Log.w(TAG, "Current state: " + new DispatchState(nextKey, targetWin));
4594 // END NOSHIP
4595 //dump();
4596 if (targetWin != null) {
4597 at = targetWin.getAppToken();
4598 } else if (targetApp != null) {
4599 at = targetApp.appToken;
4600 }
4601 }
4602
4603 boolean abort = true;
4604 if (at != null) {
4605 try {
4606 long timeout = at.getKeyDispatchingTimeout();
4607 if (timeout > waitedFor) {
4608 // we did not wait the proper amount of time for this application.
4609 // set the timeout to be the real timeout and wait again.
4610 keyDispatchingTimeout = timeout - waitedFor;
4611 continue;
4612 } else {
4613 abort = at.keyDispatchingTimedOut();
4614 }
4615 } catch (RemoteException ex) {
4616 }
4617 }
4618
4619 synchronized (this) {
4620 if (abort && (mLastWin == targetWin || targetWin == null)) {
4621 mFinished = true;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004622 if (mLastWin != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004623 if (DEBUG_INPUT) Log.v(TAG,
4624 "Window " + mLastWin +
4625 " timed out on key input");
4626 if (mLastWin.mToken.paused) {
4627 Log.w(TAG, "Un-pausing dispatching to this window");
4628 mLastWin.mToken.paused = false;
4629 }
4630 }
4631 if (mMotionTarget == targetWin) {
4632 mMotionTarget = null;
4633 }
4634 mLastWin = null;
4635 mLastBinder = null;
4636 if (failIfTimeout || targetWin == null) {
4637 return null;
4638 }
4639 } else {
4640 Log.w(TAG, "Continuing to wait for key to be dispatched");
4641 startTime = SystemClock.uptimeMillis();
4642 }
4643 }
4644 }
4645 }
4646 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004647
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004648 Object findTargetWindow(KeyEvent nextKey, QueuedEvent qev,
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004649 MotionEvent nextMotion, boolean isPointerEvent,
4650 int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004651 mOutsideTouchTargets = null;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004653 if (nextKey != null) {
4654 // Find the target window for a normal key event.
4655 final int keycode = nextKey.getKeyCode();
4656 final int repeatCount = nextKey.getRepeatCount();
4657 final boolean down = nextKey.getAction() != KeyEvent.ACTION_UP;
4658 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(keycode);
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004660 if (!dispatch) {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004661 if (callingUid == 0 ||
4662 mContext.checkPermission(
4663 android.Manifest.permission.INJECT_EVENTS,
4664 callingPid, callingUid)
4665 == PackageManager.PERMISSION_GRANTED) {
4666 mPolicy.interceptKeyTi(null, keycode,
4667 nextKey.getMetaState(), down, repeatCount);
4668 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004669 Log.w(TAG, "Event timeout during app switch: dropping "
4670 + nextKey);
4671 return SKIP_TARGET_TOKEN;
4672 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004673
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004674 // System.out.println("##### [" + SystemClock.uptimeMillis() + "] WindowManagerService.dispatchKey(" + keycode + ", " + down + ", " + repeatCount + ")");
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004676 WindowState focus = null;
4677 synchronized(mWindowMap) {
4678 focus = getFocusedWindowLocked();
4679 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004680
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004681 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004682
4683 if (callingUid == 0 ||
4684 (focus != null && callingUid == focus.mSession.mUid) ||
4685 mContext.checkPermission(
4686 android.Manifest.permission.INJECT_EVENTS,
4687 callingPid, callingUid)
4688 == PackageManager.PERMISSION_GRANTED) {
4689 if (mPolicy.interceptKeyTi(focus,
4690 keycode, nextKey.getMetaState(), down, repeatCount)) {
4691 return CONSUMED_EVENT_TOKEN;
4692 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004693 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004694
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004695 return focus;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004697 } else if (!isPointerEvent) {
4698 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(-1);
4699 if (!dispatch) {
4700 Log.w(TAG, "Event timeout during app switch: dropping trackball "
4701 + nextMotion);
4702 return SKIP_TARGET_TOKEN;
4703 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004704
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004705 WindowState focus = null;
4706 synchronized(mWindowMap) {
4707 focus = getFocusedWindowLocked();
4708 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004709
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004710 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
4711 return focus;
4712 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004713
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004714 if (nextMotion == null) {
4715 return SKIP_TARGET_TOKEN;
4716 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004717
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004718 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(
4719 KeyEvent.KEYCODE_UNKNOWN);
4720 if (!dispatch) {
4721 Log.w(TAG, "Event timeout during app switch: dropping pointer "
4722 + nextMotion);
4723 return SKIP_TARGET_TOKEN;
4724 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004725
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004726 // Find the target window for a pointer event.
4727 int action = nextMotion.getAction();
4728 final float xf = nextMotion.getX();
4729 final float yf = nextMotion.getY();
4730 final long eventTime = nextMotion.getEventTime();
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004731
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004732 final boolean screenWasOff = qev != null
4733 && (qev.flags&WindowManagerPolicy.FLAG_BRIGHT_HERE) != 0;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004734
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004735 WindowState target = null;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004736
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004737 synchronized(mWindowMap) {
4738 synchronized (this) {
4739 if (action == MotionEvent.ACTION_DOWN) {
4740 if (mMotionTarget != null) {
4741 // this is weird, we got a pen down, but we thought it was
4742 // already down!
4743 // XXX: We should probably send an ACTION_UP to the current
4744 // target.
4745 Log.w(TAG, "Pointer down received while already down in: "
4746 + mMotionTarget);
4747 mMotionTarget = null;
4748 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004749
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004750 // ACTION_DOWN is special, because we need to lock next events to
4751 // the window we'll land onto.
4752 final int x = (int)xf;
4753 final int y = (int)yf;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004754
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004755 final ArrayList windows = mWindows;
4756 final int N = windows.size();
4757 WindowState topErrWindow = null;
4758 final Rect tmpRect = mTempRect;
4759 for (int i=N-1; i>=0; i--) {
4760 WindowState child = (WindowState)windows.get(i);
4761 //Log.i(TAG, "Checking dispatch to: " + child);
4762 final int flags = child.mAttrs.flags;
4763 if ((flags & WindowManager.LayoutParams.FLAG_SYSTEM_ERROR) != 0) {
4764 if (topErrWindow == null) {
4765 topErrWindow = child;
4766 }
4767 }
4768 if (!child.isVisibleLw()) {
4769 //Log.i(TAG, "Not visible!");
4770 continue;
4771 }
4772 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
4773 //Log.i(TAG, "Not touchable!");
4774 if ((flags & WindowManager.LayoutParams
4775 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
4776 child.mNextOutsideTouch = mOutsideTouchTargets;
4777 mOutsideTouchTargets = child;
4778 }
4779 continue;
4780 }
4781 tmpRect.set(child.mFrame);
4782 if (child.mTouchableInsets == ViewTreeObserver
4783 .InternalInsetsInfo.TOUCHABLE_INSETS_CONTENT) {
4784 // The touch is inside of the window if it is
4785 // inside the frame, AND the content part of that
4786 // frame that was given by the application.
4787 tmpRect.left += child.mGivenContentInsets.left;
4788 tmpRect.top += child.mGivenContentInsets.top;
4789 tmpRect.right -= child.mGivenContentInsets.right;
4790 tmpRect.bottom -= child.mGivenContentInsets.bottom;
4791 } else if (child.mTouchableInsets == ViewTreeObserver
4792 .InternalInsetsInfo.TOUCHABLE_INSETS_VISIBLE) {
4793 // The touch is inside of the window if it is
4794 // inside the frame, AND the visible part of that
4795 // frame that was given by the application.
4796 tmpRect.left += child.mGivenVisibleInsets.left;
4797 tmpRect.top += child.mGivenVisibleInsets.top;
4798 tmpRect.right -= child.mGivenVisibleInsets.right;
4799 tmpRect.bottom -= child.mGivenVisibleInsets.bottom;
4800 }
4801 final int touchFlags = flags &
4802 (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
4803 |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
4804 if (tmpRect.contains(x, y) || touchFlags == 0) {
4805 //Log.i(TAG, "Using this target!");
4806 if (!screenWasOff || (flags &
4807 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING) != 0) {
4808 mMotionTarget = child;
4809 } else {
4810 //Log.i(TAG, "Waking, skip!");
4811 mMotionTarget = null;
4812 }
4813 break;
4814 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004815
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004816 if ((flags & WindowManager.LayoutParams
4817 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
4818 child.mNextOutsideTouch = mOutsideTouchTargets;
4819 mOutsideTouchTargets = child;
4820 //Log.i(TAG, "Adding to outside target list: " + child);
4821 }
4822 }
4823
4824 // if there's an error window but it's not accepting
4825 // focus (typically because it is not yet visible) just
4826 // wait for it -- any other focused window may in fact
4827 // be in ANR state.
4828 if (topErrWindow != null && mMotionTarget != topErrWindow) {
4829 mMotionTarget = null;
4830 }
4831 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004832
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004833 target = mMotionTarget;
4834 }
4835 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004836
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004837 wakeupIfNeeded(target, eventType(nextMotion));
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004838
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004839 // Pointer events are a little different -- if there isn't a
4840 // target found for any event, then just drop it.
4841 return target != null ? target : SKIP_TARGET_TOKEN;
4842 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004843
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004844 boolean checkShouldDispatchKey(int keycode) {
4845 synchronized (this) {
4846 if (mPolicy.isAppSwitchKeyTqTiLwLi(keycode)) {
4847 mTimeToSwitch = 0;
4848 return true;
4849 }
4850 if (mTimeToSwitch != 0
4851 && mTimeToSwitch < SystemClock.uptimeMillis()) {
4852 return false;
4853 }
4854 return true;
4855 }
4856 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004857
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004858 void bindTargetWindowLocked(WindowState win,
4859 int pendingWhat, QueuedEvent pendingMotion) {
4860 synchronized (this) {
4861 bindTargetWindowLockedLocked(win, pendingWhat, pendingMotion);
4862 }
4863 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004864
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004865 void bindTargetWindowLocked(WindowState win) {
4866 synchronized (this) {
4867 bindTargetWindowLockedLocked(win, RETURN_NOTHING, null);
4868 }
4869 }
4870
4871 void bindTargetWindowLockedLocked(WindowState win,
4872 int pendingWhat, QueuedEvent pendingMotion) {
4873 mLastWin = win;
4874 mLastBinder = win.mClient.asBinder();
4875 mFinished = false;
4876 if (pendingMotion != null) {
4877 final Session s = win.mSession;
4878 if (pendingWhat == RETURN_PENDING_POINTER) {
4879 releasePendingPointerLocked(s);
4880 s.mPendingPointerMove = pendingMotion;
4881 s.mPendingPointerWindow = win;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004882 if (DEBUG_INPUT) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004883 "bindTargetToWindow " + s.mPendingPointerMove);
4884 } else if (pendingWhat == RETURN_PENDING_TRACKBALL) {
4885 releasePendingTrackballLocked(s);
4886 s.mPendingTrackballMove = pendingMotion;
4887 s.mPendingTrackballWindow = win;
4888 }
4889 }
4890 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004891
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004892 void releasePendingPointerLocked(Session s) {
4893 if (DEBUG_INPUT) Log.v(TAG,
4894 "releasePendingPointer " + s.mPendingPointerMove);
4895 if (s.mPendingPointerMove != null) {
4896 mQueue.recycleEvent(s.mPendingPointerMove);
4897 s.mPendingPointerMove = null;
4898 }
4899 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004900
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004901 void releasePendingTrackballLocked(Session s) {
4902 if (s.mPendingTrackballMove != null) {
4903 mQueue.recycleEvent(s.mPendingTrackballMove);
4904 s.mPendingTrackballMove = null;
4905 }
4906 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004907
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004908 MotionEvent finishedKey(Session session, IWindow client, boolean force,
4909 int returnWhat) {
4910 if (DEBUG_INPUT) Log.v(
4911 TAG, "finishedKey: client=" + client + ", force=" + force);
4912
4913 if (client == null) {
4914 return null;
4915 }
4916
4917 synchronized (this) {
4918 if (DEBUG_INPUT) Log.v(
4919 TAG, "finishedKey: client=" + client.asBinder()
4920 + ", force=" + force + ", last=" + mLastBinder
4921 + " (token=" + (mLastWin != null ? mLastWin.mToken : null) + ")");
4922
4923 QueuedEvent qev = null;
4924 WindowState win = null;
4925 if (returnWhat == RETURN_PENDING_POINTER) {
4926 qev = session.mPendingPointerMove;
4927 win = session.mPendingPointerWindow;
4928 session.mPendingPointerMove = null;
4929 session.mPendingPointerWindow = null;
4930 } else if (returnWhat == RETURN_PENDING_TRACKBALL) {
4931 qev = session.mPendingTrackballMove;
4932 win = session.mPendingTrackballWindow;
4933 session.mPendingTrackballMove = null;
4934 session.mPendingTrackballWindow = null;
4935 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004936
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004937 if (mLastBinder == client.asBinder()) {
4938 if (DEBUG_INPUT) Log.v(
4939 TAG, "finishedKey: last paused="
4940 + ((mLastWin != null) ? mLastWin.mToken.paused : "null"));
4941 if (mLastWin != null && (!mLastWin.mToken.paused || force
4942 || !mEventDispatching)) {
4943 doFinishedKeyLocked(false);
4944 } else {
4945 // Make sure to wake up anyone currently waiting to
4946 // dispatch a key, so they can re-evaluate their
4947 // current situation.
4948 mFinished = true;
4949 notifyAll();
4950 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004951 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004953 if (qev != null) {
4954 MotionEvent res = (MotionEvent)qev.event;
4955 if (DEBUG_INPUT) Log.v(TAG,
4956 "Returning pending motion: " + res);
4957 mQueue.recycleEvent(qev);
4958 if (win != null && returnWhat == RETURN_PENDING_POINTER) {
4959 res.offsetLocation(-win.mFrame.left, -win.mFrame.top);
4960 }
4961 return res;
4962 }
4963 return null;
4964 }
4965 }
4966
4967 void tickle() {
4968 synchronized (this) {
4969 notifyAll();
4970 }
4971 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07004972
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004973 void handleNewWindowLocked(WindowState newWindow) {
4974 if (!newWindow.canReceiveKeys()) {
4975 return;
4976 }
4977 synchronized (this) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004978 if (DEBUG_INPUT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004979 TAG, "New key dispatch window: win="
4980 + newWindow.mClient.asBinder()
4981 + ", last=" + mLastBinder
4982 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
4983 + "), finished=" + mFinished + ", paused="
4984 + newWindow.mToken.paused);
4985
4986 // Displaying a window implicitly causes dispatching to
4987 // be unpaused. (This is to protect against bugs if someone
4988 // pauses dispatching but forgets to resume.)
4989 newWindow.mToken.paused = false;
4990
4991 mGotFirstWindow = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004992
4993 if ((newWindow.mAttrs.flags & FLAG_SYSTEM_ERROR) != 0) {
4994 if (DEBUG_INPUT) Log.v(TAG,
4995 "New SYSTEM_ERROR window; resetting state");
4996 mLastWin = null;
4997 mLastBinder = null;
4998 mMotionTarget = null;
4999 mFinished = true;
5000 } else if (mLastWin != null) {
5001 // If the new window is above the window we are
5002 // waiting on, then stop waiting and let key dispatching
5003 // start on the new guy.
5004 if (DEBUG_INPUT) Log.v(
5005 TAG, "Last win layer=" + mLastWin.mLayer
5006 + ", new win layer=" + newWindow.mLayer);
5007 if (newWindow.mLayer >= mLastWin.mLayer) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005008 // The new window is above the old; finish pending input to the last
5009 // window and start directing it to the new one.
5010 mLastWin.mToken.paused = false;
5011 doFinishedKeyLocked(true); // does a notifyAll()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005012 }
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005013 // Either the new window is lower, so there is no need to wake key waiters,
5014 // or we just finished key input to the previous window, which implicitly
5015 // notified the key waiters. In both cases, we don't need to issue the
5016 // notification here.
5017 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005018 }
5019
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005020 // Now that we've put a new window state in place, make the event waiter
5021 // take notice and retarget its attentions.
5022 notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005023 }
5024 }
5025
5026 void pauseDispatchingLocked(WindowToken token) {
5027 synchronized (this)
5028 {
5029 if (DEBUG_INPUT) Log.v(TAG, "Pausing WindowToken " + token);
5030 token.paused = true;
5031
5032 /*
5033 if (mLastWin != null && !mFinished && mLastWin.mBaseLayer <= layer) {
5034 mPaused = true;
5035 } else {
5036 if (mLastWin == null) {
Jean-Baptiste Queru843ef362009-05-20 11:28:04 -07005037 Log.i(TAG, "Key dispatching not paused: no last window.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005038 } else if (mFinished) {
Jean-Baptiste Queru843ef362009-05-20 11:28:04 -07005039 Log.i(TAG, "Key dispatching not paused: finished last key.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005040 } else {
Jean-Baptiste Queru843ef362009-05-20 11:28:04 -07005041 Log.i(TAG, "Key dispatching not paused: window in higher layer.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005042 }
5043 }
5044 */
5045 }
5046 }
5047
5048 void resumeDispatchingLocked(WindowToken token) {
5049 synchronized (this) {
5050 if (token.paused) {
5051 if (DEBUG_INPUT) Log.v(
5052 TAG, "Resuming WindowToken " + token
5053 + ", last=" + mLastBinder
5054 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
5055 + "), finished=" + mFinished + ", paused="
5056 + token.paused);
5057 token.paused = false;
5058 if (mLastWin != null && mLastWin.mToken == token && mFinished) {
5059 doFinishedKeyLocked(true);
5060 } else {
5061 notifyAll();
5062 }
5063 }
5064 }
5065 }
5066
5067 void setEventDispatchingLocked(boolean enabled) {
5068 synchronized (this) {
5069 mEventDispatching = enabled;
5070 notifyAll();
5071 }
5072 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005074 void appSwitchComing() {
5075 synchronized (this) {
5076 // Don't wait for more than .5 seconds for app to finish
5077 // processing the pending events.
5078 long now = SystemClock.uptimeMillis() + 500;
5079 if (DEBUG_INPUT) Log.v(TAG, "appSwitchComing: " + now);
5080 if (mTimeToSwitch == 0 || now < mTimeToSwitch) {
5081 mTimeToSwitch = now;
5082 }
5083 notifyAll();
5084 }
5085 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005086
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005087 private final void doFinishedKeyLocked(boolean doRecycle) {
5088 if (mLastWin != null) {
5089 releasePendingPointerLocked(mLastWin.mSession);
5090 releasePendingTrackballLocked(mLastWin.mSession);
5091 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005093 if (mLastWin == null || !mLastWin.mToken.paused
5094 || !mLastWin.isVisibleLw()) {
5095 // If the current window has been paused, we aren't -really-
5096 // finished... so let the waiters still wait.
5097 mLastWin = null;
5098 mLastBinder = null;
5099 }
5100 mFinished = true;
5101 notifyAll();
5102 }
5103 }
5104
5105 private class KeyQ extends KeyInputQueue
5106 implements KeyInputQueue.FilterCallback {
5107 PowerManager.WakeLock mHoldingScreen;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005109 KeyQ() {
5110 super(mContext);
5111 PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
5112 mHoldingScreen = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
5113 "KEEP_SCREEN_ON_FLAG");
5114 mHoldingScreen.setReferenceCounted(false);
5115 }
5116
5117 @Override
5118 boolean preprocessEvent(InputDevice device, RawInputEvent event) {
5119 if (mPolicy.preprocessInputEventTq(event)) {
5120 return true;
5121 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005123 switch (event.type) {
5124 case RawInputEvent.EV_KEY: {
5125 // XXX begin hack
5126 if (DEBUG) {
5127 if (event.keycode == KeyEvent.KEYCODE_G) {
5128 if (event.value != 0) {
5129 // G down
5130 mPolicy.screenTurnedOff(WindowManagerPolicy.OFF_BECAUSE_OF_USER);
5131 }
5132 return false;
5133 }
5134 if (event.keycode == KeyEvent.KEYCODE_D) {
5135 if (event.value != 0) {
5136 //dump();
5137 }
5138 return false;
5139 }
5140 }
5141 // XXX end hack
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005143 boolean screenIsOff = !mPowerManager.screenIsOn();
5144 boolean screenIsDim = !mPowerManager.screenIsBright();
5145 int actions = mPolicy.interceptKeyTq(event, !screenIsOff);
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005147 if ((actions & WindowManagerPolicy.ACTION_GO_TO_SLEEP) != 0) {
5148 mPowerManager.goToSleep(event.when);
5149 }
5150
5151 if (screenIsOff) {
5152 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5153 }
5154 if (screenIsDim) {
5155 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5156 }
5157 if ((actions & WindowManagerPolicy.ACTION_POKE_USER_ACTIVITY) != 0) {
5158 mPowerManager.userActivity(event.when, false,
Jean-Baptiste Queru843ef362009-05-20 11:28:04 -07005159 LocalPowerManager.BUTTON_EVENT, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005160 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005162 if ((actions & WindowManagerPolicy.ACTION_PASS_TO_USER) != 0) {
5163 if (event.value != 0 && mPolicy.isAppSwitchKeyTqTiLwLi(event.keycode)) {
5164 filterQueue(this);
5165 mKeyWaiter.appSwitchComing();
5166 }
5167 return true;
5168 } else {
5169 return false;
5170 }
5171 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005173 case RawInputEvent.EV_REL: {
5174 boolean screenIsOff = !mPowerManager.screenIsOn();
5175 boolean screenIsDim = !mPowerManager.screenIsBright();
5176 if (screenIsOff) {
5177 if (!mPolicy.isWakeRelMovementTq(event.deviceId,
5178 device.classes, event)) {
5179 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
5180 return false;
5181 }
5182 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5183 }
5184 if (screenIsDim) {
5185 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5186 }
5187 return true;
5188 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005190 case RawInputEvent.EV_ABS: {
5191 boolean screenIsOff = !mPowerManager.screenIsOn();
5192 boolean screenIsDim = !mPowerManager.screenIsBright();
5193 if (screenIsOff) {
5194 if (!mPolicy.isWakeAbsMovementTq(event.deviceId,
5195 device.classes, event)) {
5196 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
5197 return false;
5198 }
5199 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5200 }
5201 if (screenIsDim) {
5202 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5203 }
5204 return true;
5205 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005207 default:
5208 return true;
5209 }
5210 }
5211
5212 public int filterEvent(QueuedEvent ev) {
5213 switch (ev.classType) {
5214 case RawInputEvent.CLASS_KEYBOARD:
5215 KeyEvent ke = (KeyEvent)ev.event;
5216 if (mPolicy.isMovementKeyTi(ke.getKeyCode())) {
5217 Log.w(TAG, "Dropping movement key during app switch: "
5218 + ke.getKeyCode() + ", action=" + ke.getAction());
5219 return FILTER_REMOVE;
5220 }
5221 return FILTER_ABORT;
5222 default:
5223 return FILTER_KEEP;
5224 }
5225 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005227 /**
5228 * Must be called with the main window manager lock held.
5229 */
5230 void setHoldScreenLocked(boolean holding) {
5231 boolean state = mHoldingScreen.isHeld();
5232 if (holding != state) {
5233 if (holding) {
5234 mHoldingScreen.acquire();
5235 } else {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005236 mPolicy.screenOnStoppedLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005237 mHoldingScreen.release();
5238 }
5239 }
5240 }
5241 };
5242
5243 public boolean detectSafeMode() {
5244 mSafeMode = mPolicy.detectSafeMode();
5245 return mSafeMode;
5246 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005248 public void systemReady() {
5249 mPolicy.systemReady();
5250 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005252 private final class InputDispatcherThread extends Thread {
5253 // Time to wait when there is nothing to do: 9999 seconds.
5254 static final int LONG_WAIT=9999*1000;
5255
5256 public InputDispatcherThread() {
5257 super("InputDispatcher");
5258 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005260 @Override
5261 public void run() {
5262 while (true) {
5263 try {
5264 process();
5265 } catch (Exception e) {
5266 Log.e(TAG, "Exception in input dispatcher", e);
5267 }
5268 }
5269 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005271 private void process() {
5272 android.os.Process.setThreadPriority(
5273 android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005274
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005275 // The last key event we saw
5276 KeyEvent lastKey = null;
5277
5278 // Last keydown time for auto-repeating keys
5279 long lastKeyTime = SystemClock.uptimeMillis();
5280 long nextKeyTime = lastKeyTime+LONG_WAIT;
5281
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005282 // How many successive repeats we generated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005283 int keyRepeatCount = 0;
5284
5285 // Need to report that configuration has changed?
5286 boolean configChanged = false;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005288 while (true) {
5289 long curTime = SystemClock.uptimeMillis();
5290
5291 if (DEBUG_INPUT) Log.v(
5292 TAG, "Waiting for next key: now=" + curTime
5293 + ", repeat @ " + nextKeyTime);
5294
5295 // Retrieve next event, waiting only as long as the next
5296 // repeat timeout. If the configuration has changed, then
5297 // don't wait at all -- we'll report the change as soon as
5298 // we have processed all events.
5299 QueuedEvent ev = mQueue.getEvent(
5300 (int)((!configChanged && curTime < nextKeyTime)
5301 ? (nextKeyTime-curTime) : 0));
5302
5303 if (DEBUG_INPUT && ev != null) Log.v(
5304 TAG, "Event: type=" + ev.classType + " data=" + ev.event);
5305
5306 try {
5307 if (ev != null) {
5308 curTime = ev.when;
5309 int eventType;
5310 if (ev.classType == RawInputEvent.CLASS_TOUCHSCREEN) {
5311 eventType = eventType((MotionEvent)ev.event);
5312 } else if (ev.classType == RawInputEvent.CLASS_KEYBOARD ||
5313 ev.classType == RawInputEvent.CLASS_TRACKBALL) {
5314 eventType = LocalPowerManager.BUTTON_EVENT;
5315 } else {
5316 eventType = LocalPowerManager.OTHER_EVENT;
5317 }
Dianne Hackborn65c8e112009-03-31 12:11:48 -07005318 try {
Jean-Baptiste Queru843ef362009-05-20 11:28:04 -07005319 long now = SystemClock.uptimeMillis();
5320
5321 if ((now - mLastBatteryStatsCallTime)
5322 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
5323 mLastBatteryStatsCallTime = now;
5324 mBatteryStats.noteInputEvent();
5325 }
Dianne Hackborn65c8e112009-03-31 12:11:48 -07005326 } catch (RemoteException e) {
5327 // Ignore
5328 }
Jean-Baptiste Queru843ef362009-05-20 11:28:04 -07005329 mPowerManager.userActivity(curTime, false, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005330 switch (ev.classType) {
5331 case RawInputEvent.CLASS_KEYBOARD:
5332 KeyEvent ke = (KeyEvent)ev.event;
5333 if (ke.isDown()) {
5334 lastKey = ke;
5335 keyRepeatCount = 0;
5336 lastKeyTime = curTime;
5337 nextKeyTime = lastKeyTime
5338 + KEY_REPEAT_FIRST_DELAY;
5339 if (DEBUG_INPUT) Log.v(
5340 TAG, "Received key down: first repeat @ "
5341 + nextKeyTime);
5342 } else {
5343 lastKey = null;
5344 // Arbitrary long timeout.
5345 lastKeyTime = curTime;
5346 nextKeyTime = curTime + LONG_WAIT;
5347 if (DEBUG_INPUT) Log.v(
5348 TAG, "Received key up: ignore repeat @ "
5349 + nextKeyTime);
5350 }
5351 dispatchKey((KeyEvent)ev.event, 0, 0);
5352 mQueue.recycleEvent(ev);
5353 break;
5354 case RawInputEvent.CLASS_TOUCHSCREEN:
5355 //Log.i(TAG, "Read next event " + ev);
5356 dispatchPointer(ev, (MotionEvent)ev.event, 0, 0);
5357 break;
5358 case RawInputEvent.CLASS_TRACKBALL:
5359 dispatchTrackball(ev, (MotionEvent)ev.event, 0, 0);
5360 break;
5361 case RawInputEvent.CLASS_CONFIGURATION_CHANGED:
5362 configChanged = true;
5363 break;
5364 default:
5365 mQueue.recycleEvent(ev);
5366 break;
5367 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005369 } else if (configChanged) {
5370 configChanged = false;
5371 sendNewConfiguration();
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005373 } else if (lastKey != null) {
5374 curTime = SystemClock.uptimeMillis();
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005376 // Timeout occurred while key was down. If it is at or
5377 // past the key repeat time, dispatch the repeat.
5378 if (DEBUG_INPUT) Log.v(
5379 TAG, "Key timeout: repeat=" + nextKeyTime
5380 + ", now=" + curTime);
5381 if (curTime < nextKeyTime) {
5382 continue;
5383 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005385 lastKeyTime = nextKeyTime;
5386 nextKeyTime = nextKeyTime + KEY_REPEAT_DELAY;
5387 keyRepeatCount++;
5388 if (DEBUG_INPUT) Log.v(
5389 TAG, "Key repeat: count=" + keyRepeatCount
5390 + ", next @ " + nextKeyTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07005391 dispatchKey(KeyEvent.changeTimeRepeat(lastKey, curTime, keyRepeatCount), 0, 0);
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005393 } else {
5394 curTime = SystemClock.uptimeMillis();
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005396 lastKeyTime = curTime;
5397 nextKeyTime = curTime + LONG_WAIT;
5398 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005399
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005400 } catch (Exception e) {
5401 Log.e(TAG,
5402 "Input thread received uncaught exception: " + e, e);
5403 }
5404 }
5405 }
5406 }
5407
5408 // -------------------------------------------------------------
5409 // Client Session State
5410 // -------------------------------------------------------------
5411
5412 private final class Session extends IWindowSession.Stub
5413 implements IBinder.DeathRecipient {
5414 final IInputMethodClient mClient;
5415 final IInputContext mInputContext;
5416 final int mUid;
5417 final int mPid;
Jean-Baptiste Queru843ef362009-05-20 11:28:04 -07005418 final String mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005419 SurfaceSession mSurfaceSession;
5420 int mNumWindow = 0;
5421 boolean mClientDead = false;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005423 /**
5424 * Current pointer move event being dispatched to client window... must
5425 * hold key lock to access.
5426 */
5427 QueuedEvent mPendingPointerMove;
5428 WindowState mPendingPointerWindow;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005430 /**
5431 * Current trackball move event being dispatched to client window... must
5432 * hold key lock to access.
5433 */
5434 QueuedEvent mPendingTrackballMove;
5435 WindowState mPendingTrackballWindow;
5436
5437 public Session(IInputMethodClient client, IInputContext inputContext) {
5438 mClient = client;
5439 mInputContext = inputContext;
5440 mUid = Binder.getCallingUid();
5441 mPid = Binder.getCallingPid();
Jean-Baptiste Queru843ef362009-05-20 11:28:04 -07005442 StringBuilder sb = new StringBuilder();
5443 sb.append("Session{");
5444 sb.append(Integer.toHexString(System.identityHashCode(this)));
5445 sb.append(" uid ");
5446 sb.append(mUid);
5447 sb.append("}");
5448 mStringName = sb.toString();
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005450 synchronized (mWindowMap) {
5451 if (mInputMethodManager == null && mHaveInputMethods) {
5452 IBinder b = ServiceManager.getService(
5453 Context.INPUT_METHOD_SERVICE);
5454 mInputMethodManager = IInputMethodManager.Stub.asInterface(b);
5455 }
5456 }
5457 long ident = Binder.clearCallingIdentity();
5458 try {
5459 // Note: it is safe to call in to the input method manager
5460 // here because we are not holding our lock.
5461 if (mInputMethodManager != null) {
5462 mInputMethodManager.addClient(client, inputContext,
5463 mUid, mPid);
5464 } else {
5465 client.setUsingInputMethod(false);
5466 }
5467 client.asBinder().linkToDeath(this, 0);
5468 } catch (RemoteException e) {
5469 // The caller has died, so we can just forget about this.
5470 try {
5471 if (mInputMethodManager != null) {
5472 mInputMethodManager.removeClient(client);
5473 }
5474 } catch (RemoteException ee) {
5475 }
5476 } finally {
5477 Binder.restoreCallingIdentity(ident);
5478 }
5479 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005481 @Override
5482 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
5483 throws RemoteException {
5484 try {
5485 return super.onTransact(code, data, reply, flags);
5486 } catch (RuntimeException e) {
5487 // Log all 'real' exceptions thrown to the caller
5488 if (!(e instanceof SecurityException)) {
5489 Log.e(TAG, "Window Session Crash", e);
5490 }
5491 throw e;
5492 }
5493 }
5494
5495 public void binderDied() {
5496 // Note: it is safe to call in to the input method manager
5497 // here because we are not holding our lock.
5498 try {
5499 if (mInputMethodManager != null) {
5500 mInputMethodManager.removeClient(mClient);
5501 }
5502 } catch (RemoteException e) {
5503 }
5504 synchronized(mWindowMap) {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005505 mClient.asBinder().unlinkToDeath(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005506 mClientDead = true;
5507 killSessionLocked();
5508 }
5509 }
5510
5511 public int add(IWindow window, WindowManager.LayoutParams attrs,
5512 int viewVisibility, Rect outContentInsets) {
5513 return addWindow(this, window, attrs, viewVisibility, outContentInsets);
5514 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005515
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005516 public void remove(IWindow window) {
5517 removeWindow(this, window);
5518 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005520 public int relayout(IWindow window, WindowManager.LayoutParams attrs,
5521 int requestedWidth, int requestedHeight, int viewFlags,
5522 boolean insetsPending, Rect outFrame, Rect outContentInsets,
5523 Rect outVisibleInsets, Surface outSurface) {
5524 return relayoutWindow(this, window, attrs,
5525 requestedWidth, requestedHeight, viewFlags, insetsPending,
5526 outFrame, outContentInsets, outVisibleInsets, outSurface);
5527 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005528
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005529 public void setTransparentRegion(IWindow window, Region region) {
5530 setTransparentRegionWindow(this, window, region);
5531 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005532
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005533 public void setInsets(IWindow window, int touchableInsets,
5534 Rect contentInsets, Rect visibleInsets) {
5535 setInsetsWindow(this, window, touchableInsets, contentInsets,
5536 visibleInsets);
5537 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005539 public void getDisplayFrame(IWindow window, Rect outDisplayFrame) {
5540 getWindowDisplayFrame(this, window, outDisplayFrame);
5541 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005543 public void finishDrawing(IWindow window) {
5544 if (localLOGV) Log.v(
5545 TAG, "IWindow finishDrawing called for " + window);
5546 finishDrawingWindow(this, window);
5547 }
5548
5549 public void finishKey(IWindow window) {
5550 if (localLOGV) Log.v(
5551 TAG, "IWindow finishKey called for " + window);
5552 mKeyWaiter.finishedKey(this, window, false,
5553 KeyWaiter.RETURN_NOTHING);
5554 }
5555
5556 public MotionEvent getPendingPointerMove(IWindow window) {
5557 if (localLOGV) Log.v(
5558 TAG, "IWindow getPendingMotionEvent called for " + window);
5559 return mKeyWaiter.finishedKey(this, window, false,
5560 KeyWaiter.RETURN_PENDING_POINTER);
5561 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005562
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005563 public MotionEvent getPendingTrackballMove(IWindow window) {
5564 if (localLOGV) Log.v(
5565 TAG, "IWindow getPendingMotionEvent called for " + window);
5566 return mKeyWaiter.finishedKey(this, window, false,
5567 KeyWaiter.RETURN_PENDING_TRACKBALL);
5568 }
5569
5570 public void setInTouchMode(boolean mode) {
5571 synchronized(mWindowMap) {
5572 mInTouchMode = mode;
5573 }
5574 }
5575
5576 public boolean getInTouchMode() {
5577 synchronized(mWindowMap) {
5578 return mInTouchMode;
5579 }
5580 }
5581
5582 public boolean performHapticFeedback(IWindow window, int effectId,
5583 boolean always) {
5584 synchronized(mWindowMap) {
5585 long ident = Binder.clearCallingIdentity();
5586 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005587 return mPolicy.performHapticFeedbackLw(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005588 windowForClientLocked(this, window), effectId, always);
5589 } finally {
5590 Binder.restoreCallingIdentity(ident);
5591 }
5592 }
5593 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005594
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005595 void windowAddedLocked() {
5596 if (mSurfaceSession == null) {
5597 if (localLOGV) Log.v(
5598 TAG, "First window added to " + this + ", creating SurfaceSession");
5599 mSurfaceSession = new SurfaceSession();
5600 mSessions.add(this);
5601 }
5602 mNumWindow++;
5603 }
5604
5605 void windowRemovedLocked() {
5606 mNumWindow--;
5607 killSessionLocked();
5608 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005609
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005610 void killSessionLocked() {
5611 if (mNumWindow <= 0 && mClientDead) {
5612 mSessions.remove(this);
5613 if (mSurfaceSession != null) {
5614 if (localLOGV) Log.v(
5615 TAG, "Last window removed from " + this
5616 + ", destroying " + mSurfaceSession);
5617 try {
5618 mSurfaceSession.kill();
5619 } catch (Exception e) {
5620 Log.w(TAG, "Exception thrown when killing surface session "
5621 + mSurfaceSession + " in session " + this
5622 + ": " + e.toString());
5623 }
5624 mSurfaceSession = null;
5625 }
5626 }
5627 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005628
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005629 void dump(PrintWriter pw, String prefix) {
Jean-Baptiste Queru843ef362009-05-20 11:28:04 -07005630 pw.print(prefix); pw.print("mNumWindow="); pw.print(mNumWindow);
5631 pw.print(" mClientDead="); pw.print(mClientDead);
5632 pw.print(" mSurfaceSession="); pw.println(mSurfaceSession);
5633 if (mPendingPointerWindow != null || mPendingPointerMove != null) {
5634 pw.print(prefix);
5635 pw.print("mPendingPointerWindow="); pw.print(mPendingPointerWindow);
5636 pw.print(" mPendingPointerMove="); pw.println(mPendingPointerMove);
5637 }
5638 if (mPendingTrackballWindow != null || mPendingTrackballMove != null) {
5639 pw.print(prefix);
5640 pw.print("mPendingTrackballWindow="); pw.print(mPendingTrackballWindow);
5641 pw.print(" mPendingTrackballMove="); pw.println(mPendingTrackballMove);
5642 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005643 }
5644
5645 @Override
5646 public String toString() {
Jean-Baptiste Queru843ef362009-05-20 11:28:04 -07005647 return mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005648 }
5649 }
5650
5651 // -------------------------------------------------------------
5652 // Client Window State
5653 // -------------------------------------------------------------
5654
5655 private final class WindowState implements WindowManagerPolicy.WindowState {
5656 final Session mSession;
5657 final IWindow mClient;
5658 WindowToken mToken;
The Android Open Source Project10592532009-03-18 17:39:46 -07005659 WindowToken mRootToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005660 AppWindowToken mAppToken;
5661 AppWindowToken mTargetAppToken;
5662 final WindowManager.LayoutParams mAttrs = new WindowManager.LayoutParams();
5663 final DeathRecipient mDeathRecipient;
5664 final WindowState mAttachedWindow;
5665 final ArrayList mChildWindows = new ArrayList();
5666 final int mBaseLayer;
5667 final int mSubLayer;
5668 final boolean mLayoutAttached;
5669 final boolean mIsImWindow;
5670 int mViewVisibility;
5671 boolean mPolicyVisibility = true;
5672 boolean mPolicyVisibilityAfterAnim = true;
5673 boolean mAppFreezing;
5674 Surface mSurface;
5675 boolean mAttachedHidden; // is our parent window hidden?
5676 boolean mLastHidden; // was this window last hidden?
5677 int mRequestedWidth;
5678 int mRequestedHeight;
5679 int mLastRequestedWidth;
5680 int mLastRequestedHeight;
5681 int mReqXPos;
5682 int mReqYPos;
5683 int mLayer;
5684 int mAnimLayer;
5685 int mLastLayer;
5686 boolean mHaveFrame;
5687
5688 WindowState mNextOutsideTouch;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005689
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005690 // Actual frame shown on-screen (may be modified by animation)
5691 final Rect mShownFrame = new Rect();
5692 final Rect mLastShownFrame = new Rect();
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005693
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005694 /**
5695 * Insets that determine the actually visible area
5696 */
5697 final Rect mVisibleInsets = new Rect();
5698 final Rect mLastVisibleInsets = new Rect();
5699 boolean mVisibleInsetsChanged;
5700
5701 /**
5702 * Insets that are covered by system windows
5703 */
5704 final Rect mContentInsets = new Rect();
5705 final Rect mLastContentInsets = new Rect();
5706 boolean mContentInsetsChanged;
5707
5708 /**
5709 * Set to true if we are waiting for this window to receive its
5710 * given internal insets before laying out other windows based on it.
5711 */
5712 boolean mGivenInsetsPending;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005713
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005714 /**
5715 * These are the content insets that were given during layout for
5716 * this window, to be applied to windows behind it.
5717 */
5718 final Rect mGivenContentInsets = new Rect();
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005719
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005720 /**
5721 * These are the visible insets that were given during layout for
5722 * this window, to be applied to windows behind it.
5723 */
5724 final Rect mGivenVisibleInsets = new Rect();
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005725
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005726 /**
5727 * Flag indicating whether the touchable region should be adjusted by
5728 * the visible insets; if false the area outside the visible insets is
5729 * NOT touchable, so we must use those to adjust the frame during hit
5730 * tests.
5731 */
5732 int mTouchableInsets = ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005733
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005734 // Current transformation being applied.
5735 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
5736 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
5737 float mHScale=1, mVScale=1;
5738 float mLastHScale=1, mLastVScale=1;
5739 final Matrix mTmpMatrix = new Matrix();
5740
5741 // "Real" frame that the application sees.
5742 final Rect mFrame = new Rect();
5743 final Rect mLastFrame = new Rect();
5744
5745 final Rect mContainingFrame = new Rect();
5746 final Rect mDisplayFrame = new Rect();
5747 final Rect mContentFrame = new Rect();
5748 final Rect mVisibleFrame = new Rect();
5749
5750 float mShownAlpha = 1;
5751 float mAlpha = 1;
5752 float mLastAlpha = 1;
5753
5754 // Set to true if, when the window gets displayed, it should perform
5755 // an enter animation.
5756 boolean mEnterAnimationPending;
5757
5758 // Currently running animation.
5759 boolean mAnimating;
5760 boolean mLocalAnimating;
5761 Animation mAnimation;
5762 boolean mAnimationIsEntrance;
5763 boolean mHasTransformation;
5764 boolean mHasLocalTransformation;
5765 final Transformation mTransformation = new Transformation();
5766
5767 // This is set after IWindowSession.relayout() has been called at
5768 // least once for the window. It allows us to detect the situation
5769 // where we don't yet have a surface, but should have one soon, so
5770 // we can give the window focus before waiting for the relayout.
5771 boolean mRelayoutCalled;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005772
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005773 // This is set after the Surface has been created but before the
5774 // window has been drawn. During this time the surface is hidden.
5775 boolean mDrawPending;
5776
5777 // This is set after the window has finished drawing for the first
5778 // time but before its surface is shown. The surface will be
5779 // displayed when the next layout is run.
5780 boolean mCommitDrawPending;
5781
5782 // This is set during the time after the window's drawing has been
5783 // committed, and before its surface is actually shown. It is used
5784 // to delay showing the surface until all windows in a token are ready
5785 // to be shown.
5786 boolean mReadyToShow;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005787
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005788 // Set when the window has been shown in the screen the first time.
5789 boolean mHasDrawn;
5790
5791 // Currently running an exit animation?
5792 boolean mExiting;
5793
5794 // Currently on the mDestroySurface list?
5795 boolean mDestroying;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005796
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005797 // Completely remove from window manager after exit animation?
5798 boolean mRemoveOnExit;
5799
5800 // Set when the orientation is changing and this window has not yet
5801 // been updated for the new orientation.
5802 boolean mOrientationChanging;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005803
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005804 // Is this window now (or just being) removed?
5805 boolean mRemoved;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005806
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005807 WindowState(Session s, IWindow c, WindowToken token,
5808 WindowState attachedWindow, WindowManager.LayoutParams a,
5809 int viewVisibility) {
5810 mSession = s;
5811 mClient = c;
5812 mToken = token;
5813 mAttrs.copyFrom(a);
5814 mViewVisibility = viewVisibility;
5815 DeathRecipient deathRecipient = new DeathRecipient();
5816 mAlpha = a.alpha;
5817 if (localLOGV) Log.v(
5818 TAG, "Window " + this + " client=" + c.asBinder()
5819 + " token=" + token + " (" + mAttrs.token + ")");
5820 try {
5821 c.asBinder().linkToDeath(deathRecipient, 0);
5822 } catch (RemoteException e) {
5823 mDeathRecipient = null;
5824 mAttachedWindow = null;
5825 mLayoutAttached = false;
5826 mIsImWindow = false;
5827 mBaseLayer = 0;
5828 mSubLayer = 0;
5829 return;
5830 }
5831 mDeathRecipient = deathRecipient;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005832
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005833 if ((mAttrs.type >= FIRST_SUB_WINDOW &&
5834 mAttrs.type <= LAST_SUB_WINDOW)) {
5835 // The multiplier here is to reserve space for multiple
5836 // windows in the same type layer.
5837 mBaseLayer = mPolicy.windowTypeToLayerLw(
5838 attachedWindow.mAttrs.type) * TYPE_LAYER_MULTIPLIER
5839 + TYPE_LAYER_OFFSET;
5840 mSubLayer = mPolicy.subWindowTypeToLayerLw(a.type);
5841 mAttachedWindow = attachedWindow;
5842 mAttachedWindow.mChildWindows.add(this);
5843 mLayoutAttached = mAttrs.type !=
5844 WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
5845 mIsImWindow = attachedWindow.mAttrs.type == TYPE_INPUT_METHOD
5846 || attachedWindow.mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
5847 } else {
5848 // The multiplier here is to reserve space for multiple
5849 // windows in the same type layer.
5850 mBaseLayer = mPolicy.windowTypeToLayerLw(a.type)
5851 * TYPE_LAYER_MULTIPLIER
5852 + TYPE_LAYER_OFFSET;
5853 mSubLayer = 0;
5854 mAttachedWindow = null;
5855 mLayoutAttached = false;
5856 mIsImWindow = mAttrs.type == TYPE_INPUT_METHOD
5857 || mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
5858 }
5859
5860 WindowState appWin = this;
5861 while (appWin.mAttachedWindow != null) {
5862 appWin = mAttachedWindow;
5863 }
5864 WindowToken appToken = appWin.mToken;
5865 while (appToken.appWindowToken == null) {
5866 WindowToken parent = mTokenMap.get(appToken.token);
5867 if (parent == null || appToken == parent) {
5868 break;
5869 }
5870 appToken = parent;
5871 }
The Android Open Source Project10592532009-03-18 17:39:46 -07005872 mRootToken = appToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005873 mAppToken = appToken.appWindowToken;
5874
5875 mSurface = null;
5876 mRequestedWidth = 0;
5877 mRequestedHeight = 0;
5878 mLastRequestedWidth = 0;
5879 mLastRequestedHeight = 0;
5880 mReqXPos = 0;
5881 mReqYPos = 0;
5882 mLayer = 0;
5883 mAnimLayer = 0;
5884 mLastLayer = 0;
5885 }
5886
5887 void attach() {
5888 if (localLOGV) Log.v(
5889 TAG, "Attaching " + this + " token=" + mToken
5890 + ", list=" + mToken.windows);
5891 mSession.windowAddedLocked();
5892 }
5893
5894 public void computeFrameLw(Rect pf, Rect df, Rect cf, Rect vf) {
5895 mHaveFrame = true;
5896
Jean-Baptiste Querua8675f62009-07-29 14:25:07 -07005897 final Rect container = mContainingFrame;
5898 container.set(pf);
5899
5900 final Rect display = mDisplayFrame;
5901 display.set(df);
5902
5903 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
5904 container.intersect(mCompatibleScreenFrame);
5905 if ((mAttrs.flags & FLAG_LAYOUT_NO_LIMITS) == 0) {
5906 display.intersect(mCompatibleScreenFrame);
5907 }
5908 }
5909
5910 final int pw = container.right - container.left;
5911 final int ph = container.bottom - container.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005912
5913 int w,h;
5914 if ((mAttrs.flags & mAttrs.FLAG_SCALED) != 0) {
5915 w = mAttrs.width < 0 ? pw : mAttrs.width;
5916 h = mAttrs.height< 0 ? ph : mAttrs.height;
5917 } else {
5918 w = mAttrs.width == mAttrs.FILL_PARENT ? pw : mRequestedWidth;
5919 h = mAttrs.height== mAttrs.FILL_PARENT ? ph : mRequestedHeight;
5920 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005921
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005922 final Rect content = mContentFrame;
5923 content.set(cf);
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005925 final Rect visible = mVisibleFrame;
5926 visible.set(vf);
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005927
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005928 final Rect frame = mFrame;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005929
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005930 //System.out.println("In: w=" + w + " h=" + h + " container=" +
5931 // container + " x=" + mAttrs.x + " y=" + mAttrs.y);
5932
5933 Gravity.apply(mAttrs.gravity, w, h, container,
5934 (int) (mAttrs.x + mAttrs.horizontalMargin * pw),
5935 (int) (mAttrs.y + mAttrs.verticalMargin * ph), frame);
5936
5937 //System.out.println("Out: " + mFrame);
5938
5939 // Now make sure the window fits in the overall display.
5940 Gravity.applyDisplay(mAttrs.gravity, df, frame);
Jean-Baptiste Querua8675f62009-07-29 14:25:07 -07005941
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005942 // Make sure the content and visible frames are inside of the
5943 // final window frame.
5944 if (content.left < frame.left) content.left = frame.left;
5945 if (content.top < frame.top) content.top = frame.top;
5946 if (content.right > frame.right) content.right = frame.right;
5947 if (content.bottom > frame.bottom) content.bottom = frame.bottom;
5948 if (visible.left < frame.left) visible.left = frame.left;
5949 if (visible.top < frame.top) visible.top = frame.top;
5950 if (visible.right > frame.right) visible.right = frame.right;
5951 if (visible.bottom > frame.bottom) visible.bottom = frame.bottom;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005953 final Rect contentInsets = mContentInsets;
5954 contentInsets.left = content.left-frame.left;
5955 contentInsets.top = content.top-frame.top;
5956 contentInsets.right = frame.right-content.right;
5957 contentInsets.bottom = frame.bottom-content.bottom;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005958
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005959 final Rect visibleInsets = mVisibleInsets;
5960 visibleInsets.left = visible.left-frame.left;
5961 visibleInsets.top = visible.top-frame.top;
5962 visibleInsets.right = frame.right-visible.right;
5963 visibleInsets.bottom = frame.bottom-visible.bottom;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005964
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005965 if (localLOGV) {
5966 //if ("com.google.android.youtube".equals(mAttrs.packageName)
5967 // && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
5968 Log.v(TAG, "Resolving (mRequestedWidth="
5969 + mRequestedWidth + ", mRequestedheight="
5970 + mRequestedHeight + ") to" + " (pw=" + pw + ", ph=" + ph
5971 + "): frame=" + mFrame.toShortString()
5972 + " ci=" + contentInsets.toShortString()
5973 + " vi=" + visibleInsets.toShortString());
5974 //}
5975 }
5976 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07005977
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005978 public Rect getFrameLw() {
5979 return mFrame;
5980 }
5981
5982 public Rect getShownFrameLw() {
5983 return mShownFrame;
5984 }
5985
5986 public Rect getDisplayFrameLw() {
5987 return mDisplayFrame;
5988 }
5989
5990 public Rect getContentFrameLw() {
5991 return mContentFrame;
5992 }
5993
5994 public Rect getVisibleFrameLw() {
5995 return mVisibleFrame;
5996 }
5997
5998 public boolean getGivenInsetsPendingLw() {
5999 return mGivenInsetsPending;
6000 }
6001
6002 public Rect getGivenContentInsetsLw() {
6003 return mGivenContentInsets;
6004 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07006005
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006006 public Rect getGivenVisibleInsetsLw() {
6007 return mGivenVisibleInsets;
6008 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07006009
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006010 public WindowManager.LayoutParams getAttrs() {
6011 return mAttrs;
6012 }
6013
6014 public int getSurfaceLayer() {
6015 return mLayer;
6016 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07006017
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006018 public IApplicationToken getAppToken() {
6019 return mAppToken != null ? mAppToken.appToken : null;
6020 }
6021
6022 public boolean hasAppShownWindows() {
6023 return mAppToken != null ? mAppToken.firstWindowDrawn : false;
6024 }
6025
6026 public boolean hasAppStartingIcon() {
6027 return mAppToken != null ? (mAppToken.startingData != null) : false;
6028 }
6029
6030 public WindowManagerPolicy.WindowState getAppStartingWindow() {
6031 return mAppToken != null ? mAppToken.startingWindow : null;
6032 }
6033
6034 public void setAnimation(Animation anim) {
6035 if (localLOGV) Log.v(
6036 TAG, "Setting animation in " + this + ": " + anim);
6037 mAnimating = false;
6038 mLocalAnimating = false;
6039 mAnimation = anim;
6040 mAnimation.restrictDuration(MAX_ANIMATION_DURATION);
6041 mAnimation.scaleCurrentDuration(mWindowAnimationScale);
6042 }
6043
6044 public void clearAnimation() {
6045 if (mAnimation != null) {
6046 mAnimating = true;
6047 mLocalAnimating = false;
6048 mAnimation = null;
6049 }
6050 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07006051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006052 Surface createSurfaceLocked() {
6053 if (mSurface == null) {
6054 mDrawPending = true;
6055 mCommitDrawPending = false;
6056 mReadyToShow = false;
6057 if (mAppToken != null) {
6058 mAppToken.allDrawn = false;
6059 }
6060
6061 int flags = 0;
6062 if (mAttrs.memoryType == MEMORY_TYPE_HARDWARE) {
6063 flags |= Surface.HARDWARE;
6064 } else if (mAttrs.memoryType == MEMORY_TYPE_GPU) {
6065 flags |= Surface.GPU;
6066 } else if (mAttrs.memoryType == MEMORY_TYPE_PUSH_BUFFERS) {
6067 flags |= Surface.PUSH_BUFFERS;
6068 }
6069
6070 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
6071 flags |= Surface.SECURE;
6072 }
6073 if (DEBUG_VISIBILITY) Log.v(
6074 TAG, "Creating surface in session "
6075 + mSession.mSurfaceSession + " window " + this
6076 + " w=" + mFrame.width()
6077 + " h=" + mFrame.height() + " format="
6078 + mAttrs.format + " flags=" + flags);
6079
6080 int w = mFrame.width();
6081 int h = mFrame.height();
6082 if ((mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
6083 // for a scaled surface, we always want the requested
6084 // size.
6085 w = mRequestedWidth;
6086 h = mRequestedHeight;
6087 }
6088
6089 try {
6090 mSurface = new Surface(
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07006091 mSession.mSurfaceSession, mSession.mPid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006092 0, w, h, mAttrs.format, flags);
6093 } catch (Surface.OutOfResourcesException e) {
6094 Log.w(TAG, "OutOfResourcesException creating surface");
6095 reclaimSomeSurfaceMemoryLocked(this, "create");
6096 return null;
6097 } catch (Exception e) {
6098 Log.e(TAG, "Exception creating surface", e);
6099 return null;
6100 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07006101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006102 if (localLOGV) Log.v(
6103 TAG, "Got surface: " + mSurface
6104 + ", set left=" + mFrame.left + " top=" + mFrame.top
6105 + ", animLayer=" + mAnimLayer);
6106 if (SHOW_TRANSACTIONS) {
6107 Log.i(TAG, ">>> OPEN TRANSACTION");
6108 Log.i(TAG, " SURFACE " + mSurface + ": CREATE ("
6109 + mAttrs.getTitle() + ") pos=(" +
6110 mFrame.left + "," + mFrame.top + ") (" +
6111 mFrame.width() + "x" + mFrame.height() + "), layer=" +
6112 mAnimLayer + " HIDE");
6113 }
6114 Surface.openTransaction();
6115 try {
6116 try {
6117 mSurface.setPosition(mFrame.left, mFrame.top);
6118 mSurface.setLayer(mAnimLayer);
6119 mSurface.hide();
6120 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
6121 mSurface.setFlags(Surface.SURFACE_DITHER,
6122 Surface.SURFACE_DITHER);
6123 }
6124 } catch (RuntimeException e) {
6125 Log.w(TAG, "Error creating surface in " + w, e);
6126 reclaimSomeSurfaceMemoryLocked(this, "create-init");
6127 }
6128 mLastHidden = true;
6129 } finally {
6130 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
6131 Surface.closeTransaction();
6132 }
6133 if (localLOGV) Log.v(
6134 TAG, "Created surface " + this);
6135 }
6136 return mSurface;
6137 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07006138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006139 void destroySurfaceLocked() {
6140 // Window is no longer on-screen, so can no longer receive
6141 // key events... if we were waiting for it to finish
6142 // handling a key event, the wait is over!
6143 mKeyWaiter.finishedKey(mSession, mClient, true,
6144 KeyWaiter.RETURN_NOTHING);
6145 mKeyWaiter.releasePendingPointerLocked(mSession);
6146 mKeyWaiter.releasePendingTrackballLocked(mSession);
6147
6148 if (mAppToken != null && this == mAppToken.startingWindow) {
6149 mAppToken.startingDisplayed = false;
6150 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07006151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006152 if (localLOGV) Log.v(
6153 TAG, "Window " + this
6154 + " destroying surface " + mSurface + ", session " + mSession);
6155 if (mSurface != null) {
6156 try {
6157 if (SHOW_TRANSACTIONS) {
6158 RuntimeException ex = new RuntimeException();
6159 ex.fillInStackTrace();
6160 Log.i(TAG, " SURFACE " + mSurface + ": DESTROY ("
6161 + mAttrs.getTitle() + ")", ex);
6162 }
6163 mSurface.clear();
6164 } catch (RuntimeException e) {
6165 Log.w(TAG, "Exception thrown when destroying Window " + this
6166 + " surface " + mSurface + " session " + mSession
6167 + ": " + e.toString());
6168 }
6169 mSurface = null;
6170 mDrawPending = false;
6171 mCommitDrawPending = false;
6172 mReadyToShow = false;
6173
6174 int i = mChildWindows.size();
6175 while (i > 0) {
6176 i--;
6177 WindowState c = (WindowState)mChildWindows.get(i);
6178 c.mAttachedHidden = true;
6179 }
6180 }
6181 }
6182
6183 boolean finishDrawingLocked() {
6184 if (mDrawPending) {
6185 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.v(
6186 TAG, "finishDrawingLocked: " + mSurface);
6187 mCommitDrawPending = true;
6188 mDrawPending = false;
6189 return true;
6190 }
6191 return false;
6192 }
6193
6194 // This must be called while inside a transaction.
6195 void commitFinishDrawingLocked(long currentTime) {
6196 //Log.i(TAG, "commitFinishDrawingLocked: " + mSurface);
6197 if (!mCommitDrawPending) {
6198 return;
6199 }
6200 mCommitDrawPending = false;
6201 mReadyToShow = true;
6202 final boolean starting = mAttrs.type == TYPE_APPLICATION_STARTING;
6203 final AppWindowToken atoken = mAppToken;
6204 if (atoken == null || atoken.allDrawn || starting) {
6205 performShowLocked();
6206 }
6207 }
6208
6209 // This must be called while inside a transaction.
6210 boolean performShowLocked() {
6211 if (DEBUG_VISIBILITY) {
6212 RuntimeException e = new RuntimeException();
6213 e.fillInStackTrace();
6214 Log.v(TAG, "performShow on " + this
6215 + ": readyToShow=" + mReadyToShow + " readyForDisplay=" + isReadyForDisplay()
6216 + " starting=" + (mAttrs.type == TYPE_APPLICATION_STARTING), e);
6217 }
6218 if (mReadyToShow && isReadyForDisplay()) {
6219 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.i(
6220 TAG, " SURFACE " + mSurface + ": SHOW (performShowLocked)");
6221 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + this
6222 + " during animation: policyVis=" + mPolicyVisibility
6223 + " attHidden=" + mAttachedHidden
6224 + " tok.hiddenRequested="
6225 + (mAppToken != null ? mAppToken.hiddenRequested : false)
6226 + " tok.idden="
6227 + (mAppToken != null ? mAppToken.hidden : false)
6228 + " animating=" + mAnimating
6229 + " tok animating="
6230 + (mAppToken != null ? mAppToken.animating : false));
6231 if (!showSurfaceRobustlyLocked(this)) {
6232 return false;
6233 }
6234 mLastAlpha = -1;
6235 mHasDrawn = true;
6236 mLastHidden = false;
6237 mReadyToShow = false;
6238 enableScreenIfNeededLocked();
6239
6240 applyEnterAnimationLocked(this);
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07006241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006242 int i = mChildWindows.size();
6243 while (i > 0) {
6244 i--;
6245 WindowState c = (WindowState)mChildWindows.get(i);
6246 if (c.mSurface != null && c.mAttachedHidden) {
6247 c.mAttachedHidden = false;
6248 c.performShowLocked();
6249 }
6250 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07006251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006252 if (mAttrs.type != TYPE_APPLICATION_STARTING
6253 && mAppToken != null) {
6254 mAppToken.firstWindowDrawn = true;
6255 if (mAnimation == null && mAppToken.startingData != null) {
6256 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
6257 + mToken
6258 + ": first real window is shown, no animation");
6259 mFinishedStarting.add(mAppToken);
6260 mH.sendEmptyMessage(H.FINISHED_STARTING);
6261 }
6262 mAppToken.updateReportedVisibilityLocked();
6263 }
6264 }
6265 return true;
6266 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07006267
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006268 // This must be called while inside a transaction. Returns true if
6269 // there is more animation to run.
6270 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
6271 if (!mDisplayFrozen) {
6272 // We will run animations as long as the display isn't frozen.
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07006273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006274 if (!mDrawPending && !mCommitDrawPending && mAnimation != null) {
6275 mHasTransformation = true;
6276 mHasLocalTransformation = true;
6277 if (!mLocalAnimating) {
6278 if (DEBUG_ANIM) Log.v(
6279 TAG, "Starting animation in " + this +
6280 " @ " + currentTime + ": ww=" + mFrame.width() + " wh=" + mFrame.height() +
6281 " dw=" + dw + " dh=" + dh + " scale=" + mWindowAnimationScale);
6282 mAnimation.initialize(mFrame.width(), mFrame.height(), dw, dh);
6283 mAnimation.setStartTime(currentTime);
6284 mLocalAnimating = true;
6285 mAnimating = true;
6286 }
6287 mTransformation.clear();
6288 final boolean more = mAnimation.getTransformation(
6289 currentTime, mTransformation);
6290 if (DEBUG_ANIM) Log.v(
6291 TAG, "Stepped animation in " + this +
6292 ": more=" + more + ", xform=" + mTransformation);
6293 if (more) {
6294 // we're not done!
6295 return true;
6296 }
6297 if (DEBUG_ANIM) Log.v(
6298 TAG, "Finished animation in " + this +
6299 " @ " + currentTime);
6300 mAnimation = null;
6301 //WindowManagerService.this.dump();
6302 }
6303 mHasLocalTransformation = false;
6304 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppToken != null
6305 && mAppToken.hasTransformation) {
6306 // When our app token is animating, we kind-of pretend like
6307 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
6308 // part of this check means that we will only do this if
6309 // our window is not currently exiting, or it is not
6310 // locally animating itself. The idea being that one that
6311 // is exiting and doing a local animation should be removed
6312 // once that animation is done.
6313 mAnimating = true;
6314 mHasTransformation = true;
6315 mTransformation.clear();
6316 return false;
6317 } else if (mHasTransformation) {
6318 // Little trick to get through the path below to act like
6319 // we have finished an animation.
6320 mAnimating = true;
6321 } else if (isAnimating()) {
6322 mAnimating = true;
6323 }
6324 } else if (mAnimation != null) {
6325 // If the display is frozen, and there is a pending animation,
6326 // clear it and make sure we run the cleanup code.
6327 mAnimating = true;
6328 mLocalAnimating = true;
6329 mAnimation = null;
6330 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07006331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006332 if (!mAnimating && !mLocalAnimating) {
6333 return false;
6334 }
6335
6336 if (DEBUG_ANIM) Log.v(
6337 TAG, "Animation done in " + this + ": exiting=" + mExiting
6338 + ", reportedVisible="
6339 + (mAppToken != null ? mAppToken.reportedVisible : false));
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07006340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006341 mAnimating = false;
6342 mLocalAnimating = false;
6343 mAnimation = null;
6344 mAnimLayer = mLayer;
6345 if (mIsImWindow) {
6346 mAnimLayer += mInputMethodAnimLayerAdjustment;
6347 }
6348 if (DEBUG_LAYERS) Log.v(TAG, "Stepping win " + this
6349 + " anim layer: " + mAnimLayer);
6350 mHasTransformation = false;
6351 mHasLocalTransformation = false;
6352 mPolicyVisibility = mPolicyVisibilityAfterAnim;
6353 mTransformation.clear();
6354 if (mHasDrawn
6355 && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
6356 && mAppToken != null
6357 && mAppToken.firstWindowDrawn
6358 && mAppToken.startingData != null) {
6359 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
6360 + mToken + ": first real window done animating");
6361 mFinishedStarting.add(mAppToken);
6362 mH.sendEmptyMessage(H.FINISHED_STARTING);
6363 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07006364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006365 finishExit();
6366
6367 if (mAppToken != null) {
6368 mAppToken.updateReportedVisibilityLocked();
6369 }
6370
6371 return false;
6372 }
6373
6374 void finishExit() {
6375 if (DEBUG_ANIM) Log.v(
6376 TAG, "finishExit in " + this
6377 + ": exiting=" + mExiting
6378 + " remove=" + mRemoveOnExit
6379 + " windowAnimating=" + isWindowAnimating());
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07006380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006381 final int N = mChildWindows.size();
6382 for (int i=0; i<N; i++) {
6383 ((WindowState)mChildWindows.get(i)).finishExit();
6384 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07006385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006386 if (!mExiting) {
6387 return;
6388 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07006389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006390 if (isWindowAnimating()) {
6391 return;
6392 }
6393
6394 if (localLOGV) Log.v(
6395 TAG, "Exit animation finished in " + this
6396 + ": remove=" + mRemoveOnExit);
6397 if (mSurface != null) {
6398 mDestroySurface.add(this);
6399 mDestroying = true;
6400 if (SHOW_TRANSACTIONS) Log.i(
6401 TAG, " SURFACE " + mSurface + ": HIDE (finishExit)");
6402 try {
6403 mSurface.hide();
6404 } catch (RuntimeException e) {
6405 Log.w(TAG, "Error hiding surface in " + this, e);
6406 }
6407 mLastHidden = true;
6408 mKeyWaiter.releasePendingPointerLocked(mSession);
6409 }
6410 mExiting = false;
6411 if (mRemoveOnExit) {
6412 mPendingRemove.add(this);
6413 mRemoveOnExit = false;
6414 }
6415 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07006416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006417 boolean isIdentityMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
6418 if (dsdx < .99999f || dsdx > 1.00001f) return false;
6419 if (dtdy < .99999f || dtdy > 1.00001f) return false;
6420 if (dtdx < -.000001f || dtdx > .000001f) return false;
6421 if (dsdy < -.000001f || dsdy > .000001f) return false;
6422 return true;
6423 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07006424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006425 void computeShownFrameLocked() {
6426 final boolean selfTransformation = mHasLocalTransformation;
6427 Transformation attachedTransformation =
6428 (mAttachedWindow != null && mAttachedWindow.mHasLocalTransformation)
6429 ? mAttachedWindow.mTransformation : null;
6430 Transformation appTransformation =
6431 (mAppToken != null && mAppToken.hasTransformation)
6432 ? mAppToken.transformation : null;
6433 if (selfTransformation || attachedTransformation != null
6434 || appTransformation != null) {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07006435 // cache often used attributes locally
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006436 final Rect frame = mFrame;
6437 final float tmpFloats[] = mTmpFloats;
6438 final Matrix tmpMatrix = mTmpMatrix;
6439
6440 // Compute the desired transformation.
6441 tmpMatrix.setTranslate(frame.left, frame.top);
6442 if (selfTransformation) {
6443 tmpMatrix.preConcat(mTransformation.getMatrix());
6444 }
6445 if (attachedTransformation != null) {
6446 tmpMatrix.preConcat(attachedTransformation.getMatrix());
6447 }
6448 if (appTransformation != null) {
6449 tmpMatrix.preConcat(appTransformation.getMatrix());
6450 }
6451
6452 // "convert" it into SurfaceFlinger's format
6453 // (a 2x2 matrix + an offset)
6454 // Here we must not transform the position of the surface
6455 // since it is already included in the transformation.
6456 //Log.i(TAG, "Transform: " + matrix);
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07006457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006458 tmpMatrix.getValues(tmpFloats);
6459 mDsDx = tmpFloats[Matrix.MSCALE_X];
6460 mDtDx = tmpFloats[Matrix.MSKEW_X];
6461 mDsDy = tmpFloats[Matrix.MSKEW_Y];
6462 mDtDy = tmpFloats[Matrix.MSCALE_Y];
6463 int x = (int)tmpFloats[Matrix.MTRANS_X];
6464 int y = (int)tmpFloats[Matrix.MTRANS_Y];
6465 int w = frame.width();
6466 int h = frame.height();
6467 mShownFrame.set(x, y, x+w, y+h);
6468
6469 // Now set the alpha... but because our current hardware
6470 // can't do alpha transformation on a non-opaque surface,
6471 // turn it off if we are running an animation that is also
6472 // transforming since it is more important to have that
6473 // animation be smooth.
6474 mShownAlpha = mAlpha;
6475 if (!mLimitedAlphaCompositing
6476 || (!PixelFormat.formatHasAlpha(mAttrs.format)
6477 || (isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
6478 && x == frame.left && y == frame.top))) {
6479 //Log.i(TAG, "Applying alpha transform");
6480 if (selfTransformation) {
6481 mShownAlpha *= mTransformation.getAlpha();
6482 }
6483 if (attachedTransformation != null) {
6484 mShownAlpha *= attachedTransformation.getAlpha();
6485 }
6486 if (appTransformation != null) {
6487 mShownAlpha *= appTransformation.getAlpha();
6488 }
6489 } else {
6490 //Log.i(TAG, "Not applying alpha transform");
6491 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07006492
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006493 if (localLOGV) Log.v(
6494 TAG, "Continuing animation in " + this +
6495 ": " + mShownFrame +
6496 ", alpha=" + mTransformation.getAlpha());
6497 return;
6498 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07006499
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006500 mShownFrame.set(mFrame);
6501 mShownAlpha = mAlpha;
6502 mDsDx = 1;
6503 mDtDx = 0;
6504 mDsDy = 0;
6505 mDtDy = 1;
6506 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07006507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006508 /**
6509 * Is this window visible? It is not visible if there is no
6510 * surface, or we are in the process of running an exit animation
6511 * that will remove the surface, or its app token has been hidden.
6512 */
6513 public boolean isVisibleLw() {
6514 final AppWindowToken atoken = mAppToken;
6515 return mSurface != null && mPolicyVisibility && !mAttachedHidden
6516 && (atoken == null || !atoken.hiddenRequested)
6517 && !mExiting && !mDestroying;
6518 }
6519
6520 /**
6521 * Is this window visible, ignoring its app token? It is not visible
6522 * if there is no surface, or we are in the process of running an exit animation
6523 * that will remove the surface.
6524 */
6525 public boolean isWinVisibleLw() {
6526 final AppWindowToken atoken = mAppToken;
6527 return mSurface != null && mPolicyVisibility && !mAttachedHidden
6528 && (atoken == null || !atoken.hiddenRequested || atoken.animating)
6529 && !mExiting && !mDestroying;
6530 }
6531
6532 /**
6533 * The same as isVisible(), but follows the current hidden state of
6534 * the associated app token, not the pending requested hidden state.
6535 */
6536 boolean isVisibleNow() {
6537 return mSurface != null && mPolicyVisibility && !mAttachedHidden
The Android Open Source Project10592532009-03-18 17:39:46 -07006538 && !mRootToken.hidden && !mExiting && !mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006539 }
6540
6541 /**
6542 * Same as isVisible(), but we also count it as visible between the
6543 * call to IWindowSession.add() and the first relayout().
6544 */
6545 boolean isVisibleOrAdding() {
6546 final AppWindowToken atoken = mAppToken;
6547 return (mSurface != null
6548 || (!mRelayoutCalled && mViewVisibility == View.VISIBLE))
6549 && mPolicyVisibility && !mAttachedHidden
6550 && (atoken == null || !atoken.hiddenRequested)
6551 && !mExiting && !mDestroying;
6552 }
6553
6554 /**
6555 * Is this window currently on-screen? It is on-screen either if it
6556 * is visible or it is currently running an animation before no longer
6557 * being visible.
6558 */
6559 boolean isOnScreen() {
6560 final AppWindowToken atoken = mAppToken;
6561 if (atoken != null) {
6562 return mSurface != null && mPolicyVisibility && !mDestroying
6563 && ((!mAttachedHidden && !atoken.hiddenRequested)
6564 || mAnimating || atoken.animating);
6565 } else {
6566 return mSurface != null && mPolicyVisibility && !mDestroying
6567 && (!mAttachedHidden || mAnimating);
6568 }
6569 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07006570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006571 /**
6572 * Like isOnScreen(), but we don't return true if the window is part
6573 * of a transition that has not yet been started.
6574 */
6575 boolean isReadyForDisplay() {
6576 final AppWindowToken atoken = mAppToken;
6577 final boolean animating = atoken != null ? atoken.animating : false;
6578 return mSurface != null && mPolicyVisibility && !mDestroying
The Android Open Source Project10592532009-03-18 17:39:46 -07006579 && ((!mAttachedHidden && !mRootToken.hidden)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006580 || mAnimating || animating);
6581 }
6582
6583 /** Is the window or its container currently animating? */
6584 boolean isAnimating() {
6585 final WindowState attached = mAttachedWindow;
6586 final AppWindowToken atoken = mAppToken;
6587 return mAnimation != null
6588 || (attached != null && attached.mAnimation != null)
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07006589 || (atoken != null &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006590 (atoken.animation != null
6591 || atoken.inPendingTransaction));
6592 }
6593
6594 /** Is this window currently animating? */
6595 boolean isWindowAnimating() {
6596 return mAnimation != null;
6597 }
6598
6599 /**
6600 * Like isOnScreen, but returns false if the surface hasn't yet
6601 * been drawn.
6602 */
6603 public boolean isDisplayedLw() {
6604 final AppWindowToken atoken = mAppToken;
6605 return mSurface != null && mPolicyVisibility && !mDestroying
6606 && !mDrawPending && !mCommitDrawPending
6607 && ((!mAttachedHidden &&
6608 (atoken == null || !atoken.hiddenRequested))
6609 || mAnimating);
6610 }
6611
6612 public boolean fillsScreenLw(int screenWidth, int screenHeight,
6613 boolean shownFrame, boolean onlyOpaque) {
6614 if (mSurface == null) {
6615 return false;
6616 }
6617 if (mAppToken != null && !mAppToken.appFullscreen) {
6618 return false;
6619 }
6620 if (onlyOpaque && mAttrs.format != PixelFormat.OPAQUE) {
6621 return false;
6622 }
6623 final Rect frame = shownFrame ? mShownFrame : mFrame;
Jean-Baptiste Querua8675f62009-07-29 14:25:07 -07006624
6625 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
6626 return frame.left <= mCompatibleScreenFrame.left &&
6627 frame.top <= mCompatibleScreenFrame.top &&
6628 frame.right >= mCompatibleScreenFrame.right &&
6629 frame.bottom >= mCompatibleScreenFrame.bottom;
6630 } else {
6631 return frame.left <= 0 && frame.top <= 0
6632 && frame.right >= screenWidth
6633 && frame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006634 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006635 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07006636
Jean-Baptiste Querua8675f62009-07-29 14:25:07 -07006637 /**
6638 * Return true if the window is opaque and fully drawn.
6639 */
6640 boolean isOpaqueDrawn() {
6641 return mAttrs.format == PixelFormat.OPAQUE && mSurface != null
6642 && mAnimation == null && !mDrawPending && !mCommitDrawPending;
6643 }
6644
6645 boolean needsBackgroundFiller(int screenWidth, int screenHeight) {
6646 return
6647 // only if the application is requesting compatible window
6648 (mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0 &&
6649 // only if it's visible
6650 mHasDrawn && mViewVisibility == View.VISIBLE &&
6651 // and only if the application fills the compatible screen
6652 mFrame.left <= mCompatibleScreenFrame.left &&
6653 mFrame.top <= mCompatibleScreenFrame.top &&
6654 mFrame.right >= mCompatibleScreenFrame.right &&
6655 mFrame.bottom >= mCompatibleScreenFrame.bottom &&
6656 // and starting window do not need background filler
6657 mAttrs.type != mAttrs.TYPE_APPLICATION_STARTING;
6658 }
6659
6660 boolean isFullscreen(int screenWidth, int screenHeight) {
6661 return mFrame.left <= 0 && mFrame.top <= 0 &&
6662 mFrame.right >= screenWidth && mFrame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006663 }
6664
6665 void removeLocked() {
6666 if (mAttachedWindow != null) {
6667 mAttachedWindow.mChildWindows.remove(this);
6668 }
6669 destroySurfaceLocked();
6670 mSession.windowRemovedLocked();
6671 try {
6672 mClient.asBinder().unlinkToDeath(mDeathRecipient, 0);
6673 } catch (RuntimeException e) {
6674 // Ignore if it has already been removed (usually because
6675 // we are doing this as part of processing a death note.)
6676 }
6677 }
6678
6679 private class DeathRecipient implements IBinder.DeathRecipient {
6680 public void binderDied() {
6681 try {
6682 synchronized(mWindowMap) {
6683 WindowState win = windowForClientLocked(mSession, mClient);
6684 Log.i(TAG, "WIN DEATH: " + win);
6685 if (win != null) {
6686 removeWindowLocked(mSession, win);
6687 }
6688 }
6689 } catch (IllegalArgumentException ex) {
6690 // This will happen if the window has already been
6691 // removed.
6692 }
6693 }
6694 }
6695
6696 /** Returns true if this window desires key events. */
6697 public final boolean canReceiveKeys() {
6698 return isVisibleOrAdding()
6699 && (mViewVisibility == View.VISIBLE)
6700 && ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0);
6701 }
6702
6703 public boolean hasDrawnLw() {
6704 return mHasDrawn;
6705 }
6706
6707 public boolean showLw(boolean doAnimation) {
6708 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim) {
6709 mPolicyVisibility = true;
6710 mPolicyVisibilityAfterAnim = true;
6711 if (doAnimation) {
6712 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_ENTER, true);
6713 }
6714 requestAnimationLocked(0);
6715 return true;
6716 }
6717 return false;
6718 }
6719
6720 public boolean hideLw(boolean doAnimation) {
6721 boolean current = doAnimation ? mPolicyVisibilityAfterAnim
6722 : mPolicyVisibility;
6723 if (current) {
6724 if (doAnimation) {
6725 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_EXIT, false);
6726 if (mAnimation == null) {
6727 doAnimation = false;
6728 }
6729 }
6730 if (doAnimation) {
6731 mPolicyVisibilityAfterAnim = false;
6732 } else {
6733 mPolicyVisibilityAfterAnim = false;
6734 mPolicyVisibility = false;
6735 }
6736 requestAnimationLocked(0);
6737 return true;
6738 }
6739 return false;
6740 }
6741
6742 void dump(PrintWriter pw, String prefix) {
Jean-Baptiste Queru843ef362009-05-20 11:28:04 -07006743 StringBuilder sb = new StringBuilder(64);
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07006744
Jean-Baptiste Queru843ef362009-05-20 11:28:04 -07006745 pw.print(prefix); pw.print("mSession="); pw.print(mSession);
6746 pw.print(" mClient="); pw.println(mClient.asBinder());
6747 pw.print(prefix); pw.print("mAttrs="); pw.println(mAttrs);
6748 if (mAttachedWindow != null || mLayoutAttached) {
6749 pw.print(prefix); pw.print("mAttachedWindow="); pw.print(mAttachedWindow);
6750 pw.print(" mLayoutAttached="); pw.println(mLayoutAttached);
6751 }
6752 if (mIsImWindow) {
6753 pw.print(prefix); pw.print("mIsImWindow="); pw.println(mIsImWindow);
6754 }
6755 pw.print(prefix); pw.print("mBaseLayer="); pw.print(mBaseLayer);
6756 pw.print(" mSubLayer="); pw.print(mSubLayer);
6757 pw.print(" mAnimLayer="); pw.print(mLayer); pw.print("+");
6758 pw.print((mTargetAppToken != null ? mTargetAppToken.animLayerAdjustment
6759 : (mAppToken != null ? mAppToken.animLayerAdjustment : 0)));
6760 pw.print("="); pw.print(mAnimLayer);
6761 pw.print(" mLastLayer="); pw.println(mLastLayer);
6762 if (mSurface != null) {
6763 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
6764 }
6765 pw.print(prefix); pw.print("mToken="); pw.println(mToken);
6766 pw.print(prefix); pw.print("mRootToken="); pw.println(mRootToken);
6767 if (mAppToken != null) {
6768 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
6769 }
6770 if (mTargetAppToken != null) {
6771 pw.print(prefix); pw.print("mTargetAppToken="); pw.println(mTargetAppToken);
6772 }
6773 pw.print(prefix); pw.print("mViewVisibility=0x");
6774 pw.print(Integer.toHexString(mViewVisibility));
6775 pw.print(" mLastHidden="); pw.print(mLastHidden);
6776 pw.print(" mHaveFrame="); pw.println(mHaveFrame);
6777 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim || mAttachedHidden) {
6778 pw.print(prefix); pw.print("mPolicyVisibility=");
6779 pw.print(mPolicyVisibility);
6780 pw.print(" mPolicyVisibilityAfterAnim=");
6781 pw.print(mPolicyVisibilityAfterAnim);
6782 pw.print(" mAttachedHidden="); pw.println(mAttachedHidden);
6783 }
6784 pw.print(prefix); pw.print("Requested w="); pw.print(mRequestedWidth);
6785 pw.print(" h="); pw.print(mRequestedHeight);
6786 pw.print(" x="); pw.print(mReqXPos);
6787 pw.print(" y="); pw.println(mReqYPos);
6788 pw.print(prefix); pw.print("mGivenContentInsets=");
6789 mGivenContentInsets.printShortString(pw);
6790 pw.print(" mGivenVisibleInsets=");
6791 mGivenVisibleInsets.printShortString(pw);
6792 pw.println();
6793 if (mTouchableInsets != 0 || mGivenInsetsPending) {
6794 pw.print(prefix); pw.print("mTouchableInsets="); pw.print(mTouchableInsets);
6795 pw.print(" mGivenInsetsPending="); pw.println(mGivenInsetsPending);
6796 }
6797 pw.print(prefix); pw.print("mShownFrame=");
6798 mShownFrame.printShortString(pw);
6799 pw.print(" last="); mLastShownFrame.printShortString(pw);
6800 pw.println();
6801 pw.print(prefix); pw.print("mFrame="); mFrame.printShortString(pw);
6802 pw.print(" last="); mLastFrame.printShortString(pw);
6803 pw.println();
6804 pw.print(prefix); pw.print("mContainingFrame=");
6805 mContainingFrame.printShortString(pw);
6806 pw.print(" mDisplayFrame=");
6807 mDisplayFrame.printShortString(pw);
6808 pw.println();
6809 pw.print(prefix); pw.print("mContentFrame="); mContentFrame.printShortString(pw);
6810 pw.print(" mVisibleFrame="); mVisibleFrame.printShortString(pw);
6811 pw.println();
6812 pw.print(prefix); pw.print("mContentInsets="); mContentInsets.printShortString(pw);
6813 pw.print(" last="); mLastContentInsets.printShortString(pw);
6814 pw.print(" mVisibleInsets="); mVisibleInsets.printShortString(pw);
6815 pw.print(" last="); mLastVisibleInsets.printShortString(pw);
6816 pw.println();
6817 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
6818 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
6819 pw.print(" mAlpha="); pw.print(mAlpha);
6820 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
6821 }
6822 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
6823 || mAnimation != null) {
6824 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
6825 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
6826 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
6827 pw.print(" mAnimation="); pw.println(mAnimation);
6828 }
6829 if (mHasTransformation || mHasLocalTransformation) {
6830 pw.print(prefix); pw.print("XForm: has=");
6831 pw.print(mHasTransformation);
6832 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
6833 pw.print(" "); mTransformation.printShortString(pw);
6834 pw.println();
6835 }
6836 pw.print(prefix); pw.print("mDrawPending="); pw.print(mDrawPending);
6837 pw.print(" mCommitDrawPending="); pw.print(mCommitDrawPending);
6838 pw.print(" mReadyToShow="); pw.print(mReadyToShow);
6839 pw.print(" mHasDrawn="); pw.println(mHasDrawn);
6840 if (mExiting || mRemoveOnExit || mDestroying || mRemoved) {
6841 pw.print(prefix); pw.print("mExiting="); pw.print(mExiting);
6842 pw.print(" mRemoveOnExit="); pw.print(mRemoveOnExit);
6843 pw.print(" mDestroying="); pw.print(mDestroying);
6844 pw.print(" mRemoved="); pw.println(mRemoved);
6845 }
6846 if (mOrientationChanging || mAppFreezing) {
6847 pw.print(prefix); pw.print("mOrientationChanging=");
6848 pw.print(mOrientationChanging);
6849 pw.print(" mAppFreezing="); pw.println(mAppFreezing);
6850 }
Jean-Baptiste Queru5c1207b2009-07-31 17:38:20 -07006851 if (mHScale != 1 || mVScale != 1) {
6852 pw.print(prefix); pw.print("mHScale="); pw.print(mHScale);
6853 pw.print(" mVScale="); pw.println(mVScale);
6854 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006855 }
6856
6857 @Override
6858 public String toString() {
6859 return "Window{"
6860 + Integer.toHexString(System.identityHashCode(this))
6861 + " " + mAttrs.getTitle() + " paused=" + mToken.paused + "}";
6862 }
6863 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07006864
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006865 // -------------------------------------------------------------
6866 // Window Token State
6867 // -------------------------------------------------------------
6868
6869 class WindowToken {
6870 // The actual token.
6871 final IBinder token;
6872
6873 // The type of window this token is for, as per WindowManager.LayoutParams.
6874 final int windowType;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07006875
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006876 // Set if this token was explicitly added by a client, so should
6877 // not be removed when all windows are removed.
6878 final boolean explicit;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07006879
Jean-Baptiste Queru843ef362009-05-20 11:28:04 -07006880 // For printing.
6881 String stringName;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07006882
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006883 // If this is an AppWindowToken, this is non-null.
6884 AppWindowToken appWindowToken;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07006885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006886 // All of the windows associated with this token.
6887 final ArrayList<WindowState> windows = new ArrayList<WindowState>();
6888
6889 // Is key dispatching paused for this token?
6890 boolean paused = false;
6891
6892 // Should this token's windows be hidden?
6893 boolean hidden;
6894
6895 // Temporary for finding which tokens no longer have visible windows.
6896 boolean hasVisible;
6897
6898 WindowToken(IBinder _token, int type, boolean _explicit) {
6899 token = _token;
6900 windowType = type;
6901 explicit = _explicit;
6902 }
6903
6904 void dump(PrintWriter pw, String prefix) {
Jean-Baptiste Queru843ef362009-05-20 11:28:04 -07006905 pw.print(prefix); pw.print("token="); pw.println(token);
6906 pw.print(prefix); pw.print("windows="); pw.println(windows);
6907 pw.print(prefix); pw.print("windowType="); pw.print(windowType);
6908 pw.print(" hidden="); pw.print(hidden);
6909 pw.print(" hasVisible="); pw.println(hasVisible);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006910 }
6911
6912 @Override
6913 public String toString() {
Jean-Baptiste Queru843ef362009-05-20 11:28:04 -07006914 if (stringName == null) {
6915 StringBuilder sb = new StringBuilder();
6916 sb.append("WindowToken{");
6917 sb.append(Integer.toHexString(System.identityHashCode(this)));
6918 sb.append(" token="); sb.append(token); sb.append('}');
6919 stringName = sb.toString();
6920 }
6921 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006922 }
6923 };
6924
6925 class AppWindowToken extends WindowToken {
6926 // Non-null only for application tokens.
6927 final IApplicationToken appToken;
6928
6929 // All of the windows and child windows that are included in this
6930 // application token. Note this list is NOT sorted!
6931 final ArrayList<WindowState> allAppWindows = new ArrayList<WindowState>();
6932
6933 int groupId = -1;
6934 boolean appFullscreen;
6935 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07006936
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006937 // These are used for determining when all windows associated with
6938 // an activity have been drawn, so they can be made visible together
6939 // at the same time.
6940 int lastTransactionSequence = mTransactionSequence-1;
6941 int numInterestingWindows;
6942 int numDrawnWindows;
6943 boolean inPendingTransaction;
6944 boolean allDrawn;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07006945
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006946 // Is this token going to be hidden in a little while? If so, it
6947 // won't be taken into account for setting the screen orientation.
6948 boolean willBeHidden;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07006949
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006950 // Is this window's surface needed? This is almost like hidden, except
6951 // it will sometimes be true a little earlier: when the token has
6952 // been shown, but is still waiting for its app transition to execute
6953 // before making its windows shown.
6954 boolean hiddenRequested;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07006955
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006956 // Have we told the window clients to hide themselves?
6957 boolean clientHidden;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07006958
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006959 // Last visibility state we reported to the app token.
6960 boolean reportedVisible;
6961
6962 // Set to true when the token has been removed from the window mgr.
6963 boolean removed;
6964
6965 // Have we been asked to have this token keep the screen frozen?
6966 boolean freezingScreen;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07006967
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006968 boolean animating;
6969 Animation animation;
6970 boolean hasTransformation;
6971 final Transformation transformation = new Transformation();
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07006972
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006973 // Offset to the window of all layers in the token, for use by
6974 // AppWindowToken animations.
6975 int animLayerAdjustment;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07006976
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006977 // Information about an application starting window if displayed.
6978 StartingData startingData;
6979 WindowState startingWindow;
6980 View startingView;
6981 boolean startingDisplayed;
6982 boolean startingMoved;
6983 boolean firstWindowDrawn;
6984
6985 AppWindowToken(IApplicationToken _token) {
6986 super(_token.asBinder(),
6987 WindowManager.LayoutParams.TYPE_APPLICATION, true);
6988 appWindowToken = this;
6989 appToken = _token;
6990 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07006991
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006992 public void setAnimation(Animation anim) {
6993 if (localLOGV) Log.v(
6994 TAG, "Setting animation in " + this + ": " + anim);
6995 animation = anim;
6996 animating = false;
6997 anim.restrictDuration(MAX_ANIMATION_DURATION);
6998 anim.scaleCurrentDuration(mTransitionAnimationScale);
6999 int zorder = anim.getZAdjustment();
7000 int adj = 0;
7001 if (zorder == Animation.ZORDER_TOP) {
7002 adj = TYPE_LAYER_OFFSET;
7003 } else if (zorder == Animation.ZORDER_BOTTOM) {
7004 adj = -TYPE_LAYER_OFFSET;
7005 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07007006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007007 if (animLayerAdjustment != adj) {
7008 animLayerAdjustment = adj;
7009 updateLayers();
7010 }
7011 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07007012
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007013 public void setDummyAnimation() {
7014 if (animation == null) {
7015 if (localLOGV) Log.v(
7016 TAG, "Setting dummy animation in " + this);
7017 animation = sDummyAnimation;
7018 }
7019 }
7020
7021 public void clearAnimation() {
7022 if (animation != null) {
7023 animation = null;
7024 animating = true;
7025 }
7026 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07007027
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007028 void updateLayers() {
7029 final int N = allAppWindows.size();
7030 final int adj = animLayerAdjustment;
7031 for (int i=0; i<N; i++) {
7032 WindowState w = allAppWindows.get(i);
7033 w.mAnimLayer = w.mLayer + adj;
7034 if (DEBUG_LAYERS) Log.v(TAG, "Updating layer " + w + ": "
7035 + w.mAnimLayer);
7036 if (w == mInputMethodTarget) {
7037 setInputMethodAnimLayerAdjustment(adj);
7038 }
7039 }
7040 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07007041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007042 void sendAppVisibilityToClients() {
7043 final int N = allAppWindows.size();
7044 for (int i=0; i<N; i++) {
7045 WindowState win = allAppWindows.get(i);
7046 if (win == startingWindow && clientHidden) {
7047 // Don't hide the starting window.
7048 continue;
7049 }
7050 try {
7051 if (DEBUG_VISIBILITY) Log.v(TAG,
7052 "Setting visibility of " + win + ": " + (!clientHidden));
7053 win.mClient.dispatchAppVisibility(!clientHidden);
7054 } catch (RemoteException e) {
7055 }
7056 }
7057 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07007058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007059 void showAllWindowsLocked() {
7060 final int NW = allAppWindows.size();
7061 for (int i=0; i<NW; i++) {
7062 WindowState w = allAppWindows.get(i);
7063 if (DEBUG_VISIBILITY) Log.v(TAG,
7064 "performing show on: " + w);
7065 w.performShowLocked();
7066 }
7067 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07007068
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007069 // This must be called while inside a transaction.
7070 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
7071 if (!mDisplayFrozen) {
7072 // We will run animations as long as the display isn't frozen.
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07007073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007074 if (animation == sDummyAnimation) {
7075 // This guy is going to animate, but not yet. For now count
7076 // it is not animating for purposes of scheduling transactions;
7077 // when it is really time to animate, this will be set to
7078 // a real animation and the next call will execute normally.
7079 return false;
7080 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07007081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007082 if ((allDrawn || animating || startingDisplayed) && animation != null) {
7083 if (!animating) {
7084 if (DEBUG_ANIM) Log.v(
7085 TAG, "Starting animation in " + this +
7086 " @ " + currentTime + ": dw=" + dw + " dh=" + dh
7087 + " scale=" + mTransitionAnimationScale
7088 + " allDrawn=" + allDrawn + " animating=" + animating);
7089 animation.initialize(dw, dh, dw, dh);
7090 animation.setStartTime(currentTime);
7091 animating = true;
7092 }
7093 transformation.clear();
7094 final boolean more = animation.getTransformation(
7095 currentTime, transformation);
7096 if (DEBUG_ANIM) Log.v(
7097 TAG, "Stepped animation in " + this +
7098 ": more=" + more + ", xform=" + transformation);
7099 if (more) {
7100 // we're done!
7101 hasTransformation = true;
7102 return true;
7103 }
7104 if (DEBUG_ANIM) Log.v(
7105 TAG, "Finished animation in " + this +
7106 " @ " + currentTime);
7107 animation = null;
7108 }
7109 } else if (animation != null) {
7110 // If the display is frozen, and there is a pending animation,
7111 // clear it and make sure we run the cleanup code.
7112 animating = true;
7113 animation = null;
7114 }
7115
7116 hasTransformation = false;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07007117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007118 if (!animating) {
7119 return false;
7120 }
7121
7122 clearAnimation();
7123 animating = false;
7124 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == this) {
7125 moveInputMethodWindowsIfNeededLocked(true);
7126 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07007127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007128 if (DEBUG_ANIM) Log.v(
7129 TAG, "Animation done in " + this
7130 + ": reportedVisible=" + reportedVisible);
7131
7132 transformation.clear();
7133 if (animLayerAdjustment != 0) {
7134 animLayerAdjustment = 0;
7135 updateLayers();
7136 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07007137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007138 final int N = windows.size();
7139 for (int i=0; i<N; i++) {
7140 ((WindowState)windows.get(i)).finishExit();
7141 }
7142 updateReportedVisibilityLocked();
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07007143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007144 return false;
7145 }
7146
7147 void updateReportedVisibilityLocked() {
7148 if (appToken == null) {
7149 return;
7150 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07007151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007152 int numInteresting = 0;
7153 int numVisible = 0;
7154 boolean nowGone = true;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07007155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007156 if (DEBUG_VISIBILITY) Log.v(TAG, "Update reported visibility: " + this);
7157 final int N = allAppWindows.size();
7158 for (int i=0; i<N; i++) {
7159 WindowState win = allAppWindows.get(i);
7160 if (win == startingWindow || win.mAppFreezing) {
7161 continue;
7162 }
7163 if (DEBUG_VISIBILITY) {
7164 Log.v(TAG, "Win " + win + ": isDisplayed="
7165 + win.isDisplayedLw()
7166 + ", isAnimating=" + win.isAnimating());
7167 if (!win.isDisplayedLw()) {
7168 Log.v(TAG, "Not displayed: s=" + win.mSurface
7169 + " pv=" + win.mPolicyVisibility
7170 + " dp=" + win.mDrawPending
7171 + " cdp=" + win.mCommitDrawPending
7172 + " ah=" + win.mAttachedHidden
7173 + " th="
7174 + (win.mAppToken != null
7175 ? win.mAppToken.hiddenRequested : false)
7176 + " a=" + win.mAnimating);
7177 }
7178 }
7179 numInteresting++;
7180 if (win.isDisplayedLw()) {
7181 if (!win.isAnimating()) {
7182 numVisible++;
7183 }
7184 nowGone = false;
7185 } else if (win.isAnimating()) {
7186 nowGone = false;
7187 }
7188 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07007189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007190 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
7191 if (DEBUG_VISIBILITY) Log.v(TAG, "VIS " + this + ": interesting="
7192 + numInteresting + " visible=" + numVisible);
7193 if (nowVisible != reportedVisible) {
7194 if (DEBUG_VISIBILITY) Log.v(
7195 TAG, "Visibility changed in " + this
7196 + ": vis=" + nowVisible);
7197 reportedVisible = nowVisible;
7198 Message m = mH.obtainMessage(
7199 H.REPORT_APPLICATION_TOKEN_WINDOWS,
7200 nowVisible ? 1 : 0,
7201 nowGone ? 1 : 0,
7202 this);
7203 mH.sendMessage(m);
7204 }
7205 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07007206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007207 void dump(PrintWriter pw, String prefix) {
7208 super.dump(pw, prefix);
Jean-Baptiste Queru843ef362009-05-20 11:28:04 -07007209 if (appToken != null) {
7210 pw.print(prefix); pw.println("app=true");
7211 }
7212 if (allAppWindows.size() > 0) {
7213 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
7214 }
7215 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
7216 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
7217 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
7218 pw.print(" clientHidden="); pw.print(clientHidden);
7219 pw.print(" willBeHidden="); pw.print(willBeHidden);
7220 pw.print(" reportedVisible="); pw.println(reportedVisible);
7221 if (paused || freezingScreen) {
7222 pw.print(prefix); pw.print("paused="); pw.print(paused);
7223 pw.print(" freezingScreen="); pw.println(freezingScreen);
7224 }
7225 if (numInterestingWindows != 0 || numDrawnWindows != 0
7226 || inPendingTransaction || allDrawn) {
7227 pw.print(prefix); pw.print("numInterestingWindows=");
7228 pw.print(numInterestingWindows);
7229 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
7230 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
7231 pw.print(" allDrawn="); pw.println(allDrawn);
7232 }
7233 if (animating || animation != null) {
7234 pw.print(prefix); pw.print("animating="); pw.print(animating);
7235 pw.print(" animation="); pw.println(animation);
7236 }
7237 if (animLayerAdjustment != 0) {
7238 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
7239 }
7240 if (hasTransformation) {
7241 pw.print(prefix); pw.print("hasTransformation="); pw.print(hasTransformation);
7242 pw.print(" transformation="); transformation.printShortString(pw);
7243 pw.println();
7244 }
7245 if (startingData != null || removed || firstWindowDrawn) {
7246 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
7247 pw.print(" removed="); pw.print(removed);
7248 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
7249 }
7250 if (startingWindow != null || startingView != null
7251 || startingDisplayed || startingMoved) {
7252 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
7253 pw.print(" startingView="); pw.print(startingView);
7254 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
7255 pw.print(" startingMoved"); pw.println(startingMoved);
7256 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007257 }
7258
7259 @Override
7260 public String toString() {
Jean-Baptiste Queru843ef362009-05-20 11:28:04 -07007261 if (stringName == null) {
7262 StringBuilder sb = new StringBuilder();
7263 sb.append("AppWindowToken{");
7264 sb.append(Integer.toHexString(System.identityHashCode(this)));
7265 sb.append(" token="); sb.append(token); sb.append('}');
7266 stringName = sb.toString();
7267 }
7268 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007269 }
7270 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07007271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007272 public static WindowManager.LayoutParams findAnimations(
7273 ArrayList<AppWindowToken> order,
Jean-Baptiste Querua8675f62009-07-29 14:25:07 -07007274 ArrayList<AppWindowToken> openingTokenList1,
7275 ArrayList<AppWindowToken> closingTokenList2) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007276 // We need to figure out which animation to use...
Jean-Baptiste Querua8675f62009-07-29 14:25:07 -07007277
7278 // First, check if there is a compatible window in opening/closing
7279 // apps, and use it if exists.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007280 WindowManager.LayoutParams animParams = null;
7281 int animSrc = 0;
Jean-Baptiste Querua8675f62009-07-29 14:25:07 -07007282 animParams = findCompatibleWindowParams(openingTokenList1);
7283 if (animParams == null) {
7284 animParams = findCompatibleWindowParams(closingTokenList2);
7285 }
7286 if (animParams != null) {
7287 return animParams;
7288 }
7289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007290 //Log.i(TAG, "Looking for animations...");
7291 for (int i=order.size()-1; i>=0; i--) {
7292 AppWindowToken wtoken = order.get(i);
7293 //Log.i(TAG, "Token " + wtoken + " with " + wtoken.windows.size() + " windows");
Jean-Baptiste Querua8675f62009-07-29 14:25:07 -07007294 if (openingTokenList1.contains(wtoken) || closingTokenList2.contains(wtoken)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007295 int j = wtoken.windows.size();
7296 while (j > 0) {
7297 j--;
7298 WindowState win = wtoken.windows.get(j);
7299 //Log.i(TAG, "Window " + win + ": type=" + win.mAttrs.type);
7300 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
7301 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
7302 //Log.i(TAG, "Found base or application window, done!");
7303 if (wtoken.appFullscreen) {
7304 return win.mAttrs;
7305 }
7306 if (animSrc < 2) {
7307 animParams = win.mAttrs;
7308 animSrc = 2;
7309 }
7310 } else if (animSrc < 1 && win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION) {
7311 //Log.i(TAG, "Found normal window, we may use this...");
7312 animParams = win.mAttrs;
7313 animSrc = 1;
7314 }
7315 }
7316 }
7317 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07007318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007319 return animParams;
7320 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07007321
Jean-Baptiste Querua8675f62009-07-29 14:25:07 -07007322 private static LayoutParams findCompatibleWindowParams(ArrayList<AppWindowToken> tokenList) {
7323 for (int appCount = tokenList.size() - 1; appCount >= 0; appCount--) {
7324 AppWindowToken wtoken = tokenList.get(appCount);
7325 // Just checking one window is sufficient as all windows have the compatible flag
7326 // if the application is in compatibility mode.
7327 if (wtoken.windows.size() > 0) {
7328 WindowManager.LayoutParams params = wtoken.windows.get(0).mAttrs;
7329 if ((params.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
7330 return params;
7331 }
7332 }
7333 }
7334 return null;
7335 }
7336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007337 // -------------------------------------------------------------
7338 // DummyAnimation
7339 // -------------------------------------------------------------
7340
7341 // This is an animation that does nothing: it just immediately finishes
7342 // itself every time it is called. It is used as a stub animation in cases
7343 // where we want to synchronize multiple things that may be animating.
7344 static final class DummyAnimation extends Animation {
7345 public boolean getTransformation(long currentTime, Transformation outTransformation) {
7346 return false;
7347 }
7348 }
7349 static final Animation sDummyAnimation = new DummyAnimation();
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07007350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007351 // -------------------------------------------------------------
7352 // Async Handler
7353 // -------------------------------------------------------------
7354
7355 static final class StartingData {
7356 final String pkg;
7357 final int theme;
7358 final CharSequence nonLocalizedLabel;
7359 final int labelRes;
7360 final int icon;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07007361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007362 StartingData(String _pkg, int _theme, CharSequence _nonLocalizedLabel,
7363 int _labelRes, int _icon) {
7364 pkg = _pkg;
7365 theme = _theme;
7366 nonLocalizedLabel = _nonLocalizedLabel;
7367 labelRes = _labelRes;
7368 icon = _icon;
7369 }
7370 }
7371
7372 private final class H extends Handler {
7373 public static final int REPORT_FOCUS_CHANGE = 2;
7374 public static final int REPORT_LOSING_FOCUS = 3;
7375 public static final int ANIMATE = 4;
7376 public static final int ADD_STARTING = 5;
7377 public static final int REMOVE_STARTING = 6;
7378 public static final int FINISHED_STARTING = 7;
7379 public static final int REPORT_APPLICATION_TOKEN_WINDOWS = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007380 public static final int WINDOW_FREEZE_TIMEOUT = 11;
7381 public static final int HOLD_SCREEN_CHANGED = 12;
7382 public static final int APP_TRANSITION_TIMEOUT = 13;
7383 public static final int PERSIST_ANIMATION_SCALE = 14;
7384 public static final int FORCE_GC = 15;
7385 public static final int ENABLE_SCREEN = 16;
7386 public static final int APP_FREEZE_TIMEOUT = 17;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07007387 public static final int COMPUTE_AND_SEND_NEW_CONFIGURATION = 18;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07007388
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007389 private Session mLastReportedHold;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07007390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007391 public H() {
7392 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07007393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007394 @Override
7395 public void handleMessage(Message msg) {
7396 switch (msg.what) {
7397 case REPORT_FOCUS_CHANGE: {
7398 WindowState lastFocus;
7399 WindowState newFocus;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07007400
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007401 synchronized(mWindowMap) {
7402 lastFocus = mLastFocus;
7403 newFocus = mCurrentFocus;
7404 if (lastFocus == newFocus) {
7405 // Focus is not changing, so nothing to do.
7406 return;
7407 }
7408 mLastFocus = newFocus;
7409 //Log.i(TAG, "Focus moving from " + lastFocus
7410 // + " to " + newFocus);
7411 if (newFocus != null && lastFocus != null
7412 && !newFocus.isDisplayedLw()) {
7413 //Log.i(TAG, "Delaying loss of focus...");
7414 mLosingFocus.add(lastFocus);
7415 lastFocus = null;
7416 }
7417 }
7418
7419 if (lastFocus != newFocus) {
7420 //System.out.println("Changing focus from " + lastFocus
7421 // + " to " + newFocus);
7422 if (newFocus != null) {
7423 try {
7424 //Log.i(TAG, "Gaining focus: " + newFocus);
7425 newFocus.mClient.windowFocusChanged(true, mInTouchMode);
7426 } catch (RemoteException e) {
7427 // Ignore if process has died.
7428 }
7429 }
7430
7431 if (lastFocus != null) {
7432 try {
7433 //Log.i(TAG, "Losing focus: " + lastFocus);
7434 lastFocus.mClient.windowFocusChanged(false, mInTouchMode);
7435 } catch (RemoteException e) {
7436 // Ignore if process has died.
7437 }
7438 }
7439 }
7440 } break;
7441
7442 case REPORT_LOSING_FOCUS: {
7443 ArrayList<WindowState> losers;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07007444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007445 synchronized(mWindowMap) {
7446 losers = mLosingFocus;
7447 mLosingFocus = new ArrayList<WindowState>();
7448 }
7449
7450 final int N = losers.size();
7451 for (int i=0; i<N; i++) {
7452 try {
7453 //Log.i(TAG, "Losing delayed focus: " + losers.get(i));
7454 losers.get(i).mClient.windowFocusChanged(false, mInTouchMode);
7455 } catch (RemoteException e) {
7456 // Ignore if process has died.
7457 }
7458 }
7459 } break;
7460
7461 case ANIMATE: {
7462 synchronized(mWindowMap) {
7463 mAnimationPending = false;
7464 performLayoutAndPlaceSurfacesLocked();
7465 }
7466 } break;
7467
7468 case ADD_STARTING: {
7469 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
7470 final StartingData sd = wtoken.startingData;
7471
7472 if (sd == null) {
7473 // Animation has been canceled... do nothing.
7474 return;
7475 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07007476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007477 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Add starting "
7478 + wtoken + ": pkg=" + sd.pkg);
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07007479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007480 View view = null;
7481 try {
7482 view = mPolicy.addStartingWindow(
7483 wtoken.token, sd.pkg,
7484 sd.theme, sd.nonLocalizedLabel, sd.labelRes,
7485 sd.icon);
7486 } catch (Exception e) {
7487 Log.w(TAG, "Exception when adding starting window", e);
7488 }
7489
7490 if (view != null) {
7491 boolean abort = false;
7492
7493 synchronized(mWindowMap) {
7494 if (wtoken.removed || wtoken.startingData == null) {
7495 // If the window was successfully added, then
7496 // we need to remove it.
7497 if (wtoken.startingWindow != null) {
7498 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
7499 "Aborted starting " + wtoken
7500 + ": removed=" + wtoken.removed
7501 + " startingData=" + wtoken.startingData);
7502 wtoken.startingWindow = null;
7503 wtoken.startingData = null;
7504 abort = true;
7505 }
7506 } else {
7507 wtoken.startingView = view;
7508 }
7509 if (DEBUG_STARTING_WINDOW && !abort) Log.v(TAG,
7510 "Added starting " + wtoken
7511 + ": startingWindow="
7512 + wtoken.startingWindow + " startingView="
7513 + wtoken.startingView);
7514 }
7515
7516 if (abort) {
7517 try {
7518 mPolicy.removeStartingWindow(wtoken.token, view);
7519 } catch (Exception e) {
7520 Log.w(TAG, "Exception when removing starting window", e);
7521 }
7522 }
7523 }
7524 } break;
7525
7526 case REMOVE_STARTING: {
7527 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
7528 IBinder token = null;
7529 View view = null;
7530 synchronized (mWindowMap) {
7531 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Remove starting "
7532 + wtoken + ": startingWindow="
7533 + wtoken.startingWindow + " startingView="
7534 + wtoken.startingView);
7535 if (wtoken.startingWindow != null) {
7536 view = wtoken.startingView;
7537 token = wtoken.token;
7538 wtoken.startingData = null;
7539 wtoken.startingView = null;
7540 wtoken.startingWindow = null;
7541 }
7542 }
7543 if (view != null) {
7544 try {
7545 mPolicy.removeStartingWindow(token, view);
7546 } catch (Exception e) {
7547 Log.w(TAG, "Exception when removing starting window", e);
7548 }
7549 }
7550 } break;
7551
7552 case FINISHED_STARTING: {
7553 IBinder token = null;
7554 View view = null;
7555 while (true) {
7556 synchronized (mWindowMap) {
7557 final int N = mFinishedStarting.size();
7558 if (N <= 0) {
7559 break;
7560 }
7561 AppWindowToken wtoken = mFinishedStarting.remove(N-1);
7562
7563 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
7564 "Finished starting " + wtoken
7565 + ": startingWindow=" + wtoken.startingWindow
7566 + " startingView=" + wtoken.startingView);
7567
7568 if (wtoken.startingWindow == null) {
7569 continue;
7570 }
7571
7572 view = wtoken.startingView;
7573 token = wtoken.token;
7574 wtoken.startingData = null;
7575 wtoken.startingView = null;
7576 wtoken.startingWindow = null;
7577 }
7578
7579 try {
7580 mPolicy.removeStartingWindow(token, view);
7581 } catch (Exception e) {
7582 Log.w(TAG, "Exception when removing starting window", e);
7583 }
7584 }
7585 } break;
7586
7587 case REPORT_APPLICATION_TOKEN_WINDOWS: {
7588 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
7589
7590 boolean nowVisible = msg.arg1 != 0;
7591 boolean nowGone = msg.arg2 != 0;
7592
7593 try {
7594 if (DEBUG_VISIBILITY) Log.v(
7595 TAG, "Reporting visible in " + wtoken
7596 + " visible=" + nowVisible
7597 + " gone=" + nowGone);
7598 if (nowVisible) {
7599 wtoken.appToken.windowsVisible();
7600 } else {
7601 wtoken.appToken.windowsGone();
7602 }
7603 } catch (RemoteException ex) {
7604 }
7605 } break;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07007606
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007607 case WINDOW_FREEZE_TIMEOUT: {
7608 synchronized (mWindowMap) {
7609 Log.w(TAG, "Window freeze timeout expired.");
7610 int i = mWindows.size();
7611 while (i > 0) {
7612 i--;
7613 WindowState w = (WindowState)mWindows.get(i);
7614 if (w.mOrientationChanging) {
7615 w.mOrientationChanging = false;
7616 Log.w(TAG, "Force clearing orientation change: " + w);
7617 }
7618 }
7619 performLayoutAndPlaceSurfacesLocked();
7620 }
7621 break;
7622 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07007623
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007624 case HOLD_SCREEN_CHANGED: {
7625 Session oldHold;
7626 Session newHold;
7627 synchronized (mWindowMap) {
7628 oldHold = mLastReportedHold;
7629 newHold = (Session)msg.obj;
7630 mLastReportedHold = newHold;
7631 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07007632
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007633 if (oldHold != newHold) {
7634 try {
7635 if (oldHold != null) {
7636 mBatteryStats.noteStopWakelock(oldHold.mUid,
7637 "window",
7638 BatteryStats.WAKE_TYPE_WINDOW);
7639 }
7640 if (newHold != null) {
7641 mBatteryStats.noteStartWakelock(newHold.mUid,
7642 "window",
7643 BatteryStats.WAKE_TYPE_WINDOW);
7644 }
7645 } catch (RemoteException e) {
7646 }
7647 }
7648 break;
7649 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07007650
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007651 case APP_TRANSITION_TIMEOUT: {
7652 synchronized (mWindowMap) {
7653 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
7654 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
7655 "*** APP TRANSITION TIMEOUT");
7656 mAppTransitionReady = true;
7657 mAppTransitionTimeout = true;
7658 performLayoutAndPlaceSurfacesLocked();
7659 }
7660 }
7661 break;
7662 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07007663
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007664 case PERSIST_ANIMATION_SCALE: {
7665 Settings.System.putFloat(mContext.getContentResolver(),
7666 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
7667 Settings.System.putFloat(mContext.getContentResolver(),
7668 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
7669 break;
7670 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07007671
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007672 case FORCE_GC: {
7673 synchronized(mWindowMap) {
7674 if (mAnimationPending) {
7675 // If we are animating, don't do the gc now but
7676 // delay a bit so we don't interrupt the animation.
7677 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
7678 2000);
7679 return;
7680 }
7681 // If we are currently rotating the display, it will
7682 // schedule a new message when done.
7683 if (mDisplayFrozen) {
7684 return;
7685 }
7686 mFreezeGcPending = 0;
7687 }
7688 Runtime.getRuntime().gc();
7689 break;
7690 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07007691
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007692 case ENABLE_SCREEN: {
7693 performEnableScreen();
7694 break;
7695 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07007696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007697 case APP_FREEZE_TIMEOUT: {
7698 synchronized (mWindowMap) {
7699 Log.w(TAG, "App freeze timeout expired.");
7700 int i = mAppTokens.size();
7701 while (i > 0) {
7702 i--;
7703 AppWindowToken tok = mAppTokens.get(i);
7704 if (tok.freezingScreen) {
7705 Log.w(TAG, "Force clearing freeze: " + tok);
7706 unsetAppFreezingScreenLocked(tok, true, true);
7707 }
7708 }
7709 }
7710 break;
7711 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07007712
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07007713 case COMPUTE_AND_SEND_NEW_CONFIGURATION: {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07007714 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07007715 sendNewConfiguration();
7716 }
7717 break;
7718 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07007719
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007720 }
7721 }
7722 }
7723
7724 // -------------------------------------------------------------
7725 // IWindowManager API
7726 // -------------------------------------------------------------
7727
7728 public IWindowSession openSession(IInputMethodClient client,
7729 IInputContext inputContext) {
7730 if (client == null) throw new IllegalArgumentException("null client");
7731 if (inputContext == null) throw new IllegalArgumentException("null inputContext");
7732 return new Session(client, inputContext);
7733 }
7734
7735 public boolean inputMethodClientHasFocus(IInputMethodClient client) {
7736 synchronized (mWindowMap) {
7737 // The focus for the client is the window immediately below
7738 // where we would place the input method window.
7739 int idx = findDesiredInputMethodWindowIndexLocked(false);
7740 WindowState imFocus;
7741 if (idx > 0) {
7742 imFocus = (WindowState)mWindows.get(idx-1);
7743 if (imFocus != null) {
7744 if (imFocus.mSession.mClient != null &&
7745 imFocus.mSession.mClient.asBinder() == client.asBinder()) {
7746 return true;
7747 }
7748 }
7749 }
7750 }
7751 return false;
7752 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07007753
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007754 // -------------------------------------------------------------
7755 // Internals
7756 // -------------------------------------------------------------
7757
7758 final WindowState windowForClientLocked(Session session, IWindow client) {
7759 return windowForClientLocked(session, client.asBinder());
7760 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07007761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007762 final WindowState windowForClientLocked(Session session, IBinder client) {
7763 WindowState win = mWindowMap.get(client);
7764 if (localLOGV) Log.v(
7765 TAG, "Looking up client " + client + ": " + win);
7766 if (win == null) {
7767 RuntimeException ex = new RuntimeException();
7768 Log.w(TAG, "Requested window " + client + " does not exist", ex);
7769 return null;
7770 }
7771 if (session != null && win.mSession != session) {
7772 RuntimeException ex = new RuntimeException();
7773 Log.w(TAG, "Requested window " + client + " is in session " +
7774 win.mSession + ", not " + session, ex);
7775 return null;
7776 }
7777
7778 return win;
7779 }
7780
7781 private final void assignLayersLocked() {
7782 int N = mWindows.size();
7783 int curBaseLayer = 0;
7784 int curLayer = 0;
7785 int i;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07007786
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007787 for (i=0; i<N; i++) {
7788 WindowState w = (WindowState)mWindows.get(i);
7789 if (w.mBaseLayer == curBaseLayer || w.mIsImWindow) {
7790 curLayer += WINDOW_LAYER_MULTIPLIER;
7791 w.mLayer = curLayer;
7792 } else {
7793 curBaseLayer = curLayer = w.mBaseLayer;
7794 w.mLayer = curLayer;
7795 }
7796 if (w.mTargetAppToken != null) {
7797 w.mAnimLayer = w.mLayer + w.mTargetAppToken.animLayerAdjustment;
7798 } else if (w.mAppToken != null) {
7799 w.mAnimLayer = w.mLayer + w.mAppToken.animLayerAdjustment;
7800 } else {
7801 w.mAnimLayer = w.mLayer;
7802 }
7803 if (w.mIsImWindow) {
7804 w.mAnimLayer += mInputMethodAnimLayerAdjustment;
7805 }
7806 if (DEBUG_LAYERS) Log.v(TAG, "Assign layer " + w + ": "
7807 + w.mAnimLayer);
7808 //System.out.println(
7809 // "Assigned layer " + curLayer + " to " + w.mClient.asBinder());
7810 }
7811 }
7812
7813 private boolean mInLayout = false;
7814 private final void performLayoutAndPlaceSurfacesLocked() {
7815 if (mInLayout) {
Jean-Baptiste Queru843ef362009-05-20 11:28:04 -07007816 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007817 throw new RuntimeException("Recursive call!");
7818 }
7819 Log.w(TAG, "performLayoutAndPlaceSurfacesLocked called while in layout");
7820 return;
7821 }
7822
7823 boolean recoveringMemory = false;
7824 if (mForceRemoves != null) {
7825 recoveringMemory = true;
7826 // Wait a little it for things to settle down, and off we go.
7827 for (int i=0; i<mForceRemoves.size(); i++) {
7828 WindowState ws = mForceRemoves.get(i);
7829 Log.i(TAG, "Force removing: " + ws);
7830 removeWindowInnerLocked(ws.mSession, ws);
7831 }
7832 mForceRemoves = null;
7833 Log.w(TAG, "Due to memory failure, waiting a bit for next layout");
7834 Object tmp = new Object();
7835 synchronized (tmp) {
7836 try {
7837 tmp.wait(250);
7838 } catch (InterruptedException e) {
7839 }
7840 }
7841 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07007842
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007843 mInLayout = true;
7844 try {
7845 performLayoutAndPlaceSurfacesLockedInner(recoveringMemory);
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07007846
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007847 int i = mPendingRemove.size()-1;
7848 if (i >= 0) {
7849 while (i >= 0) {
7850 WindowState w = mPendingRemove.get(i);
7851 removeWindowInnerLocked(w.mSession, w);
7852 i--;
7853 }
7854 mPendingRemove.clear();
7855
7856 mInLayout = false;
7857 assignLayersLocked();
7858 mLayoutNeeded = true;
7859 performLayoutAndPlaceSurfacesLocked();
7860
7861 } else {
7862 mInLayout = false;
7863 if (mLayoutNeeded) {
7864 requestAnimationLocked(0);
7865 }
7866 }
7867 } catch (RuntimeException e) {
7868 mInLayout = false;
7869 Log.e(TAG, "Unhandled exception while layout out windows", e);
7870 }
7871 }
7872
7873 private final void performLayoutLockedInner() {
7874 final int dw = mDisplay.getWidth();
7875 final int dh = mDisplay.getHeight();
7876
7877 final int N = mWindows.size();
Dianne Hackbornb3785302009-03-31 17:58:46 -07007878 int repeats = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007879 int i;
7880
7881 // FIRST LOOP: Perform a layout, if needed.
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07007882
Dianne Hackbornb3785302009-03-31 17:58:46 -07007883 while (mLayoutNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007884 mPolicy.beginLayoutLw(dw, dh);
7885
7886 // First perform layout of any root windows (not attached
7887 // to another window).
7888 int topAttached = -1;
7889 for (i = N-1; i >= 0; i--) {
7890 WindowState win = (WindowState) mWindows.get(i);
Dianne Hackbornb3785302009-03-31 17:58:46 -07007891
7892 // Don't do layout of a window if it is not visible, or
7893 // soon won't be visible, to avoid wasting time and funky
7894 // changes while a window is animating away.
7895 final AppWindowToken atoken = win.mAppToken;
7896 final boolean gone = win.mViewVisibility == View.GONE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007897 || !win.mRelayoutCalled
Dianne Hackbornb3785302009-03-31 17:58:46 -07007898 || win.mRootToken.hidden
7899 || (atoken != null && atoken.hiddenRequested)
7900 || !win.mPolicyVisibility
7901 || win.mAttachedHidden
7902 || win.mExiting || win.mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007903
7904 // If this view is GONE, then skip it -- keep the current
7905 // frame, and let the caller know so they can ignore it
7906 // if they want. (We do the normal layout for INVISIBLE
7907 // windows, since that means "perform layout as normal,
7908 // just don't display").
7909 if (!gone || !win.mHaveFrame) {
7910 if (!win.mLayoutAttached) {
7911 mPolicy.layoutWindowLw(win, win.mAttrs, null);
7912 } else {
7913 if (topAttached < 0) topAttached = i;
7914 }
7915 }
7916 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07007917
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007918 // Now perform layout of attached windows, which usually
7919 // depend on the position of the window they are attached to.
7920 // XXX does not deal with windows that are attached to windows
7921 // that are themselves attached.
7922 for (i = topAttached; i >= 0; i--) {
7923 WindowState win = (WindowState) mWindows.get(i);
7924
7925 // If this view is GONE, then skip it -- keep the current
7926 // frame, and let the caller know so they can ignore it
7927 // if they want. (We do the normal layout for INVISIBLE
7928 // windows, since that means "perform layout as normal,
7929 // just don't display").
7930 if (win.mLayoutAttached) {
7931 if ((win.mViewVisibility != View.GONE && win.mRelayoutCalled)
7932 || !win.mHaveFrame) {
7933 mPolicy.layoutWindowLw(win, win.mAttrs, win.mAttachedWindow);
7934 }
7935 }
7936 }
7937
Dianne Hackbornb3785302009-03-31 17:58:46 -07007938 if (!mPolicy.finishLayoutLw()) {
7939 mLayoutNeeded = false;
7940 } else if (repeats > 2) {
7941 Log.w(TAG, "Layout repeat aborted after too many iterations");
7942 mLayoutNeeded = false;
7943 } else {
7944 repeats++;
7945 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007946 }
7947 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07007948
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007949 private final void performLayoutAndPlaceSurfacesLockedInner(
7950 boolean recoveringMemory) {
7951 final long currentTime = SystemClock.uptimeMillis();
7952 final int dw = mDisplay.getWidth();
7953 final int dh = mDisplay.getHeight();
7954
7955 final int N = mWindows.size();
7956 int i;
7957
7958 // FIRST LOOP: Perform a layout, if needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007959 performLayoutLockedInner();
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07007960
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007961 if (mFxSession == null) {
7962 mFxSession = new SurfaceSession();
7963 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07007964
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007965 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
7966
7967 // Initialize state of exiting tokens.
7968 for (i=mExitingTokens.size()-1; i>=0; i--) {
7969 mExitingTokens.get(i).hasVisible = false;
7970 }
7971
7972 // Initialize state of exiting applications.
7973 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
7974 mExitingAppTokens.get(i).hasVisible = false;
7975 }
7976
7977 // SECOND LOOP: Execute animations and update visibility of windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007978 boolean orientationChangeComplete = true;
7979 Session holdScreen = null;
7980 float screenBrightness = -1;
7981 boolean focusDisplayed = false;
7982 boolean animating = false;
7983
7984 Surface.openTransaction();
7985 try {
7986 boolean restart;
7987
7988 do {
7989 final int transactionSequence = ++mTransactionSequence;
7990
7991 // Update animations of all applications, including those
7992 // associated with exiting/removed apps
7993 boolean tokensAnimating = false;
7994 final int NAT = mAppTokens.size();
7995 for (i=0; i<NAT; i++) {
7996 if (mAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
7997 tokensAnimating = true;
7998 }
7999 }
8000 final int NEAT = mExitingAppTokens.size();
8001 for (i=0; i<NEAT; i++) {
8002 if (mExitingAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
8003 tokensAnimating = true;
8004 }
8005 }
8006
8007 animating = tokensAnimating;
8008 restart = false;
8009
8010 boolean tokenMayBeDrawn = false;
8011
8012 mPolicy.beginAnimationLw(dw, dh);
8013
8014 for (i=N-1; i>=0; i--) {
8015 WindowState w = (WindowState)mWindows.get(i);
8016
8017 final WindowManager.LayoutParams attrs = w.mAttrs;
8018
8019 if (w.mSurface != null) {
8020 // Execute animation.
8021 w.commitFinishDrawingLocked(currentTime);
8022 if (w.stepAnimationLocked(currentTime, dw, dh)) {
8023 animating = true;
8024 //w.dump(" ");
8025 }
8026
8027 mPolicy.animatingWindowLw(w, attrs);
8028 }
8029
8030 final AppWindowToken atoken = w.mAppToken;
8031 if (atoken != null && (!atoken.allDrawn || atoken.freezingScreen)) {
8032 if (atoken.lastTransactionSequence != transactionSequence) {
8033 atoken.lastTransactionSequence = transactionSequence;
8034 atoken.numInterestingWindows = atoken.numDrawnWindows = 0;
8035 atoken.startingDisplayed = false;
8036 }
8037 if ((w.isOnScreen() || w.mAttrs.type
8038 == WindowManager.LayoutParams.TYPE_BASE_APPLICATION)
8039 && !w.mExiting && !w.mDestroying) {
8040 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
8041 Log.v(TAG, "Eval win " + w + ": isDisplayed="
8042 + w.isDisplayedLw()
8043 + ", isAnimating=" + w.isAnimating());
8044 if (!w.isDisplayedLw()) {
8045 Log.v(TAG, "Not displayed: s=" + w.mSurface
8046 + " pv=" + w.mPolicyVisibility
8047 + " dp=" + w.mDrawPending
8048 + " cdp=" + w.mCommitDrawPending
8049 + " ah=" + w.mAttachedHidden
8050 + " th=" + atoken.hiddenRequested
8051 + " a=" + w.mAnimating);
8052 }
8053 }
8054 if (w != atoken.startingWindow) {
8055 if (!atoken.freezingScreen || !w.mAppFreezing) {
8056 atoken.numInterestingWindows++;
8057 if (w.isDisplayedLw()) {
8058 atoken.numDrawnWindows++;
8059 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Log.v(TAG,
8060 "tokenMayBeDrawn: " + atoken
8061 + " freezingScreen=" + atoken.freezingScreen
8062 + " mAppFreezing=" + w.mAppFreezing);
8063 tokenMayBeDrawn = true;
8064 }
8065 }
8066 } else if (w.isDisplayedLw()) {
8067 atoken.startingDisplayed = true;
8068 }
8069 }
8070 } else if (w.mReadyToShow) {
8071 w.performShowLocked();
8072 }
8073 }
8074
8075 if (mPolicy.finishAnimationLw()) {
8076 restart = true;
8077 }
8078
8079 if (tokenMayBeDrawn) {
8080 // See if any windows have been drawn, so they (and others
8081 // associated with them) can now be shown.
8082 final int NT = mTokenList.size();
8083 for (i=0; i<NT; i++) {
8084 AppWindowToken wtoken = mTokenList.get(i).appWindowToken;
8085 if (wtoken == null) {
8086 continue;
8087 }
8088 if (wtoken.freezingScreen) {
8089 int numInteresting = wtoken.numInterestingWindows;
8090 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
8091 if (DEBUG_VISIBILITY) Log.v(TAG,
8092 "allDrawn: " + wtoken
8093 + " interesting=" + numInteresting
8094 + " drawn=" + wtoken.numDrawnWindows);
8095 wtoken.showAllWindowsLocked();
8096 unsetAppFreezingScreenLocked(wtoken, false, true);
8097 orientationChangeComplete = true;
8098 }
8099 } else if (!wtoken.allDrawn) {
8100 int numInteresting = wtoken.numInterestingWindows;
8101 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
8102 if (DEBUG_VISIBILITY) Log.v(TAG,
8103 "allDrawn: " + wtoken
8104 + " interesting=" + numInteresting
8105 + " drawn=" + wtoken.numDrawnWindows);
8106 wtoken.allDrawn = true;
8107 restart = true;
8108
8109 // We can now show all of the drawn windows!
8110 if (!mOpeningApps.contains(wtoken)) {
8111 wtoken.showAllWindowsLocked();
8112 }
8113 }
8114 }
8115 }
8116 }
8117
8118 // If we are ready to perform an app transition, check through
8119 // all of the app tokens to be shown and see if they are ready
8120 // to go.
8121 if (mAppTransitionReady) {
8122 int NN = mOpeningApps.size();
8123 boolean goodToGo = true;
8124 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8125 "Checking " + NN + " opening apps (frozen="
8126 + mDisplayFrozen + " timeout="
8127 + mAppTransitionTimeout + ")...");
8128 if (!mDisplayFrozen && !mAppTransitionTimeout) {
8129 // If the display isn't frozen, wait to do anything until
8130 // all of the apps are ready. Otherwise just go because
8131 // we'll unfreeze the display when everyone is ready.
8132 for (i=0; i<NN && goodToGo; i++) {
8133 AppWindowToken wtoken = mOpeningApps.get(i);
8134 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8135 "Check opening app" + wtoken + ": allDrawn="
8136 + wtoken.allDrawn + " startingDisplayed="
8137 + wtoken.startingDisplayed);
8138 if (!wtoken.allDrawn && !wtoken.startingDisplayed
8139 && !wtoken.startingMoved) {
8140 goodToGo = false;
8141 }
8142 }
8143 }
8144 if (goodToGo) {
8145 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "**** GOOD TO GO");
8146 int transit = mNextAppTransition;
8147 if (mSkipAppTransitionAnimation) {
8148 transit = WindowManagerPolicy.TRANSIT_NONE;
8149 }
8150 mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
8151 mAppTransitionReady = false;
8152 mAppTransitionTimeout = false;
8153 mStartingIconInTransition = false;
8154 mSkipAppTransitionAnimation = false;
8155
8156 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
8157
8158 // We need to figure out which animation to use...
8159 WindowManager.LayoutParams lp = findAnimations(mAppTokens,
8160 mOpeningApps, mClosingApps);
8161
8162 NN = mOpeningApps.size();
8163 for (i=0; i<NN; i++) {
8164 AppWindowToken wtoken = mOpeningApps.get(i);
8165 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8166 "Now opening app" + wtoken);
8167 wtoken.reportedVisible = false;
8168 wtoken.inPendingTransaction = false;
8169 setTokenVisibilityLocked(wtoken, lp, true, transit, false);
8170 wtoken.updateReportedVisibilityLocked();
8171 wtoken.showAllWindowsLocked();
8172 }
8173 NN = mClosingApps.size();
8174 for (i=0; i<NN; i++) {
8175 AppWindowToken wtoken = mClosingApps.get(i);
8176 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8177 "Now closing app" + wtoken);
8178 wtoken.inPendingTransaction = false;
8179 setTokenVisibilityLocked(wtoken, lp, false, transit, false);
8180 wtoken.updateReportedVisibilityLocked();
8181 // Force the allDrawn flag, because we want to start
8182 // this guy's animations regardless of whether it's
8183 // gotten drawn.
8184 wtoken.allDrawn = true;
8185 }
8186
8187 mOpeningApps.clear();
8188 mClosingApps.clear();
8189
8190 // This has changed the visibility of windows, so perform
8191 // a new layout to get them all up-to-date.
8192 mLayoutNeeded = true;
Jean-Baptiste Queru5c1207b2009-07-31 17:38:20 -07008193 if (!moveInputMethodWindowsIfNeededLocked(true)) {
8194 assignLayersLocked();
8195 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008196 performLayoutLockedInner();
8197 updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES);
8198
8199 restart = true;
8200 }
8201 }
8202 } while (restart);
8203
8204 // THIRD LOOP: Update the surfaces of all windows.
8205
8206 final boolean someoneLosingFocus = mLosingFocus.size() != 0;
8207
8208 boolean obscured = false;
8209 boolean blurring = false;
8210 boolean dimming = false;
8211 boolean covered = false;
Dianne Hackborn2f847b92009-03-25 15:25:33 -07008212 boolean syswin = false;
Jean-Baptiste Querua8675f62009-07-29 14:25:07 -07008213 boolean backgroundFillerShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008214
8215 for (i=N-1; i>=0; i--) {
8216 WindowState w = (WindowState)mWindows.get(i);
8217
8218 boolean displayed = false;
8219 final WindowManager.LayoutParams attrs = w.mAttrs;
8220 final int attrFlags = attrs.flags;
8221
8222 if (w.mSurface != null) {
8223 w.computeShownFrameLocked();
8224 if (localLOGV) Log.v(
8225 TAG, "Placing surface #" + i + " " + w.mSurface
8226 + ": new=" + w.mShownFrame + ", old="
8227 + w.mLastShownFrame);
8228
8229 boolean resize;
8230 int width, height;
8231 if ((w.mAttrs.flags & w.mAttrs.FLAG_SCALED) != 0) {
8232 resize = w.mLastRequestedWidth != w.mRequestedWidth ||
8233 w.mLastRequestedHeight != w.mRequestedHeight;
8234 // for a scaled surface, we just want to use
8235 // the requested size.
8236 width = w.mRequestedWidth;
8237 height = w.mRequestedHeight;
8238 w.mLastRequestedWidth = width;
8239 w.mLastRequestedHeight = height;
8240 w.mLastShownFrame.set(w.mShownFrame);
8241 try {
8242 w.mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
8243 } catch (RuntimeException e) {
8244 Log.w(TAG, "Error positioning surface in " + w, e);
8245 if (!recoveringMemory) {
8246 reclaimSomeSurfaceMemoryLocked(w, "position");
8247 }
8248 }
8249 } else {
8250 resize = !w.mLastShownFrame.equals(w.mShownFrame);
8251 width = w.mShownFrame.width();
8252 height = w.mShownFrame.height();
8253 w.mLastShownFrame.set(w.mShownFrame);
8254 if (resize) {
8255 if (SHOW_TRANSACTIONS) Log.i(
8256 TAG, " SURFACE " + w.mSurface + ": ("
8257 + w.mShownFrame.left + ","
8258 + w.mShownFrame.top + ") ("
8259 + w.mShownFrame.width() + "x"
8260 + w.mShownFrame.height() + ")");
8261 }
8262 }
8263
8264 if (resize) {
8265 if (width < 1) width = 1;
8266 if (height < 1) height = 1;
8267 if (w.mSurface != null) {
8268 try {
8269 w.mSurface.setSize(width, height);
8270 w.mSurface.setPosition(w.mShownFrame.left,
8271 w.mShownFrame.top);
8272 } catch (RuntimeException e) {
8273 // If something goes wrong with the surface (such
8274 // as running out of memory), don't take down the
8275 // entire system.
8276 Log.e(TAG, "Failure updating surface of " + w
8277 + "size=(" + width + "x" + height
8278 + "), pos=(" + w.mShownFrame.left
8279 + "," + w.mShownFrame.top + ")", e);
8280 if (!recoveringMemory) {
8281 reclaimSomeSurfaceMemoryLocked(w, "size");
8282 }
8283 }
8284 }
8285 }
8286 if (!w.mAppFreezing) {
8287 w.mContentInsetsChanged =
8288 !w.mLastContentInsets.equals(w.mContentInsets);
8289 w.mVisibleInsetsChanged =
8290 !w.mLastVisibleInsets.equals(w.mVisibleInsets);
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07008291 if (!w.mLastFrame.equals(w.mFrame)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008292 || w.mContentInsetsChanged
8293 || w.mVisibleInsetsChanged) {
8294 w.mLastFrame.set(w.mFrame);
8295 w.mLastContentInsets.set(w.mContentInsets);
8296 w.mLastVisibleInsets.set(w.mVisibleInsets);
8297 // If the orientation is changing, then we need to
8298 // hold off on unfreezing the display until this
8299 // window has been redrawn; to do that, we need
8300 // to go through the process of getting informed
8301 // by the application when it has finished drawing.
8302 if (w.mOrientationChanging) {
8303 if (DEBUG_ORIENTATION) Log.v(TAG,
8304 "Orientation start waiting for draw in "
8305 + w + ", surface " + w.mSurface);
8306 w.mDrawPending = true;
8307 w.mCommitDrawPending = false;
8308 w.mReadyToShow = false;
8309 if (w.mAppToken != null) {
8310 w.mAppToken.allDrawn = false;
8311 }
8312 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07008313 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008314 "Resizing window " + w + " to " + w.mFrame);
8315 mResizingWindows.add(w);
8316 } else if (w.mOrientationChanging) {
8317 if (!w.mDrawPending && !w.mCommitDrawPending) {
8318 if (DEBUG_ORIENTATION) Log.v(TAG,
8319 "Orientation not waiting for draw in "
8320 + w + ", surface " + w.mSurface);
8321 w.mOrientationChanging = false;
8322 }
8323 }
8324 }
8325
8326 if (w.mAttachedHidden) {
8327 if (!w.mLastHidden) {
8328 //dump();
8329 w.mLastHidden = true;
8330 if (SHOW_TRANSACTIONS) Log.i(
8331 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-attached)");
8332 if (w.mSurface != null) {
8333 try {
8334 w.mSurface.hide();
8335 } catch (RuntimeException e) {
8336 Log.w(TAG, "Exception hiding surface in " + w);
8337 }
8338 }
8339 mKeyWaiter.releasePendingPointerLocked(w.mSession);
8340 }
8341 // If we are waiting for this window to handle an
8342 // orientation change, well, it is hidden, so
8343 // doesn't really matter. Note that this does
8344 // introduce a potential glitch if the window
8345 // becomes unhidden before it has drawn for the
8346 // new orientation.
8347 if (w.mOrientationChanging) {
8348 w.mOrientationChanging = false;
8349 if (DEBUG_ORIENTATION) Log.v(TAG,
8350 "Orientation change skips hidden " + w);
8351 }
8352 } else if (!w.isReadyForDisplay()) {
8353 if (!w.mLastHidden) {
8354 //dump();
8355 w.mLastHidden = true;
8356 if (SHOW_TRANSACTIONS) Log.i(
8357 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-ready)");
8358 if (w.mSurface != null) {
8359 try {
8360 w.mSurface.hide();
8361 } catch (RuntimeException e) {
8362 Log.w(TAG, "Exception exception hiding surface in " + w);
8363 }
8364 }
8365 mKeyWaiter.releasePendingPointerLocked(w.mSession);
8366 }
8367 // If we are waiting for this window to handle an
8368 // orientation change, well, it is hidden, so
8369 // doesn't really matter. Note that this does
8370 // introduce a potential glitch if the window
8371 // becomes unhidden before it has drawn for the
8372 // new orientation.
8373 if (w.mOrientationChanging) {
8374 w.mOrientationChanging = false;
8375 if (DEBUG_ORIENTATION) Log.v(TAG,
8376 "Orientation change skips hidden " + w);
8377 }
8378 } else if (w.mLastLayer != w.mAnimLayer
8379 || w.mLastAlpha != w.mShownAlpha
8380 || w.mLastDsDx != w.mDsDx
8381 || w.mLastDtDx != w.mDtDx
8382 || w.mLastDsDy != w.mDsDy
8383 || w.mLastDtDy != w.mDtDy
8384 || w.mLastHScale != w.mHScale
8385 || w.mLastVScale != w.mVScale
8386 || w.mLastHidden) {
8387 displayed = true;
8388 w.mLastAlpha = w.mShownAlpha;
8389 w.mLastLayer = w.mAnimLayer;
8390 w.mLastDsDx = w.mDsDx;
8391 w.mLastDtDx = w.mDtDx;
8392 w.mLastDsDy = w.mDsDy;
8393 w.mLastDtDy = w.mDtDy;
8394 w.mLastHScale = w.mHScale;
8395 w.mLastVScale = w.mVScale;
8396 if (SHOW_TRANSACTIONS) Log.i(
8397 TAG, " SURFACE " + w.mSurface + ": alpha="
8398 + w.mShownAlpha + " layer=" + w.mAnimLayer);
8399 if (w.mSurface != null) {
8400 try {
8401 w.mSurface.setAlpha(w.mShownAlpha);
8402 w.mSurface.setLayer(w.mAnimLayer);
8403 w.mSurface.setMatrix(
8404 w.mDsDx*w.mHScale, w.mDtDx*w.mVScale,
8405 w.mDsDy*w.mHScale, w.mDtDy*w.mVScale);
8406 } catch (RuntimeException e) {
8407 Log.w(TAG, "Error updating surface in " + w, e);
8408 if (!recoveringMemory) {
8409 reclaimSomeSurfaceMemoryLocked(w, "update");
8410 }
8411 }
8412 }
8413
8414 if (w.mLastHidden && !w.mDrawPending
8415 && !w.mCommitDrawPending
8416 && !w.mReadyToShow) {
8417 if (SHOW_TRANSACTIONS) Log.i(
8418 TAG, " SURFACE " + w.mSurface + ": SHOW (performLayout)");
8419 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + w
8420 + " during relayout");
8421 if (showSurfaceRobustlyLocked(w)) {
8422 w.mHasDrawn = true;
8423 w.mLastHidden = false;
8424 } else {
8425 w.mOrientationChanging = false;
8426 }
8427 }
8428 if (w.mSurface != null) {
8429 w.mToken.hasVisible = true;
8430 }
8431 } else {
8432 displayed = true;
8433 }
8434
8435 if (displayed) {
8436 if (!covered) {
8437 if (attrs.width == LayoutParams.FILL_PARENT
8438 && attrs.height == LayoutParams.FILL_PARENT) {
8439 covered = true;
8440 }
8441 }
8442 if (w.mOrientationChanging) {
8443 if (w.mDrawPending || w.mCommitDrawPending) {
8444 orientationChangeComplete = false;
8445 if (DEBUG_ORIENTATION) Log.v(TAG,
8446 "Orientation continue waiting for draw in " + w);
8447 } else {
8448 w.mOrientationChanging = false;
8449 if (DEBUG_ORIENTATION) Log.v(TAG,
8450 "Orientation change complete in " + w);
8451 }
8452 }
8453 w.mToken.hasVisible = true;
8454 }
8455 } else if (w.mOrientationChanging) {
8456 if (DEBUG_ORIENTATION) Log.v(TAG,
8457 "Orientation change skips hidden " + w);
8458 w.mOrientationChanging = false;
8459 }
8460
8461 final boolean canBeSeen = w.isDisplayedLw();
8462
8463 if (someoneLosingFocus && w == mCurrentFocus && canBeSeen) {
8464 focusDisplayed = true;
8465 }
8466
8467 // Update effect.
8468 if (!obscured) {
8469 if (w.mSurface != null) {
8470 if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
8471 holdScreen = w.mSession;
8472 }
Dianne Hackborn2f847b92009-03-25 15:25:33 -07008473 if (!syswin && w.mAttrs.screenBrightness >= 0
8474 && screenBrightness < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008475 screenBrightness = w.mAttrs.screenBrightness;
8476 }
Dianne Hackborn2f847b92009-03-25 15:25:33 -07008477 if (attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG
8478 || attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD
8479 || attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_ERROR) {
8480 syswin = true;
8481 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008482 }
Jean-Baptiste Querua8675f62009-07-29 14:25:07 -07008483
8484 boolean opaqueDrawn = w.isOpaqueDrawn();
8485 if (opaqueDrawn && w.isFullscreen(dw, dh)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008486 // This window completely covers everything behind it,
8487 // so we want to leave all of them as unblurred (for
8488 // performance reasons).
8489 obscured = true;
Jean-Baptiste Querua8675f62009-07-29 14:25:07 -07008490 } else if (opaqueDrawn && w.needsBackgroundFiller(dw, dh)) {
8491 if (SHOW_TRANSACTIONS) Log.d(TAG, "showing background filler");
8492 // This window is in compatibility mode, and needs background filler.
8493 obscured = true;
8494 if (mBackgroundFillerSurface == null) {
8495 try {
8496 mBackgroundFillerSurface = new Surface(mFxSession, 0,
8497 0, dw, dh,
8498 PixelFormat.OPAQUE,
8499 Surface.FX_SURFACE_NORMAL);
8500 } catch (Exception e) {
8501 Log.e(TAG, "Exception creating filler surface", e);
8502 }
8503 }
8504 try {
8505 mBackgroundFillerSurface.setPosition(0, 0);
8506 mBackgroundFillerSurface.setSize(dw, dh);
8507 // Using the same layer as Dim because they will never be shown at the
8508 // same time.
8509 mBackgroundFillerSurface.setLayer(w.mAnimLayer - 1);
8510 mBackgroundFillerSurface.show();
8511 } catch (RuntimeException e) {
8512 Log.e(TAG, "Exception showing filler surface");
8513 }
8514 backgroundFillerShown = true;
8515 mBackgroundFillerShown = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008516 } else if (canBeSeen && !obscured &&
8517 (attrFlags&FLAG_BLUR_BEHIND|FLAG_DIM_BEHIND) != 0) {
8518 if (localLOGV) Log.v(TAG, "Win " + w
8519 + ": blurring=" + blurring
8520 + " obscured=" + obscured
8521 + " displayed=" + displayed);
8522 if ((attrFlags&FLAG_DIM_BEHIND) != 0) {
8523 if (!dimming) {
8524 //Log.i(TAG, "DIM BEHIND: " + w);
8525 dimming = true;
Jean-Baptiste Querua8675f62009-07-29 14:25:07 -07008526 if (mDimAnimator == null) {
8527 mDimAnimator = new DimAnimator(mFxSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008528 }
Jean-Baptiste Querua8675f62009-07-29 14:25:07 -07008529 mDimAnimator.show(dw, dh);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008530 }
Jean-Baptiste Querua8675f62009-07-29 14:25:07 -07008531 mDimAnimator.updateParameters(w, currentTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008532 }
8533 if ((attrFlags&FLAG_BLUR_BEHIND) != 0) {
8534 if (!blurring) {
8535 //Log.i(TAG, "BLUR BEHIND: " + w);
8536 blurring = true;
8537 mBlurShown = true;
8538 if (mBlurSurface == null) {
8539 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
8540 + mBlurSurface + ": CREATE");
8541 try {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07008542 mBlurSurface = new Surface(mFxSession, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008543 -1, 16, 16,
8544 PixelFormat.OPAQUE,
8545 Surface.FX_SURFACE_BLUR);
8546 } catch (Exception e) {
8547 Log.e(TAG, "Exception creating Blur surface", e);
8548 }
8549 }
8550 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
8551 + mBlurSurface + ": SHOW pos=(0,0) (" +
8552 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
8553 if (mBlurSurface != null) {
8554 mBlurSurface.setPosition(0, 0);
8555 mBlurSurface.setSize(dw, dh);
8556 try {
8557 mBlurSurface.show();
8558 } catch (RuntimeException e) {
8559 Log.w(TAG, "Failure showing blur surface", e);
8560 }
8561 }
8562 }
8563 mBlurSurface.setLayer(w.mAnimLayer-2);
8564 }
8565 }
8566 }
8567 }
Jean-Baptiste Querua8675f62009-07-29 14:25:07 -07008568
8569 if (backgroundFillerShown == false && mBackgroundFillerShown) {
8570 mBackgroundFillerShown = false;
8571 if (SHOW_TRANSACTIONS) Log.d(TAG, "hiding background filler");
8572 try {
8573 mBackgroundFillerSurface.hide();
8574 } catch (RuntimeException e) {
8575 Log.e(TAG, "Exception hiding filler surface", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008576 }
8577 }
8578
Jean-Baptiste Querua8675f62009-07-29 14:25:07 -07008579 if (mDimAnimator != null && mDimAnimator.mDimShown) {
8580 animating |= mDimAnimator.updateSurface(dimming, currentTime, mDisplayFrozen);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008581 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07008582
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008583 if (!blurring && mBlurShown) {
8584 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR " + mBlurSurface
8585 + ": HIDE");
8586 try {
8587 mBlurSurface.hide();
8588 } catch (IllegalArgumentException e) {
8589 Log.w(TAG, "Illegal argument exception hiding blur surface");
8590 }
8591 mBlurShown = false;
8592 }
8593
8594 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
8595 } catch (RuntimeException e) {
8596 Log.e(TAG, "Unhandled exception in Window Manager", e);
8597 }
8598
8599 Surface.closeTransaction();
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07008600
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008601 if (DEBUG_ORIENTATION && mDisplayFrozen) Log.v(TAG,
8602 "With display frozen, orientationChangeComplete="
8603 + orientationChangeComplete);
8604 if (orientationChangeComplete) {
8605 if (mWindowsFreezingScreen) {
8606 mWindowsFreezingScreen = false;
8607 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
8608 }
8609 if (mAppsFreezingScreen == 0) {
8610 stopFreezingDisplayLocked();
8611 }
8612 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07008613
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008614 i = mResizingWindows.size();
8615 if (i > 0) {
8616 do {
8617 i--;
8618 WindowState win = mResizingWindows.get(i);
8619 try {
8620 win.mClient.resized(win.mFrame.width(),
8621 win.mFrame.height(), win.mLastContentInsets,
8622 win.mLastVisibleInsets, win.mDrawPending);
8623 win.mContentInsetsChanged = false;
8624 win.mVisibleInsetsChanged = false;
8625 } catch (RemoteException e) {
8626 win.mOrientationChanging = false;
8627 }
8628 } while (i > 0);
8629 mResizingWindows.clear();
8630 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07008631
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008632 // Destroy the surface of any windows that are no longer visible.
8633 i = mDestroySurface.size();
8634 if (i > 0) {
8635 do {
8636 i--;
8637 WindowState win = mDestroySurface.get(i);
8638 win.mDestroying = false;
8639 if (mInputMethodWindow == win) {
8640 mInputMethodWindow = null;
8641 }
8642 win.destroySurfaceLocked();
8643 } while (i > 0);
8644 mDestroySurface.clear();
8645 }
8646
8647 // Time to remove any exiting tokens?
8648 for (i=mExitingTokens.size()-1; i>=0; i--) {
8649 WindowToken token = mExitingTokens.get(i);
8650 if (!token.hasVisible) {
8651 mExitingTokens.remove(i);
8652 }
8653 }
8654
8655 // Time to remove any exiting applications?
8656 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
8657 AppWindowToken token = mExitingAppTokens.get(i);
8658 if (!token.hasVisible && !mClosingApps.contains(token)) {
8659 mAppTokens.remove(token);
8660 mExitingAppTokens.remove(i);
8661 }
8662 }
8663
8664 if (focusDisplayed) {
8665 mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
8666 }
8667 if (animating) {
8668 requestAnimationLocked(currentTime+(1000/60)-SystemClock.uptimeMillis());
8669 }
8670 mQueue.setHoldScreenLocked(holdScreen != null);
8671 if (screenBrightness < 0 || screenBrightness > 1.0f) {
8672 mPowerManager.setScreenBrightnessOverride(-1);
8673 } else {
8674 mPowerManager.setScreenBrightnessOverride((int)
8675 (screenBrightness * Power.BRIGHTNESS_ON));
8676 }
8677 if (holdScreen != mHoldingScreenOn) {
8678 mHoldingScreenOn = holdScreen;
8679 Message m = mH.obtainMessage(H.HOLD_SCREEN_CHANGED, holdScreen);
8680 mH.sendMessage(m);
8681 }
8682 }
8683
8684 void requestAnimationLocked(long delay) {
8685 if (!mAnimationPending) {
8686 mAnimationPending = true;
8687 mH.sendMessageDelayed(mH.obtainMessage(H.ANIMATE), delay);
8688 }
8689 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07008690
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008691 /**
8692 * Have the surface flinger show a surface, robustly dealing with
8693 * error conditions. In particular, if there is not enough memory
8694 * to show the surface, then we will try to get rid of other surfaces
8695 * in order to succeed.
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07008696 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008697 * @return Returns true if the surface was successfully shown.
8698 */
8699 boolean showSurfaceRobustlyLocked(WindowState win) {
8700 try {
8701 if (win.mSurface != null) {
8702 win.mSurface.show();
8703 }
8704 return true;
8705 } catch (RuntimeException e) {
8706 Log.w(TAG, "Failure showing surface " + win.mSurface + " in " + win);
8707 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07008708
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008709 reclaimSomeSurfaceMemoryLocked(win, "show");
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07008710
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008711 return false;
8712 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07008713
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008714 void reclaimSomeSurfaceMemoryLocked(WindowState win, String operation) {
8715 final Surface surface = win.mSurface;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07008716
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008717 EventLog.writeEvent(LOG_WM_NO_SURFACE_MEMORY, win.toString(),
8718 win.mSession.mPid, operation);
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07008719
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008720 if (mForceRemoves == null) {
8721 mForceRemoves = new ArrayList<WindowState>();
8722 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07008723
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008724 long callingIdentity = Binder.clearCallingIdentity();
8725 try {
8726 // There was some problem... first, do a sanity check of the
8727 // window list to make sure we haven't left any dangling surfaces
8728 // around.
8729 int N = mWindows.size();
8730 boolean leakedSurface = false;
8731 Log.i(TAG, "Out of memory for surface! Looking for leaks...");
8732 for (int i=0; i<N; i++) {
8733 WindowState ws = (WindowState)mWindows.get(i);
8734 if (ws.mSurface != null) {
8735 if (!mSessions.contains(ws.mSession)) {
8736 Log.w(TAG, "LEAKED SURFACE (session doesn't exist): "
8737 + ws + " surface=" + ws.mSurface
8738 + " token=" + win.mToken
8739 + " pid=" + ws.mSession.mPid
8740 + " uid=" + ws.mSession.mUid);
8741 ws.mSurface.clear();
8742 ws.mSurface = null;
8743 mForceRemoves.add(ws);
8744 i--;
8745 N--;
8746 leakedSurface = true;
8747 } else if (win.mAppToken != null && win.mAppToken.clientHidden) {
8748 Log.w(TAG, "LEAKED SURFACE (app token hidden): "
8749 + ws + " surface=" + ws.mSurface
8750 + " token=" + win.mAppToken);
8751 ws.mSurface.clear();
8752 ws.mSurface = null;
8753 leakedSurface = true;
8754 }
8755 }
8756 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07008757
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008758 boolean killedApps = false;
8759 if (!leakedSurface) {
8760 Log.w(TAG, "No leaked surfaces; killing applicatons!");
8761 SparseIntArray pidCandidates = new SparseIntArray();
8762 for (int i=0; i<N; i++) {
8763 WindowState ws = (WindowState)mWindows.get(i);
8764 if (ws.mSurface != null) {
8765 pidCandidates.append(ws.mSession.mPid, ws.mSession.mPid);
8766 }
8767 }
8768 if (pidCandidates.size() > 0) {
8769 int[] pids = new int[pidCandidates.size()];
8770 for (int i=0; i<pids.length; i++) {
8771 pids[i] = pidCandidates.keyAt(i);
8772 }
8773 try {
8774 if (mActivityManager.killPidsForMemory(pids)) {
8775 killedApps = true;
8776 }
8777 } catch (RemoteException e) {
8778 }
8779 }
8780 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07008781
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008782 if (leakedSurface || killedApps) {
8783 // We managed to reclaim some memory, so get rid of the trouble
8784 // surface and ask the app to request another one.
8785 Log.w(TAG, "Looks like we have reclaimed some memory, clearing surface for retry.");
8786 if (surface != null) {
8787 surface.clear();
8788 win.mSurface = null;
8789 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07008790
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008791 try {
8792 win.mClient.dispatchGetNewSurface();
8793 } catch (RemoteException e) {
8794 }
8795 }
8796 } finally {
8797 Binder.restoreCallingIdentity(callingIdentity);
8798 }
8799 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07008800
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008801 private boolean updateFocusedWindowLocked(int mode) {
8802 WindowState newFocus = computeFocusedWindowLocked();
8803 if (mCurrentFocus != newFocus) {
8804 // This check makes sure that we don't already have the focus
8805 // change message pending.
8806 mH.removeMessages(H.REPORT_FOCUS_CHANGE);
8807 mH.sendEmptyMessage(H.REPORT_FOCUS_CHANGE);
8808 if (localLOGV) Log.v(
8809 TAG, "Changing focus from " + mCurrentFocus + " to " + newFocus);
8810 final WindowState oldFocus = mCurrentFocus;
8811 mCurrentFocus = newFocus;
8812 mLosingFocus.remove(newFocus);
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07008813
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008814 final WindowState imWindow = mInputMethodWindow;
8815 if (newFocus != imWindow && oldFocus != imWindow) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08008816 if (moveInputMethodWindowsIfNeededLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008817 mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS &&
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08008818 mode != UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
8819 mLayoutNeeded = true;
8820 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008821 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
8822 performLayoutLockedInner();
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08008823 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
8824 // Client will do the layout, but we need to assign layers
8825 // for handleNewWindowLocked() below.
8826 assignLayersLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008827 }
8828 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07008829
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08008830 if (newFocus != null && mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
8831 mKeyWaiter.handleNewWindowLocked(newFocus);
8832 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008833 return true;
8834 }
8835 return false;
8836 }
8837
8838 private WindowState computeFocusedWindowLocked() {
8839 WindowState result = null;
8840 WindowState win;
8841
8842 int i = mWindows.size() - 1;
8843 int nextAppIndex = mAppTokens.size()-1;
8844 WindowToken nextApp = nextAppIndex >= 0
8845 ? mAppTokens.get(nextAppIndex) : null;
8846
8847 while (i >= 0) {
8848 win = (WindowState)mWindows.get(i);
8849
8850 if (localLOGV || DEBUG_FOCUS) Log.v(
8851 TAG, "Looking for focus: " + i
8852 + " = " + win
8853 + ", flags=" + win.mAttrs.flags
8854 + ", canReceive=" + win.canReceiveKeys());
8855
8856 AppWindowToken thisApp = win.mAppToken;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07008857
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008858 // If this window's application has been removed, just skip it.
8859 if (thisApp != null && thisApp.removed) {
8860 i--;
8861 continue;
8862 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07008863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008864 // If there is a focused app, don't allow focus to go to any
8865 // windows below it. If this is an application window, step
8866 // through the app tokens until we find its app.
8867 if (thisApp != null && nextApp != null && thisApp != nextApp
8868 && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
8869 int origAppIndex = nextAppIndex;
8870 while (nextAppIndex > 0) {
8871 if (nextApp == mFocusedApp) {
8872 // Whoops, we are below the focused app... no focus
8873 // for you!
8874 if (localLOGV || DEBUG_FOCUS) Log.v(
8875 TAG, "Reached focused app: " + mFocusedApp);
8876 return null;
8877 }
8878 nextAppIndex--;
8879 nextApp = mAppTokens.get(nextAppIndex);
8880 if (nextApp == thisApp) {
8881 break;
8882 }
8883 }
8884 if (thisApp != nextApp) {
8885 // Uh oh, the app token doesn't exist! This shouldn't
8886 // happen, but if it does we can get totally hosed...
8887 // so restart at the original app.
8888 nextAppIndex = origAppIndex;
8889 nextApp = mAppTokens.get(nextAppIndex);
8890 }
8891 }
8892
8893 // Dispatch to this window if it is wants key events.
8894 if (win.canReceiveKeys()) {
8895 if (DEBUG_FOCUS) Log.v(
8896 TAG, "Found focus @ " + i + " = " + win);
8897 result = win;
8898 break;
8899 }
8900
8901 i--;
8902 }
8903
8904 return result;
8905 }
8906
8907 private void startFreezingDisplayLocked() {
8908 if (mDisplayFrozen) {
Chris Tate0f8fc102009-03-25 16:22:54 -07008909 // Freezing the display also suspends key event delivery, to
8910 // keep events from going astray while the display is reconfigured.
8911 // If someone has changed orientation again while the screen is
8912 // still frozen, the events will continue to be blocked while the
8913 // successive orientation change is processed. To prevent spurious
8914 // ANRs, we reset the event dispatch timeout in this case.
8915 synchronized (mKeyWaiter) {
8916 mKeyWaiter.mWasFrozen = true;
8917 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008918 return;
8919 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07008920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008921 mScreenFrozenLock.acquire();
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07008922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008923 long now = SystemClock.uptimeMillis();
8924 //Log.i(TAG, "Freezing, gc pending: " + mFreezeGcPending + ", now " + now);
8925 if (mFreezeGcPending != 0) {
8926 if (now > (mFreezeGcPending+1000)) {
8927 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
8928 mH.removeMessages(H.FORCE_GC);
8929 Runtime.getRuntime().gc();
8930 mFreezeGcPending = now;
8931 }
8932 } else {
8933 mFreezeGcPending = now;
8934 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07008935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008936 mDisplayFrozen = true;
8937 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
8938 mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
8939 mAppTransitionReady = true;
8940 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07008941
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008942 if (PROFILE_ORIENTATION) {
8943 File file = new File("/data/system/frozen");
8944 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
8945 }
8946 Surface.freezeDisplay(0);
8947 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07008948
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008949 private void stopFreezingDisplayLocked() {
8950 if (!mDisplayFrozen) {
8951 return;
8952 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07008953
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008954 mDisplayFrozen = false;
8955 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
8956 if (PROFILE_ORIENTATION) {
8957 Debug.stopMethodTracing();
8958 }
8959 Surface.unfreezeDisplay(0);
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07008960
Chris Tate0f8fc102009-03-25 16:22:54 -07008961 // Reset the key delivery timeout on unfreeze, too. We force a wakeup here
8962 // too because regular key delivery processing should resume immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008963 synchronized (mKeyWaiter) {
8964 mKeyWaiter.mWasFrozen = true;
8965 mKeyWaiter.notifyAll();
8966 }
8967
8968 // A little kludge: a lot could have happened while the
8969 // display was frozen, so now that we are coming back we
8970 // do a gc so that any remote references the system
8971 // processes holds on others can be released if they are
8972 // no longer needed.
8973 mH.removeMessages(H.FORCE_GC);
8974 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
8975 2000);
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07008976
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008977 mScreenFrozenLock.release();
8978 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07008979
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008980 @Override
8981 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
8982 if (mContext.checkCallingOrSelfPermission("android.permission.DUMP")
8983 != PackageManager.PERMISSION_GRANTED) {
8984 pw.println("Permission Denial: can't dump WindowManager from from pid="
8985 + Binder.getCallingPid()
8986 + ", uid=" + Binder.getCallingUid());
8987 return;
8988 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -07008989
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008990 synchronized(mWindowMap) {
8991 pw.println("Current Window Manager state:");
8992 for (int i=mWindows.size()-1; i>=0; i--) {
8993 WindowState w = (WindowState)mWindows.get(i);
Jean-Baptiste Queru843ef362009-05-20 11:28:04 -07008994 pw.print(" Window #"); 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 if (mInputMethodDialogs.size() > 0) {
8999 pw.println(" ");
9000 pw.println(" Input method dialogs:");
9001 for (int i=mInputMethodDialogs.size()-1; i>=0; i--) {
9002 WindowState w = mInputMethodDialogs.get(i);
Jean-Baptiste Queru843ef362009-05-20 11:28:04 -07009003 pw.print(" IM Dialog #"); pw.print(i); pw.print(": "); pw.println(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009004 }
9005 }
9006 if (mPendingRemove.size() > 0) {
9007 pw.println(" ");
9008 pw.println(" Remove pending for:");
9009 for (int i=mPendingRemove.size()-1; i>=0; i--) {
9010 WindowState w = mPendingRemove.get(i);
Jean-Baptiste Queru843ef362009-05-20 11:28:04 -07009011 pw.print(" Remove #"); pw.print(i); pw.print(' ');
9012 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009013 w.dump(pw, " ");
9014 }
9015 }
9016 if (mForceRemoves != null && mForceRemoves.size() > 0) {
9017 pw.println(" ");
9018 pw.println(" Windows force removing:");
9019 for (int i=mForceRemoves.size()-1; i>=0; i--) {
9020 WindowState w = mForceRemoves.get(i);
Jean-Baptiste Queru843ef362009-05-20 11:28:04 -07009021 pw.print(" Removing #"); pw.print(i); pw.print(' ');
9022 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009023 w.dump(pw, " ");
9024 }
9025 }
9026 if (mDestroySurface.size() > 0) {
9027 pw.println(" ");
9028 pw.println(" Windows waiting to destroy their surface:");
9029 for (int i=mDestroySurface.size()-1; i>=0; i--) {
9030 WindowState w = mDestroySurface.get(i);
Jean-Baptiste Queru843ef362009-05-20 11:28:04 -07009031 pw.print(" Destroy #"); pw.print(i); pw.print(' ');
9032 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009033 w.dump(pw, " ");
9034 }
9035 }
9036 if (mLosingFocus.size() > 0) {
9037 pw.println(" ");
9038 pw.println(" Windows losing focus:");
9039 for (int i=mLosingFocus.size()-1; i>=0; i--) {
9040 WindowState w = mLosingFocus.get(i);
Jean-Baptiste Queru843ef362009-05-20 11:28:04 -07009041 pw.print(" Losing #"); pw.print(i); pw.print(' ');
9042 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009043 w.dump(pw, " ");
9044 }
9045 }
9046 if (mSessions.size() > 0) {
9047 pw.println(" ");
9048 pw.println(" All active sessions:");
9049 Iterator<Session> it = mSessions.iterator();
9050 while (it.hasNext()) {
9051 Session s = it.next();
Jean-Baptiste Queru843ef362009-05-20 11:28:04 -07009052 pw.print(" Session "); pw.print(s); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009053 s.dump(pw, " ");
9054 }
9055 }
9056 if (mTokenMap.size() > 0) {
9057 pw.println(" ");
9058 pw.println(" All tokens:");
9059 Iterator<WindowToken> it = mTokenMap.values().iterator();
9060 while (it.hasNext()) {
9061 WindowToken token = it.next();
Jean-Baptiste Queru843ef362009-05-20 11:28:04 -07009062 pw.print(" Token "); pw.print(token.token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009063 token.dump(pw, " ");
9064 }
9065 }
9066 if (mTokenList.size() > 0) {
9067 pw.println(" ");
9068 pw.println(" Window token list:");
9069 for (int i=0; i<mTokenList.size(); i++) {
Jean-Baptiste Queru843ef362009-05-20 11:28:04 -07009070 pw.print(" #"); pw.print(i); pw.print(": ");
9071 pw.println(mTokenList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009072 }
9073 }
9074 if (mAppTokens.size() > 0) {
9075 pw.println(" ");
9076 pw.println(" Application tokens in Z order:");
9077 for (int i=mAppTokens.size()-1; i>=0; i--) {
Jean-Baptiste Queru843ef362009-05-20 11:28:04 -07009078 pw.print(" App #"); pw.print(i); pw.print(": ");
9079 pw.println(mAppTokens.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009080 }
9081 }
9082 if (mFinishedStarting.size() > 0) {
9083 pw.println(" ");
9084 pw.println(" Finishing start of application tokens:");
9085 for (int i=mFinishedStarting.size()-1; i>=0; i--) {
9086 WindowToken token = mFinishedStarting.get(i);
Jean-Baptiste Queru843ef362009-05-20 11:28:04 -07009087 pw.print(" Finished Starting #"); pw.print(i);
9088 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009089 token.dump(pw, " ");
9090 }
9091 }
9092 if (mExitingTokens.size() > 0) {
9093 pw.println(" ");
9094 pw.println(" Exiting tokens:");
9095 for (int i=mExitingTokens.size()-1; i>=0; i--) {
9096 WindowToken token = mExitingTokens.get(i);
Jean-Baptiste Queru843ef362009-05-20 11:28:04 -07009097 pw.print(" Exiting #"); pw.print(i);
9098 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009099 token.dump(pw, " ");
9100 }
9101 }
9102 if (mExitingAppTokens.size() > 0) {
9103 pw.println(" ");
9104 pw.println(" Exiting application tokens:");
9105 for (int i=mExitingAppTokens.size()-1; i>=0; i--) {
9106 WindowToken token = mExitingAppTokens.get(i);
Jean-Baptiste Queru843ef362009-05-20 11:28:04 -07009107 pw.print(" Exiting App #"); pw.print(i);
9108 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009109 token.dump(pw, " ");
9110 }
9111 }
9112 pw.println(" ");
Jean-Baptiste Queru843ef362009-05-20 11:28:04 -07009113 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
9114 pw.print(" mLastFocus="); pw.println(mLastFocus);
9115 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
9116 pw.print(" mInputMethodTarget="); pw.println(mInputMethodTarget);
9117 pw.print(" mInputMethodWindow="); pw.println(mInputMethodWindow);
9118 pw.print(" mInTouchMode="); pw.println(mInTouchMode);
9119 pw.print(" mSystemBooted="); pw.print(mSystemBooted);
9120 pw.print(" mDisplayEnabled="); pw.println(mDisplayEnabled);
9121 pw.print(" mLayoutNeeded="); pw.print(mLayoutNeeded);
9122 pw.print(" mBlurShown="); pw.println(mBlurShown);
Jean-Baptiste Querua8675f62009-07-29 14:25:07 -07009123 if (mDimAnimator != null) {
9124 mDimAnimator.printTo(pw);
9125 } else {
9126 pw.print( " no DimAnimator ");
9127 }
Jean-Baptiste Queru843ef362009-05-20 11:28:04 -07009128 pw.print(" mInputMethodAnimLayerAdjustment=");
9129 pw.println(mInputMethodAnimLayerAdjustment);
9130 pw.print(" mDisplayFrozen="); pw.print(mDisplayFrozen);
9131 pw.print(" mWindowsFreezingScreen="); pw.print(mWindowsFreezingScreen);
9132 pw.print(" mAppsFreezingScreen="); pw.println(mAppsFreezingScreen);
9133 pw.print(" mRotation="); pw.print(mRotation);
9134 pw.print(", mForcedAppOrientation="); pw.print(mForcedAppOrientation);
9135 pw.print(", mRequestedRotation="); pw.println(mRequestedRotation);
9136 pw.print(" mAnimationPending="); pw.print(mAnimationPending);
9137 pw.print(" mWindowAnimationScale="); pw.print(mWindowAnimationScale);
9138 pw.print(" mTransitionWindowAnimationScale="); pw.println(mTransitionAnimationScale);
9139 pw.print(" mNextAppTransition=0x");
9140 pw.print(Integer.toHexString(mNextAppTransition));
9141 pw.print(", mAppTransitionReady="); pw.print(mAppTransitionReady);
9142 pw.print(", mAppTransitionTimeout="); pw.println( mAppTransitionTimeout);
9143 pw.print(" mStartingIconInTransition="); pw.print(mStartingIconInTransition);
9144 pw.print(", mSkipAppTransitionAnimation="); pw.println(mSkipAppTransitionAnimation);
9145 if (mOpeningApps.size() > 0) {
9146 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
9147 }
9148 if (mClosingApps.size() > 0) {
9149 pw.print(" mClosingApps="); pw.println(mClosingApps);
9150 }
9151 pw.print(" DisplayWidth="); pw.print(mDisplay.getWidth());
9152 pw.print(" DisplayHeight="); pw.println(mDisplay.getHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009153 pw.println(" KeyWaiter state:");
Jean-Baptiste Queru843ef362009-05-20 11:28:04 -07009154 pw.print(" mLastWin="); pw.print(mKeyWaiter.mLastWin);
9155 pw.print(" mLastBinder="); pw.println(mKeyWaiter.mLastBinder);
9156 pw.print(" mFinished="); pw.print(mKeyWaiter.mFinished);
9157 pw.print(" mGotFirstWindow="); pw.print(mKeyWaiter.mGotFirstWindow);
9158 pw.print(" mEventDispatching="); pw.print(mKeyWaiter.mEventDispatching);
9159 pw.print(" mTimeToSwitch="); pw.println(mKeyWaiter.mTimeToSwitch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009160 }
9161 }
9162
9163 public void monitor() {
9164 synchronized (mWindowMap) { }
9165 synchronized (mKeyguardDisabled) { }
9166 synchronized (mKeyWaiter) { }
9167 }
Jean-Baptiste Querua8675f62009-07-29 14:25:07 -07009168
9169 /**
9170 * DimAnimator class that controls the dim animation. This holds the surface and
9171 * all state used for dim animation.
9172 */
9173 private static class DimAnimator {
9174 Surface mDimSurface;
9175 boolean mDimShown = false;
9176 float mDimCurrentAlpha;
9177 float mDimTargetAlpha;
9178 float mDimDeltaPerMs;
9179 long mLastDimAnimTime;
9180
9181 DimAnimator (SurfaceSession session) {
9182 if (mDimSurface == null) {
9183 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
9184 + mDimSurface + ": CREATE");
9185 try {
9186 mDimSurface = new Surface(session, 0, -1, 16, 16, PixelFormat.OPAQUE,
9187 Surface.FX_SURFACE_DIM);
9188 } catch (Exception e) {
9189 Log.e(TAG, "Exception creating Dim surface", e);
9190 }
9191 }
9192 }
9193
9194 /**
9195 * Show the dim surface.
9196 */
9197 void show(int dw, int dh) {
9198 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface + ": SHOW pos=(0,0) (" +
9199 dw + "x" + dh + ")");
9200 mDimShown = true;
9201 try {
9202 mDimSurface.setPosition(0, 0);
9203 mDimSurface.setSize(dw, dh);
9204 mDimSurface.show();
9205 } catch (RuntimeException e) {
9206 Log.w(TAG, "Failure showing dim surface", e);
9207 }
9208 }
9209
9210 /**
9211 * Set's the dim surface's layer and update dim parameters that will be used in
9212 * {@link updateSurface} after all windows are examined.
9213 */
9214 void updateParameters(WindowState w, long currentTime) {
9215 mDimSurface.setLayer(w.mAnimLayer-1);
9216
9217 final float target = w.mExiting ? 0 : w.mAttrs.dimAmount;
9218 if (SHOW_TRANSACTIONS) Log.i(TAG, "layer=" + (w.mAnimLayer-1) + ", target=" + target);
9219 if (mDimTargetAlpha != target) {
9220 // If the desired dim level has changed, then
9221 // start an animation to it.
9222 mLastDimAnimTime = currentTime;
9223 long duration = (w.mAnimating && w.mAnimation != null)
9224 ? w.mAnimation.computeDurationHint()
9225 : DEFAULT_DIM_DURATION;
9226 if (target > mDimTargetAlpha) {
9227 // This is happening behind the activity UI,
9228 // so we can make it run a little longer to
9229 // give a stronger impression without disrupting
9230 // the user.
9231 duration *= DIM_DURATION_MULTIPLIER;
9232 }
9233 if (duration < 1) {
9234 // Don't divide by zero
9235 duration = 1;
9236 }
9237 mDimTargetAlpha = target;
9238 mDimDeltaPerMs = (mDimTargetAlpha-mDimCurrentAlpha) / duration;
9239 }
9240 }
9241
9242 /**
9243 * Updating the surface's alpha. Returns true if the animation continues, or returns
9244 * false when the animation is finished and the dim surface is hidden.
9245 */
9246 boolean updateSurface(boolean dimming, long currentTime, boolean displayFrozen) {
9247 if (!dimming) {
9248 if (mDimTargetAlpha != 0) {
9249 mLastDimAnimTime = currentTime;
9250 mDimTargetAlpha = 0;
9251 mDimDeltaPerMs = (-mDimCurrentAlpha) / DEFAULT_DIM_DURATION;
9252 }
9253 }
9254
9255 boolean animating = false;
9256 if (mLastDimAnimTime != 0) {
9257 mDimCurrentAlpha += mDimDeltaPerMs
9258 * (currentTime-mLastDimAnimTime);
9259 boolean more = true;
9260 if (displayFrozen) {
9261 // If the display is frozen, there is no reason to animate.
9262 more = false;
9263 } else if (mDimDeltaPerMs > 0) {
9264 if (mDimCurrentAlpha > mDimTargetAlpha) {
9265 more = false;
9266 }
9267 } else if (mDimDeltaPerMs < 0) {
9268 if (mDimCurrentAlpha < mDimTargetAlpha) {
9269 more = false;
9270 }
9271 } else {
9272 more = false;
9273 }
9274
9275 // Do we need to continue animating?
9276 if (more) {
9277 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
9278 + mDimSurface + ": alpha=" + mDimCurrentAlpha);
9279 mLastDimAnimTime = currentTime;
9280 mDimSurface.setAlpha(mDimCurrentAlpha);
9281 animating = true;
9282 } else {
9283 mDimCurrentAlpha = mDimTargetAlpha;
9284 mLastDimAnimTime = 0;
9285 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
9286 + mDimSurface + ": final alpha=" + mDimCurrentAlpha);
9287 mDimSurface.setAlpha(mDimCurrentAlpha);
9288 if (!dimming) {
9289 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
9290 + ": HIDE");
9291 try {
9292 mDimSurface.hide();
9293 } catch (RuntimeException e) {
9294 Log.w(TAG, "Illegal argument exception hiding dim surface");
9295 }
9296 mDimShown = false;
9297 }
9298 }
9299 }
9300 return animating;
9301 }
9302
9303 public void printTo(PrintWriter pw) {
9304 pw.print(" mDimShown="); pw.print(mDimShown);
9305 pw.print(" current="); pw.print(mDimCurrentAlpha);
9306 pw.print(" target="); pw.print(mDimTargetAlpha);
9307 pw.print(" delta="); pw.print(mDimDeltaPerMs);
9308 pw.print(" lastAnimTime="); pw.println(mLastDimAnimTime);
9309 }
9310 }
9311
9312 /**
9313 * Animation that fade in after 0.5 interpolate time, or fade out in reverse order.
9314 * This is used for opening/closing transition for apps in compatible mode.
9315 */
9316 private static class FadeInOutAnimation extends Animation {
9317 int mWidth;
9318 boolean mFadeIn;
9319
9320 public FadeInOutAnimation(boolean fadeIn) {
9321 setInterpolator(new AccelerateInterpolator());
9322 setDuration(DEFAULT_FADE_IN_OUT_DURATION);
9323 mFadeIn = fadeIn;
9324 }
9325
9326 @Override
9327 protected void applyTransformation(float interpolatedTime, Transformation t) {
9328 float x = interpolatedTime;
9329 if (!mFadeIn) {
9330 x = 1.0f - x; // reverse the interpolation for fade out
9331 }
9332 if (x < 0.5) {
9333 // move the window out of the screen.
9334 t.getMatrix().setTranslate(mWidth, 0);
9335 } else {
9336 t.getMatrix().setTranslate(0, 0);// show
9337 t.setAlpha((x - 0.5f) * 2);
9338 }
9339 }
9340
9341 @Override
9342 public void initialize(int width, int height, int parentWidth, int parentHeight) {
9343 // width is the screen width {@see AppWindowToken#stepAnimatinoLocked}
9344 mWidth = width;
9345 }
9346
9347 @Override
9348 public int getZAdjustment() {
9349 return Animation.ZORDER_TOP;
9350 }
9351 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009352}