blob: 2937ed00b45a72a21a499db4bf0216bb18235b80 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
19import static android.os.LocalPowerManager.CHEEK_EVENT;
20import static android.os.LocalPowerManager.OTHER_EVENT;
21import static android.os.LocalPowerManager.TOUCH_EVENT;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -070022import static android.os.LocalPowerManager.LONG_TOUCH_EVENT;
23import static android.os.LocalPowerManager.TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import static android.view.WindowManager.LayoutParams.FIRST_APPLICATION_WINDOW;
25import static android.view.WindowManager.LayoutParams.FIRST_SUB_WINDOW;
26import static android.view.WindowManager.LayoutParams.FLAG_BLUR_BEHIND;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070027import static android.view.WindowManager.LayoutParams.FLAG_COMPATIBLE_WINDOW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import static android.view.WindowManager.LayoutParams.FLAG_DIM_BEHIND;
29import static android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -070030import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import static android.view.WindowManager.LayoutParams.FLAG_SYSTEM_ERROR;
32import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
33import static android.view.WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
34import static android.view.WindowManager.LayoutParams.LAST_APPLICATION_WINDOW;
35import static android.view.WindowManager.LayoutParams.LAST_SUB_WINDOW;
36import static android.view.WindowManager.LayoutParams.MEMORY_TYPE_GPU;
37import static android.view.WindowManager.LayoutParams.MEMORY_TYPE_HARDWARE;
38import static android.view.WindowManager.LayoutParams.MEMORY_TYPE_PUSH_BUFFERS;
39import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
40import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
41import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
42import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
43
44import com.android.internal.app.IBatteryStats;
45import com.android.internal.policy.PolicyManager;
46import com.android.internal.view.IInputContext;
47import com.android.internal.view.IInputMethodClient;
48import com.android.internal.view.IInputMethodManager;
49import com.android.server.KeyInputQueue.QueuedEvent;
50import com.android.server.am.BatteryStatsService;
51
52import android.Manifest;
53import android.app.ActivityManagerNative;
54import android.app.IActivityManager;
55import android.content.Context;
56import android.content.pm.ActivityInfo;
57import android.content.pm.PackageManager;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070058import android.content.res.CompatibilityInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.content.res.Configuration;
60import android.graphics.Matrix;
61import android.graphics.PixelFormat;
62import android.graphics.Rect;
63import android.graphics.Region;
64import android.os.BatteryStats;
65import android.os.Binder;
66import android.os.Debug;
67import android.os.Handler;
68import android.os.IBinder;
69import android.os.LocalPowerManager;
70import android.os.Looper;
71import android.os.Message;
72import android.os.Parcel;
73import android.os.ParcelFileDescriptor;
74import android.os.Power;
75import android.os.PowerManager;
76import android.os.Process;
77import android.os.RemoteException;
78import android.os.ServiceManager;
79import android.os.SystemClock;
80import android.os.SystemProperties;
81import android.os.TokenWatcher;
82import android.provider.Settings;
Dianne Hackborn723738c2009-06-25 19:48:04 -070083import android.util.DisplayMetrics;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084import android.util.EventLog;
85import android.util.Log;
86import android.util.SparseIntArray;
87import android.view.Display;
88import android.view.Gravity;
89import android.view.IApplicationToken;
90import android.view.IOnKeyguardExitResult;
91import android.view.IRotationWatcher;
92import android.view.IWindow;
93import android.view.IWindowManager;
94import android.view.IWindowSession;
95import android.view.KeyEvent;
96import android.view.MotionEvent;
97import android.view.RawInputEvent;
98import android.view.Surface;
99import android.view.SurfaceSession;
100import android.view.View;
101import android.view.ViewTreeObserver;
102import android.view.WindowManager;
103import android.view.WindowManagerImpl;
104import android.view.WindowManagerPolicy;
105import android.view.WindowManager.LayoutParams;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700106import android.view.animation.AccelerateInterpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107import android.view.animation.Animation;
108import android.view.animation.AnimationUtils;
109import android.view.animation.Transformation;
110
111import java.io.BufferedWriter;
112import java.io.File;
113import java.io.FileDescriptor;
114import java.io.IOException;
115import java.io.OutputStream;
116import java.io.OutputStreamWriter;
117import java.io.PrintWriter;
118import java.io.StringWriter;
119import java.net.Socket;
120import java.util.ArrayList;
121import java.util.HashMap;
122import java.util.HashSet;
123import java.util.Iterator;
124import java.util.List;
125
126/** {@hide} */
127public class WindowManagerService extends IWindowManager.Stub implements Watchdog.Monitor {
128 static final String TAG = "WindowManager";
129 static final boolean DEBUG = false;
130 static final boolean DEBUG_FOCUS = false;
131 static final boolean DEBUG_ANIM = false;
132 static final boolean DEBUG_LAYERS = false;
133 static final boolean DEBUG_INPUT = false;
134 static final boolean DEBUG_INPUT_METHOD = false;
135 static final boolean DEBUG_VISIBILITY = false;
136 static final boolean DEBUG_ORIENTATION = false;
137 static final boolean DEBUG_APP_TRANSITIONS = false;
138 static final boolean DEBUG_STARTING_WINDOW = false;
139 static final boolean DEBUG_REORDER = false;
140 static final boolean SHOW_TRANSACTIONS = false;
Romain Guy06882f82009-06-10 13:36:04 -0700141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 static final boolean PROFILE_ORIENTATION = false;
143 static final boolean BLUR = true;
Dave Bortcfe65242009-04-09 14:51:04 -0700144 static final boolean localLOGV = DEBUG;
Romain Guy06882f82009-06-10 13:36:04 -0700145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 static final int LOG_WM_NO_SURFACE_MEMORY = 31000;
Romain Guy06882f82009-06-10 13:36:04 -0700147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 /** How long to wait for first key repeat, in milliseconds */
149 static final int KEY_REPEAT_FIRST_DELAY = 750;
Romain Guy06882f82009-06-10 13:36:04 -0700150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 /** How long to wait for subsequent key repeats, in milliseconds */
152 static final int KEY_REPEAT_DELAY = 50;
153
154 /** How much to multiply the policy's type layer, to reserve room
155 * for multiple windows of the same type and Z-ordering adjustment
156 * with TYPE_LAYER_OFFSET. */
157 static final int TYPE_LAYER_MULTIPLIER = 10000;
Romain Guy06882f82009-06-10 13:36:04 -0700158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 /** Offset from TYPE_LAYER_MULTIPLIER for moving a group of windows above
160 * or below others in the same layer. */
161 static final int TYPE_LAYER_OFFSET = 1000;
Romain Guy06882f82009-06-10 13:36:04 -0700162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 /** How much to increment the layer for each window, to reserve room
164 * for effect surfaces between them.
165 */
166 static final int WINDOW_LAYER_MULTIPLIER = 5;
Romain Guy06882f82009-06-10 13:36:04 -0700167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 /** The maximum length we will accept for a loaded animation duration:
169 * this is 10 seconds.
170 */
171 static final int MAX_ANIMATION_DURATION = 10*1000;
172
173 /** Amount of time (in milliseconds) to animate the dim surface from one
174 * value to another, when no window animation is driving it.
175 */
176 static final int DEFAULT_DIM_DURATION = 200;
177
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700178 /** Amount of time (in milliseconds) to animate the fade-in-out transition for
179 * compatible windows.
180 */
181 static final int DEFAULT_FADE_IN_OUT_DURATION = 400;
182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 /** Adjustment to time to perform a dim, to make it more dramatic.
184 */
185 static final int DIM_DURATION_MULTIPLIER = 6;
Romain Guy06882f82009-06-10 13:36:04 -0700186
Dianne Hackborncfaef692009-06-15 14:24:44 -0700187 static final int INJECT_FAILED = 0;
188 static final int INJECT_SUCCEEDED = 1;
189 static final int INJECT_NO_PERMISSION = -1;
190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 static final int UPDATE_FOCUS_NORMAL = 0;
192 static final int UPDATE_FOCUS_WILL_ASSIGN_LAYERS = 1;
193 static final int UPDATE_FOCUS_PLACING_SURFACES = 2;
194 static final int UPDATE_FOCUS_WILL_PLACE_SURFACES = 3;
Romain Guy06882f82009-06-10 13:36:04 -0700195
Michael Chane96440f2009-05-06 10:27:36 -0700196 /** The minimum time between dispatching touch events. */
197 int mMinWaitTimeBetweenTouchEvents = 1000 / 35;
198
199 // Last touch event time
200 long mLastTouchEventTime = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700201
Michael Chane96440f2009-05-06 10:27:36 -0700202 // Last touch event type
203 int mLastTouchEventType = OTHER_EVENT;
Romain Guy06882f82009-06-10 13:36:04 -0700204
Michael Chane96440f2009-05-06 10:27:36 -0700205 // Time to wait before calling useractivity again. This saves CPU usage
206 // when we get a flood of touch events.
207 static final int MIN_TIME_BETWEEN_USERACTIVITIES = 1000;
208
209 // Last time we call user activity
210 long mLastUserActivityCallTime = 0;
211
Romain Guy06882f82009-06-10 13:36:04 -0700212 // Last time we updated battery stats
Michael Chane96440f2009-05-06 10:27:36 -0700213 long mLastBatteryStatsCallTime = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 private static final String SYSTEM_SECURE = "ro.secure";
Romain Guy06882f82009-06-10 13:36:04 -0700216 private static final String SYSTEM_DEBUGGABLE = "ro.debuggable";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217
218 /**
219 * Condition waited on by {@link #reenableKeyguard} to know the call to
220 * the window policy has finished.
221 */
222 private boolean mWaitingUntilKeyguardReenabled = false;
223
224
225 final TokenWatcher mKeyguardDisabled = new TokenWatcher(
226 new Handler(), "WindowManagerService.mKeyguardDisabled") {
227 public void acquired() {
228 mPolicy.enableKeyguard(false);
229 }
230 public void released() {
231 synchronized (mKeyguardDisabled) {
232 mPolicy.enableKeyguard(true);
233 mWaitingUntilKeyguardReenabled = false;
234 mKeyguardDisabled.notifyAll();
235 }
236 }
237 };
238
239 final Context mContext;
240
241 final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700242
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 final boolean mLimitedAlphaCompositing;
Romain Guy06882f82009-06-10 13:36:04 -0700244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 final WindowManagerPolicy mPolicy = PolicyManager.makeNewWindowManager();
246
247 final IActivityManager mActivityManager;
Romain Guy06882f82009-06-10 13:36:04 -0700248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 final IBatteryStats mBatteryStats;
Romain Guy06882f82009-06-10 13:36:04 -0700250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 /**
252 * All currently active sessions with clients.
253 */
254 final HashSet<Session> mSessions = new HashSet<Session>();
Romain Guy06882f82009-06-10 13:36:04 -0700255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 /**
257 * Mapping from an IWindow IBinder to the server's Window object.
258 * This is also used as the lock for all of our state.
259 */
260 final HashMap<IBinder, WindowState> mWindowMap = new HashMap<IBinder, WindowState>();
261
262 /**
263 * Mapping from a token IBinder to a WindowToken object.
264 */
265 final HashMap<IBinder, WindowToken> mTokenMap =
266 new HashMap<IBinder, WindowToken>();
267
268 /**
269 * The same tokens as mTokenMap, stored in a list for efficient iteration
270 * over them.
271 */
272 final ArrayList<WindowToken> mTokenList = new ArrayList<WindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 /**
275 * Window tokens that are in the process of exiting, but still
276 * on screen for animations.
277 */
278 final ArrayList<WindowToken> mExitingTokens = new ArrayList<WindowToken>();
279
280 /**
281 * Z-ordered (bottom-most first) list of all application tokens, for
282 * controlling the ordering of windows in different applications. This
283 * contains WindowToken objects.
284 */
285 final ArrayList<AppWindowToken> mAppTokens = new ArrayList<AppWindowToken>();
286
287 /**
288 * Application tokens that are in the process of exiting, but still
289 * on screen for animations.
290 */
291 final ArrayList<AppWindowToken> mExitingAppTokens = new ArrayList<AppWindowToken>();
292
293 /**
294 * List of window tokens that have finished starting their application,
295 * and now need to have the policy remove their windows.
296 */
297 final ArrayList<AppWindowToken> mFinishedStarting = new ArrayList<AppWindowToken>();
298
299 /**
300 * Z-ordered (bottom-most first) list of all Window objects.
301 */
302 final ArrayList mWindows = new ArrayList();
303
304 /**
305 * Windows that are being resized. Used so we can tell the client about
306 * the resize after closing the transaction in which we resized the
307 * underlying surface.
308 */
309 final ArrayList<WindowState> mResizingWindows = new ArrayList<WindowState>();
310
311 /**
312 * Windows whose animations have ended and now must be removed.
313 */
314 final ArrayList<WindowState> mPendingRemove = new ArrayList<WindowState>();
315
316 /**
317 * Windows whose surface should be destroyed.
318 */
319 final ArrayList<WindowState> mDestroySurface = new ArrayList<WindowState>();
320
321 /**
322 * Windows that have lost input focus and are waiting for the new
323 * focus window to be displayed before they are told about this.
324 */
325 ArrayList<WindowState> mLosingFocus = new ArrayList<WindowState>();
326
327 /**
328 * This is set when we have run out of memory, and will either be an empty
329 * list or contain windows that need to be force removed.
330 */
331 ArrayList<WindowState> mForceRemoves;
Romain Guy06882f82009-06-10 13:36:04 -0700332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 IInputMethodManager mInputMethodManager;
Romain Guy06882f82009-06-10 13:36:04 -0700334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 SurfaceSession mFxSession;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700336 private DimAnimator mDimAnimator = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 Surface mBlurSurface;
338 boolean mBlurShown;
Romain Guy06882f82009-06-10 13:36:04 -0700339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 int mTransactionSequence = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 final float[] mTmpFloats = new float[9];
343
344 boolean mSafeMode;
345 boolean mDisplayEnabled = false;
346 boolean mSystemBooted = false;
347 int mRotation = 0;
348 int mRequestedRotation = 0;
349 int mForcedAppOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Dianne Hackborn321ae682009-03-27 16:16:03 -0700350 int mLastRotationFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 ArrayList<IRotationWatcher> mRotationWatchers
352 = new ArrayList<IRotationWatcher>();
Romain Guy06882f82009-06-10 13:36:04 -0700353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 boolean mLayoutNeeded = true;
355 boolean mAnimationPending = false;
356 boolean mDisplayFrozen = false;
357 boolean mWindowsFreezingScreen = false;
358 long mFreezeGcPending = 0;
359 int mAppsFreezingScreen = 0;
360
361 // This is held as long as we have the screen frozen, to give us time to
362 // perform a rotation animation when turning off shows the lock screen which
363 // changes the orientation.
364 PowerManager.WakeLock mScreenFrozenLock;
Romain Guy06882f82009-06-10 13:36:04 -0700365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 // State management of app transitions. When we are preparing for a
367 // transition, mNextAppTransition will be the kind of transition to
368 // perform or TRANSIT_NONE if we are not waiting. If we are waiting,
369 // mOpeningApps and mClosingApps are the lists of tokens that will be
370 // made visible or hidden at the next transition.
371 int mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
372 boolean mAppTransitionReady = false;
373 boolean mAppTransitionTimeout = false;
374 boolean mStartingIconInTransition = false;
375 boolean mSkipAppTransitionAnimation = false;
376 final ArrayList<AppWindowToken> mOpeningApps = new ArrayList<AppWindowToken>();
377 final ArrayList<AppWindowToken> mClosingApps = new ArrayList<AppWindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 //flag to detect fat touch events
380 boolean mFatTouch = false;
381 Display mDisplay;
Romain Guy06882f82009-06-10 13:36:04 -0700382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 H mH = new H();
384
385 WindowState mCurrentFocus = null;
386 WindowState mLastFocus = null;
Romain Guy06882f82009-06-10 13:36:04 -0700387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 // This just indicates the window the input method is on top of, not
389 // necessarily the window its input is going to.
390 WindowState mInputMethodTarget = null;
391 WindowState mUpcomingInputMethodTarget = null;
392 boolean mInputMethodTargetWaitingAnim;
393 int mInputMethodAnimLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -0700394
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 WindowState mInputMethodWindow = null;
396 final ArrayList<WindowState> mInputMethodDialogs = new ArrayList<WindowState>();
397
398 AppWindowToken mFocusedApp = null;
399
400 PowerManagerService mPowerManager;
Romain Guy06882f82009-06-10 13:36:04 -0700401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 float mWindowAnimationScale = 1.0f;
403 float mTransitionAnimationScale = 1.0f;
Romain Guy06882f82009-06-10 13:36:04 -0700404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 final KeyWaiter mKeyWaiter = new KeyWaiter();
406 final KeyQ mQueue;
407 final InputDispatcherThread mInputThread;
408
409 // Who is holding the screen on.
410 Session mHoldingScreenOn;
Romain Guy06882f82009-06-10 13:36:04 -0700411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 /**
413 * Whether the UI is currently running in touch mode (not showing
414 * navigational focus because the user is directly pressing the screen).
415 */
416 boolean mInTouchMode = false;
417
418 private ViewServer mViewServer;
419
420 final Rect mTempRect = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -0700421
Dianne Hackbornc485a602009-03-24 22:39:49 -0700422 final Configuration mTempConfiguration = new Configuration();
Dianne Hackborn723738c2009-06-25 19:48:04 -0700423 int screenLayout = Configuration.SCREENLAYOUT_UNDEFINED;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -0700424
425 // The frame use to limit the size of the app running in compatibility mode.
426 Rect mCompatibleScreenFrame = new Rect();
427 // The surface used to fill the outer rim of the app running in compatibility mode.
428 Surface mBackgroundFillerSurface = null;
429 boolean mBackgroundFillerShown = false;
430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 public static WindowManagerService main(Context context,
432 PowerManagerService pm, boolean haveInputMethods) {
433 WMThread thr = new WMThread(context, pm, haveInputMethods);
434 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700435
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 synchronized (thr) {
437 while (thr.mService == null) {
438 try {
439 thr.wait();
440 } catch (InterruptedException e) {
441 }
442 }
443 }
Romain Guy06882f82009-06-10 13:36:04 -0700444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 return thr.mService;
446 }
Romain Guy06882f82009-06-10 13:36:04 -0700447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 static class WMThread extends Thread {
449 WindowManagerService mService;
Romain Guy06882f82009-06-10 13:36:04 -0700450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 private final Context mContext;
452 private final PowerManagerService mPM;
453 private final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 public WMThread(Context context, PowerManagerService pm,
456 boolean haveInputMethods) {
457 super("WindowManager");
458 mContext = context;
459 mPM = pm;
460 mHaveInputMethods = haveInputMethods;
461 }
Romain Guy06882f82009-06-10 13:36:04 -0700462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 public void run() {
464 Looper.prepare();
465 WindowManagerService s = new WindowManagerService(mContext, mPM,
466 mHaveInputMethods);
467 android.os.Process.setThreadPriority(
468 android.os.Process.THREAD_PRIORITY_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -0700469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 synchronized (this) {
471 mService = s;
472 notifyAll();
473 }
Romain Guy06882f82009-06-10 13:36:04 -0700474
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 Looper.loop();
476 }
477 }
478
479 static class PolicyThread extends Thread {
480 private final WindowManagerPolicy mPolicy;
481 private final WindowManagerService mService;
482 private final Context mContext;
483 private final PowerManagerService mPM;
484 boolean mRunning = false;
Romain Guy06882f82009-06-10 13:36:04 -0700485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 public PolicyThread(WindowManagerPolicy policy,
487 WindowManagerService service, Context context,
488 PowerManagerService pm) {
489 super("WindowManagerPolicy");
490 mPolicy = policy;
491 mService = service;
492 mContext = context;
493 mPM = pm;
494 }
Romain Guy06882f82009-06-10 13:36:04 -0700495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 public void run() {
497 Looper.prepare();
498 //Looper.myLooper().setMessageLogging(new LogPrinter(
499 // Log.VERBOSE, "WindowManagerPolicy"));
500 android.os.Process.setThreadPriority(
501 android.os.Process.THREAD_PRIORITY_FOREGROUND);
502 mPolicy.init(mContext, mService, mPM);
Romain Guy06882f82009-06-10 13:36:04 -0700503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 synchronized (this) {
505 mRunning = true;
506 notifyAll();
507 }
Romain Guy06882f82009-06-10 13:36:04 -0700508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 Looper.loop();
510 }
511 }
512
513 private WindowManagerService(Context context, PowerManagerService pm,
514 boolean haveInputMethods) {
515 mContext = context;
516 mHaveInputMethods = haveInputMethods;
517 mLimitedAlphaCompositing = context.getResources().getBoolean(
518 com.android.internal.R.bool.config_sf_limitedAlpha);
Romain Guy06882f82009-06-10 13:36:04 -0700519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 mPowerManager = pm;
521 mPowerManager.setPolicy(mPolicy);
522 PowerManager pmc = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
523 mScreenFrozenLock = pmc.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
524 "SCREEN_FROZEN");
525 mScreenFrozenLock.setReferenceCounted(false);
526
527 mActivityManager = ActivityManagerNative.getDefault();
528 mBatteryStats = BatteryStatsService.getService();
529
530 // Get persisted window scale setting
531 mWindowAnimationScale = Settings.System.getFloat(context.getContentResolver(),
532 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
533 mTransitionAnimationScale = Settings.System.getFloat(context.getContentResolver(),
534 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
Romain Guy06882f82009-06-10 13:36:04 -0700535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536 mQueue = new KeyQ();
537
538 mInputThread = new InputDispatcherThread();
Romain Guy06882f82009-06-10 13:36:04 -0700539
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 PolicyThread thr = new PolicyThread(mPolicy, this, context, pm);
541 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543 synchronized (thr) {
544 while (!thr.mRunning) {
545 try {
546 thr.wait();
547 } catch (InterruptedException e) {
548 }
549 }
550 }
Romain Guy06882f82009-06-10 13:36:04 -0700551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 mInputThread.start();
Romain Guy06882f82009-06-10 13:36:04 -0700553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 // Add ourself to the Watchdog monitors.
555 Watchdog.getInstance().addMonitor(this);
556 }
557
558 @Override
559 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
560 throws RemoteException {
561 try {
562 return super.onTransact(code, data, reply, flags);
563 } catch (RuntimeException e) {
564 // The window manager only throws security exceptions, so let's
565 // log all others.
566 if (!(e instanceof SecurityException)) {
567 Log.e(TAG, "Window Manager Crash", e);
568 }
569 throw e;
570 }
571 }
572
573 private void placeWindowAfter(Object pos, WindowState window) {
574 final int i = mWindows.indexOf(pos);
575 if (localLOGV || DEBUG_FOCUS) Log.v(
576 TAG, "Adding window " + window + " at "
577 + (i+1) + " of " + mWindows.size() + " (after " + pos + ")");
578 mWindows.add(i+1, window);
579 }
580
581 private void placeWindowBefore(Object pos, WindowState window) {
582 final int i = mWindows.indexOf(pos);
583 if (localLOGV || DEBUG_FOCUS) Log.v(
584 TAG, "Adding window " + window + " at "
585 + i + " of " + mWindows.size() + " (before " + pos + ")");
586 mWindows.add(i, window);
587 }
588
589 //This method finds out the index of a window that has the same app token as
590 //win. used for z ordering the windows in mWindows
591 private int findIdxBasedOnAppTokens(WindowState win) {
592 //use a local variable to cache mWindows
593 ArrayList localmWindows = mWindows;
594 int jmax = localmWindows.size();
595 if(jmax == 0) {
596 return -1;
597 }
598 for(int j = (jmax-1); j >= 0; j--) {
599 WindowState wentry = (WindowState)localmWindows.get(j);
600 if(wentry.mAppToken == win.mAppToken) {
601 return j;
602 }
603 }
604 return -1;
605 }
Romain Guy06882f82009-06-10 13:36:04 -0700606
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 private void addWindowToListInOrderLocked(WindowState win, boolean addToToken) {
608 final IWindow client = win.mClient;
609 final WindowToken token = win.mToken;
610 final ArrayList localmWindows = mWindows;
Romain Guy06882f82009-06-10 13:36:04 -0700611
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 final int N = localmWindows.size();
613 final WindowState attached = win.mAttachedWindow;
614 int i;
615 if (attached == null) {
616 int tokenWindowsPos = token.windows.size();
617 if (token.appWindowToken != null) {
618 int index = tokenWindowsPos-1;
619 if (index >= 0) {
620 // If this application has existing windows, we
621 // simply place the new window on top of them... but
622 // keep the starting window on top.
623 if (win.mAttrs.type == TYPE_BASE_APPLICATION) {
624 // Base windows go behind everything else.
625 placeWindowBefore(token.windows.get(0), win);
626 tokenWindowsPos = 0;
627 } else {
628 AppWindowToken atoken = win.mAppToken;
629 if (atoken != null &&
630 token.windows.get(index) == atoken.startingWindow) {
631 placeWindowBefore(token.windows.get(index), win);
632 tokenWindowsPos--;
633 } else {
634 int newIdx = findIdxBasedOnAppTokens(win);
635 if(newIdx != -1) {
Romain Guy06882f82009-06-10 13:36:04 -0700636 //there is a window above this one associated with the same
637 //apptoken note that the window could be a floating window
638 //that was created later or a window at the top of the list of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 //windows associated with this token.
640 localmWindows.add(newIdx+1, win);
Romain Guy06882f82009-06-10 13:36:04 -0700641 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 }
643 }
644 } else {
645 if (localLOGV) Log.v(
646 TAG, "Figuring out where to add app window "
647 + client.asBinder() + " (token=" + token + ")");
648 // Figure out where the window should go, based on the
649 // order of applications.
650 final int NA = mAppTokens.size();
651 Object pos = null;
652 for (i=NA-1; i>=0; i--) {
653 AppWindowToken t = mAppTokens.get(i);
654 if (t == token) {
655 i--;
656 break;
657 }
658 if (t.windows.size() > 0) {
659 pos = t.windows.get(0);
660 }
661 }
662 // We now know the index into the apps. If we found
663 // an app window above, that gives us the position; else
664 // we need to look some more.
665 if (pos != null) {
666 // Move behind any windows attached to this one.
Romain Guy06882f82009-06-10 13:36:04 -0700667 WindowToken atoken =
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800668 mTokenMap.get(((WindowState)pos).mClient.asBinder());
669 if (atoken != null) {
670 final int NC = atoken.windows.size();
671 if (NC > 0) {
672 WindowState bottom = atoken.windows.get(0);
673 if (bottom.mSubLayer < 0) {
674 pos = bottom;
675 }
676 }
677 }
678 placeWindowBefore(pos, win);
679 } else {
680 while (i >= 0) {
681 AppWindowToken t = mAppTokens.get(i);
682 final int NW = t.windows.size();
683 if (NW > 0) {
684 pos = t.windows.get(NW-1);
685 break;
686 }
687 i--;
688 }
689 if (pos != null) {
690 // Move in front of any windows attached to this
691 // one.
692 WindowToken atoken =
693 mTokenMap.get(((WindowState)pos).mClient.asBinder());
694 if (atoken != null) {
695 final int NC = atoken.windows.size();
696 if (NC > 0) {
697 WindowState top = atoken.windows.get(NC-1);
698 if (top.mSubLayer >= 0) {
699 pos = top;
700 }
701 }
702 }
703 placeWindowAfter(pos, win);
704 } else {
705 // Just search for the start of this layer.
706 final int myLayer = win.mBaseLayer;
707 for (i=0; i<N; i++) {
708 WindowState w = (WindowState)localmWindows.get(i);
709 if (w.mBaseLayer > myLayer) {
710 break;
711 }
712 }
713 if (localLOGV || DEBUG_FOCUS) Log.v(
714 TAG, "Adding window " + win + " at "
715 + i + " of " + N);
716 localmWindows.add(i, win);
717 }
718 }
719 }
720 } else {
721 // Figure out where window should go, based on layer.
722 final int myLayer = win.mBaseLayer;
723 for (i=N-1; i>=0; i--) {
724 if (((WindowState)localmWindows.get(i)).mBaseLayer <= myLayer) {
725 i++;
726 break;
727 }
728 }
729 if (i < 0) i = 0;
730 if (localLOGV || DEBUG_FOCUS) Log.v(
731 TAG, "Adding window " + win + " at "
732 + i + " of " + N);
733 localmWindows.add(i, win);
734 }
735 if (addToToken) {
736 token.windows.add(tokenWindowsPos, win);
737 }
738
739 } else {
740 // Figure out this window's ordering relative to the window
741 // it is attached to.
742 final int NA = token.windows.size();
743 final int sublayer = win.mSubLayer;
744 int largestSublayer = Integer.MIN_VALUE;
745 WindowState windowWithLargestSublayer = null;
746 for (i=0; i<NA; i++) {
747 WindowState w = token.windows.get(i);
748 final int wSublayer = w.mSubLayer;
749 if (wSublayer >= largestSublayer) {
750 largestSublayer = wSublayer;
751 windowWithLargestSublayer = w;
752 }
753 if (sublayer < 0) {
754 // For negative sublayers, we go below all windows
755 // in the same sublayer.
756 if (wSublayer >= sublayer) {
757 if (addToToken) {
758 token.windows.add(i, win);
759 }
760 placeWindowBefore(
761 wSublayer >= 0 ? attached : w, win);
762 break;
763 }
764 } else {
765 // For positive sublayers, we go above all windows
766 // in the same sublayer.
767 if (wSublayer > sublayer) {
768 if (addToToken) {
769 token.windows.add(i, win);
770 }
771 placeWindowBefore(w, win);
772 break;
773 }
774 }
775 }
776 if (i >= NA) {
777 if (addToToken) {
778 token.windows.add(win);
779 }
780 if (sublayer < 0) {
781 placeWindowBefore(attached, win);
782 } else {
783 placeWindowAfter(largestSublayer >= 0
784 ? windowWithLargestSublayer
785 : attached,
786 win);
787 }
788 }
789 }
Romain Guy06882f82009-06-10 13:36:04 -0700790
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800791 if (win.mAppToken != null && addToToken) {
792 win.mAppToken.allAppWindows.add(win);
793 }
794 }
Romain Guy06882f82009-06-10 13:36:04 -0700795
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800796 static boolean canBeImeTarget(WindowState w) {
797 final int fl = w.mAttrs.flags
798 & (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM);
799 if (fl == 0 || fl == (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM)) {
800 return w.isVisibleOrAdding();
801 }
802 return false;
803 }
Romain Guy06882f82009-06-10 13:36:04 -0700804
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800805 int findDesiredInputMethodWindowIndexLocked(boolean willMove) {
806 final ArrayList localmWindows = mWindows;
807 final int N = localmWindows.size();
808 WindowState w = null;
809 int i = N;
810 while (i > 0) {
811 i--;
812 w = (WindowState)localmWindows.get(i);
Romain Guy06882f82009-06-10 13:36:04 -0700813
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800814 //Log.i(TAG, "Checking window @" + i + " " + w + " fl=0x"
815 // + Integer.toHexString(w.mAttrs.flags));
816 if (canBeImeTarget(w)) {
817 //Log.i(TAG, "Putting input method here!");
Romain Guy06882f82009-06-10 13:36:04 -0700818
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819 // Yet more tricksyness! If this window is a "starting"
820 // window, we do actually want to be on top of it, but
821 // it is not -really- where input will go. So if the caller
822 // is not actually looking to move the IME, look down below
823 // for a real window to target...
824 if (!willMove
825 && w.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
826 && i > 0) {
827 WindowState wb = (WindowState)localmWindows.get(i-1);
828 if (wb.mAppToken == w.mAppToken && canBeImeTarget(wb)) {
829 i--;
830 w = wb;
831 }
832 }
833 break;
834 }
835 }
Romain Guy06882f82009-06-10 13:36:04 -0700836
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837 mUpcomingInputMethodTarget = w;
Romain Guy06882f82009-06-10 13:36:04 -0700838
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800839 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Desired input method target="
840 + w + " willMove=" + willMove);
Romain Guy06882f82009-06-10 13:36:04 -0700841
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800842 if (willMove && w != null) {
843 final WindowState curTarget = mInputMethodTarget;
844 if (curTarget != null && curTarget.mAppToken != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700845
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800846 // Now some fun for dealing with window animations that
847 // modify the Z order. We need to look at all windows below
848 // the current target that are in this app, finding the highest
849 // visible one in layering.
850 AppWindowToken token = curTarget.mAppToken;
851 WindowState highestTarget = null;
852 int highestPos = 0;
853 if (token.animating || token.animation != null) {
854 int pos = 0;
855 pos = localmWindows.indexOf(curTarget);
856 while (pos >= 0) {
857 WindowState win = (WindowState)localmWindows.get(pos);
858 if (win.mAppToken != token) {
859 break;
860 }
861 if (!win.mRemoved) {
862 if (highestTarget == null || win.mAnimLayer >
863 highestTarget.mAnimLayer) {
864 highestTarget = win;
865 highestPos = pos;
866 }
867 }
868 pos--;
869 }
870 }
Romain Guy06882f82009-06-10 13:36:04 -0700871
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800872 if (highestTarget != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700873 if (DEBUG_INPUT_METHOD) Log.v(TAG, "mNextAppTransition="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800874 + mNextAppTransition + " " + highestTarget
875 + " animating=" + highestTarget.isAnimating()
876 + " layer=" + highestTarget.mAnimLayer
877 + " new layer=" + w.mAnimLayer);
Romain Guy06882f82009-06-10 13:36:04 -0700878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800879 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
880 // If we are currently setting up for an animation,
881 // hold everything until we can find out what will happen.
882 mInputMethodTargetWaitingAnim = true;
883 mInputMethodTarget = highestTarget;
884 return highestPos + 1;
885 } else if (highestTarget.isAnimating() &&
886 highestTarget.mAnimLayer > w.mAnimLayer) {
887 // If the window we are currently targeting is involved
888 // with an animation, and it is on top of the next target
889 // we will be over, then hold off on moving until
890 // that is done.
891 mInputMethodTarget = highestTarget;
892 return highestPos + 1;
893 }
894 }
895 }
896 }
Romain Guy06882f82009-06-10 13:36:04 -0700897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800898 //Log.i(TAG, "Placing input method @" + (i+1));
899 if (w != null) {
900 if (willMove) {
901 RuntimeException e = new RuntimeException();
902 e.fillInStackTrace();
903 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
904 + mInputMethodTarget + " to " + w, e);
905 mInputMethodTarget = w;
906 if (w.mAppToken != null) {
907 setInputMethodAnimLayerAdjustment(w.mAppToken.animLayerAdjustment);
908 } else {
909 setInputMethodAnimLayerAdjustment(0);
910 }
911 }
912 return i+1;
913 }
914 if (willMove) {
915 RuntimeException e = new RuntimeException();
916 e.fillInStackTrace();
917 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
918 + mInputMethodTarget + " to null", e);
919 mInputMethodTarget = null;
920 setInputMethodAnimLayerAdjustment(0);
921 }
922 return -1;
923 }
Romain Guy06882f82009-06-10 13:36:04 -0700924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925 void addInputMethodWindowToListLocked(WindowState win) {
926 int pos = findDesiredInputMethodWindowIndexLocked(true);
927 if (pos >= 0) {
928 win.mTargetAppToken = mInputMethodTarget.mAppToken;
929 mWindows.add(pos, win);
930 moveInputMethodDialogsLocked(pos+1);
931 return;
932 }
933 win.mTargetAppToken = null;
934 addWindowToListInOrderLocked(win, true);
935 moveInputMethodDialogsLocked(pos);
936 }
Romain Guy06882f82009-06-10 13:36:04 -0700937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938 void setInputMethodAnimLayerAdjustment(int adj) {
939 if (DEBUG_LAYERS) Log.v(TAG, "Setting im layer adj to " + adj);
940 mInputMethodAnimLayerAdjustment = adj;
941 WindowState imw = mInputMethodWindow;
942 if (imw != null) {
943 imw.mAnimLayer = imw.mLayer + adj;
944 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
945 + " anim layer: " + imw.mAnimLayer);
946 int wi = imw.mChildWindows.size();
947 while (wi > 0) {
948 wi--;
949 WindowState cw = (WindowState)imw.mChildWindows.get(wi);
950 cw.mAnimLayer = cw.mLayer + adj;
951 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + cw
952 + " anim layer: " + cw.mAnimLayer);
953 }
954 }
955 int di = mInputMethodDialogs.size();
956 while (di > 0) {
957 di --;
958 imw = mInputMethodDialogs.get(di);
959 imw.mAnimLayer = imw.mLayer + adj;
960 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
961 + " anim layer: " + imw.mAnimLayer);
962 }
963 }
Romain Guy06882f82009-06-10 13:36:04 -0700964
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800965 private int tmpRemoveWindowLocked(int interestingPos, WindowState win) {
966 int wpos = mWindows.indexOf(win);
967 if (wpos >= 0) {
968 if (wpos < interestingPos) interestingPos--;
969 mWindows.remove(wpos);
970 int NC = win.mChildWindows.size();
971 while (NC > 0) {
972 NC--;
973 WindowState cw = (WindowState)win.mChildWindows.get(NC);
974 int cpos = mWindows.indexOf(cw);
975 if (cpos >= 0) {
976 if (cpos < interestingPos) interestingPos--;
977 mWindows.remove(cpos);
978 }
979 }
980 }
981 return interestingPos;
982 }
Romain Guy06882f82009-06-10 13:36:04 -0700983
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984 private void reAddWindowToListInOrderLocked(WindowState win) {
985 addWindowToListInOrderLocked(win, false);
986 // This is a hack to get all of the child windows added as well
987 // at the right position. Child windows should be rare and
988 // this case should be rare, so it shouldn't be that big a deal.
989 int wpos = mWindows.indexOf(win);
990 if (wpos >= 0) {
991 mWindows.remove(wpos);
992 reAddWindowLocked(wpos, win);
993 }
994 }
Romain Guy06882f82009-06-10 13:36:04 -0700995
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996 void logWindowList(String prefix) {
997 int N = mWindows.size();
998 while (N > 0) {
999 N--;
1000 Log.v(TAG, prefix + "#" + N + ": " + mWindows.get(N));
1001 }
1002 }
Romain Guy06882f82009-06-10 13:36:04 -07001003
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001004 void moveInputMethodDialogsLocked(int pos) {
1005 ArrayList<WindowState> dialogs = mInputMethodDialogs;
Romain Guy06882f82009-06-10 13:36:04 -07001006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 final int N = dialogs.size();
1008 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Removing " + N + " dialogs w/pos=" + pos);
1009 for (int i=0; i<N; i++) {
1010 pos = tmpRemoveWindowLocked(pos, dialogs.get(i));
1011 }
1012 if (DEBUG_INPUT_METHOD) {
1013 Log.v(TAG, "Window list w/pos=" + pos);
1014 logWindowList(" ");
1015 }
Romain Guy06882f82009-06-10 13:36:04 -07001016
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 if (pos >= 0) {
1018 final AppWindowToken targetAppToken = mInputMethodTarget.mAppToken;
1019 if (pos < mWindows.size()) {
1020 WindowState wp = (WindowState)mWindows.get(pos);
1021 if (wp == mInputMethodWindow) {
1022 pos++;
1023 }
1024 }
1025 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Adding " + N + " dialogs at pos=" + pos);
1026 for (int i=0; i<N; i++) {
1027 WindowState win = dialogs.get(i);
1028 win.mTargetAppToken = targetAppToken;
1029 pos = reAddWindowLocked(pos, win);
1030 }
1031 if (DEBUG_INPUT_METHOD) {
1032 Log.v(TAG, "Final window list:");
1033 logWindowList(" ");
1034 }
1035 return;
1036 }
1037 for (int i=0; i<N; i++) {
1038 WindowState win = dialogs.get(i);
1039 win.mTargetAppToken = null;
1040 reAddWindowToListInOrderLocked(win);
1041 if (DEBUG_INPUT_METHOD) {
1042 Log.v(TAG, "No IM target, final list:");
1043 logWindowList(" ");
1044 }
1045 }
1046 }
Romain Guy06882f82009-06-10 13:36:04 -07001047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 boolean moveInputMethodWindowsIfNeededLocked(boolean needAssignLayers) {
1049 final WindowState imWin = mInputMethodWindow;
1050 final int DN = mInputMethodDialogs.size();
1051 if (imWin == null && DN == 0) {
1052 return false;
1053 }
Romain Guy06882f82009-06-10 13:36:04 -07001054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055 int imPos = findDesiredInputMethodWindowIndexLocked(true);
1056 if (imPos >= 0) {
1057 // In this case, the input method windows are to be placed
1058 // immediately above the window they are targeting.
Romain Guy06882f82009-06-10 13:36:04 -07001059
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 // First check to see if the input method windows are already
1061 // located here, and contiguous.
1062 final int N = mWindows.size();
1063 WindowState firstImWin = imPos < N
1064 ? (WindowState)mWindows.get(imPos) : null;
Romain Guy06882f82009-06-10 13:36:04 -07001065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001066 // Figure out the actual input method window that should be
1067 // at the bottom of their stack.
1068 WindowState baseImWin = imWin != null
1069 ? imWin : mInputMethodDialogs.get(0);
1070 if (baseImWin.mChildWindows.size() > 0) {
1071 WindowState cw = (WindowState)baseImWin.mChildWindows.get(0);
1072 if (cw.mSubLayer < 0) baseImWin = cw;
1073 }
Romain Guy06882f82009-06-10 13:36:04 -07001074
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075 if (firstImWin == baseImWin) {
1076 // The windows haven't moved... but are they still contiguous?
1077 // First find the top IM window.
1078 int pos = imPos+1;
1079 while (pos < N) {
1080 if (!((WindowState)mWindows.get(pos)).mIsImWindow) {
1081 break;
1082 }
1083 pos++;
1084 }
1085 pos++;
1086 // Now there should be no more input method windows above.
1087 while (pos < N) {
1088 if (((WindowState)mWindows.get(pos)).mIsImWindow) {
1089 break;
1090 }
1091 pos++;
1092 }
1093 if (pos >= N) {
1094 // All is good!
1095 return false;
1096 }
1097 }
Romain Guy06882f82009-06-10 13:36:04 -07001098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099 if (imWin != null) {
1100 if (DEBUG_INPUT_METHOD) {
1101 Log.v(TAG, "Moving IM from " + imPos);
1102 logWindowList(" ");
1103 }
1104 imPos = tmpRemoveWindowLocked(imPos, imWin);
1105 if (DEBUG_INPUT_METHOD) {
1106 Log.v(TAG, "List after moving with new pos " + imPos + ":");
1107 logWindowList(" ");
1108 }
1109 imWin.mTargetAppToken = mInputMethodTarget.mAppToken;
1110 reAddWindowLocked(imPos, imWin);
1111 if (DEBUG_INPUT_METHOD) {
1112 Log.v(TAG, "List after moving IM to " + imPos + ":");
1113 logWindowList(" ");
1114 }
1115 if (DN > 0) moveInputMethodDialogsLocked(imPos+1);
1116 } else {
1117 moveInputMethodDialogsLocked(imPos);
1118 }
Romain Guy06882f82009-06-10 13:36:04 -07001119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120 } else {
1121 // In this case, the input method windows go in a fixed layer,
1122 // because they aren't currently associated with a focus window.
Romain Guy06882f82009-06-10 13:36:04 -07001123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001124 if (imWin != null) {
1125 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Moving IM from " + imPos);
1126 tmpRemoveWindowLocked(0, imWin);
1127 imWin.mTargetAppToken = null;
1128 reAddWindowToListInOrderLocked(imWin);
1129 if (DEBUG_INPUT_METHOD) {
1130 Log.v(TAG, "List with no IM target:");
1131 logWindowList(" ");
1132 }
1133 if (DN > 0) moveInputMethodDialogsLocked(-1);;
1134 } else {
1135 moveInputMethodDialogsLocked(-1);;
1136 }
Romain Guy06882f82009-06-10 13:36:04 -07001137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 }
Romain Guy06882f82009-06-10 13:36:04 -07001139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140 if (needAssignLayers) {
1141 assignLayersLocked();
1142 }
Romain Guy06882f82009-06-10 13:36:04 -07001143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 return true;
1145 }
Romain Guy06882f82009-06-10 13:36:04 -07001146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147 void adjustInputMethodDialogsLocked() {
1148 moveInputMethodDialogsLocked(findDesiredInputMethodWindowIndexLocked(true));
1149 }
Romain Guy06882f82009-06-10 13:36:04 -07001150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 public int addWindow(Session session, IWindow client,
1152 WindowManager.LayoutParams attrs, int viewVisibility,
1153 Rect outContentInsets) {
1154 int res = mPolicy.checkAddPermission(attrs);
1155 if (res != WindowManagerImpl.ADD_OKAY) {
1156 return res;
1157 }
Romain Guy06882f82009-06-10 13:36:04 -07001158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 boolean reportNewConfig = false;
1160 WindowState attachedWindow = null;
1161 WindowState win = null;
Romain Guy06882f82009-06-10 13:36:04 -07001162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 synchronized(mWindowMap) {
1164 // Instantiating a Display requires talking with the simulator,
1165 // so don't do it until we know the system is mostly up and
1166 // running.
1167 if (mDisplay == null) {
1168 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
1169 mDisplay = wm.getDefaultDisplay();
1170 mQueue.setDisplay(mDisplay);
1171 reportNewConfig = true;
1172 }
Romain Guy06882f82009-06-10 13:36:04 -07001173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174 if (mWindowMap.containsKey(client.asBinder())) {
1175 Log.w(TAG, "Window " + client + " is already added");
1176 return WindowManagerImpl.ADD_DUPLICATE_ADD;
1177 }
1178
1179 if (attrs.type >= FIRST_SUB_WINDOW && attrs.type <= LAST_SUB_WINDOW) {
Romain Guy06882f82009-06-10 13:36:04 -07001180 attachedWindow = windowForClientLocked(null, attrs.token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 if (attachedWindow == null) {
1182 Log.w(TAG, "Attempted to add window with token that is not a window: "
1183 + attrs.token + ". Aborting.");
1184 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1185 }
1186 if (attachedWindow.mAttrs.type >= FIRST_SUB_WINDOW
1187 && attachedWindow.mAttrs.type <= LAST_SUB_WINDOW) {
1188 Log.w(TAG, "Attempted to add window with token that is a sub-window: "
1189 + attrs.token + ". Aborting.");
1190 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1191 }
1192 }
1193
1194 boolean addToken = false;
1195 WindowToken token = mTokenMap.get(attrs.token);
1196 if (token == null) {
1197 if (attrs.type >= FIRST_APPLICATION_WINDOW
1198 && attrs.type <= LAST_APPLICATION_WINDOW) {
1199 Log.w(TAG, "Attempted to add application window with unknown token "
1200 + attrs.token + ". Aborting.");
1201 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1202 }
1203 if (attrs.type == TYPE_INPUT_METHOD) {
1204 Log.w(TAG, "Attempted to add input method window with unknown token "
1205 + attrs.token + ". Aborting.");
1206 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1207 }
1208 token = new WindowToken(attrs.token, -1, false);
1209 addToken = true;
1210 } else if (attrs.type >= FIRST_APPLICATION_WINDOW
1211 && attrs.type <= LAST_APPLICATION_WINDOW) {
1212 AppWindowToken atoken = token.appWindowToken;
1213 if (atoken == null) {
1214 Log.w(TAG, "Attempted to add window with non-application token "
1215 + token + ". Aborting.");
1216 return WindowManagerImpl.ADD_NOT_APP_TOKEN;
1217 } else if (atoken.removed) {
1218 Log.w(TAG, "Attempted to add window with exiting application token "
1219 + token + ". Aborting.");
1220 return WindowManagerImpl.ADD_APP_EXITING;
1221 }
1222 if (attrs.type == TYPE_APPLICATION_STARTING && atoken.firstWindowDrawn) {
1223 // No need for this guy!
1224 if (localLOGV) Log.v(
1225 TAG, "**** NO NEED TO START: " + attrs.getTitle());
1226 return WindowManagerImpl.ADD_STARTING_NOT_NEEDED;
1227 }
1228 } else if (attrs.type == TYPE_INPUT_METHOD) {
1229 if (token.windowType != TYPE_INPUT_METHOD) {
1230 Log.w(TAG, "Attempted to add input method window with bad token "
1231 + attrs.token + ". Aborting.");
1232 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1233 }
1234 }
1235
1236 win = new WindowState(session, client, token,
1237 attachedWindow, attrs, viewVisibility);
1238 if (win.mDeathRecipient == null) {
1239 // Client has apparently died, so there is no reason to
1240 // continue.
1241 Log.w(TAG, "Adding window client " + client.asBinder()
1242 + " that is dead, aborting.");
1243 return WindowManagerImpl.ADD_APP_EXITING;
1244 }
1245
1246 mPolicy.adjustWindowParamsLw(win.mAttrs);
Romain Guy06882f82009-06-10 13:36:04 -07001247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001248 res = mPolicy.prepareAddWindowLw(win, attrs);
1249 if (res != WindowManagerImpl.ADD_OKAY) {
1250 return res;
1251 }
1252
1253 // From now on, no exceptions or errors allowed!
1254
1255 res = WindowManagerImpl.ADD_OKAY;
Romain Guy06882f82009-06-10 13:36:04 -07001256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259 if (addToken) {
1260 mTokenMap.put(attrs.token, token);
1261 mTokenList.add(token);
1262 }
1263 win.attach();
1264 mWindowMap.put(client.asBinder(), win);
1265
1266 if (attrs.type == TYPE_APPLICATION_STARTING &&
1267 token.appWindowToken != null) {
1268 token.appWindowToken.startingWindow = win;
1269 }
1270
1271 boolean imMayMove = true;
Romain Guy06882f82009-06-10 13:36:04 -07001272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 if (attrs.type == TYPE_INPUT_METHOD) {
1274 mInputMethodWindow = win;
1275 addInputMethodWindowToListLocked(win);
1276 imMayMove = false;
1277 } else if (attrs.type == TYPE_INPUT_METHOD_DIALOG) {
1278 mInputMethodDialogs.add(win);
1279 addWindowToListInOrderLocked(win, true);
1280 adjustInputMethodDialogsLocked();
1281 imMayMove = false;
1282 } else {
1283 addWindowToListInOrderLocked(win, true);
1284 }
Romain Guy06882f82009-06-10 13:36:04 -07001285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001286 win.mEnterAnimationPending = true;
Romain Guy06882f82009-06-10 13:36:04 -07001287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 mPolicy.getContentInsetHintLw(attrs, outContentInsets);
Romain Guy06882f82009-06-10 13:36:04 -07001289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 if (mInTouchMode) {
1291 res |= WindowManagerImpl.ADD_FLAG_IN_TOUCH_MODE;
1292 }
1293 if (win == null || win.mAppToken == null || !win.mAppToken.clientHidden) {
1294 res |= WindowManagerImpl.ADD_FLAG_APP_VISIBLE;
1295 }
Romain Guy06882f82009-06-10 13:36:04 -07001296
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001297 boolean focusChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001298 if (win.canReceiveKeys()) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001299 if ((focusChanged=updateFocusedWindowLocked(UPDATE_FOCUS_WILL_ASSIGN_LAYERS))
1300 == true) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001301 imMayMove = false;
1302 }
1303 }
Romain Guy06882f82009-06-10 13:36:04 -07001304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001305 if (imMayMove) {
Romain Guy06882f82009-06-10 13:36:04 -07001306 moveInputMethodWindowsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001307 }
Romain Guy06882f82009-06-10 13:36:04 -07001308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 assignLayersLocked();
1310 // Don't do layout here, the window must call
1311 // relayout to be displayed, so we'll do it there.
Romain Guy06882f82009-06-10 13:36:04 -07001312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001313 //dump();
1314
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001315 if (focusChanged) {
1316 if (mCurrentFocus != null) {
1317 mKeyWaiter.handleNewWindowLocked(mCurrentFocus);
1318 }
1319 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 if (localLOGV) Log.v(
1321 TAG, "New client " + client.asBinder()
1322 + ": window=" + win);
1323 }
1324
1325 // sendNewConfiguration() checks caller permissions so we must call it with
1326 // privilege. updateOrientationFromAppTokens() clears and resets the caller
1327 // identity anyway, so it's safe to just clear & restore around this whole
1328 // block.
1329 final long origId = Binder.clearCallingIdentity();
1330 if (reportNewConfig) {
1331 sendNewConfiguration();
1332 } else {
1333 // Update Orientation after adding a window, only if the window needs to be
1334 // displayed right away
1335 if (win.isVisibleOrAdding()) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07001336 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001337 sendNewConfiguration();
1338 }
1339 }
1340 }
1341 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07001342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343 return res;
1344 }
Romain Guy06882f82009-06-10 13:36:04 -07001345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001346 public void removeWindow(Session session, IWindow client) {
1347 synchronized(mWindowMap) {
1348 WindowState win = windowForClientLocked(session, client);
1349 if (win == null) {
1350 return;
1351 }
1352 removeWindowLocked(session, win);
1353 }
1354 }
Romain Guy06882f82009-06-10 13:36:04 -07001355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001356 public void removeWindowLocked(Session session, WindowState win) {
1357
1358 if (localLOGV || DEBUG_FOCUS) Log.v(
1359 TAG, "Remove " + win + " client="
1360 + Integer.toHexString(System.identityHashCode(
1361 win.mClient.asBinder()))
1362 + ", surface=" + win.mSurface);
1363
1364 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001366 if (DEBUG_APP_TRANSITIONS) Log.v(
1367 TAG, "Remove " + win + ": mSurface=" + win.mSurface
1368 + " mExiting=" + win.mExiting
1369 + " isAnimating=" + win.isAnimating()
1370 + " app-animation="
1371 + (win.mAppToken != null ? win.mAppToken.animation : null)
1372 + " inPendingTransaction="
1373 + (win.mAppToken != null ? win.mAppToken.inPendingTransaction : false)
1374 + " mDisplayFrozen=" + mDisplayFrozen);
1375 // Visibility of the removed window. Will be used later to update orientation later on.
1376 boolean wasVisible = false;
1377 // First, see if we need to run an animation. If we do, we have
1378 // to hold off on removing the window until the animation is done.
1379 // If the display is frozen, just remove immediately, since the
1380 // animation wouldn't be seen.
1381 if (win.mSurface != null && !mDisplayFrozen) {
1382 // If we are not currently running the exit animation, we
1383 // need to see about starting one.
1384 if (wasVisible=win.isWinVisibleLw()) {
Romain Guy06882f82009-06-10 13:36:04 -07001385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001386 int transit = WindowManagerPolicy.TRANSIT_EXIT;
1387 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
1388 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
1389 }
1390 // Try starting an animation.
1391 if (applyAnimationLocked(win, transit, false)) {
1392 win.mExiting = true;
1393 }
1394 }
1395 if (win.mExiting || win.isAnimating()) {
1396 // The exit animation is running... wait for it!
1397 //Log.i(TAG, "*** Running exit animation...");
1398 win.mExiting = true;
1399 win.mRemoveOnExit = true;
1400 mLayoutNeeded = true;
1401 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
1402 performLayoutAndPlaceSurfacesLocked();
1403 if (win.mAppToken != null) {
1404 win.mAppToken.updateReportedVisibilityLocked();
1405 }
1406 //dump();
1407 Binder.restoreCallingIdentity(origId);
1408 return;
1409 }
1410 }
1411
1412 removeWindowInnerLocked(session, win);
1413 // Removing a visible window will effect the computed orientation
1414 // So just update orientation if needed.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001415 if (wasVisible && computeForcedAppOrientationLocked()
1416 != mForcedAppOrientation) {
1417 mH.sendMessage(mH.obtainMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001418 }
1419 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
1420 Binder.restoreCallingIdentity(origId);
1421 }
Romain Guy06882f82009-06-10 13:36:04 -07001422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001423 private void removeWindowInnerLocked(Session session, WindowState win) {
1424 mKeyWaiter.releasePendingPointerLocked(win.mSession);
1425 mKeyWaiter.releasePendingTrackballLocked(win.mSession);
Romain Guy06882f82009-06-10 13:36:04 -07001426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001427 win.mRemoved = true;
Romain Guy06882f82009-06-10 13:36:04 -07001428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001429 if (mInputMethodTarget == win) {
1430 moveInputMethodWindowsIfNeededLocked(false);
1431 }
Romain Guy06882f82009-06-10 13:36:04 -07001432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001433 mPolicy.removeWindowLw(win);
1434 win.removeLocked();
1435
1436 mWindowMap.remove(win.mClient.asBinder());
1437 mWindows.remove(win);
1438
1439 if (mInputMethodWindow == win) {
1440 mInputMethodWindow = null;
1441 } else if (win.mAttrs.type == TYPE_INPUT_METHOD_DIALOG) {
1442 mInputMethodDialogs.remove(win);
1443 }
Romain Guy06882f82009-06-10 13:36:04 -07001444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001445 final WindowToken token = win.mToken;
1446 final AppWindowToken atoken = win.mAppToken;
1447 token.windows.remove(win);
1448 if (atoken != null) {
1449 atoken.allAppWindows.remove(win);
1450 }
1451 if (localLOGV) Log.v(
1452 TAG, "**** Removing window " + win + ": count="
1453 + token.windows.size());
1454 if (token.windows.size() == 0) {
1455 if (!token.explicit) {
1456 mTokenMap.remove(token.token);
1457 mTokenList.remove(token);
1458 } else if (atoken != null) {
1459 atoken.firstWindowDrawn = false;
1460 }
1461 }
1462
1463 if (atoken != null) {
1464 if (atoken.startingWindow == win) {
1465 atoken.startingWindow = null;
1466 } else if (atoken.allAppWindows.size() == 0 && atoken.startingData != null) {
1467 // If this is the last window and we had requested a starting
1468 // transition window, well there is no point now.
1469 atoken.startingData = null;
1470 } else if (atoken.allAppWindows.size() == 1 && atoken.startingView != null) {
1471 // If this is the last window except for a starting transition
1472 // window, we need to get rid of the starting transition.
1473 if (DEBUG_STARTING_WINDOW) {
1474 Log.v(TAG, "Schedule remove starting " + token
1475 + ": no more real windows");
1476 }
1477 Message m = mH.obtainMessage(H.REMOVE_STARTING, atoken);
1478 mH.sendMessage(m);
1479 }
1480 }
Romain Guy06882f82009-06-10 13:36:04 -07001481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001482 if (!mInLayout) {
1483 assignLayersLocked();
1484 mLayoutNeeded = true;
1485 performLayoutAndPlaceSurfacesLocked();
1486 if (win.mAppToken != null) {
1487 win.mAppToken.updateReportedVisibilityLocked();
1488 }
1489 }
1490 }
1491
1492 private void setTransparentRegionWindow(Session session, IWindow client, Region region) {
1493 long origId = Binder.clearCallingIdentity();
1494 try {
1495 synchronized (mWindowMap) {
1496 WindowState w = windowForClientLocked(session, client);
1497 if ((w != null) && (w.mSurface != null)) {
1498 Surface.openTransaction();
1499 try {
1500 w.mSurface.setTransparentRegionHint(region);
1501 } finally {
1502 Surface.closeTransaction();
1503 }
1504 }
1505 }
1506 } finally {
1507 Binder.restoreCallingIdentity(origId);
1508 }
1509 }
1510
1511 void setInsetsWindow(Session session, IWindow client,
Romain Guy06882f82009-06-10 13:36:04 -07001512 int touchableInsets, Rect contentInsets,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001513 Rect visibleInsets) {
1514 long origId = Binder.clearCallingIdentity();
1515 try {
1516 synchronized (mWindowMap) {
1517 WindowState w = windowForClientLocked(session, client);
1518 if (w != null) {
1519 w.mGivenInsetsPending = false;
1520 w.mGivenContentInsets.set(contentInsets);
1521 w.mGivenVisibleInsets.set(visibleInsets);
1522 w.mTouchableInsets = touchableInsets;
1523 mLayoutNeeded = true;
1524 performLayoutAndPlaceSurfacesLocked();
1525 }
1526 }
1527 } finally {
1528 Binder.restoreCallingIdentity(origId);
1529 }
1530 }
Romain Guy06882f82009-06-10 13:36:04 -07001531
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001532 public void getWindowDisplayFrame(Session session, IWindow client,
1533 Rect outDisplayFrame) {
1534 synchronized(mWindowMap) {
1535 WindowState win = windowForClientLocked(session, client);
1536 if (win == null) {
1537 outDisplayFrame.setEmpty();
1538 return;
1539 }
1540 outDisplayFrame.set(win.mDisplayFrame);
1541 }
1542 }
1543
1544 public int relayoutWindow(Session session, IWindow client,
1545 WindowManager.LayoutParams attrs, int requestedWidth,
1546 int requestedHeight, int viewVisibility, boolean insetsPending,
1547 Rect outFrame, Rect outContentInsets, Rect outVisibleInsets,
1548 Surface outSurface) {
1549 boolean displayed = false;
1550 boolean inTouchMode;
1551 Configuration newConfig = null;
1552 long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001554 synchronized(mWindowMap) {
1555 WindowState win = windowForClientLocked(session, client);
1556 if (win == null) {
1557 return 0;
1558 }
1559 win.mRequestedWidth = requestedWidth;
1560 win.mRequestedHeight = requestedHeight;
1561
1562 if (attrs != null) {
1563 mPolicy.adjustWindowParamsLw(attrs);
1564 }
Romain Guy06882f82009-06-10 13:36:04 -07001565
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001566 int attrChanges = 0;
1567 int flagChanges = 0;
1568 if (attrs != null) {
1569 flagChanges = win.mAttrs.flags ^= attrs.flags;
1570 attrChanges = win.mAttrs.copyFrom(attrs);
1571 }
1572
1573 if (localLOGV) Log.v(
1574 TAG, "Relayout given client " + client.asBinder()
1575 + " (" + win.mAttrs.getTitle() + ")");
1576
1577
1578 if ((attrChanges & WindowManager.LayoutParams.ALPHA_CHANGED) != 0) {
1579 win.mAlpha = attrs.alpha;
1580 }
1581
1582 final boolean scaledWindow =
1583 ((win.mAttrs.flags & WindowManager.LayoutParams.FLAG_SCALED) != 0);
1584
1585 if (scaledWindow) {
1586 // requested{Width|Height} Surface's physical size
1587 // attrs.{width|height} Size on screen
1588 win.mHScale = (attrs.width != requestedWidth) ?
1589 (attrs.width / (float)requestedWidth) : 1.0f;
1590 win.mVScale = (attrs.height != requestedHeight) ?
1591 (attrs.height / (float)requestedHeight) : 1.0f;
1592 }
1593
1594 boolean imMayMove = (flagChanges&(
1595 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
1596 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07001597
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001598 boolean focusMayChange = win.mViewVisibility != viewVisibility
1599 || ((flagChanges&WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) != 0)
1600 || (!win.mRelayoutCalled);
Romain Guy06882f82009-06-10 13:36:04 -07001601
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602 win.mRelayoutCalled = true;
1603 final int oldVisibility = win.mViewVisibility;
1604 win.mViewVisibility = viewVisibility;
1605 if (viewVisibility == View.VISIBLE &&
1606 (win.mAppToken == null || !win.mAppToken.clientHidden)) {
1607 displayed = !win.isVisibleLw();
1608 if (win.mExiting) {
1609 win.mExiting = false;
1610 win.mAnimation = null;
1611 }
1612 if (win.mDestroying) {
1613 win.mDestroying = false;
1614 mDestroySurface.remove(win);
1615 }
1616 if (oldVisibility == View.GONE) {
1617 win.mEnterAnimationPending = true;
1618 }
1619 if (displayed && win.mSurface != null && !win.mDrawPending
1620 && !win.mCommitDrawPending && !mDisplayFrozen) {
1621 applyEnterAnimationLocked(win);
1622 }
1623 if ((attrChanges&WindowManager.LayoutParams.FORMAT_CHANGED) != 0) {
1624 // To change the format, we need to re-build the surface.
1625 win.destroySurfaceLocked();
1626 displayed = true;
1627 }
1628 try {
1629 Surface surface = win.createSurfaceLocked();
1630 if (surface != null) {
1631 outSurface.copyFrom(surface);
1632 } else {
1633 outSurface.clear();
1634 }
1635 } catch (Exception e) {
1636 Log.w(TAG, "Exception thrown when creating surface for client "
1637 + client + " (" + win.mAttrs.getTitle() + ")",
1638 e);
1639 Binder.restoreCallingIdentity(origId);
1640 return 0;
1641 }
1642 if (displayed) {
1643 focusMayChange = true;
1644 }
1645 if (win.mAttrs.type == TYPE_INPUT_METHOD
1646 && mInputMethodWindow == null) {
1647 mInputMethodWindow = win;
1648 imMayMove = true;
1649 }
1650 } else {
1651 win.mEnterAnimationPending = false;
1652 if (win.mSurface != null) {
1653 // If we are not currently running the exit animation, we
1654 // need to see about starting one.
1655 if (!win.mExiting) {
1656 // Try starting an animation; if there isn't one, we
1657 // can destroy the surface right away.
1658 int transit = WindowManagerPolicy.TRANSIT_EXIT;
1659 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
1660 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
1661 }
1662 if (win.isWinVisibleLw() &&
1663 applyAnimationLocked(win, transit, false)) {
1664 win.mExiting = true;
1665 mKeyWaiter.finishedKey(session, client, true,
1666 KeyWaiter.RETURN_NOTHING);
1667 } else if (win.isAnimating()) {
1668 // Currently in a hide animation... turn this into
1669 // an exit.
1670 win.mExiting = true;
1671 } else {
1672 if (mInputMethodWindow == win) {
1673 mInputMethodWindow = null;
1674 }
1675 win.destroySurfaceLocked();
1676 }
1677 }
1678 }
1679 outSurface.clear();
1680 }
1681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 if (focusMayChange) {
1683 //System.out.println("Focus may change: " + win.mAttrs.getTitle());
1684 if (updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001685 imMayMove = false;
1686 }
1687 //System.out.println("Relayout " + win + ": focus=" + mCurrentFocus);
1688 }
Romain Guy06882f82009-06-10 13:36:04 -07001689
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001690 // updateFocusedWindowLocked() already assigned layers so we only need to
1691 // reassign them at this point if the IM window state gets shuffled
1692 boolean assignLayers = false;
Romain Guy06882f82009-06-10 13:36:04 -07001693
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001694 if (imMayMove) {
1695 if (moveInputMethodWindowsIfNeededLocked(false)) {
1696 assignLayers = true;
1697 }
1698 }
Romain Guy06882f82009-06-10 13:36:04 -07001699
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001700 mLayoutNeeded = true;
1701 win.mGivenInsetsPending = insetsPending;
1702 if (assignLayers) {
1703 assignLayersLocked();
1704 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001705 newConfig = updateOrientationFromAppTokensLocked(null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001706 performLayoutAndPlaceSurfacesLocked();
1707 if (win.mAppToken != null) {
1708 win.mAppToken.updateReportedVisibilityLocked();
1709 }
1710 outFrame.set(win.mFrame);
1711 outContentInsets.set(win.mContentInsets);
1712 outVisibleInsets.set(win.mVisibleInsets);
1713 if (localLOGV) Log.v(
1714 TAG, "Relayout given client " + client.asBinder()
Romain Guy06882f82009-06-10 13:36:04 -07001715 + ", requestedWidth=" + requestedWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001716 + ", requestedHeight=" + requestedHeight
1717 + ", viewVisibility=" + viewVisibility
1718 + "\nRelayout returning frame=" + outFrame
1719 + ", surface=" + outSurface);
1720
1721 if (localLOGV || DEBUG_FOCUS) Log.v(
1722 TAG, "Relayout of " + win + ": focusMayChange=" + focusMayChange);
1723
1724 inTouchMode = mInTouchMode;
1725 }
1726
1727 if (newConfig != null) {
1728 sendNewConfiguration();
1729 }
Romain Guy06882f82009-06-10 13:36:04 -07001730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001731 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07001732
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001733 return (inTouchMode ? WindowManagerImpl.RELAYOUT_IN_TOUCH_MODE : 0)
1734 | (displayed ? WindowManagerImpl.RELAYOUT_FIRST_TIME : 0);
1735 }
1736
1737 public void finishDrawingWindow(Session session, IWindow client) {
1738 final long origId = Binder.clearCallingIdentity();
1739 synchronized(mWindowMap) {
1740 WindowState win = windowForClientLocked(session, client);
1741 if (win != null && win.finishDrawingLocked()) {
1742 mLayoutNeeded = true;
1743 performLayoutAndPlaceSurfacesLocked();
1744 }
1745 }
1746 Binder.restoreCallingIdentity(origId);
1747 }
1748
1749 private AttributeCache.Entry getCachedAnimations(WindowManager.LayoutParams lp) {
1750 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: params package="
1751 + (lp != null ? lp.packageName : null)
1752 + " resId=0x" + (lp != null ? Integer.toHexString(lp.windowAnimations) : null));
1753 if (lp != null && lp.windowAnimations != 0) {
1754 // If this is a system resource, don't try to load it from the
1755 // application resources. It is nice to avoid loading application
1756 // resources if we can.
1757 String packageName = lp.packageName != null ? lp.packageName : "android";
1758 int resId = lp.windowAnimations;
1759 if ((resId&0xFF000000) == 0x01000000) {
1760 packageName = "android";
1761 }
1762 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: picked package="
1763 + packageName);
1764 return AttributeCache.instance().get(packageName, resId,
1765 com.android.internal.R.styleable.WindowAnimation);
1766 }
1767 return null;
1768 }
Romain Guy06882f82009-06-10 13:36:04 -07001769
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001770 private void applyEnterAnimationLocked(WindowState win) {
1771 int transit = WindowManagerPolicy.TRANSIT_SHOW;
1772 if (win.mEnterAnimationPending) {
1773 win.mEnterAnimationPending = false;
1774 transit = WindowManagerPolicy.TRANSIT_ENTER;
1775 }
1776
1777 applyAnimationLocked(win, transit, true);
1778 }
1779
1780 private boolean applyAnimationLocked(WindowState win,
1781 int transit, boolean isEntrance) {
1782 if (win.mLocalAnimating && win.mAnimationIsEntrance == isEntrance) {
1783 // If we are trying to apply an animation, but already running
1784 // an animation of the same type, then just leave that one alone.
1785 return true;
1786 }
Romain Guy06882f82009-06-10 13:36:04 -07001787
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001788 // Only apply an animation if the display isn't frozen. If it is
1789 // frozen, there is no reason to animate and it can cause strange
1790 // artifacts when we unfreeze the display if some different animation
1791 // is running.
1792 if (!mDisplayFrozen) {
1793 int anim = mPolicy.selectAnimationLw(win, transit);
1794 int attr = -1;
1795 Animation a = null;
1796 if (anim != 0) {
1797 a = AnimationUtils.loadAnimation(mContext, anim);
1798 } else {
1799 switch (transit) {
1800 case WindowManagerPolicy.TRANSIT_ENTER:
1801 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
1802 break;
1803 case WindowManagerPolicy.TRANSIT_EXIT:
1804 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
1805 break;
1806 case WindowManagerPolicy.TRANSIT_SHOW:
1807 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
1808 break;
1809 case WindowManagerPolicy.TRANSIT_HIDE:
1810 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
1811 break;
1812 }
1813 if (attr >= 0) {
1814 a = loadAnimation(win.mAttrs, attr);
1815 }
1816 }
1817 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: win=" + win
1818 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
1819 + " mAnimation=" + win.mAnimation
1820 + " isEntrance=" + isEntrance);
1821 if (a != null) {
1822 if (DEBUG_ANIM) {
1823 RuntimeException e = new RuntimeException();
1824 e.fillInStackTrace();
1825 Log.v(TAG, "Loaded animation " + a + " for " + win, e);
1826 }
1827 win.setAnimation(a);
1828 win.mAnimationIsEntrance = isEntrance;
1829 }
1830 } else {
1831 win.clearAnimation();
1832 }
1833
1834 return win.mAnimation != null;
1835 }
1836
1837 private Animation loadAnimation(WindowManager.LayoutParams lp, int animAttr) {
1838 int anim = 0;
1839 Context context = mContext;
1840 if (animAttr >= 0) {
1841 AttributeCache.Entry ent = getCachedAnimations(lp);
1842 if (ent != null) {
1843 context = ent.context;
1844 anim = ent.array.getResourceId(animAttr, 0);
1845 }
1846 }
1847 if (anim != 0) {
1848 return AnimationUtils.loadAnimation(context, anim);
1849 }
1850 return null;
1851 }
Romain Guy06882f82009-06-10 13:36:04 -07001852
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001853 private boolean applyAnimationLocked(AppWindowToken wtoken,
1854 WindowManager.LayoutParams lp, int transit, boolean enter) {
1855 // Only apply an animation if the display isn't frozen. If it is
1856 // frozen, there is no reason to animate and it can cause strange
1857 // artifacts when we unfreeze the display if some different animation
1858 // is running.
1859 if (!mDisplayFrozen) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07001860 Animation a;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07001861 if (lp != null && (lp.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07001862 a = new FadeInOutAnimation(enter);
1863 if (DEBUG_ANIM) Log.v(TAG,
1864 "applying FadeInOutAnimation for a window in compatibility mode");
1865 } else {
1866 int animAttr = 0;
1867 switch (transit) {
1868 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
1869 animAttr = enter
1870 ? com.android.internal.R.styleable.WindowAnimation_activityOpenEnterAnimation
1871 : com.android.internal.R.styleable.WindowAnimation_activityOpenExitAnimation;
1872 break;
1873 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
1874 animAttr = enter
1875 ? com.android.internal.R.styleable.WindowAnimation_activityCloseEnterAnimation
1876 : com.android.internal.R.styleable.WindowAnimation_activityCloseExitAnimation;
1877 break;
1878 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
1879 animAttr = enter
1880 ? com.android.internal.R.styleable.WindowAnimation_taskOpenEnterAnimation
1881 : com.android.internal.R.styleable.WindowAnimation_taskOpenExitAnimation;
1882 break;
1883 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
1884 animAttr = enter
1885 ? com.android.internal.R.styleable.WindowAnimation_taskCloseEnterAnimation
1886 : com.android.internal.R.styleable.WindowAnimation_taskCloseExitAnimation;
1887 break;
1888 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
1889 animAttr = enter
1890 ? com.android.internal.R.styleable.WindowAnimation_taskToFrontEnterAnimation
1891 : com.android.internal.R.styleable.WindowAnimation_taskToFrontExitAnimation;
1892 break;
1893 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
1894 animAttr = enter
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07001895 ? com.android.internal.R.styleable.WindowAnimation_taskToBackEnterAnimation
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07001896 : com.android.internal.R.styleable.WindowAnimation_taskToBackExitAnimation;
1897 break;
1898 }
1899 a = loadAnimation(lp, animAttr);
1900 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: wtoken=" + wtoken
1901 + " anim=" + a
1902 + " animAttr=0x" + Integer.toHexString(animAttr)
1903 + " transit=" + transit);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001904 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001905 if (a != null) {
1906 if (DEBUG_ANIM) {
1907 RuntimeException e = new RuntimeException();
1908 e.fillInStackTrace();
1909 Log.v(TAG, "Loaded animation " + a + " for " + wtoken, e);
1910 }
1911 wtoken.setAnimation(a);
1912 }
1913 } else {
1914 wtoken.clearAnimation();
1915 }
1916
1917 return wtoken.animation != null;
1918 }
1919
1920 // -------------------------------------------------------------
1921 // Application Window Tokens
1922 // -------------------------------------------------------------
1923
1924 public void validateAppTokens(List tokens) {
1925 int v = tokens.size()-1;
1926 int m = mAppTokens.size()-1;
1927 while (v >= 0 && m >= 0) {
1928 AppWindowToken wtoken = mAppTokens.get(m);
1929 if (wtoken.removed) {
1930 m--;
1931 continue;
1932 }
1933 if (tokens.get(v) != wtoken.token) {
1934 Log.w(TAG, "Tokens out of sync: external is " + tokens.get(v)
1935 + " @ " + v + ", internal is " + wtoken.token + " @ " + m);
1936 }
1937 v--;
1938 m--;
1939 }
1940 while (v >= 0) {
1941 Log.w(TAG, "External token not found: " + tokens.get(v) + " @ " + v);
1942 v--;
1943 }
1944 while (m >= 0) {
1945 AppWindowToken wtoken = mAppTokens.get(m);
1946 if (!wtoken.removed) {
1947 Log.w(TAG, "Invalid internal token: " + wtoken.token + " @ " + m);
1948 }
1949 m--;
1950 }
1951 }
1952
1953 boolean checkCallingPermission(String permission, String func) {
1954 // Quick check: if the calling permission is me, it's all okay.
1955 if (Binder.getCallingPid() == Process.myPid()) {
1956 return true;
1957 }
Romain Guy06882f82009-06-10 13:36:04 -07001958
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001959 if (mContext.checkCallingPermission(permission)
1960 == PackageManager.PERMISSION_GRANTED) {
1961 return true;
1962 }
1963 String msg = "Permission Denial: " + func + " from pid="
1964 + Binder.getCallingPid()
1965 + ", uid=" + Binder.getCallingUid()
1966 + " requires " + permission;
1967 Log.w(TAG, msg);
1968 return false;
1969 }
Romain Guy06882f82009-06-10 13:36:04 -07001970
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001971 AppWindowToken findAppWindowToken(IBinder token) {
1972 WindowToken wtoken = mTokenMap.get(token);
1973 if (wtoken == null) {
1974 return null;
1975 }
1976 return wtoken.appWindowToken;
1977 }
Romain Guy06882f82009-06-10 13:36:04 -07001978
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001979 public void addWindowToken(IBinder token, int type) {
1980 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
1981 "addWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07001982 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001983 }
Romain Guy06882f82009-06-10 13:36:04 -07001984
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001985 synchronized(mWindowMap) {
1986 WindowToken wtoken = mTokenMap.get(token);
1987 if (wtoken != null) {
1988 Log.w(TAG, "Attempted to add existing input method token: " + token);
1989 return;
1990 }
1991 wtoken = new WindowToken(token, type, true);
1992 mTokenMap.put(token, wtoken);
1993 mTokenList.add(wtoken);
1994 }
1995 }
Romain Guy06882f82009-06-10 13:36:04 -07001996
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001997 public void removeWindowToken(IBinder token) {
1998 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
1999 "removeWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002000 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002001 }
2002
2003 final long origId = Binder.clearCallingIdentity();
2004 synchronized(mWindowMap) {
2005 WindowToken wtoken = mTokenMap.remove(token);
2006 mTokenList.remove(wtoken);
2007 if (wtoken != null) {
2008 boolean delayed = false;
2009 if (!wtoken.hidden) {
2010 wtoken.hidden = true;
Romain Guy06882f82009-06-10 13:36:04 -07002011
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002012 final int N = wtoken.windows.size();
2013 boolean changed = false;
Romain Guy06882f82009-06-10 13:36:04 -07002014
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002015 for (int i=0; i<N; i++) {
2016 WindowState win = wtoken.windows.get(i);
2017
2018 if (win.isAnimating()) {
2019 delayed = true;
2020 }
Romain Guy06882f82009-06-10 13:36:04 -07002021
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002022 if (win.isVisibleNow()) {
2023 applyAnimationLocked(win,
2024 WindowManagerPolicy.TRANSIT_EXIT, false);
2025 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2026 KeyWaiter.RETURN_NOTHING);
2027 changed = true;
2028 }
2029 }
2030
2031 if (changed) {
2032 mLayoutNeeded = true;
2033 performLayoutAndPlaceSurfacesLocked();
2034 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2035 }
Romain Guy06882f82009-06-10 13:36:04 -07002036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002037 if (delayed) {
2038 mExitingTokens.add(wtoken);
2039 }
2040 }
Romain Guy06882f82009-06-10 13:36:04 -07002041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002042 } else {
2043 Log.w(TAG, "Attempted to remove non-existing token: " + token);
2044 }
2045 }
2046 Binder.restoreCallingIdentity(origId);
2047 }
2048
2049 public void addAppToken(int addPos, IApplicationToken token,
2050 int groupId, int requestedOrientation, boolean fullscreen) {
2051 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2052 "addAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002053 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002054 }
Romain Guy06882f82009-06-10 13:36:04 -07002055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002056 synchronized(mWindowMap) {
2057 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2058 if (wtoken != null) {
2059 Log.w(TAG, "Attempted to add existing app token: " + token);
2060 return;
2061 }
2062 wtoken = new AppWindowToken(token);
2063 wtoken.groupId = groupId;
2064 wtoken.appFullscreen = fullscreen;
2065 wtoken.requestedOrientation = requestedOrientation;
2066 mAppTokens.add(addPos, wtoken);
Dave Bortcfe65242009-04-09 14:51:04 -07002067 if (localLOGV) Log.v(TAG, "Adding new app token: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002068 mTokenMap.put(token.asBinder(), wtoken);
2069 mTokenList.add(wtoken);
Romain Guy06882f82009-06-10 13:36:04 -07002070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002071 // Application tokens start out hidden.
2072 wtoken.hidden = true;
2073 wtoken.hiddenRequested = true;
Romain Guy06882f82009-06-10 13:36:04 -07002074
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002075 //dump();
2076 }
2077 }
Romain Guy06882f82009-06-10 13:36:04 -07002078
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002079 public void setAppGroupId(IBinder token, int groupId) {
2080 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2081 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002082 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002083 }
2084
2085 synchronized(mWindowMap) {
2086 AppWindowToken wtoken = findAppWindowToken(token);
2087 if (wtoken == null) {
2088 Log.w(TAG, "Attempted to set group id of non-existing app token: " + token);
2089 return;
2090 }
2091 wtoken.groupId = groupId;
2092 }
2093 }
Romain Guy06882f82009-06-10 13:36:04 -07002094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002095 public int getOrientationFromWindowsLocked() {
2096 int pos = mWindows.size() - 1;
2097 while (pos >= 0) {
2098 WindowState wtoken = (WindowState) mWindows.get(pos);
2099 pos--;
2100 if (wtoken.mAppToken != null) {
2101 // We hit an application window. so the orientation will be determined by the
2102 // app window. No point in continuing further.
2103 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2104 }
2105 if (!wtoken.isVisibleLw()) {
2106 continue;
2107 }
2108 int req = wtoken.mAttrs.screenOrientation;
2109 if((req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) ||
2110 (req == ActivityInfo.SCREEN_ORIENTATION_BEHIND)){
2111 continue;
2112 } else {
2113 return req;
2114 }
2115 }
2116 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2117 }
Romain Guy06882f82009-06-10 13:36:04 -07002118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002119 public int getOrientationFromAppTokensLocked() {
2120 int pos = mAppTokens.size() - 1;
2121 int curGroup = 0;
2122 int lastOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Owen Lin3413b892009-05-01 17:12:32 -07002123 boolean findingBehind = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002124 boolean haveGroup = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002125 boolean lastFullscreen = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002126 while (pos >= 0) {
2127 AppWindowToken wtoken = mAppTokens.get(pos);
2128 pos--;
Owen Lin3413b892009-05-01 17:12:32 -07002129 // if we're about to tear down this window and not seek for
2130 // the behind activity, don't use it for orientation
2131 if (!findingBehind
2132 && (!wtoken.hidden && wtoken.hiddenRequested)) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002133 continue;
2134 }
2135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002136 if (!haveGroup) {
2137 // We ignore any hidden applications on the top.
2138 if (wtoken.hiddenRequested || wtoken.willBeHidden) {
2139 continue;
2140 }
2141 haveGroup = true;
2142 curGroup = wtoken.groupId;
2143 lastOrientation = wtoken.requestedOrientation;
2144 } else if (curGroup != wtoken.groupId) {
2145 // If we have hit a new application group, and the bottom
2146 // of the previous group didn't explicitly say to use
The Android Open Source Project4df24232009-03-05 14:34:35 -08002147 // the orientation behind it, and the last app was
2148 // full screen, then we'll stick with the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002149 // user's orientation.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002150 if (lastOrientation != ActivityInfo.SCREEN_ORIENTATION_BEHIND
2151 && lastFullscreen) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002152 return lastOrientation;
2153 }
2154 }
2155 int or = wtoken.requestedOrientation;
Owen Lin3413b892009-05-01 17:12:32 -07002156 // If this application is fullscreen, and didn't explicitly say
2157 // to use the orientation behind it, then just take whatever
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002158 // orientation it has and ignores whatever is under it.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002159 lastFullscreen = wtoken.appFullscreen;
Romain Guy06882f82009-06-10 13:36:04 -07002160 if (lastFullscreen
Owen Lin3413b892009-05-01 17:12:32 -07002161 && or != ActivityInfo.SCREEN_ORIENTATION_BEHIND) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002162 return or;
2163 }
2164 // If this application has requested an explicit orientation,
2165 // then use it.
2166 if (or == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE ||
2167 or == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ||
2168 or == ActivityInfo.SCREEN_ORIENTATION_SENSOR ||
2169 or == ActivityInfo.SCREEN_ORIENTATION_NOSENSOR ||
2170 or == ActivityInfo.SCREEN_ORIENTATION_USER) {
2171 return or;
2172 }
Owen Lin3413b892009-05-01 17:12:32 -07002173 findingBehind |= (or == ActivityInfo.SCREEN_ORIENTATION_BEHIND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002174 }
2175 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2176 }
Romain Guy06882f82009-06-10 13:36:04 -07002177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002178 public Configuration updateOrientationFromAppTokens(
The Android Open Source Project10592532009-03-18 17:39:46 -07002179 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002180 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2181 "updateOrientationFromAppTokens()")) {
2182 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
2183 }
2184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002185 Configuration config;
2186 long ident = Binder.clearCallingIdentity();
Dianne Hackborncfaef692009-06-15 14:24:44 -07002187 config = updateOrientationFromAppTokensUnchecked(currentConfig,
2188 freezeThisOneIfNeeded);
2189 Binder.restoreCallingIdentity(ident);
2190 return config;
2191 }
2192
2193 Configuration updateOrientationFromAppTokensUnchecked(
2194 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
2195 Configuration config;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002196 synchronized(mWindowMap) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002197 config = updateOrientationFromAppTokensLocked(currentConfig, freezeThisOneIfNeeded);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002198 }
2199 if (config != null) {
2200 mLayoutNeeded = true;
2201 performLayoutAndPlaceSurfacesLocked();
2202 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002203 return config;
2204 }
Romain Guy06882f82009-06-10 13:36:04 -07002205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002206 /*
2207 * The orientation is computed from non-application windows first. If none of
2208 * the non-application windows specify orientation, the orientation is computed from
Romain Guy06882f82009-06-10 13:36:04 -07002209 * application tokens.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002210 * @see android.view.IWindowManager#updateOrientationFromAppTokens(
2211 * android.os.IBinder)
2212 */
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002213 Configuration updateOrientationFromAppTokensLocked(
The Android Open Source Project10592532009-03-18 17:39:46 -07002214 Configuration appConfig, IBinder freezeThisOneIfNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002215 boolean changed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002216 long ident = Binder.clearCallingIdentity();
2217 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002218 int req = computeForcedAppOrientationLocked();
Romain Guy06882f82009-06-10 13:36:04 -07002219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002220 if (req != mForcedAppOrientation) {
2221 changed = true;
2222 mForcedAppOrientation = req;
2223 //send a message to Policy indicating orientation change to take
2224 //action like disabling/enabling sensors etc.,
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002225 mPolicy.setCurrentOrientationLw(req);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002226 }
Romain Guy06882f82009-06-10 13:36:04 -07002227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002228 if (changed) {
2229 changed = setRotationUncheckedLocked(
Dianne Hackborn321ae682009-03-27 16:16:03 -07002230 WindowManagerPolicy.USE_LAST_ROTATION,
2231 mLastRotationFlags & (~Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002232 if (changed) {
2233 if (freezeThisOneIfNeeded != null) {
2234 AppWindowToken wtoken = findAppWindowToken(
2235 freezeThisOneIfNeeded);
2236 if (wtoken != null) {
2237 startAppFreezingScreenLocked(wtoken,
2238 ActivityInfo.CONFIG_ORIENTATION);
2239 }
2240 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07002241 return computeNewConfigurationLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002242 }
2243 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002244
2245 // No obvious action we need to take, but if our current
2246 // state mismatches the activity maanager's, update it
2247 if (appConfig != null) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07002248 mTempConfiguration.setToDefaults();
2249 if (computeNewConfigurationLocked(mTempConfiguration)) {
2250 if (appConfig.diff(mTempConfiguration) != 0) {
2251 Log.i(TAG, "Config changed: " + mTempConfiguration);
2252 return new Configuration(mTempConfiguration);
2253 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002254 }
2255 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002256 } finally {
2257 Binder.restoreCallingIdentity(ident);
2258 }
Romain Guy06882f82009-06-10 13:36:04 -07002259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002260 return null;
2261 }
Romain Guy06882f82009-06-10 13:36:04 -07002262
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002263 int computeForcedAppOrientationLocked() {
2264 int req = getOrientationFromWindowsLocked();
2265 if (req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
2266 req = getOrientationFromAppTokensLocked();
2267 }
2268 return req;
2269 }
Romain Guy06882f82009-06-10 13:36:04 -07002270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002271 public void setAppOrientation(IApplicationToken token, int requestedOrientation) {
2272 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2273 "setAppOrientation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002274 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002275 }
Romain Guy06882f82009-06-10 13:36:04 -07002276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002277 synchronized(mWindowMap) {
2278 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2279 if (wtoken == null) {
2280 Log.w(TAG, "Attempted to set orientation of non-existing app token: " + token);
2281 return;
2282 }
Romain Guy06882f82009-06-10 13:36:04 -07002283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002284 wtoken.requestedOrientation = requestedOrientation;
2285 }
2286 }
Romain Guy06882f82009-06-10 13:36:04 -07002287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002288 public int getAppOrientation(IApplicationToken token) {
2289 synchronized(mWindowMap) {
2290 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2291 if (wtoken == null) {
2292 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2293 }
Romain Guy06882f82009-06-10 13:36:04 -07002294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002295 return wtoken.requestedOrientation;
2296 }
2297 }
Romain Guy06882f82009-06-10 13:36:04 -07002298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002299 public void setFocusedApp(IBinder token, boolean moveFocusNow) {
2300 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2301 "setFocusedApp()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002302 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002303 }
2304
2305 synchronized(mWindowMap) {
2306 boolean changed = false;
2307 if (token == null) {
2308 if (DEBUG_FOCUS) Log.v(TAG, "Clearing focused app, was " + mFocusedApp);
2309 changed = mFocusedApp != null;
2310 mFocusedApp = null;
2311 mKeyWaiter.tickle();
2312 } else {
2313 AppWindowToken newFocus = findAppWindowToken(token);
2314 if (newFocus == null) {
2315 Log.w(TAG, "Attempted to set focus to non-existing app token: " + token);
2316 return;
2317 }
2318 changed = mFocusedApp != newFocus;
2319 mFocusedApp = newFocus;
2320 if (DEBUG_FOCUS) Log.v(TAG, "Set focused app to: " + mFocusedApp);
2321 mKeyWaiter.tickle();
2322 }
2323
2324 if (moveFocusNow && changed) {
2325 final long origId = Binder.clearCallingIdentity();
2326 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2327 Binder.restoreCallingIdentity(origId);
2328 }
2329 }
2330 }
2331
2332 public void prepareAppTransition(int transit) {
2333 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2334 "prepareAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002335 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002336 }
Romain Guy06882f82009-06-10 13:36:04 -07002337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002338 synchronized(mWindowMap) {
2339 if (DEBUG_APP_TRANSITIONS) Log.v(
2340 TAG, "Prepare app transition: transit=" + transit
2341 + " mNextAppTransition=" + mNextAppTransition);
2342 if (!mDisplayFrozen) {
2343 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
2344 mNextAppTransition = transit;
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07002345 } else if (transit == WindowManagerPolicy.TRANSIT_TASK_OPEN
2346 && mNextAppTransition == WindowManagerPolicy.TRANSIT_TASK_CLOSE) {
2347 // Opening a new task always supersedes a close for the anim.
2348 mNextAppTransition = transit;
2349 } else if (transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN
2350 && mNextAppTransition == WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE) {
2351 // Opening a new activity always supersedes a close for the anim.
2352 mNextAppTransition = transit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002353 }
2354 mAppTransitionReady = false;
2355 mAppTransitionTimeout = false;
2356 mStartingIconInTransition = false;
2357 mSkipAppTransitionAnimation = false;
2358 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
2359 mH.sendMessageDelayed(mH.obtainMessage(H.APP_TRANSITION_TIMEOUT),
2360 5000);
2361 }
2362 }
2363 }
2364
2365 public int getPendingAppTransition() {
2366 return mNextAppTransition;
2367 }
Romain Guy06882f82009-06-10 13:36:04 -07002368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002369 public void executeAppTransition() {
2370 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2371 "executeAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002372 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002373 }
Romain Guy06882f82009-06-10 13:36:04 -07002374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002375 synchronized(mWindowMap) {
2376 if (DEBUG_APP_TRANSITIONS) Log.v(
2377 TAG, "Execute app transition: mNextAppTransition=" + mNextAppTransition);
2378 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
2379 mAppTransitionReady = true;
2380 final long origId = Binder.clearCallingIdentity();
2381 performLayoutAndPlaceSurfacesLocked();
2382 Binder.restoreCallingIdentity(origId);
2383 }
2384 }
2385 }
2386
2387 public void setAppStartingWindow(IBinder token, String pkg,
2388 int theme, CharSequence nonLocalizedLabel, int labelRes, int icon,
2389 IBinder transferFrom, boolean createIfNeeded) {
2390 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2391 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002392 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002393 }
2394
2395 synchronized(mWindowMap) {
2396 if (DEBUG_STARTING_WINDOW) Log.v(
2397 TAG, "setAppStartingIcon: token=" + token + " pkg=" + pkg
2398 + " transferFrom=" + transferFrom);
Romain Guy06882f82009-06-10 13:36:04 -07002399
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002400 AppWindowToken wtoken = findAppWindowToken(token);
2401 if (wtoken == null) {
2402 Log.w(TAG, "Attempted to set icon of non-existing app token: " + token);
2403 return;
2404 }
2405
2406 // If the display is frozen, we won't do anything until the
2407 // actual window is displayed so there is no reason to put in
2408 // the starting window.
2409 if (mDisplayFrozen) {
2410 return;
2411 }
Romain Guy06882f82009-06-10 13:36:04 -07002412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002413 if (wtoken.startingData != null) {
2414 return;
2415 }
Romain Guy06882f82009-06-10 13:36:04 -07002416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002417 if (transferFrom != null) {
2418 AppWindowToken ttoken = findAppWindowToken(transferFrom);
2419 if (ttoken != null) {
2420 WindowState startingWindow = ttoken.startingWindow;
2421 if (startingWindow != null) {
2422 if (mStartingIconInTransition) {
2423 // In this case, the starting icon has already
2424 // been displayed, so start letting windows get
2425 // shown immediately without any more transitions.
2426 mSkipAppTransitionAnimation = true;
2427 }
2428 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
2429 "Moving existing starting from " + ttoken
2430 + " to " + wtoken);
2431 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002433 // Transfer the starting window over to the new
2434 // token.
2435 wtoken.startingData = ttoken.startingData;
2436 wtoken.startingView = ttoken.startingView;
2437 wtoken.startingWindow = startingWindow;
2438 ttoken.startingData = null;
2439 ttoken.startingView = null;
2440 ttoken.startingWindow = null;
2441 ttoken.startingMoved = true;
2442 startingWindow.mToken = wtoken;
Dianne Hackbornef49c572009-03-24 19:27:32 -07002443 startingWindow.mRootToken = wtoken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002444 startingWindow.mAppToken = wtoken;
2445 mWindows.remove(startingWindow);
2446 ttoken.windows.remove(startingWindow);
2447 ttoken.allAppWindows.remove(startingWindow);
2448 addWindowToListInOrderLocked(startingWindow, true);
2449 wtoken.allAppWindows.add(startingWindow);
Romain Guy06882f82009-06-10 13:36:04 -07002450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002451 // Propagate other interesting state between the
2452 // tokens. If the old token is displayed, we should
2453 // immediately force the new one to be displayed. If
2454 // it is animating, we need to move that animation to
2455 // the new one.
2456 if (ttoken.allDrawn) {
2457 wtoken.allDrawn = true;
2458 }
2459 if (ttoken.firstWindowDrawn) {
2460 wtoken.firstWindowDrawn = true;
2461 }
2462 if (!ttoken.hidden) {
2463 wtoken.hidden = false;
2464 wtoken.hiddenRequested = false;
2465 wtoken.willBeHidden = false;
2466 }
2467 if (wtoken.clientHidden != ttoken.clientHidden) {
2468 wtoken.clientHidden = ttoken.clientHidden;
2469 wtoken.sendAppVisibilityToClients();
2470 }
2471 if (ttoken.animation != null) {
2472 wtoken.animation = ttoken.animation;
2473 wtoken.animating = ttoken.animating;
2474 wtoken.animLayerAdjustment = ttoken.animLayerAdjustment;
2475 ttoken.animation = null;
2476 ttoken.animLayerAdjustment = 0;
2477 wtoken.updateLayers();
2478 ttoken.updateLayers();
2479 }
Romain Guy06882f82009-06-10 13:36:04 -07002480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002481 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002482 mLayoutNeeded = true;
2483 performLayoutAndPlaceSurfacesLocked();
2484 Binder.restoreCallingIdentity(origId);
2485 return;
2486 } else if (ttoken.startingData != null) {
2487 // The previous app was getting ready to show a
2488 // starting window, but hasn't yet done so. Steal it!
2489 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
2490 "Moving pending starting from " + ttoken
2491 + " to " + wtoken);
2492 wtoken.startingData = ttoken.startingData;
2493 ttoken.startingData = null;
2494 ttoken.startingMoved = true;
2495 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
2496 // Note: we really want to do sendMessageAtFrontOfQueue() because we
2497 // want to process the message ASAP, before any other queued
2498 // messages.
2499 mH.sendMessageAtFrontOfQueue(m);
2500 return;
2501 }
2502 }
2503 }
2504
2505 // There is no existing starting window, and the caller doesn't
2506 // want us to create one, so that's it!
2507 if (!createIfNeeded) {
2508 return;
2509 }
Romain Guy06882f82009-06-10 13:36:04 -07002510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002511 mStartingIconInTransition = true;
2512 wtoken.startingData = new StartingData(
2513 pkg, theme, nonLocalizedLabel,
2514 labelRes, icon);
2515 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
2516 // Note: we really want to do sendMessageAtFrontOfQueue() because we
2517 // want to process the message ASAP, before any other queued
2518 // messages.
2519 mH.sendMessageAtFrontOfQueue(m);
2520 }
2521 }
2522
2523 public void setAppWillBeHidden(IBinder token) {
2524 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2525 "setAppWillBeHidden()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002526 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002527 }
2528
2529 AppWindowToken wtoken;
2530
2531 synchronized(mWindowMap) {
2532 wtoken = findAppWindowToken(token);
2533 if (wtoken == null) {
2534 Log.w(TAG, "Attempted to set will be hidden of non-existing app token: " + token);
2535 return;
2536 }
2537 wtoken.willBeHidden = true;
2538 }
2539 }
Romain Guy06882f82009-06-10 13:36:04 -07002540
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002541 boolean setTokenVisibilityLocked(AppWindowToken wtoken, WindowManager.LayoutParams lp,
2542 boolean visible, int transit, boolean performLayout) {
2543 boolean delayed = false;
2544
2545 if (wtoken.clientHidden == visible) {
2546 wtoken.clientHidden = !visible;
2547 wtoken.sendAppVisibilityToClients();
2548 }
Romain Guy06882f82009-06-10 13:36:04 -07002549
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002550 wtoken.willBeHidden = false;
2551 if (wtoken.hidden == visible) {
2552 final int N = wtoken.allAppWindows.size();
2553 boolean changed = false;
2554 if (DEBUG_APP_TRANSITIONS) Log.v(
2555 TAG, "Changing app " + wtoken + " hidden=" + wtoken.hidden
2556 + " performLayout=" + performLayout);
Romain Guy06882f82009-06-10 13:36:04 -07002557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002558 boolean runningAppAnimation = false;
Romain Guy06882f82009-06-10 13:36:04 -07002559
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002560 if (transit != WindowManagerPolicy.TRANSIT_NONE) {
2561 if (wtoken.animation == sDummyAnimation) {
2562 wtoken.animation = null;
2563 }
2564 applyAnimationLocked(wtoken, lp, transit, visible);
2565 changed = true;
2566 if (wtoken.animation != null) {
2567 delayed = runningAppAnimation = true;
2568 }
2569 }
Romain Guy06882f82009-06-10 13:36:04 -07002570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002571 for (int i=0; i<N; i++) {
2572 WindowState win = wtoken.allAppWindows.get(i);
2573 if (win == wtoken.startingWindow) {
2574 continue;
2575 }
2576
2577 if (win.isAnimating()) {
2578 delayed = true;
2579 }
Romain Guy06882f82009-06-10 13:36:04 -07002580
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002581 //Log.i(TAG, "Window " + win + ": vis=" + win.isVisible());
2582 //win.dump(" ");
2583 if (visible) {
2584 if (!win.isVisibleNow()) {
2585 if (!runningAppAnimation) {
2586 applyAnimationLocked(win,
2587 WindowManagerPolicy.TRANSIT_ENTER, true);
2588 }
2589 changed = true;
2590 }
2591 } else if (win.isVisibleNow()) {
2592 if (!runningAppAnimation) {
2593 applyAnimationLocked(win,
2594 WindowManagerPolicy.TRANSIT_EXIT, false);
2595 }
2596 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2597 KeyWaiter.RETURN_NOTHING);
2598 changed = true;
2599 }
2600 }
2601
2602 wtoken.hidden = wtoken.hiddenRequested = !visible;
2603 if (!visible) {
2604 unsetAppFreezingScreenLocked(wtoken, true, true);
2605 } else {
2606 // If we are being set visible, and the starting window is
2607 // not yet displayed, then make sure it doesn't get displayed.
2608 WindowState swin = wtoken.startingWindow;
2609 if (swin != null && (swin.mDrawPending
2610 || swin.mCommitDrawPending)) {
2611 swin.mPolicyVisibility = false;
2612 swin.mPolicyVisibilityAfterAnim = false;
2613 }
2614 }
Romain Guy06882f82009-06-10 13:36:04 -07002615
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002616 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "setTokenVisibilityLocked: " + wtoken
2617 + ": hidden=" + wtoken.hidden + " hiddenRequested="
2618 + wtoken.hiddenRequested);
Romain Guy06882f82009-06-10 13:36:04 -07002619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002620 if (changed && performLayout) {
2621 mLayoutNeeded = true;
2622 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002623 performLayoutAndPlaceSurfacesLocked();
2624 }
2625 }
2626
2627 if (wtoken.animation != null) {
2628 delayed = true;
2629 }
Romain Guy06882f82009-06-10 13:36:04 -07002630
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002631 return delayed;
2632 }
2633
2634 public void setAppVisibility(IBinder token, boolean visible) {
2635 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2636 "setAppVisibility()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002637 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002638 }
2639
2640 AppWindowToken wtoken;
2641
2642 synchronized(mWindowMap) {
2643 wtoken = findAppWindowToken(token);
2644 if (wtoken == null) {
2645 Log.w(TAG, "Attempted to set visibility of non-existing app token: " + token);
2646 return;
2647 }
2648
2649 if (DEBUG_APP_TRANSITIONS || DEBUG_ORIENTATION) {
2650 RuntimeException e = new RuntimeException();
2651 e.fillInStackTrace();
2652 Log.v(TAG, "setAppVisibility(" + token + ", " + visible
2653 + "): mNextAppTransition=" + mNextAppTransition
2654 + " hidden=" + wtoken.hidden
2655 + " hiddenRequested=" + wtoken.hiddenRequested, e);
2656 }
Romain Guy06882f82009-06-10 13:36:04 -07002657
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002658 // If we are preparing an app transition, then delay changing
2659 // the visibility of this token until we execute that transition.
2660 if (!mDisplayFrozen && mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
2661 // Already in requested state, don't do anything more.
2662 if (wtoken.hiddenRequested != visible) {
2663 return;
2664 }
2665 wtoken.hiddenRequested = !visible;
Romain Guy06882f82009-06-10 13:36:04 -07002666
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002667 if (DEBUG_APP_TRANSITIONS) Log.v(
2668 TAG, "Setting dummy animation on: " + wtoken);
2669 wtoken.setDummyAnimation();
2670 mOpeningApps.remove(wtoken);
2671 mClosingApps.remove(wtoken);
2672 wtoken.inPendingTransaction = true;
2673 if (visible) {
2674 mOpeningApps.add(wtoken);
2675 wtoken.allDrawn = false;
2676 wtoken.startingDisplayed = false;
2677 wtoken.startingMoved = false;
Romain Guy06882f82009-06-10 13:36:04 -07002678
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002679 if (wtoken.clientHidden) {
2680 // In the case where we are making an app visible
2681 // but holding off for a transition, we still need
2682 // to tell the client to make its windows visible so
2683 // they get drawn. Otherwise, we will wait on
2684 // performing the transition until all windows have
2685 // been drawn, they never will be, and we are sad.
2686 wtoken.clientHidden = false;
2687 wtoken.sendAppVisibilityToClients();
2688 }
2689 } else {
2690 mClosingApps.add(wtoken);
2691 }
2692 return;
2693 }
Romain Guy06882f82009-06-10 13:36:04 -07002694
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002695 final long origId = Binder.clearCallingIdentity();
2696 setTokenVisibilityLocked(wtoken, null, visible, WindowManagerPolicy.TRANSIT_NONE, true);
2697 wtoken.updateReportedVisibilityLocked();
2698 Binder.restoreCallingIdentity(origId);
2699 }
2700 }
2701
2702 void unsetAppFreezingScreenLocked(AppWindowToken wtoken,
2703 boolean unfreezeSurfaceNow, boolean force) {
2704 if (wtoken.freezingScreen) {
2705 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + wtoken
2706 + " force=" + force);
2707 final int N = wtoken.allAppWindows.size();
2708 boolean unfrozeWindows = false;
2709 for (int i=0; i<N; i++) {
2710 WindowState w = wtoken.allAppWindows.get(i);
2711 if (w.mAppFreezing) {
2712 w.mAppFreezing = false;
2713 if (w.mSurface != null && !w.mOrientationChanging) {
2714 w.mOrientationChanging = true;
2715 }
2716 unfrozeWindows = true;
2717 }
2718 }
2719 if (force || unfrozeWindows) {
2720 if (DEBUG_ORIENTATION) Log.v(TAG, "No longer freezing: " + wtoken);
2721 wtoken.freezingScreen = false;
2722 mAppsFreezingScreen--;
2723 }
2724 if (unfreezeSurfaceNow) {
2725 if (unfrozeWindows) {
2726 mLayoutNeeded = true;
2727 performLayoutAndPlaceSurfacesLocked();
2728 }
2729 if (mAppsFreezingScreen == 0 && !mWindowsFreezingScreen) {
2730 stopFreezingDisplayLocked();
2731 }
2732 }
2733 }
2734 }
Romain Guy06882f82009-06-10 13:36:04 -07002735
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002736 public void startAppFreezingScreenLocked(AppWindowToken wtoken,
2737 int configChanges) {
2738 if (DEBUG_ORIENTATION) {
2739 RuntimeException e = new RuntimeException();
2740 e.fillInStackTrace();
2741 Log.i(TAG, "Set freezing of " + wtoken.appToken
2742 + ": hidden=" + wtoken.hidden + " freezing="
2743 + wtoken.freezingScreen, e);
2744 }
2745 if (!wtoken.hiddenRequested) {
2746 if (!wtoken.freezingScreen) {
2747 wtoken.freezingScreen = true;
2748 mAppsFreezingScreen++;
2749 if (mAppsFreezingScreen == 1) {
2750 startFreezingDisplayLocked();
2751 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
2752 mH.sendMessageDelayed(mH.obtainMessage(H.APP_FREEZE_TIMEOUT),
2753 5000);
2754 }
2755 }
2756 final int N = wtoken.allAppWindows.size();
2757 for (int i=0; i<N; i++) {
2758 WindowState w = wtoken.allAppWindows.get(i);
2759 w.mAppFreezing = true;
2760 }
2761 }
2762 }
Romain Guy06882f82009-06-10 13:36:04 -07002763
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002764 public void startAppFreezingScreen(IBinder token, int configChanges) {
2765 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2766 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002767 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002768 }
2769
2770 synchronized(mWindowMap) {
2771 if (configChanges == 0 && !mDisplayFrozen) {
2772 if (DEBUG_ORIENTATION) Log.v(TAG, "Skipping set freeze of " + token);
2773 return;
2774 }
Romain Guy06882f82009-06-10 13:36:04 -07002775
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002776 AppWindowToken wtoken = findAppWindowToken(token);
2777 if (wtoken == null || wtoken.appToken == null) {
2778 Log.w(TAG, "Attempted to freeze screen with non-existing app token: " + wtoken);
2779 return;
2780 }
2781 final long origId = Binder.clearCallingIdentity();
2782 startAppFreezingScreenLocked(wtoken, configChanges);
2783 Binder.restoreCallingIdentity(origId);
2784 }
2785 }
Romain Guy06882f82009-06-10 13:36:04 -07002786
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002787 public void stopAppFreezingScreen(IBinder token, boolean force) {
2788 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2789 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002790 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002791 }
2792
2793 synchronized(mWindowMap) {
2794 AppWindowToken wtoken = findAppWindowToken(token);
2795 if (wtoken == null || wtoken.appToken == null) {
2796 return;
2797 }
2798 final long origId = Binder.clearCallingIdentity();
2799 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + token
2800 + ": hidden=" + wtoken.hidden + " freezing=" + wtoken.freezingScreen);
2801 unsetAppFreezingScreenLocked(wtoken, true, force);
2802 Binder.restoreCallingIdentity(origId);
2803 }
2804 }
Romain Guy06882f82009-06-10 13:36:04 -07002805
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002806 public void removeAppToken(IBinder token) {
2807 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2808 "removeAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002809 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002810 }
2811
2812 AppWindowToken wtoken = null;
2813 AppWindowToken startingToken = null;
2814 boolean delayed = false;
2815
2816 final long origId = Binder.clearCallingIdentity();
2817 synchronized(mWindowMap) {
2818 WindowToken basewtoken = mTokenMap.remove(token);
2819 mTokenList.remove(basewtoken);
2820 if (basewtoken != null && (wtoken=basewtoken.appWindowToken) != null) {
2821 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "Removing app token: " + wtoken);
2822 delayed = setTokenVisibilityLocked(wtoken, null, false, WindowManagerPolicy.TRANSIT_NONE, true);
2823 wtoken.inPendingTransaction = false;
2824 mOpeningApps.remove(wtoken);
2825 if (mClosingApps.contains(wtoken)) {
2826 delayed = true;
2827 } else if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
2828 mClosingApps.add(wtoken);
2829 delayed = true;
2830 }
2831 if (DEBUG_APP_TRANSITIONS) Log.v(
2832 TAG, "Removing app " + wtoken + " delayed=" + delayed
2833 + " animation=" + wtoken.animation
2834 + " animating=" + wtoken.animating);
2835 if (delayed) {
2836 // set the token aside because it has an active animation to be finished
2837 mExitingAppTokens.add(wtoken);
2838 }
2839 mAppTokens.remove(wtoken);
2840 wtoken.removed = true;
2841 if (wtoken.startingData != null) {
2842 startingToken = wtoken;
2843 }
2844 unsetAppFreezingScreenLocked(wtoken, true, true);
2845 if (mFocusedApp == wtoken) {
2846 if (DEBUG_FOCUS) Log.v(TAG, "Removing focused app token:" + wtoken);
2847 mFocusedApp = null;
2848 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2849 mKeyWaiter.tickle();
2850 }
2851 } else {
2852 Log.w(TAG, "Attempted to remove non-existing app token: " + token);
2853 }
Romain Guy06882f82009-06-10 13:36:04 -07002854
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002855 if (!delayed && wtoken != null) {
2856 wtoken.updateReportedVisibilityLocked();
2857 }
2858 }
2859 Binder.restoreCallingIdentity(origId);
2860
2861 if (startingToken != null) {
2862 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Schedule remove starting "
2863 + startingToken + ": app token removed");
2864 Message m = mH.obtainMessage(H.REMOVE_STARTING, startingToken);
2865 mH.sendMessage(m);
2866 }
2867 }
2868
2869 private boolean tmpRemoveAppWindowsLocked(WindowToken token) {
2870 final int NW = token.windows.size();
2871 for (int i=0; i<NW; i++) {
2872 WindowState win = token.windows.get(i);
2873 mWindows.remove(win);
2874 int j = win.mChildWindows.size();
2875 while (j > 0) {
2876 j--;
2877 mWindows.remove(win.mChildWindows.get(j));
2878 }
2879 }
2880 return NW > 0;
2881 }
2882
2883 void dumpAppTokensLocked() {
2884 for (int i=mAppTokens.size()-1; i>=0; i--) {
2885 Log.v(TAG, " #" + i + ": " + mAppTokens.get(i).token);
2886 }
2887 }
Romain Guy06882f82009-06-10 13:36:04 -07002888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002889 void dumpWindowsLocked() {
2890 for (int i=mWindows.size()-1; i>=0; i--) {
2891 Log.v(TAG, " #" + i + ": " + mWindows.get(i));
2892 }
2893 }
Romain Guy06882f82009-06-10 13:36:04 -07002894
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002895 private int findWindowOffsetLocked(int tokenPos) {
2896 final int NW = mWindows.size();
2897
2898 if (tokenPos >= mAppTokens.size()) {
2899 int i = NW;
2900 while (i > 0) {
2901 i--;
2902 WindowState win = (WindowState)mWindows.get(i);
2903 if (win.getAppToken() != null) {
2904 return i+1;
2905 }
2906 }
2907 }
2908
2909 while (tokenPos > 0) {
2910 // Find the first app token below the new position that has
2911 // a window displayed.
2912 final AppWindowToken wtoken = mAppTokens.get(tokenPos-1);
2913 if (DEBUG_REORDER) Log.v(TAG, "Looking for lower windows @ "
2914 + tokenPos + " -- " + wtoken.token);
2915 int i = wtoken.windows.size();
2916 while (i > 0) {
2917 i--;
2918 WindowState win = wtoken.windows.get(i);
2919 int j = win.mChildWindows.size();
2920 while (j > 0) {
2921 j--;
2922 WindowState cwin = (WindowState)win.mChildWindows.get(j);
2923 if (cwin.mSubLayer >= 0 ) {
2924 for (int pos=NW-1; pos>=0; pos--) {
2925 if (mWindows.get(pos) == cwin) {
2926 if (DEBUG_REORDER) Log.v(TAG,
2927 "Found child win @" + (pos+1));
2928 return pos+1;
2929 }
2930 }
2931 }
2932 }
2933 for (int pos=NW-1; pos>=0; pos--) {
2934 if (mWindows.get(pos) == win) {
2935 if (DEBUG_REORDER) Log.v(TAG, "Found win @" + (pos+1));
2936 return pos+1;
2937 }
2938 }
2939 }
2940 tokenPos--;
2941 }
2942
2943 return 0;
2944 }
2945
2946 private final int reAddWindowLocked(int index, WindowState win) {
2947 final int NCW = win.mChildWindows.size();
2948 boolean added = false;
2949 for (int j=0; j<NCW; j++) {
2950 WindowState cwin = (WindowState)win.mChildWindows.get(j);
2951 if (!added && cwin.mSubLayer >= 0) {
2952 mWindows.add(index, win);
2953 index++;
2954 added = true;
2955 }
2956 mWindows.add(index, cwin);
2957 index++;
2958 }
2959 if (!added) {
2960 mWindows.add(index, win);
2961 index++;
2962 }
2963 return index;
2964 }
Romain Guy06882f82009-06-10 13:36:04 -07002965
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002966 private final int reAddAppWindowsLocked(int index, WindowToken token) {
2967 final int NW = token.windows.size();
2968 for (int i=0; i<NW; i++) {
2969 index = reAddWindowLocked(index, token.windows.get(i));
2970 }
2971 return index;
2972 }
2973
2974 public void moveAppToken(int index, IBinder token) {
2975 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2976 "moveAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002977 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002978 }
2979
2980 synchronized(mWindowMap) {
2981 if (DEBUG_REORDER) Log.v(TAG, "Initial app tokens:");
2982 if (DEBUG_REORDER) dumpAppTokensLocked();
2983 final AppWindowToken wtoken = findAppWindowToken(token);
2984 if (wtoken == null || !mAppTokens.remove(wtoken)) {
2985 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
2986 + token + " (" + wtoken + ")");
2987 return;
2988 }
2989 mAppTokens.add(index, wtoken);
2990 if (DEBUG_REORDER) Log.v(TAG, "Moved " + token + " to " + index + ":");
2991 if (DEBUG_REORDER) dumpAppTokensLocked();
Romain Guy06882f82009-06-10 13:36:04 -07002992
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002993 final long origId = Binder.clearCallingIdentity();
2994 if (DEBUG_REORDER) Log.v(TAG, "Removing windows in " + token + ":");
2995 if (DEBUG_REORDER) dumpWindowsLocked();
2996 if (tmpRemoveAppWindowsLocked(wtoken)) {
2997 if (DEBUG_REORDER) Log.v(TAG, "Adding windows back in:");
2998 if (DEBUG_REORDER) dumpWindowsLocked();
2999 reAddAppWindowsLocked(findWindowOffsetLocked(index), wtoken);
3000 if (DEBUG_REORDER) Log.v(TAG, "Final window list:");
3001 if (DEBUG_REORDER) dumpWindowsLocked();
3002 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003003 mLayoutNeeded = true;
3004 performLayoutAndPlaceSurfacesLocked();
3005 }
3006 Binder.restoreCallingIdentity(origId);
3007 }
3008 }
3009
3010 private void removeAppTokensLocked(List<IBinder> tokens) {
3011 // XXX This should be done more efficiently!
3012 // (take advantage of the fact that both lists should be
3013 // ordered in the same way.)
3014 int N = tokens.size();
3015 for (int i=0; i<N; i++) {
3016 IBinder token = tokens.get(i);
3017 final AppWindowToken wtoken = findAppWindowToken(token);
3018 if (!mAppTokens.remove(wtoken)) {
3019 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3020 + token + " (" + wtoken + ")");
3021 i--;
3022 N--;
3023 }
3024 }
3025 }
3026
3027 private void moveAppWindowsLocked(List<IBinder> tokens, int tokenPos) {
3028 // First remove all of the windows from the list.
3029 final int N = tokens.size();
3030 int i;
3031 for (i=0; i<N; i++) {
3032 WindowToken token = mTokenMap.get(tokens.get(i));
3033 if (token != null) {
3034 tmpRemoveAppWindowsLocked(token);
3035 }
3036 }
3037
3038 // Where to start adding?
3039 int pos = findWindowOffsetLocked(tokenPos);
3040
3041 // And now add them back at the correct place.
3042 for (i=0; i<N; i++) {
3043 WindowToken token = mTokenMap.get(tokens.get(i));
3044 if (token != null) {
3045 pos = reAddAppWindowsLocked(pos, token);
3046 }
3047 }
3048
3049 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003050 mLayoutNeeded = true;
3051 performLayoutAndPlaceSurfacesLocked();
3052
3053 //dump();
3054 }
3055
3056 public void moveAppTokensToTop(List<IBinder> tokens) {
3057 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3058 "moveAppTokensToTop()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003059 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003060 }
3061
3062 final long origId = Binder.clearCallingIdentity();
3063 synchronized(mWindowMap) {
3064 removeAppTokensLocked(tokens);
3065 final int N = tokens.size();
3066 for (int i=0; i<N; i++) {
3067 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3068 if (wt != null) {
3069 mAppTokens.add(wt);
3070 }
3071 }
3072 moveAppWindowsLocked(tokens, mAppTokens.size());
3073 }
3074 Binder.restoreCallingIdentity(origId);
3075 }
3076
3077 public void moveAppTokensToBottom(List<IBinder> tokens) {
3078 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3079 "moveAppTokensToBottom()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003080 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003081 }
3082
3083 final long origId = Binder.clearCallingIdentity();
3084 synchronized(mWindowMap) {
3085 removeAppTokensLocked(tokens);
3086 final int N = tokens.size();
3087 int pos = 0;
3088 for (int i=0; i<N; i++) {
3089 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3090 if (wt != null) {
3091 mAppTokens.add(pos, wt);
3092 pos++;
3093 }
3094 }
3095 moveAppWindowsLocked(tokens, 0);
3096 }
3097 Binder.restoreCallingIdentity(origId);
3098 }
3099
3100 // -------------------------------------------------------------
3101 // Misc IWindowSession methods
3102 // -------------------------------------------------------------
Romain Guy06882f82009-06-10 13:36:04 -07003103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003104 public void disableKeyguard(IBinder token, String tag) {
3105 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3106 != PackageManager.PERMISSION_GRANTED) {
3107 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3108 }
3109 mKeyguardDisabled.acquire(token, tag);
3110 }
3111
3112 public void reenableKeyguard(IBinder token) {
3113 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3114 != PackageManager.PERMISSION_GRANTED) {
3115 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3116 }
3117 synchronized (mKeyguardDisabled) {
3118 mKeyguardDisabled.release(token);
3119
3120 if (!mKeyguardDisabled.isAcquired()) {
3121 // if we are the last one to reenable the keyguard wait until
3122 // we have actaully finished reenabling until returning
3123 mWaitingUntilKeyguardReenabled = true;
3124 while (mWaitingUntilKeyguardReenabled) {
3125 try {
3126 mKeyguardDisabled.wait();
3127 } catch (InterruptedException e) {
3128 Thread.currentThread().interrupt();
3129 }
3130 }
3131 }
3132 }
3133 }
3134
3135 /**
3136 * @see android.app.KeyguardManager#exitKeyguardSecurely
3137 */
3138 public void exitKeyguardSecurely(final IOnKeyguardExitResult callback) {
3139 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3140 != PackageManager.PERMISSION_GRANTED) {
3141 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3142 }
3143 mPolicy.exitKeyguardSecurely(new WindowManagerPolicy.OnKeyguardExitResult() {
3144 public void onKeyguardExitResult(boolean success) {
3145 try {
3146 callback.onKeyguardExitResult(success);
3147 } catch (RemoteException e) {
3148 // Client has died, we don't care.
3149 }
3150 }
3151 });
3152 }
3153
3154 public boolean inKeyguardRestrictedInputMode() {
3155 return mPolicy.inKeyguardRestrictedKeyInputMode();
3156 }
Romain Guy06882f82009-06-10 13:36:04 -07003157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003158 static float fixScale(float scale) {
3159 if (scale < 0) scale = 0;
3160 else if (scale > 20) scale = 20;
3161 return Math.abs(scale);
3162 }
Romain Guy06882f82009-06-10 13:36:04 -07003163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003164 public void setAnimationScale(int which, float scale) {
3165 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
3166 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003167 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003168 }
3169
3170 if (scale < 0) scale = 0;
3171 else if (scale > 20) scale = 20;
3172 scale = Math.abs(scale);
3173 switch (which) {
3174 case 0: mWindowAnimationScale = fixScale(scale); break;
3175 case 1: mTransitionAnimationScale = fixScale(scale); break;
3176 }
Romain Guy06882f82009-06-10 13:36:04 -07003177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003178 // Persist setting
3179 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
3180 }
Romain Guy06882f82009-06-10 13:36:04 -07003181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003182 public void setAnimationScales(float[] scales) {
3183 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
3184 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003185 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003186 }
3187
3188 if (scales != null) {
3189 if (scales.length >= 1) {
3190 mWindowAnimationScale = fixScale(scales[0]);
3191 }
3192 if (scales.length >= 2) {
3193 mTransitionAnimationScale = fixScale(scales[1]);
3194 }
3195 }
Romain Guy06882f82009-06-10 13:36:04 -07003196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003197 // Persist setting
3198 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
3199 }
Romain Guy06882f82009-06-10 13:36:04 -07003200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003201 public float getAnimationScale(int which) {
3202 switch (which) {
3203 case 0: return mWindowAnimationScale;
3204 case 1: return mTransitionAnimationScale;
3205 }
3206 return 0;
3207 }
Romain Guy06882f82009-06-10 13:36:04 -07003208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003209 public float[] getAnimationScales() {
3210 return new float[] { mWindowAnimationScale, mTransitionAnimationScale };
3211 }
Romain Guy06882f82009-06-10 13:36:04 -07003212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003213 public int getSwitchState(int sw) {
3214 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3215 "getSwitchState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003216 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003217 }
3218 return KeyInputQueue.getSwitchState(sw);
3219 }
Romain Guy06882f82009-06-10 13:36:04 -07003220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003221 public int getSwitchStateForDevice(int devid, int sw) {
3222 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3223 "getSwitchStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003224 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003225 }
3226 return KeyInputQueue.getSwitchState(devid, sw);
3227 }
Romain Guy06882f82009-06-10 13:36:04 -07003228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003229 public int getScancodeState(int sw) {
3230 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3231 "getScancodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003232 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003233 }
3234 return KeyInputQueue.getScancodeState(sw);
3235 }
Romain Guy06882f82009-06-10 13:36:04 -07003236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003237 public int getScancodeStateForDevice(int devid, int sw) {
3238 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3239 "getScancodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003240 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003241 }
3242 return KeyInputQueue.getScancodeState(devid, sw);
3243 }
Romain Guy06882f82009-06-10 13:36:04 -07003244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003245 public int getKeycodeState(int sw) {
3246 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3247 "getKeycodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003248 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003249 }
3250 return KeyInputQueue.getKeycodeState(sw);
3251 }
Romain Guy06882f82009-06-10 13:36:04 -07003252
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003253 public int getKeycodeStateForDevice(int devid, int sw) {
3254 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3255 "getKeycodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003256 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003257 }
3258 return KeyInputQueue.getKeycodeState(devid, sw);
3259 }
Romain Guy06882f82009-06-10 13:36:04 -07003260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003261 public boolean hasKeys(int[] keycodes, boolean[] keyExists) {
3262 return KeyInputQueue.hasKeys(keycodes, keyExists);
3263 }
Romain Guy06882f82009-06-10 13:36:04 -07003264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003265 public void enableScreenAfterBoot() {
3266 synchronized(mWindowMap) {
3267 if (mSystemBooted) {
3268 return;
3269 }
3270 mSystemBooted = true;
3271 }
Romain Guy06882f82009-06-10 13:36:04 -07003272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003273 performEnableScreen();
3274 }
Romain Guy06882f82009-06-10 13:36:04 -07003275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003276 public void enableScreenIfNeededLocked() {
3277 if (mDisplayEnabled) {
3278 return;
3279 }
3280 if (!mSystemBooted) {
3281 return;
3282 }
3283 mH.sendMessage(mH.obtainMessage(H.ENABLE_SCREEN));
3284 }
Romain Guy06882f82009-06-10 13:36:04 -07003285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003286 public void performEnableScreen() {
3287 synchronized(mWindowMap) {
3288 if (mDisplayEnabled) {
3289 return;
3290 }
3291 if (!mSystemBooted) {
3292 return;
3293 }
Romain Guy06882f82009-06-10 13:36:04 -07003294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003295 // Don't enable the screen until all existing windows
3296 // have been drawn.
3297 final int N = mWindows.size();
3298 for (int i=0; i<N; i++) {
3299 WindowState w = (WindowState)mWindows.get(i);
3300 if (w.isVisibleLw() && !w.isDisplayedLw()) {
3301 return;
3302 }
3303 }
Romain Guy06882f82009-06-10 13:36:04 -07003304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003305 mDisplayEnabled = true;
3306 if (false) {
3307 Log.i(TAG, "ENABLING SCREEN!");
3308 StringWriter sw = new StringWriter();
3309 PrintWriter pw = new PrintWriter(sw);
3310 this.dump(null, pw, null);
3311 Log.i(TAG, sw.toString());
3312 }
3313 try {
3314 IBinder surfaceFlinger = ServiceManager.getService("SurfaceFlinger");
3315 if (surfaceFlinger != null) {
3316 //Log.i(TAG, "******* TELLING SURFACE FLINGER WE ARE BOOTED!");
3317 Parcel data = Parcel.obtain();
3318 data.writeInterfaceToken("android.ui.ISurfaceComposer");
3319 surfaceFlinger.transact(IBinder.FIRST_CALL_TRANSACTION,
3320 data, null, 0);
3321 data.recycle();
3322 }
3323 } catch (RemoteException ex) {
3324 Log.e(TAG, "Boot completed: SurfaceFlinger is dead!");
3325 }
3326 }
Romain Guy06882f82009-06-10 13:36:04 -07003327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003328 mPolicy.enableScreenAfterBoot();
Romain Guy06882f82009-06-10 13:36:04 -07003329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003330 // Make sure the last requested orientation has been applied.
Dianne Hackborn321ae682009-03-27 16:16:03 -07003331 setRotationUnchecked(WindowManagerPolicy.USE_LAST_ROTATION, false,
3332 mLastRotationFlags | Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003333 }
Romain Guy06882f82009-06-10 13:36:04 -07003334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003335 public void setInTouchMode(boolean mode) {
3336 synchronized(mWindowMap) {
3337 mInTouchMode = mode;
3338 }
3339 }
3340
Romain Guy06882f82009-06-10 13:36:04 -07003341 public void setRotation(int rotation,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003342 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003343 if (!checkCallingPermission(android.Manifest.permission.SET_ORIENTATION,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003344 "setRotation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003345 throw new SecurityException("Requires SET_ORIENTATION permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003346 }
3347
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003348 setRotationUnchecked(rotation, alwaysSendConfiguration, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003349 }
Romain Guy06882f82009-06-10 13:36:04 -07003350
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003351 public void setRotationUnchecked(int rotation,
3352 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003353 if(DEBUG_ORIENTATION) Log.v(TAG,
3354 "alwaysSendConfiguration set to "+alwaysSendConfiguration);
Romain Guy06882f82009-06-10 13:36:04 -07003355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003356 long origId = Binder.clearCallingIdentity();
3357 boolean changed;
3358 synchronized(mWindowMap) {
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003359 changed = setRotationUncheckedLocked(rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003360 }
Romain Guy06882f82009-06-10 13:36:04 -07003361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003362 if (changed) {
3363 sendNewConfiguration();
3364 synchronized(mWindowMap) {
3365 mLayoutNeeded = true;
3366 performLayoutAndPlaceSurfacesLocked();
3367 }
3368 } else if (alwaysSendConfiguration) {
3369 //update configuration ignoring orientation change
3370 sendNewConfiguration();
3371 }
Romain Guy06882f82009-06-10 13:36:04 -07003372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003373 Binder.restoreCallingIdentity(origId);
3374 }
Romain Guy06882f82009-06-10 13:36:04 -07003375
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003376 public boolean setRotationUncheckedLocked(int rotation, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003377 boolean changed;
3378 if (rotation == WindowManagerPolicy.USE_LAST_ROTATION) {
3379 rotation = mRequestedRotation;
3380 } else {
3381 mRequestedRotation = rotation;
Dianne Hackborn321ae682009-03-27 16:16:03 -07003382 mLastRotationFlags = animFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003383 }
3384 if (DEBUG_ORIENTATION) Log.v(TAG, "Overwriting rotation value from " + rotation);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003385 rotation = mPolicy.rotationForOrientationLw(mForcedAppOrientation,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003386 mRotation, mDisplayEnabled);
3387 if (DEBUG_ORIENTATION) Log.v(TAG, "new rotation is set to " + rotation);
3388 changed = mDisplayEnabled && mRotation != rotation;
Romain Guy06882f82009-06-10 13:36:04 -07003389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003390 if (changed) {
Romain Guy06882f82009-06-10 13:36:04 -07003391 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003392 "Rotation changed to " + rotation
3393 + " from " + mRotation
3394 + " (forceApp=" + mForcedAppOrientation
3395 + ", req=" + mRequestedRotation + ")");
3396 mRotation = rotation;
3397 mWindowsFreezingScreen = true;
3398 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
3399 mH.sendMessageDelayed(mH.obtainMessage(H.WINDOW_FREEZE_TIMEOUT),
3400 2000);
3401 startFreezingDisplayLocked();
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003402 Log.i(TAG, "Setting rotation to " + rotation + ", animFlags=" + animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003403 mQueue.setOrientation(rotation);
3404 if (mDisplayEnabled) {
Dianne Hackborn321ae682009-03-27 16:16:03 -07003405 Surface.setOrientation(0, rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003406 }
3407 for (int i=mWindows.size()-1; i>=0; i--) {
3408 WindowState w = (WindowState)mWindows.get(i);
3409 if (w.mSurface != null) {
3410 w.mOrientationChanging = true;
3411 }
3412 }
3413 for (int i=mRotationWatchers.size()-1; i>=0; i--) {
3414 try {
3415 mRotationWatchers.get(i).onRotationChanged(rotation);
3416 } catch (RemoteException e) {
3417 }
3418 }
3419 } //end if changed
Romain Guy06882f82009-06-10 13:36:04 -07003420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003421 return changed;
3422 }
Romain Guy06882f82009-06-10 13:36:04 -07003423
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003424 public int getRotation() {
3425 return mRotation;
3426 }
3427
3428 public int watchRotation(IRotationWatcher watcher) {
3429 final IBinder watcherBinder = watcher.asBinder();
3430 IBinder.DeathRecipient dr = new IBinder.DeathRecipient() {
3431 public void binderDied() {
3432 synchronized (mWindowMap) {
3433 for (int i=0; i<mRotationWatchers.size(); i++) {
3434 if (watcherBinder == mRotationWatchers.get(i).asBinder()) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07003435 IRotationWatcher removed = mRotationWatchers.remove(i);
3436 if (removed != null) {
3437 removed.asBinder().unlinkToDeath(this, 0);
3438 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003439 i--;
3440 }
3441 }
3442 }
3443 }
3444 };
Romain Guy06882f82009-06-10 13:36:04 -07003445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003446 synchronized (mWindowMap) {
3447 try {
3448 watcher.asBinder().linkToDeath(dr, 0);
3449 mRotationWatchers.add(watcher);
3450 } catch (RemoteException e) {
3451 // Client died, no cleanup needed.
3452 }
Romain Guy06882f82009-06-10 13:36:04 -07003453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003454 return mRotation;
3455 }
3456 }
3457
3458 /**
3459 * Starts the view server on the specified port.
3460 *
3461 * @param port The port to listener to.
3462 *
3463 * @return True if the server was successfully started, false otherwise.
3464 *
3465 * @see com.android.server.ViewServer
3466 * @see com.android.server.ViewServer#VIEW_SERVER_DEFAULT_PORT
3467 */
3468 public boolean startViewServer(int port) {
Romain Guy06882f82009-06-10 13:36:04 -07003469 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003470 return false;
3471 }
3472
3473 if (!checkCallingPermission(Manifest.permission.DUMP, "startViewServer")) {
3474 return false;
3475 }
3476
3477 if (port < 1024) {
3478 return false;
3479 }
3480
3481 if (mViewServer != null) {
3482 if (!mViewServer.isRunning()) {
3483 try {
3484 return mViewServer.start();
3485 } catch (IOException e) {
Romain Guy06882f82009-06-10 13:36:04 -07003486 Log.w(TAG, "View server did not start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003487 }
3488 }
3489 return false;
3490 }
3491
3492 try {
3493 mViewServer = new ViewServer(this, port);
3494 return mViewServer.start();
3495 } catch (IOException e) {
3496 Log.w(TAG, "View server did not start");
3497 }
3498 return false;
3499 }
3500
Romain Guy06882f82009-06-10 13:36:04 -07003501 private boolean isSystemSecure() {
3502 return "1".equals(SystemProperties.get(SYSTEM_SECURE, "1")) &&
3503 "0".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
3504 }
3505
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003506 /**
3507 * Stops the view server if it exists.
3508 *
3509 * @return True if the server stopped, false if it wasn't started or
3510 * couldn't be stopped.
3511 *
3512 * @see com.android.server.ViewServer
3513 */
3514 public boolean stopViewServer() {
Romain Guy06882f82009-06-10 13:36:04 -07003515 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003516 return false;
3517 }
3518
3519 if (!checkCallingPermission(Manifest.permission.DUMP, "stopViewServer")) {
3520 return false;
3521 }
3522
3523 if (mViewServer != null) {
3524 return mViewServer.stop();
3525 }
3526 return false;
3527 }
3528
3529 /**
3530 * Indicates whether the view server is running.
3531 *
3532 * @return True if the server is running, false otherwise.
3533 *
3534 * @see com.android.server.ViewServer
3535 */
3536 public boolean isViewServerRunning() {
Romain Guy06882f82009-06-10 13:36:04 -07003537 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003538 return false;
3539 }
3540
3541 if (!checkCallingPermission(Manifest.permission.DUMP, "isViewServerRunning")) {
3542 return false;
3543 }
3544
3545 return mViewServer != null && mViewServer.isRunning();
3546 }
3547
3548 /**
3549 * Lists all availble windows in the system. The listing is written in the
3550 * specified Socket's output stream with the following syntax:
3551 * windowHashCodeInHexadecimal windowName
3552 * Each line of the ouput represents a different window.
3553 *
3554 * @param client The remote client to send the listing to.
3555 * @return False if an error occured, true otherwise.
3556 */
3557 boolean viewServerListWindows(Socket client) {
Romain Guy06882f82009-06-10 13:36:04 -07003558 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003559 return false;
3560 }
3561
3562 boolean result = true;
3563
3564 Object[] windows;
3565 synchronized (mWindowMap) {
3566 windows = new Object[mWindows.size()];
3567 //noinspection unchecked
3568 windows = mWindows.toArray(windows);
3569 }
3570
3571 BufferedWriter out = null;
3572
3573 // Any uncaught exception will crash the system process
3574 try {
3575 OutputStream clientStream = client.getOutputStream();
3576 out = new BufferedWriter(new OutputStreamWriter(clientStream), 8 * 1024);
3577
3578 final int count = windows.length;
3579 for (int i = 0; i < count; i++) {
3580 final WindowState w = (WindowState) windows[i];
3581 out.write(Integer.toHexString(System.identityHashCode(w)));
3582 out.write(' ');
3583 out.append(w.mAttrs.getTitle());
3584 out.write('\n');
3585 }
3586
3587 out.write("DONE.\n");
3588 out.flush();
3589 } catch (Exception e) {
3590 result = false;
3591 } finally {
3592 if (out != null) {
3593 try {
3594 out.close();
3595 } catch (IOException e) {
3596 result = false;
3597 }
3598 }
3599 }
3600
3601 return result;
3602 }
3603
3604 /**
3605 * Sends a command to a target window. The result of the command, if any, will be
3606 * written in the output stream of the specified socket.
3607 *
3608 * The parameters must follow this syntax:
3609 * windowHashcode extra
3610 *
3611 * Where XX is the length in characeters of the windowTitle.
3612 *
3613 * The first parameter is the target window. The window with the specified hashcode
3614 * will be the target. If no target can be found, nothing happens. The extra parameters
3615 * will be delivered to the target window and as parameters to the command itself.
3616 *
3617 * @param client The remote client to sent the result, if any, to.
3618 * @param command The command to execute.
3619 * @param parameters The command parameters.
3620 *
3621 * @return True if the command was successfully delivered, false otherwise. This does
3622 * not indicate whether the command itself was successful.
3623 */
3624 boolean viewServerWindowCommand(Socket client, String command, String parameters) {
Romain Guy06882f82009-06-10 13:36:04 -07003625 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003626 return false;
3627 }
3628
3629 boolean success = true;
3630 Parcel data = null;
3631 Parcel reply = null;
3632
3633 // Any uncaught exception will crash the system process
3634 try {
3635 // Find the hashcode of the window
3636 int index = parameters.indexOf(' ');
3637 if (index == -1) {
3638 index = parameters.length();
3639 }
3640 final String code = parameters.substring(0, index);
3641 int hashCode = "ffffffff".equals(code) ? -1 : Integer.parseInt(code, 16);
3642
3643 // Extract the command's parameter after the window description
3644 if (index < parameters.length()) {
3645 parameters = parameters.substring(index + 1);
3646 } else {
3647 parameters = "";
3648 }
3649
3650 final WindowManagerService.WindowState window = findWindow(hashCode);
3651 if (window == null) {
3652 return false;
3653 }
3654
3655 data = Parcel.obtain();
3656 data.writeInterfaceToken("android.view.IWindow");
3657 data.writeString(command);
3658 data.writeString(parameters);
3659 data.writeInt(1);
3660 ParcelFileDescriptor.fromSocket(client).writeToParcel(data, 0);
3661
3662 reply = Parcel.obtain();
3663
3664 final IBinder binder = window.mClient.asBinder();
3665 // TODO: GET THE TRANSACTION CODE IN A SAFER MANNER
3666 binder.transact(IBinder.FIRST_CALL_TRANSACTION, data, reply, 0);
3667
3668 reply.readException();
3669
3670 } catch (Exception e) {
3671 Log.w(TAG, "Could not send command " + command + " with parameters " + parameters, e);
3672 success = false;
3673 } finally {
3674 if (data != null) {
3675 data.recycle();
3676 }
3677 if (reply != null) {
3678 reply.recycle();
3679 }
3680 }
3681
3682 return success;
3683 }
3684
3685 private WindowState findWindow(int hashCode) {
3686 if (hashCode == -1) {
3687 return getFocusedWindow();
3688 }
3689
3690 synchronized (mWindowMap) {
3691 final ArrayList windows = mWindows;
3692 final int count = windows.size();
3693
3694 for (int i = 0; i < count; i++) {
3695 WindowState w = (WindowState) windows.get(i);
3696 if (System.identityHashCode(w) == hashCode) {
3697 return w;
3698 }
3699 }
3700 }
3701
3702 return null;
3703 }
3704
3705 /*
3706 * Instruct the Activity Manager to fetch the current configuration and broadcast
3707 * that to config-changed listeners if appropriate.
3708 */
3709 void sendNewConfiguration() {
3710 try {
3711 mActivityManager.updateConfiguration(null);
3712 } catch (RemoteException e) {
3713 }
3714 }
Romain Guy06882f82009-06-10 13:36:04 -07003715
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003716 public Configuration computeNewConfiguration() {
3717 synchronized (mWindowMap) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07003718 return computeNewConfigurationLocked();
3719 }
3720 }
Romain Guy06882f82009-06-10 13:36:04 -07003721
Dianne Hackbornc485a602009-03-24 22:39:49 -07003722 Configuration computeNewConfigurationLocked() {
3723 Configuration config = new Configuration();
3724 if (!computeNewConfigurationLocked(config)) {
3725 return null;
3726 }
3727 Log.i(TAG, "Config changed: " + config);
3728 long now = SystemClock.uptimeMillis();
3729 //Log.i(TAG, "Config changing, gc pending: " + mFreezeGcPending + ", now " + now);
3730 if (mFreezeGcPending != 0) {
3731 if (now > (mFreezeGcPending+1000)) {
3732 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
3733 mH.removeMessages(H.FORCE_GC);
3734 Runtime.getRuntime().gc();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003735 mFreezeGcPending = now;
3736 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07003737 } else {
3738 mFreezeGcPending = now;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003739 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07003740 return config;
3741 }
Romain Guy06882f82009-06-10 13:36:04 -07003742
Dianne Hackbornc485a602009-03-24 22:39:49 -07003743 boolean computeNewConfigurationLocked(Configuration config) {
3744 if (mDisplay == null) {
3745 return false;
3746 }
3747 mQueue.getInputConfiguration(config);
3748 final int dw = mDisplay.getWidth();
3749 final int dh = mDisplay.getHeight();
3750 int orientation = Configuration.ORIENTATION_SQUARE;
3751 if (dw < dh) {
3752 orientation = Configuration.ORIENTATION_PORTRAIT;
3753 } else if (dw > dh) {
3754 orientation = Configuration.ORIENTATION_LANDSCAPE;
3755 }
3756 config.orientation = orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -07003757
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07003758 DisplayMetrics dm = new DisplayMetrics();
3759 mDisplay.getMetrics(dm);
3760 CompatibilityInfo.updateCompatibleScreenFrame(dm, orientation, mCompatibleScreenFrame);
3761
Dianne Hackborn723738c2009-06-25 19:48:04 -07003762 if (screenLayout == Configuration.SCREENLAYOUT_UNDEFINED) {
3763 // Note we only do this once because at this point we don't
3764 // expect the screen to change in this way at runtime, and want
3765 // to avoid all of this computation for every config change.
Dianne Hackborn723738c2009-06-25 19:48:04 -07003766 int longSize = dw;
3767 int shortSize = dh;
3768 if (longSize < shortSize) {
3769 int tmp = longSize;
3770 longSize = shortSize;
3771 shortSize = tmp;
3772 }
3773 longSize = (int)(longSize/dm.density);
3774 shortSize = (int)(shortSize/dm.density);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07003775
Dianne Hackborn723738c2009-06-25 19:48:04 -07003776 // These semi-magic numbers define our compatibility modes for
3777 // applications with different screens. Don't change unless you
3778 // make sure to test lots and lots of apps!
3779 if (longSize < 470) {
3780 // This is shorter than an HVGA normal density screen (which
3781 // is 480 pixels on its long side).
3782 screenLayout = Configuration.SCREENLAYOUT_SMALL;
3783 } else if (longSize > 490 && shortSize > 330) {
3784 // This is larger than an HVGA normal density screen (which
3785 // is 480x320 pixels).
3786 screenLayout = Configuration.SCREENLAYOUT_LARGE;
3787 } else {
3788 screenLayout = Configuration.SCREENLAYOUT_NORMAL;
3789 }
3790 }
3791 config.screenLayout = screenLayout;
3792
Dianne Hackbornc485a602009-03-24 22:39:49 -07003793 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
3794 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
3795 mPolicy.adjustConfigurationLw(config);
3796 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003797 }
Romain Guy06882f82009-06-10 13:36:04 -07003798
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003799 // -------------------------------------------------------------
3800 // Input Events and Focus Management
3801 // -------------------------------------------------------------
3802
3803 private final void wakeupIfNeeded(WindowState targetWin, int eventType) {
Michael Chane96440f2009-05-06 10:27:36 -07003804 long curTime = SystemClock.uptimeMillis();
3805
3806 if (eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT) {
3807 if (mLastTouchEventType == eventType &&
3808 (curTime - mLastUserActivityCallTime) < MIN_TIME_BETWEEN_USERACTIVITIES) {
3809 return;
3810 }
3811 mLastUserActivityCallTime = curTime;
3812 mLastTouchEventType = eventType;
3813 }
3814
3815 if (targetWin == null
3816 || targetWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
3817 mPowerManager.userActivity(curTime, false, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003818 }
3819 }
3820
3821 // tells if it's a cheek event or not -- this function is stateful
3822 private static final int EVENT_NONE = 0;
3823 private static final int EVENT_UNKNOWN = 0;
3824 private static final int EVENT_CHEEK = 0;
3825 private static final int EVENT_IGNORE_DURATION = 300; // ms
3826 private static final float CHEEK_THRESHOLD = 0.6f;
3827 private int mEventState = EVENT_NONE;
3828 private float mEventSize;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07003829
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003830 private int eventType(MotionEvent ev) {
3831 float size = ev.getSize();
3832 switch (ev.getAction()) {
3833 case MotionEvent.ACTION_DOWN:
3834 mEventSize = size;
3835 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_EVENT;
3836 case MotionEvent.ACTION_UP:
3837 if (size > mEventSize) mEventSize = size;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07003838 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003839 case MotionEvent.ACTION_MOVE:
3840 final int N = ev.getHistorySize();
3841 if (size > mEventSize) mEventSize = size;
3842 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
3843 for (int i=0; i<N; i++) {
3844 size = ev.getHistoricalSize(i);
3845 if (size > mEventSize) mEventSize = size;
3846 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
3847 }
3848 if (ev.getEventTime() < ev.getDownTime() + EVENT_IGNORE_DURATION) {
3849 return TOUCH_EVENT;
3850 } else {
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07003851 return LONG_TOUCH_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003852 }
3853 default:
3854 // not good
3855 return OTHER_EVENT;
3856 }
3857 }
3858
3859 /**
3860 * @return Returns true if event was dispatched, false if it was dropped for any reason
3861 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07003862 private int dispatchPointer(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003863 if (DEBUG_INPUT || WindowManagerPolicy.WATCH_POINTER) Log.v(TAG,
3864 "dispatchPointer " + ev);
3865
3866 Object targetObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07003867 ev, true, false, pid, uid);
Romain Guy06882f82009-06-10 13:36:04 -07003868
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003869 int action = ev.getAction();
Romain Guy06882f82009-06-10 13:36:04 -07003870
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003871 if (action == MotionEvent.ACTION_UP) {
3872 // let go of our target
3873 mKeyWaiter.mMotionTarget = null;
3874 mPowerManager.logPointerUpEvent();
3875 } else if (action == MotionEvent.ACTION_DOWN) {
3876 mPowerManager.logPointerDownEvent();
3877 }
3878
3879 if (targetObj == null) {
3880 // In this case we are either dropping the event, or have received
3881 // a move or up without a down. It is common to receive move
3882 // events in such a way, since this means the user is moving the
3883 // pointer without actually pressing down. All other cases should
3884 // be atypical, so let's log them.
Michael Chane96440f2009-05-06 10:27:36 -07003885 if (action != MotionEvent.ACTION_MOVE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003886 Log.w(TAG, "No window to dispatch pointer action " + ev.getAction());
3887 }
3888 if (qev != null) {
3889 mQueue.recycleEvent(qev);
3890 }
3891 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07003892 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003893 }
3894 if (targetObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
3895 if (qev != null) {
3896 mQueue.recycleEvent(qev);
3897 }
3898 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07003899 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003900 }
Romain Guy06882f82009-06-10 13:36:04 -07003901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003902 WindowState target = (WindowState)targetObj;
Romain Guy06882f82009-06-10 13:36:04 -07003903
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003904 final long eventTime = ev.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07003905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003906 //Log.i(TAG, "Sending " + ev + " to " + target);
3907
3908 if (uid != 0 && uid != target.mSession.mUid) {
3909 if (mContext.checkPermission(
3910 android.Manifest.permission.INJECT_EVENTS, pid, uid)
3911 != PackageManager.PERMISSION_GRANTED) {
3912 Log.w(TAG, "Permission denied: injecting pointer event from pid "
3913 + pid + " uid " + uid + " to window " + target
3914 + " owned by uid " + target.mSession.mUid);
3915 if (qev != null) {
3916 mQueue.recycleEvent(qev);
3917 }
3918 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07003919 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003920 }
3921 }
Romain Guy06882f82009-06-10 13:36:04 -07003922
3923 if ((target.mAttrs.flags &
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003924 WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES) != 0) {
3925 //target wants to ignore fat touch events
3926 boolean cheekPress = mPolicy.isCheekPressedAgainstScreen(ev);
3927 //explicit flag to return without processing event further
3928 boolean returnFlag = false;
3929 if((action == MotionEvent.ACTION_DOWN)) {
3930 mFatTouch = false;
3931 if(cheekPress) {
3932 mFatTouch = true;
3933 returnFlag = true;
3934 }
3935 } else {
3936 if(action == MotionEvent.ACTION_UP) {
3937 if(mFatTouch) {
3938 //earlier even was invalid doesnt matter if current up is cheekpress or not
3939 mFatTouch = false;
3940 returnFlag = true;
3941 } else if(cheekPress) {
3942 //cancel the earlier event
3943 ev.setAction(MotionEvent.ACTION_CANCEL);
3944 action = MotionEvent.ACTION_CANCEL;
3945 }
3946 } else if(action == MotionEvent.ACTION_MOVE) {
3947 if(mFatTouch) {
3948 //two cases here
3949 //an invalid down followed by 0 or moves(valid or invalid)
Romain Guy06882f82009-06-10 13:36:04 -07003950 //a valid down, invalid move, more moves. want to ignore till up
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003951 returnFlag = true;
3952 } else if(cheekPress) {
3953 //valid down followed by invalid moves
3954 //an invalid move have to cancel earlier action
3955 ev.setAction(MotionEvent.ACTION_CANCEL);
3956 action = MotionEvent.ACTION_CANCEL;
3957 if (DEBUG_INPUT) Log.v(TAG, "Sending cancel for invalid ACTION_MOVE");
3958 //note that the subsequent invalid moves will not get here
3959 mFatTouch = true;
3960 }
3961 }
3962 } //else if action
3963 if(returnFlag) {
3964 //recycle que, ev
3965 if (qev != null) {
3966 mQueue.recycleEvent(qev);
3967 }
3968 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07003969 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003970 }
3971 } //end if target
Michael Chane96440f2009-05-06 10:27:36 -07003972
3973 // TODO remove once we settle on a value or make it app specific
3974 if (action == MotionEvent.ACTION_DOWN) {
3975 int max_events_per_sec = 35;
3976 try {
3977 max_events_per_sec = Integer.parseInt(SystemProperties
3978 .get("windowsmgr.max_events_per_sec"));
3979 if (max_events_per_sec < 1) {
3980 max_events_per_sec = 35;
3981 }
3982 } catch (NumberFormatException e) {
3983 }
3984 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
3985 }
3986
3987 /*
3988 * Throttle events to minimize CPU usage when there's a flood of events
3989 * e.g. constant contact with the screen
3990 */
3991 if (action == MotionEvent.ACTION_MOVE) {
3992 long nextEventTime = mLastTouchEventTime + mMinWaitTimeBetweenTouchEvents;
3993 long now = SystemClock.uptimeMillis();
3994 if (now < nextEventTime) {
3995 try {
3996 Thread.sleep(nextEventTime - now);
3997 } catch (InterruptedException e) {
3998 }
3999 mLastTouchEventTime = nextEventTime;
4000 } else {
4001 mLastTouchEventTime = now;
4002 }
4003 }
4004
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004005 synchronized(mWindowMap) {
4006 if (qev != null && action == MotionEvent.ACTION_MOVE) {
4007 mKeyWaiter.bindTargetWindowLocked(target,
4008 KeyWaiter.RETURN_PENDING_POINTER, qev);
4009 ev = null;
4010 } else {
4011 if (action == MotionEvent.ACTION_DOWN) {
4012 WindowState out = mKeyWaiter.mOutsideTouchTargets;
4013 if (out != null) {
4014 MotionEvent oev = MotionEvent.obtain(ev);
4015 oev.setAction(MotionEvent.ACTION_OUTSIDE);
4016 do {
4017 final Rect frame = out.mFrame;
4018 oev.offsetLocation(-(float)frame.left, -(float)frame.top);
4019 try {
4020 out.mClient.dispatchPointer(oev, eventTime);
4021 } catch (android.os.RemoteException e) {
4022 Log.i(TAG, "WINDOW DIED during outside motion dispatch: " + out);
4023 }
4024 oev.offsetLocation((float)frame.left, (float)frame.top);
4025 out = out.mNextOutsideTouch;
4026 } while (out != null);
4027 mKeyWaiter.mOutsideTouchTargets = null;
4028 }
4029 }
4030 final Rect frame = target.mFrame;
4031 ev.offsetLocation(-(float)frame.left, -(float)frame.top);
4032 mKeyWaiter.bindTargetWindowLocked(target);
4033 }
4034 }
Romain Guy06882f82009-06-10 13:36:04 -07004035
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004036 // finally offset the event to the target's coordinate system and
4037 // dispatch the event.
4038 try {
4039 if (DEBUG_INPUT || DEBUG_FOCUS || WindowManagerPolicy.WATCH_POINTER) {
4040 Log.v(TAG, "Delivering pointer " + qev + " to " + target);
4041 }
4042 target.mClient.dispatchPointer(ev, eventTime);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004043 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004044 } catch (android.os.RemoteException e) {
4045 Log.i(TAG, "WINDOW DIED during motion dispatch: " + target);
4046 mKeyWaiter.mMotionTarget = null;
4047 try {
4048 removeWindow(target.mSession, target.mClient);
4049 } catch (java.util.NoSuchElementException ex) {
4050 // This will happen if the window has already been
4051 // removed.
4052 }
4053 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07004054 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004055 }
Romain Guy06882f82009-06-10 13:36:04 -07004056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004057 /**
4058 * @return Returns true if event was dispatched, false if it was dropped for any reason
4059 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004060 private int dispatchTrackball(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004061 if (DEBUG_INPUT) Log.v(
4062 TAG, "dispatchTrackball [" + ev.getAction() +"] <" + ev.getX() + ", " + ev.getY() + ">");
Romain Guy06882f82009-06-10 13:36:04 -07004063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004064 Object focusObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004065 ev, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004066 if (focusObj == null) {
4067 Log.w(TAG, "No focus window, dropping trackball: " + ev);
4068 if (qev != null) {
4069 mQueue.recycleEvent(qev);
4070 }
4071 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004072 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004073 }
4074 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
4075 if (qev != null) {
4076 mQueue.recycleEvent(qev);
4077 }
4078 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004079 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004080 }
Romain Guy06882f82009-06-10 13:36:04 -07004081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004082 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07004083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004084 if (uid != 0 && uid != focus.mSession.mUid) {
4085 if (mContext.checkPermission(
4086 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4087 != PackageManager.PERMISSION_GRANTED) {
4088 Log.w(TAG, "Permission denied: injecting key event from pid "
4089 + pid + " uid " + uid + " to window " + focus
4090 + " owned by uid " + focus.mSession.mUid);
4091 if (qev != null) {
4092 mQueue.recycleEvent(qev);
4093 }
4094 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004095 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004096 }
4097 }
Romain Guy06882f82009-06-10 13:36:04 -07004098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004099 final long eventTime = ev.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07004100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004101 synchronized(mWindowMap) {
4102 if (qev != null && ev.getAction() == MotionEvent.ACTION_MOVE) {
4103 mKeyWaiter.bindTargetWindowLocked(focus,
4104 KeyWaiter.RETURN_PENDING_TRACKBALL, qev);
4105 // We don't deliver movement events to the client, we hold
4106 // them and wait for them to call back.
4107 ev = null;
4108 } else {
4109 mKeyWaiter.bindTargetWindowLocked(focus);
4110 }
4111 }
Romain Guy06882f82009-06-10 13:36:04 -07004112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004113 try {
4114 focus.mClient.dispatchTrackball(ev, eventTime);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004115 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004116 } catch (android.os.RemoteException e) {
4117 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
4118 try {
4119 removeWindow(focus.mSession, focus.mClient);
4120 } catch (java.util.NoSuchElementException ex) {
4121 // This will happen if the window has already been
4122 // removed.
4123 }
4124 }
Romain Guy06882f82009-06-10 13:36:04 -07004125
Dianne Hackborncfaef692009-06-15 14:24:44 -07004126 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004127 }
Romain Guy06882f82009-06-10 13:36:04 -07004128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004129 /**
4130 * @return Returns true if event was dispatched, false if it was dropped for any reason
4131 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004132 private int dispatchKey(KeyEvent event, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004133 if (DEBUG_INPUT) Log.v(TAG, "Dispatch key: " + event);
4134
4135 Object focusObj = mKeyWaiter.waitForNextEventTarget(event, null,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004136 null, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004137 if (focusObj == null) {
4138 Log.w(TAG, "No focus window, dropping: " + event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004139 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004140 }
4141 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004142 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004143 }
Romain Guy06882f82009-06-10 13:36:04 -07004144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004145 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07004146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004147 if (DEBUG_INPUT) Log.v(
4148 TAG, "Dispatching to " + focus + ": " + event);
4149
4150 if (uid != 0 && uid != focus.mSession.mUid) {
4151 if (mContext.checkPermission(
4152 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4153 != PackageManager.PERMISSION_GRANTED) {
4154 Log.w(TAG, "Permission denied: injecting key event from pid "
4155 + pid + " uid " + uid + " to window " + focus
4156 + " owned by uid " + focus.mSession.mUid);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004157 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004158 }
4159 }
Romain Guy06882f82009-06-10 13:36:04 -07004160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004161 synchronized(mWindowMap) {
4162 mKeyWaiter.bindTargetWindowLocked(focus);
4163 }
4164
4165 // NOSHIP extra state logging
4166 mKeyWaiter.recordDispatchState(event, focus);
4167 // END NOSHIP
Romain Guy06882f82009-06-10 13:36:04 -07004168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004169 try {
4170 if (DEBUG_INPUT || DEBUG_FOCUS) {
4171 Log.v(TAG, "Delivering key " + event.getKeyCode()
4172 + " to " + focus);
4173 }
4174 focus.mClient.dispatchKey(event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004175 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004176 } catch (android.os.RemoteException e) {
4177 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
4178 try {
4179 removeWindow(focus.mSession, focus.mClient);
4180 } catch (java.util.NoSuchElementException ex) {
4181 // This will happen if the window has already been
4182 // removed.
4183 }
4184 }
Romain Guy06882f82009-06-10 13:36:04 -07004185
Dianne Hackborncfaef692009-06-15 14:24:44 -07004186 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004187 }
Romain Guy06882f82009-06-10 13:36:04 -07004188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004189 public void pauseKeyDispatching(IBinder _token) {
4190 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4191 "pauseKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004192 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004193 }
4194
4195 synchronized (mWindowMap) {
4196 WindowToken token = mTokenMap.get(_token);
4197 if (token != null) {
4198 mKeyWaiter.pauseDispatchingLocked(token);
4199 }
4200 }
4201 }
4202
4203 public void resumeKeyDispatching(IBinder _token) {
4204 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4205 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004206 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004207 }
4208
4209 synchronized (mWindowMap) {
4210 WindowToken token = mTokenMap.get(_token);
4211 if (token != null) {
4212 mKeyWaiter.resumeDispatchingLocked(token);
4213 }
4214 }
4215 }
4216
4217 public void setEventDispatching(boolean enabled) {
4218 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4219 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004220 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004221 }
4222
4223 synchronized (mWindowMap) {
4224 mKeyWaiter.setEventDispatchingLocked(enabled);
4225 }
4226 }
Romain Guy06882f82009-06-10 13:36:04 -07004227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004228 /**
4229 * Injects a keystroke event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004230 *
4231 * @param ev A motion event describing the keystroke action. (Be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004232 * {@link SystemClock#uptimeMillis()} as the timebase.)
4233 * @param sync If true, wait for the event to be completed before returning to the caller.
4234 * @return Returns true if event was dispatched, false if it was dropped for any reason
4235 */
4236 public boolean injectKeyEvent(KeyEvent ev, boolean sync) {
4237 long downTime = ev.getDownTime();
4238 long eventTime = ev.getEventTime();
4239
4240 int action = ev.getAction();
4241 int code = ev.getKeyCode();
4242 int repeatCount = ev.getRepeatCount();
4243 int metaState = ev.getMetaState();
4244 int deviceId = ev.getDeviceId();
4245 int scancode = ev.getScanCode();
4246
4247 if (eventTime == 0) eventTime = SystemClock.uptimeMillis();
4248 if (downTime == 0) downTime = eventTime;
4249
4250 KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
The Android Open Source Project10592532009-03-18 17:39:46 -07004251 deviceId, scancode, KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004252
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004253 final int pid = Binder.getCallingPid();
4254 final int uid = Binder.getCallingUid();
4255 final long ident = Binder.clearCallingIdentity();
4256 final int result = dispatchKey(newEvent, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004257 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004258 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004259 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004260 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004261 switch (result) {
4262 case INJECT_NO_PERMISSION:
4263 throw new SecurityException(
4264 "Injecting to another application requires INJECT_EVENT permission");
4265 case INJECT_SUCCEEDED:
4266 return true;
4267 }
4268 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004269 }
4270
4271 /**
4272 * Inject a pointer (touch) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004273 *
4274 * @param ev A motion event describing the pointer (touch) action. (As noted in
4275 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004276 * {@link SystemClock#uptimeMillis()} as the timebase.)
4277 * @param sync If true, wait for the event to be completed before returning to the caller.
4278 * @return Returns true if event was dispatched, false if it was dropped for any reason
4279 */
4280 public boolean injectPointerEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004281 final int pid = Binder.getCallingPid();
4282 final int uid = Binder.getCallingUid();
4283 final long ident = Binder.clearCallingIdentity();
4284 final int result = dispatchPointer(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004285 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004286 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004287 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004288 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004289 switch (result) {
4290 case INJECT_NO_PERMISSION:
4291 throw new SecurityException(
4292 "Injecting to another application requires INJECT_EVENT permission");
4293 case INJECT_SUCCEEDED:
4294 return true;
4295 }
4296 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004297 }
Romain Guy06882f82009-06-10 13:36:04 -07004298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004299 /**
4300 * Inject a trackball (navigation device) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004301 *
4302 * @param ev A motion event describing the trackball action. (As noted in
4303 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004304 * {@link SystemClock#uptimeMillis()} as the timebase.)
4305 * @param sync If true, wait for the event to be completed before returning to the caller.
4306 * @return Returns true if event was dispatched, false if it was dropped for any reason
4307 */
4308 public boolean injectTrackballEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004309 final int pid = Binder.getCallingPid();
4310 final int uid = Binder.getCallingUid();
4311 final long ident = Binder.clearCallingIdentity();
4312 final int result = dispatchTrackball(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004313 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004314 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004315 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004316 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004317 switch (result) {
4318 case INJECT_NO_PERMISSION:
4319 throw new SecurityException(
4320 "Injecting to another application requires INJECT_EVENT permission");
4321 case INJECT_SUCCEEDED:
4322 return true;
4323 }
4324 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004325 }
Romain Guy06882f82009-06-10 13:36:04 -07004326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004327 private WindowState getFocusedWindow() {
4328 synchronized (mWindowMap) {
4329 return getFocusedWindowLocked();
4330 }
4331 }
4332
4333 private WindowState getFocusedWindowLocked() {
4334 return mCurrentFocus;
4335 }
Romain Guy06882f82009-06-10 13:36:04 -07004336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004337 /**
4338 * This class holds the state for dispatching key events. This state
4339 * is protected by the KeyWaiter instance, NOT by the window lock. You
4340 * can be holding the main window lock while acquire the KeyWaiter lock,
4341 * but not the other way around.
4342 */
4343 final class KeyWaiter {
4344 // NOSHIP debugging
4345 public class DispatchState {
4346 private KeyEvent event;
4347 private WindowState focus;
4348 private long time;
4349 private WindowState lastWin;
4350 private IBinder lastBinder;
4351 private boolean finished;
4352 private boolean gotFirstWindow;
4353 private boolean eventDispatching;
4354 private long timeToSwitch;
4355 private boolean wasFrozen;
4356 private boolean focusPaused;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004357 private WindowState curFocus;
Romain Guy06882f82009-06-10 13:36:04 -07004358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004359 DispatchState(KeyEvent theEvent, WindowState theFocus) {
4360 focus = theFocus;
4361 event = theEvent;
4362 time = System.currentTimeMillis();
4363 // snapshot KeyWaiter state
4364 lastWin = mLastWin;
4365 lastBinder = mLastBinder;
4366 finished = mFinished;
4367 gotFirstWindow = mGotFirstWindow;
4368 eventDispatching = mEventDispatching;
4369 timeToSwitch = mTimeToSwitch;
4370 wasFrozen = mWasFrozen;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004371 curFocus = mCurrentFocus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004372 // cache the paused state at ctor time as well
4373 if (theFocus == null || theFocus.mToken == null) {
4374 Log.i(TAG, "focus " + theFocus + " mToken is null at event dispatch!");
4375 focusPaused = false;
4376 } else {
4377 focusPaused = theFocus.mToken.paused;
4378 }
4379 }
Romain Guy06882f82009-06-10 13:36:04 -07004380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004381 public String toString() {
4382 return "{{" + event + " to " + focus + " @ " + time
4383 + " lw=" + lastWin + " lb=" + lastBinder
4384 + " fin=" + finished + " gfw=" + gotFirstWindow
4385 + " ed=" + eventDispatching + " tts=" + timeToSwitch
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004386 + " wf=" + wasFrozen + " fp=" + focusPaused
4387 + " mcf=" + mCurrentFocus + "}}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004388 }
4389 };
4390 private DispatchState mDispatchState = null;
4391 public void recordDispatchState(KeyEvent theEvent, WindowState theFocus) {
4392 mDispatchState = new DispatchState(theEvent, theFocus);
4393 }
4394 // END NOSHIP
4395
4396 public static final int RETURN_NOTHING = 0;
4397 public static final int RETURN_PENDING_POINTER = 1;
4398 public static final int RETURN_PENDING_TRACKBALL = 2;
Romain Guy06882f82009-06-10 13:36:04 -07004399
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004400 final Object SKIP_TARGET_TOKEN = new Object();
4401 final Object CONSUMED_EVENT_TOKEN = new Object();
Romain Guy06882f82009-06-10 13:36:04 -07004402
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004403 private WindowState mLastWin = null;
4404 private IBinder mLastBinder = null;
4405 private boolean mFinished = true;
4406 private boolean mGotFirstWindow = false;
4407 private boolean mEventDispatching = true;
4408 private long mTimeToSwitch = 0;
4409 /* package */ boolean mWasFrozen = false;
Romain Guy06882f82009-06-10 13:36:04 -07004410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004411 // Target of Motion events
4412 WindowState mMotionTarget;
Romain Guy06882f82009-06-10 13:36:04 -07004413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004414 // Windows above the target who would like to receive an "outside"
4415 // touch event for any down events outside of them.
4416 WindowState mOutsideTouchTargets;
4417
4418 /**
4419 * Wait for the last event dispatch to complete, then find the next
4420 * target that should receive the given event and wait for that one
4421 * to be ready to receive it.
4422 */
4423 Object waitForNextEventTarget(KeyEvent nextKey, QueuedEvent qev,
4424 MotionEvent nextMotion, boolean isPointerEvent,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004425 boolean failIfTimeout, int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004426 long startTime = SystemClock.uptimeMillis();
4427 long keyDispatchingTimeout = 5 * 1000;
4428 long waitedFor = 0;
4429
4430 while (true) {
4431 // Figure out which window we care about. It is either the
4432 // last window we are waiting to have process the event or,
4433 // if none, then the next window we think the event should go
4434 // to. Note: we retrieve mLastWin outside of the lock, so
4435 // it may change before we lock. Thus we must check it again.
4436 WindowState targetWin = mLastWin;
4437 boolean targetIsNew = targetWin == null;
4438 if (DEBUG_INPUT) Log.v(
4439 TAG, "waitForLastKey: mFinished=" + mFinished +
4440 ", mLastWin=" + mLastWin);
4441 if (targetIsNew) {
4442 Object target = findTargetWindow(nextKey, qev, nextMotion,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004443 isPointerEvent, callingPid, callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004444 if (target == SKIP_TARGET_TOKEN) {
4445 // The user has pressed a special key, and we are
4446 // dropping all pending events before it.
4447 if (DEBUG_INPUT) Log.v(TAG, "Skipping: " + nextKey
4448 + " " + nextMotion);
4449 return null;
4450 }
4451 if (target == CONSUMED_EVENT_TOKEN) {
4452 if (DEBUG_INPUT) Log.v(TAG, "Consumed: " + nextKey
4453 + " " + nextMotion);
4454 return target;
4455 }
4456 targetWin = (WindowState)target;
4457 }
Romain Guy06882f82009-06-10 13:36:04 -07004458
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004459 AppWindowToken targetApp = null;
Romain Guy06882f82009-06-10 13:36:04 -07004460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004461 // Now: is it okay to send the next event to this window?
4462 synchronized (this) {
4463 // First: did we come here based on the last window not
4464 // being null, but it changed by the time we got here?
4465 // If so, try again.
4466 if (!targetIsNew && mLastWin == null) {
4467 continue;
4468 }
Romain Guy06882f82009-06-10 13:36:04 -07004469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004470 // We never dispatch events if not finished with the
4471 // last one, or the display is frozen.
4472 if (mFinished && !mDisplayFrozen) {
4473 // If event dispatching is disabled, then we
4474 // just consume the events.
4475 if (!mEventDispatching) {
4476 if (DEBUG_INPUT) Log.v(TAG,
4477 "Skipping event; dispatching disabled: "
4478 + nextKey + " " + nextMotion);
4479 return null;
4480 }
4481 if (targetWin != null) {
4482 // If this is a new target, and that target is not
4483 // paused or unresponsive, then all looks good to
4484 // handle the event.
4485 if (targetIsNew && !targetWin.mToken.paused) {
4486 return targetWin;
4487 }
Romain Guy06882f82009-06-10 13:36:04 -07004488
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004489 // If we didn't find a target window, and there is no
4490 // focused app window, then just eat the events.
4491 } else if (mFocusedApp == null) {
4492 if (DEBUG_INPUT) Log.v(TAG,
4493 "Skipping event; no focused app: "
4494 + nextKey + " " + nextMotion);
4495 return null;
4496 }
4497 }
Romain Guy06882f82009-06-10 13:36:04 -07004498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004499 if (DEBUG_INPUT) Log.v(
4500 TAG, "Waiting for last key in " + mLastBinder
4501 + " target=" + targetWin
4502 + " mFinished=" + mFinished
4503 + " mDisplayFrozen=" + mDisplayFrozen
4504 + " targetIsNew=" + targetIsNew
4505 + " paused="
4506 + (targetWin != null ? targetWin.mToken.paused : false)
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004507 + " mFocusedApp=" + mFocusedApp
4508 + " mCurrentFocus=" + mCurrentFocus);
Romain Guy06882f82009-06-10 13:36:04 -07004509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004510 targetApp = targetWin != null
4511 ? targetWin.mAppToken : mFocusedApp;
Romain Guy06882f82009-06-10 13:36:04 -07004512
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004513 long curTimeout = keyDispatchingTimeout;
4514 if (mTimeToSwitch != 0) {
4515 long now = SystemClock.uptimeMillis();
4516 if (mTimeToSwitch <= now) {
4517 // If an app switch key has been pressed, and we have
4518 // waited too long for the current app to finish
4519 // processing keys, then wait no more!
4520 doFinishedKeyLocked(true);
4521 continue;
4522 }
4523 long switchTimeout = mTimeToSwitch - now;
4524 if (curTimeout > switchTimeout) {
4525 curTimeout = switchTimeout;
4526 }
4527 }
Romain Guy06882f82009-06-10 13:36:04 -07004528
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004529 try {
4530 // after that continue
4531 // processing keys, so we don't get stuck.
4532 if (DEBUG_INPUT) Log.v(
4533 TAG, "Waiting for key dispatch: " + curTimeout);
4534 wait(curTimeout);
4535 if (DEBUG_INPUT) Log.v(TAG, "Finished waiting @"
4536 + SystemClock.uptimeMillis() + " startTime="
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004537 + startTime + " switchTime=" + mTimeToSwitch
4538 + " target=" + targetWin + " mLW=" + mLastWin
4539 + " mLB=" + mLastBinder + " fin=" + mFinished
4540 + " mCurrentFocus=" + mCurrentFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004541 } catch (InterruptedException e) {
4542 }
4543 }
4544
4545 // If we were frozen during configuration change, restart the
4546 // timeout checks from now; otherwise look at whether we timed
4547 // out before awakening.
4548 if (mWasFrozen) {
4549 waitedFor = 0;
4550 mWasFrozen = false;
4551 } else {
4552 waitedFor = SystemClock.uptimeMillis() - startTime;
4553 }
4554
4555 if (waitedFor >= keyDispatchingTimeout && mTimeToSwitch == 0) {
4556 IApplicationToken at = null;
4557 synchronized (this) {
4558 Log.w(TAG, "Key dispatching timed out sending to " +
4559 (targetWin != null ? targetWin.mAttrs.getTitle()
4560 : "<null>"));
4561 // NOSHIP debugging
4562 Log.w(TAG, "Dispatch state: " + mDispatchState);
4563 Log.w(TAG, "Current state: " + new DispatchState(nextKey, targetWin));
4564 // END NOSHIP
4565 //dump();
4566 if (targetWin != null) {
4567 at = targetWin.getAppToken();
4568 } else if (targetApp != null) {
4569 at = targetApp.appToken;
4570 }
4571 }
4572
4573 boolean abort = true;
4574 if (at != null) {
4575 try {
4576 long timeout = at.getKeyDispatchingTimeout();
4577 if (timeout > waitedFor) {
4578 // we did not wait the proper amount of time for this application.
4579 // set the timeout to be the real timeout and wait again.
4580 keyDispatchingTimeout = timeout - waitedFor;
4581 continue;
4582 } else {
4583 abort = at.keyDispatchingTimedOut();
4584 }
4585 } catch (RemoteException ex) {
4586 }
4587 }
4588
4589 synchronized (this) {
4590 if (abort && (mLastWin == targetWin || targetWin == null)) {
4591 mFinished = true;
Romain Guy06882f82009-06-10 13:36:04 -07004592 if (mLastWin != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004593 if (DEBUG_INPUT) Log.v(TAG,
4594 "Window " + mLastWin +
4595 " timed out on key input");
4596 if (mLastWin.mToken.paused) {
4597 Log.w(TAG, "Un-pausing dispatching to this window");
4598 mLastWin.mToken.paused = false;
4599 }
4600 }
4601 if (mMotionTarget == targetWin) {
4602 mMotionTarget = null;
4603 }
4604 mLastWin = null;
4605 mLastBinder = null;
4606 if (failIfTimeout || targetWin == null) {
4607 return null;
4608 }
4609 } else {
4610 Log.w(TAG, "Continuing to wait for key to be dispatched");
4611 startTime = SystemClock.uptimeMillis();
4612 }
4613 }
4614 }
4615 }
4616 }
Romain Guy06882f82009-06-10 13:36:04 -07004617
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004618 Object findTargetWindow(KeyEvent nextKey, QueuedEvent qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004619 MotionEvent nextMotion, boolean isPointerEvent,
4620 int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004621 mOutsideTouchTargets = null;
Romain Guy06882f82009-06-10 13:36:04 -07004622
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004623 if (nextKey != null) {
4624 // Find the target window for a normal key event.
4625 final int keycode = nextKey.getKeyCode();
4626 final int repeatCount = nextKey.getRepeatCount();
4627 final boolean down = nextKey.getAction() != KeyEvent.ACTION_UP;
4628 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(keycode);
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004629
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004630 if (!dispatch) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004631 if (callingUid == 0 ||
4632 mContext.checkPermission(
4633 android.Manifest.permission.INJECT_EVENTS,
4634 callingPid, callingUid)
4635 == PackageManager.PERMISSION_GRANTED) {
4636 mPolicy.interceptKeyTi(null, keycode,
4637 nextKey.getMetaState(), down, repeatCount);
4638 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004639 Log.w(TAG, "Event timeout during app switch: dropping "
4640 + nextKey);
4641 return SKIP_TARGET_TOKEN;
4642 }
Romain Guy06882f82009-06-10 13:36:04 -07004643
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004644 // System.out.println("##### [" + SystemClock.uptimeMillis() + "] WindowManagerService.dispatchKey(" + keycode + ", " + down + ", " + repeatCount + ")");
Romain Guy06882f82009-06-10 13:36:04 -07004645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004646 WindowState focus = null;
4647 synchronized(mWindowMap) {
4648 focus = getFocusedWindowLocked();
4649 }
Romain Guy06882f82009-06-10 13:36:04 -07004650
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004651 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
Romain Guy06882f82009-06-10 13:36:04 -07004652
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004653 if (callingUid == 0 ||
4654 (focus != null && callingUid == focus.mSession.mUid) ||
4655 mContext.checkPermission(
4656 android.Manifest.permission.INJECT_EVENTS,
4657 callingPid, callingUid)
4658 == PackageManager.PERMISSION_GRANTED) {
4659 if (mPolicy.interceptKeyTi(focus,
4660 keycode, nextKey.getMetaState(), down, repeatCount)) {
4661 return CONSUMED_EVENT_TOKEN;
4662 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004663 }
Romain Guy06882f82009-06-10 13:36:04 -07004664
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004665 return focus;
Romain Guy06882f82009-06-10 13:36:04 -07004666
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004667 } else if (!isPointerEvent) {
4668 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(-1);
4669 if (!dispatch) {
4670 Log.w(TAG, "Event timeout during app switch: dropping trackball "
4671 + nextMotion);
4672 return SKIP_TARGET_TOKEN;
4673 }
Romain Guy06882f82009-06-10 13:36:04 -07004674
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004675 WindowState focus = null;
4676 synchronized(mWindowMap) {
4677 focus = getFocusedWindowLocked();
4678 }
Romain Guy06882f82009-06-10 13:36:04 -07004679
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004680 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
4681 return focus;
4682 }
Romain Guy06882f82009-06-10 13:36:04 -07004683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004684 if (nextMotion == null) {
4685 return SKIP_TARGET_TOKEN;
4686 }
Romain Guy06882f82009-06-10 13:36:04 -07004687
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004688 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(
4689 KeyEvent.KEYCODE_UNKNOWN);
4690 if (!dispatch) {
4691 Log.w(TAG, "Event timeout during app switch: dropping pointer "
4692 + nextMotion);
4693 return SKIP_TARGET_TOKEN;
4694 }
Romain Guy06882f82009-06-10 13:36:04 -07004695
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004696 // Find the target window for a pointer event.
4697 int action = nextMotion.getAction();
4698 final float xf = nextMotion.getX();
4699 final float yf = nextMotion.getY();
4700 final long eventTime = nextMotion.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07004701
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004702 final boolean screenWasOff = qev != null
4703 && (qev.flags&WindowManagerPolicy.FLAG_BRIGHT_HERE) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07004704
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004705 WindowState target = null;
Romain Guy06882f82009-06-10 13:36:04 -07004706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004707 synchronized(mWindowMap) {
4708 synchronized (this) {
4709 if (action == MotionEvent.ACTION_DOWN) {
4710 if (mMotionTarget != null) {
4711 // this is weird, we got a pen down, but we thought it was
4712 // already down!
4713 // XXX: We should probably send an ACTION_UP to the current
4714 // target.
4715 Log.w(TAG, "Pointer down received while already down in: "
4716 + mMotionTarget);
4717 mMotionTarget = null;
4718 }
Romain Guy06882f82009-06-10 13:36:04 -07004719
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004720 // ACTION_DOWN is special, because we need to lock next events to
4721 // the window we'll land onto.
4722 final int x = (int)xf;
4723 final int y = (int)yf;
Romain Guy06882f82009-06-10 13:36:04 -07004724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004725 final ArrayList windows = mWindows;
4726 final int N = windows.size();
4727 WindowState topErrWindow = null;
4728 final Rect tmpRect = mTempRect;
4729 for (int i=N-1; i>=0; i--) {
4730 WindowState child = (WindowState)windows.get(i);
4731 //Log.i(TAG, "Checking dispatch to: " + child);
4732 final int flags = child.mAttrs.flags;
4733 if ((flags & WindowManager.LayoutParams.FLAG_SYSTEM_ERROR) != 0) {
4734 if (topErrWindow == null) {
4735 topErrWindow = child;
4736 }
4737 }
4738 if (!child.isVisibleLw()) {
4739 //Log.i(TAG, "Not visible!");
4740 continue;
4741 }
4742 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
4743 //Log.i(TAG, "Not touchable!");
4744 if ((flags & WindowManager.LayoutParams
4745 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
4746 child.mNextOutsideTouch = mOutsideTouchTargets;
4747 mOutsideTouchTargets = child;
4748 }
4749 continue;
4750 }
4751 tmpRect.set(child.mFrame);
4752 if (child.mTouchableInsets == ViewTreeObserver
4753 .InternalInsetsInfo.TOUCHABLE_INSETS_CONTENT) {
4754 // The touch is inside of the window if it is
4755 // inside the frame, AND the content part of that
4756 // frame that was given by the application.
4757 tmpRect.left += child.mGivenContentInsets.left;
4758 tmpRect.top += child.mGivenContentInsets.top;
4759 tmpRect.right -= child.mGivenContentInsets.right;
4760 tmpRect.bottom -= child.mGivenContentInsets.bottom;
4761 } else if (child.mTouchableInsets == ViewTreeObserver
4762 .InternalInsetsInfo.TOUCHABLE_INSETS_VISIBLE) {
4763 // The touch is inside of the window if it is
4764 // inside the frame, AND the visible part of that
4765 // frame that was given by the application.
4766 tmpRect.left += child.mGivenVisibleInsets.left;
4767 tmpRect.top += child.mGivenVisibleInsets.top;
4768 tmpRect.right -= child.mGivenVisibleInsets.right;
4769 tmpRect.bottom -= child.mGivenVisibleInsets.bottom;
4770 }
4771 final int touchFlags = flags &
4772 (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
4773 |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
4774 if (tmpRect.contains(x, y) || touchFlags == 0) {
4775 //Log.i(TAG, "Using this target!");
4776 if (!screenWasOff || (flags &
4777 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING) != 0) {
4778 mMotionTarget = child;
4779 } else {
4780 //Log.i(TAG, "Waking, skip!");
4781 mMotionTarget = null;
4782 }
4783 break;
4784 }
Romain Guy06882f82009-06-10 13:36:04 -07004785
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004786 if ((flags & WindowManager.LayoutParams
4787 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
4788 child.mNextOutsideTouch = mOutsideTouchTargets;
4789 mOutsideTouchTargets = child;
4790 //Log.i(TAG, "Adding to outside target list: " + child);
4791 }
4792 }
4793
4794 // if there's an error window but it's not accepting
4795 // focus (typically because it is not yet visible) just
4796 // wait for it -- any other focused window may in fact
4797 // be in ANR state.
4798 if (topErrWindow != null && mMotionTarget != topErrWindow) {
4799 mMotionTarget = null;
4800 }
4801 }
Romain Guy06882f82009-06-10 13:36:04 -07004802
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004803 target = mMotionTarget;
4804 }
4805 }
Romain Guy06882f82009-06-10 13:36:04 -07004806
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004807 wakeupIfNeeded(target, eventType(nextMotion));
Romain Guy06882f82009-06-10 13:36:04 -07004808
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004809 // Pointer events are a little different -- if there isn't a
4810 // target found for any event, then just drop it.
4811 return target != null ? target : SKIP_TARGET_TOKEN;
4812 }
Romain Guy06882f82009-06-10 13:36:04 -07004813
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004814 boolean checkShouldDispatchKey(int keycode) {
4815 synchronized (this) {
4816 if (mPolicy.isAppSwitchKeyTqTiLwLi(keycode)) {
4817 mTimeToSwitch = 0;
4818 return true;
4819 }
4820 if (mTimeToSwitch != 0
4821 && mTimeToSwitch < SystemClock.uptimeMillis()) {
4822 return false;
4823 }
4824 return true;
4825 }
4826 }
Romain Guy06882f82009-06-10 13:36:04 -07004827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004828 void bindTargetWindowLocked(WindowState win,
4829 int pendingWhat, QueuedEvent pendingMotion) {
4830 synchronized (this) {
4831 bindTargetWindowLockedLocked(win, pendingWhat, pendingMotion);
4832 }
4833 }
Romain Guy06882f82009-06-10 13:36:04 -07004834
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004835 void bindTargetWindowLocked(WindowState win) {
4836 synchronized (this) {
4837 bindTargetWindowLockedLocked(win, RETURN_NOTHING, null);
4838 }
4839 }
4840
4841 void bindTargetWindowLockedLocked(WindowState win,
4842 int pendingWhat, QueuedEvent pendingMotion) {
4843 mLastWin = win;
4844 mLastBinder = win.mClient.asBinder();
4845 mFinished = false;
4846 if (pendingMotion != null) {
4847 final Session s = win.mSession;
4848 if (pendingWhat == RETURN_PENDING_POINTER) {
4849 releasePendingPointerLocked(s);
4850 s.mPendingPointerMove = pendingMotion;
4851 s.mPendingPointerWindow = win;
Romain Guy06882f82009-06-10 13:36:04 -07004852 if (DEBUG_INPUT) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004853 "bindTargetToWindow " + s.mPendingPointerMove);
4854 } else if (pendingWhat == RETURN_PENDING_TRACKBALL) {
4855 releasePendingTrackballLocked(s);
4856 s.mPendingTrackballMove = pendingMotion;
4857 s.mPendingTrackballWindow = win;
4858 }
4859 }
4860 }
Romain Guy06882f82009-06-10 13:36:04 -07004861
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004862 void releasePendingPointerLocked(Session s) {
4863 if (DEBUG_INPUT) Log.v(TAG,
4864 "releasePendingPointer " + s.mPendingPointerMove);
4865 if (s.mPendingPointerMove != null) {
4866 mQueue.recycleEvent(s.mPendingPointerMove);
4867 s.mPendingPointerMove = null;
4868 }
4869 }
Romain Guy06882f82009-06-10 13:36:04 -07004870
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004871 void releasePendingTrackballLocked(Session s) {
4872 if (s.mPendingTrackballMove != null) {
4873 mQueue.recycleEvent(s.mPendingTrackballMove);
4874 s.mPendingTrackballMove = null;
4875 }
4876 }
Romain Guy06882f82009-06-10 13:36:04 -07004877
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004878 MotionEvent finishedKey(Session session, IWindow client, boolean force,
4879 int returnWhat) {
4880 if (DEBUG_INPUT) Log.v(
4881 TAG, "finishedKey: client=" + client + ", force=" + force);
4882
4883 if (client == null) {
4884 return null;
4885 }
4886
4887 synchronized (this) {
4888 if (DEBUG_INPUT) Log.v(
4889 TAG, "finishedKey: client=" + client.asBinder()
4890 + ", force=" + force + ", last=" + mLastBinder
4891 + " (token=" + (mLastWin != null ? mLastWin.mToken : null) + ")");
4892
4893 QueuedEvent qev = null;
4894 WindowState win = null;
4895 if (returnWhat == RETURN_PENDING_POINTER) {
4896 qev = session.mPendingPointerMove;
4897 win = session.mPendingPointerWindow;
4898 session.mPendingPointerMove = null;
4899 session.mPendingPointerWindow = null;
4900 } else if (returnWhat == RETURN_PENDING_TRACKBALL) {
4901 qev = session.mPendingTrackballMove;
4902 win = session.mPendingTrackballWindow;
4903 session.mPendingTrackballMove = null;
4904 session.mPendingTrackballWindow = null;
4905 }
Romain Guy06882f82009-06-10 13:36:04 -07004906
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004907 if (mLastBinder == client.asBinder()) {
4908 if (DEBUG_INPUT) Log.v(
4909 TAG, "finishedKey: last paused="
4910 + ((mLastWin != null) ? mLastWin.mToken.paused : "null"));
4911 if (mLastWin != null && (!mLastWin.mToken.paused || force
4912 || !mEventDispatching)) {
4913 doFinishedKeyLocked(false);
4914 } else {
4915 // Make sure to wake up anyone currently waiting to
4916 // dispatch a key, so they can re-evaluate their
4917 // current situation.
4918 mFinished = true;
4919 notifyAll();
4920 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004921 }
Romain Guy06882f82009-06-10 13:36:04 -07004922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004923 if (qev != null) {
4924 MotionEvent res = (MotionEvent)qev.event;
4925 if (DEBUG_INPUT) Log.v(TAG,
4926 "Returning pending motion: " + res);
4927 mQueue.recycleEvent(qev);
4928 if (win != null && returnWhat == RETURN_PENDING_POINTER) {
4929 res.offsetLocation(-win.mFrame.left, -win.mFrame.top);
4930 }
4931 return res;
4932 }
4933 return null;
4934 }
4935 }
4936
4937 void tickle() {
4938 synchronized (this) {
4939 notifyAll();
4940 }
4941 }
Romain Guy06882f82009-06-10 13:36:04 -07004942
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004943 void handleNewWindowLocked(WindowState newWindow) {
4944 if (!newWindow.canReceiveKeys()) {
4945 return;
4946 }
4947 synchronized (this) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004948 if (DEBUG_INPUT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004949 TAG, "New key dispatch window: win="
4950 + newWindow.mClient.asBinder()
4951 + ", last=" + mLastBinder
4952 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
4953 + "), finished=" + mFinished + ", paused="
4954 + newWindow.mToken.paused);
4955
4956 // Displaying a window implicitly causes dispatching to
4957 // be unpaused. (This is to protect against bugs if someone
4958 // pauses dispatching but forgets to resume.)
4959 newWindow.mToken.paused = false;
4960
4961 mGotFirstWindow = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004962
4963 if ((newWindow.mAttrs.flags & FLAG_SYSTEM_ERROR) != 0) {
4964 if (DEBUG_INPUT) Log.v(TAG,
4965 "New SYSTEM_ERROR window; resetting state");
4966 mLastWin = null;
4967 mLastBinder = null;
4968 mMotionTarget = null;
4969 mFinished = true;
4970 } else if (mLastWin != null) {
4971 // If the new window is above the window we are
4972 // waiting on, then stop waiting and let key dispatching
4973 // start on the new guy.
4974 if (DEBUG_INPUT) Log.v(
4975 TAG, "Last win layer=" + mLastWin.mLayer
4976 + ", new win layer=" + newWindow.mLayer);
4977 if (newWindow.mLayer >= mLastWin.mLayer) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004978 // The new window is above the old; finish pending input to the last
4979 // window and start directing it to the new one.
4980 mLastWin.mToken.paused = false;
4981 doFinishedKeyLocked(true); // does a notifyAll()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004982 }
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004983 // Either the new window is lower, so there is no need to wake key waiters,
4984 // or we just finished key input to the previous window, which implicitly
4985 // notified the key waiters. In both cases, we don't need to issue the
4986 // notification here.
4987 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004988 }
4989
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004990 // Now that we've put a new window state in place, make the event waiter
4991 // take notice and retarget its attentions.
4992 notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004993 }
4994 }
4995
4996 void pauseDispatchingLocked(WindowToken token) {
4997 synchronized (this)
4998 {
4999 if (DEBUG_INPUT) Log.v(TAG, "Pausing WindowToken " + token);
5000 token.paused = true;
5001
5002 /*
5003 if (mLastWin != null && !mFinished && mLastWin.mBaseLayer <= layer) {
5004 mPaused = true;
5005 } else {
5006 if (mLastWin == null) {
Dave Bortcfe65242009-04-09 14:51:04 -07005007 Log.i(TAG, "Key dispatching not paused: no last window.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005008 } else if (mFinished) {
Dave Bortcfe65242009-04-09 14:51:04 -07005009 Log.i(TAG, "Key dispatching not paused: finished last key.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005010 } else {
Dave Bortcfe65242009-04-09 14:51:04 -07005011 Log.i(TAG, "Key dispatching not paused: window in higher layer.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005012 }
5013 }
5014 */
5015 }
5016 }
5017
5018 void resumeDispatchingLocked(WindowToken token) {
5019 synchronized (this) {
5020 if (token.paused) {
5021 if (DEBUG_INPUT) Log.v(
5022 TAG, "Resuming WindowToken " + token
5023 + ", last=" + mLastBinder
5024 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
5025 + "), finished=" + mFinished + ", paused="
5026 + token.paused);
5027 token.paused = false;
5028 if (mLastWin != null && mLastWin.mToken == token && mFinished) {
5029 doFinishedKeyLocked(true);
5030 } else {
5031 notifyAll();
5032 }
5033 }
5034 }
5035 }
5036
5037 void setEventDispatchingLocked(boolean enabled) {
5038 synchronized (this) {
5039 mEventDispatching = enabled;
5040 notifyAll();
5041 }
5042 }
Romain Guy06882f82009-06-10 13:36:04 -07005043
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005044 void appSwitchComing() {
5045 synchronized (this) {
5046 // Don't wait for more than .5 seconds for app to finish
5047 // processing the pending events.
5048 long now = SystemClock.uptimeMillis() + 500;
5049 if (DEBUG_INPUT) Log.v(TAG, "appSwitchComing: " + now);
5050 if (mTimeToSwitch == 0 || now < mTimeToSwitch) {
5051 mTimeToSwitch = now;
5052 }
5053 notifyAll();
5054 }
5055 }
Romain Guy06882f82009-06-10 13:36:04 -07005056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005057 private final void doFinishedKeyLocked(boolean doRecycle) {
5058 if (mLastWin != null) {
5059 releasePendingPointerLocked(mLastWin.mSession);
5060 releasePendingTrackballLocked(mLastWin.mSession);
5061 }
Romain Guy06882f82009-06-10 13:36:04 -07005062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005063 if (mLastWin == null || !mLastWin.mToken.paused
5064 || !mLastWin.isVisibleLw()) {
5065 // If the current window has been paused, we aren't -really-
5066 // finished... so let the waiters still wait.
5067 mLastWin = null;
5068 mLastBinder = null;
5069 }
5070 mFinished = true;
5071 notifyAll();
5072 }
5073 }
5074
5075 private class KeyQ extends KeyInputQueue
5076 implements KeyInputQueue.FilterCallback {
5077 PowerManager.WakeLock mHoldingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07005078
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005079 KeyQ() {
5080 super(mContext);
5081 PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
5082 mHoldingScreen = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
5083 "KEEP_SCREEN_ON_FLAG");
5084 mHoldingScreen.setReferenceCounted(false);
5085 }
5086
5087 @Override
5088 boolean preprocessEvent(InputDevice device, RawInputEvent event) {
5089 if (mPolicy.preprocessInputEventTq(event)) {
5090 return true;
5091 }
Romain Guy06882f82009-06-10 13:36:04 -07005092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005093 switch (event.type) {
5094 case RawInputEvent.EV_KEY: {
5095 // XXX begin hack
5096 if (DEBUG) {
5097 if (event.keycode == KeyEvent.KEYCODE_G) {
5098 if (event.value != 0) {
5099 // G down
5100 mPolicy.screenTurnedOff(WindowManagerPolicy.OFF_BECAUSE_OF_USER);
5101 }
5102 return false;
5103 }
5104 if (event.keycode == KeyEvent.KEYCODE_D) {
5105 if (event.value != 0) {
5106 //dump();
5107 }
5108 return false;
5109 }
5110 }
5111 // XXX end hack
Romain Guy06882f82009-06-10 13:36:04 -07005112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005113 boolean screenIsOff = !mPowerManager.screenIsOn();
5114 boolean screenIsDim = !mPowerManager.screenIsBright();
5115 int actions = mPolicy.interceptKeyTq(event, !screenIsOff);
Romain Guy06882f82009-06-10 13:36:04 -07005116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005117 if ((actions & WindowManagerPolicy.ACTION_GO_TO_SLEEP) != 0) {
5118 mPowerManager.goToSleep(event.when);
5119 }
5120
5121 if (screenIsOff) {
5122 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5123 }
5124 if (screenIsDim) {
5125 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5126 }
5127 if ((actions & WindowManagerPolicy.ACTION_POKE_USER_ACTIVITY) != 0) {
5128 mPowerManager.userActivity(event.when, false,
Michael Chane96440f2009-05-06 10:27:36 -07005129 LocalPowerManager.BUTTON_EVENT, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005130 }
Romain Guy06882f82009-06-10 13:36:04 -07005131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005132 if ((actions & WindowManagerPolicy.ACTION_PASS_TO_USER) != 0) {
5133 if (event.value != 0 && mPolicy.isAppSwitchKeyTqTiLwLi(event.keycode)) {
5134 filterQueue(this);
5135 mKeyWaiter.appSwitchComing();
5136 }
5137 return true;
5138 } else {
5139 return false;
5140 }
5141 }
Romain Guy06882f82009-06-10 13:36:04 -07005142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005143 case RawInputEvent.EV_REL: {
5144 boolean screenIsOff = !mPowerManager.screenIsOn();
5145 boolean screenIsDim = !mPowerManager.screenIsBright();
5146 if (screenIsOff) {
5147 if (!mPolicy.isWakeRelMovementTq(event.deviceId,
5148 device.classes, event)) {
5149 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
5150 return false;
5151 }
5152 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5153 }
5154 if (screenIsDim) {
5155 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5156 }
5157 return true;
5158 }
Romain Guy06882f82009-06-10 13:36:04 -07005159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005160 case RawInputEvent.EV_ABS: {
5161 boolean screenIsOff = !mPowerManager.screenIsOn();
5162 boolean screenIsDim = !mPowerManager.screenIsBright();
5163 if (screenIsOff) {
5164 if (!mPolicy.isWakeAbsMovementTq(event.deviceId,
5165 device.classes, event)) {
5166 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
5167 return false;
5168 }
5169 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5170 }
5171 if (screenIsDim) {
5172 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5173 }
5174 return true;
5175 }
Romain Guy06882f82009-06-10 13:36:04 -07005176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005177 default:
5178 return true;
5179 }
5180 }
5181
5182 public int filterEvent(QueuedEvent ev) {
5183 switch (ev.classType) {
5184 case RawInputEvent.CLASS_KEYBOARD:
5185 KeyEvent ke = (KeyEvent)ev.event;
5186 if (mPolicy.isMovementKeyTi(ke.getKeyCode())) {
5187 Log.w(TAG, "Dropping movement key during app switch: "
5188 + ke.getKeyCode() + ", action=" + ke.getAction());
5189 return FILTER_REMOVE;
5190 }
5191 return FILTER_ABORT;
5192 default:
5193 return FILTER_KEEP;
5194 }
5195 }
Romain Guy06882f82009-06-10 13:36:04 -07005196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005197 /**
5198 * Must be called with the main window manager lock held.
5199 */
5200 void setHoldScreenLocked(boolean holding) {
5201 boolean state = mHoldingScreen.isHeld();
5202 if (holding != state) {
5203 if (holding) {
5204 mHoldingScreen.acquire();
5205 } else {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005206 mPolicy.screenOnStoppedLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005207 mHoldingScreen.release();
5208 }
5209 }
5210 }
5211 };
5212
5213 public boolean detectSafeMode() {
5214 mSafeMode = mPolicy.detectSafeMode();
5215 return mSafeMode;
5216 }
Romain Guy06882f82009-06-10 13:36:04 -07005217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005218 public void systemReady() {
5219 mPolicy.systemReady();
5220 }
Romain Guy06882f82009-06-10 13:36:04 -07005221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005222 private final class InputDispatcherThread extends Thread {
5223 // Time to wait when there is nothing to do: 9999 seconds.
5224 static final int LONG_WAIT=9999*1000;
5225
5226 public InputDispatcherThread() {
5227 super("InputDispatcher");
5228 }
Romain Guy06882f82009-06-10 13:36:04 -07005229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005230 @Override
5231 public void run() {
5232 while (true) {
5233 try {
5234 process();
5235 } catch (Exception e) {
5236 Log.e(TAG, "Exception in input dispatcher", e);
5237 }
5238 }
5239 }
Romain Guy06882f82009-06-10 13:36:04 -07005240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005241 private void process() {
5242 android.os.Process.setThreadPriority(
5243 android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -07005244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005245 // The last key event we saw
5246 KeyEvent lastKey = null;
5247
5248 // Last keydown time for auto-repeating keys
5249 long lastKeyTime = SystemClock.uptimeMillis();
5250 long nextKeyTime = lastKeyTime+LONG_WAIT;
5251
Romain Guy06882f82009-06-10 13:36:04 -07005252 // How many successive repeats we generated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005253 int keyRepeatCount = 0;
5254
5255 // Need to report that configuration has changed?
5256 boolean configChanged = false;
Romain Guy06882f82009-06-10 13:36:04 -07005257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005258 while (true) {
5259 long curTime = SystemClock.uptimeMillis();
5260
5261 if (DEBUG_INPUT) Log.v(
5262 TAG, "Waiting for next key: now=" + curTime
5263 + ", repeat @ " + nextKeyTime);
5264
5265 // Retrieve next event, waiting only as long as the next
5266 // repeat timeout. If the configuration has changed, then
5267 // don't wait at all -- we'll report the change as soon as
5268 // we have processed all events.
5269 QueuedEvent ev = mQueue.getEvent(
5270 (int)((!configChanged && curTime < nextKeyTime)
5271 ? (nextKeyTime-curTime) : 0));
5272
5273 if (DEBUG_INPUT && ev != null) Log.v(
5274 TAG, "Event: type=" + ev.classType + " data=" + ev.event);
5275
5276 try {
5277 if (ev != null) {
5278 curTime = ev.when;
5279 int eventType;
5280 if (ev.classType == RawInputEvent.CLASS_TOUCHSCREEN) {
5281 eventType = eventType((MotionEvent)ev.event);
5282 } else if (ev.classType == RawInputEvent.CLASS_KEYBOARD ||
5283 ev.classType == RawInputEvent.CLASS_TRACKBALL) {
5284 eventType = LocalPowerManager.BUTTON_EVENT;
5285 } else {
5286 eventType = LocalPowerManager.OTHER_EVENT;
5287 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07005288 try {
Michael Chane96440f2009-05-06 10:27:36 -07005289 long now = SystemClock.uptimeMillis();
5290
5291 if ((now - mLastBatteryStatsCallTime)
5292 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
5293 mLastBatteryStatsCallTime = now;
5294 mBatteryStats.noteInputEvent();
5295 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07005296 } catch (RemoteException e) {
5297 // Ignore
5298 }
Michael Chane96440f2009-05-06 10:27:36 -07005299 mPowerManager.userActivity(curTime, false, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005300 switch (ev.classType) {
5301 case RawInputEvent.CLASS_KEYBOARD:
5302 KeyEvent ke = (KeyEvent)ev.event;
5303 if (ke.isDown()) {
5304 lastKey = ke;
5305 keyRepeatCount = 0;
5306 lastKeyTime = curTime;
5307 nextKeyTime = lastKeyTime
5308 + KEY_REPEAT_FIRST_DELAY;
5309 if (DEBUG_INPUT) Log.v(
5310 TAG, "Received key down: first repeat @ "
5311 + nextKeyTime);
5312 } else {
5313 lastKey = null;
5314 // Arbitrary long timeout.
5315 lastKeyTime = curTime;
5316 nextKeyTime = curTime + LONG_WAIT;
5317 if (DEBUG_INPUT) Log.v(
5318 TAG, "Received key up: ignore repeat @ "
5319 + nextKeyTime);
5320 }
5321 dispatchKey((KeyEvent)ev.event, 0, 0);
5322 mQueue.recycleEvent(ev);
5323 break;
5324 case RawInputEvent.CLASS_TOUCHSCREEN:
5325 //Log.i(TAG, "Read next event " + ev);
5326 dispatchPointer(ev, (MotionEvent)ev.event, 0, 0);
5327 break;
5328 case RawInputEvent.CLASS_TRACKBALL:
5329 dispatchTrackball(ev, (MotionEvent)ev.event, 0, 0);
5330 break;
5331 case RawInputEvent.CLASS_CONFIGURATION_CHANGED:
5332 configChanged = true;
5333 break;
5334 default:
5335 mQueue.recycleEvent(ev);
5336 break;
5337 }
Romain Guy06882f82009-06-10 13:36:04 -07005338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005339 } else if (configChanged) {
5340 configChanged = false;
5341 sendNewConfiguration();
Romain Guy06882f82009-06-10 13:36:04 -07005342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005343 } else if (lastKey != null) {
5344 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07005345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005346 // Timeout occurred while key was down. If it is at or
5347 // past the key repeat time, dispatch the repeat.
5348 if (DEBUG_INPUT) Log.v(
5349 TAG, "Key timeout: repeat=" + nextKeyTime
5350 + ", now=" + curTime);
5351 if (curTime < nextKeyTime) {
5352 continue;
5353 }
Romain Guy06882f82009-06-10 13:36:04 -07005354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005355 lastKeyTime = nextKeyTime;
5356 nextKeyTime = nextKeyTime + KEY_REPEAT_DELAY;
5357 keyRepeatCount++;
5358 if (DEBUG_INPUT) Log.v(
5359 TAG, "Key repeat: count=" + keyRepeatCount
5360 + ", next @ " + nextKeyTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07005361 dispatchKey(KeyEvent.changeTimeRepeat(lastKey, curTime, keyRepeatCount), 0, 0);
Romain Guy06882f82009-06-10 13:36:04 -07005362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005363 } else {
5364 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07005365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005366 lastKeyTime = curTime;
5367 nextKeyTime = curTime + LONG_WAIT;
5368 }
Romain Guy06882f82009-06-10 13:36:04 -07005369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005370 } catch (Exception e) {
5371 Log.e(TAG,
5372 "Input thread received uncaught exception: " + e, e);
5373 }
5374 }
5375 }
5376 }
5377
5378 // -------------------------------------------------------------
5379 // Client Session State
5380 // -------------------------------------------------------------
5381
5382 private final class Session extends IWindowSession.Stub
5383 implements IBinder.DeathRecipient {
5384 final IInputMethodClient mClient;
5385 final IInputContext mInputContext;
5386 final int mUid;
5387 final int mPid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005388 final String mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005389 SurfaceSession mSurfaceSession;
5390 int mNumWindow = 0;
5391 boolean mClientDead = false;
Romain Guy06882f82009-06-10 13:36:04 -07005392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005393 /**
5394 * Current pointer move event being dispatched to client window... must
5395 * hold key lock to access.
5396 */
5397 QueuedEvent mPendingPointerMove;
5398 WindowState mPendingPointerWindow;
Romain Guy06882f82009-06-10 13:36:04 -07005399
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005400 /**
5401 * Current trackball move event being dispatched to client window... must
5402 * hold key lock to access.
5403 */
5404 QueuedEvent mPendingTrackballMove;
5405 WindowState mPendingTrackballWindow;
5406
5407 public Session(IInputMethodClient client, IInputContext inputContext) {
5408 mClient = client;
5409 mInputContext = inputContext;
5410 mUid = Binder.getCallingUid();
5411 mPid = Binder.getCallingPid();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005412 StringBuilder sb = new StringBuilder();
5413 sb.append("Session{");
5414 sb.append(Integer.toHexString(System.identityHashCode(this)));
5415 sb.append(" uid ");
5416 sb.append(mUid);
5417 sb.append("}");
5418 mStringName = sb.toString();
Romain Guy06882f82009-06-10 13:36:04 -07005419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005420 synchronized (mWindowMap) {
5421 if (mInputMethodManager == null && mHaveInputMethods) {
5422 IBinder b = ServiceManager.getService(
5423 Context.INPUT_METHOD_SERVICE);
5424 mInputMethodManager = IInputMethodManager.Stub.asInterface(b);
5425 }
5426 }
5427 long ident = Binder.clearCallingIdentity();
5428 try {
5429 // Note: it is safe to call in to the input method manager
5430 // here because we are not holding our lock.
5431 if (mInputMethodManager != null) {
5432 mInputMethodManager.addClient(client, inputContext,
5433 mUid, mPid);
5434 } else {
5435 client.setUsingInputMethod(false);
5436 }
5437 client.asBinder().linkToDeath(this, 0);
5438 } catch (RemoteException e) {
5439 // The caller has died, so we can just forget about this.
5440 try {
5441 if (mInputMethodManager != null) {
5442 mInputMethodManager.removeClient(client);
5443 }
5444 } catch (RemoteException ee) {
5445 }
5446 } finally {
5447 Binder.restoreCallingIdentity(ident);
5448 }
5449 }
Romain Guy06882f82009-06-10 13:36:04 -07005450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005451 @Override
5452 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
5453 throws RemoteException {
5454 try {
5455 return super.onTransact(code, data, reply, flags);
5456 } catch (RuntimeException e) {
5457 // Log all 'real' exceptions thrown to the caller
5458 if (!(e instanceof SecurityException)) {
5459 Log.e(TAG, "Window Session Crash", e);
5460 }
5461 throw e;
5462 }
5463 }
5464
5465 public void binderDied() {
5466 // Note: it is safe to call in to the input method manager
5467 // here because we are not holding our lock.
5468 try {
5469 if (mInputMethodManager != null) {
5470 mInputMethodManager.removeClient(mClient);
5471 }
5472 } catch (RemoteException e) {
5473 }
5474 synchronized(mWindowMap) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07005475 mClient.asBinder().unlinkToDeath(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005476 mClientDead = true;
5477 killSessionLocked();
5478 }
5479 }
5480
5481 public int add(IWindow window, WindowManager.LayoutParams attrs,
5482 int viewVisibility, Rect outContentInsets) {
5483 return addWindow(this, window, attrs, viewVisibility, outContentInsets);
5484 }
Romain Guy06882f82009-06-10 13:36:04 -07005485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005486 public void remove(IWindow window) {
5487 removeWindow(this, window);
5488 }
Romain Guy06882f82009-06-10 13:36:04 -07005489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005490 public int relayout(IWindow window, WindowManager.LayoutParams attrs,
5491 int requestedWidth, int requestedHeight, int viewFlags,
5492 boolean insetsPending, Rect outFrame, Rect outContentInsets,
5493 Rect outVisibleInsets, Surface outSurface) {
5494 return relayoutWindow(this, window, attrs,
5495 requestedWidth, requestedHeight, viewFlags, insetsPending,
5496 outFrame, outContentInsets, outVisibleInsets, outSurface);
5497 }
Romain Guy06882f82009-06-10 13:36:04 -07005498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005499 public void setTransparentRegion(IWindow window, Region region) {
5500 setTransparentRegionWindow(this, window, region);
5501 }
Romain Guy06882f82009-06-10 13:36:04 -07005502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005503 public void setInsets(IWindow window, int touchableInsets,
5504 Rect contentInsets, Rect visibleInsets) {
5505 setInsetsWindow(this, window, touchableInsets, contentInsets,
5506 visibleInsets);
5507 }
Romain Guy06882f82009-06-10 13:36:04 -07005508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005509 public void getDisplayFrame(IWindow window, Rect outDisplayFrame) {
5510 getWindowDisplayFrame(this, window, outDisplayFrame);
5511 }
Romain Guy06882f82009-06-10 13:36:04 -07005512
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005513 public void finishDrawing(IWindow window) {
5514 if (localLOGV) Log.v(
5515 TAG, "IWindow finishDrawing called for " + window);
5516 finishDrawingWindow(this, window);
5517 }
5518
5519 public void finishKey(IWindow window) {
5520 if (localLOGV) Log.v(
5521 TAG, "IWindow finishKey called for " + window);
5522 mKeyWaiter.finishedKey(this, window, false,
5523 KeyWaiter.RETURN_NOTHING);
5524 }
5525
5526 public MotionEvent getPendingPointerMove(IWindow window) {
5527 if (localLOGV) Log.v(
5528 TAG, "IWindow getPendingMotionEvent called for " + window);
5529 return mKeyWaiter.finishedKey(this, window, false,
5530 KeyWaiter.RETURN_PENDING_POINTER);
5531 }
Romain Guy06882f82009-06-10 13:36:04 -07005532
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005533 public MotionEvent getPendingTrackballMove(IWindow window) {
5534 if (localLOGV) Log.v(
5535 TAG, "IWindow getPendingMotionEvent called for " + window);
5536 return mKeyWaiter.finishedKey(this, window, false,
5537 KeyWaiter.RETURN_PENDING_TRACKBALL);
5538 }
5539
5540 public void setInTouchMode(boolean mode) {
5541 synchronized(mWindowMap) {
5542 mInTouchMode = mode;
5543 }
5544 }
5545
5546 public boolean getInTouchMode() {
5547 synchronized(mWindowMap) {
5548 return mInTouchMode;
5549 }
5550 }
5551
5552 public boolean performHapticFeedback(IWindow window, int effectId,
5553 boolean always) {
5554 synchronized(mWindowMap) {
5555 long ident = Binder.clearCallingIdentity();
5556 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005557 return mPolicy.performHapticFeedbackLw(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005558 windowForClientLocked(this, window), effectId, always);
5559 } finally {
5560 Binder.restoreCallingIdentity(ident);
5561 }
5562 }
5563 }
Romain Guy06882f82009-06-10 13:36:04 -07005564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005565 void windowAddedLocked() {
5566 if (mSurfaceSession == null) {
5567 if (localLOGV) Log.v(
5568 TAG, "First window added to " + this + ", creating SurfaceSession");
5569 mSurfaceSession = new SurfaceSession();
5570 mSessions.add(this);
5571 }
5572 mNumWindow++;
5573 }
5574
5575 void windowRemovedLocked() {
5576 mNumWindow--;
5577 killSessionLocked();
5578 }
Romain Guy06882f82009-06-10 13:36:04 -07005579
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005580 void killSessionLocked() {
5581 if (mNumWindow <= 0 && mClientDead) {
5582 mSessions.remove(this);
5583 if (mSurfaceSession != null) {
5584 if (localLOGV) Log.v(
5585 TAG, "Last window removed from " + this
5586 + ", destroying " + mSurfaceSession);
5587 try {
5588 mSurfaceSession.kill();
5589 } catch (Exception e) {
5590 Log.w(TAG, "Exception thrown when killing surface session "
5591 + mSurfaceSession + " in session " + this
5592 + ": " + e.toString());
5593 }
5594 mSurfaceSession = null;
5595 }
5596 }
5597 }
Romain Guy06882f82009-06-10 13:36:04 -07005598
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005599 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005600 pw.print(prefix); pw.print("mNumWindow="); pw.print(mNumWindow);
5601 pw.print(" mClientDead="); pw.print(mClientDead);
5602 pw.print(" mSurfaceSession="); pw.println(mSurfaceSession);
5603 if (mPendingPointerWindow != null || mPendingPointerMove != null) {
5604 pw.print(prefix);
5605 pw.print("mPendingPointerWindow="); pw.print(mPendingPointerWindow);
5606 pw.print(" mPendingPointerMove="); pw.println(mPendingPointerMove);
5607 }
5608 if (mPendingTrackballWindow != null || mPendingTrackballMove != null) {
5609 pw.print(prefix);
5610 pw.print("mPendingTrackballWindow="); pw.print(mPendingTrackballWindow);
5611 pw.print(" mPendingTrackballMove="); pw.println(mPendingTrackballMove);
5612 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005613 }
5614
5615 @Override
5616 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005617 return mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005618 }
5619 }
5620
5621 // -------------------------------------------------------------
5622 // Client Window State
5623 // -------------------------------------------------------------
5624
5625 private final class WindowState implements WindowManagerPolicy.WindowState {
5626 final Session mSession;
5627 final IWindow mClient;
5628 WindowToken mToken;
The Android Open Source Project10592532009-03-18 17:39:46 -07005629 WindowToken mRootToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005630 AppWindowToken mAppToken;
5631 AppWindowToken mTargetAppToken;
5632 final WindowManager.LayoutParams mAttrs = new WindowManager.LayoutParams();
5633 final DeathRecipient mDeathRecipient;
5634 final WindowState mAttachedWindow;
5635 final ArrayList mChildWindows = new ArrayList();
5636 final int mBaseLayer;
5637 final int mSubLayer;
5638 final boolean mLayoutAttached;
5639 final boolean mIsImWindow;
5640 int mViewVisibility;
5641 boolean mPolicyVisibility = true;
5642 boolean mPolicyVisibilityAfterAnim = true;
5643 boolean mAppFreezing;
5644 Surface mSurface;
5645 boolean mAttachedHidden; // is our parent window hidden?
5646 boolean mLastHidden; // was this window last hidden?
5647 int mRequestedWidth;
5648 int mRequestedHeight;
5649 int mLastRequestedWidth;
5650 int mLastRequestedHeight;
5651 int mReqXPos;
5652 int mReqYPos;
5653 int mLayer;
5654 int mAnimLayer;
5655 int mLastLayer;
5656 boolean mHaveFrame;
5657
5658 WindowState mNextOutsideTouch;
Romain Guy06882f82009-06-10 13:36:04 -07005659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005660 // Actual frame shown on-screen (may be modified by animation)
5661 final Rect mShownFrame = new Rect();
5662 final Rect mLastShownFrame = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07005663
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005664 /**
5665 * Insets that determine the actually visible area
5666 */
5667 final Rect mVisibleInsets = new Rect();
5668 final Rect mLastVisibleInsets = new Rect();
5669 boolean mVisibleInsetsChanged;
5670
5671 /**
5672 * Insets that are covered by system windows
5673 */
5674 final Rect mContentInsets = new Rect();
5675 final Rect mLastContentInsets = new Rect();
5676 boolean mContentInsetsChanged;
5677
5678 /**
5679 * Set to true if we are waiting for this window to receive its
5680 * given internal insets before laying out other windows based on it.
5681 */
5682 boolean mGivenInsetsPending;
Romain Guy06882f82009-06-10 13:36:04 -07005683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005684 /**
5685 * These are the content insets that were given during layout for
5686 * this window, to be applied to windows behind it.
5687 */
5688 final Rect mGivenContentInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07005689
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005690 /**
5691 * These are the visible insets that were given during layout for
5692 * this window, to be applied to windows behind it.
5693 */
5694 final Rect mGivenVisibleInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07005695
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005696 /**
5697 * Flag indicating whether the touchable region should be adjusted by
5698 * the visible insets; if false the area outside the visible insets is
5699 * NOT touchable, so we must use those to adjust the frame during hit
5700 * tests.
5701 */
5702 int mTouchableInsets = ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME;
Romain Guy06882f82009-06-10 13:36:04 -07005703
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005704 // Current transformation being applied.
5705 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
5706 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
5707 float mHScale=1, mVScale=1;
5708 float mLastHScale=1, mLastVScale=1;
5709 final Matrix mTmpMatrix = new Matrix();
5710
5711 // "Real" frame that the application sees.
5712 final Rect mFrame = new Rect();
5713 final Rect mLastFrame = new Rect();
5714
5715 final Rect mContainingFrame = new Rect();
5716 final Rect mDisplayFrame = new Rect();
5717 final Rect mContentFrame = new Rect();
5718 final Rect mVisibleFrame = new Rect();
5719
5720 float mShownAlpha = 1;
5721 float mAlpha = 1;
5722 float mLastAlpha = 1;
5723
5724 // Set to true if, when the window gets displayed, it should perform
5725 // an enter animation.
5726 boolean mEnterAnimationPending;
5727
5728 // Currently running animation.
5729 boolean mAnimating;
5730 boolean mLocalAnimating;
5731 Animation mAnimation;
5732 boolean mAnimationIsEntrance;
5733 boolean mHasTransformation;
5734 boolean mHasLocalTransformation;
5735 final Transformation mTransformation = new Transformation();
5736
5737 // This is set after IWindowSession.relayout() has been called at
5738 // least once for the window. It allows us to detect the situation
5739 // where we don't yet have a surface, but should have one soon, so
5740 // we can give the window focus before waiting for the relayout.
5741 boolean mRelayoutCalled;
Romain Guy06882f82009-06-10 13:36:04 -07005742
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005743 // This is set after the Surface has been created but before the
5744 // window has been drawn. During this time the surface is hidden.
5745 boolean mDrawPending;
5746
5747 // This is set after the window has finished drawing for the first
5748 // time but before its surface is shown. The surface will be
5749 // displayed when the next layout is run.
5750 boolean mCommitDrawPending;
5751
5752 // This is set during the time after the window's drawing has been
5753 // committed, and before its surface is actually shown. It is used
5754 // to delay showing the surface until all windows in a token are ready
5755 // to be shown.
5756 boolean mReadyToShow;
Romain Guy06882f82009-06-10 13:36:04 -07005757
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005758 // Set when the window has been shown in the screen the first time.
5759 boolean mHasDrawn;
5760
5761 // Currently running an exit animation?
5762 boolean mExiting;
5763
5764 // Currently on the mDestroySurface list?
5765 boolean mDestroying;
Romain Guy06882f82009-06-10 13:36:04 -07005766
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005767 // Completely remove from window manager after exit animation?
5768 boolean mRemoveOnExit;
5769
5770 // Set when the orientation is changing and this window has not yet
5771 // been updated for the new orientation.
5772 boolean mOrientationChanging;
Romain Guy06882f82009-06-10 13:36:04 -07005773
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005774 // Is this window now (or just being) removed?
5775 boolean mRemoved;
Romain Guy06882f82009-06-10 13:36:04 -07005776
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005777 WindowState(Session s, IWindow c, WindowToken token,
5778 WindowState attachedWindow, WindowManager.LayoutParams a,
5779 int viewVisibility) {
5780 mSession = s;
5781 mClient = c;
5782 mToken = token;
5783 mAttrs.copyFrom(a);
5784 mViewVisibility = viewVisibility;
5785 DeathRecipient deathRecipient = new DeathRecipient();
5786 mAlpha = a.alpha;
5787 if (localLOGV) Log.v(
5788 TAG, "Window " + this + " client=" + c.asBinder()
5789 + " token=" + token + " (" + mAttrs.token + ")");
5790 try {
5791 c.asBinder().linkToDeath(deathRecipient, 0);
5792 } catch (RemoteException e) {
5793 mDeathRecipient = null;
5794 mAttachedWindow = null;
5795 mLayoutAttached = false;
5796 mIsImWindow = false;
5797 mBaseLayer = 0;
5798 mSubLayer = 0;
5799 return;
5800 }
5801 mDeathRecipient = deathRecipient;
Romain Guy06882f82009-06-10 13:36:04 -07005802
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005803 if ((mAttrs.type >= FIRST_SUB_WINDOW &&
5804 mAttrs.type <= LAST_SUB_WINDOW)) {
5805 // The multiplier here is to reserve space for multiple
5806 // windows in the same type layer.
5807 mBaseLayer = mPolicy.windowTypeToLayerLw(
5808 attachedWindow.mAttrs.type) * TYPE_LAYER_MULTIPLIER
5809 + TYPE_LAYER_OFFSET;
5810 mSubLayer = mPolicy.subWindowTypeToLayerLw(a.type);
5811 mAttachedWindow = attachedWindow;
5812 mAttachedWindow.mChildWindows.add(this);
5813 mLayoutAttached = mAttrs.type !=
5814 WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
5815 mIsImWindow = attachedWindow.mAttrs.type == TYPE_INPUT_METHOD
5816 || attachedWindow.mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
5817 } else {
5818 // The multiplier here is to reserve space for multiple
5819 // windows in the same type layer.
5820 mBaseLayer = mPolicy.windowTypeToLayerLw(a.type)
5821 * TYPE_LAYER_MULTIPLIER
5822 + TYPE_LAYER_OFFSET;
5823 mSubLayer = 0;
5824 mAttachedWindow = null;
5825 mLayoutAttached = false;
5826 mIsImWindow = mAttrs.type == TYPE_INPUT_METHOD
5827 || mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
5828 }
5829
5830 WindowState appWin = this;
5831 while (appWin.mAttachedWindow != null) {
5832 appWin = mAttachedWindow;
5833 }
5834 WindowToken appToken = appWin.mToken;
5835 while (appToken.appWindowToken == null) {
5836 WindowToken parent = mTokenMap.get(appToken.token);
5837 if (parent == null || appToken == parent) {
5838 break;
5839 }
5840 appToken = parent;
5841 }
The Android Open Source Project10592532009-03-18 17:39:46 -07005842 mRootToken = appToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005843 mAppToken = appToken.appWindowToken;
5844
5845 mSurface = null;
5846 mRequestedWidth = 0;
5847 mRequestedHeight = 0;
5848 mLastRequestedWidth = 0;
5849 mLastRequestedHeight = 0;
5850 mReqXPos = 0;
5851 mReqYPos = 0;
5852 mLayer = 0;
5853 mAnimLayer = 0;
5854 mLastLayer = 0;
5855 }
5856
5857 void attach() {
5858 if (localLOGV) Log.v(
5859 TAG, "Attaching " + this + " token=" + mToken
5860 + ", list=" + mToken.windows);
5861 mSession.windowAddedLocked();
5862 }
5863
5864 public void computeFrameLw(Rect pf, Rect df, Rect cf, Rect vf) {
5865 mHaveFrame = true;
5866
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07005867 final Rect container = mContainingFrame;
5868 container.set(pf);
5869
5870 final Rect display = mDisplayFrame;
5871 display.set(df);
5872
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07005873 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07005874 container.intersect(mCompatibleScreenFrame);
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07005875 if ((mAttrs.flags & FLAG_LAYOUT_NO_LIMITS) == 0) {
5876 display.intersect(mCompatibleScreenFrame);
5877 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07005878 }
5879
5880 final int pw = container.right - container.left;
5881 final int ph = container.bottom - container.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005882
5883 int w,h;
5884 if ((mAttrs.flags & mAttrs.FLAG_SCALED) != 0) {
5885 w = mAttrs.width < 0 ? pw : mAttrs.width;
5886 h = mAttrs.height< 0 ? ph : mAttrs.height;
5887 } else {
5888 w = mAttrs.width == mAttrs.FILL_PARENT ? pw : mRequestedWidth;
5889 h = mAttrs.height== mAttrs.FILL_PARENT ? ph : mRequestedHeight;
5890 }
Romain Guy06882f82009-06-10 13:36:04 -07005891
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005892 final Rect content = mContentFrame;
5893 content.set(cf);
Romain Guy06882f82009-06-10 13:36:04 -07005894
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005895 final Rect visible = mVisibleFrame;
5896 visible.set(vf);
Romain Guy06882f82009-06-10 13:36:04 -07005897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005898 final Rect frame = mFrame;
Romain Guy06882f82009-06-10 13:36:04 -07005899
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005900 //System.out.println("In: w=" + w + " h=" + h + " container=" +
5901 // container + " x=" + mAttrs.x + " y=" + mAttrs.y);
5902
5903 Gravity.apply(mAttrs.gravity, w, h, container,
5904 (int) (mAttrs.x + mAttrs.horizontalMargin * pw),
5905 (int) (mAttrs.y + mAttrs.verticalMargin * ph), frame);
5906
5907 //System.out.println("Out: " + mFrame);
5908
5909 // Now make sure the window fits in the overall display.
5910 Gravity.applyDisplay(mAttrs.gravity, df, frame);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07005911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005912 // Make sure the content and visible frames are inside of the
5913 // final window frame.
5914 if (content.left < frame.left) content.left = frame.left;
5915 if (content.top < frame.top) content.top = frame.top;
5916 if (content.right > frame.right) content.right = frame.right;
5917 if (content.bottom > frame.bottom) content.bottom = frame.bottom;
5918 if (visible.left < frame.left) visible.left = frame.left;
5919 if (visible.top < frame.top) visible.top = frame.top;
5920 if (visible.right > frame.right) visible.right = frame.right;
5921 if (visible.bottom > frame.bottom) visible.bottom = frame.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07005922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005923 final Rect contentInsets = mContentInsets;
5924 contentInsets.left = content.left-frame.left;
5925 contentInsets.top = content.top-frame.top;
5926 contentInsets.right = frame.right-content.right;
5927 contentInsets.bottom = frame.bottom-content.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07005928
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005929 final Rect visibleInsets = mVisibleInsets;
5930 visibleInsets.left = visible.left-frame.left;
5931 visibleInsets.top = visible.top-frame.top;
5932 visibleInsets.right = frame.right-visible.right;
5933 visibleInsets.bottom = frame.bottom-visible.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07005934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005935 if (localLOGV) {
5936 //if ("com.google.android.youtube".equals(mAttrs.packageName)
5937 // && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
5938 Log.v(TAG, "Resolving (mRequestedWidth="
5939 + mRequestedWidth + ", mRequestedheight="
5940 + mRequestedHeight + ") to" + " (pw=" + pw + ", ph=" + ph
5941 + "): frame=" + mFrame.toShortString()
5942 + " ci=" + contentInsets.toShortString()
5943 + " vi=" + visibleInsets.toShortString());
5944 //}
5945 }
5946 }
Romain Guy06882f82009-06-10 13:36:04 -07005947
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005948 public Rect getFrameLw() {
5949 return mFrame;
5950 }
5951
5952 public Rect getShownFrameLw() {
5953 return mShownFrame;
5954 }
5955
5956 public Rect getDisplayFrameLw() {
5957 return mDisplayFrame;
5958 }
5959
5960 public Rect getContentFrameLw() {
5961 return mContentFrame;
5962 }
5963
5964 public Rect getVisibleFrameLw() {
5965 return mVisibleFrame;
5966 }
5967
5968 public boolean getGivenInsetsPendingLw() {
5969 return mGivenInsetsPending;
5970 }
5971
5972 public Rect getGivenContentInsetsLw() {
5973 return mGivenContentInsets;
5974 }
Romain Guy06882f82009-06-10 13:36:04 -07005975
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005976 public Rect getGivenVisibleInsetsLw() {
5977 return mGivenVisibleInsets;
5978 }
Romain Guy06882f82009-06-10 13:36:04 -07005979
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005980 public WindowManager.LayoutParams getAttrs() {
5981 return mAttrs;
5982 }
5983
5984 public int getSurfaceLayer() {
5985 return mLayer;
5986 }
Romain Guy06882f82009-06-10 13:36:04 -07005987
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005988 public IApplicationToken getAppToken() {
5989 return mAppToken != null ? mAppToken.appToken : null;
5990 }
5991
5992 public boolean hasAppShownWindows() {
5993 return mAppToken != null ? mAppToken.firstWindowDrawn : false;
5994 }
5995
5996 public boolean hasAppStartingIcon() {
5997 return mAppToken != null ? (mAppToken.startingData != null) : false;
5998 }
5999
6000 public WindowManagerPolicy.WindowState getAppStartingWindow() {
6001 return mAppToken != null ? mAppToken.startingWindow : null;
6002 }
6003
6004 public void setAnimation(Animation anim) {
6005 if (localLOGV) Log.v(
6006 TAG, "Setting animation in " + this + ": " + anim);
6007 mAnimating = false;
6008 mLocalAnimating = false;
6009 mAnimation = anim;
6010 mAnimation.restrictDuration(MAX_ANIMATION_DURATION);
6011 mAnimation.scaleCurrentDuration(mWindowAnimationScale);
6012 }
6013
6014 public void clearAnimation() {
6015 if (mAnimation != null) {
6016 mAnimating = true;
6017 mLocalAnimating = false;
6018 mAnimation = null;
6019 }
6020 }
Romain Guy06882f82009-06-10 13:36:04 -07006021
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006022 Surface createSurfaceLocked() {
6023 if (mSurface == null) {
6024 mDrawPending = true;
6025 mCommitDrawPending = false;
6026 mReadyToShow = false;
6027 if (mAppToken != null) {
6028 mAppToken.allDrawn = false;
6029 }
6030
6031 int flags = 0;
6032 if (mAttrs.memoryType == MEMORY_TYPE_HARDWARE) {
6033 flags |= Surface.HARDWARE;
6034 } else if (mAttrs.memoryType == MEMORY_TYPE_GPU) {
6035 flags |= Surface.GPU;
6036 } else if (mAttrs.memoryType == MEMORY_TYPE_PUSH_BUFFERS) {
6037 flags |= Surface.PUSH_BUFFERS;
6038 }
6039
6040 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
6041 flags |= Surface.SECURE;
6042 }
6043 if (DEBUG_VISIBILITY) Log.v(
6044 TAG, "Creating surface in session "
6045 + mSession.mSurfaceSession + " window " + this
6046 + " w=" + mFrame.width()
6047 + " h=" + mFrame.height() + " format="
6048 + mAttrs.format + " flags=" + flags);
6049
6050 int w = mFrame.width();
6051 int h = mFrame.height();
6052 if ((mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
6053 // for a scaled surface, we always want the requested
6054 // size.
6055 w = mRequestedWidth;
6056 h = mRequestedHeight;
6057 }
6058
6059 try {
6060 mSurface = new Surface(
Romain Guy06882f82009-06-10 13:36:04 -07006061 mSession.mSurfaceSession, mSession.mPid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006062 0, w, h, mAttrs.format, flags);
6063 } catch (Surface.OutOfResourcesException e) {
6064 Log.w(TAG, "OutOfResourcesException creating surface");
6065 reclaimSomeSurfaceMemoryLocked(this, "create");
6066 return null;
6067 } catch (Exception e) {
6068 Log.e(TAG, "Exception creating surface", e);
6069 return null;
6070 }
Romain Guy06882f82009-06-10 13:36:04 -07006071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006072 if (localLOGV) Log.v(
6073 TAG, "Got surface: " + mSurface
6074 + ", set left=" + mFrame.left + " top=" + mFrame.top
6075 + ", animLayer=" + mAnimLayer);
6076 if (SHOW_TRANSACTIONS) {
6077 Log.i(TAG, ">>> OPEN TRANSACTION");
6078 Log.i(TAG, " SURFACE " + mSurface + ": CREATE ("
6079 + mAttrs.getTitle() + ") pos=(" +
6080 mFrame.left + "," + mFrame.top + ") (" +
6081 mFrame.width() + "x" + mFrame.height() + "), layer=" +
6082 mAnimLayer + " HIDE");
6083 }
6084 Surface.openTransaction();
6085 try {
6086 try {
6087 mSurface.setPosition(mFrame.left, mFrame.top);
6088 mSurface.setLayer(mAnimLayer);
6089 mSurface.hide();
6090 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
6091 mSurface.setFlags(Surface.SURFACE_DITHER,
6092 Surface.SURFACE_DITHER);
6093 }
6094 } catch (RuntimeException e) {
6095 Log.w(TAG, "Error creating surface in " + w, e);
6096 reclaimSomeSurfaceMemoryLocked(this, "create-init");
6097 }
6098 mLastHidden = true;
6099 } finally {
6100 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
6101 Surface.closeTransaction();
6102 }
6103 if (localLOGV) Log.v(
6104 TAG, "Created surface " + this);
6105 }
6106 return mSurface;
6107 }
Romain Guy06882f82009-06-10 13:36:04 -07006108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006109 void destroySurfaceLocked() {
6110 // Window is no longer on-screen, so can no longer receive
6111 // key events... if we were waiting for it to finish
6112 // handling a key event, the wait is over!
6113 mKeyWaiter.finishedKey(mSession, mClient, true,
6114 KeyWaiter.RETURN_NOTHING);
6115 mKeyWaiter.releasePendingPointerLocked(mSession);
6116 mKeyWaiter.releasePendingTrackballLocked(mSession);
6117
6118 if (mAppToken != null && this == mAppToken.startingWindow) {
6119 mAppToken.startingDisplayed = false;
6120 }
Romain Guy06882f82009-06-10 13:36:04 -07006121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006122 if (localLOGV) Log.v(
6123 TAG, "Window " + this
6124 + " destroying surface " + mSurface + ", session " + mSession);
6125 if (mSurface != null) {
6126 try {
6127 if (SHOW_TRANSACTIONS) {
6128 RuntimeException ex = new RuntimeException();
6129 ex.fillInStackTrace();
6130 Log.i(TAG, " SURFACE " + mSurface + ": DESTROY ("
6131 + mAttrs.getTitle() + ")", ex);
6132 }
6133 mSurface.clear();
6134 } catch (RuntimeException e) {
6135 Log.w(TAG, "Exception thrown when destroying Window " + this
6136 + " surface " + mSurface + " session " + mSession
6137 + ": " + e.toString());
6138 }
6139 mSurface = null;
6140 mDrawPending = false;
6141 mCommitDrawPending = false;
6142 mReadyToShow = false;
6143
6144 int i = mChildWindows.size();
6145 while (i > 0) {
6146 i--;
6147 WindowState c = (WindowState)mChildWindows.get(i);
6148 c.mAttachedHidden = true;
6149 }
6150 }
6151 }
6152
6153 boolean finishDrawingLocked() {
6154 if (mDrawPending) {
6155 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.v(
6156 TAG, "finishDrawingLocked: " + mSurface);
6157 mCommitDrawPending = true;
6158 mDrawPending = false;
6159 return true;
6160 }
6161 return false;
6162 }
6163
6164 // This must be called while inside a transaction.
6165 void commitFinishDrawingLocked(long currentTime) {
6166 //Log.i(TAG, "commitFinishDrawingLocked: " + mSurface);
6167 if (!mCommitDrawPending) {
6168 return;
6169 }
6170 mCommitDrawPending = false;
6171 mReadyToShow = true;
6172 final boolean starting = mAttrs.type == TYPE_APPLICATION_STARTING;
6173 final AppWindowToken atoken = mAppToken;
6174 if (atoken == null || atoken.allDrawn || starting) {
6175 performShowLocked();
6176 }
6177 }
6178
6179 // This must be called while inside a transaction.
6180 boolean performShowLocked() {
6181 if (DEBUG_VISIBILITY) {
6182 RuntimeException e = new RuntimeException();
6183 e.fillInStackTrace();
6184 Log.v(TAG, "performShow on " + this
6185 + ": readyToShow=" + mReadyToShow + " readyForDisplay=" + isReadyForDisplay()
6186 + " starting=" + (mAttrs.type == TYPE_APPLICATION_STARTING), e);
6187 }
6188 if (mReadyToShow && isReadyForDisplay()) {
6189 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.i(
6190 TAG, " SURFACE " + mSurface + ": SHOW (performShowLocked)");
6191 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + this
6192 + " during animation: policyVis=" + mPolicyVisibility
6193 + " attHidden=" + mAttachedHidden
6194 + " tok.hiddenRequested="
6195 + (mAppToken != null ? mAppToken.hiddenRequested : false)
6196 + " tok.idden="
6197 + (mAppToken != null ? mAppToken.hidden : false)
6198 + " animating=" + mAnimating
6199 + " tok animating="
6200 + (mAppToken != null ? mAppToken.animating : false));
6201 if (!showSurfaceRobustlyLocked(this)) {
6202 return false;
6203 }
6204 mLastAlpha = -1;
6205 mHasDrawn = true;
6206 mLastHidden = false;
6207 mReadyToShow = false;
6208 enableScreenIfNeededLocked();
6209
6210 applyEnterAnimationLocked(this);
Romain Guy06882f82009-06-10 13:36:04 -07006211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006212 int i = mChildWindows.size();
6213 while (i > 0) {
6214 i--;
6215 WindowState c = (WindowState)mChildWindows.get(i);
6216 if (c.mSurface != null && c.mAttachedHidden) {
6217 c.mAttachedHidden = false;
6218 c.performShowLocked();
6219 }
6220 }
Romain Guy06882f82009-06-10 13:36:04 -07006221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006222 if (mAttrs.type != TYPE_APPLICATION_STARTING
6223 && mAppToken != null) {
6224 mAppToken.firstWindowDrawn = true;
6225 if (mAnimation == null && mAppToken.startingData != null) {
6226 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
6227 + mToken
6228 + ": first real window is shown, no animation");
6229 mFinishedStarting.add(mAppToken);
6230 mH.sendEmptyMessage(H.FINISHED_STARTING);
6231 }
6232 mAppToken.updateReportedVisibilityLocked();
6233 }
6234 }
6235 return true;
6236 }
Romain Guy06882f82009-06-10 13:36:04 -07006237
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006238 // This must be called while inside a transaction. Returns true if
6239 // there is more animation to run.
6240 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
6241 if (!mDisplayFrozen) {
6242 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07006243
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006244 if (!mDrawPending && !mCommitDrawPending && mAnimation != null) {
6245 mHasTransformation = true;
6246 mHasLocalTransformation = true;
6247 if (!mLocalAnimating) {
6248 if (DEBUG_ANIM) Log.v(
6249 TAG, "Starting animation in " + this +
6250 " @ " + currentTime + ": ww=" + mFrame.width() + " wh=" + mFrame.height() +
6251 " dw=" + dw + " dh=" + dh + " scale=" + mWindowAnimationScale);
6252 mAnimation.initialize(mFrame.width(), mFrame.height(), dw, dh);
6253 mAnimation.setStartTime(currentTime);
6254 mLocalAnimating = true;
6255 mAnimating = true;
6256 }
6257 mTransformation.clear();
6258 final boolean more = mAnimation.getTransformation(
6259 currentTime, mTransformation);
6260 if (DEBUG_ANIM) Log.v(
6261 TAG, "Stepped animation in " + this +
6262 ": more=" + more + ", xform=" + mTransformation);
6263 if (more) {
6264 // we're not done!
6265 return true;
6266 }
6267 if (DEBUG_ANIM) Log.v(
6268 TAG, "Finished animation in " + this +
6269 " @ " + currentTime);
6270 mAnimation = null;
6271 //WindowManagerService.this.dump();
6272 }
6273 mHasLocalTransformation = false;
6274 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppToken != null
6275 && mAppToken.hasTransformation) {
6276 // When our app token is animating, we kind-of pretend like
6277 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
6278 // part of this check means that we will only do this if
6279 // our window is not currently exiting, or it is not
6280 // locally animating itself. The idea being that one that
6281 // is exiting and doing a local animation should be removed
6282 // once that animation is done.
6283 mAnimating = true;
6284 mHasTransformation = true;
6285 mTransformation.clear();
6286 return false;
6287 } else if (mHasTransformation) {
6288 // Little trick to get through the path below to act like
6289 // we have finished an animation.
6290 mAnimating = true;
6291 } else if (isAnimating()) {
6292 mAnimating = true;
6293 }
6294 } else if (mAnimation != null) {
6295 // If the display is frozen, and there is a pending animation,
6296 // clear it and make sure we run the cleanup code.
6297 mAnimating = true;
6298 mLocalAnimating = true;
6299 mAnimation = null;
6300 }
Romain Guy06882f82009-06-10 13:36:04 -07006301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006302 if (!mAnimating && !mLocalAnimating) {
6303 return false;
6304 }
6305
6306 if (DEBUG_ANIM) Log.v(
6307 TAG, "Animation done in " + this + ": exiting=" + mExiting
6308 + ", reportedVisible="
6309 + (mAppToken != null ? mAppToken.reportedVisible : false));
Romain Guy06882f82009-06-10 13:36:04 -07006310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006311 mAnimating = false;
6312 mLocalAnimating = false;
6313 mAnimation = null;
6314 mAnimLayer = mLayer;
6315 if (mIsImWindow) {
6316 mAnimLayer += mInputMethodAnimLayerAdjustment;
6317 }
6318 if (DEBUG_LAYERS) Log.v(TAG, "Stepping win " + this
6319 + " anim layer: " + mAnimLayer);
6320 mHasTransformation = false;
6321 mHasLocalTransformation = false;
6322 mPolicyVisibility = mPolicyVisibilityAfterAnim;
6323 mTransformation.clear();
6324 if (mHasDrawn
6325 && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
6326 && mAppToken != null
6327 && mAppToken.firstWindowDrawn
6328 && mAppToken.startingData != null) {
6329 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
6330 + mToken + ": first real window done animating");
6331 mFinishedStarting.add(mAppToken);
6332 mH.sendEmptyMessage(H.FINISHED_STARTING);
6333 }
Romain Guy06882f82009-06-10 13:36:04 -07006334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006335 finishExit();
6336
6337 if (mAppToken != null) {
6338 mAppToken.updateReportedVisibilityLocked();
6339 }
6340
6341 return false;
6342 }
6343
6344 void finishExit() {
6345 if (DEBUG_ANIM) Log.v(
6346 TAG, "finishExit in " + this
6347 + ": exiting=" + mExiting
6348 + " remove=" + mRemoveOnExit
6349 + " windowAnimating=" + isWindowAnimating());
Romain Guy06882f82009-06-10 13:36:04 -07006350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006351 final int N = mChildWindows.size();
6352 for (int i=0; i<N; i++) {
6353 ((WindowState)mChildWindows.get(i)).finishExit();
6354 }
Romain Guy06882f82009-06-10 13:36:04 -07006355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006356 if (!mExiting) {
6357 return;
6358 }
Romain Guy06882f82009-06-10 13:36:04 -07006359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006360 if (isWindowAnimating()) {
6361 return;
6362 }
6363
6364 if (localLOGV) Log.v(
6365 TAG, "Exit animation finished in " + this
6366 + ": remove=" + mRemoveOnExit);
6367 if (mSurface != null) {
6368 mDestroySurface.add(this);
6369 mDestroying = true;
6370 if (SHOW_TRANSACTIONS) Log.i(
6371 TAG, " SURFACE " + mSurface + ": HIDE (finishExit)");
6372 try {
6373 mSurface.hide();
6374 } catch (RuntimeException e) {
6375 Log.w(TAG, "Error hiding surface in " + this, e);
6376 }
6377 mLastHidden = true;
6378 mKeyWaiter.releasePendingPointerLocked(mSession);
6379 }
6380 mExiting = false;
6381 if (mRemoveOnExit) {
6382 mPendingRemove.add(this);
6383 mRemoveOnExit = false;
6384 }
6385 }
Romain Guy06882f82009-06-10 13:36:04 -07006386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006387 boolean isIdentityMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
6388 if (dsdx < .99999f || dsdx > 1.00001f) return false;
6389 if (dtdy < .99999f || dtdy > 1.00001f) return false;
6390 if (dtdx < -.000001f || dtdx > .000001f) return false;
6391 if (dsdy < -.000001f || dsdy > .000001f) return false;
6392 return true;
6393 }
Romain Guy06882f82009-06-10 13:36:04 -07006394
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006395 void computeShownFrameLocked() {
6396 final boolean selfTransformation = mHasLocalTransformation;
6397 Transformation attachedTransformation =
6398 (mAttachedWindow != null && mAttachedWindow.mHasLocalTransformation)
6399 ? mAttachedWindow.mTransformation : null;
6400 Transformation appTransformation =
6401 (mAppToken != null && mAppToken.hasTransformation)
6402 ? mAppToken.transformation : null;
6403 if (selfTransformation || attachedTransformation != null
6404 || appTransformation != null) {
Romain Guy06882f82009-06-10 13:36:04 -07006405 // cache often used attributes locally
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006406 final Rect frame = mFrame;
6407 final float tmpFloats[] = mTmpFloats;
6408 final Matrix tmpMatrix = mTmpMatrix;
6409
6410 // Compute the desired transformation.
6411 tmpMatrix.setTranslate(frame.left, frame.top);
6412 if (selfTransformation) {
6413 tmpMatrix.preConcat(mTransformation.getMatrix());
6414 }
6415 if (attachedTransformation != null) {
6416 tmpMatrix.preConcat(attachedTransformation.getMatrix());
6417 }
6418 if (appTransformation != null) {
6419 tmpMatrix.preConcat(appTransformation.getMatrix());
6420 }
6421
6422 // "convert" it into SurfaceFlinger's format
6423 // (a 2x2 matrix + an offset)
6424 // Here we must not transform the position of the surface
6425 // since it is already included in the transformation.
6426 //Log.i(TAG, "Transform: " + matrix);
Romain Guy06882f82009-06-10 13:36:04 -07006427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006428 tmpMatrix.getValues(tmpFloats);
6429 mDsDx = tmpFloats[Matrix.MSCALE_X];
6430 mDtDx = tmpFloats[Matrix.MSKEW_X];
6431 mDsDy = tmpFloats[Matrix.MSKEW_Y];
6432 mDtDy = tmpFloats[Matrix.MSCALE_Y];
6433 int x = (int)tmpFloats[Matrix.MTRANS_X];
6434 int y = (int)tmpFloats[Matrix.MTRANS_Y];
6435 int w = frame.width();
6436 int h = frame.height();
6437 mShownFrame.set(x, y, x+w, y+h);
6438
6439 // Now set the alpha... but because our current hardware
6440 // can't do alpha transformation on a non-opaque surface,
6441 // turn it off if we are running an animation that is also
6442 // transforming since it is more important to have that
6443 // animation be smooth.
6444 mShownAlpha = mAlpha;
6445 if (!mLimitedAlphaCompositing
6446 || (!PixelFormat.formatHasAlpha(mAttrs.format)
6447 || (isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
6448 && x == frame.left && y == frame.top))) {
6449 //Log.i(TAG, "Applying alpha transform");
6450 if (selfTransformation) {
6451 mShownAlpha *= mTransformation.getAlpha();
6452 }
6453 if (attachedTransformation != null) {
6454 mShownAlpha *= attachedTransformation.getAlpha();
6455 }
6456 if (appTransformation != null) {
6457 mShownAlpha *= appTransformation.getAlpha();
6458 }
6459 } else {
6460 //Log.i(TAG, "Not applying alpha transform");
6461 }
Romain Guy06882f82009-06-10 13:36:04 -07006462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006463 if (localLOGV) Log.v(
6464 TAG, "Continuing animation in " + this +
6465 ": " + mShownFrame +
6466 ", alpha=" + mTransformation.getAlpha());
6467 return;
6468 }
Romain Guy06882f82009-06-10 13:36:04 -07006469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006470 mShownFrame.set(mFrame);
6471 mShownAlpha = mAlpha;
6472 mDsDx = 1;
6473 mDtDx = 0;
6474 mDsDy = 0;
6475 mDtDy = 1;
6476 }
Romain Guy06882f82009-06-10 13:36:04 -07006477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006478 /**
6479 * Is this window visible? It is not visible if there is no
6480 * surface, or we are in the process of running an exit animation
6481 * that will remove the surface, or its app token has been hidden.
6482 */
6483 public boolean isVisibleLw() {
6484 final AppWindowToken atoken = mAppToken;
6485 return mSurface != null && mPolicyVisibility && !mAttachedHidden
6486 && (atoken == null || !atoken.hiddenRequested)
6487 && !mExiting && !mDestroying;
6488 }
6489
6490 /**
6491 * Is this window visible, ignoring its app token? It is not visible
6492 * if there is no surface, or we are in the process of running an exit animation
6493 * that will remove the surface.
6494 */
6495 public boolean isWinVisibleLw() {
6496 final AppWindowToken atoken = mAppToken;
6497 return mSurface != null && mPolicyVisibility && !mAttachedHidden
6498 && (atoken == null || !atoken.hiddenRequested || atoken.animating)
6499 && !mExiting && !mDestroying;
6500 }
6501
6502 /**
6503 * The same as isVisible(), but follows the current hidden state of
6504 * the associated app token, not the pending requested hidden state.
6505 */
6506 boolean isVisibleNow() {
6507 return mSurface != null && mPolicyVisibility && !mAttachedHidden
The Android Open Source Project10592532009-03-18 17:39:46 -07006508 && !mRootToken.hidden && !mExiting && !mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006509 }
6510
6511 /**
6512 * Same as isVisible(), but we also count it as visible between the
6513 * call to IWindowSession.add() and the first relayout().
6514 */
6515 boolean isVisibleOrAdding() {
6516 final AppWindowToken atoken = mAppToken;
6517 return (mSurface != null
6518 || (!mRelayoutCalled && mViewVisibility == View.VISIBLE))
6519 && mPolicyVisibility && !mAttachedHidden
6520 && (atoken == null || !atoken.hiddenRequested)
6521 && !mExiting && !mDestroying;
6522 }
6523
6524 /**
6525 * Is this window currently on-screen? It is on-screen either if it
6526 * is visible or it is currently running an animation before no longer
6527 * being visible.
6528 */
6529 boolean isOnScreen() {
6530 final AppWindowToken atoken = mAppToken;
6531 if (atoken != null) {
6532 return mSurface != null && mPolicyVisibility && !mDestroying
6533 && ((!mAttachedHidden && !atoken.hiddenRequested)
6534 || mAnimating || atoken.animating);
6535 } else {
6536 return mSurface != null && mPolicyVisibility && !mDestroying
6537 && (!mAttachedHidden || mAnimating);
6538 }
6539 }
Romain Guy06882f82009-06-10 13:36:04 -07006540
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006541 /**
6542 * Like isOnScreen(), but we don't return true if the window is part
6543 * of a transition that has not yet been started.
6544 */
6545 boolean isReadyForDisplay() {
6546 final AppWindowToken atoken = mAppToken;
6547 final boolean animating = atoken != null ? atoken.animating : false;
6548 return mSurface != null && mPolicyVisibility && !mDestroying
The Android Open Source Project10592532009-03-18 17:39:46 -07006549 && ((!mAttachedHidden && !mRootToken.hidden)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006550 || mAnimating || animating);
6551 }
6552
6553 /** Is the window or its container currently animating? */
6554 boolean isAnimating() {
6555 final WindowState attached = mAttachedWindow;
6556 final AppWindowToken atoken = mAppToken;
6557 return mAnimation != null
6558 || (attached != null && attached.mAnimation != null)
Romain Guy06882f82009-06-10 13:36:04 -07006559 || (atoken != null &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006560 (atoken.animation != null
6561 || atoken.inPendingTransaction));
6562 }
6563
6564 /** Is this window currently animating? */
6565 boolean isWindowAnimating() {
6566 return mAnimation != null;
6567 }
6568
6569 /**
6570 * Like isOnScreen, but returns false if the surface hasn't yet
6571 * been drawn.
6572 */
6573 public boolean isDisplayedLw() {
6574 final AppWindowToken atoken = mAppToken;
6575 return mSurface != null && mPolicyVisibility && !mDestroying
6576 && !mDrawPending && !mCommitDrawPending
6577 && ((!mAttachedHidden &&
6578 (atoken == null || !atoken.hiddenRequested))
6579 || mAnimating);
6580 }
6581
6582 public boolean fillsScreenLw(int screenWidth, int screenHeight,
6583 boolean shownFrame, boolean onlyOpaque) {
6584 if (mSurface == null) {
6585 return false;
6586 }
6587 if (mAppToken != null && !mAppToken.appFullscreen) {
6588 return false;
6589 }
6590 if (onlyOpaque && mAttrs.format != PixelFormat.OPAQUE) {
6591 return false;
6592 }
6593 final Rect frame = shownFrame ? mShownFrame : mFrame;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07006594
6595 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
6596 return frame.left <= mCompatibleScreenFrame.left &&
6597 frame.top <= mCompatibleScreenFrame.top &&
6598 frame.right >= mCompatibleScreenFrame.right &&
6599 frame.bottom >= mCompatibleScreenFrame.bottom;
6600 } else {
6601 return frame.left <= 0 && frame.top <= 0
6602 && frame.right >= screenWidth
6603 && frame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006604 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006605 }
Romain Guy06882f82009-06-10 13:36:04 -07006606
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006607 /**
6608 * Return true if the window is opaque and fully drawn.
6609 */
6610 boolean isOpaqueDrawn() {
6611 return mAttrs.format == PixelFormat.OPAQUE && mSurface != null
6612 && mAnimation == null && !mDrawPending && !mCommitDrawPending;
6613 }
6614
6615 boolean needsBackgroundFiller(int screenWidth, int screenHeight) {
6616 return
6617 // only if the application is requesting compatible window
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07006618 (mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0 &&
6619 // only if it's visible
6620 mHasDrawn && mViewVisibility == View.VISIBLE &&
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07006621 // and only if the application fills the compatible screen
6622 mFrame.left <= mCompatibleScreenFrame.left &&
6623 mFrame.top <= mCompatibleScreenFrame.top &&
6624 mFrame.right >= mCompatibleScreenFrame.right &&
6625 mFrame.bottom >= mCompatibleScreenFrame.bottom &&
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07006626 // and starting window do not need background filler
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07006627 mAttrs.type != mAttrs.TYPE_APPLICATION_STARTING;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006628 }
6629
6630 boolean isFullscreen(int screenWidth, int screenHeight) {
6631 return mFrame.left <= 0 && mFrame.top <= 0 &&
6632 mFrame.right >= screenWidth && mFrame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006633 }
6634
6635 void removeLocked() {
6636 if (mAttachedWindow != null) {
6637 mAttachedWindow.mChildWindows.remove(this);
6638 }
6639 destroySurfaceLocked();
6640 mSession.windowRemovedLocked();
6641 try {
6642 mClient.asBinder().unlinkToDeath(mDeathRecipient, 0);
6643 } catch (RuntimeException e) {
6644 // Ignore if it has already been removed (usually because
6645 // we are doing this as part of processing a death note.)
6646 }
6647 }
6648
6649 private class DeathRecipient implements IBinder.DeathRecipient {
6650 public void binderDied() {
6651 try {
6652 synchronized(mWindowMap) {
6653 WindowState win = windowForClientLocked(mSession, mClient);
6654 Log.i(TAG, "WIN DEATH: " + win);
6655 if (win != null) {
6656 removeWindowLocked(mSession, win);
6657 }
6658 }
6659 } catch (IllegalArgumentException ex) {
6660 // This will happen if the window has already been
6661 // removed.
6662 }
6663 }
6664 }
6665
6666 /** Returns true if this window desires key events. */
6667 public final boolean canReceiveKeys() {
6668 return isVisibleOrAdding()
6669 && (mViewVisibility == View.VISIBLE)
6670 && ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0);
6671 }
6672
6673 public boolean hasDrawnLw() {
6674 return mHasDrawn;
6675 }
6676
6677 public boolean showLw(boolean doAnimation) {
6678 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim) {
6679 mPolicyVisibility = true;
6680 mPolicyVisibilityAfterAnim = true;
6681 if (doAnimation) {
6682 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_ENTER, true);
6683 }
6684 requestAnimationLocked(0);
6685 return true;
6686 }
6687 return false;
6688 }
6689
6690 public boolean hideLw(boolean doAnimation) {
6691 boolean current = doAnimation ? mPolicyVisibilityAfterAnim
6692 : mPolicyVisibility;
6693 if (current) {
6694 if (doAnimation) {
6695 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_EXIT, false);
6696 if (mAnimation == null) {
6697 doAnimation = false;
6698 }
6699 }
6700 if (doAnimation) {
6701 mPolicyVisibilityAfterAnim = false;
6702 } else {
6703 mPolicyVisibilityAfterAnim = false;
6704 mPolicyVisibility = false;
6705 }
6706 requestAnimationLocked(0);
6707 return true;
6708 }
6709 return false;
6710 }
6711
6712 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006713 StringBuilder sb = new StringBuilder(64);
Romain Guy06882f82009-06-10 13:36:04 -07006714
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006715 pw.print(prefix); pw.print("mSession="); pw.print(mSession);
6716 pw.print(" mClient="); pw.println(mClient.asBinder());
6717 pw.print(prefix); pw.print("mAttrs="); pw.println(mAttrs);
6718 if (mAttachedWindow != null || mLayoutAttached) {
6719 pw.print(prefix); pw.print("mAttachedWindow="); pw.print(mAttachedWindow);
6720 pw.print(" mLayoutAttached="); pw.println(mLayoutAttached);
6721 }
6722 if (mIsImWindow) {
6723 pw.print(prefix); pw.print("mIsImWindow="); pw.println(mIsImWindow);
6724 }
6725 pw.print(prefix); pw.print("mBaseLayer="); pw.print(mBaseLayer);
6726 pw.print(" mSubLayer="); pw.print(mSubLayer);
6727 pw.print(" mAnimLayer="); pw.print(mLayer); pw.print("+");
6728 pw.print((mTargetAppToken != null ? mTargetAppToken.animLayerAdjustment
6729 : (mAppToken != null ? mAppToken.animLayerAdjustment : 0)));
6730 pw.print("="); pw.print(mAnimLayer);
6731 pw.print(" mLastLayer="); pw.println(mLastLayer);
6732 if (mSurface != null) {
6733 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
6734 }
6735 pw.print(prefix); pw.print("mToken="); pw.println(mToken);
6736 pw.print(prefix); pw.print("mRootToken="); pw.println(mRootToken);
6737 if (mAppToken != null) {
6738 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
6739 }
6740 if (mTargetAppToken != null) {
6741 pw.print(prefix); pw.print("mTargetAppToken="); pw.println(mTargetAppToken);
6742 }
6743 pw.print(prefix); pw.print("mViewVisibility=0x");
6744 pw.print(Integer.toHexString(mViewVisibility));
6745 pw.print(" mLastHidden="); pw.print(mLastHidden);
6746 pw.print(" mHaveFrame="); pw.println(mHaveFrame);
6747 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim || mAttachedHidden) {
6748 pw.print(prefix); pw.print("mPolicyVisibility=");
6749 pw.print(mPolicyVisibility);
6750 pw.print(" mPolicyVisibilityAfterAnim=");
6751 pw.print(mPolicyVisibilityAfterAnim);
6752 pw.print(" mAttachedHidden="); pw.println(mAttachedHidden);
6753 }
6754 pw.print(prefix); pw.print("Requested w="); pw.print(mRequestedWidth);
6755 pw.print(" h="); pw.print(mRequestedHeight);
6756 pw.print(" x="); pw.print(mReqXPos);
6757 pw.print(" y="); pw.println(mReqYPos);
6758 pw.print(prefix); pw.print("mGivenContentInsets=");
6759 mGivenContentInsets.printShortString(pw);
6760 pw.print(" mGivenVisibleInsets=");
6761 mGivenVisibleInsets.printShortString(pw);
6762 pw.println();
6763 if (mTouchableInsets != 0 || mGivenInsetsPending) {
6764 pw.print(prefix); pw.print("mTouchableInsets="); pw.print(mTouchableInsets);
6765 pw.print(" mGivenInsetsPending="); pw.println(mGivenInsetsPending);
6766 }
6767 pw.print(prefix); pw.print("mShownFrame=");
6768 mShownFrame.printShortString(pw);
6769 pw.print(" last="); mLastShownFrame.printShortString(pw);
6770 pw.println();
6771 pw.print(prefix); pw.print("mFrame="); mFrame.printShortString(pw);
6772 pw.print(" last="); mLastFrame.printShortString(pw);
6773 pw.println();
6774 pw.print(prefix); pw.print("mContainingFrame=");
6775 mContainingFrame.printShortString(pw);
6776 pw.print(" mDisplayFrame=");
6777 mDisplayFrame.printShortString(pw);
6778 pw.println();
6779 pw.print(prefix); pw.print("mContentFrame="); mContentFrame.printShortString(pw);
6780 pw.print(" mVisibleFrame="); mVisibleFrame.printShortString(pw);
6781 pw.println();
6782 pw.print(prefix); pw.print("mContentInsets="); mContentInsets.printShortString(pw);
6783 pw.print(" last="); mLastContentInsets.printShortString(pw);
6784 pw.print(" mVisibleInsets="); mVisibleInsets.printShortString(pw);
6785 pw.print(" last="); mLastVisibleInsets.printShortString(pw);
6786 pw.println();
6787 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
6788 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
6789 pw.print(" mAlpha="); pw.print(mAlpha);
6790 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
6791 }
6792 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
6793 || mAnimation != null) {
6794 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
6795 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
6796 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
6797 pw.print(" mAnimation="); pw.println(mAnimation);
6798 }
6799 if (mHasTransformation || mHasLocalTransformation) {
6800 pw.print(prefix); pw.print("XForm: has=");
6801 pw.print(mHasTransformation);
6802 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
6803 pw.print(" "); mTransformation.printShortString(pw);
6804 pw.println();
6805 }
6806 pw.print(prefix); pw.print("mDrawPending="); pw.print(mDrawPending);
6807 pw.print(" mCommitDrawPending="); pw.print(mCommitDrawPending);
6808 pw.print(" mReadyToShow="); pw.print(mReadyToShow);
6809 pw.print(" mHasDrawn="); pw.println(mHasDrawn);
6810 if (mExiting || mRemoveOnExit || mDestroying || mRemoved) {
6811 pw.print(prefix); pw.print("mExiting="); pw.print(mExiting);
6812 pw.print(" mRemoveOnExit="); pw.print(mRemoveOnExit);
6813 pw.print(" mDestroying="); pw.print(mDestroying);
6814 pw.print(" mRemoved="); pw.println(mRemoved);
6815 }
6816 if (mOrientationChanging || mAppFreezing) {
6817 pw.print(prefix); pw.print("mOrientationChanging=");
6818 pw.print(mOrientationChanging);
6819 pw.print(" mAppFreezing="); pw.println(mAppFreezing);
6820 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006821 }
6822
6823 @Override
6824 public String toString() {
6825 return "Window{"
6826 + Integer.toHexString(System.identityHashCode(this))
6827 + " " + mAttrs.getTitle() + " paused=" + mToken.paused + "}";
6828 }
6829 }
Romain Guy06882f82009-06-10 13:36:04 -07006830
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006831 // -------------------------------------------------------------
6832 // Window Token State
6833 // -------------------------------------------------------------
6834
6835 class WindowToken {
6836 // The actual token.
6837 final IBinder token;
6838
6839 // The type of window this token is for, as per WindowManager.LayoutParams.
6840 final int windowType;
Romain Guy06882f82009-06-10 13:36:04 -07006841
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006842 // Set if this token was explicitly added by a client, so should
6843 // not be removed when all windows are removed.
6844 final boolean explicit;
Romain Guy06882f82009-06-10 13:36:04 -07006845
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006846 // For printing.
6847 String stringName;
Romain Guy06882f82009-06-10 13:36:04 -07006848
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006849 // If this is an AppWindowToken, this is non-null.
6850 AppWindowToken appWindowToken;
Romain Guy06882f82009-06-10 13:36:04 -07006851
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006852 // All of the windows associated with this token.
6853 final ArrayList<WindowState> windows = new ArrayList<WindowState>();
6854
6855 // Is key dispatching paused for this token?
6856 boolean paused = false;
6857
6858 // Should this token's windows be hidden?
6859 boolean hidden;
6860
6861 // Temporary for finding which tokens no longer have visible windows.
6862 boolean hasVisible;
6863
6864 WindowToken(IBinder _token, int type, boolean _explicit) {
6865 token = _token;
6866 windowType = type;
6867 explicit = _explicit;
6868 }
6869
6870 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006871 pw.print(prefix); pw.print("token="); pw.println(token);
6872 pw.print(prefix); pw.print("windows="); pw.println(windows);
6873 pw.print(prefix); pw.print("windowType="); pw.print(windowType);
6874 pw.print(" hidden="); pw.print(hidden);
6875 pw.print(" hasVisible="); pw.println(hasVisible);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006876 }
6877
6878 @Override
6879 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006880 if (stringName == null) {
6881 StringBuilder sb = new StringBuilder();
6882 sb.append("WindowToken{");
6883 sb.append(Integer.toHexString(System.identityHashCode(this)));
6884 sb.append(" token="); sb.append(token); sb.append('}');
6885 stringName = sb.toString();
6886 }
6887 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006888 }
6889 };
6890
6891 class AppWindowToken extends WindowToken {
6892 // Non-null only for application tokens.
6893 final IApplicationToken appToken;
6894
6895 // All of the windows and child windows that are included in this
6896 // application token. Note this list is NOT sorted!
6897 final ArrayList<WindowState> allAppWindows = new ArrayList<WindowState>();
6898
6899 int groupId = -1;
6900 boolean appFullscreen;
6901 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Romain Guy06882f82009-06-10 13:36:04 -07006902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006903 // These are used for determining when all windows associated with
6904 // an activity have been drawn, so they can be made visible together
6905 // at the same time.
6906 int lastTransactionSequence = mTransactionSequence-1;
6907 int numInterestingWindows;
6908 int numDrawnWindows;
6909 boolean inPendingTransaction;
6910 boolean allDrawn;
Romain Guy06882f82009-06-10 13:36:04 -07006911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006912 // Is this token going to be hidden in a little while? If so, it
6913 // won't be taken into account for setting the screen orientation.
6914 boolean willBeHidden;
Romain Guy06882f82009-06-10 13:36:04 -07006915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006916 // Is this window's surface needed? This is almost like hidden, except
6917 // it will sometimes be true a little earlier: when the token has
6918 // been shown, but is still waiting for its app transition to execute
6919 // before making its windows shown.
6920 boolean hiddenRequested;
Romain Guy06882f82009-06-10 13:36:04 -07006921
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006922 // Have we told the window clients to hide themselves?
6923 boolean clientHidden;
Romain Guy06882f82009-06-10 13:36:04 -07006924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006925 // Last visibility state we reported to the app token.
6926 boolean reportedVisible;
6927
6928 // Set to true when the token has been removed from the window mgr.
6929 boolean removed;
6930
6931 // Have we been asked to have this token keep the screen frozen?
6932 boolean freezingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07006933
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006934 boolean animating;
6935 Animation animation;
6936 boolean hasTransformation;
6937 final Transformation transformation = new Transformation();
Romain Guy06882f82009-06-10 13:36:04 -07006938
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006939 // Offset to the window of all layers in the token, for use by
6940 // AppWindowToken animations.
6941 int animLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -07006942
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006943 // Information about an application starting window if displayed.
6944 StartingData startingData;
6945 WindowState startingWindow;
6946 View startingView;
6947 boolean startingDisplayed;
6948 boolean startingMoved;
6949 boolean firstWindowDrawn;
6950
6951 AppWindowToken(IApplicationToken _token) {
6952 super(_token.asBinder(),
6953 WindowManager.LayoutParams.TYPE_APPLICATION, true);
6954 appWindowToken = this;
6955 appToken = _token;
6956 }
Romain Guy06882f82009-06-10 13:36:04 -07006957
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006958 public void setAnimation(Animation anim) {
6959 if (localLOGV) Log.v(
6960 TAG, "Setting animation in " + this + ": " + anim);
6961 animation = anim;
6962 animating = false;
6963 anim.restrictDuration(MAX_ANIMATION_DURATION);
6964 anim.scaleCurrentDuration(mTransitionAnimationScale);
6965 int zorder = anim.getZAdjustment();
6966 int adj = 0;
6967 if (zorder == Animation.ZORDER_TOP) {
6968 adj = TYPE_LAYER_OFFSET;
6969 } else if (zorder == Animation.ZORDER_BOTTOM) {
6970 adj = -TYPE_LAYER_OFFSET;
6971 }
Romain Guy06882f82009-06-10 13:36:04 -07006972
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006973 if (animLayerAdjustment != adj) {
6974 animLayerAdjustment = adj;
6975 updateLayers();
6976 }
6977 }
Romain Guy06882f82009-06-10 13:36:04 -07006978
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006979 public void setDummyAnimation() {
6980 if (animation == null) {
6981 if (localLOGV) Log.v(
6982 TAG, "Setting dummy animation in " + this);
6983 animation = sDummyAnimation;
6984 }
6985 }
6986
6987 public void clearAnimation() {
6988 if (animation != null) {
6989 animation = null;
6990 animating = true;
6991 }
6992 }
Romain Guy06882f82009-06-10 13:36:04 -07006993
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006994 void updateLayers() {
6995 final int N = allAppWindows.size();
6996 final int adj = animLayerAdjustment;
6997 for (int i=0; i<N; i++) {
6998 WindowState w = allAppWindows.get(i);
6999 w.mAnimLayer = w.mLayer + adj;
7000 if (DEBUG_LAYERS) Log.v(TAG, "Updating layer " + w + ": "
7001 + w.mAnimLayer);
7002 if (w == mInputMethodTarget) {
7003 setInputMethodAnimLayerAdjustment(adj);
7004 }
7005 }
7006 }
Romain Guy06882f82009-06-10 13:36:04 -07007007
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007008 void sendAppVisibilityToClients() {
7009 final int N = allAppWindows.size();
7010 for (int i=0; i<N; i++) {
7011 WindowState win = allAppWindows.get(i);
7012 if (win == startingWindow && clientHidden) {
7013 // Don't hide the starting window.
7014 continue;
7015 }
7016 try {
7017 if (DEBUG_VISIBILITY) Log.v(TAG,
7018 "Setting visibility of " + win + ": " + (!clientHidden));
7019 win.mClient.dispatchAppVisibility(!clientHidden);
7020 } catch (RemoteException e) {
7021 }
7022 }
7023 }
Romain Guy06882f82009-06-10 13:36:04 -07007024
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007025 void showAllWindowsLocked() {
7026 final int NW = allAppWindows.size();
7027 for (int i=0; i<NW; i++) {
7028 WindowState w = allAppWindows.get(i);
7029 if (DEBUG_VISIBILITY) Log.v(TAG,
7030 "performing show on: " + w);
7031 w.performShowLocked();
7032 }
7033 }
Romain Guy06882f82009-06-10 13:36:04 -07007034
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007035 // This must be called while inside a transaction.
7036 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
7037 if (!mDisplayFrozen) {
7038 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07007039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007040 if (animation == sDummyAnimation) {
7041 // This guy is going to animate, but not yet. For now count
7042 // it is not animating for purposes of scheduling transactions;
7043 // when it is really time to animate, this will be set to
7044 // a real animation and the next call will execute normally.
7045 return false;
7046 }
Romain Guy06882f82009-06-10 13:36:04 -07007047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007048 if ((allDrawn || animating || startingDisplayed) && animation != null) {
7049 if (!animating) {
7050 if (DEBUG_ANIM) Log.v(
7051 TAG, "Starting animation in " + this +
7052 " @ " + currentTime + ": dw=" + dw + " dh=" + dh
7053 + " scale=" + mTransitionAnimationScale
7054 + " allDrawn=" + allDrawn + " animating=" + animating);
7055 animation.initialize(dw, dh, dw, dh);
7056 animation.setStartTime(currentTime);
7057 animating = true;
7058 }
7059 transformation.clear();
7060 final boolean more = animation.getTransformation(
7061 currentTime, transformation);
7062 if (DEBUG_ANIM) Log.v(
7063 TAG, "Stepped animation in " + this +
7064 ": more=" + more + ", xform=" + transformation);
7065 if (more) {
7066 // we're done!
7067 hasTransformation = true;
7068 return true;
7069 }
7070 if (DEBUG_ANIM) Log.v(
7071 TAG, "Finished animation in " + this +
7072 " @ " + currentTime);
7073 animation = null;
7074 }
7075 } else if (animation != null) {
7076 // If the display is frozen, and there is a pending animation,
7077 // clear it and make sure we run the cleanup code.
7078 animating = true;
7079 animation = null;
7080 }
7081
7082 hasTransformation = false;
Romain Guy06882f82009-06-10 13:36:04 -07007083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007084 if (!animating) {
7085 return false;
7086 }
7087
7088 clearAnimation();
7089 animating = false;
7090 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == this) {
7091 moveInputMethodWindowsIfNeededLocked(true);
7092 }
Romain Guy06882f82009-06-10 13:36:04 -07007093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007094 if (DEBUG_ANIM) Log.v(
7095 TAG, "Animation done in " + this
7096 + ": reportedVisible=" + reportedVisible);
7097
7098 transformation.clear();
7099 if (animLayerAdjustment != 0) {
7100 animLayerAdjustment = 0;
7101 updateLayers();
7102 }
Romain Guy06882f82009-06-10 13:36:04 -07007103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007104 final int N = windows.size();
7105 for (int i=0; i<N; i++) {
7106 ((WindowState)windows.get(i)).finishExit();
7107 }
7108 updateReportedVisibilityLocked();
Romain Guy06882f82009-06-10 13:36:04 -07007109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007110 return false;
7111 }
7112
7113 void updateReportedVisibilityLocked() {
7114 if (appToken == null) {
7115 return;
7116 }
Romain Guy06882f82009-06-10 13:36:04 -07007117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007118 int numInteresting = 0;
7119 int numVisible = 0;
7120 boolean nowGone = true;
Romain Guy06882f82009-06-10 13:36:04 -07007121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007122 if (DEBUG_VISIBILITY) Log.v(TAG, "Update reported visibility: " + this);
7123 final int N = allAppWindows.size();
7124 for (int i=0; i<N; i++) {
7125 WindowState win = allAppWindows.get(i);
7126 if (win == startingWindow || win.mAppFreezing) {
7127 continue;
7128 }
7129 if (DEBUG_VISIBILITY) {
7130 Log.v(TAG, "Win " + win + ": isDisplayed="
7131 + win.isDisplayedLw()
7132 + ", isAnimating=" + win.isAnimating());
7133 if (!win.isDisplayedLw()) {
7134 Log.v(TAG, "Not displayed: s=" + win.mSurface
7135 + " pv=" + win.mPolicyVisibility
7136 + " dp=" + win.mDrawPending
7137 + " cdp=" + win.mCommitDrawPending
7138 + " ah=" + win.mAttachedHidden
7139 + " th="
7140 + (win.mAppToken != null
7141 ? win.mAppToken.hiddenRequested : false)
7142 + " a=" + win.mAnimating);
7143 }
7144 }
7145 numInteresting++;
7146 if (win.isDisplayedLw()) {
7147 if (!win.isAnimating()) {
7148 numVisible++;
7149 }
7150 nowGone = false;
7151 } else if (win.isAnimating()) {
7152 nowGone = false;
7153 }
7154 }
Romain Guy06882f82009-06-10 13:36:04 -07007155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007156 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
7157 if (DEBUG_VISIBILITY) Log.v(TAG, "VIS " + this + ": interesting="
7158 + numInteresting + " visible=" + numVisible);
7159 if (nowVisible != reportedVisible) {
7160 if (DEBUG_VISIBILITY) Log.v(
7161 TAG, "Visibility changed in " + this
7162 + ": vis=" + nowVisible);
7163 reportedVisible = nowVisible;
7164 Message m = mH.obtainMessage(
7165 H.REPORT_APPLICATION_TOKEN_WINDOWS,
7166 nowVisible ? 1 : 0,
7167 nowGone ? 1 : 0,
7168 this);
7169 mH.sendMessage(m);
7170 }
7171 }
Romain Guy06882f82009-06-10 13:36:04 -07007172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007173 void dump(PrintWriter pw, String prefix) {
7174 super.dump(pw, prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007175 if (appToken != null) {
7176 pw.print(prefix); pw.println("app=true");
7177 }
7178 if (allAppWindows.size() > 0) {
7179 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
7180 }
7181 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
7182 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
7183 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
7184 pw.print(" clientHidden="); pw.print(clientHidden);
7185 pw.print(" willBeHidden="); pw.print(willBeHidden);
7186 pw.print(" reportedVisible="); pw.println(reportedVisible);
7187 if (paused || freezingScreen) {
7188 pw.print(prefix); pw.print("paused="); pw.print(paused);
7189 pw.print(" freezingScreen="); pw.println(freezingScreen);
7190 }
7191 if (numInterestingWindows != 0 || numDrawnWindows != 0
7192 || inPendingTransaction || allDrawn) {
7193 pw.print(prefix); pw.print("numInterestingWindows=");
7194 pw.print(numInterestingWindows);
7195 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
7196 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
7197 pw.print(" allDrawn="); pw.println(allDrawn);
7198 }
7199 if (animating || animation != null) {
7200 pw.print(prefix); pw.print("animating="); pw.print(animating);
7201 pw.print(" animation="); pw.println(animation);
7202 }
7203 if (animLayerAdjustment != 0) {
7204 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
7205 }
7206 if (hasTransformation) {
7207 pw.print(prefix); pw.print("hasTransformation="); pw.print(hasTransformation);
7208 pw.print(" transformation="); transformation.printShortString(pw);
7209 pw.println();
7210 }
7211 if (startingData != null || removed || firstWindowDrawn) {
7212 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
7213 pw.print(" removed="); pw.print(removed);
7214 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
7215 }
7216 if (startingWindow != null || startingView != null
7217 || startingDisplayed || startingMoved) {
7218 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
7219 pw.print(" startingView="); pw.print(startingView);
7220 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
7221 pw.print(" startingMoved"); pw.println(startingMoved);
7222 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007223 }
7224
7225 @Override
7226 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007227 if (stringName == null) {
7228 StringBuilder sb = new StringBuilder();
7229 sb.append("AppWindowToken{");
7230 sb.append(Integer.toHexString(System.identityHashCode(this)));
7231 sb.append(" token="); sb.append(token); sb.append('}');
7232 stringName = sb.toString();
7233 }
7234 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007235 }
7236 }
Romain Guy06882f82009-06-10 13:36:04 -07007237
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007238 public static WindowManager.LayoutParams findAnimations(
7239 ArrayList<AppWindowToken> order,
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007240 ArrayList<AppWindowToken> openingTokenList1,
7241 ArrayList<AppWindowToken> closingTokenList2) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007242 // We need to figure out which animation to use...
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007243
7244 // First, check if there is a compatible window in opening/closing
7245 // apps, and use it if exists.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007246 WindowManager.LayoutParams animParams = null;
7247 int animSrc = 0;
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007248 animParams = findCompatibleWindowParams(openingTokenList1);
7249 if (animParams == null) {
7250 animParams = findCompatibleWindowParams(closingTokenList2);
7251 }
7252 if (animParams != null) {
7253 return animParams;
7254 }
7255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007256 //Log.i(TAG, "Looking for animations...");
7257 for (int i=order.size()-1; i>=0; i--) {
7258 AppWindowToken wtoken = order.get(i);
7259 //Log.i(TAG, "Token " + wtoken + " with " + wtoken.windows.size() + " windows");
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007260 if (openingTokenList1.contains(wtoken) || closingTokenList2.contains(wtoken)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007261 int j = wtoken.windows.size();
7262 while (j > 0) {
7263 j--;
7264 WindowState win = wtoken.windows.get(j);
7265 //Log.i(TAG, "Window " + win + ": type=" + win.mAttrs.type);
7266 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
7267 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
7268 //Log.i(TAG, "Found base or application window, done!");
7269 if (wtoken.appFullscreen) {
7270 return win.mAttrs;
7271 }
7272 if (animSrc < 2) {
7273 animParams = win.mAttrs;
7274 animSrc = 2;
7275 }
7276 } else if (animSrc < 1 && win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION) {
7277 //Log.i(TAG, "Found normal window, we may use this...");
7278 animParams = win.mAttrs;
7279 animSrc = 1;
7280 }
7281 }
7282 }
7283 }
Romain Guy06882f82009-06-10 13:36:04 -07007284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007285 return animParams;
7286 }
Romain Guy06882f82009-06-10 13:36:04 -07007287
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007288 private static LayoutParams findCompatibleWindowParams(ArrayList<AppWindowToken> tokenList) {
7289 for (int appCount = tokenList.size() - 1; appCount >= 0; appCount--) {
7290 AppWindowToken wtoken = tokenList.get(appCount);
7291 // Just checking one window is sufficient as all windows have the compatible flag
7292 // if the application is in compatibility mode.
7293 if (wtoken.windows.size() > 0) {
7294 WindowManager.LayoutParams params = wtoken.windows.get(0).mAttrs;
7295 if ((params.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
7296 return params;
7297 }
7298 }
7299 }
7300 return null;
7301 }
7302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007303 // -------------------------------------------------------------
7304 // DummyAnimation
7305 // -------------------------------------------------------------
7306
7307 // This is an animation that does nothing: it just immediately finishes
7308 // itself every time it is called. It is used as a stub animation in cases
7309 // where we want to synchronize multiple things that may be animating.
7310 static final class DummyAnimation extends Animation {
7311 public boolean getTransformation(long currentTime, Transformation outTransformation) {
7312 return false;
7313 }
7314 }
7315 static final Animation sDummyAnimation = new DummyAnimation();
Romain Guy06882f82009-06-10 13:36:04 -07007316
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007317 // -------------------------------------------------------------
7318 // Async Handler
7319 // -------------------------------------------------------------
7320
7321 static final class StartingData {
7322 final String pkg;
7323 final int theme;
7324 final CharSequence nonLocalizedLabel;
7325 final int labelRes;
7326 final int icon;
Romain Guy06882f82009-06-10 13:36:04 -07007327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007328 StartingData(String _pkg, int _theme, CharSequence _nonLocalizedLabel,
7329 int _labelRes, int _icon) {
7330 pkg = _pkg;
7331 theme = _theme;
7332 nonLocalizedLabel = _nonLocalizedLabel;
7333 labelRes = _labelRes;
7334 icon = _icon;
7335 }
7336 }
7337
7338 private final class H extends Handler {
7339 public static final int REPORT_FOCUS_CHANGE = 2;
7340 public static final int REPORT_LOSING_FOCUS = 3;
7341 public static final int ANIMATE = 4;
7342 public static final int ADD_STARTING = 5;
7343 public static final int REMOVE_STARTING = 6;
7344 public static final int FINISHED_STARTING = 7;
7345 public static final int REPORT_APPLICATION_TOKEN_WINDOWS = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007346 public static final int WINDOW_FREEZE_TIMEOUT = 11;
7347 public static final int HOLD_SCREEN_CHANGED = 12;
7348 public static final int APP_TRANSITION_TIMEOUT = 13;
7349 public static final int PERSIST_ANIMATION_SCALE = 14;
7350 public static final int FORCE_GC = 15;
7351 public static final int ENABLE_SCREEN = 16;
7352 public static final int APP_FREEZE_TIMEOUT = 17;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07007353 public static final int COMPUTE_AND_SEND_NEW_CONFIGURATION = 18;
Romain Guy06882f82009-06-10 13:36:04 -07007354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007355 private Session mLastReportedHold;
Romain Guy06882f82009-06-10 13:36:04 -07007356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007357 public H() {
7358 }
Romain Guy06882f82009-06-10 13:36:04 -07007359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007360 @Override
7361 public void handleMessage(Message msg) {
7362 switch (msg.what) {
7363 case REPORT_FOCUS_CHANGE: {
7364 WindowState lastFocus;
7365 WindowState newFocus;
Romain Guy06882f82009-06-10 13:36:04 -07007366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007367 synchronized(mWindowMap) {
7368 lastFocus = mLastFocus;
7369 newFocus = mCurrentFocus;
7370 if (lastFocus == newFocus) {
7371 // Focus is not changing, so nothing to do.
7372 return;
7373 }
7374 mLastFocus = newFocus;
7375 //Log.i(TAG, "Focus moving from " + lastFocus
7376 // + " to " + newFocus);
7377 if (newFocus != null && lastFocus != null
7378 && !newFocus.isDisplayedLw()) {
7379 //Log.i(TAG, "Delaying loss of focus...");
7380 mLosingFocus.add(lastFocus);
7381 lastFocus = null;
7382 }
7383 }
7384
7385 if (lastFocus != newFocus) {
7386 //System.out.println("Changing focus from " + lastFocus
7387 // + " to " + newFocus);
7388 if (newFocus != null) {
7389 try {
7390 //Log.i(TAG, "Gaining focus: " + newFocus);
7391 newFocus.mClient.windowFocusChanged(true, mInTouchMode);
7392 } catch (RemoteException e) {
7393 // Ignore if process has died.
7394 }
7395 }
7396
7397 if (lastFocus != null) {
7398 try {
7399 //Log.i(TAG, "Losing focus: " + lastFocus);
7400 lastFocus.mClient.windowFocusChanged(false, mInTouchMode);
7401 } catch (RemoteException e) {
7402 // Ignore if process has died.
7403 }
7404 }
7405 }
7406 } break;
7407
7408 case REPORT_LOSING_FOCUS: {
7409 ArrayList<WindowState> losers;
Romain Guy06882f82009-06-10 13:36:04 -07007410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007411 synchronized(mWindowMap) {
7412 losers = mLosingFocus;
7413 mLosingFocus = new ArrayList<WindowState>();
7414 }
7415
7416 final int N = losers.size();
7417 for (int i=0; i<N; i++) {
7418 try {
7419 //Log.i(TAG, "Losing delayed focus: " + losers.get(i));
7420 losers.get(i).mClient.windowFocusChanged(false, mInTouchMode);
7421 } catch (RemoteException e) {
7422 // Ignore if process has died.
7423 }
7424 }
7425 } break;
7426
7427 case ANIMATE: {
7428 synchronized(mWindowMap) {
7429 mAnimationPending = false;
7430 performLayoutAndPlaceSurfacesLocked();
7431 }
7432 } break;
7433
7434 case ADD_STARTING: {
7435 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
7436 final StartingData sd = wtoken.startingData;
7437
7438 if (sd == null) {
7439 // Animation has been canceled... do nothing.
7440 return;
7441 }
Romain Guy06882f82009-06-10 13:36:04 -07007442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007443 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Add starting "
7444 + wtoken + ": pkg=" + sd.pkg);
Romain Guy06882f82009-06-10 13:36:04 -07007445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007446 View view = null;
7447 try {
7448 view = mPolicy.addStartingWindow(
7449 wtoken.token, sd.pkg,
7450 sd.theme, sd.nonLocalizedLabel, sd.labelRes,
7451 sd.icon);
7452 } catch (Exception e) {
7453 Log.w(TAG, "Exception when adding starting window", e);
7454 }
7455
7456 if (view != null) {
7457 boolean abort = false;
7458
7459 synchronized(mWindowMap) {
7460 if (wtoken.removed || wtoken.startingData == null) {
7461 // If the window was successfully added, then
7462 // we need to remove it.
7463 if (wtoken.startingWindow != null) {
7464 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
7465 "Aborted starting " + wtoken
7466 + ": removed=" + wtoken.removed
7467 + " startingData=" + wtoken.startingData);
7468 wtoken.startingWindow = null;
7469 wtoken.startingData = null;
7470 abort = true;
7471 }
7472 } else {
7473 wtoken.startingView = view;
7474 }
7475 if (DEBUG_STARTING_WINDOW && !abort) Log.v(TAG,
7476 "Added starting " + wtoken
7477 + ": startingWindow="
7478 + wtoken.startingWindow + " startingView="
7479 + wtoken.startingView);
7480 }
7481
7482 if (abort) {
7483 try {
7484 mPolicy.removeStartingWindow(wtoken.token, view);
7485 } catch (Exception e) {
7486 Log.w(TAG, "Exception when removing starting window", e);
7487 }
7488 }
7489 }
7490 } break;
7491
7492 case REMOVE_STARTING: {
7493 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
7494 IBinder token = null;
7495 View view = null;
7496 synchronized (mWindowMap) {
7497 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Remove starting "
7498 + wtoken + ": startingWindow="
7499 + wtoken.startingWindow + " startingView="
7500 + wtoken.startingView);
7501 if (wtoken.startingWindow != null) {
7502 view = wtoken.startingView;
7503 token = wtoken.token;
7504 wtoken.startingData = null;
7505 wtoken.startingView = null;
7506 wtoken.startingWindow = null;
7507 }
7508 }
7509 if (view != null) {
7510 try {
7511 mPolicy.removeStartingWindow(token, view);
7512 } catch (Exception e) {
7513 Log.w(TAG, "Exception when removing starting window", e);
7514 }
7515 }
7516 } break;
7517
7518 case FINISHED_STARTING: {
7519 IBinder token = null;
7520 View view = null;
7521 while (true) {
7522 synchronized (mWindowMap) {
7523 final int N = mFinishedStarting.size();
7524 if (N <= 0) {
7525 break;
7526 }
7527 AppWindowToken wtoken = mFinishedStarting.remove(N-1);
7528
7529 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
7530 "Finished starting " + wtoken
7531 + ": startingWindow=" + wtoken.startingWindow
7532 + " startingView=" + wtoken.startingView);
7533
7534 if (wtoken.startingWindow == null) {
7535 continue;
7536 }
7537
7538 view = wtoken.startingView;
7539 token = wtoken.token;
7540 wtoken.startingData = null;
7541 wtoken.startingView = null;
7542 wtoken.startingWindow = null;
7543 }
7544
7545 try {
7546 mPolicy.removeStartingWindow(token, view);
7547 } catch (Exception e) {
7548 Log.w(TAG, "Exception when removing starting window", e);
7549 }
7550 }
7551 } break;
7552
7553 case REPORT_APPLICATION_TOKEN_WINDOWS: {
7554 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
7555
7556 boolean nowVisible = msg.arg1 != 0;
7557 boolean nowGone = msg.arg2 != 0;
7558
7559 try {
7560 if (DEBUG_VISIBILITY) Log.v(
7561 TAG, "Reporting visible in " + wtoken
7562 + " visible=" + nowVisible
7563 + " gone=" + nowGone);
7564 if (nowVisible) {
7565 wtoken.appToken.windowsVisible();
7566 } else {
7567 wtoken.appToken.windowsGone();
7568 }
7569 } catch (RemoteException ex) {
7570 }
7571 } break;
Romain Guy06882f82009-06-10 13:36:04 -07007572
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007573 case WINDOW_FREEZE_TIMEOUT: {
7574 synchronized (mWindowMap) {
7575 Log.w(TAG, "Window freeze timeout expired.");
7576 int i = mWindows.size();
7577 while (i > 0) {
7578 i--;
7579 WindowState w = (WindowState)mWindows.get(i);
7580 if (w.mOrientationChanging) {
7581 w.mOrientationChanging = false;
7582 Log.w(TAG, "Force clearing orientation change: " + w);
7583 }
7584 }
7585 performLayoutAndPlaceSurfacesLocked();
7586 }
7587 break;
7588 }
Romain Guy06882f82009-06-10 13:36:04 -07007589
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007590 case HOLD_SCREEN_CHANGED: {
7591 Session oldHold;
7592 Session newHold;
7593 synchronized (mWindowMap) {
7594 oldHold = mLastReportedHold;
7595 newHold = (Session)msg.obj;
7596 mLastReportedHold = newHold;
7597 }
Romain Guy06882f82009-06-10 13:36:04 -07007598
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007599 if (oldHold != newHold) {
7600 try {
7601 if (oldHold != null) {
7602 mBatteryStats.noteStopWakelock(oldHold.mUid,
7603 "window",
7604 BatteryStats.WAKE_TYPE_WINDOW);
7605 }
7606 if (newHold != null) {
7607 mBatteryStats.noteStartWakelock(newHold.mUid,
7608 "window",
7609 BatteryStats.WAKE_TYPE_WINDOW);
7610 }
7611 } catch (RemoteException e) {
7612 }
7613 }
7614 break;
7615 }
Romain Guy06882f82009-06-10 13:36:04 -07007616
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007617 case APP_TRANSITION_TIMEOUT: {
7618 synchronized (mWindowMap) {
7619 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
7620 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
7621 "*** APP TRANSITION TIMEOUT");
7622 mAppTransitionReady = true;
7623 mAppTransitionTimeout = true;
7624 performLayoutAndPlaceSurfacesLocked();
7625 }
7626 }
7627 break;
7628 }
Romain Guy06882f82009-06-10 13:36:04 -07007629
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007630 case PERSIST_ANIMATION_SCALE: {
7631 Settings.System.putFloat(mContext.getContentResolver(),
7632 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
7633 Settings.System.putFloat(mContext.getContentResolver(),
7634 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
7635 break;
7636 }
Romain Guy06882f82009-06-10 13:36:04 -07007637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007638 case FORCE_GC: {
7639 synchronized(mWindowMap) {
7640 if (mAnimationPending) {
7641 // If we are animating, don't do the gc now but
7642 // delay a bit so we don't interrupt the animation.
7643 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
7644 2000);
7645 return;
7646 }
7647 // If we are currently rotating the display, it will
7648 // schedule a new message when done.
7649 if (mDisplayFrozen) {
7650 return;
7651 }
7652 mFreezeGcPending = 0;
7653 }
7654 Runtime.getRuntime().gc();
7655 break;
7656 }
Romain Guy06882f82009-06-10 13:36:04 -07007657
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007658 case ENABLE_SCREEN: {
7659 performEnableScreen();
7660 break;
7661 }
Romain Guy06882f82009-06-10 13:36:04 -07007662
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007663 case APP_FREEZE_TIMEOUT: {
7664 synchronized (mWindowMap) {
7665 Log.w(TAG, "App freeze timeout expired.");
7666 int i = mAppTokens.size();
7667 while (i > 0) {
7668 i--;
7669 AppWindowToken tok = mAppTokens.get(i);
7670 if (tok.freezingScreen) {
7671 Log.w(TAG, "Force clearing freeze: " + tok);
7672 unsetAppFreezingScreenLocked(tok, true, true);
7673 }
7674 }
7675 }
7676 break;
7677 }
Romain Guy06882f82009-06-10 13:36:04 -07007678
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07007679 case COMPUTE_AND_SEND_NEW_CONFIGURATION: {
Dianne Hackborncfaef692009-06-15 14:24:44 -07007680 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07007681 sendNewConfiguration();
7682 }
7683 break;
7684 }
Romain Guy06882f82009-06-10 13:36:04 -07007685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007686 }
7687 }
7688 }
7689
7690 // -------------------------------------------------------------
7691 // IWindowManager API
7692 // -------------------------------------------------------------
7693
7694 public IWindowSession openSession(IInputMethodClient client,
7695 IInputContext inputContext) {
7696 if (client == null) throw new IllegalArgumentException("null client");
7697 if (inputContext == null) throw new IllegalArgumentException("null inputContext");
7698 return new Session(client, inputContext);
7699 }
7700
7701 public boolean inputMethodClientHasFocus(IInputMethodClient client) {
7702 synchronized (mWindowMap) {
7703 // The focus for the client is the window immediately below
7704 // where we would place the input method window.
7705 int idx = findDesiredInputMethodWindowIndexLocked(false);
7706 WindowState imFocus;
7707 if (idx > 0) {
7708 imFocus = (WindowState)mWindows.get(idx-1);
7709 if (imFocus != null) {
7710 if (imFocus.mSession.mClient != null &&
7711 imFocus.mSession.mClient.asBinder() == client.asBinder()) {
7712 return true;
7713 }
7714 }
7715 }
7716 }
7717 return false;
7718 }
Romain Guy06882f82009-06-10 13:36:04 -07007719
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007720 // -------------------------------------------------------------
7721 // Internals
7722 // -------------------------------------------------------------
7723
7724 final WindowState windowForClientLocked(Session session, IWindow client) {
7725 return windowForClientLocked(session, client.asBinder());
7726 }
Romain Guy06882f82009-06-10 13:36:04 -07007727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007728 final WindowState windowForClientLocked(Session session, IBinder client) {
7729 WindowState win = mWindowMap.get(client);
7730 if (localLOGV) Log.v(
7731 TAG, "Looking up client " + client + ": " + win);
7732 if (win == null) {
7733 RuntimeException ex = new RuntimeException();
7734 Log.w(TAG, "Requested window " + client + " does not exist", ex);
7735 return null;
7736 }
7737 if (session != null && win.mSession != session) {
7738 RuntimeException ex = new RuntimeException();
7739 Log.w(TAG, "Requested window " + client + " is in session " +
7740 win.mSession + ", not " + session, ex);
7741 return null;
7742 }
7743
7744 return win;
7745 }
7746
7747 private final void assignLayersLocked() {
7748 int N = mWindows.size();
7749 int curBaseLayer = 0;
7750 int curLayer = 0;
7751 int i;
Romain Guy06882f82009-06-10 13:36:04 -07007752
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007753 for (i=0; i<N; i++) {
7754 WindowState w = (WindowState)mWindows.get(i);
7755 if (w.mBaseLayer == curBaseLayer || w.mIsImWindow) {
7756 curLayer += WINDOW_LAYER_MULTIPLIER;
7757 w.mLayer = curLayer;
7758 } else {
7759 curBaseLayer = curLayer = w.mBaseLayer;
7760 w.mLayer = curLayer;
7761 }
7762 if (w.mTargetAppToken != null) {
7763 w.mAnimLayer = w.mLayer + w.mTargetAppToken.animLayerAdjustment;
7764 } else if (w.mAppToken != null) {
7765 w.mAnimLayer = w.mLayer + w.mAppToken.animLayerAdjustment;
7766 } else {
7767 w.mAnimLayer = w.mLayer;
7768 }
7769 if (w.mIsImWindow) {
7770 w.mAnimLayer += mInputMethodAnimLayerAdjustment;
7771 }
7772 if (DEBUG_LAYERS) Log.v(TAG, "Assign layer " + w + ": "
7773 + w.mAnimLayer);
7774 //System.out.println(
7775 // "Assigned layer " + curLayer + " to " + w.mClient.asBinder());
7776 }
7777 }
7778
7779 private boolean mInLayout = false;
7780 private final void performLayoutAndPlaceSurfacesLocked() {
7781 if (mInLayout) {
Dave Bortcfe65242009-04-09 14:51:04 -07007782 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007783 throw new RuntimeException("Recursive call!");
7784 }
7785 Log.w(TAG, "performLayoutAndPlaceSurfacesLocked called while in layout");
7786 return;
7787 }
7788
7789 boolean recoveringMemory = false;
7790 if (mForceRemoves != null) {
7791 recoveringMemory = true;
7792 // Wait a little it for things to settle down, and off we go.
7793 for (int i=0; i<mForceRemoves.size(); i++) {
7794 WindowState ws = mForceRemoves.get(i);
7795 Log.i(TAG, "Force removing: " + ws);
7796 removeWindowInnerLocked(ws.mSession, ws);
7797 }
7798 mForceRemoves = null;
7799 Log.w(TAG, "Due to memory failure, waiting a bit for next layout");
7800 Object tmp = new Object();
7801 synchronized (tmp) {
7802 try {
7803 tmp.wait(250);
7804 } catch (InterruptedException e) {
7805 }
7806 }
7807 }
Romain Guy06882f82009-06-10 13:36:04 -07007808
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007809 mInLayout = true;
7810 try {
7811 performLayoutAndPlaceSurfacesLockedInner(recoveringMemory);
Romain Guy06882f82009-06-10 13:36:04 -07007812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007813 int i = mPendingRemove.size()-1;
7814 if (i >= 0) {
7815 while (i >= 0) {
7816 WindowState w = mPendingRemove.get(i);
7817 removeWindowInnerLocked(w.mSession, w);
7818 i--;
7819 }
7820 mPendingRemove.clear();
7821
7822 mInLayout = false;
7823 assignLayersLocked();
7824 mLayoutNeeded = true;
7825 performLayoutAndPlaceSurfacesLocked();
7826
7827 } else {
7828 mInLayout = false;
7829 if (mLayoutNeeded) {
7830 requestAnimationLocked(0);
7831 }
7832 }
7833 } catch (RuntimeException e) {
7834 mInLayout = false;
7835 Log.e(TAG, "Unhandled exception while layout out windows", e);
7836 }
7837 }
7838
7839 private final void performLayoutLockedInner() {
7840 final int dw = mDisplay.getWidth();
7841 final int dh = mDisplay.getHeight();
7842
7843 final int N = mWindows.size();
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07007844 int repeats = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007845 int i;
7846
7847 // FIRST LOOP: Perform a layout, if needed.
Romain Guy06882f82009-06-10 13:36:04 -07007848
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07007849 while (mLayoutNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007850 mPolicy.beginLayoutLw(dw, dh);
7851
7852 // First perform layout of any root windows (not attached
7853 // to another window).
7854 int topAttached = -1;
7855 for (i = N-1; i >= 0; i--) {
7856 WindowState win = (WindowState) mWindows.get(i);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07007857
7858 // Don't do layout of a window if it is not visible, or
7859 // soon won't be visible, to avoid wasting time and funky
7860 // changes while a window is animating away.
7861 final AppWindowToken atoken = win.mAppToken;
7862 final boolean gone = win.mViewVisibility == View.GONE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007863 || !win.mRelayoutCalled
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07007864 || win.mRootToken.hidden
7865 || (atoken != null && atoken.hiddenRequested)
7866 || !win.mPolicyVisibility
7867 || win.mAttachedHidden
7868 || win.mExiting || win.mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007869
7870 // If this view is GONE, then skip it -- keep the current
7871 // frame, and let the caller know so they can ignore it
7872 // if they want. (We do the normal layout for INVISIBLE
7873 // windows, since that means "perform layout as normal,
7874 // just don't display").
7875 if (!gone || !win.mHaveFrame) {
7876 if (!win.mLayoutAttached) {
7877 mPolicy.layoutWindowLw(win, win.mAttrs, null);
7878 } else {
7879 if (topAttached < 0) topAttached = i;
7880 }
7881 }
7882 }
Romain Guy06882f82009-06-10 13:36:04 -07007883
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007884 // Now perform layout of attached windows, which usually
7885 // depend on the position of the window they are attached to.
7886 // XXX does not deal with windows that are attached to windows
7887 // that are themselves attached.
7888 for (i = topAttached; i >= 0; i--) {
7889 WindowState win = (WindowState) mWindows.get(i);
7890
7891 // If this view is GONE, then skip it -- keep the current
7892 // frame, and let the caller know so they can ignore it
7893 // if they want. (We do the normal layout for INVISIBLE
7894 // windows, since that means "perform layout as normal,
7895 // just don't display").
7896 if (win.mLayoutAttached) {
7897 if ((win.mViewVisibility != View.GONE && win.mRelayoutCalled)
7898 || !win.mHaveFrame) {
7899 mPolicy.layoutWindowLw(win, win.mAttrs, win.mAttachedWindow);
7900 }
7901 }
7902 }
7903
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07007904 if (!mPolicy.finishLayoutLw()) {
7905 mLayoutNeeded = false;
7906 } else if (repeats > 2) {
7907 Log.w(TAG, "Layout repeat aborted after too many iterations");
7908 mLayoutNeeded = false;
7909 } else {
7910 repeats++;
7911 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007912 }
7913 }
Romain Guy06882f82009-06-10 13:36:04 -07007914
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007915 private final void performLayoutAndPlaceSurfacesLockedInner(
7916 boolean recoveringMemory) {
7917 final long currentTime = SystemClock.uptimeMillis();
7918 final int dw = mDisplay.getWidth();
7919 final int dh = mDisplay.getHeight();
7920
7921 final int N = mWindows.size();
7922 int i;
7923
7924 // FIRST LOOP: Perform a layout, if needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007925 performLayoutLockedInner();
Romain Guy06882f82009-06-10 13:36:04 -07007926
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007927 if (mFxSession == null) {
7928 mFxSession = new SurfaceSession();
7929 }
Romain Guy06882f82009-06-10 13:36:04 -07007930
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007931 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
7932
7933 // Initialize state of exiting tokens.
7934 for (i=mExitingTokens.size()-1; i>=0; i--) {
7935 mExitingTokens.get(i).hasVisible = false;
7936 }
7937
7938 // Initialize state of exiting applications.
7939 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
7940 mExitingAppTokens.get(i).hasVisible = false;
7941 }
7942
7943 // SECOND LOOP: Execute animations and update visibility of windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007944 boolean orientationChangeComplete = true;
7945 Session holdScreen = null;
7946 float screenBrightness = -1;
7947 boolean focusDisplayed = false;
7948 boolean animating = false;
7949
7950 Surface.openTransaction();
7951 try {
7952 boolean restart;
7953
7954 do {
7955 final int transactionSequence = ++mTransactionSequence;
7956
7957 // Update animations of all applications, including those
7958 // associated with exiting/removed apps
7959 boolean tokensAnimating = false;
7960 final int NAT = mAppTokens.size();
7961 for (i=0; i<NAT; i++) {
7962 if (mAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
7963 tokensAnimating = true;
7964 }
7965 }
7966 final int NEAT = mExitingAppTokens.size();
7967 for (i=0; i<NEAT; i++) {
7968 if (mExitingAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
7969 tokensAnimating = true;
7970 }
7971 }
7972
7973 animating = tokensAnimating;
7974 restart = false;
7975
7976 boolean tokenMayBeDrawn = false;
7977
7978 mPolicy.beginAnimationLw(dw, dh);
7979
7980 for (i=N-1; i>=0; i--) {
7981 WindowState w = (WindowState)mWindows.get(i);
7982
7983 final WindowManager.LayoutParams attrs = w.mAttrs;
7984
7985 if (w.mSurface != null) {
7986 // Execute animation.
7987 w.commitFinishDrawingLocked(currentTime);
7988 if (w.stepAnimationLocked(currentTime, dw, dh)) {
7989 animating = true;
7990 //w.dump(" ");
7991 }
7992
7993 mPolicy.animatingWindowLw(w, attrs);
7994 }
7995
7996 final AppWindowToken atoken = w.mAppToken;
7997 if (atoken != null && (!atoken.allDrawn || atoken.freezingScreen)) {
7998 if (atoken.lastTransactionSequence != transactionSequence) {
7999 atoken.lastTransactionSequence = transactionSequence;
8000 atoken.numInterestingWindows = atoken.numDrawnWindows = 0;
8001 atoken.startingDisplayed = false;
8002 }
8003 if ((w.isOnScreen() || w.mAttrs.type
8004 == WindowManager.LayoutParams.TYPE_BASE_APPLICATION)
8005 && !w.mExiting && !w.mDestroying) {
8006 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
8007 Log.v(TAG, "Eval win " + w + ": isDisplayed="
8008 + w.isDisplayedLw()
8009 + ", isAnimating=" + w.isAnimating());
8010 if (!w.isDisplayedLw()) {
8011 Log.v(TAG, "Not displayed: s=" + w.mSurface
8012 + " pv=" + w.mPolicyVisibility
8013 + " dp=" + w.mDrawPending
8014 + " cdp=" + w.mCommitDrawPending
8015 + " ah=" + w.mAttachedHidden
8016 + " th=" + atoken.hiddenRequested
8017 + " a=" + w.mAnimating);
8018 }
8019 }
8020 if (w != atoken.startingWindow) {
8021 if (!atoken.freezingScreen || !w.mAppFreezing) {
8022 atoken.numInterestingWindows++;
8023 if (w.isDisplayedLw()) {
8024 atoken.numDrawnWindows++;
8025 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Log.v(TAG,
8026 "tokenMayBeDrawn: " + atoken
8027 + " freezingScreen=" + atoken.freezingScreen
8028 + " mAppFreezing=" + w.mAppFreezing);
8029 tokenMayBeDrawn = true;
8030 }
8031 }
8032 } else if (w.isDisplayedLw()) {
8033 atoken.startingDisplayed = true;
8034 }
8035 }
8036 } else if (w.mReadyToShow) {
8037 w.performShowLocked();
8038 }
8039 }
8040
8041 if (mPolicy.finishAnimationLw()) {
8042 restart = true;
8043 }
8044
8045 if (tokenMayBeDrawn) {
8046 // See if any windows have been drawn, so they (and others
8047 // associated with them) can now be shown.
8048 final int NT = mTokenList.size();
8049 for (i=0; i<NT; i++) {
8050 AppWindowToken wtoken = mTokenList.get(i).appWindowToken;
8051 if (wtoken == null) {
8052 continue;
8053 }
8054 if (wtoken.freezingScreen) {
8055 int numInteresting = wtoken.numInterestingWindows;
8056 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
8057 if (DEBUG_VISIBILITY) Log.v(TAG,
8058 "allDrawn: " + wtoken
8059 + " interesting=" + numInteresting
8060 + " drawn=" + wtoken.numDrawnWindows);
8061 wtoken.showAllWindowsLocked();
8062 unsetAppFreezingScreenLocked(wtoken, false, true);
8063 orientationChangeComplete = true;
8064 }
8065 } else if (!wtoken.allDrawn) {
8066 int numInteresting = wtoken.numInterestingWindows;
8067 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
8068 if (DEBUG_VISIBILITY) Log.v(TAG,
8069 "allDrawn: " + wtoken
8070 + " interesting=" + numInteresting
8071 + " drawn=" + wtoken.numDrawnWindows);
8072 wtoken.allDrawn = true;
8073 restart = true;
8074
8075 // We can now show all of the drawn windows!
8076 if (!mOpeningApps.contains(wtoken)) {
8077 wtoken.showAllWindowsLocked();
8078 }
8079 }
8080 }
8081 }
8082 }
8083
8084 // If we are ready to perform an app transition, check through
8085 // all of the app tokens to be shown and see if they are ready
8086 // to go.
8087 if (mAppTransitionReady) {
8088 int NN = mOpeningApps.size();
8089 boolean goodToGo = true;
8090 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8091 "Checking " + NN + " opening apps (frozen="
8092 + mDisplayFrozen + " timeout="
8093 + mAppTransitionTimeout + ")...");
8094 if (!mDisplayFrozen && !mAppTransitionTimeout) {
8095 // If the display isn't frozen, wait to do anything until
8096 // all of the apps are ready. Otherwise just go because
8097 // we'll unfreeze the display when everyone is ready.
8098 for (i=0; i<NN && goodToGo; i++) {
8099 AppWindowToken wtoken = mOpeningApps.get(i);
8100 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8101 "Check opening app" + wtoken + ": allDrawn="
8102 + wtoken.allDrawn + " startingDisplayed="
8103 + wtoken.startingDisplayed);
8104 if (!wtoken.allDrawn && !wtoken.startingDisplayed
8105 && !wtoken.startingMoved) {
8106 goodToGo = false;
8107 }
8108 }
8109 }
8110 if (goodToGo) {
8111 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "**** GOOD TO GO");
8112 int transit = mNextAppTransition;
8113 if (mSkipAppTransitionAnimation) {
8114 transit = WindowManagerPolicy.TRANSIT_NONE;
8115 }
8116 mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
8117 mAppTransitionReady = false;
8118 mAppTransitionTimeout = false;
8119 mStartingIconInTransition = false;
8120 mSkipAppTransitionAnimation = false;
8121
8122 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
8123
8124 // We need to figure out which animation to use...
8125 WindowManager.LayoutParams lp = findAnimations(mAppTokens,
8126 mOpeningApps, mClosingApps);
8127
8128 NN = mOpeningApps.size();
8129 for (i=0; i<NN; i++) {
8130 AppWindowToken wtoken = mOpeningApps.get(i);
8131 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8132 "Now opening app" + wtoken);
8133 wtoken.reportedVisible = false;
8134 wtoken.inPendingTransaction = false;
8135 setTokenVisibilityLocked(wtoken, lp, true, transit, false);
8136 wtoken.updateReportedVisibilityLocked();
8137 wtoken.showAllWindowsLocked();
8138 }
8139 NN = mClosingApps.size();
8140 for (i=0; i<NN; i++) {
8141 AppWindowToken wtoken = mClosingApps.get(i);
8142 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8143 "Now closing app" + wtoken);
8144 wtoken.inPendingTransaction = false;
8145 setTokenVisibilityLocked(wtoken, lp, false, transit, false);
8146 wtoken.updateReportedVisibilityLocked();
8147 // Force the allDrawn flag, because we want to start
8148 // this guy's animations regardless of whether it's
8149 // gotten drawn.
8150 wtoken.allDrawn = true;
8151 }
8152
8153 mOpeningApps.clear();
8154 mClosingApps.clear();
8155
8156 // This has changed the visibility of windows, so perform
8157 // a new layout to get them all up-to-date.
8158 mLayoutNeeded = true;
8159 moveInputMethodWindowsIfNeededLocked(true);
8160 performLayoutLockedInner();
8161 updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES);
8162
8163 restart = true;
8164 }
8165 }
8166 } while (restart);
8167
8168 // THIRD LOOP: Update the surfaces of all windows.
8169
8170 final boolean someoneLosingFocus = mLosingFocus.size() != 0;
8171
8172 boolean obscured = false;
8173 boolean blurring = false;
8174 boolean dimming = false;
8175 boolean covered = false;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07008176 boolean syswin = false;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008177 boolean backgroundFillerShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008178
8179 for (i=N-1; i>=0; i--) {
8180 WindowState w = (WindowState)mWindows.get(i);
8181
8182 boolean displayed = false;
8183 final WindowManager.LayoutParams attrs = w.mAttrs;
8184 final int attrFlags = attrs.flags;
8185
8186 if (w.mSurface != null) {
8187 w.computeShownFrameLocked();
8188 if (localLOGV) Log.v(
8189 TAG, "Placing surface #" + i + " " + w.mSurface
8190 + ": new=" + w.mShownFrame + ", old="
8191 + w.mLastShownFrame);
8192
8193 boolean resize;
8194 int width, height;
8195 if ((w.mAttrs.flags & w.mAttrs.FLAG_SCALED) != 0) {
8196 resize = w.mLastRequestedWidth != w.mRequestedWidth ||
8197 w.mLastRequestedHeight != w.mRequestedHeight;
8198 // for a scaled surface, we just want to use
8199 // the requested size.
8200 width = w.mRequestedWidth;
8201 height = w.mRequestedHeight;
8202 w.mLastRequestedWidth = width;
8203 w.mLastRequestedHeight = height;
8204 w.mLastShownFrame.set(w.mShownFrame);
8205 try {
8206 w.mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
8207 } catch (RuntimeException e) {
8208 Log.w(TAG, "Error positioning surface in " + w, e);
8209 if (!recoveringMemory) {
8210 reclaimSomeSurfaceMemoryLocked(w, "position");
8211 }
8212 }
8213 } else {
8214 resize = !w.mLastShownFrame.equals(w.mShownFrame);
8215 width = w.mShownFrame.width();
8216 height = w.mShownFrame.height();
8217 w.mLastShownFrame.set(w.mShownFrame);
8218 if (resize) {
8219 if (SHOW_TRANSACTIONS) Log.i(
8220 TAG, " SURFACE " + w.mSurface + ": ("
8221 + w.mShownFrame.left + ","
8222 + w.mShownFrame.top + ") ("
8223 + w.mShownFrame.width() + "x"
8224 + w.mShownFrame.height() + ")");
8225 }
8226 }
8227
8228 if (resize) {
8229 if (width < 1) width = 1;
8230 if (height < 1) height = 1;
8231 if (w.mSurface != null) {
8232 try {
8233 w.mSurface.setSize(width, height);
8234 w.mSurface.setPosition(w.mShownFrame.left,
8235 w.mShownFrame.top);
8236 } catch (RuntimeException e) {
8237 // If something goes wrong with the surface (such
8238 // as running out of memory), don't take down the
8239 // entire system.
8240 Log.e(TAG, "Failure updating surface of " + w
8241 + "size=(" + width + "x" + height
8242 + "), pos=(" + w.mShownFrame.left
8243 + "," + w.mShownFrame.top + ")", e);
8244 if (!recoveringMemory) {
8245 reclaimSomeSurfaceMemoryLocked(w, "size");
8246 }
8247 }
8248 }
8249 }
8250 if (!w.mAppFreezing) {
8251 w.mContentInsetsChanged =
8252 !w.mLastContentInsets.equals(w.mContentInsets);
8253 w.mVisibleInsetsChanged =
8254 !w.mLastVisibleInsets.equals(w.mVisibleInsets);
Romain Guy06882f82009-06-10 13:36:04 -07008255 if (!w.mLastFrame.equals(w.mFrame)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008256 || w.mContentInsetsChanged
8257 || w.mVisibleInsetsChanged) {
8258 w.mLastFrame.set(w.mFrame);
8259 w.mLastContentInsets.set(w.mContentInsets);
8260 w.mLastVisibleInsets.set(w.mVisibleInsets);
8261 // If the orientation is changing, then we need to
8262 // hold off on unfreezing the display until this
8263 // window has been redrawn; to do that, we need
8264 // to go through the process of getting informed
8265 // by the application when it has finished drawing.
8266 if (w.mOrientationChanging) {
8267 if (DEBUG_ORIENTATION) Log.v(TAG,
8268 "Orientation start waiting for draw in "
8269 + w + ", surface " + w.mSurface);
8270 w.mDrawPending = true;
8271 w.mCommitDrawPending = false;
8272 w.mReadyToShow = false;
8273 if (w.mAppToken != null) {
8274 w.mAppToken.allDrawn = false;
8275 }
8276 }
Romain Guy06882f82009-06-10 13:36:04 -07008277 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008278 "Resizing window " + w + " to " + w.mFrame);
8279 mResizingWindows.add(w);
8280 } else if (w.mOrientationChanging) {
8281 if (!w.mDrawPending && !w.mCommitDrawPending) {
8282 if (DEBUG_ORIENTATION) Log.v(TAG,
8283 "Orientation not waiting for draw in "
8284 + w + ", surface " + w.mSurface);
8285 w.mOrientationChanging = false;
8286 }
8287 }
8288 }
8289
8290 if (w.mAttachedHidden) {
8291 if (!w.mLastHidden) {
8292 //dump();
8293 w.mLastHidden = true;
8294 if (SHOW_TRANSACTIONS) Log.i(
8295 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-attached)");
8296 if (w.mSurface != null) {
8297 try {
8298 w.mSurface.hide();
8299 } catch (RuntimeException e) {
8300 Log.w(TAG, "Exception hiding surface in " + w);
8301 }
8302 }
8303 mKeyWaiter.releasePendingPointerLocked(w.mSession);
8304 }
8305 // If we are waiting for this window to handle an
8306 // orientation change, well, it is hidden, so
8307 // doesn't really matter. Note that this does
8308 // introduce a potential glitch if the window
8309 // becomes unhidden before it has drawn for the
8310 // new orientation.
8311 if (w.mOrientationChanging) {
8312 w.mOrientationChanging = false;
8313 if (DEBUG_ORIENTATION) Log.v(TAG,
8314 "Orientation change skips hidden " + w);
8315 }
8316 } else if (!w.isReadyForDisplay()) {
8317 if (!w.mLastHidden) {
8318 //dump();
8319 w.mLastHidden = true;
8320 if (SHOW_TRANSACTIONS) Log.i(
8321 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-ready)");
8322 if (w.mSurface != null) {
8323 try {
8324 w.mSurface.hide();
8325 } catch (RuntimeException e) {
8326 Log.w(TAG, "Exception exception hiding surface in " + w);
8327 }
8328 }
8329 mKeyWaiter.releasePendingPointerLocked(w.mSession);
8330 }
8331 // If we are waiting for this window to handle an
8332 // orientation change, well, it is hidden, so
8333 // doesn't really matter. Note that this does
8334 // introduce a potential glitch if the window
8335 // becomes unhidden before it has drawn for the
8336 // new orientation.
8337 if (w.mOrientationChanging) {
8338 w.mOrientationChanging = false;
8339 if (DEBUG_ORIENTATION) Log.v(TAG,
8340 "Orientation change skips hidden " + w);
8341 }
8342 } else if (w.mLastLayer != w.mAnimLayer
8343 || w.mLastAlpha != w.mShownAlpha
8344 || w.mLastDsDx != w.mDsDx
8345 || w.mLastDtDx != w.mDtDx
8346 || w.mLastDsDy != w.mDsDy
8347 || w.mLastDtDy != w.mDtDy
8348 || w.mLastHScale != w.mHScale
8349 || w.mLastVScale != w.mVScale
8350 || w.mLastHidden) {
8351 displayed = true;
8352 w.mLastAlpha = w.mShownAlpha;
8353 w.mLastLayer = w.mAnimLayer;
8354 w.mLastDsDx = w.mDsDx;
8355 w.mLastDtDx = w.mDtDx;
8356 w.mLastDsDy = w.mDsDy;
8357 w.mLastDtDy = w.mDtDy;
8358 w.mLastHScale = w.mHScale;
8359 w.mLastVScale = w.mVScale;
8360 if (SHOW_TRANSACTIONS) Log.i(
8361 TAG, " SURFACE " + w.mSurface + ": alpha="
8362 + w.mShownAlpha + " layer=" + w.mAnimLayer);
8363 if (w.mSurface != null) {
8364 try {
8365 w.mSurface.setAlpha(w.mShownAlpha);
8366 w.mSurface.setLayer(w.mAnimLayer);
8367 w.mSurface.setMatrix(
8368 w.mDsDx*w.mHScale, w.mDtDx*w.mVScale,
8369 w.mDsDy*w.mHScale, w.mDtDy*w.mVScale);
8370 } catch (RuntimeException e) {
8371 Log.w(TAG, "Error updating surface in " + w, e);
8372 if (!recoveringMemory) {
8373 reclaimSomeSurfaceMemoryLocked(w, "update");
8374 }
8375 }
8376 }
8377
8378 if (w.mLastHidden && !w.mDrawPending
8379 && !w.mCommitDrawPending
8380 && !w.mReadyToShow) {
8381 if (SHOW_TRANSACTIONS) Log.i(
8382 TAG, " SURFACE " + w.mSurface + ": SHOW (performLayout)");
8383 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + w
8384 + " during relayout");
8385 if (showSurfaceRobustlyLocked(w)) {
8386 w.mHasDrawn = true;
8387 w.mLastHidden = false;
8388 } else {
8389 w.mOrientationChanging = false;
8390 }
8391 }
8392 if (w.mSurface != null) {
8393 w.mToken.hasVisible = true;
8394 }
8395 } else {
8396 displayed = true;
8397 }
8398
8399 if (displayed) {
8400 if (!covered) {
8401 if (attrs.width == LayoutParams.FILL_PARENT
8402 && attrs.height == LayoutParams.FILL_PARENT) {
8403 covered = true;
8404 }
8405 }
8406 if (w.mOrientationChanging) {
8407 if (w.mDrawPending || w.mCommitDrawPending) {
8408 orientationChangeComplete = false;
8409 if (DEBUG_ORIENTATION) Log.v(TAG,
8410 "Orientation continue waiting for draw in " + w);
8411 } else {
8412 w.mOrientationChanging = false;
8413 if (DEBUG_ORIENTATION) Log.v(TAG,
8414 "Orientation change complete in " + w);
8415 }
8416 }
8417 w.mToken.hasVisible = true;
8418 }
8419 } else if (w.mOrientationChanging) {
8420 if (DEBUG_ORIENTATION) Log.v(TAG,
8421 "Orientation change skips hidden " + w);
8422 w.mOrientationChanging = false;
8423 }
8424
8425 final boolean canBeSeen = w.isDisplayedLw();
8426
8427 if (someoneLosingFocus && w == mCurrentFocus && canBeSeen) {
8428 focusDisplayed = true;
8429 }
8430
8431 // Update effect.
8432 if (!obscured) {
8433 if (w.mSurface != null) {
8434 if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
8435 holdScreen = w.mSession;
8436 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07008437 if (!syswin && w.mAttrs.screenBrightness >= 0
8438 && screenBrightness < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008439 screenBrightness = w.mAttrs.screenBrightness;
8440 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07008441 if (attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG
8442 || attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD
8443 || attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_ERROR) {
8444 syswin = true;
8445 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008446 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008447
8448 boolean opaqueDrawn = w.isOpaqueDrawn();
8449 if (opaqueDrawn && w.isFullscreen(dw, dh)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008450 // This window completely covers everything behind it,
8451 // so we want to leave all of them as unblurred (for
8452 // performance reasons).
8453 obscured = true;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008454 } else if (opaqueDrawn && w.needsBackgroundFiller(dw, dh)) {
8455 if (SHOW_TRANSACTIONS) Log.d(TAG, "showing background filler");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008456 // This window is in compatibility mode, and needs background filler.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008457 obscured = true;
8458 if (mBackgroundFillerSurface == null) {
8459 try {
8460 mBackgroundFillerSurface = new Surface(mFxSession, 0,
8461 0, dw, dh,
8462 PixelFormat.OPAQUE,
8463 Surface.FX_SURFACE_NORMAL);
8464 } catch (Exception e) {
8465 Log.e(TAG, "Exception creating filler surface", e);
8466 }
8467 }
8468 try {
8469 mBackgroundFillerSurface.setPosition(0, 0);
8470 mBackgroundFillerSurface.setSize(dw, dh);
8471 // Using the same layer as Dim because they will never be shown at the
8472 // same time.
8473 mBackgroundFillerSurface.setLayer(w.mAnimLayer - 1);
8474 mBackgroundFillerSurface.show();
8475 } catch (RuntimeException e) {
8476 Log.e(TAG, "Exception showing filler surface");
8477 }
8478 backgroundFillerShown = true;
8479 mBackgroundFillerShown = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008480 } else if (canBeSeen && !obscured &&
8481 (attrFlags&FLAG_BLUR_BEHIND|FLAG_DIM_BEHIND) != 0) {
8482 if (localLOGV) Log.v(TAG, "Win " + w
8483 + ": blurring=" + blurring
8484 + " obscured=" + obscured
8485 + " displayed=" + displayed);
8486 if ((attrFlags&FLAG_DIM_BEHIND) != 0) {
8487 if (!dimming) {
8488 //Log.i(TAG, "DIM BEHIND: " + w);
8489 dimming = true;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008490 if (mDimAnimator == null) {
8491 mDimAnimator = new DimAnimator(mFxSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008492 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008493 mDimAnimator.show(dw, dh);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008494 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008495 mDimAnimator.updateParameters(w, currentTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008496 }
8497 if ((attrFlags&FLAG_BLUR_BEHIND) != 0) {
8498 if (!blurring) {
8499 //Log.i(TAG, "BLUR BEHIND: " + w);
8500 blurring = true;
8501 mBlurShown = true;
8502 if (mBlurSurface == null) {
8503 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
8504 + mBlurSurface + ": CREATE");
8505 try {
Romain Guy06882f82009-06-10 13:36:04 -07008506 mBlurSurface = new Surface(mFxSession, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008507 -1, 16, 16,
8508 PixelFormat.OPAQUE,
8509 Surface.FX_SURFACE_BLUR);
8510 } catch (Exception e) {
8511 Log.e(TAG, "Exception creating Blur surface", e);
8512 }
8513 }
8514 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
8515 + mBlurSurface + ": SHOW pos=(0,0) (" +
8516 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
8517 if (mBlurSurface != null) {
8518 mBlurSurface.setPosition(0, 0);
8519 mBlurSurface.setSize(dw, dh);
8520 try {
8521 mBlurSurface.show();
8522 } catch (RuntimeException e) {
8523 Log.w(TAG, "Failure showing blur surface", e);
8524 }
8525 }
8526 }
8527 mBlurSurface.setLayer(w.mAnimLayer-2);
8528 }
8529 }
8530 }
8531 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008532
8533 if (backgroundFillerShown == false && mBackgroundFillerShown) {
8534 mBackgroundFillerShown = false;
8535 if (SHOW_TRANSACTIONS) Log.d(TAG, "hiding background filler");
8536 try {
8537 mBackgroundFillerSurface.hide();
8538 } catch (RuntimeException e) {
8539 Log.e(TAG, "Exception hiding filler surface", e);
8540 }
8541 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008542
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008543 if (mDimAnimator != null && mDimAnimator.mDimShown) {
8544 animating |= mDimAnimator.updateSurface(dimming, currentTime, mDisplayFrozen);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008545 }
Romain Guy06882f82009-06-10 13:36:04 -07008546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008547 if (!blurring && mBlurShown) {
8548 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR " + mBlurSurface
8549 + ": HIDE");
8550 try {
8551 mBlurSurface.hide();
8552 } catch (IllegalArgumentException e) {
8553 Log.w(TAG, "Illegal argument exception hiding blur surface");
8554 }
8555 mBlurShown = false;
8556 }
8557
8558 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
8559 } catch (RuntimeException e) {
8560 Log.e(TAG, "Unhandled exception in Window Manager", e);
8561 }
8562
8563 Surface.closeTransaction();
Romain Guy06882f82009-06-10 13:36:04 -07008564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008565 if (DEBUG_ORIENTATION && mDisplayFrozen) Log.v(TAG,
8566 "With display frozen, orientationChangeComplete="
8567 + orientationChangeComplete);
8568 if (orientationChangeComplete) {
8569 if (mWindowsFreezingScreen) {
8570 mWindowsFreezingScreen = false;
8571 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
8572 }
8573 if (mAppsFreezingScreen == 0) {
8574 stopFreezingDisplayLocked();
8575 }
8576 }
Romain Guy06882f82009-06-10 13:36:04 -07008577
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008578 i = mResizingWindows.size();
8579 if (i > 0) {
8580 do {
8581 i--;
8582 WindowState win = mResizingWindows.get(i);
8583 try {
8584 win.mClient.resized(win.mFrame.width(),
8585 win.mFrame.height(), win.mLastContentInsets,
8586 win.mLastVisibleInsets, win.mDrawPending);
8587 win.mContentInsetsChanged = false;
8588 win.mVisibleInsetsChanged = false;
8589 } catch (RemoteException e) {
8590 win.mOrientationChanging = false;
8591 }
8592 } while (i > 0);
8593 mResizingWindows.clear();
8594 }
Romain Guy06882f82009-06-10 13:36:04 -07008595
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008596 // Destroy the surface of any windows that are no longer visible.
8597 i = mDestroySurface.size();
8598 if (i > 0) {
8599 do {
8600 i--;
8601 WindowState win = mDestroySurface.get(i);
8602 win.mDestroying = false;
8603 if (mInputMethodWindow == win) {
8604 mInputMethodWindow = null;
8605 }
8606 win.destroySurfaceLocked();
8607 } while (i > 0);
8608 mDestroySurface.clear();
8609 }
8610
8611 // Time to remove any exiting tokens?
8612 for (i=mExitingTokens.size()-1; i>=0; i--) {
8613 WindowToken token = mExitingTokens.get(i);
8614 if (!token.hasVisible) {
8615 mExitingTokens.remove(i);
8616 }
8617 }
8618
8619 // Time to remove any exiting applications?
8620 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
8621 AppWindowToken token = mExitingAppTokens.get(i);
8622 if (!token.hasVisible && !mClosingApps.contains(token)) {
8623 mAppTokens.remove(token);
8624 mExitingAppTokens.remove(i);
8625 }
8626 }
8627
8628 if (focusDisplayed) {
8629 mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
8630 }
8631 if (animating) {
8632 requestAnimationLocked(currentTime+(1000/60)-SystemClock.uptimeMillis());
8633 }
8634 mQueue.setHoldScreenLocked(holdScreen != null);
8635 if (screenBrightness < 0 || screenBrightness > 1.0f) {
8636 mPowerManager.setScreenBrightnessOverride(-1);
8637 } else {
8638 mPowerManager.setScreenBrightnessOverride((int)
8639 (screenBrightness * Power.BRIGHTNESS_ON));
8640 }
8641 if (holdScreen != mHoldingScreenOn) {
8642 mHoldingScreenOn = holdScreen;
8643 Message m = mH.obtainMessage(H.HOLD_SCREEN_CHANGED, holdScreen);
8644 mH.sendMessage(m);
8645 }
8646 }
8647
8648 void requestAnimationLocked(long delay) {
8649 if (!mAnimationPending) {
8650 mAnimationPending = true;
8651 mH.sendMessageDelayed(mH.obtainMessage(H.ANIMATE), delay);
8652 }
8653 }
Romain Guy06882f82009-06-10 13:36:04 -07008654
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008655 /**
8656 * Have the surface flinger show a surface, robustly dealing with
8657 * error conditions. In particular, if there is not enough memory
8658 * to show the surface, then we will try to get rid of other surfaces
8659 * in order to succeed.
Romain Guy06882f82009-06-10 13:36:04 -07008660 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008661 * @return Returns true if the surface was successfully shown.
8662 */
8663 boolean showSurfaceRobustlyLocked(WindowState win) {
8664 try {
8665 if (win.mSurface != null) {
8666 win.mSurface.show();
8667 }
8668 return true;
8669 } catch (RuntimeException e) {
8670 Log.w(TAG, "Failure showing surface " + win.mSurface + " in " + win);
8671 }
Romain Guy06882f82009-06-10 13:36:04 -07008672
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008673 reclaimSomeSurfaceMemoryLocked(win, "show");
Romain Guy06882f82009-06-10 13:36:04 -07008674
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008675 return false;
8676 }
Romain Guy06882f82009-06-10 13:36:04 -07008677
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008678 void reclaimSomeSurfaceMemoryLocked(WindowState win, String operation) {
8679 final Surface surface = win.mSurface;
Romain Guy06882f82009-06-10 13:36:04 -07008680
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008681 EventLog.writeEvent(LOG_WM_NO_SURFACE_MEMORY, win.toString(),
8682 win.mSession.mPid, operation);
Romain Guy06882f82009-06-10 13:36:04 -07008683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008684 if (mForceRemoves == null) {
8685 mForceRemoves = new ArrayList<WindowState>();
8686 }
Romain Guy06882f82009-06-10 13:36:04 -07008687
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008688 long callingIdentity = Binder.clearCallingIdentity();
8689 try {
8690 // There was some problem... first, do a sanity check of the
8691 // window list to make sure we haven't left any dangling surfaces
8692 // around.
8693 int N = mWindows.size();
8694 boolean leakedSurface = false;
8695 Log.i(TAG, "Out of memory for surface! Looking for leaks...");
8696 for (int i=0; i<N; i++) {
8697 WindowState ws = (WindowState)mWindows.get(i);
8698 if (ws.mSurface != null) {
8699 if (!mSessions.contains(ws.mSession)) {
8700 Log.w(TAG, "LEAKED SURFACE (session doesn't exist): "
8701 + ws + " surface=" + ws.mSurface
8702 + " token=" + win.mToken
8703 + " pid=" + ws.mSession.mPid
8704 + " uid=" + ws.mSession.mUid);
8705 ws.mSurface.clear();
8706 ws.mSurface = null;
8707 mForceRemoves.add(ws);
8708 i--;
8709 N--;
8710 leakedSurface = true;
8711 } else if (win.mAppToken != null && win.mAppToken.clientHidden) {
8712 Log.w(TAG, "LEAKED SURFACE (app token hidden): "
8713 + ws + " surface=" + ws.mSurface
8714 + " token=" + win.mAppToken);
8715 ws.mSurface.clear();
8716 ws.mSurface = null;
8717 leakedSurface = true;
8718 }
8719 }
8720 }
Romain Guy06882f82009-06-10 13:36:04 -07008721
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008722 boolean killedApps = false;
8723 if (!leakedSurface) {
8724 Log.w(TAG, "No leaked surfaces; killing applicatons!");
8725 SparseIntArray pidCandidates = new SparseIntArray();
8726 for (int i=0; i<N; i++) {
8727 WindowState ws = (WindowState)mWindows.get(i);
8728 if (ws.mSurface != null) {
8729 pidCandidates.append(ws.mSession.mPid, ws.mSession.mPid);
8730 }
8731 }
8732 if (pidCandidates.size() > 0) {
8733 int[] pids = new int[pidCandidates.size()];
8734 for (int i=0; i<pids.length; i++) {
8735 pids[i] = pidCandidates.keyAt(i);
8736 }
8737 try {
8738 if (mActivityManager.killPidsForMemory(pids)) {
8739 killedApps = true;
8740 }
8741 } catch (RemoteException e) {
8742 }
8743 }
8744 }
Romain Guy06882f82009-06-10 13:36:04 -07008745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008746 if (leakedSurface || killedApps) {
8747 // We managed to reclaim some memory, so get rid of the trouble
8748 // surface and ask the app to request another one.
8749 Log.w(TAG, "Looks like we have reclaimed some memory, clearing surface for retry.");
8750 if (surface != null) {
8751 surface.clear();
8752 win.mSurface = null;
8753 }
Romain Guy06882f82009-06-10 13:36:04 -07008754
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008755 try {
8756 win.mClient.dispatchGetNewSurface();
8757 } catch (RemoteException e) {
8758 }
8759 }
8760 } finally {
8761 Binder.restoreCallingIdentity(callingIdentity);
8762 }
8763 }
Romain Guy06882f82009-06-10 13:36:04 -07008764
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008765 private boolean updateFocusedWindowLocked(int mode) {
8766 WindowState newFocus = computeFocusedWindowLocked();
8767 if (mCurrentFocus != newFocus) {
8768 // This check makes sure that we don't already have the focus
8769 // change message pending.
8770 mH.removeMessages(H.REPORT_FOCUS_CHANGE);
8771 mH.sendEmptyMessage(H.REPORT_FOCUS_CHANGE);
8772 if (localLOGV) Log.v(
8773 TAG, "Changing focus from " + mCurrentFocus + " to " + newFocus);
8774 final WindowState oldFocus = mCurrentFocus;
8775 mCurrentFocus = newFocus;
8776 mLosingFocus.remove(newFocus);
Romain Guy06882f82009-06-10 13:36:04 -07008777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008778 final WindowState imWindow = mInputMethodWindow;
8779 if (newFocus != imWindow && oldFocus != imWindow) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08008780 if (moveInputMethodWindowsIfNeededLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008781 mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS &&
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08008782 mode != UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
8783 mLayoutNeeded = true;
8784 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008785 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
8786 performLayoutLockedInner();
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08008787 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
8788 // Client will do the layout, but we need to assign layers
8789 // for handleNewWindowLocked() below.
8790 assignLayersLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008791 }
8792 }
Romain Guy06882f82009-06-10 13:36:04 -07008793
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08008794 if (newFocus != null && mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
8795 mKeyWaiter.handleNewWindowLocked(newFocus);
8796 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008797 return true;
8798 }
8799 return false;
8800 }
8801
8802 private WindowState computeFocusedWindowLocked() {
8803 WindowState result = null;
8804 WindowState win;
8805
8806 int i = mWindows.size() - 1;
8807 int nextAppIndex = mAppTokens.size()-1;
8808 WindowToken nextApp = nextAppIndex >= 0
8809 ? mAppTokens.get(nextAppIndex) : null;
8810
8811 while (i >= 0) {
8812 win = (WindowState)mWindows.get(i);
8813
8814 if (localLOGV || DEBUG_FOCUS) Log.v(
8815 TAG, "Looking for focus: " + i
8816 + " = " + win
8817 + ", flags=" + win.mAttrs.flags
8818 + ", canReceive=" + win.canReceiveKeys());
8819
8820 AppWindowToken thisApp = win.mAppToken;
Romain Guy06882f82009-06-10 13:36:04 -07008821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008822 // If this window's application has been removed, just skip it.
8823 if (thisApp != null && thisApp.removed) {
8824 i--;
8825 continue;
8826 }
Romain Guy06882f82009-06-10 13:36:04 -07008827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008828 // If there is a focused app, don't allow focus to go to any
8829 // windows below it. If this is an application window, step
8830 // through the app tokens until we find its app.
8831 if (thisApp != null && nextApp != null && thisApp != nextApp
8832 && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
8833 int origAppIndex = nextAppIndex;
8834 while (nextAppIndex > 0) {
8835 if (nextApp == mFocusedApp) {
8836 // Whoops, we are below the focused app... no focus
8837 // for you!
8838 if (localLOGV || DEBUG_FOCUS) Log.v(
8839 TAG, "Reached focused app: " + mFocusedApp);
8840 return null;
8841 }
8842 nextAppIndex--;
8843 nextApp = mAppTokens.get(nextAppIndex);
8844 if (nextApp == thisApp) {
8845 break;
8846 }
8847 }
8848 if (thisApp != nextApp) {
8849 // Uh oh, the app token doesn't exist! This shouldn't
8850 // happen, but if it does we can get totally hosed...
8851 // so restart at the original app.
8852 nextAppIndex = origAppIndex;
8853 nextApp = mAppTokens.get(nextAppIndex);
8854 }
8855 }
8856
8857 // Dispatch to this window if it is wants key events.
8858 if (win.canReceiveKeys()) {
8859 if (DEBUG_FOCUS) Log.v(
8860 TAG, "Found focus @ " + i + " = " + win);
8861 result = win;
8862 break;
8863 }
8864
8865 i--;
8866 }
8867
8868 return result;
8869 }
8870
8871 private void startFreezingDisplayLocked() {
8872 if (mDisplayFrozen) {
Chris Tate2ad63a92009-03-25 17:36:48 -07008873 // Freezing the display also suspends key event delivery, to
8874 // keep events from going astray while the display is reconfigured.
8875 // If someone has changed orientation again while the screen is
8876 // still frozen, the events will continue to be blocked while the
8877 // successive orientation change is processed. To prevent spurious
8878 // ANRs, we reset the event dispatch timeout in this case.
8879 synchronized (mKeyWaiter) {
8880 mKeyWaiter.mWasFrozen = true;
8881 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008882 return;
8883 }
Romain Guy06882f82009-06-10 13:36:04 -07008884
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008885 mScreenFrozenLock.acquire();
Romain Guy06882f82009-06-10 13:36:04 -07008886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008887 long now = SystemClock.uptimeMillis();
8888 //Log.i(TAG, "Freezing, gc pending: " + mFreezeGcPending + ", now " + now);
8889 if (mFreezeGcPending != 0) {
8890 if (now > (mFreezeGcPending+1000)) {
8891 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
8892 mH.removeMessages(H.FORCE_GC);
8893 Runtime.getRuntime().gc();
8894 mFreezeGcPending = now;
8895 }
8896 } else {
8897 mFreezeGcPending = now;
8898 }
Romain Guy06882f82009-06-10 13:36:04 -07008899
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008900 mDisplayFrozen = true;
8901 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
8902 mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
8903 mAppTransitionReady = true;
8904 }
Romain Guy06882f82009-06-10 13:36:04 -07008905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008906 if (PROFILE_ORIENTATION) {
8907 File file = new File("/data/system/frozen");
8908 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
8909 }
8910 Surface.freezeDisplay(0);
8911 }
Romain Guy06882f82009-06-10 13:36:04 -07008912
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008913 private void stopFreezingDisplayLocked() {
8914 if (!mDisplayFrozen) {
8915 return;
8916 }
Romain Guy06882f82009-06-10 13:36:04 -07008917
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008918 mDisplayFrozen = false;
8919 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
8920 if (PROFILE_ORIENTATION) {
8921 Debug.stopMethodTracing();
8922 }
8923 Surface.unfreezeDisplay(0);
Romain Guy06882f82009-06-10 13:36:04 -07008924
Chris Tate2ad63a92009-03-25 17:36:48 -07008925 // Reset the key delivery timeout on unfreeze, too. We force a wakeup here
8926 // too because regular key delivery processing should resume immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008927 synchronized (mKeyWaiter) {
8928 mKeyWaiter.mWasFrozen = true;
8929 mKeyWaiter.notifyAll();
8930 }
8931
8932 // A little kludge: a lot could have happened while the
8933 // display was frozen, so now that we are coming back we
8934 // do a gc so that any remote references the system
8935 // processes holds on others can be released if they are
8936 // no longer needed.
8937 mH.removeMessages(H.FORCE_GC);
8938 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
8939 2000);
Romain Guy06882f82009-06-10 13:36:04 -07008940
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008941 mScreenFrozenLock.release();
8942 }
Romain Guy06882f82009-06-10 13:36:04 -07008943
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008944 @Override
8945 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
8946 if (mContext.checkCallingOrSelfPermission("android.permission.DUMP")
8947 != PackageManager.PERMISSION_GRANTED) {
8948 pw.println("Permission Denial: can't dump WindowManager from from pid="
8949 + Binder.getCallingPid()
8950 + ", uid=" + Binder.getCallingUid());
8951 return;
8952 }
Romain Guy06882f82009-06-10 13:36:04 -07008953
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008954 synchronized(mWindowMap) {
8955 pw.println("Current Window Manager state:");
8956 for (int i=mWindows.size()-1; i>=0; i--) {
8957 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008958 pw.print(" Window #"); pw.print(i); pw.print(' ');
8959 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008960 w.dump(pw, " ");
8961 }
8962 if (mInputMethodDialogs.size() > 0) {
8963 pw.println(" ");
8964 pw.println(" Input method dialogs:");
8965 for (int i=mInputMethodDialogs.size()-1; i>=0; i--) {
8966 WindowState w = mInputMethodDialogs.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008967 pw.print(" IM Dialog #"); pw.print(i); pw.print(": "); pw.println(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008968 }
8969 }
8970 if (mPendingRemove.size() > 0) {
8971 pw.println(" ");
8972 pw.println(" Remove pending for:");
8973 for (int i=mPendingRemove.size()-1; i>=0; i--) {
8974 WindowState w = mPendingRemove.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008975 pw.print(" Remove #"); pw.print(i); pw.print(' ');
8976 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008977 w.dump(pw, " ");
8978 }
8979 }
8980 if (mForceRemoves != null && mForceRemoves.size() > 0) {
8981 pw.println(" ");
8982 pw.println(" Windows force removing:");
8983 for (int i=mForceRemoves.size()-1; i>=0; i--) {
8984 WindowState w = mForceRemoves.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008985 pw.print(" Removing #"); pw.print(i); pw.print(' ');
8986 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008987 w.dump(pw, " ");
8988 }
8989 }
8990 if (mDestroySurface.size() > 0) {
8991 pw.println(" ");
8992 pw.println(" Windows waiting to destroy their surface:");
8993 for (int i=mDestroySurface.size()-1; i>=0; i--) {
8994 WindowState w = mDestroySurface.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008995 pw.print(" Destroy #"); pw.print(i); pw.print(' ');
8996 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008997 w.dump(pw, " ");
8998 }
8999 }
9000 if (mLosingFocus.size() > 0) {
9001 pw.println(" ");
9002 pw.println(" Windows losing focus:");
9003 for (int i=mLosingFocus.size()-1; i>=0; i--) {
9004 WindowState w = mLosingFocus.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009005 pw.print(" Losing #"); pw.print(i); pw.print(' ');
9006 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009007 w.dump(pw, " ");
9008 }
9009 }
9010 if (mSessions.size() > 0) {
9011 pw.println(" ");
9012 pw.println(" All active sessions:");
9013 Iterator<Session> it = mSessions.iterator();
9014 while (it.hasNext()) {
9015 Session s = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009016 pw.print(" Session "); pw.print(s); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009017 s.dump(pw, " ");
9018 }
9019 }
9020 if (mTokenMap.size() > 0) {
9021 pw.println(" ");
9022 pw.println(" All tokens:");
9023 Iterator<WindowToken> it = mTokenMap.values().iterator();
9024 while (it.hasNext()) {
9025 WindowToken token = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009026 pw.print(" Token "); pw.print(token.token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009027 token.dump(pw, " ");
9028 }
9029 }
9030 if (mTokenList.size() > 0) {
9031 pw.println(" ");
9032 pw.println(" Window token list:");
9033 for (int i=0; i<mTokenList.size(); i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009034 pw.print(" #"); pw.print(i); pw.print(": ");
9035 pw.println(mTokenList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009036 }
9037 }
9038 if (mAppTokens.size() > 0) {
9039 pw.println(" ");
9040 pw.println(" Application tokens in Z order:");
9041 for (int i=mAppTokens.size()-1; i>=0; i--) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009042 pw.print(" App #"); pw.print(i); pw.print(": ");
9043 pw.println(mAppTokens.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009044 }
9045 }
9046 if (mFinishedStarting.size() > 0) {
9047 pw.println(" ");
9048 pw.println(" Finishing start of application tokens:");
9049 for (int i=mFinishedStarting.size()-1; i>=0; i--) {
9050 WindowToken token = mFinishedStarting.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009051 pw.print(" Finished Starting #"); pw.print(i);
9052 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009053 token.dump(pw, " ");
9054 }
9055 }
9056 if (mExitingTokens.size() > 0) {
9057 pw.println(" ");
9058 pw.println(" Exiting tokens:");
9059 for (int i=mExitingTokens.size()-1; i>=0; i--) {
9060 WindowToken token = mExitingTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009061 pw.print(" Exiting #"); pw.print(i);
9062 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009063 token.dump(pw, " ");
9064 }
9065 }
9066 if (mExitingAppTokens.size() > 0) {
9067 pw.println(" ");
9068 pw.println(" Exiting application tokens:");
9069 for (int i=mExitingAppTokens.size()-1; i>=0; i--) {
9070 WindowToken token = mExitingAppTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009071 pw.print(" Exiting App #"); pw.print(i);
9072 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009073 token.dump(pw, " ");
9074 }
9075 }
9076 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009077 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
9078 pw.print(" mLastFocus="); pw.println(mLastFocus);
9079 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
9080 pw.print(" mInputMethodTarget="); pw.println(mInputMethodTarget);
9081 pw.print(" mInputMethodWindow="); pw.println(mInputMethodWindow);
9082 pw.print(" mInTouchMode="); pw.println(mInTouchMode);
9083 pw.print(" mSystemBooted="); pw.print(mSystemBooted);
9084 pw.print(" mDisplayEnabled="); pw.println(mDisplayEnabled);
9085 pw.print(" mLayoutNeeded="); pw.print(mLayoutNeeded);
9086 pw.print(" mBlurShown="); pw.println(mBlurShown);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009087 if (mDimAnimator != null) {
9088 mDimAnimator.printTo(pw);
9089 } else {
9090 pw.print( " no DimAnimator ");
9091 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009092 pw.print(" mInputMethodAnimLayerAdjustment=");
9093 pw.println(mInputMethodAnimLayerAdjustment);
9094 pw.print(" mDisplayFrozen="); pw.print(mDisplayFrozen);
9095 pw.print(" mWindowsFreezingScreen="); pw.print(mWindowsFreezingScreen);
9096 pw.print(" mAppsFreezingScreen="); pw.println(mAppsFreezingScreen);
9097 pw.print(" mRotation="); pw.print(mRotation);
9098 pw.print(", mForcedAppOrientation="); pw.print(mForcedAppOrientation);
9099 pw.print(", mRequestedRotation="); pw.println(mRequestedRotation);
9100 pw.print(" mAnimationPending="); pw.print(mAnimationPending);
9101 pw.print(" mWindowAnimationScale="); pw.print(mWindowAnimationScale);
9102 pw.print(" mTransitionWindowAnimationScale="); pw.println(mTransitionAnimationScale);
9103 pw.print(" mNextAppTransition=0x");
9104 pw.print(Integer.toHexString(mNextAppTransition));
9105 pw.print(", mAppTransitionReady="); pw.print(mAppTransitionReady);
9106 pw.print(", mAppTransitionTimeout="); pw.println( mAppTransitionTimeout);
9107 pw.print(" mStartingIconInTransition="); pw.print(mStartingIconInTransition);
9108 pw.print(", mSkipAppTransitionAnimation="); pw.println(mSkipAppTransitionAnimation);
9109 if (mOpeningApps.size() > 0) {
9110 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
9111 }
9112 if (mClosingApps.size() > 0) {
9113 pw.print(" mClosingApps="); pw.println(mClosingApps);
9114 }
9115 pw.print(" DisplayWidth="); pw.print(mDisplay.getWidth());
9116 pw.print(" DisplayHeight="); pw.println(mDisplay.getHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009117 pw.println(" KeyWaiter state:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009118 pw.print(" mLastWin="); pw.print(mKeyWaiter.mLastWin);
9119 pw.print(" mLastBinder="); pw.println(mKeyWaiter.mLastBinder);
9120 pw.print(" mFinished="); pw.print(mKeyWaiter.mFinished);
9121 pw.print(" mGotFirstWindow="); pw.print(mKeyWaiter.mGotFirstWindow);
9122 pw.print(" mEventDispatching="); pw.print(mKeyWaiter.mEventDispatching);
9123 pw.print(" mTimeToSwitch="); pw.println(mKeyWaiter.mTimeToSwitch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009124 }
9125 }
9126
9127 public void monitor() {
9128 synchronized (mWindowMap) { }
9129 synchronized (mKeyguardDisabled) { }
9130 synchronized (mKeyWaiter) { }
9131 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009132
9133 /**
9134 * DimAnimator class that controls the dim animation. This holds the surface and
9135 * all state used for dim animation.
9136 */
9137 private static class DimAnimator {
9138 Surface mDimSurface;
9139 boolean mDimShown = false;
9140 float mDimCurrentAlpha;
9141 float mDimTargetAlpha;
9142 float mDimDeltaPerMs;
9143 long mLastDimAnimTime;
9144
9145 DimAnimator (SurfaceSession session) {
9146 if (mDimSurface == null) {
9147 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
9148 + mDimSurface + ": CREATE");
9149 try {
9150 mDimSurface = new Surface(session, 0, -1, 16, 16, PixelFormat.OPAQUE,
9151 Surface.FX_SURFACE_DIM);
9152 } catch (Exception e) {
9153 Log.e(TAG, "Exception creating Dim surface", e);
9154 }
9155 }
9156 }
9157
9158 /**
9159 * Show the dim surface.
9160 */
9161 void show(int dw, int dh) {
9162 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface + ": SHOW pos=(0,0) (" +
9163 dw + "x" + dh + ")");
9164 mDimShown = true;
9165 try {
9166 mDimSurface.setPosition(0, 0);
9167 mDimSurface.setSize(dw, dh);
9168 mDimSurface.show();
9169 } catch (RuntimeException e) {
9170 Log.w(TAG, "Failure showing dim surface", e);
9171 }
9172 }
9173
9174 /**
9175 * Set's the dim surface's layer and update dim parameters that will be used in
9176 * {@link updateSurface} after all windows are examined.
9177 */
9178 void updateParameters(WindowState w, long currentTime) {
9179 mDimSurface.setLayer(w.mAnimLayer-1);
9180
9181 final float target = w.mExiting ? 0 : w.mAttrs.dimAmount;
9182 if (SHOW_TRANSACTIONS) Log.i(TAG, "layer=" + (w.mAnimLayer-1) + ", target=" + target);
9183 if (mDimTargetAlpha != target) {
9184 // If the desired dim level has changed, then
9185 // start an animation to it.
9186 mLastDimAnimTime = currentTime;
9187 long duration = (w.mAnimating && w.mAnimation != null)
9188 ? w.mAnimation.computeDurationHint()
9189 : DEFAULT_DIM_DURATION;
9190 if (target > mDimTargetAlpha) {
9191 // This is happening behind the activity UI,
9192 // so we can make it run a little longer to
9193 // give a stronger impression without disrupting
9194 // the user.
9195 duration *= DIM_DURATION_MULTIPLIER;
9196 }
9197 if (duration < 1) {
9198 // Don't divide by zero
9199 duration = 1;
9200 }
9201 mDimTargetAlpha = target;
9202 mDimDeltaPerMs = (mDimTargetAlpha-mDimCurrentAlpha) / duration;
9203 }
9204 }
9205
9206 /**
9207 * Updating the surface's alpha. Returns true if the animation continues, or returns
9208 * false when the animation is finished and the dim surface is hidden.
9209 */
9210 boolean updateSurface(boolean dimming, long currentTime, boolean displayFrozen) {
9211 if (!dimming) {
9212 if (mDimTargetAlpha != 0) {
9213 mLastDimAnimTime = currentTime;
9214 mDimTargetAlpha = 0;
9215 mDimDeltaPerMs = (-mDimCurrentAlpha) / DEFAULT_DIM_DURATION;
9216 }
9217 }
9218
9219 boolean animating = false;
9220 if (mLastDimAnimTime != 0) {
9221 mDimCurrentAlpha += mDimDeltaPerMs
9222 * (currentTime-mLastDimAnimTime);
9223 boolean more = true;
9224 if (displayFrozen) {
9225 // If the display is frozen, there is no reason to animate.
9226 more = false;
9227 } else if (mDimDeltaPerMs > 0) {
9228 if (mDimCurrentAlpha > mDimTargetAlpha) {
9229 more = false;
9230 }
9231 } else if (mDimDeltaPerMs < 0) {
9232 if (mDimCurrentAlpha < mDimTargetAlpha) {
9233 more = false;
9234 }
9235 } else {
9236 more = false;
9237 }
9238
9239 // Do we need to continue animating?
9240 if (more) {
9241 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
9242 + mDimSurface + ": alpha=" + mDimCurrentAlpha);
9243 mLastDimAnimTime = currentTime;
9244 mDimSurface.setAlpha(mDimCurrentAlpha);
9245 animating = true;
9246 } else {
9247 mDimCurrentAlpha = mDimTargetAlpha;
9248 mLastDimAnimTime = 0;
9249 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
9250 + mDimSurface + ": final alpha=" + mDimCurrentAlpha);
9251 mDimSurface.setAlpha(mDimCurrentAlpha);
9252 if (!dimming) {
9253 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
9254 + ": HIDE");
9255 try {
9256 mDimSurface.hide();
9257 } catch (RuntimeException e) {
9258 Log.w(TAG, "Illegal argument exception hiding dim surface");
9259 }
9260 mDimShown = false;
9261 }
9262 }
9263 }
9264 return animating;
9265 }
9266
9267 public void printTo(PrintWriter pw) {
9268 pw.print(" mDimShown="); pw.print(mDimShown);
9269 pw.print(" current="); pw.print(mDimCurrentAlpha);
9270 pw.print(" target="); pw.print(mDimTargetAlpha);
9271 pw.print(" delta="); pw.print(mDimDeltaPerMs);
9272 pw.print(" lastAnimTime="); pw.println(mLastDimAnimTime);
9273 }
9274 }
9275
9276 /**
9277 * Animation that fade in after 0.5 interpolate time, or fade out in reverse order.
9278 * This is used for opening/closing transition for apps in compatible mode.
9279 */
9280 private static class FadeInOutAnimation extends Animation {
9281 int mWidth;
9282 boolean mFadeIn;
9283
9284 public FadeInOutAnimation(boolean fadeIn) {
9285 setInterpolator(new AccelerateInterpolator());
9286 setDuration(DEFAULT_FADE_IN_OUT_DURATION);
9287 mFadeIn = fadeIn;
9288 }
9289
9290 @Override
9291 protected void applyTransformation(float interpolatedTime, Transformation t) {
9292 float x = interpolatedTime;
9293 if (!mFadeIn) {
9294 x = 1.0f - x; // reverse the interpolation for fade out
9295 }
9296 if (x < 0.5) {
9297 // move the window out of the screen.
9298 t.getMatrix().setTranslate(mWidth, 0);
9299 } else {
9300 t.getMatrix().setTranslate(0, 0);// show
9301 t.setAlpha((x - 0.5f) * 2);
9302 }
9303 }
9304
9305 @Override
9306 public void initialize(int width, int height, int parentWidth, int parentHeight) {
9307 // width is the screen width {@see AppWindowToken#stepAnimatinoLocked}
9308 mWidth = width;
9309 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009310
9311 @Override
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07009312 public int getZAdjustment() {
9313 return Animation.ZORDER_TOP;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009314 }
9315 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009316}