blob: 1b7efeb36df739a0dfae0a9de66516efa23e2396 [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;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070034import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import static android.view.WindowManager.LayoutParams.LAST_APPLICATION_WINDOW;
36import static android.view.WindowManager.LayoutParams.LAST_SUB_WINDOW;
37import static android.view.WindowManager.LayoutParams.MEMORY_TYPE_GPU;
38import static android.view.WindowManager.LayoutParams.MEMORY_TYPE_HARDWARE;
39import static android.view.WindowManager.LayoutParams.MEMORY_TYPE_PUSH_BUFFERS;
40import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
41import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
42import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
43import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070044import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045
46import com.android.internal.app.IBatteryStats;
47import com.android.internal.policy.PolicyManager;
48import com.android.internal.view.IInputContext;
49import com.android.internal.view.IInputMethodClient;
50import com.android.internal.view.IInputMethodManager;
51import com.android.server.KeyInputQueue.QueuedEvent;
52import com.android.server.am.BatteryStatsService;
53
54import android.Manifest;
55import android.app.ActivityManagerNative;
56import android.app.IActivityManager;
57import android.content.Context;
58import android.content.pm.ActivityInfo;
59import android.content.pm.PackageManager;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070060import android.content.res.CompatibilityInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.content.res.Configuration;
62import android.graphics.Matrix;
63import android.graphics.PixelFormat;
64import android.graphics.Rect;
65import android.graphics.Region;
66import android.os.BatteryStats;
67import android.os.Binder;
68import android.os.Debug;
69import android.os.Handler;
70import android.os.IBinder;
Michael Chan53071d62009-05-13 17:29:48 -070071import android.os.LatencyTimer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072import android.os.LocalPowerManager;
73import android.os.Looper;
74import android.os.Message;
75import android.os.Parcel;
76import android.os.ParcelFileDescriptor;
77import android.os.Power;
78import android.os.PowerManager;
79import android.os.Process;
80import android.os.RemoteException;
81import android.os.ServiceManager;
82import android.os.SystemClock;
83import android.os.SystemProperties;
84import android.os.TokenWatcher;
85import android.provider.Settings;
Dianne Hackborn723738c2009-06-25 19:48:04 -070086import android.util.DisplayMetrics;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087import android.util.EventLog;
88import android.util.Log;
89import android.util.SparseIntArray;
90import android.view.Display;
91import android.view.Gravity;
92import android.view.IApplicationToken;
93import android.view.IOnKeyguardExitResult;
94import android.view.IRotationWatcher;
95import android.view.IWindow;
96import android.view.IWindowManager;
97import android.view.IWindowSession;
98import android.view.KeyEvent;
99import android.view.MotionEvent;
100import android.view.RawInputEvent;
101import android.view.Surface;
102import android.view.SurfaceSession;
103import android.view.View;
104import android.view.ViewTreeObserver;
105import android.view.WindowManager;
106import android.view.WindowManagerImpl;
107import android.view.WindowManagerPolicy;
108import android.view.WindowManager.LayoutParams;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700109import android.view.animation.AccelerateInterpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110import android.view.animation.Animation;
111import android.view.animation.AnimationUtils;
112import android.view.animation.Transformation;
113
114import java.io.BufferedWriter;
115import java.io.File;
116import java.io.FileDescriptor;
117import java.io.IOException;
118import java.io.OutputStream;
119import java.io.OutputStreamWriter;
120import java.io.PrintWriter;
121import java.io.StringWriter;
122import java.net.Socket;
123import java.util.ArrayList;
124import java.util.HashMap;
125import java.util.HashSet;
126import java.util.Iterator;
127import java.util.List;
128
129/** {@hide} */
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700130public class WindowManagerService extends IWindowManager.Stub
131 implements Watchdog.Monitor, KeyInputQueue.HapticFeedbackCallback {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 static final String TAG = "WindowManager";
133 static final boolean DEBUG = false;
134 static final boolean DEBUG_FOCUS = false;
135 static final boolean DEBUG_ANIM = false;
136 static final boolean DEBUG_LAYERS = false;
137 static final boolean DEBUG_INPUT = false;
138 static final boolean DEBUG_INPUT_METHOD = false;
139 static final boolean DEBUG_VISIBILITY = false;
140 static final boolean DEBUG_ORIENTATION = false;
141 static final boolean DEBUG_APP_TRANSITIONS = false;
142 static final boolean DEBUG_STARTING_WINDOW = false;
143 static final boolean DEBUG_REORDER = false;
144 static final boolean SHOW_TRANSACTIONS = false;
Michael Chan53071d62009-05-13 17:29:48 -0700145 static final boolean MEASURE_LATENCY = false;
146 static private LatencyTimer lt;
147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 static final boolean PROFILE_ORIENTATION = false;
149 static final boolean BLUR = true;
Dave Bortcfe65242009-04-09 14:51:04 -0700150 static final boolean localLOGV = DEBUG;
Romain Guy06882f82009-06-10 13:36:04 -0700151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 static final int LOG_WM_NO_SURFACE_MEMORY = 31000;
Romain Guy06882f82009-06-10 13:36:04 -0700153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 /** How long to wait for first key repeat, in milliseconds */
155 static final int KEY_REPEAT_FIRST_DELAY = 750;
Romain Guy06882f82009-06-10 13:36:04 -0700156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 /** How long to wait for subsequent key repeats, in milliseconds */
158 static final int KEY_REPEAT_DELAY = 50;
159
160 /** How much to multiply the policy's type layer, to reserve room
161 * for multiple windows of the same type and Z-ordering adjustment
162 * with TYPE_LAYER_OFFSET. */
163 static final int TYPE_LAYER_MULTIPLIER = 10000;
Romain Guy06882f82009-06-10 13:36:04 -0700164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 /** Offset from TYPE_LAYER_MULTIPLIER for moving a group of windows above
166 * or below others in the same layer. */
167 static final int TYPE_LAYER_OFFSET = 1000;
Romain Guy06882f82009-06-10 13:36:04 -0700168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 /** How much to increment the layer for each window, to reserve room
170 * for effect surfaces between them.
171 */
172 static final int WINDOW_LAYER_MULTIPLIER = 5;
Romain Guy06882f82009-06-10 13:36:04 -0700173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 /** The maximum length we will accept for a loaded animation duration:
175 * this is 10 seconds.
176 */
177 static final int MAX_ANIMATION_DURATION = 10*1000;
178
179 /** Amount of time (in milliseconds) to animate the dim surface from one
180 * value to another, when no window animation is driving it.
181 */
182 static final int DEFAULT_DIM_DURATION = 200;
183
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700184 /** Amount of time (in milliseconds) to animate the fade-in-out transition for
185 * compatible windows.
186 */
187 static final int DEFAULT_FADE_IN_OUT_DURATION = 400;
188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 /** Adjustment to time to perform a dim, to make it more dramatic.
190 */
191 static final int DIM_DURATION_MULTIPLIER = 6;
Romain Guy06882f82009-06-10 13:36:04 -0700192
Dianne Hackborncfaef692009-06-15 14:24:44 -0700193 static final int INJECT_FAILED = 0;
194 static final int INJECT_SUCCEEDED = 1;
195 static final int INJECT_NO_PERMISSION = -1;
196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 static final int UPDATE_FOCUS_NORMAL = 0;
198 static final int UPDATE_FOCUS_WILL_ASSIGN_LAYERS = 1;
199 static final int UPDATE_FOCUS_PLACING_SURFACES = 2;
200 static final int UPDATE_FOCUS_WILL_PLACE_SURFACES = 3;
Romain Guy06882f82009-06-10 13:36:04 -0700201
Michael Chane96440f2009-05-06 10:27:36 -0700202 /** The minimum time between dispatching touch events. */
203 int mMinWaitTimeBetweenTouchEvents = 1000 / 35;
204
205 // Last touch event time
206 long mLastTouchEventTime = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700207
Michael Chane96440f2009-05-06 10:27:36 -0700208 // Last touch event type
209 int mLastTouchEventType = OTHER_EVENT;
Romain Guy06882f82009-06-10 13:36:04 -0700210
Michael Chane96440f2009-05-06 10:27:36 -0700211 // Time to wait before calling useractivity again. This saves CPU usage
212 // when we get a flood of touch events.
213 static final int MIN_TIME_BETWEEN_USERACTIVITIES = 1000;
214
215 // Last time we call user activity
216 long mLastUserActivityCallTime = 0;
217
Romain Guy06882f82009-06-10 13:36:04 -0700218 // Last time we updated battery stats
Michael Chane96440f2009-05-06 10:27:36 -0700219 long mLastBatteryStatsCallTime = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 private static final String SYSTEM_SECURE = "ro.secure";
Romain Guy06882f82009-06-10 13:36:04 -0700222 private static final String SYSTEM_DEBUGGABLE = "ro.debuggable";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223
224 /**
225 * Condition waited on by {@link #reenableKeyguard} to know the call to
226 * the window policy has finished.
227 */
228 private boolean mWaitingUntilKeyguardReenabled = false;
229
230
231 final TokenWatcher mKeyguardDisabled = new TokenWatcher(
232 new Handler(), "WindowManagerService.mKeyguardDisabled") {
233 public void acquired() {
234 mPolicy.enableKeyguard(false);
235 }
236 public void released() {
237 synchronized (mKeyguardDisabled) {
238 mPolicy.enableKeyguard(true);
239 mWaitingUntilKeyguardReenabled = false;
240 mKeyguardDisabled.notifyAll();
241 }
242 }
243 };
244
245 final Context mContext;
246
247 final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 final boolean mLimitedAlphaCompositing;
Romain Guy06882f82009-06-10 13:36:04 -0700250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 final WindowManagerPolicy mPolicy = PolicyManager.makeNewWindowManager();
252
253 final IActivityManager mActivityManager;
Romain Guy06882f82009-06-10 13:36:04 -0700254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 final IBatteryStats mBatteryStats;
Romain Guy06882f82009-06-10 13:36:04 -0700256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 /**
258 * All currently active sessions with clients.
259 */
260 final HashSet<Session> mSessions = new HashSet<Session>();
Romain Guy06882f82009-06-10 13:36:04 -0700261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 /**
263 * Mapping from an IWindow IBinder to the server's Window object.
264 * This is also used as the lock for all of our state.
265 */
266 final HashMap<IBinder, WindowState> mWindowMap = new HashMap<IBinder, WindowState>();
267
268 /**
269 * Mapping from a token IBinder to a WindowToken object.
270 */
271 final HashMap<IBinder, WindowToken> mTokenMap =
272 new HashMap<IBinder, WindowToken>();
273
274 /**
275 * The same tokens as mTokenMap, stored in a list for efficient iteration
276 * over them.
277 */
278 final ArrayList<WindowToken> mTokenList = new ArrayList<WindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 /**
281 * Window tokens that are in the process of exiting, but still
282 * on screen for animations.
283 */
284 final ArrayList<WindowToken> mExitingTokens = new ArrayList<WindowToken>();
285
286 /**
287 * Z-ordered (bottom-most first) list of all application tokens, for
288 * controlling the ordering of windows in different applications. This
289 * contains WindowToken objects.
290 */
291 final ArrayList<AppWindowToken> mAppTokens = new ArrayList<AppWindowToken>();
292
293 /**
294 * Application tokens that are in the process of exiting, but still
295 * on screen for animations.
296 */
297 final ArrayList<AppWindowToken> mExitingAppTokens = new ArrayList<AppWindowToken>();
298
299 /**
300 * List of window tokens that have finished starting their application,
301 * and now need to have the policy remove their windows.
302 */
303 final ArrayList<AppWindowToken> mFinishedStarting = new ArrayList<AppWindowToken>();
304
305 /**
306 * Z-ordered (bottom-most first) list of all Window objects.
307 */
308 final ArrayList mWindows = new ArrayList();
309
310 /**
311 * Windows that are being resized. Used so we can tell the client about
312 * the resize after closing the transaction in which we resized the
313 * underlying surface.
314 */
315 final ArrayList<WindowState> mResizingWindows = new ArrayList<WindowState>();
316
317 /**
318 * Windows whose animations have ended and now must be removed.
319 */
320 final ArrayList<WindowState> mPendingRemove = new ArrayList<WindowState>();
321
322 /**
323 * Windows whose surface should be destroyed.
324 */
325 final ArrayList<WindowState> mDestroySurface = new ArrayList<WindowState>();
326
327 /**
328 * Windows that have lost input focus and are waiting for the new
329 * focus window to be displayed before they are told about this.
330 */
331 ArrayList<WindowState> mLosingFocus = new ArrayList<WindowState>();
332
333 /**
334 * This is set when we have run out of memory, and will either be an empty
335 * list or contain windows that need to be force removed.
336 */
337 ArrayList<WindowState> mForceRemoves;
Romain Guy06882f82009-06-10 13:36:04 -0700338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 IInputMethodManager mInputMethodManager;
Romain Guy06882f82009-06-10 13:36:04 -0700340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 SurfaceSession mFxSession;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700342 private DimAnimator mDimAnimator = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 Surface mBlurSurface;
344 boolean mBlurShown;
Romain Guy06882f82009-06-10 13:36:04 -0700345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 int mTransactionSequence = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 final float[] mTmpFloats = new float[9];
349
350 boolean mSafeMode;
351 boolean mDisplayEnabled = false;
352 boolean mSystemBooted = false;
353 int mRotation = 0;
354 int mRequestedRotation = 0;
355 int mForcedAppOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Dianne Hackborn321ae682009-03-27 16:16:03 -0700356 int mLastRotationFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 ArrayList<IRotationWatcher> mRotationWatchers
358 = new ArrayList<IRotationWatcher>();
Romain Guy06882f82009-06-10 13:36:04 -0700359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 boolean mLayoutNeeded = true;
361 boolean mAnimationPending = false;
362 boolean mDisplayFrozen = false;
363 boolean mWindowsFreezingScreen = false;
364 long mFreezeGcPending = 0;
365 int mAppsFreezingScreen = 0;
366
367 // This is held as long as we have the screen frozen, to give us time to
368 // perform a rotation animation when turning off shows the lock screen which
369 // changes the orientation.
370 PowerManager.WakeLock mScreenFrozenLock;
Romain Guy06882f82009-06-10 13:36:04 -0700371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 // State management of app transitions. When we are preparing for a
373 // transition, mNextAppTransition will be the kind of transition to
374 // perform or TRANSIT_NONE if we are not waiting. If we are waiting,
375 // mOpeningApps and mClosingApps are the lists of tokens that will be
376 // made visible or hidden at the next transition.
377 int mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
378 boolean mAppTransitionReady = false;
379 boolean mAppTransitionTimeout = false;
380 boolean mStartingIconInTransition = false;
381 boolean mSkipAppTransitionAnimation = false;
382 final ArrayList<AppWindowToken> mOpeningApps = new ArrayList<AppWindowToken>();
383 final ArrayList<AppWindowToken> mClosingApps = new ArrayList<AppWindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 //flag to detect fat touch events
386 boolean mFatTouch = false;
387 Display mDisplay;
Romain Guy06882f82009-06-10 13:36:04 -0700388
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 H mH = new H();
390
391 WindowState mCurrentFocus = null;
392 WindowState mLastFocus = null;
Romain Guy06882f82009-06-10 13:36:04 -0700393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 // This just indicates the window the input method is on top of, not
395 // necessarily the window its input is going to.
396 WindowState mInputMethodTarget = null;
397 WindowState mUpcomingInputMethodTarget = null;
398 boolean mInputMethodTargetWaitingAnim;
399 int mInputMethodAnimLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -0700400
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 WindowState mInputMethodWindow = null;
402 final ArrayList<WindowState> mInputMethodDialogs = new ArrayList<WindowState>();
403
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700404 final ArrayList<WindowToken> mWallpaperTokens = new ArrayList<WindowToken>();
405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 AppWindowToken mFocusedApp = null;
407
408 PowerManagerService mPowerManager;
Romain Guy06882f82009-06-10 13:36:04 -0700409
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 float mWindowAnimationScale = 1.0f;
411 float mTransitionAnimationScale = 1.0f;
Romain Guy06882f82009-06-10 13:36:04 -0700412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 final KeyWaiter mKeyWaiter = new KeyWaiter();
414 final KeyQ mQueue;
415 final InputDispatcherThread mInputThread;
416
417 // Who is holding the screen on.
418 Session mHoldingScreenOn;
Romain Guy06882f82009-06-10 13:36:04 -0700419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 /**
421 * Whether the UI is currently running in touch mode (not showing
422 * navigational focus because the user is directly pressing the screen).
423 */
424 boolean mInTouchMode = false;
425
426 private ViewServer mViewServer;
427
428 final Rect mTempRect = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -0700429
Dianne Hackbornc485a602009-03-24 22:39:49 -0700430 final Configuration mTempConfiguration = new Configuration();
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700431 int mScreenLayout = Configuration.SCREENLAYOUT_SIZE_UNDEFINED;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -0700432
433 // The frame use to limit the size of the app running in compatibility mode.
434 Rect mCompatibleScreenFrame = new Rect();
435 // The surface used to fill the outer rim of the app running in compatibility mode.
436 Surface mBackgroundFillerSurface = null;
437 boolean mBackgroundFillerShown = false;
438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 public static WindowManagerService main(Context context,
440 PowerManagerService pm, boolean haveInputMethods) {
441 WMThread thr = new WMThread(context, pm, haveInputMethods);
442 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 synchronized (thr) {
445 while (thr.mService == null) {
446 try {
447 thr.wait();
448 } catch (InterruptedException e) {
449 }
450 }
451 }
Romain Guy06882f82009-06-10 13:36:04 -0700452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 return thr.mService;
454 }
Romain Guy06882f82009-06-10 13:36:04 -0700455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 static class WMThread extends Thread {
457 WindowManagerService mService;
Romain Guy06882f82009-06-10 13:36:04 -0700458
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 private final Context mContext;
460 private final PowerManagerService mPM;
461 private final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 public WMThread(Context context, PowerManagerService pm,
464 boolean haveInputMethods) {
465 super("WindowManager");
466 mContext = context;
467 mPM = pm;
468 mHaveInputMethods = haveInputMethods;
469 }
Romain Guy06882f82009-06-10 13:36:04 -0700470
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 public void run() {
472 Looper.prepare();
473 WindowManagerService s = new WindowManagerService(mContext, mPM,
474 mHaveInputMethods);
475 android.os.Process.setThreadPriority(
476 android.os.Process.THREAD_PRIORITY_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -0700477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 synchronized (this) {
479 mService = s;
480 notifyAll();
481 }
Romain Guy06882f82009-06-10 13:36:04 -0700482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 Looper.loop();
484 }
485 }
486
487 static class PolicyThread extends Thread {
488 private final WindowManagerPolicy mPolicy;
489 private final WindowManagerService mService;
490 private final Context mContext;
491 private final PowerManagerService mPM;
492 boolean mRunning = false;
Romain Guy06882f82009-06-10 13:36:04 -0700493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 public PolicyThread(WindowManagerPolicy policy,
495 WindowManagerService service, Context context,
496 PowerManagerService pm) {
497 super("WindowManagerPolicy");
498 mPolicy = policy;
499 mService = service;
500 mContext = context;
501 mPM = pm;
502 }
Romain Guy06882f82009-06-10 13:36:04 -0700503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 public void run() {
505 Looper.prepare();
506 //Looper.myLooper().setMessageLogging(new LogPrinter(
507 // Log.VERBOSE, "WindowManagerPolicy"));
508 android.os.Process.setThreadPriority(
509 android.os.Process.THREAD_PRIORITY_FOREGROUND);
510 mPolicy.init(mContext, mService, mPM);
Romain Guy06882f82009-06-10 13:36:04 -0700511
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512 synchronized (this) {
513 mRunning = true;
514 notifyAll();
515 }
Romain Guy06882f82009-06-10 13:36:04 -0700516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 Looper.loop();
518 }
519 }
520
521 private WindowManagerService(Context context, PowerManagerService pm,
522 boolean haveInputMethods) {
Michael Chan53071d62009-05-13 17:29:48 -0700523 if (MEASURE_LATENCY) {
524 lt = new LatencyTimer(100, 1000);
525 }
526
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 mContext = context;
528 mHaveInputMethods = haveInputMethods;
529 mLimitedAlphaCompositing = context.getResources().getBoolean(
530 com.android.internal.R.bool.config_sf_limitedAlpha);
Romain Guy06882f82009-06-10 13:36:04 -0700531
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 mPowerManager = pm;
533 mPowerManager.setPolicy(mPolicy);
534 PowerManager pmc = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
535 mScreenFrozenLock = pmc.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
536 "SCREEN_FROZEN");
537 mScreenFrozenLock.setReferenceCounted(false);
538
539 mActivityManager = ActivityManagerNative.getDefault();
540 mBatteryStats = BatteryStatsService.getService();
541
542 // Get persisted window scale setting
543 mWindowAnimationScale = Settings.System.getFloat(context.getContentResolver(),
544 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
545 mTransitionAnimationScale = Settings.System.getFloat(context.getContentResolver(),
546 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
Romain Guy06882f82009-06-10 13:36:04 -0700547
Michael Chan9f028e62009-08-04 17:37:46 -0700548 int max_events_per_sec = 35;
549 try {
550 max_events_per_sec = Integer.parseInt(SystemProperties
551 .get("windowsmgr.max_events_per_sec"));
552 if (max_events_per_sec < 1) {
553 max_events_per_sec = 35;
554 }
555 } catch (NumberFormatException e) {
556 }
557 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
558
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 mQueue = new KeyQ();
560
561 mInputThread = new InputDispatcherThread();
Romain Guy06882f82009-06-10 13:36:04 -0700562
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 PolicyThread thr = new PolicyThread(mPolicy, this, context, pm);
564 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700565
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 synchronized (thr) {
567 while (!thr.mRunning) {
568 try {
569 thr.wait();
570 } catch (InterruptedException e) {
571 }
572 }
573 }
Romain Guy06882f82009-06-10 13:36:04 -0700574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575 mInputThread.start();
Romain Guy06882f82009-06-10 13:36:04 -0700576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 // Add ourself to the Watchdog monitors.
578 Watchdog.getInstance().addMonitor(this);
579 }
580
581 @Override
582 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
583 throws RemoteException {
584 try {
585 return super.onTransact(code, data, reply, flags);
586 } catch (RuntimeException e) {
587 // The window manager only throws security exceptions, so let's
588 // log all others.
589 if (!(e instanceof SecurityException)) {
590 Log.e(TAG, "Window Manager Crash", e);
591 }
592 throw e;
593 }
594 }
595
596 private void placeWindowAfter(Object pos, WindowState window) {
597 final int i = mWindows.indexOf(pos);
598 if (localLOGV || DEBUG_FOCUS) Log.v(
599 TAG, "Adding window " + window + " at "
600 + (i+1) + " of " + mWindows.size() + " (after " + pos + ")");
601 mWindows.add(i+1, window);
602 }
603
604 private void placeWindowBefore(Object pos, WindowState window) {
605 final int i = mWindows.indexOf(pos);
606 if (localLOGV || DEBUG_FOCUS) Log.v(
607 TAG, "Adding window " + window + " at "
608 + i + " of " + mWindows.size() + " (before " + pos + ")");
609 mWindows.add(i, window);
610 }
611
612 //This method finds out the index of a window that has the same app token as
613 //win. used for z ordering the windows in mWindows
614 private int findIdxBasedOnAppTokens(WindowState win) {
615 //use a local variable to cache mWindows
616 ArrayList localmWindows = mWindows;
617 int jmax = localmWindows.size();
618 if(jmax == 0) {
619 return -1;
620 }
621 for(int j = (jmax-1); j >= 0; j--) {
622 WindowState wentry = (WindowState)localmWindows.get(j);
623 if(wentry.mAppToken == win.mAppToken) {
624 return j;
625 }
626 }
627 return -1;
628 }
Romain Guy06882f82009-06-10 13:36:04 -0700629
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630 private void addWindowToListInOrderLocked(WindowState win, boolean addToToken) {
631 final IWindow client = win.mClient;
632 final WindowToken token = win.mToken;
633 final ArrayList localmWindows = mWindows;
Romain Guy06882f82009-06-10 13:36:04 -0700634
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 final int N = localmWindows.size();
636 final WindowState attached = win.mAttachedWindow;
637 int i;
638 if (attached == null) {
639 int tokenWindowsPos = token.windows.size();
640 if (token.appWindowToken != null) {
641 int index = tokenWindowsPos-1;
642 if (index >= 0) {
643 // If this application has existing windows, we
644 // simply place the new window on top of them... but
645 // keep the starting window on top.
646 if (win.mAttrs.type == TYPE_BASE_APPLICATION) {
647 // Base windows go behind everything else.
648 placeWindowBefore(token.windows.get(0), win);
649 tokenWindowsPos = 0;
650 } else {
651 AppWindowToken atoken = win.mAppToken;
652 if (atoken != null &&
653 token.windows.get(index) == atoken.startingWindow) {
654 placeWindowBefore(token.windows.get(index), win);
655 tokenWindowsPos--;
656 } else {
657 int newIdx = findIdxBasedOnAppTokens(win);
658 if(newIdx != -1) {
Romain Guy06882f82009-06-10 13:36:04 -0700659 //there is a window above this one associated with the same
660 //apptoken note that the window could be a floating window
661 //that was created later or a window at the top of the list of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800662 //windows associated with this token.
663 localmWindows.add(newIdx+1, win);
Romain Guy06882f82009-06-10 13:36:04 -0700664 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665 }
666 }
667 } else {
668 if (localLOGV) Log.v(
669 TAG, "Figuring out where to add app window "
670 + client.asBinder() + " (token=" + token + ")");
671 // Figure out where the window should go, based on the
672 // order of applications.
673 final int NA = mAppTokens.size();
674 Object pos = null;
675 for (i=NA-1; i>=0; i--) {
676 AppWindowToken t = mAppTokens.get(i);
677 if (t == token) {
678 i--;
679 break;
680 }
681 if (t.windows.size() > 0) {
682 pos = t.windows.get(0);
683 }
684 }
685 // We now know the index into the apps. If we found
686 // an app window above, that gives us the position; else
687 // we need to look some more.
688 if (pos != null) {
689 // Move behind any windows attached to this one.
Romain Guy06882f82009-06-10 13:36:04 -0700690 WindowToken atoken =
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691 mTokenMap.get(((WindowState)pos).mClient.asBinder());
692 if (atoken != null) {
693 final int NC = atoken.windows.size();
694 if (NC > 0) {
695 WindowState bottom = atoken.windows.get(0);
696 if (bottom.mSubLayer < 0) {
697 pos = bottom;
698 }
699 }
700 }
701 placeWindowBefore(pos, win);
702 } else {
703 while (i >= 0) {
704 AppWindowToken t = mAppTokens.get(i);
705 final int NW = t.windows.size();
706 if (NW > 0) {
707 pos = t.windows.get(NW-1);
708 break;
709 }
710 i--;
711 }
712 if (pos != null) {
713 // Move in front of any windows attached to this
714 // one.
715 WindowToken atoken =
716 mTokenMap.get(((WindowState)pos).mClient.asBinder());
717 if (atoken != null) {
718 final int NC = atoken.windows.size();
719 if (NC > 0) {
720 WindowState top = atoken.windows.get(NC-1);
721 if (top.mSubLayer >= 0) {
722 pos = top;
723 }
724 }
725 }
726 placeWindowAfter(pos, win);
727 } else {
728 // Just search for the start of this layer.
729 final int myLayer = win.mBaseLayer;
730 for (i=0; i<N; i++) {
731 WindowState w = (WindowState)localmWindows.get(i);
732 if (w.mBaseLayer > myLayer) {
733 break;
734 }
735 }
736 if (localLOGV || DEBUG_FOCUS) Log.v(
737 TAG, "Adding window " + win + " at "
738 + i + " of " + N);
739 localmWindows.add(i, win);
740 }
741 }
742 }
743 } else {
744 // Figure out where window should go, based on layer.
745 final int myLayer = win.mBaseLayer;
746 for (i=N-1; i>=0; i--) {
747 if (((WindowState)localmWindows.get(i)).mBaseLayer <= myLayer) {
748 i++;
749 break;
750 }
751 }
752 if (i < 0) i = 0;
753 if (localLOGV || DEBUG_FOCUS) Log.v(
754 TAG, "Adding window " + win + " at "
755 + i + " of " + N);
756 localmWindows.add(i, win);
757 }
758 if (addToToken) {
759 token.windows.add(tokenWindowsPos, win);
760 }
761
762 } else {
763 // Figure out this window's ordering relative to the window
764 // it is attached to.
765 final int NA = token.windows.size();
766 final int sublayer = win.mSubLayer;
767 int largestSublayer = Integer.MIN_VALUE;
768 WindowState windowWithLargestSublayer = null;
769 for (i=0; i<NA; i++) {
770 WindowState w = token.windows.get(i);
771 final int wSublayer = w.mSubLayer;
772 if (wSublayer >= largestSublayer) {
773 largestSublayer = wSublayer;
774 windowWithLargestSublayer = w;
775 }
776 if (sublayer < 0) {
777 // For negative sublayers, we go below all windows
778 // in the same sublayer.
779 if (wSublayer >= sublayer) {
780 if (addToToken) {
781 token.windows.add(i, win);
782 }
783 placeWindowBefore(
784 wSublayer >= 0 ? attached : w, win);
785 break;
786 }
787 } else {
788 // For positive sublayers, we go above all windows
789 // in the same sublayer.
790 if (wSublayer > sublayer) {
791 if (addToToken) {
792 token.windows.add(i, win);
793 }
794 placeWindowBefore(w, win);
795 break;
796 }
797 }
798 }
799 if (i >= NA) {
800 if (addToToken) {
801 token.windows.add(win);
802 }
803 if (sublayer < 0) {
804 placeWindowBefore(attached, win);
805 } else {
806 placeWindowAfter(largestSublayer >= 0
807 ? windowWithLargestSublayer
808 : attached,
809 win);
810 }
811 }
812 }
Romain Guy06882f82009-06-10 13:36:04 -0700813
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800814 if (win.mAppToken != null && addToToken) {
815 win.mAppToken.allAppWindows.add(win);
816 }
817 }
Romain Guy06882f82009-06-10 13:36:04 -0700818
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819 static boolean canBeImeTarget(WindowState w) {
820 final int fl = w.mAttrs.flags
821 & (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM);
822 if (fl == 0 || fl == (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM)) {
823 return w.isVisibleOrAdding();
824 }
825 return false;
826 }
Romain Guy06882f82009-06-10 13:36:04 -0700827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800828 int findDesiredInputMethodWindowIndexLocked(boolean willMove) {
829 final ArrayList localmWindows = mWindows;
830 final int N = localmWindows.size();
831 WindowState w = null;
832 int i = N;
833 while (i > 0) {
834 i--;
835 w = (WindowState)localmWindows.get(i);
Romain Guy06882f82009-06-10 13:36:04 -0700836
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837 //Log.i(TAG, "Checking window @" + i + " " + w + " fl=0x"
838 // + Integer.toHexString(w.mAttrs.flags));
839 if (canBeImeTarget(w)) {
840 //Log.i(TAG, "Putting input method here!");
Romain Guy06882f82009-06-10 13:36:04 -0700841
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800842 // Yet more tricksyness! If this window is a "starting"
843 // window, we do actually want to be on top of it, but
844 // it is not -really- where input will go. So if the caller
845 // is not actually looking to move the IME, look down below
846 // for a real window to target...
847 if (!willMove
848 && w.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
849 && i > 0) {
850 WindowState wb = (WindowState)localmWindows.get(i-1);
851 if (wb.mAppToken == w.mAppToken && canBeImeTarget(wb)) {
852 i--;
853 w = wb;
854 }
855 }
856 break;
857 }
858 }
Romain Guy06882f82009-06-10 13:36:04 -0700859
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800860 mUpcomingInputMethodTarget = w;
Romain Guy06882f82009-06-10 13:36:04 -0700861
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800862 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Desired input method target="
863 + w + " willMove=" + willMove);
Romain Guy06882f82009-06-10 13:36:04 -0700864
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800865 if (willMove && w != null) {
866 final WindowState curTarget = mInputMethodTarget;
867 if (curTarget != null && curTarget.mAppToken != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700868
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800869 // Now some fun for dealing with window animations that
870 // modify the Z order. We need to look at all windows below
871 // the current target that are in this app, finding the highest
872 // visible one in layering.
873 AppWindowToken token = curTarget.mAppToken;
874 WindowState highestTarget = null;
875 int highestPos = 0;
876 if (token.animating || token.animation != null) {
877 int pos = 0;
878 pos = localmWindows.indexOf(curTarget);
879 while (pos >= 0) {
880 WindowState win = (WindowState)localmWindows.get(pos);
881 if (win.mAppToken != token) {
882 break;
883 }
884 if (!win.mRemoved) {
885 if (highestTarget == null || win.mAnimLayer >
886 highestTarget.mAnimLayer) {
887 highestTarget = win;
888 highestPos = pos;
889 }
890 }
891 pos--;
892 }
893 }
Romain Guy06882f82009-06-10 13:36:04 -0700894
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800895 if (highestTarget != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700896 if (DEBUG_INPUT_METHOD) Log.v(TAG, "mNextAppTransition="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897 + mNextAppTransition + " " + highestTarget
898 + " animating=" + highestTarget.isAnimating()
899 + " layer=" + highestTarget.mAnimLayer
900 + " new layer=" + w.mAnimLayer);
Romain Guy06882f82009-06-10 13:36:04 -0700901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
903 // If we are currently setting up for an animation,
904 // hold everything until we can find out what will happen.
905 mInputMethodTargetWaitingAnim = true;
906 mInputMethodTarget = highestTarget;
907 return highestPos + 1;
908 } else if (highestTarget.isAnimating() &&
909 highestTarget.mAnimLayer > w.mAnimLayer) {
910 // If the window we are currently targeting is involved
911 // with an animation, and it is on top of the next target
912 // we will be over, then hold off on moving until
913 // that is done.
914 mInputMethodTarget = highestTarget;
915 return highestPos + 1;
916 }
917 }
918 }
919 }
Romain Guy06882f82009-06-10 13:36:04 -0700920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921 //Log.i(TAG, "Placing input method @" + (i+1));
922 if (w != null) {
923 if (willMove) {
924 RuntimeException e = new RuntimeException();
925 e.fillInStackTrace();
926 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
927 + mInputMethodTarget + " to " + w, e);
928 mInputMethodTarget = w;
929 if (w.mAppToken != null) {
930 setInputMethodAnimLayerAdjustment(w.mAppToken.animLayerAdjustment);
931 } else {
932 setInputMethodAnimLayerAdjustment(0);
933 }
934 }
935 return i+1;
936 }
937 if (willMove) {
938 RuntimeException e = new RuntimeException();
939 e.fillInStackTrace();
940 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
941 + mInputMethodTarget + " to null", e);
942 mInputMethodTarget = null;
943 setInputMethodAnimLayerAdjustment(0);
944 }
945 return -1;
946 }
Romain Guy06882f82009-06-10 13:36:04 -0700947
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948 void addInputMethodWindowToListLocked(WindowState win) {
949 int pos = findDesiredInputMethodWindowIndexLocked(true);
950 if (pos >= 0) {
951 win.mTargetAppToken = mInputMethodTarget.mAppToken;
952 mWindows.add(pos, win);
953 moveInputMethodDialogsLocked(pos+1);
954 return;
955 }
956 win.mTargetAppToken = null;
957 addWindowToListInOrderLocked(win, true);
958 moveInputMethodDialogsLocked(pos);
959 }
Romain Guy06882f82009-06-10 13:36:04 -0700960
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800961 void setInputMethodAnimLayerAdjustment(int adj) {
962 if (DEBUG_LAYERS) Log.v(TAG, "Setting im layer adj to " + adj);
963 mInputMethodAnimLayerAdjustment = adj;
964 WindowState imw = mInputMethodWindow;
965 if (imw != null) {
966 imw.mAnimLayer = imw.mLayer + adj;
967 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
968 + " anim layer: " + imw.mAnimLayer);
969 int wi = imw.mChildWindows.size();
970 while (wi > 0) {
971 wi--;
972 WindowState cw = (WindowState)imw.mChildWindows.get(wi);
973 cw.mAnimLayer = cw.mLayer + adj;
974 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + cw
975 + " anim layer: " + cw.mAnimLayer);
976 }
977 }
978 int di = mInputMethodDialogs.size();
979 while (di > 0) {
980 di --;
981 imw = mInputMethodDialogs.get(di);
982 imw.mAnimLayer = imw.mLayer + adj;
983 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
984 + " anim layer: " + imw.mAnimLayer);
985 }
986 }
Romain Guy06882f82009-06-10 13:36:04 -0700987
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 private int tmpRemoveWindowLocked(int interestingPos, WindowState win) {
989 int wpos = mWindows.indexOf(win);
990 if (wpos >= 0) {
991 if (wpos < interestingPos) interestingPos--;
992 mWindows.remove(wpos);
993 int NC = win.mChildWindows.size();
994 while (NC > 0) {
995 NC--;
996 WindowState cw = (WindowState)win.mChildWindows.get(NC);
997 int cpos = mWindows.indexOf(cw);
998 if (cpos >= 0) {
999 if (cpos < interestingPos) interestingPos--;
1000 mWindows.remove(cpos);
1001 }
1002 }
1003 }
1004 return interestingPos;
1005 }
Romain Guy06882f82009-06-10 13:36:04 -07001006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 private void reAddWindowToListInOrderLocked(WindowState win) {
1008 addWindowToListInOrderLocked(win, false);
1009 // This is a hack to get all of the child windows added as well
1010 // at the right position. Child windows should be rare and
1011 // this case should be rare, so it shouldn't be that big a deal.
1012 int wpos = mWindows.indexOf(win);
1013 if (wpos >= 0) {
1014 mWindows.remove(wpos);
1015 reAddWindowLocked(wpos, win);
1016 }
1017 }
Romain Guy06882f82009-06-10 13:36:04 -07001018
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019 void logWindowList(String prefix) {
1020 int N = mWindows.size();
1021 while (N > 0) {
1022 N--;
1023 Log.v(TAG, prefix + "#" + N + ": " + mWindows.get(N));
1024 }
1025 }
Romain Guy06882f82009-06-10 13:36:04 -07001026
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 void moveInputMethodDialogsLocked(int pos) {
1028 ArrayList<WindowState> dialogs = mInputMethodDialogs;
Romain Guy06882f82009-06-10 13:36:04 -07001029
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001030 final int N = dialogs.size();
1031 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Removing " + N + " dialogs w/pos=" + pos);
1032 for (int i=0; i<N; i++) {
1033 pos = tmpRemoveWindowLocked(pos, dialogs.get(i));
1034 }
1035 if (DEBUG_INPUT_METHOD) {
1036 Log.v(TAG, "Window list w/pos=" + pos);
1037 logWindowList(" ");
1038 }
Romain Guy06882f82009-06-10 13:36:04 -07001039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 if (pos >= 0) {
1041 final AppWindowToken targetAppToken = mInputMethodTarget.mAppToken;
1042 if (pos < mWindows.size()) {
1043 WindowState wp = (WindowState)mWindows.get(pos);
1044 if (wp == mInputMethodWindow) {
1045 pos++;
1046 }
1047 }
1048 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Adding " + N + " dialogs at pos=" + pos);
1049 for (int i=0; i<N; i++) {
1050 WindowState win = dialogs.get(i);
1051 win.mTargetAppToken = targetAppToken;
1052 pos = reAddWindowLocked(pos, win);
1053 }
1054 if (DEBUG_INPUT_METHOD) {
1055 Log.v(TAG, "Final window list:");
1056 logWindowList(" ");
1057 }
1058 return;
1059 }
1060 for (int i=0; i<N; i++) {
1061 WindowState win = dialogs.get(i);
1062 win.mTargetAppToken = null;
1063 reAddWindowToListInOrderLocked(win);
1064 if (DEBUG_INPUT_METHOD) {
1065 Log.v(TAG, "No IM target, final list:");
1066 logWindowList(" ");
1067 }
1068 }
1069 }
Romain Guy06882f82009-06-10 13:36:04 -07001070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001071 boolean moveInputMethodWindowsIfNeededLocked(boolean needAssignLayers) {
1072 final WindowState imWin = mInputMethodWindow;
1073 final int DN = mInputMethodDialogs.size();
1074 if (imWin == null && DN == 0) {
1075 return false;
1076 }
Romain Guy06882f82009-06-10 13:36:04 -07001077
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078 int imPos = findDesiredInputMethodWindowIndexLocked(true);
1079 if (imPos >= 0) {
1080 // In this case, the input method windows are to be placed
1081 // immediately above the window they are targeting.
Romain Guy06882f82009-06-10 13:36:04 -07001082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 // First check to see if the input method windows are already
1084 // located here, and contiguous.
1085 final int N = mWindows.size();
1086 WindowState firstImWin = imPos < N
1087 ? (WindowState)mWindows.get(imPos) : null;
Romain Guy06882f82009-06-10 13:36:04 -07001088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001089 // Figure out the actual input method window that should be
1090 // at the bottom of their stack.
1091 WindowState baseImWin = imWin != null
1092 ? imWin : mInputMethodDialogs.get(0);
1093 if (baseImWin.mChildWindows.size() > 0) {
1094 WindowState cw = (WindowState)baseImWin.mChildWindows.get(0);
1095 if (cw.mSubLayer < 0) baseImWin = cw;
1096 }
Romain Guy06882f82009-06-10 13:36:04 -07001097
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 if (firstImWin == baseImWin) {
1099 // The windows haven't moved... but are they still contiguous?
1100 // First find the top IM window.
1101 int pos = imPos+1;
1102 while (pos < N) {
1103 if (!((WindowState)mWindows.get(pos)).mIsImWindow) {
1104 break;
1105 }
1106 pos++;
1107 }
1108 pos++;
1109 // Now there should be no more input method windows above.
1110 while (pos < N) {
1111 if (((WindowState)mWindows.get(pos)).mIsImWindow) {
1112 break;
1113 }
1114 pos++;
1115 }
1116 if (pos >= N) {
1117 // All is good!
1118 return false;
1119 }
1120 }
Romain Guy06882f82009-06-10 13:36:04 -07001121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 if (imWin != null) {
1123 if (DEBUG_INPUT_METHOD) {
1124 Log.v(TAG, "Moving IM from " + imPos);
1125 logWindowList(" ");
1126 }
1127 imPos = tmpRemoveWindowLocked(imPos, imWin);
1128 if (DEBUG_INPUT_METHOD) {
1129 Log.v(TAG, "List after moving with new pos " + imPos + ":");
1130 logWindowList(" ");
1131 }
1132 imWin.mTargetAppToken = mInputMethodTarget.mAppToken;
1133 reAddWindowLocked(imPos, imWin);
1134 if (DEBUG_INPUT_METHOD) {
1135 Log.v(TAG, "List after moving IM to " + imPos + ":");
1136 logWindowList(" ");
1137 }
1138 if (DN > 0) moveInputMethodDialogsLocked(imPos+1);
1139 } else {
1140 moveInputMethodDialogsLocked(imPos);
1141 }
Romain Guy06882f82009-06-10 13:36:04 -07001142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 } else {
1144 // In this case, the input method windows go in a fixed layer,
1145 // because they aren't currently associated with a focus window.
Romain Guy06882f82009-06-10 13:36:04 -07001146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147 if (imWin != null) {
1148 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Moving IM from " + imPos);
1149 tmpRemoveWindowLocked(0, imWin);
1150 imWin.mTargetAppToken = null;
1151 reAddWindowToListInOrderLocked(imWin);
1152 if (DEBUG_INPUT_METHOD) {
1153 Log.v(TAG, "List with no IM target:");
1154 logWindowList(" ");
1155 }
1156 if (DN > 0) moveInputMethodDialogsLocked(-1);;
1157 } else {
1158 moveInputMethodDialogsLocked(-1);;
1159 }
Romain Guy06882f82009-06-10 13:36:04 -07001160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001161 }
Romain Guy06882f82009-06-10 13:36:04 -07001162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 if (needAssignLayers) {
1164 assignLayersLocked();
1165 }
Romain Guy06882f82009-06-10 13:36:04 -07001166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167 return true;
1168 }
Romain Guy06882f82009-06-10 13:36:04 -07001169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001170 void adjustInputMethodDialogsLocked() {
1171 moveInputMethodDialogsLocked(findDesiredInputMethodWindowIndexLocked(true));
1172 }
Romain Guy06882f82009-06-10 13:36:04 -07001173
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001174 boolean adjustWallpaperWindowsLocked() {
1175 boolean changed = false;
1176
1177 // First find top-most window that has asked to be on top of the
1178 // wallpaper; all wallpapers go behind it.
1179 final ArrayList localmWindows = mWindows;
1180 int N = localmWindows.size();
1181 WindowState w = null;
1182 int i = N;
1183 while (i > 0) {
1184 i--;
1185 w = (WindowState)localmWindows.get(i);
1186 if ((w.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0 && w.isVisibleOrAdding()) {
1187 break;
1188 }
1189 }
1190
1191 if (w != null) {
1192 // Now w is the window we are supposed to be behind... but we
1193 // need to be sure to also be behind any of its attached windows,
1194 // AND any starting window associated with it.
1195 while (i > 0) {
1196 WindowState wb = (WindowState)localmWindows.get(i-1);
1197 if (wb.mAttachedWindow != w &&
1198 (wb.mAttrs.type != TYPE_APPLICATION_STARTING ||
1199 wb.mToken != w.mToken)) {
1200 // This window is not related to the previous one in any
1201 // interesting way, so stop here.
1202 break;
1203 }
1204 w = wb;
1205 i--;
1206 }
1207 }
1208
1209 // Okay i is the position immediately above the wallpaper. Look at
1210 // what is below it for later.
1211 w = i > 0 ? (WindowState)localmWindows.get(i-1) : null;
1212
1213 // Start stepping backwards from here, ensuring that our wallpaper windows
1214 // are correctly placed.
1215 int curTokenIndex = mWallpaperTokens.size();
1216 while (curTokenIndex > 0) {
1217 curTokenIndex--;
1218 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1219 int curWallpaperIndex = token.windows.size();
1220 while (curWallpaperIndex > 0) {
1221 curWallpaperIndex--;
1222 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1223 // First, if this window is at the current index, then all
1224 // is well.
1225 if (wallpaper == w) {
1226 i--;
1227 w = i > 0 ? (WindowState)localmWindows.get(i-1) : null;
1228 continue;
1229 }
1230
1231 // The window didn't match... the current wallpaper window,
1232 // wherever it is, is in the wrong place, so make sure it is
1233 // not in the list.
1234 int oldIndex = localmWindows.indexOf(wallpaper);
1235 if (oldIndex >= 0) {
1236 localmWindows.remove(oldIndex);
1237 if (oldIndex < i) {
1238 i--;
1239 }
1240 }
1241
1242 // Now stick it in.
1243 localmWindows.add(i, wallpaper);
1244 changed = true;
1245 }
1246 }
1247
1248 return changed;
1249 }
1250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 public int addWindow(Session session, IWindow client,
1252 WindowManager.LayoutParams attrs, int viewVisibility,
1253 Rect outContentInsets) {
1254 int res = mPolicy.checkAddPermission(attrs);
1255 if (res != WindowManagerImpl.ADD_OKAY) {
1256 return res;
1257 }
Romain Guy06882f82009-06-10 13:36:04 -07001258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259 boolean reportNewConfig = false;
1260 WindowState attachedWindow = null;
1261 WindowState win = null;
Romain Guy06882f82009-06-10 13:36:04 -07001262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 synchronized(mWindowMap) {
1264 // Instantiating a Display requires talking with the simulator,
1265 // so don't do it until we know the system is mostly up and
1266 // running.
1267 if (mDisplay == null) {
1268 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
1269 mDisplay = wm.getDefaultDisplay();
1270 mQueue.setDisplay(mDisplay);
1271 reportNewConfig = true;
1272 }
Romain Guy06882f82009-06-10 13:36:04 -07001273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 if (mWindowMap.containsKey(client.asBinder())) {
1275 Log.w(TAG, "Window " + client + " is already added");
1276 return WindowManagerImpl.ADD_DUPLICATE_ADD;
1277 }
1278
1279 if (attrs.type >= FIRST_SUB_WINDOW && attrs.type <= LAST_SUB_WINDOW) {
Romain Guy06882f82009-06-10 13:36:04 -07001280 attachedWindow = windowForClientLocked(null, attrs.token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001281 if (attachedWindow == null) {
1282 Log.w(TAG, "Attempted to add window with token that is not a window: "
1283 + attrs.token + ". Aborting.");
1284 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1285 }
1286 if (attachedWindow.mAttrs.type >= FIRST_SUB_WINDOW
1287 && attachedWindow.mAttrs.type <= LAST_SUB_WINDOW) {
1288 Log.w(TAG, "Attempted to add window with token that is a sub-window: "
1289 + attrs.token + ". Aborting.");
1290 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1291 }
1292 }
1293
1294 boolean addToken = false;
1295 WindowToken token = mTokenMap.get(attrs.token);
1296 if (token == null) {
1297 if (attrs.type >= FIRST_APPLICATION_WINDOW
1298 && attrs.type <= LAST_APPLICATION_WINDOW) {
1299 Log.w(TAG, "Attempted to add application window with unknown token "
1300 + attrs.token + ". Aborting.");
1301 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1302 }
1303 if (attrs.type == TYPE_INPUT_METHOD) {
1304 Log.w(TAG, "Attempted to add input method window with unknown token "
1305 + attrs.token + ". Aborting.");
1306 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1307 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001308 if (attrs.type == TYPE_WALLPAPER) {
1309 Log.w(TAG, "Attempted to add wallpaper window with unknown token "
1310 + attrs.token + ". Aborting.");
1311 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1312 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001313 token = new WindowToken(attrs.token, -1, false);
1314 addToken = true;
1315 } else if (attrs.type >= FIRST_APPLICATION_WINDOW
1316 && attrs.type <= LAST_APPLICATION_WINDOW) {
1317 AppWindowToken atoken = token.appWindowToken;
1318 if (atoken == null) {
1319 Log.w(TAG, "Attempted to add window with non-application token "
1320 + token + ". Aborting.");
1321 return WindowManagerImpl.ADD_NOT_APP_TOKEN;
1322 } else if (atoken.removed) {
1323 Log.w(TAG, "Attempted to add window with exiting application token "
1324 + token + ". Aborting.");
1325 return WindowManagerImpl.ADD_APP_EXITING;
1326 }
1327 if (attrs.type == TYPE_APPLICATION_STARTING && atoken.firstWindowDrawn) {
1328 // No need for this guy!
1329 if (localLOGV) Log.v(
1330 TAG, "**** NO NEED TO START: " + attrs.getTitle());
1331 return WindowManagerImpl.ADD_STARTING_NOT_NEEDED;
1332 }
1333 } else if (attrs.type == TYPE_INPUT_METHOD) {
1334 if (token.windowType != TYPE_INPUT_METHOD) {
1335 Log.w(TAG, "Attempted to add input method window with bad token "
1336 + attrs.token + ". Aborting.");
1337 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1338 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001339 } else if (attrs.type == TYPE_WALLPAPER) {
1340 if (token.windowType != TYPE_WALLPAPER) {
1341 Log.w(TAG, "Attempted to add wallpaper window with bad token "
1342 + attrs.token + ". Aborting.");
1343 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1344 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001345 }
1346
1347 win = new WindowState(session, client, token,
1348 attachedWindow, attrs, viewVisibility);
1349 if (win.mDeathRecipient == null) {
1350 // Client has apparently died, so there is no reason to
1351 // continue.
1352 Log.w(TAG, "Adding window client " + client.asBinder()
1353 + " that is dead, aborting.");
1354 return WindowManagerImpl.ADD_APP_EXITING;
1355 }
1356
1357 mPolicy.adjustWindowParamsLw(win.mAttrs);
Romain Guy06882f82009-06-10 13:36:04 -07001358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001359 res = mPolicy.prepareAddWindowLw(win, attrs);
1360 if (res != WindowManagerImpl.ADD_OKAY) {
1361 return res;
1362 }
1363
1364 // From now on, no exceptions or errors allowed!
1365
1366 res = WindowManagerImpl.ADD_OKAY;
Romain Guy06882f82009-06-10 13:36:04 -07001367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001368 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001370 if (addToken) {
1371 mTokenMap.put(attrs.token, token);
1372 mTokenList.add(token);
1373 }
1374 win.attach();
1375 mWindowMap.put(client.asBinder(), win);
1376
1377 if (attrs.type == TYPE_APPLICATION_STARTING &&
1378 token.appWindowToken != null) {
1379 token.appWindowToken.startingWindow = win;
1380 }
1381
1382 boolean imMayMove = true;
Romain Guy06882f82009-06-10 13:36:04 -07001383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001384 if (attrs.type == TYPE_INPUT_METHOD) {
1385 mInputMethodWindow = win;
1386 addInputMethodWindowToListLocked(win);
1387 imMayMove = false;
1388 } else if (attrs.type == TYPE_INPUT_METHOD_DIALOG) {
1389 mInputMethodDialogs.add(win);
1390 addWindowToListInOrderLocked(win, true);
1391 adjustInputMethodDialogsLocked();
1392 imMayMove = false;
1393 } else {
1394 addWindowToListInOrderLocked(win, true);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001395 if (attrs.type == TYPE_WALLPAPER ||
1396 (attrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
1397 adjustWallpaperWindowsLocked();
1398 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001399 }
Romain Guy06882f82009-06-10 13:36:04 -07001400
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001401 win.mEnterAnimationPending = true;
Romain Guy06882f82009-06-10 13:36:04 -07001402
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001403 mPolicy.getContentInsetHintLw(attrs, outContentInsets);
Romain Guy06882f82009-06-10 13:36:04 -07001404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001405 if (mInTouchMode) {
1406 res |= WindowManagerImpl.ADD_FLAG_IN_TOUCH_MODE;
1407 }
1408 if (win == null || win.mAppToken == null || !win.mAppToken.clientHidden) {
1409 res |= WindowManagerImpl.ADD_FLAG_APP_VISIBLE;
1410 }
Romain Guy06882f82009-06-10 13:36:04 -07001411
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001412 boolean focusChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001413 if (win.canReceiveKeys()) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001414 if ((focusChanged=updateFocusedWindowLocked(UPDATE_FOCUS_WILL_ASSIGN_LAYERS))
1415 == true) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001416 imMayMove = false;
1417 }
1418 }
Romain Guy06882f82009-06-10 13:36:04 -07001419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001420 if (imMayMove) {
Romain Guy06882f82009-06-10 13:36:04 -07001421 moveInputMethodWindowsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001422 }
Romain Guy06882f82009-06-10 13:36:04 -07001423
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 assignLayersLocked();
1425 // Don't do layout here, the window must call
1426 // relayout to be displayed, so we'll do it there.
Romain Guy06882f82009-06-10 13:36:04 -07001427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001428 //dump();
1429
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001430 if (focusChanged) {
1431 if (mCurrentFocus != null) {
1432 mKeyWaiter.handleNewWindowLocked(mCurrentFocus);
1433 }
1434 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001435 if (localLOGV) Log.v(
1436 TAG, "New client " + client.asBinder()
1437 + ": window=" + win);
1438 }
1439
1440 // sendNewConfiguration() checks caller permissions so we must call it with
1441 // privilege. updateOrientationFromAppTokens() clears and resets the caller
1442 // identity anyway, so it's safe to just clear & restore around this whole
1443 // block.
1444 final long origId = Binder.clearCallingIdentity();
1445 if (reportNewConfig) {
1446 sendNewConfiguration();
1447 } else {
1448 // Update Orientation after adding a window, only if the window needs to be
1449 // displayed right away
1450 if (win.isVisibleOrAdding()) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07001451 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001452 sendNewConfiguration();
1453 }
1454 }
1455 }
1456 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07001457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001458 return res;
1459 }
Romain Guy06882f82009-06-10 13:36:04 -07001460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001461 public void removeWindow(Session session, IWindow client) {
1462 synchronized(mWindowMap) {
1463 WindowState win = windowForClientLocked(session, client);
1464 if (win == null) {
1465 return;
1466 }
1467 removeWindowLocked(session, win);
1468 }
1469 }
Romain Guy06882f82009-06-10 13:36:04 -07001470
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001471 public void removeWindowLocked(Session session, WindowState win) {
1472
1473 if (localLOGV || DEBUG_FOCUS) Log.v(
1474 TAG, "Remove " + win + " client="
1475 + Integer.toHexString(System.identityHashCode(
1476 win.mClient.asBinder()))
1477 + ", surface=" + win.mSurface);
1478
1479 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001481 if (DEBUG_APP_TRANSITIONS) Log.v(
1482 TAG, "Remove " + win + ": mSurface=" + win.mSurface
1483 + " mExiting=" + win.mExiting
1484 + " isAnimating=" + win.isAnimating()
1485 + " app-animation="
1486 + (win.mAppToken != null ? win.mAppToken.animation : null)
1487 + " inPendingTransaction="
1488 + (win.mAppToken != null ? win.mAppToken.inPendingTransaction : false)
1489 + " mDisplayFrozen=" + mDisplayFrozen);
1490 // Visibility of the removed window. Will be used later to update orientation later on.
1491 boolean wasVisible = false;
1492 // First, see if we need to run an animation. If we do, we have
1493 // to hold off on removing the window until the animation is done.
1494 // If the display is frozen, just remove immediately, since the
1495 // animation wouldn't be seen.
1496 if (win.mSurface != null && !mDisplayFrozen) {
1497 // If we are not currently running the exit animation, we
1498 // need to see about starting one.
1499 if (wasVisible=win.isWinVisibleLw()) {
Romain Guy06882f82009-06-10 13:36:04 -07001500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001501 int transit = WindowManagerPolicy.TRANSIT_EXIT;
1502 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
1503 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
1504 }
1505 // Try starting an animation.
1506 if (applyAnimationLocked(win, transit, false)) {
1507 win.mExiting = true;
1508 }
1509 }
1510 if (win.mExiting || win.isAnimating()) {
1511 // The exit animation is running... wait for it!
1512 //Log.i(TAG, "*** Running exit animation...");
1513 win.mExiting = true;
1514 win.mRemoveOnExit = true;
1515 mLayoutNeeded = true;
1516 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
1517 performLayoutAndPlaceSurfacesLocked();
1518 if (win.mAppToken != null) {
1519 win.mAppToken.updateReportedVisibilityLocked();
1520 }
1521 //dump();
1522 Binder.restoreCallingIdentity(origId);
1523 return;
1524 }
1525 }
1526
1527 removeWindowInnerLocked(session, win);
1528 // Removing a visible window will effect the computed orientation
1529 // So just update orientation if needed.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001530 if (wasVisible && computeForcedAppOrientationLocked()
1531 != mForcedAppOrientation) {
1532 mH.sendMessage(mH.obtainMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001533 }
1534 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
1535 Binder.restoreCallingIdentity(origId);
1536 }
Romain Guy06882f82009-06-10 13:36:04 -07001537
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538 private void removeWindowInnerLocked(Session session, WindowState win) {
1539 mKeyWaiter.releasePendingPointerLocked(win.mSession);
1540 mKeyWaiter.releasePendingTrackballLocked(win.mSession);
Romain Guy06882f82009-06-10 13:36:04 -07001541
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001542 win.mRemoved = true;
Romain Guy06882f82009-06-10 13:36:04 -07001543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001544 if (mInputMethodTarget == win) {
1545 moveInputMethodWindowsIfNeededLocked(false);
1546 }
Romain Guy06882f82009-06-10 13:36:04 -07001547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001548 mPolicy.removeWindowLw(win);
1549 win.removeLocked();
1550
1551 mWindowMap.remove(win.mClient.asBinder());
1552 mWindows.remove(win);
1553
1554 if (mInputMethodWindow == win) {
1555 mInputMethodWindow = null;
1556 } else if (win.mAttrs.type == TYPE_INPUT_METHOD_DIALOG) {
1557 mInputMethodDialogs.remove(win);
1558 }
Romain Guy06882f82009-06-10 13:36:04 -07001559
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001560 if (win.mAttrs.type == TYPE_WALLPAPER ||
1561 (win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
1562 adjustWallpaperWindowsLocked();
1563 }
1564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001565 final WindowToken token = win.mToken;
1566 final AppWindowToken atoken = win.mAppToken;
1567 token.windows.remove(win);
1568 if (atoken != null) {
1569 atoken.allAppWindows.remove(win);
1570 }
1571 if (localLOGV) Log.v(
1572 TAG, "**** Removing window " + win + ": count="
1573 + token.windows.size());
1574 if (token.windows.size() == 0) {
1575 if (!token.explicit) {
1576 mTokenMap.remove(token.token);
1577 mTokenList.remove(token);
1578 } else if (atoken != null) {
1579 atoken.firstWindowDrawn = false;
1580 }
1581 }
1582
1583 if (atoken != null) {
1584 if (atoken.startingWindow == win) {
1585 atoken.startingWindow = null;
1586 } else if (atoken.allAppWindows.size() == 0 && atoken.startingData != null) {
1587 // If this is the last window and we had requested a starting
1588 // transition window, well there is no point now.
1589 atoken.startingData = null;
1590 } else if (atoken.allAppWindows.size() == 1 && atoken.startingView != null) {
1591 // If this is the last window except for a starting transition
1592 // window, we need to get rid of the starting transition.
1593 if (DEBUG_STARTING_WINDOW) {
1594 Log.v(TAG, "Schedule remove starting " + token
1595 + ": no more real windows");
1596 }
1597 Message m = mH.obtainMessage(H.REMOVE_STARTING, atoken);
1598 mH.sendMessage(m);
1599 }
1600 }
Romain Guy06882f82009-06-10 13:36:04 -07001601
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602 if (!mInLayout) {
1603 assignLayersLocked();
1604 mLayoutNeeded = true;
1605 performLayoutAndPlaceSurfacesLocked();
1606 if (win.mAppToken != null) {
1607 win.mAppToken.updateReportedVisibilityLocked();
1608 }
1609 }
1610 }
1611
1612 private void setTransparentRegionWindow(Session session, IWindow client, Region region) {
1613 long origId = Binder.clearCallingIdentity();
1614 try {
1615 synchronized (mWindowMap) {
1616 WindowState w = windowForClientLocked(session, client);
1617 if ((w != null) && (w.mSurface != null)) {
1618 Surface.openTransaction();
1619 try {
1620 w.mSurface.setTransparentRegionHint(region);
1621 } finally {
1622 Surface.closeTransaction();
1623 }
1624 }
1625 }
1626 } finally {
1627 Binder.restoreCallingIdentity(origId);
1628 }
1629 }
1630
1631 void setInsetsWindow(Session session, IWindow client,
Romain Guy06882f82009-06-10 13:36:04 -07001632 int touchableInsets, Rect contentInsets,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001633 Rect visibleInsets) {
1634 long origId = Binder.clearCallingIdentity();
1635 try {
1636 synchronized (mWindowMap) {
1637 WindowState w = windowForClientLocked(session, client);
1638 if (w != null) {
1639 w.mGivenInsetsPending = false;
1640 w.mGivenContentInsets.set(contentInsets);
1641 w.mGivenVisibleInsets.set(visibleInsets);
1642 w.mTouchableInsets = touchableInsets;
1643 mLayoutNeeded = true;
1644 performLayoutAndPlaceSurfacesLocked();
1645 }
1646 }
1647 } finally {
1648 Binder.restoreCallingIdentity(origId);
1649 }
1650 }
Romain Guy06882f82009-06-10 13:36:04 -07001651
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001652 public void getWindowDisplayFrame(Session session, IWindow client,
1653 Rect outDisplayFrame) {
1654 synchronized(mWindowMap) {
1655 WindowState win = windowForClientLocked(session, client);
1656 if (win == null) {
1657 outDisplayFrame.setEmpty();
1658 return;
1659 }
1660 outDisplayFrame.set(win.mDisplayFrame);
1661 }
1662 }
1663
1664 public int relayoutWindow(Session session, IWindow client,
1665 WindowManager.LayoutParams attrs, int requestedWidth,
1666 int requestedHeight, int viewVisibility, boolean insetsPending,
1667 Rect outFrame, Rect outContentInsets, Rect outVisibleInsets,
1668 Surface outSurface) {
1669 boolean displayed = false;
1670 boolean inTouchMode;
1671 Configuration newConfig = null;
1672 long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001673
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001674 synchronized(mWindowMap) {
1675 WindowState win = windowForClientLocked(session, client);
1676 if (win == null) {
1677 return 0;
1678 }
1679 win.mRequestedWidth = requestedWidth;
1680 win.mRequestedHeight = requestedHeight;
1681
1682 if (attrs != null) {
1683 mPolicy.adjustWindowParamsLw(attrs);
1684 }
Romain Guy06882f82009-06-10 13:36:04 -07001685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001686 int attrChanges = 0;
1687 int flagChanges = 0;
1688 if (attrs != null) {
1689 flagChanges = win.mAttrs.flags ^= attrs.flags;
1690 attrChanges = win.mAttrs.copyFrom(attrs);
1691 }
1692
1693 if (localLOGV) Log.v(
1694 TAG, "Relayout given client " + client.asBinder()
1695 + " (" + win.mAttrs.getTitle() + ")");
1696
1697
1698 if ((attrChanges & WindowManager.LayoutParams.ALPHA_CHANGED) != 0) {
1699 win.mAlpha = attrs.alpha;
1700 }
1701
1702 final boolean scaledWindow =
1703 ((win.mAttrs.flags & WindowManager.LayoutParams.FLAG_SCALED) != 0);
1704
1705 if (scaledWindow) {
1706 // requested{Width|Height} Surface's physical size
1707 // attrs.{width|height} Size on screen
1708 win.mHScale = (attrs.width != requestedWidth) ?
1709 (attrs.width / (float)requestedWidth) : 1.0f;
1710 win.mVScale = (attrs.height != requestedHeight) ?
1711 (attrs.height / (float)requestedHeight) : 1.0f;
1712 }
1713
1714 boolean imMayMove = (flagChanges&(
1715 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
1716 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07001717
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001718 boolean focusMayChange = win.mViewVisibility != viewVisibility
1719 || ((flagChanges&WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) != 0)
1720 || (!win.mRelayoutCalled);
Romain Guy06882f82009-06-10 13:36:04 -07001721
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001722 boolean wallpaperMayMove = win.mViewVisibility != viewVisibility
1723 && (win.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0;
1724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001725 win.mRelayoutCalled = true;
1726 final int oldVisibility = win.mViewVisibility;
1727 win.mViewVisibility = viewVisibility;
1728 if (viewVisibility == View.VISIBLE &&
1729 (win.mAppToken == null || !win.mAppToken.clientHidden)) {
1730 displayed = !win.isVisibleLw();
1731 if (win.mExiting) {
1732 win.mExiting = false;
1733 win.mAnimation = null;
1734 }
1735 if (win.mDestroying) {
1736 win.mDestroying = false;
1737 mDestroySurface.remove(win);
1738 }
1739 if (oldVisibility == View.GONE) {
1740 win.mEnterAnimationPending = true;
1741 }
1742 if (displayed && win.mSurface != null && !win.mDrawPending
1743 && !win.mCommitDrawPending && !mDisplayFrozen) {
1744 applyEnterAnimationLocked(win);
1745 }
1746 if ((attrChanges&WindowManager.LayoutParams.FORMAT_CHANGED) != 0) {
1747 // To change the format, we need to re-build the surface.
1748 win.destroySurfaceLocked();
1749 displayed = true;
1750 }
1751 try {
1752 Surface surface = win.createSurfaceLocked();
1753 if (surface != null) {
1754 outSurface.copyFrom(surface);
1755 } else {
1756 outSurface.clear();
1757 }
1758 } catch (Exception e) {
1759 Log.w(TAG, "Exception thrown when creating surface for client "
1760 + client + " (" + win.mAttrs.getTitle() + ")",
1761 e);
1762 Binder.restoreCallingIdentity(origId);
1763 return 0;
1764 }
1765 if (displayed) {
1766 focusMayChange = true;
1767 }
1768 if (win.mAttrs.type == TYPE_INPUT_METHOD
1769 && mInputMethodWindow == null) {
1770 mInputMethodWindow = win;
1771 imMayMove = true;
1772 }
1773 } else {
1774 win.mEnterAnimationPending = false;
1775 if (win.mSurface != null) {
1776 // If we are not currently running the exit animation, we
1777 // need to see about starting one.
1778 if (!win.mExiting) {
1779 // Try starting an animation; if there isn't one, we
1780 // can destroy the surface right away.
1781 int transit = WindowManagerPolicy.TRANSIT_EXIT;
1782 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
1783 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
1784 }
1785 if (win.isWinVisibleLw() &&
1786 applyAnimationLocked(win, transit, false)) {
1787 win.mExiting = true;
1788 mKeyWaiter.finishedKey(session, client, true,
1789 KeyWaiter.RETURN_NOTHING);
1790 } else if (win.isAnimating()) {
1791 // Currently in a hide animation... turn this into
1792 // an exit.
1793 win.mExiting = true;
1794 } else {
1795 if (mInputMethodWindow == win) {
1796 mInputMethodWindow = null;
1797 }
1798 win.destroySurfaceLocked();
1799 }
1800 }
1801 }
1802 outSurface.clear();
1803 }
1804
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001805 if (focusMayChange) {
1806 //System.out.println("Focus may change: " + win.mAttrs.getTitle());
1807 if (updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001808 imMayMove = false;
1809 }
1810 //System.out.println("Relayout " + win + ": focus=" + mCurrentFocus);
1811 }
Romain Guy06882f82009-06-10 13:36:04 -07001812
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001813 // updateFocusedWindowLocked() already assigned layers so we only need to
1814 // reassign them at this point if the IM window state gets shuffled
1815 boolean assignLayers = false;
Romain Guy06882f82009-06-10 13:36:04 -07001816
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001817 if (imMayMove) {
1818 if (moveInputMethodWindowsIfNeededLocked(false)) {
1819 assignLayers = true;
1820 }
1821 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001822 if (wallpaperMayMove) {
1823 if (adjustWallpaperWindowsLocked()) {
1824 assignLayers = true;
1825 }
1826 }
Romain Guy06882f82009-06-10 13:36:04 -07001827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001828 mLayoutNeeded = true;
1829 win.mGivenInsetsPending = insetsPending;
1830 if (assignLayers) {
1831 assignLayersLocked();
1832 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001833 newConfig = updateOrientationFromAppTokensLocked(null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001834 performLayoutAndPlaceSurfacesLocked();
1835 if (win.mAppToken != null) {
1836 win.mAppToken.updateReportedVisibilityLocked();
1837 }
1838 outFrame.set(win.mFrame);
1839 outContentInsets.set(win.mContentInsets);
1840 outVisibleInsets.set(win.mVisibleInsets);
1841 if (localLOGV) Log.v(
1842 TAG, "Relayout given client " + client.asBinder()
Romain Guy06882f82009-06-10 13:36:04 -07001843 + ", requestedWidth=" + requestedWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001844 + ", requestedHeight=" + requestedHeight
1845 + ", viewVisibility=" + viewVisibility
1846 + "\nRelayout returning frame=" + outFrame
1847 + ", surface=" + outSurface);
1848
1849 if (localLOGV || DEBUG_FOCUS) Log.v(
1850 TAG, "Relayout of " + win + ": focusMayChange=" + focusMayChange);
1851
1852 inTouchMode = mInTouchMode;
1853 }
1854
1855 if (newConfig != null) {
1856 sendNewConfiguration();
1857 }
Romain Guy06882f82009-06-10 13:36:04 -07001858
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001859 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07001860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001861 return (inTouchMode ? WindowManagerImpl.RELAYOUT_IN_TOUCH_MODE : 0)
1862 | (displayed ? WindowManagerImpl.RELAYOUT_FIRST_TIME : 0);
1863 }
1864
1865 public void finishDrawingWindow(Session session, IWindow client) {
1866 final long origId = Binder.clearCallingIdentity();
1867 synchronized(mWindowMap) {
1868 WindowState win = windowForClientLocked(session, client);
1869 if (win != null && win.finishDrawingLocked()) {
1870 mLayoutNeeded = true;
1871 performLayoutAndPlaceSurfacesLocked();
1872 }
1873 }
1874 Binder.restoreCallingIdentity(origId);
1875 }
1876
1877 private AttributeCache.Entry getCachedAnimations(WindowManager.LayoutParams lp) {
1878 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: params package="
1879 + (lp != null ? lp.packageName : null)
1880 + " resId=0x" + (lp != null ? Integer.toHexString(lp.windowAnimations) : null));
1881 if (lp != null && lp.windowAnimations != 0) {
1882 // If this is a system resource, don't try to load it from the
1883 // application resources. It is nice to avoid loading application
1884 // resources if we can.
1885 String packageName = lp.packageName != null ? lp.packageName : "android";
1886 int resId = lp.windowAnimations;
1887 if ((resId&0xFF000000) == 0x01000000) {
1888 packageName = "android";
1889 }
1890 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: picked package="
1891 + packageName);
1892 return AttributeCache.instance().get(packageName, resId,
1893 com.android.internal.R.styleable.WindowAnimation);
1894 }
1895 return null;
1896 }
Romain Guy06882f82009-06-10 13:36:04 -07001897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001898 private void applyEnterAnimationLocked(WindowState win) {
1899 int transit = WindowManagerPolicy.TRANSIT_SHOW;
1900 if (win.mEnterAnimationPending) {
1901 win.mEnterAnimationPending = false;
1902 transit = WindowManagerPolicy.TRANSIT_ENTER;
1903 }
1904
1905 applyAnimationLocked(win, transit, true);
1906 }
1907
1908 private boolean applyAnimationLocked(WindowState win,
1909 int transit, boolean isEntrance) {
1910 if (win.mLocalAnimating && win.mAnimationIsEntrance == isEntrance) {
1911 // If we are trying to apply an animation, but already running
1912 // an animation of the same type, then just leave that one alone.
1913 return true;
1914 }
Romain Guy06882f82009-06-10 13:36:04 -07001915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001916 // Only apply an animation if the display isn't frozen. If it is
1917 // frozen, there is no reason to animate and it can cause strange
1918 // artifacts when we unfreeze the display if some different animation
1919 // is running.
1920 if (!mDisplayFrozen) {
1921 int anim = mPolicy.selectAnimationLw(win, transit);
1922 int attr = -1;
1923 Animation a = null;
1924 if (anim != 0) {
1925 a = AnimationUtils.loadAnimation(mContext, anim);
1926 } else {
1927 switch (transit) {
1928 case WindowManagerPolicy.TRANSIT_ENTER:
1929 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
1930 break;
1931 case WindowManagerPolicy.TRANSIT_EXIT:
1932 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
1933 break;
1934 case WindowManagerPolicy.TRANSIT_SHOW:
1935 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
1936 break;
1937 case WindowManagerPolicy.TRANSIT_HIDE:
1938 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
1939 break;
1940 }
1941 if (attr >= 0) {
1942 a = loadAnimation(win.mAttrs, attr);
1943 }
1944 }
1945 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: win=" + win
1946 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
1947 + " mAnimation=" + win.mAnimation
1948 + " isEntrance=" + isEntrance);
1949 if (a != null) {
1950 if (DEBUG_ANIM) {
1951 RuntimeException e = new RuntimeException();
1952 e.fillInStackTrace();
1953 Log.v(TAG, "Loaded animation " + a + " for " + win, e);
1954 }
1955 win.setAnimation(a);
1956 win.mAnimationIsEntrance = isEntrance;
1957 }
1958 } else {
1959 win.clearAnimation();
1960 }
1961
1962 return win.mAnimation != null;
1963 }
1964
1965 private Animation loadAnimation(WindowManager.LayoutParams lp, int animAttr) {
1966 int anim = 0;
1967 Context context = mContext;
1968 if (animAttr >= 0) {
1969 AttributeCache.Entry ent = getCachedAnimations(lp);
1970 if (ent != null) {
1971 context = ent.context;
1972 anim = ent.array.getResourceId(animAttr, 0);
1973 }
1974 }
1975 if (anim != 0) {
1976 return AnimationUtils.loadAnimation(context, anim);
1977 }
1978 return null;
1979 }
Romain Guy06882f82009-06-10 13:36:04 -07001980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001981 private boolean applyAnimationLocked(AppWindowToken wtoken,
1982 WindowManager.LayoutParams lp, int transit, boolean enter) {
1983 // Only apply an animation if the display isn't frozen. If it is
1984 // frozen, there is no reason to animate and it can cause strange
1985 // artifacts when we unfreeze the display if some different animation
1986 // is running.
1987 if (!mDisplayFrozen) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07001988 Animation a;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07001989 if (lp != null && (lp.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07001990 a = new FadeInOutAnimation(enter);
1991 if (DEBUG_ANIM) Log.v(TAG,
1992 "applying FadeInOutAnimation for a window in compatibility mode");
1993 } else {
1994 int animAttr = 0;
1995 switch (transit) {
1996 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
1997 animAttr = enter
1998 ? com.android.internal.R.styleable.WindowAnimation_activityOpenEnterAnimation
1999 : com.android.internal.R.styleable.WindowAnimation_activityOpenExitAnimation;
2000 break;
2001 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
2002 animAttr = enter
2003 ? com.android.internal.R.styleable.WindowAnimation_activityCloseEnterAnimation
2004 : com.android.internal.R.styleable.WindowAnimation_activityCloseExitAnimation;
2005 break;
2006 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
2007 animAttr = enter
2008 ? com.android.internal.R.styleable.WindowAnimation_taskOpenEnterAnimation
2009 : com.android.internal.R.styleable.WindowAnimation_taskOpenExitAnimation;
2010 break;
2011 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
2012 animAttr = enter
2013 ? com.android.internal.R.styleable.WindowAnimation_taskCloseEnterAnimation
2014 : com.android.internal.R.styleable.WindowAnimation_taskCloseExitAnimation;
2015 break;
2016 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
2017 animAttr = enter
2018 ? com.android.internal.R.styleable.WindowAnimation_taskToFrontEnterAnimation
2019 : com.android.internal.R.styleable.WindowAnimation_taskToFrontExitAnimation;
2020 break;
2021 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
2022 animAttr = enter
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07002023 ? com.android.internal.R.styleable.WindowAnimation_taskToBackEnterAnimation
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002024 : com.android.internal.R.styleable.WindowAnimation_taskToBackExitAnimation;
2025 break;
2026 }
2027 a = loadAnimation(lp, animAttr);
2028 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: wtoken=" + wtoken
2029 + " anim=" + a
2030 + " animAttr=0x" + Integer.toHexString(animAttr)
2031 + " transit=" + transit);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002032 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002033 if (a != null) {
2034 if (DEBUG_ANIM) {
2035 RuntimeException e = new RuntimeException();
2036 e.fillInStackTrace();
2037 Log.v(TAG, "Loaded animation " + a + " for " + wtoken, e);
2038 }
2039 wtoken.setAnimation(a);
2040 }
2041 } else {
2042 wtoken.clearAnimation();
2043 }
2044
2045 return wtoken.animation != null;
2046 }
2047
2048 // -------------------------------------------------------------
2049 // Application Window Tokens
2050 // -------------------------------------------------------------
2051
2052 public void validateAppTokens(List tokens) {
2053 int v = tokens.size()-1;
2054 int m = mAppTokens.size()-1;
2055 while (v >= 0 && m >= 0) {
2056 AppWindowToken wtoken = mAppTokens.get(m);
2057 if (wtoken.removed) {
2058 m--;
2059 continue;
2060 }
2061 if (tokens.get(v) != wtoken.token) {
2062 Log.w(TAG, "Tokens out of sync: external is " + tokens.get(v)
2063 + " @ " + v + ", internal is " + wtoken.token + " @ " + m);
2064 }
2065 v--;
2066 m--;
2067 }
2068 while (v >= 0) {
2069 Log.w(TAG, "External token not found: " + tokens.get(v) + " @ " + v);
2070 v--;
2071 }
2072 while (m >= 0) {
2073 AppWindowToken wtoken = mAppTokens.get(m);
2074 if (!wtoken.removed) {
2075 Log.w(TAG, "Invalid internal token: " + wtoken.token + " @ " + m);
2076 }
2077 m--;
2078 }
2079 }
2080
2081 boolean checkCallingPermission(String permission, String func) {
2082 // Quick check: if the calling permission is me, it's all okay.
2083 if (Binder.getCallingPid() == Process.myPid()) {
2084 return true;
2085 }
Romain Guy06882f82009-06-10 13:36:04 -07002086
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002087 if (mContext.checkCallingPermission(permission)
2088 == PackageManager.PERMISSION_GRANTED) {
2089 return true;
2090 }
2091 String msg = "Permission Denial: " + func + " from pid="
2092 + Binder.getCallingPid()
2093 + ", uid=" + Binder.getCallingUid()
2094 + " requires " + permission;
2095 Log.w(TAG, msg);
2096 return false;
2097 }
Romain Guy06882f82009-06-10 13:36:04 -07002098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002099 AppWindowToken findAppWindowToken(IBinder token) {
2100 WindowToken wtoken = mTokenMap.get(token);
2101 if (wtoken == null) {
2102 return null;
2103 }
2104 return wtoken.appWindowToken;
2105 }
Romain Guy06882f82009-06-10 13:36:04 -07002106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002107 public void addWindowToken(IBinder token, int type) {
2108 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2109 "addWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002110 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002111 }
Romain Guy06882f82009-06-10 13:36:04 -07002112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002113 synchronized(mWindowMap) {
2114 WindowToken wtoken = mTokenMap.get(token);
2115 if (wtoken != null) {
2116 Log.w(TAG, "Attempted to add existing input method token: " + token);
2117 return;
2118 }
2119 wtoken = new WindowToken(token, type, true);
2120 mTokenMap.put(token, wtoken);
2121 mTokenList.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002122 if (type == TYPE_WALLPAPER) {
2123 mWallpaperTokens.add(wtoken);
2124 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002125 }
2126 }
Romain Guy06882f82009-06-10 13:36:04 -07002127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002128 public void removeWindowToken(IBinder token) {
2129 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2130 "removeWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002131 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002132 }
2133
2134 final long origId = Binder.clearCallingIdentity();
2135 synchronized(mWindowMap) {
2136 WindowToken wtoken = mTokenMap.remove(token);
2137 mTokenList.remove(wtoken);
2138 if (wtoken != null) {
2139 boolean delayed = false;
2140 if (!wtoken.hidden) {
2141 wtoken.hidden = true;
Romain Guy06882f82009-06-10 13:36:04 -07002142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002143 final int N = wtoken.windows.size();
2144 boolean changed = false;
Romain Guy06882f82009-06-10 13:36:04 -07002145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002146 for (int i=0; i<N; i++) {
2147 WindowState win = wtoken.windows.get(i);
2148
2149 if (win.isAnimating()) {
2150 delayed = true;
2151 }
Romain Guy06882f82009-06-10 13:36:04 -07002152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002153 if (win.isVisibleNow()) {
2154 applyAnimationLocked(win,
2155 WindowManagerPolicy.TRANSIT_EXIT, false);
2156 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2157 KeyWaiter.RETURN_NOTHING);
2158 changed = true;
2159 }
2160 }
2161
2162 if (changed) {
2163 mLayoutNeeded = true;
2164 performLayoutAndPlaceSurfacesLocked();
2165 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2166 }
Romain Guy06882f82009-06-10 13:36:04 -07002167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002168 if (delayed) {
2169 mExitingTokens.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002170 } else if (wtoken.windowType == TYPE_WALLPAPER) {
2171 mWallpaperTokens.remove(wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002172 }
2173 }
Romain Guy06882f82009-06-10 13:36:04 -07002174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002175 } else {
2176 Log.w(TAG, "Attempted to remove non-existing token: " + token);
2177 }
2178 }
2179 Binder.restoreCallingIdentity(origId);
2180 }
2181
2182 public void addAppToken(int addPos, IApplicationToken token,
2183 int groupId, int requestedOrientation, boolean fullscreen) {
2184 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2185 "addAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002186 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002187 }
Romain Guy06882f82009-06-10 13:36:04 -07002188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002189 synchronized(mWindowMap) {
2190 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2191 if (wtoken != null) {
2192 Log.w(TAG, "Attempted to add existing app token: " + token);
2193 return;
2194 }
2195 wtoken = new AppWindowToken(token);
2196 wtoken.groupId = groupId;
2197 wtoken.appFullscreen = fullscreen;
2198 wtoken.requestedOrientation = requestedOrientation;
2199 mAppTokens.add(addPos, wtoken);
Dave Bortcfe65242009-04-09 14:51:04 -07002200 if (localLOGV) Log.v(TAG, "Adding new app token: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002201 mTokenMap.put(token.asBinder(), wtoken);
2202 mTokenList.add(wtoken);
Romain Guy06882f82009-06-10 13:36:04 -07002203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002204 // Application tokens start out hidden.
2205 wtoken.hidden = true;
2206 wtoken.hiddenRequested = true;
Romain Guy06882f82009-06-10 13:36:04 -07002207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002208 //dump();
2209 }
2210 }
Romain Guy06882f82009-06-10 13:36:04 -07002211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002212 public void setAppGroupId(IBinder token, int groupId) {
2213 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2214 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002215 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002216 }
2217
2218 synchronized(mWindowMap) {
2219 AppWindowToken wtoken = findAppWindowToken(token);
2220 if (wtoken == null) {
2221 Log.w(TAG, "Attempted to set group id of non-existing app token: " + token);
2222 return;
2223 }
2224 wtoken.groupId = groupId;
2225 }
2226 }
Romain Guy06882f82009-06-10 13:36:04 -07002227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002228 public int getOrientationFromWindowsLocked() {
2229 int pos = mWindows.size() - 1;
2230 while (pos >= 0) {
2231 WindowState wtoken = (WindowState) mWindows.get(pos);
2232 pos--;
2233 if (wtoken.mAppToken != null) {
2234 // We hit an application window. so the orientation will be determined by the
2235 // app window. No point in continuing further.
2236 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2237 }
2238 if (!wtoken.isVisibleLw()) {
2239 continue;
2240 }
2241 int req = wtoken.mAttrs.screenOrientation;
2242 if((req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) ||
2243 (req == ActivityInfo.SCREEN_ORIENTATION_BEHIND)){
2244 continue;
2245 } else {
2246 return req;
2247 }
2248 }
2249 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2250 }
Romain Guy06882f82009-06-10 13:36:04 -07002251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002252 public int getOrientationFromAppTokensLocked() {
2253 int pos = mAppTokens.size() - 1;
2254 int curGroup = 0;
2255 int lastOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Owen Lin3413b892009-05-01 17:12:32 -07002256 boolean findingBehind = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002257 boolean haveGroup = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002258 boolean lastFullscreen = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002259 while (pos >= 0) {
2260 AppWindowToken wtoken = mAppTokens.get(pos);
2261 pos--;
Owen Lin3413b892009-05-01 17:12:32 -07002262 // if we're about to tear down this window and not seek for
2263 // the behind activity, don't use it for orientation
2264 if (!findingBehind
2265 && (!wtoken.hidden && wtoken.hiddenRequested)) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002266 continue;
2267 }
2268
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002269 if (!haveGroup) {
2270 // We ignore any hidden applications on the top.
2271 if (wtoken.hiddenRequested || wtoken.willBeHidden) {
2272 continue;
2273 }
2274 haveGroup = true;
2275 curGroup = wtoken.groupId;
2276 lastOrientation = wtoken.requestedOrientation;
2277 } else if (curGroup != wtoken.groupId) {
2278 // If we have hit a new application group, and the bottom
2279 // of the previous group didn't explicitly say to use
The Android Open Source Project4df24232009-03-05 14:34:35 -08002280 // the orientation behind it, and the last app was
2281 // full screen, then we'll stick with the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002282 // user's orientation.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002283 if (lastOrientation != ActivityInfo.SCREEN_ORIENTATION_BEHIND
2284 && lastFullscreen) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002285 return lastOrientation;
2286 }
2287 }
2288 int or = wtoken.requestedOrientation;
Owen Lin3413b892009-05-01 17:12:32 -07002289 // If this application is fullscreen, and didn't explicitly say
2290 // to use the orientation behind it, then just take whatever
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002291 // orientation it has and ignores whatever is under it.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002292 lastFullscreen = wtoken.appFullscreen;
Romain Guy06882f82009-06-10 13:36:04 -07002293 if (lastFullscreen
Owen Lin3413b892009-05-01 17:12:32 -07002294 && or != ActivityInfo.SCREEN_ORIENTATION_BEHIND) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002295 return or;
2296 }
2297 // If this application has requested an explicit orientation,
2298 // then use it.
2299 if (or == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE ||
2300 or == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ||
2301 or == ActivityInfo.SCREEN_ORIENTATION_SENSOR ||
2302 or == ActivityInfo.SCREEN_ORIENTATION_NOSENSOR ||
2303 or == ActivityInfo.SCREEN_ORIENTATION_USER) {
2304 return or;
2305 }
Owen Lin3413b892009-05-01 17:12:32 -07002306 findingBehind |= (or == ActivityInfo.SCREEN_ORIENTATION_BEHIND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002307 }
2308 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2309 }
Romain Guy06882f82009-06-10 13:36:04 -07002310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002311 public Configuration updateOrientationFromAppTokens(
The Android Open Source Project10592532009-03-18 17:39:46 -07002312 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002313 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2314 "updateOrientationFromAppTokens()")) {
2315 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
2316 }
2317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002318 Configuration config;
2319 long ident = Binder.clearCallingIdentity();
Dianne Hackborncfaef692009-06-15 14:24:44 -07002320 config = updateOrientationFromAppTokensUnchecked(currentConfig,
2321 freezeThisOneIfNeeded);
2322 Binder.restoreCallingIdentity(ident);
2323 return config;
2324 }
2325
2326 Configuration updateOrientationFromAppTokensUnchecked(
2327 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
2328 Configuration config;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002329 synchronized(mWindowMap) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002330 config = updateOrientationFromAppTokensLocked(currentConfig, freezeThisOneIfNeeded);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002331 }
2332 if (config != null) {
2333 mLayoutNeeded = true;
2334 performLayoutAndPlaceSurfacesLocked();
2335 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002336 return config;
2337 }
Romain Guy06882f82009-06-10 13:36:04 -07002338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002339 /*
2340 * The orientation is computed from non-application windows first. If none of
2341 * the non-application windows specify orientation, the orientation is computed from
Romain Guy06882f82009-06-10 13:36:04 -07002342 * application tokens.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002343 * @see android.view.IWindowManager#updateOrientationFromAppTokens(
2344 * android.os.IBinder)
2345 */
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002346 Configuration updateOrientationFromAppTokensLocked(
The Android Open Source Project10592532009-03-18 17:39:46 -07002347 Configuration appConfig, IBinder freezeThisOneIfNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002348 boolean changed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002349 long ident = Binder.clearCallingIdentity();
2350 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002351 int req = computeForcedAppOrientationLocked();
Romain Guy06882f82009-06-10 13:36:04 -07002352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002353 if (req != mForcedAppOrientation) {
2354 changed = true;
2355 mForcedAppOrientation = req;
2356 //send a message to Policy indicating orientation change to take
2357 //action like disabling/enabling sensors etc.,
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002358 mPolicy.setCurrentOrientationLw(req);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002359 }
Romain Guy06882f82009-06-10 13:36:04 -07002360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002361 if (changed) {
2362 changed = setRotationUncheckedLocked(
Dianne Hackborn321ae682009-03-27 16:16:03 -07002363 WindowManagerPolicy.USE_LAST_ROTATION,
2364 mLastRotationFlags & (~Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002365 if (changed) {
2366 if (freezeThisOneIfNeeded != null) {
2367 AppWindowToken wtoken = findAppWindowToken(
2368 freezeThisOneIfNeeded);
2369 if (wtoken != null) {
2370 startAppFreezingScreenLocked(wtoken,
2371 ActivityInfo.CONFIG_ORIENTATION);
2372 }
2373 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07002374 return computeNewConfigurationLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002375 }
2376 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002377
2378 // No obvious action we need to take, but if our current
2379 // state mismatches the activity maanager's, update it
2380 if (appConfig != null) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07002381 mTempConfiguration.setToDefaults();
2382 if (computeNewConfigurationLocked(mTempConfiguration)) {
2383 if (appConfig.diff(mTempConfiguration) != 0) {
2384 Log.i(TAG, "Config changed: " + mTempConfiguration);
2385 return new Configuration(mTempConfiguration);
2386 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002387 }
2388 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002389 } finally {
2390 Binder.restoreCallingIdentity(ident);
2391 }
Romain Guy06882f82009-06-10 13:36:04 -07002392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002393 return null;
2394 }
Romain Guy06882f82009-06-10 13:36:04 -07002395
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002396 int computeForcedAppOrientationLocked() {
2397 int req = getOrientationFromWindowsLocked();
2398 if (req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
2399 req = getOrientationFromAppTokensLocked();
2400 }
2401 return req;
2402 }
Romain Guy06882f82009-06-10 13:36:04 -07002403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002404 public void setAppOrientation(IApplicationToken token, int requestedOrientation) {
2405 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2406 "setAppOrientation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002407 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002408 }
Romain Guy06882f82009-06-10 13:36:04 -07002409
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002410 synchronized(mWindowMap) {
2411 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2412 if (wtoken == null) {
2413 Log.w(TAG, "Attempted to set orientation of non-existing app token: " + token);
2414 return;
2415 }
Romain Guy06882f82009-06-10 13:36:04 -07002416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002417 wtoken.requestedOrientation = requestedOrientation;
2418 }
2419 }
Romain Guy06882f82009-06-10 13:36:04 -07002420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002421 public int getAppOrientation(IApplicationToken token) {
2422 synchronized(mWindowMap) {
2423 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2424 if (wtoken == null) {
2425 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2426 }
Romain Guy06882f82009-06-10 13:36:04 -07002427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002428 return wtoken.requestedOrientation;
2429 }
2430 }
Romain Guy06882f82009-06-10 13:36:04 -07002431
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002432 public void setFocusedApp(IBinder token, boolean moveFocusNow) {
2433 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2434 "setFocusedApp()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002435 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002436 }
2437
2438 synchronized(mWindowMap) {
2439 boolean changed = false;
2440 if (token == null) {
2441 if (DEBUG_FOCUS) Log.v(TAG, "Clearing focused app, was " + mFocusedApp);
2442 changed = mFocusedApp != null;
2443 mFocusedApp = null;
2444 mKeyWaiter.tickle();
2445 } else {
2446 AppWindowToken newFocus = findAppWindowToken(token);
2447 if (newFocus == null) {
2448 Log.w(TAG, "Attempted to set focus to non-existing app token: " + token);
2449 return;
2450 }
2451 changed = mFocusedApp != newFocus;
2452 mFocusedApp = newFocus;
2453 if (DEBUG_FOCUS) Log.v(TAG, "Set focused app to: " + mFocusedApp);
2454 mKeyWaiter.tickle();
2455 }
2456
2457 if (moveFocusNow && changed) {
2458 final long origId = Binder.clearCallingIdentity();
2459 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2460 Binder.restoreCallingIdentity(origId);
2461 }
2462 }
2463 }
2464
2465 public void prepareAppTransition(int transit) {
2466 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2467 "prepareAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002468 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002469 }
Romain Guy06882f82009-06-10 13:36:04 -07002470
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002471 synchronized(mWindowMap) {
2472 if (DEBUG_APP_TRANSITIONS) Log.v(
2473 TAG, "Prepare app transition: transit=" + transit
2474 + " mNextAppTransition=" + mNextAppTransition);
2475 if (!mDisplayFrozen) {
2476 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
2477 mNextAppTransition = transit;
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07002478 } else if (transit == WindowManagerPolicy.TRANSIT_TASK_OPEN
2479 && mNextAppTransition == WindowManagerPolicy.TRANSIT_TASK_CLOSE) {
2480 // Opening a new task always supersedes a close for the anim.
2481 mNextAppTransition = transit;
2482 } else if (transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN
2483 && mNextAppTransition == WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE) {
2484 // Opening a new activity always supersedes a close for the anim.
2485 mNextAppTransition = transit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002486 }
2487 mAppTransitionReady = false;
2488 mAppTransitionTimeout = false;
2489 mStartingIconInTransition = false;
2490 mSkipAppTransitionAnimation = false;
2491 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
2492 mH.sendMessageDelayed(mH.obtainMessage(H.APP_TRANSITION_TIMEOUT),
2493 5000);
2494 }
2495 }
2496 }
2497
2498 public int getPendingAppTransition() {
2499 return mNextAppTransition;
2500 }
Romain Guy06882f82009-06-10 13:36:04 -07002501
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002502 public void executeAppTransition() {
2503 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2504 "executeAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002505 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002506 }
Romain Guy06882f82009-06-10 13:36:04 -07002507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002508 synchronized(mWindowMap) {
2509 if (DEBUG_APP_TRANSITIONS) Log.v(
2510 TAG, "Execute app transition: mNextAppTransition=" + mNextAppTransition);
2511 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
2512 mAppTransitionReady = true;
2513 final long origId = Binder.clearCallingIdentity();
2514 performLayoutAndPlaceSurfacesLocked();
2515 Binder.restoreCallingIdentity(origId);
2516 }
2517 }
2518 }
2519
2520 public void setAppStartingWindow(IBinder token, String pkg,
2521 int theme, CharSequence nonLocalizedLabel, int labelRes, int icon,
2522 IBinder transferFrom, boolean createIfNeeded) {
2523 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2524 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002525 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002526 }
2527
2528 synchronized(mWindowMap) {
2529 if (DEBUG_STARTING_WINDOW) Log.v(
2530 TAG, "setAppStartingIcon: token=" + token + " pkg=" + pkg
2531 + " transferFrom=" + transferFrom);
Romain Guy06882f82009-06-10 13:36:04 -07002532
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002533 AppWindowToken wtoken = findAppWindowToken(token);
2534 if (wtoken == null) {
2535 Log.w(TAG, "Attempted to set icon of non-existing app token: " + token);
2536 return;
2537 }
2538
2539 // If the display is frozen, we won't do anything until the
2540 // actual window is displayed so there is no reason to put in
2541 // the starting window.
2542 if (mDisplayFrozen) {
2543 return;
2544 }
Romain Guy06882f82009-06-10 13:36:04 -07002545
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002546 if (wtoken.startingData != null) {
2547 return;
2548 }
Romain Guy06882f82009-06-10 13:36:04 -07002549
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002550 if (transferFrom != null) {
2551 AppWindowToken ttoken = findAppWindowToken(transferFrom);
2552 if (ttoken != null) {
2553 WindowState startingWindow = ttoken.startingWindow;
2554 if (startingWindow != null) {
2555 if (mStartingIconInTransition) {
2556 // In this case, the starting icon has already
2557 // been displayed, so start letting windows get
2558 // shown immediately without any more transitions.
2559 mSkipAppTransitionAnimation = true;
2560 }
2561 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
2562 "Moving existing starting from " + ttoken
2563 + " to " + wtoken);
2564 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002565
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002566 // Transfer the starting window over to the new
2567 // token.
2568 wtoken.startingData = ttoken.startingData;
2569 wtoken.startingView = ttoken.startingView;
2570 wtoken.startingWindow = startingWindow;
2571 ttoken.startingData = null;
2572 ttoken.startingView = null;
2573 ttoken.startingWindow = null;
2574 ttoken.startingMoved = true;
2575 startingWindow.mToken = wtoken;
Dianne Hackbornef49c572009-03-24 19:27:32 -07002576 startingWindow.mRootToken = wtoken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002577 startingWindow.mAppToken = wtoken;
2578 mWindows.remove(startingWindow);
2579 ttoken.windows.remove(startingWindow);
2580 ttoken.allAppWindows.remove(startingWindow);
2581 addWindowToListInOrderLocked(startingWindow, true);
2582 wtoken.allAppWindows.add(startingWindow);
Romain Guy06882f82009-06-10 13:36:04 -07002583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002584 // Propagate other interesting state between the
2585 // tokens. If the old token is displayed, we should
2586 // immediately force the new one to be displayed. If
2587 // it is animating, we need to move that animation to
2588 // the new one.
2589 if (ttoken.allDrawn) {
2590 wtoken.allDrawn = true;
2591 }
2592 if (ttoken.firstWindowDrawn) {
2593 wtoken.firstWindowDrawn = true;
2594 }
2595 if (!ttoken.hidden) {
2596 wtoken.hidden = false;
2597 wtoken.hiddenRequested = false;
2598 wtoken.willBeHidden = false;
2599 }
2600 if (wtoken.clientHidden != ttoken.clientHidden) {
2601 wtoken.clientHidden = ttoken.clientHidden;
2602 wtoken.sendAppVisibilityToClients();
2603 }
2604 if (ttoken.animation != null) {
2605 wtoken.animation = ttoken.animation;
2606 wtoken.animating = ttoken.animating;
2607 wtoken.animLayerAdjustment = ttoken.animLayerAdjustment;
2608 ttoken.animation = null;
2609 ttoken.animLayerAdjustment = 0;
2610 wtoken.updateLayers();
2611 ttoken.updateLayers();
2612 }
Romain Guy06882f82009-06-10 13:36:04 -07002613
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002614 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002615 mLayoutNeeded = true;
2616 performLayoutAndPlaceSurfacesLocked();
2617 Binder.restoreCallingIdentity(origId);
2618 return;
2619 } else if (ttoken.startingData != null) {
2620 // The previous app was getting ready to show a
2621 // starting window, but hasn't yet done so. Steal it!
2622 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
2623 "Moving pending starting from " + ttoken
2624 + " to " + wtoken);
2625 wtoken.startingData = ttoken.startingData;
2626 ttoken.startingData = null;
2627 ttoken.startingMoved = true;
2628 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
2629 // Note: we really want to do sendMessageAtFrontOfQueue() because we
2630 // want to process the message ASAP, before any other queued
2631 // messages.
2632 mH.sendMessageAtFrontOfQueue(m);
2633 return;
2634 }
2635 }
2636 }
2637
2638 // There is no existing starting window, and the caller doesn't
2639 // want us to create one, so that's it!
2640 if (!createIfNeeded) {
2641 return;
2642 }
Romain Guy06882f82009-06-10 13:36:04 -07002643
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002644 mStartingIconInTransition = true;
2645 wtoken.startingData = new StartingData(
2646 pkg, theme, nonLocalizedLabel,
2647 labelRes, icon);
2648 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
2649 // Note: we really want to do sendMessageAtFrontOfQueue() because we
2650 // want to process the message ASAP, before any other queued
2651 // messages.
2652 mH.sendMessageAtFrontOfQueue(m);
2653 }
2654 }
2655
2656 public void setAppWillBeHidden(IBinder token) {
2657 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2658 "setAppWillBeHidden()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002659 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002660 }
2661
2662 AppWindowToken wtoken;
2663
2664 synchronized(mWindowMap) {
2665 wtoken = findAppWindowToken(token);
2666 if (wtoken == null) {
2667 Log.w(TAG, "Attempted to set will be hidden of non-existing app token: " + token);
2668 return;
2669 }
2670 wtoken.willBeHidden = true;
2671 }
2672 }
Romain Guy06882f82009-06-10 13:36:04 -07002673
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002674 boolean setTokenVisibilityLocked(AppWindowToken wtoken, WindowManager.LayoutParams lp,
2675 boolean visible, int transit, boolean performLayout) {
2676 boolean delayed = false;
2677
2678 if (wtoken.clientHidden == visible) {
2679 wtoken.clientHidden = !visible;
2680 wtoken.sendAppVisibilityToClients();
2681 }
Romain Guy06882f82009-06-10 13:36:04 -07002682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002683 wtoken.willBeHidden = false;
2684 if (wtoken.hidden == visible) {
2685 final int N = wtoken.allAppWindows.size();
2686 boolean changed = false;
2687 if (DEBUG_APP_TRANSITIONS) Log.v(
2688 TAG, "Changing app " + wtoken + " hidden=" + wtoken.hidden
2689 + " performLayout=" + performLayout);
Romain Guy06882f82009-06-10 13:36:04 -07002690
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002691 boolean runningAppAnimation = false;
Romain Guy06882f82009-06-10 13:36:04 -07002692
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002693 if (transit != WindowManagerPolicy.TRANSIT_NONE) {
2694 if (wtoken.animation == sDummyAnimation) {
2695 wtoken.animation = null;
2696 }
2697 applyAnimationLocked(wtoken, lp, transit, visible);
2698 changed = true;
2699 if (wtoken.animation != null) {
2700 delayed = runningAppAnimation = true;
2701 }
2702 }
Romain Guy06882f82009-06-10 13:36:04 -07002703
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002704 for (int i=0; i<N; i++) {
2705 WindowState win = wtoken.allAppWindows.get(i);
2706 if (win == wtoken.startingWindow) {
2707 continue;
2708 }
2709
2710 if (win.isAnimating()) {
2711 delayed = true;
2712 }
Romain Guy06882f82009-06-10 13:36:04 -07002713
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002714 //Log.i(TAG, "Window " + win + ": vis=" + win.isVisible());
2715 //win.dump(" ");
2716 if (visible) {
2717 if (!win.isVisibleNow()) {
2718 if (!runningAppAnimation) {
2719 applyAnimationLocked(win,
2720 WindowManagerPolicy.TRANSIT_ENTER, true);
2721 }
2722 changed = true;
2723 }
2724 } else if (win.isVisibleNow()) {
2725 if (!runningAppAnimation) {
2726 applyAnimationLocked(win,
2727 WindowManagerPolicy.TRANSIT_EXIT, false);
2728 }
2729 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2730 KeyWaiter.RETURN_NOTHING);
2731 changed = true;
2732 }
2733 }
2734
2735 wtoken.hidden = wtoken.hiddenRequested = !visible;
2736 if (!visible) {
2737 unsetAppFreezingScreenLocked(wtoken, true, true);
2738 } else {
2739 // If we are being set visible, and the starting window is
2740 // not yet displayed, then make sure it doesn't get displayed.
2741 WindowState swin = wtoken.startingWindow;
2742 if (swin != null && (swin.mDrawPending
2743 || swin.mCommitDrawPending)) {
2744 swin.mPolicyVisibility = false;
2745 swin.mPolicyVisibilityAfterAnim = false;
2746 }
2747 }
Romain Guy06882f82009-06-10 13:36:04 -07002748
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002749 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "setTokenVisibilityLocked: " + wtoken
2750 + ": hidden=" + wtoken.hidden + " hiddenRequested="
2751 + wtoken.hiddenRequested);
Romain Guy06882f82009-06-10 13:36:04 -07002752
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002753 if (changed && performLayout) {
2754 mLayoutNeeded = true;
2755 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002756 performLayoutAndPlaceSurfacesLocked();
2757 }
2758 }
2759
2760 if (wtoken.animation != null) {
2761 delayed = true;
2762 }
Romain Guy06882f82009-06-10 13:36:04 -07002763
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002764 return delayed;
2765 }
2766
2767 public void setAppVisibility(IBinder token, boolean visible) {
2768 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2769 "setAppVisibility()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002770 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002771 }
2772
2773 AppWindowToken wtoken;
2774
2775 synchronized(mWindowMap) {
2776 wtoken = findAppWindowToken(token);
2777 if (wtoken == null) {
2778 Log.w(TAG, "Attempted to set visibility of non-existing app token: " + token);
2779 return;
2780 }
2781
2782 if (DEBUG_APP_TRANSITIONS || DEBUG_ORIENTATION) {
2783 RuntimeException e = new RuntimeException();
2784 e.fillInStackTrace();
2785 Log.v(TAG, "setAppVisibility(" + token + ", " + visible
2786 + "): mNextAppTransition=" + mNextAppTransition
2787 + " hidden=" + wtoken.hidden
2788 + " hiddenRequested=" + wtoken.hiddenRequested, e);
2789 }
Romain Guy06882f82009-06-10 13:36:04 -07002790
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002791 // If we are preparing an app transition, then delay changing
2792 // the visibility of this token until we execute that transition.
2793 if (!mDisplayFrozen && mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
2794 // Already in requested state, don't do anything more.
2795 if (wtoken.hiddenRequested != visible) {
2796 return;
2797 }
2798 wtoken.hiddenRequested = !visible;
Romain Guy06882f82009-06-10 13:36:04 -07002799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002800 if (DEBUG_APP_TRANSITIONS) Log.v(
2801 TAG, "Setting dummy animation on: " + wtoken);
2802 wtoken.setDummyAnimation();
2803 mOpeningApps.remove(wtoken);
2804 mClosingApps.remove(wtoken);
2805 wtoken.inPendingTransaction = true;
2806 if (visible) {
2807 mOpeningApps.add(wtoken);
2808 wtoken.allDrawn = false;
2809 wtoken.startingDisplayed = false;
2810 wtoken.startingMoved = false;
Romain Guy06882f82009-06-10 13:36:04 -07002811
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002812 if (wtoken.clientHidden) {
2813 // In the case where we are making an app visible
2814 // but holding off for a transition, we still need
2815 // to tell the client to make its windows visible so
2816 // they get drawn. Otherwise, we will wait on
2817 // performing the transition until all windows have
2818 // been drawn, they never will be, and we are sad.
2819 wtoken.clientHidden = false;
2820 wtoken.sendAppVisibilityToClients();
2821 }
2822 } else {
2823 mClosingApps.add(wtoken);
2824 }
2825 return;
2826 }
Romain Guy06882f82009-06-10 13:36:04 -07002827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002828 final long origId = Binder.clearCallingIdentity();
2829 setTokenVisibilityLocked(wtoken, null, visible, WindowManagerPolicy.TRANSIT_NONE, true);
2830 wtoken.updateReportedVisibilityLocked();
2831 Binder.restoreCallingIdentity(origId);
2832 }
2833 }
2834
2835 void unsetAppFreezingScreenLocked(AppWindowToken wtoken,
2836 boolean unfreezeSurfaceNow, boolean force) {
2837 if (wtoken.freezingScreen) {
2838 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + wtoken
2839 + " force=" + force);
2840 final int N = wtoken.allAppWindows.size();
2841 boolean unfrozeWindows = false;
2842 for (int i=0; i<N; i++) {
2843 WindowState w = wtoken.allAppWindows.get(i);
2844 if (w.mAppFreezing) {
2845 w.mAppFreezing = false;
2846 if (w.mSurface != null && !w.mOrientationChanging) {
2847 w.mOrientationChanging = true;
2848 }
2849 unfrozeWindows = true;
2850 }
2851 }
2852 if (force || unfrozeWindows) {
2853 if (DEBUG_ORIENTATION) Log.v(TAG, "No longer freezing: " + wtoken);
2854 wtoken.freezingScreen = false;
2855 mAppsFreezingScreen--;
2856 }
2857 if (unfreezeSurfaceNow) {
2858 if (unfrozeWindows) {
2859 mLayoutNeeded = true;
2860 performLayoutAndPlaceSurfacesLocked();
2861 }
2862 if (mAppsFreezingScreen == 0 && !mWindowsFreezingScreen) {
2863 stopFreezingDisplayLocked();
2864 }
2865 }
2866 }
2867 }
Romain Guy06882f82009-06-10 13:36:04 -07002868
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002869 public void startAppFreezingScreenLocked(AppWindowToken wtoken,
2870 int configChanges) {
2871 if (DEBUG_ORIENTATION) {
2872 RuntimeException e = new RuntimeException();
2873 e.fillInStackTrace();
2874 Log.i(TAG, "Set freezing of " + wtoken.appToken
2875 + ": hidden=" + wtoken.hidden + " freezing="
2876 + wtoken.freezingScreen, e);
2877 }
2878 if (!wtoken.hiddenRequested) {
2879 if (!wtoken.freezingScreen) {
2880 wtoken.freezingScreen = true;
2881 mAppsFreezingScreen++;
2882 if (mAppsFreezingScreen == 1) {
2883 startFreezingDisplayLocked();
2884 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
2885 mH.sendMessageDelayed(mH.obtainMessage(H.APP_FREEZE_TIMEOUT),
2886 5000);
2887 }
2888 }
2889 final int N = wtoken.allAppWindows.size();
2890 for (int i=0; i<N; i++) {
2891 WindowState w = wtoken.allAppWindows.get(i);
2892 w.mAppFreezing = true;
2893 }
2894 }
2895 }
Romain Guy06882f82009-06-10 13:36:04 -07002896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002897 public void startAppFreezingScreen(IBinder token, int configChanges) {
2898 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2899 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002900 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002901 }
2902
2903 synchronized(mWindowMap) {
2904 if (configChanges == 0 && !mDisplayFrozen) {
2905 if (DEBUG_ORIENTATION) Log.v(TAG, "Skipping set freeze of " + token);
2906 return;
2907 }
Romain Guy06882f82009-06-10 13:36:04 -07002908
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002909 AppWindowToken wtoken = findAppWindowToken(token);
2910 if (wtoken == null || wtoken.appToken == null) {
2911 Log.w(TAG, "Attempted to freeze screen with non-existing app token: " + wtoken);
2912 return;
2913 }
2914 final long origId = Binder.clearCallingIdentity();
2915 startAppFreezingScreenLocked(wtoken, configChanges);
2916 Binder.restoreCallingIdentity(origId);
2917 }
2918 }
Romain Guy06882f82009-06-10 13:36:04 -07002919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002920 public void stopAppFreezingScreen(IBinder token, boolean force) {
2921 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2922 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002923 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002924 }
2925
2926 synchronized(mWindowMap) {
2927 AppWindowToken wtoken = findAppWindowToken(token);
2928 if (wtoken == null || wtoken.appToken == null) {
2929 return;
2930 }
2931 final long origId = Binder.clearCallingIdentity();
2932 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + token
2933 + ": hidden=" + wtoken.hidden + " freezing=" + wtoken.freezingScreen);
2934 unsetAppFreezingScreenLocked(wtoken, true, force);
2935 Binder.restoreCallingIdentity(origId);
2936 }
2937 }
Romain Guy06882f82009-06-10 13:36:04 -07002938
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002939 public void removeAppToken(IBinder token) {
2940 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2941 "removeAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002942 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002943 }
2944
2945 AppWindowToken wtoken = null;
2946 AppWindowToken startingToken = null;
2947 boolean delayed = false;
2948
2949 final long origId = Binder.clearCallingIdentity();
2950 synchronized(mWindowMap) {
2951 WindowToken basewtoken = mTokenMap.remove(token);
2952 mTokenList.remove(basewtoken);
2953 if (basewtoken != null && (wtoken=basewtoken.appWindowToken) != null) {
2954 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "Removing app token: " + wtoken);
2955 delayed = setTokenVisibilityLocked(wtoken, null, false, WindowManagerPolicy.TRANSIT_NONE, true);
2956 wtoken.inPendingTransaction = false;
2957 mOpeningApps.remove(wtoken);
2958 if (mClosingApps.contains(wtoken)) {
2959 delayed = true;
2960 } else if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
2961 mClosingApps.add(wtoken);
2962 delayed = true;
2963 }
2964 if (DEBUG_APP_TRANSITIONS) Log.v(
2965 TAG, "Removing app " + wtoken + " delayed=" + delayed
2966 + " animation=" + wtoken.animation
2967 + " animating=" + wtoken.animating);
2968 if (delayed) {
2969 // set the token aside because it has an active animation to be finished
2970 mExitingAppTokens.add(wtoken);
2971 }
2972 mAppTokens.remove(wtoken);
2973 wtoken.removed = true;
2974 if (wtoken.startingData != null) {
2975 startingToken = wtoken;
2976 }
2977 unsetAppFreezingScreenLocked(wtoken, true, true);
2978 if (mFocusedApp == wtoken) {
2979 if (DEBUG_FOCUS) Log.v(TAG, "Removing focused app token:" + wtoken);
2980 mFocusedApp = null;
2981 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2982 mKeyWaiter.tickle();
2983 }
2984 } else {
2985 Log.w(TAG, "Attempted to remove non-existing app token: " + token);
2986 }
Romain Guy06882f82009-06-10 13:36:04 -07002987
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002988 if (!delayed && wtoken != null) {
2989 wtoken.updateReportedVisibilityLocked();
2990 }
2991 }
2992 Binder.restoreCallingIdentity(origId);
2993
2994 if (startingToken != null) {
2995 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Schedule remove starting "
2996 + startingToken + ": app token removed");
2997 Message m = mH.obtainMessage(H.REMOVE_STARTING, startingToken);
2998 mH.sendMessage(m);
2999 }
3000 }
3001
3002 private boolean tmpRemoveAppWindowsLocked(WindowToken token) {
3003 final int NW = token.windows.size();
3004 for (int i=0; i<NW; i++) {
3005 WindowState win = token.windows.get(i);
3006 mWindows.remove(win);
3007 int j = win.mChildWindows.size();
3008 while (j > 0) {
3009 j--;
3010 mWindows.remove(win.mChildWindows.get(j));
3011 }
3012 }
3013 return NW > 0;
3014 }
3015
3016 void dumpAppTokensLocked() {
3017 for (int i=mAppTokens.size()-1; i>=0; i--) {
3018 Log.v(TAG, " #" + i + ": " + mAppTokens.get(i).token);
3019 }
3020 }
Romain Guy06882f82009-06-10 13:36:04 -07003021
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003022 void dumpWindowsLocked() {
3023 for (int i=mWindows.size()-1; i>=0; i--) {
3024 Log.v(TAG, " #" + i + ": " + mWindows.get(i));
3025 }
3026 }
Romain Guy06882f82009-06-10 13:36:04 -07003027
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003028 private int findWindowOffsetLocked(int tokenPos) {
3029 final int NW = mWindows.size();
3030
3031 if (tokenPos >= mAppTokens.size()) {
3032 int i = NW;
3033 while (i > 0) {
3034 i--;
3035 WindowState win = (WindowState)mWindows.get(i);
3036 if (win.getAppToken() != null) {
3037 return i+1;
3038 }
3039 }
3040 }
3041
3042 while (tokenPos > 0) {
3043 // Find the first app token below the new position that has
3044 // a window displayed.
3045 final AppWindowToken wtoken = mAppTokens.get(tokenPos-1);
3046 if (DEBUG_REORDER) Log.v(TAG, "Looking for lower windows @ "
3047 + tokenPos + " -- " + wtoken.token);
3048 int i = wtoken.windows.size();
3049 while (i > 0) {
3050 i--;
3051 WindowState win = wtoken.windows.get(i);
3052 int j = win.mChildWindows.size();
3053 while (j > 0) {
3054 j--;
3055 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3056 if (cwin.mSubLayer >= 0 ) {
3057 for (int pos=NW-1; pos>=0; pos--) {
3058 if (mWindows.get(pos) == cwin) {
3059 if (DEBUG_REORDER) Log.v(TAG,
3060 "Found child win @" + (pos+1));
3061 return pos+1;
3062 }
3063 }
3064 }
3065 }
3066 for (int pos=NW-1; pos>=0; pos--) {
3067 if (mWindows.get(pos) == win) {
3068 if (DEBUG_REORDER) Log.v(TAG, "Found win @" + (pos+1));
3069 return pos+1;
3070 }
3071 }
3072 }
3073 tokenPos--;
3074 }
3075
3076 return 0;
3077 }
3078
3079 private final int reAddWindowLocked(int index, WindowState win) {
3080 final int NCW = win.mChildWindows.size();
3081 boolean added = false;
3082 for (int j=0; j<NCW; j++) {
3083 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3084 if (!added && cwin.mSubLayer >= 0) {
3085 mWindows.add(index, win);
3086 index++;
3087 added = true;
3088 }
3089 mWindows.add(index, cwin);
3090 index++;
3091 }
3092 if (!added) {
3093 mWindows.add(index, win);
3094 index++;
3095 }
3096 return index;
3097 }
Romain Guy06882f82009-06-10 13:36:04 -07003098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003099 private final int reAddAppWindowsLocked(int index, WindowToken token) {
3100 final int NW = token.windows.size();
3101 for (int i=0; i<NW; i++) {
3102 index = reAddWindowLocked(index, token.windows.get(i));
3103 }
3104 return index;
3105 }
3106
3107 public void moveAppToken(int index, IBinder token) {
3108 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3109 "moveAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003110 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003111 }
3112
3113 synchronized(mWindowMap) {
3114 if (DEBUG_REORDER) Log.v(TAG, "Initial app tokens:");
3115 if (DEBUG_REORDER) dumpAppTokensLocked();
3116 final AppWindowToken wtoken = findAppWindowToken(token);
3117 if (wtoken == null || !mAppTokens.remove(wtoken)) {
3118 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3119 + token + " (" + wtoken + ")");
3120 return;
3121 }
3122 mAppTokens.add(index, wtoken);
3123 if (DEBUG_REORDER) Log.v(TAG, "Moved " + token + " to " + index + ":");
3124 if (DEBUG_REORDER) dumpAppTokensLocked();
Romain Guy06882f82009-06-10 13:36:04 -07003125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003126 final long origId = Binder.clearCallingIdentity();
3127 if (DEBUG_REORDER) Log.v(TAG, "Removing windows in " + token + ":");
3128 if (DEBUG_REORDER) dumpWindowsLocked();
3129 if (tmpRemoveAppWindowsLocked(wtoken)) {
3130 if (DEBUG_REORDER) Log.v(TAG, "Adding windows back in:");
3131 if (DEBUG_REORDER) dumpWindowsLocked();
3132 reAddAppWindowsLocked(findWindowOffsetLocked(index), wtoken);
3133 if (DEBUG_REORDER) Log.v(TAG, "Final window list:");
3134 if (DEBUG_REORDER) dumpWindowsLocked();
3135 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003136 mLayoutNeeded = true;
3137 performLayoutAndPlaceSurfacesLocked();
3138 }
3139 Binder.restoreCallingIdentity(origId);
3140 }
3141 }
3142
3143 private void removeAppTokensLocked(List<IBinder> tokens) {
3144 // XXX This should be done more efficiently!
3145 // (take advantage of the fact that both lists should be
3146 // ordered in the same way.)
3147 int N = tokens.size();
3148 for (int i=0; i<N; i++) {
3149 IBinder token = tokens.get(i);
3150 final AppWindowToken wtoken = findAppWindowToken(token);
3151 if (!mAppTokens.remove(wtoken)) {
3152 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3153 + token + " (" + wtoken + ")");
3154 i--;
3155 N--;
3156 }
3157 }
3158 }
3159
3160 private void moveAppWindowsLocked(List<IBinder> tokens, int tokenPos) {
3161 // First remove all of the windows from the list.
3162 final int N = tokens.size();
3163 int i;
3164 for (i=0; i<N; i++) {
3165 WindowToken token = mTokenMap.get(tokens.get(i));
3166 if (token != null) {
3167 tmpRemoveAppWindowsLocked(token);
3168 }
3169 }
3170
3171 // Where to start adding?
3172 int pos = findWindowOffsetLocked(tokenPos);
3173
3174 // And now add them back at the correct place.
3175 for (i=0; i<N; i++) {
3176 WindowToken token = mTokenMap.get(tokens.get(i));
3177 if (token != null) {
3178 pos = reAddAppWindowsLocked(pos, token);
3179 }
3180 }
3181
3182 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003183 mLayoutNeeded = true;
3184 performLayoutAndPlaceSurfacesLocked();
3185
3186 //dump();
3187 }
3188
3189 public void moveAppTokensToTop(List<IBinder> tokens) {
3190 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3191 "moveAppTokensToTop()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003192 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003193 }
3194
3195 final long origId = Binder.clearCallingIdentity();
3196 synchronized(mWindowMap) {
3197 removeAppTokensLocked(tokens);
3198 final int N = tokens.size();
3199 for (int i=0; i<N; i++) {
3200 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3201 if (wt != null) {
3202 mAppTokens.add(wt);
3203 }
3204 }
3205 moveAppWindowsLocked(tokens, mAppTokens.size());
3206 }
3207 Binder.restoreCallingIdentity(origId);
3208 }
3209
3210 public void moveAppTokensToBottom(List<IBinder> tokens) {
3211 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3212 "moveAppTokensToBottom()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003213 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003214 }
3215
3216 final long origId = Binder.clearCallingIdentity();
3217 synchronized(mWindowMap) {
3218 removeAppTokensLocked(tokens);
3219 final int N = tokens.size();
3220 int pos = 0;
3221 for (int i=0; i<N; i++) {
3222 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3223 if (wt != null) {
3224 mAppTokens.add(pos, wt);
3225 pos++;
3226 }
3227 }
3228 moveAppWindowsLocked(tokens, 0);
3229 }
3230 Binder.restoreCallingIdentity(origId);
3231 }
3232
3233 // -------------------------------------------------------------
3234 // Misc IWindowSession methods
3235 // -------------------------------------------------------------
Romain Guy06882f82009-06-10 13:36:04 -07003236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003237 public void disableKeyguard(IBinder token, String tag) {
3238 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3239 != PackageManager.PERMISSION_GRANTED) {
3240 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3241 }
3242 mKeyguardDisabled.acquire(token, tag);
3243 }
3244
3245 public void reenableKeyguard(IBinder token) {
3246 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3247 != PackageManager.PERMISSION_GRANTED) {
3248 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3249 }
3250 synchronized (mKeyguardDisabled) {
3251 mKeyguardDisabled.release(token);
3252
3253 if (!mKeyguardDisabled.isAcquired()) {
3254 // if we are the last one to reenable the keyguard wait until
3255 // we have actaully finished reenabling until returning
3256 mWaitingUntilKeyguardReenabled = true;
3257 while (mWaitingUntilKeyguardReenabled) {
3258 try {
3259 mKeyguardDisabled.wait();
3260 } catch (InterruptedException e) {
3261 Thread.currentThread().interrupt();
3262 }
3263 }
3264 }
3265 }
3266 }
3267
3268 /**
3269 * @see android.app.KeyguardManager#exitKeyguardSecurely
3270 */
3271 public void exitKeyguardSecurely(final IOnKeyguardExitResult callback) {
3272 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3273 != PackageManager.PERMISSION_GRANTED) {
3274 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3275 }
3276 mPolicy.exitKeyguardSecurely(new WindowManagerPolicy.OnKeyguardExitResult() {
3277 public void onKeyguardExitResult(boolean success) {
3278 try {
3279 callback.onKeyguardExitResult(success);
3280 } catch (RemoteException e) {
3281 // Client has died, we don't care.
3282 }
3283 }
3284 });
3285 }
3286
3287 public boolean inKeyguardRestrictedInputMode() {
3288 return mPolicy.inKeyguardRestrictedKeyInputMode();
3289 }
Romain Guy06882f82009-06-10 13:36:04 -07003290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003291 static float fixScale(float scale) {
3292 if (scale < 0) scale = 0;
3293 else if (scale > 20) scale = 20;
3294 return Math.abs(scale);
3295 }
Romain Guy06882f82009-06-10 13:36:04 -07003296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003297 public void setAnimationScale(int which, float scale) {
3298 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
3299 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003300 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003301 }
3302
3303 if (scale < 0) scale = 0;
3304 else if (scale > 20) scale = 20;
3305 scale = Math.abs(scale);
3306 switch (which) {
3307 case 0: mWindowAnimationScale = fixScale(scale); break;
3308 case 1: mTransitionAnimationScale = fixScale(scale); break;
3309 }
Romain Guy06882f82009-06-10 13:36:04 -07003310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003311 // Persist setting
3312 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
3313 }
Romain Guy06882f82009-06-10 13:36:04 -07003314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003315 public void setAnimationScales(float[] scales) {
3316 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
3317 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003318 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003319 }
3320
3321 if (scales != null) {
3322 if (scales.length >= 1) {
3323 mWindowAnimationScale = fixScale(scales[0]);
3324 }
3325 if (scales.length >= 2) {
3326 mTransitionAnimationScale = fixScale(scales[1]);
3327 }
3328 }
Romain Guy06882f82009-06-10 13:36:04 -07003329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003330 // Persist setting
3331 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
3332 }
Romain Guy06882f82009-06-10 13:36:04 -07003333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003334 public float getAnimationScale(int which) {
3335 switch (which) {
3336 case 0: return mWindowAnimationScale;
3337 case 1: return mTransitionAnimationScale;
3338 }
3339 return 0;
3340 }
Romain Guy06882f82009-06-10 13:36:04 -07003341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003342 public float[] getAnimationScales() {
3343 return new float[] { mWindowAnimationScale, mTransitionAnimationScale };
3344 }
Romain Guy06882f82009-06-10 13:36:04 -07003345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003346 public int getSwitchState(int sw) {
3347 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3348 "getSwitchState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003349 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003350 }
3351 return KeyInputQueue.getSwitchState(sw);
3352 }
Romain Guy06882f82009-06-10 13:36:04 -07003353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003354 public int getSwitchStateForDevice(int devid, int sw) {
3355 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3356 "getSwitchStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003357 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003358 }
3359 return KeyInputQueue.getSwitchState(devid, sw);
3360 }
Romain Guy06882f82009-06-10 13:36:04 -07003361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003362 public int getScancodeState(int sw) {
3363 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3364 "getScancodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003365 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003366 }
3367 return KeyInputQueue.getScancodeState(sw);
3368 }
Romain Guy06882f82009-06-10 13:36:04 -07003369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003370 public int getScancodeStateForDevice(int devid, int sw) {
3371 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3372 "getScancodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003373 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003374 }
3375 return KeyInputQueue.getScancodeState(devid, sw);
3376 }
Romain Guy06882f82009-06-10 13:36:04 -07003377
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003378 public int getKeycodeState(int sw) {
3379 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3380 "getKeycodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003381 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003382 }
3383 return KeyInputQueue.getKeycodeState(sw);
3384 }
Romain Guy06882f82009-06-10 13:36:04 -07003385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003386 public int getKeycodeStateForDevice(int devid, int sw) {
3387 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3388 "getKeycodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003389 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003390 }
3391 return KeyInputQueue.getKeycodeState(devid, sw);
3392 }
Romain Guy06882f82009-06-10 13:36:04 -07003393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003394 public boolean hasKeys(int[] keycodes, boolean[] keyExists) {
3395 return KeyInputQueue.hasKeys(keycodes, keyExists);
3396 }
Romain Guy06882f82009-06-10 13:36:04 -07003397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003398 public void enableScreenAfterBoot() {
3399 synchronized(mWindowMap) {
3400 if (mSystemBooted) {
3401 return;
3402 }
3403 mSystemBooted = true;
3404 }
Romain Guy06882f82009-06-10 13:36:04 -07003405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003406 performEnableScreen();
3407 }
Romain Guy06882f82009-06-10 13:36:04 -07003408
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003409 public void enableScreenIfNeededLocked() {
3410 if (mDisplayEnabled) {
3411 return;
3412 }
3413 if (!mSystemBooted) {
3414 return;
3415 }
3416 mH.sendMessage(mH.obtainMessage(H.ENABLE_SCREEN));
3417 }
Romain Guy06882f82009-06-10 13:36:04 -07003418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003419 public void performEnableScreen() {
3420 synchronized(mWindowMap) {
3421 if (mDisplayEnabled) {
3422 return;
3423 }
3424 if (!mSystemBooted) {
3425 return;
3426 }
Romain Guy06882f82009-06-10 13:36:04 -07003427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003428 // Don't enable the screen until all existing windows
3429 // have been drawn.
3430 final int N = mWindows.size();
3431 for (int i=0; i<N; i++) {
3432 WindowState w = (WindowState)mWindows.get(i);
3433 if (w.isVisibleLw() && !w.isDisplayedLw()) {
3434 return;
3435 }
3436 }
Romain Guy06882f82009-06-10 13:36:04 -07003437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003438 mDisplayEnabled = true;
3439 if (false) {
3440 Log.i(TAG, "ENABLING SCREEN!");
3441 StringWriter sw = new StringWriter();
3442 PrintWriter pw = new PrintWriter(sw);
3443 this.dump(null, pw, null);
3444 Log.i(TAG, sw.toString());
3445 }
3446 try {
3447 IBinder surfaceFlinger = ServiceManager.getService("SurfaceFlinger");
3448 if (surfaceFlinger != null) {
3449 //Log.i(TAG, "******* TELLING SURFACE FLINGER WE ARE BOOTED!");
3450 Parcel data = Parcel.obtain();
3451 data.writeInterfaceToken("android.ui.ISurfaceComposer");
3452 surfaceFlinger.transact(IBinder.FIRST_CALL_TRANSACTION,
3453 data, null, 0);
3454 data.recycle();
3455 }
3456 } catch (RemoteException ex) {
3457 Log.e(TAG, "Boot completed: SurfaceFlinger is dead!");
3458 }
3459 }
Romain Guy06882f82009-06-10 13:36:04 -07003460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003461 mPolicy.enableScreenAfterBoot();
Romain Guy06882f82009-06-10 13:36:04 -07003462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003463 // Make sure the last requested orientation has been applied.
Dianne Hackborn321ae682009-03-27 16:16:03 -07003464 setRotationUnchecked(WindowManagerPolicy.USE_LAST_ROTATION, false,
3465 mLastRotationFlags | Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003466 }
Romain Guy06882f82009-06-10 13:36:04 -07003467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003468 public void setInTouchMode(boolean mode) {
3469 synchronized(mWindowMap) {
3470 mInTouchMode = mode;
3471 }
3472 }
3473
Romain Guy06882f82009-06-10 13:36:04 -07003474 public void setRotation(int rotation,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003475 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003476 if (!checkCallingPermission(android.Manifest.permission.SET_ORIENTATION,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003477 "setRotation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003478 throw new SecurityException("Requires SET_ORIENTATION permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003479 }
3480
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003481 setRotationUnchecked(rotation, alwaysSendConfiguration, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003482 }
Romain Guy06882f82009-06-10 13:36:04 -07003483
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003484 public void setRotationUnchecked(int rotation,
3485 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003486 if(DEBUG_ORIENTATION) Log.v(TAG,
3487 "alwaysSendConfiguration set to "+alwaysSendConfiguration);
Romain Guy06882f82009-06-10 13:36:04 -07003488
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003489 long origId = Binder.clearCallingIdentity();
3490 boolean changed;
3491 synchronized(mWindowMap) {
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003492 changed = setRotationUncheckedLocked(rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003493 }
Romain Guy06882f82009-06-10 13:36:04 -07003494
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003495 if (changed) {
3496 sendNewConfiguration();
3497 synchronized(mWindowMap) {
3498 mLayoutNeeded = true;
3499 performLayoutAndPlaceSurfacesLocked();
3500 }
3501 } else if (alwaysSendConfiguration) {
3502 //update configuration ignoring orientation change
3503 sendNewConfiguration();
3504 }
Romain Guy06882f82009-06-10 13:36:04 -07003505
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003506 Binder.restoreCallingIdentity(origId);
3507 }
Romain Guy06882f82009-06-10 13:36:04 -07003508
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003509 public boolean setRotationUncheckedLocked(int rotation, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003510 boolean changed;
3511 if (rotation == WindowManagerPolicy.USE_LAST_ROTATION) {
3512 rotation = mRequestedRotation;
3513 } else {
3514 mRequestedRotation = rotation;
Dianne Hackborn321ae682009-03-27 16:16:03 -07003515 mLastRotationFlags = animFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003516 }
3517 if (DEBUG_ORIENTATION) Log.v(TAG, "Overwriting rotation value from " + rotation);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003518 rotation = mPolicy.rotationForOrientationLw(mForcedAppOrientation,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003519 mRotation, mDisplayEnabled);
3520 if (DEBUG_ORIENTATION) Log.v(TAG, "new rotation is set to " + rotation);
3521 changed = mDisplayEnabled && mRotation != rotation;
Romain Guy06882f82009-06-10 13:36:04 -07003522
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003523 if (changed) {
Romain Guy06882f82009-06-10 13:36:04 -07003524 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003525 "Rotation changed to " + rotation
3526 + " from " + mRotation
3527 + " (forceApp=" + mForcedAppOrientation
3528 + ", req=" + mRequestedRotation + ")");
3529 mRotation = rotation;
3530 mWindowsFreezingScreen = true;
3531 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
3532 mH.sendMessageDelayed(mH.obtainMessage(H.WINDOW_FREEZE_TIMEOUT),
3533 2000);
3534 startFreezingDisplayLocked();
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003535 Log.i(TAG, "Setting rotation to " + rotation + ", animFlags=" + animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003536 mQueue.setOrientation(rotation);
3537 if (mDisplayEnabled) {
Dianne Hackborn321ae682009-03-27 16:16:03 -07003538 Surface.setOrientation(0, rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003539 }
3540 for (int i=mWindows.size()-1; i>=0; i--) {
3541 WindowState w = (WindowState)mWindows.get(i);
3542 if (w.mSurface != null) {
3543 w.mOrientationChanging = true;
3544 }
3545 }
3546 for (int i=mRotationWatchers.size()-1; i>=0; i--) {
3547 try {
3548 mRotationWatchers.get(i).onRotationChanged(rotation);
3549 } catch (RemoteException e) {
3550 }
3551 }
3552 } //end if changed
Romain Guy06882f82009-06-10 13:36:04 -07003553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003554 return changed;
3555 }
Romain Guy06882f82009-06-10 13:36:04 -07003556
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003557 public int getRotation() {
3558 return mRotation;
3559 }
3560
3561 public int watchRotation(IRotationWatcher watcher) {
3562 final IBinder watcherBinder = watcher.asBinder();
3563 IBinder.DeathRecipient dr = new IBinder.DeathRecipient() {
3564 public void binderDied() {
3565 synchronized (mWindowMap) {
3566 for (int i=0; i<mRotationWatchers.size(); i++) {
3567 if (watcherBinder == mRotationWatchers.get(i).asBinder()) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07003568 IRotationWatcher removed = mRotationWatchers.remove(i);
3569 if (removed != null) {
3570 removed.asBinder().unlinkToDeath(this, 0);
3571 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003572 i--;
3573 }
3574 }
3575 }
3576 }
3577 };
Romain Guy06882f82009-06-10 13:36:04 -07003578
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003579 synchronized (mWindowMap) {
3580 try {
3581 watcher.asBinder().linkToDeath(dr, 0);
3582 mRotationWatchers.add(watcher);
3583 } catch (RemoteException e) {
3584 // Client died, no cleanup needed.
3585 }
Romain Guy06882f82009-06-10 13:36:04 -07003586
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003587 return mRotation;
3588 }
3589 }
3590
3591 /**
3592 * Starts the view server on the specified port.
3593 *
3594 * @param port The port to listener to.
3595 *
3596 * @return True if the server was successfully started, false otherwise.
3597 *
3598 * @see com.android.server.ViewServer
3599 * @see com.android.server.ViewServer#VIEW_SERVER_DEFAULT_PORT
3600 */
3601 public boolean startViewServer(int port) {
Romain Guy06882f82009-06-10 13:36:04 -07003602 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003603 return false;
3604 }
3605
3606 if (!checkCallingPermission(Manifest.permission.DUMP, "startViewServer")) {
3607 return false;
3608 }
3609
3610 if (port < 1024) {
3611 return false;
3612 }
3613
3614 if (mViewServer != null) {
3615 if (!mViewServer.isRunning()) {
3616 try {
3617 return mViewServer.start();
3618 } catch (IOException e) {
Romain Guy06882f82009-06-10 13:36:04 -07003619 Log.w(TAG, "View server did not start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003620 }
3621 }
3622 return false;
3623 }
3624
3625 try {
3626 mViewServer = new ViewServer(this, port);
3627 return mViewServer.start();
3628 } catch (IOException e) {
3629 Log.w(TAG, "View server did not start");
3630 }
3631 return false;
3632 }
3633
Romain Guy06882f82009-06-10 13:36:04 -07003634 private boolean isSystemSecure() {
3635 return "1".equals(SystemProperties.get(SYSTEM_SECURE, "1")) &&
3636 "0".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
3637 }
3638
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003639 /**
3640 * Stops the view server if it exists.
3641 *
3642 * @return True if the server stopped, false if it wasn't started or
3643 * couldn't be stopped.
3644 *
3645 * @see com.android.server.ViewServer
3646 */
3647 public boolean stopViewServer() {
Romain Guy06882f82009-06-10 13:36:04 -07003648 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003649 return false;
3650 }
3651
3652 if (!checkCallingPermission(Manifest.permission.DUMP, "stopViewServer")) {
3653 return false;
3654 }
3655
3656 if (mViewServer != null) {
3657 return mViewServer.stop();
3658 }
3659 return false;
3660 }
3661
3662 /**
3663 * Indicates whether the view server is running.
3664 *
3665 * @return True if the server is running, false otherwise.
3666 *
3667 * @see com.android.server.ViewServer
3668 */
3669 public boolean isViewServerRunning() {
Romain Guy06882f82009-06-10 13:36:04 -07003670 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003671 return false;
3672 }
3673
3674 if (!checkCallingPermission(Manifest.permission.DUMP, "isViewServerRunning")) {
3675 return false;
3676 }
3677
3678 return mViewServer != null && mViewServer.isRunning();
3679 }
3680
3681 /**
3682 * Lists all availble windows in the system. The listing is written in the
3683 * specified Socket's output stream with the following syntax:
3684 * windowHashCodeInHexadecimal windowName
3685 * Each line of the ouput represents a different window.
3686 *
3687 * @param client The remote client to send the listing to.
3688 * @return False if an error occured, true otherwise.
3689 */
3690 boolean viewServerListWindows(Socket client) {
Romain Guy06882f82009-06-10 13:36:04 -07003691 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003692 return false;
3693 }
3694
3695 boolean result = true;
3696
3697 Object[] windows;
3698 synchronized (mWindowMap) {
3699 windows = new Object[mWindows.size()];
3700 //noinspection unchecked
3701 windows = mWindows.toArray(windows);
3702 }
3703
3704 BufferedWriter out = null;
3705
3706 // Any uncaught exception will crash the system process
3707 try {
3708 OutputStream clientStream = client.getOutputStream();
3709 out = new BufferedWriter(new OutputStreamWriter(clientStream), 8 * 1024);
3710
3711 final int count = windows.length;
3712 for (int i = 0; i < count; i++) {
3713 final WindowState w = (WindowState) windows[i];
3714 out.write(Integer.toHexString(System.identityHashCode(w)));
3715 out.write(' ');
3716 out.append(w.mAttrs.getTitle());
3717 out.write('\n');
3718 }
3719
3720 out.write("DONE.\n");
3721 out.flush();
3722 } catch (Exception e) {
3723 result = false;
3724 } finally {
3725 if (out != null) {
3726 try {
3727 out.close();
3728 } catch (IOException e) {
3729 result = false;
3730 }
3731 }
3732 }
3733
3734 return result;
3735 }
3736
3737 /**
3738 * Sends a command to a target window. The result of the command, if any, will be
3739 * written in the output stream of the specified socket.
3740 *
3741 * The parameters must follow this syntax:
3742 * windowHashcode extra
3743 *
3744 * Where XX is the length in characeters of the windowTitle.
3745 *
3746 * The first parameter is the target window. The window with the specified hashcode
3747 * will be the target. If no target can be found, nothing happens. The extra parameters
3748 * will be delivered to the target window and as parameters to the command itself.
3749 *
3750 * @param client The remote client to sent the result, if any, to.
3751 * @param command The command to execute.
3752 * @param parameters The command parameters.
3753 *
3754 * @return True if the command was successfully delivered, false otherwise. This does
3755 * not indicate whether the command itself was successful.
3756 */
3757 boolean viewServerWindowCommand(Socket client, String command, String parameters) {
Romain Guy06882f82009-06-10 13:36:04 -07003758 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003759 return false;
3760 }
3761
3762 boolean success = true;
3763 Parcel data = null;
3764 Parcel reply = null;
3765
3766 // Any uncaught exception will crash the system process
3767 try {
3768 // Find the hashcode of the window
3769 int index = parameters.indexOf(' ');
3770 if (index == -1) {
3771 index = parameters.length();
3772 }
3773 final String code = parameters.substring(0, index);
3774 int hashCode = "ffffffff".equals(code) ? -1 : Integer.parseInt(code, 16);
3775
3776 // Extract the command's parameter after the window description
3777 if (index < parameters.length()) {
3778 parameters = parameters.substring(index + 1);
3779 } else {
3780 parameters = "";
3781 }
3782
3783 final WindowManagerService.WindowState window = findWindow(hashCode);
3784 if (window == null) {
3785 return false;
3786 }
3787
3788 data = Parcel.obtain();
3789 data.writeInterfaceToken("android.view.IWindow");
3790 data.writeString(command);
3791 data.writeString(parameters);
3792 data.writeInt(1);
3793 ParcelFileDescriptor.fromSocket(client).writeToParcel(data, 0);
3794
3795 reply = Parcel.obtain();
3796
3797 final IBinder binder = window.mClient.asBinder();
3798 // TODO: GET THE TRANSACTION CODE IN A SAFER MANNER
3799 binder.transact(IBinder.FIRST_CALL_TRANSACTION, data, reply, 0);
3800
3801 reply.readException();
3802
3803 } catch (Exception e) {
3804 Log.w(TAG, "Could not send command " + command + " with parameters " + parameters, e);
3805 success = false;
3806 } finally {
3807 if (data != null) {
3808 data.recycle();
3809 }
3810 if (reply != null) {
3811 reply.recycle();
3812 }
3813 }
3814
3815 return success;
3816 }
3817
3818 private WindowState findWindow(int hashCode) {
3819 if (hashCode == -1) {
3820 return getFocusedWindow();
3821 }
3822
3823 synchronized (mWindowMap) {
3824 final ArrayList windows = mWindows;
3825 final int count = windows.size();
3826
3827 for (int i = 0; i < count; i++) {
3828 WindowState w = (WindowState) windows.get(i);
3829 if (System.identityHashCode(w) == hashCode) {
3830 return w;
3831 }
3832 }
3833 }
3834
3835 return null;
3836 }
3837
3838 /*
3839 * Instruct the Activity Manager to fetch the current configuration and broadcast
3840 * that to config-changed listeners if appropriate.
3841 */
3842 void sendNewConfiguration() {
3843 try {
3844 mActivityManager.updateConfiguration(null);
3845 } catch (RemoteException e) {
3846 }
3847 }
Romain Guy06882f82009-06-10 13:36:04 -07003848
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003849 public Configuration computeNewConfiguration() {
3850 synchronized (mWindowMap) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07003851 return computeNewConfigurationLocked();
3852 }
3853 }
Romain Guy06882f82009-06-10 13:36:04 -07003854
Dianne Hackbornc485a602009-03-24 22:39:49 -07003855 Configuration computeNewConfigurationLocked() {
3856 Configuration config = new Configuration();
3857 if (!computeNewConfigurationLocked(config)) {
3858 return null;
3859 }
3860 Log.i(TAG, "Config changed: " + config);
3861 long now = SystemClock.uptimeMillis();
3862 //Log.i(TAG, "Config changing, gc pending: " + mFreezeGcPending + ", now " + now);
3863 if (mFreezeGcPending != 0) {
3864 if (now > (mFreezeGcPending+1000)) {
3865 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
3866 mH.removeMessages(H.FORCE_GC);
3867 Runtime.getRuntime().gc();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003868 mFreezeGcPending = now;
3869 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07003870 } else {
3871 mFreezeGcPending = now;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003872 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07003873 return config;
3874 }
Romain Guy06882f82009-06-10 13:36:04 -07003875
Dianne Hackbornc485a602009-03-24 22:39:49 -07003876 boolean computeNewConfigurationLocked(Configuration config) {
3877 if (mDisplay == null) {
3878 return false;
3879 }
3880 mQueue.getInputConfiguration(config);
3881 final int dw = mDisplay.getWidth();
3882 final int dh = mDisplay.getHeight();
3883 int orientation = Configuration.ORIENTATION_SQUARE;
3884 if (dw < dh) {
3885 orientation = Configuration.ORIENTATION_PORTRAIT;
3886 } else if (dw > dh) {
3887 orientation = Configuration.ORIENTATION_LANDSCAPE;
3888 }
3889 config.orientation = orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -07003890
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07003891 DisplayMetrics dm = new DisplayMetrics();
3892 mDisplay.getMetrics(dm);
3893 CompatibilityInfo.updateCompatibleScreenFrame(dm, orientation, mCompatibleScreenFrame);
3894
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07003895 if (mScreenLayout == Configuration.SCREENLAYOUT_SIZE_UNDEFINED) {
Dianne Hackborn723738c2009-06-25 19:48:04 -07003896 // Note we only do this once because at this point we don't
3897 // expect the screen to change in this way at runtime, and want
3898 // to avoid all of this computation for every config change.
Dianne Hackborn723738c2009-06-25 19:48:04 -07003899 int longSize = dw;
3900 int shortSize = dh;
3901 if (longSize < shortSize) {
3902 int tmp = longSize;
3903 longSize = shortSize;
3904 shortSize = tmp;
3905 }
3906 longSize = (int)(longSize/dm.density);
3907 shortSize = (int)(shortSize/dm.density);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07003908
Dianne Hackborn723738c2009-06-25 19:48:04 -07003909 // These semi-magic numbers define our compatibility modes for
3910 // applications with different screens. Don't change unless you
3911 // make sure to test lots and lots of apps!
3912 if (longSize < 470) {
3913 // This is shorter than an HVGA normal density screen (which
3914 // is 480 pixels on its long side).
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07003915 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_SMALL
3916 | Configuration.SCREENLAYOUT_LONG_NO;
Dianne Hackborn723738c2009-06-25 19:48:04 -07003917 } else {
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07003918 // Is this a large screen?
3919 if (longSize > 640 && shortSize >= 480) {
3920 // VGA or larger screens at medium density are the point
3921 // at which we consider it to be a large screen.
3922 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_LARGE;
3923 } else {
3924 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_NORMAL;
3925
3926 // If this screen is wider than normal HVGA, or taller
3927 // than FWVGA, then for old apps we want to run in size
3928 // compatibility mode.
3929 if (shortSize > 321 || longSize > 570) {
3930 mScreenLayout |= Configuration.SCREENLAYOUT_COMPAT_NEEDED;
3931 }
3932 }
3933
3934 // Is this a long screen?
3935 if (((longSize*3)/5) >= (shortSize-1)) {
3936 // Anything wider than WVGA (5:3) is considering to be long.
3937 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_YES;
3938 } else {
3939 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_NO;
3940 }
Dianne Hackborn723738c2009-06-25 19:48:04 -07003941 }
3942 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07003943 config.screenLayout = mScreenLayout;
Dianne Hackborn723738c2009-06-25 19:48:04 -07003944
Dianne Hackbornc485a602009-03-24 22:39:49 -07003945 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
3946 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
3947 mPolicy.adjustConfigurationLw(config);
3948 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003949 }
Romain Guy06882f82009-06-10 13:36:04 -07003950
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003951 // -------------------------------------------------------------
3952 // Input Events and Focus Management
3953 // -------------------------------------------------------------
3954
3955 private final void wakeupIfNeeded(WindowState targetWin, int eventType) {
Michael Chane96440f2009-05-06 10:27:36 -07003956 long curTime = SystemClock.uptimeMillis();
3957
Michael Chane10de972009-05-18 11:24:50 -07003958 if (eventType == TOUCH_EVENT || eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT) {
Michael Chane96440f2009-05-06 10:27:36 -07003959 if (mLastTouchEventType == eventType &&
3960 (curTime - mLastUserActivityCallTime) < MIN_TIME_BETWEEN_USERACTIVITIES) {
3961 return;
3962 }
3963 mLastUserActivityCallTime = curTime;
3964 mLastTouchEventType = eventType;
3965 }
3966
3967 if (targetWin == null
3968 || targetWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
3969 mPowerManager.userActivity(curTime, false, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003970 }
3971 }
3972
3973 // tells if it's a cheek event or not -- this function is stateful
3974 private static final int EVENT_NONE = 0;
3975 private static final int EVENT_UNKNOWN = 0;
3976 private static final int EVENT_CHEEK = 0;
3977 private static final int EVENT_IGNORE_DURATION = 300; // ms
3978 private static final float CHEEK_THRESHOLD = 0.6f;
3979 private int mEventState = EVENT_NONE;
3980 private float mEventSize;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07003981
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003982 private int eventType(MotionEvent ev) {
3983 float size = ev.getSize();
3984 switch (ev.getAction()) {
3985 case MotionEvent.ACTION_DOWN:
3986 mEventSize = size;
3987 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_EVENT;
3988 case MotionEvent.ACTION_UP:
3989 if (size > mEventSize) mEventSize = size;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07003990 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003991 case MotionEvent.ACTION_MOVE:
3992 final int N = ev.getHistorySize();
3993 if (size > mEventSize) mEventSize = size;
3994 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
3995 for (int i=0; i<N; i++) {
3996 size = ev.getHistoricalSize(i);
3997 if (size > mEventSize) mEventSize = size;
3998 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
3999 }
4000 if (ev.getEventTime() < ev.getDownTime() + EVENT_IGNORE_DURATION) {
4001 return TOUCH_EVENT;
4002 } else {
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004003 return LONG_TOUCH_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004004 }
4005 default:
4006 // not good
4007 return OTHER_EVENT;
4008 }
4009 }
4010
4011 /**
4012 * @return Returns true if event was dispatched, false if it was dropped for any reason
4013 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004014 private int dispatchPointer(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004015 if (DEBUG_INPUT || WindowManagerPolicy.WATCH_POINTER) Log.v(TAG,
4016 "dispatchPointer " + ev);
4017
Michael Chan53071d62009-05-13 17:29:48 -07004018 if (MEASURE_LATENCY) {
4019 lt.sample("3 Wait for last dispatch ", System.nanoTime() - qev.whenNano);
4020 }
4021
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004022 Object targetObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004023 ev, true, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004024
Michael Chan53071d62009-05-13 17:29:48 -07004025 if (MEASURE_LATENCY) {
4026 lt.sample("3 Last dispatch finished ", System.nanoTime() - qev.whenNano);
4027 }
4028
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004029 int action = ev.getAction();
Romain Guy06882f82009-06-10 13:36:04 -07004030
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004031 if (action == MotionEvent.ACTION_UP) {
4032 // let go of our target
4033 mKeyWaiter.mMotionTarget = null;
4034 mPowerManager.logPointerUpEvent();
4035 } else if (action == MotionEvent.ACTION_DOWN) {
4036 mPowerManager.logPointerDownEvent();
4037 }
4038
4039 if (targetObj == null) {
4040 // In this case we are either dropping the event, or have received
4041 // a move or up without a down. It is common to receive move
4042 // events in such a way, since this means the user is moving the
4043 // pointer without actually pressing down. All other cases should
4044 // be atypical, so let's log them.
Michael Chane96440f2009-05-06 10:27:36 -07004045 if (action != MotionEvent.ACTION_MOVE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004046 Log.w(TAG, "No window to dispatch pointer action " + ev.getAction());
4047 }
4048 if (qev != null) {
4049 mQueue.recycleEvent(qev);
4050 }
4051 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004052 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004053 }
4054 if (targetObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
4055 if (qev != null) {
4056 mQueue.recycleEvent(qev);
4057 }
4058 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004059 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004060 }
Romain Guy06882f82009-06-10 13:36:04 -07004061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004062 WindowState target = (WindowState)targetObj;
Romain Guy06882f82009-06-10 13:36:04 -07004063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004064 final long eventTime = ev.getEventTime();
Michael Chan53071d62009-05-13 17:29:48 -07004065 final long eventTimeNano = ev.getEventTimeNano();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004066
4067 //Log.i(TAG, "Sending " + ev + " to " + target);
4068
4069 if (uid != 0 && uid != target.mSession.mUid) {
4070 if (mContext.checkPermission(
4071 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4072 != PackageManager.PERMISSION_GRANTED) {
4073 Log.w(TAG, "Permission denied: injecting pointer event from pid "
4074 + pid + " uid " + uid + " to window " + target
4075 + " owned by uid " + target.mSession.mUid);
4076 if (qev != null) {
4077 mQueue.recycleEvent(qev);
4078 }
4079 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004080 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004081 }
4082 }
4083
Michael Chan53071d62009-05-13 17:29:48 -07004084 if (MEASURE_LATENCY) {
4085 lt.sample("4 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4086 }
4087
Romain Guy06882f82009-06-10 13:36:04 -07004088 if ((target.mAttrs.flags &
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004089 WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES) != 0) {
4090 //target wants to ignore fat touch events
4091 boolean cheekPress = mPolicy.isCheekPressedAgainstScreen(ev);
4092 //explicit flag to return without processing event further
4093 boolean returnFlag = false;
4094 if((action == MotionEvent.ACTION_DOWN)) {
4095 mFatTouch = false;
4096 if(cheekPress) {
4097 mFatTouch = true;
4098 returnFlag = true;
4099 }
4100 } else {
4101 if(action == MotionEvent.ACTION_UP) {
4102 if(mFatTouch) {
4103 //earlier even was invalid doesnt matter if current up is cheekpress or not
4104 mFatTouch = false;
4105 returnFlag = true;
4106 } else if(cheekPress) {
4107 //cancel the earlier event
4108 ev.setAction(MotionEvent.ACTION_CANCEL);
4109 action = MotionEvent.ACTION_CANCEL;
4110 }
4111 } else if(action == MotionEvent.ACTION_MOVE) {
4112 if(mFatTouch) {
4113 //two cases here
4114 //an invalid down followed by 0 or moves(valid or invalid)
Romain Guy06882f82009-06-10 13:36:04 -07004115 //a valid down, invalid move, more moves. want to ignore till up
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004116 returnFlag = true;
4117 } else if(cheekPress) {
4118 //valid down followed by invalid moves
4119 //an invalid move have to cancel earlier action
4120 ev.setAction(MotionEvent.ACTION_CANCEL);
4121 action = MotionEvent.ACTION_CANCEL;
4122 if (DEBUG_INPUT) Log.v(TAG, "Sending cancel for invalid ACTION_MOVE");
4123 //note that the subsequent invalid moves will not get here
4124 mFatTouch = true;
4125 }
4126 }
4127 } //else if action
4128 if(returnFlag) {
4129 //recycle que, ev
4130 if (qev != null) {
4131 mQueue.recycleEvent(qev);
4132 }
4133 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004134 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004135 }
4136 } //end if target
Michael Chane96440f2009-05-06 10:27:36 -07004137
Michael Chan9f028e62009-08-04 17:37:46 -07004138 // Enable this for testing the "right" value
4139 if (false && action == MotionEvent.ACTION_DOWN) {
Michael Chane96440f2009-05-06 10:27:36 -07004140 int max_events_per_sec = 35;
4141 try {
4142 max_events_per_sec = Integer.parseInt(SystemProperties
4143 .get("windowsmgr.max_events_per_sec"));
4144 if (max_events_per_sec < 1) {
4145 max_events_per_sec = 35;
4146 }
4147 } catch (NumberFormatException e) {
4148 }
4149 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
4150 }
4151
4152 /*
4153 * Throttle events to minimize CPU usage when there's a flood of events
4154 * e.g. constant contact with the screen
4155 */
4156 if (action == MotionEvent.ACTION_MOVE) {
4157 long nextEventTime = mLastTouchEventTime + mMinWaitTimeBetweenTouchEvents;
4158 long now = SystemClock.uptimeMillis();
4159 if (now < nextEventTime) {
4160 try {
4161 Thread.sleep(nextEventTime - now);
4162 } catch (InterruptedException e) {
4163 }
4164 mLastTouchEventTime = nextEventTime;
4165 } else {
4166 mLastTouchEventTime = now;
4167 }
4168 }
4169
Michael Chan53071d62009-05-13 17:29:48 -07004170 if (MEASURE_LATENCY) {
4171 lt.sample("5 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4172 }
4173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004174 synchronized(mWindowMap) {
4175 if (qev != null && action == MotionEvent.ACTION_MOVE) {
4176 mKeyWaiter.bindTargetWindowLocked(target,
4177 KeyWaiter.RETURN_PENDING_POINTER, qev);
4178 ev = null;
4179 } else {
4180 if (action == MotionEvent.ACTION_DOWN) {
4181 WindowState out = mKeyWaiter.mOutsideTouchTargets;
4182 if (out != null) {
4183 MotionEvent oev = MotionEvent.obtain(ev);
4184 oev.setAction(MotionEvent.ACTION_OUTSIDE);
4185 do {
4186 final Rect frame = out.mFrame;
4187 oev.offsetLocation(-(float)frame.left, -(float)frame.top);
4188 try {
4189 out.mClient.dispatchPointer(oev, eventTime);
4190 } catch (android.os.RemoteException e) {
4191 Log.i(TAG, "WINDOW DIED during outside motion dispatch: " + out);
4192 }
4193 oev.offsetLocation((float)frame.left, (float)frame.top);
4194 out = out.mNextOutsideTouch;
4195 } while (out != null);
4196 mKeyWaiter.mOutsideTouchTargets = null;
4197 }
4198 }
4199 final Rect frame = target.mFrame;
4200 ev.offsetLocation(-(float)frame.left, -(float)frame.top);
4201 mKeyWaiter.bindTargetWindowLocked(target);
4202 }
4203 }
Romain Guy06882f82009-06-10 13:36:04 -07004204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004205 // finally offset the event to the target's coordinate system and
4206 // dispatch the event.
4207 try {
4208 if (DEBUG_INPUT || DEBUG_FOCUS || WindowManagerPolicy.WATCH_POINTER) {
4209 Log.v(TAG, "Delivering pointer " + qev + " to " + target);
4210 }
Michael Chan53071d62009-05-13 17:29:48 -07004211
4212 if (MEASURE_LATENCY) {
4213 lt.sample("6 before svr->client ipc ", System.nanoTime() - eventTimeNano);
4214 }
4215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004216 target.mClient.dispatchPointer(ev, eventTime);
Michael Chan53071d62009-05-13 17:29:48 -07004217
4218 if (MEASURE_LATENCY) {
4219 lt.sample("7 after svr->client ipc ", System.nanoTime() - eventTimeNano);
4220 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07004221 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004222 } catch (android.os.RemoteException e) {
4223 Log.i(TAG, "WINDOW DIED during motion dispatch: " + target);
4224 mKeyWaiter.mMotionTarget = null;
4225 try {
4226 removeWindow(target.mSession, target.mClient);
4227 } catch (java.util.NoSuchElementException ex) {
4228 // This will happen if the window has already been
4229 // removed.
4230 }
4231 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07004232 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004233 }
Romain Guy06882f82009-06-10 13:36:04 -07004234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004235 /**
4236 * @return Returns true if event was dispatched, false if it was dropped for any reason
4237 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004238 private int dispatchTrackball(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004239 if (DEBUG_INPUT) Log.v(
4240 TAG, "dispatchTrackball [" + ev.getAction() +"] <" + ev.getX() + ", " + ev.getY() + ">");
Romain Guy06882f82009-06-10 13:36:04 -07004241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004242 Object focusObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004243 ev, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004244 if (focusObj == null) {
4245 Log.w(TAG, "No focus window, dropping trackball: " + ev);
4246 if (qev != null) {
4247 mQueue.recycleEvent(qev);
4248 }
4249 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004250 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004251 }
4252 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
4253 if (qev != null) {
4254 mQueue.recycleEvent(qev);
4255 }
4256 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004257 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004258 }
Romain Guy06882f82009-06-10 13:36:04 -07004259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004260 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07004261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004262 if (uid != 0 && uid != focus.mSession.mUid) {
4263 if (mContext.checkPermission(
4264 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4265 != PackageManager.PERMISSION_GRANTED) {
4266 Log.w(TAG, "Permission denied: injecting key event from pid "
4267 + pid + " uid " + uid + " to window " + focus
4268 + " owned by uid " + focus.mSession.mUid);
4269 if (qev != null) {
4270 mQueue.recycleEvent(qev);
4271 }
4272 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004273 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004274 }
4275 }
Romain Guy06882f82009-06-10 13:36:04 -07004276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004277 final long eventTime = ev.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07004278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004279 synchronized(mWindowMap) {
4280 if (qev != null && ev.getAction() == MotionEvent.ACTION_MOVE) {
4281 mKeyWaiter.bindTargetWindowLocked(focus,
4282 KeyWaiter.RETURN_PENDING_TRACKBALL, qev);
4283 // We don't deliver movement events to the client, we hold
4284 // them and wait for them to call back.
4285 ev = null;
4286 } else {
4287 mKeyWaiter.bindTargetWindowLocked(focus);
4288 }
4289 }
Romain Guy06882f82009-06-10 13:36:04 -07004290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004291 try {
4292 focus.mClient.dispatchTrackball(ev, eventTime);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004293 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004294 } catch (android.os.RemoteException e) {
4295 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
4296 try {
4297 removeWindow(focus.mSession, focus.mClient);
4298 } catch (java.util.NoSuchElementException ex) {
4299 // This will happen if the window has already been
4300 // removed.
4301 }
4302 }
Romain Guy06882f82009-06-10 13:36:04 -07004303
Dianne Hackborncfaef692009-06-15 14:24:44 -07004304 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004305 }
Romain Guy06882f82009-06-10 13:36:04 -07004306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004307 /**
4308 * @return Returns true if event was dispatched, false if it was dropped for any reason
4309 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004310 private int dispatchKey(KeyEvent event, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004311 if (DEBUG_INPUT) Log.v(TAG, "Dispatch key: " + event);
4312
4313 Object focusObj = mKeyWaiter.waitForNextEventTarget(event, null,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004314 null, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004315 if (focusObj == null) {
4316 Log.w(TAG, "No focus window, dropping: " + event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004317 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004318 }
4319 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004320 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004321 }
Romain Guy06882f82009-06-10 13:36:04 -07004322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004323 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07004324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004325 if (DEBUG_INPUT) Log.v(
4326 TAG, "Dispatching to " + focus + ": " + event);
4327
4328 if (uid != 0 && uid != focus.mSession.mUid) {
4329 if (mContext.checkPermission(
4330 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4331 != PackageManager.PERMISSION_GRANTED) {
4332 Log.w(TAG, "Permission denied: injecting key event from pid "
4333 + pid + " uid " + uid + " to window " + focus
4334 + " owned by uid " + focus.mSession.mUid);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004335 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004336 }
4337 }
Romain Guy06882f82009-06-10 13:36:04 -07004338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004339 synchronized(mWindowMap) {
4340 mKeyWaiter.bindTargetWindowLocked(focus);
4341 }
4342
4343 // NOSHIP extra state logging
4344 mKeyWaiter.recordDispatchState(event, focus);
4345 // END NOSHIP
Romain Guy06882f82009-06-10 13:36:04 -07004346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004347 try {
4348 if (DEBUG_INPUT || DEBUG_FOCUS) {
4349 Log.v(TAG, "Delivering key " + event.getKeyCode()
4350 + " to " + focus);
4351 }
4352 focus.mClient.dispatchKey(event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004353 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004354 } catch (android.os.RemoteException e) {
4355 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
4356 try {
4357 removeWindow(focus.mSession, focus.mClient);
4358 } catch (java.util.NoSuchElementException ex) {
4359 // This will happen if the window has already been
4360 // removed.
4361 }
4362 }
Romain Guy06882f82009-06-10 13:36:04 -07004363
Dianne Hackborncfaef692009-06-15 14:24:44 -07004364 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004365 }
Romain Guy06882f82009-06-10 13:36:04 -07004366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004367 public void pauseKeyDispatching(IBinder _token) {
4368 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4369 "pauseKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004370 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004371 }
4372
4373 synchronized (mWindowMap) {
4374 WindowToken token = mTokenMap.get(_token);
4375 if (token != null) {
4376 mKeyWaiter.pauseDispatchingLocked(token);
4377 }
4378 }
4379 }
4380
4381 public void resumeKeyDispatching(IBinder _token) {
4382 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4383 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004384 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004385 }
4386
4387 synchronized (mWindowMap) {
4388 WindowToken token = mTokenMap.get(_token);
4389 if (token != null) {
4390 mKeyWaiter.resumeDispatchingLocked(token);
4391 }
4392 }
4393 }
4394
4395 public void setEventDispatching(boolean enabled) {
4396 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4397 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004398 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004399 }
4400
4401 synchronized (mWindowMap) {
4402 mKeyWaiter.setEventDispatchingLocked(enabled);
4403 }
4404 }
Romain Guy06882f82009-06-10 13:36:04 -07004405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004406 /**
4407 * Injects a keystroke event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004408 *
4409 * @param ev A motion event describing the keystroke action. (Be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004410 * {@link SystemClock#uptimeMillis()} as the timebase.)
4411 * @param sync If true, wait for the event to be completed before returning to the caller.
4412 * @return Returns true if event was dispatched, false if it was dropped for any reason
4413 */
4414 public boolean injectKeyEvent(KeyEvent ev, boolean sync) {
4415 long downTime = ev.getDownTime();
4416 long eventTime = ev.getEventTime();
4417
4418 int action = ev.getAction();
4419 int code = ev.getKeyCode();
4420 int repeatCount = ev.getRepeatCount();
4421 int metaState = ev.getMetaState();
4422 int deviceId = ev.getDeviceId();
4423 int scancode = ev.getScanCode();
4424
4425 if (eventTime == 0) eventTime = SystemClock.uptimeMillis();
4426 if (downTime == 0) downTime = eventTime;
4427
4428 KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
The Android Open Source Project10592532009-03-18 17:39:46 -07004429 deviceId, scancode, KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004430
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004431 final int pid = Binder.getCallingPid();
4432 final int uid = Binder.getCallingUid();
4433 final long ident = Binder.clearCallingIdentity();
4434 final int result = dispatchKey(newEvent, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004435 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004436 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004437 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004438 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004439 switch (result) {
4440 case INJECT_NO_PERMISSION:
4441 throw new SecurityException(
4442 "Injecting to another application requires INJECT_EVENT permission");
4443 case INJECT_SUCCEEDED:
4444 return true;
4445 }
4446 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004447 }
4448
4449 /**
4450 * Inject a pointer (touch) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004451 *
4452 * @param ev A motion event describing the pointer (touch) action. (As noted in
4453 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004454 * {@link SystemClock#uptimeMillis()} as the timebase.)
4455 * @param sync If true, wait for the event to be completed before returning to the caller.
4456 * @return Returns true if event was dispatched, false if it was dropped for any reason
4457 */
4458 public boolean injectPointerEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004459 final int pid = Binder.getCallingPid();
4460 final int uid = Binder.getCallingUid();
4461 final long ident = Binder.clearCallingIdentity();
4462 final int result = dispatchPointer(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004463 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004464 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004465 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004466 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004467 switch (result) {
4468 case INJECT_NO_PERMISSION:
4469 throw new SecurityException(
4470 "Injecting to another application requires INJECT_EVENT permission");
4471 case INJECT_SUCCEEDED:
4472 return true;
4473 }
4474 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004475 }
Romain Guy06882f82009-06-10 13:36:04 -07004476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004477 /**
4478 * Inject a trackball (navigation device) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004479 *
4480 * @param ev A motion event describing the trackball action. (As noted in
4481 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004482 * {@link SystemClock#uptimeMillis()} as the timebase.)
4483 * @param sync If true, wait for the event to be completed before returning to the caller.
4484 * @return Returns true if event was dispatched, false if it was dropped for any reason
4485 */
4486 public boolean injectTrackballEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004487 final int pid = Binder.getCallingPid();
4488 final int uid = Binder.getCallingUid();
4489 final long ident = Binder.clearCallingIdentity();
4490 final int result = dispatchTrackball(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004491 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004492 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004493 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004494 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004495 switch (result) {
4496 case INJECT_NO_PERMISSION:
4497 throw new SecurityException(
4498 "Injecting to another application requires INJECT_EVENT permission");
4499 case INJECT_SUCCEEDED:
4500 return true;
4501 }
4502 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004503 }
Romain Guy06882f82009-06-10 13:36:04 -07004504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004505 private WindowState getFocusedWindow() {
4506 synchronized (mWindowMap) {
4507 return getFocusedWindowLocked();
4508 }
4509 }
4510
4511 private WindowState getFocusedWindowLocked() {
4512 return mCurrentFocus;
4513 }
Romain Guy06882f82009-06-10 13:36:04 -07004514
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004515 /**
4516 * This class holds the state for dispatching key events. This state
4517 * is protected by the KeyWaiter instance, NOT by the window lock. You
4518 * can be holding the main window lock while acquire the KeyWaiter lock,
4519 * but not the other way around.
4520 */
4521 final class KeyWaiter {
4522 // NOSHIP debugging
4523 public class DispatchState {
4524 private KeyEvent event;
4525 private WindowState focus;
4526 private long time;
4527 private WindowState lastWin;
4528 private IBinder lastBinder;
4529 private boolean finished;
4530 private boolean gotFirstWindow;
4531 private boolean eventDispatching;
4532 private long timeToSwitch;
4533 private boolean wasFrozen;
4534 private boolean focusPaused;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004535 private WindowState curFocus;
Romain Guy06882f82009-06-10 13:36:04 -07004536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004537 DispatchState(KeyEvent theEvent, WindowState theFocus) {
4538 focus = theFocus;
4539 event = theEvent;
4540 time = System.currentTimeMillis();
4541 // snapshot KeyWaiter state
4542 lastWin = mLastWin;
4543 lastBinder = mLastBinder;
4544 finished = mFinished;
4545 gotFirstWindow = mGotFirstWindow;
4546 eventDispatching = mEventDispatching;
4547 timeToSwitch = mTimeToSwitch;
4548 wasFrozen = mWasFrozen;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004549 curFocus = mCurrentFocus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004550 // cache the paused state at ctor time as well
4551 if (theFocus == null || theFocus.mToken == null) {
4552 Log.i(TAG, "focus " + theFocus + " mToken is null at event dispatch!");
4553 focusPaused = false;
4554 } else {
4555 focusPaused = theFocus.mToken.paused;
4556 }
4557 }
Romain Guy06882f82009-06-10 13:36:04 -07004558
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004559 public String toString() {
4560 return "{{" + event + " to " + focus + " @ " + time
4561 + " lw=" + lastWin + " lb=" + lastBinder
4562 + " fin=" + finished + " gfw=" + gotFirstWindow
4563 + " ed=" + eventDispatching + " tts=" + timeToSwitch
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004564 + " wf=" + wasFrozen + " fp=" + focusPaused
4565 + " mcf=" + mCurrentFocus + "}}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004566 }
4567 };
4568 private DispatchState mDispatchState = null;
4569 public void recordDispatchState(KeyEvent theEvent, WindowState theFocus) {
4570 mDispatchState = new DispatchState(theEvent, theFocus);
4571 }
4572 // END NOSHIP
4573
4574 public static final int RETURN_NOTHING = 0;
4575 public static final int RETURN_PENDING_POINTER = 1;
4576 public static final int RETURN_PENDING_TRACKBALL = 2;
Romain Guy06882f82009-06-10 13:36:04 -07004577
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004578 final Object SKIP_TARGET_TOKEN = new Object();
4579 final Object CONSUMED_EVENT_TOKEN = new Object();
Romain Guy06882f82009-06-10 13:36:04 -07004580
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004581 private WindowState mLastWin = null;
4582 private IBinder mLastBinder = null;
4583 private boolean mFinished = true;
4584 private boolean mGotFirstWindow = false;
4585 private boolean mEventDispatching = true;
4586 private long mTimeToSwitch = 0;
4587 /* package */ boolean mWasFrozen = false;
Romain Guy06882f82009-06-10 13:36:04 -07004588
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004589 // Target of Motion events
4590 WindowState mMotionTarget;
Romain Guy06882f82009-06-10 13:36:04 -07004591
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004592 // Windows above the target who would like to receive an "outside"
4593 // touch event for any down events outside of them.
4594 WindowState mOutsideTouchTargets;
4595
4596 /**
4597 * Wait for the last event dispatch to complete, then find the next
4598 * target that should receive the given event and wait for that one
4599 * to be ready to receive it.
4600 */
4601 Object waitForNextEventTarget(KeyEvent nextKey, QueuedEvent qev,
4602 MotionEvent nextMotion, boolean isPointerEvent,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004603 boolean failIfTimeout, int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004604 long startTime = SystemClock.uptimeMillis();
4605 long keyDispatchingTimeout = 5 * 1000;
4606 long waitedFor = 0;
4607
4608 while (true) {
4609 // Figure out which window we care about. It is either the
4610 // last window we are waiting to have process the event or,
4611 // if none, then the next window we think the event should go
4612 // to. Note: we retrieve mLastWin outside of the lock, so
4613 // it may change before we lock. Thus we must check it again.
4614 WindowState targetWin = mLastWin;
4615 boolean targetIsNew = targetWin == null;
4616 if (DEBUG_INPUT) Log.v(
4617 TAG, "waitForLastKey: mFinished=" + mFinished +
4618 ", mLastWin=" + mLastWin);
4619 if (targetIsNew) {
4620 Object target = findTargetWindow(nextKey, qev, nextMotion,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004621 isPointerEvent, callingPid, callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004622 if (target == SKIP_TARGET_TOKEN) {
4623 // The user has pressed a special key, and we are
4624 // dropping all pending events before it.
4625 if (DEBUG_INPUT) Log.v(TAG, "Skipping: " + nextKey
4626 + " " + nextMotion);
4627 return null;
4628 }
4629 if (target == CONSUMED_EVENT_TOKEN) {
4630 if (DEBUG_INPUT) Log.v(TAG, "Consumed: " + nextKey
4631 + " " + nextMotion);
4632 return target;
4633 }
4634 targetWin = (WindowState)target;
4635 }
Romain Guy06882f82009-06-10 13:36:04 -07004636
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004637 AppWindowToken targetApp = null;
Romain Guy06882f82009-06-10 13:36:04 -07004638
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004639 // Now: is it okay to send the next event to this window?
4640 synchronized (this) {
4641 // First: did we come here based on the last window not
4642 // being null, but it changed by the time we got here?
4643 // If so, try again.
4644 if (!targetIsNew && mLastWin == null) {
4645 continue;
4646 }
Romain Guy06882f82009-06-10 13:36:04 -07004647
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004648 // We never dispatch events if not finished with the
4649 // last one, or the display is frozen.
4650 if (mFinished && !mDisplayFrozen) {
4651 // If event dispatching is disabled, then we
4652 // just consume the events.
4653 if (!mEventDispatching) {
4654 if (DEBUG_INPUT) Log.v(TAG,
4655 "Skipping event; dispatching disabled: "
4656 + nextKey + " " + nextMotion);
4657 return null;
4658 }
4659 if (targetWin != null) {
4660 // If this is a new target, and that target is not
4661 // paused or unresponsive, then all looks good to
4662 // handle the event.
4663 if (targetIsNew && !targetWin.mToken.paused) {
4664 return targetWin;
4665 }
Romain Guy06882f82009-06-10 13:36:04 -07004666
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004667 // If we didn't find a target window, and there is no
4668 // focused app window, then just eat the events.
4669 } else if (mFocusedApp == null) {
4670 if (DEBUG_INPUT) Log.v(TAG,
4671 "Skipping event; no focused app: "
4672 + nextKey + " " + nextMotion);
4673 return null;
4674 }
4675 }
Romain Guy06882f82009-06-10 13:36:04 -07004676
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004677 if (DEBUG_INPUT) Log.v(
4678 TAG, "Waiting for last key in " + mLastBinder
4679 + " target=" + targetWin
4680 + " mFinished=" + mFinished
4681 + " mDisplayFrozen=" + mDisplayFrozen
4682 + " targetIsNew=" + targetIsNew
4683 + " paused="
4684 + (targetWin != null ? targetWin.mToken.paused : false)
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004685 + " mFocusedApp=" + mFocusedApp
4686 + " mCurrentFocus=" + mCurrentFocus);
Romain Guy06882f82009-06-10 13:36:04 -07004687
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004688 targetApp = targetWin != null
4689 ? targetWin.mAppToken : mFocusedApp;
Romain Guy06882f82009-06-10 13:36:04 -07004690
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004691 long curTimeout = keyDispatchingTimeout;
4692 if (mTimeToSwitch != 0) {
4693 long now = SystemClock.uptimeMillis();
4694 if (mTimeToSwitch <= now) {
4695 // If an app switch key has been pressed, and we have
4696 // waited too long for the current app to finish
4697 // processing keys, then wait no more!
4698 doFinishedKeyLocked(true);
4699 continue;
4700 }
4701 long switchTimeout = mTimeToSwitch - now;
4702 if (curTimeout > switchTimeout) {
4703 curTimeout = switchTimeout;
4704 }
4705 }
Romain Guy06882f82009-06-10 13:36:04 -07004706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004707 try {
4708 // after that continue
4709 // processing keys, so we don't get stuck.
4710 if (DEBUG_INPUT) Log.v(
4711 TAG, "Waiting for key dispatch: " + curTimeout);
4712 wait(curTimeout);
4713 if (DEBUG_INPUT) Log.v(TAG, "Finished waiting @"
4714 + SystemClock.uptimeMillis() + " startTime="
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004715 + startTime + " switchTime=" + mTimeToSwitch
4716 + " target=" + targetWin + " mLW=" + mLastWin
4717 + " mLB=" + mLastBinder + " fin=" + mFinished
4718 + " mCurrentFocus=" + mCurrentFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004719 } catch (InterruptedException e) {
4720 }
4721 }
4722
4723 // If we were frozen during configuration change, restart the
4724 // timeout checks from now; otherwise look at whether we timed
4725 // out before awakening.
4726 if (mWasFrozen) {
4727 waitedFor = 0;
4728 mWasFrozen = false;
4729 } else {
4730 waitedFor = SystemClock.uptimeMillis() - startTime;
4731 }
4732
4733 if (waitedFor >= keyDispatchingTimeout && mTimeToSwitch == 0) {
4734 IApplicationToken at = null;
4735 synchronized (this) {
4736 Log.w(TAG, "Key dispatching timed out sending to " +
4737 (targetWin != null ? targetWin.mAttrs.getTitle()
4738 : "<null>"));
4739 // NOSHIP debugging
4740 Log.w(TAG, "Dispatch state: " + mDispatchState);
4741 Log.w(TAG, "Current state: " + new DispatchState(nextKey, targetWin));
4742 // END NOSHIP
4743 //dump();
4744 if (targetWin != null) {
4745 at = targetWin.getAppToken();
4746 } else if (targetApp != null) {
4747 at = targetApp.appToken;
4748 }
4749 }
4750
4751 boolean abort = true;
4752 if (at != null) {
4753 try {
4754 long timeout = at.getKeyDispatchingTimeout();
4755 if (timeout > waitedFor) {
4756 // we did not wait the proper amount of time for this application.
4757 // set the timeout to be the real timeout and wait again.
4758 keyDispatchingTimeout = timeout - waitedFor;
4759 continue;
4760 } else {
4761 abort = at.keyDispatchingTimedOut();
4762 }
4763 } catch (RemoteException ex) {
4764 }
4765 }
4766
4767 synchronized (this) {
4768 if (abort && (mLastWin == targetWin || targetWin == null)) {
4769 mFinished = true;
Romain Guy06882f82009-06-10 13:36:04 -07004770 if (mLastWin != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004771 if (DEBUG_INPUT) Log.v(TAG,
4772 "Window " + mLastWin +
4773 " timed out on key input");
4774 if (mLastWin.mToken.paused) {
4775 Log.w(TAG, "Un-pausing dispatching to this window");
4776 mLastWin.mToken.paused = false;
4777 }
4778 }
4779 if (mMotionTarget == targetWin) {
4780 mMotionTarget = null;
4781 }
4782 mLastWin = null;
4783 mLastBinder = null;
4784 if (failIfTimeout || targetWin == null) {
4785 return null;
4786 }
4787 } else {
4788 Log.w(TAG, "Continuing to wait for key to be dispatched");
4789 startTime = SystemClock.uptimeMillis();
4790 }
4791 }
4792 }
4793 }
4794 }
Romain Guy06882f82009-06-10 13:36:04 -07004795
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004796 Object findTargetWindow(KeyEvent nextKey, QueuedEvent qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004797 MotionEvent nextMotion, boolean isPointerEvent,
4798 int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004799 mOutsideTouchTargets = null;
Romain Guy06882f82009-06-10 13:36:04 -07004800
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004801 if (nextKey != null) {
4802 // Find the target window for a normal key event.
4803 final int keycode = nextKey.getKeyCode();
4804 final int repeatCount = nextKey.getRepeatCount();
4805 final boolean down = nextKey.getAction() != KeyEvent.ACTION_UP;
4806 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(keycode);
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004807
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004808 if (!dispatch) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004809 if (callingUid == 0 ||
4810 mContext.checkPermission(
4811 android.Manifest.permission.INJECT_EVENTS,
4812 callingPid, callingUid)
4813 == PackageManager.PERMISSION_GRANTED) {
4814 mPolicy.interceptKeyTi(null, keycode,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07004815 nextKey.getMetaState(), down, repeatCount,
4816 nextKey.getFlags());
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004817 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004818 Log.w(TAG, "Event timeout during app switch: dropping "
4819 + nextKey);
4820 return SKIP_TARGET_TOKEN;
4821 }
Romain Guy06882f82009-06-10 13:36:04 -07004822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004823 // System.out.println("##### [" + SystemClock.uptimeMillis() + "] WindowManagerService.dispatchKey(" + keycode + ", " + down + ", " + repeatCount + ")");
Romain Guy06882f82009-06-10 13:36:04 -07004824
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004825 WindowState focus = null;
4826 synchronized(mWindowMap) {
4827 focus = getFocusedWindowLocked();
4828 }
Romain Guy06882f82009-06-10 13:36:04 -07004829
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004830 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
Romain Guy06882f82009-06-10 13:36:04 -07004831
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004832 if (callingUid == 0 ||
4833 (focus != null && callingUid == focus.mSession.mUid) ||
4834 mContext.checkPermission(
4835 android.Manifest.permission.INJECT_EVENTS,
4836 callingPid, callingUid)
4837 == PackageManager.PERMISSION_GRANTED) {
4838 if (mPolicy.interceptKeyTi(focus,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07004839 keycode, nextKey.getMetaState(), down, repeatCount,
4840 nextKey.getFlags())) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004841 return CONSUMED_EVENT_TOKEN;
4842 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004843 }
Romain Guy06882f82009-06-10 13:36:04 -07004844
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004845 return focus;
Romain Guy06882f82009-06-10 13:36:04 -07004846
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004847 } else if (!isPointerEvent) {
4848 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(-1);
4849 if (!dispatch) {
4850 Log.w(TAG, "Event timeout during app switch: dropping trackball "
4851 + nextMotion);
4852 return SKIP_TARGET_TOKEN;
4853 }
Romain Guy06882f82009-06-10 13:36:04 -07004854
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004855 WindowState focus = null;
4856 synchronized(mWindowMap) {
4857 focus = getFocusedWindowLocked();
4858 }
Romain Guy06882f82009-06-10 13:36:04 -07004859
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004860 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
4861 return focus;
4862 }
Romain Guy06882f82009-06-10 13:36:04 -07004863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004864 if (nextMotion == null) {
4865 return SKIP_TARGET_TOKEN;
4866 }
Romain Guy06882f82009-06-10 13:36:04 -07004867
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004868 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(
4869 KeyEvent.KEYCODE_UNKNOWN);
4870 if (!dispatch) {
4871 Log.w(TAG, "Event timeout during app switch: dropping pointer "
4872 + nextMotion);
4873 return SKIP_TARGET_TOKEN;
4874 }
Romain Guy06882f82009-06-10 13:36:04 -07004875
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004876 // Find the target window for a pointer event.
4877 int action = nextMotion.getAction();
4878 final float xf = nextMotion.getX();
4879 final float yf = nextMotion.getY();
4880 final long eventTime = nextMotion.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07004881
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004882 final boolean screenWasOff = qev != null
4883 && (qev.flags&WindowManagerPolicy.FLAG_BRIGHT_HERE) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07004884
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004885 WindowState target = null;
Romain Guy06882f82009-06-10 13:36:04 -07004886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004887 synchronized(mWindowMap) {
4888 synchronized (this) {
4889 if (action == MotionEvent.ACTION_DOWN) {
4890 if (mMotionTarget != null) {
4891 // this is weird, we got a pen down, but we thought it was
4892 // already down!
4893 // XXX: We should probably send an ACTION_UP to the current
4894 // target.
4895 Log.w(TAG, "Pointer down received while already down in: "
4896 + mMotionTarget);
4897 mMotionTarget = null;
4898 }
Romain Guy06882f82009-06-10 13:36:04 -07004899
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004900 // ACTION_DOWN is special, because we need to lock next events to
4901 // the window we'll land onto.
4902 final int x = (int)xf;
4903 final int y = (int)yf;
Romain Guy06882f82009-06-10 13:36:04 -07004904
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004905 final ArrayList windows = mWindows;
4906 final int N = windows.size();
4907 WindowState topErrWindow = null;
4908 final Rect tmpRect = mTempRect;
4909 for (int i=N-1; i>=0; i--) {
4910 WindowState child = (WindowState)windows.get(i);
4911 //Log.i(TAG, "Checking dispatch to: " + child);
4912 final int flags = child.mAttrs.flags;
4913 if ((flags & WindowManager.LayoutParams.FLAG_SYSTEM_ERROR) != 0) {
4914 if (topErrWindow == null) {
4915 topErrWindow = child;
4916 }
4917 }
4918 if (!child.isVisibleLw()) {
4919 //Log.i(TAG, "Not visible!");
4920 continue;
4921 }
4922 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
4923 //Log.i(TAG, "Not touchable!");
4924 if ((flags & WindowManager.LayoutParams
4925 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
4926 child.mNextOutsideTouch = mOutsideTouchTargets;
4927 mOutsideTouchTargets = child;
4928 }
4929 continue;
4930 }
4931 tmpRect.set(child.mFrame);
4932 if (child.mTouchableInsets == ViewTreeObserver
4933 .InternalInsetsInfo.TOUCHABLE_INSETS_CONTENT) {
4934 // The touch is inside of the window if it is
4935 // inside the frame, AND the content part of that
4936 // frame that was given by the application.
4937 tmpRect.left += child.mGivenContentInsets.left;
4938 tmpRect.top += child.mGivenContentInsets.top;
4939 tmpRect.right -= child.mGivenContentInsets.right;
4940 tmpRect.bottom -= child.mGivenContentInsets.bottom;
4941 } else if (child.mTouchableInsets == ViewTreeObserver
4942 .InternalInsetsInfo.TOUCHABLE_INSETS_VISIBLE) {
4943 // The touch is inside of the window if it is
4944 // inside the frame, AND the visible part of that
4945 // frame that was given by the application.
4946 tmpRect.left += child.mGivenVisibleInsets.left;
4947 tmpRect.top += child.mGivenVisibleInsets.top;
4948 tmpRect.right -= child.mGivenVisibleInsets.right;
4949 tmpRect.bottom -= child.mGivenVisibleInsets.bottom;
4950 }
4951 final int touchFlags = flags &
4952 (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
4953 |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
4954 if (tmpRect.contains(x, y) || touchFlags == 0) {
4955 //Log.i(TAG, "Using this target!");
4956 if (!screenWasOff || (flags &
4957 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING) != 0) {
4958 mMotionTarget = child;
4959 } else {
4960 //Log.i(TAG, "Waking, skip!");
4961 mMotionTarget = null;
4962 }
4963 break;
4964 }
Romain Guy06882f82009-06-10 13:36:04 -07004965
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004966 if ((flags & WindowManager.LayoutParams
4967 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
4968 child.mNextOutsideTouch = mOutsideTouchTargets;
4969 mOutsideTouchTargets = child;
4970 //Log.i(TAG, "Adding to outside target list: " + child);
4971 }
4972 }
4973
4974 // if there's an error window but it's not accepting
4975 // focus (typically because it is not yet visible) just
4976 // wait for it -- any other focused window may in fact
4977 // be in ANR state.
4978 if (topErrWindow != null && mMotionTarget != topErrWindow) {
4979 mMotionTarget = null;
4980 }
4981 }
Romain Guy06882f82009-06-10 13:36:04 -07004982
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004983 target = mMotionTarget;
4984 }
4985 }
Romain Guy06882f82009-06-10 13:36:04 -07004986
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004987 wakeupIfNeeded(target, eventType(nextMotion));
Romain Guy06882f82009-06-10 13:36:04 -07004988
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004989 // Pointer events are a little different -- if there isn't a
4990 // target found for any event, then just drop it.
4991 return target != null ? target : SKIP_TARGET_TOKEN;
4992 }
Romain Guy06882f82009-06-10 13:36:04 -07004993
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004994 boolean checkShouldDispatchKey(int keycode) {
4995 synchronized (this) {
4996 if (mPolicy.isAppSwitchKeyTqTiLwLi(keycode)) {
4997 mTimeToSwitch = 0;
4998 return true;
4999 }
5000 if (mTimeToSwitch != 0
5001 && mTimeToSwitch < SystemClock.uptimeMillis()) {
5002 return false;
5003 }
5004 return true;
5005 }
5006 }
Romain Guy06882f82009-06-10 13:36:04 -07005007
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005008 void bindTargetWindowLocked(WindowState win,
5009 int pendingWhat, QueuedEvent pendingMotion) {
5010 synchronized (this) {
5011 bindTargetWindowLockedLocked(win, pendingWhat, pendingMotion);
5012 }
5013 }
Romain Guy06882f82009-06-10 13:36:04 -07005014
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005015 void bindTargetWindowLocked(WindowState win) {
5016 synchronized (this) {
5017 bindTargetWindowLockedLocked(win, RETURN_NOTHING, null);
5018 }
5019 }
5020
5021 void bindTargetWindowLockedLocked(WindowState win,
5022 int pendingWhat, QueuedEvent pendingMotion) {
5023 mLastWin = win;
5024 mLastBinder = win.mClient.asBinder();
5025 mFinished = false;
5026 if (pendingMotion != null) {
5027 final Session s = win.mSession;
5028 if (pendingWhat == RETURN_PENDING_POINTER) {
5029 releasePendingPointerLocked(s);
5030 s.mPendingPointerMove = pendingMotion;
5031 s.mPendingPointerWindow = win;
Romain Guy06882f82009-06-10 13:36:04 -07005032 if (DEBUG_INPUT) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005033 "bindTargetToWindow " + s.mPendingPointerMove);
5034 } else if (pendingWhat == RETURN_PENDING_TRACKBALL) {
5035 releasePendingTrackballLocked(s);
5036 s.mPendingTrackballMove = pendingMotion;
5037 s.mPendingTrackballWindow = win;
5038 }
5039 }
5040 }
Romain Guy06882f82009-06-10 13:36:04 -07005041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005042 void releasePendingPointerLocked(Session s) {
5043 if (DEBUG_INPUT) Log.v(TAG,
5044 "releasePendingPointer " + s.mPendingPointerMove);
5045 if (s.mPendingPointerMove != null) {
5046 mQueue.recycleEvent(s.mPendingPointerMove);
5047 s.mPendingPointerMove = null;
5048 }
5049 }
Romain Guy06882f82009-06-10 13:36:04 -07005050
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005051 void releasePendingTrackballLocked(Session s) {
5052 if (s.mPendingTrackballMove != null) {
5053 mQueue.recycleEvent(s.mPendingTrackballMove);
5054 s.mPendingTrackballMove = null;
5055 }
5056 }
Romain Guy06882f82009-06-10 13:36:04 -07005057
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005058 MotionEvent finishedKey(Session session, IWindow client, boolean force,
5059 int returnWhat) {
5060 if (DEBUG_INPUT) Log.v(
5061 TAG, "finishedKey: client=" + client + ", force=" + force);
5062
5063 if (client == null) {
5064 return null;
5065 }
5066
5067 synchronized (this) {
5068 if (DEBUG_INPUT) Log.v(
5069 TAG, "finishedKey: client=" + client.asBinder()
5070 + ", force=" + force + ", last=" + mLastBinder
5071 + " (token=" + (mLastWin != null ? mLastWin.mToken : null) + ")");
5072
5073 QueuedEvent qev = null;
5074 WindowState win = null;
5075 if (returnWhat == RETURN_PENDING_POINTER) {
5076 qev = session.mPendingPointerMove;
5077 win = session.mPendingPointerWindow;
5078 session.mPendingPointerMove = null;
5079 session.mPendingPointerWindow = null;
5080 } else if (returnWhat == RETURN_PENDING_TRACKBALL) {
5081 qev = session.mPendingTrackballMove;
5082 win = session.mPendingTrackballWindow;
5083 session.mPendingTrackballMove = null;
5084 session.mPendingTrackballWindow = null;
5085 }
Romain Guy06882f82009-06-10 13:36:04 -07005086
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005087 if (mLastBinder == client.asBinder()) {
5088 if (DEBUG_INPUT) Log.v(
5089 TAG, "finishedKey: last paused="
5090 + ((mLastWin != null) ? mLastWin.mToken.paused : "null"));
5091 if (mLastWin != null && (!mLastWin.mToken.paused || force
5092 || !mEventDispatching)) {
5093 doFinishedKeyLocked(false);
5094 } else {
5095 // Make sure to wake up anyone currently waiting to
5096 // dispatch a key, so they can re-evaluate their
5097 // current situation.
5098 mFinished = true;
5099 notifyAll();
5100 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005101 }
Romain Guy06882f82009-06-10 13:36:04 -07005102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005103 if (qev != null) {
5104 MotionEvent res = (MotionEvent)qev.event;
5105 if (DEBUG_INPUT) Log.v(TAG,
5106 "Returning pending motion: " + res);
5107 mQueue.recycleEvent(qev);
5108 if (win != null && returnWhat == RETURN_PENDING_POINTER) {
5109 res.offsetLocation(-win.mFrame.left, -win.mFrame.top);
5110 }
5111 return res;
5112 }
5113 return null;
5114 }
5115 }
5116
5117 void tickle() {
5118 synchronized (this) {
5119 notifyAll();
5120 }
5121 }
Romain Guy06882f82009-06-10 13:36:04 -07005122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005123 void handleNewWindowLocked(WindowState newWindow) {
5124 if (!newWindow.canReceiveKeys()) {
5125 return;
5126 }
5127 synchronized (this) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005128 if (DEBUG_INPUT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005129 TAG, "New key dispatch window: win="
5130 + newWindow.mClient.asBinder()
5131 + ", last=" + mLastBinder
5132 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
5133 + "), finished=" + mFinished + ", paused="
5134 + newWindow.mToken.paused);
5135
5136 // Displaying a window implicitly causes dispatching to
5137 // be unpaused. (This is to protect against bugs if someone
5138 // pauses dispatching but forgets to resume.)
5139 newWindow.mToken.paused = false;
5140
5141 mGotFirstWindow = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005142
5143 if ((newWindow.mAttrs.flags & FLAG_SYSTEM_ERROR) != 0) {
5144 if (DEBUG_INPUT) Log.v(TAG,
5145 "New SYSTEM_ERROR window; resetting state");
5146 mLastWin = null;
5147 mLastBinder = null;
5148 mMotionTarget = null;
5149 mFinished = true;
5150 } else if (mLastWin != null) {
5151 // If the new window is above the window we are
5152 // waiting on, then stop waiting and let key dispatching
5153 // start on the new guy.
5154 if (DEBUG_INPUT) Log.v(
5155 TAG, "Last win layer=" + mLastWin.mLayer
5156 + ", new win layer=" + newWindow.mLayer);
5157 if (newWindow.mLayer >= mLastWin.mLayer) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005158 // The new window is above the old; finish pending input to the last
5159 // window and start directing it to the new one.
5160 mLastWin.mToken.paused = false;
5161 doFinishedKeyLocked(true); // does a notifyAll()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005162 }
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005163 // Either the new window is lower, so there is no need to wake key waiters,
5164 // or we just finished key input to the previous window, which implicitly
5165 // notified the key waiters. In both cases, we don't need to issue the
5166 // notification here.
5167 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005168 }
5169
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005170 // Now that we've put a new window state in place, make the event waiter
5171 // take notice and retarget its attentions.
5172 notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005173 }
5174 }
5175
5176 void pauseDispatchingLocked(WindowToken token) {
5177 synchronized (this)
5178 {
5179 if (DEBUG_INPUT) Log.v(TAG, "Pausing WindowToken " + token);
5180 token.paused = true;
5181
5182 /*
5183 if (mLastWin != null && !mFinished && mLastWin.mBaseLayer <= layer) {
5184 mPaused = true;
5185 } else {
5186 if (mLastWin == null) {
Dave Bortcfe65242009-04-09 14:51:04 -07005187 Log.i(TAG, "Key dispatching not paused: no last window.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005188 } else if (mFinished) {
Dave Bortcfe65242009-04-09 14:51:04 -07005189 Log.i(TAG, "Key dispatching not paused: finished last key.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005190 } else {
Dave Bortcfe65242009-04-09 14:51:04 -07005191 Log.i(TAG, "Key dispatching not paused: window in higher layer.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005192 }
5193 }
5194 */
5195 }
5196 }
5197
5198 void resumeDispatchingLocked(WindowToken token) {
5199 synchronized (this) {
5200 if (token.paused) {
5201 if (DEBUG_INPUT) Log.v(
5202 TAG, "Resuming WindowToken " + token
5203 + ", last=" + mLastBinder
5204 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
5205 + "), finished=" + mFinished + ", paused="
5206 + token.paused);
5207 token.paused = false;
5208 if (mLastWin != null && mLastWin.mToken == token && mFinished) {
5209 doFinishedKeyLocked(true);
5210 } else {
5211 notifyAll();
5212 }
5213 }
5214 }
5215 }
5216
5217 void setEventDispatchingLocked(boolean enabled) {
5218 synchronized (this) {
5219 mEventDispatching = enabled;
5220 notifyAll();
5221 }
5222 }
Romain Guy06882f82009-06-10 13:36:04 -07005223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005224 void appSwitchComing() {
5225 synchronized (this) {
5226 // Don't wait for more than .5 seconds for app to finish
5227 // processing the pending events.
5228 long now = SystemClock.uptimeMillis() + 500;
5229 if (DEBUG_INPUT) Log.v(TAG, "appSwitchComing: " + now);
5230 if (mTimeToSwitch == 0 || now < mTimeToSwitch) {
5231 mTimeToSwitch = now;
5232 }
5233 notifyAll();
5234 }
5235 }
Romain Guy06882f82009-06-10 13:36:04 -07005236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005237 private final void doFinishedKeyLocked(boolean doRecycle) {
5238 if (mLastWin != null) {
5239 releasePendingPointerLocked(mLastWin.mSession);
5240 releasePendingTrackballLocked(mLastWin.mSession);
5241 }
Romain Guy06882f82009-06-10 13:36:04 -07005242
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005243 if (mLastWin == null || !mLastWin.mToken.paused
5244 || !mLastWin.isVisibleLw()) {
5245 // If the current window has been paused, we aren't -really-
5246 // finished... so let the waiters still wait.
5247 mLastWin = null;
5248 mLastBinder = null;
5249 }
5250 mFinished = true;
5251 notifyAll();
5252 }
5253 }
5254
5255 private class KeyQ extends KeyInputQueue
5256 implements KeyInputQueue.FilterCallback {
5257 PowerManager.WakeLock mHoldingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07005258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005259 KeyQ() {
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005260 super(mContext, WindowManagerService.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005261 PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
5262 mHoldingScreen = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
5263 "KEEP_SCREEN_ON_FLAG");
5264 mHoldingScreen.setReferenceCounted(false);
5265 }
5266
5267 @Override
5268 boolean preprocessEvent(InputDevice device, RawInputEvent event) {
5269 if (mPolicy.preprocessInputEventTq(event)) {
5270 return true;
5271 }
Romain Guy06882f82009-06-10 13:36:04 -07005272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005273 switch (event.type) {
5274 case RawInputEvent.EV_KEY: {
5275 // XXX begin hack
5276 if (DEBUG) {
5277 if (event.keycode == KeyEvent.KEYCODE_G) {
5278 if (event.value != 0) {
5279 // G down
5280 mPolicy.screenTurnedOff(WindowManagerPolicy.OFF_BECAUSE_OF_USER);
5281 }
5282 return false;
5283 }
5284 if (event.keycode == KeyEvent.KEYCODE_D) {
5285 if (event.value != 0) {
5286 //dump();
5287 }
5288 return false;
5289 }
5290 }
5291 // XXX end hack
Romain Guy06882f82009-06-10 13:36:04 -07005292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005293 boolean screenIsOff = !mPowerManager.screenIsOn();
5294 boolean screenIsDim = !mPowerManager.screenIsBright();
5295 int actions = mPolicy.interceptKeyTq(event, !screenIsOff);
Romain Guy06882f82009-06-10 13:36:04 -07005296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005297 if ((actions & WindowManagerPolicy.ACTION_GO_TO_SLEEP) != 0) {
5298 mPowerManager.goToSleep(event.when);
5299 }
5300
5301 if (screenIsOff) {
5302 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5303 }
5304 if (screenIsDim) {
5305 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5306 }
5307 if ((actions & WindowManagerPolicy.ACTION_POKE_USER_ACTIVITY) != 0) {
5308 mPowerManager.userActivity(event.when, false,
Michael Chane96440f2009-05-06 10:27:36 -07005309 LocalPowerManager.BUTTON_EVENT, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005310 }
Romain Guy06882f82009-06-10 13:36:04 -07005311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005312 if ((actions & WindowManagerPolicy.ACTION_PASS_TO_USER) != 0) {
5313 if (event.value != 0 && mPolicy.isAppSwitchKeyTqTiLwLi(event.keycode)) {
5314 filterQueue(this);
5315 mKeyWaiter.appSwitchComing();
5316 }
5317 return true;
5318 } else {
5319 return false;
5320 }
5321 }
Romain Guy06882f82009-06-10 13:36:04 -07005322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005323 case RawInputEvent.EV_REL: {
5324 boolean screenIsOff = !mPowerManager.screenIsOn();
5325 boolean screenIsDim = !mPowerManager.screenIsBright();
5326 if (screenIsOff) {
5327 if (!mPolicy.isWakeRelMovementTq(event.deviceId,
5328 device.classes, event)) {
5329 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
5330 return false;
5331 }
5332 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5333 }
5334 if (screenIsDim) {
5335 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5336 }
5337 return true;
5338 }
Romain Guy06882f82009-06-10 13:36:04 -07005339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005340 case RawInputEvent.EV_ABS: {
5341 boolean screenIsOff = !mPowerManager.screenIsOn();
5342 boolean screenIsDim = !mPowerManager.screenIsBright();
5343 if (screenIsOff) {
5344 if (!mPolicy.isWakeAbsMovementTq(event.deviceId,
5345 device.classes, event)) {
5346 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
5347 return false;
5348 }
5349 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5350 }
5351 if (screenIsDim) {
5352 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5353 }
5354 return true;
5355 }
Romain Guy06882f82009-06-10 13:36:04 -07005356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005357 default:
5358 return true;
5359 }
5360 }
5361
5362 public int filterEvent(QueuedEvent ev) {
5363 switch (ev.classType) {
5364 case RawInputEvent.CLASS_KEYBOARD:
5365 KeyEvent ke = (KeyEvent)ev.event;
5366 if (mPolicy.isMovementKeyTi(ke.getKeyCode())) {
5367 Log.w(TAG, "Dropping movement key during app switch: "
5368 + ke.getKeyCode() + ", action=" + ke.getAction());
5369 return FILTER_REMOVE;
5370 }
5371 return FILTER_ABORT;
5372 default:
5373 return FILTER_KEEP;
5374 }
5375 }
Romain Guy06882f82009-06-10 13:36:04 -07005376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005377 /**
5378 * Must be called with the main window manager lock held.
5379 */
5380 void setHoldScreenLocked(boolean holding) {
5381 boolean state = mHoldingScreen.isHeld();
5382 if (holding != state) {
5383 if (holding) {
5384 mHoldingScreen.acquire();
5385 } else {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005386 mPolicy.screenOnStoppedLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005387 mHoldingScreen.release();
5388 }
5389 }
5390 }
Michael Chan53071d62009-05-13 17:29:48 -07005391 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005392
5393 public boolean detectSafeMode() {
5394 mSafeMode = mPolicy.detectSafeMode();
5395 return mSafeMode;
5396 }
Romain Guy06882f82009-06-10 13:36:04 -07005397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005398 public void systemReady() {
5399 mPolicy.systemReady();
5400 }
Romain Guy06882f82009-06-10 13:36:04 -07005401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005402 private final class InputDispatcherThread extends Thread {
5403 // Time to wait when there is nothing to do: 9999 seconds.
5404 static final int LONG_WAIT=9999*1000;
5405
5406 public InputDispatcherThread() {
5407 super("InputDispatcher");
5408 }
Romain Guy06882f82009-06-10 13:36:04 -07005409
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005410 @Override
5411 public void run() {
5412 while (true) {
5413 try {
5414 process();
5415 } catch (Exception e) {
5416 Log.e(TAG, "Exception in input dispatcher", e);
5417 }
5418 }
5419 }
Romain Guy06882f82009-06-10 13:36:04 -07005420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005421 private void process() {
5422 android.os.Process.setThreadPriority(
5423 android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -07005424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005425 // The last key event we saw
5426 KeyEvent lastKey = null;
5427
5428 // Last keydown time for auto-repeating keys
5429 long lastKeyTime = SystemClock.uptimeMillis();
5430 long nextKeyTime = lastKeyTime+LONG_WAIT;
5431
Romain Guy06882f82009-06-10 13:36:04 -07005432 // How many successive repeats we generated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005433 int keyRepeatCount = 0;
5434
5435 // Need to report that configuration has changed?
5436 boolean configChanged = false;
Romain Guy06882f82009-06-10 13:36:04 -07005437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005438 while (true) {
5439 long curTime = SystemClock.uptimeMillis();
5440
5441 if (DEBUG_INPUT) Log.v(
5442 TAG, "Waiting for next key: now=" + curTime
5443 + ", repeat @ " + nextKeyTime);
5444
5445 // Retrieve next event, waiting only as long as the next
5446 // repeat timeout. If the configuration has changed, then
5447 // don't wait at all -- we'll report the change as soon as
5448 // we have processed all events.
5449 QueuedEvent ev = mQueue.getEvent(
5450 (int)((!configChanged && curTime < nextKeyTime)
5451 ? (nextKeyTime-curTime) : 0));
5452
5453 if (DEBUG_INPUT && ev != null) Log.v(
5454 TAG, "Event: type=" + ev.classType + " data=" + ev.event);
5455
Michael Chan53071d62009-05-13 17:29:48 -07005456 if (MEASURE_LATENCY) {
5457 lt.sample("2 got event ", System.nanoTime() - ev.whenNano);
5458 }
5459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005460 try {
5461 if (ev != null) {
Michael Chan53071d62009-05-13 17:29:48 -07005462 curTime = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005463 int eventType;
5464 if (ev.classType == RawInputEvent.CLASS_TOUCHSCREEN) {
5465 eventType = eventType((MotionEvent)ev.event);
5466 } else if (ev.classType == RawInputEvent.CLASS_KEYBOARD ||
5467 ev.classType == RawInputEvent.CLASS_TRACKBALL) {
5468 eventType = LocalPowerManager.BUTTON_EVENT;
5469 } else {
5470 eventType = LocalPowerManager.OTHER_EVENT;
5471 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07005472 try {
Michael Chan53071d62009-05-13 17:29:48 -07005473 if ((curTime - mLastBatteryStatsCallTime)
Michael Chane96440f2009-05-06 10:27:36 -07005474 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
Michael Chan53071d62009-05-13 17:29:48 -07005475 mLastBatteryStatsCallTime = curTime;
Michael Chane96440f2009-05-06 10:27:36 -07005476 mBatteryStats.noteInputEvent();
5477 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07005478 } catch (RemoteException e) {
5479 // Ignore
5480 }
Michael Chane10de972009-05-18 11:24:50 -07005481
5482 if (eventType != TOUCH_EVENT
5483 && eventType != LONG_TOUCH_EVENT
5484 && eventType != CHEEK_EVENT) {
5485 mPowerManager.userActivity(curTime, false,
5486 eventType, false);
5487 } else if (mLastTouchEventType != eventType
5488 || (curTime - mLastUserActivityCallTime)
5489 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
5490 mLastUserActivityCallTime = curTime;
5491 mLastTouchEventType = eventType;
5492 mPowerManager.userActivity(curTime, false,
5493 eventType, false);
5494 }
5495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005496 switch (ev.classType) {
5497 case RawInputEvent.CLASS_KEYBOARD:
5498 KeyEvent ke = (KeyEvent)ev.event;
5499 if (ke.isDown()) {
5500 lastKey = ke;
5501 keyRepeatCount = 0;
5502 lastKeyTime = curTime;
5503 nextKeyTime = lastKeyTime
5504 + KEY_REPEAT_FIRST_DELAY;
5505 if (DEBUG_INPUT) Log.v(
5506 TAG, "Received key down: first repeat @ "
5507 + nextKeyTime);
5508 } else {
5509 lastKey = null;
5510 // Arbitrary long timeout.
5511 lastKeyTime = curTime;
5512 nextKeyTime = curTime + LONG_WAIT;
5513 if (DEBUG_INPUT) Log.v(
5514 TAG, "Received key up: ignore repeat @ "
5515 + nextKeyTime);
5516 }
5517 dispatchKey((KeyEvent)ev.event, 0, 0);
5518 mQueue.recycleEvent(ev);
5519 break;
5520 case RawInputEvent.CLASS_TOUCHSCREEN:
5521 //Log.i(TAG, "Read next event " + ev);
5522 dispatchPointer(ev, (MotionEvent)ev.event, 0, 0);
5523 break;
5524 case RawInputEvent.CLASS_TRACKBALL:
5525 dispatchTrackball(ev, (MotionEvent)ev.event, 0, 0);
5526 break;
5527 case RawInputEvent.CLASS_CONFIGURATION_CHANGED:
5528 configChanged = true;
5529 break;
5530 default:
5531 mQueue.recycleEvent(ev);
5532 break;
5533 }
Romain Guy06882f82009-06-10 13:36:04 -07005534
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005535 } else if (configChanged) {
5536 configChanged = false;
5537 sendNewConfiguration();
Romain Guy06882f82009-06-10 13:36:04 -07005538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005539 } else if (lastKey != null) {
5540 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07005541
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005542 // Timeout occurred while key was down. If it is at or
5543 // past the key repeat time, dispatch the repeat.
5544 if (DEBUG_INPUT) Log.v(
5545 TAG, "Key timeout: repeat=" + nextKeyTime
5546 + ", now=" + curTime);
5547 if (curTime < nextKeyTime) {
5548 continue;
5549 }
Romain Guy06882f82009-06-10 13:36:04 -07005550
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005551 lastKeyTime = nextKeyTime;
5552 nextKeyTime = nextKeyTime + KEY_REPEAT_DELAY;
5553 keyRepeatCount++;
5554 if (DEBUG_INPUT) Log.v(
5555 TAG, "Key repeat: count=" + keyRepeatCount
5556 + ", next @ " + nextKeyTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07005557 dispatchKey(KeyEvent.changeTimeRepeat(lastKey, curTime, keyRepeatCount), 0, 0);
Romain Guy06882f82009-06-10 13:36:04 -07005558
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005559 } else {
5560 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07005561
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005562 lastKeyTime = curTime;
5563 nextKeyTime = curTime + LONG_WAIT;
5564 }
Romain Guy06882f82009-06-10 13:36:04 -07005565
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005566 } catch (Exception e) {
5567 Log.e(TAG,
5568 "Input thread received uncaught exception: " + e, e);
5569 }
5570 }
5571 }
5572 }
5573
5574 // -------------------------------------------------------------
5575 // Client Session State
5576 // -------------------------------------------------------------
5577
5578 private final class Session extends IWindowSession.Stub
5579 implements IBinder.DeathRecipient {
5580 final IInputMethodClient mClient;
5581 final IInputContext mInputContext;
5582 final int mUid;
5583 final int mPid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005584 final String mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005585 SurfaceSession mSurfaceSession;
5586 int mNumWindow = 0;
5587 boolean mClientDead = false;
Romain Guy06882f82009-06-10 13:36:04 -07005588
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005589 /**
5590 * Current pointer move event being dispatched to client window... must
5591 * hold key lock to access.
5592 */
5593 QueuedEvent mPendingPointerMove;
5594 WindowState mPendingPointerWindow;
Romain Guy06882f82009-06-10 13:36:04 -07005595
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005596 /**
5597 * Current trackball move event being dispatched to client window... must
5598 * hold key lock to access.
5599 */
5600 QueuedEvent mPendingTrackballMove;
5601 WindowState mPendingTrackballWindow;
5602
5603 public Session(IInputMethodClient client, IInputContext inputContext) {
5604 mClient = client;
5605 mInputContext = inputContext;
5606 mUid = Binder.getCallingUid();
5607 mPid = Binder.getCallingPid();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005608 StringBuilder sb = new StringBuilder();
5609 sb.append("Session{");
5610 sb.append(Integer.toHexString(System.identityHashCode(this)));
5611 sb.append(" uid ");
5612 sb.append(mUid);
5613 sb.append("}");
5614 mStringName = sb.toString();
Romain Guy06882f82009-06-10 13:36:04 -07005615
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005616 synchronized (mWindowMap) {
5617 if (mInputMethodManager == null && mHaveInputMethods) {
5618 IBinder b = ServiceManager.getService(
5619 Context.INPUT_METHOD_SERVICE);
5620 mInputMethodManager = IInputMethodManager.Stub.asInterface(b);
5621 }
5622 }
5623 long ident = Binder.clearCallingIdentity();
5624 try {
5625 // Note: it is safe to call in to the input method manager
5626 // here because we are not holding our lock.
5627 if (mInputMethodManager != null) {
5628 mInputMethodManager.addClient(client, inputContext,
5629 mUid, mPid);
5630 } else {
5631 client.setUsingInputMethod(false);
5632 }
5633 client.asBinder().linkToDeath(this, 0);
5634 } catch (RemoteException e) {
5635 // The caller has died, so we can just forget about this.
5636 try {
5637 if (mInputMethodManager != null) {
5638 mInputMethodManager.removeClient(client);
5639 }
5640 } catch (RemoteException ee) {
5641 }
5642 } finally {
5643 Binder.restoreCallingIdentity(ident);
5644 }
5645 }
Romain Guy06882f82009-06-10 13:36:04 -07005646
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005647 @Override
5648 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
5649 throws RemoteException {
5650 try {
5651 return super.onTransact(code, data, reply, flags);
5652 } catch (RuntimeException e) {
5653 // Log all 'real' exceptions thrown to the caller
5654 if (!(e instanceof SecurityException)) {
5655 Log.e(TAG, "Window Session Crash", e);
5656 }
5657 throw e;
5658 }
5659 }
5660
5661 public void binderDied() {
5662 // Note: it is safe to call in to the input method manager
5663 // here because we are not holding our lock.
5664 try {
5665 if (mInputMethodManager != null) {
5666 mInputMethodManager.removeClient(mClient);
5667 }
5668 } catch (RemoteException e) {
5669 }
5670 synchronized(mWindowMap) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07005671 mClient.asBinder().unlinkToDeath(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005672 mClientDead = true;
5673 killSessionLocked();
5674 }
5675 }
5676
5677 public int add(IWindow window, WindowManager.LayoutParams attrs,
5678 int viewVisibility, Rect outContentInsets) {
5679 return addWindow(this, window, attrs, viewVisibility, outContentInsets);
5680 }
Romain Guy06882f82009-06-10 13:36:04 -07005681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005682 public void remove(IWindow window) {
5683 removeWindow(this, window);
5684 }
Romain Guy06882f82009-06-10 13:36:04 -07005685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005686 public int relayout(IWindow window, WindowManager.LayoutParams attrs,
5687 int requestedWidth, int requestedHeight, int viewFlags,
5688 boolean insetsPending, Rect outFrame, Rect outContentInsets,
5689 Rect outVisibleInsets, Surface outSurface) {
5690 return relayoutWindow(this, window, attrs,
5691 requestedWidth, requestedHeight, viewFlags, insetsPending,
5692 outFrame, outContentInsets, outVisibleInsets, outSurface);
5693 }
Romain Guy06882f82009-06-10 13:36:04 -07005694
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005695 public void setTransparentRegion(IWindow window, Region region) {
5696 setTransparentRegionWindow(this, window, region);
5697 }
Romain Guy06882f82009-06-10 13:36:04 -07005698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005699 public void setInsets(IWindow window, int touchableInsets,
5700 Rect contentInsets, Rect visibleInsets) {
5701 setInsetsWindow(this, window, touchableInsets, contentInsets,
5702 visibleInsets);
5703 }
Romain Guy06882f82009-06-10 13:36:04 -07005704
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005705 public void getDisplayFrame(IWindow window, Rect outDisplayFrame) {
5706 getWindowDisplayFrame(this, window, outDisplayFrame);
5707 }
Romain Guy06882f82009-06-10 13:36:04 -07005708
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005709 public void finishDrawing(IWindow window) {
5710 if (localLOGV) Log.v(
5711 TAG, "IWindow finishDrawing called for " + window);
5712 finishDrawingWindow(this, window);
5713 }
5714
5715 public void finishKey(IWindow window) {
5716 if (localLOGV) Log.v(
5717 TAG, "IWindow finishKey called for " + window);
5718 mKeyWaiter.finishedKey(this, window, false,
5719 KeyWaiter.RETURN_NOTHING);
5720 }
5721
5722 public MotionEvent getPendingPointerMove(IWindow window) {
5723 if (localLOGV) Log.v(
5724 TAG, "IWindow getPendingMotionEvent called for " + window);
5725 return mKeyWaiter.finishedKey(this, window, false,
5726 KeyWaiter.RETURN_PENDING_POINTER);
5727 }
Romain Guy06882f82009-06-10 13:36:04 -07005728
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005729 public MotionEvent getPendingTrackballMove(IWindow window) {
5730 if (localLOGV) Log.v(
5731 TAG, "IWindow getPendingMotionEvent called for " + window);
5732 return mKeyWaiter.finishedKey(this, window, false,
5733 KeyWaiter.RETURN_PENDING_TRACKBALL);
5734 }
5735
5736 public void setInTouchMode(boolean mode) {
5737 synchronized(mWindowMap) {
5738 mInTouchMode = mode;
5739 }
5740 }
5741
5742 public boolean getInTouchMode() {
5743 synchronized(mWindowMap) {
5744 return mInTouchMode;
5745 }
5746 }
5747
5748 public boolean performHapticFeedback(IWindow window, int effectId,
5749 boolean always) {
5750 synchronized(mWindowMap) {
5751 long ident = Binder.clearCallingIdentity();
5752 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005753 return mPolicy.performHapticFeedbackLw(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005754 windowForClientLocked(this, window), effectId, always);
5755 } finally {
5756 Binder.restoreCallingIdentity(ident);
5757 }
5758 }
5759 }
Romain Guy06882f82009-06-10 13:36:04 -07005760
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005761 void windowAddedLocked() {
5762 if (mSurfaceSession == null) {
5763 if (localLOGV) Log.v(
5764 TAG, "First window added to " + this + ", creating SurfaceSession");
5765 mSurfaceSession = new SurfaceSession();
5766 mSessions.add(this);
5767 }
5768 mNumWindow++;
5769 }
5770
5771 void windowRemovedLocked() {
5772 mNumWindow--;
5773 killSessionLocked();
5774 }
Romain Guy06882f82009-06-10 13:36:04 -07005775
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005776 void killSessionLocked() {
5777 if (mNumWindow <= 0 && mClientDead) {
5778 mSessions.remove(this);
5779 if (mSurfaceSession != null) {
5780 if (localLOGV) Log.v(
5781 TAG, "Last window removed from " + this
5782 + ", destroying " + mSurfaceSession);
5783 try {
5784 mSurfaceSession.kill();
5785 } catch (Exception e) {
5786 Log.w(TAG, "Exception thrown when killing surface session "
5787 + mSurfaceSession + " in session " + this
5788 + ": " + e.toString());
5789 }
5790 mSurfaceSession = null;
5791 }
5792 }
5793 }
Romain Guy06882f82009-06-10 13:36:04 -07005794
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005795 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005796 pw.print(prefix); pw.print("mNumWindow="); pw.print(mNumWindow);
5797 pw.print(" mClientDead="); pw.print(mClientDead);
5798 pw.print(" mSurfaceSession="); pw.println(mSurfaceSession);
5799 if (mPendingPointerWindow != null || mPendingPointerMove != null) {
5800 pw.print(prefix);
5801 pw.print("mPendingPointerWindow="); pw.print(mPendingPointerWindow);
5802 pw.print(" mPendingPointerMove="); pw.println(mPendingPointerMove);
5803 }
5804 if (mPendingTrackballWindow != null || mPendingTrackballMove != null) {
5805 pw.print(prefix);
5806 pw.print("mPendingTrackballWindow="); pw.print(mPendingTrackballWindow);
5807 pw.print(" mPendingTrackballMove="); pw.println(mPendingTrackballMove);
5808 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005809 }
5810
5811 @Override
5812 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005813 return mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005814 }
5815 }
5816
5817 // -------------------------------------------------------------
5818 // Client Window State
5819 // -------------------------------------------------------------
5820
5821 private final class WindowState implements WindowManagerPolicy.WindowState {
5822 final Session mSession;
5823 final IWindow mClient;
5824 WindowToken mToken;
The Android Open Source Project10592532009-03-18 17:39:46 -07005825 WindowToken mRootToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005826 AppWindowToken mAppToken;
5827 AppWindowToken mTargetAppToken;
5828 final WindowManager.LayoutParams mAttrs = new WindowManager.LayoutParams();
5829 final DeathRecipient mDeathRecipient;
5830 final WindowState mAttachedWindow;
5831 final ArrayList mChildWindows = new ArrayList();
5832 final int mBaseLayer;
5833 final int mSubLayer;
5834 final boolean mLayoutAttached;
5835 final boolean mIsImWindow;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07005836 final boolean mIsWallpaper;
5837 final boolean mIsFloatingLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005838 int mViewVisibility;
5839 boolean mPolicyVisibility = true;
5840 boolean mPolicyVisibilityAfterAnim = true;
5841 boolean mAppFreezing;
5842 Surface mSurface;
5843 boolean mAttachedHidden; // is our parent window hidden?
5844 boolean mLastHidden; // was this window last hidden?
5845 int mRequestedWidth;
5846 int mRequestedHeight;
5847 int mLastRequestedWidth;
5848 int mLastRequestedHeight;
5849 int mReqXPos;
5850 int mReqYPos;
5851 int mLayer;
5852 int mAnimLayer;
5853 int mLastLayer;
5854 boolean mHaveFrame;
5855
5856 WindowState mNextOutsideTouch;
Romain Guy06882f82009-06-10 13:36:04 -07005857
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005858 // Actual frame shown on-screen (may be modified by animation)
5859 final Rect mShownFrame = new Rect();
5860 final Rect mLastShownFrame = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07005861
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005862 /**
5863 * Insets that determine the actually visible area
5864 */
5865 final Rect mVisibleInsets = new Rect();
5866 final Rect mLastVisibleInsets = new Rect();
5867 boolean mVisibleInsetsChanged;
5868
5869 /**
5870 * Insets that are covered by system windows
5871 */
5872 final Rect mContentInsets = new Rect();
5873 final Rect mLastContentInsets = new Rect();
5874 boolean mContentInsetsChanged;
5875
5876 /**
5877 * Set to true if we are waiting for this window to receive its
5878 * given internal insets before laying out other windows based on it.
5879 */
5880 boolean mGivenInsetsPending;
Romain Guy06882f82009-06-10 13:36:04 -07005881
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005882 /**
5883 * These are the content insets that were given during layout for
5884 * this window, to be applied to windows behind it.
5885 */
5886 final Rect mGivenContentInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07005887
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005888 /**
5889 * These are the visible insets that were given during layout for
5890 * this window, to be applied to windows behind it.
5891 */
5892 final Rect mGivenVisibleInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07005893
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005894 /**
5895 * Flag indicating whether the touchable region should be adjusted by
5896 * the visible insets; if false the area outside the visible insets is
5897 * NOT touchable, so we must use those to adjust the frame during hit
5898 * tests.
5899 */
5900 int mTouchableInsets = ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME;
Romain Guy06882f82009-06-10 13:36:04 -07005901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005902 // Current transformation being applied.
5903 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
5904 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
5905 float mHScale=1, mVScale=1;
5906 float mLastHScale=1, mLastVScale=1;
5907 final Matrix mTmpMatrix = new Matrix();
5908
5909 // "Real" frame that the application sees.
5910 final Rect mFrame = new Rect();
5911 final Rect mLastFrame = new Rect();
5912
5913 final Rect mContainingFrame = new Rect();
5914 final Rect mDisplayFrame = new Rect();
5915 final Rect mContentFrame = new Rect();
5916 final Rect mVisibleFrame = new Rect();
5917
5918 float mShownAlpha = 1;
5919 float mAlpha = 1;
5920 float mLastAlpha = 1;
5921
5922 // Set to true if, when the window gets displayed, it should perform
5923 // an enter animation.
5924 boolean mEnterAnimationPending;
5925
5926 // Currently running animation.
5927 boolean mAnimating;
5928 boolean mLocalAnimating;
5929 Animation mAnimation;
5930 boolean mAnimationIsEntrance;
5931 boolean mHasTransformation;
5932 boolean mHasLocalTransformation;
5933 final Transformation mTransformation = new Transformation();
5934
5935 // This is set after IWindowSession.relayout() has been called at
5936 // least once for the window. It allows us to detect the situation
5937 // where we don't yet have a surface, but should have one soon, so
5938 // we can give the window focus before waiting for the relayout.
5939 boolean mRelayoutCalled;
Romain Guy06882f82009-06-10 13:36:04 -07005940
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005941 // This is set after the Surface has been created but before the
5942 // window has been drawn. During this time the surface is hidden.
5943 boolean mDrawPending;
5944
5945 // This is set after the window has finished drawing for the first
5946 // time but before its surface is shown. The surface will be
5947 // displayed when the next layout is run.
5948 boolean mCommitDrawPending;
5949
5950 // This is set during the time after the window's drawing has been
5951 // committed, and before its surface is actually shown. It is used
5952 // to delay showing the surface until all windows in a token are ready
5953 // to be shown.
5954 boolean mReadyToShow;
Romain Guy06882f82009-06-10 13:36:04 -07005955
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005956 // Set when the window has been shown in the screen the first time.
5957 boolean mHasDrawn;
5958
5959 // Currently running an exit animation?
5960 boolean mExiting;
5961
5962 // Currently on the mDestroySurface list?
5963 boolean mDestroying;
Romain Guy06882f82009-06-10 13:36:04 -07005964
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005965 // Completely remove from window manager after exit animation?
5966 boolean mRemoveOnExit;
5967
5968 // Set when the orientation is changing and this window has not yet
5969 // been updated for the new orientation.
5970 boolean mOrientationChanging;
Romain Guy06882f82009-06-10 13:36:04 -07005971
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005972 // Is this window now (or just being) removed?
5973 boolean mRemoved;
Romain Guy06882f82009-06-10 13:36:04 -07005974
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005975 WindowState(Session s, IWindow c, WindowToken token,
5976 WindowState attachedWindow, WindowManager.LayoutParams a,
5977 int viewVisibility) {
5978 mSession = s;
5979 mClient = c;
5980 mToken = token;
5981 mAttrs.copyFrom(a);
5982 mViewVisibility = viewVisibility;
5983 DeathRecipient deathRecipient = new DeathRecipient();
5984 mAlpha = a.alpha;
5985 if (localLOGV) Log.v(
5986 TAG, "Window " + this + " client=" + c.asBinder()
5987 + " token=" + token + " (" + mAttrs.token + ")");
5988 try {
5989 c.asBinder().linkToDeath(deathRecipient, 0);
5990 } catch (RemoteException e) {
5991 mDeathRecipient = null;
5992 mAttachedWindow = null;
5993 mLayoutAttached = false;
5994 mIsImWindow = false;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07005995 mIsWallpaper = false;
5996 mIsFloatingLayer = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005997 mBaseLayer = 0;
5998 mSubLayer = 0;
5999 return;
6000 }
6001 mDeathRecipient = deathRecipient;
Romain Guy06882f82009-06-10 13:36:04 -07006002
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006003 if ((mAttrs.type >= FIRST_SUB_WINDOW &&
6004 mAttrs.type <= LAST_SUB_WINDOW)) {
6005 // The multiplier here is to reserve space for multiple
6006 // windows in the same type layer.
6007 mBaseLayer = mPolicy.windowTypeToLayerLw(
6008 attachedWindow.mAttrs.type) * TYPE_LAYER_MULTIPLIER
6009 + TYPE_LAYER_OFFSET;
6010 mSubLayer = mPolicy.subWindowTypeToLayerLw(a.type);
6011 mAttachedWindow = attachedWindow;
6012 mAttachedWindow.mChildWindows.add(this);
6013 mLayoutAttached = mAttrs.type !=
6014 WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
6015 mIsImWindow = attachedWindow.mAttrs.type == TYPE_INPUT_METHOD
6016 || attachedWindow.mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006017 mIsWallpaper = attachedWindow.mAttrs.type == TYPE_WALLPAPER;
6018 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006019 } else {
6020 // The multiplier here is to reserve space for multiple
6021 // windows in the same type layer.
6022 mBaseLayer = mPolicy.windowTypeToLayerLw(a.type)
6023 * TYPE_LAYER_MULTIPLIER
6024 + TYPE_LAYER_OFFSET;
6025 mSubLayer = 0;
6026 mAttachedWindow = null;
6027 mLayoutAttached = false;
6028 mIsImWindow = mAttrs.type == TYPE_INPUT_METHOD
6029 || mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006030 mIsWallpaper = mAttrs.type == TYPE_WALLPAPER;
6031 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006032 }
6033
6034 WindowState appWin = this;
6035 while (appWin.mAttachedWindow != null) {
6036 appWin = mAttachedWindow;
6037 }
6038 WindowToken appToken = appWin.mToken;
6039 while (appToken.appWindowToken == null) {
6040 WindowToken parent = mTokenMap.get(appToken.token);
6041 if (parent == null || appToken == parent) {
6042 break;
6043 }
6044 appToken = parent;
6045 }
The Android Open Source Project10592532009-03-18 17:39:46 -07006046 mRootToken = appToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006047 mAppToken = appToken.appWindowToken;
6048
6049 mSurface = null;
6050 mRequestedWidth = 0;
6051 mRequestedHeight = 0;
6052 mLastRequestedWidth = 0;
6053 mLastRequestedHeight = 0;
6054 mReqXPos = 0;
6055 mReqYPos = 0;
6056 mLayer = 0;
6057 mAnimLayer = 0;
6058 mLastLayer = 0;
6059 }
6060
6061 void attach() {
6062 if (localLOGV) Log.v(
6063 TAG, "Attaching " + this + " token=" + mToken
6064 + ", list=" + mToken.windows);
6065 mSession.windowAddedLocked();
6066 }
6067
6068 public void computeFrameLw(Rect pf, Rect df, Rect cf, Rect vf) {
6069 mHaveFrame = true;
6070
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006071 final Rect container = mContainingFrame;
6072 container.set(pf);
6073
6074 final Rect display = mDisplayFrame;
6075 display.set(df);
6076
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07006077 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006078 container.intersect(mCompatibleScreenFrame);
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07006079 if ((mAttrs.flags & FLAG_LAYOUT_NO_LIMITS) == 0) {
6080 display.intersect(mCompatibleScreenFrame);
6081 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006082 }
6083
6084 final int pw = container.right - container.left;
6085 final int ph = container.bottom - container.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006086
6087 int w,h;
6088 if ((mAttrs.flags & mAttrs.FLAG_SCALED) != 0) {
6089 w = mAttrs.width < 0 ? pw : mAttrs.width;
6090 h = mAttrs.height< 0 ? ph : mAttrs.height;
6091 } else {
6092 w = mAttrs.width == mAttrs.FILL_PARENT ? pw : mRequestedWidth;
6093 h = mAttrs.height== mAttrs.FILL_PARENT ? ph : mRequestedHeight;
6094 }
Romain Guy06882f82009-06-10 13:36:04 -07006095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006096 final Rect content = mContentFrame;
6097 content.set(cf);
Romain Guy06882f82009-06-10 13:36:04 -07006098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006099 final Rect visible = mVisibleFrame;
6100 visible.set(vf);
Romain Guy06882f82009-06-10 13:36:04 -07006101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006102 final Rect frame = mFrame;
Romain Guy06882f82009-06-10 13:36:04 -07006103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006104 //System.out.println("In: w=" + w + " h=" + h + " container=" +
6105 // container + " x=" + mAttrs.x + " y=" + mAttrs.y);
6106
6107 Gravity.apply(mAttrs.gravity, w, h, container,
6108 (int) (mAttrs.x + mAttrs.horizontalMargin * pw),
6109 (int) (mAttrs.y + mAttrs.verticalMargin * ph), frame);
6110
6111 //System.out.println("Out: " + mFrame);
6112
6113 // Now make sure the window fits in the overall display.
6114 Gravity.applyDisplay(mAttrs.gravity, df, frame);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006116 // Make sure the content and visible frames are inside of the
6117 // final window frame.
6118 if (content.left < frame.left) content.left = frame.left;
6119 if (content.top < frame.top) content.top = frame.top;
6120 if (content.right > frame.right) content.right = frame.right;
6121 if (content.bottom > frame.bottom) content.bottom = frame.bottom;
6122 if (visible.left < frame.left) visible.left = frame.left;
6123 if (visible.top < frame.top) visible.top = frame.top;
6124 if (visible.right > frame.right) visible.right = frame.right;
6125 if (visible.bottom > frame.bottom) visible.bottom = frame.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006127 final Rect contentInsets = mContentInsets;
6128 contentInsets.left = content.left-frame.left;
6129 contentInsets.top = content.top-frame.top;
6130 contentInsets.right = frame.right-content.right;
6131 contentInsets.bottom = frame.bottom-content.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006133 final Rect visibleInsets = mVisibleInsets;
6134 visibleInsets.left = visible.left-frame.left;
6135 visibleInsets.top = visible.top-frame.top;
6136 visibleInsets.right = frame.right-visible.right;
6137 visibleInsets.bottom = frame.bottom-visible.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006139 if (localLOGV) {
6140 //if ("com.google.android.youtube".equals(mAttrs.packageName)
6141 // && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
6142 Log.v(TAG, "Resolving (mRequestedWidth="
6143 + mRequestedWidth + ", mRequestedheight="
6144 + mRequestedHeight + ") to" + " (pw=" + pw + ", ph=" + ph
6145 + "): frame=" + mFrame.toShortString()
6146 + " ci=" + contentInsets.toShortString()
6147 + " vi=" + visibleInsets.toShortString());
6148 //}
6149 }
6150 }
Romain Guy06882f82009-06-10 13:36:04 -07006151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006152 public Rect getFrameLw() {
6153 return mFrame;
6154 }
6155
6156 public Rect getShownFrameLw() {
6157 return mShownFrame;
6158 }
6159
6160 public Rect getDisplayFrameLw() {
6161 return mDisplayFrame;
6162 }
6163
6164 public Rect getContentFrameLw() {
6165 return mContentFrame;
6166 }
6167
6168 public Rect getVisibleFrameLw() {
6169 return mVisibleFrame;
6170 }
6171
6172 public boolean getGivenInsetsPendingLw() {
6173 return mGivenInsetsPending;
6174 }
6175
6176 public Rect getGivenContentInsetsLw() {
6177 return mGivenContentInsets;
6178 }
Romain Guy06882f82009-06-10 13:36:04 -07006179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006180 public Rect getGivenVisibleInsetsLw() {
6181 return mGivenVisibleInsets;
6182 }
Romain Guy06882f82009-06-10 13:36:04 -07006183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006184 public WindowManager.LayoutParams getAttrs() {
6185 return mAttrs;
6186 }
6187
6188 public int getSurfaceLayer() {
6189 return mLayer;
6190 }
Romain Guy06882f82009-06-10 13:36:04 -07006191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006192 public IApplicationToken getAppToken() {
6193 return mAppToken != null ? mAppToken.appToken : null;
6194 }
6195
6196 public boolean hasAppShownWindows() {
6197 return mAppToken != null ? mAppToken.firstWindowDrawn : false;
6198 }
6199
6200 public boolean hasAppStartingIcon() {
6201 return mAppToken != null ? (mAppToken.startingData != null) : false;
6202 }
6203
6204 public WindowManagerPolicy.WindowState getAppStartingWindow() {
6205 return mAppToken != null ? mAppToken.startingWindow : null;
6206 }
6207
6208 public void setAnimation(Animation anim) {
6209 if (localLOGV) Log.v(
6210 TAG, "Setting animation in " + this + ": " + anim);
6211 mAnimating = false;
6212 mLocalAnimating = false;
6213 mAnimation = anim;
6214 mAnimation.restrictDuration(MAX_ANIMATION_DURATION);
6215 mAnimation.scaleCurrentDuration(mWindowAnimationScale);
6216 }
6217
6218 public void clearAnimation() {
6219 if (mAnimation != null) {
6220 mAnimating = true;
6221 mLocalAnimating = false;
6222 mAnimation = null;
6223 }
6224 }
Romain Guy06882f82009-06-10 13:36:04 -07006225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006226 Surface createSurfaceLocked() {
6227 if (mSurface == null) {
6228 mDrawPending = true;
6229 mCommitDrawPending = false;
6230 mReadyToShow = false;
6231 if (mAppToken != null) {
6232 mAppToken.allDrawn = false;
6233 }
6234
6235 int flags = 0;
6236 if (mAttrs.memoryType == MEMORY_TYPE_HARDWARE) {
6237 flags |= Surface.HARDWARE;
6238 } else if (mAttrs.memoryType == MEMORY_TYPE_GPU) {
6239 flags |= Surface.GPU;
6240 } else if (mAttrs.memoryType == MEMORY_TYPE_PUSH_BUFFERS) {
6241 flags |= Surface.PUSH_BUFFERS;
6242 }
6243
6244 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
6245 flags |= Surface.SECURE;
6246 }
6247 if (DEBUG_VISIBILITY) Log.v(
6248 TAG, "Creating surface in session "
6249 + mSession.mSurfaceSession + " window " + this
6250 + " w=" + mFrame.width()
6251 + " h=" + mFrame.height() + " format="
6252 + mAttrs.format + " flags=" + flags);
6253
6254 int w = mFrame.width();
6255 int h = mFrame.height();
6256 if ((mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
6257 // for a scaled surface, we always want the requested
6258 // size.
6259 w = mRequestedWidth;
6260 h = mRequestedHeight;
6261 }
6262
6263 try {
6264 mSurface = new Surface(
Romain Guy06882f82009-06-10 13:36:04 -07006265 mSession.mSurfaceSession, mSession.mPid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006266 0, w, h, mAttrs.format, flags);
6267 } catch (Surface.OutOfResourcesException e) {
6268 Log.w(TAG, "OutOfResourcesException creating surface");
6269 reclaimSomeSurfaceMemoryLocked(this, "create");
6270 return null;
6271 } catch (Exception e) {
6272 Log.e(TAG, "Exception creating surface", e);
6273 return null;
6274 }
Romain Guy06882f82009-06-10 13:36:04 -07006275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006276 if (localLOGV) Log.v(
6277 TAG, "Got surface: " + mSurface
6278 + ", set left=" + mFrame.left + " top=" + mFrame.top
6279 + ", animLayer=" + mAnimLayer);
6280 if (SHOW_TRANSACTIONS) {
6281 Log.i(TAG, ">>> OPEN TRANSACTION");
6282 Log.i(TAG, " SURFACE " + mSurface + ": CREATE ("
6283 + mAttrs.getTitle() + ") pos=(" +
6284 mFrame.left + "," + mFrame.top + ") (" +
6285 mFrame.width() + "x" + mFrame.height() + "), layer=" +
6286 mAnimLayer + " HIDE");
6287 }
6288 Surface.openTransaction();
6289 try {
6290 try {
6291 mSurface.setPosition(mFrame.left, mFrame.top);
6292 mSurface.setLayer(mAnimLayer);
6293 mSurface.hide();
6294 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
6295 mSurface.setFlags(Surface.SURFACE_DITHER,
6296 Surface.SURFACE_DITHER);
6297 }
6298 } catch (RuntimeException e) {
6299 Log.w(TAG, "Error creating surface in " + w, e);
6300 reclaimSomeSurfaceMemoryLocked(this, "create-init");
6301 }
6302 mLastHidden = true;
6303 } finally {
6304 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
6305 Surface.closeTransaction();
6306 }
6307 if (localLOGV) Log.v(
6308 TAG, "Created surface " + this);
6309 }
6310 return mSurface;
6311 }
Romain Guy06882f82009-06-10 13:36:04 -07006312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006313 void destroySurfaceLocked() {
6314 // Window is no longer on-screen, so can no longer receive
6315 // key events... if we were waiting for it to finish
6316 // handling a key event, the wait is over!
6317 mKeyWaiter.finishedKey(mSession, mClient, true,
6318 KeyWaiter.RETURN_NOTHING);
6319 mKeyWaiter.releasePendingPointerLocked(mSession);
6320 mKeyWaiter.releasePendingTrackballLocked(mSession);
6321
6322 if (mAppToken != null && this == mAppToken.startingWindow) {
6323 mAppToken.startingDisplayed = false;
6324 }
Romain Guy06882f82009-06-10 13:36:04 -07006325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006326 if (localLOGV) Log.v(
6327 TAG, "Window " + this
6328 + " destroying surface " + mSurface + ", session " + mSession);
6329 if (mSurface != null) {
6330 try {
6331 if (SHOW_TRANSACTIONS) {
6332 RuntimeException ex = new RuntimeException();
6333 ex.fillInStackTrace();
6334 Log.i(TAG, " SURFACE " + mSurface + ": DESTROY ("
6335 + mAttrs.getTitle() + ")", ex);
6336 }
6337 mSurface.clear();
6338 } catch (RuntimeException e) {
6339 Log.w(TAG, "Exception thrown when destroying Window " + this
6340 + " surface " + mSurface + " session " + mSession
6341 + ": " + e.toString());
6342 }
6343 mSurface = null;
6344 mDrawPending = false;
6345 mCommitDrawPending = false;
6346 mReadyToShow = false;
6347
6348 int i = mChildWindows.size();
6349 while (i > 0) {
6350 i--;
6351 WindowState c = (WindowState)mChildWindows.get(i);
6352 c.mAttachedHidden = true;
6353 }
6354 }
6355 }
6356
6357 boolean finishDrawingLocked() {
6358 if (mDrawPending) {
6359 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.v(
6360 TAG, "finishDrawingLocked: " + mSurface);
6361 mCommitDrawPending = true;
6362 mDrawPending = false;
6363 return true;
6364 }
6365 return false;
6366 }
6367
6368 // This must be called while inside a transaction.
6369 void commitFinishDrawingLocked(long currentTime) {
6370 //Log.i(TAG, "commitFinishDrawingLocked: " + mSurface);
6371 if (!mCommitDrawPending) {
6372 return;
6373 }
6374 mCommitDrawPending = false;
6375 mReadyToShow = true;
6376 final boolean starting = mAttrs.type == TYPE_APPLICATION_STARTING;
6377 final AppWindowToken atoken = mAppToken;
6378 if (atoken == null || atoken.allDrawn || starting) {
6379 performShowLocked();
6380 }
6381 }
6382
6383 // This must be called while inside a transaction.
6384 boolean performShowLocked() {
6385 if (DEBUG_VISIBILITY) {
6386 RuntimeException e = new RuntimeException();
6387 e.fillInStackTrace();
6388 Log.v(TAG, "performShow on " + this
6389 + ": readyToShow=" + mReadyToShow + " readyForDisplay=" + isReadyForDisplay()
6390 + " starting=" + (mAttrs.type == TYPE_APPLICATION_STARTING), e);
6391 }
6392 if (mReadyToShow && isReadyForDisplay()) {
6393 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.i(
6394 TAG, " SURFACE " + mSurface + ": SHOW (performShowLocked)");
6395 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + this
6396 + " during animation: policyVis=" + mPolicyVisibility
6397 + " attHidden=" + mAttachedHidden
6398 + " tok.hiddenRequested="
6399 + (mAppToken != null ? mAppToken.hiddenRequested : false)
6400 + " tok.idden="
6401 + (mAppToken != null ? mAppToken.hidden : false)
6402 + " animating=" + mAnimating
6403 + " tok animating="
6404 + (mAppToken != null ? mAppToken.animating : false));
6405 if (!showSurfaceRobustlyLocked(this)) {
6406 return false;
6407 }
6408 mLastAlpha = -1;
6409 mHasDrawn = true;
6410 mLastHidden = false;
6411 mReadyToShow = false;
6412 enableScreenIfNeededLocked();
6413
6414 applyEnterAnimationLocked(this);
Romain Guy06882f82009-06-10 13:36:04 -07006415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006416 int i = mChildWindows.size();
6417 while (i > 0) {
6418 i--;
6419 WindowState c = (WindowState)mChildWindows.get(i);
6420 if (c.mSurface != null && c.mAttachedHidden) {
6421 c.mAttachedHidden = false;
6422 c.performShowLocked();
6423 }
6424 }
Romain Guy06882f82009-06-10 13:36:04 -07006425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006426 if (mAttrs.type != TYPE_APPLICATION_STARTING
6427 && mAppToken != null) {
6428 mAppToken.firstWindowDrawn = true;
6429 if (mAnimation == null && mAppToken.startingData != null) {
6430 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
6431 + mToken
6432 + ": first real window is shown, no animation");
6433 mFinishedStarting.add(mAppToken);
6434 mH.sendEmptyMessage(H.FINISHED_STARTING);
6435 }
6436 mAppToken.updateReportedVisibilityLocked();
6437 }
6438 }
6439 return true;
6440 }
Romain Guy06882f82009-06-10 13:36:04 -07006441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006442 // This must be called while inside a transaction. Returns true if
6443 // there is more animation to run.
6444 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
6445 if (!mDisplayFrozen) {
6446 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07006447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006448 if (!mDrawPending && !mCommitDrawPending && mAnimation != null) {
6449 mHasTransformation = true;
6450 mHasLocalTransformation = true;
6451 if (!mLocalAnimating) {
6452 if (DEBUG_ANIM) Log.v(
6453 TAG, "Starting animation in " + this +
6454 " @ " + currentTime + ": ww=" + mFrame.width() + " wh=" + mFrame.height() +
6455 " dw=" + dw + " dh=" + dh + " scale=" + mWindowAnimationScale);
6456 mAnimation.initialize(mFrame.width(), mFrame.height(), dw, dh);
6457 mAnimation.setStartTime(currentTime);
6458 mLocalAnimating = true;
6459 mAnimating = true;
6460 }
6461 mTransformation.clear();
6462 final boolean more = mAnimation.getTransformation(
6463 currentTime, mTransformation);
6464 if (DEBUG_ANIM) Log.v(
6465 TAG, "Stepped animation in " + this +
6466 ": more=" + more + ", xform=" + mTransformation);
6467 if (more) {
6468 // we're not done!
6469 return true;
6470 }
6471 if (DEBUG_ANIM) Log.v(
6472 TAG, "Finished animation in " + this +
6473 " @ " + currentTime);
6474 mAnimation = null;
6475 //WindowManagerService.this.dump();
6476 }
6477 mHasLocalTransformation = false;
6478 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppToken != null
6479 && mAppToken.hasTransformation) {
6480 // When our app token is animating, we kind-of pretend like
6481 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
6482 // part of this check means that we will only do this if
6483 // our window is not currently exiting, or it is not
6484 // locally animating itself. The idea being that one that
6485 // is exiting and doing a local animation should be removed
6486 // once that animation is done.
6487 mAnimating = true;
6488 mHasTransformation = true;
6489 mTransformation.clear();
6490 return false;
6491 } else if (mHasTransformation) {
6492 // Little trick to get through the path below to act like
6493 // we have finished an animation.
6494 mAnimating = true;
6495 } else if (isAnimating()) {
6496 mAnimating = true;
6497 }
6498 } else if (mAnimation != null) {
6499 // If the display is frozen, and there is a pending animation,
6500 // clear it and make sure we run the cleanup code.
6501 mAnimating = true;
6502 mLocalAnimating = true;
6503 mAnimation = null;
6504 }
Romain Guy06882f82009-06-10 13:36:04 -07006505
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006506 if (!mAnimating && !mLocalAnimating) {
6507 return false;
6508 }
6509
6510 if (DEBUG_ANIM) Log.v(
6511 TAG, "Animation done in " + this + ": exiting=" + mExiting
6512 + ", reportedVisible="
6513 + (mAppToken != null ? mAppToken.reportedVisible : false));
Romain Guy06882f82009-06-10 13:36:04 -07006514
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006515 mAnimating = false;
6516 mLocalAnimating = false;
6517 mAnimation = null;
6518 mAnimLayer = mLayer;
6519 if (mIsImWindow) {
6520 mAnimLayer += mInputMethodAnimLayerAdjustment;
6521 }
6522 if (DEBUG_LAYERS) Log.v(TAG, "Stepping win " + this
6523 + " anim layer: " + mAnimLayer);
6524 mHasTransformation = false;
6525 mHasLocalTransformation = false;
6526 mPolicyVisibility = mPolicyVisibilityAfterAnim;
6527 mTransformation.clear();
6528 if (mHasDrawn
6529 && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
6530 && mAppToken != null
6531 && mAppToken.firstWindowDrawn
6532 && mAppToken.startingData != null) {
6533 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
6534 + mToken + ": first real window done animating");
6535 mFinishedStarting.add(mAppToken);
6536 mH.sendEmptyMessage(H.FINISHED_STARTING);
6537 }
Romain Guy06882f82009-06-10 13:36:04 -07006538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006539 finishExit();
6540
6541 if (mAppToken != null) {
6542 mAppToken.updateReportedVisibilityLocked();
6543 }
6544
6545 return false;
6546 }
6547
6548 void finishExit() {
6549 if (DEBUG_ANIM) Log.v(
6550 TAG, "finishExit in " + this
6551 + ": exiting=" + mExiting
6552 + " remove=" + mRemoveOnExit
6553 + " windowAnimating=" + isWindowAnimating());
Romain Guy06882f82009-06-10 13:36:04 -07006554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006555 final int N = mChildWindows.size();
6556 for (int i=0; i<N; i++) {
6557 ((WindowState)mChildWindows.get(i)).finishExit();
6558 }
Romain Guy06882f82009-06-10 13:36:04 -07006559
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006560 if (!mExiting) {
6561 return;
6562 }
Romain Guy06882f82009-06-10 13:36:04 -07006563
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006564 if (isWindowAnimating()) {
6565 return;
6566 }
6567
6568 if (localLOGV) Log.v(
6569 TAG, "Exit animation finished in " + this
6570 + ": remove=" + mRemoveOnExit);
6571 if (mSurface != null) {
6572 mDestroySurface.add(this);
6573 mDestroying = true;
6574 if (SHOW_TRANSACTIONS) Log.i(
6575 TAG, " SURFACE " + mSurface + ": HIDE (finishExit)");
6576 try {
6577 mSurface.hide();
6578 } catch (RuntimeException e) {
6579 Log.w(TAG, "Error hiding surface in " + this, e);
6580 }
6581 mLastHidden = true;
6582 mKeyWaiter.releasePendingPointerLocked(mSession);
6583 }
6584 mExiting = false;
6585 if (mRemoveOnExit) {
6586 mPendingRemove.add(this);
6587 mRemoveOnExit = false;
6588 }
6589 }
Romain Guy06882f82009-06-10 13:36:04 -07006590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006591 boolean isIdentityMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
6592 if (dsdx < .99999f || dsdx > 1.00001f) return false;
6593 if (dtdy < .99999f || dtdy > 1.00001f) return false;
6594 if (dtdx < -.000001f || dtdx > .000001f) return false;
6595 if (dsdy < -.000001f || dsdy > .000001f) return false;
6596 return true;
6597 }
Romain Guy06882f82009-06-10 13:36:04 -07006598
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006599 void computeShownFrameLocked() {
6600 final boolean selfTransformation = mHasLocalTransformation;
6601 Transformation attachedTransformation =
6602 (mAttachedWindow != null && mAttachedWindow.mHasLocalTransformation)
6603 ? mAttachedWindow.mTransformation : null;
6604 Transformation appTransformation =
6605 (mAppToken != null && mAppToken.hasTransformation)
6606 ? mAppToken.transformation : null;
6607 if (selfTransformation || attachedTransformation != null
6608 || appTransformation != null) {
Romain Guy06882f82009-06-10 13:36:04 -07006609 // cache often used attributes locally
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006610 final Rect frame = mFrame;
6611 final float tmpFloats[] = mTmpFloats;
6612 final Matrix tmpMatrix = mTmpMatrix;
6613
6614 // Compute the desired transformation.
6615 tmpMatrix.setTranslate(frame.left, frame.top);
6616 if (selfTransformation) {
6617 tmpMatrix.preConcat(mTransformation.getMatrix());
6618 }
6619 if (attachedTransformation != null) {
6620 tmpMatrix.preConcat(attachedTransformation.getMatrix());
6621 }
6622 if (appTransformation != null) {
6623 tmpMatrix.preConcat(appTransformation.getMatrix());
6624 }
6625
6626 // "convert" it into SurfaceFlinger's format
6627 // (a 2x2 matrix + an offset)
6628 // Here we must not transform the position of the surface
6629 // since it is already included in the transformation.
6630 //Log.i(TAG, "Transform: " + matrix);
Romain Guy06882f82009-06-10 13:36:04 -07006631
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006632 tmpMatrix.getValues(tmpFloats);
6633 mDsDx = tmpFloats[Matrix.MSCALE_X];
6634 mDtDx = tmpFloats[Matrix.MSKEW_X];
6635 mDsDy = tmpFloats[Matrix.MSKEW_Y];
6636 mDtDy = tmpFloats[Matrix.MSCALE_Y];
6637 int x = (int)tmpFloats[Matrix.MTRANS_X];
6638 int y = (int)tmpFloats[Matrix.MTRANS_Y];
6639 int w = frame.width();
6640 int h = frame.height();
6641 mShownFrame.set(x, y, x+w, y+h);
6642
6643 // Now set the alpha... but because our current hardware
6644 // can't do alpha transformation on a non-opaque surface,
6645 // turn it off if we are running an animation that is also
6646 // transforming since it is more important to have that
6647 // animation be smooth.
6648 mShownAlpha = mAlpha;
6649 if (!mLimitedAlphaCompositing
6650 || (!PixelFormat.formatHasAlpha(mAttrs.format)
6651 || (isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
6652 && x == frame.left && y == frame.top))) {
6653 //Log.i(TAG, "Applying alpha transform");
6654 if (selfTransformation) {
6655 mShownAlpha *= mTransformation.getAlpha();
6656 }
6657 if (attachedTransformation != null) {
6658 mShownAlpha *= attachedTransformation.getAlpha();
6659 }
6660 if (appTransformation != null) {
6661 mShownAlpha *= appTransformation.getAlpha();
6662 }
6663 } else {
6664 //Log.i(TAG, "Not applying alpha transform");
6665 }
Romain Guy06882f82009-06-10 13:36:04 -07006666
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006667 if (localLOGV) Log.v(
6668 TAG, "Continuing animation in " + this +
6669 ": " + mShownFrame +
6670 ", alpha=" + mTransformation.getAlpha());
6671 return;
6672 }
Romain Guy06882f82009-06-10 13:36:04 -07006673
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006674 mShownFrame.set(mFrame);
6675 mShownAlpha = mAlpha;
6676 mDsDx = 1;
6677 mDtDx = 0;
6678 mDsDy = 0;
6679 mDtDy = 1;
6680 }
Romain Guy06882f82009-06-10 13:36:04 -07006681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006682 /**
6683 * Is this window visible? It is not visible if there is no
6684 * surface, or we are in the process of running an exit animation
6685 * that will remove the surface, or its app token has been hidden.
6686 */
6687 public boolean isVisibleLw() {
6688 final AppWindowToken atoken = mAppToken;
6689 return mSurface != null && mPolicyVisibility && !mAttachedHidden
6690 && (atoken == null || !atoken.hiddenRequested)
6691 && !mExiting && !mDestroying;
6692 }
6693
6694 /**
6695 * Is this window visible, ignoring its app token? It is not visible
6696 * if there is no surface, or we are in the process of running an exit animation
6697 * that will remove the surface.
6698 */
6699 public boolean isWinVisibleLw() {
6700 final AppWindowToken atoken = mAppToken;
6701 return mSurface != null && mPolicyVisibility && !mAttachedHidden
6702 && (atoken == null || !atoken.hiddenRequested || atoken.animating)
6703 && !mExiting && !mDestroying;
6704 }
6705
6706 /**
6707 * The same as isVisible(), but follows the current hidden state of
6708 * the associated app token, not the pending requested hidden state.
6709 */
6710 boolean isVisibleNow() {
6711 return mSurface != null && mPolicyVisibility && !mAttachedHidden
The Android Open Source Project10592532009-03-18 17:39:46 -07006712 && !mRootToken.hidden && !mExiting && !mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006713 }
6714
6715 /**
6716 * Same as isVisible(), but we also count it as visible between the
6717 * call to IWindowSession.add() and the first relayout().
6718 */
6719 boolean isVisibleOrAdding() {
6720 final AppWindowToken atoken = mAppToken;
6721 return (mSurface != null
6722 || (!mRelayoutCalled && mViewVisibility == View.VISIBLE))
6723 && mPolicyVisibility && !mAttachedHidden
6724 && (atoken == null || !atoken.hiddenRequested)
6725 && !mExiting && !mDestroying;
6726 }
6727
6728 /**
6729 * Is this window currently on-screen? It is on-screen either if it
6730 * is visible or it is currently running an animation before no longer
6731 * being visible.
6732 */
6733 boolean isOnScreen() {
6734 final AppWindowToken atoken = mAppToken;
6735 if (atoken != null) {
6736 return mSurface != null && mPolicyVisibility && !mDestroying
6737 && ((!mAttachedHidden && !atoken.hiddenRequested)
6738 || mAnimating || atoken.animating);
6739 } else {
6740 return mSurface != null && mPolicyVisibility && !mDestroying
6741 && (!mAttachedHidden || mAnimating);
6742 }
6743 }
Romain Guy06882f82009-06-10 13:36:04 -07006744
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006745 /**
6746 * Like isOnScreen(), but we don't return true if the window is part
6747 * of a transition that has not yet been started.
6748 */
6749 boolean isReadyForDisplay() {
6750 final AppWindowToken atoken = mAppToken;
6751 final boolean animating = atoken != null ? atoken.animating : false;
6752 return mSurface != null && mPolicyVisibility && !mDestroying
The Android Open Source Project10592532009-03-18 17:39:46 -07006753 && ((!mAttachedHidden && !mRootToken.hidden)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006754 || mAnimating || animating);
6755 }
6756
6757 /** Is the window or its container currently animating? */
6758 boolean isAnimating() {
6759 final WindowState attached = mAttachedWindow;
6760 final AppWindowToken atoken = mAppToken;
6761 return mAnimation != null
6762 || (attached != null && attached.mAnimation != null)
Romain Guy06882f82009-06-10 13:36:04 -07006763 || (atoken != null &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006764 (atoken.animation != null
6765 || atoken.inPendingTransaction));
6766 }
6767
6768 /** Is this window currently animating? */
6769 boolean isWindowAnimating() {
6770 return mAnimation != null;
6771 }
6772
6773 /**
6774 * Like isOnScreen, but returns false if the surface hasn't yet
6775 * been drawn.
6776 */
6777 public boolean isDisplayedLw() {
6778 final AppWindowToken atoken = mAppToken;
6779 return mSurface != null && mPolicyVisibility && !mDestroying
6780 && !mDrawPending && !mCommitDrawPending
6781 && ((!mAttachedHidden &&
6782 (atoken == null || !atoken.hiddenRequested))
6783 || mAnimating);
6784 }
6785
6786 public boolean fillsScreenLw(int screenWidth, int screenHeight,
6787 boolean shownFrame, boolean onlyOpaque) {
6788 if (mSurface == null) {
6789 return false;
6790 }
6791 if (mAppToken != null && !mAppToken.appFullscreen) {
6792 return false;
6793 }
6794 if (onlyOpaque && mAttrs.format != PixelFormat.OPAQUE) {
6795 return false;
6796 }
6797 final Rect frame = shownFrame ? mShownFrame : mFrame;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07006798
6799 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
6800 return frame.left <= mCompatibleScreenFrame.left &&
6801 frame.top <= mCompatibleScreenFrame.top &&
6802 frame.right >= mCompatibleScreenFrame.right &&
6803 frame.bottom >= mCompatibleScreenFrame.bottom;
6804 } else {
6805 return frame.left <= 0 && frame.top <= 0
6806 && frame.right >= screenWidth
6807 && frame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006808 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006809 }
Romain Guy06882f82009-06-10 13:36:04 -07006810
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006811 /**
6812 * Return true if the window is opaque and fully drawn.
6813 */
6814 boolean isOpaqueDrawn() {
6815 return mAttrs.format == PixelFormat.OPAQUE && mSurface != null
6816 && mAnimation == null && !mDrawPending && !mCommitDrawPending;
6817 }
6818
6819 boolean needsBackgroundFiller(int screenWidth, int screenHeight) {
6820 return
6821 // only if the application is requesting compatible window
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07006822 (mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0 &&
6823 // only if it's visible
6824 mHasDrawn && mViewVisibility == View.VISIBLE &&
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07006825 // and only if the application fills the compatible screen
6826 mFrame.left <= mCompatibleScreenFrame.left &&
6827 mFrame.top <= mCompatibleScreenFrame.top &&
6828 mFrame.right >= mCompatibleScreenFrame.right &&
6829 mFrame.bottom >= mCompatibleScreenFrame.bottom &&
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07006830 // and starting window do not need background filler
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07006831 mAttrs.type != mAttrs.TYPE_APPLICATION_STARTING;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006832 }
6833
6834 boolean isFullscreen(int screenWidth, int screenHeight) {
6835 return mFrame.left <= 0 && mFrame.top <= 0 &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006836 mFrame.right >= screenWidth && mFrame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006837 }
6838
6839 void removeLocked() {
6840 if (mAttachedWindow != null) {
6841 mAttachedWindow.mChildWindows.remove(this);
6842 }
6843 destroySurfaceLocked();
6844 mSession.windowRemovedLocked();
6845 try {
6846 mClient.asBinder().unlinkToDeath(mDeathRecipient, 0);
6847 } catch (RuntimeException e) {
6848 // Ignore if it has already been removed (usually because
6849 // we are doing this as part of processing a death note.)
6850 }
6851 }
6852
6853 private class DeathRecipient implements IBinder.DeathRecipient {
6854 public void binderDied() {
6855 try {
6856 synchronized(mWindowMap) {
6857 WindowState win = windowForClientLocked(mSession, mClient);
6858 Log.i(TAG, "WIN DEATH: " + win);
6859 if (win != null) {
6860 removeWindowLocked(mSession, win);
6861 }
6862 }
6863 } catch (IllegalArgumentException ex) {
6864 // This will happen if the window has already been
6865 // removed.
6866 }
6867 }
6868 }
6869
6870 /** Returns true if this window desires key events. */
6871 public final boolean canReceiveKeys() {
6872 return isVisibleOrAdding()
6873 && (mViewVisibility == View.VISIBLE)
6874 && ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0);
6875 }
6876
6877 public boolean hasDrawnLw() {
6878 return mHasDrawn;
6879 }
6880
6881 public boolean showLw(boolean doAnimation) {
6882 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim) {
6883 mPolicyVisibility = true;
6884 mPolicyVisibilityAfterAnim = true;
6885 if (doAnimation) {
6886 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_ENTER, true);
6887 }
6888 requestAnimationLocked(0);
6889 return true;
6890 }
6891 return false;
6892 }
6893
6894 public boolean hideLw(boolean doAnimation) {
6895 boolean current = doAnimation ? mPolicyVisibilityAfterAnim
6896 : mPolicyVisibility;
6897 if (current) {
6898 if (doAnimation) {
6899 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_EXIT, false);
6900 if (mAnimation == null) {
6901 doAnimation = false;
6902 }
6903 }
6904 if (doAnimation) {
6905 mPolicyVisibilityAfterAnim = false;
6906 } else {
6907 mPolicyVisibilityAfterAnim = false;
6908 mPolicyVisibility = false;
6909 }
6910 requestAnimationLocked(0);
6911 return true;
6912 }
6913 return false;
6914 }
6915
6916 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006917 StringBuilder sb = new StringBuilder(64);
Romain Guy06882f82009-06-10 13:36:04 -07006918
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006919 pw.print(prefix); pw.print("mSession="); pw.print(mSession);
6920 pw.print(" mClient="); pw.println(mClient.asBinder());
6921 pw.print(prefix); pw.print("mAttrs="); pw.println(mAttrs);
6922 if (mAttachedWindow != null || mLayoutAttached) {
6923 pw.print(prefix); pw.print("mAttachedWindow="); pw.print(mAttachedWindow);
6924 pw.print(" mLayoutAttached="); pw.println(mLayoutAttached);
6925 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006926 if (mIsImWindow || mIsWallpaper || mIsFloatingLayer) {
6927 pw.print(prefix); pw.print("mIsImWindow="); pw.print(mIsImWindow);
6928 pw.print(" mIsWallpaper="); pw.print(mIsWallpaper);
6929 pw.print(" mIsFloatingLayer="); pw.println(mIsFloatingLayer);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006930 }
6931 pw.print(prefix); pw.print("mBaseLayer="); pw.print(mBaseLayer);
6932 pw.print(" mSubLayer="); pw.print(mSubLayer);
6933 pw.print(" mAnimLayer="); pw.print(mLayer); pw.print("+");
6934 pw.print((mTargetAppToken != null ? mTargetAppToken.animLayerAdjustment
6935 : (mAppToken != null ? mAppToken.animLayerAdjustment : 0)));
6936 pw.print("="); pw.print(mAnimLayer);
6937 pw.print(" mLastLayer="); pw.println(mLastLayer);
6938 if (mSurface != null) {
6939 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
6940 }
6941 pw.print(prefix); pw.print("mToken="); pw.println(mToken);
6942 pw.print(prefix); pw.print("mRootToken="); pw.println(mRootToken);
6943 if (mAppToken != null) {
6944 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
6945 }
6946 if (mTargetAppToken != null) {
6947 pw.print(prefix); pw.print("mTargetAppToken="); pw.println(mTargetAppToken);
6948 }
6949 pw.print(prefix); pw.print("mViewVisibility=0x");
6950 pw.print(Integer.toHexString(mViewVisibility));
6951 pw.print(" mLastHidden="); pw.print(mLastHidden);
6952 pw.print(" mHaveFrame="); pw.println(mHaveFrame);
6953 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim || mAttachedHidden) {
6954 pw.print(prefix); pw.print("mPolicyVisibility=");
6955 pw.print(mPolicyVisibility);
6956 pw.print(" mPolicyVisibilityAfterAnim=");
6957 pw.print(mPolicyVisibilityAfterAnim);
6958 pw.print(" mAttachedHidden="); pw.println(mAttachedHidden);
6959 }
6960 pw.print(prefix); pw.print("Requested w="); pw.print(mRequestedWidth);
6961 pw.print(" h="); pw.print(mRequestedHeight);
6962 pw.print(" x="); pw.print(mReqXPos);
6963 pw.print(" y="); pw.println(mReqYPos);
6964 pw.print(prefix); pw.print("mGivenContentInsets=");
6965 mGivenContentInsets.printShortString(pw);
6966 pw.print(" mGivenVisibleInsets=");
6967 mGivenVisibleInsets.printShortString(pw);
6968 pw.println();
6969 if (mTouchableInsets != 0 || mGivenInsetsPending) {
6970 pw.print(prefix); pw.print("mTouchableInsets="); pw.print(mTouchableInsets);
6971 pw.print(" mGivenInsetsPending="); pw.println(mGivenInsetsPending);
6972 }
6973 pw.print(prefix); pw.print("mShownFrame=");
6974 mShownFrame.printShortString(pw);
6975 pw.print(" last="); mLastShownFrame.printShortString(pw);
6976 pw.println();
6977 pw.print(prefix); pw.print("mFrame="); mFrame.printShortString(pw);
6978 pw.print(" last="); mLastFrame.printShortString(pw);
6979 pw.println();
6980 pw.print(prefix); pw.print("mContainingFrame=");
6981 mContainingFrame.printShortString(pw);
6982 pw.print(" mDisplayFrame=");
6983 mDisplayFrame.printShortString(pw);
6984 pw.println();
6985 pw.print(prefix); pw.print("mContentFrame="); mContentFrame.printShortString(pw);
6986 pw.print(" mVisibleFrame="); mVisibleFrame.printShortString(pw);
6987 pw.println();
6988 pw.print(prefix); pw.print("mContentInsets="); mContentInsets.printShortString(pw);
6989 pw.print(" last="); mLastContentInsets.printShortString(pw);
6990 pw.print(" mVisibleInsets="); mVisibleInsets.printShortString(pw);
6991 pw.print(" last="); mLastVisibleInsets.printShortString(pw);
6992 pw.println();
6993 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
6994 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
6995 pw.print(" mAlpha="); pw.print(mAlpha);
6996 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
6997 }
6998 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
6999 || mAnimation != null) {
7000 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
7001 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
7002 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
7003 pw.print(" mAnimation="); pw.println(mAnimation);
7004 }
7005 if (mHasTransformation || mHasLocalTransformation) {
7006 pw.print(prefix); pw.print("XForm: has=");
7007 pw.print(mHasTransformation);
7008 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
7009 pw.print(" "); mTransformation.printShortString(pw);
7010 pw.println();
7011 }
7012 pw.print(prefix); pw.print("mDrawPending="); pw.print(mDrawPending);
7013 pw.print(" mCommitDrawPending="); pw.print(mCommitDrawPending);
7014 pw.print(" mReadyToShow="); pw.print(mReadyToShow);
7015 pw.print(" mHasDrawn="); pw.println(mHasDrawn);
7016 if (mExiting || mRemoveOnExit || mDestroying || mRemoved) {
7017 pw.print(prefix); pw.print("mExiting="); pw.print(mExiting);
7018 pw.print(" mRemoveOnExit="); pw.print(mRemoveOnExit);
7019 pw.print(" mDestroying="); pw.print(mDestroying);
7020 pw.print(" mRemoved="); pw.println(mRemoved);
7021 }
7022 if (mOrientationChanging || mAppFreezing) {
7023 pw.print(prefix); pw.print("mOrientationChanging=");
7024 pw.print(mOrientationChanging);
7025 pw.print(" mAppFreezing="); pw.println(mAppFreezing);
7026 }
Mitsuru Oshima589cebe2009-07-22 20:38:58 -07007027 if (mHScale != 1 || mVScale != 1) {
7028 pw.print(prefix); pw.print("mHScale="); pw.print(mHScale);
7029 pw.print(" mVScale="); pw.println(mVScale);
7030 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007031 }
7032
7033 @Override
7034 public String toString() {
7035 return "Window{"
7036 + Integer.toHexString(System.identityHashCode(this))
7037 + " " + mAttrs.getTitle() + " paused=" + mToken.paused + "}";
7038 }
7039 }
Romain Guy06882f82009-06-10 13:36:04 -07007040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007041 // -------------------------------------------------------------
7042 // Window Token State
7043 // -------------------------------------------------------------
7044
7045 class WindowToken {
7046 // The actual token.
7047 final IBinder token;
7048
7049 // The type of window this token is for, as per WindowManager.LayoutParams.
7050 final int windowType;
Romain Guy06882f82009-06-10 13:36:04 -07007051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007052 // Set if this token was explicitly added by a client, so should
7053 // not be removed when all windows are removed.
7054 final boolean explicit;
Romain Guy06882f82009-06-10 13:36:04 -07007055
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007056 // For printing.
7057 String stringName;
Romain Guy06882f82009-06-10 13:36:04 -07007058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007059 // If this is an AppWindowToken, this is non-null.
7060 AppWindowToken appWindowToken;
Romain Guy06882f82009-06-10 13:36:04 -07007061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007062 // All of the windows associated with this token.
7063 final ArrayList<WindowState> windows = new ArrayList<WindowState>();
7064
7065 // Is key dispatching paused for this token?
7066 boolean paused = false;
7067
7068 // Should this token's windows be hidden?
7069 boolean hidden;
7070
7071 // Temporary for finding which tokens no longer have visible windows.
7072 boolean hasVisible;
7073
7074 WindowToken(IBinder _token, int type, boolean _explicit) {
7075 token = _token;
7076 windowType = type;
7077 explicit = _explicit;
7078 }
7079
7080 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007081 pw.print(prefix); pw.print("token="); pw.println(token);
7082 pw.print(prefix); pw.print("windows="); pw.println(windows);
7083 pw.print(prefix); pw.print("windowType="); pw.print(windowType);
7084 pw.print(" hidden="); pw.print(hidden);
7085 pw.print(" hasVisible="); pw.println(hasVisible);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007086 }
7087
7088 @Override
7089 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007090 if (stringName == null) {
7091 StringBuilder sb = new StringBuilder();
7092 sb.append("WindowToken{");
7093 sb.append(Integer.toHexString(System.identityHashCode(this)));
7094 sb.append(" token="); sb.append(token); sb.append('}');
7095 stringName = sb.toString();
7096 }
7097 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007098 }
7099 };
7100
7101 class AppWindowToken extends WindowToken {
7102 // Non-null only for application tokens.
7103 final IApplicationToken appToken;
7104
7105 // All of the windows and child windows that are included in this
7106 // application token. Note this list is NOT sorted!
7107 final ArrayList<WindowState> allAppWindows = new ArrayList<WindowState>();
7108
7109 int groupId = -1;
7110 boolean appFullscreen;
7111 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Romain Guy06882f82009-06-10 13:36:04 -07007112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007113 // These are used for determining when all windows associated with
7114 // an activity have been drawn, so they can be made visible together
7115 // at the same time.
7116 int lastTransactionSequence = mTransactionSequence-1;
7117 int numInterestingWindows;
7118 int numDrawnWindows;
7119 boolean inPendingTransaction;
7120 boolean allDrawn;
Romain Guy06882f82009-06-10 13:36:04 -07007121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007122 // Is this token going to be hidden in a little while? If so, it
7123 // won't be taken into account for setting the screen orientation.
7124 boolean willBeHidden;
Romain Guy06882f82009-06-10 13:36:04 -07007125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007126 // Is this window's surface needed? This is almost like hidden, except
7127 // it will sometimes be true a little earlier: when the token has
7128 // been shown, but is still waiting for its app transition to execute
7129 // before making its windows shown.
7130 boolean hiddenRequested;
Romain Guy06882f82009-06-10 13:36:04 -07007131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007132 // Have we told the window clients to hide themselves?
7133 boolean clientHidden;
Romain Guy06882f82009-06-10 13:36:04 -07007134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007135 // Last visibility state we reported to the app token.
7136 boolean reportedVisible;
7137
7138 // Set to true when the token has been removed from the window mgr.
7139 boolean removed;
7140
7141 // Have we been asked to have this token keep the screen frozen?
7142 boolean freezingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07007143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007144 boolean animating;
7145 Animation animation;
7146 boolean hasTransformation;
7147 final Transformation transformation = new Transformation();
Romain Guy06882f82009-06-10 13:36:04 -07007148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007149 // Offset to the window of all layers in the token, for use by
7150 // AppWindowToken animations.
7151 int animLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -07007152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007153 // Information about an application starting window if displayed.
7154 StartingData startingData;
7155 WindowState startingWindow;
7156 View startingView;
7157 boolean startingDisplayed;
7158 boolean startingMoved;
7159 boolean firstWindowDrawn;
7160
7161 AppWindowToken(IApplicationToken _token) {
7162 super(_token.asBinder(),
7163 WindowManager.LayoutParams.TYPE_APPLICATION, true);
7164 appWindowToken = this;
7165 appToken = _token;
7166 }
Romain Guy06882f82009-06-10 13:36:04 -07007167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007168 public void setAnimation(Animation anim) {
7169 if (localLOGV) Log.v(
7170 TAG, "Setting animation in " + this + ": " + anim);
7171 animation = anim;
7172 animating = false;
7173 anim.restrictDuration(MAX_ANIMATION_DURATION);
7174 anim.scaleCurrentDuration(mTransitionAnimationScale);
7175 int zorder = anim.getZAdjustment();
7176 int adj = 0;
7177 if (zorder == Animation.ZORDER_TOP) {
7178 adj = TYPE_LAYER_OFFSET;
7179 } else if (zorder == Animation.ZORDER_BOTTOM) {
7180 adj = -TYPE_LAYER_OFFSET;
7181 }
Romain Guy06882f82009-06-10 13:36:04 -07007182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007183 if (animLayerAdjustment != adj) {
7184 animLayerAdjustment = adj;
7185 updateLayers();
7186 }
7187 }
Romain Guy06882f82009-06-10 13:36:04 -07007188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007189 public void setDummyAnimation() {
7190 if (animation == null) {
7191 if (localLOGV) Log.v(
7192 TAG, "Setting dummy animation in " + this);
7193 animation = sDummyAnimation;
7194 }
7195 }
7196
7197 public void clearAnimation() {
7198 if (animation != null) {
7199 animation = null;
7200 animating = true;
7201 }
7202 }
Romain Guy06882f82009-06-10 13:36:04 -07007203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007204 void updateLayers() {
7205 final int N = allAppWindows.size();
7206 final int adj = animLayerAdjustment;
7207 for (int i=0; i<N; i++) {
7208 WindowState w = allAppWindows.get(i);
7209 w.mAnimLayer = w.mLayer + adj;
7210 if (DEBUG_LAYERS) Log.v(TAG, "Updating layer " + w + ": "
7211 + w.mAnimLayer);
7212 if (w == mInputMethodTarget) {
7213 setInputMethodAnimLayerAdjustment(adj);
7214 }
7215 }
7216 }
Romain Guy06882f82009-06-10 13:36:04 -07007217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007218 void sendAppVisibilityToClients() {
7219 final int N = allAppWindows.size();
7220 for (int i=0; i<N; i++) {
7221 WindowState win = allAppWindows.get(i);
7222 if (win == startingWindow && clientHidden) {
7223 // Don't hide the starting window.
7224 continue;
7225 }
7226 try {
7227 if (DEBUG_VISIBILITY) Log.v(TAG,
7228 "Setting visibility of " + win + ": " + (!clientHidden));
7229 win.mClient.dispatchAppVisibility(!clientHidden);
7230 } catch (RemoteException e) {
7231 }
7232 }
7233 }
Romain Guy06882f82009-06-10 13:36:04 -07007234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007235 void showAllWindowsLocked() {
7236 final int NW = allAppWindows.size();
7237 for (int i=0; i<NW; i++) {
7238 WindowState w = allAppWindows.get(i);
7239 if (DEBUG_VISIBILITY) Log.v(TAG,
7240 "performing show on: " + w);
7241 w.performShowLocked();
7242 }
7243 }
Romain Guy06882f82009-06-10 13:36:04 -07007244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007245 // This must be called while inside a transaction.
7246 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
7247 if (!mDisplayFrozen) {
7248 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07007249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007250 if (animation == sDummyAnimation) {
7251 // This guy is going to animate, but not yet. For now count
7252 // it is not animating for purposes of scheduling transactions;
7253 // when it is really time to animate, this will be set to
7254 // a real animation and the next call will execute normally.
7255 return false;
7256 }
Romain Guy06882f82009-06-10 13:36:04 -07007257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007258 if ((allDrawn || animating || startingDisplayed) && animation != null) {
7259 if (!animating) {
7260 if (DEBUG_ANIM) Log.v(
7261 TAG, "Starting animation in " + this +
7262 " @ " + currentTime + ": dw=" + dw + " dh=" + dh
7263 + " scale=" + mTransitionAnimationScale
7264 + " allDrawn=" + allDrawn + " animating=" + animating);
7265 animation.initialize(dw, dh, dw, dh);
7266 animation.setStartTime(currentTime);
7267 animating = true;
7268 }
7269 transformation.clear();
7270 final boolean more = animation.getTransformation(
7271 currentTime, transformation);
7272 if (DEBUG_ANIM) Log.v(
7273 TAG, "Stepped animation in " + this +
7274 ": more=" + more + ", xform=" + transformation);
7275 if (more) {
7276 // we're done!
7277 hasTransformation = true;
7278 return true;
7279 }
7280 if (DEBUG_ANIM) Log.v(
7281 TAG, "Finished animation in " + this +
7282 " @ " + currentTime);
7283 animation = null;
7284 }
7285 } else if (animation != null) {
7286 // If the display is frozen, and there is a pending animation,
7287 // clear it and make sure we run the cleanup code.
7288 animating = true;
7289 animation = null;
7290 }
7291
7292 hasTransformation = false;
Romain Guy06882f82009-06-10 13:36:04 -07007293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007294 if (!animating) {
7295 return false;
7296 }
7297
7298 clearAnimation();
7299 animating = false;
7300 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == this) {
7301 moveInputMethodWindowsIfNeededLocked(true);
7302 }
Romain Guy06882f82009-06-10 13:36:04 -07007303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007304 if (DEBUG_ANIM) Log.v(
7305 TAG, "Animation done in " + this
7306 + ": reportedVisible=" + reportedVisible);
7307
7308 transformation.clear();
7309 if (animLayerAdjustment != 0) {
7310 animLayerAdjustment = 0;
7311 updateLayers();
7312 }
Romain Guy06882f82009-06-10 13:36:04 -07007313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007314 final int N = windows.size();
7315 for (int i=0; i<N; i++) {
7316 ((WindowState)windows.get(i)).finishExit();
7317 }
7318 updateReportedVisibilityLocked();
Romain Guy06882f82009-06-10 13:36:04 -07007319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007320 return false;
7321 }
7322
7323 void updateReportedVisibilityLocked() {
7324 if (appToken == null) {
7325 return;
7326 }
Romain Guy06882f82009-06-10 13:36:04 -07007327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007328 int numInteresting = 0;
7329 int numVisible = 0;
7330 boolean nowGone = true;
Romain Guy06882f82009-06-10 13:36:04 -07007331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007332 if (DEBUG_VISIBILITY) Log.v(TAG, "Update reported visibility: " + this);
7333 final int N = allAppWindows.size();
7334 for (int i=0; i<N; i++) {
7335 WindowState win = allAppWindows.get(i);
7336 if (win == startingWindow || win.mAppFreezing) {
7337 continue;
7338 }
7339 if (DEBUG_VISIBILITY) {
7340 Log.v(TAG, "Win " + win + ": isDisplayed="
7341 + win.isDisplayedLw()
7342 + ", isAnimating=" + win.isAnimating());
7343 if (!win.isDisplayedLw()) {
7344 Log.v(TAG, "Not displayed: s=" + win.mSurface
7345 + " pv=" + win.mPolicyVisibility
7346 + " dp=" + win.mDrawPending
7347 + " cdp=" + win.mCommitDrawPending
7348 + " ah=" + win.mAttachedHidden
7349 + " th="
7350 + (win.mAppToken != null
7351 ? win.mAppToken.hiddenRequested : false)
7352 + " a=" + win.mAnimating);
7353 }
7354 }
7355 numInteresting++;
7356 if (win.isDisplayedLw()) {
7357 if (!win.isAnimating()) {
7358 numVisible++;
7359 }
7360 nowGone = false;
7361 } else if (win.isAnimating()) {
7362 nowGone = false;
7363 }
7364 }
Romain Guy06882f82009-06-10 13:36:04 -07007365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007366 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
7367 if (DEBUG_VISIBILITY) Log.v(TAG, "VIS " + this + ": interesting="
7368 + numInteresting + " visible=" + numVisible);
7369 if (nowVisible != reportedVisible) {
7370 if (DEBUG_VISIBILITY) Log.v(
7371 TAG, "Visibility changed in " + this
7372 + ": vis=" + nowVisible);
7373 reportedVisible = nowVisible;
7374 Message m = mH.obtainMessage(
7375 H.REPORT_APPLICATION_TOKEN_WINDOWS,
7376 nowVisible ? 1 : 0,
7377 nowGone ? 1 : 0,
7378 this);
7379 mH.sendMessage(m);
7380 }
7381 }
Romain Guy06882f82009-06-10 13:36:04 -07007382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007383 void dump(PrintWriter pw, String prefix) {
7384 super.dump(pw, prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007385 if (appToken != null) {
7386 pw.print(prefix); pw.println("app=true");
7387 }
7388 if (allAppWindows.size() > 0) {
7389 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
7390 }
7391 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
7392 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
7393 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
7394 pw.print(" clientHidden="); pw.print(clientHidden);
7395 pw.print(" willBeHidden="); pw.print(willBeHidden);
7396 pw.print(" reportedVisible="); pw.println(reportedVisible);
7397 if (paused || freezingScreen) {
7398 pw.print(prefix); pw.print("paused="); pw.print(paused);
7399 pw.print(" freezingScreen="); pw.println(freezingScreen);
7400 }
7401 if (numInterestingWindows != 0 || numDrawnWindows != 0
7402 || inPendingTransaction || allDrawn) {
7403 pw.print(prefix); pw.print("numInterestingWindows=");
7404 pw.print(numInterestingWindows);
7405 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
7406 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
7407 pw.print(" allDrawn="); pw.println(allDrawn);
7408 }
7409 if (animating || animation != null) {
7410 pw.print(prefix); pw.print("animating="); pw.print(animating);
7411 pw.print(" animation="); pw.println(animation);
7412 }
7413 if (animLayerAdjustment != 0) {
7414 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
7415 }
7416 if (hasTransformation) {
7417 pw.print(prefix); pw.print("hasTransformation="); pw.print(hasTransformation);
7418 pw.print(" transformation="); transformation.printShortString(pw);
7419 pw.println();
7420 }
7421 if (startingData != null || removed || firstWindowDrawn) {
7422 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
7423 pw.print(" removed="); pw.print(removed);
7424 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
7425 }
7426 if (startingWindow != null || startingView != null
7427 || startingDisplayed || startingMoved) {
7428 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
7429 pw.print(" startingView="); pw.print(startingView);
7430 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
7431 pw.print(" startingMoved"); pw.println(startingMoved);
7432 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007433 }
7434
7435 @Override
7436 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007437 if (stringName == null) {
7438 StringBuilder sb = new StringBuilder();
7439 sb.append("AppWindowToken{");
7440 sb.append(Integer.toHexString(System.identityHashCode(this)));
7441 sb.append(" token="); sb.append(token); sb.append('}');
7442 stringName = sb.toString();
7443 }
7444 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007445 }
7446 }
Romain Guy06882f82009-06-10 13:36:04 -07007447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007448 public static WindowManager.LayoutParams findAnimations(
7449 ArrayList<AppWindowToken> order,
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007450 ArrayList<AppWindowToken> openingTokenList1,
7451 ArrayList<AppWindowToken> closingTokenList2) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007452 // We need to figure out which animation to use...
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007453
7454 // First, check if there is a compatible window in opening/closing
7455 // apps, and use it if exists.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007456 WindowManager.LayoutParams animParams = null;
7457 int animSrc = 0;
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007458 animParams = findCompatibleWindowParams(openingTokenList1);
7459 if (animParams == null) {
7460 animParams = findCompatibleWindowParams(closingTokenList2);
7461 }
7462 if (animParams != null) {
7463 return animParams;
7464 }
7465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007466 //Log.i(TAG, "Looking for animations...");
7467 for (int i=order.size()-1; i>=0; i--) {
7468 AppWindowToken wtoken = order.get(i);
7469 //Log.i(TAG, "Token " + wtoken + " with " + wtoken.windows.size() + " windows");
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007470 if (openingTokenList1.contains(wtoken) || closingTokenList2.contains(wtoken)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007471 int j = wtoken.windows.size();
7472 while (j > 0) {
7473 j--;
7474 WindowState win = wtoken.windows.get(j);
7475 //Log.i(TAG, "Window " + win + ": type=" + win.mAttrs.type);
7476 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
7477 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
7478 //Log.i(TAG, "Found base or application window, done!");
7479 if (wtoken.appFullscreen) {
7480 return win.mAttrs;
7481 }
7482 if (animSrc < 2) {
7483 animParams = win.mAttrs;
7484 animSrc = 2;
7485 }
7486 } else if (animSrc < 1 && win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION) {
7487 //Log.i(TAG, "Found normal window, we may use this...");
7488 animParams = win.mAttrs;
7489 animSrc = 1;
7490 }
7491 }
7492 }
7493 }
Romain Guy06882f82009-06-10 13:36:04 -07007494
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007495 return animParams;
7496 }
Romain Guy06882f82009-06-10 13:36:04 -07007497
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007498 private static LayoutParams findCompatibleWindowParams(ArrayList<AppWindowToken> tokenList) {
7499 for (int appCount = tokenList.size() - 1; appCount >= 0; appCount--) {
7500 AppWindowToken wtoken = tokenList.get(appCount);
7501 // Just checking one window is sufficient as all windows have the compatible flag
7502 // if the application is in compatibility mode.
7503 if (wtoken.windows.size() > 0) {
7504 WindowManager.LayoutParams params = wtoken.windows.get(0).mAttrs;
7505 if ((params.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
7506 return params;
7507 }
7508 }
7509 }
7510 return null;
7511 }
7512
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007513 // -------------------------------------------------------------
7514 // DummyAnimation
7515 // -------------------------------------------------------------
7516
7517 // This is an animation that does nothing: it just immediately finishes
7518 // itself every time it is called. It is used as a stub animation in cases
7519 // where we want to synchronize multiple things that may be animating.
7520 static final class DummyAnimation extends Animation {
7521 public boolean getTransformation(long currentTime, Transformation outTransformation) {
7522 return false;
7523 }
7524 }
7525 static final Animation sDummyAnimation = new DummyAnimation();
Romain Guy06882f82009-06-10 13:36:04 -07007526
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007527 // -------------------------------------------------------------
7528 // Async Handler
7529 // -------------------------------------------------------------
7530
7531 static final class StartingData {
7532 final String pkg;
7533 final int theme;
7534 final CharSequence nonLocalizedLabel;
7535 final int labelRes;
7536 final int icon;
Romain Guy06882f82009-06-10 13:36:04 -07007537
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007538 StartingData(String _pkg, int _theme, CharSequence _nonLocalizedLabel,
7539 int _labelRes, int _icon) {
7540 pkg = _pkg;
7541 theme = _theme;
7542 nonLocalizedLabel = _nonLocalizedLabel;
7543 labelRes = _labelRes;
7544 icon = _icon;
7545 }
7546 }
7547
7548 private final class H extends Handler {
7549 public static final int REPORT_FOCUS_CHANGE = 2;
7550 public static final int REPORT_LOSING_FOCUS = 3;
7551 public static final int ANIMATE = 4;
7552 public static final int ADD_STARTING = 5;
7553 public static final int REMOVE_STARTING = 6;
7554 public static final int FINISHED_STARTING = 7;
7555 public static final int REPORT_APPLICATION_TOKEN_WINDOWS = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007556 public static final int WINDOW_FREEZE_TIMEOUT = 11;
7557 public static final int HOLD_SCREEN_CHANGED = 12;
7558 public static final int APP_TRANSITION_TIMEOUT = 13;
7559 public static final int PERSIST_ANIMATION_SCALE = 14;
7560 public static final int FORCE_GC = 15;
7561 public static final int ENABLE_SCREEN = 16;
7562 public static final int APP_FREEZE_TIMEOUT = 17;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07007563 public static final int COMPUTE_AND_SEND_NEW_CONFIGURATION = 18;
Romain Guy06882f82009-06-10 13:36:04 -07007564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007565 private Session mLastReportedHold;
Romain Guy06882f82009-06-10 13:36:04 -07007566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007567 public H() {
7568 }
Romain Guy06882f82009-06-10 13:36:04 -07007569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007570 @Override
7571 public void handleMessage(Message msg) {
7572 switch (msg.what) {
7573 case REPORT_FOCUS_CHANGE: {
7574 WindowState lastFocus;
7575 WindowState newFocus;
Romain Guy06882f82009-06-10 13:36:04 -07007576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007577 synchronized(mWindowMap) {
7578 lastFocus = mLastFocus;
7579 newFocus = mCurrentFocus;
7580 if (lastFocus == newFocus) {
7581 // Focus is not changing, so nothing to do.
7582 return;
7583 }
7584 mLastFocus = newFocus;
7585 //Log.i(TAG, "Focus moving from " + lastFocus
7586 // + " to " + newFocus);
7587 if (newFocus != null && lastFocus != null
7588 && !newFocus.isDisplayedLw()) {
7589 //Log.i(TAG, "Delaying loss of focus...");
7590 mLosingFocus.add(lastFocus);
7591 lastFocus = null;
7592 }
7593 }
7594
7595 if (lastFocus != newFocus) {
7596 //System.out.println("Changing focus from " + lastFocus
7597 // + " to " + newFocus);
7598 if (newFocus != null) {
7599 try {
7600 //Log.i(TAG, "Gaining focus: " + newFocus);
7601 newFocus.mClient.windowFocusChanged(true, mInTouchMode);
7602 } catch (RemoteException e) {
7603 // Ignore if process has died.
7604 }
7605 }
7606
7607 if (lastFocus != null) {
7608 try {
7609 //Log.i(TAG, "Losing focus: " + lastFocus);
7610 lastFocus.mClient.windowFocusChanged(false, mInTouchMode);
7611 } catch (RemoteException e) {
7612 // Ignore if process has died.
7613 }
7614 }
7615 }
7616 } break;
7617
7618 case REPORT_LOSING_FOCUS: {
7619 ArrayList<WindowState> losers;
Romain Guy06882f82009-06-10 13:36:04 -07007620
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007621 synchronized(mWindowMap) {
7622 losers = mLosingFocus;
7623 mLosingFocus = new ArrayList<WindowState>();
7624 }
7625
7626 final int N = losers.size();
7627 for (int i=0; i<N; i++) {
7628 try {
7629 //Log.i(TAG, "Losing delayed focus: " + losers.get(i));
7630 losers.get(i).mClient.windowFocusChanged(false, mInTouchMode);
7631 } catch (RemoteException e) {
7632 // Ignore if process has died.
7633 }
7634 }
7635 } break;
7636
7637 case ANIMATE: {
7638 synchronized(mWindowMap) {
7639 mAnimationPending = false;
7640 performLayoutAndPlaceSurfacesLocked();
7641 }
7642 } break;
7643
7644 case ADD_STARTING: {
7645 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
7646 final StartingData sd = wtoken.startingData;
7647
7648 if (sd == null) {
7649 // Animation has been canceled... do nothing.
7650 return;
7651 }
Romain Guy06882f82009-06-10 13:36:04 -07007652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007653 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Add starting "
7654 + wtoken + ": pkg=" + sd.pkg);
Romain Guy06882f82009-06-10 13:36:04 -07007655
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007656 View view = null;
7657 try {
7658 view = mPolicy.addStartingWindow(
7659 wtoken.token, sd.pkg,
7660 sd.theme, sd.nonLocalizedLabel, sd.labelRes,
7661 sd.icon);
7662 } catch (Exception e) {
7663 Log.w(TAG, "Exception when adding starting window", e);
7664 }
7665
7666 if (view != null) {
7667 boolean abort = false;
7668
7669 synchronized(mWindowMap) {
7670 if (wtoken.removed || wtoken.startingData == null) {
7671 // If the window was successfully added, then
7672 // we need to remove it.
7673 if (wtoken.startingWindow != null) {
7674 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
7675 "Aborted starting " + wtoken
7676 + ": removed=" + wtoken.removed
7677 + " startingData=" + wtoken.startingData);
7678 wtoken.startingWindow = null;
7679 wtoken.startingData = null;
7680 abort = true;
7681 }
7682 } else {
7683 wtoken.startingView = view;
7684 }
7685 if (DEBUG_STARTING_WINDOW && !abort) Log.v(TAG,
7686 "Added starting " + wtoken
7687 + ": startingWindow="
7688 + wtoken.startingWindow + " startingView="
7689 + wtoken.startingView);
7690 }
7691
7692 if (abort) {
7693 try {
7694 mPolicy.removeStartingWindow(wtoken.token, view);
7695 } catch (Exception e) {
7696 Log.w(TAG, "Exception when removing starting window", e);
7697 }
7698 }
7699 }
7700 } break;
7701
7702 case REMOVE_STARTING: {
7703 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
7704 IBinder token = null;
7705 View view = null;
7706 synchronized (mWindowMap) {
7707 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Remove starting "
7708 + wtoken + ": startingWindow="
7709 + wtoken.startingWindow + " startingView="
7710 + wtoken.startingView);
7711 if (wtoken.startingWindow != null) {
7712 view = wtoken.startingView;
7713 token = wtoken.token;
7714 wtoken.startingData = null;
7715 wtoken.startingView = null;
7716 wtoken.startingWindow = null;
7717 }
7718 }
7719 if (view != null) {
7720 try {
7721 mPolicy.removeStartingWindow(token, view);
7722 } catch (Exception e) {
7723 Log.w(TAG, "Exception when removing starting window", e);
7724 }
7725 }
7726 } break;
7727
7728 case FINISHED_STARTING: {
7729 IBinder token = null;
7730 View view = null;
7731 while (true) {
7732 synchronized (mWindowMap) {
7733 final int N = mFinishedStarting.size();
7734 if (N <= 0) {
7735 break;
7736 }
7737 AppWindowToken wtoken = mFinishedStarting.remove(N-1);
7738
7739 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
7740 "Finished starting " + wtoken
7741 + ": startingWindow=" + wtoken.startingWindow
7742 + " startingView=" + wtoken.startingView);
7743
7744 if (wtoken.startingWindow == null) {
7745 continue;
7746 }
7747
7748 view = wtoken.startingView;
7749 token = wtoken.token;
7750 wtoken.startingData = null;
7751 wtoken.startingView = null;
7752 wtoken.startingWindow = null;
7753 }
7754
7755 try {
7756 mPolicy.removeStartingWindow(token, view);
7757 } catch (Exception e) {
7758 Log.w(TAG, "Exception when removing starting window", e);
7759 }
7760 }
7761 } break;
7762
7763 case REPORT_APPLICATION_TOKEN_WINDOWS: {
7764 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
7765
7766 boolean nowVisible = msg.arg1 != 0;
7767 boolean nowGone = msg.arg2 != 0;
7768
7769 try {
7770 if (DEBUG_VISIBILITY) Log.v(
7771 TAG, "Reporting visible in " + wtoken
7772 + " visible=" + nowVisible
7773 + " gone=" + nowGone);
7774 if (nowVisible) {
7775 wtoken.appToken.windowsVisible();
7776 } else {
7777 wtoken.appToken.windowsGone();
7778 }
7779 } catch (RemoteException ex) {
7780 }
7781 } break;
Romain Guy06882f82009-06-10 13:36:04 -07007782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007783 case WINDOW_FREEZE_TIMEOUT: {
7784 synchronized (mWindowMap) {
7785 Log.w(TAG, "Window freeze timeout expired.");
7786 int i = mWindows.size();
7787 while (i > 0) {
7788 i--;
7789 WindowState w = (WindowState)mWindows.get(i);
7790 if (w.mOrientationChanging) {
7791 w.mOrientationChanging = false;
7792 Log.w(TAG, "Force clearing orientation change: " + w);
7793 }
7794 }
7795 performLayoutAndPlaceSurfacesLocked();
7796 }
7797 break;
7798 }
Romain Guy06882f82009-06-10 13:36:04 -07007799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007800 case HOLD_SCREEN_CHANGED: {
7801 Session oldHold;
7802 Session newHold;
7803 synchronized (mWindowMap) {
7804 oldHold = mLastReportedHold;
7805 newHold = (Session)msg.obj;
7806 mLastReportedHold = newHold;
7807 }
Romain Guy06882f82009-06-10 13:36:04 -07007808
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007809 if (oldHold != newHold) {
7810 try {
7811 if (oldHold != null) {
7812 mBatteryStats.noteStopWakelock(oldHold.mUid,
7813 "window",
7814 BatteryStats.WAKE_TYPE_WINDOW);
7815 }
7816 if (newHold != null) {
7817 mBatteryStats.noteStartWakelock(newHold.mUid,
7818 "window",
7819 BatteryStats.WAKE_TYPE_WINDOW);
7820 }
7821 } catch (RemoteException e) {
7822 }
7823 }
7824 break;
7825 }
Romain Guy06882f82009-06-10 13:36:04 -07007826
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007827 case APP_TRANSITION_TIMEOUT: {
7828 synchronized (mWindowMap) {
7829 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
7830 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
7831 "*** APP TRANSITION TIMEOUT");
7832 mAppTransitionReady = true;
7833 mAppTransitionTimeout = true;
7834 performLayoutAndPlaceSurfacesLocked();
7835 }
7836 }
7837 break;
7838 }
Romain Guy06882f82009-06-10 13:36:04 -07007839
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007840 case PERSIST_ANIMATION_SCALE: {
7841 Settings.System.putFloat(mContext.getContentResolver(),
7842 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
7843 Settings.System.putFloat(mContext.getContentResolver(),
7844 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
7845 break;
7846 }
Romain Guy06882f82009-06-10 13:36:04 -07007847
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007848 case FORCE_GC: {
7849 synchronized(mWindowMap) {
7850 if (mAnimationPending) {
7851 // If we are animating, don't do the gc now but
7852 // delay a bit so we don't interrupt the animation.
7853 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
7854 2000);
7855 return;
7856 }
7857 // If we are currently rotating the display, it will
7858 // schedule a new message when done.
7859 if (mDisplayFrozen) {
7860 return;
7861 }
7862 mFreezeGcPending = 0;
7863 }
7864 Runtime.getRuntime().gc();
7865 break;
7866 }
Romain Guy06882f82009-06-10 13:36:04 -07007867
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007868 case ENABLE_SCREEN: {
7869 performEnableScreen();
7870 break;
7871 }
Romain Guy06882f82009-06-10 13:36:04 -07007872
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007873 case APP_FREEZE_TIMEOUT: {
7874 synchronized (mWindowMap) {
7875 Log.w(TAG, "App freeze timeout expired.");
7876 int i = mAppTokens.size();
7877 while (i > 0) {
7878 i--;
7879 AppWindowToken tok = mAppTokens.get(i);
7880 if (tok.freezingScreen) {
7881 Log.w(TAG, "Force clearing freeze: " + tok);
7882 unsetAppFreezingScreenLocked(tok, true, true);
7883 }
7884 }
7885 }
7886 break;
7887 }
Romain Guy06882f82009-06-10 13:36:04 -07007888
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07007889 case COMPUTE_AND_SEND_NEW_CONFIGURATION: {
Dianne Hackborncfaef692009-06-15 14:24:44 -07007890 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07007891 sendNewConfiguration();
7892 }
7893 break;
7894 }
Romain Guy06882f82009-06-10 13:36:04 -07007895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007896 }
7897 }
7898 }
7899
7900 // -------------------------------------------------------------
7901 // IWindowManager API
7902 // -------------------------------------------------------------
7903
7904 public IWindowSession openSession(IInputMethodClient client,
7905 IInputContext inputContext) {
7906 if (client == null) throw new IllegalArgumentException("null client");
7907 if (inputContext == null) throw new IllegalArgumentException("null inputContext");
7908 return new Session(client, inputContext);
7909 }
7910
7911 public boolean inputMethodClientHasFocus(IInputMethodClient client) {
7912 synchronized (mWindowMap) {
7913 // The focus for the client is the window immediately below
7914 // where we would place the input method window.
7915 int idx = findDesiredInputMethodWindowIndexLocked(false);
7916 WindowState imFocus;
7917 if (idx > 0) {
7918 imFocus = (WindowState)mWindows.get(idx-1);
7919 if (imFocus != null) {
7920 if (imFocus.mSession.mClient != null &&
7921 imFocus.mSession.mClient.asBinder() == client.asBinder()) {
7922 return true;
7923 }
7924 }
7925 }
7926 }
7927 return false;
7928 }
Romain Guy06882f82009-06-10 13:36:04 -07007929
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007930 // -------------------------------------------------------------
7931 // Internals
7932 // -------------------------------------------------------------
7933
7934 final WindowState windowForClientLocked(Session session, IWindow client) {
7935 return windowForClientLocked(session, client.asBinder());
7936 }
Romain Guy06882f82009-06-10 13:36:04 -07007937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007938 final WindowState windowForClientLocked(Session session, IBinder client) {
7939 WindowState win = mWindowMap.get(client);
7940 if (localLOGV) Log.v(
7941 TAG, "Looking up client " + client + ": " + win);
7942 if (win == null) {
7943 RuntimeException ex = new RuntimeException();
7944 Log.w(TAG, "Requested window " + client + " does not exist", ex);
7945 return null;
7946 }
7947 if (session != null && win.mSession != session) {
7948 RuntimeException ex = new RuntimeException();
7949 Log.w(TAG, "Requested window " + client + " is in session " +
7950 win.mSession + ", not " + session, ex);
7951 return null;
7952 }
7953
7954 return win;
7955 }
7956
7957 private final void assignLayersLocked() {
7958 int N = mWindows.size();
7959 int curBaseLayer = 0;
7960 int curLayer = 0;
7961 int i;
Romain Guy06882f82009-06-10 13:36:04 -07007962
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007963 for (i=0; i<N; i++) {
7964 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007965 if (w.mBaseLayer == curBaseLayer || w.mIsFloatingLayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007966 curLayer += WINDOW_LAYER_MULTIPLIER;
7967 w.mLayer = curLayer;
7968 } else {
7969 curBaseLayer = curLayer = w.mBaseLayer;
7970 w.mLayer = curLayer;
7971 }
7972 if (w.mTargetAppToken != null) {
7973 w.mAnimLayer = w.mLayer + w.mTargetAppToken.animLayerAdjustment;
7974 } else if (w.mAppToken != null) {
7975 w.mAnimLayer = w.mLayer + w.mAppToken.animLayerAdjustment;
7976 } else {
7977 w.mAnimLayer = w.mLayer;
7978 }
7979 if (w.mIsImWindow) {
7980 w.mAnimLayer += mInputMethodAnimLayerAdjustment;
7981 }
7982 if (DEBUG_LAYERS) Log.v(TAG, "Assign layer " + w + ": "
7983 + w.mAnimLayer);
7984 //System.out.println(
7985 // "Assigned layer " + curLayer + " to " + w.mClient.asBinder());
7986 }
7987 }
7988
7989 private boolean mInLayout = false;
7990 private final void performLayoutAndPlaceSurfacesLocked() {
7991 if (mInLayout) {
Dave Bortcfe65242009-04-09 14:51:04 -07007992 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007993 throw new RuntimeException("Recursive call!");
7994 }
7995 Log.w(TAG, "performLayoutAndPlaceSurfacesLocked called while in layout");
7996 return;
7997 }
7998
7999 boolean recoveringMemory = false;
8000 if (mForceRemoves != null) {
8001 recoveringMemory = true;
8002 // Wait a little it for things to settle down, and off we go.
8003 for (int i=0; i<mForceRemoves.size(); i++) {
8004 WindowState ws = mForceRemoves.get(i);
8005 Log.i(TAG, "Force removing: " + ws);
8006 removeWindowInnerLocked(ws.mSession, ws);
8007 }
8008 mForceRemoves = null;
8009 Log.w(TAG, "Due to memory failure, waiting a bit for next layout");
8010 Object tmp = new Object();
8011 synchronized (tmp) {
8012 try {
8013 tmp.wait(250);
8014 } catch (InterruptedException e) {
8015 }
8016 }
8017 }
Romain Guy06882f82009-06-10 13:36:04 -07008018
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008019 mInLayout = true;
8020 try {
8021 performLayoutAndPlaceSurfacesLockedInner(recoveringMemory);
Romain Guy06882f82009-06-10 13:36:04 -07008022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008023 int i = mPendingRemove.size()-1;
8024 if (i >= 0) {
8025 while (i >= 0) {
8026 WindowState w = mPendingRemove.get(i);
8027 removeWindowInnerLocked(w.mSession, w);
8028 i--;
8029 }
8030 mPendingRemove.clear();
8031
8032 mInLayout = false;
8033 assignLayersLocked();
8034 mLayoutNeeded = true;
8035 performLayoutAndPlaceSurfacesLocked();
8036
8037 } else {
8038 mInLayout = false;
8039 if (mLayoutNeeded) {
8040 requestAnimationLocked(0);
8041 }
8042 }
8043 } catch (RuntimeException e) {
8044 mInLayout = false;
8045 Log.e(TAG, "Unhandled exception while layout out windows", e);
8046 }
8047 }
8048
8049 private final void performLayoutLockedInner() {
8050 final int dw = mDisplay.getWidth();
8051 final int dh = mDisplay.getHeight();
8052
8053 final int N = mWindows.size();
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008054 int repeats = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008055 int i;
8056
8057 // FIRST LOOP: Perform a layout, if needed.
Romain Guy06882f82009-06-10 13:36:04 -07008058
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008059 while (mLayoutNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008060 mPolicy.beginLayoutLw(dw, dh);
8061
8062 // First perform layout of any root windows (not attached
8063 // to another window).
8064 int topAttached = -1;
8065 for (i = N-1; i >= 0; i--) {
8066 WindowState win = (WindowState) mWindows.get(i);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008067
8068 // Don't do layout of a window if it is not visible, or
8069 // soon won't be visible, to avoid wasting time and funky
8070 // changes while a window is animating away.
8071 final AppWindowToken atoken = win.mAppToken;
8072 final boolean gone = win.mViewVisibility == View.GONE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008073 || !win.mRelayoutCalled
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008074 || win.mRootToken.hidden
8075 || (atoken != null && atoken.hiddenRequested)
8076 || !win.mPolicyVisibility
8077 || win.mAttachedHidden
8078 || win.mExiting || win.mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008079
8080 // If this view is GONE, then skip it -- keep the current
8081 // frame, and let the caller know so they can ignore it
8082 // if they want. (We do the normal layout for INVISIBLE
8083 // windows, since that means "perform layout as normal,
8084 // just don't display").
8085 if (!gone || !win.mHaveFrame) {
8086 if (!win.mLayoutAttached) {
8087 mPolicy.layoutWindowLw(win, win.mAttrs, null);
8088 } else {
8089 if (topAttached < 0) topAttached = i;
8090 }
8091 }
8092 }
Romain Guy06882f82009-06-10 13:36:04 -07008093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008094 // Now perform layout of attached windows, which usually
8095 // depend on the position of the window they are attached to.
8096 // XXX does not deal with windows that are attached to windows
8097 // that are themselves attached.
8098 for (i = topAttached; i >= 0; i--) {
8099 WindowState win = (WindowState) mWindows.get(i);
8100
8101 // If this view is GONE, then skip it -- keep the current
8102 // frame, and let the caller know so they can ignore it
8103 // if they want. (We do the normal layout for INVISIBLE
8104 // windows, since that means "perform layout as normal,
8105 // just don't display").
8106 if (win.mLayoutAttached) {
8107 if ((win.mViewVisibility != View.GONE && win.mRelayoutCalled)
8108 || !win.mHaveFrame) {
8109 mPolicy.layoutWindowLw(win, win.mAttrs, win.mAttachedWindow);
8110 }
8111 }
8112 }
8113
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008114 if (!mPolicy.finishLayoutLw()) {
8115 mLayoutNeeded = false;
8116 } else if (repeats > 2) {
8117 Log.w(TAG, "Layout repeat aborted after too many iterations");
8118 mLayoutNeeded = false;
8119 } else {
8120 repeats++;
8121 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008122 }
8123 }
Romain Guy06882f82009-06-10 13:36:04 -07008124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008125 private final void performLayoutAndPlaceSurfacesLockedInner(
8126 boolean recoveringMemory) {
8127 final long currentTime = SystemClock.uptimeMillis();
8128 final int dw = mDisplay.getWidth();
8129 final int dh = mDisplay.getHeight();
8130
8131 final int N = mWindows.size();
8132 int i;
8133
8134 // FIRST LOOP: Perform a layout, if needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008135 performLayoutLockedInner();
Romain Guy06882f82009-06-10 13:36:04 -07008136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008137 if (mFxSession == null) {
8138 mFxSession = new SurfaceSession();
8139 }
Romain Guy06882f82009-06-10 13:36:04 -07008140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008141 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
8142
8143 // Initialize state of exiting tokens.
8144 for (i=mExitingTokens.size()-1; i>=0; i--) {
8145 mExitingTokens.get(i).hasVisible = false;
8146 }
8147
8148 // Initialize state of exiting applications.
8149 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
8150 mExitingAppTokens.get(i).hasVisible = false;
8151 }
8152
8153 // SECOND LOOP: Execute animations and update visibility of windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008154 boolean orientationChangeComplete = true;
8155 Session holdScreen = null;
8156 float screenBrightness = -1;
8157 boolean focusDisplayed = false;
8158 boolean animating = false;
8159
8160 Surface.openTransaction();
8161 try {
8162 boolean restart;
8163
8164 do {
8165 final int transactionSequence = ++mTransactionSequence;
8166
8167 // Update animations of all applications, including those
8168 // associated with exiting/removed apps
8169 boolean tokensAnimating = false;
8170 final int NAT = mAppTokens.size();
8171 for (i=0; i<NAT; i++) {
8172 if (mAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
8173 tokensAnimating = true;
8174 }
8175 }
8176 final int NEAT = mExitingAppTokens.size();
8177 for (i=0; i<NEAT; i++) {
8178 if (mExitingAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
8179 tokensAnimating = true;
8180 }
8181 }
8182
8183 animating = tokensAnimating;
8184 restart = false;
8185
8186 boolean tokenMayBeDrawn = false;
8187
8188 mPolicy.beginAnimationLw(dw, dh);
8189
8190 for (i=N-1; i>=0; i--) {
8191 WindowState w = (WindowState)mWindows.get(i);
8192
8193 final WindowManager.LayoutParams attrs = w.mAttrs;
8194
8195 if (w.mSurface != null) {
8196 // Execute animation.
8197 w.commitFinishDrawingLocked(currentTime);
8198 if (w.stepAnimationLocked(currentTime, dw, dh)) {
8199 animating = true;
8200 //w.dump(" ");
8201 }
8202
8203 mPolicy.animatingWindowLw(w, attrs);
8204 }
8205
8206 final AppWindowToken atoken = w.mAppToken;
8207 if (atoken != null && (!atoken.allDrawn || atoken.freezingScreen)) {
8208 if (atoken.lastTransactionSequence != transactionSequence) {
8209 atoken.lastTransactionSequence = transactionSequence;
8210 atoken.numInterestingWindows = atoken.numDrawnWindows = 0;
8211 atoken.startingDisplayed = false;
8212 }
8213 if ((w.isOnScreen() || w.mAttrs.type
8214 == WindowManager.LayoutParams.TYPE_BASE_APPLICATION)
8215 && !w.mExiting && !w.mDestroying) {
8216 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
8217 Log.v(TAG, "Eval win " + w + ": isDisplayed="
8218 + w.isDisplayedLw()
8219 + ", isAnimating=" + w.isAnimating());
8220 if (!w.isDisplayedLw()) {
8221 Log.v(TAG, "Not displayed: s=" + w.mSurface
8222 + " pv=" + w.mPolicyVisibility
8223 + " dp=" + w.mDrawPending
8224 + " cdp=" + w.mCommitDrawPending
8225 + " ah=" + w.mAttachedHidden
8226 + " th=" + atoken.hiddenRequested
8227 + " a=" + w.mAnimating);
8228 }
8229 }
8230 if (w != atoken.startingWindow) {
8231 if (!atoken.freezingScreen || !w.mAppFreezing) {
8232 atoken.numInterestingWindows++;
8233 if (w.isDisplayedLw()) {
8234 atoken.numDrawnWindows++;
8235 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Log.v(TAG,
8236 "tokenMayBeDrawn: " + atoken
8237 + " freezingScreen=" + atoken.freezingScreen
8238 + " mAppFreezing=" + w.mAppFreezing);
8239 tokenMayBeDrawn = true;
8240 }
8241 }
8242 } else if (w.isDisplayedLw()) {
8243 atoken.startingDisplayed = true;
8244 }
8245 }
8246 } else if (w.mReadyToShow) {
8247 w.performShowLocked();
8248 }
8249 }
8250
8251 if (mPolicy.finishAnimationLw()) {
8252 restart = true;
8253 }
8254
8255 if (tokenMayBeDrawn) {
8256 // See if any windows have been drawn, so they (and others
8257 // associated with them) can now be shown.
8258 final int NT = mTokenList.size();
8259 for (i=0; i<NT; i++) {
8260 AppWindowToken wtoken = mTokenList.get(i).appWindowToken;
8261 if (wtoken == null) {
8262 continue;
8263 }
8264 if (wtoken.freezingScreen) {
8265 int numInteresting = wtoken.numInterestingWindows;
8266 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
8267 if (DEBUG_VISIBILITY) Log.v(TAG,
8268 "allDrawn: " + wtoken
8269 + " interesting=" + numInteresting
8270 + " drawn=" + wtoken.numDrawnWindows);
8271 wtoken.showAllWindowsLocked();
8272 unsetAppFreezingScreenLocked(wtoken, false, true);
8273 orientationChangeComplete = true;
8274 }
8275 } else if (!wtoken.allDrawn) {
8276 int numInteresting = wtoken.numInterestingWindows;
8277 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
8278 if (DEBUG_VISIBILITY) Log.v(TAG,
8279 "allDrawn: " + wtoken
8280 + " interesting=" + numInteresting
8281 + " drawn=" + wtoken.numDrawnWindows);
8282 wtoken.allDrawn = true;
8283 restart = true;
8284
8285 // We can now show all of the drawn windows!
8286 if (!mOpeningApps.contains(wtoken)) {
8287 wtoken.showAllWindowsLocked();
8288 }
8289 }
8290 }
8291 }
8292 }
8293
8294 // If we are ready to perform an app transition, check through
8295 // all of the app tokens to be shown and see if they are ready
8296 // to go.
8297 if (mAppTransitionReady) {
8298 int NN = mOpeningApps.size();
8299 boolean goodToGo = true;
8300 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8301 "Checking " + NN + " opening apps (frozen="
8302 + mDisplayFrozen + " timeout="
8303 + mAppTransitionTimeout + ")...");
8304 if (!mDisplayFrozen && !mAppTransitionTimeout) {
8305 // If the display isn't frozen, wait to do anything until
8306 // all of the apps are ready. Otherwise just go because
8307 // we'll unfreeze the display when everyone is ready.
8308 for (i=0; i<NN && goodToGo; i++) {
8309 AppWindowToken wtoken = mOpeningApps.get(i);
8310 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8311 "Check opening app" + wtoken + ": allDrawn="
8312 + wtoken.allDrawn + " startingDisplayed="
8313 + wtoken.startingDisplayed);
8314 if (!wtoken.allDrawn && !wtoken.startingDisplayed
8315 && !wtoken.startingMoved) {
8316 goodToGo = false;
8317 }
8318 }
8319 }
8320 if (goodToGo) {
8321 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "**** GOOD TO GO");
8322 int transit = mNextAppTransition;
8323 if (mSkipAppTransitionAnimation) {
8324 transit = WindowManagerPolicy.TRANSIT_NONE;
8325 }
8326 mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
8327 mAppTransitionReady = false;
8328 mAppTransitionTimeout = false;
8329 mStartingIconInTransition = false;
8330 mSkipAppTransitionAnimation = false;
8331
8332 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
8333
8334 // We need to figure out which animation to use...
8335 WindowManager.LayoutParams lp = findAnimations(mAppTokens,
8336 mOpeningApps, mClosingApps);
8337
8338 NN = mOpeningApps.size();
8339 for (i=0; i<NN; i++) {
8340 AppWindowToken wtoken = mOpeningApps.get(i);
8341 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8342 "Now opening app" + wtoken);
8343 wtoken.reportedVisible = false;
8344 wtoken.inPendingTransaction = false;
8345 setTokenVisibilityLocked(wtoken, lp, true, transit, false);
8346 wtoken.updateReportedVisibilityLocked();
8347 wtoken.showAllWindowsLocked();
8348 }
8349 NN = mClosingApps.size();
8350 for (i=0; i<NN; i++) {
8351 AppWindowToken wtoken = mClosingApps.get(i);
8352 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8353 "Now closing app" + wtoken);
8354 wtoken.inPendingTransaction = false;
8355 setTokenVisibilityLocked(wtoken, lp, false, transit, false);
8356 wtoken.updateReportedVisibilityLocked();
8357 // Force the allDrawn flag, because we want to start
8358 // this guy's animations regardless of whether it's
8359 // gotten drawn.
8360 wtoken.allDrawn = true;
8361 }
8362
8363 mOpeningApps.clear();
8364 mClosingApps.clear();
8365
8366 // This has changed the visibility of windows, so perform
8367 // a new layout to get them all up-to-date.
8368 mLayoutNeeded = true;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07008369 adjustWallpaperWindowsLocked();
Dianne Hackborn20583ff2009-07-27 21:51:05 -07008370 if (!moveInputMethodWindowsIfNeededLocked(true)) {
8371 assignLayersLocked();
8372 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008373 performLayoutLockedInner();
8374 updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES);
8375
8376 restart = true;
8377 }
8378 }
8379 } while (restart);
8380
8381 // THIRD LOOP: Update the surfaces of all windows.
8382
8383 final boolean someoneLosingFocus = mLosingFocus.size() != 0;
8384
8385 boolean obscured = false;
8386 boolean blurring = false;
8387 boolean dimming = false;
8388 boolean covered = false;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07008389 boolean syswin = false;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008390 boolean backgroundFillerShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008391
8392 for (i=N-1; i>=0; i--) {
8393 WindowState w = (WindowState)mWindows.get(i);
8394
8395 boolean displayed = false;
8396 final WindowManager.LayoutParams attrs = w.mAttrs;
8397 final int attrFlags = attrs.flags;
8398
8399 if (w.mSurface != null) {
8400 w.computeShownFrameLocked();
8401 if (localLOGV) Log.v(
8402 TAG, "Placing surface #" + i + " " + w.mSurface
8403 + ": new=" + w.mShownFrame + ", old="
8404 + w.mLastShownFrame);
8405
8406 boolean resize;
8407 int width, height;
8408 if ((w.mAttrs.flags & w.mAttrs.FLAG_SCALED) != 0) {
8409 resize = w.mLastRequestedWidth != w.mRequestedWidth ||
8410 w.mLastRequestedHeight != w.mRequestedHeight;
8411 // for a scaled surface, we just want to use
8412 // the requested size.
8413 width = w.mRequestedWidth;
8414 height = w.mRequestedHeight;
8415 w.mLastRequestedWidth = width;
8416 w.mLastRequestedHeight = height;
8417 w.mLastShownFrame.set(w.mShownFrame);
8418 try {
8419 w.mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
8420 } catch (RuntimeException e) {
8421 Log.w(TAG, "Error positioning surface in " + w, e);
8422 if (!recoveringMemory) {
8423 reclaimSomeSurfaceMemoryLocked(w, "position");
8424 }
8425 }
8426 } else {
8427 resize = !w.mLastShownFrame.equals(w.mShownFrame);
8428 width = w.mShownFrame.width();
8429 height = w.mShownFrame.height();
8430 w.mLastShownFrame.set(w.mShownFrame);
8431 if (resize) {
8432 if (SHOW_TRANSACTIONS) Log.i(
8433 TAG, " SURFACE " + w.mSurface + ": ("
8434 + w.mShownFrame.left + ","
8435 + w.mShownFrame.top + ") ("
8436 + w.mShownFrame.width() + "x"
8437 + w.mShownFrame.height() + ")");
8438 }
8439 }
8440
8441 if (resize) {
8442 if (width < 1) width = 1;
8443 if (height < 1) height = 1;
8444 if (w.mSurface != null) {
8445 try {
8446 w.mSurface.setSize(width, height);
8447 w.mSurface.setPosition(w.mShownFrame.left,
8448 w.mShownFrame.top);
8449 } catch (RuntimeException e) {
8450 // If something goes wrong with the surface (such
8451 // as running out of memory), don't take down the
8452 // entire system.
8453 Log.e(TAG, "Failure updating surface of " + w
8454 + "size=(" + width + "x" + height
8455 + "), pos=(" + w.mShownFrame.left
8456 + "," + w.mShownFrame.top + ")", e);
8457 if (!recoveringMemory) {
8458 reclaimSomeSurfaceMemoryLocked(w, "size");
8459 }
8460 }
8461 }
8462 }
8463 if (!w.mAppFreezing) {
8464 w.mContentInsetsChanged =
8465 !w.mLastContentInsets.equals(w.mContentInsets);
8466 w.mVisibleInsetsChanged =
8467 !w.mLastVisibleInsets.equals(w.mVisibleInsets);
Romain Guy06882f82009-06-10 13:36:04 -07008468 if (!w.mLastFrame.equals(w.mFrame)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008469 || w.mContentInsetsChanged
8470 || w.mVisibleInsetsChanged) {
8471 w.mLastFrame.set(w.mFrame);
8472 w.mLastContentInsets.set(w.mContentInsets);
8473 w.mLastVisibleInsets.set(w.mVisibleInsets);
8474 // If the orientation is changing, then we need to
8475 // hold off on unfreezing the display until this
8476 // window has been redrawn; to do that, we need
8477 // to go through the process of getting informed
8478 // by the application when it has finished drawing.
8479 if (w.mOrientationChanging) {
8480 if (DEBUG_ORIENTATION) Log.v(TAG,
8481 "Orientation start waiting for draw in "
8482 + w + ", surface " + w.mSurface);
8483 w.mDrawPending = true;
8484 w.mCommitDrawPending = false;
8485 w.mReadyToShow = false;
8486 if (w.mAppToken != null) {
8487 w.mAppToken.allDrawn = false;
8488 }
8489 }
Romain Guy06882f82009-06-10 13:36:04 -07008490 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008491 "Resizing window " + w + " to " + w.mFrame);
8492 mResizingWindows.add(w);
8493 } else if (w.mOrientationChanging) {
8494 if (!w.mDrawPending && !w.mCommitDrawPending) {
8495 if (DEBUG_ORIENTATION) Log.v(TAG,
8496 "Orientation not waiting for draw in "
8497 + w + ", surface " + w.mSurface);
8498 w.mOrientationChanging = false;
8499 }
8500 }
8501 }
8502
8503 if (w.mAttachedHidden) {
8504 if (!w.mLastHidden) {
8505 //dump();
8506 w.mLastHidden = true;
8507 if (SHOW_TRANSACTIONS) Log.i(
8508 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-attached)");
8509 if (w.mSurface != null) {
8510 try {
8511 w.mSurface.hide();
8512 } catch (RuntimeException e) {
8513 Log.w(TAG, "Exception hiding surface in " + w);
8514 }
8515 }
8516 mKeyWaiter.releasePendingPointerLocked(w.mSession);
8517 }
8518 // If we are waiting for this window to handle an
8519 // orientation change, well, it is hidden, so
8520 // doesn't really matter. Note that this does
8521 // introduce a potential glitch if the window
8522 // becomes unhidden before it has drawn for the
8523 // new orientation.
8524 if (w.mOrientationChanging) {
8525 w.mOrientationChanging = false;
8526 if (DEBUG_ORIENTATION) Log.v(TAG,
8527 "Orientation change skips hidden " + w);
8528 }
8529 } else if (!w.isReadyForDisplay()) {
8530 if (!w.mLastHidden) {
8531 //dump();
8532 w.mLastHidden = true;
8533 if (SHOW_TRANSACTIONS) Log.i(
8534 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-ready)");
8535 if (w.mSurface != null) {
8536 try {
8537 w.mSurface.hide();
8538 } catch (RuntimeException e) {
8539 Log.w(TAG, "Exception exception hiding surface in " + w);
8540 }
8541 }
8542 mKeyWaiter.releasePendingPointerLocked(w.mSession);
8543 }
8544 // If we are waiting for this window to handle an
8545 // orientation change, well, it is hidden, so
8546 // doesn't really matter. Note that this does
8547 // introduce a potential glitch if the window
8548 // becomes unhidden before it has drawn for the
8549 // new orientation.
8550 if (w.mOrientationChanging) {
8551 w.mOrientationChanging = false;
8552 if (DEBUG_ORIENTATION) Log.v(TAG,
8553 "Orientation change skips hidden " + w);
8554 }
8555 } else if (w.mLastLayer != w.mAnimLayer
8556 || w.mLastAlpha != w.mShownAlpha
8557 || w.mLastDsDx != w.mDsDx
8558 || w.mLastDtDx != w.mDtDx
8559 || w.mLastDsDy != w.mDsDy
8560 || w.mLastDtDy != w.mDtDy
8561 || w.mLastHScale != w.mHScale
8562 || w.mLastVScale != w.mVScale
8563 || w.mLastHidden) {
8564 displayed = true;
8565 w.mLastAlpha = w.mShownAlpha;
8566 w.mLastLayer = w.mAnimLayer;
8567 w.mLastDsDx = w.mDsDx;
8568 w.mLastDtDx = w.mDtDx;
8569 w.mLastDsDy = w.mDsDy;
8570 w.mLastDtDy = w.mDtDy;
8571 w.mLastHScale = w.mHScale;
8572 w.mLastVScale = w.mVScale;
8573 if (SHOW_TRANSACTIONS) Log.i(
8574 TAG, " SURFACE " + w.mSurface + ": alpha="
8575 + w.mShownAlpha + " layer=" + w.mAnimLayer);
8576 if (w.mSurface != null) {
8577 try {
8578 w.mSurface.setAlpha(w.mShownAlpha);
8579 w.mSurface.setLayer(w.mAnimLayer);
8580 w.mSurface.setMatrix(
8581 w.mDsDx*w.mHScale, w.mDtDx*w.mVScale,
8582 w.mDsDy*w.mHScale, w.mDtDy*w.mVScale);
8583 } catch (RuntimeException e) {
8584 Log.w(TAG, "Error updating surface in " + w, e);
8585 if (!recoveringMemory) {
8586 reclaimSomeSurfaceMemoryLocked(w, "update");
8587 }
8588 }
8589 }
8590
8591 if (w.mLastHidden && !w.mDrawPending
8592 && !w.mCommitDrawPending
8593 && !w.mReadyToShow) {
8594 if (SHOW_TRANSACTIONS) Log.i(
8595 TAG, " SURFACE " + w.mSurface + ": SHOW (performLayout)");
8596 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + w
8597 + " during relayout");
8598 if (showSurfaceRobustlyLocked(w)) {
8599 w.mHasDrawn = true;
8600 w.mLastHidden = false;
8601 } else {
8602 w.mOrientationChanging = false;
8603 }
8604 }
8605 if (w.mSurface != null) {
8606 w.mToken.hasVisible = true;
8607 }
8608 } else {
8609 displayed = true;
8610 }
8611
8612 if (displayed) {
8613 if (!covered) {
8614 if (attrs.width == LayoutParams.FILL_PARENT
8615 && attrs.height == LayoutParams.FILL_PARENT) {
8616 covered = true;
8617 }
8618 }
8619 if (w.mOrientationChanging) {
8620 if (w.mDrawPending || w.mCommitDrawPending) {
8621 orientationChangeComplete = false;
8622 if (DEBUG_ORIENTATION) Log.v(TAG,
8623 "Orientation continue waiting for draw in " + w);
8624 } else {
8625 w.mOrientationChanging = false;
8626 if (DEBUG_ORIENTATION) Log.v(TAG,
8627 "Orientation change complete in " + w);
8628 }
8629 }
8630 w.mToken.hasVisible = true;
8631 }
8632 } else if (w.mOrientationChanging) {
8633 if (DEBUG_ORIENTATION) Log.v(TAG,
8634 "Orientation change skips hidden " + w);
8635 w.mOrientationChanging = false;
8636 }
8637
8638 final boolean canBeSeen = w.isDisplayedLw();
8639
8640 if (someoneLosingFocus && w == mCurrentFocus && canBeSeen) {
8641 focusDisplayed = true;
8642 }
8643
8644 // Update effect.
8645 if (!obscured) {
8646 if (w.mSurface != null) {
8647 if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
8648 holdScreen = w.mSession;
8649 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07008650 if (!syswin && w.mAttrs.screenBrightness >= 0
8651 && screenBrightness < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008652 screenBrightness = w.mAttrs.screenBrightness;
8653 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07008654 if (attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG
8655 || attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD
8656 || attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_ERROR) {
8657 syswin = true;
8658 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008659 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008660
8661 boolean opaqueDrawn = w.isOpaqueDrawn();
8662 if (opaqueDrawn && w.isFullscreen(dw, dh)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008663 // This window completely covers everything behind it,
8664 // so we want to leave all of them as unblurred (for
8665 // performance reasons).
8666 obscured = true;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008667 } else if (opaqueDrawn && w.needsBackgroundFiller(dw, dh)) {
8668 if (SHOW_TRANSACTIONS) Log.d(TAG, "showing background filler");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008669 // This window is in compatibility mode, and needs background filler.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008670 obscured = true;
8671 if (mBackgroundFillerSurface == null) {
8672 try {
8673 mBackgroundFillerSurface = new Surface(mFxSession, 0,
8674 0, dw, dh,
8675 PixelFormat.OPAQUE,
8676 Surface.FX_SURFACE_NORMAL);
8677 } catch (Exception e) {
8678 Log.e(TAG, "Exception creating filler surface", e);
8679 }
8680 }
8681 try {
8682 mBackgroundFillerSurface.setPosition(0, 0);
8683 mBackgroundFillerSurface.setSize(dw, dh);
8684 // Using the same layer as Dim because they will never be shown at the
8685 // same time.
8686 mBackgroundFillerSurface.setLayer(w.mAnimLayer - 1);
8687 mBackgroundFillerSurface.show();
8688 } catch (RuntimeException e) {
8689 Log.e(TAG, "Exception showing filler surface");
8690 }
8691 backgroundFillerShown = true;
8692 mBackgroundFillerShown = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008693 } else if (canBeSeen && !obscured &&
8694 (attrFlags&FLAG_BLUR_BEHIND|FLAG_DIM_BEHIND) != 0) {
8695 if (localLOGV) Log.v(TAG, "Win " + w
8696 + ": blurring=" + blurring
8697 + " obscured=" + obscured
8698 + " displayed=" + displayed);
8699 if ((attrFlags&FLAG_DIM_BEHIND) != 0) {
8700 if (!dimming) {
8701 //Log.i(TAG, "DIM BEHIND: " + w);
8702 dimming = true;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008703 if (mDimAnimator == null) {
8704 mDimAnimator = new DimAnimator(mFxSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008705 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008706 mDimAnimator.show(dw, dh);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008707 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008708 mDimAnimator.updateParameters(w, currentTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008709 }
8710 if ((attrFlags&FLAG_BLUR_BEHIND) != 0) {
8711 if (!blurring) {
8712 //Log.i(TAG, "BLUR BEHIND: " + w);
8713 blurring = true;
8714 mBlurShown = true;
8715 if (mBlurSurface == null) {
8716 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
8717 + mBlurSurface + ": CREATE");
8718 try {
Romain Guy06882f82009-06-10 13:36:04 -07008719 mBlurSurface = new Surface(mFxSession, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008720 -1, 16, 16,
8721 PixelFormat.OPAQUE,
8722 Surface.FX_SURFACE_BLUR);
8723 } catch (Exception e) {
8724 Log.e(TAG, "Exception creating Blur surface", e);
8725 }
8726 }
8727 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
8728 + mBlurSurface + ": SHOW pos=(0,0) (" +
8729 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
8730 if (mBlurSurface != null) {
8731 mBlurSurface.setPosition(0, 0);
8732 mBlurSurface.setSize(dw, dh);
8733 try {
8734 mBlurSurface.show();
8735 } catch (RuntimeException e) {
8736 Log.w(TAG, "Failure showing blur surface", e);
8737 }
8738 }
8739 }
8740 mBlurSurface.setLayer(w.mAnimLayer-2);
8741 }
8742 }
8743 }
8744 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008745
8746 if (backgroundFillerShown == false && mBackgroundFillerShown) {
8747 mBackgroundFillerShown = false;
8748 if (SHOW_TRANSACTIONS) Log.d(TAG, "hiding background filler");
8749 try {
8750 mBackgroundFillerSurface.hide();
8751 } catch (RuntimeException e) {
8752 Log.e(TAG, "Exception hiding filler surface", e);
8753 }
8754 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008755
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008756 if (mDimAnimator != null && mDimAnimator.mDimShown) {
8757 animating |= mDimAnimator.updateSurface(dimming, currentTime, mDisplayFrozen);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008758 }
Romain Guy06882f82009-06-10 13:36:04 -07008759
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008760 if (!blurring && mBlurShown) {
8761 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR " + mBlurSurface
8762 + ": HIDE");
8763 try {
8764 mBlurSurface.hide();
8765 } catch (IllegalArgumentException e) {
8766 Log.w(TAG, "Illegal argument exception hiding blur surface");
8767 }
8768 mBlurShown = false;
8769 }
8770
8771 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
8772 } catch (RuntimeException e) {
8773 Log.e(TAG, "Unhandled exception in Window Manager", e);
8774 }
8775
8776 Surface.closeTransaction();
Romain Guy06882f82009-06-10 13:36:04 -07008777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008778 if (DEBUG_ORIENTATION && mDisplayFrozen) Log.v(TAG,
8779 "With display frozen, orientationChangeComplete="
8780 + orientationChangeComplete);
8781 if (orientationChangeComplete) {
8782 if (mWindowsFreezingScreen) {
8783 mWindowsFreezingScreen = false;
8784 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
8785 }
8786 if (mAppsFreezingScreen == 0) {
8787 stopFreezingDisplayLocked();
8788 }
8789 }
Romain Guy06882f82009-06-10 13:36:04 -07008790
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008791 i = mResizingWindows.size();
8792 if (i > 0) {
8793 do {
8794 i--;
8795 WindowState win = mResizingWindows.get(i);
8796 try {
8797 win.mClient.resized(win.mFrame.width(),
8798 win.mFrame.height(), win.mLastContentInsets,
8799 win.mLastVisibleInsets, win.mDrawPending);
8800 win.mContentInsetsChanged = false;
8801 win.mVisibleInsetsChanged = false;
8802 } catch (RemoteException e) {
8803 win.mOrientationChanging = false;
8804 }
8805 } while (i > 0);
8806 mResizingWindows.clear();
8807 }
Romain Guy06882f82009-06-10 13:36:04 -07008808
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008809 // Destroy the surface of any windows that are no longer visible.
8810 i = mDestroySurface.size();
8811 if (i > 0) {
8812 do {
8813 i--;
8814 WindowState win = mDestroySurface.get(i);
8815 win.mDestroying = false;
8816 if (mInputMethodWindow == win) {
8817 mInputMethodWindow = null;
8818 }
8819 win.destroySurfaceLocked();
8820 } while (i > 0);
8821 mDestroySurface.clear();
8822 }
8823
8824 // Time to remove any exiting tokens?
8825 for (i=mExitingTokens.size()-1; i>=0; i--) {
8826 WindowToken token = mExitingTokens.get(i);
8827 if (!token.hasVisible) {
8828 mExitingTokens.remove(i);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07008829 if (token.windowType == TYPE_WALLPAPER) {
8830 mWallpaperTokens.remove(token);
8831 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008832 }
8833 }
8834
8835 // Time to remove any exiting applications?
8836 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
8837 AppWindowToken token = mExitingAppTokens.get(i);
8838 if (!token.hasVisible && !mClosingApps.contains(token)) {
8839 mAppTokens.remove(token);
8840 mExitingAppTokens.remove(i);
8841 }
8842 }
8843
8844 if (focusDisplayed) {
8845 mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
8846 }
8847 if (animating) {
8848 requestAnimationLocked(currentTime+(1000/60)-SystemClock.uptimeMillis());
8849 }
8850 mQueue.setHoldScreenLocked(holdScreen != null);
8851 if (screenBrightness < 0 || screenBrightness > 1.0f) {
8852 mPowerManager.setScreenBrightnessOverride(-1);
8853 } else {
8854 mPowerManager.setScreenBrightnessOverride((int)
8855 (screenBrightness * Power.BRIGHTNESS_ON));
8856 }
8857 if (holdScreen != mHoldingScreenOn) {
8858 mHoldingScreenOn = holdScreen;
8859 Message m = mH.obtainMessage(H.HOLD_SCREEN_CHANGED, holdScreen);
8860 mH.sendMessage(m);
8861 }
8862 }
8863
8864 void requestAnimationLocked(long delay) {
8865 if (!mAnimationPending) {
8866 mAnimationPending = true;
8867 mH.sendMessageDelayed(mH.obtainMessage(H.ANIMATE), delay);
8868 }
8869 }
Romain Guy06882f82009-06-10 13:36:04 -07008870
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008871 /**
8872 * Have the surface flinger show a surface, robustly dealing with
8873 * error conditions. In particular, if there is not enough memory
8874 * to show the surface, then we will try to get rid of other surfaces
8875 * in order to succeed.
Romain Guy06882f82009-06-10 13:36:04 -07008876 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008877 * @return Returns true if the surface was successfully shown.
8878 */
8879 boolean showSurfaceRobustlyLocked(WindowState win) {
8880 try {
8881 if (win.mSurface != null) {
8882 win.mSurface.show();
8883 }
8884 return true;
8885 } catch (RuntimeException e) {
8886 Log.w(TAG, "Failure showing surface " + win.mSurface + " in " + win);
8887 }
Romain Guy06882f82009-06-10 13:36:04 -07008888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008889 reclaimSomeSurfaceMemoryLocked(win, "show");
Romain Guy06882f82009-06-10 13:36:04 -07008890
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008891 return false;
8892 }
Romain Guy06882f82009-06-10 13:36:04 -07008893
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008894 void reclaimSomeSurfaceMemoryLocked(WindowState win, String operation) {
8895 final Surface surface = win.mSurface;
Romain Guy06882f82009-06-10 13:36:04 -07008896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008897 EventLog.writeEvent(LOG_WM_NO_SURFACE_MEMORY, win.toString(),
8898 win.mSession.mPid, operation);
Romain Guy06882f82009-06-10 13:36:04 -07008899
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008900 if (mForceRemoves == null) {
8901 mForceRemoves = new ArrayList<WindowState>();
8902 }
Romain Guy06882f82009-06-10 13:36:04 -07008903
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008904 long callingIdentity = Binder.clearCallingIdentity();
8905 try {
8906 // There was some problem... first, do a sanity check of the
8907 // window list to make sure we haven't left any dangling surfaces
8908 // around.
8909 int N = mWindows.size();
8910 boolean leakedSurface = false;
8911 Log.i(TAG, "Out of memory for surface! Looking for leaks...");
8912 for (int i=0; i<N; i++) {
8913 WindowState ws = (WindowState)mWindows.get(i);
8914 if (ws.mSurface != null) {
8915 if (!mSessions.contains(ws.mSession)) {
8916 Log.w(TAG, "LEAKED SURFACE (session doesn't exist): "
8917 + ws + " surface=" + ws.mSurface
8918 + " token=" + win.mToken
8919 + " pid=" + ws.mSession.mPid
8920 + " uid=" + ws.mSession.mUid);
8921 ws.mSurface.clear();
8922 ws.mSurface = null;
8923 mForceRemoves.add(ws);
8924 i--;
8925 N--;
8926 leakedSurface = true;
8927 } else if (win.mAppToken != null && win.mAppToken.clientHidden) {
8928 Log.w(TAG, "LEAKED SURFACE (app token hidden): "
8929 + ws + " surface=" + ws.mSurface
8930 + " token=" + win.mAppToken);
8931 ws.mSurface.clear();
8932 ws.mSurface = null;
8933 leakedSurface = true;
8934 }
8935 }
8936 }
Romain Guy06882f82009-06-10 13:36:04 -07008937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008938 boolean killedApps = false;
8939 if (!leakedSurface) {
8940 Log.w(TAG, "No leaked surfaces; killing applicatons!");
8941 SparseIntArray pidCandidates = new SparseIntArray();
8942 for (int i=0; i<N; i++) {
8943 WindowState ws = (WindowState)mWindows.get(i);
8944 if (ws.mSurface != null) {
8945 pidCandidates.append(ws.mSession.mPid, ws.mSession.mPid);
8946 }
8947 }
8948 if (pidCandidates.size() > 0) {
8949 int[] pids = new int[pidCandidates.size()];
8950 for (int i=0; i<pids.length; i++) {
8951 pids[i] = pidCandidates.keyAt(i);
8952 }
8953 try {
8954 if (mActivityManager.killPidsForMemory(pids)) {
8955 killedApps = true;
8956 }
8957 } catch (RemoteException e) {
8958 }
8959 }
8960 }
Romain Guy06882f82009-06-10 13:36:04 -07008961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008962 if (leakedSurface || killedApps) {
8963 // We managed to reclaim some memory, so get rid of the trouble
8964 // surface and ask the app to request another one.
8965 Log.w(TAG, "Looks like we have reclaimed some memory, clearing surface for retry.");
8966 if (surface != null) {
8967 surface.clear();
8968 win.mSurface = null;
8969 }
Romain Guy06882f82009-06-10 13:36:04 -07008970
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008971 try {
8972 win.mClient.dispatchGetNewSurface();
8973 } catch (RemoteException e) {
8974 }
8975 }
8976 } finally {
8977 Binder.restoreCallingIdentity(callingIdentity);
8978 }
8979 }
Romain Guy06882f82009-06-10 13:36:04 -07008980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008981 private boolean updateFocusedWindowLocked(int mode) {
8982 WindowState newFocus = computeFocusedWindowLocked();
8983 if (mCurrentFocus != newFocus) {
8984 // This check makes sure that we don't already have the focus
8985 // change message pending.
8986 mH.removeMessages(H.REPORT_FOCUS_CHANGE);
8987 mH.sendEmptyMessage(H.REPORT_FOCUS_CHANGE);
8988 if (localLOGV) Log.v(
8989 TAG, "Changing focus from " + mCurrentFocus + " to " + newFocus);
8990 final WindowState oldFocus = mCurrentFocus;
8991 mCurrentFocus = newFocus;
8992 mLosingFocus.remove(newFocus);
Romain Guy06882f82009-06-10 13:36:04 -07008993
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008994 final WindowState imWindow = mInputMethodWindow;
8995 if (newFocus != imWindow && oldFocus != imWindow) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08008996 if (moveInputMethodWindowsIfNeededLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008997 mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS &&
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08008998 mode != UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
8999 mLayoutNeeded = true;
9000 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009001 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
9002 performLayoutLockedInner();
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009003 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
9004 // Client will do the layout, but we need to assign layers
9005 // for handleNewWindowLocked() below.
9006 assignLayersLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009007 }
9008 }
Romain Guy06882f82009-06-10 13:36:04 -07009009
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009010 if (newFocus != null && mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
9011 mKeyWaiter.handleNewWindowLocked(newFocus);
9012 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009013 return true;
9014 }
9015 return false;
9016 }
9017
9018 private WindowState computeFocusedWindowLocked() {
9019 WindowState result = null;
9020 WindowState win;
9021
9022 int i = mWindows.size() - 1;
9023 int nextAppIndex = mAppTokens.size()-1;
9024 WindowToken nextApp = nextAppIndex >= 0
9025 ? mAppTokens.get(nextAppIndex) : null;
9026
9027 while (i >= 0) {
9028 win = (WindowState)mWindows.get(i);
9029
9030 if (localLOGV || DEBUG_FOCUS) Log.v(
9031 TAG, "Looking for focus: " + i
9032 + " = " + win
9033 + ", flags=" + win.mAttrs.flags
9034 + ", canReceive=" + win.canReceiveKeys());
9035
9036 AppWindowToken thisApp = win.mAppToken;
Romain Guy06882f82009-06-10 13:36:04 -07009037
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009038 // If this window's application has been removed, just skip it.
9039 if (thisApp != null && thisApp.removed) {
9040 i--;
9041 continue;
9042 }
Romain Guy06882f82009-06-10 13:36:04 -07009043
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009044 // If there is a focused app, don't allow focus to go to any
9045 // windows below it. If this is an application window, step
9046 // through the app tokens until we find its app.
9047 if (thisApp != null && nextApp != null && thisApp != nextApp
9048 && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
9049 int origAppIndex = nextAppIndex;
9050 while (nextAppIndex > 0) {
9051 if (nextApp == mFocusedApp) {
9052 // Whoops, we are below the focused app... no focus
9053 // for you!
9054 if (localLOGV || DEBUG_FOCUS) Log.v(
9055 TAG, "Reached focused app: " + mFocusedApp);
9056 return null;
9057 }
9058 nextAppIndex--;
9059 nextApp = mAppTokens.get(nextAppIndex);
9060 if (nextApp == thisApp) {
9061 break;
9062 }
9063 }
9064 if (thisApp != nextApp) {
9065 // Uh oh, the app token doesn't exist! This shouldn't
9066 // happen, but if it does we can get totally hosed...
9067 // so restart at the original app.
9068 nextAppIndex = origAppIndex;
9069 nextApp = mAppTokens.get(nextAppIndex);
9070 }
9071 }
9072
9073 // Dispatch to this window if it is wants key events.
9074 if (win.canReceiveKeys()) {
9075 if (DEBUG_FOCUS) Log.v(
9076 TAG, "Found focus @ " + i + " = " + win);
9077 result = win;
9078 break;
9079 }
9080
9081 i--;
9082 }
9083
9084 return result;
9085 }
9086
9087 private void startFreezingDisplayLocked() {
9088 if (mDisplayFrozen) {
Chris Tate2ad63a92009-03-25 17:36:48 -07009089 // Freezing the display also suspends key event delivery, to
9090 // keep events from going astray while the display is reconfigured.
9091 // If someone has changed orientation again while the screen is
9092 // still frozen, the events will continue to be blocked while the
9093 // successive orientation change is processed. To prevent spurious
9094 // ANRs, we reset the event dispatch timeout in this case.
9095 synchronized (mKeyWaiter) {
9096 mKeyWaiter.mWasFrozen = true;
9097 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009098 return;
9099 }
Romain Guy06882f82009-06-10 13:36:04 -07009100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009101 mScreenFrozenLock.acquire();
Romain Guy06882f82009-06-10 13:36:04 -07009102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009103 long now = SystemClock.uptimeMillis();
9104 //Log.i(TAG, "Freezing, gc pending: " + mFreezeGcPending + ", now " + now);
9105 if (mFreezeGcPending != 0) {
9106 if (now > (mFreezeGcPending+1000)) {
9107 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
9108 mH.removeMessages(H.FORCE_GC);
9109 Runtime.getRuntime().gc();
9110 mFreezeGcPending = now;
9111 }
9112 } else {
9113 mFreezeGcPending = now;
9114 }
Romain Guy06882f82009-06-10 13:36:04 -07009115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009116 mDisplayFrozen = true;
9117 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
9118 mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
9119 mAppTransitionReady = true;
9120 }
Romain Guy06882f82009-06-10 13:36:04 -07009121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009122 if (PROFILE_ORIENTATION) {
9123 File file = new File("/data/system/frozen");
9124 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
9125 }
9126 Surface.freezeDisplay(0);
9127 }
Romain Guy06882f82009-06-10 13:36:04 -07009128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009129 private void stopFreezingDisplayLocked() {
9130 if (!mDisplayFrozen) {
9131 return;
9132 }
Romain Guy06882f82009-06-10 13:36:04 -07009133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009134 mDisplayFrozen = false;
9135 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
9136 if (PROFILE_ORIENTATION) {
9137 Debug.stopMethodTracing();
9138 }
9139 Surface.unfreezeDisplay(0);
Romain Guy06882f82009-06-10 13:36:04 -07009140
Chris Tate2ad63a92009-03-25 17:36:48 -07009141 // Reset the key delivery timeout on unfreeze, too. We force a wakeup here
9142 // too because regular key delivery processing should resume immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009143 synchronized (mKeyWaiter) {
9144 mKeyWaiter.mWasFrozen = true;
9145 mKeyWaiter.notifyAll();
9146 }
9147
9148 // A little kludge: a lot could have happened while the
9149 // display was frozen, so now that we are coming back we
9150 // do a gc so that any remote references the system
9151 // processes holds on others can be released if they are
9152 // no longer needed.
9153 mH.removeMessages(H.FORCE_GC);
9154 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
9155 2000);
Romain Guy06882f82009-06-10 13:36:04 -07009156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009157 mScreenFrozenLock.release();
9158 }
Romain Guy06882f82009-06-10 13:36:04 -07009159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009160 @Override
9161 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
9162 if (mContext.checkCallingOrSelfPermission("android.permission.DUMP")
9163 != PackageManager.PERMISSION_GRANTED) {
9164 pw.println("Permission Denial: can't dump WindowManager from from pid="
9165 + Binder.getCallingPid()
9166 + ", uid=" + Binder.getCallingUid());
9167 return;
9168 }
Romain Guy06882f82009-06-10 13:36:04 -07009169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009170 synchronized(mWindowMap) {
9171 pw.println("Current Window Manager state:");
9172 for (int i=mWindows.size()-1; i>=0; i--) {
9173 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009174 pw.print(" Window #"); pw.print(i); pw.print(' ');
9175 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009176 w.dump(pw, " ");
9177 }
9178 if (mInputMethodDialogs.size() > 0) {
9179 pw.println(" ");
9180 pw.println(" Input method dialogs:");
9181 for (int i=mInputMethodDialogs.size()-1; i>=0; i--) {
9182 WindowState w = mInputMethodDialogs.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009183 pw.print(" IM Dialog #"); pw.print(i); pw.print(": "); pw.println(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009184 }
9185 }
9186 if (mPendingRemove.size() > 0) {
9187 pw.println(" ");
9188 pw.println(" Remove pending for:");
9189 for (int i=mPendingRemove.size()-1; i>=0; i--) {
9190 WindowState w = mPendingRemove.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009191 pw.print(" Remove #"); pw.print(i); pw.print(' ');
9192 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009193 w.dump(pw, " ");
9194 }
9195 }
9196 if (mForceRemoves != null && mForceRemoves.size() > 0) {
9197 pw.println(" ");
9198 pw.println(" Windows force removing:");
9199 for (int i=mForceRemoves.size()-1; i>=0; i--) {
9200 WindowState w = mForceRemoves.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009201 pw.print(" Removing #"); pw.print(i); pw.print(' ');
9202 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009203 w.dump(pw, " ");
9204 }
9205 }
9206 if (mDestroySurface.size() > 0) {
9207 pw.println(" ");
9208 pw.println(" Windows waiting to destroy their surface:");
9209 for (int i=mDestroySurface.size()-1; i>=0; i--) {
9210 WindowState w = mDestroySurface.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009211 pw.print(" Destroy #"); pw.print(i); pw.print(' ');
9212 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009213 w.dump(pw, " ");
9214 }
9215 }
9216 if (mLosingFocus.size() > 0) {
9217 pw.println(" ");
9218 pw.println(" Windows losing focus:");
9219 for (int i=mLosingFocus.size()-1; i>=0; i--) {
9220 WindowState w = mLosingFocus.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009221 pw.print(" Losing #"); pw.print(i); pw.print(' ');
9222 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009223 w.dump(pw, " ");
9224 }
9225 }
9226 if (mSessions.size() > 0) {
9227 pw.println(" ");
9228 pw.println(" All active sessions:");
9229 Iterator<Session> it = mSessions.iterator();
9230 while (it.hasNext()) {
9231 Session s = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009232 pw.print(" Session "); pw.print(s); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009233 s.dump(pw, " ");
9234 }
9235 }
9236 if (mTokenMap.size() > 0) {
9237 pw.println(" ");
9238 pw.println(" All tokens:");
9239 Iterator<WindowToken> it = mTokenMap.values().iterator();
9240 while (it.hasNext()) {
9241 WindowToken token = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009242 pw.print(" Token "); pw.print(token.token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009243 token.dump(pw, " ");
9244 }
9245 }
9246 if (mTokenList.size() > 0) {
9247 pw.println(" ");
9248 pw.println(" Window token list:");
9249 for (int i=0; i<mTokenList.size(); i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009250 pw.print(" #"); pw.print(i); pw.print(": ");
9251 pw.println(mTokenList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009252 }
9253 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07009254 if (mWallpaperTokens.size() > 0) {
9255 pw.println(" ");
9256 pw.println(" Wallpaper tokens:");
9257 for (int i=mWallpaperTokens.size()-1; i>=0; i--) {
9258 WindowToken token = mWallpaperTokens.get(i);
9259 pw.print(" Wallpaper #"); pw.print(i);
9260 pw.print(' '); pw.print(token); pw.println(':');
9261 token.dump(pw, " ");
9262 }
9263 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009264 if (mAppTokens.size() > 0) {
9265 pw.println(" ");
9266 pw.println(" Application tokens in Z order:");
9267 for (int i=mAppTokens.size()-1; i>=0; i--) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009268 pw.print(" App #"); pw.print(i); pw.print(": ");
9269 pw.println(mAppTokens.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009270 }
9271 }
9272 if (mFinishedStarting.size() > 0) {
9273 pw.println(" ");
9274 pw.println(" Finishing start of application tokens:");
9275 for (int i=mFinishedStarting.size()-1; i>=0; i--) {
9276 WindowToken token = mFinishedStarting.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009277 pw.print(" Finished Starting #"); pw.print(i);
9278 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009279 token.dump(pw, " ");
9280 }
9281 }
9282 if (mExitingTokens.size() > 0) {
9283 pw.println(" ");
9284 pw.println(" Exiting tokens:");
9285 for (int i=mExitingTokens.size()-1; i>=0; i--) {
9286 WindowToken token = mExitingTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009287 pw.print(" Exiting #"); pw.print(i);
9288 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009289 token.dump(pw, " ");
9290 }
9291 }
9292 if (mExitingAppTokens.size() > 0) {
9293 pw.println(" ");
9294 pw.println(" Exiting application tokens:");
9295 for (int i=mExitingAppTokens.size()-1; i>=0; i--) {
9296 WindowToken token = mExitingAppTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009297 pw.print(" Exiting App #"); pw.print(i);
9298 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009299 token.dump(pw, " ");
9300 }
9301 }
9302 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009303 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
9304 pw.print(" mLastFocus="); pw.println(mLastFocus);
9305 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
9306 pw.print(" mInputMethodTarget="); pw.println(mInputMethodTarget);
9307 pw.print(" mInputMethodWindow="); pw.println(mInputMethodWindow);
9308 pw.print(" mInTouchMode="); pw.println(mInTouchMode);
9309 pw.print(" mSystemBooted="); pw.print(mSystemBooted);
9310 pw.print(" mDisplayEnabled="); pw.println(mDisplayEnabled);
9311 pw.print(" mLayoutNeeded="); pw.print(mLayoutNeeded);
9312 pw.print(" mBlurShown="); pw.println(mBlurShown);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009313 if (mDimAnimator != null) {
9314 mDimAnimator.printTo(pw);
9315 } else {
9316 pw.print( " no DimAnimator ");
9317 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009318 pw.print(" mInputMethodAnimLayerAdjustment=");
9319 pw.println(mInputMethodAnimLayerAdjustment);
9320 pw.print(" mDisplayFrozen="); pw.print(mDisplayFrozen);
9321 pw.print(" mWindowsFreezingScreen="); pw.print(mWindowsFreezingScreen);
9322 pw.print(" mAppsFreezingScreen="); pw.println(mAppsFreezingScreen);
9323 pw.print(" mRotation="); pw.print(mRotation);
9324 pw.print(", mForcedAppOrientation="); pw.print(mForcedAppOrientation);
9325 pw.print(", mRequestedRotation="); pw.println(mRequestedRotation);
9326 pw.print(" mAnimationPending="); pw.print(mAnimationPending);
9327 pw.print(" mWindowAnimationScale="); pw.print(mWindowAnimationScale);
9328 pw.print(" mTransitionWindowAnimationScale="); pw.println(mTransitionAnimationScale);
9329 pw.print(" mNextAppTransition=0x");
9330 pw.print(Integer.toHexString(mNextAppTransition));
9331 pw.print(", mAppTransitionReady="); pw.print(mAppTransitionReady);
9332 pw.print(", mAppTransitionTimeout="); pw.println( mAppTransitionTimeout);
9333 pw.print(" mStartingIconInTransition="); pw.print(mStartingIconInTransition);
9334 pw.print(", mSkipAppTransitionAnimation="); pw.println(mSkipAppTransitionAnimation);
9335 if (mOpeningApps.size() > 0) {
9336 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
9337 }
9338 if (mClosingApps.size() > 0) {
9339 pw.print(" mClosingApps="); pw.println(mClosingApps);
9340 }
9341 pw.print(" DisplayWidth="); pw.print(mDisplay.getWidth());
9342 pw.print(" DisplayHeight="); pw.println(mDisplay.getHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009343 pw.println(" KeyWaiter state:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009344 pw.print(" mLastWin="); pw.print(mKeyWaiter.mLastWin);
9345 pw.print(" mLastBinder="); pw.println(mKeyWaiter.mLastBinder);
9346 pw.print(" mFinished="); pw.print(mKeyWaiter.mFinished);
9347 pw.print(" mGotFirstWindow="); pw.print(mKeyWaiter.mGotFirstWindow);
9348 pw.print(" mEventDispatching="); pw.print(mKeyWaiter.mEventDispatching);
9349 pw.print(" mTimeToSwitch="); pw.println(mKeyWaiter.mTimeToSwitch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009350 }
9351 }
9352
9353 public void monitor() {
9354 synchronized (mWindowMap) { }
9355 synchronized (mKeyguardDisabled) { }
9356 synchronized (mKeyWaiter) { }
9357 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009358
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07009359 public void virtualKeyFeedback(KeyEvent event) {
9360 mPolicy.keyFeedbackFromInput(event);
9361 }
9362
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009363 /**
9364 * DimAnimator class that controls the dim animation. This holds the surface and
9365 * all state used for dim animation.
9366 */
9367 private static class DimAnimator {
9368 Surface mDimSurface;
9369 boolean mDimShown = false;
9370 float mDimCurrentAlpha;
9371 float mDimTargetAlpha;
9372 float mDimDeltaPerMs;
9373 long mLastDimAnimTime;
9374
9375 DimAnimator (SurfaceSession session) {
9376 if (mDimSurface == null) {
9377 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
9378 + mDimSurface + ": CREATE");
9379 try {
9380 mDimSurface = new Surface(session, 0, -1, 16, 16, PixelFormat.OPAQUE,
9381 Surface.FX_SURFACE_DIM);
9382 } catch (Exception e) {
9383 Log.e(TAG, "Exception creating Dim surface", e);
9384 }
9385 }
9386 }
9387
9388 /**
9389 * Show the dim surface.
9390 */
9391 void show(int dw, int dh) {
9392 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface + ": SHOW pos=(0,0) (" +
9393 dw + "x" + dh + ")");
9394 mDimShown = true;
9395 try {
9396 mDimSurface.setPosition(0, 0);
9397 mDimSurface.setSize(dw, dh);
9398 mDimSurface.show();
9399 } catch (RuntimeException e) {
9400 Log.w(TAG, "Failure showing dim surface", e);
9401 }
9402 }
9403
9404 /**
9405 * Set's the dim surface's layer and update dim parameters that will be used in
9406 * {@link updateSurface} after all windows are examined.
9407 */
9408 void updateParameters(WindowState w, long currentTime) {
9409 mDimSurface.setLayer(w.mAnimLayer-1);
9410
9411 final float target = w.mExiting ? 0 : w.mAttrs.dimAmount;
9412 if (SHOW_TRANSACTIONS) Log.i(TAG, "layer=" + (w.mAnimLayer-1) + ", target=" + target);
9413 if (mDimTargetAlpha != target) {
9414 // If the desired dim level has changed, then
9415 // start an animation to it.
9416 mLastDimAnimTime = currentTime;
9417 long duration = (w.mAnimating && w.mAnimation != null)
9418 ? w.mAnimation.computeDurationHint()
9419 : DEFAULT_DIM_DURATION;
9420 if (target > mDimTargetAlpha) {
9421 // This is happening behind the activity UI,
9422 // so we can make it run a little longer to
9423 // give a stronger impression without disrupting
9424 // the user.
9425 duration *= DIM_DURATION_MULTIPLIER;
9426 }
9427 if (duration < 1) {
9428 // Don't divide by zero
9429 duration = 1;
9430 }
9431 mDimTargetAlpha = target;
9432 mDimDeltaPerMs = (mDimTargetAlpha-mDimCurrentAlpha) / duration;
9433 }
9434 }
9435
9436 /**
9437 * Updating the surface's alpha. Returns true if the animation continues, or returns
9438 * false when the animation is finished and the dim surface is hidden.
9439 */
9440 boolean updateSurface(boolean dimming, long currentTime, boolean displayFrozen) {
9441 if (!dimming) {
9442 if (mDimTargetAlpha != 0) {
9443 mLastDimAnimTime = currentTime;
9444 mDimTargetAlpha = 0;
9445 mDimDeltaPerMs = (-mDimCurrentAlpha) / DEFAULT_DIM_DURATION;
9446 }
9447 }
9448
9449 boolean animating = false;
9450 if (mLastDimAnimTime != 0) {
9451 mDimCurrentAlpha += mDimDeltaPerMs
9452 * (currentTime-mLastDimAnimTime);
9453 boolean more = true;
9454 if (displayFrozen) {
9455 // If the display is frozen, there is no reason to animate.
9456 more = false;
9457 } else if (mDimDeltaPerMs > 0) {
9458 if (mDimCurrentAlpha > mDimTargetAlpha) {
9459 more = false;
9460 }
9461 } else if (mDimDeltaPerMs < 0) {
9462 if (mDimCurrentAlpha < mDimTargetAlpha) {
9463 more = false;
9464 }
9465 } else {
9466 more = false;
9467 }
9468
9469 // Do we need to continue animating?
9470 if (more) {
9471 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
9472 + mDimSurface + ": alpha=" + mDimCurrentAlpha);
9473 mLastDimAnimTime = currentTime;
9474 mDimSurface.setAlpha(mDimCurrentAlpha);
9475 animating = true;
9476 } else {
9477 mDimCurrentAlpha = mDimTargetAlpha;
9478 mLastDimAnimTime = 0;
9479 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
9480 + mDimSurface + ": final alpha=" + mDimCurrentAlpha);
9481 mDimSurface.setAlpha(mDimCurrentAlpha);
9482 if (!dimming) {
9483 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
9484 + ": HIDE");
9485 try {
9486 mDimSurface.hide();
9487 } catch (RuntimeException e) {
9488 Log.w(TAG, "Illegal argument exception hiding dim surface");
9489 }
9490 mDimShown = false;
9491 }
9492 }
9493 }
9494 return animating;
9495 }
9496
9497 public void printTo(PrintWriter pw) {
9498 pw.print(" mDimShown="); pw.print(mDimShown);
9499 pw.print(" current="); pw.print(mDimCurrentAlpha);
9500 pw.print(" target="); pw.print(mDimTargetAlpha);
9501 pw.print(" delta="); pw.print(mDimDeltaPerMs);
9502 pw.print(" lastAnimTime="); pw.println(mLastDimAnimTime);
9503 }
9504 }
9505
9506 /**
9507 * Animation that fade in after 0.5 interpolate time, or fade out in reverse order.
9508 * This is used for opening/closing transition for apps in compatible mode.
9509 */
9510 private static class FadeInOutAnimation extends Animation {
9511 int mWidth;
9512 boolean mFadeIn;
9513
9514 public FadeInOutAnimation(boolean fadeIn) {
9515 setInterpolator(new AccelerateInterpolator());
9516 setDuration(DEFAULT_FADE_IN_OUT_DURATION);
9517 mFadeIn = fadeIn;
9518 }
9519
9520 @Override
9521 protected void applyTransformation(float interpolatedTime, Transformation t) {
9522 float x = interpolatedTime;
9523 if (!mFadeIn) {
9524 x = 1.0f - x; // reverse the interpolation for fade out
9525 }
9526 if (x < 0.5) {
9527 // move the window out of the screen.
9528 t.getMatrix().setTranslate(mWidth, 0);
9529 } else {
9530 t.getMatrix().setTranslate(0, 0);// show
9531 t.setAlpha((x - 0.5f) * 2);
9532 }
9533 }
9534
9535 @Override
9536 public void initialize(int width, int height, int parentWidth, int parentHeight) {
9537 // width is the screen width {@see AppWindowToken#stepAnimatinoLocked}
9538 mWidth = width;
9539 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009540
9541 @Override
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07009542 public int getZAdjustment() {
9543 return Animation.ZORDER_TOP;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009544 }
9545 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009546}