blob: b5e652085821a2637700243e616a3a9b195502df [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import static android.view.WindowManager.LayoutParams.MEMORY_TYPE_PUSH_BUFFERS;
38import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
39import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
40import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
41import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070042import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043
44import com.android.internal.app.IBatteryStats;
45import com.android.internal.policy.PolicyManager;
46import com.android.internal.view.IInputContext;
47import com.android.internal.view.IInputMethodClient;
48import com.android.internal.view.IInputMethodManager;
49import com.android.server.KeyInputQueue.QueuedEvent;
50import com.android.server.am.BatteryStatsService;
51
52import android.Manifest;
53import android.app.ActivityManagerNative;
54import android.app.IActivityManager;
55import android.content.Context;
56import android.content.pm.ActivityInfo;
57import android.content.pm.PackageManager;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070058import android.content.res.CompatibilityInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.content.res.Configuration;
60import android.graphics.Matrix;
61import android.graphics.PixelFormat;
62import android.graphics.Rect;
63import android.graphics.Region;
64import android.os.BatteryStats;
65import android.os.Binder;
Dianne Hackborn75804932009-10-20 20:15:20 -070066import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.os.Debug;
68import android.os.Handler;
69import android.os.IBinder;
Michael Chan53071d62009-05-13 17:29:48 -070070import android.os.LatencyTimer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.os.LocalPowerManager;
72import android.os.Looper;
73import android.os.Message;
74import android.os.Parcel;
75import android.os.ParcelFileDescriptor;
76import android.os.Power;
77import android.os.PowerManager;
78import android.os.Process;
79import android.os.RemoteException;
80import android.os.ServiceManager;
81import android.os.SystemClock;
82import android.os.SystemProperties;
83import android.os.TokenWatcher;
84import android.provider.Settings;
Dianne Hackborn723738c2009-06-25 19:48:04 -070085import android.util.DisplayMetrics;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086import android.util.EventLog;
87import android.util.Log;
88import android.util.SparseIntArray;
89import android.view.Display;
90import android.view.Gravity;
91import android.view.IApplicationToken;
92import android.view.IOnKeyguardExitResult;
93import android.view.IRotationWatcher;
94import android.view.IWindow;
95import android.view.IWindowManager;
96import android.view.IWindowSession;
97import android.view.KeyEvent;
98import android.view.MotionEvent;
99import android.view.RawInputEvent;
100import android.view.Surface;
101import android.view.SurfaceSession;
102import android.view.View;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -0700103import android.view.ViewConfiguration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104import 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;
Dianne Hackborn9b52a212009-12-11 14:51:35 -0800136 static final boolean DEBUG_LAYOUT = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 static final boolean DEBUG_LAYERS = false;
138 static final boolean DEBUG_INPUT = false;
139 static final boolean DEBUG_INPUT_METHOD = false;
140 static final boolean DEBUG_VISIBILITY = false;
Dianne Hackbornbdd52b22009-09-02 21:46:19 -0700141 static final boolean DEBUG_WINDOW_MOVEMENT = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 static final boolean DEBUG_ORIENTATION = false;
143 static final boolean DEBUG_APP_TRANSITIONS = false;
144 static final boolean DEBUG_STARTING_WINDOW = false;
145 static final boolean DEBUG_REORDER = false;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -0700146 static final boolean DEBUG_WALLPAPER = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 static final boolean SHOW_TRANSACTIONS = false;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700148 static final boolean HIDE_STACK_CRAWLS = true;
Michael Chan53071d62009-05-13 17:29:48 -0700149 static final boolean MEASURE_LATENCY = false;
150 static private LatencyTimer lt;
151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 static final boolean PROFILE_ORIENTATION = false;
153 static final boolean BLUR = true;
Dave Bortcfe65242009-04-09 14:51:04 -0700154 static final boolean localLOGV = DEBUG;
Romain Guy06882f82009-06-10 13:36:04 -0700155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 /** How long to wait for subsequent key repeats, in milliseconds */
157 static final int KEY_REPEAT_DELAY = 50;
158
159 /** How much to multiply the policy's type layer, to reserve room
160 * for multiple windows of the same type and Z-ordering adjustment
161 * with TYPE_LAYER_OFFSET. */
162 static final int TYPE_LAYER_MULTIPLIER = 10000;
Romain Guy06882f82009-06-10 13:36:04 -0700163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 /** Offset from TYPE_LAYER_MULTIPLIER for moving a group of windows above
165 * or below others in the same layer. */
166 static final int TYPE_LAYER_OFFSET = 1000;
Romain Guy06882f82009-06-10 13:36:04 -0700167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 /** How much to increment the layer for each window, to reserve room
169 * for effect surfaces between them.
170 */
171 static final int WINDOW_LAYER_MULTIPLIER = 5;
Romain Guy06882f82009-06-10 13:36:04 -0700172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 /** The maximum length we will accept for a loaded animation duration:
174 * this is 10 seconds.
175 */
176 static final int MAX_ANIMATION_DURATION = 10*1000;
177
178 /** Amount of time (in milliseconds) to animate the dim surface from one
179 * value to another, when no window animation is driving it.
180 */
181 static final int DEFAULT_DIM_DURATION = 200;
182
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700183 /** Amount of time (in milliseconds) to animate the fade-in-out transition for
184 * compatible windows.
185 */
186 static final int DEFAULT_FADE_IN_OUT_DURATION = 400;
187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 /** Adjustment to time to perform a dim, to make it more dramatic.
189 */
190 static final int DIM_DURATION_MULTIPLIER = 6;
Romain Guy06882f82009-06-10 13:36:04 -0700191
Dianne Hackborncfaef692009-06-15 14:24:44 -0700192 static final int INJECT_FAILED = 0;
193 static final int INJECT_SUCCEEDED = 1;
194 static final int INJECT_NO_PERMISSION = -1;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 static final int UPDATE_FOCUS_NORMAL = 0;
197 static final int UPDATE_FOCUS_WILL_ASSIGN_LAYERS = 1;
198 static final int UPDATE_FOCUS_PLACING_SURFACES = 2;
199 static final int UPDATE_FOCUS_WILL_PLACE_SURFACES = 3;
Romain Guy06882f82009-06-10 13:36:04 -0700200
Michael Chane96440f2009-05-06 10:27:36 -0700201 /** The minimum time between dispatching touch events. */
202 int mMinWaitTimeBetweenTouchEvents = 1000 / 35;
203
204 // Last touch event time
205 long mLastTouchEventTime = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700206
Michael Chane96440f2009-05-06 10:27:36 -0700207 // Last touch event type
208 int mLastTouchEventType = OTHER_EVENT;
Romain Guy06882f82009-06-10 13:36:04 -0700209
Michael Chane96440f2009-05-06 10:27:36 -0700210 // Time to wait before calling useractivity again. This saves CPU usage
211 // when we get a flood of touch events.
212 static final int MIN_TIME_BETWEEN_USERACTIVITIES = 1000;
213
214 // Last time we call user activity
215 long mLastUserActivityCallTime = 0;
216
Romain Guy06882f82009-06-10 13:36:04 -0700217 // Last time we updated battery stats
Michael Chane96440f2009-05-06 10:27:36 -0700218 long mLastBatteryStatsCallTime = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 private static final String SYSTEM_SECURE = "ro.secure";
Romain Guy06882f82009-06-10 13:36:04 -0700221 private static final String SYSTEM_DEBUGGABLE = "ro.debuggable";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222
223 /**
224 * Condition waited on by {@link #reenableKeyguard} to know the call to
225 * the window policy has finished.
Mike Lockwood983ee092009-11-22 01:42:24 -0500226 * This is set to true only if mKeyguardTokenWatcher.acquired() has
227 * actually disabled the keyguard.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 */
Mike Lockwood983ee092009-11-22 01:42:24 -0500229 private boolean mKeyguardDisabled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230
Mike Lockwood983ee092009-11-22 01:42:24 -0500231 final TokenWatcher mKeyguardTokenWatcher = new TokenWatcher(
232 new Handler(), "WindowManagerService.mKeyguardTokenWatcher") {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 public void acquired() {
234 mPolicy.enableKeyguard(false);
Mike Lockwood983ee092009-11-22 01:42:24 -0500235 mKeyguardDisabled = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 }
237 public void released() {
Dianne Hackborna33e3f72009-09-29 17:28:24 -0700238 mPolicy.enableKeyguard(true);
Mike Lockwood983ee092009-11-22 01:42:24 -0500239 synchronized (mKeyguardTokenWatcher) {
240 mKeyguardDisabled = false;
241 mKeyguardTokenWatcher.notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 }
243 }
244 };
245
246 final Context mContext;
247
248 final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 final boolean mLimitedAlphaCompositing;
Romain Guy06882f82009-06-10 13:36:04 -0700251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 final WindowManagerPolicy mPolicy = PolicyManager.makeNewWindowManager();
253
254 final IActivityManager mActivityManager;
Romain Guy06882f82009-06-10 13:36:04 -0700255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 final IBatteryStats mBatteryStats;
Romain Guy06882f82009-06-10 13:36:04 -0700257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 /**
259 * All currently active sessions with clients.
260 */
261 final HashSet<Session> mSessions = new HashSet<Session>();
Romain Guy06882f82009-06-10 13:36:04 -0700262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 /**
264 * Mapping from an IWindow IBinder to the server's Window object.
265 * This is also used as the lock for all of our state.
266 */
267 final HashMap<IBinder, WindowState> mWindowMap = new HashMap<IBinder, WindowState>();
268
269 /**
270 * Mapping from a token IBinder to a WindowToken object.
271 */
272 final HashMap<IBinder, WindowToken> mTokenMap =
273 new HashMap<IBinder, WindowToken>();
274
275 /**
276 * The same tokens as mTokenMap, stored in a list for efficient iteration
277 * over them.
278 */
279 final ArrayList<WindowToken> mTokenList = new ArrayList<WindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 /**
282 * Window tokens that are in the process of exiting, but still
283 * on screen for animations.
284 */
285 final ArrayList<WindowToken> mExitingTokens = new ArrayList<WindowToken>();
286
287 /**
288 * Z-ordered (bottom-most first) list of all application tokens, for
289 * controlling the ordering of windows in different applications. This
290 * contains WindowToken objects.
291 */
292 final ArrayList<AppWindowToken> mAppTokens = new ArrayList<AppWindowToken>();
293
294 /**
295 * Application tokens that are in the process of exiting, but still
296 * on screen for animations.
297 */
298 final ArrayList<AppWindowToken> mExitingAppTokens = new ArrayList<AppWindowToken>();
299
300 /**
301 * List of window tokens that have finished starting their application,
302 * and now need to have the policy remove their windows.
303 */
304 final ArrayList<AppWindowToken> mFinishedStarting = new ArrayList<AppWindowToken>();
305
306 /**
Dianne Hackborn3b3e1452009-09-24 19:22:12 -0700307 * This was the app token that was used to retrieve the last enter
308 * animation. It will be used for the next exit animation.
309 */
310 AppWindowToken mLastEnterAnimToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800311
Dianne Hackborn3b3e1452009-09-24 19:22:12 -0700312 /**
313 * These were the layout params used to retrieve the last enter animation.
314 * They will be used for the next exit animation.
315 */
316 LayoutParams mLastEnterAnimParams;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800317
Dianne Hackborn3b3e1452009-09-24 19:22:12 -0700318 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 * Z-ordered (bottom-most first) list of all Window objects.
320 */
321 final ArrayList mWindows = new ArrayList();
322
323 /**
324 * Windows that are being resized. Used so we can tell the client about
325 * the resize after closing the transaction in which we resized the
326 * underlying surface.
327 */
328 final ArrayList<WindowState> mResizingWindows = new ArrayList<WindowState>();
329
330 /**
331 * Windows whose animations have ended and now must be removed.
332 */
333 final ArrayList<WindowState> mPendingRemove = new ArrayList<WindowState>();
334
335 /**
336 * Windows whose surface should be destroyed.
337 */
338 final ArrayList<WindowState> mDestroySurface = new ArrayList<WindowState>();
339
340 /**
341 * Windows that have lost input focus and are waiting for the new
342 * focus window to be displayed before they are told about this.
343 */
344 ArrayList<WindowState> mLosingFocus = new ArrayList<WindowState>();
345
346 /**
347 * This is set when we have run out of memory, and will either be an empty
348 * list or contain windows that need to be force removed.
349 */
350 ArrayList<WindowState> mForceRemoves;
Romain Guy06882f82009-06-10 13:36:04 -0700351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 IInputMethodManager mInputMethodManager;
Romain Guy06882f82009-06-10 13:36:04 -0700353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 SurfaceSession mFxSession;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700355 private DimAnimator mDimAnimator = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 Surface mBlurSurface;
357 boolean mBlurShown;
Romain Guy06882f82009-06-10 13:36:04 -0700358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 int mTransactionSequence = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 final float[] mTmpFloats = new float[9];
362
363 boolean mSafeMode;
364 boolean mDisplayEnabled = false;
365 boolean mSystemBooted = false;
366 int mRotation = 0;
367 int mRequestedRotation = 0;
368 int mForcedAppOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Dianne Hackborn321ae682009-03-27 16:16:03 -0700369 int mLastRotationFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 ArrayList<IRotationWatcher> mRotationWatchers
371 = new ArrayList<IRotationWatcher>();
Romain Guy06882f82009-06-10 13:36:04 -0700372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 boolean mLayoutNeeded = true;
374 boolean mAnimationPending = false;
375 boolean mDisplayFrozen = false;
376 boolean mWindowsFreezingScreen = false;
377 long mFreezeGcPending = 0;
378 int mAppsFreezingScreen = 0;
379
380 // This is held as long as we have the screen frozen, to give us time to
381 // perform a rotation animation when turning off shows the lock screen which
382 // changes the orientation.
383 PowerManager.WakeLock mScreenFrozenLock;
Romain Guy06882f82009-06-10 13:36:04 -0700384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 // State management of app transitions. When we are preparing for a
386 // transition, mNextAppTransition will be the kind of transition to
387 // perform or TRANSIT_NONE if we are not waiting. If we are waiting,
388 // mOpeningApps and mClosingApps are the lists of tokens that will be
389 // made visible or hidden at the next transition.
Dianne Hackbornbfe319e2009-09-21 00:34:05 -0700390 int mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -0700391 String mNextAppTransitionPackage;
392 int mNextAppTransitionEnter;
393 int mNextAppTransitionExit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 boolean mAppTransitionReady = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -0700395 boolean mAppTransitionRunning = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 boolean mAppTransitionTimeout = false;
397 boolean mStartingIconInTransition = false;
398 boolean mSkipAppTransitionAnimation = false;
399 final ArrayList<AppWindowToken> mOpeningApps = new ArrayList<AppWindowToken>();
400 final ArrayList<AppWindowToken> mClosingApps = new ArrayList<AppWindowToken>();
Dianne Hackborna8f60182009-09-01 19:01:50 -0700401 final ArrayList<AppWindowToken> mToTopApps = new ArrayList<AppWindowToken>();
402 final ArrayList<AppWindowToken> mToBottomApps = new ArrayList<AppWindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 //flag to detect fat touch events
405 boolean mFatTouch = false;
406 Display mDisplay;
Romain Guy06882f82009-06-10 13:36:04 -0700407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 H mH = new H();
409
410 WindowState mCurrentFocus = null;
411 WindowState mLastFocus = null;
Romain Guy06882f82009-06-10 13:36:04 -0700412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 // This just indicates the window the input method is on top of, not
414 // necessarily the window its input is going to.
415 WindowState mInputMethodTarget = null;
416 WindowState mUpcomingInputMethodTarget = null;
417 boolean mInputMethodTargetWaitingAnim;
418 int mInputMethodAnimLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -0700419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 WindowState mInputMethodWindow = null;
421 final ArrayList<WindowState> mInputMethodDialogs = new ArrayList<WindowState>();
422
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700423 final ArrayList<WindowToken> mWallpaperTokens = new ArrayList<WindowToken>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800424
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700425 // If non-null, this is the currently visible window that is associated
426 // with the wallpaper.
427 WindowState mWallpaperTarget = null;
Dianne Hackborn3be63c02009-08-20 19:31:38 -0700428 // If non-null, we are in the middle of animating from one wallpaper target
429 // to another, and this is the lower one in Z-order.
430 WindowState mLowerWallpaperTarget = null;
431 // If non-null, we are in the middle of animating from one wallpaper target
432 // to another, and this is the higher one in Z-order.
433 WindowState mUpperWallpaperTarget = null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700434 int mWallpaperAnimLayerAdjustment;
Dianne Hackborn73e92b42009-10-15 14:29:19 -0700435 float mLastWallpaperX = -1;
436 float mLastWallpaperY = -1;
Marco Nelissenbf6956b2009-11-09 15:21:13 -0800437 float mLastWallpaperXStep = -1;
438 float mLastWallpaperYStep = -1;
Dianne Hackborn6adba242009-11-10 11:10:09 -0800439 boolean mSendingPointersToWallpaper = false;
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700440 // This is set when we are waiting for a wallpaper to tell us it is done
441 // changing its scroll position.
442 WindowState mWaitingOnWallpaper;
443 // The last time we had a timeout when waiting for a wallpaper.
444 long mLastWallpaperTimeoutTime;
445 // We give a wallpaper up to 150ms to finish scrolling.
446 static final long WALLPAPER_TIMEOUT = 150;
447 // Time we wait after a timeout before trying to wait again.
448 static final long WALLPAPER_TIMEOUT_RECOVERY = 10000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 AppWindowToken mFocusedApp = null;
451
452 PowerManagerService mPowerManager;
Romain Guy06882f82009-06-10 13:36:04 -0700453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 float mWindowAnimationScale = 1.0f;
455 float mTransitionAnimationScale = 1.0f;
Romain Guy06882f82009-06-10 13:36:04 -0700456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 final KeyWaiter mKeyWaiter = new KeyWaiter();
458 final KeyQ mQueue;
459 final InputDispatcherThread mInputThread;
460
461 // Who is holding the screen on.
462 Session mHoldingScreenOn;
Romain Guy06882f82009-06-10 13:36:04 -0700463
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700464 boolean mTurnOnScreen;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 /**
467 * Whether the UI is currently running in touch mode (not showing
468 * navigational focus because the user is directly pressing the screen).
469 */
470 boolean mInTouchMode = false;
471
472 private ViewServer mViewServer;
473
474 final Rect mTempRect = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -0700475
Dianne Hackbornc485a602009-03-24 22:39:49 -0700476 final Configuration mTempConfiguration = new Configuration();
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700477 int mScreenLayout = Configuration.SCREENLAYOUT_SIZE_UNDEFINED;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -0700478
479 // The frame use to limit the size of the app running in compatibility mode.
480 Rect mCompatibleScreenFrame = new Rect();
481 // The surface used to fill the outer rim of the app running in compatibility mode.
482 Surface mBackgroundFillerSurface = null;
483 boolean mBackgroundFillerShown = false;
484
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 public static WindowManagerService main(Context context,
486 PowerManagerService pm, boolean haveInputMethods) {
487 WMThread thr = new WMThread(context, pm, haveInputMethods);
488 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 synchronized (thr) {
491 while (thr.mService == null) {
492 try {
493 thr.wait();
494 } catch (InterruptedException e) {
495 }
496 }
497 }
Romain Guy06882f82009-06-10 13:36:04 -0700498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 return thr.mService;
500 }
Romain Guy06882f82009-06-10 13:36:04 -0700501
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 static class WMThread extends Thread {
503 WindowManagerService mService;
Romain Guy06882f82009-06-10 13:36:04 -0700504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 private final Context mContext;
506 private final PowerManagerService mPM;
507 private final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 public WMThread(Context context, PowerManagerService pm,
510 boolean haveInputMethods) {
511 super("WindowManager");
512 mContext = context;
513 mPM = pm;
514 mHaveInputMethods = haveInputMethods;
515 }
Romain Guy06882f82009-06-10 13:36:04 -0700516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 public void run() {
518 Looper.prepare();
519 WindowManagerService s = new WindowManagerService(mContext, mPM,
520 mHaveInputMethods);
521 android.os.Process.setThreadPriority(
522 android.os.Process.THREAD_PRIORITY_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -0700523
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 synchronized (this) {
525 mService = s;
526 notifyAll();
527 }
Romain Guy06882f82009-06-10 13:36:04 -0700528
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 Looper.loop();
530 }
531 }
532
533 static class PolicyThread extends Thread {
534 private final WindowManagerPolicy mPolicy;
535 private final WindowManagerService mService;
536 private final Context mContext;
537 private final PowerManagerService mPM;
538 boolean mRunning = false;
Romain Guy06882f82009-06-10 13:36:04 -0700539
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 public PolicyThread(WindowManagerPolicy policy,
541 WindowManagerService service, Context context,
542 PowerManagerService pm) {
543 super("WindowManagerPolicy");
544 mPolicy = policy;
545 mService = service;
546 mContext = context;
547 mPM = pm;
548 }
Romain Guy06882f82009-06-10 13:36:04 -0700549
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 public void run() {
551 Looper.prepare();
552 //Looper.myLooper().setMessageLogging(new LogPrinter(
553 // Log.VERBOSE, "WindowManagerPolicy"));
554 android.os.Process.setThreadPriority(
555 android.os.Process.THREAD_PRIORITY_FOREGROUND);
556 mPolicy.init(mContext, mService, mPM);
Romain Guy06882f82009-06-10 13:36:04 -0700557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 synchronized (this) {
559 mRunning = true;
560 notifyAll();
561 }
Romain Guy06882f82009-06-10 13:36:04 -0700562
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 Looper.loop();
564 }
565 }
566
567 private WindowManagerService(Context context, PowerManagerService pm,
568 boolean haveInputMethods) {
Michael Chan53071d62009-05-13 17:29:48 -0700569 if (MEASURE_LATENCY) {
570 lt = new LatencyTimer(100, 1000);
571 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800572
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573 mContext = context;
574 mHaveInputMethods = haveInputMethods;
575 mLimitedAlphaCompositing = context.getResources().getBoolean(
576 com.android.internal.R.bool.config_sf_limitedAlpha);
Romain Guy06882f82009-06-10 13:36:04 -0700577
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 mPowerManager = pm;
579 mPowerManager.setPolicy(mPolicy);
580 PowerManager pmc = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
581 mScreenFrozenLock = pmc.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
582 "SCREEN_FROZEN");
583 mScreenFrozenLock.setReferenceCounted(false);
584
585 mActivityManager = ActivityManagerNative.getDefault();
586 mBatteryStats = BatteryStatsService.getService();
587
588 // Get persisted window scale setting
589 mWindowAnimationScale = Settings.System.getFloat(context.getContentResolver(),
590 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
591 mTransitionAnimationScale = Settings.System.getFloat(context.getContentResolver(),
592 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
Romain Guy06882f82009-06-10 13:36:04 -0700593
Michael Chan9f028e62009-08-04 17:37:46 -0700594 int max_events_per_sec = 35;
595 try {
596 max_events_per_sec = Integer.parseInt(SystemProperties
597 .get("windowsmgr.max_events_per_sec"));
598 if (max_events_per_sec < 1) {
599 max_events_per_sec = 35;
600 }
601 } catch (NumberFormatException e) {
602 }
603 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 mQueue = new KeyQ();
606
607 mInputThread = new InputDispatcherThread();
Romain Guy06882f82009-06-10 13:36:04 -0700608
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 PolicyThread thr = new PolicyThread(mPolicy, this, context, pm);
610 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700611
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 synchronized (thr) {
613 while (!thr.mRunning) {
614 try {
615 thr.wait();
616 } catch (InterruptedException e) {
617 }
618 }
619 }
Romain Guy06882f82009-06-10 13:36:04 -0700620
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 mInputThread.start();
Romain Guy06882f82009-06-10 13:36:04 -0700622
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 // Add ourself to the Watchdog monitors.
624 Watchdog.getInstance().addMonitor(this);
625 }
626
627 @Override
628 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
629 throws RemoteException {
630 try {
631 return super.onTransact(code, data, reply, flags);
632 } catch (RuntimeException e) {
633 // The window manager only throws security exceptions, so let's
634 // log all others.
635 if (!(e instanceof SecurityException)) {
636 Log.e(TAG, "Window Manager Crash", e);
637 }
638 throw e;
639 }
640 }
641
642 private void placeWindowAfter(Object pos, WindowState window) {
643 final int i = mWindows.indexOf(pos);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700644 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 TAG, "Adding window " + window + " at "
646 + (i+1) + " of " + mWindows.size() + " (after " + pos + ")");
647 mWindows.add(i+1, window);
648 }
649
650 private void placeWindowBefore(Object pos, WindowState window) {
651 final int i = mWindows.indexOf(pos);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700652 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653 TAG, "Adding window " + window + " at "
654 + i + " of " + mWindows.size() + " (before " + pos + ")");
655 mWindows.add(i, window);
656 }
657
658 //This method finds out the index of a window that has the same app token as
659 //win. used for z ordering the windows in mWindows
660 private int findIdxBasedOnAppTokens(WindowState win) {
661 //use a local variable to cache mWindows
662 ArrayList localmWindows = mWindows;
663 int jmax = localmWindows.size();
664 if(jmax == 0) {
665 return -1;
666 }
667 for(int j = (jmax-1); j >= 0; j--) {
668 WindowState wentry = (WindowState)localmWindows.get(j);
669 if(wentry.mAppToken == win.mAppToken) {
670 return j;
671 }
672 }
673 return -1;
674 }
Romain Guy06882f82009-06-10 13:36:04 -0700675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 private void addWindowToListInOrderLocked(WindowState win, boolean addToToken) {
677 final IWindow client = win.mClient;
678 final WindowToken token = win.mToken;
679 final ArrayList localmWindows = mWindows;
Romain Guy06882f82009-06-10 13:36:04 -0700680
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 final int N = localmWindows.size();
682 final WindowState attached = win.mAttachedWindow;
683 int i;
684 if (attached == null) {
685 int tokenWindowsPos = token.windows.size();
686 if (token.appWindowToken != null) {
687 int index = tokenWindowsPos-1;
688 if (index >= 0) {
689 // If this application has existing windows, we
690 // simply place the new window on top of them... but
691 // keep the starting window on top.
692 if (win.mAttrs.type == TYPE_BASE_APPLICATION) {
693 // Base windows go behind everything else.
694 placeWindowBefore(token.windows.get(0), win);
695 tokenWindowsPos = 0;
696 } else {
697 AppWindowToken atoken = win.mAppToken;
698 if (atoken != null &&
699 token.windows.get(index) == atoken.startingWindow) {
700 placeWindowBefore(token.windows.get(index), win);
701 tokenWindowsPos--;
702 } else {
703 int newIdx = findIdxBasedOnAppTokens(win);
704 if(newIdx != -1) {
Romain Guy06882f82009-06-10 13:36:04 -0700705 //there is a window above this one associated with the same
706 //apptoken note that the window could be a floating window
707 //that was created later or a window at the top of the list of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708 //windows associated with this token.
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700709 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
710 TAG, "Adding window " + win + " at "
711 + (newIdx+1) + " of " + N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712 localmWindows.add(newIdx+1, win);
Romain Guy06882f82009-06-10 13:36:04 -0700713 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800714 }
715 }
716 } else {
717 if (localLOGV) Log.v(
718 TAG, "Figuring out where to add app window "
719 + client.asBinder() + " (token=" + token + ")");
720 // Figure out where the window should go, based on the
721 // order of applications.
722 final int NA = mAppTokens.size();
723 Object pos = null;
724 for (i=NA-1; i>=0; i--) {
725 AppWindowToken t = mAppTokens.get(i);
726 if (t == token) {
727 i--;
728 break;
729 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800730
Dianne Hackborna8f60182009-09-01 19:01:50 -0700731 // We haven't reached the token yet; if this token
732 // is not going to the bottom and has windows, we can
733 // use it as an anchor for when we do reach the token.
734 if (!t.sendingToBottom && t.windows.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735 pos = t.windows.get(0);
736 }
737 }
738 // We now know the index into the apps. If we found
739 // an app window above, that gives us the position; else
740 // we need to look some more.
741 if (pos != null) {
742 // Move behind any windows attached to this one.
Romain Guy06882f82009-06-10 13:36:04 -0700743 WindowToken atoken =
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744 mTokenMap.get(((WindowState)pos).mClient.asBinder());
745 if (atoken != null) {
746 final int NC = atoken.windows.size();
747 if (NC > 0) {
748 WindowState bottom = atoken.windows.get(0);
749 if (bottom.mSubLayer < 0) {
750 pos = bottom;
751 }
752 }
753 }
754 placeWindowBefore(pos, win);
755 } else {
Dianne Hackborna8f60182009-09-01 19:01:50 -0700756 // Continue looking down until we find the first
757 // token that has windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758 while (i >= 0) {
759 AppWindowToken t = mAppTokens.get(i);
760 final int NW = t.windows.size();
761 if (NW > 0) {
762 pos = t.windows.get(NW-1);
763 break;
764 }
765 i--;
766 }
767 if (pos != null) {
768 // Move in front of any windows attached to this
769 // one.
770 WindowToken atoken =
771 mTokenMap.get(((WindowState)pos).mClient.asBinder());
772 if (atoken != null) {
773 final int NC = atoken.windows.size();
774 if (NC > 0) {
775 WindowState top = atoken.windows.get(NC-1);
776 if (top.mSubLayer >= 0) {
777 pos = top;
778 }
779 }
780 }
781 placeWindowAfter(pos, win);
782 } else {
783 // Just search for the start of this layer.
784 final int myLayer = win.mBaseLayer;
785 for (i=0; i<N; i++) {
786 WindowState w = (WindowState)localmWindows.get(i);
787 if (w.mBaseLayer > myLayer) {
788 break;
789 }
790 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700791 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
792 TAG, "Adding window " + win + " at "
793 + i + " of " + N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794 localmWindows.add(i, win);
795 }
796 }
797 }
798 } else {
799 // Figure out where window should go, based on layer.
800 final int myLayer = win.mBaseLayer;
801 for (i=N-1; i>=0; i--) {
802 if (((WindowState)localmWindows.get(i)).mBaseLayer <= myLayer) {
803 i++;
804 break;
805 }
806 }
807 if (i < 0) i = 0;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700808 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
809 TAG, "Adding window " + win + " at "
810 + i + " of " + N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800811 localmWindows.add(i, win);
812 }
813 if (addToToken) {
814 token.windows.add(tokenWindowsPos, win);
815 }
816
817 } else {
818 // Figure out this window's ordering relative to the window
819 // it is attached to.
820 final int NA = token.windows.size();
821 final int sublayer = win.mSubLayer;
822 int largestSublayer = Integer.MIN_VALUE;
823 WindowState windowWithLargestSublayer = null;
824 for (i=0; i<NA; i++) {
825 WindowState w = token.windows.get(i);
826 final int wSublayer = w.mSubLayer;
827 if (wSublayer >= largestSublayer) {
828 largestSublayer = wSublayer;
829 windowWithLargestSublayer = w;
830 }
831 if (sublayer < 0) {
832 // For negative sublayers, we go below all windows
833 // in the same sublayer.
834 if (wSublayer >= sublayer) {
835 if (addToToken) {
836 token.windows.add(i, win);
837 }
838 placeWindowBefore(
839 wSublayer >= 0 ? attached : w, win);
840 break;
841 }
842 } else {
843 // For positive sublayers, we go above all windows
844 // in the same sublayer.
845 if (wSublayer > sublayer) {
846 if (addToToken) {
847 token.windows.add(i, win);
848 }
849 placeWindowBefore(w, win);
850 break;
851 }
852 }
853 }
854 if (i >= NA) {
855 if (addToToken) {
856 token.windows.add(win);
857 }
858 if (sublayer < 0) {
859 placeWindowBefore(attached, win);
860 } else {
861 placeWindowAfter(largestSublayer >= 0
862 ? windowWithLargestSublayer
863 : attached,
864 win);
865 }
866 }
867 }
Romain Guy06882f82009-06-10 13:36:04 -0700868
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800869 if (win.mAppToken != null && addToToken) {
870 win.mAppToken.allAppWindows.add(win);
871 }
872 }
Romain Guy06882f82009-06-10 13:36:04 -0700873
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800874 static boolean canBeImeTarget(WindowState w) {
875 final int fl = w.mAttrs.flags
876 & (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM);
877 if (fl == 0 || fl == (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM)) {
878 return w.isVisibleOrAdding();
879 }
880 return false;
881 }
Romain Guy06882f82009-06-10 13:36:04 -0700882
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800883 int findDesiredInputMethodWindowIndexLocked(boolean willMove) {
884 final ArrayList localmWindows = mWindows;
885 final int N = localmWindows.size();
886 WindowState w = null;
887 int i = N;
888 while (i > 0) {
889 i--;
890 w = (WindowState)localmWindows.get(i);
Romain Guy06882f82009-06-10 13:36:04 -0700891
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800892 //Log.i(TAG, "Checking window @" + i + " " + w + " fl=0x"
893 // + Integer.toHexString(w.mAttrs.flags));
894 if (canBeImeTarget(w)) {
895 //Log.i(TAG, "Putting input method here!");
Romain Guy06882f82009-06-10 13:36:04 -0700896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897 // Yet more tricksyness! If this window is a "starting"
898 // window, we do actually want to be on top of it, but
899 // it is not -really- where input will go. So if the caller
900 // is not actually looking to move the IME, look down below
901 // for a real window to target...
902 if (!willMove
903 && w.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
904 && i > 0) {
905 WindowState wb = (WindowState)localmWindows.get(i-1);
906 if (wb.mAppToken == w.mAppToken && canBeImeTarget(wb)) {
907 i--;
908 w = wb;
909 }
910 }
911 break;
912 }
913 }
Romain Guy06882f82009-06-10 13:36:04 -0700914
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800915 mUpcomingInputMethodTarget = w;
Romain Guy06882f82009-06-10 13:36:04 -0700916
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Desired input method target="
918 + w + " willMove=" + willMove);
Romain Guy06882f82009-06-10 13:36:04 -0700919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920 if (willMove && w != null) {
921 final WindowState curTarget = mInputMethodTarget;
922 if (curTarget != null && curTarget.mAppToken != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700923
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800924 // Now some fun for dealing with window animations that
925 // modify the Z order. We need to look at all windows below
926 // the current target that are in this app, finding the highest
927 // visible one in layering.
928 AppWindowToken token = curTarget.mAppToken;
929 WindowState highestTarget = null;
930 int highestPos = 0;
931 if (token.animating || token.animation != null) {
932 int pos = 0;
933 pos = localmWindows.indexOf(curTarget);
934 while (pos >= 0) {
935 WindowState win = (WindowState)localmWindows.get(pos);
936 if (win.mAppToken != token) {
937 break;
938 }
939 if (!win.mRemoved) {
940 if (highestTarget == null || win.mAnimLayer >
941 highestTarget.mAnimLayer) {
942 highestTarget = win;
943 highestPos = pos;
944 }
945 }
946 pos--;
947 }
948 }
Romain Guy06882f82009-06-10 13:36:04 -0700949
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800950 if (highestTarget != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700951 if (DEBUG_INPUT_METHOD) Log.v(TAG, "mNextAppTransition="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952 + mNextAppTransition + " " + highestTarget
953 + " animating=" + highestTarget.isAnimating()
954 + " layer=" + highestTarget.mAnimLayer
955 + " new layer=" + w.mAnimLayer);
Romain Guy06882f82009-06-10 13:36:04 -0700956
Dianne Hackbornbfe319e2009-09-21 00:34:05 -0700957 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800958 // If we are currently setting up for an animation,
959 // hold everything until we can find out what will happen.
960 mInputMethodTargetWaitingAnim = true;
961 mInputMethodTarget = highestTarget;
962 return highestPos + 1;
963 } else if (highestTarget.isAnimating() &&
964 highestTarget.mAnimLayer > w.mAnimLayer) {
965 // If the window we are currently targeting is involved
966 // with an animation, and it is on top of the next target
967 // we will be over, then hold off on moving until
968 // that is done.
969 mInputMethodTarget = highestTarget;
970 return highestPos + 1;
971 }
972 }
973 }
974 }
Romain Guy06882f82009-06-10 13:36:04 -0700975
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800976 //Log.i(TAG, "Placing input method @" + (i+1));
977 if (w != null) {
978 if (willMove) {
979 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700980 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
982 + mInputMethodTarget + " to " + w, e);
983 mInputMethodTarget = w;
984 if (w.mAppToken != null) {
985 setInputMethodAnimLayerAdjustment(w.mAppToken.animLayerAdjustment);
986 } else {
987 setInputMethodAnimLayerAdjustment(0);
988 }
989 }
990 return i+1;
991 }
992 if (willMove) {
993 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700994 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800995 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
996 + mInputMethodTarget + " to null", e);
997 mInputMethodTarget = null;
998 setInputMethodAnimLayerAdjustment(0);
999 }
1000 return -1;
1001 }
Romain Guy06882f82009-06-10 13:36:04 -07001002
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001003 void addInputMethodWindowToListLocked(WindowState win) {
1004 int pos = findDesiredInputMethodWindowIndexLocked(true);
1005 if (pos >= 0) {
1006 win.mTargetAppToken = mInputMethodTarget.mAppToken;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001007 if (DEBUG_WINDOW_MOVEMENT) Log.v(
1008 TAG, "Adding input method window " + win + " at " + pos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001009 mWindows.add(pos, win);
1010 moveInputMethodDialogsLocked(pos+1);
1011 return;
1012 }
1013 win.mTargetAppToken = null;
1014 addWindowToListInOrderLocked(win, true);
1015 moveInputMethodDialogsLocked(pos);
1016 }
Romain Guy06882f82009-06-10 13:36:04 -07001017
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 void setInputMethodAnimLayerAdjustment(int adj) {
1019 if (DEBUG_LAYERS) Log.v(TAG, "Setting im layer adj to " + adj);
1020 mInputMethodAnimLayerAdjustment = adj;
1021 WindowState imw = mInputMethodWindow;
1022 if (imw != null) {
1023 imw.mAnimLayer = imw.mLayer + adj;
1024 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
1025 + " anim layer: " + imw.mAnimLayer);
1026 int wi = imw.mChildWindows.size();
1027 while (wi > 0) {
1028 wi--;
1029 WindowState cw = (WindowState)imw.mChildWindows.get(wi);
1030 cw.mAnimLayer = cw.mLayer + adj;
1031 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + cw
1032 + " anim layer: " + cw.mAnimLayer);
1033 }
1034 }
1035 int di = mInputMethodDialogs.size();
1036 while (di > 0) {
1037 di --;
1038 imw = mInputMethodDialogs.get(di);
1039 imw.mAnimLayer = imw.mLayer + adj;
1040 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
1041 + " anim layer: " + imw.mAnimLayer);
1042 }
1043 }
Romain Guy06882f82009-06-10 13:36:04 -07001044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 private int tmpRemoveWindowLocked(int interestingPos, WindowState win) {
1046 int wpos = mWindows.indexOf(win);
1047 if (wpos >= 0) {
1048 if (wpos < interestingPos) interestingPos--;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001049 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Temp removing at " + wpos + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001050 mWindows.remove(wpos);
1051 int NC = win.mChildWindows.size();
1052 while (NC > 0) {
1053 NC--;
1054 WindowState cw = (WindowState)win.mChildWindows.get(NC);
1055 int cpos = mWindows.indexOf(cw);
1056 if (cpos >= 0) {
1057 if (cpos < interestingPos) interestingPos--;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001058 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Temp removing child at "
1059 + cpos + ": " + cw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 mWindows.remove(cpos);
1061 }
1062 }
1063 }
1064 return interestingPos;
1065 }
Romain Guy06882f82009-06-10 13:36:04 -07001066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001067 private void reAddWindowToListInOrderLocked(WindowState win) {
1068 addWindowToListInOrderLocked(win, false);
1069 // This is a hack to get all of the child windows added as well
1070 // at the right position. Child windows should be rare and
1071 // this case should be rare, so it shouldn't be that big a deal.
1072 int wpos = mWindows.indexOf(win);
1073 if (wpos >= 0) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001074 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "ReAdd removing from " + wpos
1075 + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076 mWindows.remove(wpos);
1077 reAddWindowLocked(wpos, win);
1078 }
1079 }
Romain Guy06882f82009-06-10 13:36:04 -07001080
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081 void logWindowList(String prefix) {
1082 int N = mWindows.size();
1083 while (N > 0) {
1084 N--;
1085 Log.v(TAG, prefix + "#" + N + ": " + mWindows.get(N));
1086 }
1087 }
Romain Guy06882f82009-06-10 13:36:04 -07001088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001089 void moveInputMethodDialogsLocked(int pos) {
1090 ArrayList<WindowState> dialogs = mInputMethodDialogs;
Romain Guy06882f82009-06-10 13:36:04 -07001091
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 final int N = dialogs.size();
1093 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Removing " + N + " dialogs w/pos=" + pos);
1094 for (int i=0; i<N; i++) {
1095 pos = tmpRemoveWindowLocked(pos, dialogs.get(i));
1096 }
1097 if (DEBUG_INPUT_METHOD) {
1098 Log.v(TAG, "Window list w/pos=" + pos);
1099 logWindowList(" ");
1100 }
Romain Guy06882f82009-06-10 13:36:04 -07001101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001102 if (pos >= 0) {
1103 final AppWindowToken targetAppToken = mInputMethodTarget.mAppToken;
1104 if (pos < mWindows.size()) {
1105 WindowState wp = (WindowState)mWindows.get(pos);
1106 if (wp == mInputMethodWindow) {
1107 pos++;
1108 }
1109 }
1110 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Adding " + N + " dialogs at pos=" + pos);
1111 for (int i=0; i<N; i++) {
1112 WindowState win = dialogs.get(i);
1113 win.mTargetAppToken = targetAppToken;
1114 pos = reAddWindowLocked(pos, win);
1115 }
1116 if (DEBUG_INPUT_METHOD) {
1117 Log.v(TAG, "Final window list:");
1118 logWindowList(" ");
1119 }
1120 return;
1121 }
1122 for (int i=0; i<N; i++) {
1123 WindowState win = dialogs.get(i);
1124 win.mTargetAppToken = null;
1125 reAddWindowToListInOrderLocked(win);
1126 if (DEBUG_INPUT_METHOD) {
1127 Log.v(TAG, "No IM target, final list:");
1128 logWindowList(" ");
1129 }
1130 }
1131 }
Romain Guy06882f82009-06-10 13:36:04 -07001132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001133 boolean moveInputMethodWindowsIfNeededLocked(boolean needAssignLayers) {
1134 final WindowState imWin = mInputMethodWindow;
1135 final int DN = mInputMethodDialogs.size();
1136 if (imWin == null && DN == 0) {
1137 return false;
1138 }
Romain Guy06882f82009-06-10 13:36:04 -07001139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140 int imPos = findDesiredInputMethodWindowIndexLocked(true);
1141 if (imPos >= 0) {
1142 // In this case, the input method windows are to be placed
1143 // immediately above the window they are targeting.
Romain Guy06882f82009-06-10 13:36:04 -07001144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145 // First check to see if the input method windows are already
1146 // located here, and contiguous.
1147 final int N = mWindows.size();
1148 WindowState firstImWin = imPos < N
1149 ? (WindowState)mWindows.get(imPos) : null;
Romain Guy06882f82009-06-10 13:36:04 -07001150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 // Figure out the actual input method window that should be
1152 // at the bottom of their stack.
1153 WindowState baseImWin = imWin != null
1154 ? imWin : mInputMethodDialogs.get(0);
1155 if (baseImWin.mChildWindows.size() > 0) {
1156 WindowState cw = (WindowState)baseImWin.mChildWindows.get(0);
1157 if (cw.mSubLayer < 0) baseImWin = cw;
1158 }
Romain Guy06882f82009-06-10 13:36:04 -07001159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 if (firstImWin == baseImWin) {
1161 // The windows haven't moved... but are they still contiguous?
1162 // First find the top IM window.
1163 int pos = imPos+1;
1164 while (pos < N) {
1165 if (!((WindowState)mWindows.get(pos)).mIsImWindow) {
1166 break;
1167 }
1168 pos++;
1169 }
1170 pos++;
1171 // Now there should be no more input method windows above.
1172 while (pos < N) {
1173 if (((WindowState)mWindows.get(pos)).mIsImWindow) {
1174 break;
1175 }
1176 pos++;
1177 }
1178 if (pos >= N) {
1179 // All is good!
1180 return false;
1181 }
1182 }
Romain Guy06882f82009-06-10 13:36:04 -07001183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 if (imWin != null) {
1185 if (DEBUG_INPUT_METHOD) {
1186 Log.v(TAG, "Moving IM from " + imPos);
1187 logWindowList(" ");
1188 }
1189 imPos = tmpRemoveWindowLocked(imPos, imWin);
1190 if (DEBUG_INPUT_METHOD) {
1191 Log.v(TAG, "List after moving with new pos " + imPos + ":");
1192 logWindowList(" ");
1193 }
1194 imWin.mTargetAppToken = mInputMethodTarget.mAppToken;
1195 reAddWindowLocked(imPos, imWin);
1196 if (DEBUG_INPUT_METHOD) {
1197 Log.v(TAG, "List after moving IM to " + imPos + ":");
1198 logWindowList(" ");
1199 }
1200 if (DN > 0) moveInputMethodDialogsLocked(imPos+1);
1201 } else {
1202 moveInputMethodDialogsLocked(imPos);
1203 }
Romain Guy06882f82009-06-10 13:36:04 -07001204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 } else {
1206 // In this case, the input method windows go in a fixed layer,
1207 // because they aren't currently associated with a focus window.
Romain Guy06882f82009-06-10 13:36:04 -07001208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 if (imWin != null) {
1210 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Moving IM from " + imPos);
1211 tmpRemoveWindowLocked(0, imWin);
1212 imWin.mTargetAppToken = null;
1213 reAddWindowToListInOrderLocked(imWin);
1214 if (DEBUG_INPUT_METHOD) {
1215 Log.v(TAG, "List with no IM target:");
1216 logWindowList(" ");
1217 }
1218 if (DN > 0) moveInputMethodDialogsLocked(-1);;
1219 } else {
1220 moveInputMethodDialogsLocked(-1);;
1221 }
Romain Guy06882f82009-06-10 13:36:04 -07001222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 }
Romain Guy06882f82009-06-10 13:36:04 -07001224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001225 if (needAssignLayers) {
1226 assignLayersLocked();
1227 }
Romain Guy06882f82009-06-10 13:36:04 -07001228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 return true;
1230 }
Romain Guy06882f82009-06-10 13:36:04 -07001231
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 void adjustInputMethodDialogsLocked() {
1233 moveInputMethodDialogsLocked(findDesiredInputMethodWindowIndexLocked(true));
1234 }
Romain Guy06882f82009-06-10 13:36:04 -07001235
Dianne Hackborn25994b42009-09-04 14:21:19 -07001236 final boolean isWallpaperVisible(WindowState wallpaperTarget) {
1237 if (DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper vis: target obscured="
1238 + (wallpaperTarget != null ? Boolean.toString(wallpaperTarget.mObscured) : "??")
1239 + " anim=" + ((wallpaperTarget != null && wallpaperTarget.mAppToken != null)
1240 ? wallpaperTarget.mAppToken.animation : null)
1241 + " upper=" + mUpperWallpaperTarget
1242 + " lower=" + mLowerWallpaperTarget);
1243 return (wallpaperTarget != null
1244 && (!wallpaperTarget.mObscured || (wallpaperTarget.mAppToken != null
1245 && wallpaperTarget.mAppToken.animation != null)))
1246 || mUpperWallpaperTarget != null
1247 || mLowerWallpaperTarget != null;
1248 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001249
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001250 static final int ADJUST_WALLPAPER_LAYERS_CHANGED = 1<<1;
1251 static final int ADJUST_WALLPAPER_VISIBILITY_CHANGED = 1<<2;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001252
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001253 int adjustWallpaperWindowsLocked() {
1254 int changed = 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001255
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001256 final int dw = mDisplay.getWidth();
1257 final int dh = mDisplay.getHeight();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001258
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001259 // First find top-most window that has asked to be on top of the
1260 // wallpaper; all wallpapers go behind it.
1261 final ArrayList localmWindows = mWindows;
1262 int N = localmWindows.size();
1263 WindowState w = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001264 WindowState foundW = null;
1265 int foundI = 0;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001266 WindowState topCurW = null;
1267 int topCurI = 0;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001268 int i = N;
1269 while (i > 0) {
1270 i--;
1271 w = (WindowState)localmWindows.get(i);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001272 if ((w.mAttrs.type == WindowManager.LayoutParams.TYPE_WALLPAPER)) {
1273 if (topCurW == null) {
1274 topCurW = w;
1275 topCurI = i;
1276 }
1277 continue;
1278 }
1279 topCurW = null;
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001280 if (w.mAppToken != null) {
1281 // If this window's app token is hidden and not animating,
1282 // it is of no interest to us.
1283 if (w.mAppToken.hidden && w.mAppToken.animation == null) {
1284 if (DEBUG_WALLPAPER) Log.v(TAG,
1285 "Skipping hidden or animating token: " + w);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001286 topCurW = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001287 continue;
1288 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001289 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001290 if (DEBUG_WALLPAPER) Log.v(TAG, "Win " + w + ": readyfordisplay="
1291 + w.isReadyForDisplay() + " drawpending=" + w.mDrawPending
1292 + " commitdrawpending=" + w.mCommitDrawPending);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001293 if ((w.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0 && w.isReadyForDisplay()
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07001294 && (mWallpaperTarget == w
1295 || (!w.mDrawPending && !w.mCommitDrawPending))) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001296 if (DEBUG_WALLPAPER) Log.v(TAG,
1297 "Found wallpaper activity: #" + i + "=" + w);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001298 foundW = w;
1299 foundI = i;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001300 if (w == mWallpaperTarget && ((w.mAppToken != null
1301 && w.mAppToken.animation != null)
1302 || w.mAnimation != null)) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001303 // The current wallpaper target is animating, so we'll
1304 // look behind it for another possible target and figure
1305 // out what is going on below.
1306 if (DEBUG_WALLPAPER) Log.v(TAG, "Win " + w
1307 + ": token animating, looking behind.");
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001308 continue;
1309 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001310 break;
1311 }
1312 }
1313
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07001314 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001315 // If we are currently waiting for an app transition, and either
1316 // the current target or the next target are involved with it,
1317 // then hold off on doing anything with the wallpaper.
1318 // Note that we are checking here for just whether the target
1319 // is part of an app token... which is potentially overly aggressive
1320 // (the app token may not be involved in the transition), but good
1321 // enough (we'll just wait until whatever transition is pending
1322 // executes).
1323 if (mWallpaperTarget != null && mWallpaperTarget.mAppToken != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001324 if (DEBUG_WALLPAPER) Log.v(TAG,
1325 "Wallpaper not changing: waiting for app anim in current target");
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001326 return 0;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001327 }
1328 if (foundW != null && foundW.mAppToken != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001329 if (DEBUG_WALLPAPER) Log.v(TAG,
1330 "Wallpaper not changing: waiting for app anim in found target");
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001331 return 0;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001332 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001333 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001334
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001335 if (mWallpaperTarget != foundW) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001336 if (DEBUG_WALLPAPER) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001337 Log.v(TAG, "New wallpaper target: " + foundW
1338 + " oldTarget: " + mWallpaperTarget);
1339 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001340
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001341 mLowerWallpaperTarget = null;
1342 mUpperWallpaperTarget = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001343
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001344 WindowState oldW = mWallpaperTarget;
1345 mWallpaperTarget = foundW;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001346
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001347 // Now what is happening... if the current and new targets are
1348 // animating, then we are in our super special mode!
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001349 if (foundW != null && oldW != null) {
1350 boolean oldAnim = oldW.mAnimation != null
1351 || (oldW.mAppToken != null && oldW.mAppToken.animation != null);
1352 boolean foundAnim = foundW.mAnimation != null
1353 || (foundW.mAppToken != null && foundW.mAppToken.animation != null);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001354 if (DEBUG_WALLPAPER) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001355 Log.v(TAG, "New animation: " + foundAnim
1356 + " old animation: " + oldAnim);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001357 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001358 if (foundAnim && oldAnim) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001359 int oldI = localmWindows.indexOf(oldW);
1360 if (DEBUG_WALLPAPER) {
1361 Log.v(TAG, "New i: " + foundI + " old i: " + oldI);
1362 }
1363 if (oldI >= 0) {
1364 if (DEBUG_WALLPAPER) {
1365 Log.v(TAG, "Animating wallpapers: old#" + oldI
1366 + "=" + oldW + "; new#" + foundI
1367 + "=" + foundW);
1368 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001369
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001370 // Set the new target correctly.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001371 if (foundW.mAppToken != null && foundW.mAppToken.hiddenRequested) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001372 if (DEBUG_WALLPAPER) {
1373 Log.v(TAG, "Old wallpaper still the target.");
1374 }
1375 mWallpaperTarget = oldW;
1376 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001377
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001378 // Now set the upper and lower wallpaper targets
1379 // correctly, and make sure that we are positioning
1380 // the wallpaper below the lower.
1381 if (foundI > oldI) {
1382 // The new target is on top of the old one.
1383 if (DEBUG_WALLPAPER) {
1384 Log.v(TAG, "Found target above old target.");
1385 }
1386 mUpperWallpaperTarget = foundW;
1387 mLowerWallpaperTarget = oldW;
1388 foundW = oldW;
1389 foundI = oldI;
1390 } else {
1391 // The new target is below the old one.
1392 if (DEBUG_WALLPAPER) {
1393 Log.v(TAG, "Found target below old target.");
1394 }
1395 mUpperWallpaperTarget = oldW;
1396 mLowerWallpaperTarget = foundW;
1397 }
1398 }
1399 }
1400 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001401
Dianne Hackborn6b1cb352009-09-28 18:27:26 -07001402 } else if (mLowerWallpaperTarget != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001403 // Is it time to stop animating?
Dianne Hackborn6b1cb352009-09-28 18:27:26 -07001404 boolean lowerAnimating = mLowerWallpaperTarget.mAnimation != null
1405 || (mLowerWallpaperTarget.mAppToken != null
1406 && mLowerWallpaperTarget.mAppToken.animation != null);
1407 boolean upperAnimating = mUpperWallpaperTarget.mAnimation != null
1408 || (mUpperWallpaperTarget.mAppToken != null
1409 && mUpperWallpaperTarget.mAppToken.animation != null);
1410 if (!lowerAnimating || !upperAnimating) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001411 if (DEBUG_WALLPAPER) {
1412 Log.v(TAG, "No longer animating wallpaper targets!");
1413 }
1414 mLowerWallpaperTarget = null;
1415 mUpperWallpaperTarget = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001416 }
1417 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001418
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001419 boolean visible = foundW != null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001420 if (visible) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001421 // The window is visible to the compositor... but is it visible
1422 // to the user? That is what the wallpaper cares about.
Dianne Hackborn25994b42009-09-04 14:21:19 -07001423 visible = isWallpaperVisible(foundW);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001424 if (DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper visibility: " + visible);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001425
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001426 // If the wallpaper target is animating, we may need to copy
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001427 // its layer adjustment. Only do this if we are not transfering
1428 // between two wallpaper targets.
1429 mWallpaperAnimLayerAdjustment =
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001430 (mLowerWallpaperTarget == null && foundW.mAppToken != null)
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001431 ? foundW.mAppToken.animLayerAdjustment : 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001432
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001433 final int maxLayer = mPolicy.getMaxWallpaperLayer()
1434 * TYPE_LAYER_MULTIPLIER
1435 + TYPE_LAYER_OFFSET;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001436
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001437 // Now w is the window we are supposed to be behind... but we
1438 // need to be sure to also be behind any of its attached windows,
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001439 // AND any starting window associated with it, AND below the
1440 // maximum layer the policy allows for wallpapers.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001441 while (foundI > 0) {
1442 WindowState wb = (WindowState)localmWindows.get(foundI-1);
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001443 if (wb.mBaseLayer < maxLayer &&
1444 wb.mAttachedWindow != foundW &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001445 (wb.mAttrs.type != TYPE_APPLICATION_STARTING ||
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001446 wb.mToken != foundW.mToken)) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001447 // This window is not related to the previous one in any
1448 // interesting way, so stop here.
1449 break;
1450 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001451 foundW = wb;
1452 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001453 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001454 } else {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001455 if (DEBUG_WALLPAPER) Log.v(TAG, "No wallpaper target");
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001456 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001457
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001458 if (foundW == null && topCurW != null) {
1459 // There is no wallpaper target, so it goes at the bottom.
1460 // We will assume it is the same place as last time, if known.
1461 foundW = topCurW;
1462 foundI = topCurI+1;
1463 } else {
1464 // Okay i is the position immediately above the wallpaper. Look at
1465 // what is below it for later.
1466 foundW = foundI > 0 ? (WindowState)localmWindows.get(foundI-1) : null;
1467 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001468
Dianne Hackborn284ac932009-08-28 10:34:25 -07001469 if (visible) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001470 if (mWallpaperTarget.mWallpaperX >= 0) {
1471 mLastWallpaperX = mWallpaperTarget.mWallpaperX;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001472 mLastWallpaperXStep = mWallpaperTarget.mWallpaperXStep;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001473 }
1474 if (mWallpaperTarget.mWallpaperY >= 0) {
1475 mLastWallpaperY = mWallpaperTarget.mWallpaperY;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001476 mLastWallpaperYStep = mWallpaperTarget.mWallpaperYStep;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001477 }
Dianne Hackborn284ac932009-08-28 10:34:25 -07001478 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001479
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001480 // Start stepping backwards from here, ensuring that our wallpaper windows
1481 // are correctly placed.
1482 int curTokenIndex = mWallpaperTokens.size();
1483 while (curTokenIndex > 0) {
1484 curTokenIndex--;
1485 WindowToken token = mWallpaperTokens.get(curTokenIndex);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001486 if (token.hidden == visible) {
1487 changed |= ADJUST_WALLPAPER_VISIBILITY_CHANGED;
1488 token.hidden = !visible;
1489 // Need to do a layout to ensure the wallpaper now has the
1490 // correct size.
1491 mLayoutNeeded = true;
1492 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001493
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001494 int curWallpaperIndex = token.windows.size();
1495 while (curWallpaperIndex > 0) {
1496 curWallpaperIndex--;
1497 WindowState wallpaper = token.windows.get(curWallpaperIndex);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001498
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001499 if (visible) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001500 updateWallpaperOffsetLocked(wallpaper, dw, dh, false);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001501 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001502
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001503 // First, make sure the client has the current visibility
1504 // state.
1505 if (wallpaper.mWallpaperVisible != visible) {
1506 wallpaper.mWallpaperVisible = visible;
1507 try {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001508 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Log.v(TAG,
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001509 "Setting visibility of wallpaper " + wallpaper
1510 + ": " + visible);
1511 wallpaper.mClient.dispatchAppVisibility(visible);
1512 } catch (RemoteException e) {
1513 }
1514 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001515
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001516 wallpaper.mAnimLayer = wallpaper.mLayer + mWallpaperAnimLayerAdjustment;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001517 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper win "
1518 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001519
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001520 // First, if this window is at the current index, then all
1521 // is well.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001522 if (wallpaper == foundW) {
1523 foundI--;
1524 foundW = foundI > 0
1525 ? (WindowState)localmWindows.get(foundI-1) : null;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001526 continue;
1527 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001528
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001529 // The window didn't match... the current wallpaper window,
1530 // wherever it is, is in the wrong place, so make sure it is
1531 // not in the list.
1532 int oldIndex = localmWindows.indexOf(wallpaper);
1533 if (oldIndex >= 0) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001534 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Wallpaper removing at "
1535 + oldIndex + ": " + wallpaper);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001536 localmWindows.remove(oldIndex);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001537 if (oldIndex < foundI) {
1538 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001539 }
1540 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001541
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001542 // Now stick it in.
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001543 if (DEBUG_WALLPAPER || DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
1544 "Moving wallpaper " + wallpaper
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001545 + " from " + oldIndex + " to " + foundI);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001546
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001547 localmWindows.add(foundI, wallpaper);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001548 changed |= ADJUST_WALLPAPER_LAYERS_CHANGED;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001549 }
1550 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001551
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001552 return changed;
1553 }
1554
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001555 void setWallpaperAnimLayerAdjustmentLocked(int adj) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001556 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG,
1557 "Setting wallpaper layer adj to " + adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001558 mWallpaperAnimLayerAdjustment = adj;
1559 int curTokenIndex = mWallpaperTokens.size();
1560 while (curTokenIndex > 0) {
1561 curTokenIndex--;
1562 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1563 int curWallpaperIndex = token.windows.size();
1564 while (curWallpaperIndex > 0) {
1565 curWallpaperIndex--;
1566 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1567 wallpaper.mAnimLayer = wallpaper.mLayer + adj;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001568 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper win "
1569 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001570 }
1571 }
1572 }
1573
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001574 boolean updateWallpaperOffsetLocked(WindowState wallpaperWin, int dw, int dh,
1575 boolean sync) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001576 boolean changed = false;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001577 boolean rawChanged = false;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001578 float wpx = mLastWallpaperX >= 0 ? mLastWallpaperX : 0.5f;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001579 float wpxs = mLastWallpaperXStep >= 0 ? mLastWallpaperXStep : -1.0f;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001580 int availw = wallpaperWin.mFrame.right-wallpaperWin.mFrame.left-dw;
1581 int offset = availw > 0 ? -(int)(availw*wpx+.5f) : 0;
1582 changed = wallpaperWin.mXOffset != offset;
1583 if (changed) {
1584 if (DEBUG_WALLPAPER) Log.v(TAG, "Update wallpaper "
1585 + wallpaperWin + " x: " + offset);
1586 wallpaperWin.mXOffset = offset;
1587 }
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001588 if (wallpaperWin.mWallpaperX != wpx || wallpaperWin.mWallpaperXStep != wpxs) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001589 wallpaperWin.mWallpaperX = wpx;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001590 wallpaperWin.mWallpaperXStep = wpxs;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001591 rawChanged = true;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001592 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001593
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001594 float wpy = mLastWallpaperY >= 0 ? mLastWallpaperY : 0.5f;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001595 float wpys = mLastWallpaperYStep >= 0 ? mLastWallpaperYStep : -1.0f;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001596 int availh = wallpaperWin.mFrame.bottom-wallpaperWin.mFrame.top-dh;
1597 offset = availh > 0 ? -(int)(availh*wpy+.5f) : 0;
1598 if (wallpaperWin.mYOffset != offset) {
1599 if (DEBUG_WALLPAPER) Log.v(TAG, "Update wallpaper "
1600 + wallpaperWin + " y: " + offset);
1601 changed = true;
1602 wallpaperWin.mYOffset = offset;
1603 }
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001604 if (wallpaperWin.mWallpaperY != wpy || wallpaperWin.mWallpaperYStep != wpys) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001605 wallpaperWin.mWallpaperY = wpy;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001606 wallpaperWin.mWallpaperYStep = wpys;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001607 rawChanged = true;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001608 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001609
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001610 if (rawChanged) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001611 try {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001612 if (DEBUG_WALLPAPER) Log.v(TAG, "Report new wp offset "
1613 + wallpaperWin + " x=" + wallpaperWin.mWallpaperX
1614 + " y=" + wallpaperWin.mWallpaperY);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001615 if (sync) {
Dianne Hackborn75804932009-10-20 20:15:20 -07001616 mWaitingOnWallpaper = wallpaperWin;
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001617 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001618 wallpaperWin.mClient.dispatchWallpaperOffsets(
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001619 wallpaperWin.mWallpaperX, wallpaperWin.mWallpaperY,
1620 wallpaperWin.mWallpaperXStep, wallpaperWin.mWallpaperYStep, sync);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001621 if (sync) {
Dianne Hackborn75804932009-10-20 20:15:20 -07001622 if (mWaitingOnWallpaper != null) {
1623 long start = SystemClock.uptimeMillis();
1624 if ((mLastWallpaperTimeoutTime+WALLPAPER_TIMEOUT_RECOVERY)
1625 < start) {
1626 try {
1627 if (DEBUG_WALLPAPER) Log.v(TAG,
1628 "Waiting for offset complete...");
1629 mWindowMap.wait(WALLPAPER_TIMEOUT);
1630 } catch (InterruptedException e) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001631 }
Dianne Hackborn75804932009-10-20 20:15:20 -07001632 if (DEBUG_WALLPAPER) Log.v(TAG, "Offset complete!");
1633 if ((start+WALLPAPER_TIMEOUT)
1634 < SystemClock.uptimeMillis()) {
1635 Log.i(TAG, "Timeout waiting for wallpaper to offset: "
1636 + wallpaperWin);
1637 mLastWallpaperTimeoutTime = start;
1638 }
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001639 }
Dianne Hackborn75804932009-10-20 20:15:20 -07001640 mWaitingOnWallpaper = null;
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001641 }
1642 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001643 } catch (RemoteException e) {
1644 }
1645 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001646
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001647 return changed;
1648 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001649
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001650 void wallpaperOffsetsComplete(IBinder window) {
Dianne Hackborn75804932009-10-20 20:15:20 -07001651 synchronized (mWindowMap) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001652 if (mWaitingOnWallpaper != null &&
1653 mWaitingOnWallpaper.mClient.asBinder() == window) {
1654 mWaitingOnWallpaper = null;
Dianne Hackborn75804932009-10-20 20:15:20 -07001655 mWindowMap.notifyAll();
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001656 }
1657 }
1658 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001659
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001660 boolean updateWallpaperOffsetLocked(WindowState changingTarget, boolean sync) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001661 final int dw = mDisplay.getWidth();
1662 final int dh = mDisplay.getHeight();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001663
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001664 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001665
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001666 WindowState target = mWallpaperTarget;
1667 if (target != null) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001668 if (target.mWallpaperX >= 0) {
1669 mLastWallpaperX = target.mWallpaperX;
1670 } else if (changingTarget.mWallpaperX >= 0) {
1671 mLastWallpaperX = changingTarget.mWallpaperX;
1672 }
1673 if (target.mWallpaperY >= 0) {
1674 mLastWallpaperY = target.mWallpaperY;
1675 } else if (changingTarget.mWallpaperY >= 0) {
1676 mLastWallpaperY = changingTarget.mWallpaperY;
1677 }
1678 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001679
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001680 int curTokenIndex = mWallpaperTokens.size();
1681 while (curTokenIndex > 0) {
1682 curTokenIndex--;
1683 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1684 int curWallpaperIndex = token.windows.size();
1685 while (curWallpaperIndex > 0) {
1686 curWallpaperIndex--;
1687 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1688 if (updateWallpaperOffsetLocked(wallpaper, dw, dh, sync)) {
1689 wallpaper.computeShownFrameLocked();
1690 changed = true;
1691 // We only want to be synchronous with one wallpaper.
1692 sync = false;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001693 }
1694 }
1695 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001696
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001697 return changed;
1698 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001699
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001700 void updateWallpaperVisibilityLocked() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07001701 final boolean visible = isWallpaperVisible(mWallpaperTarget);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001702 final int dw = mDisplay.getWidth();
1703 final int dh = mDisplay.getHeight();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001704
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001705 int curTokenIndex = mWallpaperTokens.size();
1706 while (curTokenIndex > 0) {
1707 curTokenIndex--;
1708 WindowToken token = mWallpaperTokens.get(curTokenIndex);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001709 if (token.hidden == visible) {
1710 token.hidden = !visible;
1711 // Need to do a layout to ensure the wallpaper now has the
1712 // correct size.
1713 mLayoutNeeded = true;
1714 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001715
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001716 int curWallpaperIndex = token.windows.size();
1717 while (curWallpaperIndex > 0) {
1718 curWallpaperIndex--;
1719 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1720 if (visible) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001721 updateWallpaperOffsetLocked(wallpaper, dw, dh, false);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001722 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001723
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001724 if (wallpaper.mWallpaperVisible != visible) {
1725 wallpaper.mWallpaperVisible = visible;
1726 try {
1727 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Log.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -07001728 "Updating visibility of wallpaper " + wallpaper
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001729 + ": " + visible);
1730 wallpaper.mClient.dispatchAppVisibility(visible);
1731 } catch (RemoteException e) {
1732 }
1733 }
1734 }
1735 }
1736 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001737
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001738 void sendPointerToWallpaperLocked(WindowState srcWin,
1739 MotionEvent pointer, long eventTime) {
1740 int curTokenIndex = mWallpaperTokens.size();
1741 while (curTokenIndex > 0) {
1742 curTokenIndex--;
1743 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1744 int curWallpaperIndex = token.windows.size();
1745 while (curWallpaperIndex > 0) {
1746 curWallpaperIndex--;
1747 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1748 if ((wallpaper.mAttrs.flags &
1749 WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
1750 continue;
1751 }
1752 try {
1753 MotionEvent ev = MotionEvent.obtainNoHistory(pointer);
Dianne Hackborn6adba242009-11-10 11:10:09 -08001754 if (srcWin != null) {
1755 ev.offsetLocation(srcWin.mFrame.left-wallpaper.mFrame.left,
1756 srcWin.mFrame.top-wallpaper.mFrame.top);
1757 } else {
1758 ev.offsetLocation(-wallpaper.mFrame.left, -wallpaper.mFrame.top);
1759 }
1760 switch (pointer.getAction()) {
1761 case MotionEvent.ACTION_DOWN:
1762 mSendingPointersToWallpaper = true;
1763 break;
1764 case MotionEvent.ACTION_UP:
1765 mSendingPointersToWallpaper = false;
1766 break;
1767 }
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001768 wallpaper.mClient.dispatchPointer(ev, eventTime, false);
1769 } catch (RemoteException e) {
1770 Log.w(TAG, "Failure sending pointer to wallpaper", e);
1771 }
1772 }
1773 }
1774 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001775
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001776 public int addWindow(Session session, IWindow client,
1777 WindowManager.LayoutParams attrs, int viewVisibility,
1778 Rect outContentInsets) {
1779 int res = mPolicy.checkAddPermission(attrs);
1780 if (res != WindowManagerImpl.ADD_OKAY) {
1781 return res;
1782 }
Romain Guy06882f82009-06-10 13:36:04 -07001783
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001784 boolean reportNewConfig = false;
1785 WindowState attachedWindow = null;
1786 WindowState win = null;
Romain Guy06882f82009-06-10 13:36:04 -07001787
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001788 synchronized(mWindowMap) {
1789 // Instantiating a Display requires talking with the simulator,
1790 // so don't do it until we know the system is mostly up and
1791 // running.
1792 if (mDisplay == null) {
1793 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
1794 mDisplay = wm.getDefaultDisplay();
1795 mQueue.setDisplay(mDisplay);
1796 reportNewConfig = true;
1797 }
Romain Guy06882f82009-06-10 13:36:04 -07001798
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001799 if (mWindowMap.containsKey(client.asBinder())) {
1800 Log.w(TAG, "Window " + client + " is already added");
1801 return WindowManagerImpl.ADD_DUPLICATE_ADD;
1802 }
1803
1804 if (attrs.type >= FIRST_SUB_WINDOW && attrs.type <= LAST_SUB_WINDOW) {
Romain Guy06882f82009-06-10 13:36:04 -07001805 attachedWindow = windowForClientLocked(null, attrs.token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001806 if (attachedWindow == null) {
1807 Log.w(TAG, "Attempted to add window with token that is not a window: "
1808 + attrs.token + ". Aborting.");
1809 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1810 }
1811 if (attachedWindow.mAttrs.type >= FIRST_SUB_WINDOW
1812 && attachedWindow.mAttrs.type <= LAST_SUB_WINDOW) {
1813 Log.w(TAG, "Attempted to add window with token that is a sub-window: "
1814 + attrs.token + ". Aborting.");
1815 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1816 }
1817 }
1818
1819 boolean addToken = false;
1820 WindowToken token = mTokenMap.get(attrs.token);
1821 if (token == null) {
1822 if (attrs.type >= FIRST_APPLICATION_WINDOW
1823 && attrs.type <= LAST_APPLICATION_WINDOW) {
1824 Log.w(TAG, "Attempted to add application window with unknown token "
1825 + attrs.token + ". Aborting.");
1826 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1827 }
1828 if (attrs.type == TYPE_INPUT_METHOD) {
1829 Log.w(TAG, "Attempted to add input method window with unknown token "
1830 + attrs.token + ". Aborting.");
1831 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1832 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001833 if (attrs.type == TYPE_WALLPAPER) {
1834 Log.w(TAG, "Attempted to add wallpaper window with unknown token "
1835 + attrs.token + ". Aborting.");
1836 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1837 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001838 token = new WindowToken(attrs.token, -1, false);
1839 addToken = true;
1840 } else if (attrs.type >= FIRST_APPLICATION_WINDOW
1841 && attrs.type <= LAST_APPLICATION_WINDOW) {
1842 AppWindowToken atoken = token.appWindowToken;
1843 if (atoken == null) {
1844 Log.w(TAG, "Attempted to add window with non-application token "
1845 + token + ". Aborting.");
1846 return WindowManagerImpl.ADD_NOT_APP_TOKEN;
1847 } else if (atoken.removed) {
1848 Log.w(TAG, "Attempted to add window with exiting application token "
1849 + token + ". Aborting.");
1850 return WindowManagerImpl.ADD_APP_EXITING;
1851 }
1852 if (attrs.type == TYPE_APPLICATION_STARTING && atoken.firstWindowDrawn) {
1853 // No need for this guy!
1854 if (localLOGV) Log.v(
1855 TAG, "**** NO NEED TO START: " + attrs.getTitle());
1856 return WindowManagerImpl.ADD_STARTING_NOT_NEEDED;
1857 }
1858 } else if (attrs.type == TYPE_INPUT_METHOD) {
1859 if (token.windowType != TYPE_INPUT_METHOD) {
1860 Log.w(TAG, "Attempted to add input method window with bad token "
1861 + attrs.token + ". Aborting.");
1862 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1863 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001864 } else if (attrs.type == TYPE_WALLPAPER) {
1865 if (token.windowType != TYPE_WALLPAPER) {
1866 Log.w(TAG, "Attempted to add wallpaper window with bad token "
1867 + attrs.token + ". Aborting.");
1868 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1869 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001870 }
1871
1872 win = new WindowState(session, client, token,
1873 attachedWindow, attrs, viewVisibility);
1874 if (win.mDeathRecipient == null) {
1875 // Client has apparently died, so there is no reason to
1876 // continue.
1877 Log.w(TAG, "Adding window client " + client.asBinder()
1878 + " that is dead, aborting.");
1879 return WindowManagerImpl.ADD_APP_EXITING;
1880 }
1881
1882 mPolicy.adjustWindowParamsLw(win.mAttrs);
Romain Guy06882f82009-06-10 13:36:04 -07001883
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001884 res = mPolicy.prepareAddWindowLw(win, attrs);
1885 if (res != WindowManagerImpl.ADD_OKAY) {
1886 return res;
1887 }
1888
1889 // From now on, no exceptions or errors allowed!
1890
1891 res = WindowManagerImpl.ADD_OKAY;
Romain Guy06882f82009-06-10 13:36:04 -07001892
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001893 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001894
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001895 if (addToken) {
1896 mTokenMap.put(attrs.token, token);
1897 mTokenList.add(token);
1898 }
1899 win.attach();
1900 mWindowMap.put(client.asBinder(), win);
1901
1902 if (attrs.type == TYPE_APPLICATION_STARTING &&
1903 token.appWindowToken != null) {
1904 token.appWindowToken.startingWindow = win;
1905 }
1906
1907 boolean imMayMove = true;
Romain Guy06882f82009-06-10 13:36:04 -07001908
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001909 if (attrs.type == TYPE_INPUT_METHOD) {
1910 mInputMethodWindow = win;
1911 addInputMethodWindowToListLocked(win);
1912 imMayMove = false;
1913 } else if (attrs.type == TYPE_INPUT_METHOD_DIALOG) {
1914 mInputMethodDialogs.add(win);
1915 addWindowToListInOrderLocked(win, true);
1916 adjustInputMethodDialogsLocked();
1917 imMayMove = false;
1918 } else {
1919 addWindowToListInOrderLocked(win, true);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001920 if (attrs.type == TYPE_WALLPAPER) {
1921 mLastWallpaperTimeoutTime = 0;
1922 adjustWallpaperWindowsLocked();
1923 } else if ((attrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001924 adjustWallpaperWindowsLocked();
1925 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001926 }
Romain Guy06882f82009-06-10 13:36:04 -07001927
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001928 win.mEnterAnimationPending = true;
Romain Guy06882f82009-06-10 13:36:04 -07001929
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930 mPolicy.getContentInsetHintLw(attrs, outContentInsets);
Romain Guy06882f82009-06-10 13:36:04 -07001931
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001932 if (mInTouchMode) {
1933 res |= WindowManagerImpl.ADD_FLAG_IN_TOUCH_MODE;
1934 }
1935 if (win == null || win.mAppToken == null || !win.mAppToken.clientHidden) {
1936 res |= WindowManagerImpl.ADD_FLAG_APP_VISIBLE;
1937 }
Romain Guy06882f82009-06-10 13:36:04 -07001938
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001939 boolean focusChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001940 if (win.canReceiveKeys()) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001941 if ((focusChanged=updateFocusedWindowLocked(UPDATE_FOCUS_WILL_ASSIGN_LAYERS))
1942 == true) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001943 imMayMove = false;
1944 }
1945 }
Romain Guy06882f82009-06-10 13:36:04 -07001946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001947 if (imMayMove) {
Romain Guy06882f82009-06-10 13:36:04 -07001948 moveInputMethodWindowsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001949 }
Romain Guy06882f82009-06-10 13:36:04 -07001950
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001951 assignLayersLocked();
1952 // Don't do layout here, the window must call
1953 // relayout to be displayed, so we'll do it there.
Romain Guy06882f82009-06-10 13:36:04 -07001954
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001955 //dump();
1956
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001957 if (focusChanged) {
1958 if (mCurrentFocus != null) {
1959 mKeyWaiter.handleNewWindowLocked(mCurrentFocus);
1960 }
1961 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001962 if (localLOGV) Log.v(
1963 TAG, "New client " + client.asBinder()
1964 + ": window=" + win);
1965 }
1966
1967 // sendNewConfiguration() checks caller permissions so we must call it with
1968 // privilege. updateOrientationFromAppTokens() clears and resets the caller
1969 // identity anyway, so it's safe to just clear & restore around this whole
1970 // block.
1971 final long origId = Binder.clearCallingIdentity();
1972 if (reportNewConfig) {
1973 sendNewConfiguration();
1974 } else {
1975 // Update Orientation after adding a window, only if the window needs to be
1976 // displayed right away
1977 if (win.isVisibleOrAdding()) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07001978 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001979 sendNewConfiguration();
1980 }
1981 }
1982 }
1983 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07001984
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001985 return res;
1986 }
Romain Guy06882f82009-06-10 13:36:04 -07001987
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001988 public void removeWindow(Session session, IWindow client) {
1989 synchronized(mWindowMap) {
1990 WindowState win = windowForClientLocked(session, client);
1991 if (win == null) {
1992 return;
1993 }
1994 removeWindowLocked(session, win);
1995 }
1996 }
Romain Guy06882f82009-06-10 13:36:04 -07001997
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001998 public void removeWindowLocked(Session session, WindowState win) {
1999
2000 if (localLOGV || DEBUG_FOCUS) Log.v(
2001 TAG, "Remove " + win + " client="
2002 + Integer.toHexString(System.identityHashCode(
2003 win.mClient.asBinder()))
2004 + ", surface=" + win.mSurface);
2005
2006 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002007
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002008 if (DEBUG_APP_TRANSITIONS) Log.v(
2009 TAG, "Remove " + win + ": mSurface=" + win.mSurface
2010 + " mExiting=" + win.mExiting
2011 + " isAnimating=" + win.isAnimating()
2012 + " app-animation="
2013 + (win.mAppToken != null ? win.mAppToken.animation : null)
2014 + " inPendingTransaction="
2015 + (win.mAppToken != null ? win.mAppToken.inPendingTransaction : false)
2016 + " mDisplayFrozen=" + mDisplayFrozen);
2017 // Visibility of the removed window. Will be used later to update orientation later on.
2018 boolean wasVisible = false;
2019 // First, see if we need to run an animation. If we do, we have
2020 // to hold off on removing the window until the animation is done.
2021 // If the display is frozen, just remove immediately, since the
2022 // animation wouldn't be seen.
Dianne Hackbornde2606d2009-12-18 16:53:55 -08002023 if (win.mSurface != null && !mDisplayFrozen && mPolicy.isScreenOn()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002024 // If we are not currently running the exit animation, we
2025 // need to see about starting one.
2026 if (wasVisible=win.isWinVisibleLw()) {
Romain Guy06882f82009-06-10 13:36:04 -07002027
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002028 int transit = WindowManagerPolicy.TRANSIT_EXIT;
2029 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
2030 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
2031 }
2032 // Try starting an animation.
2033 if (applyAnimationLocked(win, transit, false)) {
2034 win.mExiting = true;
2035 }
2036 }
2037 if (win.mExiting || win.isAnimating()) {
2038 // The exit animation is running... wait for it!
2039 //Log.i(TAG, "*** Running exit animation...");
2040 win.mExiting = true;
2041 win.mRemoveOnExit = true;
2042 mLayoutNeeded = true;
2043 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
2044 performLayoutAndPlaceSurfacesLocked();
2045 if (win.mAppToken != null) {
2046 win.mAppToken.updateReportedVisibilityLocked();
2047 }
2048 //dump();
2049 Binder.restoreCallingIdentity(origId);
2050 return;
2051 }
2052 }
2053
2054 removeWindowInnerLocked(session, win);
2055 // Removing a visible window will effect the computed orientation
2056 // So just update orientation if needed.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002057 if (wasVisible && computeForcedAppOrientationLocked()
2058 != mForcedAppOrientation) {
2059 mH.sendMessage(mH.obtainMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002060 }
2061 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2062 Binder.restoreCallingIdentity(origId);
2063 }
Romain Guy06882f82009-06-10 13:36:04 -07002064
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002065 private void removeWindowInnerLocked(Session session, WindowState win) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07002066 mKeyWaiter.finishedKey(session, win.mClient, true,
2067 KeyWaiter.RETURN_NOTHING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002068 mKeyWaiter.releasePendingPointerLocked(win.mSession);
2069 mKeyWaiter.releasePendingTrackballLocked(win.mSession);
Romain Guy06882f82009-06-10 13:36:04 -07002070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002071 win.mRemoved = true;
Romain Guy06882f82009-06-10 13:36:04 -07002072
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002073 if (mInputMethodTarget == win) {
2074 moveInputMethodWindowsIfNeededLocked(false);
2075 }
Romain Guy06882f82009-06-10 13:36:04 -07002076
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07002077 if (false) {
2078 RuntimeException e = new RuntimeException("here");
2079 e.fillInStackTrace();
2080 Log.w(TAG, "Removing window " + win, e);
2081 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002083 mPolicy.removeWindowLw(win);
2084 win.removeLocked();
2085
2086 mWindowMap.remove(win.mClient.asBinder());
2087 mWindows.remove(win);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07002088 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Final remove of window: " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002089
2090 if (mInputMethodWindow == win) {
2091 mInputMethodWindow = null;
2092 } else if (win.mAttrs.type == TYPE_INPUT_METHOD_DIALOG) {
2093 mInputMethodDialogs.remove(win);
2094 }
Romain Guy06882f82009-06-10 13:36:04 -07002095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002096 final WindowToken token = win.mToken;
2097 final AppWindowToken atoken = win.mAppToken;
2098 token.windows.remove(win);
2099 if (atoken != null) {
2100 atoken.allAppWindows.remove(win);
2101 }
2102 if (localLOGV) Log.v(
2103 TAG, "**** Removing window " + win + ": count="
2104 + token.windows.size());
2105 if (token.windows.size() == 0) {
2106 if (!token.explicit) {
2107 mTokenMap.remove(token.token);
2108 mTokenList.remove(token);
2109 } else if (atoken != null) {
2110 atoken.firstWindowDrawn = false;
2111 }
2112 }
2113
2114 if (atoken != null) {
2115 if (atoken.startingWindow == win) {
2116 atoken.startingWindow = null;
2117 } else if (atoken.allAppWindows.size() == 0 && atoken.startingData != null) {
2118 // If this is the last window and we had requested a starting
2119 // transition window, well there is no point now.
2120 atoken.startingData = null;
2121 } else if (atoken.allAppWindows.size() == 1 && atoken.startingView != null) {
2122 // If this is the last window except for a starting transition
2123 // window, we need to get rid of the starting transition.
2124 if (DEBUG_STARTING_WINDOW) {
2125 Log.v(TAG, "Schedule remove starting " + token
2126 + ": no more real windows");
2127 }
2128 Message m = mH.obtainMessage(H.REMOVE_STARTING, atoken);
2129 mH.sendMessage(m);
2130 }
2131 }
Romain Guy06882f82009-06-10 13:36:04 -07002132
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002133 if (win.mAttrs.type == TYPE_WALLPAPER) {
2134 mLastWallpaperTimeoutTime = 0;
2135 adjustWallpaperWindowsLocked();
2136 } else if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07002137 adjustWallpaperWindowsLocked();
2138 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002140 if (!mInLayout) {
2141 assignLayersLocked();
2142 mLayoutNeeded = true;
2143 performLayoutAndPlaceSurfacesLocked();
2144 if (win.mAppToken != null) {
2145 win.mAppToken.updateReportedVisibilityLocked();
2146 }
2147 }
2148 }
2149
2150 private void setTransparentRegionWindow(Session session, IWindow client, Region region) {
2151 long origId = Binder.clearCallingIdentity();
2152 try {
2153 synchronized (mWindowMap) {
2154 WindowState w = windowForClientLocked(session, client);
2155 if ((w != null) && (w.mSurface != null)) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002156 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002157 Surface.openTransaction();
2158 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002159 if (SHOW_TRANSACTIONS) Log.i(
2160 TAG, " SURFACE " + w.mSurface
2161 + ": transparentRegionHint=" + region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002162 w.mSurface.setTransparentRegionHint(region);
2163 } finally {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002164 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002165 Surface.closeTransaction();
2166 }
2167 }
2168 }
2169 } finally {
2170 Binder.restoreCallingIdentity(origId);
2171 }
2172 }
2173
2174 void setInsetsWindow(Session session, IWindow client,
Romain Guy06882f82009-06-10 13:36:04 -07002175 int touchableInsets, Rect contentInsets,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002176 Rect visibleInsets) {
2177 long origId = Binder.clearCallingIdentity();
2178 try {
2179 synchronized (mWindowMap) {
2180 WindowState w = windowForClientLocked(session, client);
2181 if (w != null) {
2182 w.mGivenInsetsPending = false;
2183 w.mGivenContentInsets.set(contentInsets);
2184 w.mGivenVisibleInsets.set(visibleInsets);
2185 w.mTouchableInsets = touchableInsets;
2186 mLayoutNeeded = true;
2187 performLayoutAndPlaceSurfacesLocked();
2188 }
2189 }
2190 } finally {
2191 Binder.restoreCallingIdentity(origId);
2192 }
2193 }
Romain Guy06882f82009-06-10 13:36:04 -07002194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002195 public void getWindowDisplayFrame(Session session, IWindow client,
2196 Rect outDisplayFrame) {
2197 synchronized(mWindowMap) {
2198 WindowState win = windowForClientLocked(session, client);
2199 if (win == null) {
2200 outDisplayFrame.setEmpty();
2201 return;
2202 }
2203 outDisplayFrame.set(win.mDisplayFrame);
2204 }
2205 }
2206
Marco Nelissenbf6956b2009-11-09 15:21:13 -08002207 public void setWindowWallpaperPositionLocked(WindowState window, float x, float y,
2208 float xStep, float yStep) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002209 if (window.mWallpaperX != x || window.mWallpaperY != y) {
2210 window.mWallpaperX = x;
2211 window.mWallpaperY = y;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08002212 window.mWallpaperXStep = xStep;
2213 window.mWallpaperYStep = yStep;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07002214 if (updateWallpaperOffsetLocked(window, true)) {
2215 performLayoutAndPlaceSurfacesLocked();
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002216 }
2217 }
2218 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002219
Dianne Hackborn75804932009-10-20 20:15:20 -07002220 void wallpaperCommandComplete(IBinder window, Bundle result) {
2221 synchronized (mWindowMap) {
2222 if (mWaitingOnWallpaper != null &&
2223 mWaitingOnWallpaper.mClient.asBinder() == window) {
2224 mWaitingOnWallpaper = null;
2225 mWindowMap.notifyAll();
2226 }
2227 }
2228 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002229
Dianne Hackborn75804932009-10-20 20:15:20 -07002230 public Bundle sendWindowWallpaperCommandLocked(WindowState window,
2231 String action, int x, int y, int z, Bundle extras, boolean sync) {
2232 if (window == mWallpaperTarget || window == mLowerWallpaperTarget
2233 || window == mUpperWallpaperTarget) {
2234 boolean doWait = sync;
2235 int curTokenIndex = mWallpaperTokens.size();
2236 while (curTokenIndex > 0) {
2237 curTokenIndex--;
2238 WindowToken token = mWallpaperTokens.get(curTokenIndex);
2239 int curWallpaperIndex = token.windows.size();
2240 while (curWallpaperIndex > 0) {
2241 curWallpaperIndex--;
2242 WindowState wallpaper = token.windows.get(curWallpaperIndex);
2243 try {
2244 wallpaper.mClient.dispatchWallpaperCommand(action,
2245 x, y, z, extras, sync);
2246 // We only want to be synchronous with one wallpaper.
2247 sync = false;
2248 } catch (RemoteException e) {
2249 }
2250 }
2251 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002252
Dianne Hackborn75804932009-10-20 20:15:20 -07002253 if (doWait) {
2254 // XXX Need to wait for result.
2255 }
2256 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002257
Dianne Hackborn75804932009-10-20 20:15:20 -07002258 return null;
2259 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002261 public int relayoutWindow(Session session, IWindow client,
2262 WindowManager.LayoutParams attrs, int requestedWidth,
2263 int requestedHeight, int viewVisibility, boolean insetsPending,
2264 Rect outFrame, Rect outContentInsets, Rect outVisibleInsets,
2265 Surface outSurface) {
2266 boolean displayed = false;
2267 boolean inTouchMode;
2268 Configuration newConfig = null;
2269 long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002271 synchronized(mWindowMap) {
2272 WindowState win = windowForClientLocked(session, client);
2273 if (win == null) {
2274 return 0;
2275 }
2276 win.mRequestedWidth = requestedWidth;
2277 win.mRequestedHeight = requestedHeight;
2278
2279 if (attrs != null) {
2280 mPolicy.adjustWindowParamsLw(attrs);
2281 }
Romain Guy06882f82009-06-10 13:36:04 -07002282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002283 int attrChanges = 0;
2284 int flagChanges = 0;
2285 if (attrs != null) {
2286 flagChanges = win.mAttrs.flags ^= attrs.flags;
2287 attrChanges = win.mAttrs.copyFrom(attrs);
2288 }
2289
Dianne Hackborn9b52a212009-12-11 14:51:35 -08002290 if (DEBUG_LAYOUT) Log.v(TAG, "Relayout " + win + ": " + win.mAttrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002291
2292 if ((attrChanges & WindowManager.LayoutParams.ALPHA_CHANGED) != 0) {
2293 win.mAlpha = attrs.alpha;
2294 }
2295
2296 final boolean scaledWindow =
2297 ((win.mAttrs.flags & WindowManager.LayoutParams.FLAG_SCALED) != 0);
2298
2299 if (scaledWindow) {
2300 // requested{Width|Height} Surface's physical size
2301 // attrs.{width|height} Size on screen
2302 win.mHScale = (attrs.width != requestedWidth) ?
2303 (attrs.width / (float)requestedWidth) : 1.0f;
2304 win.mVScale = (attrs.height != requestedHeight) ?
2305 (attrs.height / (float)requestedHeight) : 1.0f;
Dianne Hackborn9b52a212009-12-11 14:51:35 -08002306 } else {
2307 win.mHScale = win.mVScale = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002308 }
2309
2310 boolean imMayMove = (flagChanges&(
2311 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
2312 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07002313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002314 boolean focusMayChange = win.mViewVisibility != viewVisibility
2315 || ((flagChanges&WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) != 0)
2316 || (!win.mRelayoutCalled);
Romain Guy06882f82009-06-10 13:36:04 -07002317
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002318 boolean wallpaperMayMove = win.mViewVisibility != viewVisibility
2319 && (win.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002321 win.mRelayoutCalled = true;
2322 final int oldVisibility = win.mViewVisibility;
2323 win.mViewVisibility = viewVisibility;
2324 if (viewVisibility == View.VISIBLE &&
2325 (win.mAppToken == null || !win.mAppToken.clientHidden)) {
2326 displayed = !win.isVisibleLw();
2327 if (win.mExiting) {
2328 win.mExiting = false;
2329 win.mAnimation = null;
2330 }
2331 if (win.mDestroying) {
2332 win.mDestroying = false;
2333 mDestroySurface.remove(win);
2334 }
2335 if (oldVisibility == View.GONE) {
2336 win.mEnterAnimationPending = true;
2337 }
2338 if (displayed && win.mSurface != null && !win.mDrawPending
Dianne Hackbornde2606d2009-12-18 16:53:55 -08002339 && !win.mCommitDrawPending && !mDisplayFrozen
2340 && mPolicy.isScreenOn()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002341 applyEnterAnimationLocked(win);
2342 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07002343 if (displayed && (win.mAttrs.flags
2344 & WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON) != 0) {
2345 win.mTurnOnScreen = true;
2346 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002347 if ((attrChanges&WindowManager.LayoutParams.FORMAT_CHANGED) != 0) {
2348 // To change the format, we need to re-build the surface.
2349 win.destroySurfaceLocked();
2350 displayed = true;
2351 }
2352 try {
2353 Surface surface = win.createSurfaceLocked();
2354 if (surface != null) {
2355 outSurface.copyFrom(surface);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002356 win.mReportDestroySurface = false;
2357 win.mSurfacePendingDestroy = false;
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002358 if (SHOW_TRANSACTIONS) Log.i(TAG,
2359 " OUT SURFACE " + outSurface + ": copied");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002360 } else {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002361 // For some reason there isn't a surface. Clear the
2362 // caller's object so they see the same state.
2363 outSurface.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002364 }
2365 } catch (Exception e) {
2366 Log.w(TAG, "Exception thrown when creating surface for client "
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002367 + client + " (" + win.mAttrs.getTitle() + ")",
2368 e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002369 Binder.restoreCallingIdentity(origId);
2370 return 0;
2371 }
2372 if (displayed) {
2373 focusMayChange = true;
2374 }
2375 if (win.mAttrs.type == TYPE_INPUT_METHOD
2376 && mInputMethodWindow == null) {
2377 mInputMethodWindow = win;
2378 imMayMove = true;
2379 }
Dianne Hackborn558947c2009-12-18 16:02:50 -08002380 if (win.mAttrs.type == TYPE_BASE_APPLICATION
2381 && win.mAppToken != null
2382 && win.mAppToken.startingWindow != null) {
2383 // Special handling of starting window over the base
2384 // window of the app: propagate lock screen flags to it,
2385 // to provide the correct semantics while starting.
2386 final int mask =
2387 WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
Mike Lockwoodef731622010-01-27 17:51:34 -05002388 | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
2389 | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON;
Dianne Hackborn558947c2009-12-18 16:02:50 -08002390 WindowManager.LayoutParams sa = win.mAppToken.startingWindow.mAttrs;
2391 sa.flags = (sa.flags&~mask) | (win.mAttrs.flags&mask);
2392 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002393 } else {
2394 win.mEnterAnimationPending = false;
2395 if (win.mSurface != null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002396 if (DEBUG_VISIBILITY) Log.i(TAG, "Relayout invis " + win
2397 + ": mExiting=" + win.mExiting
2398 + " mSurfacePendingDestroy=" + win.mSurfacePendingDestroy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002399 // If we are not currently running the exit animation, we
2400 // need to see about starting one.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002401 if (!win.mExiting || win.mSurfacePendingDestroy) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002402 // Try starting an animation; if there isn't one, we
2403 // can destroy the surface right away.
2404 int transit = WindowManagerPolicy.TRANSIT_EXIT;
2405 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
2406 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
2407 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002408 if (!win.mSurfacePendingDestroy && win.isWinVisibleLw() &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002409 applyAnimationLocked(win, transit, false)) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002410 focusMayChange = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002411 win.mExiting = true;
2412 mKeyWaiter.finishedKey(session, client, true,
2413 KeyWaiter.RETURN_NOTHING);
2414 } else if (win.isAnimating()) {
2415 // Currently in a hide animation... turn this into
2416 // an exit.
2417 win.mExiting = true;
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07002418 } else if (win == mWallpaperTarget) {
2419 // If the wallpaper is currently behind this
2420 // window, we need to change both of them inside
2421 // of a transaction to avoid artifacts.
2422 win.mExiting = true;
2423 win.mAnimating = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002424 } else {
2425 if (mInputMethodWindow == win) {
2426 mInputMethodWindow = null;
2427 }
2428 win.destroySurfaceLocked();
2429 }
2430 }
2431 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002432
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002433 if (win.mSurface == null || (win.getAttrs().flags
2434 & WindowManager.LayoutParams.FLAG_KEEP_SURFACE_WHILE_ANIMATING) == 0
2435 || win.mSurfacePendingDestroy) {
2436 // We are being called from a local process, which
2437 // means outSurface holds its current surface. Ensure the
2438 // surface object is cleared, but we don't want it actually
2439 // destroyed at this point.
2440 win.mSurfacePendingDestroy = false;
2441 outSurface.release();
2442 if (DEBUG_VISIBILITY) Log.i(TAG, "Releasing surface in: " + win);
2443 } else if (win.mSurface != null) {
2444 if (DEBUG_VISIBILITY) Log.i(TAG,
2445 "Keeping surface, will report destroy: " + win);
2446 win.mReportDestroySurface = true;
2447 outSurface.copyFrom(win.mSurface);
2448 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002449 }
2450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002451 if (focusMayChange) {
2452 //System.out.println("Focus may change: " + win.mAttrs.getTitle());
2453 if (updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002454 imMayMove = false;
2455 }
2456 //System.out.println("Relayout " + win + ": focus=" + mCurrentFocus);
2457 }
Romain Guy06882f82009-06-10 13:36:04 -07002458
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08002459 // updateFocusedWindowLocked() already assigned layers so we only need to
2460 // reassign them at this point if the IM window state gets shuffled
2461 boolean assignLayers = false;
Romain Guy06882f82009-06-10 13:36:04 -07002462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002463 if (imMayMove) {
Dianne Hackborn8abd5f02009-11-20 18:09:03 -08002464 if (moveInputMethodWindowsIfNeededLocked(false) || displayed) {
2465 // Little hack here -- we -should- be able to rely on the
2466 // function to return true if the IME has moved and needs
2467 // its layer recomputed. However, if the IME was hidden
2468 // and isn't actually moved in the list, its layer may be
2469 // out of data so we make sure to recompute it.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002470 assignLayers = true;
2471 }
2472 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002473 if (wallpaperMayMove) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002474 if ((adjustWallpaperWindowsLocked()&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002475 assignLayers = true;
2476 }
2477 }
Romain Guy06882f82009-06-10 13:36:04 -07002478
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002479 mLayoutNeeded = true;
2480 win.mGivenInsetsPending = insetsPending;
2481 if (assignLayers) {
2482 assignLayersLocked();
2483 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002484 newConfig = updateOrientationFromAppTokensLocked(null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002485 performLayoutAndPlaceSurfacesLocked();
Dianne Hackborn284ac932009-08-28 10:34:25 -07002486 if (displayed && win.mIsWallpaper) {
2487 updateWallpaperOffsetLocked(win, mDisplay.getWidth(),
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002488 mDisplay.getHeight(), false);
Dianne Hackborn284ac932009-08-28 10:34:25 -07002489 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002490 if (win.mAppToken != null) {
2491 win.mAppToken.updateReportedVisibilityLocked();
2492 }
2493 outFrame.set(win.mFrame);
2494 outContentInsets.set(win.mContentInsets);
2495 outVisibleInsets.set(win.mVisibleInsets);
2496 if (localLOGV) Log.v(
2497 TAG, "Relayout given client " + client.asBinder()
Romain Guy06882f82009-06-10 13:36:04 -07002498 + ", requestedWidth=" + requestedWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002499 + ", requestedHeight=" + requestedHeight
2500 + ", viewVisibility=" + viewVisibility
2501 + "\nRelayout returning frame=" + outFrame
2502 + ", surface=" + outSurface);
2503
2504 if (localLOGV || DEBUG_FOCUS) Log.v(
2505 TAG, "Relayout of " + win + ": focusMayChange=" + focusMayChange);
2506
2507 inTouchMode = mInTouchMode;
2508 }
2509
2510 if (newConfig != null) {
2511 sendNewConfiguration();
2512 }
Romain Guy06882f82009-06-10 13:36:04 -07002513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002514 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07002515
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002516 return (inTouchMode ? WindowManagerImpl.RELAYOUT_IN_TOUCH_MODE : 0)
2517 | (displayed ? WindowManagerImpl.RELAYOUT_FIRST_TIME : 0);
2518 }
2519
2520 public void finishDrawingWindow(Session session, IWindow client) {
2521 final long origId = Binder.clearCallingIdentity();
2522 synchronized(mWindowMap) {
2523 WindowState win = windowForClientLocked(session, client);
2524 if (win != null && win.finishDrawingLocked()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07002525 if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
2526 adjustWallpaperWindowsLocked();
2527 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002528 mLayoutNeeded = true;
2529 performLayoutAndPlaceSurfacesLocked();
2530 }
2531 }
2532 Binder.restoreCallingIdentity(origId);
2533 }
2534
2535 private AttributeCache.Entry getCachedAnimations(WindowManager.LayoutParams lp) {
2536 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: params package="
2537 + (lp != null ? lp.packageName : null)
2538 + " resId=0x" + (lp != null ? Integer.toHexString(lp.windowAnimations) : null));
2539 if (lp != null && lp.windowAnimations != 0) {
2540 // If this is a system resource, don't try to load it from the
2541 // application resources. It is nice to avoid loading application
2542 // resources if we can.
2543 String packageName = lp.packageName != null ? lp.packageName : "android";
2544 int resId = lp.windowAnimations;
2545 if ((resId&0xFF000000) == 0x01000000) {
2546 packageName = "android";
2547 }
2548 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: picked package="
2549 + packageName);
2550 return AttributeCache.instance().get(packageName, resId,
2551 com.android.internal.R.styleable.WindowAnimation);
2552 }
2553 return null;
2554 }
Romain Guy06882f82009-06-10 13:36:04 -07002555
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002556 private AttributeCache.Entry getCachedAnimations(String packageName, int resId) {
2557 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: params package="
2558 + packageName + " resId=0x" + Integer.toHexString(resId));
2559 if (packageName != null) {
2560 if ((resId&0xFF000000) == 0x01000000) {
2561 packageName = "android";
2562 }
2563 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: picked package="
2564 + packageName);
2565 return AttributeCache.instance().get(packageName, resId,
2566 com.android.internal.R.styleable.WindowAnimation);
2567 }
2568 return null;
2569 }
2570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002571 private void applyEnterAnimationLocked(WindowState win) {
2572 int transit = WindowManagerPolicy.TRANSIT_SHOW;
2573 if (win.mEnterAnimationPending) {
2574 win.mEnterAnimationPending = false;
2575 transit = WindowManagerPolicy.TRANSIT_ENTER;
2576 }
2577
2578 applyAnimationLocked(win, transit, true);
2579 }
2580
2581 private boolean applyAnimationLocked(WindowState win,
2582 int transit, boolean isEntrance) {
2583 if (win.mLocalAnimating && win.mAnimationIsEntrance == isEntrance) {
2584 // If we are trying to apply an animation, but already running
2585 // an animation of the same type, then just leave that one alone.
2586 return true;
2587 }
Romain Guy06882f82009-06-10 13:36:04 -07002588
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002589 // Only apply an animation if the display isn't frozen. If it is
2590 // frozen, there is no reason to animate and it can cause strange
2591 // artifacts when we unfreeze the display if some different animation
2592 // is running.
Dianne Hackbornde2606d2009-12-18 16:53:55 -08002593 if (!mDisplayFrozen && mPolicy.isScreenOn()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002594 int anim = mPolicy.selectAnimationLw(win, transit);
2595 int attr = -1;
2596 Animation a = null;
2597 if (anim != 0) {
2598 a = AnimationUtils.loadAnimation(mContext, anim);
2599 } else {
2600 switch (transit) {
2601 case WindowManagerPolicy.TRANSIT_ENTER:
2602 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
2603 break;
2604 case WindowManagerPolicy.TRANSIT_EXIT:
2605 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
2606 break;
2607 case WindowManagerPolicy.TRANSIT_SHOW:
2608 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
2609 break;
2610 case WindowManagerPolicy.TRANSIT_HIDE:
2611 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
2612 break;
2613 }
2614 if (attr >= 0) {
2615 a = loadAnimation(win.mAttrs, attr);
2616 }
2617 }
2618 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: win=" + win
2619 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
2620 + " mAnimation=" + win.mAnimation
2621 + " isEntrance=" + isEntrance);
2622 if (a != null) {
2623 if (DEBUG_ANIM) {
2624 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07002625 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002626 Log.v(TAG, "Loaded animation " + a + " for " + win, e);
2627 }
2628 win.setAnimation(a);
2629 win.mAnimationIsEntrance = isEntrance;
2630 }
2631 } else {
2632 win.clearAnimation();
2633 }
2634
2635 return win.mAnimation != null;
2636 }
2637
2638 private Animation loadAnimation(WindowManager.LayoutParams lp, int animAttr) {
2639 int anim = 0;
2640 Context context = mContext;
2641 if (animAttr >= 0) {
2642 AttributeCache.Entry ent = getCachedAnimations(lp);
2643 if (ent != null) {
2644 context = ent.context;
2645 anim = ent.array.getResourceId(animAttr, 0);
2646 }
2647 }
2648 if (anim != 0) {
2649 return AnimationUtils.loadAnimation(context, anim);
2650 }
2651 return null;
2652 }
Romain Guy06882f82009-06-10 13:36:04 -07002653
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002654 private Animation loadAnimation(String packageName, int resId) {
2655 int anim = 0;
2656 Context context = mContext;
2657 if (resId >= 0) {
2658 AttributeCache.Entry ent = getCachedAnimations(packageName, resId);
2659 if (ent != null) {
2660 context = ent.context;
2661 anim = resId;
2662 }
2663 }
2664 if (anim != 0) {
2665 return AnimationUtils.loadAnimation(context, anim);
2666 }
2667 return null;
2668 }
2669
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002670 private boolean applyAnimationLocked(AppWindowToken wtoken,
2671 WindowManager.LayoutParams lp, int transit, boolean enter) {
2672 // Only apply an animation if the display isn't frozen. If it is
2673 // frozen, there is no reason to animate and it can cause strange
2674 // artifacts when we unfreeze the display if some different animation
2675 // is running.
Dianne Hackbornde2606d2009-12-18 16:53:55 -08002676 if (!mDisplayFrozen && mPolicy.isScreenOn()) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002677 Animation a;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07002678 if (lp != null && (lp.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002679 a = new FadeInOutAnimation(enter);
2680 if (DEBUG_ANIM) Log.v(TAG,
2681 "applying FadeInOutAnimation for a window in compatibility mode");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002682 } else if (mNextAppTransitionPackage != null) {
2683 a = loadAnimation(mNextAppTransitionPackage, enter ?
2684 mNextAppTransitionEnter : mNextAppTransitionExit);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002685 } else {
2686 int animAttr = 0;
2687 switch (transit) {
2688 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
2689 animAttr = enter
2690 ? com.android.internal.R.styleable.WindowAnimation_activityOpenEnterAnimation
2691 : com.android.internal.R.styleable.WindowAnimation_activityOpenExitAnimation;
2692 break;
2693 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
2694 animAttr = enter
2695 ? com.android.internal.R.styleable.WindowAnimation_activityCloseEnterAnimation
2696 : com.android.internal.R.styleable.WindowAnimation_activityCloseExitAnimation;
2697 break;
2698 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
2699 animAttr = enter
2700 ? com.android.internal.R.styleable.WindowAnimation_taskOpenEnterAnimation
2701 : com.android.internal.R.styleable.WindowAnimation_taskOpenExitAnimation;
2702 break;
2703 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
2704 animAttr = enter
2705 ? com.android.internal.R.styleable.WindowAnimation_taskCloseEnterAnimation
2706 : com.android.internal.R.styleable.WindowAnimation_taskCloseExitAnimation;
2707 break;
2708 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
2709 animAttr = enter
2710 ? com.android.internal.R.styleable.WindowAnimation_taskToFrontEnterAnimation
2711 : com.android.internal.R.styleable.WindowAnimation_taskToFrontExitAnimation;
2712 break;
2713 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
2714 animAttr = enter
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07002715 ? com.android.internal.R.styleable.WindowAnimation_taskToBackEnterAnimation
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002716 : com.android.internal.R.styleable.WindowAnimation_taskToBackExitAnimation;
2717 break;
Dianne Hackborn25994b42009-09-04 14:21:19 -07002718 case WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN:
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002719 animAttr = enter
Dianne Hackborn25994b42009-09-04 14:21:19 -07002720 ? com.android.internal.R.styleable.WindowAnimation_wallpaperOpenEnterAnimation
2721 : com.android.internal.R.styleable.WindowAnimation_wallpaperOpenExitAnimation;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002722 break;
Dianne Hackborn25994b42009-09-04 14:21:19 -07002723 case WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE:
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002724 animAttr = enter
Dianne Hackborn25994b42009-09-04 14:21:19 -07002725 ? com.android.internal.R.styleable.WindowAnimation_wallpaperCloseEnterAnimation
2726 : com.android.internal.R.styleable.WindowAnimation_wallpaperCloseExitAnimation;
2727 break;
2728 case WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN:
2729 animAttr = enter
2730 ? com.android.internal.R.styleable.WindowAnimation_wallpaperIntraOpenEnterAnimation
2731 : com.android.internal.R.styleable.WindowAnimation_wallpaperIntraOpenExitAnimation;
2732 break;
2733 case WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE:
2734 animAttr = enter
2735 ? com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseEnterAnimation
2736 : com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseExitAnimation;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002737 break;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002738 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07002739 a = animAttr != 0 ? loadAnimation(lp, animAttr) : null;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002740 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: wtoken=" + wtoken
2741 + " anim=" + a
2742 + " animAttr=0x" + Integer.toHexString(animAttr)
2743 + " transit=" + transit);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002744 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002745 if (a != null) {
2746 if (DEBUG_ANIM) {
2747 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07002748 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002749 Log.v(TAG, "Loaded animation " + a + " for " + wtoken, e);
2750 }
2751 wtoken.setAnimation(a);
2752 }
2753 } else {
2754 wtoken.clearAnimation();
2755 }
2756
2757 return wtoken.animation != null;
2758 }
2759
2760 // -------------------------------------------------------------
2761 // Application Window Tokens
2762 // -------------------------------------------------------------
2763
2764 public void validateAppTokens(List tokens) {
2765 int v = tokens.size()-1;
2766 int m = mAppTokens.size()-1;
2767 while (v >= 0 && m >= 0) {
2768 AppWindowToken wtoken = mAppTokens.get(m);
2769 if (wtoken.removed) {
2770 m--;
2771 continue;
2772 }
2773 if (tokens.get(v) != wtoken.token) {
2774 Log.w(TAG, "Tokens out of sync: external is " + tokens.get(v)
2775 + " @ " + v + ", internal is " + wtoken.token + " @ " + m);
2776 }
2777 v--;
2778 m--;
2779 }
2780 while (v >= 0) {
2781 Log.w(TAG, "External token not found: " + tokens.get(v) + " @ " + v);
2782 v--;
2783 }
2784 while (m >= 0) {
2785 AppWindowToken wtoken = mAppTokens.get(m);
2786 if (!wtoken.removed) {
2787 Log.w(TAG, "Invalid internal token: " + wtoken.token + " @ " + m);
2788 }
2789 m--;
2790 }
2791 }
2792
2793 boolean checkCallingPermission(String permission, String func) {
2794 // Quick check: if the calling permission is me, it's all okay.
2795 if (Binder.getCallingPid() == Process.myPid()) {
2796 return true;
2797 }
Romain Guy06882f82009-06-10 13:36:04 -07002798
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002799 if (mContext.checkCallingPermission(permission)
2800 == PackageManager.PERMISSION_GRANTED) {
2801 return true;
2802 }
2803 String msg = "Permission Denial: " + func + " from pid="
2804 + Binder.getCallingPid()
2805 + ", uid=" + Binder.getCallingUid()
2806 + " requires " + permission;
2807 Log.w(TAG, msg);
2808 return false;
2809 }
Romain Guy06882f82009-06-10 13:36:04 -07002810
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002811 AppWindowToken findAppWindowToken(IBinder token) {
2812 WindowToken wtoken = mTokenMap.get(token);
2813 if (wtoken == null) {
2814 return null;
2815 }
2816 return wtoken.appWindowToken;
2817 }
Romain Guy06882f82009-06-10 13:36:04 -07002818
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002819 public void addWindowToken(IBinder token, int type) {
2820 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2821 "addWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002822 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002823 }
Romain Guy06882f82009-06-10 13:36:04 -07002824
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002825 synchronized(mWindowMap) {
2826 WindowToken wtoken = mTokenMap.get(token);
2827 if (wtoken != null) {
2828 Log.w(TAG, "Attempted to add existing input method token: " + token);
2829 return;
2830 }
2831 wtoken = new WindowToken(token, type, true);
2832 mTokenMap.put(token, wtoken);
2833 mTokenList.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002834 if (type == TYPE_WALLPAPER) {
2835 mWallpaperTokens.add(wtoken);
2836 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002837 }
2838 }
Romain Guy06882f82009-06-10 13:36:04 -07002839
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002840 public void removeWindowToken(IBinder token) {
2841 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2842 "removeWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002843 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002844 }
2845
2846 final long origId = Binder.clearCallingIdentity();
2847 synchronized(mWindowMap) {
2848 WindowToken wtoken = mTokenMap.remove(token);
2849 mTokenList.remove(wtoken);
2850 if (wtoken != null) {
2851 boolean delayed = false;
2852 if (!wtoken.hidden) {
2853 wtoken.hidden = true;
Romain Guy06882f82009-06-10 13:36:04 -07002854
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002855 final int N = wtoken.windows.size();
2856 boolean changed = false;
Romain Guy06882f82009-06-10 13:36:04 -07002857
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002858 for (int i=0; i<N; i++) {
2859 WindowState win = wtoken.windows.get(i);
2860
2861 if (win.isAnimating()) {
2862 delayed = true;
2863 }
Romain Guy06882f82009-06-10 13:36:04 -07002864
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002865 if (win.isVisibleNow()) {
2866 applyAnimationLocked(win,
2867 WindowManagerPolicy.TRANSIT_EXIT, false);
2868 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2869 KeyWaiter.RETURN_NOTHING);
2870 changed = true;
2871 }
2872 }
2873
2874 if (changed) {
2875 mLayoutNeeded = true;
2876 performLayoutAndPlaceSurfacesLocked();
2877 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2878 }
Romain Guy06882f82009-06-10 13:36:04 -07002879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002880 if (delayed) {
2881 mExitingTokens.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002882 } else if (wtoken.windowType == TYPE_WALLPAPER) {
2883 mWallpaperTokens.remove(wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002884 }
2885 }
Romain Guy06882f82009-06-10 13:36:04 -07002886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002887 } else {
2888 Log.w(TAG, "Attempted to remove non-existing token: " + token);
2889 }
2890 }
2891 Binder.restoreCallingIdentity(origId);
2892 }
2893
2894 public void addAppToken(int addPos, IApplicationToken token,
2895 int groupId, int requestedOrientation, boolean fullscreen) {
2896 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2897 "addAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002898 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002899 }
Romain Guy06882f82009-06-10 13:36:04 -07002900
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002901 synchronized(mWindowMap) {
2902 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2903 if (wtoken != null) {
2904 Log.w(TAG, "Attempted to add existing app token: " + token);
2905 return;
2906 }
2907 wtoken = new AppWindowToken(token);
2908 wtoken.groupId = groupId;
2909 wtoken.appFullscreen = fullscreen;
2910 wtoken.requestedOrientation = requestedOrientation;
2911 mAppTokens.add(addPos, wtoken);
Dave Bortcfe65242009-04-09 14:51:04 -07002912 if (localLOGV) Log.v(TAG, "Adding new app token: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002913 mTokenMap.put(token.asBinder(), wtoken);
2914 mTokenList.add(wtoken);
Romain Guy06882f82009-06-10 13:36:04 -07002915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002916 // Application tokens start out hidden.
2917 wtoken.hidden = true;
2918 wtoken.hiddenRequested = true;
Romain Guy06882f82009-06-10 13:36:04 -07002919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002920 //dump();
2921 }
2922 }
Romain Guy06882f82009-06-10 13:36:04 -07002923
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002924 public void setAppGroupId(IBinder token, int groupId) {
2925 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2926 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002927 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002928 }
2929
2930 synchronized(mWindowMap) {
2931 AppWindowToken wtoken = findAppWindowToken(token);
2932 if (wtoken == null) {
2933 Log.w(TAG, "Attempted to set group id of non-existing app token: " + token);
2934 return;
2935 }
2936 wtoken.groupId = groupId;
2937 }
2938 }
Romain Guy06882f82009-06-10 13:36:04 -07002939
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002940 public int getOrientationFromWindowsLocked() {
2941 int pos = mWindows.size() - 1;
2942 while (pos >= 0) {
2943 WindowState wtoken = (WindowState) mWindows.get(pos);
2944 pos--;
2945 if (wtoken.mAppToken != null) {
2946 // We hit an application window. so the orientation will be determined by the
2947 // app window. No point in continuing further.
2948 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2949 }
2950 if (!wtoken.isVisibleLw()) {
2951 continue;
2952 }
2953 int req = wtoken.mAttrs.screenOrientation;
2954 if((req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) ||
2955 (req == ActivityInfo.SCREEN_ORIENTATION_BEHIND)){
2956 continue;
2957 } else {
2958 return req;
2959 }
2960 }
2961 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2962 }
Romain Guy06882f82009-06-10 13:36:04 -07002963
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002964 public int getOrientationFromAppTokensLocked() {
2965 int pos = mAppTokens.size() - 1;
2966 int curGroup = 0;
2967 int lastOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Owen Lin3413b892009-05-01 17:12:32 -07002968 boolean findingBehind = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002969 boolean haveGroup = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002970 boolean lastFullscreen = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002971 while (pos >= 0) {
2972 AppWindowToken wtoken = mAppTokens.get(pos);
2973 pos--;
Owen Lin3413b892009-05-01 17:12:32 -07002974 // if we're about to tear down this window and not seek for
2975 // the behind activity, don't use it for orientation
2976 if (!findingBehind
2977 && (!wtoken.hidden && wtoken.hiddenRequested)) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002978 continue;
2979 }
2980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002981 if (!haveGroup) {
2982 // We ignore any hidden applications on the top.
2983 if (wtoken.hiddenRequested || wtoken.willBeHidden) {
2984 continue;
2985 }
2986 haveGroup = true;
2987 curGroup = wtoken.groupId;
2988 lastOrientation = wtoken.requestedOrientation;
2989 } else if (curGroup != wtoken.groupId) {
2990 // If we have hit a new application group, and the bottom
2991 // of the previous group didn't explicitly say to use
The Android Open Source Project4df24232009-03-05 14:34:35 -08002992 // the orientation behind it, and the last app was
2993 // full screen, then we'll stick with the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002994 // user's orientation.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002995 if (lastOrientation != ActivityInfo.SCREEN_ORIENTATION_BEHIND
2996 && lastFullscreen) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002997 return lastOrientation;
2998 }
2999 }
3000 int or = wtoken.requestedOrientation;
Owen Lin3413b892009-05-01 17:12:32 -07003001 // If this application is fullscreen, and didn't explicitly say
3002 // to use the orientation behind it, then just take whatever
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003003 // orientation it has and ignores whatever is under it.
The Android Open Source Project4df24232009-03-05 14:34:35 -08003004 lastFullscreen = wtoken.appFullscreen;
Romain Guy06882f82009-06-10 13:36:04 -07003005 if (lastFullscreen
Owen Lin3413b892009-05-01 17:12:32 -07003006 && or != ActivityInfo.SCREEN_ORIENTATION_BEHIND) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003007 return or;
3008 }
3009 // If this application has requested an explicit orientation,
3010 // then use it.
3011 if (or == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE ||
3012 or == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ||
3013 or == ActivityInfo.SCREEN_ORIENTATION_SENSOR ||
3014 or == ActivityInfo.SCREEN_ORIENTATION_NOSENSOR ||
3015 or == ActivityInfo.SCREEN_ORIENTATION_USER) {
3016 return or;
3017 }
Owen Lin3413b892009-05-01 17:12:32 -07003018 findingBehind |= (or == ActivityInfo.SCREEN_ORIENTATION_BEHIND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003019 }
3020 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
3021 }
Romain Guy06882f82009-06-10 13:36:04 -07003022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003023 public Configuration updateOrientationFromAppTokens(
The Android Open Source Project10592532009-03-18 17:39:46 -07003024 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003025 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3026 "updateOrientationFromAppTokens()")) {
3027 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
3028 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003029
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003030 Configuration config;
3031 long ident = Binder.clearCallingIdentity();
Dianne Hackborncfaef692009-06-15 14:24:44 -07003032 config = updateOrientationFromAppTokensUnchecked(currentConfig,
3033 freezeThisOneIfNeeded);
3034 Binder.restoreCallingIdentity(ident);
3035 return config;
3036 }
3037
3038 Configuration updateOrientationFromAppTokensUnchecked(
3039 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
3040 Configuration config;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003041 synchronized(mWindowMap) {
The Android Open Source Project10592532009-03-18 17:39:46 -07003042 config = updateOrientationFromAppTokensLocked(currentConfig, freezeThisOneIfNeeded);
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003043 if (config != null) {
3044 mLayoutNeeded = true;
3045 performLayoutAndPlaceSurfacesLocked();
3046 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003047 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003048 return config;
3049 }
Romain Guy06882f82009-06-10 13:36:04 -07003050
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003051 /*
3052 * The orientation is computed from non-application windows first. If none of
3053 * the non-application windows specify orientation, the orientation is computed from
Romain Guy06882f82009-06-10 13:36:04 -07003054 * application tokens.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003055 * @see android.view.IWindowManager#updateOrientationFromAppTokens(
3056 * android.os.IBinder)
3057 */
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003058 Configuration updateOrientationFromAppTokensLocked(
The Android Open Source Project10592532009-03-18 17:39:46 -07003059 Configuration appConfig, IBinder freezeThisOneIfNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003060 boolean changed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003061 long ident = Binder.clearCallingIdentity();
3062 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003063 int req = computeForcedAppOrientationLocked();
Romain Guy06882f82009-06-10 13:36:04 -07003064
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003065 if (req != mForcedAppOrientation) {
3066 changed = true;
3067 mForcedAppOrientation = req;
3068 //send a message to Policy indicating orientation change to take
3069 //action like disabling/enabling sensors etc.,
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003070 mPolicy.setCurrentOrientationLw(req);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003071 }
Romain Guy06882f82009-06-10 13:36:04 -07003072
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003073 if (changed) {
3074 changed = setRotationUncheckedLocked(
Dianne Hackborn321ae682009-03-27 16:16:03 -07003075 WindowManagerPolicy.USE_LAST_ROTATION,
3076 mLastRotationFlags & (~Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003077 if (changed) {
3078 if (freezeThisOneIfNeeded != null) {
3079 AppWindowToken wtoken = findAppWindowToken(
3080 freezeThisOneIfNeeded);
3081 if (wtoken != null) {
3082 startAppFreezingScreenLocked(wtoken,
3083 ActivityInfo.CONFIG_ORIENTATION);
3084 }
3085 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07003086 return computeNewConfigurationLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003087 }
3088 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003089
3090 // No obvious action we need to take, but if our current
3091 // state mismatches the activity maanager's, update it
3092 if (appConfig != null) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07003093 mTempConfiguration.setToDefaults();
3094 if (computeNewConfigurationLocked(mTempConfiguration)) {
3095 if (appConfig.diff(mTempConfiguration) != 0) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07003096 return new Configuration(mTempConfiguration);
3097 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003098 }
3099 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003100 } finally {
3101 Binder.restoreCallingIdentity(ident);
3102 }
Romain Guy06882f82009-06-10 13:36:04 -07003103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003104 return null;
3105 }
Romain Guy06882f82009-06-10 13:36:04 -07003106
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003107 int computeForcedAppOrientationLocked() {
3108 int req = getOrientationFromWindowsLocked();
3109 if (req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
3110 req = getOrientationFromAppTokensLocked();
3111 }
3112 return req;
3113 }
Romain Guy06882f82009-06-10 13:36:04 -07003114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003115 public void setAppOrientation(IApplicationToken token, int requestedOrientation) {
3116 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3117 "setAppOrientation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003118 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003119 }
Romain Guy06882f82009-06-10 13:36:04 -07003120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003121 synchronized(mWindowMap) {
3122 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
3123 if (wtoken == null) {
3124 Log.w(TAG, "Attempted to set orientation of non-existing app token: " + token);
3125 return;
3126 }
Romain Guy06882f82009-06-10 13:36:04 -07003127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003128 wtoken.requestedOrientation = requestedOrientation;
3129 }
3130 }
Romain Guy06882f82009-06-10 13:36:04 -07003131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003132 public int getAppOrientation(IApplicationToken token) {
3133 synchronized(mWindowMap) {
3134 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
3135 if (wtoken == null) {
3136 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
3137 }
Romain Guy06882f82009-06-10 13:36:04 -07003138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003139 return wtoken.requestedOrientation;
3140 }
3141 }
Romain Guy06882f82009-06-10 13:36:04 -07003142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003143 public void setFocusedApp(IBinder token, boolean moveFocusNow) {
3144 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3145 "setFocusedApp()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003146 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003147 }
3148
3149 synchronized(mWindowMap) {
3150 boolean changed = false;
3151 if (token == null) {
3152 if (DEBUG_FOCUS) Log.v(TAG, "Clearing focused app, was " + mFocusedApp);
3153 changed = mFocusedApp != null;
3154 mFocusedApp = null;
3155 mKeyWaiter.tickle();
3156 } else {
3157 AppWindowToken newFocus = findAppWindowToken(token);
3158 if (newFocus == null) {
3159 Log.w(TAG, "Attempted to set focus to non-existing app token: " + token);
3160 return;
3161 }
3162 changed = mFocusedApp != newFocus;
3163 mFocusedApp = newFocus;
3164 if (DEBUG_FOCUS) Log.v(TAG, "Set focused app to: " + mFocusedApp);
3165 mKeyWaiter.tickle();
3166 }
3167
3168 if (moveFocusNow && changed) {
3169 final long origId = Binder.clearCallingIdentity();
3170 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3171 Binder.restoreCallingIdentity(origId);
3172 }
3173 }
3174 }
3175
3176 public void prepareAppTransition(int transit) {
3177 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3178 "prepareAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003179 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003180 }
Romain Guy06882f82009-06-10 13:36:04 -07003181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003182 synchronized(mWindowMap) {
3183 if (DEBUG_APP_TRANSITIONS) Log.v(
3184 TAG, "Prepare app transition: transit=" + transit
3185 + " mNextAppTransition=" + mNextAppTransition);
3186 if (!mDisplayFrozen) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003187 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET
3188 || mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003189 mNextAppTransition = transit;
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07003190 } else if (transit == WindowManagerPolicy.TRANSIT_TASK_OPEN
3191 && mNextAppTransition == WindowManagerPolicy.TRANSIT_TASK_CLOSE) {
3192 // Opening a new task always supersedes a close for the anim.
3193 mNextAppTransition = transit;
3194 } else if (transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN
3195 && mNextAppTransition == WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE) {
3196 // Opening a new activity always supersedes a close for the anim.
3197 mNextAppTransition = transit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003198 }
3199 mAppTransitionReady = false;
3200 mAppTransitionTimeout = false;
3201 mStartingIconInTransition = false;
3202 mSkipAppTransitionAnimation = false;
3203 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
3204 mH.sendMessageDelayed(mH.obtainMessage(H.APP_TRANSITION_TIMEOUT),
3205 5000);
3206 }
3207 }
3208 }
3209
3210 public int getPendingAppTransition() {
3211 return mNextAppTransition;
3212 }
Romain Guy06882f82009-06-10 13:36:04 -07003213
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003214 public void overridePendingAppTransition(String packageName,
3215 int enterAnim, int exitAnim) {
Dianne Hackborn8b571a82009-09-25 16:09:43 -07003216 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003217 mNextAppTransitionPackage = packageName;
3218 mNextAppTransitionEnter = enterAnim;
3219 mNextAppTransitionExit = exitAnim;
3220 }
3221 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003223 public void executeAppTransition() {
3224 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3225 "executeAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003226 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003227 }
Romain Guy06882f82009-06-10 13:36:04 -07003228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003229 synchronized(mWindowMap) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003230 if (DEBUG_APP_TRANSITIONS) {
3231 RuntimeException e = new RuntimeException("here");
3232 e.fillInStackTrace();
3233 Log.w(TAG, "Execute app transition: mNextAppTransition="
3234 + mNextAppTransition, e);
3235 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003236 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003237 mAppTransitionReady = true;
3238 final long origId = Binder.clearCallingIdentity();
3239 performLayoutAndPlaceSurfacesLocked();
3240 Binder.restoreCallingIdentity(origId);
3241 }
3242 }
3243 }
3244
3245 public void setAppStartingWindow(IBinder token, String pkg,
3246 int theme, CharSequence nonLocalizedLabel, int labelRes, int icon,
3247 IBinder transferFrom, boolean createIfNeeded) {
3248 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3249 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003250 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003251 }
3252
3253 synchronized(mWindowMap) {
3254 if (DEBUG_STARTING_WINDOW) Log.v(
3255 TAG, "setAppStartingIcon: token=" + token + " pkg=" + pkg
3256 + " transferFrom=" + transferFrom);
Romain Guy06882f82009-06-10 13:36:04 -07003257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003258 AppWindowToken wtoken = findAppWindowToken(token);
3259 if (wtoken == null) {
3260 Log.w(TAG, "Attempted to set icon of non-existing app token: " + token);
3261 return;
3262 }
3263
3264 // If the display is frozen, we won't do anything until the
3265 // actual window is displayed so there is no reason to put in
3266 // the starting window.
Dianne Hackbornde2606d2009-12-18 16:53:55 -08003267 if (mDisplayFrozen || !mPolicy.isScreenOn()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003268 return;
3269 }
Romain Guy06882f82009-06-10 13:36:04 -07003270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003271 if (wtoken.startingData != null) {
3272 return;
3273 }
Romain Guy06882f82009-06-10 13:36:04 -07003274
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003275 if (transferFrom != null) {
3276 AppWindowToken ttoken = findAppWindowToken(transferFrom);
3277 if (ttoken != null) {
3278 WindowState startingWindow = ttoken.startingWindow;
3279 if (startingWindow != null) {
3280 if (mStartingIconInTransition) {
3281 // In this case, the starting icon has already
3282 // been displayed, so start letting windows get
3283 // shown immediately without any more transitions.
3284 mSkipAppTransitionAnimation = true;
3285 }
3286 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
3287 "Moving existing starting from " + ttoken
3288 + " to " + wtoken);
3289 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07003290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003291 // Transfer the starting window over to the new
3292 // token.
3293 wtoken.startingData = ttoken.startingData;
3294 wtoken.startingView = ttoken.startingView;
3295 wtoken.startingWindow = startingWindow;
3296 ttoken.startingData = null;
3297 ttoken.startingView = null;
3298 ttoken.startingWindow = null;
3299 ttoken.startingMoved = true;
3300 startingWindow.mToken = wtoken;
Dianne Hackbornef49c572009-03-24 19:27:32 -07003301 startingWindow.mRootToken = wtoken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003302 startingWindow.mAppToken = wtoken;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003303 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
3304 "Removing starting window: " + startingWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003305 mWindows.remove(startingWindow);
3306 ttoken.windows.remove(startingWindow);
3307 ttoken.allAppWindows.remove(startingWindow);
3308 addWindowToListInOrderLocked(startingWindow, true);
Romain Guy06882f82009-06-10 13:36:04 -07003309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003310 // Propagate other interesting state between the
3311 // tokens. If the old token is displayed, we should
3312 // immediately force the new one to be displayed. If
3313 // it is animating, we need to move that animation to
3314 // the new one.
3315 if (ttoken.allDrawn) {
3316 wtoken.allDrawn = true;
3317 }
3318 if (ttoken.firstWindowDrawn) {
3319 wtoken.firstWindowDrawn = true;
3320 }
3321 if (!ttoken.hidden) {
3322 wtoken.hidden = false;
3323 wtoken.hiddenRequested = false;
3324 wtoken.willBeHidden = false;
3325 }
3326 if (wtoken.clientHidden != ttoken.clientHidden) {
3327 wtoken.clientHidden = ttoken.clientHidden;
3328 wtoken.sendAppVisibilityToClients();
3329 }
3330 if (ttoken.animation != null) {
3331 wtoken.animation = ttoken.animation;
3332 wtoken.animating = ttoken.animating;
3333 wtoken.animLayerAdjustment = ttoken.animLayerAdjustment;
3334 ttoken.animation = null;
3335 ttoken.animLayerAdjustment = 0;
3336 wtoken.updateLayers();
3337 ttoken.updateLayers();
3338 }
Romain Guy06882f82009-06-10 13:36:04 -07003339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003340 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003341 mLayoutNeeded = true;
3342 performLayoutAndPlaceSurfacesLocked();
3343 Binder.restoreCallingIdentity(origId);
3344 return;
3345 } else if (ttoken.startingData != null) {
3346 // The previous app was getting ready to show a
3347 // starting window, but hasn't yet done so. Steal it!
3348 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
3349 "Moving pending starting from " + ttoken
3350 + " to " + wtoken);
3351 wtoken.startingData = ttoken.startingData;
3352 ttoken.startingData = null;
3353 ttoken.startingMoved = true;
3354 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3355 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3356 // want to process the message ASAP, before any other queued
3357 // messages.
3358 mH.sendMessageAtFrontOfQueue(m);
3359 return;
3360 }
3361 }
3362 }
3363
3364 // There is no existing starting window, and the caller doesn't
3365 // want us to create one, so that's it!
3366 if (!createIfNeeded) {
3367 return;
3368 }
Romain Guy06882f82009-06-10 13:36:04 -07003369
Dianne Hackborn284ac932009-08-28 10:34:25 -07003370 // If this is a translucent or wallpaper window, then don't
3371 // show a starting window -- the current effect (a full-screen
3372 // opaque starting window that fades away to the real contents
3373 // when it is ready) does not work for this.
3374 if (theme != 0) {
3375 AttributeCache.Entry ent = AttributeCache.instance().get(pkg, theme,
3376 com.android.internal.R.styleable.Window);
3377 if (ent.array.getBoolean(
3378 com.android.internal.R.styleable.Window_windowIsTranslucent, false)) {
3379 return;
3380 }
3381 if (ent.array.getBoolean(
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07003382 com.android.internal.R.styleable.Window_windowIsFloating, false)) {
3383 return;
3384 }
3385 if (ent.array.getBoolean(
Dianne Hackborn284ac932009-08-28 10:34:25 -07003386 com.android.internal.R.styleable.Window_windowShowWallpaper, false)) {
3387 return;
3388 }
3389 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003391 mStartingIconInTransition = true;
3392 wtoken.startingData = new StartingData(
3393 pkg, theme, nonLocalizedLabel,
3394 labelRes, icon);
3395 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3396 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3397 // want to process the message ASAP, before any other queued
3398 // messages.
3399 mH.sendMessageAtFrontOfQueue(m);
3400 }
3401 }
3402
3403 public void setAppWillBeHidden(IBinder token) {
3404 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3405 "setAppWillBeHidden()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003406 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003407 }
3408
3409 AppWindowToken wtoken;
3410
3411 synchronized(mWindowMap) {
3412 wtoken = findAppWindowToken(token);
3413 if (wtoken == null) {
3414 Log.w(TAG, "Attempted to set will be hidden of non-existing app token: " + token);
3415 return;
3416 }
3417 wtoken.willBeHidden = true;
3418 }
3419 }
Romain Guy06882f82009-06-10 13:36:04 -07003420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003421 boolean setTokenVisibilityLocked(AppWindowToken wtoken, WindowManager.LayoutParams lp,
3422 boolean visible, int transit, boolean performLayout) {
3423 boolean delayed = false;
3424
3425 if (wtoken.clientHidden == visible) {
3426 wtoken.clientHidden = !visible;
3427 wtoken.sendAppVisibilityToClients();
3428 }
Romain Guy06882f82009-06-10 13:36:04 -07003429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003430 wtoken.willBeHidden = false;
3431 if (wtoken.hidden == visible) {
3432 final int N = wtoken.allAppWindows.size();
3433 boolean changed = false;
3434 if (DEBUG_APP_TRANSITIONS) Log.v(
3435 TAG, "Changing app " + wtoken + " hidden=" + wtoken.hidden
3436 + " performLayout=" + performLayout);
Romain Guy06882f82009-06-10 13:36:04 -07003437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003438 boolean runningAppAnimation = false;
Romain Guy06882f82009-06-10 13:36:04 -07003439
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003440 if (transit != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003441 if (wtoken.animation == sDummyAnimation) {
3442 wtoken.animation = null;
3443 }
3444 applyAnimationLocked(wtoken, lp, transit, visible);
3445 changed = true;
3446 if (wtoken.animation != null) {
3447 delayed = runningAppAnimation = true;
3448 }
3449 }
Romain Guy06882f82009-06-10 13:36:04 -07003450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003451 for (int i=0; i<N; i++) {
3452 WindowState win = wtoken.allAppWindows.get(i);
3453 if (win == wtoken.startingWindow) {
3454 continue;
3455 }
3456
3457 if (win.isAnimating()) {
3458 delayed = true;
3459 }
Romain Guy06882f82009-06-10 13:36:04 -07003460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003461 //Log.i(TAG, "Window " + win + ": vis=" + win.isVisible());
3462 //win.dump(" ");
3463 if (visible) {
3464 if (!win.isVisibleNow()) {
3465 if (!runningAppAnimation) {
3466 applyAnimationLocked(win,
3467 WindowManagerPolicy.TRANSIT_ENTER, true);
3468 }
3469 changed = true;
3470 }
3471 } else if (win.isVisibleNow()) {
3472 if (!runningAppAnimation) {
3473 applyAnimationLocked(win,
3474 WindowManagerPolicy.TRANSIT_EXIT, false);
3475 }
3476 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
3477 KeyWaiter.RETURN_NOTHING);
3478 changed = true;
3479 }
3480 }
3481
3482 wtoken.hidden = wtoken.hiddenRequested = !visible;
3483 if (!visible) {
3484 unsetAppFreezingScreenLocked(wtoken, true, true);
3485 } else {
3486 // If we are being set visible, and the starting window is
3487 // not yet displayed, then make sure it doesn't get displayed.
3488 WindowState swin = wtoken.startingWindow;
3489 if (swin != null && (swin.mDrawPending
3490 || swin.mCommitDrawPending)) {
3491 swin.mPolicyVisibility = false;
3492 swin.mPolicyVisibilityAfterAnim = false;
3493 }
3494 }
Romain Guy06882f82009-06-10 13:36:04 -07003495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003496 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "setTokenVisibilityLocked: " + wtoken
3497 + ": hidden=" + wtoken.hidden + " hiddenRequested="
3498 + wtoken.hiddenRequested);
Romain Guy06882f82009-06-10 13:36:04 -07003499
Dianne Hackborn9b52a212009-12-11 14:51:35 -08003500 if (changed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003501 mLayoutNeeded = true;
Dianne Hackborn9b52a212009-12-11 14:51:35 -08003502 if (performLayout) {
3503 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
3504 performLayoutAndPlaceSurfacesLocked();
3505 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003506 }
3507 }
3508
3509 if (wtoken.animation != null) {
3510 delayed = true;
3511 }
Romain Guy06882f82009-06-10 13:36:04 -07003512
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003513 return delayed;
3514 }
3515
3516 public void setAppVisibility(IBinder token, boolean visible) {
3517 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3518 "setAppVisibility()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003519 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003520 }
3521
3522 AppWindowToken wtoken;
3523
3524 synchronized(mWindowMap) {
3525 wtoken = findAppWindowToken(token);
3526 if (wtoken == null) {
3527 Log.w(TAG, "Attempted to set visibility of non-existing app token: " + token);
3528 return;
3529 }
3530
3531 if (DEBUG_APP_TRANSITIONS || DEBUG_ORIENTATION) {
3532 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003533 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003534 Log.v(TAG, "setAppVisibility(" + token + ", " + visible
3535 + "): mNextAppTransition=" + mNextAppTransition
3536 + " hidden=" + wtoken.hidden
3537 + " hiddenRequested=" + wtoken.hiddenRequested, e);
3538 }
Romain Guy06882f82009-06-10 13:36:04 -07003539
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003540 // If we are preparing an app transition, then delay changing
3541 // the visibility of this token until we execute that transition.
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003542 if (!mDisplayFrozen && mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003543 // Already in requested state, don't do anything more.
3544 if (wtoken.hiddenRequested != visible) {
3545 return;
3546 }
3547 wtoken.hiddenRequested = !visible;
Romain Guy06882f82009-06-10 13:36:04 -07003548
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003549 if (DEBUG_APP_TRANSITIONS) Log.v(
3550 TAG, "Setting dummy animation on: " + wtoken);
3551 wtoken.setDummyAnimation();
3552 mOpeningApps.remove(wtoken);
3553 mClosingApps.remove(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003554 wtoken.waitingToShow = wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003555 wtoken.inPendingTransaction = true;
3556 if (visible) {
3557 mOpeningApps.add(wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003558 wtoken.startingDisplayed = false;
3559 wtoken.startingMoved = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003560
Dianne Hackborn195f6a02009-11-24 11:26:00 -08003561 // If the token is currently hidden (should be the
3562 // common case), then we need to set up to wait for
3563 // its windows to be ready.
3564 if (wtoken.hidden) {
3565 wtoken.allDrawn = false;
3566 wtoken.waitingToShow = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003567
Dianne Hackborn195f6a02009-11-24 11:26:00 -08003568 if (wtoken.clientHidden) {
3569 // In the case where we are making an app visible
3570 // but holding off for a transition, we still need
3571 // to tell the client to make its windows visible so
3572 // they get drawn. Otherwise, we will wait on
3573 // performing the transition until all windows have
3574 // been drawn, they never will be, and we are sad.
3575 wtoken.clientHidden = false;
3576 wtoken.sendAppVisibilityToClients();
3577 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003578 }
3579 } else {
3580 mClosingApps.add(wtoken);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003581
Dianne Hackborn195f6a02009-11-24 11:26:00 -08003582 // If the token is currently visible (should be the
3583 // common case), then set up to wait for it to be hidden.
3584 if (!wtoken.hidden) {
3585 wtoken.waitingToHide = true;
3586 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003587 }
3588 return;
3589 }
Romain Guy06882f82009-06-10 13:36:04 -07003590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003591 final long origId = Binder.clearCallingIdentity();
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003592 setTokenVisibilityLocked(wtoken, null, visible, WindowManagerPolicy.TRANSIT_UNSET, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003593 wtoken.updateReportedVisibilityLocked();
3594 Binder.restoreCallingIdentity(origId);
3595 }
3596 }
3597
3598 void unsetAppFreezingScreenLocked(AppWindowToken wtoken,
3599 boolean unfreezeSurfaceNow, boolean force) {
3600 if (wtoken.freezingScreen) {
3601 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + wtoken
3602 + " force=" + force);
3603 final int N = wtoken.allAppWindows.size();
3604 boolean unfrozeWindows = false;
3605 for (int i=0; i<N; i++) {
3606 WindowState w = wtoken.allAppWindows.get(i);
3607 if (w.mAppFreezing) {
3608 w.mAppFreezing = false;
3609 if (w.mSurface != null && !w.mOrientationChanging) {
3610 w.mOrientationChanging = true;
3611 }
3612 unfrozeWindows = true;
3613 }
3614 }
3615 if (force || unfrozeWindows) {
3616 if (DEBUG_ORIENTATION) Log.v(TAG, "No longer freezing: " + wtoken);
3617 wtoken.freezingScreen = false;
3618 mAppsFreezingScreen--;
3619 }
3620 if (unfreezeSurfaceNow) {
3621 if (unfrozeWindows) {
3622 mLayoutNeeded = true;
3623 performLayoutAndPlaceSurfacesLocked();
3624 }
3625 if (mAppsFreezingScreen == 0 && !mWindowsFreezingScreen) {
3626 stopFreezingDisplayLocked();
3627 }
3628 }
3629 }
3630 }
Romain Guy06882f82009-06-10 13:36:04 -07003631
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003632 public void startAppFreezingScreenLocked(AppWindowToken wtoken,
3633 int configChanges) {
3634 if (DEBUG_ORIENTATION) {
3635 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003636 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003637 Log.i(TAG, "Set freezing of " + wtoken.appToken
3638 + ": hidden=" + wtoken.hidden + " freezing="
3639 + wtoken.freezingScreen, e);
3640 }
3641 if (!wtoken.hiddenRequested) {
3642 if (!wtoken.freezingScreen) {
3643 wtoken.freezingScreen = true;
3644 mAppsFreezingScreen++;
3645 if (mAppsFreezingScreen == 1) {
3646 startFreezingDisplayLocked();
3647 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
3648 mH.sendMessageDelayed(mH.obtainMessage(H.APP_FREEZE_TIMEOUT),
3649 5000);
3650 }
3651 }
3652 final int N = wtoken.allAppWindows.size();
3653 for (int i=0; i<N; i++) {
3654 WindowState w = wtoken.allAppWindows.get(i);
3655 w.mAppFreezing = true;
3656 }
3657 }
3658 }
Romain Guy06882f82009-06-10 13:36:04 -07003659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003660 public void startAppFreezingScreen(IBinder token, int configChanges) {
3661 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3662 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003663 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003664 }
3665
3666 synchronized(mWindowMap) {
3667 if (configChanges == 0 && !mDisplayFrozen) {
3668 if (DEBUG_ORIENTATION) Log.v(TAG, "Skipping set freeze of " + token);
3669 return;
3670 }
Romain Guy06882f82009-06-10 13:36:04 -07003671
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003672 AppWindowToken wtoken = findAppWindowToken(token);
3673 if (wtoken == null || wtoken.appToken == null) {
3674 Log.w(TAG, "Attempted to freeze screen with non-existing app token: " + wtoken);
3675 return;
3676 }
3677 final long origId = Binder.clearCallingIdentity();
3678 startAppFreezingScreenLocked(wtoken, configChanges);
3679 Binder.restoreCallingIdentity(origId);
3680 }
3681 }
Romain Guy06882f82009-06-10 13:36:04 -07003682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003683 public void stopAppFreezingScreen(IBinder token, boolean force) {
3684 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3685 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003686 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003687 }
3688
3689 synchronized(mWindowMap) {
3690 AppWindowToken wtoken = findAppWindowToken(token);
3691 if (wtoken == null || wtoken.appToken == null) {
3692 return;
3693 }
3694 final long origId = Binder.clearCallingIdentity();
3695 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + token
3696 + ": hidden=" + wtoken.hidden + " freezing=" + wtoken.freezingScreen);
3697 unsetAppFreezingScreenLocked(wtoken, true, force);
3698 Binder.restoreCallingIdentity(origId);
3699 }
3700 }
Romain Guy06882f82009-06-10 13:36:04 -07003701
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003702 public void removeAppToken(IBinder token) {
3703 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3704 "removeAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003705 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003706 }
3707
3708 AppWindowToken wtoken = null;
3709 AppWindowToken startingToken = null;
3710 boolean delayed = false;
3711
3712 final long origId = Binder.clearCallingIdentity();
3713 synchronized(mWindowMap) {
3714 WindowToken basewtoken = mTokenMap.remove(token);
3715 mTokenList.remove(basewtoken);
3716 if (basewtoken != null && (wtoken=basewtoken.appWindowToken) != null) {
3717 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "Removing app token: " + wtoken);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003718 delayed = setTokenVisibilityLocked(wtoken, null, false, WindowManagerPolicy.TRANSIT_UNSET, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003719 wtoken.inPendingTransaction = false;
3720 mOpeningApps.remove(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003721 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003722 if (mClosingApps.contains(wtoken)) {
3723 delayed = true;
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003724 } else if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003725 mClosingApps.add(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003726 wtoken.waitingToHide = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003727 delayed = true;
3728 }
3729 if (DEBUG_APP_TRANSITIONS) Log.v(
3730 TAG, "Removing app " + wtoken + " delayed=" + delayed
3731 + " animation=" + wtoken.animation
3732 + " animating=" + wtoken.animating);
3733 if (delayed) {
3734 // set the token aside because it has an active animation to be finished
3735 mExitingAppTokens.add(wtoken);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003736 } else {
3737 // Make sure there is no animation running on this token,
3738 // so any windows associated with it will be removed as
3739 // soon as their animations are complete
3740 wtoken.animation = null;
3741 wtoken.animating = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003742 }
3743 mAppTokens.remove(wtoken);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003744 if (mLastEnterAnimToken == wtoken) {
3745 mLastEnterAnimToken = null;
3746 mLastEnterAnimParams = null;
3747 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003748 wtoken.removed = true;
3749 if (wtoken.startingData != null) {
3750 startingToken = wtoken;
3751 }
3752 unsetAppFreezingScreenLocked(wtoken, true, true);
3753 if (mFocusedApp == wtoken) {
3754 if (DEBUG_FOCUS) Log.v(TAG, "Removing focused app token:" + wtoken);
3755 mFocusedApp = null;
3756 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3757 mKeyWaiter.tickle();
3758 }
3759 } else {
3760 Log.w(TAG, "Attempted to remove non-existing app token: " + token);
3761 }
Romain Guy06882f82009-06-10 13:36:04 -07003762
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003763 if (!delayed && wtoken != null) {
3764 wtoken.updateReportedVisibilityLocked();
3765 }
3766 }
3767 Binder.restoreCallingIdentity(origId);
3768
3769 if (startingToken != null) {
3770 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Schedule remove starting "
3771 + startingToken + ": app token removed");
3772 Message m = mH.obtainMessage(H.REMOVE_STARTING, startingToken);
3773 mH.sendMessage(m);
3774 }
3775 }
3776
3777 private boolean tmpRemoveAppWindowsLocked(WindowToken token) {
3778 final int NW = token.windows.size();
3779 for (int i=0; i<NW; i++) {
3780 WindowState win = token.windows.get(i);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003781 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Tmp removing app window " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003782 mWindows.remove(win);
3783 int j = win.mChildWindows.size();
3784 while (j > 0) {
3785 j--;
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003786 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3787 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
3788 "Tmp removing child window " + cwin);
3789 mWindows.remove(cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003790 }
3791 }
3792 return NW > 0;
3793 }
3794
3795 void dumpAppTokensLocked() {
3796 for (int i=mAppTokens.size()-1; i>=0; i--) {
3797 Log.v(TAG, " #" + i + ": " + mAppTokens.get(i).token);
3798 }
3799 }
Romain Guy06882f82009-06-10 13:36:04 -07003800
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003801 void dumpWindowsLocked() {
3802 for (int i=mWindows.size()-1; i>=0; i--) {
3803 Log.v(TAG, " #" + i + ": " + mWindows.get(i));
3804 }
3805 }
Romain Guy06882f82009-06-10 13:36:04 -07003806
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003807 private int findWindowOffsetLocked(int tokenPos) {
3808 final int NW = mWindows.size();
3809
3810 if (tokenPos >= mAppTokens.size()) {
3811 int i = NW;
3812 while (i > 0) {
3813 i--;
3814 WindowState win = (WindowState)mWindows.get(i);
3815 if (win.getAppToken() != null) {
3816 return i+1;
3817 }
3818 }
3819 }
3820
3821 while (tokenPos > 0) {
3822 // Find the first app token below the new position that has
3823 // a window displayed.
3824 final AppWindowToken wtoken = mAppTokens.get(tokenPos-1);
3825 if (DEBUG_REORDER) Log.v(TAG, "Looking for lower windows @ "
3826 + tokenPos + " -- " + wtoken.token);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003827 if (wtoken.sendingToBottom) {
3828 if (DEBUG_REORDER) Log.v(TAG,
3829 "Skipping token -- currently sending to bottom");
3830 tokenPos--;
3831 continue;
3832 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003833 int i = wtoken.windows.size();
3834 while (i > 0) {
3835 i--;
3836 WindowState win = wtoken.windows.get(i);
3837 int j = win.mChildWindows.size();
3838 while (j > 0) {
3839 j--;
3840 WindowState cwin = (WindowState)win.mChildWindows.get(j);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003841 if (cwin.mSubLayer >= 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003842 for (int pos=NW-1; pos>=0; pos--) {
3843 if (mWindows.get(pos) == cwin) {
3844 if (DEBUG_REORDER) Log.v(TAG,
3845 "Found child win @" + (pos+1));
3846 return pos+1;
3847 }
3848 }
3849 }
3850 }
3851 for (int pos=NW-1; pos>=0; pos--) {
3852 if (mWindows.get(pos) == win) {
3853 if (DEBUG_REORDER) Log.v(TAG, "Found win @" + (pos+1));
3854 return pos+1;
3855 }
3856 }
3857 }
3858 tokenPos--;
3859 }
3860
3861 return 0;
3862 }
3863
3864 private final int reAddWindowLocked(int index, WindowState win) {
3865 final int NCW = win.mChildWindows.size();
3866 boolean added = false;
3867 for (int j=0; j<NCW; j++) {
3868 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3869 if (!added && cwin.mSubLayer >= 0) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003870 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding child window at "
3871 + index + ": " + cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003872 mWindows.add(index, win);
3873 index++;
3874 added = true;
3875 }
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003876 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding window at "
3877 + index + ": " + cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003878 mWindows.add(index, cwin);
3879 index++;
3880 }
3881 if (!added) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003882 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding window at "
3883 + index + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003884 mWindows.add(index, win);
3885 index++;
3886 }
3887 return index;
3888 }
Romain Guy06882f82009-06-10 13:36:04 -07003889
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003890 private final int reAddAppWindowsLocked(int index, WindowToken token) {
3891 final int NW = token.windows.size();
3892 for (int i=0; i<NW; i++) {
3893 index = reAddWindowLocked(index, token.windows.get(i));
3894 }
3895 return index;
3896 }
3897
3898 public void moveAppToken(int index, IBinder token) {
3899 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3900 "moveAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003901 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003902 }
3903
3904 synchronized(mWindowMap) {
3905 if (DEBUG_REORDER) Log.v(TAG, "Initial app tokens:");
3906 if (DEBUG_REORDER) dumpAppTokensLocked();
3907 final AppWindowToken wtoken = findAppWindowToken(token);
3908 if (wtoken == null || !mAppTokens.remove(wtoken)) {
3909 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3910 + token + " (" + wtoken + ")");
3911 return;
3912 }
3913 mAppTokens.add(index, wtoken);
3914 if (DEBUG_REORDER) Log.v(TAG, "Moved " + token + " to " + index + ":");
3915 if (DEBUG_REORDER) dumpAppTokensLocked();
Romain Guy06882f82009-06-10 13:36:04 -07003916
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003917 final long origId = Binder.clearCallingIdentity();
3918 if (DEBUG_REORDER) Log.v(TAG, "Removing windows in " + token + ":");
3919 if (DEBUG_REORDER) dumpWindowsLocked();
3920 if (tmpRemoveAppWindowsLocked(wtoken)) {
3921 if (DEBUG_REORDER) Log.v(TAG, "Adding windows back in:");
3922 if (DEBUG_REORDER) dumpWindowsLocked();
3923 reAddAppWindowsLocked(findWindowOffsetLocked(index), wtoken);
3924 if (DEBUG_REORDER) Log.v(TAG, "Final window list:");
3925 if (DEBUG_REORDER) dumpWindowsLocked();
3926 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003927 mLayoutNeeded = true;
3928 performLayoutAndPlaceSurfacesLocked();
3929 }
3930 Binder.restoreCallingIdentity(origId);
3931 }
3932 }
3933
3934 private void removeAppTokensLocked(List<IBinder> tokens) {
3935 // XXX This should be done more efficiently!
3936 // (take advantage of the fact that both lists should be
3937 // ordered in the same way.)
3938 int N = tokens.size();
3939 for (int i=0; i<N; i++) {
3940 IBinder token = tokens.get(i);
3941 final AppWindowToken wtoken = findAppWindowToken(token);
3942 if (!mAppTokens.remove(wtoken)) {
3943 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3944 + token + " (" + wtoken + ")");
3945 i--;
3946 N--;
3947 }
3948 }
3949 }
3950
Dianne Hackborna8f60182009-09-01 19:01:50 -07003951 private void moveAppWindowsLocked(AppWindowToken wtoken, int tokenPos,
3952 boolean updateFocusAndLayout) {
3953 // First remove all of the windows from the list.
3954 tmpRemoveAppWindowsLocked(wtoken);
3955
3956 // Where to start adding?
3957 int pos = findWindowOffsetLocked(tokenPos);
3958
3959 // And now add them back at the correct place.
3960 pos = reAddAppWindowsLocked(pos, wtoken);
3961
3962 if (updateFocusAndLayout) {
3963 if (!updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
3964 assignLayersLocked();
3965 }
3966 mLayoutNeeded = true;
3967 performLayoutAndPlaceSurfacesLocked();
3968 }
3969 }
3970
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003971 private void moveAppWindowsLocked(List<IBinder> tokens, int tokenPos) {
3972 // First remove all of the windows from the list.
3973 final int N = tokens.size();
3974 int i;
3975 for (i=0; i<N; i++) {
3976 WindowToken token = mTokenMap.get(tokens.get(i));
3977 if (token != null) {
3978 tmpRemoveAppWindowsLocked(token);
3979 }
3980 }
3981
3982 // Where to start adding?
3983 int pos = findWindowOffsetLocked(tokenPos);
3984
3985 // And now add them back at the correct place.
3986 for (i=0; i<N; i++) {
3987 WindowToken token = mTokenMap.get(tokens.get(i));
3988 if (token != null) {
3989 pos = reAddAppWindowsLocked(pos, token);
3990 }
3991 }
3992
Dianne Hackborna8f60182009-09-01 19:01:50 -07003993 if (!updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
3994 assignLayersLocked();
3995 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003996 mLayoutNeeded = true;
3997 performLayoutAndPlaceSurfacesLocked();
3998
3999 //dump();
4000 }
4001
4002 public void moveAppTokensToTop(List<IBinder> tokens) {
4003 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4004 "moveAppTokensToTop()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004005 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004006 }
4007
4008 final long origId = Binder.clearCallingIdentity();
4009 synchronized(mWindowMap) {
4010 removeAppTokensLocked(tokens);
4011 final int N = tokens.size();
4012 for (int i=0; i<N; i++) {
4013 AppWindowToken wt = findAppWindowToken(tokens.get(i));
4014 if (wt != null) {
4015 mAppTokens.add(wt);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004016 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004017 mToTopApps.remove(wt);
4018 mToBottomApps.remove(wt);
4019 mToTopApps.add(wt);
4020 wt.sendingToBottom = false;
4021 wt.sendingToTop = true;
4022 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004023 }
4024 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004025
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004026 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004027 moveAppWindowsLocked(tokens, mAppTokens.size());
4028 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004029 }
4030 Binder.restoreCallingIdentity(origId);
4031 }
4032
4033 public void moveAppTokensToBottom(List<IBinder> tokens) {
4034 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4035 "moveAppTokensToBottom()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004036 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004037 }
4038
4039 final long origId = Binder.clearCallingIdentity();
4040 synchronized(mWindowMap) {
4041 removeAppTokensLocked(tokens);
4042 final int N = tokens.size();
4043 int pos = 0;
4044 for (int i=0; i<N; i++) {
4045 AppWindowToken wt = findAppWindowToken(tokens.get(i));
4046 if (wt != null) {
4047 mAppTokens.add(pos, wt);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004048 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004049 mToTopApps.remove(wt);
4050 mToBottomApps.remove(wt);
4051 mToBottomApps.add(i, wt);
4052 wt.sendingToTop = false;
4053 wt.sendingToBottom = true;
4054 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004055 pos++;
4056 }
4057 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004058
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004059 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004060 moveAppWindowsLocked(tokens, 0);
4061 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004062 }
4063 Binder.restoreCallingIdentity(origId);
4064 }
4065
4066 // -------------------------------------------------------------
4067 // Misc IWindowSession methods
4068 // -------------------------------------------------------------
Romain Guy06882f82009-06-10 13:36:04 -07004069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004070 public void disableKeyguard(IBinder token, String tag) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04004071 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004072 != PackageManager.PERMISSION_GRANTED) {
4073 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
4074 }
Mike Lockwood983ee092009-11-22 01:42:24 -05004075 synchronized (mKeyguardTokenWatcher) {
4076 mKeyguardTokenWatcher.acquire(token, tag);
Mike Lockwooddd884682009-10-11 16:57:08 -04004077 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004078 }
4079
4080 public void reenableKeyguard(IBinder token) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04004081 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004082 != PackageManager.PERMISSION_GRANTED) {
4083 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
4084 }
Mike Lockwood983ee092009-11-22 01:42:24 -05004085 synchronized (mKeyguardTokenWatcher) {
4086 mKeyguardTokenWatcher.release(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004087
Mike Lockwood983ee092009-11-22 01:42:24 -05004088 if (!mKeyguardTokenWatcher.isAcquired()) {
4089 // If we are the last one to reenable the keyguard wait until
4090 // we have actaully finished reenabling until returning.
4091 // It is possible that reenableKeyguard() can be called before
4092 // the previous disableKeyguard() is handled, in which case
4093 // neither mKeyguardTokenWatcher.acquired() or released() would
4094 // be called. In that case mKeyguardDisabled will be false here
4095 // and we have nothing to wait for.
4096 while (mKeyguardDisabled) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004097 try {
Mike Lockwood983ee092009-11-22 01:42:24 -05004098 mKeyguardTokenWatcher.wait();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004099 } catch (InterruptedException e) {
4100 Thread.currentThread().interrupt();
4101 }
4102 }
4103 }
4104 }
4105 }
4106
4107 /**
4108 * @see android.app.KeyguardManager#exitKeyguardSecurely
4109 */
4110 public void exitKeyguardSecurely(final IOnKeyguardExitResult callback) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04004111 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004112 != PackageManager.PERMISSION_GRANTED) {
4113 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
4114 }
4115 mPolicy.exitKeyguardSecurely(new WindowManagerPolicy.OnKeyguardExitResult() {
4116 public void onKeyguardExitResult(boolean success) {
4117 try {
4118 callback.onKeyguardExitResult(success);
4119 } catch (RemoteException e) {
4120 // Client has died, we don't care.
4121 }
4122 }
4123 });
4124 }
4125
4126 public boolean inKeyguardRestrictedInputMode() {
4127 return mPolicy.inKeyguardRestrictedKeyInputMode();
4128 }
Romain Guy06882f82009-06-10 13:36:04 -07004129
Dianne Hackbornffa42482009-09-23 22:20:11 -07004130 public void closeSystemDialogs(String reason) {
4131 synchronized(mWindowMap) {
4132 for (int i=mWindows.size()-1; i>=0; i--) {
4133 WindowState w = (WindowState)mWindows.get(i);
4134 if (w.mSurface != null) {
4135 try {
4136 w.mClient.closeSystemDialogs(reason);
4137 } catch (RemoteException e) {
4138 }
4139 }
4140 }
4141 }
4142 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004144 static float fixScale(float scale) {
4145 if (scale < 0) scale = 0;
4146 else if (scale > 20) scale = 20;
4147 return Math.abs(scale);
4148 }
Romain Guy06882f82009-06-10 13:36:04 -07004149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004150 public void setAnimationScale(int which, float scale) {
4151 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
4152 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004153 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004154 }
4155
4156 if (scale < 0) scale = 0;
4157 else if (scale > 20) scale = 20;
4158 scale = Math.abs(scale);
4159 switch (which) {
4160 case 0: mWindowAnimationScale = fixScale(scale); break;
4161 case 1: mTransitionAnimationScale = fixScale(scale); break;
4162 }
Romain Guy06882f82009-06-10 13:36:04 -07004163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004164 // Persist setting
4165 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
4166 }
Romain Guy06882f82009-06-10 13:36:04 -07004167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004168 public void setAnimationScales(float[] scales) {
4169 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
4170 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004171 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004172 }
4173
4174 if (scales != null) {
4175 if (scales.length >= 1) {
4176 mWindowAnimationScale = fixScale(scales[0]);
4177 }
4178 if (scales.length >= 2) {
4179 mTransitionAnimationScale = fixScale(scales[1]);
4180 }
4181 }
Romain Guy06882f82009-06-10 13:36:04 -07004182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004183 // Persist setting
4184 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
4185 }
Romain Guy06882f82009-06-10 13:36:04 -07004186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004187 public float getAnimationScale(int which) {
4188 switch (which) {
4189 case 0: return mWindowAnimationScale;
4190 case 1: return mTransitionAnimationScale;
4191 }
4192 return 0;
4193 }
Romain Guy06882f82009-06-10 13:36:04 -07004194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004195 public float[] getAnimationScales() {
4196 return new float[] { mWindowAnimationScale, mTransitionAnimationScale };
4197 }
Romain Guy06882f82009-06-10 13:36:04 -07004198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004199 public int getSwitchState(int sw) {
4200 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4201 "getSwitchState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004202 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004203 }
4204 return KeyInputQueue.getSwitchState(sw);
4205 }
Romain Guy06882f82009-06-10 13:36:04 -07004206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004207 public int getSwitchStateForDevice(int devid, int sw) {
4208 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4209 "getSwitchStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004210 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004211 }
4212 return KeyInputQueue.getSwitchState(devid, sw);
4213 }
Romain Guy06882f82009-06-10 13:36:04 -07004214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004215 public int getScancodeState(int sw) {
4216 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4217 "getScancodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004218 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004219 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004220 return mQueue.getScancodeState(sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004221 }
Romain Guy06882f82009-06-10 13:36:04 -07004222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004223 public int getScancodeStateForDevice(int devid, int sw) {
4224 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4225 "getScancodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004226 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004227 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004228 return mQueue.getScancodeState(devid, sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004229 }
Romain Guy06882f82009-06-10 13:36:04 -07004230
Dianne Hackborn1d62ea92009-11-17 12:49:50 -08004231 public int getTrackballScancodeState(int sw) {
4232 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4233 "getTrackballScancodeState()")) {
4234 throw new SecurityException("Requires READ_INPUT_STATE permission");
4235 }
4236 return mQueue.getTrackballScancodeState(sw);
4237 }
4238
4239 public int getDPadScancodeState(int sw) {
4240 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4241 "getDPadScancodeState()")) {
4242 throw new SecurityException("Requires READ_INPUT_STATE permission");
4243 }
4244 return mQueue.getDPadScancodeState(sw);
4245 }
4246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004247 public int getKeycodeState(int sw) {
4248 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4249 "getKeycodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004250 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004251 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004252 return mQueue.getKeycodeState(sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004253 }
Romain Guy06882f82009-06-10 13:36:04 -07004254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004255 public int getKeycodeStateForDevice(int devid, int sw) {
4256 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4257 "getKeycodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004258 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004259 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004260 return mQueue.getKeycodeState(devid, sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004261 }
Romain Guy06882f82009-06-10 13:36:04 -07004262
Dianne Hackborn1d62ea92009-11-17 12:49:50 -08004263 public int getTrackballKeycodeState(int sw) {
4264 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4265 "getTrackballKeycodeState()")) {
4266 throw new SecurityException("Requires READ_INPUT_STATE permission");
4267 }
4268 return mQueue.getTrackballKeycodeState(sw);
4269 }
4270
4271 public int getDPadKeycodeState(int sw) {
4272 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4273 "getDPadKeycodeState()")) {
4274 throw new SecurityException("Requires READ_INPUT_STATE permission");
4275 }
4276 return mQueue.getDPadKeycodeState(sw);
4277 }
4278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004279 public boolean hasKeys(int[] keycodes, boolean[] keyExists) {
4280 return KeyInputQueue.hasKeys(keycodes, keyExists);
4281 }
Romain Guy06882f82009-06-10 13:36:04 -07004282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004283 public void enableScreenAfterBoot() {
4284 synchronized(mWindowMap) {
4285 if (mSystemBooted) {
4286 return;
4287 }
4288 mSystemBooted = true;
4289 }
Romain Guy06882f82009-06-10 13:36:04 -07004290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004291 performEnableScreen();
4292 }
Romain Guy06882f82009-06-10 13:36:04 -07004293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004294 public void enableScreenIfNeededLocked() {
4295 if (mDisplayEnabled) {
4296 return;
4297 }
4298 if (!mSystemBooted) {
4299 return;
4300 }
4301 mH.sendMessage(mH.obtainMessage(H.ENABLE_SCREEN));
4302 }
Romain Guy06882f82009-06-10 13:36:04 -07004303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004304 public void performEnableScreen() {
4305 synchronized(mWindowMap) {
4306 if (mDisplayEnabled) {
4307 return;
4308 }
4309 if (!mSystemBooted) {
4310 return;
4311 }
Romain Guy06882f82009-06-10 13:36:04 -07004312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004313 // Don't enable the screen until all existing windows
4314 // have been drawn.
4315 final int N = mWindows.size();
4316 for (int i=0; i<N; i++) {
4317 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackbornf3bea9c2009-12-09 18:26:21 -08004318 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004319 return;
4320 }
4321 }
Romain Guy06882f82009-06-10 13:36:04 -07004322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004323 mDisplayEnabled = true;
4324 if (false) {
4325 Log.i(TAG, "ENABLING SCREEN!");
4326 StringWriter sw = new StringWriter();
4327 PrintWriter pw = new PrintWriter(sw);
4328 this.dump(null, pw, null);
4329 Log.i(TAG, sw.toString());
4330 }
4331 try {
4332 IBinder surfaceFlinger = ServiceManager.getService("SurfaceFlinger");
4333 if (surfaceFlinger != null) {
4334 //Log.i(TAG, "******* TELLING SURFACE FLINGER WE ARE BOOTED!");
4335 Parcel data = Parcel.obtain();
4336 data.writeInterfaceToken("android.ui.ISurfaceComposer");
4337 surfaceFlinger.transact(IBinder.FIRST_CALL_TRANSACTION,
4338 data, null, 0);
4339 data.recycle();
4340 }
4341 } catch (RemoteException ex) {
4342 Log.e(TAG, "Boot completed: SurfaceFlinger is dead!");
4343 }
4344 }
Romain Guy06882f82009-06-10 13:36:04 -07004345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004346 mPolicy.enableScreenAfterBoot();
Romain Guy06882f82009-06-10 13:36:04 -07004347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004348 // Make sure the last requested orientation has been applied.
Dianne Hackborn321ae682009-03-27 16:16:03 -07004349 setRotationUnchecked(WindowManagerPolicy.USE_LAST_ROTATION, false,
4350 mLastRotationFlags | Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004351 }
Romain Guy06882f82009-06-10 13:36:04 -07004352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004353 public void setInTouchMode(boolean mode) {
4354 synchronized(mWindowMap) {
4355 mInTouchMode = mode;
4356 }
4357 }
4358
Romain Guy06882f82009-06-10 13:36:04 -07004359 public void setRotation(int rotation,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004360 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004361 if (!checkCallingPermission(android.Manifest.permission.SET_ORIENTATION,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004362 "setRotation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004363 throw new SecurityException("Requires SET_ORIENTATION permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004364 }
4365
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004366 setRotationUnchecked(rotation, alwaysSendConfiguration, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004367 }
Romain Guy06882f82009-06-10 13:36:04 -07004368
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004369 public void setRotationUnchecked(int rotation,
4370 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004371 if(DEBUG_ORIENTATION) Log.v(TAG,
4372 "alwaysSendConfiguration set to "+alwaysSendConfiguration);
Romain Guy06882f82009-06-10 13:36:04 -07004373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004374 long origId = Binder.clearCallingIdentity();
4375 boolean changed;
4376 synchronized(mWindowMap) {
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004377 changed = setRotationUncheckedLocked(rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004378 }
Romain Guy06882f82009-06-10 13:36:04 -07004379
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004380 if (changed) {
4381 sendNewConfiguration();
4382 synchronized(mWindowMap) {
4383 mLayoutNeeded = true;
4384 performLayoutAndPlaceSurfacesLocked();
4385 }
4386 } else if (alwaysSendConfiguration) {
4387 //update configuration ignoring orientation change
4388 sendNewConfiguration();
4389 }
Romain Guy06882f82009-06-10 13:36:04 -07004390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004391 Binder.restoreCallingIdentity(origId);
4392 }
Romain Guy06882f82009-06-10 13:36:04 -07004393
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004394 public boolean setRotationUncheckedLocked(int rotation, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004395 boolean changed;
4396 if (rotation == WindowManagerPolicy.USE_LAST_ROTATION) {
4397 rotation = mRequestedRotation;
4398 } else {
4399 mRequestedRotation = rotation;
Dianne Hackborn321ae682009-03-27 16:16:03 -07004400 mLastRotationFlags = animFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004401 }
4402 if (DEBUG_ORIENTATION) Log.v(TAG, "Overwriting rotation value from " + rotation);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004403 rotation = mPolicy.rotationForOrientationLw(mForcedAppOrientation,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004404 mRotation, mDisplayEnabled);
4405 if (DEBUG_ORIENTATION) Log.v(TAG, "new rotation is set to " + rotation);
4406 changed = mDisplayEnabled && mRotation != rotation;
Romain Guy06882f82009-06-10 13:36:04 -07004407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004408 if (changed) {
Romain Guy06882f82009-06-10 13:36:04 -07004409 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004410 "Rotation changed to " + rotation
4411 + " from " + mRotation
4412 + " (forceApp=" + mForcedAppOrientation
4413 + ", req=" + mRequestedRotation + ")");
4414 mRotation = rotation;
4415 mWindowsFreezingScreen = true;
4416 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
4417 mH.sendMessageDelayed(mH.obtainMessage(H.WINDOW_FREEZE_TIMEOUT),
4418 2000);
4419 startFreezingDisplayLocked();
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004420 Log.i(TAG, "Setting rotation to " + rotation + ", animFlags=" + animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004421 mQueue.setOrientation(rotation);
4422 if (mDisplayEnabled) {
Dianne Hackborn321ae682009-03-27 16:16:03 -07004423 Surface.setOrientation(0, rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004424 }
4425 for (int i=mWindows.size()-1; i>=0; i--) {
4426 WindowState w = (WindowState)mWindows.get(i);
4427 if (w.mSurface != null) {
4428 w.mOrientationChanging = true;
4429 }
4430 }
4431 for (int i=mRotationWatchers.size()-1; i>=0; i--) {
4432 try {
4433 mRotationWatchers.get(i).onRotationChanged(rotation);
4434 } catch (RemoteException e) {
4435 }
4436 }
4437 } //end if changed
Romain Guy06882f82009-06-10 13:36:04 -07004438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004439 return changed;
4440 }
Romain Guy06882f82009-06-10 13:36:04 -07004441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004442 public int getRotation() {
4443 return mRotation;
4444 }
4445
4446 public int watchRotation(IRotationWatcher watcher) {
4447 final IBinder watcherBinder = watcher.asBinder();
4448 IBinder.DeathRecipient dr = new IBinder.DeathRecipient() {
4449 public void binderDied() {
4450 synchronized (mWindowMap) {
4451 for (int i=0; i<mRotationWatchers.size(); i++) {
4452 if (watcherBinder == mRotationWatchers.get(i).asBinder()) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07004453 IRotationWatcher removed = mRotationWatchers.remove(i);
4454 if (removed != null) {
4455 removed.asBinder().unlinkToDeath(this, 0);
4456 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004457 i--;
4458 }
4459 }
4460 }
4461 }
4462 };
Romain Guy06882f82009-06-10 13:36:04 -07004463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004464 synchronized (mWindowMap) {
4465 try {
4466 watcher.asBinder().linkToDeath(dr, 0);
4467 mRotationWatchers.add(watcher);
4468 } catch (RemoteException e) {
4469 // Client died, no cleanup needed.
4470 }
Romain Guy06882f82009-06-10 13:36:04 -07004471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004472 return mRotation;
4473 }
4474 }
4475
4476 /**
4477 * Starts the view server on the specified port.
4478 *
4479 * @param port The port to listener to.
4480 *
4481 * @return True if the server was successfully started, false otherwise.
4482 *
4483 * @see com.android.server.ViewServer
4484 * @see com.android.server.ViewServer#VIEW_SERVER_DEFAULT_PORT
4485 */
4486 public boolean startViewServer(int port) {
Romain Guy06882f82009-06-10 13:36:04 -07004487 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004488 return false;
4489 }
4490
4491 if (!checkCallingPermission(Manifest.permission.DUMP, "startViewServer")) {
4492 return false;
4493 }
4494
4495 if (port < 1024) {
4496 return false;
4497 }
4498
4499 if (mViewServer != null) {
4500 if (!mViewServer.isRunning()) {
4501 try {
4502 return mViewServer.start();
4503 } catch (IOException e) {
Romain Guy06882f82009-06-10 13:36:04 -07004504 Log.w(TAG, "View server did not start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004505 }
4506 }
4507 return false;
4508 }
4509
4510 try {
4511 mViewServer = new ViewServer(this, port);
4512 return mViewServer.start();
4513 } catch (IOException e) {
4514 Log.w(TAG, "View server did not start");
4515 }
4516 return false;
4517 }
4518
Romain Guy06882f82009-06-10 13:36:04 -07004519 private boolean isSystemSecure() {
4520 return "1".equals(SystemProperties.get(SYSTEM_SECURE, "1")) &&
4521 "0".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
4522 }
4523
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004524 /**
4525 * Stops the view server if it exists.
4526 *
4527 * @return True if the server stopped, false if it wasn't started or
4528 * couldn't be stopped.
4529 *
4530 * @see com.android.server.ViewServer
4531 */
4532 public boolean stopViewServer() {
Romain Guy06882f82009-06-10 13:36:04 -07004533 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004534 return false;
4535 }
4536
4537 if (!checkCallingPermission(Manifest.permission.DUMP, "stopViewServer")) {
4538 return false;
4539 }
4540
4541 if (mViewServer != null) {
4542 return mViewServer.stop();
4543 }
4544 return false;
4545 }
4546
4547 /**
4548 * Indicates whether the view server is running.
4549 *
4550 * @return True if the server is running, false otherwise.
4551 *
4552 * @see com.android.server.ViewServer
4553 */
4554 public boolean isViewServerRunning() {
Romain Guy06882f82009-06-10 13:36:04 -07004555 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004556 return false;
4557 }
4558
4559 if (!checkCallingPermission(Manifest.permission.DUMP, "isViewServerRunning")) {
4560 return false;
4561 }
4562
4563 return mViewServer != null && mViewServer.isRunning();
4564 }
4565
4566 /**
4567 * Lists all availble windows in the system. The listing is written in the
4568 * specified Socket's output stream with the following syntax:
4569 * windowHashCodeInHexadecimal windowName
4570 * Each line of the ouput represents a different window.
4571 *
4572 * @param client The remote client to send the listing to.
4573 * @return False if an error occured, true otherwise.
4574 */
4575 boolean viewServerListWindows(Socket client) {
Romain Guy06882f82009-06-10 13:36:04 -07004576 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004577 return false;
4578 }
4579
4580 boolean result = true;
4581
4582 Object[] windows;
4583 synchronized (mWindowMap) {
4584 windows = new Object[mWindows.size()];
4585 //noinspection unchecked
4586 windows = mWindows.toArray(windows);
4587 }
4588
4589 BufferedWriter out = null;
4590
4591 // Any uncaught exception will crash the system process
4592 try {
4593 OutputStream clientStream = client.getOutputStream();
4594 out = new BufferedWriter(new OutputStreamWriter(clientStream), 8 * 1024);
4595
4596 final int count = windows.length;
4597 for (int i = 0; i < count; i++) {
4598 final WindowState w = (WindowState) windows[i];
4599 out.write(Integer.toHexString(System.identityHashCode(w)));
4600 out.write(' ');
4601 out.append(w.mAttrs.getTitle());
4602 out.write('\n');
4603 }
4604
4605 out.write("DONE.\n");
4606 out.flush();
4607 } catch (Exception e) {
4608 result = false;
4609 } finally {
4610 if (out != null) {
4611 try {
4612 out.close();
4613 } catch (IOException e) {
4614 result = false;
4615 }
4616 }
4617 }
4618
4619 return result;
4620 }
4621
4622 /**
4623 * Sends a command to a target window. The result of the command, if any, will be
4624 * written in the output stream of the specified socket.
4625 *
4626 * The parameters must follow this syntax:
4627 * windowHashcode extra
4628 *
4629 * Where XX is the length in characeters of the windowTitle.
4630 *
4631 * The first parameter is the target window. The window with the specified hashcode
4632 * will be the target. If no target can be found, nothing happens. The extra parameters
4633 * will be delivered to the target window and as parameters to the command itself.
4634 *
4635 * @param client The remote client to sent the result, if any, to.
4636 * @param command The command to execute.
4637 * @param parameters The command parameters.
4638 *
4639 * @return True if the command was successfully delivered, false otherwise. This does
4640 * not indicate whether the command itself was successful.
4641 */
4642 boolean viewServerWindowCommand(Socket client, String command, String parameters) {
Romain Guy06882f82009-06-10 13:36:04 -07004643 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004644 return false;
4645 }
4646
4647 boolean success = true;
4648 Parcel data = null;
4649 Parcel reply = null;
4650
4651 // Any uncaught exception will crash the system process
4652 try {
4653 // Find the hashcode of the window
4654 int index = parameters.indexOf(' ');
4655 if (index == -1) {
4656 index = parameters.length();
4657 }
4658 final String code = parameters.substring(0, index);
Romain Guy236092a2009-12-14 15:31:48 -08004659 int hashCode = (int) Long.parseLong(code, 16);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004660
4661 // Extract the command's parameter after the window description
4662 if (index < parameters.length()) {
4663 parameters = parameters.substring(index + 1);
4664 } else {
4665 parameters = "";
4666 }
4667
4668 final WindowManagerService.WindowState window = findWindow(hashCode);
4669 if (window == null) {
4670 return false;
4671 }
4672
4673 data = Parcel.obtain();
4674 data.writeInterfaceToken("android.view.IWindow");
4675 data.writeString(command);
4676 data.writeString(parameters);
4677 data.writeInt(1);
4678 ParcelFileDescriptor.fromSocket(client).writeToParcel(data, 0);
4679
4680 reply = Parcel.obtain();
4681
4682 final IBinder binder = window.mClient.asBinder();
4683 // TODO: GET THE TRANSACTION CODE IN A SAFER MANNER
4684 binder.transact(IBinder.FIRST_CALL_TRANSACTION, data, reply, 0);
4685
4686 reply.readException();
4687
4688 } catch (Exception e) {
4689 Log.w(TAG, "Could not send command " + command + " with parameters " + parameters, e);
4690 success = false;
4691 } finally {
4692 if (data != null) {
4693 data.recycle();
4694 }
4695 if (reply != null) {
4696 reply.recycle();
4697 }
4698 }
4699
4700 return success;
4701 }
4702
4703 private WindowState findWindow(int hashCode) {
4704 if (hashCode == -1) {
4705 return getFocusedWindow();
4706 }
4707
4708 synchronized (mWindowMap) {
4709 final ArrayList windows = mWindows;
4710 final int count = windows.size();
4711
4712 for (int i = 0; i < count; i++) {
4713 WindowState w = (WindowState) windows.get(i);
4714 if (System.identityHashCode(w) == hashCode) {
4715 return w;
4716 }
4717 }
4718 }
4719
4720 return null;
4721 }
4722
4723 /*
4724 * Instruct the Activity Manager to fetch the current configuration and broadcast
4725 * that to config-changed listeners if appropriate.
4726 */
4727 void sendNewConfiguration() {
4728 try {
4729 mActivityManager.updateConfiguration(null);
4730 } catch (RemoteException e) {
4731 }
4732 }
Romain Guy06882f82009-06-10 13:36:04 -07004733
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004734 public Configuration computeNewConfiguration() {
4735 synchronized (mWindowMap) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07004736 return computeNewConfigurationLocked();
4737 }
4738 }
Romain Guy06882f82009-06-10 13:36:04 -07004739
Dianne Hackbornc485a602009-03-24 22:39:49 -07004740 Configuration computeNewConfigurationLocked() {
4741 Configuration config = new Configuration();
4742 if (!computeNewConfigurationLocked(config)) {
4743 return null;
4744 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07004745 return config;
4746 }
Romain Guy06882f82009-06-10 13:36:04 -07004747
Dianne Hackbornc485a602009-03-24 22:39:49 -07004748 boolean computeNewConfigurationLocked(Configuration config) {
4749 if (mDisplay == null) {
4750 return false;
4751 }
4752 mQueue.getInputConfiguration(config);
4753 final int dw = mDisplay.getWidth();
4754 final int dh = mDisplay.getHeight();
4755 int orientation = Configuration.ORIENTATION_SQUARE;
4756 if (dw < dh) {
4757 orientation = Configuration.ORIENTATION_PORTRAIT;
4758 } else if (dw > dh) {
4759 orientation = Configuration.ORIENTATION_LANDSCAPE;
4760 }
4761 config.orientation = orientation;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004762
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004763 DisplayMetrics dm = new DisplayMetrics();
4764 mDisplay.getMetrics(dm);
4765 CompatibilityInfo.updateCompatibleScreenFrame(dm, orientation, mCompatibleScreenFrame);
4766
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004767 if (mScreenLayout == Configuration.SCREENLAYOUT_SIZE_UNDEFINED) {
Dianne Hackborn723738c2009-06-25 19:48:04 -07004768 // Note we only do this once because at this point we don't
4769 // expect the screen to change in this way at runtime, and want
4770 // to avoid all of this computation for every config change.
Dianne Hackborn723738c2009-06-25 19:48:04 -07004771 int longSize = dw;
4772 int shortSize = dh;
4773 if (longSize < shortSize) {
4774 int tmp = longSize;
4775 longSize = shortSize;
4776 shortSize = tmp;
4777 }
4778 longSize = (int)(longSize/dm.density);
4779 shortSize = (int)(shortSize/dm.density);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004780
Dianne Hackborn723738c2009-06-25 19:48:04 -07004781 // These semi-magic numbers define our compatibility modes for
4782 // applications with different screens. Don't change unless you
4783 // make sure to test lots and lots of apps!
4784 if (longSize < 470) {
4785 // This is shorter than an HVGA normal density screen (which
4786 // is 480 pixels on its long side).
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004787 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_SMALL
4788 | Configuration.SCREENLAYOUT_LONG_NO;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004789 } else {
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004790 // Is this a large screen?
4791 if (longSize > 640 && shortSize >= 480) {
4792 // VGA or larger screens at medium density are the point
4793 // at which we consider it to be a large screen.
4794 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_LARGE;
4795 } else {
4796 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_NORMAL;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004797
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004798 // If this screen is wider than normal HVGA, or taller
4799 // than FWVGA, then for old apps we want to run in size
4800 // compatibility mode.
4801 if (shortSize > 321 || longSize > 570) {
4802 mScreenLayout |= Configuration.SCREENLAYOUT_COMPAT_NEEDED;
4803 }
4804 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004805
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004806 // Is this a long screen?
4807 if (((longSize*3)/5) >= (shortSize-1)) {
4808 // Anything wider than WVGA (5:3) is considering to be long.
4809 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_YES;
4810 } else {
4811 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_NO;
4812 }
Dianne Hackborn723738c2009-06-25 19:48:04 -07004813 }
4814 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004815 config.screenLayout = mScreenLayout;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004816
Dianne Hackbornc485a602009-03-24 22:39:49 -07004817 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
4818 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
4819 mPolicy.adjustConfigurationLw(config);
4820 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004821 }
Romain Guy06882f82009-06-10 13:36:04 -07004822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004823 // -------------------------------------------------------------
4824 // Input Events and Focus Management
4825 // -------------------------------------------------------------
4826
4827 private final void wakeupIfNeeded(WindowState targetWin, int eventType) {
Michael Chane96440f2009-05-06 10:27:36 -07004828 long curTime = SystemClock.uptimeMillis();
4829
Michael Chane10de972009-05-18 11:24:50 -07004830 if (eventType == TOUCH_EVENT || eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT) {
Michael Chane96440f2009-05-06 10:27:36 -07004831 if (mLastTouchEventType == eventType &&
4832 (curTime - mLastUserActivityCallTime) < MIN_TIME_BETWEEN_USERACTIVITIES) {
4833 return;
4834 }
4835 mLastUserActivityCallTime = curTime;
4836 mLastTouchEventType = eventType;
4837 }
4838
4839 if (targetWin == null
4840 || targetWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
4841 mPowerManager.userActivity(curTime, false, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004842 }
4843 }
4844
4845 // tells if it's a cheek event or not -- this function is stateful
4846 private static final int EVENT_NONE = 0;
4847 private static final int EVENT_UNKNOWN = 0;
4848 private static final int EVENT_CHEEK = 0;
4849 private static final int EVENT_IGNORE_DURATION = 300; // ms
4850 private static final float CHEEK_THRESHOLD = 0.6f;
4851 private int mEventState = EVENT_NONE;
4852 private float mEventSize;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004853
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004854 private int eventType(MotionEvent ev) {
4855 float size = ev.getSize();
4856 switch (ev.getAction()) {
4857 case MotionEvent.ACTION_DOWN:
4858 mEventSize = size;
4859 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_EVENT;
4860 case MotionEvent.ACTION_UP:
4861 if (size > mEventSize) mEventSize = size;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004862 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004863 case MotionEvent.ACTION_MOVE:
4864 final int N = ev.getHistorySize();
4865 if (size > mEventSize) mEventSize = size;
4866 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4867 for (int i=0; i<N; i++) {
4868 size = ev.getHistoricalSize(i);
4869 if (size > mEventSize) mEventSize = size;
4870 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4871 }
4872 if (ev.getEventTime() < ev.getDownTime() + EVENT_IGNORE_DURATION) {
4873 return TOUCH_EVENT;
4874 } else {
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004875 return LONG_TOUCH_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004876 }
4877 default:
4878 // not good
4879 return OTHER_EVENT;
4880 }
4881 }
4882
4883 /**
4884 * @return Returns true if event was dispatched, false if it was dropped for any reason
4885 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004886 private int dispatchPointer(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004887 if (DEBUG_INPUT || WindowManagerPolicy.WATCH_POINTER) Log.v(TAG,
4888 "dispatchPointer " + ev);
4889
Michael Chan53071d62009-05-13 17:29:48 -07004890 if (MEASURE_LATENCY) {
4891 lt.sample("3 Wait for last dispatch ", System.nanoTime() - qev.whenNano);
4892 }
4893
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004894 Object targetObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004895 ev, true, false, pid, uid);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004896
Michael Chan53071d62009-05-13 17:29:48 -07004897 if (MEASURE_LATENCY) {
4898 lt.sample("3 Last dispatch finished ", System.nanoTime() - qev.whenNano);
4899 }
4900
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004901 int action = ev.getAction();
Romain Guy06882f82009-06-10 13:36:04 -07004902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004903 if (action == MotionEvent.ACTION_UP) {
4904 // let go of our target
4905 mKeyWaiter.mMotionTarget = null;
4906 mPowerManager.logPointerUpEvent();
4907 } else if (action == MotionEvent.ACTION_DOWN) {
4908 mPowerManager.logPointerDownEvent();
4909 }
4910
4911 if (targetObj == null) {
4912 // In this case we are either dropping the event, or have received
4913 // a move or up without a down. It is common to receive move
4914 // events in such a way, since this means the user is moving the
4915 // pointer without actually pressing down. All other cases should
4916 // be atypical, so let's log them.
Michael Chane96440f2009-05-06 10:27:36 -07004917 if (action != MotionEvent.ACTION_MOVE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004918 Log.w(TAG, "No window to dispatch pointer action " + ev.getAction());
4919 }
Dianne Hackborn6adba242009-11-10 11:10:09 -08004920 synchronized (mWindowMap) {
4921 if (mSendingPointersToWallpaper) {
4922 Log.i(TAG, "Sending skipped pointer to wallpaper!");
4923 sendPointerToWallpaperLocked(null, ev, ev.getEventTime());
4924 }
4925 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004926 if (qev != null) {
4927 mQueue.recycleEvent(qev);
4928 }
4929 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004930 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004931 }
4932 if (targetObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborn6adba242009-11-10 11:10:09 -08004933 synchronized (mWindowMap) {
4934 if (mSendingPointersToWallpaper) {
4935 Log.i(TAG, "Sending skipped pointer to wallpaper!");
4936 sendPointerToWallpaperLocked(null, ev, ev.getEventTime());
4937 }
4938 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004939 if (qev != null) {
4940 mQueue.recycleEvent(qev);
4941 }
4942 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004943 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004944 }
Romain Guy06882f82009-06-10 13:36:04 -07004945
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004946 WindowState target = (WindowState)targetObj;
Romain Guy06882f82009-06-10 13:36:04 -07004947
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004948 final long eventTime = ev.getEventTime();
Michael Chan53071d62009-05-13 17:29:48 -07004949 final long eventTimeNano = ev.getEventTimeNano();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004950
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004951 //Log.i(TAG, "Sending " + ev + " to " + target);
4952
4953 if (uid != 0 && uid != target.mSession.mUid) {
4954 if (mContext.checkPermission(
4955 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4956 != PackageManager.PERMISSION_GRANTED) {
4957 Log.w(TAG, "Permission denied: injecting pointer event from pid "
4958 + pid + " uid " + uid + " to window " + target
4959 + " owned by uid " + target.mSession.mUid);
4960 if (qev != null) {
4961 mQueue.recycleEvent(qev);
4962 }
4963 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004964 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004965 }
4966 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004967
Michael Chan53071d62009-05-13 17:29:48 -07004968 if (MEASURE_LATENCY) {
4969 lt.sample("4 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4970 }
4971
Romain Guy06882f82009-06-10 13:36:04 -07004972 if ((target.mAttrs.flags &
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004973 WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES) != 0) {
4974 //target wants to ignore fat touch events
4975 boolean cheekPress = mPolicy.isCheekPressedAgainstScreen(ev);
4976 //explicit flag to return without processing event further
4977 boolean returnFlag = false;
4978 if((action == MotionEvent.ACTION_DOWN)) {
4979 mFatTouch = false;
4980 if(cheekPress) {
4981 mFatTouch = true;
4982 returnFlag = true;
4983 }
4984 } else {
4985 if(action == MotionEvent.ACTION_UP) {
4986 if(mFatTouch) {
4987 //earlier even was invalid doesnt matter if current up is cheekpress or not
4988 mFatTouch = false;
4989 returnFlag = true;
4990 } else if(cheekPress) {
4991 //cancel the earlier event
4992 ev.setAction(MotionEvent.ACTION_CANCEL);
4993 action = MotionEvent.ACTION_CANCEL;
4994 }
4995 } else if(action == MotionEvent.ACTION_MOVE) {
4996 if(mFatTouch) {
4997 //two cases here
4998 //an invalid down followed by 0 or moves(valid or invalid)
Romain Guy06882f82009-06-10 13:36:04 -07004999 //a valid down, invalid move, more moves. want to ignore till up
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005000 returnFlag = true;
5001 } else if(cheekPress) {
5002 //valid down followed by invalid moves
5003 //an invalid move have to cancel earlier action
5004 ev.setAction(MotionEvent.ACTION_CANCEL);
5005 action = MotionEvent.ACTION_CANCEL;
5006 if (DEBUG_INPUT) Log.v(TAG, "Sending cancel for invalid ACTION_MOVE");
5007 //note that the subsequent invalid moves will not get here
5008 mFatTouch = true;
5009 }
5010 }
5011 } //else if action
5012 if(returnFlag) {
5013 //recycle que, ev
5014 if (qev != null) {
5015 mQueue.recycleEvent(qev);
5016 }
5017 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005018 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005019 }
5020 } //end if target
Michael Chane96440f2009-05-06 10:27:36 -07005021
Michael Chan9f028e62009-08-04 17:37:46 -07005022 // Enable this for testing the "right" value
5023 if (false && action == MotionEvent.ACTION_DOWN) {
Michael Chane96440f2009-05-06 10:27:36 -07005024 int max_events_per_sec = 35;
5025 try {
5026 max_events_per_sec = Integer.parseInt(SystemProperties
5027 .get("windowsmgr.max_events_per_sec"));
5028 if (max_events_per_sec < 1) {
5029 max_events_per_sec = 35;
5030 }
5031 } catch (NumberFormatException e) {
5032 }
5033 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
5034 }
5035
5036 /*
5037 * Throttle events to minimize CPU usage when there's a flood of events
5038 * e.g. constant contact with the screen
5039 */
5040 if (action == MotionEvent.ACTION_MOVE) {
5041 long nextEventTime = mLastTouchEventTime + mMinWaitTimeBetweenTouchEvents;
5042 long now = SystemClock.uptimeMillis();
5043 if (now < nextEventTime) {
5044 try {
5045 Thread.sleep(nextEventTime - now);
5046 } catch (InterruptedException e) {
5047 }
5048 mLastTouchEventTime = nextEventTime;
5049 } else {
5050 mLastTouchEventTime = now;
5051 }
5052 }
5053
Michael Chan53071d62009-05-13 17:29:48 -07005054 if (MEASURE_LATENCY) {
5055 lt.sample("5 in dispatchPointer ", System.nanoTime() - eventTimeNano);
5056 }
5057
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005058 synchronized(mWindowMap) {
Dianne Hackborn6adba242009-11-10 11:10:09 -08005059 if (!target.isVisibleLw()) {
5060 // During this motion dispatch, the target window has become
5061 // invisible.
5062 if (mSendingPointersToWallpaper) {
5063 sendPointerToWallpaperLocked(null, ev, eventTime);
5064 }
5065 if (qev != null) {
5066 mQueue.recycleEvent(qev);
5067 }
5068 ev.recycle();
5069 return INJECT_SUCCEEDED;
5070 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005072 if (qev != null && action == MotionEvent.ACTION_MOVE) {
5073 mKeyWaiter.bindTargetWindowLocked(target,
5074 KeyWaiter.RETURN_PENDING_POINTER, qev);
5075 ev = null;
5076 } else {
5077 if (action == MotionEvent.ACTION_DOWN) {
5078 WindowState out = mKeyWaiter.mOutsideTouchTargets;
5079 if (out != null) {
5080 MotionEvent oev = MotionEvent.obtain(ev);
5081 oev.setAction(MotionEvent.ACTION_OUTSIDE);
5082 do {
5083 final Rect frame = out.mFrame;
5084 oev.offsetLocation(-(float)frame.left, -(float)frame.top);
5085 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005086 out.mClient.dispatchPointer(oev, eventTime, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005087 } catch (android.os.RemoteException e) {
5088 Log.i(TAG, "WINDOW DIED during outside motion dispatch: " + out);
5089 }
5090 oev.offsetLocation((float)frame.left, (float)frame.top);
5091 out = out.mNextOutsideTouch;
5092 } while (out != null);
5093 mKeyWaiter.mOutsideTouchTargets = null;
5094 }
5095 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005096
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005097 // If we are on top of the wallpaper, then the wallpaper also
5098 // gets to see this movement.
Dianne Hackborn9b52a212009-12-11 14:51:35 -08005099 if ((mWallpaperTarget == target &&
5100 target.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD)
5101 || mSendingPointersToWallpaper) {
Dianne Hackborn6adba242009-11-10 11:10:09 -08005102 sendPointerToWallpaperLocked(null, ev, eventTime);
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005103 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005104
Dianne Hackborn6adba242009-11-10 11:10:09 -08005105 final Rect frame = target.mFrame;
5106 ev.offsetLocation(-(float)frame.left, -(float)frame.top);
5107 mKeyWaiter.bindTargetWindowLocked(target);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005108 }
5109 }
Romain Guy06882f82009-06-10 13:36:04 -07005110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005111 // finally offset the event to the target's coordinate system and
5112 // dispatch the event.
5113 try {
5114 if (DEBUG_INPUT || DEBUG_FOCUS || WindowManagerPolicy.WATCH_POINTER) {
5115 Log.v(TAG, "Delivering pointer " + qev + " to " + target);
5116 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005117
Michael Chan53071d62009-05-13 17:29:48 -07005118 if (MEASURE_LATENCY) {
5119 lt.sample("6 before svr->client ipc ", System.nanoTime() - eventTimeNano);
5120 }
5121
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005122 target.mClient.dispatchPointer(ev, eventTime, true);
Michael Chan53071d62009-05-13 17:29:48 -07005123
5124 if (MEASURE_LATENCY) {
5125 lt.sample("7 after svr->client ipc ", System.nanoTime() - eventTimeNano);
5126 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07005127 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005128 } catch (android.os.RemoteException e) {
5129 Log.i(TAG, "WINDOW DIED during motion dispatch: " + target);
5130 mKeyWaiter.mMotionTarget = null;
5131 try {
5132 removeWindow(target.mSession, target.mClient);
5133 } catch (java.util.NoSuchElementException ex) {
5134 // This will happen if the window has already been
5135 // removed.
5136 }
5137 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07005138 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005139 }
Romain Guy06882f82009-06-10 13:36:04 -07005140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005141 /**
5142 * @return Returns true if event was dispatched, false if it was dropped for any reason
5143 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07005144 private int dispatchTrackball(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005145 if (DEBUG_INPUT) Log.v(
5146 TAG, "dispatchTrackball [" + ev.getAction() +"] <" + ev.getX() + ", " + ev.getY() + ">");
Romain Guy06882f82009-06-10 13:36:04 -07005147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005148 Object focusObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005149 ev, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005150 if (focusObj == null) {
5151 Log.w(TAG, "No focus window, dropping trackball: " + ev);
5152 if (qev != null) {
5153 mQueue.recycleEvent(qev);
5154 }
5155 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005156 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005157 }
5158 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
5159 if (qev != null) {
5160 mQueue.recycleEvent(qev);
5161 }
5162 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005163 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005164 }
Romain Guy06882f82009-06-10 13:36:04 -07005165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005166 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07005167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005168 if (uid != 0 && uid != focus.mSession.mUid) {
5169 if (mContext.checkPermission(
5170 android.Manifest.permission.INJECT_EVENTS, pid, uid)
5171 != PackageManager.PERMISSION_GRANTED) {
5172 Log.w(TAG, "Permission denied: injecting key event from pid "
5173 + pid + " uid " + uid + " to window " + focus
5174 + " owned by uid " + focus.mSession.mUid);
5175 if (qev != null) {
5176 mQueue.recycleEvent(qev);
5177 }
5178 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005179 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005180 }
5181 }
Romain Guy06882f82009-06-10 13:36:04 -07005182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005183 final long eventTime = ev.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07005184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005185 synchronized(mWindowMap) {
5186 if (qev != null && ev.getAction() == MotionEvent.ACTION_MOVE) {
5187 mKeyWaiter.bindTargetWindowLocked(focus,
5188 KeyWaiter.RETURN_PENDING_TRACKBALL, qev);
5189 // We don't deliver movement events to the client, we hold
5190 // them and wait for them to call back.
5191 ev = null;
5192 } else {
5193 mKeyWaiter.bindTargetWindowLocked(focus);
5194 }
5195 }
Romain Guy06882f82009-06-10 13:36:04 -07005196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005197 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005198 focus.mClient.dispatchTrackball(ev, eventTime, true);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005199 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005200 } catch (android.os.RemoteException e) {
5201 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
5202 try {
5203 removeWindow(focus.mSession, focus.mClient);
5204 } catch (java.util.NoSuchElementException ex) {
5205 // This will happen if the window has already been
5206 // removed.
5207 }
5208 }
Romain Guy06882f82009-06-10 13:36:04 -07005209
Dianne Hackborncfaef692009-06-15 14:24:44 -07005210 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005211 }
Romain Guy06882f82009-06-10 13:36:04 -07005212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005213 /**
5214 * @return Returns true if event was dispatched, false if it was dropped for any reason
5215 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07005216 private int dispatchKey(KeyEvent event, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005217 if (DEBUG_INPUT) Log.v(TAG, "Dispatch key: " + event);
5218
5219 Object focusObj = mKeyWaiter.waitForNextEventTarget(event, null,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005220 null, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005221 if (focusObj == null) {
5222 Log.w(TAG, "No focus window, dropping: " + event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005223 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005224 }
5225 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005226 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005227 }
Romain Guy06882f82009-06-10 13:36:04 -07005228
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005229 // Okay we have finished waiting for the last event to be processed.
5230 // First off, if this is a repeat event, check to see if there is
5231 // a corresponding up event in the queue. If there is, we will
5232 // just drop the repeat, because it makes no sense to repeat after
5233 // the user has released a key. (This is especially important for
5234 // long presses.)
5235 if (event.getRepeatCount() > 0 && mQueue.hasKeyUpEvent(event)) {
5236 return INJECT_SUCCEEDED;
5237 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005239 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07005240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005241 if (DEBUG_INPUT) Log.v(
5242 TAG, "Dispatching to " + focus + ": " + event);
5243
5244 if (uid != 0 && uid != focus.mSession.mUid) {
5245 if (mContext.checkPermission(
5246 android.Manifest.permission.INJECT_EVENTS, pid, uid)
5247 != PackageManager.PERMISSION_GRANTED) {
5248 Log.w(TAG, "Permission denied: injecting key event from pid "
5249 + pid + " uid " + uid + " to window " + focus
5250 + " owned by uid " + focus.mSession.mUid);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005251 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005252 }
5253 }
Romain Guy06882f82009-06-10 13:36:04 -07005254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005255 synchronized(mWindowMap) {
5256 mKeyWaiter.bindTargetWindowLocked(focus);
5257 }
5258
5259 // NOSHIP extra state logging
5260 mKeyWaiter.recordDispatchState(event, focus);
5261 // END NOSHIP
Romain Guy06882f82009-06-10 13:36:04 -07005262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005263 try {
5264 if (DEBUG_INPUT || DEBUG_FOCUS) {
5265 Log.v(TAG, "Delivering key " + event.getKeyCode()
5266 + " to " + focus);
5267 }
5268 focus.mClient.dispatchKey(event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005269 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005270 } catch (android.os.RemoteException e) {
5271 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
5272 try {
5273 removeWindow(focus.mSession, focus.mClient);
5274 } catch (java.util.NoSuchElementException ex) {
5275 // This will happen if the window has already been
5276 // removed.
5277 }
5278 }
Romain Guy06882f82009-06-10 13:36:04 -07005279
Dianne Hackborncfaef692009-06-15 14:24:44 -07005280 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005281 }
Romain Guy06882f82009-06-10 13:36:04 -07005282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005283 public void pauseKeyDispatching(IBinder _token) {
5284 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5285 "pauseKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005286 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005287 }
5288
5289 synchronized (mWindowMap) {
5290 WindowToken token = mTokenMap.get(_token);
5291 if (token != null) {
5292 mKeyWaiter.pauseDispatchingLocked(token);
5293 }
5294 }
5295 }
5296
5297 public void resumeKeyDispatching(IBinder _token) {
5298 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5299 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005300 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005301 }
5302
5303 synchronized (mWindowMap) {
5304 WindowToken token = mTokenMap.get(_token);
5305 if (token != null) {
5306 mKeyWaiter.resumeDispatchingLocked(token);
5307 }
5308 }
5309 }
5310
5311 public void setEventDispatching(boolean enabled) {
5312 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5313 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005314 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005315 }
5316
5317 synchronized (mWindowMap) {
5318 mKeyWaiter.setEventDispatchingLocked(enabled);
5319 }
5320 }
Romain Guy06882f82009-06-10 13:36:04 -07005321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005322 /**
5323 * Injects a keystroke event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005324 *
5325 * @param ev A motion event describing the keystroke action. (Be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005326 * {@link SystemClock#uptimeMillis()} as the timebase.)
5327 * @param sync If true, wait for the event to be completed before returning to the caller.
5328 * @return Returns true if event was dispatched, false if it was dropped for any reason
5329 */
5330 public boolean injectKeyEvent(KeyEvent ev, boolean sync) {
5331 long downTime = ev.getDownTime();
5332 long eventTime = ev.getEventTime();
5333
5334 int action = ev.getAction();
5335 int code = ev.getKeyCode();
5336 int repeatCount = ev.getRepeatCount();
5337 int metaState = ev.getMetaState();
5338 int deviceId = ev.getDeviceId();
5339 int scancode = ev.getScanCode();
5340
5341 if (eventTime == 0) eventTime = SystemClock.uptimeMillis();
5342 if (downTime == 0) downTime = eventTime;
5343
5344 KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
The Android Open Source Project10592532009-03-18 17:39:46 -07005345 deviceId, scancode, KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005346
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005347 final int pid = Binder.getCallingPid();
5348 final int uid = Binder.getCallingUid();
5349 final long ident = Binder.clearCallingIdentity();
5350 final int result = dispatchKey(newEvent, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005351 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005352 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005353 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005354 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005355 switch (result) {
5356 case INJECT_NO_PERMISSION:
5357 throw new SecurityException(
Chander S Pechetty27f3de62010-02-10 22:14:00 +05305358 "Injecting to another application requires INJECT_EVENTS permission");
Dianne Hackborncfaef692009-06-15 14:24:44 -07005359 case INJECT_SUCCEEDED:
5360 return true;
5361 }
5362 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005363 }
5364
5365 /**
5366 * Inject a pointer (touch) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005367 *
5368 * @param ev A motion event describing the pointer (touch) action. (As noted in
5369 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005370 * {@link SystemClock#uptimeMillis()} as the timebase.)
5371 * @param sync If true, wait for the event to be completed before returning to the caller.
5372 * @return Returns true if event was dispatched, false if it was dropped for any reason
5373 */
5374 public boolean injectPointerEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005375 final int pid = Binder.getCallingPid();
5376 final int uid = Binder.getCallingUid();
5377 final long ident = Binder.clearCallingIdentity();
5378 final int result = dispatchPointer(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005379 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005380 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005381 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005382 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005383 switch (result) {
5384 case INJECT_NO_PERMISSION:
5385 throw new SecurityException(
Chander S Pechetty27f3de62010-02-10 22:14:00 +05305386 "Injecting to another application requires INJECT_EVENTS permission");
Dianne Hackborncfaef692009-06-15 14:24:44 -07005387 case INJECT_SUCCEEDED:
5388 return true;
5389 }
5390 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005391 }
Romain Guy06882f82009-06-10 13:36:04 -07005392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005393 /**
5394 * Inject a trackball (navigation device) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005395 *
5396 * @param ev A motion event describing the trackball action. (As noted in
5397 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005398 * {@link SystemClock#uptimeMillis()} as the timebase.)
5399 * @param sync If true, wait for the event to be completed before returning to the caller.
5400 * @return Returns true if event was dispatched, false if it was dropped for any reason
5401 */
5402 public boolean injectTrackballEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005403 final int pid = Binder.getCallingPid();
5404 final int uid = Binder.getCallingUid();
5405 final long ident = Binder.clearCallingIdentity();
5406 final int result = dispatchTrackball(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005407 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005408 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005409 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005410 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005411 switch (result) {
5412 case INJECT_NO_PERMISSION:
5413 throw new SecurityException(
Chander S Pechetty27f3de62010-02-10 22:14:00 +05305414 "Injecting to another application requires INJECT_EVENTS permission");
Dianne Hackborncfaef692009-06-15 14:24:44 -07005415 case INJECT_SUCCEEDED:
5416 return true;
5417 }
5418 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005419 }
Romain Guy06882f82009-06-10 13:36:04 -07005420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005421 private WindowState getFocusedWindow() {
5422 synchronized (mWindowMap) {
5423 return getFocusedWindowLocked();
5424 }
5425 }
5426
5427 private WindowState getFocusedWindowLocked() {
5428 return mCurrentFocus;
5429 }
Romain Guy06882f82009-06-10 13:36:04 -07005430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005431 /**
5432 * This class holds the state for dispatching key events. This state
5433 * is protected by the KeyWaiter instance, NOT by the window lock. You
5434 * can be holding the main window lock while acquire the KeyWaiter lock,
5435 * but not the other way around.
5436 */
5437 final class KeyWaiter {
5438 // NOSHIP debugging
5439 public class DispatchState {
5440 private KeyEvent event;
5441 private WindowState focus;
5442 private long time;
5443 private WindowState lastWin;
5444 private IBinder lastBinder;
5445 private boolean finished;
5446 private boolean gotFirstWindow;
5447 private boolean eventDispatching;
5448 private long timeToSwitch;
5449 private boolean wasFrozen;
5450 private boolean focusPaused;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005451 private WindowState curFocus;
Romain Guy06882f82009-06-10 13:36:04 -07005452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005453 DispatchState(KeyEvent theEvent, WindowState theFocus) {
5454 focus = theFocus;
5455 event = theEvent;
5456 time = System.currentTimeMillis();
5457 // snapshot KeyWaiter state
5458 lastWin = mLastWin;
5459 lastBinder = mLastBinder;
5460 finished = mFinished;
5461 gotFirstWindow = mGotFirstWindow;
5462 eventDispatching = mEventDispatching;
5463 timeToSwitch = mTimeToSwitch;
5464 wasFrozen = mWasFrozen;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005465 curFocus = mCurrentFocus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005466 // cache the paused state at ctor time as well
5467 if (theFocus == null || theFocus.mToken == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005468 focusPaused = false;
5469 } else {
5470 focusPaused = theFocus.mToken.paused;
5471 }
5472 }
Romain Guy06882f82009-06-10 13:36:04 -07005473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005474 public String toString() {
5475 return "{{" + event + " to " + focus + " @ " + time
5476 + " lw=" + lastWin + " lb=" + lastBinder
5477 + " fin=" + finished + " gfw=" + gotFirstWindow
5478 + " ed=" + eventDispatching + " tts=" + timeToSwitch
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005479 + " wf=" + wasFrozen + " fp=" + focusPaused
Christopher Tate46d45252010-02-09 15:48:57 -08005480 + " mcf=" + curFocus + "}}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005481 }
5482 };
5483 private DispatchState mDispatchState = null;
5484 public void recordDispatchState(KeyEvent theEvent, WindowState theFocus) {
5485 mDispatchState = new DispatchState(theEvent, theFocus);
5486 }
5487 // END NOSHIP
5488
5489 public static final int RETURN_NOTHING = 0;
5490 public static final int RETURN_PENDING_POINTER = 1;
5491 public static final int RETURN_PENDING_TRACKBALL = 2;
Romain Guy06882f82009-06-10 13:36:04 -07005492
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005493 final Object SKIP_TARGET_TOKEN = new Object();
5494 final Object CONSUMED_EVENT_TOKEN = new Object();
Romain Guy06882f82009-06-10 13:36:04 -07005495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005496 private WindowState mLastWin = null;
5497 private IBinder mLastBinder = null;
5498 private boolean mFinished = true;
5499 private boolean mGotFirstWindow = false;
5500 private boolean mEventDispatching = true;
5501 private long mTimeToSwitch = 0;
5502 /* package */ boolean mWasFrozen = false;
Romain Guy06882f82009-06-10 13:36:04 -07005503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005504 // Target of Motion events
5505 WindowState mMotionTarget;
Romain Guy06882f82009-06-10 13:36:04 -07005506
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005507 // Windows above the target who would like to receive an "outside"
5508 // touch event for any down events outside of them.
5509 WindowState mOutsideTouchTargets;
5510
5511 /**
5512 * Wait for the last event dispatch to complete, then find the next
5513 * target that should receive the given event and wait for that one
5514 * to be ready to receive it.
5515 */
5516 Object waitForNextEventTarget(KeyEvent nextKey, QueuedEvent qev,
5517 MotionEvent nextMotion, boolean isPointerEvent,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005518 boolean failIfTimeout, int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005519 long startTime = SystemClock.uptimeMillis();
5520 long keyDispatchingTimeout = 5 * 1000;
5521 long waitedFor = 0;
5522
5523 while (true) {
5524 // Figure out which window we care about. It is either the
5525 // last window we are waiting to have process the event or,
5526 // if none, then the next window we think the event should go
5527 // to. Note: we retrieve mLastWin outside of the lock, so
5528 // it may change before we lock. Thus we must check it again.
5529 WindowState targetWin = mLastWin;
5530 boolean targetIsNew = targetWin == null;
5531 if (DEBUG_INPUT) Log.v(
5532 TAG, "waitForLastKey: mFinished=" + mFinished +
5533 ", mLastWin=" + mLastWin);
5534 if (targetIsNew) {
5535 Object target = findTargetWindow(nextKey, qev, nextMotion,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005536 isPointerEvent, callingPid, callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005537 if (target == SKIP_TARGET_TOKEN) {
5538 // The user has pressed a special key, and we are
5539 // dropping all pending events before it.
5540 if (DEBUG_INPUT) Log.v(TAG, "Skipping: " + nextKey
5541 + " " + nextMotion);
5542 return null;
5543 }
5544 if (target == CONSUMED_EVENT_TOKEN) {
5545 if (DEBUG_INPUT) Log.v(TAG, "Consumed: " + nextKey
5546 + " " + nextMotion);
5547 return target;
5548 }
5549 targetWin = (WindowState)target;
5550 }
Romain Guy06882f82009-06-10 13:36:04 -07005551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005552 AppWindowToken targetApp = null;
Romain Guy06882f82009-06-10 13:36:04 -07005553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005554 // Now: is it okay to send the next event to this window?
5555 synchronized (this) {
5556 // First: did we come here based on the last window not
5557 // being null, but it changed by the time we got here?
5558 // If so, try again.
5559 if (!targetIsNew && mLastWin == null) {
5560 continue;
5561 }
Romain Guy06882f82009-06-10 13:36:04 -07005562
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005563 // We never dispatch events if not finished with the
5564 // last one, or the display is frozen.
5565 if (mFinished && !mDisplayFrozen) {
5566 // If event dispatching is disabled, then we
5567 // just consume the events.
5568 if (!mEventDispatching) {
5569 if (DEBUG_INPUT) Log.v(TAG,
5570 "Skipping event; dispatching disabled: "
5571 + nextKey + " " + nextMotion);
5572 return null;
5573 }
5574 if (targetWin != null) {
5575 // If this is a new target, and that target is not
5576 // paused or unresponsive, then all looks good to
5577 // handle the event.
5578 if (targetIsNew && !targetWin.mToken.paused) {
5579 return targetWin;
5580 }
Romain Guy06882f82009-06-10 13:36:04 -07005581
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005582 // If we didn't find a target window, and there is no
5583 // focused app window, then just eat the events.
5584 } else if (mFocusedApp == null) {
5585 if (DEBUG_INPUT) Log.v(TAG,
5586 "Skipping event; no focused app: "
5587 + nextKey + " " + nextMotion);
5588 return null;
5589 }
5590 }
Romain Guy06882f82009-06-10 13:36:04 -07005591
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005592 if (DEBUG_INPUT) Log.v(
5593 TAG, "Waiting for last key in " + mLastBinder
5594 + " target=" + targetWin
5595 + " mFinished=" + mFinished
5596 + " mDisplayFrozen=" + mDisplayFrozen
5597 + " targetIsNew=" + targetIsNew
5598 + " paused="
5599 + (targetWin != null ? targetWin.mToken.paused : false)
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005600 + " mFocusedApp=" + mFocusedApp
5601 + " mCurrentFocus=" + mCurrentFocus);
Romain Guy06882f82009-06-10 13:36:04 -07005602
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005603 targetApp = targetWin != null
5604 ? targetWin.mAppToken : mFocusedApp;
Romain Guy06882f82009-06-10 13:36:04 -07005605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005606 long curTimeout = keyDispatchingTimeout;
5607 if (mTimeToSwitch != 0) {
5608 long now = SystemClock.uptimeMillis();
5609 if (mTimeToSwitch <= now) {
5610 // If an app switch key has been pressed, and we have
5611 // waited too long for the current app to finish
5612 // processing keys, then wait no more!
Christopher Tate136b1f92010-02-11 17:51:24 -08005613 doFinishedKeyLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005614 continue;
5615 }
5616 long switchTimeout = mTimeToSwitch - now;
5617 if (curTimeout > switchTimeout) {
5618 curTimeout = switchTimeout;
5619 }
5620 }
Romain Guy06882f82009-06-10 13:36:04 -07005621
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005622 try {
5623 // after that continue
5624 // processing keys, so we don't get stuck.
5625 if (DEBUG_INPUT) Log.v(
5626 TAG, "Waiting for key dispatch: " + curTimeout);
5627 wait(curTimeout);
5628 if (DEBUG_INPUT) Log.v(TAG, "Finished waiting @"
5629 + SystemClock.uptimeMillis() + " startTime="
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005630 + startTime + " switchTime=" + mTimeToSwitch
5631 + " target=" + targetWin + " mLW=" + mLastWin
5632 + " mLB=" + mLastBinder + " fin=" + mFinished
5633 + " mCurrentFocus=" + mCurrentFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005634 } catch (InterruptedException e) {
5635 }
5636 }
5637
5638 // If we were frozen during configuration change, restart the
5639 // timeout checks from now; otherwise look at whether we timed
5640 // out before awakening.
5641 if (mWasFrozen) {
5642 waitedFor = 0;
5643 mWasFrozen = false;
5644 } else {
5645 waitedFor = SystemClock.uptimeMillis() - startTime;
5646 }
5647
5648 if (waitedFor >= keyDispatchingTimeout && mTimeToSwitch == 0) {
5649 IApplicationToken at = null;
5650 synchronized (this) {
5651 Log.w(TAG, "Key dispatching timed out sending to " +
5652 (targetWin != null ? targetWin.mAttrs.getTitle()
Ken Shirriff8200b202010-02-04 13:34:37 -08005653 : "<null>: no window ready for key dispatch"));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005654 // NOSHIP debugging
Ken Shirriff8200b202010-02-04 13:34:37 -08005655 Log.w(TAG, "Previous dispatch state: " + mDispatchState);
5656 Log.w(TAG, "Current dispatch state: " +
5657 new DispatchState(nextKey, targetWin));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005658 // END NOSHIP
5659 //dump();
5660 if (targetWin != null) {
5661 at = targetWin.getAppToken();
5662 } else if (targetApp != null) {
5663 at = targetApp.appToken;
5664 }
5665 }
5666
5667 boolean abort = true;
5668 if (at != null) {
5669 try {
5670 long timeout = at.getKeyDispatchingTimeout();
5671 if (timeout > waitedFor) {
5672 // we did not wait the proper amount of time for this application.
5673 // set the timeout to be the real timeout and wait again.
5674 keyDispatchingTimeout = timeout - waitedFor;
5675 continue;
5676 } else {
5677 abort = at.keyDispatchingTimedOut();
5678 }
5679 } catch (RemoteException ex) {
5680 }
5681 }
5682
5683 synchronized (this) {
5684 if (abort && (mLastWin == targetWin || targetWin == null)) {
5685 mFinished = true;
Romain Guy06882f82009-06-10 13:36:04 -07005686 if (mLastWin != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005687 if (DEBUG_INPUT) Log.v(TAG,
5688 "Window " + mLastWin +
5689 " timed out on key input");
5690 if (mLastWin.mToken.paused) {
5691 Log.w(TAG, "Un-pausing dispatching to this window");
5692 mLastWin.mToken.paused = false;
5693 }
5694 }
5695 if (mMotionTarget == targetWin) {
5696 mMotionTarget = null;
5697 }
5698 mLastWin = null;
5699 mLastBinder = null;
5700 if (failIfTimeout || targetWin == null) {
5701 return null;
5702 }
5703 } else {
5704 Log.w(TAG, "Continuing to wait for key to be dispatched");
5705 startTime = SystemClock.uptimeMillis();
5706 }
5707 }
5708 }
5709 }
5710 }
Romain Guy06882f82009-06-10 13:36:04 -07005711
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005712 Object findTargetWindow(KeyEvent nextKey, QueuedEvent qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005713 MotionEvent nextMotion, boolean isPointerEvent,
5714 int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005715 mOutsideTouchTargets = null;
Romain Guy06882f82009-06-10 13:36:04 -07005716
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005717 if (nextKey != null) {
5718 // Find the target window for a normal key event.
5719 final int keycode = nextKey.getKeyCode();
5720 final int repeatCount = nextKey.getRepeatCount();
5721 final boolean down = nextKey.getAction() != KeyEvent.ACTION_UP;
5722 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(keycode);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005723
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005724 if (!dispatch) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005725 if (callingUid == 0 ||
5726 mContext.checkPermission(
5727 android.Manifest.permission.INJECT_EVENTS,
5728 callingPid, callingUid)
5729 == PackageManager.PERMISSION_GRANTED) {
5730 mPolicy.interceptKeyTi(null, keycode,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005731 nextKey.getMetaState(), down, repeatCount,
5732 nextKey.getFlags());
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005733 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005734 Log.w(TAG, "Event timeout during app switch: dropping "
5735 + nextKey);
5736 return SKIP_TARGET_TOKEN;
5737 }
Romain Guy06882f82009-06-10 13:36:04 -07005738
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005739 // System.out.println("##### [" + SystemClock.uptimeMillis() + "] WindowManagerService.dispatchKey(" + keycode + ", " + down + ", " + repeatCount + ")");
Romain Guy06882f82009-06-10 13:36:04 -07005740
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005741 WindowState focus = null;
5742 synchronized(mWindowMap) {
5743 focus = getFocusedWindowLocked();
5744 }
Romain Guy06882f82009-06-10 13:36:04 -07005745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005746 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
Romain Guy06882f82009-06-10 13:36:04 -07005747
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005748 if (callingUid == 0 ||
5749 (focus != null && callingUid == focus.mSession.mUid) ||
5750 mContext.checkPermission(
5751 android.Manifest.permission.INJECT_EVENTS,
5752 callingPid, callingUid)
5753 == PackageManager.PERMISSION_GRANTED) {
5754 if (mPolicy.interceptKeyTi(focus,
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005755 keycode, nextKey.getMetaState(), down, repeatCount,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005756 nextKey.getFlags())) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005757 return CONSUMED_EVENT_TOKEN;
5758 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005759 }
Romain Guy06882f82009-06-10 13:36:04 -07005760
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005761 return focus;
Romain Guy06882f82009-06-10 13:36:04 -07005762
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005763 } else if (!isPointerEvent) {
5764 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(-1);
5765 if (!dispatch) {
5766 Log.w(TAG, "Event timeout during app switch: dropping trackball "
5767 + nextMotion);
5768 return SKIP_TARGET_TOKEN;
5769 }
Romain Guy06882f82009-06-10 13:36:04 -07005770
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005771 WindowState focus = null;
5772 synchronized(mWindowMap) {
5773 focus = getFocusedWindowLocked();
5774 }
Romain Guy06882f82009-06-10 13:36:04 -07005775
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005776 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
5777 return focus;
5778 }
Romain Guy06882f82009-06-10 13:36:04 -07005779
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005780 if (nextMotion == null) {
5781 return SKIP_TARGET_TOKEN;
5782 }
Romain Guy06882f82009-06-10 13:36:04 -07005783
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005784 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(
5785 KeyEvent.KEYCODE_UNKNOWN);
5786 if (!dispatch) {
5787 Log.w(TAG, "Event timeout during app switch: dropping pointer "
5788 + nextMotion);
5789 return SKIP_TARGET_TOKEN;
5790 }
Romain Guy06882f82009-06-10 13:36:04 -07005791
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005792 // Find the target window for a pointer event.
5793 int action = nextMotion.getAction();
5794 final float xf = nextMotion.getX();
5795 final float yf = nextMotion.getY();
5796 final long eventTime = nextMotion.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07005797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005798 final boolean screenWasOff = qev != null
5799 && (qev.flags&WindowManagerPolicy.FLAG_BRIGHT_HERE) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07005800
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005801 WindowState target = null;
Romain Guy06882f82009-06-10 13:36:04 -07005802
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005803 synchronized(mWindowMap) {
5804 synchronized (this) {
5805 if (action == MotionEvent.ACTION_DOWN) {
5806 if (mMotionTarget != null) {
5807 // this is weird, we got a pen down, but we thought it was
5808 // already down!
5809 // XXX: We should probably send an ACTION_UP to the current
5810 // target.
5811 Log.w(TAG, "Pointer down received while already down in: "
5812 + mMotionTarget);
5813 mMotionTarget = null;
5814 }
Romain Guy06882f82009-06-10 13:36:04 -07005815
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005816 // ACTION_DOWN is special, because we need to lock next events to
5817 // the window we'll land onto.
5818 final int x = (int)xf;
5819 final int y = (int)yf;
Romain Guy06882f82009-06-10 13:36:04 -07005820
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005821 final ArrayList windows = mWindows;
5822 final int N = windows.size();
5823 WindowState topErrWindow = null;
5824 final Rect tmpRect = mTempRect;
5825 for (int i=N-1; i>=0; i--) {
5826 WindowState child = (WindowState)windows.get(i);
5827 //Log.i(TAG, "Checking dispatch to: " + child);
5828 final int flags = child.mAttrs.flags;
5829 if ((flags & WindowManager.LayoutParams.FLAG_SYSTEM_ERROR) != 0) {
5830 if (topErrWindow == null) {
5831 topErrWindow = child;
5832 }
5833 }
5834 if (!child.isVisibleLw()) {
5835 //Log.i(TAG, "Not visible!");
5836 continue;
5837 }
5838 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
5839 //Log.i(TAG, "Not touchable!");
5840 if ((flags & WindowManager.LayoutParams
5841 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5842 child.mNextOutsideTouch = mOutsideTouchTargets;
5843 mOutsideTouchTargets = child;
5844 }
5845 continue;
5846 }
5847 tmpRect.set(child.mFrame);
5848 if (child.mTouchableInsets == ViewTreeObserver
5849 .InternalInsetsInfo.TOUCHABLE_INSETS_CONTENT) {
5850 // The touch is inside of the window if it is
5851 // inside the frame, AND the content part of that
5852 // frame that was given by the application.
5853 tmpRect.left += child.mGivenContentInsets.left;
5854 tmpRect.top += child.mGivenContentInsets.top;
5855 tmpRect.right -= child.mGivenContentInsets.right;
5856 tmpRect.bottom -= child.mGivenContentInsets.bottom;
5857 } else if (child.mTouchableInsets == ViewTreeObserver
5858 .InternalInsetsInfo.TOUCHABLE_INSETS_VISIBLE) {
5859 // The touch is inside of the window if it is
5860 // inside the frame, AND the visible part of that
5861 // frame that was given by the application.
5862 tmpRect.left += child.mGivenVisibleInsets.left;
5863 tmpRect.top += child.mGivenVisibleInsets.top;
5864 tmpRect.right -= child.mGivenVisibleInsets.right;
5865 tmpRect.bottom -= child.mGivenVisibleInsets.bottom;
5866 }
5867 final int touchFlags = flags &
5868 (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
5869 |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
5870 if (tmpRect.contains(x, y) || touchFlags == 0) {
5871 //Log.i(TAG, "Using this target!");
5872 if (!screenWasOff || (flags &
5873 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING) != 0) {
5874 mMotionTarget = child;
5875 } else {
5876 //Log.i(TAG, "Waking, skip!");
5877 mMotionTarget = null;
5878 }
5879 break;
5880 }
Romain Guy06882f82009-06-10 13:36:04 -07005881
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005882 if ((flags & WindowManager.LayoutParams
5883 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5884 child.mNextOutsideTouch = mOutsideTouchTargets;
5885 mOutsideTouchTargets = child;
5886 //Log.i(TAG, "Adding to outside target list: " + child);
5887 }
5888 }
5889
5890 // if there's an error window but it's not accepting
5891 // focus (typically because it is not yet visible) just
5892 // wait for it -- any other focused window may in fact
5893 // be in ANR state.
5894 if (topErrWindow != null && mMotionTarget != topErrWindow) {
5895 mMotionTarget = null;
5896 }
5897 }
Romain Guy06882f82009-06-10 13:36:04 -07005898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005899 target = mMotionTarget;
5900 }
5901 }
Romain Guy06882f82009-06-10 13:36:04 -07005902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005903 wakeupIfNeeded(target, eventType(nextMotion));
Romain Guy06882f82009-06-10 13:36:04 -07005904
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005905 // Pointer events are a little different -- if there isn't a
5906 // target found for any event, then just drop it.
5907 return target != null ? target : SKIP_TARGET_TOKEN;
5908 }
Romain Guy06882f82009-06-10 13:36:04 -07005909
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005910 boolean checkShouldDispatchKey(int keycode) {
5911 synchronized (this) {
5912 if (mPolicy.isAppSwitchKeyTqTiLwLi(keycode)) {
5913 mTimeToSwitch = 0;
5914 return true;
5915 }
5916 if (mTimeToSwitch != 0
5917 && mTimeToSwitch < SystemClock.uptimeMillis()) {
5918 return false;
5919 }
5920 return true;
5921 }
5922 }
Romain Guy06882f82009-06-10 13:36:04 -07005923
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005924 void bindTargetWindowLocked(WindowState win,
5925 int pendingWhat, QueuedEvent pendingMotion) {
5926 synchronized (this) {
5927 bindTargetWindowLockedLocked(win, pendingWhat, pendingMotion);
5928 }
5929 }
Romain Guy06882f82009-06-10 13:36:04 -07005930
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005931 void bindTargetWindowLocked(WindowState win) {
5932 synchronized (this) {
5933 bindTargetWindowLockedLocked(win, RETURN_NOTHING, null);
5934 }
5935 }
5936
5937 void bindTargetWindowLockedLocked(WindowState win,
5938 int pendingWhat, QueuedEvent pendingMotion) {
5939 mLastWin = win;
5940 mLastBinder = win.mClient.asBinder();
5941 mFinished = false;
5942 if (pendingMotion != null) {
5943 final Session s = win.mSession;
5944 if (pendingWhat == RETURN_PENDING_POINTER) {
5945 releasePendingPointerLocked(s);
5946 s.mPendingPointerMove = pendingMotion;
5947 s.mPendingPointerWindow = win;
Romain Guy06882f82009-06-10 13:36:04 -07005948 if (DEBUG_INPUT) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005949 "bindTargetToWindow " + s.mPendingPointerMove);
5950 } else if (pendingWhat == RETURN_PENDING_TRACKBALL) {
5951 releasePendingTrackballLocked(s);
5952 s.mPendingTrackballMove = pendingMotion;
5953 s.mPendingTrackballWindow = win;
5954 }
5955 }
5956 }
Romain Guy06882f82009-06-10 13:36:04 -07005957
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005958 void releasePendingPointerLocked(Session s) {
5959 if (DEBUG_INPUT) Log.v(TAG,
5960 "releasePendingPointer " + s.mPendingPointerMove);
5961 if (s.mPendingPointerMove != null) {
5962 mQueue.recycleEvent(s.mPendingPointerMove);
5963 s.mPendingPointerMove = null;
5964 }
5965 }
Romain Guy06882f82009-06-10 13:36:04 -07005966
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005967 void releasePendingTrackballLocked(Session s) {
5968 if (s.mPendingTrackballMove != null) {
5969 mQueue.recycleEvent(s.mPendingTrackballMove);
5970 s.mPendingTrackballMove = null;
5971 }
5972 }
Romain Guy06882f82009-06-10 13:36:04 -07005973
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005974 MotionEvent finishedKey(Session session, IWindow client, boolean force,
5975 int returnWhat) {
5976 if (DEBUG_INPUT) Log.v(
5977 TAG, "finishedKey: client=" + client + ", force=" + force);
5978
5979 if (client == null) {
5980 return null;
5981 }
5982
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005983 MotionEvent res = null;
5984 QueuedEvent qev = null;
5985 WindowState win = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005986
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005987 synchronized (this) {
5988 if (DEBUG_INPUT) Log.v(
5989 TAG, "finishedKey: client=" + client.asBinder()
5990 + ", force=" + force + ", last=" + mLastBinder
5991 + " (token=" + (mLastWin != null ? mLastWin.mToken : null) + ")");
5992
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005993 if (returnWhat == RETURN_PENDING_POINTER) {
5994 qev = session.mPendingPointerMove;
5995 win = session.mPendingPointerWindow;
5996 session.mPendingPointerMove = null;
5997 session.mPendingPointerWindow = null;
5998 } else if (returnWhat == RETURN_PENDING_TRACKBALL) {
5999 qev = session.mPendingTrackballMove;
6000 win = session.mPendingTrackballWindow;
6001 session.mPendingTrackballMove = null;
6002 session.mPendingTrackballWindow = null;
6003 }
Romain Guy06882f82009-06-10 13:36:04 -07006004
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006005 if (mLastBinder == client.asBinder()) {
6006 if (DEBUG_INPUT) Log.v(
6007 TAG, "finishedKey: last paused="
6008 + ((mLastWin != null) ? mLastWin.mToken.paused : "null"));
6009 if (mLastWin != null && (!mLastWin.mToken.paused || force
6010 || !mEventDispatching)) {
Christopher Tate136b1f92010-02-11 17:51:24 -08006011 doFinishedKeyLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006012 } else {
6013 // Make sure to wake up anyone currently waiting to
6014 // dispatch a key, so they can re-evaluate their
6015 // current situation.
6016 mFinished = true;
6017 notifyAll();
6018 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006019 }
Romain Guy06882f82009-06-10 13:36:04 -07006020
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006021 if (qev != null) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07006022 res = (MotionEvent)qev.event;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006023 if (DEBUG_INPUT) Log.v(TAG,
6024 "Returning pending motion: " + res);
6025 mQueue.recycleEvent(qev);
6026 if (win != null && returnWhat == RETURN_PENDING_POINTER) {
6027 res.offsetLocation(-win.mFrame.left, -win.mFrame.top);
6028 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006029 }
Christopher Tate2624fbc2009-12-11 12:11:31 -08006030 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006031
Christopher Tate2624fbc2009-12-11 12:11:31 -08006032 if (res != null && returnWhat == RETURN_PENDING_POINTER) {
6033 synchronized (mWindowMap) {
6034 if ((mWallpaperTarget == win &&
6035 win.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD)
6036 || mSendingPointersToWallpaper) {
6037 sendPointerToWallpaperLocked(win, res, res.getEventTime());
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07006038 }
6039 }
6040 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006041
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07006042 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006043 }
6044
6045 void tickle() {
6046 synchronized (this) {
6047 notifyAll();
6048 }
6049 }
Romain Guy06882f82009-06-10 13:36:04 -07006050
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006051 void handleNewWindowLocked(WindowState newWindow) {
6052 if (!newWindow.canReceiveKeys()) {
6053 return;
6054 }
6055 synchronized (this) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006056 if (DEBUG_INPUT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006057 TAG, "New key dispatch window: win="
6058 + newWindow.mClient.asBinder()
6059 + ", last=" + mLastBinder
6060 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
6061 + "), finished=" + mFinished + ", paused="
6062 + newWindow.mToken.paused);
6063
6064 // Displaying a window implicitly causes dispatching to
6065 // be unpaused. (This is to protect against bugs if someone
6066 // pauses dispatching but forgets to resume.)
6067 newWindow.mToken.paused = false;
6068
6069 mGotFirstWindow = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006070
6071 if ((newWindow.mAttrs.flags & FLAG_SYSTEM_ERROR) != 0) {
6072 if (DEBUG_INPUT) Log.v(TAG,
6073 "New SYSTEM_ERROR window; resetting state");
6074 mLastWin = null;
6075 mLastBinder = null;
6076 mMotionTarget = null;
6077 mFinished = true;
6078 } else if (mLastWin != null) {
6079 // If the new window is above the window we are
6080 // waiting on, then stop waiting and let key dispatching
6081 // start on the new guy.
6082 if (DEBUG_INPUT) Log.v(
6083 TAG, "Last win layer=" + mLastWin.mLayer
6084 + ", new win layer=" + newWindow.mLayer);
6085 if (newWindow.mLayer >= mLastWin.mLayer) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006086 // The new window is above the old; finish pending input to the last
6087 // window and start directing it to the new one.
6088 mLastWin.mToken.paused = false;
Christopher Tate136b1f92010-02-11 17:51:24 -08006089 doFinishedKeyLocked(false); // does a notifyAll()
6090 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006091 }
6092 }
6093
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006094 // Now that we've put a new window state in place, make the event waiter
6095 // take notice and retarget its attentions.
6096 notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006097 }
6098 }
6099
6100 void pauseDispatchingLocked(WindowToken token) {
6101 synchronized (this)
6102 {
6103 if (DEBUG_INPUT) Log.v(TAG, "Pausing WindowToken " + token);
6104 token.paused = true;
6105
6106 /*
6107 if (mLastWin != null && !mFinished && mLastWin.mBaseLayer <= layer) {
6108 mPaused = true;
6109 } else {
6110 if (mLastWin == null) {
Dave Bortcfe65242009-04-09 14:51:04 -07006111 Log.i(TAG, "Key dispatching not paused: no last window.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006112 } else if (mFinished) {
Dave Bortcfe65242009-04-09 14:51:04 -07006113 Log.i(TAG, "Key dispatching not paused: finished last key.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006114 } else {
Dave Bortcfe65242009-04-09 14:51:04 -07006115 Log.i(TAG, "Key dispatching not paused: window in higher layer.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006116 }
6117 }
6118 */
6119 }
6120 }
6121
6122 void resumeDispatchingLocked(WindowToken token) {
6123 synchronized (this) {
6124 if (token.paused) {
6125 if (DEBUG_INPUT) Log.v(
6126 TAG, "Resuming WindowToken " + token
6127 + ", last=" + mLastBinder
6128 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
6129 + "), finished=" + mFinished + ", paused="
6130 + token.paused);
6131 token.paused = false;
6132 if (mLastWin != null && mLastWin.mToken == token && mFinished) {
Christopher Tate136b1f92010-02-11 17:51:24 -08006133 doFinishedKeyLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006134 } else {
6135 notifyAll();
6136 }
6137 }
6138 }
6139 }
6140
6141 void setEventDispatchingLocked(boolean enabled) {
6142 synchronized (this) {
6143 mEventDispatching = enabled;
6144 notifyAll();
6145 }
6146 }
Romain Guy06882f82009-06-10 13:36:04 -07006147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006148 void appSwitchComing() {
6149 synchronized (this) {
6150 // Don't wait for more than .5 seconds for app to finish
6151 // processing the pending events.
6152 long now = SystemClock.uptimeMillis() + 500;
6153 if (DEBUG_INPUT) Log.v(TAG, "appSwitchComing: " + now);
6154 if (mTimeToSwitch == 0 || now < mTimeToSwitch) {
6155 mTimeToSwitch = now;
6156 }
6157 notifyAll();
6158 }
6159 }
Romain Guy06882f82009-06-10 13:36:04 -07006160
Christopher Tate136b1f92010-02-11 17:51:24 -08006161 private final void doFinishedKeyLocked(boolean force) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006162 if (mLastWin != null) {
6163 releasePendingPointerLocked(mLastWin.mSession);
6164 releasePendingTrackballLocked(mLastWin.mSession);
6165 }
Romain Guy06882f82009-06-10 13:36:04 -07006166
Christopher Tate136b1f92010-02-11 17:51:24 -08006167 if (force || mLastWin == null || !mLastWin.mToken.paused
6168 || !mLastWin.isVisibleLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006169 // If the current window has been paused, we aren't -really-
6170 // finished... so let the waiters still wait.
6171 mLastWin = null;
6172 mLastBinder = null;
6173 }
6174 mFinished = true;
6175 notifyAll();
6176 }
6177 }
6178
6179 private class KeyQ extends KeyInputQueue
6180 implements KeyInputQueue.FilterCallback {
6181 PowerManager.WakeLock mHoldingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07006182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006183 KeyQ() {
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07006184 super(mContext, WindowManagerService.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006185 PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
6186 mHoldingScreen = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
6187 "KEEP_SCREEN_ON_FLAG");
6188 mHoldingScreen.setReferenceCounted(false);
6189 }
6190
6191 @Override
6192 boolean preprocessEvent(InputDevice device, RawInputEvent event) {
6193 if (mPolicy.preprocessInputEventTq(event)) {
6194 return true;
6195 }
Romain Guy06882f82009-06-10 13:36:04 -07006196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006197 switch (event.type) {
6198 case RawInputEvent.EV_KEY: {
6199 // XXX begin hack
6200 if (DEBUG) {
6201 if (event.keycode == KeyEvent.KEYCODE_G) {
6202 if (event.value != 0) {
6203 // G down
6204 mPolicy.screenTurnedOff(WindowManagerPolicy.OFF_BECAUSE_OF_USER);
6205 }
6206 return false;
6207 }
6208 if (event.keycode == KeyEvent.KEYCODE_D) {
6209 if (event.value != 0) {
6210 //dump();
6211 }
6212 return false;
6213 }
6214 }
6215 // XXX end hack
Romain Guy06882f82009-06-10 13:36:04 -07006216
Charles Mendis322591c2009-10-29 11:06:59 -07006217 boolean screenIsOff = !mPowerManager.isScreenOn();
6218 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006219 int actions = mPolicy.interceptKeyTq(event, !screenIsOff);
Romain Guy06882f82009-06-10 13:36:04 -07006220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006221 if ((actions & WindowManagerPolicy.ACTION_GO_TO_SLEEP) != 0) {
6222 mPowerManager.goToSleep(event.when);
6223 }
6224
6225 if (screenIsOff) {
6226 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6227 }
6228 if (screenIsDim) {
6229 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6230 }
6231 if ((actions & WindowManagerPolicy.ACTION_POKE_USER_ACTIVITY) != 0) {
6232 mPowerManager.userActivity(event.when, false,
Michael Chane96440f2009-05-06 10:27:36 -07006233 LocalPowerManager.BUTTON_EVENT, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006234 }
Romain Guy06882f82009-06-10 13:36:04 -07006235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006236 if ((actions & WindowManagerPolicy.ACTION_PASS_TO_USER) != 0) {
6237 if (event.value != 0 && mPolicy.isAppSwitchKeyTqTiLwLi(event.keycode)) {
6238 filterQueue(this);
6239 mKeyWaiter.appSwitchComing();
6240 }
6241 return true;
6242 } else {
6243 return false;
6244 }
6245 }
Romain Guy06882f82009-06-10 13:36:04 -07006246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006247 case RawInputEvent.EV_REL: {
Charles Mendis322591c2009-10-29 11:06:59 -07006248 boolean screenIsOff = !mPowerManager.isScreenOn();
6249 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006250 if (screenIsOff) {
6251 if (!mPolicy.isWakeRelMovementTq(event.deviceId,
6252 device.classes, event)) {
6253 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
6254 return false;
6255 }
6256 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6257 }
6258 if (screenIsDim) {
6259 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6260 }
6261 return true;
6262 }
Romain Guy06882f82009-06-10 13:36:04 -07006263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006264 case RawInputEvent.EV_ABS: {
Charles Mendis322591c2009-10-29 11:06:59 -07006265 boolean screenIsOff = !mPowerManager.isScreenOn();
6266 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006267 if (screenIsOff) {
6268 if (!mPolicy.isWakeAbsMovementTq(event.deviceId,
6269 device.classes, event)) {
6270 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
6271 return false;
6272 }
6273 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6274 }
6275 if (screenIsDim) {
6276 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6277 }
6278 return true;
6279 }
Romain Guy06882f82009-06-10 13:36:04 -07006280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006281 default:
6282 return true;
6283 }
6284 }
6285
6286 public int filterEvent(QueuedEvent ev) {
6287 switch (ev.classType) {
6288 case RawInputEvent.CLASS_KEYBOARD:
6289 KeyEvent ke = (KeyEvent)ev.event;
6290 if (mPolicy.isMovementKeyTi(ke.getKeyCode())) {
6291 Log.w(TAG, "Dropping movement key during app switch: "
6292 + ke.getKeyCode() + ", action=" + ke.getAction());
6293 return FILTER_REMOVE;
6294 }
6295 return FILTER_ABORT;
6296 default:
6297 return FILTER_KEEP;
6298 }
6299 }
Romain Guy06882f82009-06-10 13:36:04 -07006300
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006301 /**
6302 * Must be called with the main window manager lock held.
6303 */
6304 void setHoldScreenLocked(boolean holding) {
6305 boolean state = mHoldingScreen.isHeld();
6306 if (holding != state) {
6307 if (holding) {
6308 mHoldingScreen.acquire();
6309 } else {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006310 mPolicy.screenOnStoppedLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006311 mHoldingScreen.release();
6312 }
6313 }
6314 }
Michael Chan53071d62009-05-13 17:29:48 -07006315 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006316
6317 public boolean detectSafeMode() {
6318 mSafeMode = mPolicy.detectSafeMode();
6319 return mSafeMode;
6320 }
Romain Guy06882f82009-06-10 13:36:04 -07006321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006322 public void systemReady() {
6323 mPolicy.systemReady();
6324 }
Romain Guy06882f82009-06-10 13:36:04 -07006325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006326 private final class InputDispatcherThread extends Thread {
6327 // Time to wait when there is nothing to do: 9999 seconds.
6328 static final int LONG_WAIT=9999*1000;
6329
6330 public InputDispatcherThread() {
6331 super("InputDispatcher");
6332 }
Romain Guy06882f82009-06-10 13:36:04 -07006333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006334 @Override
6335 public void run() {
6336 while (true) {
6337 try {
6338 process();
6339 } catch (Exception e) {
6340 Log.e(TAG, "Exception in input dispatcher", e);
6341 }
6342 }
6343 }
Romain Guy06882f82009-06-10 13:36:04 -07006344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006345 private void process() {
6346 android.os.Process.setThreadPriority(
6347 android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -07006348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006349 // The last key event we saw
6350 KeyEvent lastKey = null;
6351
6352 // Last keydown time for auto-repeating keys
6353 long lastKeyTime = SystemClock.uptimeMillis();
6354 long nextKeyTime = lastKeyTime+LONG_WAIT;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006355 long downTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006356
Romain Guy06882f82009-06-10 13:36:04 -07006357 // How many successive repeats we generated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006358 int keyRepeatCount = 0;
6359
6360 // Need to report that configuration has changed?
6361 boolean configChanged = false;
Romain Guy06882f82009-06-10 13:36:04 -07006362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006363 while (true) {
6364 long curTime = SystemClock.uptimeMillis();
6365
6366 if (DEBUG_INPUT) Log.v(
6367 TAG, "Waiting for next key: now=" + curTime
6368 + ", repeat @ " + nextKeyTime);
6369
6370 // Retrieve next event, waiting only as long as the next
6371 // repeat timeout. If the configuration has changed, then
6372 // don't wait at all -- we'll report the change as soon as
6373 // we have processed all events.
6374 QueuedEvent ev = mQueue.getEvent(
6375 (int)((!configChanged && curTime < nextKeyTime)
6376 ? (nextKeyTime-curTime) : 0));
6377
6378 if (DEBUG_INPUT && ev != null) Log.v(
6379 TAG, "Event: type=" + ev.classType + " data=" + ev.event);
6380
Michael Chan53071d62009-05-13 17:29:48 -07006381 if (MEASURE_LATENCY) {
6382 lt.sample("2 got event ", System.nanoTime() - ev.whenNano);
6383 }
6384
Mike Lockwood3d0ea722009-10-21 22:58:29 -04006385 if (lastKey != null && !mPolicy.allowKeyRepeat()) {
6386 // cancel key repeat at the request of the policy.
6387 lastKey = null;
6388 downTime = 0;
6389 lastKeyTime = curTime;
6390 nextKeyTime = curTime + LONG_WAIT;
6391 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006392 try {
6393 if (ev != null) {
Michael Chan53071d62009-05-13 17:29:48 -07006394 curTime = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006395 int eventType;
6396 if (ev.classType == RawInputEvent.CLASS_TOUCHSCREEN) {
6397 eventType = eventType((MotionEvent)ev.event);
6398 } else if (ev.classType == RawInputEvent.CLASS_KEYBOARD ||
6399 ev.classType == RawInputEvent.CLASS_TRACKBALL) {
6400 eventType = LocalPowerManager.BUTTON_EVENT;
6401 } else {
6402 eventType = LocalPowerManager.OTHER_EVENT;
6403 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006404 try {
Michael Chan53071d62009-05-13 17:29:48 -07006405 if ((curTime - mLastBatteryStatsCallTime)
Michael Chane96440f2009-05-06 10:27:36 -07006406 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
Michael Chan53071d62009-05-13 17:29:48 -07006407 mLastBatteryStatsCallTime = curTime;
Michael Chane96440f2009-05-06 10:27:36 -07006408 mBatteryStats.noteInputEvent();
6409 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006410 } catch (RemoteException e) {
6411 // Ignore
6412 }
Michael Chane10de972009-05-18 11:24:50 -07006413
Mike Lockwood5db42402009-11-30 14:51:51 -05006414 if (ev.classType == RawInputEvent.CLASS_CONFIGURATION_CHANGED) {
6415 // do not wake screen in this case
6416 } else if (eventType != TOUCH_EVENT
Michael Chane10de972009-05-18 11:24:50 -07006417 && eventType != LONG_TOUCH_EVENT
6418 && eventType != CHEEK_EVENT) {
6419 mPowerManager.userActivity(curTime, false,
6420 eventType, false);
6421 } else if (mLastTouchEventType != eventType
6422 || (curTime - mLastUserActivityCallTime)
6423 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
6424 mLastUserActivityCallTime = curTime;
6425 mLastTouchEventType = eventType;
6426 mPowerManager.userActivity(curTime, false,
6427 eventType, false);
6428 }
6429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006430 switch (ev.classType) {
6431 case RawInputEvent.CLASS_KEYBOARD:
6432 KeyEvent ke = (KeyEvent)ev.event;
6433 if (ke.isDown()) {
6434 lastKey = ke;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006435 downTime = curTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006436 keyRepeatCount = 0;
6437 lastKeyTime = curTime;
6438 nextKeyTime = lastKeyTime
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006439 + ViewConfiguration.getLongPressTimeout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006440 if (DEBUG_INPUT) Log.v(
6441 TAG, "Received key down: first repeat @ "
6442 + nextKeyTime);
6443 } else {
6444 lastKey = null;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006445 downTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006446 // Arbitrary long timeout.
6447 lastKeyTime = curTime;
6448 nextKeyTime = curTime + LONG_WAIT;
6449 if (DEBUG_INPUT) Log.v(
6450 TAG, "Received key up: ignore repeat @ "
6451 + nextKeyTime);
6452 }
6453 dispatchKey((KeyEvent)ev.event, 0, 0);
6454 mQueue.recycleEvent(ev);
6455 break;
6456 case RawInputEvent.CLASS_TOUCHSCREEN:
6457 //Log.i(TAG, "Read next event " + ev);
6458 dispatchPointer(ev, (MotionEvent)ev.event, 0, 0);
6459 break;
6460 case RawInputEvent.CLASS_TRACKBALL:
6461 dispatchTrackball(ev, (MotionEvent)ev.event, 0, 0);
6462 break;
6463 case RawInputEvent.CLASS_CONFIGURATION_CHANGED:
6464 configChanged = true;
6465 break;
6466 default:
6467 mQueue.recycleEvent(ev);
6468 break;
6469 }
Romain Guy06882f82009-06-10 13:36:04 -07006470
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006471 } else if (configChanged) {
6472 configChanged = false;
6473 sendNewConfiguration();
Romain Guy06882f82009-06-10 13:36:04 -07006474
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006475 } else if (lastKey != null) {
6476 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006478 // Timeout occurred while key was down. If it is at or
6479 // past the key repeat time, dispatch the repeat.
6480 if (DEBUG_INPUT) Log.v(
6481 TAG, "Key timeout: repeat=" + nextKeyTime
6482 + ", now=" + curTime);
6483 if (curTime < nextKeyTime) {
6484 continue;
6485 }
Romain Guy06882f82009-06-10 13:36:04 -07006486
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006487 lastKeyTime = nextKeyTime;
6488 nextKeyTime = nextKeyTime + KEY_REPEAT_DELAY;
6489 keyRepeatCount++;
6490 if (DEBUG_INPUT) Log.v(
6491 TAG, "Key repeat: count=" + keyRepeatCount
6492 + ", next @ " + nextKeyTime);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006493 KeyEvent newEvent;
6494 if (downTime != 0 && (downTime
6495 + ViewConfiguration.getLongPressTimeout())
6496 <= curTime) {
6497 newEvent = KeyEvent.changeTimeRepeat(lastKey,
6498 curTime, keyRepeatCount,
6499 lastKey.getFlags() | KeyEvent.FLAG_LONG_PRESS);
6500 downTime = 0;
6501 } else {
6502 newEvent = KeyEvent.changeTimeRepeat(lastKey,
6503 curTime, keyRepeatCount);
6504 }
6505 dispatchKey(newEvent, 0, 0);
Romain Guy06882f82009-06-10 13:36:04 -07006506
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006507 } else {
6508 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006510 lastKeyTime = curTime;
6511 nextKeyTime = curTime + LONG_WAIT;
6512 }
Romain Guy06882f82009-06-10 13:36:04 -07006513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006514 } catch (Exception e) {
6515 Log.e(TAG,
6516 "Input thread received uncaught exception: " + e, e);
6517 }
6518 }
6519 }
6520 }
6521
6522 // -------------------------------------------------------------
6523 // Client Session State
6524 // -------------------------------------------------------------
6525
6526 private final class Session extends IWindowSession.Stub
6527 implements IBinder.DeathRecipient {
6528 final IInputMethodClient mClient;
6529 final IInputContext mInputContext;
6530 final int mUid;
6531 final int mPid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006532 final String mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006533 SurfaceSession mSurfaceSession;
6534 int mNumWindow = 0;
6535 boolean mClientDead = false;
Romain Guy06882f82009-06-10 13:36:04 -07006536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006537 /**
6538 * Current pointer move event being dispatched to client window... must
6539 * hold key lock to access.
6540 */
6541 QueuedEvent mPendingPointerMove;
6542 WindowState mPendingPointerWindow;
Romain Guy06882f82009-06-10 13:36:04 -07006543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006544 /**
6545 * Current trackball move event being dispatched to client window... must
6546 * hold key lock to access.
6547 */
6548 QueuedEvent mPendingTrackballMove;
6549 WindowState mPendingTrackballWindow;
6550
6551 public Session(IInputMethodClient client, IInputContext inputContext) {
6552 mClient = client;
6553 mInputContext = inputContext;
6554 mUid = Binder.getCallingUid();
6555 mPid = Binder.getCallingPid();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006556 StringBuilder sb = new StringBuilder();
6557 sb.append("Session{");
6558 sb.append(Integer.toHexString(System.identityHashCode(this)));
6559 sb.append(" uid ");
6560 sb.append(mUid);
6561 sb.append("}");
6562 mStringName = sb.toString();
Romain Guy06882f82009-06-10 13:36:04 -07006563
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006564 synchronized (mWindowMap) {
6565 if (mInputMethodManager == null && mHaveInputMethods) {
6566 IBinder b = ServiceManager.getService(
6567 Context.INPUT_METHOD_SERVICE);
6568 mInputMethodManager = IInputMethodManager.Stub.asInterface(b);
6569 }
6570 }
6571 long ident = Binder.clearCallingIdentity();
6572 try {
6573 // Note: it is safe to call in to the input method manager
6574 // here because we are not holding our lock.
6575 if (mInputMethodManager != null) {
6576 mInputMethodManager.addClient(client, inputContext,
6577 mUid, mPid);
6578 } else {
6579 client.setUsingInputMethod(false);
6580 }
6581 client.asBinder().linkToDeath(this, 0);
6582 } catch (RemoteException e) {
6583 // The caller has died, so we can just forget about this.
6584 try {
6585 if (mInputMethodManager != null) {
6586 mInputMethodManager.removeClient(client);
6587 }
6588 } catch (RemoteException ee) {
6589 }
6590 } finally {
6591 Binder.restoreCallingIdentity(ident);
6592 }
6593 }
Romain Guy06882f82009-06-10 13:36:04 -07006594
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006595 @Override
6596 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
6597 throws RemoteException {
6598 try {
6599 return super.onTransact(code, data, reply, flags);
6600 } catch (RuntimeException e) {
6601 // Log all 'real' exceptions thrown to the caller
6602 if (!(e instanceof SecurityException)) {
6603 Log.e(TAG, "Window Session Crash", e);
6604 }
6605 throw e;
6606 }
6607 }
6608
6609 public void binderDied() {
6610 // Note: it is safe to call in to the input method manager
6611 // here because we are not holding our lock.
6612 try {
6613 if (mInputMethodManager != null) {
6614 mInputMethodManager.removeClient(mClient);
6615 }
6616 } catch (RemoteException e) {
6617 }
6618 synchronized(mWindowMap) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07006619 mClient.asBinder().unlinkToDeath(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006620 mClientDead = true;
6621 killSessionLocked();
6622 }
6623 }
6624
6625 public int add(IWindow window, WindowManager.LayoutParams attrs,
6626 int viewVisibility, Rect outContentInsets) {
6627 return addWindow(this, window, attrs, viewVisibility, outContentInsets);
6628 }
Romain Guy06882f82009-06-10 13:36:04 -07006629
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006630 public void remove(IWindow window) {
6631 removeWindow(this, window);
6632 }
Romain Guy06882f82009-06-10 13:36:04 -07006633
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006634 public int relayout(IWindow window, WindowManager.LayoutParams attrs,
6635 int requestedWidth, int requestedHeight, int viewFlags,
6636 boolean insetsPending, Rect outFrame, Rect outContentInsets,
6637 Rect outVisibleInsets, Surface outSurface) {
6638 return relayoutWindow(this, window, attrs,
6639 requestedWidth, requestedHeight, viewFlags, insetsPending,
6640 outFrame, outContentInsets, outVisibleInsets, outSurface);
6641 }
Romain Guy06882f82009-06-10 13:36:04 -07006642
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006643 public void setTransparentRegion(IWindow window, Region region) {
6644 setTransparentRegionWindow(this, window, region);
6645 }
Romain Guy06882f82009-06-10 13:36:04 -07006646
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006647 public void setInsets(IWindow window, int touchableInsets,
6648 Rect contentInsets, Rect visibleInsets) {
6649 setInsetsWindow(this, window, touchableInsets, contentInsets,
6650 visibleInsets);
6651 }
Romain Guy06882f82009-06-10 13:36:04 -07006652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006653 public void getDisplayFrame(IWindow window, Rect outDisplayFrame) {
6654 getWindowDisplayFrame(this, window, outDisplayFrame);
6655 }
Romain Guy06882f82009-06-10 13:36:04 -07006656
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006657 public void finishDrawing(IWindow window) {
6658 if (localLOGV) Log.v(
6659 TAG, "IWindow finishDrawing called for " + window);
6660 finishDrawingWindow(this, window);
6661 }
6662
6663 public void finishKey(IWindow window) {
6664 if (localLOGV) Log.v(
6665 TAG, "IWindow finishKey called for " + window);
6666 mKeyWaiter.finishedKey(this, window, false,
6667 KeyWaiter.RETURN_NOTHING);
6668 }
6669
6670 public MotionEvent getPendingPointerMove(IWindow window) {
6671 if (localLOGV) Log.v(
6672 TAG, "IWindow getPendingMotionEvent called for " + window);
6673 return mKeyWaiter.finishedKey(this, window, false,
6674 KeyWaiter.RETURN_PENDING_POINTER);
6675 }
Romain Guy06882f82009-06-10 13:36:04 -07006676
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006677 public MotionEvent getPendingTrackballMove(IWindow window) {
6678 if (localLOGV) Log.v(
6679 TAG, "IWindow getPendingMotionEvent called for " + window);
6680 return mKeyWaiter.finishedKey(this, window, false,
6681 KeyWaiter.RETURN_PENDING_TRACKBALL);
6682 }
6683
6684 public void setInTouchMode(boolean mode) {
6685 synchronized(mWindowMap) {
6686 mInTouchMode = mode;
6687 }
6688 }
6689
6690 public boolean getInTouchMode() {
6691 synchronized(mWindowMap) {
6692 return mInTouchMode;
6693 }
6694 }
6695
6696 public boolean performHapticFeedback(IWindow window, int effectId,
6697 boolean always) {
6698 synchronized(mWindowMap) {
6699 long ident = Binder.clearCallingIdentity();
6700 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006701 return mPolicy.performHapticFeedbackLw(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006702 windowForClientLocked(this, window), effectId, always);
6703 } finally {
6704 Binder.restoreCallingIdentity(ident);
6705 }
6706 }
6707 }
Romain Guy06882f82009-06-10 13:36:04 -07006708
Marco Nelissenbf6956b2009-11-09 15:21:13 -08006709 public void setWallpaperPosition(IBinder window, float x, float y, float xStep, float yStep) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006710 synchronized(mWindowMap) {
6711 long ident = Binder.clearCallingIdentity();
6712 try {
6713 setWindowWallpaperPositionLocked(windowForClientLocked(this, window),
Marco Nelissenbf6956b2009-11-09 15:21:13 -08006714 x, y, xStep, yStep);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006715 } finally {
6716 Binder.restoreCallingIdentity(ident);
6717 }
6718 }
6719 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006720
Dianne Hackborn19382ac2009-09-11 21:13:37 -07006721 public void wallpaperOffsetsComplete(IBinder window) {
6722 WindowManagerService.this.wallpaperOffsetsComplete(window);
6723 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006724
Dianne Hackborn75804932009-10-20 20:15:20 -07006725 public Bundle sendWallpaperCommand(IBinder window, String action, int x, int y,
6726 int z, Bundle extras, boolean sync) {
6727 synchronized(mWindowMap) {
6728 long ident = Binder.clearCallingIdentity();
6729 try {
6730 return sendWindowWallpaperCommandLocked(
6731 windowForClientLocked(this, window),
6732 action, x, y, z, extras, sync);
6733 } finally {
6734 Binder.restoreCallingIdentity(ident);
6735 }
6736 }
6737 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006738
Dianne Hackborn75804932009-10-20 20:15:20 -07006739 public void wallpaperCommandComplete(IBinder window, Bundle result) {
6740 WindowManagerService.this.wallpaperCommandComplete(window, result);
6741 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006742
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006743 void windowAddedLocked() {
6744 if (mSurfaceSession == null) {
6745 if (localLOGV) Log.v(
6746 TAG, "First window added to " + this + ", creating SurfaceSession");
6747 mSurfaceSession = new SurfaceSession();
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006748 if (SHOW_TRANSACTIONS) Log.i(
6749 TAG, " NEW SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006750 mSessions.add(this);
6751 }
6752 mNumWindow++;
6753 }
6754
6755 void windowRemovedLocked() {
6756 mNumWindow--;
6757 killSessionLocked();
6758 }
Romain Guy06882f82009-06-10 13:36:04 -07006759
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006760 void killSessionLocked() {
6761 if (mNumWindow <= 0 && mClientDead) {
6762 mSessions.remove(this);
6763 if (mSurfaceSession != null) {
6764 if (localLOGV) Log.v(
6765 TAG, "Last window removed from " + this
6766 + ", destroying " + mSurfaceSession);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006767 if (SHOW_TRANSACTIONS) Log.i(
6768 TAG, " KILL SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006769 try {
6770 mSurfaceSession.kill();
6771 } catch (Exception e) {
6772 Log.w(TAG, "Exception thrown when killing surface session "
6773 + mSurfaceSession + " in session " + this
6774 + ": " + e.toString());
6775 }
6776 mSurfaceSession = null;
6777 }
6778 }
6779 }
Romain Guy06882f82009-06-10 13:36:04 -07006780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006781 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006782 pw.print(prefix); pw.print("mNumWindow="); pw.print(mNumWindow);
6783 pw.print(" mClientDead="); pw.print(mClientDead);
6784 pw.print(" mSurfaceSession="); pw.println(mSurfaceSession);
6785 if (mPendingPointerWindow != null || mPendingPointerMove != null) {
6786 pw.print(prefix);
6787 pw.print("mPendingPointerWindow="); pw.print(mPendingPointerWindow);
6788 pw.print(" mPendingPointerMove="); pw.println(mPendingPointerMove);
6789 }
6790 if (mPendingTrackballWindow != null || mPendingTrackballMove != null) {
6791 pw.print(prefix);
6792 pw.print("mPendingTrackballWindow="); pw.print(mPendingTrackballWindow);
6793 pw.print(" mPendingTrackballMove="); pw.println(mPendingTrackballMove);
6794 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006795 }
6796
6797 @Override
6798 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006799 return mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006800 }
6801 }
6802
6803 // -------------------------------------------------------------
6804 // Client Window State
6805 // -------------------------------------------------------------
6806
6807 private final class WindowState implements WindowManagerPolicy.WindowState {
6808 final Session mSession;
6809 final IWindow mClient;
6810 WindowToken mToken;
The Android Open Source Project10592532009-03-18 17:39:46 -07006811 WindowToken mRootToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006812 AppWindowToken mAppToken;
6813 AppWindowToken mTargetAppToken;
6814 final WindowManager.LayoutParams mAttrs = new WindowManager.LayoutParams();
6815 final DeathRecipient mDeathRecipient;
6816 final WindowState mAttachedWindow;
6817 final ArrayList mChildWindows = new ArrayList();
6818 final int mBaseLayer;
6819 final int mSubLayer;
6820 final boolean mLayoutAttached;
6821 final boolean mIsImWindow;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006822 final boolean mIsWallpaper;
6823 final boolean mIsFloatingLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006824 int mViewVisibility;
6825 boolean mPolicyVisibility = true;
6826 boolean mPolicyVisibilityAfterAnim = true;
6827 boolean mAppFreezing;
6828 Surface mSurface;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07006829 boolean mReportDestroySurface;
6830 boolean mSurfacePendingDestroy;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006831 boolean mAttachedHidden; // is our parent window hidden?
6832 boolean mLastHidden; // was this window last hidden?
Dianne Hackborn759a39e2009-08-09 17:20:27 -07006833 boolean mWallpaperVisible; // for wallpaper, what was last vis report?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006834 int mRequestedWidth;
6835 int mRequestedHeight;
6836 int mLastRequestedWidth;
6837 int mLastRequestedHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006838 int mLayer;
6839 int mAnimLayer;
6840 int mLastLayer;
6841 boolean mHaveFrame;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07006842 boolean mObscured;
Dianne Hackborn93e462b2009-09-15 22:50:40 -07006843 boolean mTurnOnScreen;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006844
6845 WindowState mNextOutsideTouch;
Romain Guy06882f82009-06-10 13:36:04 -07006846
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006847 // Actual frame shown on-screen (may be modified by animation)
6848 final Rect mShownFrame = new Rect();
6849 final Rect mLastShownFrame = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006850
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006851 /**
6852 * Insets that determine the actually visible area
6853 */
6854 final Rect mVisibleInsets = new Rect();
6855 final Rect mLastVisibleInsets = new Rect();
6856 boolean mVisibleInsetsChanged;
6857
6858 /**
6859 * Insets that are covered by system windows
6860 */
6861 final Rect mContentInsets = new Rect();
6862 final Rect mLastContentInsets = new Rect();
6863 boolean mContentInsetsChanged;
6864
6865 /**
6866 * Set to true if we are waiting for this window to receive its
6867 * given internal insets before laying out other windows based on it.
6868 */
6869 boolean mGivenInsetsPending;
Romain Guy06882f82009-06-10 13:36:04 -07006870
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006871 /**
6872 * These are the content insets that were given during layout for
6873 * this window, to be applied to windows behind it.
6874 */
6875 final Rect mGivenContentInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006876
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006877 /**
6878 * These are the visible insets that were given during layout for
6879 * this window, to be applied to windows behind it.
6880 */
6881 final Rect mGivenVisibleInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006882
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006883 /**
6884 * Flag indicating whether the touchable region should be adjusted by
6885 * the visible insets; if false the area outside the visible insets is
6886 * NOT touchable, so we must use those to adjust the frame during hit
6887 * tests.
6888 */
6889 int mTouchableInsets = ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME;
Romain Guy06882f82009-06-10 13:36:04 -07006890
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006891 // Current transformation being applied.
6892 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
6893 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
6894 float mHScale=1, mVScale=1;
6895 float mLastHScale=1, mLastVScale=1;
6896 final Matrix mTmpMatrix = new Matrix();
6897
6898 // "Real" frame that the application sees.
6899 final Rect mFrame = new Rect();
6900 final Rect mLastFrame = new Rect();
6901
6902 final Rect mContainingFrame = new Rect();
6903 final Rect mDisplayFrame = new Rect();
6904 final Rect mContentFrame = new Rect();
6905 final Rect mVisibleFrame = new Rect();
6906
6907 float mShownAlpha = 1;
6908 float mAlpha = 1;
6909 float mLastAlpha = 1;
6910
6911 // Set to true if, when the window gets displayed, it should perform
6912 // an enter animation.
6913 boolean mEnterAnimationPending;
6914
6915 // Currently running animation.
6916 boolean mAnimating;
6917 boolean mLocalAnimating;
6918 Animation mAnimation;
6919 boolean mAnimationIsEntrance;
6920 boolean mHasTransformation;
6921 boolean mHasLocalTransformation;
6922 final Transformation mTransformation = new Transformation();
6923
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07006924 // If a window showing a wallpaper: the requested offset for the
6925 // wallpaper; if a wallpaper window: the currently applied offset.
6926 float mWallpaperX = -1;
6927 float mWallpaperY = -1;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08006928
6929 // If a window showing a wallpaper: what fraction of the offset
6930 // range corresponds to a full virtual screen.
6931 float mWallpaperXStep = -1;
6932 float mWallpaperYStep = -1;
6933
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07006934 // Wallpaper windows: pixels offset based on above variables.
6935 int mXOffset;
6936 int mYOffset;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006938 // This is set after IWindowSession.relayout() has been called at
6939 // least once for the window. It allows us to detect the situation
6940 // where we don't yet have a surface, but should have one soon, so
6941 // we can give the window focus before waiting for the relayout.
6942 boolean mRelayoutCalled;
Romain Guy06882f82009-06-10 13:36:04 -07006943
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006944 // This is set after the Surface has been created but before the
6945 // window has been drawn. During this time the surface is hidden.
6946 boolean mDrawPending;
6947
6948 // This is set after the window has finished drawing for the first
6949 // time but before its surface is shown. The surface will be
6950 // displayed when the next layout is run.
6951 boolean mCommitDrawPending;
6952
6953 // This is set during the time after the window's drawing has been
6954 // committed, and before its surface is actually shown. It is used
6955 // to delay showing the surface until all windows in a token are ready
6956 // to be shown.
6957 boolean mReadyToShow;
Romain Guy06882f82009-06-10 13:36:04 -07006958
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006959 // Set when the window has been shown in the screen the first time.
6960 boolean mHasDrawn;
6961
6962 // Currently running an exit animation?
6963 boolean mExiting;
6964
6965 // Currently on the mDestroySurface list?
6966 boolean mDestroying;
Romain Guy06882f82009-06-10 13:36:04 -07006967
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006968 // Completely remove from window manager after exit animation?
6969 boolean mRemoveOnExit;
6970
6971 // Set when the orientation is changing and this window has not yet
6972 // been updated for the new orientation.
6973 boolean mOrientationChanging;
Romain Guy06882f82009-06-10 13:36:04 -07006974
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006975 // Is this window now (or just being) removed?
6976 boolean mRemoved;
Romain Guy06882f82009-06-10 13:36:04 -07006977
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006978 WindowState(Session s, IWindow c, WindowToken token,
6979 WindowState attachedWindow, WindowManager.LayoutParams a,
6980 int viewVisibility) {
6981 mSession = s;
6982 mClient = c;
6983 mToken = token;
6984 mAttrs.copyFrom(a);
6985 mViewVisibility = viewVisibility;
6986 DeathRecipient deathRecipient = new DeathRecipient();
6987 mAlpha = a.alpha;
6988 if (localLOGV) Log.v(
6989 TAG, "Window " + this + " client=" + c.asBinder()
6990 + " token=" + token + " (" + mAttrs.token + ")");
6991 try {
6992 c.asBinder().linkToDeath(deathRecipient, 0);
6993 } catch (RemoteException e) {
6994 mDeathRecipient = null;
6995 mAttachedWindow = null;
6996 mLayoutAttached = false;
6997 mIsImWindow = false;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006998 mIsWallpaper = false;
6999 mIsFloatingLayer = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007000 mBaseLayer = 0;
7001 mSubLayer = 0;
7002 return;
7003 }
7004 mDeathRecipient = deathRecipient;
Romain Guy06882f82009-06-10 13:36:04 -07007005
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007006 if ((mAttrs.type >= FIRST_SUB_WINDOW &&
7007 mAttrs.type <= LAST_SUB_WINDOW)) {
7008 // The multiplier here is to reserve space for multiple
7009 // windows in the same type layer.
7010 mBaseLayer = mPolicy.windowTypeToLayerLw(
7011 attachedWindow.mAttrs.type) * TYPE_LAYER_MULTIPLIER
7012 + TYPE_LAYER_OFFSET;
7013 mSubLayer = mPolicy.subWindowTypeToLayerLw(a.type);
7014 mAttachedWindow = attachedWindow;
7015 mAttachedWindow.mChildWindows.add(this);
7016 mLayoutAttached = mAttrs.type !=
7017 WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
7018 mIsImWindow = attachedWindow.mAttrs.type == TYPE_INPUT_METHOD
7019 || attachedWindow.mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007020 mIsWallpaper = attachedWindow.mAttrs.type == TYPE_WALLPAPER;
7021 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007022 } else {
7023 // The multiplier here is to reserve space for multiple
7024 // windows in the same type layer.
7025 mBaseLayer = mPolicy.windowTypeToLayerLw(a.type)
7026 * TYPE_LAYER_MULTIPLIER
7027 + TYPE_LAYER_OFFSET;
7028 mSubLayer = 0;
7029 mAttachedWindow = null;
7030 mLayoutAttached = false;
7031 mIsImWindow = mAttrs.type == TYPE_INPUT_METHOD
7032 || mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007033 mIsWallpaper = mAttrs.type == TYPE_WALLPAPER;
7034 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007035 }
7036
7037 WindowState appWin = this;
7038 while (appWin.mAttachedWindow != null) {
7039 appWin = mAttachedWindow;
7040 }
7041 WindowToken appToken = appWin.mToken;
7042 while (appToken.appWindowToken == null) {
7043 WindowToken parent = mTokenMap.get(appToken.token);
7044 if (parent == null || appToken == parent) {
7045 break;
7046 }
7047 appToken = parent;
7048 }
The Android Open Source Project10592532009-03-18 17:39:46 -07007049 mRootToken = appToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007050 mAppToken = appToken.appWindowToken;
7051
7052 mSurface = null;
7053 mRequestedWidth = 0;
7054 mRequestedHeight = 0;
7055 mLastRequestedWidth = 0;
7056 mLastRequestedHeight = 0;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007057 mXOffset = 0;
7058 mYOffset = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007059 mLayer = 0;
7060 mAnimLayer = 0;
7061 mLastLayer = 0;
7062 }
7063
7064 void attach() {
7065 if (localLOGV) Log.v(
7066 TAG, "Attaching " + this + " token=" + mToken
7067 + ", list=" + mToken.windows);
7068 mSession.windowAddedLocked();
7069 }
7070
7071 public void computeFrameLw(Rect pf, Rect df, Rect cf, Rect vf) {
7072 mHaveFrame = true;
7073
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007074 final Rect container = mContainingFrame;
7075 container.set(pf);
7076
7077 final Rect display = mDisplayFrame;
7078 display.set(df);
7079
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007080 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007081 container.intersect(mCompatibleScreenFrame);
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007082 if ((mAttrs.flags & FLAG_LAYOUT_NO_LIMITS) == 0) {
7083 display.intersect(mCompatibleScreenFrame);
7084 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007085 }
7086
7087 final int pw = container.right - container.left;
7088 final int ph = container.bottom - container.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007089
7090 int w,h;
7091 if ((mAttrs.flags & mAttrs.FLAG_SCALED) != 0) {
7092 w = mAttrs.width < 0 ? pw : mAttrs.width;
7093 h = mAttrs.height< 0 ? ph : mAttrs.height;
7094 } else {
Romain Guy980a9382010-01-08 15:06:28 -08007095 w = mAttrs.width == mAttrs.MATCH_PARENT ? pw : mRequestedWidth;
7096 h = mAttrs.height== mAttrs.MATCH_PARENT ? ph : mRequestedHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007097 }
Romain Guy06882f82009-06-10 13:36:04 -07007098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007099 final Rect content = mContentFrame;
7100 content.set(cf);
Romain Guy06882f82009-06-10 13:36:04 -07007101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007102 final Rect visible = mVisibleFrame;
7103 visible.set(vf);
Romain Guy06882f82009-06-10 13:36:04 -07007104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007105 final Rect frame = mFrame;
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007106 final int fw = frame.width();
7107 final int fh = frame.height();
Romain Guy06882f82009-06-10 13:36:04 -07007108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007109 //System.out.println("In: w=" + w + " h=" + h + " container=" +
7110 // container + " x=" + mAttrs.x + " y=" + mAttrs.y);
7111
7112 Gravity.apply(mAttrs.gravity, w, h, container,
7113 (int) (mAttrs.x + mAttrs.horizontalMargin * pw),
7114 (int) (mAttrs.y + mAttrs.verticalMargin * ph), frame);
7115
7116 //System.out.println("Out: " + mFrame);
7117
7118 // Now make sure the window fits in the overall display.
7119 Gravity.applyDisplay(mAttrs.gravity, df, frame);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007121 // Make sure the content and visible frames are inside of the
7122 // final window frame.
7123 if (content.left < frame.left) content.left = frame.left;
7124 if (content.top < frame.top) content.top = frame.top;
7125 if (content.right > frame.right) content.right = frame.right;
7126 if (content.bottom > frame.bottom) content.bottom = frame.bottom;
7127 if (visible.left < frame.left) visible.left = frame.left;
7128 if (visible.top < frame.top) visible.top = frame.top;
7129 if (visible.right > frame.right) visible.right = frame.right;
7130 if (visible.bottom > frame.bottom) visible.bottom = frame.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007132 final Rect contentInsets = mContentInsets;
7133 contentInsets.left = content.left-frame.left;
7134 contentInsets.top = content.top-frame.top;
7135 contentInsets.right = frame.right-content.right;
7136 contentInsets.bottom = frame.bottom-content.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007138 final Rect visibleInsets = mVisibleInsets;
7139 visibleInsets.left = visible.left-frame.left;
7140 visibleInsets.top = visible.top-frame.top;
7141 visibleInsets.right = frame.right-visible.right;
7142 visibleInsets.bottom = frame.bottom-visible.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007143
Dianne Hackborn284ac932009-08-28 10:34:25 -07007144 if (mIsWallpaper && (fw != frame.width() || fh != frame.height())) {
7145 updateWallpaperOffsetLocked(this, mDisplay.getWidth(),
Dianne Hackborn19382ac2009-09-11 21:13:37 -07007146 mDisplay.getHeight(), false);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007147 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007149 if (localLOGV) {
7150 //if ("com.google.android.youtube".equals(mAttrs.packageName)
7151 // && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
7152 Log.v(TAG, "Resolving (mRequestedWidth="
7153 + mRequestedWidth + ", mRequestedheight="
7154 + mRequestedHeight + ") to" + " (pw=" + pw + ", ph=" + ph
7155 + "): frame=" + mFrame.toShortString()
7156 + " ci=" + contentInsets.toShortString()
7157 + " vi=" + visibleInsets.toShortString());
7158 //}
7159 }
7160 }
Romain Guy06882f82009-06-10 13:36:04 -07007161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007162 public Rect getFrameLw() {
7163 return mFrame;
7164 }
7165
7166 public Rect getShownFrameLw() {
7167 return mShownFrame;
7168 }
7169
7170 public Rect getDisplayFrameLw() {
7171 return mDisplayFrame;
7172 }
7173
7174 public Rect getContentFrameLw() {
7175 return mContentFrame;
7176 }
7177
7178 public Rect getVisibleFrameLw() {
7179 return mVisibleFrame;
7180 }
7181
7182 public boolean getGivenInsetsPendingLw() {
7183 return mGivenInsetsPending;
7184 }
7185
7186 public Rect getGivenContentInsetsLw() {
7187 return mGivenContentInsets;
7188 }
Romain Guy06882f82009-06-10 13:36:04 -07007189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007190 public Rect getGivenVisibleInsetsLw() {
7191 return mGivenVisibleInsets;
7192 }
Romain Guy06882f82009-06-10 13:36:04 -07007193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007194 public WindowManager.LayoutParams getAttrs() {
7195 return mAttrs;
7196 }
7197
7198 public int getSurfaceLayer() {
7199 return mLayer;
7200 }
Romain Guy06882f82009-06-10 13:36:04 -07007201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007202 public IApplicationToken getAppToken() {
7203 return mAppToken != null ? mAppToken.appToken : null;
7204 }
7205
7206 public boolean hasAppShownWindows() {
7207 return mAppToken != null ? mAppToken.firstWindowDrawn : false;
7208 }
7209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007210 public void setAnimation(Animation anim) {
7211 if (localLOGV) Log.v(
7212 TAG, "Setting animation in " + this + ": " + anim);
7213 mAnimating = false;
7214 mLocalAnimating = false;
7215 mAnimation = anim;
7216 mAnimation.restrictDuration(MAX_ANIMATION_DURATION);
7217 mAnimation.scaleCurrentDuration(mWindowAnimationScale);
7218 }
7219
7220 public void clearAnimation() {
7221 if (mAnimation != null) {
7222 mAnimating = true;
7223 mLocalAnimating = false;
7224 mAnimation = null;
7225 }
7226 }
Romain Guy06882f82009-06-10 13:36:04 -07007227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007228 Surface createSurfaceLocked() {
7229 if (mSurface == null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007230 mReportDestroySurface = false;
7231 mSurfacePendingDestroy = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007232 mDrawPending = true;
7233 mCommitDrawPending = false;
7234 mReadyToShow = false;
7235 if (mAppToken != null) {
7236 mAppToken.allDrawn = false;
7237 }
7238
7239 int flags = 0;
Mathias Agopian317a6282009-08-13 17:29:02 -07007240 if (mAttrs.memoryType == MEMORY_TYPE_PUSH_BUFFERS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007241 flags |= Surface.PUSH_BUFFERS;
7242 }
7243
7244 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
7245 flags |= Surface.SECURE;
7246 }
7247 if (DEBUG_VISIBILITY) Log.v(
7248 TAG, "Creating surface in session "
7249 + mSession.mSurfaceSession + " window " + this
7250 + " w=" + mFrame.width()
7251 + " h=" + mFrame.height() + " format="
7252 + mAttrs.format + " flags=" + flags);
7253
7254 int w = mFrame.width();
7255 int h = mFrame.height();
7256 if ((mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
7257 // for a scaled surface, we always want the requested
7258 // size.
7259 w = mRequestedWidth;
7260 h = mRequestedHeight;
7261 }
7262
Romain Guy9825ec62009-10-01 00:58:09 -07007263 // Something is wrong and SurfaceFlinger will not like this,
7264 // try to revert to sane values
7265 if (w <= 0) w = 1;
7266 if (h <= 0) h = 1;
7267
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007268 try {
7269 mSurface = new Surface(
Romain Guy06882f82009-06-10 13:36:04 -07007270 mSession.mSurfaceSession, mSession.mPid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007271 0, w, h, mAttrs.format, flags);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007272 if (SHOW_TRANSACTIONS) Log.i(TAG, " CREATE SURFACE "
7273 + mSurface + " IN SESSION "
7274 + mSession.mSurfaceSession
7275 + ": pid=" + mSession.mPid + " format="
7276 + mAttrs.format + " flags=0x"
7277 + Integer.toHexString(flags));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007278 } catch (Surface.OutOfResourcesException e) {
7279 Log.w(TAG, "OutOfResourcesException creating surface");
7280 reclaimSomeSurfaceMemoryLocked(this, "create");
7281 return null;
7282 } catch (Exception e) {
7283 Log.e(TAG, "Exception creating surface", e);
7284 return null;
7285 }
Romain Guy06882f82009-06-10 13:36:04 -07007286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007287 if (localLOGV) Log.v(
7288 TAG, "Got surface: " + mSurface
7289 + ", set left=" + mFrame.left + " top=" + mFrame.top
7290 + ", animLayer=" + mAnimLayer);
7291 if (SHOW_TRANSACTIONS) {
7292 Log.i(TAG, ">>> OPEN TRANSACTION");
7293 Log.i(TAG, " SURFACE " + mSurface + ": CREATE ("
7294 + mAttrs.getTitle() + ") pos=(" +
7295 mFrame.left + "," + mFrame.top + ") (" +
7296 mFrame.width() + "x" + mFrame.height() + "), layer=" +
7297 mAnimLayer + " HIDE");
7298 }
7299 Surface.openTransaction();
7300 try {
7301 try {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007302 mSurface.setPosition(mFrame.left + mXOffset,
7303 mFrame.top + mYOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007304 mSurface.setLayer(mAnimLayer);
7305 mSurface.hide();
7306 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007307 if (SHOW_TRANSACTIONS) Log.i(TAG, " SURFACE "
7308 + mSurface + ": DITHER");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007309 mSurface.setFlags(Surface.SURFACE_DITHER,
7310 Surface.SURFACE_DITHER);
7311 }
7312 } catch (RuntimeException e) {
7313 Log.w(TAG, "Error creating surface in " + w, e);
7314 reclaimSomeSurfaceMemoryLocked(this, "create-init");
7315 }
7316 mLastHidden = true;
7317 } finally {
7318 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
7319 Surface.closeTransaction();
7320 }
7321 if (localLOGV) Log.v(
7322 TAG, "Created surface " + this);
7323 }
7324 return mSurface;
7325 }
Romain Guy06882f82009-06-10 13:36:04 -07007326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007327 void destroySurfaceLocked() {
7328 // Window is no longer on-screen, so can no longer receive
7329 // key events... if we were waiting for it to finish
7330 // handling a key event, the wait is over!
7331 mKeyWaiter.finishedKey(mSession, mClient, true,
7332 KeyWaiter.RETURN_NOTHING);
7333 mKeyWaiter.releasePendingPointerLocked(mSession);
7334 mKeyWaiter.releasePendingTrackballLocked(mSession);
7335
7336 if (mAppToken != null && this == mAppToken.startingWindow) {
7337 mAppToken.startingDisplayed = false;
7338 }
Romain Guy06882f82009-06-10 13:36:04 -07007339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007340 if (mSurface != null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007341 mDrawPending = false;
7342 mCommitDrawPending = false;
7343 mReadyToShow = false;
7344
7345 int i = mChildWindows.size();
7346 while (i > 0) {
7347 i--;
7348 WindowState c = (WindowState)mChildWindows.get(i);
7349 c.mAttachedHidden = true;
7350 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007351
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007352 if (mReportDestroySurface) {
7353 mReportDestroySurface = false;
7354 mSurfacePendingDestroy = true;
7355 try {
7356 mClient.dispatchGetNewSurface();
7357 // We'll really destroy on the next time around.
7358 return;
7359 } catch (RemoteException e) {
7360 }
7361 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007363 try {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007364 if (DEBUG_VISIBILITY) {
7365 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07007366 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007367 Log.w(TAG, "Window " + this + " destroying surface "
7368 + mSurface + ", session " + mSession, e);
7369 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007370 if (SHOW_TRANSACTIONS) {
7371 RuntimeException ex = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07007372 if (!HIDE_STACK_CRAWLS) ex.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007373 Log.i(TAG, " SURFACE " + mSurface + ": DESTROY ("
7374 + mAttrs.getTitle() + ")", ex);
7375 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007376 mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007377 } catch (RuntimeException e) {
7378 Log.w(TAG, "Exception thrown when destroying Window " + this
7379 + " surface " + mSurface + " session " + mSession
7380 + ": " + e.toString());
7381 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007383 mSurface = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007384 }
7385 }
7386
7387 boolean finishDrawingLocked() {
7388 if (mDrawPending) {
7389 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.v(
7390 TAG, "finishDrawingLocked: " + mSurface);
7391 mCommitDrawPending = true;
7392 mDrawPending = false;
7393 return true;
7394 }
7395 return false;
7396 }
7397
7398 // This must be called while inside a transaction.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007399 boolean commitFinishDrawingLocked(long currentTime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007400 //Log.i(TAG, "commitFinishDrawingLocked: " + mSurface);
7401 if (!mCommitDrawPending) {
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007402 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007403 }
7404 mCommitDrawPending = false;
7405 mReadyToShow = true;
7406 final boolean starting = mAttrs.type == TYPE_APPLICATION_STARTING;
7407 final AppWindowToken atoken = mAppToken;
7408 if (atoken == null || atoken.allDrawn || starting) {
7409 performShowLocked();
7410 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007411 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007412 }
7413
7414 // This must be called while inside a transaction.
7415 boolean performShowLocked() {
7416 if (DEBUG_VISIBILITY) {
7417 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07007418 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007419 Log.v(TAG, "performShow on " + this
7420 + ": readyToShow=" + mReadyToShow + " readyForDisplay=" + isReadyForDisplay()
7421 + " starting=" + (mAttrs.type == TYPE_APPLICATION_STARTING), e);
7422 }
7423 if (mReadyToShow && isReadyForDisplay()) {
7424 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.i(
7425 TAG, " SURFACE " + mSurface + ": SHOW (performShowLocked)");
7426 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + this
7427 + " during animation: policyVis=" + mPolicyVisibility
7428 + " attHidden=" + mAttachedHidden
7429 + " tok.hiddenRequested="
7430 + (mAppToken != null ? mAppToken.hiddenRequested : false)
Dianne Hackborn248b1882009-09-16 16:46:44 -07007431 + " tok.hidden="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007432 + (mAppToken != null ? mAppToken.hidden : false)
7433 + " animating=" + mAnimating
7434 + " tok animating="
7435 + (mAppToken != null ? mAppToken.animating : false));
7436 if (!showSurfaceRobustlyLocked(this)) {
7437 return false;
7438 }
7439 mLastAlpha = -1;
7440 mHasDrawn = true;
7441 mLastHidden = false;
7442 mReadyToShow = false;
7443 enableScreenIfNeededLocked();
7444
7445 applyEnterAnimationLocked(this);
Romain Guy06882f82009-06-10 13:36:04 -07007446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007447 int i = mChildWindows.size();
7448 while (i > 0) {
7449 i--;
7450 WindowState c = (WindowState)mChildWindows.get(i);
7451 if (c.mSurface != null && c.mAttachedHidden) {
7452 c.mAttachedHidden = false;
7453 c.performShowLocked();
Dianne Hackborn9b52a212009-12-11 14:51:35 -08007454 // It hadn't been shown, which means layout not
7455 // performed on it, so now we want to make sure to
7456 // do a layout. If called from within the transaction
7457 // loop, this will cause it to restart with a new
7458 // layout.
7459 mLayoutNeeded = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007460 }
7461 }
Romain Guy06882f82009-06-10 13:36:04 -07007462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007463 if (mAttrs.type != TYPE_APPLICATION_STARTING
7464 && mAppToken != null) {
7465 mAppToken.firstWindowDrawn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007466
Dianne Hackborn248b1882009-09-16 16:46:44 -07007467 if (mAppToken.startingData != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007468 if (DEBUG_STARTING_WINDOW || DEBUG_ANIM) Log.v(TAG,
7469 "Finish starting " + mToken
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007470 + ": first real window is shown, no animation");
Dianne Hackborn248b1882009-09-16 16:46:44 -07007471 // If this initial window is animating, stop it -- we
7472 // will do an animation to reveal it from behind the
7473 // starting window, so there is no need for it to also
7474 // be doing its own stuff.
7475 if (mAnimation != null) {
7476 mAnimation = null;
7477 // Make sure we clean up the animation.
7478 mAnimating = true;
7479 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007480 mFinishedStarting.add(mAppToken);
7481 mH.sendEmptyMessage(H.FINISHED_STARTING);
7482 }
7483 mAppToken.updateReportedVisibilityLocked();
7484 }
7485 }
7486 return true;
7487 }
Romain Guy06882f82009-06-10 13:36:04 -07007488
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007489 // This must be called while inside a transaction. Returns true if
7490 // there is more animation to run.
7491 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
Dianne Hackbornde2606d2009-12-18 16:53:55 -08007492 if (!mDisplayFrozen && mPolicy.isScreenOn()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007493 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07007494
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007495 if (!mDrawPending && !mCommitDrawPending && mAnimation != null) {
7496 mHasTransformation = true;
7497 mHasLocalTransformation = true;
7498 if (!mLocalAnimating) {
7499 if (DEBUG_ANIM) Log.v(
7500 TAG, "Starting animation in " + this +
7501 " @ " + currentTime + ": ww=" + mFrame.width() + " wh=" + mFrame.height() +
7502 " dw=" + dw + " dh=" + dh + " scale=" + mWindowAnimationScale);
7503 mAnimation.initialize(mFrame.width(), mFrame.height(), dw, dh);
7504 mAnimation.setStartTime(currentTime);
7505 mLocalAnimating = true;
7506 mAnimating = true;
7507 }
7508 mTransformation.clear();
7509 final boolean more = mAnimation.getTransformation(
7510 currentTime, mTransformation);
7511 if (DEBUG_ANIM) Log.v(
7512 TAG, "Stepped animation in " + this +
7513 ": more=" + more + ", xform=" + mTransformation);
7514 if (more) {
7515 // we're not done!
7516 return true;
7517 }
7518 if (DEBUG_ANIM) Log.v(
7519 TAG, "Finished animation in " + this +
7520 " @ " + currentTime);
7521 mAnimation = null;
7522 //WindowManagerService.this.dump();
7523 }
7524 mHasLocalTransformation = false;
7525 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppToken != null
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007526 && mAppToken.animation != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007527 // When our app token is animating, we kind-of pretend like
7528 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
7529 // part of this check means that we will only do this if
7530 // our window is not currently exiting, or it is not
7531 // locally animating itself. The idea being that one that
7532 // is exiting and doing a local animation should be removed
7533 // once that animation is done.
7534 mAnimating = true;
7535 mHasTransformation = true;
7536 mTransformation.clear();
7537 return false;
7538 } else if (mHasTransformation) {
7539 // Little trick to get through the path below to act like
7540 // we have finished an animation.
7541 mAnimating = true;
7542 } else if (isAnimating()) {
7543 mAnimating = true;
7544 }
7545 } else if (mAnimation != null) {
7546 // If the display is frozen, and there is a pending animation,
7547 // clear it and make sure we run the cleanup code.
7548 mAnimating = true;
7549 mLocalAnimating = true;
7550 mAnimation = null;
7551 }
Romain Guy06882f82009-06-10 13:36:04 -07007552
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007553 if (!mAnimating && !mLocalAnimating) {
7554 return false;
7555 }
7556
7557 if (DEBUG_ANIM) Log.v(
7558 TAG, "Animation done in " + this + ": exiting=" + mExiting
7559 + ", reportedVisible="
7560 + (mAppToken != null ? mAppToken.reportedVisible : false));
Romain Guy06882f82009-06-10 13:36:04 -07007561
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007562 mAnimating = false;
7563 mLocalAnimating = false;
7564 mAnimation = null;
7565 mAnimLayer = mLayer;
7566 if (mIsImWindow) {
7567 mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007568 } else if (mIsWallpaper) {
7569 mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007570 }
7571 if (DEBUG_LAYERS) Log.v(TAG, "Stepping win " + this
7572 + " anim layer: " + mAnimLayer);
7573 mHasTransformation = false;
7574 mHasLocalTransformation = false;
7575 mPolicyVisibility = mPolicyVisibilityAfterAnim;
Dianne Hackbornf3bea9c2009-12-09 18:26:21 -08007576 if (!mPolicyVisibility) {
7577 // Window is no longer visible -- make sure if we were waiting
7578 // for it to be displayed before enabling the display, that
7579 // we allow the display to be enabled now.
7580 enableScreenIfNeededLocked();
7581 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007582 mTransformation.clear();
7583 if (mHasDrawn
7584 && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
7585 && mAppToken != null
7586 && mAppToken.firstWindowDrawn
7587 && mAppToken.startingData != null) {
7588 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
7589 + mToken + ": first real window done animating");
7590 mFinishedStarting.add(mAppToken);
7591 mH.sendEmptyMessage(H.FINISHED_STARTING);
7592 }
Romain Guy06882f82009-06-10 13:36:04 -07007593
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007594 finishExit();
7595
7596 if (mAppToken != null) {
7597 mAppToken.updateReportedVisibilityLocked();
7598 }
7599
7600 return false;
7601 }
7602
7603 void finishExit() {
7604 if (DEBUG_ANIM) Log.v(
7605 TAG, "finishExit in " + this
7606 + ": exiting=" + mExiting
7607 + " remove=" + mRemoveOnExit
7608 + " windowAnimating=" + isWindowAnimating());
Romain Guy06882f82009-06-10 13:36:04 -07007609
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007610 final int N = mChildWindows.size();
7611 for (int i=0; i<N; i++) {
7612 ((WindowState)mChildWindows.get(i)).finishExit();
7613 }
Romain Guy06882f82009-06-10 13:36:04 -07007614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007615 if (!mExiting) {
7616 return;
7617 }
Romain Guy06882f82009-06-10 13:36:04 -07007618
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007619 if (isWindowAnimating()) {
7620 return;
7621 }
7622
7623 if (localLOGV) Log.v(
7624 TAG, "Exit animation finished in " + this
7625 + ": remove=" + mRemoveOnExit);
7626 if (mSurface != null) {
7627 mDestroySurface.add(this);
7628 mDestroying = true;
7629 if (SHOW_TRANSACTIONS) Log.i(
7630 TAG, " SURFACE " + mSurface + ": HIDE (finishExit)");
7631 try {
7632 mSurface.hide();
7633 } catch (RuntimeException e) {
7634 Log.w(TAG, "Error hiding surface in " + this, e);
7635 }
7636 mLastHidden = true;
7637 mKeyWaiter.releasePendingPointerLocked(mSession);
7638 }
7639 mExiting = false;
7640 if (mRemoveOnExit) {
7641 mPendingRemove.add(this);
7642 mRemoveOnExit = false;
7643 }
7644 }
Romain Guy06882f82009-06-10 13:36:04 -07007645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007646 boolean isIdentityMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
7647 if (dsdx < .99999f || dsdx > 1.00001f) return false;
7648 if (dtdy < .99999f || dtdy > 1.00001f) return false;
7649 if (dtdx < -.000001f || dtdx > .000001f) return false;
7650 if (dsdy < -.000001f || dsdy > .000001f) return false;
7651 return true;
7652 }
Romain Guy06882f82009-06-10 13:36:04 -07007653
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007654 void computeShownFrameLocked() {
7655 final boolean selfTransformation = mHasLocalTransformation;
7656 Transformation attachedTransformation =
7657 (mAttachedWindow != null && mAttachedWindow.mHasLocalTransformation)
7658 ? mAttachedWindow.mTransformation : null;
7659 Transformation appTransformation =
7660 (mAppToken != null && mAppToken.hasTransformation)
7661 ? mAppToken.transformation : null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007662
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007663 // Wallpapers are animated based on the "real" window they
7664 // are currently targeting.
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007665 if (mAttrs.type == TYPE_WALLPAPER && mLowerWallpaperTarget == null
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07007666 && mWallpaperTarget != null) {
Dianne Hackborn5baba162009-09-23 17:01:12 -07007667 if (mWallpaperTarget.mHasLocalTransformation &&
7668 mWallpaperTarget.mAnimation != null &&
7669 !mWallpaperTarget.mAnimation.getDetachWallpaper()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007670 attachedTransformation = mWallpaperTarget.mTransformation;
Dianne Hackborn5baba162009-09-23 17:01:12 -07007671 if (DEBUG_WALLPAPER && attachedTransformation != null) {
7672 Log.v(TAG, "WP target attached xform: " + attachedTransformation);
7673 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007674 }
7675 if (mWallpaperTarget.mAppToken != null &&
Dianne Hackborn5baba162009-09-23 17:01:12 -07007676 mWallpaperTarget.mAppToken.hasTransformation &&
7677 mWallpaperTarget.mAppToken.animation != null &&
7678 !mWallpaperTarget.mAppToken.animation.getDetachWallpaper()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007679 appTransformation = mWallpaperTarget.mAppToken.transformation;
Dianne Hackborn5baba162009-09-23 17:01:12 -07007680 if (DEBUG_WALLPAPER && appTransformation != null) {
7681 Log.v(TAG, "WP target app xform: " + appTransformation);
7682 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007683 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007684 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007686 if (selfTransformation || attachedTransformation != null
7687 || appTransformation != null) {
Romain Guy06882f82009-06-10 13:36:04 -07007688 // cache often used attributes locally
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007689 final Rect frame = mFrame;
7690 final float tmpFloats[] = mTmpFloats;
7691 final Matrix tmpMatrix = mTmpMatrix;
7692
7693 // Compute the desired transformation.
Dianne Hackborn65c23872009-09-18 17:47:02 -07007694 tmpMatrix.setTranslate(0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007695 if (selfTransformation) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007696 tmpMatrix.postConcat(mTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007697 }
Dianne Hackborn65c23872009-09-18 17:47:02 -07007698 tmpMatrix.postTranslate(frame.left, frame.top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007699 if (attachedTransformation != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007700 tmpMatrix.postConcat(attachedTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007701 }
7702 if (appTransformation != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007703 tmpMatrix.postConcat(appTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007704 }
7705
7706 // "convert" it into SurfaceFlinger's format
7707 // (a 2x2 matrix + an offset)
7708 // Here we must not transform the position of the surface
7709 // since it is already included in the transformation.
7710 //Log.i(TAG, "Transform: " + matrix);
Romain Guy06882f82009-06-10 13:36:04 -07007711
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007712 tmpMatrix.getValues(tmpFloats);
7713 mDsDx = tmpFloats[Matrix.MSCALE_X];
7714 mDtDx = tmpFloats[Matrix.MSKEW_X];
7715 mDsDy = tmpFloats[Matrix.MSKEW_Y];
7716 mDtDy = tmpFloats[Matrix.MSCALE_Y];
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007717 int x = (int)tmpFloats[Matrix.MTRANS_X] + mXOffset;
7718 int y = (int)tmpFloats[Matrix.MTRANS_Y] + mYOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007719 int w = frame.width();
7720 int h = frame.height();
7721 mShownFrame.set(x, y, x+w, y+h);
7722
7723 // Now set the alpha... but because our current hardware
7724 // can't do alpha transformation on a non-opaque surface,
7725 // turn it off if we are running an animation that is also
7726 // transforming since it is more important to have that
7727 // animation be smooth.
7728 mShownAlpha = mAlpha;
7729 if (!mLimitedAlphaCompositing
7730 || (!PixelFormat.formatHasAlpha(mAttrs.format)
7731 || (isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
7732 && x == frame.left && y == frame.top))) {
7733 //Log.i(TAG, "Applying alpha transform");
7734 if (selfTransformation) {
7735 mShownAlpha *= mTransformation.getAlpha();
7736 }
7737 if (attachedTransformation != null) {
7738 mShownAlpha *= attachedTransformation.getAlpha();
7739 }
7740 if (appTransformation != null) {
7741 mShownAlpha *= appTransformation.getAlpha();
7742 }
7743 } else {
7744 //Log.i(TAG, "Not applying alpha transform");
7745 }
Romain Guy06882f82009-06-10 13:36:04 -07007746
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007747 if (localLOGV) Log.v(
7748 TAG, "Continuing animation in " + this +
7749 ": " + mShownFrame +
7750 ", alpha=" + mTransformation.getAlpha());
7751 return;
7752 }
Romain Guy06882f82009-06-10 13:36:04 -07007753
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007754 mShownFrame.set(mFrame);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007755 if (mXOffset != 0 || mYOffset != 0) {
7756 mShownFrame.offset(mXOffset, mYOffset);
7757 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007758 mShownAlpha = mAlpha;
7759 mDsDx = 1;
7760 mDtDx = 0;
7761 mDsDy = 0;
7762 mDtDy = 1;
7763 }
Romain Guy06882f82009-06-10 13:36:04 -07007764
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007765 /**
7766 * Is this window visible? It is not visible if there is no
7767 * surface, or we are in the process of running an exit animation
7768 * that will remove the surface, or its app token has been hidden.
7769 */
7770 public boolean isVisibleLw() {
7771 final AppWindowToken atoken = mAppToken;
7772 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7773 && (atoken == null || !atoken.hiddenRequested)
7774 && !mExiting && !mDestroying;
7775 }
7776
7777 /**
Dianne Hackborn3d163f072009-10-07 21:26:57 -07007778 * Like {@link #isVisibleLw}, but also counts a window that is currently
7779 * "hidden" behind the keyguard as visible. This allows us to apply
7780 * things like window flags that impact the keyguard.
7781 * XXX I am starting to think we need to have ANOTHER visibility flag
7782 * for this "hidden behind keyguard" state rather than overloading
7783 * mPolicyVisibility. Ungh.
7784 */
7785 public boolean isVisibleOrBehindKeyguardLw() {
7786 final AppWindowToken atoken = mAppToken;
7787 return mSurface != null && !mAttachedHidden
7788 && (atoken == null ? mPolicyVisibility : !atoken.hiddenRequested)
7789 && !mExiting && !mDestroying;
7790 }
7791
7792 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007793 * Is this window visible, ignoring its app token? It is not visible
7794 * if there is no surface, or we are in the process of running an exit animation
7795 * that will remove the surface.
7796 */
7797 public boolean isWinVisibleLw() {
7798 final AppWindowToken atoken = mAppToken;
7799 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7800 && (atoken == null || !atoken.hiddenRequested || atoken.animating)
7801 && !mExiting && !mDestroying;
7802 }
7803
7804 /**
7805 * The same as isVisible(), but follows the current hidden state of
7806 * the associated app token, not the pending requested hidden state.
7807 */
7808 boolean isVisibleNow() {
7809 return mSurface != null && mPolicyVisibility && !mAttachedHidden
The Android Open Source Project10592532009-03-18 17:39:46 -07007810 && !mRootToken.hidden && !mExiting && !mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007811 }
7812
7813 /**
7814 * Same as isVisible(), but we also count it as visible between the
7815 * call to IWindowSession.add() and the first relayout().
7816 */
7817 boolean isVisibleOrAdding() {
7818 final AppWindowToken atoken = mAppToken;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007819 return ((mSurface != null && !mReportDestroySurface)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007820 || (!mRelayoutCalled && mViewVisibility == View.VISIBLE))
7821 && mPolicyVisibility && !mAttachedHidden
7822 && (atoken == null || !atoken.hiddenRequested)
7823 && !mExiting && !mDestroying;
7824 }
7825
7826 /**
7827 * Is this window currently on-screen? It is on-screen either if it
7828 * is visible or it is currently running an animation before no longer
7829 * being visible.
7830 */
7831 boolean isOnScreen() {
7832 final AppWindowToken atoken = mAppToken;
7833 if (atoken != null) {
7834 return mSurface != null && mPolicyVisibility && !mDestroying
7835 && ((!mAttachedHidden && !atoken.hiddenRequested)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007836 || mAnimation != null || atoken.animation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007837 } else {
7838 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007839 && (!mAttachedHidden || mAnimation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007840 }
7841 }
Romain Guy06882f82009-06-10 13:36:04 -07007842
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007843 /**
7844 * Like isOnScreen(), but we don't return true if the window is part
7845 * of a transition that has not yet been started.
7846 */
7847 boolean isReadyForDisplay() {
Dianne Hackborna8f60182009-09-01 19:01:50 -07007848 if (mRootToken.waitingToShow &&
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07007849 mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07007850 return false;
7851 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007852 final AppWindowToken atoken = mAppToken;
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007853 final boolean animating = atoken != null
7854 ? (atoken.animation != null) : false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007855 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007856 && ((!mAttachedHidden && mViewVisibility == View.VISIBLE
7857 && !mRootToken.hidden)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007858 || mAnimation != null || animating);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007859 }
7860
7861 /** Is the window or its container currently animating? */
7862 boolean isAnimating() {
7863 final WindowState attached = mAttachedWindow;
7864 final AppWindowToken atoken = mAppToken;
7865 return mAnimation != null
7866 || (attached != null && attached.mAnimation != null)
Romain Guy06882f82009-06-10 13:36:04 -07007867 || (atoken != null &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007868 (atoken.animation != null
7869 || atoken.inPendingTransaction));
7870 }
7871
7872 /** Is this window currently animating? */
7873 boolean isWindowAnimating() {
7874 return mAnimation != null;
7875 }
7876
7877 /**
7878 * Like isOnScreen, but returns false if the surface hasn't yet
7879 * been drawn.
7880 */
7881 public boolean isDisplayedLw() {
7882 final AppWindowToken atoken = mAppToken;
7883 return mSurface != null && mPolicyVisibility && !mDestroying
7884 && !mDrawPending && !mCommitDrawPending
7885 && ((!mAttachedHidden &&
7886 (atoken == null || !atoken.hiddenRequested))
7887 || mAnimating);
7888 }
7889
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07007890 /**
7891 * Returns true if the window has a surface that it has drawn a
7892 * complete UI in to.
7893 */
7894 public boolean isDrawnLw() {
7895 final AppWindowToken atoken = mAppToken;
7896 return mSurface != null && !mDestroying
7897 && !mDrawPending && !mCommitDrawPending;
7898 }
7899
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007900 public boolean fillsScreenLw(int screenWidth, int screenHeight,
7901 boolean shownFrame, boolean onlyOpaque) {
7902 if (mSurface == null) {
7903 return false;
7904 }
7905 if (mAppToken != null && !mAppToken.appFullscreen) {
7906 return false;
7907 }
7908 if (onlyOpaque && mAttrs.format != PixelFormat.OPAQUE) {
7909 return false;
7910 }
7911 final Rect frame = shownFrame ? mShownFrame : mFrame;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007912
7913 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
7914 return frame.left <= mCompatibleScreenFrame.left &&
7915 frame.top <= mCompatibleScreenFrame.top &&
7916 frame.right >= mCompatibleScreenFrame.right &&
7917 frame.bottom >= mCompatibleScreenFrame.bottom;
7918 } else {
7919 return frame.left <= 0 && frame.top <= 0
7920 && frame.right >= screenWidth
7921 && frame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007922 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007923 }
Romain Guy06882f82009-06-10 13:36:04 -07007924
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007925 /**
Dianne Hackborn25994b42009-09-04 14:21:19 -07007926 * Return true if the window is opaque and fully drawn. This indicates
7927 * it may obscure windows behind it.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007928 */
7929 boolean isOpaqueDrawn() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07007930 return (mAttrs.format == PixelFormat.OPAQUE
7931 || mAttrs.type == TYPE_WALLPAPER)
7932 && mSurface != null && mAnimation == null
7933 && (mAppToken == null || mAppToken.animation == null)
7934 && !mDrawPending && !mCommitDrawPending;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007935 }
7936
7937 boolean needsBackgroundFiller(int screenWidth, int screenHeight) {
7938 return
7939 // only if the application is requesting compatible window
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007940 (mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0 &&
7941 // only if it's visible
7942 mHasDrawn && mViewVisibility == View.VISIBLE &&
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007943 // and only if the application fills the compatible screen
7944 mFrame.left <= mCompatibleScreenFrame.left &&
7945 mFrame.top <= mCompatibleScreenFrame.top &&
7946 mFrame.right >= mCompatibleScreenFrame.right &&
7947 mFrame.bottom >= mCompatibleScreenFrame.bottom &&
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007948 // and starting window do not need background filler
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007949 mAttrs.type != mAttrs.TYPE_APPLICATION_STARTING;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007950 }
7951
7952 boolean isFullscreen(int screenWidth, int screenHeight) {
7953 return mFrame.left <= 0 && mFrame.top <= 0 &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007954 mFrame.right >= screenWidth && mFrame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007955 }
7956
7957 void removeLocked() {
7958 if (mAttachedWindow != null) {
7959 mAttachedWindow.mChildWindows.remove(this);
7960 }
7961 destroySurfaceLocked();
7962 mSession.windowRemovedLocked();
7963 try {
7964 mClient.asBinder().unlinkToDeath(mDeathRecipient, 0);
7965 } catch (RuntimeException e) {
7966 // Ignore if it has already been removed (usually because
7967 // we are doing this as part of processing a death note.)
7968 }
7969 }
7970
7971 private class DeathRecipient implements IBinder.DeathRecipient {
7972 public void binderDied() {
7973 try {
7974 synchronized(mWindowMap) {
7975 WindowState win = windowForClientLocked(mSession, mClient);
7976 Log.i(TAG, "WIN DEATH: " + win);
7977 if (win != null) {
7978 removeWindowLocked(mSession, win);
7979 }
7980 }
7981 } catch (IllegalArgumentException ex) {
7982 // This will happen if the window has already been
7983 // removed.
7984 }
7985 }
7986 }
7987
7988 /** Returns true if this window desires key events. */
7989 public final boolean canReceiveKeys() {
7990 return isVisibleOrAdding()
7991 && (mViewVisibility == View.VISIBLE)
7992 && ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0);
7993 }
7994
7995 public boolean hasDrawnLw() {
7996 return mHasDrawn;
7997 }
7998
7999 public boolean showLw(boolean doAnimation) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008000 return showLw(doAnimation, true);
8001 }
8002
8003 boolean showLw(boolean doAnimation, boolean requestAnim) {
8004 if (mPolicyVisibility && mPolicyVisibilityAfterAnim) {
8005 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008006 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008007 mPolicyVisibility = true;
8008 mPolicyVisibilityAfterAnim = true;
8009 if (doAnimation) {
8010 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_ENTER, true);
8011 }
8012 if (requestAnim) {
8013 requestAnimationLocked(0);
8014 }
8015 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008016 }
8017
8018 public boolean hideLw(boolean doAnimation) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008019 return hideLw(doAnimation, true);
8020 }
8021
8022 boolean hideLw(boolean doAnimation, boolean requestAnim) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008023 boolean current = doAnimation ? mPolicyVisibilityAfterAnim
8024 : mPolicyVisibility;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008025 if (!current) {
8026 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008027 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008028 if (doAnimation) {
8029 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_EXIT, false);
8030 if (mAnimation == null) {
8031 doAnimation = false;
8032 }
8033 }
8034 if (doAnimation) {
8035 mPolicyVisibilityAfterAnim = false;
8036 } else {
8037 mPolicyVisibilityAfterAnim = false;
8038 mPolicyVisibility = false;
Dianne Hackbornf3bea9c2009-12-09 18:26:21 -08008039 // Window is no longer visible -- make sure if we were waiting
8040 // for it to be displayed before enabling the display, that
8041 // we allow the display to be enabled now.
8042 enableScreenIfNeededLocked();
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008043 }
8044 if (requestAnim) {
8045 requestAnimationLocked(0);
8046 }
8047 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008048 }
8049
8050 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008051 StringBuilder sb = new StringBuilder(64);
Romain Guy06882f82009-06-10 13:36:04 -07008052
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008053 pw.print(prefix); pw.print("mSession="); pw.print(mSession);
8054 pw.print(" mClient="); pw.println(mClient.asBinder());
8055 pw.print(prefix); pw.print("mAttrs="); pw.println(mAttrs);
8056 if (mAttachedWindow != null || mLayoutAttached) {
8057 pw.print(prefix); pw.print("mAttachedWindow="); pw.print(mAttachedWindow);
8058 pw.print(" mLayoutAttached="); pw.println(mLayoutAttached);
8059 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07008060 if (mIsImWindow || mIsWallpaper || mIsFloatingLayer) {
8061 pw.print(prefix); pw.print("mIsImWindow="); pw.print(mIsImWindow);
8062 pw.print(" mIsWallpaper="); pw.print(mIsWallpaper);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008063 pw.print(" mIsFloatingLayer="); pw.print(mIsFloatingLayer);
8064 pw.print(" mWallpaperVisible="); pw.println(mWallpaperVisible);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008065 }
8066 pw.print(prefix); pw.print("mBaseLayer="); pw.print(mBaseLayer);
8067 pw.print(" mSubLayer="); pw.print(mSubLayer);
8068 pw.print(" mAnimLayer="); pw.print(mLayer); pw.print("+");
8069 pw.print((mTargetAppToken != null ? mTargetAppToken.animLayerAdjustment
8070 : (mAppToken != null ? mAppToken.animLayerAdjustment : 0)));
8071 pw.print("="); pw.print(mAnimLayer);
8072 pw.print(" mLastLayer="); pw.println(mLastLayer);
8073 if (mSurface != null) {
8074 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
8075 }
8076 pw.print(prefix); pw.print("mToken="); pw.println(mToken);
8077 pw.print(prefix); pw.print("mRootToken="); pw.println(mRootToken);
8078 if (mAppToken != null) {
8079 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
8080 }
8081 if (mTargetAppToken != null) {
8082 pw.print(prefix); pw.print("mTargetAppToken="); pw.println(mTargetAppToken);
8083 }
8084 pw.print(prefix); pw.print("mViewVisibility=0x");
8085 pw.print(Integer.toHexString(mViewVisibility));
8086 pw.print(" mLastHidden="); pw.print(mLastHidden);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07008087 pw.print(" mHaveFrame="); pw.print(mHaveFrame);
8088 pw.print(" mObscured="); pw.println(mObscured);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008089 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim || mAttachedHidden) {
8090 pw.print(prefix); pw.print("mPolicyVisibility=");
8091 pw.print(mPolicyVisibility);
8092 pw.print(" mPolicyVisibilityAfterAnim=");
8093 pw.print(mPolicyVisibilityAfterAnim);
8094 pw.print(" mAttachedHidden="); pw.println(mAttachedHidden);
8095 }
Dianne Hackborn9b52a212009-12-11 14:51:35 -08008096 if (!mRelayoutCalled) {
8097 pw.print(prefix); pw.print("mRelayoutCalled="); pw.println(mRelayoutCalled);
8098 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008099 pw.print(prefix); pw.print("Requested w="); pw.print(mRequestedWidth);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008100 pw.print(" h="); pw.println(mRequestedHeight);
8101 if (mXOffset != 0 || mYOffset != 0) {
8102 pw.print(prefix); pw.print("Offsets x="); pw.print(mXOffset);
8103 pw.print(" y="); pw.println(mYOffset);
8104 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008105 pw.print(prefix); pw.print("mGivenContentInsets=");
8106 mGivenContentInsets.printShortString(pw);
8107 pw.print(" mGivenVisibleInsets=");
8108 mGivenVisibleInsets.printShortString(pw);
8109 pw.println();
8110 if (mTouchableInsets != 0 || mGivenInsetsPending) {
8111 pw.print(prefix); pw.print("mTouchableInsets="); pw.print(mTouchableInsets);
8112 pw.print(" mGivenInsetsPending="); pw.println(mGivenInsetsPending);
8113 }
8114 pw.print(prefix); pw.print("mShownFrame=");
8115 mShownFrame.printShortString(pw);
8116 pw.print(" last="); mLastShownFrame.printShortString(pw);
8117 pw.println();
8118 pw.print(prefix); pw.print("mFrame="); mFrame.printShortString(pw);
8119 pw.print(" last="); mLastFrame.printShortString(pw);
8120 pw.println();
8121 pw.print(prefix); pw.print("mContainingFrame=");
8122 mContainingFrame.printShortString(pw);
8123 pw.print(" mDisplayFrame=");
8124 mDisplayFrame.printShortString(pw);
8125 pw.println();
8126 pw.print(prefix); pw.print("mContentFrame="); mContentFrame.printShortString(pw);
8127 pw.print(" mVisibleFrame="); mVisibleFrame.printShortString(pw);
8128 pw.println();
8129 pw.print(prefix); pw.print("mContentInsets="); mContentInsets.printShortString(pw);
8130 pw.print(" last="); mLastContentInsets.printShortString(pw);
8131 pw.print(" mVisibleInsets="); mVisibleInsets.printShortString(pw);
8132 pw.print(" last="); mLastVisibleInsets.printShortString(pw);
8133 pw.println();
8134 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
8135 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
8136 pw.print(" mAlpha="); pw.print(mAlpha);
8137 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
8138 }
8139 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
8140 || mAnimation != null) {
8141 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
8142 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
8143 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
8144 pw.print(" mAnimation="); pw.println(mAnimation);
8145 }
8146 if (mHasTransformation || mHasLocalTransformation) {
8147 pw.print(prefix); pw.print("XForm: has=");
8148 pw.print(mHasTransformation);
8149 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
8150 pw.print(" "); mTransformation.printShortString(pw);
8151 pw.println();
8152 }
8153 pw.print(prefix); pw.print("mDrawPending="); pw.print(mDrawPending);
8154 pw.print(" mCommitDrawPending="); pw.print(mCommitDrawPending);
8155 pw.print(" mReadyToShow="); pw.print(mReadyToShow);
8156 pw.print(" mHasDrawn="); pw.println(mHasDrawn);
8157 if (mExiting || mRemoveOnExit || mDestroying || mRemoved) {
8158 pw.print(prefix); pw.print("mExiting="); pw.print(mExiting);
8159 pw.print(" mRemoveOnExit="); pw.print(mRemoveOnExit);
8160 pw.print(" mDestroying="); pw.print(mDestroying);
8161 pw.print(" mRemoved="); pw.println(mRemoved);
8162 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07008163 if (mOrientationChanging || mAppFreezing || mTurnOnScreen) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008164 pw.print(prefix); pw.print("mOrientationChanging=");
8165 pw.print(mOrientationChanging);
Dianne Hackborn93e462b2009-09-15 22:50:40 -07008166 pw.print(" mAppFreezing="); pw.print(mAppFreezing);
8167 pw.print(" mTurnOnScreen="); pw.println(mTurnOnScreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008168 }
Mitsuru Oshima589cebe2009-07-22 20:38:58 -07008169 if (mHScale != 1 || mVScale != 1) {
8170 pw.print(prefix); pw.print("mHScale="); pw.print(mHScale);
8171 pw.print(" mVScale="); pw.println(mVScale);
8172 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07008173 if (mWallpaperX != -1 || mWallpaperY != -1) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008174 pw.print(prefix); pw.print("mWallpaperX="); pw.print(mWallpaperX);
8175 pw.print(" mWallpaperY="); pw.println(mWallpaperY);
8176 }
Marco Nelissenbf6956b2009-11-09 15:21:13 -08008177 if (mWallpaperXStep != -1 || mWallpaperYStep != -1) {
8178 pw.print(prefix); pw.print("mWallpaperXStep="); pw.print(mWallpaperXStep);
8179 pw.print(" mWallpaperYStep="); pw.println(mWallpaperYStep);
8180 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008181 }
8182
8183 @Override
8184 public String toString() {
8185 return "Window{"
8186 + Integer.toHexString(System.identityHashCode(this))
8187 + " " + mAttrs.getTitle() + " paused=" + mToken.paused + "}";
8188 }
8189 }
Romain Guy06882f82009-06-10 13:36:04 -07008190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008191 // -------------------------------------------------------------
8192 // Window Token State
8193 // -------------------------------------------------------------
8194
8195 class WindowToken {
8196 // The actual token.
8197 final IBinder token;
8198
8199 // The type of window this token is for, as per WindowManager.LayoutParams.
8200 final int windowType;
Romain Guy06882f82009-06-10 13:36:04 -07008201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008202 // Set if this token was explicitly added by a client, so should
8203 // not be removed when all windows are removed.
8204 final boolean explicit;
Romain Guy06882f82009-06-10 13:36:04 -07008205
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008206 // For printing.
8207 String stringName;
Romain Guy06882f82009-06-10 13:36:04 -07008208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008209 // If this is an AppWindowToken, this is non-null.
8210 AppWindowToken appWindowToken;
Romain Guy06882f82009-06-10 13:36:04 -07008211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008212 // All of the windows associated with this token.
8213 final ArrayList<WindowState> windows = new ArrayList<WindowState>();
8214
8215 // Is key dispatching paused for this token?
8216 boolean paused = false;
8217
8218 // Should this token's windows be hidden?
8219 boolean hidden;
8220
8221 // Temporary for finding which tokens no longer have visible windows.
8222 boolean hasVisible;
8223
Dianne Hackborna8f60182009-09-01 19:01:50 -07008224 // Set to true when this token is in a pending transaction where it
8225 // will be shown.
8226 boolean waitingToShow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008227
Dianne Hackborna8f60182009-09-01 19:01:50 -07008228 // Set to true when this token is in a pending transaction where it
8229 // will be hidden.
8230 boolean waitingToHide;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008231
Dianne Hackborna8f60182009-09-01 19:01:50 -07008232 // Set to true when this token is in a pending transaction where its
8233 // windows will be put to the bottom of the list.
8234 boolean sendingToBottom;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008235
Dianne Hackborna8f60182009-09-01 19:01:50 -07008236 // Set to true when this token is in a pending transaction where its
8237 // windows will be put to the top of the list.
8238 boolean sendingToTop;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008240 WindowToken(IBinder _token, int type, boolean _explicit) {
8241 token = _token;
8242 windowType = type;
8243 explicit = _explicit;
8244 }
8245
8246 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008247 pw.print(prefix); pw.print("token="); pw.println(token);
8248 pw.print(prefix); pw.print("windows="); pw.println(windows);
8249 pw.print(prefix); pw.print("windowType="); pw.print(windowType);
8250 pw.print(" hidden="); pw.print(hidden);
8251 pw.print(" hasVisible="); pw.println(hasVisible);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008252 if (waitingToShow || waitingToHide || sendingToBottom || sendingToTop) {
8253 pw.print(prefix); pw.print("waitingToShow="); pw.print(waitingToShow);
8254 pw.print(" waitingToHide="); pw.print(waitingToHide);
8255 pw.print(" sendingToBottom="); pw.print(sendingToBottom);
8256 pw.print(" sendingToTop="); pw.println(sendingToTop);
8257 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008258 }
8259
8260 @Override
8261 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008262 if (stringName == null) {
8263 StringBuilder sb = new StringBuilder();
8264 sb.append("WindowToken{");
8265 sb.append(Integer.toHexString(System.identityHashCode(this)));
8266 sb.append(" token="); sb.append(token); sb.append('}');
8267 stringName = sb.toString();
8268 }
8269 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008270 }
8271 };
8272
8273 class AppWindowToken extends WindowToken {
8274 // Non-null only for application tokens.
8275 final IApplicationToken appToken;
8276
8277 // All of the windows and child windows that are included in this
8278 // application token. Note this list is NOT sorted!
8279 final ArrayList<WindowState> allAppWindows = new ArrayList<WindowState>();
8280
8281 int groupId = -1;
8282 boolean appFullscreen;
8283 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Romain Guy06882f82009-06-10 13:36:04 -07008284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008285 // These are used for determining when all windows associated with
8286 // an activity have been drawn, so they can be made visible together
8287 // at the same time.
8288 int lastTransactionSequence = mTransactionSequence-1;
8289 int numInterestingWindows;
8290 int numDrawnWindows;
8291 boolean inPendingTransaction;
8292 boolean allDrawn;
Romain Guy06882f82009-06-10 13:36:04 -07008293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008294 // Is this token going to be hidden in a little while? If so, it
8295 // won't be taken into account for setting the screen orientation.
8296 boolean willBeHidden;
Romain Guy06882f82009-06-10 13:36:04 -07008297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008298 // Is this window's surface needed? This is almost like hidden, except
8299 // it will sometimes be true a little earlier: when the token has
8300 // been shown, but is still waiting for its app transition to execute
8301 // before making its windows shown.
8302 boolean hiddenRequested;
Romain Guy06882f82009-06-10 13:36:04 -07008303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008304 // Have we told the window clients to hide themselves?
8305 boolean clientHidden;
Romain Guy06882f82009-06-10 13:36:04 -07008306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008307 // Last visibility state we reported to the app token.
8308 boolean reportedVisible;
8309
8310 // Set to true when the token has been removed from the window mgr.
8311 boolean removed;
8312
8313 // Have we been asked to have this token keep the screen frozen?
8314 boolean freezingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07008315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008316 boolean animating;
8317 Animation animation;
8318 boolean hasTransformation;
8319 final Transformation transformation = new Transformation();
Romain Guy06882f82009-06-10 13:36:04 -07008320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008321 // Offset to the window of all layers in the token, for use by
8322 // AppWindowToken animations.
8323 int animLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -07008324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008325 // Information about an application starting window if displayed.
8326 StartingData startingData;
8327 WindowState startingWindow;
8328 View startingView;
8329 boolean startingDisplayed;
8330 boolean startingMoved;
8331 boolean firstWindowDrawn;
8332
8333 AppWindowToken(IApplicationToken _token) {
8334 super(_token.asBinder(),
8335 WindowManager.LayoutParams.TYPE_APPLICATION, true);
8336 appWindowToken = this;
8337 appToken = _token;
8338 }
Romain Guy06882f82009-06-10 13:36:04 -07008339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008340 public void setAnimation(Animation anim) {
8341 if (localLOGV) Log.v(
8342 TAG, "Setting animation in " + this + ": " + anim);
8343 animation = anim;
8344 animating = false;
8345 anim.restrictDuration(MAX_ANIMATION_DURATION);
8346 anim.scaleCurrentDuration(mTransitionAnimationScale);
8347 int zorder = anim.getZAdjustment();
8348 int adj = 0;
8349 if (zorder == Animation.ZORDER_TOP) {
8350 adj = TYPE_LAYER_OFFSET;
8351 } else if (zorder == Animation.ZORDER_BOTTOM) {
8352 adj = -TYPE_LAYER_OFFSET;
8353 }
Romain Guy06882f82009-06-10 13:36:04 -07008354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008355 if (animLayerAdjustment != adj) {
8356 animLayerAdjustment = adj;
8357 updateLayers();
8358 }
8359 }
Romain Guy06882f82009-06-10 13:36:04 -07008360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008361 public void setDummyAnimation() {
8362 if (animation == null) {
8363 if (localLOGV) Log.v(
8364 TAG, "Setting dummy animation in " + this);
8365 animation = sDummyAnimation;
8366 }
8367 }
8368
8369 public void clearAnimation() {
8370 if (animation != null) {
8371 animation = null;
8372 animating = true;
8373 }
8374 }
Romain Guy06882f82009-06-10 13:36:04 -07008375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008376 void updateLayers() {
8377 final int N = allAppWindows.size();
8378 final int adj = animLayerAdjustment;
8379 for (int i=0; i<N; i++) {
8380 WindowState w = allAppWindows.get(i);
8381 w.mAnimLayer = w.mLayer + adj;
8382 if (DEBUG_LAYERS) Log.v(TAG, "Updating layer " + w + ": "
8383 + w.mAnimLayer);
8384 if (w == mInputMethodTarget) {
8385 setInputMethodAnimLayerAdjustment(adj);
8386 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008387 if (w == mWallpaperTarget && mLowerWallpaperTarget == null) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008388 setWallpaperAnimLayerAdjustmentLocked(adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008389 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008390 }
8391 }
Romain Guy06882f82009-06-10 13:36:04 -07008392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008393 void sendAppVisibilityToClients() {
8394 final int N = allAppWindows.size();
8395 for (int i=0; i<N; i++) {
8396 WindowState win = allAppWindows.get(i);
8397 if (win == startingWindow && clientHidden) {
8398 // Don't hide the starting window.
8399 continue;
8400 }
8401 try {
8402 if (DEBUG_VISIBILITY) Log.v(TAG,
8403 "Setting visibility of " + win + ": " + (!clientHidden));
8404 win.mClient.dispatchAppVisibility(!clientHidden);
8405 } catch (RemoteException e) {
8406 }
8407 }
8408 }
Romain Guy06882f82009-06-10 13:36:04 -07008409
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008410 void showAllWindowsLocked() {
8411 final int NW = allAppWindows.size();
8412 for (int i=0; i<NW; i++) {
8413 WindowState w = allAppWindows.get(i);
8414 if (DEBUG_VISIBILITY) Log.v(TAG,
8415 "performing show on: " + w);
8416 w.performShowLocked();
8417 }
8418 }
Romain Guy06882f82009-06-10 13:36:04 -07008419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008420 // This must be called while inside a transaction.
8421 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
Dianne Hackbornde2606d2009-12-18 16:53:55 -08008422 if (!mDisplayFrozen && mPolicy.isScreenOn()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008423 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07008424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008425 if (animation == sDummyAnimation) {
8426 // This guy is going to animate, but not yet. For now count
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008427 // it as not animating for purposes of scheduling transactions;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008428 // when it is really time to animate, this will be set to
8429 // a real animation and the next call will execute normally.
8430 return false;
8431 }
Romain Guy06882f82009-06-10 13:36:04 -07008432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008433 if ((allDrawn || animating || startingDisplayed) && animation != null) {
8434 if (!animating) {
8435 if (DEBUG_ANIM) Log.v(
8436 TAG, "Starting animation in " + this +
8437 " @ " + currentTime + ": dw=" + dw + " dh=" + dh
8438 + " scale=" + mTransitionAnimationScale
8439 + " allDrawn=" + allDrawn + " animating=" + animating);
8440 animation.initialize(dw, dh, dw, dh);
8441 animation.setStartTime(currentTime);
8442 animating = true;
8443 }
8444 transformation.clear();
8445 final boolean more = animation.getTransformation(
8446 currentTime, transformation);
8447 if (DEBUG_ANIM) Log.v(
8448 TAG, "Stepped animation in " + this +
8449 ": more=" + more + ", xform=" + transformation);
8450 if (more) {
8451 // we're done!
8452 hasTransformation = true;
8453 return true;
8454 }
8455 if (DEBUG_ANIM) Log.v(
8456 TAG, "Finished animation in " + this +
8457 " @ " + currentTime);
8458 animation = null;
8459 }
8460 } else if (animation != null) {
8461 // If the display is frozen, and there is a pending animation,
8462 // clear it and make sure we run the cleanup code.
8463 animating = true;
8464 animation = null;
8465 }
8466
8467 hasTransformation = false;
Romain Guy06882f82009-06-10 13:36:04 -07008468
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008469 if (!animating) {
8470 return false;
8471 }
8472
8473 clearAnimation();
8474 animating = false;
8475 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == this) {
8476 moveInputMethodWindowsIfNeededLocked(true);
8477 }
Romain Guy06882f82009-06-10 13:36:04 -07008478
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008479 if (DEBUG_ANIM) Log.v(
8480 TAG, "Animation done in " + this
8481 + ": reportedVisible=" + reportedVisible);
8482
8483 transformation.clear();
8484 if (animLayerAdjustment != 0) {
8485 animLayerAdjustment = 0;
8486 updateLayers();
8487 }
Romain Guy06882f82009-06-10 13:36:04 -07008488
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008489 final int N = windows.size();
8490 for (int i=0; i<N; i++) {
8491 ((WindowState)windows.get(i)).finishExit();
8492 }
8493 updateReportedVisibilityLocked();
Romain Guy06882f82009-06-10 13:36:04 -07008494
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008495 return false;
8496 }
8497
8498 void updateReportedVisibilityLocked() {
8499 if (appToken == null) {
8500 return;
8501 }
Romain Guy06882f82009-06-10 13:36:04 -07008502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008503 int numInteresting = 0;
8504 int numVisible = 0;
8505 boolean nowGone = true;
Romain Guy06882f82009-06-10 13:36:04 -07008506
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008507 if (DEBUG_VISIBILITY) Log.v(TAG, "Update reported visibility: " + this);
8508 final int N = allAppWindows.size();
8509 for (int i=0; i<N; i++) {
8510 WindowState win = allAppWindows.get(i);
Dianne Hackborn6cf67fa2009-12-21 16:46:34 -08008511 if (win == startingWindow || win.mAppFreezing
8512 || win.mViewVisibility != View.VISIBLE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008513 continue;
8514 }
8515 if (DEBUG_VISIBILITY) {
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008516 Log.v(TAG, "Win " + win + ": isDrawn="
8517 + win.isDrawnLw()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008518 + ", isAnimating=" + win.isAnimating());
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008519 if (!win.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008520 Log.v(TAG, "Not displayed: s=" + win.mSurface
8521 + " pv=" + win.mPolicyVisibility
8522 + " dp=" + win.mDrawPending
8523 + " cdp=" + win.mCommitDrawPending
8524 + " ah=" + win.mAttachedHidden
8525 + " th="
8526 + (win.mAppToken != null
8527 ? win.mAppToken.hiddenRequested : false)
8528 + " a=" + win.mAnimating);
8529 }
8530 }
8531 numInteresting++;
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008532 if (win.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008533 if (!win.isAnimating()) {
8534 numVisible++;
8535 }
8536 nowGone = false;
8537 } else if (win.isAnimating()) {
8538 nowGone = false;
8539 }
8540 }
Romain Guy06882f82009-06-10 13:36:04 -07008541
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008542 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
8543 if (DEBUG_VISIBILITY) Log.v(TAG, "VIS " + this + ": interesting="
8544 + numInteresting + " visible=" + numVisible);
8545 if (nowVisible != reportedVisible) {
8546 if (DEBUG_VISIBILITY) Log.v(
8547 TAG, "Visibility changed in " + this
8548 + ": vis=" + nowVisible);
8549 reportedVisible = nowVisible;
8550 Message m = mH.obtainMessage(
8551 H.REPORT_APPLICATION_TOKEN_WINDOWS,
8552 nowVisible ? 1 : 0,
8553 nowGone ? 1 : 0,
8554 this);
8555 mH.sendMessage(m);
8556 }
8557 }
Romain Guy06882f82009-06-10 13:36:04 -07008558
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07008559 WindowState findMainWindow() {
8560 int j = windows.size();
8561 while (j > 0) {
8562 j--;
8563 WindowState win = windows.get(j);
8564 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
8565 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
8566 return win;
8567 }
8568 }
8569 return null;
8570 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008572 void dump(PrintWriter pw, String prefix) {
8573 super.dump(pw, prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008574 if (appToken != null) {
8575 pw.print(prefix); pw.println("app=true");
8576 }
8577 if (allAppWindows.size() > 0) {
8578 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
8579 }
8580 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008581 pw.print(" appFullscreen="); pw.print(appFullscreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008582 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
8583 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
8584 pw.print(" clientHidden="); pw.print(clientHidden);
8585 pw.print(" willBeHidden="); pw.print(willBeHidden);
8586 pw.print(" reportedVisible="); pw.println(reportedVisible);
8587 if (paused || freezingScreen) {
8588 pw.print(prefix); pw.print("paused="); pw.print(paused);
8589 pw.print(" freezingScreen="); pw.println(freezingScreen);
8590 }
8591 if (numInterestingWindows != 0 || numDrawnWindows != 0
8592 || inPendingTransaction || allDrawn) {
8593 pw.print(prefix); pw.print("numInterestingWindows=");
8594 pw.print(numInterestingWindows);
8595 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
8596 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
8597 pw.print(" allDrawn="); pw.println(allDrawn);
8598 }
8599 if (animating || animation != null) {
8600 pw.print(prefix); pw.print("animating="); pw.print(animating);
8601 pw.print(" animation="); pw.println(animation);
8602 }
8603 if (animLayerAdjustment != 0) {
8604 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
8605 }
8606 if (hasTransformation) {
8607 pw.print(prefix); pw.print("hasTransformation="); pw.print(hasTransformation);
8608 pw.print(" transformation="); transformation.printShortString(pw);
8609 pw.println();
8610 }
8611 if (startingData != null || removed || firstWindowDrawn) {
8612 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
8613 pw.print(" removed="); pw.print(removed);
8614 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
8615 }
8616 if (startingWindow != null || startingView != null
8617 || startingDisplayed || startingMoved) {
8618 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
8619 pw.print(" startingView="); pw.print(startingView);
8620 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
8621 pw.print(" startingMoved"); pw.println(startingMoved);
8622 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008623 }
8624
8625 @Override
8626 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008627 if (stringName == null) {
8628 StringBuilder sb = new StringBuilder();
8629 sb.append("AppWindowToken{");
8630 sb.append(Integer.toHexString(System.identityHashCode(this)));
8631 sb.append(" token="); sb.append(token); sb.append('}');
8632 stringName = sb.toString();
8633 }
8634 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008635 }
8636 }
Romain Guy06882f82009-06-10 13:36:04 -07008637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008638 // -------------------------------------------------------------
8639 // DummyAnimation
8640 // -------------------------------------------------------------
8641
8642 // This is an animation that does nothing: it just immediately finishes
8643 // itself every time it is called. It is used as a stub animation in cases
8644 // where we want to synchronize multiple things that may be animating.
8645 static final class DummyAnimation extends Animation {
8646 public boolean getTransformation(long currentTime, Transformation outTransformation) {
8647 return false;
8648 }
8649 }
8650 static final Animation sDummyAnimation = new DummyAnimation();
Romain Guy06882f82009-06-10 13:36:04 -07008651
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008652 // -------------------------------------------------------------
8653 // Async Handler
8654 // -------------------------------------------------------------
8655
8656 static final class StartingData {
8657 final String pkg;
8658 final int theme;
8659 final CharSequence nonLocalizedLabel;
8660 final int labelRes;
8661 final int icon;
Romain Guy06882f82009-06-10 13:36:04 -07008662
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008663 StartingData(String _pkg, int _theme, CharSequence _nonLocalizedLabel,
8664 int _labelRes, int _icon) {
8665 pkg = _pkg;
8666 theme = _theme;
8667 nonLocalizedLabel = _nonLocalizedLabel;
8668 labelRes = _labelRes;
8669 icon = _icon;
8670 }
8671 }
8672
8673 private final class H extends Handler {
8674 public static final int REPORT_FOCUS_CHANGE = 2;
8675 public static final int REPORT_LOSING_FOCUS = 3;
8676 public static final int ANIMATE = 4;
8677 public static final int ADD_STARTING = 5;
8678 public static final int REMOVE_STARTING = 6;
8679 public static final int FINISHED_STARTING = 7;
8680 public static final int REPORT_APPLICATION_TOKEN_WINDOWS = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008681 public static final int WINDOW_FREEZE_TIMEOUT = 11;
8682 public static final int HOLD_SCREEN_CHANGED = 12;
8683 public static final int APP_TRANSITION_TIMEOUT = 13;
8684 public static final int PERSIST_ANIMATION_SCALE = 14;
8685 public static final int FORCE_GC = 15;
8686 public static final int ENABLE_SCREEN = 16;
8687 public static final int APP_FREEZE_TIMEOUT = 17;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008688 public static final int COMPUTE_AND_SEND_NEW_CONFIGURATION = 18;
Romain Guy06882f82009-06-10 13:36:04 -07008689
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008690 private Session mLastReportedHold;
Romain Guy06882f82009-06-10 13:36:04 -07008691
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008692 public H() {
8693 }
Romain Guy06882f82009-06-10 13:36:04 -07008694
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008695 @Override
8696 public void handleMessage(Message msg) {
8697 switch (msg.what) {
8698 case REPORT_FOCUS_CHANGE: {
8699 WindowState lastFocus;
8700 WindowState newFocus;
Romain Guy06882f82009-06-10 13:36:04 -07008701
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008702 synchronized(mWindowMap) {
8703 lastFocus = mLastFocus;
8704 newFocus = mCurrentFocus;
8705 if (lastFocus == newFocus) {
8706 // Focus is not changing, so nothing to do.
8707 return;
8708 }
8709 mLastFocus = newFocus;
8710 //Log.i(TAG, "Focus moving from " + lastFocus
8711 // + " to " + newFocus);
8712 if (newFocus != null && lastFocus != null
8713 && !newFocus.isDisplayedLw()) {
8714 //Log.i(TAG, "Delaying loss of focus...");
8715 mLosingFocus.add(lastFocus);
8716 lastFocus = null;
8717 }
8718 }
8719
8720 if (lastFocus != newFocus) {
8721 //System.out.println("Changing focus from " + lastFocus
8722 // + " to " + newFocus);
8723 if (newFocus != null) {
8724 try {
8725 //Log.i(TAG, "Gaining focus: " + newFocus);
8726 newFocus.mClient.windowFocusChanged(true, mInTouchMode);
8727 } catch (RemoteException e) {
8728 // Ignore if process has died.
8729 }
8730 }
8731
8732 if (lastFocus != null) {
8733 try {
8734 //Log.i(TAG, "Losing focus: " + lastFocus);
8735 lastFocus.mClient.windowFocusChanged(false, mInTouchMode);
8736 } catch (RemoteException e) {
8737 // Ignore if process has died.
8738 }
8739 }
8740 }
8741 } break;
8742
8743 case REPORT_LOSING_FOCUS: {
8744 ArrayList<WindowState> losers;
Romain Guy06882f82009-06-10 13:36:04 -07008745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008746 synchronized(mWindowMap) {
8747 losers = mLosingFocus;
8748 mLosingFocus = new ArrayList<WindowState>();
8749 }
8750
8751 final int N = losers.size();
8752 for (int i=0; i<N; i++) {
8753 try {
8754 //Log.i(TAG, "Losing delayed focus: " + losers.get(i));
8755 losers.get(i).mClient.windowFocusChanged(false, mInTouchMode);
8756 } catch (RemoteException e) {
8757 // Ignore if process has died.
8758 }
8759 }
8760 } break;
8761
8762 case ANIMATE: {
8763 synchronized(mWindowMap) {
8764 mAnimationPending = false;
8765 performLayoutAndPlaceSurfacesLocked();
8766 }
8767 } break;
8768
8769 case ADD_STARTING: {
8770 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8771 final StartingData sd = wtoken.startingData;
8772
8773 if (sd == null) {
8774 // Animation has been canceled... do nothing.
8775 return;
8776 }
Romain Guy06882f82009-06-10 13:36:04 -07008777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008778 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Add starting "
8779 + wtoken + ": pkg=" + sd.pkg);
Romain Guy06882f82009-06-10 13:36:04 -07008780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008781 View view = null;
8782 try {
8783 view = mPolicy.addStartingWindow(
8784 wtoken.token, sd.pkg,
8785 sd.theme, sd.nonLocalizedLabel, sd.labelRes,
8786 sd.icon);
8787 } catch (Exception e) {
8788 Log.w(TAG, "Exception when adding starting window", e);
8789 }
8790
8791 if (view != null) {
8792 boolean abort = false;
8793
8794 synchronized(mWindowMap) {
8795 if (wtoken.removed || wtoken.startingData == null) {
8796 // If the window was successfully added, then
8797 // we need to remove it.
8798 if (wtoken.startingWindow != null) {
8799 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8800 "Aborted starting " + wtoken
8801 + ": removed=" + wtoken.removed
8802 + " startingData=" + wtoken.startingData);
8803 wtoken.startingWindow = null;
8804 wtoken.startingData = null;
8805 abort = true;
8806 }
8807 } else {
8808 wtoken.startingView = view;
8809 }
8810 if (DEBUG_STARTING_WINDOW && !abort) Log.v(TAG,
8811 "Added starting " + wtoken
8812 + ": startingWindow="
8813 + wtoken.startingWindow + " startingView="
8814 + wtoken.startingView);
8815 }
8816
8817 if (abort) {
8818 try {
8819 mPolicy.removeStartingWindow(wtoken.token, view);
8820 } catch (Exception e) {
8821 Log.w(TAG, "Exception when removing starting window", e);
8822 }
8823 }
8824 }
8825 } break;
8826
8827 case REMOVE_STARTING: {
8828 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8829 IBinder token = null;
8830 View view = null;
8831 synchronized (mWindowMap) {
8832 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Remove starting "
8833 + wtoken + ": startingWindow="
8834 + wtoken.startingWindow + " startingView="
8835 + wtoken.startingView);
8836 if (wtoken.startingWindow != null) {
8837 view = wtoken.startingView;
8838 token = wtoken.token;
8839 wtoken.startingData = null;
8840 wtoken.startingView = null;
8841 wtoken.startingWindow = null;
8842 }
8843 }
8844 if (view != null) {
8845 try {
8846 mPolicy.removeStartingWindow(token, view);
8847 } catch (Exception e) {
8848 Log.w(TAG, "Exception when removing starting window", e);
8849 }
8850 }
8851 } break;
8852
8853 case FINISHED_STARTING: {
8854 IBinder token = null;
8855 View view = null;
8856 while (true) {
8857 synchronized (mWindowMap) {
8858 final int N = mFinishedStarting.size();
8859 if (N <= 0) {
8860 break;
8861 }
8862 AppWindowToken wtoken = mFinishedStarting.remove(N-1);
8863
8864 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8865 "Finished starting " + wtoken
8866 + ": startingWindow=" + wtoken.startingWindow
8867 + " startingView=" + wtoken.startingView);
8868
8869 if (wtoken.startingWindow == null) {
8870 continue;
8871 }
8872
8873 view = wtoken.startingView;
8874 token = wtoken.token;
8875 wtoken.startingData = null;
8876 wtoken.startingView = null;
8877 wtoken.startingWindow = null;
8878 }
8879
8880 try {
8881 mPolicy.removeStartingWindow(token, view);
8882 } catch (Exception e) {
8883 Log.w(TAG, "Exception when removing starting window", e);
8884 }
8885 }
8886 } break;
8887
8888 case REPORT_APPLICATION_TOKEN_WINDOWS: {
8889 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8890
8891 boolean nowVisible = msg.arg1 != 0;
8892 boolean nowGone = msg.arg2 != 0;
8893
8894 try {
8895 if (DEBUG_VISIBILITY) Log.v(
8896 TAG, "Reporting visible in " + wtoken
8897 + " visible=" + nowVisible
8898 + " gone=" + nowGone);
8899 if (nowVisible) {
8900 wtoken.appToken.windowsVisible();
8901 } else {
8902 wtoken.appToken.windowsGone();
8903 }
8904 } catch (RemoteException ex) {
8905 }
8906 } break;
Romain Guy06882f82009-06-10 13:36:04 -07008907
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008908 case WINDOW_FREEZE_TIMEOUT: {
8909 synchronized (mWindowMap) {
8910 Log.w(TAG, "Window freeze timeout expired.");
8911 int i = mWindows.size();
8912 while (i > 0) {
8913 i--;
8914 WindowState w = (WindowState)mWindows.get(i);
8915 if (w.mOrientationChanging) {
8916 w.mOrientationChanging = false;
8917 Log.w(TAG, "Force clearing orientation change: " + w);
8918 }
8919 }
8920 performLayoutAndPlaceSurfacesLocked();
8921 }
8922 break;
8923 }
Romain Guy06882f82009-06-10 13:36:04 -07008924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008925 case HOLD_SCREEN_CHANGED: {
8926 Session oldHold;
8927 Session newHold;
8928 synchronized (mWindowMap) {
8929 oldHold = mLastReportedHold;
8930 newHold = (Session)msg.obj;
8931 mLastReportedHold = newHold;
8932 }
Romain Guy06882f82009-06-10 13:36:04 -07008933
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008934 if (oldHold != newHold) {
8935 try {
8936 if (oldHold != null) {
8937 mBatteryStats.noteStopWakelock(oldHold.mUid,
8938 "window",
8939 BatteryStats.WAKE_TYPE_WINDOW);
8940 }
8941 if (newHold != null) {
8942 mBatteryStats.noteStartWakelock(newHold.mUid,
8943 "window",
8944 BatteryStats.WAKE_TYPE_WINDOW);
8945 }
8946 } catch (RemoteException e) {
8947 }
8948 }
8949 break;
8950 }
Romain Guy06882f82009-06-10 13:36:04 -07008951
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008952 case APP_TRANSITION_TIMEOUT: {
8953 synchronized (mWindowMap) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07008954 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008955 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8956 "*** APP TRANSITION TIMEOUT");
8957 mAppTransitionReady = true;
8958 mAppTransitionTimeout = true;
8959 performLayoutAndPlaceSurfacesLocked();
8960 }
8961 }
8962 break;
8963 }
Romain Guy06882f82009-06-10 13:36:04 -07008964
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008965 case PERSIST_ANIMATION_SCALE: {
8966 Settings.System.putFloat(mContext.getContentResolver(),
8967 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
8968 Settings.System.putFloat(mContext.getContentResolver(),
8969 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
8970 break;
8971 }
Romain Guy06882f82009-06-10 13:36:04 -07008972
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008973 case FORCE_GC: {
8974 synchronized(mWindowMap) {
8975 if (mAnimationPending) {
8976 // If we are animating, don't do the gc now but
8977 // delay a bit so we don't interrupt the animation.
8978 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
8979 2000);
8980 return;
8981 }
8982 // If we are currently rotating the display, it will
8983 // schedule a new message when done.
8984 if (mDisplayFrozen) {
8985 return;
8986 }
8987 mFreezeGcPending = 0;
8988 }
8989 Runtime.getRuntime().gc();
8990 break;
8991 }
Romain Guy06882f82009-06-10 13:36:04 -07008992
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008993 case ENABLE_SCREEN: {
8994 performEnableScreen();
8995 break;
8996 }
Romain Guy06882f82009-06-10 13:36:04 -07008997
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008998 case APP_FREEZE_TIMEOUT: {
8999 synchronized (mWindowMap) {
9000 Log.w(TAG, "App freeze timeout expired.");
9001 int i = mAppTokens.size();
9002 while (i > 0) {
9003 i--;
9004 AppWindowToken tok = mAppTokens.get(i);
9005 if (tok.freezingScreen) {
9006 Log.w(TAG, "Force clearing freeze: " + tok);
9007 unsetAppFreezingScreenLocked(tok, true, true);
9008 }
9009 }
9010 }
9011 break;
9012 }
Romain Guy06882f82009-06-10 13:36:04 -07009013
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07009014 case COMPUTE_AND_SEND_NEW_CONFIGURATION: {
Dianne Hackborncfaef692009-06-15 14:24:44 -07009015 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07009016 sendNewConfiguration();
9017 }
9018 break;
9019 }
Romain Guy06882f82009-06-10 13:36:04 -07009020
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009021 }
9022 }
9023 }
9024
9025 // -------------------------------------------------------------
9026 // IWindowManager API
9027 // -------------------------------------------------------------
9028
9029 public IWindowSession openSession(IInputMethodClient client,
9030 IInputContext inputContext) {
9031 if (client == null) throw new IllegalArgumentException("null client");
9032 if (inputContext == null) throw new IllegalArgumentException("null inputContext");
9033 return new Session(client, inputContext);
9034 }
9035
9036 public boolean inputMethodClientHasFocus(IInputMethodClient client) {
9037 synchronized (mWindowMap) {
9038 // The focus for the client is the window immediately below
9039 // where we would place the input method window.
9040 int idx = findDesiredInputMethodWindowIndexLocked(false);
9041 WindowState imFocus;
9042 if (idx > 0) {
9043 imFocus = (WindowState)mWindows.get(idx-1);
9044 if (imFocus != null) {
9045 if (imFocus.mSession.mClient != null &&
9046 imFocus.mSession.mClient.asBinder() == client.asBinder()) {
9047 return true;
9048 }
9049 }
9050 }
9051 }
9052 return false;
9053 }
Romain Guy06882f82009-06-10 13:36:04 -07009054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009055 // -------------------------------------------------------------
9056 // Internals
9057 // -------------------------------------------------------------
9058
9059 final WindowState windowForClientLocked(Session session, IWindow client) {
9060 return windowForClientLocked(session, client.asBinder());
9061 }
Romain Guy06882f82009-06-10 13:36:04 -07009062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009063 final WindowState windowForClientLocked(Session session, IBinder client) {
9064 WindowState win = mWindowMap.get(client);
9065 if (localLOGV) Log.v(
9066 TAG, "Looking up client " + client + ": " + win);
9067 if (win == null) {
9068 RuntimeException ex = new RuntimeException();
9069 Log.w(TAG, "Requested window " + client + " does not exist", ex);
9070 return null;
9071 }
9072 if (session != null && win.mSession != session) {
9073 RuntimeException ex = new RuntimeException();
9074 Log.w(TAG, "Requested window " + client + " is in session " +
9075 win.mSession + ", not " + session, ex);
9076 return null;
9077 }
9078
9079 return win;
9080 }
9081
Dianne Hackborna8f60182009-09-01 19:01:50 -07009082 final void rebuildAppWindowListLocked() {
9083 int NW = mWindows.size();
9084 int i;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009085 int lastWallpaper = -1;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009086 int numRemoved = 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009087
Dianne Hackborna8f60182009-09-01 19:01:50 -07009088 // First remove all existing app windows.
9089 i=0;
9090 while (i < NW) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009091 WindowState w = (WindowState)mWindows.get(i);
9092 if (w.mAppToken != null) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009093 WindowState win = (WindowState)mWindows.remove(i);
9094 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
9095 "Rebuild removing window: " + win);
Dianne Hackborna8f60182009-09-01 19:01:50 -07009096 NW--;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009097 numRemoved++;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009098 continue;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009099 } else if (w.mAttrs.type == WindowManager.LayoutParams.TYPE_WALLPAPER
9100 && lastWallpaper == i-1) {
9101 lastWallpaper = i;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009102 }
9103 i++;
9104 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009105
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009106 // The wallpaper window(s) typically live at the bottom of the stack,
9107 // so skip them before adding app tokens.
9108 lastWallpaper++;
9109 i = lastWallpaper;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009110
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009111 // First add all of the exiting app tokens... these are no longer
9112 // in the main app list, but still have windows shown. We put them
9113 // in the back because now that the animation is over we no longer
9114 // will care about them.
9115 int NT = mExitingAppTokens.size();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009116 for (int j=0; j<NT; j++) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009117 i = reAddAppWindowsLocked(i, mExitingAppTokens.get(j));
9118 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009119
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009120 // And add in the still active app tokens in Z order.
9121 NT = mAppTokens.size();
9122 for (int j=0; j<NT; j++) {
9123 i = reAddAppWindowsLocked(i, mAppTokens.get(j));
Dianne Hackborna8f60182009-09-01 19:01:50 -07009124 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009125
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009126 i -= lastWallpaper;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009127 if (i != numRemoved) {
9128 Log.w(TAG, "Rebuild removed " + numRemoved
9129 + " windows but added " + i);
9130 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07009131 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009133 private final void assignLayersLocked() {
9134 int N = mWindows.size();
9135 int curBaseLayer = 0;
9136 int curLayer = 0;
9137 int i;
Romain Guy06882f82009-06-10 13:36:04 -07009138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009139 for (i=0; i<N; i++) {
9140 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009141 if (w.mBaseLayer == curBaseLayer || w.mIsImWindow
9142 || (i > 0 && w.mIsWallpaper)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009143 curLayer += WINDOW_LAYER_MULTIPLIER;
9144 w.mLayer = curLayer;
9145 } else {
9146 curBaseLayer = curLayer = w.mBaseLayer;
9147 w.mLayer = curLayer;
9148 }
9149 if (w.mTargetAppToken != null) {
9150 w.mAnimLayer = w.mLayer + w.mTargetAppToken.animLayerAdjustment;
9151 } else if (w.mAppToken != null) {
9152 w.mAnimLayer = w.mLayer + w.mAppToken.animLayerAdjustment;
9153 } else {
9154 w.mAnimLayer = w.mLayer;
9155 }
9156 if (w.mIsImWindow) {
9157 w.mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07009158 } else if (w.mIsWallpaper) {
9159 w.mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009160 }
9161 if (DEBUG_LAYERS) Log.v(TAG, "Assign layer " + w + ": "
9162 + w.mAnimLayer);
9163 //System.out.println(
9164 // "Assigned layer " + curLayer + " to " + w.mClient.asBinder());
9165 }
9166 }
9167
9168 private boolean mInLayout = false;
9169 private final void performLayoutAndPlaceSurfacesLocked() {
9170 if (mInLayout) {
Dave Bortcfe65242009-04-09 14:51:04 -07009171 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009172 throw new RuntimeException("Recursive call!");
9173 }
9174 Log.w(TAG, "performLayoutAndPlaceSurfacesLocked called while in layout");
9175 return;
9176 }
9177
9178 boolean recoveringMemory = false;
9179 if (mForceRemoves != null) {
9180 recoveringMemory = true;
9181 // Wait a little it for things to settle down, and off we go.
9182 for (int i=0; i<mForceRemoves.size(); i++) {
9183 WindowState ws = mForceRemoves.get(i);
9184 Log.i(TAG, "Force removing: " + ws);
9185 removeWindowInnerLocked(ws.mSession, ws);
9186 }
9187 mForceRemoves = null;
9188 Log.w(TAG, "Due to memory failure, waiting a bit for next layout");
9189 Object tmp = new Object();
9190 synchronized (tmp) {
9191 try {
9192 tmp.wait(250);
9193 } catch (InterruptedException e) {
9194 }
9195 }
9196 }
Romain Guy06882f82009-06-10 13:36:04 -07009197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009198 mInLayout = true;
9199 try {
9200 performLayoutAndPlaceSurfacesLockedInner(recoveringMemory);
Romain Guy06882f82009-06-10 13:36:04 -07009201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009202 int i = mPendingRemove.size()-1;
9203 if (i >= 0) {
9204 while (i >= 0) {
9205 WindowState w = mPendingRemove.get(i);
9206 removeWindowInnerLocked(w.mSession, w);
9207 i--;
9208 }
9209 mPendingRemove.clear();
9210
9211 mInLayout = false;
9212 assignLayersLocked();
9213 mLayoutNeeded = true;
9214 performLayoutAndPlaceSurfacesLocked();
9215
9216 } else {
9217 mInLayout = false;
9218 if (mLayoutNeeded) {
9219 requestAnimationLocked(0);
9220 }
9221 }
9222 } catch (RuntimeException e) {
9223 mInLayout = false;
9224 Log.e(TAG, "Unhandled exception while layout out windows", e);
9225 }
9226 }
9227
9228 private final void performLayoutLockedInner() {
9229 final int dw = mDisplay.getWidth();
9230 final int dh = mDisplay.getHeight();
9231
9232 final int N = mWindows.size();
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009233 int repeats = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009234 int i;
9235
Dianne Hackborn9b52a212009-12-11 14:51:35 -08009236 if (DEBUG_LAYOUT) Log.v(TAG, "performLayout: needed="
9237 + mLayoutNeeded + " dw=" + dw + " dh=" + dh);
9238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009239 // FIRST LOOP: Perform a layout, if needed.
Romain Guy06882f82009-06-10 13:36:04 -07009240
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009241 while (mLayoutNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009242 mPolicy.beginLayoutLw(dw, dh);
9243
9244 // First perform layout of any root windows (not attached
9245 // to another window).
9246 int topAttached = -1;
9247 for (i = N-1; i >= 0; i--) {
9248 WindowState win = (WindowState) mWindows.get(i);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009249
9250 // Don't do layout of a window if it is not visible, or
9251 // soon won't be visible, to avoid wasting time and funky
9252 // changes while a window is animating away.
9253 final AppWindowToken atoken = win.mAppToken;
9254 final boolean gone = win.mViewVisibility == View.GONE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009255 || !win.mRelayoutCalled
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009256 || win.mRootToken.hidden
9257 || (atoken != null && atoken.hiddenRequested)
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009258 || win.mAttachedHidden
9259 || win.mExiting || win.mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009260
Dianne Hackborn9b52a212009-12-11 14:51:35 -08009261 if (win.mLayoutAttached) {
9262 if (DEBUG_LAYOUT) Log.v(TAG, "First pass " + win
9263 + ": gone=" + gone + " mHaveFrame=" + win.mHaveFrame
9264 + " mLayoutAttached=" + win.mLayoutAttached);
9265 if (DEBUG_LAYOUT && gone) Log.v(TAG, " (mViewVisibility="
9266 + win.mViewVisibility + " mRelayoutCalled="
9267 + win.mRelayoutCalled + " hidden="
9268 + win.mRootToken.hidden + " hiddenRequested="
9269 + (atoken != null && atoken.hiddenRequested)
9270 + " mAttachedHidden=" + win.mAttachedHidden);
9271 }
9272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009273 // If this view is GONE, then skip it -- keep the current
9274 // frame, and let the caller know so they can ignore it
9275 // if they want. (We do the normal layout for INVISIBLE
9276 // windows, since that means "perform layout as normal,
9277 // just don't display").
9278 if (!gone || !win.mHaveFrame) {
9279 if (!win.mLayoutAttached) {
9280 mPolicy.layoutWindowLw(win, win.mAttrs, null);
Dianne Hackborn9b52a212009-12-11 14:51:35 -08009281 if (DEBUG_LAYOUT) Log.v(TAG, "-> mFrame="
9282 + win.mFrame + " mContainingFrame="
9283 + win.mContainingFrame + " mDisplayFrame="
9284 + win.mDisplayFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009285 } else {
9286 if (topAttached < 0) topAttached = i;
9287 }
9288 }
9289 }
Romain Guy06882f82009-06-10 13:36:04 -07009290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009291 // Now perform layout of attached windows, which usually
9292 // depend on the position of the window they are attached to.
9293 // XXX does not deal with windows that are attached to windows
9294 // that are themselves attached.
9295 for (i = topAttached; i >= 0; i--) {
9296 WindowState win = (WindowState) mWindows.get(i);
9297
9298 // If this view is GONE, then skip it -- keep the current
9299 // frame, and let the caller know so they can ignore it
9300 // if they want. (We do the normal layout for INVISIBLE
9301 // windows, since that means "perform layout as normal,
9302 // just don't display").
9303 if (win.mLayoutAttached) {
Dianne Hackborn9b52a212009-12-11 14:51:35 -08009304 if (DEBUG_LAYOUT) Log.v(TAG, "Second pass " + win
9305 + " mHaveFrame=" + win.mHaveFrame
9306 + " mViewVisibility=" + win.mViewVisibility
9307 + " mRelayoutCalled=" + win.mRelayoutCalled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009308 if ((win.mViewVisibility != View.GONE && win.mRelayoutCalled)
9309 || !win.mHaveFrame) {
9310 mPolicy.layoutWindowLw(win, win.mAttrs, win.mAttachedWindow);
Dianne Hackborn9b52a212009-12-11 14:51:35 -08009311 if (DEBUG_LAYOUT) Log.v(TAG, "-> mFrame="
9312 + win.mFrame + " mContainingFrame="
9313 + win.mContainingFrame + " mDisplayFrame="
9314 + win.mDisplayFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009315 }
9316 }
9317 }
9318
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009319 int changes = mPolicy.finishLayoutLw();
9320 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
9321 if ((adjustWallpaperWindowsLocked()&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
9322 assignLayersLocked();
9323 }
9324 }
9325 if (changes == 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009326 mLayoutNeeded = false;
9327 } else if (repeats > 2) {
9328 Log.w(TAG, "Layout repeat aborted after too many iterations");
9329 mLayoutNeeded = false;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009330 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG) != 0) {
9331 Configuration newConfig = updateOrientationFromAppTokensLocked(
9332 null, null);
9333 if (newConfig != null) {
9334 mLayoutNeeded = true;
9335 mH.sendEmptyMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION);
9336 }
9337 }
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009338 } else {
Dianne Hackborn9b52a212009-12-11 14:51:35 -08009339 if (DEBUG_LAYOUT) Log.v(TAG, "Repeating layout because changes=0x"
9340 + Integer.toHexString(changes));
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009341 repeats++;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009342 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG) != 0) {
Dianne Hackborn9b52a212009-12-11 14:51:35 -08009343 if (DEBUG_LAYOUT) Log.v(TAG, "Computing new config from layout");
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009344 Configuration newConfig = updateOrientationFromAppTokensLocked(
9345 null, null);
9346 if (newConfig != null) {
9347 mH.sendEmptyMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION);
9348 }
9349 }
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009350 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009351 }
9352 }
Romain Guy06882f82009-06-10 13:36:04 -07009353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009354 private final void performLayoutAndPlaceSurfacesLockedInner(
9355 boolean recoveringMemory) {
9356 final long currentTime = SystemClock.uptimeMillis();
9357 final int dw = mDisplay.getWidth();
9358 final int dh = mDisplay.getHeight();
9359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009360 int i;
9361
9362 // FIRST LOOP: Perform a layout, if needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009363 performLayoutLockedInner();
Romain Guy06882f82009-06-10 13:36:04 -07009364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009365 if (mFxSession == null) {
9366 mFxSession = new SurfaceSession();
9367 }
Romain Guy06882f82009-06-10 13:36:04 -07009368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009369 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
9370
9371 // Initialize state of exiting tokens.
9372 for (i=mExitingTokens.size()-1; i>=0; i--) {
9373 mExitingTokens.get(i).hasVisible = false;
9374 }
9375
9376 // Initialize state of exiting applications.
9377 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
9378 mExitingAppTokens.get(i).hasVisible = false;
9379 }
9380
9381 // SECOND LOOP: Execute animations and update visibility of windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009382 boolean orientationChangeComplete = true;
9383 Session holdScreen = null;
9384 float screenBrightness = -1;
Mike Lockwoodfb73f792009-11-20 11:31:18 -05009385 float buttonBrightness = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009386 boolean focusDisplayed = false;
9387 boolean animating = false;
9388
9389 Surface.openTransaction();
9390 try {
9391 boolean restart;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009392 boolean forceHiding = false;
Dianne Hackbornde2606d2009-12-18 16:53:55 -08009393 boolean wallpaperForceHidingChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009394
9395 do {
9396 final int transactionSequence = ++mTransactionSequence;
9397
9398 // Update animations of all applications, including those
9399 // associated with exiting/removed apps
9400 boolean tokensAnimating = false;
9401 final int NAT = mAppTokens.size();
9402 for (i=0; i<NAT; i++) {
9403 if (mAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
9404 tokensAnimating = true;
9405 }
9406 }
9407 final int NEAT = mExitingAppTokens.size();
9408 for (i=0; i<NEAT; i++) {
9409 if (mExitingAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
9410 tokensAnimating = true;
9411 }
9412 }
9413
Dianne Hackbornde2606d2009-12-18 16:53:55 -08009414 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "*** ANIM STEP: seq="
9415 + transactionSequence + " tokensAnimating="
9416 + tokensAnimating);
9417
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009418 animating = tokensAnimating;
9419 restart = false;
9420
9421 boolean tokenMayBeDrawn = false;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009422 boolean wallpaperMayChange = false;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009423 boolean focusMayChange = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009424
9425 mPolicy.beginAnimationLw(dw, dh);
9426
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009427 final int N = mWindows.size();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009429 for (i=N-1; i>=0; i--) {
9430 WindowState w = (WindowState)mWindows.get(i);
9431
9432 final WindowManager.LayoutParams attrs = w.mAttrs;
9433
9434 if (w.mSurface != null) {
9435 // Execute animation.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009436 if (w.commitFinishDrawingLocked(currentTime)) {
9437 if ((w.mAttrs.flags
9438 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009439 if (DEBUG_WALLPAPER) Log.v(TAG,
9440 "First draw done in potential wallpaper target " + w);
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009441 wallpaperMayChange = true;
9442 }
9443 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009444
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009445 boolean wasAnimating = w.mAnimating;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009446 if (w.stepAnimationLocked(currentTime, dw, dh)) {
9447 animating = true;
9448 //w.dump(" ");
9449 }
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009450 if (wasAnimating && !w.mAnimating && mWallpaperTarget == w) {
9451 wallpaperMayChange = true;
9452 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009453
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009454 if (mPolicy.doesForceHide(w, attrs)) {
9455 if (!wasAnimating && animating) {
9456 wallpaperForceHidingChanged = true;
9457 focusMayChange = true;
9458 } else if (w.isReadyForDisplay() && w.mAnimation == null) {
9459 forceHiding = true;
9460 }
9461 } else if (mPolicy.canBeForceHidden(w, attrs)) {
9462 boolean changed;
9463 if (forceHiding) {
9464 changed = w.hideLw(false, false);
9465 } else {
9466 changed = w.showLw(false, false);
9467 if (changed && wallpaperForceHidingChanged
9468 && w.isReadyForDisplay()) {
9469 // Assume we will need to animate. If
9470 // we don't (because the wallpaper will
9471 // stay with the lock screen), then we will
9472 // clean up later.
9473 Animation a = mPolicy.createForceHideEnterAnimation();
9474 if (a != null) {
9475 w.setAnimation(a);
9476 }
9477 }
9478 }
9479 if (changed && (attrs.flags
9480 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
9481 wallpaperMayChange = true;
9482 }
9483 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009484
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009485 mPolicy.animatingWindowLw(w, attrs);
9486 }
9487
9488 final AppWindowToken atoken = w.mAppToken;
9489 if (atoken != null && (!atoken.allDrawn || atoken.freezingScreen)) {
9490 if (atoken.lastTransactionSequence != transactionSequence) {
9491 atoken.lastTransactionSequence = transactionSequence;
9492 atoken.numInterestingWindows = atoken.numDrawnWindows = 0;
9493 atoken.startingDisplayed = false;
9494 }
9495 if ((w.isOnScreen() || w.mAttrs.type
9496 == WindowManager.LayoutParams.TYPE_BASE_APPLICATION)
9497 && !w.mExiting && !w.mDestroying) {
9498 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009499 Log.v(TAG, "Eval win " + w + ": isDrawn="
9500 + w.isDrawnLw()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009501 + ", isAnimating=" + w.isAnimating());
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009502 if (!w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009503 Log.v(TAG, "Not displayed: s=" + w.mSurface
9504 + " pv=" + w.mPolicyVisibility
9505 + " dp=" + w.mDrawPending
9506 + " cdp=" + w.mCommitDrawPending
9507 + " ah=" + w.mAttachedHidden
9508 + " th=" + atoken.hiddenRequested
9509 + " a=" + w.mAnimating);
9510 }
9511 }
9512 if (w != atoken.startingWindow) {
9513 if (!atoken.freezingScreen || !w.mAppFreezing) {
9514 atoken.numInterestingWindows++;
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009515 if (w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009516 atoken.numDrawnWindows++;
9517 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Log.v(TAG,
9518 "tokenMayBeDrawn: " + atoken
9519 + " freezingScreen=" + atoken.freezingScreen
9520 + " mAppFreezing=" + w.mAppFreezing);
9521 tokenMayBeDrawn = true;
9522 }
9523 }
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009524 } else if (w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009525 atoken.startingDisplayed = true;
9526 }
9527 }
9528 } else if (w.mReadyToShow) {
9529 w.performShowLocked();
9530 }
9531 }
9532
9533 if (mPolicy.finishAnimationLw()) {
9534 restart = true;
9535 }
9536
9537 if (tokenMayBeDrawn) {
9538 // See if any windows have been drawn, so they (and others
9539 // associated with them) can now be shown.
9540 final int NT = mTokenList.size();
9541 for (i=0; i<NT; i++) {
9542 AppWindowToken wtoken = mTokenList.get(i).appWindowToken;
9543 if (wtoken == null) {
9544 continue;
9545 }
9546 if (wtoken.freezingScreen) {
9547 int numInteresting = wtoken.numInterestingWindows;
9548 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
9549 if (DEBUG_VISIBILITY) Log.v(TAG,
9550 "allDrawn: " + wtoken
9551 + " interesting=" + numInteresting
9552 + " drawn=" + wtoken.numDrawnWindows);
9553 wtoken.showAllWindowsLocked();
9554 unsetAppFreezingScreenLocked(wtoken, false, true);
9555 orientationChangeComplete = true;
9556 }
9557 } else if (!wtoken.allDrawn) {
9558 int numInteresting = wtoken.numInterestingWindows;
9559 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
9560 if (DEBUG_VISIBILITY) Log.v(TAG,
9561 "allDrawn: " + wtoken
9562 + " interesting=" + numInteresting
9563 + " drawn=" + wtoken.numDrawnWindows);
9564 wtoken.allDrawn = true;
9565 restart = true;
9566
9567 // We can now show all of the drawn windows!
9568 if (!mOpeningApps.contains(wtoken)) {
9569 wtoken.showAllWindowsLocked();
9570 }
9571 }
9572 }
9573 }
9574 }
9575
9576 // If we are ready to perform an app transition, check through
9577 // all of the app tokens to be shown and see if they are ready
9578 // to go.
9579 if (mAppTransitionReady) {
9580 int NN = mOpeningApps.size();
9581 boolean goodToGo = true;
9582 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9583 "Checking " + NN + " opening apps (frozen="
9584 + mDisplayFrozen + " timeout="
9585 + mAppTransitionTimeout + ")...");
9586 if (!mDisplayFrozen && !mAppTransitionTimeout) {
9587 // If the display isn't frozen, wait to do anything until
9588 // all of the apps are ready. Otherwise just go because
9589 // we'll unfreeze the display when everyone is ready.
9590 for (i=0; i<NN && goodToGo; i++) {
9591 AppWindowToken wtoken = mOpeningApps.get(i);
9592 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9593 "Check opening app" + wtoken + ": allDrawn="
9594 + wtoken.allDrawn + " startingDisplayed="
9595 + wtoken.startingDisplayed);
9596 if (!wtoken.allDrawn && !wtoken.startingDisplayed
9597 && !wtoken.startingMoved) {
9598 goodToGo = false;
9599 }
9600 }
9601 }
9602 if (goodToGo) {
9603 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "**** GOOD TO GO");
9604 int transit = mNextAppTransition;
9605 if (mSkipAppTransitionAnimation) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009606 transit = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009607 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009608 mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009609 mAppTransitionReady = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009610 mAppTransitionRunning = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009611 mAppTransitionTimeout = false;
9612 mStartingIconInTransition = false;
9613 mSkipAppTransitionAnimation = false;
9614
9615 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
9616
Dianne Hackborna8f60182009-09-01 19:01:50 -07009617 // If there are applications waiting to come to the
9618 // top of the stack, now is the time to move their windows.
9619 // (Note that we don't do apps going to the bottom
9620 // here -- we want to keep their windows in the old
9621 // Z-order until the animation completes.)
9622 if (mToTopApps.size() > 0) {
9623 NN = mAppTokens.size();
9624 for (i=0; i<NN; i++) {
9625 AppWindowToken wtoken = mAppTokens.get(i);
9626 if (wtoken.sendingToTop) {
9627 wtoken.sendingToTop = false;
9628 moveAppWindowsLocked(wtoken, NN, false);
9629 }
9630 }
9631 mToTopApps.clear();
9632 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009633
Dianne Hackborn25994b42009-09-04 14:21:19 -07009634 WindowState oldWallpaper = mWallpaperTarget;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009635
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009636 adjustWallpaperWindowsLocked();
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009637 wallpaperMayChange = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009638
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009639 // The top-most window will supply the layout params,
9640 // and we will determine it below.
9641 LayoutParams animLp = null;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009642 AppWindowToken animToken = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009643 int bestAnimLayer = -1;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009644
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009645 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009646 "New wallpaper target=" + mWallpaperTarget
9647 + ", lower target=" + mLowerWallpaperTarget
9648 + ", upper target=" + mUpperWallpaperTarget);
Dianne Hackborn25994b42009-09-04 14:21:19 -07009649 int foundWallpapers = 0;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009650 // Do a first pass through the tokens for two
9651 // things:
9652 // (1) Determine if both the closing and opening
9653 // app token sets are wallpaper targets, in which
9654 // case special animations are needed
9655 // (since the wallpaper needs to stay static
9656 // behind them).
9657 // (2) Find the layout params of the top-most
9658 // application window in the tokens, which is
9659 // what will control the animation theme.
9660 final int NC = mClosingApps.size();
9661 NN = NC + mOpeningApps.size();
9662 for (i=0; i<NN; i++) {
9663 AppWindowToken wtoken;
9664 int mode;
9665 if (i < NC) {
9666 wtoken = mClosingApps.get(i);
9667 mode = 1;
9668 } else {
9669 wtoken = mOpeningApps.get(i-NC);
9670 mode = 2;
9671 }
9672 if (mLowerWallpaperTarget != null) {
9673 if (mLowerWallpaperTarget.mAppToken == wtoken
9674 || mUpperWallpaperTarget.mAppToken == wtoken) {
9675 foundWallpapers |= mode;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009676 }
9677 }
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009678 if (wtoken.appFullscreen) {
9679 WindowState ws = wtoken.findMainWindow();
9680 if (ws != null) {
9681 // If this is a compatibility mode
9682 // window, we will always use its anim.
9683 if ((ws.mAttrs.flags&FLAG_COMPATIBLE_WINDOW) != 0) {
9684 animLp = ws.mAttrs;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009685 animToken = ws.mAppToken;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009686 bestAnimLayer = Integer.MAX_VALUE;
9687 } else if (ws.mLayer > bestAnimLayer) {
9688 animLp = ws.mAttrs;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009689 animToken = ws.mAppToken;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009690 bestAnimLayer = ws.mLayer;
9691 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07009692 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009693 }
9694 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009695
Dianne Hackborn25994b42009-09-04 14:21:19 -07009696 if (foundWallpapers == 3) {
9697 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9698 "Wallpaper animation!");
9699 switch (transit) {
9700 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
9701 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
9702 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
9703 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN;
9704 break;
9705 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
9706 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
9707 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
9708 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE;
9709 break;
9710 }
9711 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9712 "New transit: " + transit);
9713 } else if (oldWallpaper != null) {
9714 // We are transitioning from an activity with
9715 // a wallpaper to one without.
9716 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE;
9717 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9718 "New transit away from wallpaper: " + transit);
9719 } else if (mWallpaperTarget != null) {
9720 // We are transitioning from an activity without
9721 // a wallpaper to now showing the wallpaper
9722 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN;
9723 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9724 "New transit into wallpaper: " + transit);
9725 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009726
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009727 if ((transit&WindowManagerPolicy.TRANSIT_ENTER_MASK) != 0) {
9728 mLastEnterAnimToken = animToken;
9729 mLastEnterAnimParams = animLp;
9730 } else if (mLastEnterAnimParams != null) {
9731 animLp = mLastEnterAnimParams;
9732 mLastEnterAnimToken = null;
9733 mLastEnterAnimParams = null;
9734 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009735
Dianne Hackbornde2606d2009-12-18 16:53:55 -08009736 // If all closing windows are obscured, then there is
9737 // no need to do an animation. This is the case, for
9738 // example, when this transition is being done behind
9739 // the lock screen.
9740 if (!mPolicy.allowAppAnimationsLw()) {
9741 animLp = null;
9742 }
9743
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009744 NN = mOpeningApps.size();
9745 for (i=0; i<NN; i++) {
9746 AppWindowToken wtoken = mOpeningApps.get(i);
9747 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9748 "Now opening app" + wtoken);
9749 wtoken.reportedVisible = false;
9750 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009751 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009752 setTokenVisibilityLocked(wtoken, animLp, true, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009753 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009754 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009755 wtoken.showAllWindowsLocked();
9756 }
9757 NN = mClosingApps.size();
9758 for (i=0; i<NN; i++) {
9759 AppWindowToken wtoken = mClosingApps.get(i);
9760 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9761 "Now closing app" + wtoken);
9762 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009763 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009764 setTokenVisibilityLocked(wtoken, animLp, false, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009765 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009766 wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009767 // Force the allDrawn flag, because we want to start
9768 // this guy's animations regardless of whether it's
9769 // gotten drawn.
9770 wtoken.allDrawn = true;
9771 }
9772
Dianne Hackborn8b571a82009-09-25 16:09:43 -07009773 mNextAppTransitionPackage = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009775 mOpeningApps.clear();
9776 mClosingApps.clear();
9777
9778 // This has changed the visibility of windows, so perform
9779 // a new layout to get them all up-to-date.
9780 mLayoutNeeded = true;
Dianne Hackborn20583ff2009-07-27 21:51:05 -07009781 if (!moveInputMethodWindowsIfNeededLocked(true)) {
9782 assignLayersLocked();
9783 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009784 performLayoutLockedInner();
9785 updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009786 focusMayChange = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009787
9788 restart = true;
9789 }
9790 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009791
Dianne Hackborna8f60182009-09-01 19:01:50 -07009792 if (!animating && mAppTransitionRunning) {
9793 // We have finished the animation of an app transition. To do
9794 // this, we have delayed a lot of operations like showing and
9795 // hiding apps, moving apps in Z-order, etc. The app token list
9796 // reflects the correct Z-order, but the window list may now
9797 // be out of sync with it. So here we will just rebuild the
9798 // entire app window list. Fun!
9799 mAppTransitionRunning = false;
9800 // Clear information about apps that were moving.
9801 mToBottomApps.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009802
Dianne Hackborna8f60182009-09-01 19:01:50 -07009803 rebuildAppWindowListLocked();
9804 restart = true;
9805 moveInputMethodWindowsIfNeededLocked(false);
9806 wallpaperMayChange = true;
9807 mLayoutNeeded = true;
Suchi Amalapurapuc9568e32009-11-05 18:51:16 -08009808 // Since the window list has been rebuilt, focus might
9809 // have to be recomputed since the actual order of windows
9810 // might have changed again.
9811 focusMayChange = true;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009812 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009813
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009814 int adjResult = 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009815
Dianne Hackbornde2606d2009-12-18 16:53:55 -08009816 if (wallpaperForceHidingChanged && !restart && !mAppTransitionReady) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009817 // At this point, there was a window with a wallpaper that
9818 // was force hiding other windows behind it, but now it
9819 // is going away. This may be simple -- just animate
9820 // away the wallpaper and its window -- or it may be
9821 // hard -- the wallpaper now needs to be shown behind
9822 // something that was hidden.
9823 WindowState oldWallpaper = mWallpaperTarget;
Dianne Hackbornde2606d2009-12-18 16:53:55 -08009824 if (mLowerWallpaperTarget != null
9825 && mLowerWallpaperTarget.mAppToken != null) {
9826 if (DEBUG_WALLPAPER) Log.v(TAG,
9827 "wallpaperForceHiding changed with lower="
9828 + mLowerWallpaperTarget);
9829 if (DEBUG_WALLPAPER) Log.v(TAG,
9830 "hidden=" + mLowerWallpaperTarget.mAppToken.hidden +
9831 " hiddenRequested=" + mLowerWallpaperTarget.mAppToken.hiddenRequested);
9832 if (mLowerWallpaperTarget.mAppToken.hidden) {
9833 // The lower target has become hidden before we
9834 // actually started the animation... let's completely
9835 // re-evaluate everything.
9836 mLowerWallpaperTarget = mUpperWallpaperTarget = null;
9837 restart = true;
9838 }
9839 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009840 adjResult = adjustWallpaperWindowsLocked();
9841 wallpaperMayChange = false;
Dianne Hackbornde2606d2009-12-18 16:53:55 -08009842 wallpaperForceHidingChanged = false;
9843 if (DEBUG_WALLPAPER) Log.v(TAG, "****** OLD: " + oldWallpaper
9844 + " NEW: " + mWallpaperTarget
9845 + " LOWER: " + mLowerWallpaperTarget);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009846 if (mLowerWallpaperTarget == null) {
9847 // Whoops, we don't need a special wallpaper animation.
9848 // Clear them out.
9849 forceHiding = false;
9850 for (i=N-1; i>=0; i--) {
9851 WindowState w = (WindowState)mWindows.get(i);
9852 if (w.mSurface != null) {
9853 final WindowManager.LayoutParams attrs = w.mAttrs;
Suchi Amalapurapuc03d28b2009-10-28 14:32:05 -07009854 if (mPolicy.doesForceHide(w, attrs) && w.isVisibleLw()) {
9855 if (DEBUG_FOCUS) Log.i(TAG, "win=" + w + " force hides other windows");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009856 forceHiding = true;
9857 } else if (mPolicy.canBeForceHidden(w, attrs)) {
9858 if (!w.mAnimating) {
9859 // We set the animation above so it
9860 // is not yet running.
9861 w.clearAnimation();
9862 }
9863 }
9864 }
9865 }
9866 }
9867 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009868
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009869 if (wallpaperMayChange) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009870 if (DEBUG_WALLPAPER) Log.v(TAG,
9871 "Wallpaper may change! Adjusting");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009872 adjResult = adjustWallpaperWindowsLocked();
9873 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009874
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009875 if ((adjResult&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
9876 if (DEBUG_WALLPAPER) Log.v(TAG,
9877 "Wallpaper layer changed: assigning layers + relayout");
9878 restart = true;
9879 mLayoutNeeded = true;
9880 assignLayersLocked();
9881 } else if ((adjResult&ADJUST_WALLPAPER_VISIBILITY_CHANGED) != 0) {
9882 if (DEBUG_WALLPAPER) Log.v(TAG,
9883 "Wallpaper visibility changed: relayout");
9884 restart = true;
9885 mLayoutNeeded = true;
9886 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009887
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009888 if (focusMayChange) {
9889 if (updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES)) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009890 restart = true;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009891 adjResult = 0;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009892 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009893 }
9894
9895 if (mLayoutNeeded) {
9896 restart = true;
9897 performLayoutLockedInner();
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009898 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009899
Dianne Hackbornde2606d2009-12-18 16:53:55 -08009900 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "*** ANIM STEP: restart="
9901 + restart);
9902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009903 } while (restart);
9904
9905 // THIRD LOOP: Update the surfaces of all windows.
9906
9907 final boolean someoneLosingFocus = mLosingFocus.size() != 0;
9908
9909 boolean obscured = false;
9910 boolean blurring = false;
9911 boolean dimming = false;
9912 boolean covered = false;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009913 boolean syswin = false;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009914 boolean backgroundFillerShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009915
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009916 final int N = mWindows.size();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009917
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009918 for (i=N-1; i>=0; i--) {
9919 WindowState w = (WindowState)mWindows.get(i);
9920
9921 boolean displayed = false;
9922 final WindowManager.LayoutParams attrs = w.mAttrs;
9923 final int attrFlags = attrs.flags;
9924
9925 if (w.mSurface != null) {
9926 w.computeShownFrameLocked();
9927 if (localLOGV) Log.v(
9928 TAG, "Placing surface #" + i + " " + w.mSurface
9929 + ": new=" + w.mShownFrame + ", old="
9930 + w.mLastShownFrame);
9931
9932 boolean resize;
9933 int width, height;
9934 if ((w.mAttrs.flags & w.mAttrs.FLAG_SCALED) != 0) {
9935 resize = w.mLastRequestedWidth != w.mRequestedWidth ||
9936 w.mLastRequestedHeight != w.mRequestedHeight;
9937 // for a scaled surface, we just want to use
9938 // the requested size.
9939 width = w.mRequestedWidth;
9940 height = w.mRequestedHeight;
9941 w.mLastRequestedWidth = width;
9942 w.mLastRequestedHeight = height;
9943 w.mLastShownFrame.set(w.mShownFrame);
9944 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009945 if (SHOW_TRANSACTIONS) Log.i(
9946 TAG, " SURFACE " + w.mSurface
9947 + ": POS " + w.mShownFrame.left
9948 + ", " + w.mShownFrame.top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009949 w.mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
9950 } catch (RuntimeException e) {
9951 Log.w(TAG, "Error positioning surface in " + w, e);
9952 if (!recoveringMemory) {
9953 reclaimSomeSurfaceMemoryLocked(w, "position");
9954 }
9955 }
9956 } else {
9957 resize = !w.mLastShownFrame.equals(w.mShownFrame);
9958 width = w.mShownFrame.width();
9959 height = w.mShownFrame.height();
9960 w.mLastShownFrame.set(w.mShownFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009961 }
9962
9963 if (resize) {
9964 if (width < 1) width = 1;
9965 if (height < 1) height = 1;
9966 if (w.mSurface != null) {
9967 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009968 if (SHOW_TRANSACTIONS) Log.i(
9969 TAG, " SURFACE " + w.mSurface + ": POS "
9970 + w.mShownFrame.left + ","
9971 + w.mShownFrame.top + " SIZE "
9972 + w.mShownFrame.width() + "x"
9973 + w.mShownFrame.height());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009974 w.mSurface.setSize(width, height);
9975 w.mSurface.setPosition(w.mShownFrame.left,
9976 w.mShownFrame.top);
9977 } catch (RuntimeException e) {
9978 // If something goes wrong with the surface (such
9979 // as running out of memory), don't take down the
9980 // entire system.
9981 Log.e(TAG, "Failure updating surface of " + w
9982 + "size=(" + width + "x" + height
9983 + "), pos=(" + w.mShownFrame.left
9984 + "," + w.mShownFrame.top + ")", e);
9985 if (!recoveringMemory) {
9986 reclaimSomeSurfaceMemoryLocked(w, "size");
9987 }
9988 }
9989 }
9990 }
9991 if (!w.mAppFreezing) {
9992 w.mContentInsetsChanged =
9993 !w.mLastContentInsets.equals(w.mContentInsets);
9994 w.mVisibleInsetsChanged =
9995 !w.mLastVisibleInsets.equals(w.mVisibleInsets);
Romain Guy06882f82009-06-10 13:36:04 -07009996 if (!w.mLastFrame.equals(w.mFrame)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009997 || w.mContentInsetsChanged
9998 || w.mVisibleInsetsChanged) {
9999 w.mLastFrame.set(w.mFrame);
10000 w.mLastContentInsets.set(w.mContentInsets);
10001 w.mLastVisibleInsets.set(w.mVisibleInsets);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010002 // If the screen is currently frozen, then keep
10003 // it frozen until this window draws at its new
10004 // orientation.
10005 if (mDisplayFrozen) {
10006 if (DEBUG_ORIENTATION) Log.v(TAG,
10007 "Resizing while display frozen: " + w);
10008 w.mOrientationChanging = true;
10009 if (mWindowsFreezingScreen) {
10010 mWindowsFreezingScreen = true;
10011 // XXX should probably keep timeout from
10012 // when we first froze the display.
10013 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
10014 mH.sendMessageDelayed(mH.obtainMessage(
10015 H.WINDOW_FREEZE_TIMEOUT), 2000);
10016 }
10017 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010018 // If the orientation is changing, then we need to
10019 // hold off on unfreezing the display until this
10020 // window has been redrawn; to do that, we need
10021 // to go through the process of getting informed
10022 // by the application when it has finished drawing.
10023 if (w.mOrientationChanging) {
10024 if (DEBUG_ORIENTATION) Log.v(TAG,
10025 "Orientation start waiting for draw in "
10026 + w + ", surface " + w.mSurface);
10027 w.mDrawPending = true;
10028 w.mCommitDrawPending = false;
10029 w.mReadyToShow = false;
10030 if (w.mAppToken != null) {
10031 w.mAppToken.allDrawn = false;
10032 }
10033 }
Romain Guy06882f82009-06-10 13:36:04 -070010034 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010035 "Resizing window " + w + " to " + w.mFrame);
10036 mResizingWindows.add(w);
10037 } else if (w.mOrientationChanging) {
10038 if (!w.mDrawPending && !w.mCommitDrawPending) {
10039 if (DEBUG_ORIENTATION) Log.v(TAG,
10040 "Orientation not waiting for draw in "
10041 + w + ", surface " + w.mSurface);
10042 w.mOrientationChanging = false;
10043 }
10044 }
10045 }
10046
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010047 if (w.mAttachedHidden || !w.isReadyForDisplay()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010048 if (!w.mLastHidden) {
10049 //dump();
10050 w.mLastHidden = true;
10051 if (SHOW_TRANSACTIONS) Log.i(
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010052 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010053 if (w.mSurface != null) {
10054 try {
10055 w.mSurface.hide();
10056 } catch (RuntimeException e) {
10057 Log.w(TAG, "Exception hiding surface in " + w);
10058 }
10059 }
10060 mKeyWaiter.releasePendingPointerLocked(w.mSession);
10061 }
10062 // If we are waiting for this window to handle an
10063 // orientation change, well, it is hidden, so
10064 // doesn't really matter. Note that this does
10065 // introduce a potential glitch if the window
10066 // becomes unhidden before it has drawn for the
10067 // new orientation.
10068 if (w.mOrientationChanging) {
10069 w.mOrientationChanging = false;
10070 if (DEBUG_ORIENTATION) Log.v(TAG,
10071 "Orientation change skips hidden " + w);
10072 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010073 } else if (w.mLastLayer != w.mAnimLayer
10074 || w.mLastAlpha != w.mShownAlpha
10075 || w.mLastDsDx != w.mDsDx
10076 || w.mLastDtDx != w.mDtDx
10077 || w.mLastDsDy != w.mDsDy
10078 || w.mLastDtDy != w.mDtDy
10079 || w.mLastHScale != w.mHScale
10080 || w.mLastVScale != w.mVScale
10081 || w.mLastHidden) {
10082 displayed = true;
10083 w.mLastAlpha = w.mShownAlpha;
10084 w.mLastLayer = w.mAnimLayer;
10085 w.mLastDsDx = w.mDsDx;
10086 w.mLastDtDx = w.mDtDx;
10087 w.mLastDsDy = w.mDsDy;
10088 w.mLastDtDy = w.mDtDy;
10089 w.mLastHScale = w.mHScale;
10090 w.mLastVScale = w.mVScale;
10091 if (SHOW_TRANSACTIONS) Log.i(
10092 TAG, " SURFACE " + w.mSurface + ": alpha="
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010093 + w.mShownAlpha + " layer=" + w.mAnimLayer
10094 + " matrix=[" + (w.mDsDx*w.mHScale)
10095 + "," + (w.mDtDx*w.mVScale)
10096 + "][" + (w.mDsDy*w.mHScale)
10097 + "," + (w.mDtDy*w.mVScale) + "]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010098 if (w.mSurface != null) {
10099 try {
10100 w.mSurface.setAlpha(w.mShownAlpha);
10101 w.mSurface.setLayer(w.mAnimLayer);
10102 w.mSurface.setMatrix(
10103 w.mDsDx*w.mHScale, w.mDtDx*w.mVScale,
10104 w.mDsDy*w.mHScale, w.mDtDy*w.mVScale);
10105 } catch (RuntimeException e) {
10106 Log.w(TAG, "Error updating surface in " + w, e);
10107 if (!recoveringMemory) {
10108 reclaimSomeSurfaceMemoryLocked(w, "update");
10109 }
10110 }
10111 }
10112
10113 if (w.mLastHidden && !w.mDrawPending
10114 && !w.mCommitDrawPending
10115 && !w.mReadyToShow) {
10116 if (SHOW_TRANSACTIONS) Log.i(
10117 TAG, " SURFACE " + w.mSurface + ": SHOW (performLayout)");
10118 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + w
10119 + " during relayout");
10120 if (showSurfaceRobustlyLocked(w)) {
10121 w.mHasDrawn = true;
10122 w.mLastHidden = false;
10123 } else {
10124 w.mOrientationChanging = false;
10125 }
10126 }
10127 if (w.mSurface != null) {
10128 w.mToken.hasVisible = true;
10129 }
10130 } else {
10131 displayed = true;
10132 }
10133
10134 if (displayed) {
10135 if (!covered) {
Romain Guy980a9382010-01-08 15:06:28 -080010136 if (attrs.width == LayoutParams.MATCH_PARENT
10137 && attrs.height == LayoutParams.MATCH_PARENT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010138 covered = true;
10139 }
10140 }
10141 if (w.mOrientationChanging) {
10142 if (w.mDrawPending || w.mCommitDrawPending) {
10143 orientationChangeComplete = false;
10144 if (DEBUG_ORIENTATION) Log.v(TAG,
10145 "Orientation continue waiting for draw in " + w);
10146 } else {
10147 w.mOrientationChanging = false;
10148 if (DEBUG_ORIENTATION) Log.v(TAG,
10149 "Orientation change complete in " + w);
10150 }
10151 }
10152 w.mToken.hasVisible = true;
10153 }
10154 } else if (w.mOrientationChanging) {
10155 if (DEBUG_ORIENTATION) Log.v(TAG,
10156 "Orientation change skips hidden " + w);
10157 w.mOrientationChanging = false;
10158 }
10159
10160 final boolean canBeSeen = w.isDisplayedLw();
10161
10162 if (someoneLosingFocus && w == mCurrentFocus && canBeSeen) {
10163 focusDisplayed = true;
10164 }
10165
Dianne Hackborne9e9bca2009-08-18 15:08:22 -070010166 final boolean obscuredChanged = w.mObscured != obscured;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010168 // Update effect.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010169 if (!(w.mObscured=obscured)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010170 if (w.mSurface != null) {
10171 if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
10172 holdScreen = w.mSession;
10173 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -070010174 if (!syswin && w.mAttrs.screenBrightness >= 0
10175 && screenBrightness < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010176 screenBrightness = w.mAttrs.screenBrightness;
10177 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -050010178 if (!syswin && w.mAttrs.buttonBrightness >= 0
10179 && buttonBrightness < 0) {
10180 buttonBrightness = w.mAttrs.buttonBrightness;
10181 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -070010182 if (attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG
10183 || attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD
10184 || attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_ERROR) {
10185 syswin = true;
10186 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010187 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010188
Dianne Hackborn25994b42009-09-04 14:21:19 -070010189 boolean opaqueDrawn = canBeSeen && w.isOpaqueDrawn();
10190 if (opaqueDrawn && w.isFullscreen(dw, dh)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010191 // This window completely covers everything behind it,
10192 // so we want to leave all of them as unblurred (for
10193 // performance reasons).
10194 obscured = true;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010195 } else if (opaqueDrawn && w.needsBackgroundFiller(dw, dh)) {
10196 if (SHOW_TRANSACTIONS) Log.d(TAG, "showing background filler");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010197 // This window is in compatibility mode, and needs background filler.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010198 obscured = true;
10199 if (mBackgroundFillerSurface == null) {
10200 try {
10201 mBackgroundFillerSurface = new Surface(mFxSession, 0,
10202 0, dw, dh,
10203 PixelFormat.OPAQUE,
10204 Surface.FX_SURFACE_NORMAL);
10205 } catch (Exception e) {
10206 Log.e(TAG, "Exception creating filler surface", e);
10207 }
10208 }
10209 try {
10210 mBackgroundFillerSurface.setPosition(0, 0);
10211 mBackgroundFillerSurface.setSize(dw, dh);
10212 // Using the same layer as Dim because they will never be shown at the
10213 // same time.
10214 mBackgroundFillerSurface.setLayer(w.mAnimLayer - 1);
10215 mBackgroundFillerSurface.show();
10216 } catch (RuntimeException e) {
10217 Log.e(TAG, "Exception showing filler surface");
10218 }
10219 backgroundFillerShown = true;
10220 mBackgroundFillerShown = true;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010221 } else if (canBeSeen && !obscured &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010222 (attrFlags&FLAG_BLUR_BEHIND|FLAG_DIM_BEHIND) != 0) {
10223 if (localLOGV) Log.v(TAG, "Win " + w
10224 + ": blurring=" + blurring
10225 + " obscured=" + obscured
10226 + " displayed=" + displayed);
10227 if ((attrFlags&FLAG_DIM_BEHIND) != 0) {
10228 if (!dimming) {
10229 //Log.i(TAG, "DIM BEHIND: " + w);
10230 dimming = true;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010231 if (mDimAnimator == null) {
10232 mDimAnimator = new DimAnimator(mFxSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010233 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010234 mDimAnimator.show(dw, dh);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010235 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010236 mDimAnimator.updateParameters(w, currentTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010237 }
10238 if ((attrFlags&FLAG_BLUR_BEHIND) != 0) {
10239 if (!blurring) {
10240 //Log.i(TAG, "BLUR BEHIND: " + w);
10241 blurring = true;
10242 mBlurShown = true;
10243 if (mBlurSurface == null) {
10244 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
10245 + mBlurSurface + ": CREATE");
10246 try {
Romain Guy06882f82009-06-10 13:36:04 -070010247 mBlurSurface = new Surface(mFxSession, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010248 -1, 16, 16,
10249 PixelFormat.OPAQUE,
10250 Surface.FX_SURFACE_BLUR);
10251 } catch (Exception e) {
10252 Log.e(TAG, "Exception creating Blur surface", e);
10253 }
10254 }
10255 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
10256 + mBlurSurface + ": SHOW pos=(0,0) (" +
10257 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
10258 if (mBlurSurface != null) {
10259 mBlurSurface.setPosition(0, 0);
10260 mBlurSurface.setSize(dw, dh);
10261 try {
10262 mBlurSurface.show();
10263 } catch (RuntimeException e) {
10264 Log.w(TAG, "Failure showing blur surface", e);
10265 }
10266 }
10267 }
10268 mBlurSurface.setLayer(w.mAnimLayer-2);
10269 }
10270 }
10271 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010272
Dianne Hackborne9e9bca2009-08-18 15:08:22 -070010273 if (obscuredChanged && mWallpaperTarget == w) {
10274 // This is the wallpaper target and its obscured state
10275 // changed... make sure the current wallaper's visibility
10276 // has been updated accordingly.
10277 updateWallpaperVisibilityLocked();
10278 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010279 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010280
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010281 if (backgroundFillerShown == false && mBackgroundFillerShown) {
10282 mBackgroundFillerShown = false;
10283 if (SHOW_TRANSACTIONS) Log.d(TAG, "hiding background filler");
10284 try {
10285 mBackgroundFillerSurface.hide();
10286 } catch (RuntimeException e) {
10287 Log.e(TAG, "Exception hiding filler surface", e);
10288 }
10289 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010290
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010291 if (mDimAnimator != null && mDimAnimator.mDimShown) {
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010292 animating |= mDimAnimator.updateSurface(dimming, currentTime,
10293 mDisplayFrozen || !mPolicy.isScreenOn());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010294 }
Romain Guy06882f82009-06-10 13:36:04 -070010295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010296 if (!blurring && mBlurShown) {
10297 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR " + mBlurSurface
10298 + ": HIDE");
10299 try {
10300 mBlurSurface.hide();
10301 } catch (IllegalArgumentException e) {
10302 Log.w(TAG, "Illegal argument exception hiding blur surface");
10303 }
10304 mBlurShown = false;
10305 }
10306
10307 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
10308 } catch (RuntimeException e) {
10309 Log.e(TAG, "Unhandled exception in Window Manager", e);
10310 }
10311
10312 Surface.closeTransaction();
Romain Guy06882f82009-06-10 13:36:04 -070010313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010314 if (DEBUG_ORIENTATION && mDisplayFrozen) Log.v(TAG,
10315 "With display frozen, orientationChangeComplete="
10316 + orientationChangeComplete);
10317 if (orientationChangeComplete) {
10318 if (mWindowsFreezingScreen) {
10319 mWindowsFreezingScreen = false;
10320 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
10321 }
10322 if (mAppsFreezingScreen == 0) {
10323 stopFreezingDisplayLocked();
10324 }
10325 }
Romain Guy06882f82009-06-10 13:36:04 -070010326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010327 i = mResizingWindows.size();
10328 if (i > 0) {
10329 do {
10330 i--;
10331 WindowState win = mResizingWindows.get(i);
10332 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010333 if (DEBUG_ORIENTATION) Log.v(TAG, "Reporting new frame to "
10334 + win + ": " + win.mFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010335 win.mClient.resized(win.mFrame.width(),
10336 win.mFrame.height(), win.mLastContentInsets,
10337 win.mLastVisibleInsets, win.mDrawPending);
10338 win.mContentInsetsChanged = false;
10339 win.mVisibleInsetsChanged = false;
10340 } catch (RemoteException e) {
10341 win.mOrientationChanging = false;
10342 }
10343 } while (i > 0);
10344 mResizingWindows.clear();
10345 }
Romain Guy06882f82009-06-10 13:36:04 -070010346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010347 // Destroy the surface of any windows that are no longer visible.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010348 boolean wallpaperDestroyed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010349 i = mDestroySurface.size();
10350 if (i > 0) {
10351 do {
10352 i--;
10353 WindowState win = mDestroySurface.get(i);
10354 win.mDestroying = false;
10355 if (mInputMethodWindow == win) {
10356 mInputMethodWindow = null;
10357 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010358 if (win == mWallpaperTarget) {
10359 wallpaperDestroyed = true;
10360 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010361 win.destroySurfaceLocked();
10362 } while (i > 0);
10363 mDestroySurface.clear();
10364 }
10365
10366 // Time to remove any exiting tokens?
10367 for (i=mExitingTokens.size()-1; i>=0; i--) {
10368 WindowToken token = mExitingTokens.get(i);
10369 if (!token.hasVisible) {
10370 mExitingTokens.remove(i);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070010371 if (token.windowType == TYPE_WALLPAPER) {
10372 mWallpaperTokens.remove(token);
10373 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010374 }
10375 }
10376
10377 // Time to remove any exiting applications?
10378 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
10379 AppWindowToken token = mExitingAppTokens.get(i);
10380 if (!token.hasVisible && !mClosingApps.contains(token)) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -070010381 // Make sure there is no animation running on this token,
10382 // so any windows associated with it will be removed as
10383 // soon as their animations are complete
10384 token.animation = null;
10385 token.animating = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010386 mAppTokens.remove(token);
10387 mExitingAppTokens.remove(i);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010388 if (mLastEnterAnimToken == token) {
10389 mLastEnterAnimToken = null;
10390 mLastEnterAnimParams = null;
10391 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010392 }
10393 }
10394
Dianne Hackborna8f60182009-09-01 19:01:50 -070010395 boolean needRelayout = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010396
Dianne Hackborna8f60182009-09-01 19:01:50 -070010397 if (!animating && mAppTransitionRunning) {
10398 // We have finished the animation of an app transition. To do
10399 // this, we have delayed a lot of operations like showing and
10400 // hiding apps, moving apps in Z-order, etc. The app token list
10401 // reflects the correct Z-order, but the window list may now
10402 // be out of sync with it. So here we will just rebuild the
10403 // entire app window list. Fun!
10404 mAppTransitionRunning = false;
10405 needRelayout = true;
10406 rebuildAppWindowListLocked();
10407 // Clear information about apps that were moving.
10408 mToBottomApps.clear();
10409 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010411 if (focusDisplayed) {
10412 mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
10413 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010414 if (wallpaperDestroyed) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010415 needRelayout = adjustWallpaperWindowsLocked() != 0;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010416 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070010417 if (needRelayout) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010418 requestAnimationLocked(0);
10419 } else if (animating) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010420 requestAnimationLocked(currentTime+(1000/60)-SystemClock.uptimeMillis());
10421 }
10422 mQueue.setHoldScreenLocked(holdScreen != null);
10423 if (screenBrightness < 0 || screenBrightness > 1.0f) {
10424 mPowerManager.setScreenBrightnessOverride(-1);
10425 } else {
10426 mPowerManager.setScreenBrightnessOverride((int)
10427 (screenBrightness * Power.BRIGHTNESS_ON));
10428 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -050010429 if (buttonBrightness < 0 || buttonBrightness > 1.0f) {
10430 mPowerManager.setButtonBrightnessOverride(-1);
10431 } else {
10432 mPowerManager.setButtonBrightnessOverride((int)
10433 (buttonBrightness * Power.BRIGHTNESS_ON));
10434 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010435 if (holdScreen != mHoldingScreenOn) {
10436 mHoldingScreenOn = holdScreen;
10437 Message m = mH.obtainMessage(H.HOLD_SCREEN_CHANGED, holdScreen);
10438 mH.sendMessage(m);
10439 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010440
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010441 if (mTurnOnScreen) {
10442 mPowerManager.userActivity(SystemClock.uptimeMillis(), false,
10443 LocalPowerManager.BUTTON_EVENT, true);
10444 mTurnOnScreen = false;
10445 }
Dianne Hackbornf3bea9c2009-12-09 18:26:21 -080010446
10447 // Check to see if we are now in a state where the screen should
10448 // be enabled, because the window obscured flags have changed.
10449 enableScreenIfNeededLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010450 }
10451
10452 void requestAnimationLocked(long delay) {
10453 if (!mAnimationPending) {
10454 mAnimationPending = true;
10455 mH.sendMessageDelayed(mH.obtainMessage(H.ANIMATE), delay);
10456 }
10457 }
Romain Guy06882f82009-06-10 13:36:04 -070010458
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010459 /**
10460 * Have the surface flinger show a surface, robustly dealing with
10461 * error conditions. In particular, if there is not enough memory
10462 * to show the surface, then we will try to get rid of other surfaces
10463 * in order to succeed.
Romain Guy06882f82009-06-10 13:36:04 -070010464 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010465 * @return Returns true if the surface was successfully shown.
10466 */
10467 boolean showSurfaceRobustlyLocked(WindowState win) {
10468 try {
10469 if (win.mSurface != null) {
10470 win.mSurface.show();
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010471 if (win.mTurnOnScreen) {
10472 win.mTurnOnScreen = false;
10473 mTurnOnScreen = true;
10474 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010475 }
10476 return true;
10477 } catch (RuntimeException e) {
10478 Log.w(TAG, "Failure showing surface " + win.mSurface + " in " + win);
10479 }
Romain Guy06882f82009-06-10 13:36:04 -070010480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010481 reclaimSomeSurfaceMemoryLocked(win, "show");
Romain Guy06882f82009-06-10 13:36:04 -070010482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010483 return false;
10484 }
Romain Guy06882f82009-06-10 13:36:04 -070010485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010486 void reclaimSomeSurfaceMemoryLocked(WindowState win, String operation) {
10487 final Surface surface = win.mSurface;
Romain Guy06882f82009-06-10 13:36:04 -070010488
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010489 EventLog.writeEvent(EventLogTags.WM_NO_SURFACE_MEMORY, win.toString(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010490 win.mSession.mPid, operation);
Romain Guy06882f82009-06-10 13:36:04 -070010491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010492 if (mForceRemoves == null) {
10493 mForceRemoves = new ArrayList<WindowState>();
10494 }
Romain Guy06882f82009-06-10 13:36:04 -070010495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010496 long callingIdentity = Binder.clearCallingIdentity();
10497 try {
10498 // There was some problem... first, do a sanity check of the
10499 // window list to make sure we haven't left any dangling surfaces
10500 // around.
10501 int N = mWindows.size();
10502 boolean leakedSurface = false;
10503 Log.i(TAG, "Out of memory for surface! Looking for leaks...");
10504 for (int i=0; i<N; i++) {
10505 WindowState ws = (WindowState)mWindows.get(i);
10506 if (ws.mSurface != null) {
10507 if (!mSessions.contains(ws.mSession)) {
10508 Log.w(TAG, "LEAKED SURFACE (session doesn't exist): "
10509 + ws + " surface=" + ws.mSurface
10510 + " token=" + win.mToken
10511 + " pid=" + ws.mSession.mPid
10512 + " uid=" + ws.mSession.mUid);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010513 ws.mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010514 ws.mSurface = null;
10515 mForceRemoves.add(ws);
10516 i--;
10517 N--;
10518 leakedSurface = true;
10519 } else if (win.mAppToken != null && win.mAppToken.clientHidden) {
10520 Log.w(TAG, "LEAKED SURFACE (app token hidden): "
10521 + ws + " surface=" + ws.mSurface
10522 + " token=" + win.mAppToken);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010523 ws.mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010524 ws.mSurface = null;
10525 leakedSurface = true;
10526 }
10527 }
10528 }
Romain Guy06882f82009-06-10 13:36:04 -070010529
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010530 boolean killedApps = false;
10531 if (!leakedSurface) {
10532 Log.w(TAG, "No leaked surfaces; killing applicatons!");
10533 SparseIntArray pidCandidates = new SparseIntArray();
10534 for (int i=0; i<N; i++) {
10535 WindowState ws = (WindowState)mWindows.get(i);
10536 if (ws.mSurface != null) {
10537 pidCandidates.append(ws.mSession.mPid, ws.mSession.mPid);
10538 }
10539 }
10540 if (pidCandidates.size() > 0) {
10541 int[] pids = new int[pidCandidates.size()];
10542 for (int i=0; i<pids.length; i++) {
10543 pids[i] = pidCandidates.keyAt(i);
10544 }
10545 try {
10546 if (mActivityManager.killPidsForMemory(pids)) {
10547 killedApps = true;
10548 }
10549 } catch (RemoteException e) {
10550 }
10551 }
10552 }
Romain Guy06882f82009-06-10 13:36:04 -070010553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010554 if (leakedSurface || killedApps) {
10555 // We managed to reclaim some memory, so get rid of the trouble
10556 // surface and ask the app to request another one.
10557 Log.w(TAG, "Looks like we have reclaimed some memory, clearing surface for retry.");
10558 if (surface != null) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010559 surface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010560 win.mSurface = null;
10561 }
Romain Guy06882f82009-06-10 13:36:04 -070010562
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010563 try {
10564 win.mClient.dispatchGetNewSurface();
10565 } catch (RemoteException e) {
10566 }
10567 }
10568 } finally {
10569 Binder.restoreCallingIdentity(callingIdentity);
10570 }
10571 }
Romain Guy06882f82009-06-10 13:36:04 -070010572
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010573 private boolean updateFocusedWindowLocked(int mode) {
10574 WindowState newFocus = computeFocusedWindowLocked();
10575 if (mCurrentFocus != newFocus) {
10576 // This check makes sure that we don't already have the focus
10577 // change message pending.
10578 mH.removeMessages(H.REPORT_FOCUS_CHANGE);
10579 mH.sendEmptyMessage(H.REPORT_FOCUS_CHANGE);
10580 if (localLOGV) Log.v(
10581 TAG, "Changing focus from " + mCurrentFocus + " to " + newFocus);
10582 final WindowState oldFocus = mCurrentFocus;
10583 mCurrentFocus = newFocus;
10584 mLosingFocus.remove(newFocus);
Romain Guy06882f82009-06-10 13:36:04 -070010585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010586 final WindowState imWindow = mInputMethodWindow;
10587 if (newFocus != imWindow && oldFocus != imWindow) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010588 if (moveInputMethodWindowsIfNeededLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010589 mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS &&
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010590 mode != UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
10591 mLayoutNeeded = true;
10592 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010593 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
10594 performLayoutLockedInner();
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010595 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
10596 // Client will do the layout, but we need to assign layers
10597 // for handleNewWindowLocked() below.
10598 assignLayersLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010599 }
10600 }
Romain Guy06882f82009-06-10 13:36:04 -070010601
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010602 if (newFocus != null && mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
10603 mKeyWaiter.handleNewWindowLocked(newFocus);
10604 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010605 return true;
10606 }
10607 return false;
10608 }
10609
10610 private WindowState computeFocusedWindowLocked() {
10611 WindowState result = null;
10612 WindowState win;
10613
10614 int i = mWindows.size() - 1;
10615 int nextAppIndex = mAppTokens.size()-1;
10616 WindowToken nextApp = nextAppIndex >= 0
10617 ? mAppTokens.get(nextAppIndex) : null;
10618
10619 while (i >= 0) {
10620 win = (WindowState)mWindows.get(i);
10621
10622 if (localLOGV || DEBUG_FOCUS) Log.v(
10623 TAG, "Looking for focus: " + i
10624 + " = " + win
10625 + ", flags=" + win.mAttrs.flags
10626 + ", canReceive=" + win.canReceiveKeys());
10627
10628 AppWindowToken thisApp = win.mAppToken;
Romain Guy06882f82009-06-10 13:36:04 -070010629
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010630 // If this window's application has been removed, just skip it.
10631 if (thisApp != null && thisApp.removed) {
10632 i--;
10633 continue;
10634 }
Romain Guy06882f82009-06-10 13:36:04 -070010635
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010636 // If there is a focused app, don't allow focus to go to any
10637 // windows below it. If this is an application window, step
10638 // through the app tokens until we find its app.
10639 if (thisApp != null && nextApp != null && thisApp != nextApp
10640 && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
10641 int origAppIndex = nextAppIndex;
10642 while (nextAppIndex > 0) {
10643 if (nextApp == mFocusedApp) {
10644 // Whoops, we are below the focused app... no focus
10645 // for you!
10646 if (localLOGV || DEBUG_FOCUS) Log.v(
10647 TAG, "Reached focused app: " + mFocusedApp);
10648 return null;
10649 }
10650 nextAppIndex--;
10651 nextApp = mAppTokens.get(nextAppIndex);
10652 if (nextApp == thisApp) {
10653 break;
10654 }
10655 }
10656 if (thisApp != nextApp) {
10657 // Uh oh, the app token doesn't exist! This shouldn't
10658 // happen, but if it does we can get totally hosed...
10659 // so restart at the original app.
10660 nextAppIndex = origAppIndex;
10661 nextApp = mAppTokens.get(nextAppIndex);
10662 }
10663 }
10664
10665 // Dispatch to this window if it is wants key events.
10666 if (win.canReceiveKeys()) {
10667 if (DEBUG_FOCUS) Log.v(
10668 TAG, "Found focus @ " + i + " = " + win);
10669 result = win;
10670 break;
10671 }
10672
10673 i--;
10674 }
10675
10676 return result;
10677 }
10678
10679 private void startFreezingDisplayLocked() {
10680 if (mDisplayFrozen) {
Chris Tate2ad63a92009-03-25 17:36:48 -070010681 // Freezing the display also suspends key event delivery, to
10682 // keep events from going astray while the display is reconfigured.
10683 // If someone has changed orientation again while the screen is
10684 // still frozen, the events will continue to be blocked while the
10685 // successive orientation change is processed. To prevent spurious
10686 // ANRs, we reset the event dispatch timeout in this case.
10687 synchronized (mKeyWaiter) {
10688 mKeyWaiter.mWasFrozen = true;
10689 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010690 return;
10691 }
Romain Guy06882f82009-06-10 13:36:04 -070010692
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010693 mScreenFrozenLock.acquire();
Romain Guy06882f82009-06-10 13:36:04 -070010694
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010695 long now = SystemClock.uptimeMillis();
10696 //Log.i(TAG, "Freezing, gc pending: " + mFreezeGcPending + ", now " + now);
10697 if (mFreezeGcPending != 0) {
10698 if (now > (mFreezeGcPending+1000)) {
10699 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
10700 mH.removeMessages(H.FORCE_GC);
10701 Runtime.getRuntime().gc();
10702 mFreezeGcPending = now;
10703 }
10704 } else {
10705 mFreezeGcPending = now;
10706 }
Romain Guy06882f82009-06-10 13:36:04 -070010707
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010708 mDisplayFrozen = true;
Dianne Hackbornbfe319e2009-09-21 00:34:05 -070010709 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
10710 mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010711 mNextAppTransitionPackage = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010712 mAppTransitionReady = true;
10713 }
Romain Guy06882f82009-06-10 13:36:04 -070010714
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010715 if (PROFILE_ORIENTATION) {
10716 File file = new File("/data/system/frozen");
10717 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
10718 }
10719 Surface.freezeDisplay(0);
10720 }
Romain Guy06882f82009-06-10 13:36:04 -070010721
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010722 private void stopFreezingDisplayLocked() {
10723 if (!mDisplayFrozen) {
10724 return;
10725 }
Romain Guy06882f82009-06-10 13:36:04 -070010726
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010727 mDisplayFrozen = false;
10728 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
10729 if (PROFILE_ORIENTATION) {
10730 Debug.stopMethodTracing();
10731 }
10732 Surface.unfreezeDisplay(0);
Romain Guy06882f82009-06-10 13:36:04 -070010733
Chris Tate2ad63a92009-03-25 17:36:48 -070010734 // Reset the key delivery timeout on unfreeze, too. We force a wakeup here
10735 // too because regular key delivery processing should resume immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010736 synchronized (mKeyWaiter) {
10737 mKeyWaiter.mWasFrozen = true;
10738 mKeyWaiter.notifyAll();
10739 }
10740
10741 // A little kludge: a lot could have happened while the
10742 // display was frozen, so now that we are coming back we
10743 // do a gc so that any remote references the system
10744 // processes holds on others can be released if they are
10745 // no longer needed.
10746 mH.removeMessages(H.FORCE_GC);
10747 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
10748 2000);
Romain Guy06882f82009-06-10 13:36:04 -070010749
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010750 mScreenFrozenLock.release();
10751 }
Romain Guy06882f82009-06-10 13:36:04 -070010752
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010753 @Override
10754 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
10755 if (mContext.checkCallingOrSelfPermission("android.permission.DUMP")
10756 != PackageManager.PERMISSION_GRANTED) {
10757 pw.println("Permission Denial: can't dump WindowManager from from pid="
10758 + Binder.getCallingPid()
10759 + ", uid=" + Binder.getCallingUid());
10760 return;
10761 }
Romain Guy06882f82009-06-10 13:36:04 -070010762
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010763 synchronized(mWindowMap) {
10764 pw.println("Current Window Manager state:");
10765 for (int i=mWindows.size()-1; i>=0; i--) {
10766 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010767 pw.print(" Window #"); pw.print(i); pw.print(' ');
10768 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010769 w.dump(pw, " ");
10770 }
10771 if (mInputMethodDialogs.size() > 0) {
10772 pw.println(" ");
10773 pw.println(" Input method dialogs:");
10774 for (int i=mInputMethodDialogs.size()-1; i>=0; i--) {
10775 WindowState w = mInputMethodDialogs.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010776 pw.print(" IM Dialog #"); pw.print(i); pw.print(": "); pw.println(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010777 }
10778 }
10779 if (mPendingRemove.size() > 0) {
10780 pw.println(" ");
10781 pw.println(" Remove pending for:");
10782 for (int i=mPendingRemove.size()-1; i>=0; i--) {
10783 WindowState w = mPendingRemove.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010784 pw.print(" Remove #"); pw.print(i); pw.print(' ');
10785 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010786 w.dump(pw, " ");
10787 }
10788 }
10789 if (mForceRemoves != null && mForceRemoves.size() > 0) {
10790 pw.println(" ");
10791 pw.println(" Windows force removing:");
10792 for (int i=mForceRemoves.size()-1; i>=0; i--) {
10793 WindowState w = mForceRemoves.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010794 pw.print(" Removing #"); pw.print(i); pw.print(' ');
10795 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010796 w.dump(pw, " ");
10797 }
10798 }
10799 if (mDestroySurface.size() > 0) {
10800 pw.println(" ");
10801 pw.println(" Windows waiting to destroy their surface:");
10802 for (int i=mDestroySurface.size()-1; i>=0; i--) {
10803 WindowState w = mDestroySurface.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010804 pw.print(" Destroy #"); pw.print(i); pw.print(' ');
10805 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010806 w.dump(pw, " ");
10807 }
10808 }
10809 if (mLosingFocus.size() > 0) {
10810 pw.println(" ");
10811 pw.println(" Windows losing focus:");
10812 for (int i=mLosingFocus.size()-1; i>=0; i--) {
10813 WindowState w = mLosingFocus.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010814 pw.print(" Losing #"); pw.print(i); pw.print(' ');
10815 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010816 w.dump(pw, " ");
10817 }
10818 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010819 if (mResizingWindows.size() > 0) {
10820 pw.println(" ");
10821 pw.println(" Windows waiting to resize:");
10822 for (int i=mResizingWindows.size()-1; i>=0; i--) {
10823 WindowState w = mResizingWindows.get(i);
10824 pw.print(" Resizing #"); pw.print(i); pw.print(' ');
10825 pw.print(w); pw.println(":");
10826 w.dump(pw, " ");
10827 }
10828 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010829 if (mSessions.size() > 0) {
10830 pw.println(" ");
10831 pw.println(" All active sessions:");
10832 Iterator<Session> it = mSessions.iterator();
10833 while (it.hasNext()) {
10834 Session s = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010835 pw.print(" Session "); pw.print(s); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010836 s.dump(pw, " ");
10837 }
10838 }
10839 if (mTokenMap.size() > 0) {
10840 pw.println(" ");
10841 pw.println(" All tokens:");
10842 Iterator<WindowToken> it = mTokenMap.values().iterator();
10843 while (it.hasNext()) {
10844 WindowToken token = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010845 pw.print(" Token "); pw.print(token.token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010846 token.dump(pw, " ");
10847 }
10848 }
10849 if (mTokenList.size() > 0) {
10850 pw.println(" ");
10851 pw.println(" Window token list:");
10852 for (int i=0; i<mTokenList.size(); i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010853 pw.print(" #"); pw.print(i); pw.print(": ");
10854 pw.println(mTokenList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010855 }
10856 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070010857 if (mWallpaperTokens.size() > 0) {
10858 pw.println(" ");
10859 pw.println(" Wallpaper tokens:");
10860 for (int i=mWallpaperTokens.size()-1; i>=0; i--) {
10861 WindowToken token = mWallpaperTokens.get(i);
10862 pw.print(" Wallpaper #"); pw.print(i);
10863 pw.print(' '); pw.print(token); pw.println(':');
10864 token.dump(pw, " ");
10865 }
10866 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010867 if (mAppTokens.size() > 0) {
10868 pw.println(" ");
10869 pw.println(" Application tokens in Z order:");
10870 for (int i=mAppTokens.size()-1; i>=0; i--) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010871 pw.print(" App #"); pw.print(i); pw.print(": ");
10872 pw.println(mAppTokens.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010873 }
10874 }
10875 if (mFinishedStarting.size() > 0) {
10876 pw.println(" ");
10877 pw.println(" Finishing start of application tokens:");
10878 for (int i=mFinishedStarting.size()-1; i>=0; i--) {
10879 WindowToken token = mFinishedStarting.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010880 pw.print(" Finished Starting #"); pw.print(i);
10881 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010882 token.dump(pw, " ");
10883 }
10884 }
10885 if (mExitingTokens.size() > 0) {
10886 pw.println(" ");
10887 pw.println(" Exiting tokens:");
10888 for (int i=mExitingTokens.size()-1; i>=0; i--) {
10889 WindowToken token = mExitingTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010890 pw.print(" Exiting #"); pw.print(i);
10891 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010892 token.dump(pw, " ");
10893 }
10894 }
10895 if (mExitingAppTokens.size() > 0) {
10896 pw.println(" ");
10897 pw.println(" Exiting application tokens:");
10898 for (int i=mExitingAppTokens.size()-1; i>=0; i--) {
10899 WindowToken token = mExitingAppTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010900 pw.print(" Exiting App #"); pw.print(i);
10901 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010902 token.dump(pw, " ");
10903 }
10904 }
10905 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010906 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
10907 pw.print(" mLastFocus="); pw.println(mLastFocus);
10908 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
10909 pw.print(" mInputMethodTarget="); pw.println(mInputMethodTarget);
10910 pw.print(" mInputMethodWindow="); pw.println(mInputMethodWindow);
Dianne Hackbornf21adf62009-08-13 10:20:21 -070010911 pw.print(" mWallpaperTarget="); pw.println(mWallpaperTarget);
Dianne Hackborn284ac932009-08-28 10:34:25 -070010912 if (mLowerWallpaperTarget != null && mUpperWallpaperTarget != null) {
10913 pw.print(" mLowerWallpaperTarget="); pw.println(mLowerWallpaperTarget);
10914 pw.print(" mUpperWallpaperTarget="); pw.println(mUpperWallpaperTarget);
10915 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010916 pw.print(" mInTouchMode="); pw.println(mInTouchMode);
10917 pw.print(" mSystemBooted="); pw.print(mSystemBooted);
10918 pw.print(" mDisplayEnabled="); pw.println(mDisplayEnabled);
10919 pw.print(" mLayoutNeeded="); pw.print(mLayoutNeeded);
10920 pw.print(" mBlurShown="); pw.println(mBlurShown);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010921 if (mDimAnimator != null) {
10922 mDimAnimator.printTo(pw);
10923 } else {
10924 pw.print( " no DimAnimator ");
10925 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010926 pw.print(" mInputMethodAnimLayerAdjustment=");
Dianne Hackborn759a39e2009-08-09 17:20:27 -070010927 pw.print(mInputMethodAnimLayerAdjustment);
10928 pw.print(" mWallpaperAnimLayerAdjustment=");
10929 pw.println(mWallpaperAnimLayerAdjustment);
Dianne Hackborn284ac932009-08-28 10:34:25 -070010930 pw.print(" mLastWallpaperX="); pw.print(mLastWallpaperX);
10931 pw.print(" mLastWallpaperY="); pw.println(mLastWallpaperY);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010932 pw.print(" mDisplayFrozen="); pw.print(mDisplayFrozen);
10933 pw.print(" mWindowsFreezingScreen="); pw.print(mWindowsFreezingScreen);
10934 pw.print(" mAppsFreezingScreen="); pw.println(mAppsFreezingScreen);
10935 pw.print(" mRotation="); pw.print(mRotation);
10936 pw.print(", mForcedAppOrientation="); pw.print(mForcedAppOrientation);
10937 pw.print(", mRequestedRotation="); pw.println(mRequestedRotation);
10938 pw.print(" mAnimationPending="); pw.print(mAnimationPending);
10939 pw.print(" mWindowAnimationScale="); pw.print(mWindowAnimationScale);
10940 pw.print(" mTransitionWindowAnimationScale="); pw.println(mTransitionAnimationScale);
10941 pw.print(" mNextAppTransition=0x");
10942 pw.print(Integer.toHexString(mNextAppTransition));
10943 pw.print(", mAppTransitionReady="); pw.print(mAppTransitionReady);
Dianne Hackborna8f60182009-09-01 19:01:50 -070010944 pw.print(", mAppTransitionRunning="); pw.print(mAppTransitionRunning);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010945 pw.print(", mAppTransitionTimeout="); pw.println( mAppTransitionTimeout);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010946 if (mNextAppTransitionPackage != null) {
10947 pw.print(" mNextAppTransitionPackage=");
10948 pw.print(mNextAppTransitionPackage);
10949 pw.print(", mNextAppTransitionEnter=0x");
10950 pw.print(Integer.toHexString(mNextAppTransitionEnter));
10951 pw.print(", mNextAppTransitionExit=0x");
10952 pw.print(Integer.toHexString(mNextAppTransitionExit));
10953 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010954 pw.print(" mStartingIconInTransition="); pw.print(mStartingIconInTransition);
10955 pw.print(", mSkipAppTransitionAnimation="); pw.println(mSkipAppTransitionAnimation);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010956 if (mLastEnterAnimToken != null || mLastEnterAnimToken != null) {
10957 pw.print(" mLastEnterAnimToken="); pw.print(mLastEnterAnimToken);
10958 pw.print(", mLastEnterAnimParams="); pw.println(mLastEnterAnimParams);
10959 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010960 if (mOpeningApps.size() > 0) {
10961 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
10962 }
10963 if (mClosingApps.size() > 0) {
10964 pw.print(" mClosingApps="); pw.println(mClosingApps);
10965 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070010966 if (mToTopApps.size() > 0) {
10967 pw.print(" mToTopApps="); pw.println(mToTopApps);
10968 }
10969 if (mToBottomApps.size() > 0) {
10970 pw.print(" mToBottomApps="); pw.println(mToBottomApps);
10971 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010972 pw.print(" DisplayWidth="); pw.print(mDisplay.getWidth());
10973 pw.print(" DisplayHeight="); pw.println(mDisplay.getHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010974 pw.println(" KeyWaiter state:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010975 pw.print(" mLastWin="); pw.print(mKeyWaiter.mLastWin);
10976 pw.print(" mLastBinder="); pw.println(mKeyWaiter.mLastBinder);
10977 pw.print(" mFinished="); pw.print(mKeyWaiter.mFinished);
10978 pw.print(" mGotFirstWindow="); pw.print(mKeyWaiter.mGotFirstWindow);
10979 pw.print(" mEventDispatching="); pw.print(mKeyWaiter.mEventDispatching);
10980 pw.print(" mTimeToSwitch="); pw.println(mKeyWaiter.mTimeToSwitch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010981 }
10982 }
10983
10984 public void monitor() {
10985 synchronized (mWindowMap) { }
Mike Lockwood983ee092009-11-22 01:42:24 -050010986 synchronized (mKeyguardTokenWatcher) { }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010987 synchronized (mKeyWaiter) { }
10988 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010989
Dianne Hackbornddca3ee2009-07-23 19:01:31 -070010990 public void virtualKeyFeedback(KeyEvent event) {
10991 mPolicy.keyFeedbackFromInput(event);
10992 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010993
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010994 /**
10995 * DimAnimator class that controls the dim animation. This holds the surface and
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010996 * all state used for dim animation.
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010997 */
10998 private static class DimAnimator {
10999 Surface mDimSurface;
11000 boolean mDimShown = false;
11001 float mDimCurrentAlpha;
11002 float mDimTargetAlpha;
11003 float mDimDeltaPerMs;
11004 long mLastDimAnimTime;
11005
11006 DimAnimator (SurfaceSession session) {
11007 if (mDimSurface == null) {
11008 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
11009 + mDimSurface + ": CREATE");
11010 try {
11011 mDimSurface = new Surface(session, 0, -1, 16, 16, PixelFormat.OPAQUE,
11012 Surface.FX_SURFACE_DIM);
11013 } catch (Exception e) {
11014 Log.e(TAG, "Exception creating Dim surface", e);
11015 }
11016 }
11017 }
11018
11019 /**
11020 * Show the dim surface.
11021 */
11022 void show(int dw, int dh) {
11023 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface + ": SHOW pos=(0,0) (" +
11024 dw + "x" + dh + ")");
11025 mDimShown = true;
11026 try {
11027 mDimSurface.setPosition(0, 0);
11028 mDimSurface.setSize(dw, dh);
11029 mDimSurface.show();
11030 } catch (RuntimeException e) {
11031 Log.w(TAG, "Failure showing dim surface", e);
11032 }
11033 }
11034
11035 /**
11036 * Set's the dim surface's layer and update dim parameters that will be used in
11037 * {@link updateSurface} after all windows are examined.
11038 */
11039 void updateParameters(WindowState w, long currentTime) {
11040 mDimSurface.setLayer(w.mAnimLayer-1);
11041
11042 final float target = w.mExiting ? 0 : w.mAttrs.dimAmount;
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070011043 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
11044 + ": layer=" + (w.mAnimLayer-1) + " target=" + target);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011045 if (mDimTargetAlpha != target) {
11046 // If the desired dim level has changed, then
11047 // start an animation to it.
11048 mLastDimAnimTime = currentTime;
11049 long duration = (w.mAnimating && w.mAnimation != null)
11050 ? w.mAnimation.computeDurationHint()
11051 : DEFAULT_DIM_DURATION;
11052 if (target > mDimTargetAlpha) {
11053 // This is happening behind the activity UI,
11054 // so we can make it run a little longer to
11055 // give a stronger impression without disrupting
11056 // the user.
11057 duration *= DIM_DURATION_MULTIPLIER;
11058 }
11059 if (duration < 1) {
11060 // Don't divide by zero
11061 duration = 1;
11062 }
11063 mDimTargetAlpha = target;
11064 mDimDeltaPerMs = (mDimTargetAlpha-mDimCurrentAlpha) / duration;
11065 }
11066 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080011067
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011068 /**
11069 * Updating the surface's alpha. Returns true if the animation continues, or returns
11070 * false when the animation is finished and the dim surface is hidden.
11071 */
11072 boolean updateSurface(boolean dimming, long currentTime, boolean displayFrozen) {
11073 if (!dimming) {
11074 if (mDimTargetAlpha != 0) {
11075 mLastDimAnimTime = currentTime;
11076 mDimTargetAlpha = 0;
11077 mDimDeltaPerMs = (-mDimCurrentAlpha) / DEFAULT_DIM_DURATION;
11078 }
11079 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080011080
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011081 boolean animating = false;
11082 if (mLastDimAnimTime != 0) {
11083 mDimCurrentAlpha += mDimDeltaPerMs
11084 * (currentTime-mLastDimAnimTime);
11085 boolean more = true;
11086 if (displayFrozen) {
11087 // If the display is frozen, there is no reason to animate.
11088 more = false;
11089 } else if (mDimDeltaPerMs > 0) {
11090 if (mDimCurrentAlpha > mDimTargetAlpha) {
11091 more = false;
11092 }
11093 } else if (mDimDeltaPerMs < 0) {
11094 if (mDimCurrentAlpha < mDimTargetAlpha) {
11095 more = false;
11096 }
11097 } else {
11098 more = false;
11099 }
11100
11101 // Do we need to continue animating?
11102 if (more) {
11103 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
11104 + mDimSurface + ": alpha=" + mDimCurrentAlpha);
11105 mLastDimAnimTime = currentTime;
11106 mDimSurface.setAlpha(mDimCurrentAlpha);
11107 animating = true;
11108 } else {
11109 mDimCurrentAlpha = mDimTargetAlpha;
11110 mLastDimAnimTime = 0;
11111 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
11112 + mDimSurface + ": final alpha=" + mDimCurrentAlpha);
11113 mDimSurface.setAlpha(mDimCurrentAlpha);
11114 if (!dimming) {
11115 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
11116 + ": HIDE");
11117 try {
11118 mDimSurface.hide();
11119 } catch (RuntimeException e) {
11120 Log.w(TAG, "Illegal argument exception hiding dim surface");
11121 }
11122 mDimShown = false;
11123 }
11124 }
11125 }
11126 return animating;
11127 }
11128
11129 public void printTo(PrintWriter pw) {
11130 pw.print(" mDimShown="); pw.print(mDimShown);
11131 pw.print(" current="); pw.print(mDimCurrentAlpha);
11132 pw.print(" target="); pw.print(mDimTargetAlpha);
11133 pw.print(" delta="); pw.print(mDimDeltaPerMs);
11134 pw.print(" lastAnimTime="); pw.println(mLastDimAnimTime);
11135 }
11136 }
11137
11138 /**
11139 * Animation that fade in after 0.5 interpolate time, or fade out in reverse order.
11140 * This is used for opening/closing transition for apps in compatible mode.
11141 */
11142 private static class FadeInOutAnimation extends Animation {
11143 int mWidth;
11144 boolean mFadeIn;
11145
11146 public FadeInOutAnimation(boolean fadeIn) {
11147 setInterpolator(new AccelerateInterpolator());
11148 setDuration(DEFAULT_FADE_IN_OUT_DURATION);
11149 mFadeIn = fadeIn;
11150 }
11151
11152 @Override
11153 protected void applyTransformation(float interpolatedTime, Transformation t) {
11154 float x = interpolatedTime;
11155 if (!mFadeIn) {
11156 x = 1.0f - x; // reverse the interpolation for fade out
11157 }
11158 if (x < 0.5) {
11159 // move the window out of the screen.
11160 t.getMatrix().setTranslate(mWidth, 0);
11161 } else {
11162 t.getMatrix().setTranslate(0, 0);// show
11163 t.setAlpha((x - 0.5f) * 2);
11164 }
11165 }
11166
11167 @Override
11168 public void initialize(int width, int height, int parentWidth, int parentHeight) {
11169 // width is the screen width {@see AppWindowToken#stepAnimatinoLocked}
11170 mWidth = width;
11171 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011172
11173 @Override
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -070011174 public int getZAdjustment() {
11175 return Animation.ZORDER_TOP;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011176 }
11177 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011178}