blob: 11b6310f1c297a60869a0a5dc2fd543a87d0782c [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;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080046import com.android.internal.policy.impl.PhoneWindowManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import com.android.internal.view.IInputContext;
48import com.android.internal.view.IInputMethodClient;
49import com.android.internal.view.IInputMethodManager;
Dianne Hackbornac3587d2010-03-11 11:12:11 -080050import com.android.internal.view.WindowManagerPolicyThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import com.android.server.KeyInputQueue.QueuedEvent;
52import com.android.server.am.BatteryStatsService;
53
54import android.Manifest;
55import android.app.ActivityManagerNative;
56import android.app.IActivityManager;
Jim Millerd6b57052010-06-07 17:52:42 -070057import android.app.admin.DevicePolicyManager;
Jim Miller284b62e2010-06-08 14:27:42 -070058import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.content.Context;
Jim Miller284b62e2010-06-08 14:27:42 -070060import android.content.Intent;
61import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062import android.content.pm.ActivityInfo;
63import android.content.pm.PackageManager;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070064import android.content.res.CompatibilityInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.content.res.Configuration;
66import android.graphics.Matrix;
67import android.graphics.PixelFormat;
68import android.graphics.Rect;
69import android.graphics.Region;
70import android.os.BatteryStats;
71import android.os.Binder;
Dianne Hackborn75804932009-10-20 20:15:20 -070072import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073import android.os.Debug;
74import android.os.Handler;
75import android.os.IBinder;
Michael Chan53071d62009-05-13 17:29:48 -070076import android.os.LatencyTimer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import android.os.LocalPowerManager;
78import android.os.Looper;
79import android.os.Message;
80import android.os.Parcel;
81import android.os.ParcelFileDescriptor;
82import android.os.Power;
83import android.os.PowerManager;
84import android.os.Process;
85import android.os.RemoteException;
86import android.os.ServiceManager;
87import android.os.SystemClock;
88import android.os.SystemProperties;
89import android.os.TokenWatcher;
90import android.provider.Settings;
Dianne Hackborn723738c2009-06-25 19:48:04 -070091import android.util.DisplayMetrics;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092import android.util.EventLog;
Jim Millerd6b57052010-06-07 17:52:42 -070093import android.util.Log;
Joe Onorato8a9b2202010-02-26 18:56:32 -080094import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095import android.util.SparseIntArray;
96import android.view.Display;
97import android.view.Gravity;
98import android.view.IApplicationToken;
99import android.view.IOnKeyguardExitResult;
100import android.view.IRotationWatcher;
101import android.view.IWindow;
102import android.view.IWindowManager;
103import android.view.IWindowSession;
104import android.view.KeyEvent;
105import android.view.MotionEvent;
106import android.view.RawInputEvent;
107import android.view.Surface;
108import android.view.SurfaceSession;
109import android.view.View;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -0700110import android.view.ViewConfiguration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111import android.view.ViewTreeObserver;
112import android.view.WindowManager;
113import android.view.WindowManagerImpl;
114import android.view.WindowManagerPolicy;
115import android.view.WindowManager.LayoutParams;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700116import android.view.animation.AccelerateInterpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117import android.view.animation.Animation;
118import android.view.animation.AnimationUtils;
119import android.view.animation.Transformation;
120
121import java.io.BufferedWriter;
122import java.io.File;
123import java.io.FileDescriptor;
124import java.io.IOException;
125import java.io.OutputStream;
126import java.io.OutputStreamWriter;
127import java.io.PrintWriter;
128import java.io.StringWriter;
129import java.net.Socket;
130import java.util.ArrayList;
131import java.util.HashMap;
132import java.util.HashSet;
133import java.util.Iterator;
134import java.util.List;
135
136/** {@hide} */
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700137public class WindowManagerService extends IWindowManager.Stub
138 implements Watchdog.Monitor, KeyInputQueue.HapticFeedbackCallback {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 static final String TAG = "WindowManager";
140 static final boolean DEBUG = false;
141 static final boolean DEBUG_FOCUS = false;
142 static final boolean DEBUG_ANIM = false;
Dianne Hackborn9b52a212009-12-11 14:51:35 -0800143 static final boolean DEBUG_LAYOUT = false;
Dianne Hackbornac3587d2010-03-11 11:12:11 -0800144 static final boolean DEBUG_RESIZE = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 static final boolean DEBUG_LAYERS = false;
146 static final boolean DEBUG_INPUT = false;
147 static final boolean DEBUG_INPUT_METHOD = false;
148 static final boolean DEBUG_VISIBILITY = false;
Dianne Hackbornbdd52b22009-09-02 21:46:19 -0700149 static final boolean DEBUG_WINDOW_MOVEMENT = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 static final boolean DEBUG_ORIENTATION = false;
Dianne Hackborn694f79b2010-03-17 19:44:59 -0700151 static final boolean DEBUG_CONFIGURATION = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 static final boolean DEBUG_APP_TRANSITIONS = false;
153 static final boolean DEBUG_STARTING_WINDOW = false;
154 static final boolean DEBUG_REORDER = false;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -0700155 static final boolean DEBUG_WALLPAPER = false;
Dianne Hackbornce73c1e2010-04-12 23:11:38 -0700156 static final boolean DEBUG_FREEZE = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 static final boolean SHOW_TRANSACTIONS = false;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700158 static final boolean HIDE_STACK_CRAWLS = true;
Michael Chan53071d62009-05-13 17:29:48 -0700159 static final boolean MEASURE_LATENCY = false;
160 static private LatencyTimer lt;
161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 static final boolean PROFILE_ORIENTATION = false;
163 static final boolean BLUR = true;
Dave Bortcfe65242009-04-09 14:51:04 -0700164 static final boolean localLOGV = DEBUG;
Romain Guy06882f82009-06-10 13:36:04 -0700165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 /** How long to wait for subsequent key repeats, in milliseconds */
167 static final int KEY_REPEAT_DELAY = 50;
168
169 /** How much to multiply the policy's type layer, to reserve room
170 * for multiple windows of the same type and Z-ordering adjustment
171 * with TYPE_LAYER_OFFSET. */
172 static final int TYPE_LAYER_MULTIPLIER = 10000;
Romain Guy06882f82009-06-10 13:36:04 -0700173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 /** Offset from TYPE_LAYER_MULTIPLIER for moving a group of windows above
175 * or below others in the same layer. */
176 static final int TYPE_LAYER_OFFSET = 1000;
Romain Guy06882f82009-06-10 13:36:04 -0700177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 /** How much to increment the layer for each window, to reserve room
179 * for effect surfaces between them.
180 */
181 static final int WINDOW_LAYER_MULTIPLIER = 5;
Romain Guy06882f82009-06-10 13:36:04 -0700182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 /** The maximum length we will accept for a loaded animation duration:
184 * this is 10 seconds.
185 */
186 static final int MAX_ANIMATION_DURATION = 10*1000;
187
188 /** Amount of time (in milliseconds) to animate the dim surface from one
189 * value to another, when no window animation is driving it.
190 */
191 static final int DEFAULT_DIM_DURATION = 200;
192
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700193 /** Amount of time (in milliseconds) to animate the fade-in-out transition for
194 * compatible windows.
195 */
196 static final int DEFAULT_FADE_IN_OUT_DURATION = 400;
197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 /** Adjustment to time to perform a dim, to make it more dramatic.
199 */
200 static final int DIM_DURATION_MULTIPLIER = 6;
Romain Guy06882f82009-06-10 13:36:04 -0700201
Dianne Hackborncfaef692009-06-15 14:24:44 -0700202 static final int INJECT_FAILED = 0;
203 static final int INJECT_SUCCEEDED = 1;
204 static final int INJECT_NO_PERMISSION = -1;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 static final int UPDATE_FOCUS_NORMAL = 0;
207 static final int UPDATE_FOCUS_WILL_ASSIGN_LAYERS = 1;
208 static final int UPDATE_FOCUS_PLACING_SURFACES = 2;
209 static final int UPDATE_FOCUS_WILL_PLACE_SURFACES = 3;
Romain Guy06882f82009-06-10 13:36:04 -0700210
Michael Chane96440f2009-05-06 10:27:36 -0700211 /** The minimum time between dispatching touch events. */
212 int mMinWaitTimeBetweenTouchEvents = 1000 / 35;
213
214 // Last touch event time
215 long mLastTouchEventTime = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700216
Michael Chane96440f2009-05-06 10:27:36 -0700217 // Last touch event type
218 int mLastTouchEventType = OTHER_EVENT;
Romain Guy06882f82009-06-10 13:36:04 -0700219
Michael Chane96440f2009-05-06 10:27:36 -0700220 // Time to wait before calling useractivity again. This saves CPU usage
221 // when we get a flood of touch events.
222 static final int MIN_TIME_BETWEEN_USERACTIVITIES = 1000;
223
224 // Last time we call user activity
225 long mLastUserActivityCallTime = 0;
226
Romain Guy06882f82009-06-10 13:36:04 -0700227 // Last time we updated battery stats
Michael Chane96440f2009-05-06 10:27:36 -0700228 long mLastBatteryStatsCallTime = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 private static final String SYSTEM_SECURE = "ro.secure";
Romain Guy06882f82009-06-10 13:36:04 -0700231 private static final String SYSTEM_DEBUGGABLE = "ro.debuggable";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232
233 /**
234 * Condition waited on by {@link #reenableKeyguard} to know the call to
235 * the window policy has finished.
Mike Lockwood983ee092009-11-22 01:42:24 -0500236 * This is set to true only if mKeyguardTokenWatcher.acquired() has
237 * actually disabled the keyguard.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 */
Mike Lockwood983ee092009-11-22 01:42:24 -0500239 private boolean mKeyguardDisabled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240
Jim Miller284b62e2010-06-08 14:27:42 -0700241 private static final int ALLOW_DISABLE_YES = 1;
242 private static final int ALLOW_DISABLE_NO = 0;
243 private static final int ALLOW_DISABLE_UNKNOWN = -1; // check with DevicePolicyManager
244 private int mAllowDisableKeyguard = ALLOW_DISABLE_UNKNOWN; // sync'd by mKeyguardTokenWatcher
245
Mike Lockwood983ee092009-11-22 01:42:24 -0500246 final TokenWatcher mKeyguardTokenWatcher = new TokenWatcher(
247 new Handler(), "WindowManagerService.mKeyguardTokenWatcher") {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 public void acquired() {
Jim Miller284b62e2010-06-08 14:27:42 -0700249 if (shouldAllowDisableKeyguard()) {
250 mPolicy.enableKeyguard(false);
251 mKeyguardDisabled = true;
252 } else {
253 Log.v(TAG, "Not disabling keyguard since device policy is enforced");
254 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 }
256 public void released() {
Dianne Hackborna33e3f72009-09-29 17:28:24 -0700257 mPolicy.enableKeyguard(true);
Mike Lockwood983ee092009-11-22 01:42:24 -0500258 synchronized (mKeyguardTokenWatcher) {
259 mKeyguardDisabled = false;
260 mKeyguardTokenWatcher.notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 }
262 }
263 };
264
Jim Miller284b62e2010-06-08 14:27:42 -0700265 final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
266 @Override
267 public void onReceive(Context context, Intent intent) {
268 mPolicy.enableKeyguard(true);
269 synchronized(mKeyguardTokenWatcher) {
270 // lazily evaluate this next time we're asked to disable keyguard
271 mAllowDisableKeyguard = ALLOW_DISABLE_UNKNOWN;
272 mKeyguardDisabled = false;
273 }
274 }
275 };
276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 final Context mContext;
278
279 final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 final boolean mLimitedAlphaCompositing;
Romain Guy06882f82009-06-10 13:36:04 -0700282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 final WindowManagerPolicy mPolicy = PolicyManager.makeNewWindowManager();
284
285 final IActivityManager mActivityManager;
Romain Guy06882f82009-06-10 13:36:04 -0700286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 final IBatteryStats mBatteryStats;
Romain Guy06882f82009-06-10 13:36:04 -0700288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 /**
290 * All currently active sessions with clients.
291 */
292 final HashSet<Session> mSessions = new HashSet<Session>();
Romain Guy06882f82009-06-10 13:36:04 -0700293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 /**
295 * Mapping from an IWindow IBinder to the server's Window object.
296 * This is also used as the lock for all of our state.
297 */
298 final HashMap<IBinder, WindowState> mWindowMap = new HashMap<IBinder, WindowState>();
299
300 /**
301 * Mapping from a token IBinder to a WindowToken object.
302 */
303 final HashMap<IBinder, WindowToken> mTokenMap =
304 new HashMap<IBinder, WindowToken>();
305
306 /**
307 * The same tokens as mTokenMap, stored in a list for efficient iteration
308 * over them.
309 */
310 final ArrayList<WindowToken> mTokenList = new ArrayList<WindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 /**
313 * Window tokens that are in the process of exiting, but still
314 * on screen for animations.
315 */
316 final ArrayList<WindowToken> mExitingTokens = new ArrayList<WindowToken>();
317
318 /**
319 * Z-ordered (bottom-most first) list of all application tokens, for
320 * controlling the ordering of windows in different applications. This
321 * contains WindowToken objects.
322 */
323 final ArrayList<AppWindowToken> mAppTokens = new ArrayList<AppWindowToken>();
324
325 /**
326 * Application tokens that are in the process of exiting, but still
327 * on screen for animations.
328 */
329 final ArrayList<AppWindowToken> mExitingAppTokens = new ArrayList<AppWindowToken>();
330
331 /**
332 * List of window tokens that have finished starting their application,
333 * and now need to have the policy remove their windows.
334 */
335 final ArrayList<AppWindowToken> mFinishedStarting = new ArrayList<AppWindowToken>();
336
337 /**
Dianne Hackborn3b3e1452009-09-24 19:22:12 -0700338 * This was the app token that was used to retrieve the last enter
339 * animation. It will be used for the next exit animation.
340 */
341 AppWindowToken mLastEnterAnimToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800342
Dianne Hackborn3b3e1452009-09-24 19:22:12 -0700343 /**
344 * These were the layout params used to retrieve the last enter animation.
345 * They will be used for the next exit animation.
346 */
347 LayoutParams mLastEnterAnimParams;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800348
Dianne Hackborn3b3e1452009-09-24 19:22:12 -0700349 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 * Z-ordered (bottom-most first) list of all Window objects.
351 */
352 final ArrayList mWindows = new ArrayList();
353
354 /**
355 * Windows that are being resized. Used so we can tell the client about
356 * the resize after closing the transaction in which we resized the
357 * underlying surface.
358 */
359 final ArrayList<WindowState> mResizingWindows = new ArrayList<WindowState>();
360
361 /**
362 * Windows whose animations have ended and now must be removed.
363 */
364 final ArrayList<WindowState> mPendingRemove = new ArrayList<WindowState>();
365
366 /**
367 * Windows whose surface should be destroyed.
368 */
369 final ArrayList<WindowState> mDestroySurface = new ArrayList<WindowState>();
370
371 /**
372 * Windows that have lost input focus and are waiting for the new
373 * focus window to be displayed before they are told about this.
374 */
375 ArrayList<WindowState> mLosingFocus = new ArrayList<WindowState>();
376
377 /**
378 * This is set when we have run out of memory, and will either be an empty
379 * list or contain windows that need to be force removed.
380 */
381 ArrayList<WindowState> mForceRemoves;
Romain Guy06882f82009-06-10 13:36:04 -0700382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 IInputMethodManager mInputMethodManager;
Romain Guy06882f82009-06-10 13:36:04 -0700384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 SurfaceSession mFxSession;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700386 private DimAnimator mDimAnimator = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 Surface mBlurSurface;
388 boolean mBlurShown;
Romain Guy06882f82009-06-10 13:36:04 -0700389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 int mTransactionSequence = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 final float[] mTmpFloats = new float[9];
393
394 boolean mSafeMode;
395 boolean mDisplayEnabled = false;
396 boolean mSystemBooted = false;
Christopher Tateb696aee2010-04-02 19:08:30 -0700397 int mInitialDisplayWidth = 0;
398 int mInitialDisplayHeight = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 int mRotation = 0;
400 int mRequestedRotation = 0;
401 int mForcedAppOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Dianne Hackborn321ae682009-03-27 16:16:03 -0700402 int mLastRotationFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 ArrayList<IRotationWatcher> mRotationWatchers
404 = new ArrayList<IRotationWatcher>();
Romain Guy06882f82009-06-10 13:36:04 -0700405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 boolean mLayoutNeeded = true;
407 boolean mAnimationPending = false;
408 boolean mDisplayFrozen = false;
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800409 boolean mWaitingForConfig = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 boolean mWindowsFreezingScreen = false;
411 long mFreezeGcPending = 0;
412 int mAppsFreezingScreen = 0;
413
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800414 int mLayoutSeq = 0;
415
Dianne Hackbornb601ce12010-03-01 23:36:02 -0800416 // State while inside of layoutAndPlaceSurfacesLocked().
417 boolean mFocusMayChange;
418
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800419 Configuration mCurConfiguration = new Configuration();
420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 // This is held as long as we have the screen frozen, to give us time to
422 // perform a rotation animation when turning off shows the lock screen which
423 // changes the orientation.
424 PowerManager.WakeLock mScreenFrozenLock;
Romain Guy06882f82009-06-10 13:36:04 -0700425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 // State management of app transitions. When we are preparing for a
427 // transition, mNextAppTransition will be the kind of transition to
428 // perform or TRANSIT_NONE if we are not waiting. If we are waiting,
429 // mOpeningApps and mClosingApps are the lists of tokens that will be
430 // made visible or hidden at the next transition.
Dianne Hackbornbfe319e2009-09-21 00:34:05 -0700431 int mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -0700432 String mNextAppTransitionPackage;
433 int mNextAppTransitionEnter;
434 int mNextAppTransitionExit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 boolean mAppTransitionReady = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -0700436 boolean mAppTransitionRunning = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437 boolean mAppTransitionTimeout = false;
438 boolean mStartingIconInTransition = false;
439 boolean mSkipAppTransitionAnimation = false;
440 final ArrayList<AppWindowToken> mOpeningApps = new ArrayList<AppWindowToken>();
441 final ArrayList<AppWindowToken> mClosingApps = new ArrayList<AppWindowToken>();
Dianne Hackborna8f60182009-09-01 19:01:50 -0700442 final ArrayList<AppWindowToken> mToTopApps = new ArrayList<AppWindowToken>();
443 final ArrayList<AppWindowToken> mToBottomApps = new ArrayList<AppWindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 //flag to detect fat touch events
446 boolean mFatTouch = false;
447 Display mDisplay;
Romain Guy06882f82009-06-10 13:36:04 -0700448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 H mH = new H();
450
451 WindowState mCurrentFocus = null;
452 WindowState mLastFocus = null;
Romain Guy06882f82009-06-10 13:36:04 -0700453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 // This just indicates the window the input method is on top of, not
455 // necessarily the window its input is going to.
456 WindowState mInputMethodTarget = null;
457 WindowState mUpcomingInputMethodTarget = null;
458 boolean mInputMethodTargetWaitingAnim;
459 int mInputMethodAnimLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -0700460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 WindowState mInputMethodWindow = null;
462 final ArrayList<WindowState> mInputMethodDialogs = new ArrayList<WindowState>();
463
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700464 final ArrayList<WindowToken> mWallpaperTokens = new ArrayList<WindowToken>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800465
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700466 // If non-null, this is the currently visible window that is associated
467 // with the wallpaper.
468 WindowState mWallpaperTarget = null;
Dianne Hackborn3be63c02009-08-20 19:31:38 -0700469 // If non-null, we are in the middle of animating from one wallpaper target
470 // to another, and this is the lower one in Z-order.
471 WindowState mLowerWallpaperTarget = null;
472 // If non-null, we are in the middle of animating from one wallpaper target
473 // to another, and this is the higher one in Z-order.
474 WindowState mUpperWallpaperTarget = null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700475 int mWallpaperAnimLayerAdjustment;
Dianne Hackborn73e92b42009-10-15 14:29:19 -0700476 float mLastWallpaperX = -1;
477 float mLastWallpaperY = -1;
Marco Nelissenbf6956b2009-11-09 15:21:13 -0800478 float mLastWallpaperXStep = -1;
479 float mLastWallpaperYStep = -1;
Dianne Hackborn6adba242009-11-10 11:10:09 -0800480 boolean mSendingPointersToWallpaper = false;
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700481 // This is set when we are waiting for a wallpaper to tell us it is done
482 // changing its scroll position.
483 WindowState mWaitingOnWallpaper;
484 // The last time we had a timeout when waiting for a wallpaper.
485 long mLastWallpaperTimeoutTime;
486 // We give a wallpaper up to 150ms to finish scrolling.
487 static final long WALLPAPER_TIMEOUT = 150;
488 // Time we wait after a timeout before trying to wait again.
489 static final long WALLPAPER_TIMEOUT_RECOVERY = 10000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 AppWindowToken mFocusedApp = null;
492
493 PowerManagerService mPowerManager;
Romain Guy06882f82009-06-10 13:36:04 -0700494
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 float mWindowAnimationScale = 1.0f;
496 float mTransitionAnimationScale = 1.0f;
Romain Guy06882f82009-06-10 13:36:04 -0700497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 final KeyWaiter mKeyWaiter = new KeyWaiter();
499 final KeyQ mQueue;
500 final InputDispatcherThread mInputThread;
501
502 // Who is holding the screen on.
503 Session mHoldingScreenOn;
Romain Guy06882f82009-06-10 13:36:04 -0700504
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700505 boolean mTurnOnScreen;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800506
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 /**
508 * Whether the UI is currently running in touch mode (not showing
509 * navigational focus because the user is directly pressing the screen).
510 */
511 boolean mInTouchMode = false;
512
513 private ViewServer mViewServer;
514
515 final Rect mTempRect = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -0700516
Dianne Hackbornc485a602009-03-24 22:39:49 -0700517 final Configuration mTempConfiguration = new Configuration();
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700518 int mScreenLayout = Configuration.SCREENLAYOUT_SIZE_UNDEFINED;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -0700519
520 // The frame use to limit the size of the app running in compatibility mode.
521 Rect mCompatibleScreenFrame = new Rect();
522 // The surface used to fill the outer rim of the app running in compatibility mode.
523 Surface mBackgroundFillerSurface = null;
524 boolean mBackgroundFillerShown = false;
525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 public static WindowManagerService main(Context context,
527 PowerManagerService pm, boolean haveInputMethods) {
528 WMThread thr = new WMThread(context, pm, haveInputMethods);
529 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700530
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 synchronized (thr) {
532 while (thr.mService == null) {
533 try {
534 thr.wait();
535 } catch (InterruptedException e) {
536 }
537 }
538 }
Romain Guy06882f82009-06-10 13:36:04 -0700539
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 return thr.mService;
541 }
Romain Guy06882f82009-06-10 13:36:04 -0700542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543 static class WMThread extends Thread {
544 WindowManagerService mService;
Romain Guy06882f82009-06-10 13:36:04 -0700545
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546 private final Context mContext;
547 private final PowerManagerService mPM;
548 private final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700549
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 public WMThread(Context context, PowerManagerService pm,
551 boolean haveInputMethods) {
552 super("WindowManager");
553 mContext = context;
554 mPM = pm;
555 mHaveInputMethods = haveInputMethods;
556 }
Romain Guy06882f82009-06-10 13:36:04 -0700557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 public void run() {
559 Looper.prepare();
560 WindowManagerService s = new WindowManagerService(mContext, mPM,
561 mHaveInputMethods);
562 android.os.Process.setThreadPriority(
563 android.os.Process.THREAD_PRIORITY_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -0700564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 synchronized (this) {
566 mService = s;
567 notifyAll();
568 }
Romain Guy06882f82009-06-10 13:36:04 -0700569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 Looper.loop();
571 }
572 }
573
574 static class PolicyThread extends Thread {
575 private final WindowManagerPolicy mPolicy;
576 private final WindowManagerService mService;
577 private final Context mContext;
578 private final PowerManagerService mPM;
579 boolean mRunning = false;
Romain Guy06882f82009-06-10 13:36:04 -0700580
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 public PolicyThread(WindowManagerPolicy policy,
582 WindowManagerService service, Context context,
583 PowerManagerService pm) {
584 super("WindowManagerPolicy");
585 mPolicy = policy;
586 mService = service;
587 mContext = context;
588 mPM = pm;
589 }
Romain Guy06882f82009-06-10 13:36:04 -0700590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591 public void run() {
592 Looper.prepare();
Dianne Hackbornac3587d2010-03-11 11:12:11 -0800593 WindowManagerPolicyThread.set(this, Looper.myLooper());
594
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 //Looper.myLooper().setMessageLogging(new LogPrinter(
Joe Onorato8a9b2202010-02-26 18:56:32 -0800596 // Log.VERBOSE, "WindowManagerPolicy", Log.LOG_ID_SYSTEM));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800597 android.os.Process.setThreadPriority(
598 android.os.Process.THREAD_PRIORITY_FOREGROUND);
599 mPolicy.init(mContext, mService, mPM);
Romain Guy06882f82009-06-10 13:36:04 -0700600
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 synchronized (this) {
602 mRunning = true;
603 notifyAll();
604 }
Romain Guy06882f82009-06-10 13:36:04 -0700605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 Looper.loop();
607 }
608 }
609
610 private WindowManagerService(Context context, PowerManagerService pm,
611 boolean haveInputMethods) {
Michael Chan53071d62009-05-13 17:29:48 -0700612 if (MEASURE_LATENCY) {
613 lt = new LatencyTimer(100, 1000);
614 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800615
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616 mContext = context;
617 mHaveInputMethods = haveInputMethods;
618 mLimitedAlphaCompositing = context.getResources().getBoolean(
619 com.android.internal.R.bool.config_sf_limitedAlpha);
Romain Guy06882f82009-06-10 13:36:04 -0700620
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 mPowerManager = pm;
622 mPowerManager.setPolicy(mPolicy);
623 PowerManager pmc = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
624 mScreenFrozenLock = pmc.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
625 "SCREEN_FROZEN");
626 mScreenFrozenLock.setReferenceCounted(false);
627
628 mActivityManager = ActivityManagerNative.getDefault();
629 mBatteryStats = BatteryStatsService.getService();
630
631 // Get persisted window scale setting
632 mWindowAnimationScale = Settings.System.getFloat(context.getContentResolver(),
633 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
634 mTransitionAnimationScale = Settings.System.getFloat(context.getContentResolver(),
635 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
Romain Guy06882f82009-06-10 13:36:04 -0700636
Jim Miller284b62e2010-06-08 14:27:42 -0700637 // Track changes to DevicePolicyManager state so we can enable/disable keyguard.
638 IntentFilter filter = new IntentFilter();
639 filter.addAction(DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED);
640 mContext.registerReceiver(mBroadcastReceiver, filter);
641
Michael Chan9f028e62009-08-04 17:37:46 -0700642 int max_events_per_sec = 35;
643 try {
644 max_events_per_sec = Integer.parseInt(SystemProperties
645 .get("windowsmgr.max_events_per_sec"));
646 if (max_events_per_sec < 1) {
647 max_events_per_sec = 35;
648 }
649 } catch (NumberFormatException e) {
650 }
651 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653 mQueue = new KeyQ();
654
655 mInputThread = new InputDispatcherThread();
Romain Guy06882f82009-06-10 13:36:04 -0700656
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800657 PolicyThread thr = new PolicyThread(mPolicy, this, context, pm);
658 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660 synchronized (thr) {
661 while (!thr.mRunning) {
662 try {
663 thr.wait();
664 } catch (InterruptedException e) {
665 }
666 }
667 }
Romain Guy06882f82009-06-10 13:36:04 -0700668
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669 mInputThread.start();
Christian Petersson15f66ce2010-06-03 11:16:39 +0200670 mQueue.start();
Romain Guy06882f82009-06-10 13:36:04 -0700671
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 // Add ourself to the Watchdog monitors.
673 Watchdog.getInstance().addMonitor(this);
674 }
675
676 @Override
677 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
678 throws RemoteException {
679 try {
680 return super.onTransact(code, data, reply, flags);
681 } catch (RuntimeException e) {
682 // The window manager only throws security exceptions, so let's
683 // log all others.
684 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800685 Slog.e(TAG, "Window Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686 }
687 throw e;
688 }
689 }
690
691 private void placeWindowAfter(Object pos, WindowState window) {
692 final int i = mWindows.indexOf(pos);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800693 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694 TAG, "Adding window " + window + " at "
695 + (i+1) + " of " + mWindows.size() + " (after " + pos + ")");
696 mWindows.add(i+1, window);
697 }
698
699 private void placeWindowBefore(Object pos, WindowState window) {
700 final int i = mWindows.indexOf(pos);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800701 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702 TAG, "Adding window " + window + " at "
703 + i + " of " + mWindows.size() + " (before " + pos + ")");
704 mWindows.add(i, window);
705 }
706
707 //This method finds out the index of a window that has the same app token as
708 //win. used for z ordering the windows in mWindows
709 private int findIdxBasedOnAppTokens(WindowState win) {
710 //use a local variable to cache mWindows
711 ArrayList localmWindows = mWindows;
712 int jmax = localmWindows.size();
713 if(jmax == 0) {
714 return -1;
715 }
716 for(int j = (jmax-1); j >= 0; j--) {
717 WindowState wentry = (WindowState)localmWindows.get(j);
718 if(wentry.mAppToken == win.mAppToken) {
719 return j;
720 }
721 }
722 return -1;
723 }
Romain Guy06882f82009-06-10 13:36:04 -0700724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 private void addWindowToListInOrderLocked(WindowState win, boolean addToToken) {
726 final IWindow client = win.mClient;
727 final WindowToken token = win.mToken;
728 final ArrayList localmWindows = mWindows;
Romain Guy06882f82009-06-10 13:36:04 -0700729
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800730 final int N = localmWindows.size();
731 final WindowState attached = win.mAttachedWindow;
732 int i;
733 if (attached == null) {
734 int tokenWindowsPos = token.windows.size();
735 if (token.appWindowToken != null) {
736 int index = tokenWindowsPos-1;
737 if (index >= 0) {
738 // If this application has existing windows, we
739 // simply place the new window on top of them... but
740 // keep the starting window on top.
741 if (win.mAttrs.type == TYPE_BASE_APPLICATION) {
742 // Base windows go behind everything else.
743 placeWindowBefore(token.windows.get(0), win);
744 tokenWindowsPos = 0;
745 } else {
746 AppWindowToken atoken = win.mAppToken;
747 if (atoken != null &&
748 token.windows.get(index) == atoken.startingWindow) {
749 placeWindowBefore(token.windows.get(index), win);
750 tokenWindowsPos--;
751 } else {
752 int newIdx = findIdxBasedOnAppTokens(win);
753 if(newIdx != -1) {
Romain Guy06882f82009-06-10 13:36:04 -0700754 //there is a window above this one associated with the same
755 //apptoken note that the window could be a floating window
756 //that was created later or a window at the top of the list of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800757 //windows associated with this token.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800758 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Slog.v(
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700759 TAG, "Adding window " + win + " at "
760 + (newIdx+1) + " of " + N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800761 localmWindows.add(newIdx+1, win);
Romain Guy06882f82009-06-10 13:36:04 -0700762 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800763 }
764 }
765 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800766 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800767 TAG, "Figuring out where to add app window "
768 + client.asBinder() + " (token=" + token + ")");
769 // Figure out where the window should go, based on the
770 // order of applications.
771 final int NA = mAppTokens.size();
772 Object pos = null;
773 for (i=NA-1; i>=0; i--) {
774 AppWindowToken t = mAppTokens.get(i);
775 if (t == token) {
776 i--;
777 break;
778 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800779
Dianne Hackborna8f60182009-09-01 19:01:50 -0700780 // We haven't reached the token yet; if this token
781 // is not going to the bottom and has windows, we can
782 // use it as an anchor for when we do reach the token.
783 if (!t.sendingToBottom && t.windows.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800784 pos = t.windows.get(0);
785 }
786 }
787 // We now know the index into the apps. If we found
788 // an app window above, that gives us the position; else
789 // we need to look some more.
790 if (pos != null) {
791 // Move behind any windows attached to this one.
Romain Guy06882f82009-06-10 13:36:04 -0700792 WindowToken atoken =
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800793 mTokenMap.get(((WindowState)pos).mClient.asBinder());
794 if (atoken != null) {
795 final int NC = atoken.windows.size();
796 if (NC > 0) {
797 WindowState bottom = atoken.windows.get(0);
798 if (bottom.mSubLayer < 0) {
799 pos = bottom;
800 }
801 }
802 }
803 placeWindowBefore(pos, win);
804 } else {
Dianne Hackborna8f60182009-09-01 19:01:50 -0700805 // Continue looking down until we find the first
806 // token that has windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800807 while (i >= 0) {
808 AppWindowToken t = mAppTokens.get(i);
809 final int NW = t.windows.size();
810 if (NW > 0) {
811 pos = t.windows.get(NW-1);
812 break;
813 }
814 i--;
815 }
816 if (pos != null) {
817 // Move in front of any windows attached to this
818 // one.
819 WindowToken atoken =
820 mTokenMap.get(((WindowState)pos).mClient.asBinder());
821 if (atoken != null) {
822 final int NC = atoken.windows.size();
823 if (NC > 0) {
824 WindowState top = atoken.windows.get(NC-1);
825 if (top.mSubLayer >= 0) {
826 pos = top;
827 }
828 }
829 }
830 placeWindowAfter(pos, win);
831 } else {
832 // Just search for the start of this layer.
833 final int myLayer = win.mBaseLayer;
834 for (i=0; i<N; i++) {
835 WindowState w = (WindowState)localmWindows.get(i);
836 if (w.mBaseLayer > myLayer) {
837 break;
838 }
839 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800840 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Slog.v(
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700841 TAG, "Adding window " + win + " at "
842 + i + " of " + N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800843 localmWindows.add(i, win);
844 }
845 }
846 }
847 } else {
848 // Figure out where window should go, based on layer.
849 final int myLayer = win.mBaseLayer;
850 for (i=N-1; i>=0; i--) {
851 if (((WindowState)localmWindows.get(i)).mBaseLayer <= myLayer) {
852 i++;
853 break;
854 }
855 }
856 if (i < 0) i = 0;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800857 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Slog.v(
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700858 TAG, "Adding window " + win + " at "
859 + i + " of " + N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800860 localmWindows.add(i, win);
861 }
862 if (addToToken) {
863 token.windows.add(tokenWindowsPos, win);
864 }
865
866 } else {
867 // Figure out this window's ordering relative to the window
868 // it is attached to.
869 final int NA = token.windows.size();
870 final int sublayer = win.mSubLayer;
871 int largestSublayer = Integer.MIN_VALUE;
872 WindowState windowWithLargestSublayer = null;
873 for (i=0; i<NA; i++) {
874 WindowState w = token.windows.get(i);
875 final int wSublayer = w.mSubLayer;
876 if (wSublayer >= largestSublayer) {
877 largestSublayer = wSublayer;
878 windowWithLargestSublayer = w;
879 }
880 if (sublayer < 0) {
881 // For negative sublayers, we go below all windows
882 // in the same sublayer.
883 if (wSublayer >= sublayer) {
884 if (addToToken) {
885 token.windows.add(i, win);
886 }
887 placeWindowBefore(
888 wSublayer >= 0 ? attached : w, win);
889 break;
890 }
891 } else {
892 // For positive sublayers, we go above all windows
893 // in the same sublayer.
894 if (wSublayer > sublayer) {
895 if (addToToken) {
896 token.windows.add(i, win);
897 }
898 placeWindowBefore(w, win);
899 break;
900 }
901 }
902 }
903 if (i >= NA) {
904 if (addToToken) {
905 token.windows.add(win);
906 }
907 if (sublayer < 0) {
908 placeWindowBefore(attached, win);
909 } else {
910 placeWindowAfter(largestSublayer >= 0
911 ? windowWithLargestSublayer
912 : attached,
913 win);
914 }
915 }
916 }
Romain Guy06882f82009-06-10 13:36:04 -0700917
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800918 if (win.mAppToken != null && addToToken) {
919 win.mAppToken.allAppWindows.add(win);
920 }
921 }
Romain Guy06882f82009-06-10 13:36:04 -0700922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 static boolean canBeImeTarget(WindowState w) {
924 final int fl = w.mAttrs.flags
925 & (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM);
926 if (fl == 0 || fl == (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM)) {
927 return w.isVisibleOrAdding();
928 }
929 return false;
930 }
Romain Guy06882f82009-06-10 13:36:04 -0700931
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800932 int findDesiredInputMethodWindowIndexLocked(boolean willMove) {
933 final ArrayList localmWindows = mWindows;
934 final int N = localmWindows.size();
935 WindowState w = null;
936 int i = N;
937 while (i > 0) {
938 i--;
939 w = (WindowState)localmWindows.get(i);
Romain Guy06882f82009-06-10 13:36:04 -0700940
Joe Onorato8a9b2202010-02-26 18:56:32 -0800941 //Slog.i(TAG, "Checking window @" + i + " " + w + " fl=0x"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942 // + Integer.toHexString(w.mAttrs.flags));
943 if (canBeImeTarget(w)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800944 //Slog.i(TAG, "Putting input method here!");
Romain Guy06882f82009-06-10 13:36:04 -0700945
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800946 // Yet more tricksyness! If this window is a "starting"
947 // window, we do actually want to be on top of it, but
948 // it is not -really- where input will go. So if the caller
949 // is not actually looking to move the IME, look down below
950 // for a real window to target...
951 if (!willMove
952 && w.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
953 && i > 0) {
954 WindowState wb = (WindowState)localmWindows.get(i-1);
955 if (wb.mAppToken == w.mAppToken && canBeImeTarget(wb)) {
956 i--;
957 w = wb;
958 }
959 }
960 break;
961 }
962 }
Romain Guy06882f82009-06-10 13:36:04 -0700963
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 mUpcomingInputMethodTarget = w;
Romain Guy06882f82009-06-10 13:36:04 -0700965
Joe Onorato8a9b2202010-02-26 18:56:32 -0800966 if (DEBUG_INPUT_METHOD) Slog.v(TAG, "Desired input method target="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800967 + w + " willMove=" + willMove);
Romain Guy06882f82009-06-10 13:36:04 -0700968
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800969 if (willMove && w != null) {
970 final WindowState curTarget = mInputMethodTarget;
971 if (curTarget != null && curTarget.mAppToken != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700972
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800973 // Now some fun for dealing with window animations that
974 // modify the Z order. We need to look at all windows below
975 // the current target that are in this app, finding the highest
976 // visible one in layering.
977 AppWindowToken token = curTarget.mAppToken;
978 WindowState highestTarget = null;
979 int highestPos = 0;
980 if (token.animating || token.animation != null) {
981 int pos = 0;
982 pos = localmWindows.indexOf(curTarget);
983 while (pos >= 0) {
984 WindowState win = (WindowState)localmWindows.get(pos);
985 if (win.mAppToken != token) {
986 break;
987 }
988 if (!win.mRemoved) {
989 if (highestTarget == null || win.mAnimLayer >
990 highestTarget.mAnimLayer) {
991 highestTarget = win;
992 highestPos = pos;
993 }
994 }
995 pos--;
996 }
997 }
Romain Guy06882f82009-06-10 13:36:04 -0700998
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800999 if (highestTarget != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001000 if (DEBUG_INPUT_METHOD) Slog.v(TAG, "mNextAppTransition="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 + mNextAppTransition + " " + highestTarget
1002 + " animating=" + highestTarget.isAnimating()
1003 + " layer=" + highestTarget.mAnimLayer
1004 + " new layer=" + w.mAnimLayer);
Romain Guy06882f82009-06-10 13:36:04 -07001005
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07001006 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 // If we are currently setting up for an animation,
1008 // hold everything until we can find out what will happen.
1009 mInputMethodTargetWaitingAnim = true;
1010 mInputMethodTarget = highestTarget;
1011 return highestPos + 1;
1012 } else if (highestTarget.isAnimating() &&
1013 highestTarget.mAnimLayer > w.mAnimLayer) {
1014 // If the window we are currently targeting is involved
1015 // with an animation, and it is on top of the next target
1016 // we will be over, then hold off on moving until
1017 // that is done.
1018 mInputMethodTarget = highestTarget;
1019 return highestPos + 1;
1020 }
1021 }
1022 }
1023 }
Romain Guy06882f82009-06-10 13:36:04 -07001024
Joe Onorato8a9b2202010-02-26 18:56:32 -08001025 //Slog.i(TAG, "Placing input method @" + (i+1));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026 if (w != null) {
1027 if (willMove) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08001028 if (DEBUG_INPUT_METHOD) {
1029 RuntimeException e = null;
1030 if (!HIDE_STACK_CRAWLS) {
1031 e = new RuntimeException();
1032 e.fillInStackTrace();
1033 }
1034 Slog.w(TAG, "Moving IM target from "
1035 + mInputMethodTarget + " to " + w, e);
1036 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 mInputMethodTarget = w;
1038 if (w.mAppToken != null) {
1039 setInputMethodAnimLayerAdjustment(w.mAppToken.animLayerAdjustment);
1040 } else {
1041 setInputMethodAnimLayerAdjustment(0);
1042 }
1043 }
1044 return i+1;
1045 }
1046 if (willMove) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08001047 if (DEBUG_INPUT_METHOD) {
1048 RuntimeException e = null;
1049 if (!HIDE_STACK_CRAWLS) {
1050 e = new RuntimeException();
1051 e.fillInStackTrace();
1052 }
1053 Slog.w(TAG, "Moving IM target from "
1054 + mInputMethodTarget + " to null", e);
1055 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001056 mInputMethodTarget = null;
1057 setInputMethodAnimLayerAdjustment(0);
1058 }
1059 return -1;
1060 }
Romain Guy06882f82009-06-10 13:36:04 -07001061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 void addInputMethodWindowToListLocked(WindowState win) {
1063 int pos = findDesiredInputMethodWindowIndexLocked(true);
1064 if (pos >= 0) {
1065 win.mTargetAppToken = mInputMethodTarget.mAppToken;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001066 if (DEBUG_WINDOW_MOVEMENT) Slog.v(
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001067 TAG, "Adding input method window " + win + " at " + pos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068 mWindows.add(pos, win);
1069 moveInputMethodDialogsLocked(pos+1);
1070 return;
1071 }
1072 win.mTargetAppToken = null;
1073 addWindowToListInOrderLocked(win, true);
1074 moveInputMethodDialogsLocked(pos);
1075 }
Romain Guy06882f82009-06-10 13:36:04 -07001076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077 void setInputMethodAnimLayerAdjustment(int adj) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001078 if (DEBUG_LAYERS) Slog.v(TAG, "Setting im layer adj to " + adj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001079 mInputMethodAnimLayerAdjustment = adj;
1080 WindowState imw = mInputMethodWindow;
1081 if (imw != null) {
1082 imw.mAnimLayer = imw.mLayer + adj;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001083 if (DEBUG_LAYERS) Slog.v(TAG, "IM win " + imw
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001084 + " anim layer: " + imw.mAnimLayer);
1085 int wi = imw.mChildWindows.size();
1086 while (wi > 0) {
1087 wi--;
1088 WindowState cw = (WindowState)imw.mChildWindows.get(wi);
1089 cw.mAnimLayer = cw.mLayer + adj;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001090 if (DEBUG_LAYERS) Slog.v(TAG, "IM win " + cw
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 + " anim layer: " + cw.mAnimLayer);
1092 }
1093 }
1094 int di = mInputMethodDialogs.size();
1095 while (di > 0) {
1096 di --;
1097 imw = mInputMethodDialogs.get(di);
1098 imw.mAnimLayer = imw.mLayer + adj;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001099 if (DEBUG_LAYERS) Slog.v(TAG, "IM win " + imw
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001100 + " anim layer: " + imw.mAnimLayer);
1101 }
1102 }
Romain Guy06882f82009-06-10 13:36:04 -07001103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001104 private int tmpRemoveWindowLocked(int interestingPos, WindowState win) {
1105 int wpos = mWindows.indexOf(win);
1106 if (wpos >= 0) {
1107 if (wpos < interestingPos) interestingPos--;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001108 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "Temp removing at " + wpos + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001109 mWindows.remove(wpos);
1110 int NC = win.mChildWindows.size();
1111 while (NC > 0) {
1112 NC--;
1113 WindowState cw = (WindowState)win.mChildWindows.get(NC);
1114 int cpos = mWindows.indexOf(cw);
1115 if (cpos >= 0) {
1116 if (cpos < interestingPos) interestingPos--;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001117 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "Temp removing child at "
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001118 + cpos + ": " + cw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119 mWindows.remove(cpos);
1120 }
1121 }
1122 }
1123 return interestingPos;
1124 }
Romain Guy06882f82009-06-10 13:36:04 -07001125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126 private void reAddWindowToListInOrderLocked(WindowState win) {
1127 addWindowToListInOrderLocked(win, false);
1128 // This is a hack to get all of the child windows added as well
1129 // at the right position. Child windows should be rare and
1130 // this case should be rare, so it shouldn't be that big a deal.
1131 int wpos = mWindows.indexOf(win);
1132 if (wpos >= 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001133 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "ReAdd removing from " + wpos
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001134 + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 mWindows.remove(wpos);
1136 reAddWindowLocked(wpos, win);
1137 }
1138 }
Romain Guy06882f82009-06-10 13:36:04 -07001139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140 void logWindowList(String prefix) {
1141 int N = mWindows.size();
1142 while (N > 0) {
1143 N--;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001144 Slog.v(TAG, prefix + "#" + N + ": " + mWindows.get(N));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145 }
1146 }
Romain Guy06882f82009-06-10 13:36:04 -07001147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001148 void moveInputMethodDialogsLocked(int pos) {
1149 ArrayList<WindowState> dialogs = mInputMethodDialogs;
Romain Guy06882f82009-06-10 13:36:04 -07001150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 final int N = dialogs.size();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001152 if (DEBUG_INPUT_METHOD) Slog.v(TAG, "Removing " + N + " dialogs w/pos=" + pos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153 for (int i=0; i<N; i++) {
1154 pos = tmpRemoveWindowLocked(pos, dialogs.get(i));
1155 }
1156 if (DEBUG_INPUT_METHOD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001157 Slog.v(TAG, "Window list w/pos=" + pos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001158 logWindowList(" ");
1159 }
Romain Guy06882f82009-06-10 13:36:04 -07001160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001161 if (pos >= 0) {
1162 final AppWindowToken targetAppToken = mInputMethodTarget.mAppToken;
1163 if (pos < mWindows.size()) {
1164 WindowState wp = (WindowState)mWindows.get(pos);
1165 if (wp == mInputMethodWindow) {
1166 pos++;
1167 }
1168 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001169 if (DEBUG_INPUT_METHOD) Slog.v(TAG, "Adding " + N + " dialogs at pos=" + pos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001170 for (int i=0; i<N; i++) {
1171 WindowState win = dialogs.get(i);
1172 win.mTargetAppToken = targetAppToken;
1173 pos = reAddWindowLocked(pos, win);
1174 }
1175 if (DEBUG_INPUT_METHOD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001176 Slog.v(TAG, "Final window list:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001177 logWindowList(" ");
1178 }
1179 return;
1180 }
1181 for (int i=0; i<N; i++) {
1182 WindowState win = dialogs.get(i);
1183 win.mTargetAppToken = null;
1184 reAddWindowToListInOrderLocked(win);
1185 if (DEBUG_INPUT_METHOD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001186 Slog.v(TAG, "No IM target, final list:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187 logWindowList(" ");
1188 }
1189 }
1190 }
Romain Guy06882f82009-06-10 13:36:04 -07001191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 boolean moveInputMethodWindowsIfNeededLocked(boolean needAssignLayers) {
1193 final WindowState imWin = mInputMethodWindow;
1194 final int DN = mInputMethodDialogs.size();
1195 if (imWin == null && DN == 0) {
1196 return false;
1197 }
Romain Guy06882f82009-06-10 13:36:04 -07001198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001199 int imPos = findDesiredInputMethodWindowIndexLocked(true);
1200 if (imPos >= 0) {
1201 // In this case, the input method windows are to be placed
1202 // immediately above the window they are targeting.
Romain Guy06882f82009-06-10 13:36:04 -07001203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 // First check to see if the input method windows are already
1205 // located here, and contiguous.
1206 final int N = mWindows.size();
1207 WindowState firstImWin = imPos < N
1208 ? (WindowState)mWindows.get(imPos) : null;
Romain Guy06882f82009-06-10 13:36:04 -07001209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 // Figure out the actual input method window that should be
1211 // at the bottom of their stack.
1212 WindowState baseImWin = imWin != null
1213 ? imWin : mInputMethodDialogs.get(0);
1214 if (baseImWin.mChildWindows.size() > 0) {
1215 WindowState cw = (WindowState)baseImWin.mChildWindows.get(0);
1216 if (cw.mSubLayer < 0) baseImWin = cw;
1217 }
Romain Guy06882f82009-06-10 13:36:04 -07001218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219 if (firstImWin == baseImWin) {
1220 // The windows haven't moved... but are they still contiguous?
1221 // First find the top IM window.
1222 int pos = imPos+1;
1223 while (pos < N) {
1224 if (!((WindowState)mWindows.get(pos)).mIsImWindow) {
1225 break;
1226 }
1227 pos++;
1228 }
1229 pos++;
1230 // Now there should be no more input method windows above.
1231 while (pos < N) {
1232 if (((WindowState)mWindows.get(pos)).mIsImWindow) {
1233 break;
1234 }
1235 pos++;
1236 }
1237 if (pos >= N) {
1238 // All is good!
1239 return false;
1240 }
1241 }
Romain Guy06882f82009-06-10 13:36:04 -07001242
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 if (imWin != null) {
1244 if (DEBUG_INPUT_METHOD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001245 Slog.v(TAG, "Moving IM from " + imPos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 logWindowList(" ");
1247 }
1248 imPos = tmpRemoveWindowLocked(imPos, imWin);
1249 if (DEBUG_INPUT_METHOD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001250 Slog.v(TAG, "List after moving with new pos " + imPos + ":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 logWindowList(" ");
1252 }
1253 imWin.mTargetAppToken = mInputMethodTarget.mAppToken;
1254 reAddWindowLocked(imPos, imWin);
1255 if (DEBUG_INPUT_METHOD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001256 Slog.v(TAG, "List after moving IM to " + imPos + ":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 logWindowList(" ");
1258 }
1259 if (DN > 0) moveInputMethodDialogsLocked(imPos+1);
1260 } else {
1261 moveInputMethodDialogsLocked(imPos);
1262 }
Romain Guy06882f82009-06-10 13:36:04 -07001263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 } else {
1265 // In this case, the input method windows go in a fixed layer,
1266 // because they aren't currently associated with a focus window.
Romain Guy06882f82009-06-10 13:36:04 -07001267
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268 if (imWin != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001269 if (DEBUG_INPUT_METHOD) Slog.v(TAG, "Moving IM from " + imPos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001270 tmpRemoveWindowLocked(0, imWin);
1271 imWin.mTargetAppToken = null;
1272 reAddWindowToListInOrderLocked(imWin);
1273 if (DEBUG_INPUT_METHOD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001274 Slog.v(TAG, "List with no IM target:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001275 logWindowList(" ");
1276 }
1277 if (DN > 0) moveInputMethodDialogsLocked(-1);;
1278 } else {
1279 moveInputMethodDialogsLocked(-1);;
1280 }
Romain Guy06882f82009-06-10 13:36:04 -07001281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 }
Romain Guy06882f82009-06-10 13:36:04 -07001283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 if (needAssignLayers) {
1285 assignLayersLocked();
1286 }
Romain Guy06882f82009-06-10 13:36:04 -07001287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 return true;
1289 }
Romain Guy06882f82009-06-10 13:36:04 -07001290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 void adjustInputMethodDialogsLocked() {
1292 moveInputMethodDialogsLocked(findDesiredInputMethodWindowIndexLocked(true));
1293 }
Romain Guy06882f82009-06-10 13:36:04 -07001294
Dianne Hackborn25994b42009-09-04 14:21:19 -07001295 final boolean isWallpaperVisible(WindowState wallpaperTarget) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001296 if (DEBUG_WALLPAPER) Slog.v(TAG, "Wallpaper vis: target obscured="
Dianne Hackborn25994b42009-09-04 14:21:19 -07001297 + (wallpaperTarget != null ? Boolean.toString(wallpaperTarget.mObscured) : "??")
1298 + " anim=" + ((wallpaperTarget != null && wallpaperTarget.mAppToken != null)
1299 ? wallpaperTarget.mAppToken.animation : null)
1300 + " upper=" + mUpperWallpaperTarget
1301 + " lower=" + mLowerWallpaperTarget);
1302 return (wallpaperTarget != null
1303 && (!wallpaperTarget.mObscured || (wallpaperTarget.mAppToken != null
1304 && wallpaperTarget.mAppToken.animation != null)))
1305 || mUpperWallpaperTarget != null
1306 || mLowerWallpaperTarget != null;
1307 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001308
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001309 static final int ADJUST_WALLPAPER_LAYERS_CHANGED = 1<<1;
1310 static final int ADJUST_WALLPAPER_VISIBILITY_CHANGED = 1<<2;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001311
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001312 int adjustWallpaperWindowsLocked() {
1313 int changed = 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001314
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001315 final int dw = mDisplay.getWidth();
1316 final int dh = mDisplay.getHeight();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001317
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001318 // First find top-most window that has asked to be on top of the
1319 // wallpaper; all wallpapers go behind it.
1320 final ArrayList localmWindows = mWindows;
1321 int N = localmWindows.size();
1322 WindowState w = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001323 WindowState foundW = null;
1324 int foundI = 0;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001325 WindowState topCurW = null;
1326 int topCurI = 0;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001327 int i = N;
1328 while (i > 0) {
1329 i--;
1330 w = (WindowState)localmWindows.get(i);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001331 if ((w.mAttrs.type == WindowManager.LayoutParams.TYPE_WALLPAPER)) {
1332 if (topCurW == null) {
1333 topCurW = w;
1334 topCurI = i;
1335 }
1336 continue;
1337 }
1338 topCurW = null;
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001339 if (w.mAppToken != null) {
1340 // If this window's app token is hidden and not animating,
1341 // it is of no interest to us.
1342 if (w.mAppToken.hidden && w.mAppToken.animation == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001343 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001344 "Skipping hidden or animating token: " + w);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001345 topCurW = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001346 continue;
1347 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001348 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001349 if (DEBUG_WALLPAPER) Slog.v(TAG, "Win " + w + ": readyfordisplay="
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001350 + w.isReadyForDisplay() + " drawpending=" + w.mDrawPending
1351 + " commitdrawpending=" + w.mCommitDrawPending);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001352 if ((w.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0 && w.isReadyForDisplay()
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07001353 && (mWallpaperTarget == w
1354 || (!w.mDrawPending && !w.mCommitDrawPending))) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001355 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001356 "Found wallpaper activity: #" + i + "=" + w);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001357 foundW = w;
1358 foundI = i;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001359 if (w == mWallpaperTarget && ((w.mAppToken != null
1360 && w.mAppToken.animation != null)
1361 || w.mAnimation != null)) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001362 // The current wallpaper target is animating, so we'll
1363 // look behind it for another possible target and figure
1364 // out what is going on below.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001365 if (DEBUG_WALLPAPER) Slog.v(TAG, "Win " + w
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001366 + ": token animating, looking behind.");
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001367 continue;
1368 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001369 break;
1370 }
1371 }
1372
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07001373 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001374 // If we are currently waiting for an app transition, and either
1375 // the current target or the next target are involved with it,
1376 // then hold off on doing anything with the wallpaper.
1377 // Note that we are checking here for just whether the target
1378 // is part of an app token... which is potentially overly aggressive
1379 // (the app token may not be involved in the transition), but good
1380 // enough (we'll just wait until whatever transition is pending
1381 // executes).
1382 if (mWallpaperTarget != null && mWallpaperTarget.mAppToken != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001383 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001384 "Wallpaper not changing: waiting for app anim in current target");
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001385 return 0;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001386 }
1387 if (foundW != null && foundW.mAppToken != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001388 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001389 "Wallpaper not changing: waiting for app anim in found target");
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001390 return 0;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001391 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001392 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001393
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001394 if (mWallpaperTarget != foundW) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001395 if (DEBUG_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001396 Slog.v(TAG, "New wallpaper target: " + foundW
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001397 + " oldTarget: " + mWallpaperTarget);
1398 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001399
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001400 mLowerWallpaperTarget = null;
1401 mUpperWallpaperTarget = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001402
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001403 WindowState oldW = mWallpaperTarget;
1404 mWallpaperTarget = foundW;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001405
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001406 // Now what is happening... if the current and new targets are
1407 // animating, then we are in our super special mode!
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001408 if (foundW != null && oldW != null) {
1409 boolean oldAnim = oldW.mAnimation != null
1410 || (oldW.mAppToken != null && oldW.mAppToken.animation != null);
1411 boolean foundAnim = foundW.mAnimation != null
1412 || (foundW.mAppToken != null && foundW.mAppToken.animation != null);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001413 if (DEBUG_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001414 Slog.v(TAG, "New animation: " + foundAnim
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001415 + " old animation: " + oldAnim);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001416 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001417 if (foundAnim && oldAnim) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001418 int oldI = localmWindows.indexOf(oldW);
1419 if (DEBUG_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001420 Slog.v(TAG, "New i: " + foundI + " old i: " + oldI);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001421 }
1422 if (oldI >= 0) {
1423 if (DEBUG_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001424 Slog.v(TAG, "Animating wallpapers: old#" + oldI
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001425 + "=" + oldW + "; new#" + foundI
1426 + "=" + foundW);
1427 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001428
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001429 // Set the new target correctly.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001430 if (foundW.mAppToken != null && foundW.mAppToken.hiddenRequested) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001431 if (DEBUG_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001432 Slog.v(TAG, "Old wallpaper still the target.");
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001433 }
1434 mWallpaperTarget = oldW;
1435 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001436
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001437 // Now set the upper and lower wallpaper targets
1438 // correctly, and make sure that we are positioning
1439 // the wallpaper below the lower.
1440 if (foundI > oldI) {
1441 // The new target is on top of the old one.
1442 if (DEBUG_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001443 Slog.v(TAG, "Found target above old target.");
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001444 }
1445 mUpperWallpaperTarget = foundW;
1446 mLowerWallpaperTarget = oldW;
1447 foundW = oldW;
1448 foundI = oldI;
1449 } else {
1450 // The new target is below the old one.
1451 if (DEBUG_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001452 Slog.v(TAG, "Found target below old target.");
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001453 }
1454 mUpperWallpaperTarget = oldW;
1455 mLowerWallpaperTarget = foundW;
1456 }
1457 }
1458 }
1459 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001460
Dianne Hackborn6b1cb352009-09-28 18:27:26 -07001461 } else if (mLowerWallpaperTarget != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001462 // Is it time to stop animating?
Dianne Hackborn6b1cb352009-09-28 18:27:26 -07001463 boolean lowerAnimating = mLowerWallpaperTarget.mAnimation != null
1464 || (mLowerWallpaperTarget.mAppToken != null
1465 && mLowerWallpaperTarget.mAppToken.animation != null);
1466 boolean upperAnimating = mUpperWallpaperTarget.mAnimation != null
1467 || (mUpperWallpaperTarget.mAppToken != null
1468 && mUpperWallpaperTarget.mAppToken.animation != null);
1469 if (!lowerAnimating || !upperAnimating) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001470 if (DEBUG_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001471 Slog.v(TAG, "No longer animating wallpaper targets!");
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001472 }
1473 mLowerWallpaperTarget = null;
1474 mUpperWallpaperTarget = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001475 }
1476 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001477
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001478 boolean visible = foundW != null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001479 if (visible) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001480 // The window is visible to the compositor... but is it visible
1481 // to the user? That is what the wallpaper cares about.
Dianne Hackborn25994b42009-09-04 14:21:19 -07001482 visible = isWallpaperVisible(foundW);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001483 if (DEBUG_WALLPAPER) Slog.v(TAG, "Wallpaper visibility: " + visible);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001484
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001485 // If the wallpaper target is animating, we may need to copy
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001486 // its layer adjustment. Only do this if we are not transfering
1487 // between two wallpaper targets.
1488 mWallpaperAnimLayerAdjustment =
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001489 (mLowerWallpaperTarget == null && foundW.mAppToken != null)
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001490 ? foundW.mAppToken.animLayerAdjustment : 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001491
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001492 final int maxLayer = mPolicy.getMaxWallpaperLayer()
1493 * TYPE_LAYER_MULTIPLIER
1494 + TYPE_LAYER_OFFSET;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001495
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001496 // Now w is the window we are supposed to be behind... but we
1497 // need to be sure to also be behind any of its attached windows,
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001498 // AND any starting window associated with it, AND below the
1499 // maximum layer the policy allows for wallpapers.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001500 while (foundI > 0) {
1501 WindowState wb = (WindowState)localmWindows.get(foundI-1);
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001502 if (wb.mBaseLayer < maxLayer &&
1503 wb.mAttachedWindow != foundW &&
Pal Szasz73dc2592010-09-03 11:46:26 +02001504 wb.mAttachedWindow != foundW.mAttachedWindow &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001505 (wb.mAttrs.type != TYPE_APPLICATION_STARTING ||
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001506 wb.mToken != foundW.mToken)) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001507 // This window is not related to the previous one in any
1508 // interesting way, so stop here.
1509 break;
1510 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001511 foundW = wb;
1512 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001513 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001514 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001515 if (DEBUG_WALLPAPER) Slog.v(TAG, "No wallpaper target");
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001516 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001517
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001518 if (foundW == null && topCurW != null) {
1519 // There is no wallpaper target, so it goes at the bottom.
1520 // We will assume it is the same place as last time, if known.
1521 foundW = topCurW;
1522 foundI = topCurI+1;
1523 } else {
1524 // Okay i is the position immediately above the wallpaper. Look at
1525 // what is below it for later.
1526 foundW = foundI > 0 ? (WindowState)localmWindows.get(foundI-1) : null;
1527 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001528
Dianne Hackborn284ac932009-08-28 10:34:25 -07001529 if (visible) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001530 if (mWallpaperTarget.mWallpaperX >= 0) {
1531 mLastWallpaperX = mWallpaperTarget.mWallpaperX;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001532 mLastWallpaperXStep = mWallpaperTarget.mWallpaperXStep;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001533 }
1534 if (mWallpaperTarget.mWallpaperY >= 0) {
1535 mLastWallpaperY = mWallpaperTarget.mWallpaperY;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001536 mLastWallpaperYStep = mWallpaperTarget.mWallpaperYStep;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001537 }
Dianne Hackborn284ac932009-08-28 10:34:25 -07001538 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001539
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001540 // Start stepping backwards from here, ensuring that our wallpaper windows
1541 // are correctly placed.
1542 int curTokenIndex = mWallpaperTokens.size();
1543 while (curTokenIndex > 0) {
1544 curTokenIndex--;
1545 WindowToken token = mWallpaperTokens.get(curTokenIndex);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001546 if (token.hidden == visible) {
1547 changed |= ADJUST_WALLPAPER_VISIBILITY_CHANGED;
1548 token.hidden = !visible;
1549 // Need to do a layout to ensure the wallpaper now has the
1550 // correct size.
1551 mLayoutNeeded = true;
1552 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001553
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001554 int curWallpaperIndex = token.windows.size();
1555 while (curWallpaperIndex > 0) {
1556 curWallpaperIndex--;
1557 WindowState wallpaper = token.windows.get(curWallpaperIndex);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001558
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001559 if (visible) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001560 updateWallpaperOffsetLocked(wallpaper, dw, dh, false);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001561 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001562
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001563 // First, make sure the client has the current visibility
1564 // state.
1565 if (wallpaper.mWallpaperVisible != visible) {
1566 wallpaper.mWallpaperVisible = visible;
1567 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001568 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001569 "Setting visibility of wallpaper " + wallpaper
1570 + ": " + visible);
1571 wallpaper.mClient.dispatchAppVisibility(visible);
1572 } catch (RemoteException e) {
1573 }
1574 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001575
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001576 wallpaper.mAnimLayer = wallpaper.mLayer + mWallpaperAnimLayerAdjustment;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001577 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Slog.v(TAG, "Wallpaper win "
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001578 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001579
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001580 // First, if this window is at the current index, then all
1581 // is well.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001582 if (wallpaper == foundW) {
1583 foundI--;
1584 foundW = foundI > 0
1585 ? (WindowState)localmWindows.get(foundI-1) : null;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001586 continue;
1587 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001588
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001589 // The window didn't match... the current wallpaper window,
1590 // wherever it is, is in the wrong place, so make sure it is
1591 // not in the list.
1592 int oldIndex = localmWindows.indexOf(wallpaper);
1593 if (oldIndex >= 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001594 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "Wallpaper removing at "
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001595 + oldIndex + ": " + wallpaper);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001596 localmWindows.remove(oldIndex);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001597 if (oldIndex < foundI) {
1598 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001599 }
1600 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001601
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001602 // Now stick it in.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001603 if (DEBUG_WALLPAPER || DEBUG_WINDOW_MOVEMENT) Slog.v(TAG,
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001604 "Moving wallpaper " + wallpaper
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001605 + " from " + oldIndex + " to " + foundI);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001606
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001607 localmWindows.add(foundI, wallpaper);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001608 changed |= ADJUST_WALLPAPER_LAYERS_CHANGED;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001609 }
1610 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001611
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001612 return changed;
1613 }
1614
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001615 void setWallpaperAnimLayerAdjustmentLocked(int adj) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001616 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001617 "Setting wallpaper layer adj to " + adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001618 mWallpaperAnimLayerAdjustment = adj;
1619 int curTokenIndex = mWallpaperTokens.size();
1620 while (curTokenIndex > 0) {
1621 curTokenIndex--;
1622 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1623 int curWallpaperIndex = token.windows.size();
1624 while (curWallpaperIndex > 0) {
1625 curWallpaperIndex--;
1626 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1627 wallpaper.mAnimLayer = wallpaper.mLayer + adj;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001628 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Slog.v(TAG, "Wallpaper win "
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001629 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001630 }
1631 }
1632 }
1633
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001634 boolean updateWallpaperOffsetLocked(WindowState wallpaperWin, int dw, int dh,
1635 boolean sync) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001636 boolean changed = false;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001637 boolean rawChanged = false;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001638 float wpx = mLastWallpaperX >= 0 ? mLastWallpaperX : 0.5f;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001639 float wpxs = mLastWallpaperXStep >= 0 ? mLastWallpaperXStep : -1.0f;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001640 int availw = wallpaperWin.mFrame.right-wallpaperWin.mFrame.left-dw;
1641 int offset = availw > 0 ? -(int)(availw*wpx+.5f) : 0;
1642 changed = wallpaperWin.mXOffset != offset;
1643 if (changed) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001644 if (DEBUG_WALLPAPER) Slog.v(TAG, "Update wallpaper "
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001645 + wallpaperWin + " x: " + offset);
1646 wallpaperWin.mXOffset = offset;
1647 }
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001648 if (wallpaperWin.mWallpaperX != wpx || wallpaperWin.mWallpaperXStep != wpxs) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001649 wallpaperWin.mWallpaperX = wpx;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001650 wallpaperWin.mWallpaperXStep = wpxs;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001651 rawChanged = true;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001652 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001653
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001654 float wpy = mLastWallpaperY >= 0 ? mLastWallpaperY : 0.5f;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001655 float wpys = mLastWallpaperYStep >= 0 ? mLastWallpaperYStep : -1.0f;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001656 int availh = wallpaperWin.mFrame.bottom-wallpaperWin.mFrame.top-dh;
1657 offset = availh > 0 ? -(int)(availh*wpy+.5f) : 0;
1658 if (wallpaperWin.mYOffset != offset) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001659 if (DEBUG_WALLPAPER) Slog.v(TAG, "Update wallpaper "
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001660 + wallpaperWin + " y: " + offset);
1661 changed = true;
1662 wallpaperWin.mYOffset = offset;
1663 }
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001664 if (wallpaperWin.mWallpaperY != wpy || wallpaperWin.mWallpaperYStep != wpys) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001665 wallpaperWin.mWallpaperY = wpy;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001666 wallpaperWin.mWallpaperYStep = wpys;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001667 rawChanged = true;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001668 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001669
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001670 if (rawChanged) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001671 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001672 if (DEBUG_WALLPAPER) Slog.v(TAG, "Report new wp offset "
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001673 + wallpaperWin + " x=" + wallpaperWin.mWallpaperX
1674 + " y=" + wallpaperWin.mWallpaperY);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001675 if (sync) {
Dianne Hackborn75804932009-10-20 20:15:20 -07001676 mWaitingOnWallpaper = wallpaperWin;
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001677 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001678 wallpaperWin.mClient.dispatchWallpaperOffsets(
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001679 wallpaperWin.mWallpaperX, wallpaperWin.mWallpaperY,
1680 wallpaperWin.mWallpaperXStep, wallpaperWin.mWallpaperYStep, sync);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001681 if (sync) {
Dianne Hackborn75804932009-10-20 20:15:20 -07001682 if (mWaitingOnWallpaper != null) {
1683 long start = SystemClock.uptimeMillis();
1684 if ((mLastWallpaperTimeoutTime+WALLPAPER_TIMEOUT_RECOVERY)
1685 < start) {
1686 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001687 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn75804932009-10-20 20:15:20 -07001688 "Waiting for offset complete...");
1689 mWindowMap.wait(WALLPAPER_TIMEOUT);
1690 } catch (InterruptedException e) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001691 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001692 if (DEBUG_WALLPAPER) Slog.v(TAG, "Offset complete!");
Dianne Hackborn75804932009-10-20 20:15:20 -07001693 if ((start+WALLPAPER_TIMEOUT)
1694 < SystemClock.uptimeMillis()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001695 Slog.i(TAG, "Timeout waiting for wallpaper to offset: "
Dianne Hackborn75804932009-10-20 20:15:20 -07001696 + wallpaperWin);
1697 mLastWallpaperTimeoutTime = start;
1698 }
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001699 }
Dianne Hackborn75804932009-10-20 20:15:20 -07001700 mWaitingOnWallpaper = null;
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001701 }
1702 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001703 } catch (RemoteException e) {
1704 }
1705 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001706
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001707 return changed;
1708 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001709
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001710 void wallpaperOffsetsComplete(IBinder window) {
Dianne Hackborn75804932009-10-20 20:15:20 -07001711 synchronized (mWindowMap) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001712 if (mWaitingOnWallpaper != null &&
1713 mWaitingOnWallpaper.mClient.asBinder() == window) {
1714 mWaitingOnWallpaper = null;
Dianne Hackborn75804932009-10-20 20:15:20 -07001715 mWindowMap.notifyAll();
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001716 }
1717 }
1718 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001719
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001720 boolean updateWallpaperOffsetLocked(WindowState changingTarget, boolean sync) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001721 final int dw = mDisplay.getWidth();
1722 final int dh = mDisplay.getHeight();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001723
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001724 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001725
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001726 WindowState target = mWallpaperTarget;
1727 if (target != null) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001728 if (target.mWallpaperX >= 0) {
1729 mLastWallpaperX = target.mWallpaperX;
1730 } else if (changingTarget.mWallpaperX >= 0) {
1731 mLastWallpaperX = changingTarget.mWallpaperX;
1732 }
1733 if (target.mWallpaperY >= 0) {
1734 mLastWallpaperY = target.mWallpaperY;
1735 } else if (changingTarget.mWallpaperY >= 0) {
1736 mLastWallpaperY = changingTarget.mWallpaperY;
1737 }
1738 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001739
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001740 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 (updateWallpaperOffsetLocked(wallpaper, dw, dh, sync)) {
1749 wallpaper.computeShownFrameLocked();
1750 changed = true;
1751 // We only want to be synchronous with one wallpaper.
1752 sync = false;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001753 }
1754 }
1755 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001756
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001757 return changed;
1758 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001759
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001760 void updateWallpaperVisibilityLocked() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07001761 final boolean visible = isWallpaperVisible(mWallpaperTarget);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001762 final int dw = mDisplay.getWidth();
1763 final int dh = mDisplay.getHeight();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001764
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001765 int curTokenIndex = mWallpaperTokens.size();
1766 while (curTokenIndex > 0) {
1767 curTokenIndex--;
1768 WindowToken token = mWallpaperTokens.get(curTokenIndex);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001769 if (token.hidden == visible) {
1770 token.hidden = !visible;
1771 // Need to do a layout to ensure the wallpaper now has the
1772 // correct size.
1773 mLayoutNeeded = true;
1774 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001775
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001776 int curWallpaperIndex = token.windows.size();
1777 while (curWallpaperIndex > 0) {
1778 curWallpaperIndex--;
1779 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1780 if (visible) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001781 updateWallpaperOffsetLocked(wallpaper, dw, dh, false);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001782 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001783
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001784 if (wallpaper.mWallpaperVisible != visible) {
1785 wallpaper.mWallpaperVisible = visible;
1786 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001787 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -07001788 "Updating visibility of wallpaper " + wallpaper
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001789 + ": " + visible);
1790 wallpaper.mClient.dispatchAppVisibility(visible);
1791 } catch (RemoteException e) {
1792 }
1793 }
1794 }
1795 }
1796 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001797
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001798 void sendPointerToWallpaperLocked(WindowState srcWin,
1799 MotionEvent pointer, long eventTime) {
1800 int curTokenIndex = mWallpaperTokens.size();
1801 while (curTokenIndex > 0) {
1802 curTokenIndex--;
1803 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1804 int curWallpaperIndex = token.windows.size();
1805 while (curWallpaperIndex > 0) {
1806 curWallpaperIndex--;
1807 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1808 if ((wallpaper.mAttrs.flags &
1809 WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
1810 continue;
1811 }
1812 try {
1813 MotionEvent ev = MotionEvent.obtainNoHistory(pointer);
Dianne Hackborn6adba242009-11-10 11:10:09 -08001814 if (srcWin != null) {
1815 ev.offsetLocation(srcWin.mFrame.left-wallpaper.mFrame.left,
1816 srcWin.mFrame.top-wallpaper.mFrame.top);
1817 } else {
1818 ev.offsetLocation(-wallpaper.mFrame.left, -wallpaper.mFrame.top);
1819 }
1820 switch (pointer.getAction()) {
1821 case MotionEvent.ACTION_DOWN:
1822 mSendingPointersToWallpaper = true;
1823 break;
1824 case MotionEvent.ACTION_UP:
1825 mSendingPointersToWallpaper = false;
1826 break;
1827 }
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001828 wallpaper.mClient.dispatchPointer(ev, eventTime, false);
1829 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001830 Slog.w(TAG, "Failure sending pointer to wallpaper", e);
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001831 }
1832 }
1833 }
1834 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001835
Dianne Hackborn90d2db32010-02-11 22:19:06 -08001836 void dispatchPointerElsewhereLocked(WindowState srcWin, WindowState relWin,
1837 MotionEvent pointer, long eventTime, boolean skipped) {
1838 if (relWin != null) {
1839 mPolicy.dispatchedPointerEventLw(pointer, relWin.mFrame.left, relWin.mFrame.top);
1840 } else {
1841 mPolicy.dispatchedPointerEventLw(pointer, 0, 0);
1842 }
1843
1844 // If we sent an initial down to the wallpaper, then continue
1845 // sending events until the final up.
1846 if (mSendingPointersToWallpaper) {
1847 if (skipped) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001848 Slog.i(TAG, "Sending skipped pointer to wallpaper!");
Dianne Hackborn90d2db32010-02-11 22:19:06 -08001849 }
1850 sendPointerToWallpaperLocked(relWin, pointer, eventTime);
1851
1852 // If we are on top of the wallpaper, then the wallpaper also
1853 // gets to see this movement.
1854 } else if (srcWin != null
1855 && pointer.getAction() == MotionEvent.ACTION_DOWN
1856 && mWallpaperTarget == srcWin
1857 && srcWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
1858 sendPointerToWallpaperLocked(relWin, pointer, eventTime);
1859 }
1860 }
1861
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001862 public int addWindow(Session session, IWindow client,
1863 WindowManager.LayoutParams attrs, int viewVisibility,
1864 Rect outContentInsets) {
1865 int res = mPolicy.checkAddPermission(attrs);
1866 if (res != WindowManagerImpl.ADD_OKAY) {
1867 return res;
1868 }
Romain Guy06882f82009-06-10 13:36:04 -07001869
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001870 boolean reportNewConfig = false;
1871 WindowState attachedWindow = null;
1872 WindowState win = null;
Romain Guy06882f82009-06-10 13:36:04 -07001873
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001874 synchronized(mWindowMap) {
1875 // Instantiating a Display requires talking with the simulator,
1876 // so don't do it until we know the system is mostly up and
1877 // running.
1878 if (mDisplay == null) {
1879 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
1880 mDisplay = wm.getDefaultDisplay();
Christopher Tateb696aee2010-04-02 19:08:30 -07001881 mInitialDisplayWidth = mDisplay.getWidth();
1882 mInitialDisplayHeight = mDisplay.getHeight();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001883 mQueue.setDisplay(mDisplay);
1884 reportNewConfig = true;
1885 }
Romain Guy06882f82009-06-10 13:36:04 -07001886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001887 if (mWindowMap.containsKey(client.asBinder())) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001888 Slog.w(TAG, "Window " + client + " is already added");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001889 return WindowManagerImpl.ADD_DUPLICATE_ADD;
1890 }
1891
1892 if (attrs.type >= FIRST_SUB_WINDOW && attrs.type <= LAST_SUB_WINDOW) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001893 attachedWindow = windowForClientLocked(null, attrs.token, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001894 if (attachedWindow == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001895 Slog.w(TAG, "Attempted to add window with token that is not a window: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001896 + attrs.token + ". Aborting.");
1897 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1898 }
1899 if (attachedWindow.mAttrs.type >= FIRST_SUB_WINDOW
1900 && attachedWindow.mAttrs.type <= LAST_SUB_WINDOW) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001901 Slog.w(TAG, "Attempted to add window with token that is a sub-window: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001902 + attrs.token + ". Aborting.");
1903 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1904 }
1905 }
1906
1907 boolean addToken = false;
1908 WindowToken token = mTokenMap.get(attrs.token);
1909 if (token == null) {
1910 if (attrs.type >= FIRST_APPLICATION_WINDOW
1911 && attrs.type <= LAST_APPLICATION_WINDOW) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001912 Slog.w(TAG, "Attempted to add application window with unknown token "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001913 + attrs.token + ". Aborting.");
1914 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1915 }
1916 if (attrs.type == TYPE_INPUT_METHOD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001917 Slog.w(TAG, "Attempted to add input method window with unknown token "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001918 + attrs.token + ". Aborting.");
1919 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1920 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001921 if (attrs.type == TYPE_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001922 Slog.w(TAG, "Attempted to add wallpaper window with unknown token "
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001923 + attrs.token + ". Aborting.");
1924 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1925 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001926 token = new WindowToken(attrs.token, -1, false);
1927 addToken = true;
1928 } else if (attrs.type >= FIRST_APPLICATION_WINDOW
1929 && attrs.type <= LAST_APPLICATION_WINDOW) {
1930 AppWindowToken atoken = token.appWindowToken;
1931 if (atoken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001932 Slog.w(TAG, "Attempted to add window with non-application token "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001933 + token + ". Aborting.");
1934 return WindowManagerImpl.ADD_NOT_APP_TOKEN;
1935 } else if (atoken.removed) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001936 Slog.w(TAG, "Attempted to add window with exiting application token "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001937 + token + ". Aborting.");
1938 return WindowManagerImpl.ADD_APP_EXITING;
1939 }
1940 if (attrs.type == TYPE_APPLICATION_STARTING && atoken.firstWindowDrawn) {
1941 // No need for this guy!
Joe Onorato8a9b2202010-02-26 18:56:32 -08001942 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001943 TAG, "**** NO NEED TO START: " + attrs.getTitle());
1944 return WindowManagerImpl.ADD_STARTING_NOT_NEEDED;
1945 }
1946 } else if (attrs.type == TYPE_INPUT_METHOD) {
1947 if (token.windowType != TYPE_INPUT_METHOD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001948 Slog.w(TAG, "Attempted to add input method window with bad token "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001949 + attrs.token + ". Aborting.");
1950 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1951 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001952 } else if (attrs.type == TYPE_WALLPAPER) {
1953 if (token.windowType != TYPE_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001954 Slog.w(TAG, "Attempted to add wallpaper window with bad token "
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001955 + attrs.token + ". Aborting.");
1956 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1957 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001958 }
1959
1960 win = new WindowState(session, client, token,
1961 attachedWindow, attrs, viewVisibility);
1962 if (win.mDeathRecipient == null) {
1963 // Client has apparently died, so there is no reason to
1964 // continue.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001965 Slog.w(TAG, "Adding window client " + client.asBinder()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001966 + " that is dead, aborting.");
1967 return WindowManagerImpl.ADD_APP_EXITING;
1968 }
1969
1970 mPolicy.adjustWindowParamsLw(win.mAttrs);
Romain Guy06882f82009-06-10 13:36:04 -07001971
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001972 res = mPolicy.prepareAddWindowLw(win, attrs);
1973 if (res != WindowManagerImpl.ADD_OKAY) {
1974 return res;
1975 }
1976
1977 // From now on, no exceptions or errors allowed!
1978
1979 res = WindowManagerImpl.ADD_OKAY;
Romain Guy06882f82009-06-10 13:36:04 -07001980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001981 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001982
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001983 if (addToken) {
1984 mTokenMap.put(attrs.token, token);
1985 mTokenList.add(token);
1986 }
1987 win.attach();
1988 mWindowMap.put(client.asBinder(), win);
1989
1990 if (attrs.type == TYPE_APPLICATION_STARTING &&
1991 token.appWindowToken != null) {
1992 token.appWindowToken.startingWindow = win;
1993 }
1994
1995 boolean imMayMove = true;
Romain Guy06882f82009-06-10 13:36:04 -07001996
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001997 if (attrs.type == TYPE_INPUT_METHOD) {
1998 mInputMethodWindow = win;
1999 addInputMethodWindowToListLocked(win);
2000 imMayMove = false;
2001 } else if (attrs.type == TYPE_INPUT_METHOD_DIALOG) {
2002 mInputMethodDialogs.add(win);
2003 addWindowToListInOrderLocked(win, true);
2004 adjustInputMethodDialogsLocked();
2005 imMayMove = false;
2006 } else {
2007 addWindowToListInOrderLocked(win, true);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002008 if (attrs.type == TYPE_WALLPAPER) {
2009 mLastWallpaperTimeoutTime = 0;
2010 adjustWallpaperWindowsLocked();
2011 } else if ((attrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002012 adjustWallpaperWindowsLocked();
2013 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002014 }
Romain Guy06882f82009-06-10 13:36:04 -07002015
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002016 win.mEnterAnimationPending = true;
Romain Guy06882f82009-06-10 13:36:04 -07002017
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002018 mPolicy.getContentInsetHintLw(attrs, outContentInsets);
Romain Guy06882f82009-06-10 13:36:04 -07002019
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002020 if (mInTouchMode) {
2021 res |= WindowManagerImpl.ADD_FLAG_IN_TOUCH_MODE;
2022 }
2023 if (win == null || win.mAppToken == null || !win.mAppToken.clientHidden) {
2024 res |= WindowManagerImpl.ADD_FLAG_APP_VISIBLE;
2025 }
Romain Guy06882f82009-06-10 13:36:04 -07002026
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08002027 boolean focusChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002028 if (win.canReceiveKeys()) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08002029 if ((focusChanged=updateFocusedWindowLocked(UPDATE_FOCUS_WILL_ASSIGN_LAYERS))
2030 == true) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002031 imMayMove = false;
2032 }
2033 }
Romain Guy06882f82009-06-10 13:36:04 -07002034
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002035 if (imMayMove) {
Romain Guy06882f82009-06-10 13:36:04 -07002036 moveInputMethodWindowsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002037 }
Romain Guy06882f82009-06-10 13:36:04 -07002038
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002039 assignLayersLocked();
2040 // Don't do layout here, the window must call
2041 // relayout to be displayed, so we'll do it there.
Romain Guy06882f82009-06-10 13:36:04 -07002042
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002043 //dump();
2044
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08002045 if (focusChanged) {
2046 if (mCurrentFocus != null) {
2047 mKeyWaiter.handleNewWindowLocked(mCurrentFocus);
2048 }
2049 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08002050 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002051 TAG, "New client " + client.asBinder()
2052 + ": window=" + win);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002053
2054 if (win.isVisibleOrAdding() && updateOrientationFromAppTokensLocked()) {
2055 reportNewConfig = true;
2056 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002057 }
2058
2059 // sendNewConfiguration() checks caller permissions so we must call it with
2060 // privilege. updateOrientationFromAppTokens() clears and resets the caller
2061 // identity anyway, so it's safe to just clear & restore around this whole
2062 // block.
2063 final long origId = Binder.clearCallingIdentity();
2064 if (reportNewConfig) {
2065 sendNewConfiguration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002066 }
2067 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07002068
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002069 return res;
2070 }
Romain Guy06882f82009-06-10 13:36:04 -07002071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002072 public void removeWindow(Session session, IWindow client) {
2073 synchronized(mWindowMap) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002074 WindowState win = windowForClientLocked(session, client, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002075 if (win == null) {
2076 return;
2077 }
2078 removeWindowLocked(session, win);
2079 }
2080 }
Romain Guy06882f82009-06-10 13:36:04 -07002081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002082 public void removeWindowLocked(Session session, WindowState win) {
2083
Joe Onorato8a9b2202010-02-26 18:56:32 -08002084 if (localLOGV || DEBUG_FOCUS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002085 TAG, "Remove " + win + " client="
2086 + Integer.toHexString(System.identityHashCode(
2087 win.mClient.asBinder()))
2088 + ", surface=" + win.mSurface);
2089
2090 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002091
Joe Onorato8a9b2202010-02-26 18:56:32 -08002092 if (DEBUG_APP_TRANSITIONS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002093 TAG, "Remove " + win + ": mSurface=" + win.mSurface
2094 + " mExiting=" + win.mExiting
2095 + " isAnimating=" + win.isAnimating()
2096 + " app-animation="
2097 + (win.mAppToken != null ? win.mAppToken.animation : null)
2098 + " inPendingTransaction="
2099 + (win.mAppToken != null ? win.mAppToken.inPendingTransaction : false)
2100 + " mDisplayFrozen=" + mDisplayFrozen);
2101 // Visibility of the removed window. Will be used later to update orientation later on.
2102 boolean wasVisible = false;
2103 // First, see if we need to run an animation. If we do, we have
2104 // to hold off on removing the window until the animation is done.
2105 // If the display is frozen, just remove immediately, since the
2106 // animation wouldn't be seen.
Dianne Hackbornde2606d2009-12-18 16:53:55 -08002107 if (win.mSurface != null && !mDisplayFrozen && mPolicy.isScreenOn()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002108 // If we are not currently running the exit animation, we
2109 // need to see about starting one.
2110 if (wasVisible=win.isWinVisibleLw()) {
Romain Guy06882f82009-06-10 13:36:04 -07002111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002112 int transit = WindowManagerPolicy.TRANSIT_EXIT;
2113 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
2114 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
2115 }
2116 // Try starting an animation.
2117 if (applyAnimationLocked(win, transit, false)) {
2118 win.mExiting = true;
2119 }
2120 }
2121 if (win.mExiting || win.isAnimating()) {
2122 // The exit animation is running... wait for it!
Joe Onorato8a9b2202010-02-26 18:56:32 -08002123 //Slog.i(TAG, "*** Running exit animation...");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002124 win.mExiting = true;
2125 win.mRemoveOnExit = true;
2126 mLayoutNeeded = true;
2127 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
2128 performLayoutAndPlaceSurfacesLocked();
2129 if (win.mAppToken != null) {
2130 win.mAppToken.updateReportedVisibilityLocked();
2131 }
2132 //dump();
2133 Binder.restoreCallingIdentity(origId);
2134 return;
2135 }
2136 }
2137
2138 removeWindowInnerLocked(session, win);
2139 // Removing a visible window will effect the computed orientation
2140 // So just update orientation if needed.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002141 if (wasVisible && computeForcedAppOrientationLocked()
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002142 != mForcedAppOrientation
2143 && updateOrientationFromAppTokensLocked()) {
2144 mH.sendEmptyMessage(H.SEND_NEW_CONFIGURATION);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002145 }
2146 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2147 Binder.restoreCallingIdentity(origId);
2148 }
Romain Guy06882f82009-06-10 13:36:04 -07002149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002150 private void removeWindowInnerLocked(Session session, WindowState win) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07002151 mKeyWaiter.finishedKey(session, win.mClient, true,
2152 KeyWaiter.RETURN_NOTHING);
Maciej Białka26824b72010-03-05 08:40:09 +01002153 mKeyWaiter.releaseMotionTarget(win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002154 mKeyWaiter.releasePendingPointerLocked(win.mSession);
2155 mKeyWaiter.releasePendingTrackballLocked(win.mSession);
Romain Guy06882f82009-06-10 13:36:04 -07002156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002157 win.mRemoved = true;
Romain Guy06882f82009-06-10 13:36:04 -07002158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002159 if (mInputMethodTarget == win) {
2160 moveInputMethodWindowsIfNeededLocked(false);
2161 }
Romain Guy06882f82009-06-10 13:36:04 -07002162
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07002163 if (false) {
2164 RuntimeException e = new RuntimeException("here");
2165 e.fillInStackTrace();
Joe Onorato8a9b2202010-02-26 18:56:32 -08002166 Slog.w(TAG, "Removing window " + win, e);
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07002167 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002169 mPolicy.removeWindowLw(win);
2170 win.removeLocked();
2171
2172 mWindowMap.remove(win.mClient.asBinder());
2173 mWindows.remove(win);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002174 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "Final remove of window: " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002175
2176 if (mInputMethodWindow == win) {
2177 mInputMethodWindow = null;
2178 } else if (win.mAttrs.type == TYPE_INPUT_METHOD_DIALOG) {
2179 mInputMethodDialogs.remove(win);
2180 }
Romain Guy06882f82009-06-10 13:36:04 -07002181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002182 final WindowToken token = win.mToken;
2183 final AppWindowToken atoken = win.mAppToken;
2184 token.windows.remove(win);
2185 if (atoken != null) {
2186 atoken.allAppWindows.remove(win);
2187 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08002188 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002189 TAG, "**** Removing window " + win + ": count="
2190 + token.windows.size());
2191 if (token.windows.size() == 0) {
2192 if (!token.explicit) {
2193 mTokenMap.remove(token.token);
2194 mTokenList.remove(token);
2195 } else if (atoken != null) {
2196 atoken.firstWindowDrawn = false;
2197 }
2198 }
2199
2200 if (atoken != null) {
2201 if (atoken.startingWindow == win) {
2202 atoken.startingWindow = null;
2203 } else if (atoken.allAppWindows.size() == 0 && atoken.startingData != null) {
2204 // If this is the last window and we had requested a starting
2205 // transition window, well there is no point now.
2206 atoken.startingData = null;
2207 } else if (atoken.allAppWindows.size() == 1 && atoken.startingView != null) {
2208 // If this is the last window except for a starting transition
2209 // window, we need to get rid of the starting transition.
2210 if (DEBUG_STARTING_WINDOW) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002211 Slog.v(TAG, "Schedule remove starting " + token
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002212 + ": no more real windows");
2213 }
2214 Message m = mH.obtainMessage(H.REMOVE_STARTING, atoken);
2215 mH.sendMessage(m);
2216 }
2217 }
Romain Guy06882f82009-06-10 13:36:04 -07002218
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002219 if (win.mAttrs.type == TYPE_WALLPAPER) {
2220 mLastWallpaperTimeoutTime = 0;
2221 adjustWallpaperWindowsLocked();
2222 } else if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07002223 adjustWallpaperWindowsLocked();
2224 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002226 if (!mInLayout) {
2227 assignLayersLocked();
2228 mLayoutNeeded = true;
2229 performLayoutAndPlaceSurfacesLocked();
2230 if (win.mAppToken != null) {
2231 win.mAppToken.updateReportedVisibilityLocked();
2232 }
2233 }
2234 }
2235
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08002236 private static void logSurface(WindowState w, String msg, RuntimeException where) {
2237 String str = " SURFACE " + Integer.toHexString(w.hashCode())
2238 + ": " + msg + " / " + w.mAttrs.getTitle();
2239 if (where != null) {
2240 Slog.i(TAG, str, where);
2241 } else {
2242 Slog.i(TAG, str);
2243 }
2244 }
2245
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002246 private void setTransparentRegionWindow(Session session, IWindow client, Region region) {
2247 long origId = Binder.clearCallingIdentity();
2248 try {
2249 synchronized (mWindowMap) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002250 WindowState w = windowForClientLocked(session, client, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002251 if ((w != null) && (w.mSurface != null)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002252 if (SHOW_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002253 Surface.openTransaction();
2254 try {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08002255 if (SHOW_TRANSACTIONS) logSurface(w,
2256 "transparentRegionHint=" + region, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002257 w.mSurface.setTransparentRegionHint(region);
2258 } finally {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002259 if (SHOW_TRANSACTIONS) Slog.i(TAG, "<<< CLOSE TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002260 Surface.closeTransaction();
2261 }
2262 }
2263 }
2264 } finally {
2265 Binder.restoreCallingIdentity(origId);
2266 }
2267 }
2268
2269 void setInsetsWindow(Session session, IWindow client,
Romain Guy06882f82009-06-10 13:36:04 -07002270 int touchableInsets, Rect contentInsets,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002271 Rect visibleInsets) {
2272 long origId = Binder.clearCallingIdentity();
2273 try {
2274 synchronized (mWindowMap) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002275 WindowState w = windowForClientLocked(session, client, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002276 if (w != null) {
2277 w.mGivenInsetsPending = false;
2278 w.mGivenContentInsets.set(contentInsets);
2279 w.mGivenVisibleInsets.set(visibleInsets);
2280 w.mTouchableInsets = touchableInsets;
2281 mLayoutNeeded = true;
2282 performLayoutAndPlaceSurfacesLocked();
2283 }
2284 }
2285 } finally {
2286 Binder.restoreCallingIdentity(origId);
2287 }
2288 }
Romain Guy06882f82009-06-10 13:36:04 -07002289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002290 public void getWindowDisplayFrame(Session session, IWindow client,
2291 Rect outDisplayFrame) {
2292 synchronized(mWindowMap) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002293 WindowState win = windowForClientLocked(session, client, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002294 if (win == null) {
2295 outDisplayFrame.setEmpty();
2296 return;
2297 }
2298 outDisplayFrame.set(win.mDisplayFrame);
2299 }
2300 }
2301
Marco Nelissenbf6956b2009-11-09 15:21:13 -08002302 public void setWindowWallpaperPositionLocked(WindowState window, float x, float y,
2303 float xStep, float yStep) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002304 if (window.mWallpaperX != x || window.mWallpaperY != y) {
2305 window.mWallpaperX = x;
2306 window.mWallpaperY = y;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08002307 window.mWallpaperXStep = xStep;
2308 window.mWallpaperYStep = yStep;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07002309 if (updateWallpaperOffsetLocked(window, true)) {
2310 performLayoutAndPlaceSurfacesLocked();
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002311 }
2312 }
2313 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002314
Dianne Hackborn75804932009-10-20 20:15:20 -07002315 void wallpaperCommandComplete(IBinder window, Bundle result) {
2316 synchronized (mWindowMap) {
2317 if (mWaitingOnWallpaper != null &&
2318 mWaitingOnWallpaper.mClient.asBinder() == window) {
2319 mWaitingOnWallpaper = null;
2320 mWindowMap.notifyAll();
2321 }
2322 }
2323 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002324
Dianne Hackborn75804932009-10-20 20:15:20 -07002325 public Bundle sendWindowWallpaperCommandLocked(WindowState window,
2326 String action, int x, int y, int z, Bundle extras, boolean sync) {
2327 if (window == mWallpaperTarget || window == mLowerWallpaperTarget
2328 || window == mUpperWallpaperTarget) {
2329 boolean doWait = sync;
2330 int curTokenIndex = mWallpaperTokens.size();
2331 while (curTokenIndex > 0) {
2332 curTokenIndex--;
2333 WindowToken token = mWallpaperTokens.get(curTokenIndex);
2334 int curWallpaperIndex = token.windows.size();
2335 while (curWallpaperIndex > 0) {
2336 curWallpaperIndex--;
2337 WindowState wallpaper = token.windows.get(curWallpaperIndex);
2338 try {
2339 wallpaper.mClient.dispatchWallpaperCommand(action,
2340 x, y, z, extras, sync);
2341 // We only want to be synchronous with one wallpaper.
2342 sync = false;
2343 } catch (RemoteException e) {
2344 }
2345 }
2346 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002347
Dianne Hackborn75804932009-10-20 20:15:20 -07002348 if (doWait) {
2349 // XXX Need to wait for result.
2350 }
2351 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002352
Dianne Hackborn75804932009-10-20 20:15:20 -07002353 return null;
2354 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002356 public int relayoutWindow(Session session, IWindow client,
2357 WindowManager.LayoutParams attrs, int requestedWidth,
2358 int requestedHeight, int viewVisibility, boolean insetsPending,
2359 Rect outFrame, Rect outContentInsets, Rect outVisibleInsets,
Dianne Hackborn694f79b2010-03-17 19:44:59 -07002360 Configuration outConfig, Surface outSurface) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002361 boolean displayed = false;
2362 boolean inTouchMode;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002363 boolean configChanged;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002364 long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002366 synchronized(mWindowMap) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002367 WindowState win = windowForClientLocked(session, client, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002368 if (win == null) {
2369 return 0;
2370 }
2371 win.mRequestedWidth = requestedWidth;
2372 win.mRequestedHeight = requestedHeight;
2373
2374 if (attrs != null) {
2375 mPolicy.adjustWindowParamsLw(attrs);
2376 }
Romain Guy06882f82009-06-10 13:36:04 -07002377
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002378 int attrChanges = 0;
2379 int flagChanges = 0;
2380 if (attrs != null) {
2381 flagChanges = win.mAttrs.flags ^= attrs.flags;
2382 attrChanges = win.mAttrs.copyFrom(attrs);
2383 }
2384
Joe Onorato8a9b2202010-02-26 18:56:32 -08002385 if (DEBUG_LAYOUT) Slog.v(TAG, "Relayout " + win + ": " + win.mAttrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002386
2387 if ((attrChanges & WindowManager.LayoutParams.ALPHA_CHANGED) != 0) {
2388 win.mAlpha = attrs.alpha;
2389 }
2390
2391 final boolean scaledWindow =
2392 ((win.mAttrs.flags & WindowManager.LayoutParams.FLAG_SCALED) != 0);
2393
2394 if (scaledWindow) {
2395 // requested{Width|Height} Surface's physical size
2396 // attrs.{width|height} Size on screen
2397 win.mHScale = (attrs.width != requestedWidth) ?
2398 (attrs.width / (float)requestedWidth) : 1.0f;
2399 win.mVScale = (attrs.height != requestedHeight) ?
2400 (attrs.height / (float)requestedHeight) : 1.0f;
Dianne Hackborn9b52a212009-12-11 14:51:35 -08002401 } else {
2402 win.mHScale = win.mVScale = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002403 }
2404
2405 boolean imMayMove = (flagChanges&(
2406 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
2407 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07002408
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002409 boolean focusMayChange = win.mViewVisibility != viewVisibility
2410 || ((flagChanges&WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) != 0)
2411 || (!win.mRelayoutCalled);
Romain Guy06882f82009-06-10 13:36:04 -07002412
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002413 boolean wallpaperMayMove = win.mViewVisibility != viewVisibility
2414 && (win.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002416 win.mRelayoutCalled = true;
2417 final int oldVisibility = win.mViewVisibility;
2418 win.mViewVisibility = viewVisibility;
2419 if (viewVisibility == View.VISIBLE &&
2420 (win.mAppToken == null || !win.mAppToken.clientHidden)) {
2421 displayed = !win.isVisibleLw();
2422 if (win.mExiting) {
2423 win.mExiting = false;
2424 win.mAnimation = null;
2425 }
2426 if (win.mDestroying) {
2427 win.mDestroying = false;
2428 mDestroySurface.remove(win);
2429 }
2430 if (oldVisibility == View.GONE) {
2431 win.mEnterAnimationPending = true;
2432 }
Dianne Hackborn694f79b2010-03-17 19:44:59 -07002433 if (displayed) {
2434 if (win.mSurface != null && !win.mDrawPending
2435 && !win.mCommitDrawPending && !mDisplayFrozen
2436 && mPolicy.isScreenOn()) {
2437 applyEnterAnimationLocked(win);
2438 }
2439 if ((win.mAttrs.flags
2440 & WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON) != 0) {
2441 if (DEBUG_VISIBILITY) Slog.v(TAG,
2442 "Relayout window turning screen on: " + win);
2443 win.mTurnOnScreen = true;
2444 }
2445 int diff = 0;
2446 if (win.mConfiguration != mCurConfiguration
2447 && (win.mConfiguration == null
2448 || (diff=mCurConfiguration.diff(win.mConfiguration)) != 0)) {
2449 win.mConfiguration = mCurConfiguration;
2450 if (DEBUG_CONFIGURATION) {
2451 Slog.i(TAG, "Window " + win + " visible with new config: "
2452 + win.mConfiguration + " / 0x"
2453 + Integer.toHexString(diff));
2454 }
2455 outConfig.setTo(mCurConfiguration);
2456 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07002457 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002458 if ((attrChanges&WindowManager.LayoutParams.FORMAT_CHANGED) != 0) {
2459 // To change the format, we need to re-build the surface.
2460 win.destroySurfaceLocked();
2461 displayed = true;
2462 }
2463 try {
2464 Surface surface = win.createSurfaceLocked();
2465 if (surface != null) {
2466 outSurface.copyFrom(surface);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002467 win.mReportDestroySurface = false;
2468 win.mSurfacePendingDestroy = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -08002469 if (SHOW_TRANSACTIONS) Slog.i(TAG,
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002470 " OUT SURFACE " + outSurface + ": copied");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002471 } else {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002472 // For some reason there isn't a surface. Clear the
2473 // caller's object so they see the same state.
2474 outSurface.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002475 }
2476 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002477 Slog.w(TAG, "Exception thrown when creating surface for client "
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002478 + client + " (" + win.mAttrs.getTitle() + ")",
2479 e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002480 Binder.restoreCallingIdentity(origId);
2481 return 0;
2482 }
2483 if (displayed) {
2484 focusMayChange = true;
2485 }
2486 if (win.mAttrs.type == TYPE_INPUT_METHOD
2487 && mInputMethodWindow == null) {
2488 mInputMethodWindow = win;
2489 imMayMove = true;
2490 }
Dianne Hackborn558947c2009-12-18 16:02:50 -08002491 if (win.mAttrs.type == TYPE_BASE_APPLICATION
2492 && win.mAppToken != null
2493 && win.mAppToken.startingWindow != null) {
2494 // Special handling of starting window over the base
2495 // window of the app: propagate lock screen flags to it,
2496 // to provide the correct semantics while starting.
2497 final int mask =
2498 WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
Mike Lockwoodef731622010-01-27 17:51:34 -05002499 | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
2500 | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON;
Dianne Hackborn558947c2009-12-18 16:02:50 -08002501 WindowManager.LayoutParams sa = win.mAppToken.startingWindow.mAttrs;
2502 sa.flags = (sa.flags&~mask) | (win.mAttrs.flags&mask);
2503 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002504 } else {
2505 win.mEnterAnimationPending = false;
2506 if (win.mSurface != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002507 if (DEBUG_VISIBILITY) Slog.i(TAG, "Relayout invis " + win
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002508 + ": mExiting=" + win.mExiting
2509 + " mSurfacePendingDestroy=" + win.mSurfacePendingDestroy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002510 // If we are not currently running the exit animation, we
2511 // need to see about starting one.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002512 if (!win.mExiting || win.mSurfacePendingDestroy) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002513 // Try starting an animation; if there isn't one, we
2514 // can destroy the surface right away.
2515 int transit = WindowManagerPolicy.TRANSIT_EXIT;
2516 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
2517 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
2518 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002519 if (!win.mSurfacePendingDestroy && win.isWinVisibleLw() &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002520 applyAnimationLocked(win, transit, false)) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002521 focusMayChange = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002522 win.mExiting = true;
2523 mKeyWaiter.finishedKey(session, client, true,
2524 KeyWaiter.RETURN_NOTHING);
2525 } else if (win.isAnimating()) {
2526 // Currently in a hide animation... turn this into
2527 // an exit.
2528 win.mExiting = true;
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07002529 } else if (win == mWallpaperTarget) {
2530 // If the wallpaper is currently behind this
2531 // window, we need to change both of them inside
2532 // of a transaction to avoid artifacts.
2533 win.mExiting = true;
2534 win.mAnimating = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002535 } else {
2536 if (mInputMethodWindow == win) {
2537 mInputMethodWindow = null;
2538 }
2539 win.destroySurfaceLocked();
2540 }
2541 }
2542 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002543
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002544 if (win.mSurface == null || (win.getAttrs().flags
2545 & WindowManager.LayoutParams.FLAG_KEEP_SURFACE_WHILE_ANIMATING) == 0
2546 || win.mSurfacePendingDestroy) {
2547 // We are being called from a local process, which
2548 // means outSurface holds its current surface. Ensure the
2549 // surface object is cleared, but we don't want it actually
2550 // destroyed at this point.
2551 win.mSurfacePendingDestroy = false;
2552 outSurface.release();
Joe Onorato8a9b2202010-02-26 18:56:32 -08002553 if (DEBUG_VISIBILITY) Slog.i(TAG, "Releasing surface in: " + win);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002554 } else if (win.mSurface != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002555 if (DEBUG_VISIBILITY) Slog.i(TAG,
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002556 "Keeping surface, will report destroy: " + win);
2557 win.mReportDestroySurface = true;
2558 outSurface.copyFrom(win.mSurface);
2559 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002560 }
2561
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002562 if (focusMayChange) {
2563 //System.out.println("Focus may change: " + win.mAttrs.getTitle());
2564 if (updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002565 imMayMove = false;
2566 }
2567 //System.out.println("Relayout " + win + ": focus=" + mCurrentFocus);
2568 }
Romain Guy06882f82009-06-10 13:36:04 -07002569
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08002570 // updateFocusedWindowLocked() already assigned layers so we only need to
2571 // reassign them at this point if the IM window state gets shuffled
2572 boolean assignLayers = false;
Romain Guy06882f82009-06-10 13:36:04 -07002573
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002574 if (imMayMove) {
Dianne Hackborn8abd5f02009-11-20 18:09:03 -08002575 if (moveInputMethodWindowsIfNeededLocked(false) || displayed) {
2576 // Little hack here -- we -should- be able to rely on the
2577 // function to return true if the IME has moved and needs
2578 // its layer recomputed. However, if the IME was hidden
2579 // and isn't actually moved in the list, its layer may be
2580 // out of data so we make sure to recompute it.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002581 assignLayers = true;
2582 }
2583 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002584 if (wallpaperMayMove) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002585 if ((adjustWallpaperWindowsLocked()&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002586 assignLayers = true;
2587 }
2588 }
Romain Guy06882f82009-06-10 13:36:04 -07002589
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002590 mLayoutNeeded = true;
2591 win.mGivenInsetsPending = insetsPending;
2592 if (assignLayers) {
2593 assignLayersLocked();
2594 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002595 configChanged = updateOrientationFromAppTokensLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002596 performLayoutAndPlaceSurfacesLocked();
Dianne Hackborn284ac932009-08-28 10:34:25 -07002597 if (displayed && win.mIsWallpaper) {
2598 updateWallpaperOffsetLocked(win, mDisplay.getWidth(),
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002599 mDisplay.getHeight(), false);
Dianne Hackborn284ac932009-08-28 10:34:25 -07002600 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002601 if (win.mAppToken != null) {
2602 win.mAppToken.updateReportedVisibilityLocked();
2603 }
2604 outFrame.set(win.mFrame);
2605 outContentInsets.set(win.mContentInsets);
2606 outVisibleInsets.set(win.mVisibleInsets);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002607 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002608 TAG, "Relayout given client " + client.asBinder()
Romain Guy06882f82009-06-10 13:36:04 -07002609 + ", requestedWidth=" + requestedWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002610 + ", requestedHeight=" + requestedHeight
2611 + ", viewVisibility=" + viewVisibility
2612 + "\nRelayout returning frame=" + outFrame
2613 + ", surface=" + outSurface);
2614
Joe Onorato8a9b2202010-02-26 18:56:32 -08002615 if (localLOGV || DEBUG_FOCUS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002616 TAG, "Relayout of " + win + ": focusMayChange=" + focusMayChange);
2617
2618 inTouchMode = mInTouchMode;
2619 }
2620
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002621 if (configChanged) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002622 sendNewConfiguration();
2623 }
Romain Guy06882f82009-06-10 13:36:04 -07002624
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002625 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07002626
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002627 return (inTouchMode ? WindowManagerImpl.RELAYOUT_IN_TOUCH_MODE : 0)
2628 | (displayed ? WindowManagerImpl.RELAYOUT_FIRST_TIME : 0);
2629 }
2630
2631 public void finishDrawingWindow(Session session, IWindow client) {
2632 final long origId = Binder.clearCallingIdentity();
2633 synchronized(mWindowMap) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002634 WindowState win = windowForClientLocked(session, client, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002635 if (win != null && win.finishDrawingLocked()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07002636 if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
2637 adjustWallpaperWindowsLocked();
2638 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002639 mLayoutNeeded = true;
2640 performLayoutAndPlaceSurfacesLocked();
2641 }
2642 }
2643 Binder.restoreCallingIdentity(origId);
2644 }
2645
2646 private AttributeCache.Entry getCachedAnimations(WindowManager.LayoutParams lp) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002647 if (DEBUG_ANIM) Slog.v(TAG, "Loading animations: params package="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002648 + (lp != null ? lp.packageName : null)
2649 + " resId=0x" + (lp != null ? Integer.toHexString(lp.windowAnimations) : null));
2650 if (lp != null && lp.windowAnimations != 0) {
2651 // If this is a system resource, don't try to load it from the
2652 // application resources. It is nice to avoid loading application
2653 // resources if we can.
2654 String packageName = lp.packageName != null ? lp.packageName : "android";
2655 int resId = lp.windowAnimations;
2656 if ((resId&0xFF000000) == 0x01000000) {
2657 packageName = "android";
2658 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08002659 if (DEBUG_ANIM) Slog.v(TAG, "Loading animations: picked package="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002660 + packageName);
2661 return AttributeCache.instance().get(packageName, resId,
2662 com.android.internal.R.styleable.WindowAnimation);
2663 }
2664 return null;
2665 }
Romain Guy06882f82009-06-10 13:36:04 -07002666
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002667 private AttributeCache.Entry getCachedAnimations(String packageName, int resId) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002668 if (DEBUG_ANIM) Slog.v(TAG, "Loading animations: params package="
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002669 + packageName + " resId=0x" + Integer.toHexString(resId));
2670 if (packageName != null) {
2671 if ((resId&0xFF000000) == 0x01000000) {
2672 packageName = "android";
2673 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08002674 if (DEBUG_ANIM) Slog.v(TAG, "Loading animations: picked package="
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002675 + packageName);
2676 return AttributeCache.instance().get(packageName, resId,
2677 com.android.internal.R.styleable.WindowAnimation);
2678 }
2679 return null;
2680 }
2681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002682 private void applyEnterAnimationLocked(WindowState win) {
2683 int transit = WindowManagerPolicy.TRANSIT_SHOW;
2684 if (win.mEnterAnimationPending) {
2685 win.mEnterAnimationPending = false;
2686 transit = WindowManagerPolicy.TRANSIT_ENTER;
2687 }
2688
2689 applyAnimationLocked(win, transit, true);
2690 }
2691
2692 private boolean applyAnimationLocked(WindowState win,
2693 int transit, boolean isEntrance) {
2694 if (win.mLocalAnimating && win.mAnimationIsEntrance == isEntrance) {
2695 // If we are trying to apply an animation, but already running
2696 // an animation of the same type, then just leave that one alone.
2697 return true;
2698 }
Romain Guy06882f82009-06-10 13:36:04 -07002699
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002700 // Only apply an animation if the display isn't frozen. If it is
2701 // frozen, there is no reason to animate and it can cause strange
2702 // artifacts when we unfreeze the display if some different animation
2703 // is running.
Dianne Hackbornde2606d2009-12-18 16:53:55 -08002704 if (!mDisplayFrozen && mPolicy.isScreenOn()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002705 int anim = mPolicy.selectAnimationLw(win, transit);
2706 int attr = -1;
2707 Animation a = null;
2708 if (anim != 0) {
2709 a = AnimationUtils.loadAnimation(mContext, anim);
2710 } else {
2711 switch (transit) {
2712 case WindowManagerPolicy.TRANSIT_ENTER:
2713 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
2714 break;
2715 case WindowManagerPolicy.TRANSIT_EXIT:
2716 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
2717 break;
2718 case WindowManagerPolicy.TRANSIT_SHOW:
2719 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
2720 break;
2721 case WindowManagerPolicy.TRANSIT_HIDE:
2722 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
2723 break;
2724 }
2725 if (attr >= 0) {
2726 a = loadAnimation(win.mAttrs, attr);
2727 }
2728 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08002729 if (DEBUG_ANIM) Slog.v(TAG, "applyAnimation: win=" + win
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002730 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
2731 + " mAnimation=" + win.mAnimation
2732 + " isEntrance=" + isEntrance);
2733 if (a != null) {
2734 if (DEBUG_ANIM) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08002735 RuntimeException e = null;
2736 if (!HIDE_STACK_CRAWLS) {
2737 e = new RuntimeException();
2738 e.fillInStackTrace();
2739 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08002740 Slog.v(TAG, "Loaded animation " + a + " for " + win, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002741 }
2742 win.setAnimation(a);
2743 win.mAnimationIsEntrance = isEntrance;
2744 }
2745 } else {
2746 win.clearAnimation();
2747 }
2748
2749 return win.mAnimation != null;
2750 }
2751
2752 private Animation loadAnimation(WindowManager.LayoutParams lp, int animAttr) {
2753 int anim = 0;
2754 Context context = mContext;
2755 if (animAttr >= 0) {
2756 AttributeCache.Entry ent = getCachedAnimations(lp);
2757 if (ent != null) {
2758 context = ent.context;
2759 anim = ent.array.getResourceId(animAttr, 0);
2760 }
2761 }
2762 if (anim != 0) {
2763 return AnimationUtils.loadAnimation(context, anim);
2764 }
2765 return null;
2766 }
Romain Guy06882f82009-06-10 13:36:04 -07002767
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002768 private Animation loadAnimation(String packageName, int resId) {
2769 int anim = 0;
2770 Context context = mContext;
2771 if (resId >= 0) {
2772 AttributeCache.Entry ent = getCachedAnimations(packageName, resId);
2773 if (ent != null) {
2774 context = ent.context;
2775 anim = resId;
2776 }
2777 }
2778 if (anim != 0) {
2779 return AnimationUtils.loadAnimation(context, anim);
2780 }
2781 return null;
2782 }
2783
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002784 private boolean applyAnimationLocked(AppWindowToken wtoken,
2785 WindowManager.LayoutParams lp, int transit, boolean enter) {
2786 // Only apply an animation if the display isn't frozen. If it is
2787 // frozen, there is no reason to animate and it can cause strange
2788 // artifacts when we unfreeze the display if some different animation
2789 // is running.
Dianne Hackbornde2606d2009-12-18 16:53:55 -08002790 if (!mDisplayFrozen && mPolicy.isScreenOn()) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002791 Animation a;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07002792 if (lp != null && (lp.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002793 a = new FadeInOutAnimation(enter);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002794 if (DEBUG_ANIM) Slog.v(TAG,
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002795 "applying FadeInOutAnimation for a window in compatibility mode");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002796 } else if (mNextAppTransitionPackage != null) {
2797 a = loadAnimation(mNextAppTransitionPackage, enter ?
2798 mNextAppTransitionEnter : mNextAppTransitionExit);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002799 } else {
2800 int animAttr = 0;
2801 switch (transit) {
2802 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
2803 animAttr = enter
2804 ? com.android.internal.R.styleable.WindowAnimation_activityOpenEnterAnimation
2805 : com.android.internal.R.styleable.WindowAnimation_activityOpenExitAnimation;
2806 break;
2807 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
2808 animAttr = enter
2809 ? com.android.internal.R.styleable.WindowAnimation_activityCloseEnterAnimation
2810 : com.android.internal.R.styleable.WindowAnimation_activityCloseExitAnimation;
2811 break;
2812 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
2813 animAttr = enter
2814 ? com.android.internal.R.styleable.WindowAnimation_taskOpenEnterAnimation
2815 : com.android.internal.R.styleable.WindowAnimation_taskOpenExitAnimation;
2816 break;
2817 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
2818 animAttr = enter
2819 ? com.android.internal.R.styleable.WindowAnimation_taskCloseEnterAnimation
2820 : com.android.internal.R.styleable.WindowAnimation_taskCloseExitAnimation;
2821 break;
2822 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
2823 animAttr = enter
2824 ? com.android.internal.R.styleable.WindowAnimation_taskToFrontEnterAnimation
2825 : com.android.internal.R.styleable.WindowAnimation_taskToFrontExitAnimation;
2826 break;
2827 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
2828 animAttr = enter
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07002829 ? com.android.internal.R.styleable.WindowAnimation_taskToBackEnterAnimation
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002830 : com.android.internal.R.styleable.WindowAnimation_taskToBackExitAnimation;
2831 break;
Dianne Hackborn25994b42009-09-04 14:21:19 -07002832 case WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN:
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002833 animAttr = enter
Dianne Hackborn25994b42009-09-04 14:21:19 -07002834 ? com.android.internal.R.styleable.WindowAnimation_wallpaperOpenEnterAnimation
2835 : com.android.internal.R.styleable.WindowAnimation_wallpaperOpenExitAnimation;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002836 break;
Dianne Hackborn25994b42009-09-04 14:21:19 -07002837 case WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE:
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002838 animAttr = enter
Dianne Hackborn25994b42009-09-04 14:21:19 -07002839 ? com.android.internal.R.styleable.WindowAnimation_wallpaperCloseEnterAnimation
2840 : com.android.internal.R.styleable.WindowAnimation_wallpaperCloseExitAnimation;
2841 break;
2842 case WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN:
2843 animAttr = enter
2844 ? com.android.internal.R.styleable.WindowAnimation_wallpaperIntraOpenEnterAnimation
2845 : com.android.internal.R.styleable.WindowAnimation_wallpaperIntraOpenExitAnimation;
2846 break;
2847 case WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE:
2848 animAttr = enter
2849 ? com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseEnterAnimation
2850 : com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseExitAnimation;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002851 break;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002852 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07002853 a = animAttr != 0 ? loadAnimation(lp, animAttr) : null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08002854 if (DEBUG_ANIM) Slog.v(TAG, "applyAnimation: wtoken=" + wtoken
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002855 + " anim=" + a
2856 + " animAttr=0x" + Integer.toHexString(animAttr)
2857 + " transit=" + transit);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002858 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002859 if (a != null) {
2860 if (DEBUG_ANIM) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08002861 RuntimeException e = null;
2862 if (!HIDE_STACK_CRAWLS) {
2863 e = new RuntimeException();
2864 e.fillInStackTrace();
2865 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08002866 Slog.v(TAG, "Loaded animation " + a + " for " + wtoken, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002867 }
2868 wtoken.setAnimation(a);
2869 }
2870 } else {
2871 wtoken.clearAnimation();
2872 }
2873
2874 return wtoken.animation != null;
2875 }
2876
2877 // -------------------------------------------------------------
2878 // Application Window Tokens
2879 // -------------------------------------------------------------
2880
2881 public void validateAppTokens(List tokens) {
2882 int v = tokens.size()-1;
2883 int m = mAppTokens.size()-1;
2884 while (v >= 0 && m >= 0) {
2885 AppWindowToken wtoken = mAppTokens.get(m);
2886 if (wtoken.removed) {
2887 m--;
2888 continue;
2889 }
2890 if (tokens.get(v) != wtoken.token) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002891 Slog.w(TAG, "Tokens out of sync: external is " + tokens.get(v)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002892 + " @ " + v + ", internal is " + wtoken.token + " @ " + m);
2893 }
2894 v--;
2895 m--;
2896 }
2897 while (v >= 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002898 Slog.w(TAG, "External token not found: " + tokens.get(v) + " @ " + v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002899 v--;
2900 }
2901 while (m >= 0) {
2902 AppWindowToken wtoken = mAppTokens.get(m);
2903 if (!wtoken.removed) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002904 Slog.w(TAG, "Invalid internal token: " + wtoken.token + " @ " + m);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002905 }
2906 m--;
2907 }
2908 }
2909
2910 boolean checkCallingPermission(String permission, String func) {
2911 // Quick check: if the calling permission is me, it's all okay.
2912 if (Binder.getCallingPid() == Process.myPid()) {
2913 return true;
2914 }
Romain Guy06882f82009-06-10 13:36:04 -07002915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002916 if (mContext.checkCallingPermission(permission)
2917 == PackageManager.PERMISSION_GRANTED) {
2918 return true;
2919 }
2920 String msg = "Permission Denial: " + func + " from pid="
2921 + Binder.getCallingPid()
2922 + ", uid=" + Binder.getCallingUid()
2923 + " requires " + permission;
Joe Onorato8a9b2202010-02-26 18:56:32 -08002924 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002925 return false;
2926 }
Romain Guy06882f82009-06-10 13:36:04 -07002927
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002928 AppWindowToken findAppWindowToken(IBinder token) {
2929 WindowToken wtoken = mTokenMap.get(token);
2930 if (wtoken == null) {
2931 return null;
2932 }
2933 return wtoken.appWindowToken;
2934 }
Romain Guy06882f82009-06-10 13:36:04 -07002935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002936 public void addWindowToken(IBinder token, int type) {
2937 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2938 "addWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002939 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002940 }
Romain Guy06882f82009-06-10 13:36:04 -07002941
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002942 synchronized(mWindowMap) {
2943 WindowToken wtoken = mTokenMap.get(token);
2944 if (wtoken != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002945 Slog.w(TAG, "Attempted to add existing input method token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002946 return;
2947 }
2948 wtoken = new WindowToken(token, type, true);
2949 mTokenMap.put(token, wtoken);
2950 mTokenList.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002951 if (type == TYPE_WALLPAPER) {
2952 mWallpaperTokens.add(wtoken);
2953 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002954 }
2955 }
Romain Guy06882f82009-06-10 13:36:04 -07002956
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002957 public void removeWindowToken(IBinder token) {
2958 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2959 "removeWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002960 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002961 }
2962
2963 final long origId = Binder.clearCallingIdentity();
2964 synchronized(mWindowMap) {
2965 WindowToken wtoken = mTokenMap.remove(token);
2966 mTokenList.remove(wtoken);
2967 if (wtoken != null) {
2968 boolean delayed = false;
2969 if (!wtoken.hidden) {
2970 wtoken.hidden = true;
Romain Guy06882f82009-06-10 13:36:04 -07002971
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002972 final int N = wtoken.windows.size();
2973 boolean changed = false;
Romain Guy06882f82009-06-10 13:36:04 -07002974
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002975 for (int i=0; i<N; i++) {
2976 WindowState win = wtoken.windows.get(i);
2977
2978 if (win.isAnimating()) {
2979 delayed = true;
2980 }
Romain Guy06882f82009-06-10 13:36:04 -07002981
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002982 if (win.isVisibleNow()) {
2983 applyAnimationLocked(win,
2984 WindowManagerPolicy.TRANSIT_EXIT, false);
2985 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2986 KeyWaiter.RETURN_NOTHING);
2987 changed = true;
2988 }
2989 }
2990
2991 if (changed) {
2992 mLayoutNeeded = true;
2993 performLayoutAndPlaceSurfacesLocked();
2994 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2995 }
Romain Guy06882f82009-06-10 13:36:04 -07002996
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002997 if (delayed) {
2998 mExitingTokens.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002999 } else if (wtoken.windowType == TYPE_WALLPAPER) {
3000 mWallpaperTokens.remove(wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003001 }
3002 }
Romain Guy06882f82009-06-10 13:36:04 -07003003
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003004 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003005 Slog.w(TAG, "Attempted to remove non-existing token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003006 }
3007 }
3008 Binder.restoreCallingIdentity(origId);
3009 }
3010
3011 public void addAppToken(int addPos, IApplicationToken token,
3012 int groupId, int requestedOrientation, boolean fullscreen) {
3013 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3014 "addAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003015 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003016 }
Romain Guy06882f82009-06-10 13:36:04 -07003017
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003018 synchronized(mWindowMap) {
3019 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
3020 if (wtoken != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003021 Slog.w(TAG, "Attempted to add existing app token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003022 return;
3023 }
3024 wtoken = new AppWindowToken(token);
3025 wtoken.groupId = groupId;
3026 wtoken.appFullscreen = fullscreen;
3027 wtoken.requestedOrientation = requestedOrientation;
3028 mAppTokens.add(addPos, wtoken);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003029 if (localLOGV) Slog.v(TAG, "Adding new app token: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003030 mTokenMap.put(token.asBinder(), wtoken);
3031 mTokenList.add(wtoken);
Romain Guy06882f82009-06-10 13:36:04 -07003032
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003033 // Application tokens start out hidden.
3034 wtoken.hidden = true;
3035 wtoken.hiddenRequested = true;
Romain Guy06882f82009-06-10 13:36:04 -07003036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003037 //dump();
3038 }
3039 }
Romain Guy06882f82009-06-10 13:36:04 -07003040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003041 public void setAppGroupId(IBinder token, int groupId) {
3042 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3043 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003044 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003045 }
3046
3047 synchronized(mWindowMap) {
3048 AppWindowToken wtoken = findAppWindowToken(token);
3049 if (wtoken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003050 Slog.w(TAG, "Attempted to set group id of non-existing app token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003051 return;
3052 }
3053 wtoken.groupId = groupId;
3054 }
3055 }
Romain Guy06882f82009-06-10 13:36:04 -07003056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003057 public int getOrientationFromWindowsLocked() {
3058 int pos = mWindows.size() - 1;
3059 while (pos >= 0) {
3060 WindowState wtoken = (WindowState) mWindows.get(pos);
3061 pos--;
3062 if (wtoken.mAppToken != null) {
3063 // We hit an application window. so the orientation will be determined by the
3064 // app window. No point in continuing further.
3065 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
3066 }
Christopher Tateb696aee2010-04-02 19:08:30 -07003067 if (!wtoken.isVisibleLw() || !wtoken.mPolicyVisibilityAfterAnim) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003068 continue;
3069 }
3070 int req = wtoken.mAttrs.screenOrientation;
3071 if((req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) ||
3072 (req == ActivityInfo.SCREEN_ORIENTATION_BEHIND)){
3073 continue;
3074 } else {
3075 return req;
3076 }
3077 }
3078 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
3079 }
Romain Guy06882f82009-06-10 13:36:04 -07003080
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003081 public int getOrientationFromAppTokensLocked() {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003082 int pos = mAppTokens.size() - 1;
3083 int curGroup = 0;
3084 int lastOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
3085 boolean findingBehind = false;
3086 boolean haveGroup = false;
3087 boolean lastFullscreen = false;
3088 while (pos >= 0) {
3089 AppWindowToken wtoken = mAppTokens.get(pos);
3090 pos--;
3091 // if we're about to tear down this window and not seek for
3092 // the behind activity, don't use it for orientation
3093 if (!findingBehind
3094 && (!wtoken.hidden && wtoken.hiddenRequested)) {
3095 continue;
3096 }
3097
3098 if (!haveGroup) {
3099 // We ignore any hidden applications on the top.
3100 if (wtoken.hiddenRequested || wtoken.willBeHidden) {
The Android Open Source Project10592532009-03-18 17:39:46 -07003101 continue;
3102 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003103 haveGroup = true;
3104 curGroup = wtoken.groupId;
3105 lastOrientation = wtoken.requestedOrientation;
3106 } else if (curGroup != wtoken.groupId) {
3107 // If we have hit a new application group, and the bottom
3108 // of the previous group didn't explicitly say to use
3109 // the orientation behind it, and the last app was
3110 // full screen, then we'll stick with the
3111 // user's orientation.
3112 if (lastOrientation != ActivityInfo.SCREEN_ORIENTATION_BEHIND
3113 && lastFullscreen) {
3114 return lastOrientation;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003115 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003116 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003117 int or = wtoken.requestedOrientation;
3118 // If this application is fullscreen, and didn't explicitly say
3119 // to use the orientation behind it, then just take whatever
3120 // orientation it has and ignores whatever is under it.
3121 lastFullscreen = wtoken.appFullscreen;
3122 if (lastFullscreen
3123 && or != ActivityInfo.SCREEN_ORIENTATION_BEHIND) {
3124 return or;
3125 }
3126 // If this application has requested an explicit orientation,
3127 // then use it.
3128 if (or == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE ||
3129 or == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ||
3130 or == ActivityInfo.SCREEN_ORIENTATION_SENSOR ||
3131 or == ActivityInfo.SCREEN_ORIENTATION_NOSENSOR ||
3132 or == ActivityInfo.SCREEN_ORIENTATION_USER) {
3133 return or;
3134 }
3135 findingBehind |= (or == ActivityInfo.SCREEN_ORIENTATION_BEHIND);
3136 }
3137 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003138 }
Romain Guy06882f82009-06-10 13:36:04 -07003139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003140 public Configuration updateOrientationFromAppTokens(
The Android Open Source Project10592532009-03-18 17:39:46 -07003141 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003142 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3143 "updateOrientationFromAppTokens()")) {
3144 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
3145 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003146
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003147 Configuration config = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003148 long ident = Binder.clearCallingIdentity();
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003149
3150 synchronized(mWindowMap) {
3151 if (updateOrientationFromAppTokensLocked()) {
3152 if (freezeThisOneIfNeeded != null) {
3153 AppWindowToken wtoken = findAppWindowToken(
3154 freezeThisOneIfNeeded);
3155 if (wtoken != null) {
3156 startAppFreezingScreenLocked(wtoken,
3157 ActivityInfo.CONFIG_ORIENTATION);
3158 }
3159 }
3160 config = computeNewConfigurationLocked();
3161
3162 } else if (currentConfig != null) {
3163 // No obvious action we need to take, but if our current
3164 // state mismatches the activity maanager's, update it
3165 mTempConfiguration.setToDefaults();
3166 if (computeNewConfigurationLocked(mTempConfiguration)) {
3167 if (currentConfig.diff(mTempConfiguration) != 0) {
3168 mWaitingForConfig = true;
3169 mLayoutNeeded = true;
3170 startFreezingDisplayLocked();
3171 config = new Configuration(mTempConfiguration);
3172 }
3173 }
3174 }
3175 }
3176
Dianne Hackborncfaef692009-06-15 14:24:44 -07003177 Binder.restoreCallingIdentity(ident);
3178 return config;
3179 }
3180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003181 /*
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003182 * Determine the new desired orientation of the display, returning
3183 * a non-null new Configuration if it has changed from the current
3184 * orientation. IF TRUE IS RETURNED SOMEONE MUST CALL
3185 * setNewConfiguration() TO TELL THE WINDOW MANAGER IT CAN UNFREEZE THE
3186 * SCREEN. This will typically be done for you if you call
3187 * sendNewConfiguration().
3188 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003189 * The orientation is computed from non-application windows first. If none of
3190 * the non-application windows specify orientation, the orientation is computed from
Romain Guy06882f82009-06-10 13:36:04 -07003191 * application tokens.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003192 * @see android.view.IWindowManager#updateOrientationFromAppTokens(
3193 * android.os.IBinder)
3194 */
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003195 boolean updateOrientationFromAppTokensLocked() {
Christopher Tateb696aee2010-04-02 19:08:30 -07003196 if (mDisplayFrozen) {
3197 // If the display is frozen, some activities may be in the middle
3198 // of restarting, and thus have removed their old window. If the
3199 // window has the flag to hide the lock screen, then the lock screen
3200 // can re-appear and inflict its own orientation on us. Keep the
3201 // orientation stable until this all settles down.
3202 return false;
3203 }
3204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003205 boolean changed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003206 long ident = Binder.clearCallingIdentity();
3207 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003208 int req = computeForcedAppOrientationLocked();
Romain Guy06882f82009-06-10 13:36:04 -07003209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003210 if (req != mForcedAppOrientation) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003211 mForcedAppOrientation = req;
3212 //send a message to Policy indicating orientation change to take
3213 //action like disabling/enabling sensors etc.,
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003214 mPolicy.setCurrentOrientationLw(req);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003215 if (setRotationUncheckedLocked(WindowManagerPolicy.USE_LAST_ROTATION,
3216 mLastRotationFlags | Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE)) {
3217 changed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003218 }
3219 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003220
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003221 return changed;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003222 } finally {
3223 Binder.restoreCallingIdentity(ident);
3224 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003225 }
Romain Guy06882f82009-06-10 13:36:04 -07003226
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003227 int computeForcedAppOrientationLocked() {
3228 int req = getOrientationFromWindowsLocked();
3229 if (req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
3230 req = getOrientationFromAppTokensLocked();
3231 }
3232 return req;
3233 }
Romain Guy06882f82009-06-10 13:36:04 -07003234
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003235 public void setNewConfiguration(Configuration config) {
3236 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3237 "setNewConfiguration()")) {
3238 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
3239 }
3240
3241 synchronized(mWindowMap) {
3242 mCurConfiguration = new Configuration(config);
3243 mWaitingForConfig = false;
3244 performLayoutAndPlaceSurfacesLocked();
3245 }
3246 }
3247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003248 public void setAppOrientation(IApplicationToken token, int requestedOrientation) {
3249 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3250 "setAppOrientation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003251 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003252 }
Romain Guy06882f82009-06-10 13:36:04 -07003253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003254 synchronized(mWindowMap) {
3255 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
3256 if (wtoken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003257 Slog.w(TAG, "Attempted to set orientation of non-existing app token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003258 return;
3259 }
Romain Guy06882f82009-06-10 13:36:04 -07003260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003261 wtoken.requestedOrientation = requestedOrientation;
3262 }
3263 }
Romain Guy06882f82009-06-10 13:36:04 -07003264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003265 public int getAppOrientation(IApplicationToken token) {
3266 synchronized(mWindowMap) {
3267 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
3268 if (wtoken == null) {
3269 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
3270 }
Romain Guy06882f82009-06-10 13:36:04 -07003271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003272 return wtoken.requestedOrientation;
3273 }
3274 }
Romain Guy06882f82009-06-10 13:36:04 -07003275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003276 public void setFocusedApp(IBinder token, boolean moveFocusNow) {
3277 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3278 "setFocusedApp()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003279 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003280 }
3281
3282 synchronized(mWindowMap) {
3283 boolean changed = false;
3284 if (token == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003285 if (DEBUG_FOCUS) Slog.v(TAG, "Clearing focused app, was " + mFocusedApp);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003286 changed = mFocusedApp != null;
3287 mFocusedApp = null;
3288 mKeyWaiter.tickle();
3289 } else {
3290 AppWindowToken newFocus = findAppWindowToken(token);
3291 if (newFocus == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003292 Slog.w(TAG, "Attempted to set focus to non-existing app token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003293 return;
3294 }
3295 changed = mFocusedApp != newFocus;
3296 mFocusedApp = newFocus;
Joe Onorato8a9b2202010-02-26 18:56:32 -08003297 if (DEBUG_FOCUS) Slog.v(TAG, "Set focused app to: " + mFocusedApp);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003298 mKeyWaiter.tickle();
3299 }
3300
3301 if (moveFocusNow && changed) {
3302 final long origId = Binder.clearCallingIdentity();
3303 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3304 Binder.restoreCallingIdentity(origId);
3305 }
3306 }
3307 }
3308
3309 public void prepareAppTransition(int transit) {
3310 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3311 "prepareAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003312 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003313 }
Romain Guy06882f82009-06-10 13:36:04 -07003314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003315 synchronized(mWindowMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003316 if (DEBUG_APP_TRANSITIONS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003317 TAG, "Prepare app transition: transit=" + transit
3318 + " mNextAppTransition=" + mNextAppTransition);
Dianne Hackbornb601ce12010-03-01 23:36:02 -08003319 if (!mDisplayFrozen && mPolicy.isScreenOn()) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003320 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET
3321 || mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003322 mNextAppTransition = transit;
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07003323 } else if (transit == WindowManagerPolicy.TRANSIT_TASK_OPEN
3324 && mNextAppTransition == WindowManagerPolicy.TRANSIT_TASK_CLOSE) {
3325 // Opening a new task always supersedes a close for the anim.
3326 mNextAppTransition = transit;
3327 } else if (transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN
3328 && mNextAppTransition == WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE) {
3329 // Opening a new activity always supersedes a close for the anim.
3330 mNextAppTransition = transit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003331 }
3332 mAppTransitionReady = false;
3333 mAppTransitionTimeout = false;
3334 mStartingIconInTransition = false;
3335 mSkipAppTransitionAnimation = false;
3336 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
3337 mH.sendMessageDelayed(mH.obtainMessage(H.APP_TRANSITION_TIMEOUT),
3338 5000);
3339 }
3340 }
3341 }
3342
3343 public int getPendingAppTransition() {
3344 return mNextAppTransition;
3345 }
Romain Guy06882f82009-06-10 13:36:04 -07003346
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003347 public void overridePendingAppTransition(String packageName,
3348 int enterAnim, int exitAnim) {
Dianne Hackborn8b571a82009-09-25 16:09:43 -07003349 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003350 mNextAppTransitionPackage = packageName;
3351 mNextAppTransitionEnter = enterAnim;
3352 mNextAppTransitionExit = exitAnim;
3353 }
3354 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003356 public void executeAppTransition() {
3357 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3358 "executeAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003359 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003360 }
Romain Guy06882f82009-06-10 13:36:04 -07003361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003362 synchronized(mWindowMap) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003363 if (DEBUG_APP_TRANSITIONS) {
3364 RuntimeException e = new RuntimeException("here");
3365 e.fillInStackTrace();
Joe Onorato8a9b2202010-02-26 18:56:32 -08003366 Slog.w(TAG, "Execute app transition: mNextAppTransition="
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003367 + mNextAppTransition, e);
3368 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003369 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003370 mAppTransitionReady = true;
3371 final long origId = Binder.clearCallingIdentity();
3372 performLayoutAndPlaceSurfacesLocked();
3373 Binder.restoreCallingIdentity(origId);
3374 }
3375 }
3376 }
3377
3378 public void setAppStartingWindow(IBinder token, String pkg,
3379 int theme, CharSequence nonLocalizedLabel, int labelRes, int icon,
3380 IBinder transferFrom, boolean createIfNeeded) {
3381 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3382 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003383 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003384 }
3385
3386 synchronized(mWindowMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003387 if (DEBUG_STARTING_WINDOW) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003388 TAG, "setAppStartingIcon: token=" + token + " pkg=" + pkg
3389 + " transferFrom=" + transferFrom);
Romain Guy06882f82009-06-10 13:36:04 -07003390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003391 AppWindowToken wtoken = findAppWindowToken(token);
3392 if (wtoken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003393 Slog.w(TAG, "Attempted to set icon of non-existing app token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003394 return;
3395 }
3396
3397 // If the display is frozen, we won't do anything until the
3398 // actual window is displayed so there is no reason to put in
3399 // the starting window.
Dianne Hackbornde2606d2009-12-18 16:53:55 -08003400 if (mDisplayFrozen || !mPolicy.isScreenOn()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003401 return;
3402 }
Romain Guy06882f82009-06-10 13:36:04 -07003403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003404 if (wtoken.startingData != null) {
3405 return;
3406 }
Romain Guy06882f82009-06-10 13:36:04 -07003407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003408 if (transferFrom != null) {
3409 AppWindowToken ttoken = findAppWindowToken(transferFrom);
3410 if (ttoken != null) {
3411 WindowState startingWindow = ttoken.startingWindow;
3412 if (startingWindow != null) {
3413 if (mStartingIconInTransition) {
3414 // In this case, the starting icon has already
3415 // been displayed, so start letting windows get
3416 // shown immediately without any more transitions.
3417 mSkipAppTransitionAnimation = true;
3418 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08003419 if (DEBUG_STARTING_WINDOW) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003420 "Moving existing starting from " + ttoken
3421 + " to " + wtoken);
3422 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07003423
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003424 // Transfer the starting window over to the new
3425 // token.
3426 wtoken.startingData = ttoken.startingData;
3427 wtoken.startingView = ttoken.startingView;
3428 wtoken.startingWindow = startingWindow;
3429 ttoken.startingData = null;
3430 ttoken.startingView = null;
3431 ttoken.startingWindow = null;
3432 ttoken.startingMoved = true;
3433 startingWindow.mToken = wtoken;
Dianne Hackbornef49c572009-03-24 19:27:32 -07003434 startingWindow.mRootToken = wtoken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003435 startingWindow.mAppToken = wtoken;
Joe Onorato8a9b2202010-02-26 18:56:32 -08003436 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG,
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003437 "Removing starting window: " + startingWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003438 mWindows.remove(startingWindow);
3439 ttoken.windows.remove(startingWindow);
3440 ttoken.allAppWindows.remove(startingWindow);
3441 addWindowToListInOrderLocked(startingWindow, true);
Romain Guy06882f82009-06-10 13:36:04 -07003442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003443 // Propagate other interesting state between the
3444 // tokens. If the old token is displayed, we should
3445 // immediately force the new one to be displayed. If
3446 // it is animating, we need to move that animation to
3447 // the new one.
3448 if (ttoken.allDrawn) {
3449 wtoken.allDrawn = true;
3450 }
3451 if (ttoken.firstWindowDrawn) {
3452 wtoken.firstWindowDrawn = true;
3453 }
3454 if (!ttoken.hidden) {
3455 wtoken.hidden = false;
3456 wtoken.hiddenRequested = false;
3457 wtoken.willBeHidden = false;
3458 }
3459 if (wtoken.clientHidden != ttoken.clientHidden) {
3460 wtoken.clientHidden = ttoken.clientHidden;
3461 wtoken.sendAppVisibilityToClients();
3462 }
3463 if (ttoken.animation != null) {
3464 wtoken.animation = ttoken.animation;
3465 wtoken.animating = ttoken.animating;
3466 wtoken.animLayerAdjustment = ttoken.animLayerAdjustment;
3467 ttoken.animation = null;
3468 ttoken.animLayerAdjustment = 0;
3469 wtoken.updateLayers();
3470 ttoken.updateLayers();
3471 }
Romain Guy06882f82009-06-10 13:36:04 -07003472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003473 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003474 mLayoutNeeded = true;
3475 performLayoutAndPlaceSurfacesLocked();
3476 Binder.restoreCallingIdentity(origId);
3477 return;
3478 } else if (ttoken.startingData != null) {
3479 // The previous app was getting ready to show a
3480 // starting window, but hasn't yet done so. Steal it!
Joe Onorato8a9b2202010-02-26 18:56:32 -08003481 if (DEBUG_STARTING_WINDOW) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003482 "Moving pending starting from " + ttoken
3483 + " to " + wtoken);
3484 wtoken.startingData = ttoken.startingData;
3485 ttoken.startingData = null;
3486 ttoken.startingMoved = true;
3487 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3488 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3489 // want to process the message ASAP, before any other queued
3490 // messages.
3491 mH.sendMessageAtFrontOfQueue(m);
3492 return;
3493 }
3494 }
3495 }
3496
3497 // There is no existing starting window, and the caller doesn't
3498 // want us to create one, so that's it!
3499 if (!createIfNeeded) {
3500 return;
3501 }
Romain Guy06882f82009-06-10 13:36:04 -07003502
Dianne Hackborn284ac932009-08-28 10:34:25 -07003503 // If this is a translucent or wallpaper window, then don't
3504 // show a starting window -- the current effect (a full-screen
3505 // opaque starting window that fades away to the real contents
3506 // when it is ready) does not work for this.
3507 if (theme != 0) {
3508 AttributeCache.Entry ent = AttributeCache.instance().get(pkg, theme,
3509 com.android.internal.R.styleable.Window);
3510 if (ent.array.getBoolean(
3511 com.android.internal.R.styleable.Window_windowIsTranslucent, false)) {
3512 return;
3513 }
3514 if (ent.array.getBoolean(
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07003515 com.android.internal.R.styleable.Window_windowIsFloating, false)) {
3516 return;
3517 }
3518 if (ent.array.getBoolean(
Dianne Hackborn284ac932009-08-28 10:34:25 -07003519 com.android.internal.R.styleable.Window_windowShowWallpaper, false)) {
3520 return;
3521 }
3522 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003523
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003524 mStartingIconInTransition = true;
3525 wtoken.startingData = new StartingData(
3526 pkg, theme, nonLocalizedLabel,
3527 labelRes, icon);
3528 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3529 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3530 // want to process the message ASAP, before any other queued
3531 // messages.
3532 mH.sendMessageAtFrontOfQueue(m);
3533 }
3534 }
3535
3536 public void setAppWillBeHidden(IBinder token) {
3537 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3538 "setAppWillBeHidden()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003539 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003540 }
3541
3542 AppWindowToken wtoken;
3543
3544 synchronized(mWindowMap) {
3545 wtoken = findAppWindowToken(token);
3546 if (wtoken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003547 Slog.w(TAG, "Attempted to set will be hidden of non-existing app token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003548 return;
3549 }
3550 wtoken.willBeHidden = true;
3551 }
3552 }
Romain Guy06882f82009-06-10 13:36:04 -07003553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003554 boolean setTokenVisibilityLocked(AppWindowToken wtoken, WindowManager.LayoutParams lp,
3555 boolean visible, int transit, boolean performLayout) {
3556 boolean delayed = false;
3557
3558 if (wtoken.clientHidden == visible) {
3559 wtoken.clientHidden = !visible;
3560 wtoken.sendAppVisibilityToClients();
3561 }
Romain Guy06882f82009-06-10 13:36:04 -07003562
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003563 wtoken.willBeHidden = false;
3564 if (wtoken.hidden == visible) {
3565 final int N = wtoken.allAppWindows.size();
3566 boolean changed = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -08003567 if (DEBUG_APP_TRANSITIONS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003568 TAG, "Changing app " + wtoken + " hidden=" + wtoken.hidden
3569 + " performLayout=" + performLayout);
Romain Guy06882f82009-06-10 13:36:04 -07003570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003571 boolean runningAppAnimation = false;
Romain Guy06882f82009-06-10 13:36:04 -07003572
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003573 if (transit != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003574 if (wtoken.animation == sDummyAnimation) {
3575 wtoken.animation = null;
3576 }
3577 applyAnimationLocked(wtoken, lp, transit, visible);
3578 changed = true;
3579 if (wtoken.animation != null) {
3580 delayed = runningAppAnimation = true;
3581 }
3582 }
Romain Guy06882f82009-06-10 13:36:04 -07003583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003584 for (int i=0; i<N; i++) {
3585 WindowState win = wtoken.allAppWindows.get(i);
3586 if (win == wtoken.startingWindow) {
3587 continue;
3588 }
3589
3590 if (win.isAnimating()) {
3591 delayed = true;
3592 }
Romain Guy06882f82009-06-10 13:36:04 -07003593
Joe Onorato8a9b2202010-02-26 18:56:32 -08003594 //Slog.i(TAG, "Window " + win + ": vis=" + win.isVisible());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003595 //win.dump(" ");
3596 if (visible) {
3597 if (!win.isVisibleNow()) {
3598 if (!runningAppAnimation) {
3599 applyAnimationLocked(win,
3600 WindowManagerPolicy.TRANSIT_ENTER, true);
3601 }
3602 changed = true;
3603 }
3604 } else if (win.isVisibleNow()) {
3605 if (!runningAppAnimation) {
3606 applyAnimationLocked(win,
3607 WindowManagerPolicy.TRANSIT_EXIT, false);
3608 }
3609 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
3610 KeyWaiter.RETURN_NOTHING);
3611 changed = true;
3612 }
3613 }
3614
3615 wtoken.hidden = wtoken.hiddenRequested = !visible;
3616 if (!visible) {
3617 unsetAppFreezingScreenLocked(wtoken, true, true);
3618 } else {
3619 // If we are being set visible, and the starting window is
3620 // not yet displayed, then make sure it doesn't get displayed.
3621 WindowState swin = wtoken.startingWindow;
3622 if (swin != null && (swin.mDrawPending
3623 || swin.mCommitDrawPending)) {
3624 swin.mPolicyVisibility = false;
3625 swin.mPolicyVisibilityAfterAnim = false;
3626 }
3627 }
Romain Guy06882f82009-06-10 13:36:04 -07003628
Joe Onorato8a9b2202010-02-26 18:56:32 -08003629 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG, "setTokenVisibilityLocked: " + wtoken
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003630 + ": hidden=" + wtoken.hidden + " hiddenRequested="
3631 + wtoken.hiddenRequested);
Romain Guy06882f82009-06-10 13:36:04 -07003632
Dianne Hackborn9b52a212009-12-11 14:51:35 -08003633 if (changed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003634 mLayoutNeeded = true;
Dianne Hackborn9b52a212009-12-11 14:51:35 -08003635 if (performLayout) {
3636 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
3637 performLayoutAndPlaceSurfacesLocked();
3638 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003639 }
3640 }
3641
3642 if (wtoken.animation != null) {
3643 delayed = true;
3644 }
Romain Guy06882f82009-06-10 13:36:04 -07003645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003646 return delayed;
3647 }
3648
3649 public void setAppVisibility(IBinder token, boolean visible) {
3650 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3651 "setAppVisibility()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003652 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003653 }
3654
3655 AppWindowToken wtoken;
3656
3657 synchronized(mWindowMap) {
3658 wtoken = findAppWindowToken(token);
3659 if (wtoken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003660 Slog.w(TAG, "Attempted to set visibility of non-existing app token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003661 return;
3662 }
3663
3664 if (DEBUG_APP_TRANSITIONS || DEBUG_ORIENTATION) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08003665 RuntimeException e = null;
3666 if (!HIDE_STACK_CRAWLS) {
3667 e = new RuntimeException();
3668 e.fillInStackTrace();
3669 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08003670 Slog.v(TAG, "setAppVisibility(" + token + ", " + visible
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003671 + "): mNextAppTransition=" + mNextAppTransition
3672 + " hidden=" + wtoken.hidden
3673 + " hiddenRequested=" + wtoken.hiddenRequested, e);
3674 }
Romain Guy06882f82009-06-10 13:36:04 -07003675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003676 // If we are preparing an app transition, then delay changing
3677 // the visibility of this token until we execute that transition.
Dianne Hackbornb601ce12010-03-01 23:36:02 -08003678 if (!mDisplayFrozen && mPolicy.isScreenOn()
3679 && mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003680 // Already in requested state, don't do anything more.
3681 if (wtoken.hiddenRequested != visible) {
3682 return;
3683 }
3684 wtoken.hiddenRequested = !visible;
Romain Guy06882f82009-06-10 13:36:04 -07003685
Joe Onorato8a9b2202010-02-26 18:56:32 -08003686 if (DEBUG_APP_TRANSITIONS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003687 TAG, "Setting dummy animation on: " + wtoken);
3688 wtoken.setDummyAnimation();
3689 mOpeningApps.remove(wtoken);
3690 mClosingApps.remove(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003691 wtoken.waitingToShow = wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003692 wtoken.inPendingTransaction = true;
3693 if (visible) {
3694 mOpeningApps.add(wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003695 wtoken.startingDisplayed = false;
3696 wtoken.startingMoved = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003697
Dianne Hackborn195f6a02009-11-24 11:26:00 -08003698 // If the token is currently hidden (should be the
3699 // common case), then we need to set up to wait for
3700 // its windows to be ready.
3701 if (wtoken.hidden) {
3702 wtoken.allDrawn = false;
3703 wtoken.waitingToShow = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003704
Dianne Hackborn195f6a02009-11-24 11:26:00 -08003705 if (wtoken.clientHidden) {
3706 // In the case where we are making an app visible
3707 // but holding off for a transition, we still need
3708 // to tell the client to make its windows visible so
3709 // they get drawn. Otherwise, we will wait on
3710 // performing the transition until all windows have
3711 // been drawn, they never will be, and we are sad.
3712 wtoken.clientHidden = false;
3713 wtoken.sendAppVisibilityToClients();
3714 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003715 }
3716 } else {
3717 mClosingApps.add(wtoken);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003718
Dianne Hackborn195f6a02009-11-24 11:26:00 -08003719 // If the token is currently visible (should be the
3720 // common case), then set up to wait for it to be hidden.
3721 if (!wtoken.hidden) {
3722 wtoken.waitingToHide = true;
3723 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003724 }
3725 return;
3726 }
Romain Guy06882f82009-06-10 13:36:04 -07003727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003728 final long origId = Binder.clearCallingIdentity();
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003729 setTokenVisibilityLocked(wtoken, null, visible, WindowManagerPolicy.TRANSIT_UNSET, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003730 wtoken.updateReportedVisibilityLocked();
3731 Binder.restoreCallingIdentity(origId);
3732 }
3733 }
3734
3735 void unsetAppFreezingScreenLocked(AppWindowToken wtoken,
3736 boolean unfreezeSurfaceNow, boolean force) {
3737 if (wtoken.freezingScreen) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003738 if (DEBUG_ORIENTATION) Slog.v(TAG, "Clear freezing of " + wtoken
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003739 + " force=" + force);
3740 final int N = wtoken.allAppWindows.size();
3741 boolean unfrozeWindows = false;
3742 for (int i=0; i<N; i++) {
3743 WindowState w = wtoken.allAppWindows.get(i);
3744 if (w.mAppFreezing) {
3745 w.mAppFreezing = false;
3746 if (w.mSurface != null && !w.mOrientationChanging) {
3747 w.mOrientationChanging = true;
3748 }
3749 unfrozeWindows = true;
3750 }
3751 }
3752 if (force || unfrozeWindows) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003753 if (DEBUG_ORIENTATION) Slog.v(TAG, "No longer freezing: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003754 wtoken.freezingScreen = false;
3755 mAppsFreezingScreen--;
3756 }
3757 if (unfreezeSurfaceNow) {
3758 if (unfrozeWindows) {
3759 mLayoutNeeded = true;
3760 performLayoutAndPlaceSurfacesLocked();
3761 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003762 stopFreezingDisplayLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003763 }
3764 }
3765 }
Romain Guy06882f82009-06-10 13:36:04 -07003766
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003767 public void startAppFreezingScreenLocked(AppWindowToken wtoken,
3768 int configChanges) {
3769 if (DEBUG_ORIENTATION) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08003770 RuntimeException e = null;
3771 if (!HIDE_STACK_CRAWLS) {
3772 e = new RuntimeException();
3773 e.fillInStackTrace();
3774 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08003775 Slog.i(TAG, "Set freezing of " + wtoken.appToken
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003776 + ": hidden=" + wtoken.hidden + " freezing="
3777 + wtoken.freezingScreen, e);
3778 }
3779 if (!wtoken.hiddenRequested) {
3780 if (!wtoken.freezingScreen) {
3781 wtoken.freezingScreen = true;
3782 mAppsFreezingScreen++;
3783 if (mAppsFreezingScreen == 1) {
3784 startFreezingDisplayLocked();
3785 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
3786 mH.sendMessageDelayed(mH.obtainMessage(H.APP_FREEZE_TIMEOUT),
3787 5000);
3788 }
3789 }
3790 final int N = wtoken.allAppWindows.size();
3791 for (int i=0; i<N; i++) {
3792 WindowState w = wtoken.allAppWindows.get(i);
3793 w.mAppFreezing = true;
3794 }
3795 }
3796 }
Romain Guy06882f82009-06-10 13:36:04 -07003797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003798 public void startAppFreezingScreen(IBinder token, int configChanges) {
3799 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3800 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003801 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003802 }
3803
3804 synchronized(mWindowMap) {
Dianne Hackbornb601ce12010-03-01 23:36:02 -08003805 if (configChanges == 0 && !mDisplayFrozen && mPolicy.isScreenOn()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003806 if (DEBUG_ORIENTATION) Slog.v(TAG, "Skipping set freeze of " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003807 return;
3808 }
Romain Guy06882f82009-06-10 13:36:04 -07003809
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003810 AppWindowToken wtoken = findAppWindowToken(token);
3811 if (wtoken == null || wtoken.appToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003812 Slog.w(TAG, "Attempted to freeze screen with non-existing app token: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003813 return;
3814 }
3815 final long origId = Binder.clearCallingIdentity();
3816 startAppFreezingScreenLocked(wtoken, configChanges);
3817 Binder.restoreCallingIdentity(origId);
3818 }
3819 }
Romain Guy06882f82009-06-10 13:36:04 -07003820
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003821 public void stopAppFreezingScreen(IBinder token, boolean force) {
3822 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3823 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003824 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003825 }
3826
3827 synchronized(mWindowMap) {
3828 AppWindowToken wtoken = findAppWindowToken(token);
3829 if (wtoken == null || wtoken.appToken == null) {
3830 return;
3831 }
3832 final long origId = Binder.clearCallingIdentity();
Joe Onorato8a9b2202010-02-26 18:56:32 -08003833 if (DEBUG_ORIENTATION) Slog.v(TAG, "Clear freezing of " + token
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003834 + ": hidden=" + wtoken.hidden + " freezing=" + wtoken.freezingScreen);
3835 unsetAppFreezingScreenLocked(wtoken, true, force);
3836 Binder.restoreCallingIdentity(origId);
3837 }
3838 }
Romain Guy06882f82009-06-10 13:36:04 -07003839
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003840 public void removeAppToken(IBinder token) {
3841 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3842 "removeAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003843 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003844 }
3845
3846 AppWindowToken wtoken = null;
3847 AppWindowToken startingToken = null;
3848 boolean delayed = false;
3849
3850 final long origId = Binder.clearCallingIdentity();
3851 synchronized(mWindowMap) {
3852 WindowToken basewtoken = mTokenMap.remove(token);
3853 mTokenList.remove(basewtoken);
3854 if (basewtoken != null && (wtoken=basewtoken.appWindowToken) != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003855 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG, "Removing app token: " + wtoken);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003856 delayed = setTokenVisibilityLocked(wtoken, null, false, WindowManagerPolicy.TRANSIT_UNSET, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003857 wtoken.inPendingTransaction = false;
3858 mOpeningApps.remove(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003859 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003860 if (mClosingApps.contains(wtoken)) {
3861 delayed = true;
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003862 } else if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003863 mClosingApps.add(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003864 wtoken.waitingToHide = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003865 delayed = true;
3866 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08003867 if (DEBUG_APP_TRANSITIONS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003868 TAG, "Removing app " + wtoken + " delayed=" + delayed
3869 + " animation=" + wtoken.animation
3870 + " animating=" + wtoken.animating);
3871 if (delayed) {
3872 // set the token aside because it has an active animation to be finished
3873 mExitingAppTokens.add(wtoken);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003874 } else {
3875 // Make sure there is no animation running on this token,
3876 // so any windows associated with it will be removed as
3877 // soon as their animations are complete
3878 wtoken.animation = null;
3879 wtoken.animating = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003880 }
3881 mAppTokens.remove(wtoken);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003882 if (mLastEnterAnimToken == wtoken) {
3883 mLastEnterAnimToken = null;
3884 mLastEnterAnimParams = null;
3885 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003886 wtoken.removed = true;
3887 if (wtoken.startingData != null) {
3888 startingToken = wtoken;
3889 }
3890 unsetAppFreezingScreenLocked(wtoken, true, true);
3891 if (mFocusedApp == wtoken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003892 if (DEBUG_FOCUS) Slog.v(TAG, "Removing focused app token:" + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003893 mFocusedApp = null;
3894 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3895 mKeyWaiter.tickle();
3896 }
3897 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003898 Slog.w(TAG, "Attempted to remove non-existing app token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003899 }
Romain Guy06882f82009-06-10 13:36:04 -07003900
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003901 if (!delayed && wtoken != null) {
3902 wtoken.updateReportedVisibilityLocked();
3903 }
3904 }
3905 Binder.restoreCallingIdentity(origId);
3906
3907 if (startingToken != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003908 if (DEBUG_STARTING_WINDOW) Slog.v(TAG, "Schedule remove starting "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003909 + startingToken + ": app token removed");
3910 Message m = mH.obtainMessage(H.REMOVE_STARTING, startingToken);
3911 mH.sendMessage(m);
3912 }
3913 }
3914
3915 private boolean tmpRemoveAppWindowsLocked(WindowToken token) {
3916 final int NW = token.windows.size();
3917 for (int i=0; i<NW; i++) {
3918 WindowState win = token.windows.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003919 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "Tmp removing app window " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003920 mWindows.remove(win);
3921 int j = win.mChildWindows.size();
3922 while (j > 0) {
3923 j--;
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003924 WindowState cwin = (WindowState)win.mChildWindows.get(j);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003925 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG,
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003926 "Tmp removing child window " + cwin);
3927 mWindows.remove(cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003928 }
3929 }
3930 return NW > 0;
3931 }
3932
3933 void dumpAppTokensLocked() {
3934 for (int i=mAppTokens.size()-1; i>=0; i--) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003935 Slog.v(TAG, " #" + i + ": " + mAppTokens.get(i).token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003936 }
3937 }
Romain Guy06882f82009-06-10 13:36:04 -07003938
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003939 void dumpWindowsLocked() {
3940 for (int i=mWindows.size()-1; i>=0; i--) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003941 Slog.v(TAG, " #" + i + ": " + mWindows.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003942 }
3943 }
Romain Guy06882f82009-06-10 13:36:04 -07003944
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003945 private int findWindowOffsetLocked(int tokenPos) {
3946 final int NW = mWindows.size();
3947
3948 if (tokenPos >= mAppTokens.size()) {
3949 int i = NW;
3950 while (i > 0) {
3951 i--;
3952 WindowState win = (WindowState)mWindows.get(i);
3953 if (win.getAppToken() != null) {
3954 return i+1;
3955 }
3956 }
3957 }
3958
3959 while (tokenPos > 0) {
3960 // Find the first app token below the new position that has
3961 // a window displayed.
3962 final AppWindowToken wtoken = mAppTokens.get(tokenPos-1);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003963 if (DEBUG_REORDER) Slog.v(TAG, "Looking for lower windows @ "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003964 + tokenPos + " -- " + wtoken.token);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003965 if (wtoken.sendingToBottom) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003966 if (DEBUG_REORDER) Slog.v(TAG,
Dianne Hackborna8f60182009-09-01 19:01:50 -07003967 "Skipping token -- currently sending to bottom");
3968 tokenPos--;
3969 continue;
3970 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003971 int i = wtoken.windows.size();
3972 while (i > 0) {
3973 i--;
3974 WindowState win = wtoken.windows.get(i);
3975 int j = win.mChildWindows.size();
3976 while (j > 0) {
3977 j--;
3978 WindowState cwin = (WindowState)win.mChildWindows.get(j);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003979 if (cwin.mSubLayer >= 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003980 for (int pos=NW-1; pos>=0; pos--) {
3981 if (mWindows.get(pos) == cwin) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003982 if (DEBUG_REORDER) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003983 "Found child win @" + (pos+1));
3984 return pos+1;
3985 }
3986 }
3987 }
3988 }
3989 for (int pos=NW-1; pos>=0; pos--) {
3990 if (mWindows.get(pos) == win) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003991 if (DEBUG_REORDER) Slog.v(TAG, "Found win @" + (pos+1));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003992 return pos+1;
3993 }
3994 }
3995 }
3996 tokenPos--;
3997 }
3998
3999 return 0;
4000 }
4001
4002 private final int reAddWindowLocked(int index, WindowState win) {
4003 final int NCW = win.mChildWindows.size();
4004 boolean added = false;
4005 for (int j=0; j<NCW; j++) {
4006 WindowState cwin = (WindowState)win.mChildWindows.get(j);
4007 if (!added && cwin.mSubLayer >= 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004008 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "Re-adding child window at "
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07004009 + index + ": " + cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004010 mWindows.add(index, win);
4011 index++;
4012 added = true;
4013 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08004014 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "Re-adding window at "
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07004015 + index + ": " + cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004016 mWindows.add(index, cwin);
4017 index++;
4018 }
4019 if (!added) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004020 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "Re-adding window at "
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07004021 + index + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004022 mWindows.add(index, win);
4023 index++;
4024 }
4025 return index;
4026 }
Romain Guy06882f82009-06-10 13:36:04 -07004027
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004028 private final int reAddAppWindowsLocked(int index, WindowToken token) {
4029 final int NW = token.windows.size();
4030 for (int i=0; i<NW; i++) {
4031 index = reAddWindowLocked(index, token.windows.get(i));
4032 }
4033 return index;
4034 }
4035
4036 public void moveAppToken(int index, IBinder token) {
4037 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4038 "moveAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004039 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004040 }
4041
4042 synchronized(mWindowMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004043 if (DEBUG_REORDER) Slog.v(TAG, "Initial app tokens:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004044 if (DEBUG_REORDER) dumpAppTokensLocked();
4045 final AppWindowToken wtoken = findAppWindowToken(token);
4046 if (wtoken == null || !mAppTokens.remove(wtoken)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004047 Slog.w(TAG, "Attempting to reorder token that doesn't exist: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004048 + token + " (" + wtoken + ")");
4049 return;
4050 }
4051 mAppTokens.add(index, wtoken);
Joe Onorato8a9b2202010-02-26 18:56:32 -08004052 if (DEBUG_REORDER) Slog.v(TAG, "Moved " + token + " to " + index + ":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004053 if (DEBUG_REORDER) dumpAppTokensLocked();
Romain Guy06882f82009-06-10 13:36:04 -07004054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004055 final long origId = Binder.clearCallingIdentity();
Joe Onorato8a9b2202010-02-26 18:56:32 -08004056 if (DEBUG_REORDER) Slog.v(TAG, "Removing windows in " + token + ":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004057 if (DEBUG_REORDER) dumpWindowsLocked();
4058 if (tmpRemoveAppWindowsLocked(wtoken)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004059 if (DEBUG_REORDER) Slog.v(TAG, "Adding windows back in:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004060 if (DEBUG_REORDER) dumpWindowsLocked();
4061 reAddAppWindowsLocked(findWindowOffsetLocked(index), wtoken);
Joe Onorato8a9b2202010-02-26 18:56:32 -08004062 if (DEBUG_REORDER) Slog.v(TAG, "Final window list:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004063 if (DEBUG_REORDER) dumpWindowsLocked();
4064 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004065 mLayoutNeeded = true;
4066 performLayoutAndPlaceSurfacesLocked();
4067 }
4068 Binder.restoreCallingIdentity(origId);
4069 }
4070 }
4071
4072 private void removeAppTokensLocked(List<IBinder> tokens) {
4073 // XXX This should be done more efficiently!
4074 // (take advantage of the fact that both lists should be
4075 // ordered in the same way.)
4076 int N = tokens.size();
4077 for (int i=0; i<N; i++) {
4078 IBinder token = tokens.get(i);
4079 final AppWindowToken wtoken = findAppWindowToken(token);
4080 if (!mAppTokens.remove(wtoken)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004081 Slog.w(TAG, "Attempting to reorder token that doesn't exist: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004082 + token + " (" + wtoken + ")");
4083 i--;
4084 N--;
4085 }
4086 }
4087 }
4088
Dianne Hackborna8f60182009-09-01 19:01:50 -07004089 private void moveAppWindowsLocked(AppWindowToken wtoken, int tokenPos,
4090 boolean updateFocusAndLayout) {
4091 // First remove all of the windows from the list.
4092 tmpRemoveAppWindowsLocked(wtoken);
4093
4094 // Where to start adding?
4095 int pos = findWindowOffsetLocked(tokenPos);
4096
4097 // And now add them back at the correct place.
4098 pos = reAddAppWindowsLocked(pos, wtoken);
4099
4100 if (updateFocusAndLayout) {
4101 if (!updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
4102 assignLayersLocked();
4103 }
4104 mLayoutNeeded = true;
4105 performLayoutAndPlaceSurfacesLocked();
4106 }
4107 }
4108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004109 private void moveAppWindowsLocked(List<IBinder> tokens, int tokenPos) {
4110 // First remove all of the windows from the list.
4111 final int N = tokens.size();
4112 int i;
4113 for (i=0; i<N; i++) {
4114 WindowToken token = mTokenMap.get(tokens.get(i));
4115 if (token != null) {
4116 tmpRemoveAppWindowsLocked(token);
4117 }
4118 }
4119
4120 // Where to start adding?
4121 int pos = findWindowOffsetLocked(tokenPos);
4122
4123 // And now add them back at the correct place.
4124 for (i=0; i<N; i++) {
4125 WindowToken token = mTokenMap.get(tokens.get(i));
4126 if (token != null) {
4127 pos = reAddAppWindowsLocked(pos, token);
4128 }
4129 }
4130
Dianne Hackborna8f60182009-09-01 19:01:50 -07004131 if (!updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
4132 assignLayersLocked();
4133 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004134 mLayoutNeeded = true;
4135 performLayoutAndPlaceSurfacesLocked();
4136
4137 //dump();
4138 }
4139
4140 public void moveAppTokensToTop(List<IBinder> tokens) {
4141 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4142 "moveAppTokensToTop()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004143 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004144 }
4145
4146 final long origId = Binder.clearCallingIdentity();
4147 synchronized(mWindowMap) {
4148 removeAppTokensLocked(tokens);
4149 final int N = tokens.size();
4150 for (int i=0; i<N; i++) {
4151 AppWindowToken wt = findAppWindowToken(tokens.get(i));
4152 if (wt != null) {
4153 mAppTokens.add(wt);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004154 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004155 mToTopApps.remove(wt);
4156 mToBottomApps.remove(wt);
4157 mToTopApps.add(wt);
4158 wt.sendingToBottom = false;
4159 wt.sendingToTop = true;
4160 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004161 }
4162 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004163
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004164 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004165 moveAppWindowsLocked(tokens, mAppTokens.size());
4166 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004167 }
4168 Binder.restoreCallingIdentity(origId);
4169 }
4170
4171 public void moveAppTokensToBottom(List<IBinder> tokens) {
4172 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4173 "moveAppTokensToBottom()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004174 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004175 }
4176
4177 final long origId = Binder.clearCallingIdentity();
4178 synchronized(mWindowMap) {
4179 removeAppTokensLocked(tokens);
4180 final int N = tokens.size();
4181 int pos = 0;
4182 for (int i=0; i<N; i++) {
4183 AppWindowToken wt = findAppWindowToken(tokens.get(i));
4184 if (wt != null) {
4185 mAppTokens.add(pos, wt);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004186 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004187 mToTopApps.remove(wt);
4188 mToBottomApps.remove(wt);
4189 mToBottomApps.add(i, wt);
4190 wt.sendingToTop = false;
4191 wt.sendingToBottom = true;
4192 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004193 pos++;
4194 }
4195 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004196
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004197 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004198 moveAppWindowsLocked(tokens, 0);
4199 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004200 }
4201 Binder.restoreCallingIdentity(origId);
4202 }
4203
4204 // -------------------------------------------------------------
4205 // Misc IWindowSession methods
4206 // -------------------------------------------------------------
Romain Guy06882f82009-06-10 13:36:04 -07004207
Jim Miller284b62e2010-06-08 14:27:42 -07004208 private boolean shouldAllowDisableKeyguard()
Jim Millerd6b57052010-06-07 17:52:42 -07004209 {
Jim Miller284b62e2010-06-08 14:27:42 -07004210 // We fail safe and prevent disabling keyguard in the unlikely event this gets
4211 // called before DevicePolicyManagerService has started.
4212 if (mAllowDisableKeyguard == ALLOW_DISABLE_UNKNOWN) {
4213 DevicePolicyManager dpm = (DevicePolicyManager) mContext.getSystemService(
4214 Context.DEVICE_POLICY_SERVICE);
4215 if (dpm != null) {
4216 mAllowDisableKeyguard = dpm.getPasswordQuality(null)
4217 == DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED ?
4218 ALLOW_DISABLE_YES : ALLOW_DISABLE_NO;
4219 }
Jim Millerd6b57052010-06-07 17:52:42 -07004220 }
Jim Miller284b62e2010-06-08 14:27:42 -07004221 return mAllowDisableKeyguard == ALLOW_DISABLE_YES;
Jim Millerd6b57052010-06-07 17:52:42 -07004222 }
4223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004224 public void disableKeyguard(IBinder token, String tag) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04004225 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004226 != PackageManager.PERMISSION_GRANTED) {
4227 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
4228 }
Jim Millerd6b57052010-06-07 17:52:42 -07004229
Jim Miller284b62e2010-06-08 14:27:42 -07004230 synchronized (mKeyguardTokenWatcher) {
4231 mKeyguardTokenWatcher.acquire(token, tag);
Mike Lockwooddd884682009-10-11 16:57:08 -04004232 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004233 }
4234
4235 public void reenableKeyguard(IBinder token) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04004236 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004237 != PackageManager.PERMISSION_GRANTED) {
4238 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
4239 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004240
Jim Miller284b62e2010-06-08 14:27:42 -07004241 synchronized (mKeyguardTokenWatcher) {
4242 mKeyguardTokenWatcher.release(token);
Jim Millerd6b57052010-06-07 17:52:42 -07004243
Jim Miller284b62e2010-06-08 14:27:42 -07004244 if (!mKeyguardTokenWatcher.isAcquired()) {
4245 // If we are the last one to reenable the keyguard wait until
4246 // we have actually finished reenabling until returning.
4247 // It is possible that reenableKeyguard() can be called before
4248 // the previous disableKeyguard() is handled, in which case
4249 // neither mKeyguardTokenWatcher.acquired() or released() would
4250 // be called. In that case mKeyguardDisabled will be false here
4251 // and we have nothing to wait for.
4252 while (mKeyguardDisabled) {
4253 try {
4254 mKeyguardTokenWatcher.wait();
4255 } catch (InterruptedException e) {
4256 Thread.currentThread().interrupt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004257 }
4258 }
4259 }
4260 }
4261 }
4262
4263 /**
4264 * @see android.app.KeyguardManager#exitKeyguardSecurely
4265 */
4266 public void exitKeyguardSecurely(final IOnKeyguardExitResult callback) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04004267 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004268 != PackageManager.PERMISSION_GRANTED) {
4269 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
4270 }
4271 mPolicy.exitKeyguardSecurely(new WindowManagerPolicy.OnKeyguardExitResult() {
4272 public void onKeyguardExitResult(boolean success) {
4273 try {
4274 callback.onKeyguardExitResult(success);
4275 } catch (RemoteException e) {
4276 // Client has died, we don't care.
4277 }
4278 }
4279 });
4280 }
4281
4282 public boolean inKeyguardRestrictedInputMode() {
4283 return mPolicy.inKeyguardRestrictedKeyInputMode();
4284 }
Romain Guy06882f82009-06-10 13:36:04 -07004285
Dianne Hackbornffa42482009-09-23 22:20:11 -07004286 public void closeSystemDialogs(String reason) {
4287 synchronized(mWindowMap) {
4288 for (int i=mWindows.size()-1; i>=0; i--) {
4289 WindowState w = (WindowState)mWindows.get(i);
4290 if (w.mSurface != null) {
4291 try {
4292 w.mClient.closeSystemDialogs(reason);
4293 } catch (RemoteException e) {
4294 }
4295 }
4296 }
4297 }
4298 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004300 static float fixScale(float scale) {
4301 if (scale < 0) scale = 0;
4302 else if (scale > 20) scale = 20;
4303 return Math.abs(scale);
4304 }
Romain Guy06882f82009-06-10 13:36:04 -07004305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004306 public void setAnimationScale(int which, float scale) {
4307 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
4308 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004309 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004310 }
4311
4312 if (scale < 0) scale = 0;
4313 else if (scale > 20) scale = 20;
4314 scale = Math.abs(scale);
4315 switch (which) {
4316 case 0: mWindowAnimationScale = fixScale(scale); break;
4317 case 1: mTransitionAnimationScale = fixScale(scale); break;
4318 }
Romain Guy06882f82009-06-10 13:36:04 -07004319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004320 // Persist setting
4321 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
4322 }
Romain Guy06882f82009-06-10 13:36:04 -07004323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004324 public void setAnimationScales(float[] scales) {
4325 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
4326 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004327 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004328 }
4329
4330 if (scales != null) {
4331 if (scales.length >= 1) {
4332 mWindowAnimationScale = fixScale(scales[0]);
4333 }
4334 if (scales.length >= 2) {
4335 mTransitionAnimationScale = fixScale(scales[1]);
4336 }
4337 }
Romain Guy06882f82009-06-10 13:36:04 -07004338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004339 // Persist setting
4340 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
4341 }
Romain Guy06882f82009-06-10 13:36:04 -07004342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004343 public float getAnimationScale(int which) {
4344 switch (which) {
4345 case 0: return mWindowAnimationScale;
4346 case 1: return mTransitionAnimationScale;
4347 }
4348 return 0;
4349 }
Romain Guy06882f82009-06-10 13:36:04 -07004350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004351 public float[] getAnimationScales() {
4352 return new float[] { mWindowAnimationScale, mTransitionAnimationScale };
4353 }
Romain Guy06882f82009-06-10 13:36:04 -07004354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004355 public int getSwitchState(int sw) {
4356 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4357 "getSwitchState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004358 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004359 }
4360 return KeyInputQueue.getSwitchState(sw);
4361 }
Romain Guy06882f82009-06-10 13:36:04 -07004362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004363 public int getSwitchStateForDevice(int devid, int sw) {
4364 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4365 "getSwitchStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004366 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004367 }
4368 return KeyInputQueue.getSwitchState(devid, sw);
4369 }
Romain Guy06882f82009-06-10 13:36:04 -07004370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004371 public int getScancodeState(int sw) {
4372 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4373 "getScancodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004374 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004375 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004376 return mQueue.getScancodeState(sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004377 }
Romain Guy06882f82009-06-10 13:36:04 -07004378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004379 public int getScancodeStateForDevice(int devid, int sw) {
4380 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4381 "getScancodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004382 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004383 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004384 return mQueue.getScancodeState(devid, sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004385 }
Romain Guy06882f82009-06-10 13:36:04 -07004386
Dianne Hackborn1d62ea92009-11-17 12:49:50 -08004387 public int getTrackballScancodeState(int sw) {
4388 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4389 "getTrackballScancodeState()")) {
4390 throw new SecurityException("Requires READ_INPUT_STATE permission");
4391 }
4392 return mQueue.getTrackballScancodeState(sw);
4393 }
4394
4395 public int getDPadScancodeState(int sw) {
4396 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4397 "getDPadScancodeState()")) {
4398 throw new SecurityException("Requires READ_INPUT_STATE permission");
4399 }
4400 return mQueue.getDPadScancodeState(sw);
4401 }
4402
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004403 public int getKeycodeState(int sw) {
4404 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4405 "getKeycodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004406 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004407 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004408 return mQueue.getKeycodeState(sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004409 }
Romain Guy06882f82009-06-10 13:36:04 -07004410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004411 public int getKeycodeStateForDevice(int devid, int sw) {
4412 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4413 "getKeycodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004414 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004415 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004416 return mQueue.getKeycodeState(devid, sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004417 }
Romain Guy06882f82009-06-10 13:36:04 -07004418
Dianne Hackborn1d62ea92009-11-17 12:49:50 -08004419 public int getTrackballKeycodeState(int sw) {
4420 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4421 "getTrackballKeycodeState()")) {
4422 throw new SecurityException("Requires READ_INPUT_STATE permission");
4423 }
4424 return mQueue.getTrackballKeycodeState(sw);
4425 }
4426
4427 public int getDPadKeycodeState(int sw) {
4428 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4429 "getDPadKeycodeState()")) {
4430 throw new SecurityException("Requires READ_INPUT_STATE permission");
4431 }
4432 return mQueue.getDPadKeycodeState(sw);
4433 }
4434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004435 public boolean hasKeys(int[] keycodes, boolean[] keyExists) {
4436 return KeyInputQueue.hasKeys(keycodes, keyExists);
4437 }
Romain Guy06882f82009-06-10 13:36:04 -07004438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004439 public void enableScreenAfterBoot() {
4440 synchronized(mWindowMap) {
4441 if (mSystemBooted) {
4442 return;
4443 }
4444 mSystemBooted = true;
4445 }
Romain Guy06882f82009-06-10 13:36:04 -07004446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004447 performEnableScreen();
4448 }
Romain Guy06882f82009-06-10 13:36:04 -07004449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004450 public void enableScreenIfNeededLocked() {
4451 if (mDisplayEnabled) {
4452 return;
4453 }
4454 if (!mSystemBooted) {
4455 return;
4456 }
4457 mH.sendMessage(mH.obtainMessage(H.ENABLE_SCREEN));
4458 }
Romain Guy06882f82009-06-10 13:36:04 -07004459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004460 public void performEnableScreen() {
4461 synchronized(mWindowMap) {
4462 if (mDisplayEnabled) {
4463 return;
4464 }
4465 if (!mSystemBooted) {
4466 return;
4467 }
Romain Guy06882f82009-06-10 13:36:04 -07004468
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004469 // Don't enable the screen until all existing windows
4470 // have been drawn.
4471 final int N = mWindows.size();
4472 for (int i=0; i<N; i++) {
4473 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn5943c202010-04-12 21:36:49 -07004474 if (w.isVisibleLw() && !w.mObscured
4475 && (w.mOrientationChanging || !w.isDrawnLw())) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004476 return;
4477 }
4478 }
Romain Guy06882f82009-06-10 13:36:04 -07004479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004480 mDisplayEnabled = true;
4481 if (false) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004482 Slog.i(TAG, "ENABLING SCREEN!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004483 StringWriter sw = new StringWriter();
4484 PrintWriter pw = new PrintWriter(sw);
4485 this.dump(null, pw, null);
Joe Onorato8a9b2202010-02-26 18:56:32 -08004486 Slog.i(TAG, sw.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004487 }
4488 try {
4489 IBinder surfaceFlinger = ServiceManager.getService("SurfaceFlinger");
4490 if (surfaceFlinger != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004491 //Slog.i(TAG, "******* TELLING SURFACE FLINGER WE ARE BOOTED!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004492 Parcel data = Parcel.obtain();
4493 data.writeInterfaceToken("android.ui.ISurfaceComposer");
4494 surfaceFlinger.transact(IBinder.FIRST_CALL_TRANSACTION,
4495 data, null, 0);
4496 data.recycle();
4497 }
4498 } catch (RemoteException ex) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004499 Slog.e(TAG, "Boot completed: SurfaceFlinger is dead!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004500 }
4501 }
Romain Guy06882f82009-06-10 13:36:04 -07004502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004503 mPolicy.enableScreenAfterBoot();
Romain Guy06882f82009-06-10 13:36:04 -07004504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004505 // Make sure the last requested orientation has been applied.
Dianne Hackborn321ae682009-03-27 16:16:03 -07004506 setRotationUnchecked(WindowManagerPolicy.USE_LAST_ROTATION, false,
4507 mLastRotationFlags | Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004508 }
Romain Guy06882f82009-06-10 13:36:04 -07004509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004510 public void setInTouchMode(boolean mode) {
4511 synchronized(mWindowMap) {
4512 mInTouchMode = mode;
4513 }
4514 }
4515
Romain Guy06882f82009-06-10 13:36:04 -07004516 public void setRotation(int rotation,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004517 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004518 if (!checkCallingPermission(android.Manifest.permission.SET_ORIENTATION,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004519 "setRotation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004520 throw new SecurityException("Requires SET_ORIENTATION permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004521 }
4522
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004523 setRotationUnchecked(rotation, alwaysSendConfiguration, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004524 }
Romain Guy06882f82009-06-10 13:36:04 -07004525
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004526 public void setRotationUnchecked(int rotation,
4527 boolean alwaysSendConfiguration, int animFlags) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004528 if(DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004529 "alwaysSendConfiguration set to "+alwaysSendConfiguration);
Romain Guy06882f82009-06-10 13:36:04 -07004530
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004531 long origId = Binder.clearCallingIdentity();
4532 boolean changed;
4533 synchronized(mWindowMap) {
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004534 changed = setRotationUncheckedLocked(rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004535 }
Romain Guy06882f82009-06-10 13:36:04 -07004536
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004537 if (changed || alwaysSendConfiguration) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004538 sendNewConfiguration();
4539 }
Romain Guy06882f82009-06-10 13:36:04 -07004540
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004541 Binder.restoreCallingIdentity(origId);
4542 }
Romain Guy06882f82009-06-10 13:36:04 -07004543
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004544 /**
4545 * Apply a new rotation to the screen, respecting the requests of
4546 * applications. Use WindowManagerPolicy.USE_LAST_ROTATION to simply
4547 * re-evaluate the desired rotation.
4548 *
4549 * Returns null if the rotation has been changed. In this case YOU
4550 * MUST CALL setNewConfiguration() TO UNFREEZE THE SCREEN.
4551 */
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004552 public boolean setRotationUncheckedLocked(int rotation, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004553 boolean changed;
4554 if (rotation == WindowManagerPolicy.USE_LAST_ROTATION) {
4555 rotation = mRequestedRotation;
4556 } else {
4557 mRequestedRotation = rotation;
Dianne Hackborn321ae682009-03-27 16:16:03 -07004558 mLastRotationFlags = animFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004559 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08004560 if (DEBUG_ORIENTATION) Slog.v(TAG, "Overwriting rotation value from " + rotation);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004561 rotation = mPolicy.rotationForOrientationLw(mForcedAppOrientation,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004562 mRotation, mDisplayEnabled);
Joe Onorato8a9b2202010-02-26 18:56:32 -08004563 if (DEBUG_ORIENTATION) Slog.v(TAG, "new rotation is set to " + rotation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004564 changed = mDisplayEnabled && mRotation != rotation;
Romain Guy06882f82009-06-10 13:36:04 -07004565
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004566 if (changed) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004567 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004568 "Rotation changed to " + rotation
4569 + " from " + mRotation
4570 + " (forceApp=" + mForcedAppOrientation
4571 + ", req=" + mRequestedRotation + ")");
4572 mRotation = rotation;
4573 mWindowsFreezingScreen = true;
4574 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
4575 mH.sendMessageDelayed(mH.obtainMessage(H.WINDOW_FREEZE_TIMEOUT),
4576 2000);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004577 mWaitingForConfig = true;
4578 mLayoutNeeded = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004579 startFreezingDisplayLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08004580 Slog.i(TAG, "Setting rotation to " + rotation + ", animFlags=" + animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004581 mQueue.setOrientation(rotation);
4582 if (mDisplayEnabled) {
Dianne Hackborn321ae682009-03-27 16:16:03 -07004583 Surface.setOrientation(0, rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004584 }
4585 for (int i=mWindows.size()-1; i>=0; i--) {
4586 WindowState w = (WindowState)mWindows.get(i);
4587 if (w.mSurface != null) {
4588 w.mOrientationChanging = true;
4589 }
4590 }
4591 for (int i=mRotationWatchers.size()-1; i>=0; i--) {
4592 try {
4593 mRotationWatchers.get(i).onRotationChanged(rotation);
4594 } catch (RemoteException e) {
4595 }
4596 }
4597 } //end if changed
Romain Guy06882f82009-06-10 13:36:04 -07004598
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004599 return changed;
4600 }
Romain Guy06882f82009-06-10 13:36:04 -07004601
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004602 public int getRotation() {
4603 return mRotation;
4604 }
4605
4606 public int watchRotation(IRotationWatcher watcher) {
4607 final IBinder watcherBinder = watcher.asBinder();
4608 IBinder.DeathRecipient dr = new IBinder.DeathRecipient() {
4609 public void binderDied() {
4610 synchronized (mWindowMap) {
4611 for (int i=0; i<mRotationWatchers.size(); i++) {
4612 if (watcherBinder == mRotationWatchers.get(i).asBinder()) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07004613 IRotationWatcher removed = mRotationWatchers.remove(i);
4614 if (removed != null) {
4615 removed.asBinder().unlinkToDeath(this, 0);
4616 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004617 i--;
4618 }
4619 }
4620 }
4621 }
4622 };
Romain Guy06882f82009-06-10 13:36:04 -07004623
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004624 synchronized (mWindowMap) {
4625 try {
4626 watcher.asBinder().linkToDeath(dr, 0);
4627 mRotationWatchers.add(watcher);
4628 } catch (RemoteException e) {
4629 // Client died, no cleanup needed.
4630 }
Romain Guy06882f82009-06-10 13:36:04 -07004631
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004632 return mRotation;
4633 }
4634 }
4635
4636 /**
4637 * Starts the view server on the specified port.
4638 *
4639 * @param port The port to listener to.
4640 *
4641 * @return True if the server was successfully started, false otherwise.
4642 *
4643 * @see com.android.server.ViewServer
4644 * @see com.android.server.ViewServer#VIEW_SERVER_DEFAULT_PORT
4645 */
4646 public boolean startViewServer(int port) {
Romain Guy06882f82009-06-10 13:36:04 -07004647 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004648 return false;
4649 }
4650
4651 if (!checkCallingPermission(Manifest.permission.DUMP, "startViewServer")) {
4652 return false;
4653 }
4654
4655 if (port < 1024) {
4656 return false;
4657 }
4658
4659 if (mViewServer != null) {
4660 if (!mViewServer.isRunning()) {
4661 try {
4662 return mViewServer.start();
4663 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004664 Slog.w(TAG, "View server did not start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004665 }
4666 }
4667 return false;
4668 }
4669
4670 try {
4671 mViewServer = new ViewServer(this, port);
4672 return mViewServer.start();
4673 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004674 Slog.w(TAG, "View server did not start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004675 }
4676 return false;
4677 }
4678
Romain Guy06882f82009-06-10 13:36:04 -07004679 private boolean isSystemSecure() {
4680 return "1".equals(SystemProperties.get(SYSTEM_SECURE, "1")) &&
4681 "0".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
4682 }
4683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004684 /**
4685 * Stops the view server if it exists.
4686 *
4687 * @return True if the server stopped, false if it wasn't started or
4688 * couldn't be stopped.
4689 *
4690 * @see com.android.server.ViewServer
4691 */
4692 public boolean stopViewServer() {
Romain Guy06882f82009-06-10 13:36:04 -07004693 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004694 return false;
4695 }
4696
4697 if (!checkCallingPermission(Manifest.permission.DUMP, "stopViewServer")) {
4698 return false;
4699 }
4700
4701 if (mViewServer != null) {
4702 return mViewServer.stop();
4703 }
4704 return false;
4705 }
4706
4707 /**
4708 * Indicates whether the view server is running.
4709 *
4710 * @return True if the server is running, false otherwise.
4711 *
4712 * @see com.android.server.ViewServer
4713 */
4714 public boolean isViewServerRunning() {
Romain Guy06882f82009-06-10 13:36:04 -07004715 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004716 return false;
4717 }
4718
4719 if (!checkCallingPermission(Manifest.permission.DUMP, "isViewServerRunning")) {
4720 return false;
4721 }
4722
4723 return mViewServer != null && mViewServer.isRunning();
4724 }
4725
4726 /**
4727 * Lists all availble windows in the system. The listing is written in the
4728 * specified Socket's output stream with the following syntax:
4729 * windowHashCodeInHexadecimal windowName
4730 * Each line of the ouput represents a different window.
4731 *
4732 * @param client The remote client to send the listing to.
4733 * @return False if an error occured, true otherwise.
4734 */
4735 boolean viewServerListWindows(Socket client) {
Romain Guy06882f82009-06-10 13:36:04 -07004736 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004737 return false;
4738 }
4739
4740 boolean result = true;
4741
4742 Object[] windows;
4743 synchronized (mWindowMap) {
4744 windows = new Object[mWindows.size()];
4745 //noinspection unchecked
4746 windows = mWindows.toArray(windows);
4747 }
4748
4749 BufferedWriter out = null;
4750
4751 // Any uncaught exception will crash the system process
4752 try {
4753 OutputStream clientStream = client.getOutputStream();
4754 out = new BufferedWriter(new OutputStreamWriter(clientStream), 8 * 1024);
4755
4756 final int count = windows.length;
4757 for (int i = 0; i < count; i++) {
4758 final WindowState w = (WindowState) windows[i];
4759 out.write(Integer.toHexString(System.identityHashCode(w)));
4760 out.write(' ');
4761 out.append(w.mAttrs.getTitle());
4762 out.write('\n');
4763 }
4764
4765 out.write("DONE.\n");
4766 out.flush();
4767 } catch (Exception e) {
4768 result = false;
4769 } finally {
4770 if (out != null) {
4771 try {
4772 out.close();
4773 } catch (IOException e) {
4774 result = false;
4775 }
4776 }
4777 }
4778
4779 return result;
4780 }
4781
4782 /**
4783 * Sends a command to a target window. The result of the command, if any, will be
4784 * written in the output stream of the specified socket.
4785 *
4786 * The parameters must follow this syntax:
4787 * windowHashcode extra
4788 *
4789 * Where XX is the length in characeters of the windowTitle.
4790 *
4791 * The first parameter is the target window. The window with the specified hashcode
4792 * will be the target. If no target can be found, nothing happens. The extra parameters
4793 * will be delivered to the target window and as parameters to the command itself.
4794 *
4795 * @param client The remote client to sent the result, if any, to.
4796 * @param command The command to execute.
4797 * @param parameters The command parameters.
4798 *
4799 * @return True if the command was successfully delivered, false otherwise. This does
4800 * not indicate whether the command itself was successful.
4801 */
4802 boolean viewServerWindowCommand(Socket client, String command, String parameters) {
Romain Guy06882f82009-06-10 13:36:04 -07004803 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004804 return false;
4805 }
4806
4807 boolean success = true;
4808 Parcel data = null;
4809 Parcel reply = null;
4810
4811 // Any uncaught exception will crash the system process
4812 try {
4813 // Find the hashcode of the window
4814 int index = parameters.indexOf(' ');
4815 if (index == -1) {
4816 index = parameters.length();
4817 }
4818 final String code = parameters.substring(0, index);
Romain Guy236092a2009-12-14 15:31:48 -08004819 int hashCode = (int) Long.parseLong(code, 16);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004820
4821 // Extract the command's parameter after the window description
4822 if (index < parameters.length()) {
4823 parameters = parameters.substring(index + 1);
4824 } else {
4825 parameters = "";
4826 }
4827
4828 final WindowManagerService.WindowState window = findWindow(hashCode);
4829 if (window == null) {
4830 return false;
4831 }
4832
4833 data = Parcel.obtain();
4834 data.writeInterfaceToken("android.view.IWindow");
4835 data.writeString(command);
4836 data.writeString(parameters);
4837 data.writeInt(1);
4838 ParcelFileDescriptor.fromSocket(client).writeToParcel(data, 0);
4839
4840 reply = Parcel.obtain();
4841
4842 final IBinder binder = window.mClient.asBinder();
4843 // TODO: GET THE TRANSACTION CODE IN A SAFER MANNER
4844 binder.transact(IBinder.FIRST_CALL_TRANSACTION, data, reply, 0);
4845
4846 reply.readException();
4847
4848 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004849 Slog.w(TAG, "Could not send command " + command + " with parameters " + parameters, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004850 success = false;
4851 } finally {
4852 if (data != null) {
4853 data.recycle();
4854 }
4855 if (reply != null) {
4856 reply.recycle();
4857 }
4858 }
4859
4860 return success;
4861 }
4862
4863 private WindowState findWindow(int hashCode) {
4864 if (hashCode == -1) {
4865 return getFocusedWindow();
4866 }
4867
4868 synchronized (mWindowMap) {
4869 final ArrayList windows = mWindows;
4870 final int count = windows.size();
4871
4872 for (int i = 0; i < count; i++) {
4873 WindowState w = (WindowState) windows.get(i);
4874 if (System.identityHashCode(w) == hashCode) {
4875 return w;
4876 }
4877 }
4878 }
4879
4880 return null;
4881 }
4882
4883 /*
4884 * Instruct the Activity Manager to fetch the current configuration and broadcast
4885 * that to config-changed listeners if appropriate.
4886 */
4887 void sendNewConfiguration() {
4888 try {
4889 mActivityManager.updateConfiguration(null);
4890 } catch (RemoteException e) {
4891 }
4892 }
Romain Guy06882f82009-06-10 13:36:04 -07004893
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004894 public Configuration computeNewConfiguration() {
4895 synchronized (mWindowMap) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07004896 return computeNewConfigurationLocked();
4897 }
4898 }
Romain Guy06882f82009-06-10 13:36:04 -07004899
Dianne Hackbornc485a602009-03-24 22:39:49 -07004900 Configuration computeNewConfigurationLocked() {
4901 Configuration config = new Configuration();
4902 if (!computeNewConfigurationLocked(config)) {
4903 return null;
4904 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07004905 return config;
4906 }
Romain Guy06882f82009-06-10 13:36:04 -07004907
Dianne Hackbornc485a602009-03-24 22:39:49 -07004908 boolean computeNewConfigurationLocked(Configuration config) {
4909 if (mDisplay == null) {
4910 return false;
4911 }
4912 mQueue.getInputConfiguration(config);
Christopher Tateb696aee2010-04-02 19:08:30 -07004913
4914 // Use the effective "visual" dimensions based on current rotation
4915 final boolean rotated = (mRotation == Surface.ROTATION_90
4916 || mRotation == Surface.ROTATION_270);
4917 final int dw = rotated ? mInitialDisplayHeight : mInitialDisplayWidth;
4918 final int dh = rotated ? mInitialDisplayWidth : mInitialDisplayHeight;
4919
Dianne Hackbornc485a602009-03-24 22:39:49 -07004920 int orientation = Configuration.ORIENTATION_SQUARE;
4921 if (dw < dh) {
4922 orientation = Configuration.ORIENTATION_PORTRAIT;
4923 } else if (dw > dh) {
4924 orientation = Configuration.ORIENTATION_LANDSCAPE;
4925 }
4926 config.orientation = orientation;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004927
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004928 DisplayMetrics dm = new DisplayMetrics();
4929 mDisplay.getMetrics(dm);
4930 CompatibilityInfo.updateCompatibleScreenFrame(dm, orientation, mCompatibleScreenFrame);
4931
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004932 if (mScreenLayout == Configuration.SCREENLAYOUT_SIZE_UNDEFINED) {
Dianne Hackborn723738c2009-06-25 19:48:04 -07004933 // Note we only do this once because at this point we don't
4934 // expect the screen to change in this way at runtime, and want
4935 // to avoid all of this computation for every config change.
Dianne Hackborn723738c2009-06-25 19:48:04 -07004936 int longSize = dw;
4937 int shortSize = dh;
4938 if (longSize < shortSize) {
4939 int tmp = longSize;
4940 longSize = shortSize;
4941 shortSize = tmp;
4942 }
4943 longSize = (int)(longSize/dm.density);
4944 shortSize = (int)(shortSize/dm.density);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004945
Dianne Hackborn723738c2009-06-25 19:48:04 -07004946 // These semi-magic numbers define our compatibility modes for
4947 // applications with different screens. Don't change unless you
4948 // make sure to test lots and lots of apps!
4949 if (longSize < 470) {
4950 // This is shorter than an HVGA normal density screen (which
4951 // is 480 pixels on its long side).
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004952 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_SMALL
4953 | Configuration.SCREENLAYOUT_LONG_NO;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004954 } else {
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004955 // Is this a large screen?
4956 if (longSize > 640 && shortSize >= 480) {
4957 // VGA or larger screens at medium density are the point
4958 // at which we consider it to be a large screen.
4959 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_LARGE;
4960 } else {
4961 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_NORMAL;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004962
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004963 // If this screen is wider than normal HVGA, or taller
4964 // than FWVGA, then for old apps we want to run in size
4965 // compatibility mode.
4966 if (shortSize > 321 || longSize > 570) {
4967 mScreenLayout |= Configuration.SCREENLAYOUT_COMPAT_NEEDED;
4968 }
4969 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004970
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004971 // Is this a long screen?
4972 if (((longSize*3)/5) >= (shortSize-1)) {
4973 // Anything wider than WVGA (5:3) is considering to be long.
4974 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_YES;
4975 } else {
4976 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_NO;
4977 }
Dianne Hackborn723738c2009-06-25 19:48:04 -07004978 }
4979 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004980 config.screenLayout = mScreenLayout;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004981
Dianne Hackbornc485a602009-03-24 22:39:49 -07004982 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
4983 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
4984 mPolicy.adjustConfigurationLw(config);
4985 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004986 }
Romain Guy06882f82009-06-10 13:36:04 -07004987
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004988 // -------------------------------------------------------------
4989 // Input Events and Focus Management
4990 // -------------------------------------------------------------
4991
4992 private final void wakeupIfNeeded(WindowState targetWin, int eventType) {
Michael Chane96440f2009-05-06 10:27:36 -07004993 long curTime = SystemClock.uptimeMillis();
4994
Michael Chane10de972009-05-18 11:24:50 -07004995 if (eventType == TOUCH_EVENT || eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT) {
Michael Chane96440f2009-05-06 10:27:36 -07004996 if (mLastTouchEventType == eventType &&
4997 (curTime - mLastUserActivityCallTime) < MIN_TIME_BETWEEN_USERACTIVITIES) {
4998 return;
4999 }
5000 mLastUserActivityCallTime = curTime;
5001 mLastTouchEventType = eventType;
5002 }
5003
5004 if (targetWin == null
5005 || targetWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
5006 mPowerManager.userActivity(curTime, false, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005007 }
5008 }
5009
5010 // tells if it's a cheek event or not -- this function is stateful
5011 private static final int EVENT_NONE = 0;
5012 private static final int EVENT_UNKNOWN = 0;
5013 private static final int EVENT_CHEEK = 0;
5014 private static final int EVENT_IGNORE_DURATION = 300; // ms
5015 private static final float CHEEK_THRESHOLD = 0.6f;
5016 private int mEventState = EVENT_NONE;
5017 private float mEventSize;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07005018
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005019 private int eventType(MotionEvent ev) {
5020 float size = ev.getSize();
5021 switch (ev.getAction()) {
5022 case MotionEvent.ACTION_DOWN:
5023 mEventSize = size;
5024 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_EVENT;
5025 case MotionEvent.ACTION_UP:
5026 if (size > mEventSize) mEventSize = size;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07005027 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005028 case MotionEvent.ACTION_MOVE:
5029 final int N = ev.getHistorySize();
5030 if (size > mEventSize) mEventSize = size;
5031 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
5032 for (int i=0; i<N; i++) {
5033 size = ev.getHistoricalSize(i);
5034 if (size > mEventSize) mEventSize = size;
5035 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
5036 }
5037 if (ev.getEventTime() < ev.getDownTime() + EVENT_IGNORE_DURATION) {
5038 return TOUCH_EVENT;
5039 } else {
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07005040 return LONG_TOUCH_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005041 }
5042 default:
5043 // not good
5044 return OTHER_EVENT;
5045 }
5046 }
5047
5048 /**
5049 * @return Returns true if event was dispatched, false if it was dropped for any reason
5050 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07005051 private int dispatchPointer(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005052 if (DEBUG_INPUT || WindowManagerPolicy.WATCH_POINTER) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005053 "dispatchPointer " + ev);
5054
Michael Chan53071d62009-05-13 17:29:48 -07005055 if (MEASURE_LATENCY) {
5056 lt.sample("3 Wait for last dispatch ", System.nanoTime() - qev.whenNano);
5057 }
5058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005059 Object targetObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005060 ev, true, false, pid, uid);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005061
Michael Chan53071d62009-05-13 17:29:48 -07005062 if (MEASURE_LATENCY) {
5063 lt.sample("3 Last dispatch finished ", System.nanoTime() - qev.whenNano);
5064 }
5065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005066 int action = ev.getAction();
Romain Guy06882f82009-06-10 13:36:04 -07005067
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005068 if (action == MotionEvent.ACTION_UP) {
5069 // let go of our target
5070 mKeyWaiter.mMotionTarget = null;
5071 mPowerManager.logPointerUpEvent();
5072 } else if (action == MotionEvent.ACTION_DOWN) {
5073 mPowerManager.logPointerDownEvent();
5074 }
5075
5076 if (targetObj == null) {
5077 // In this case we are either dropping the event, or have received
5078 // a move or up without a down. It is common to receive move
5079 // events in such a way, since this means the user is moving the
5080 // pointer without actually pressing down. All other cases should
5081 // be atypical, so let's log them.
Michael Chane96440f2009-05-06 10:27:36 -07005082 if (action != MotionEvent.ACTION_MOVE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005083 Slog.w(TAG, "No window to dispatch pointer action " + ev.getAction());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005084 }
Dianne Hackborn6adba242009-11-10 11:10:09 -08005085 synchronized (mWindowMap) {
Dianne Hackborn90d2db32010-02-11 22:19:06 -08005086 dispatchPointerElsewhereLocked(null, null, ev, ev.getEventTime(), true);
Dianne Hackborn6adba242009-11-10 11:10:09 -08005087 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005088 if (qev != null) {
5089 mQueue.recycleEvent(qev);
5090 }
5091 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005092 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005093 }
5094 if (targetObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborn6adba242009-11-10 11:10:09 -08005095 synchronized (mWindowMap) {
Dianne Hackborn90d2db32010-02-11 22:19:06 -08005096 dispatchPointerElsewhereLocked(null, null, ev, ev.getEventTime(), true);
Dianne Hackborn6adba242009-11-10 11:10:09 -08005097 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005098 if (qev != null) {
5099 mQueue.recycleEvent(qev);
5100 }
5101 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005102 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005103 }
Romain Guy06882f82009-06-10 13:36:04 -07005104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005105 WindowState target = (WindowState)targetObj;
Romain Guy06882f82009-06-10 13:36:04 -07005106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005107 final long eventTime = ev.getEventTime();
Michael Chan53071d62009-05-13 17:29:48 -07005108 final long eventTimeNano = ev.getEventTimeNano();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005109
Joe Onorato8a9b2202010-02-26 18:56:32 -08005110 //Slog.i(TAG, "Sending " + ev + " to " + target);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005111
5112 if (uid != 0 && uid != target.mSession.mUid) {
5113 if (mContext.checkPermission(
5114 android.Manifest.permission.INJECT_EVENTS, pid, uid)
5115 != PackageManager.PERMISSION_GRANTED) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005116 Slog.w(TAG, "Permission denied: injecting pointer event from pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005117 + pid + " uid " + uid + " to window " + target
5118 + " owned by uid " + target.mSession.mUid);
5119 if (qev != null) {
5120 mQueue.recycleEvent(qev);
5121 }
5122 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005123 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005124 }
5125 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005126
Michael Chan53071d62009-05-13 17:29:48 -07005127 if (MEASURE_LATENCY) {
5128 lt.sample("4 in dispatchPointer ", System.nanoTime() - eventTimeNano);
5129 }
5130
Romain Guy06882f82009-06-10 13:36:04 -07005131 if ((target.mAttrs.flags &
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005132 WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES) != 0) {
5133 //target wants to ignore fat touch events
5134 boolean cheekPress = mPolicy.isCheekPressedAgainstScreen(ev);
5135 //explicit flag to return without processing event further
5136 boolean returnFlag = false;
5137 if((action == MotionEvent.ACTION_DOWN)) {
5138 mFatTouch = false;
5139 if(cheekPress) {
5140 mFatTouch = true;
5141 returnFlag = true;
5142 }
5143 } else {
5144 if(action == MotionEvent.ACTION_UP) {
5145 if(mFatTouch) {
5146 //earlier even was invalid doesnt matter if current up is cheekpress or not
5147 mFatTouch = false;
5148 returnFlag = true;
5149 } else if(cheekPress) {
5150 //cancel the earlier event
5151 ev.setAction(MotionEvent.ACTION_CANCEL);
5152 action = MotionEvent.ACTION_CANCEL;
5153 }
5154 } else if(action == MotionEvent.ACTION_MOVE) {
5155 if(mFatTouch) {
5156 //two cases here
5157 //an invalid down followed by 0 or moves(valid or invalid)
Romain Guy06882f82009-06-10 13:36:04 -07005158 //a valid down, invalid move, more moves. want to ignore till up
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005159 returnFlag = true;
5160 } else if(cheekPress) {
5161 //valid down followed by invalid moves
5162 //an invalid move have to cancel earlier action
5163 ev.setAction(MotionEvent.ACTION_CANCEL);
5164 action = MotionEvent.ACTION_CANCEL;
Joe Onorato8a9b2202010-02-26 18:56:32 -08005165 if (DEBUG_INPUT) Slog.v(TAG, "Sending cancel for invalid ACTION_MOVE");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005166 //note that the subsequent invalid moves will not get here
5167 mFatTouch = true;
5168 }
5169 }
5170 } //else if action
5171 if(returnFlag) {
5172 //recycle que, ev
5173 if (qev != null) {
5174 mQueue.recycleEvent(qev);
5175 }
5176 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005177 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005178 }
5179 } //end if target
Michael Chane96440f2009-05-06 10:27:36 -07005180
Michael Chan9f028e62009-08-04 17:37:46 -07005181 // Enable this for testing the "right" value
5182 if (false && action == MotionEvent.ACTION_DOWN) {
Michael Chane96440f2009-05-06 10:27:36 -07005183 int max_events_per_sec = 35;
5184 try {
5185 max_events_per_sec = Integer.parseInt(SystemProperties
5186 .get("windowsmgr.max_events_per_sec"));
5187 if (max_events_per_sec < 1) {
5188 max_events_per_sec = 35;
5189 }
5190 } catch (NumberFormatException e) {
5191 }
5192 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
5193 }
5194
5195 /*
5196 * Throttle events to minimize CPU usage when there's a flood of events
5197 * e.g. constant contact with the screen
5198 */
5199 if (action == MotionEvent.ACTION_MOVE) {
5200 long nextEventTime = mLastTouchEventTime + mMinWaitTimeBetweenTouchEvents;
5201 long now = SystemClock.uptimeMillis();
5202 if (now < nextEventTime) {
5203 try {
5204 Thread.sleep(nextEventTime - now);
5205 } catch (InterruptedException e) {
5206 }
5207 mLastTouchEventTime = nextEventTime;
5208 } else {
5209 mLastTouchEventTime = now;
5210 }
5211 }
5212
Michael Chan53071d62009-05-13 17:29:48 -07005213 if (MEASURE_LATENCY) {
5214 lt.sample("5 in dispatchPointer ", System.nanoTime() - eventTimeNano);
5215 }
5216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005217 synchronized(mWindowMap) {
Dianne Hackborn6adba242009-11-10 11:10:09 -08005218 if (!target.isVisibleLw()) {
5219 // During this motion dispatch, the target window has become
5220 // invisible.
Dianne Hackborn90d2db32010-02-11 22:19:06 -08005221 dispatchPointerElsewhereLocked(null, null, ev, ev.getEventTime(), false);
Dianne Hackborn6adba242009-11-10 11:10:09 -08005222 if (qev != null) {
5223 mQueue.recycleEvent(qev);
5224 }
5225 ev.recycle();
5226 return INJECT_SUCCEEDED;
5227 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005229 if (qev != null && action == MotionEvent.ACTION_MOVE) {
5230 mKeyWaiter.bindTargetWindowLocked(target,
5231 KeyWaiter.RETURN_PENDING_POINTER, qev);
5232 ev = null;
5233 } else {
5234 if (action == MotionEvent.ACTION_DOWN) {
5235 WindowState out = mKeyWaiter.mOutsideTouchTargets;
5236 if (out != null) {
5237 MotionEvent oev = MotionEvent.obtain(ev);
5238 oev.setAction(MotionEvent.ACTION_OUTSIDE);
5239 do {
5240 final Rect frame = out.mFrame;
5241 oev.offsetLocation(-(float)frame.left, -(float)frame.top);
5242 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005243 out.mClient.dispatchPointer(oev, eventTime, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005244 } catch (android.os.RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005245 Slog.i(TAG, "WINDOW DIED during outside motion dispatch: " + out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005246 }
5247 oev.offsetLocation((float)frame.left, (float)frame.top);
5248 out = out.mNextOutsideTouch;
5249 } while (out != null);
5250 mKeyWaiter.mOutsideTouchTargets = null;
5251 }
5252 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005253
Dianne Hackborn90d2db32010-02-11 22:19:06 -08005254 dispatchPointerElsewhereLocked(target, null, ev, ev.getEventTime(), false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005255
Dianne Hackborn6adba242009-11-10 11:10:09 -08005256 final Rect frame = target.mFrame;
5257 ev.offsetLocation(-(float)frame.left, -(float)frame.top);
5258 mKeyWaiter.bindTargetWindowLocked(target);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005259 }
5260 }
Romain Guy06882f82009-06-10 13:36:04 -07005261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005262 // finally offset the event to the target's coordinate system and
5263 // dispatch the event.
5264 try {
5265 if (DEBUG_INPUT || DEBUG_FOCUS || WindowManagerPolicy.WATCH_POINTER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005266 Slog.v(TAG, "Delivering pointer " + qev + " to " + target);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005267 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005268
Michael Chan53071d62009-05-13 17:29:48 -07005269 if (MEASURE_LATENCY) {
5270 lt.sample("6 before svr->client ipc ", System.nanoTime() - eventTimeNano);
5271 }
5272
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005273 target.mClient.dispatchPointer(ev, eventTime, true);
Michael Chan53071d62009-05-13 17:29:48 -07005274
5275 if (MEASURE_LATENCY) {
5276 lt.sample("7 after svr->client ipc ", System.nanoTime() - eventTimeNano);
5277 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07005278 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005279 } catch (android.os.RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005280 Slog.i(TAG, "WINDOW DIED during motion dispatch: " + target);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005281 mKeyWaiter.mMotionTarget = null;
5282 try {
5283 removeWindow(target.mSession, target.mClient);
5284 } catch (java.util.NoSuchElementException ex) {
5285 // This will happen if the window has already been
5286 // removed.
5287 }
5288 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07005289 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005290 }
Romain Guy06882f82009-06-10 13:36:04 -07005291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005292 /**
5293 * @return Returns true if event was dispatched, false if it was dropped for any reason
5294 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07005295 private int dispatchTrackball(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005296 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005297 TAG, "dispatchTrackball [" + ev.getAction() +"] <" + ev.getX() + ", " + ev.getY() + ">");
Romain Guy06882f82009-06-10 13:36:04 -07005298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005299 Object focusObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005300 ev, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005301 if (focusObj == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005302 Slog.w(TAG, "No focus window, dropping trackball: " + ev);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005303 if (qev != null) {
5304 mQueue.recycleEvent(qev);
5305 }
5306 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005307 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005308 }
5309 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
5310 if (qev != null) {
5311 mQueue.recycleEvent(qev);
5312 }
5313 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005314 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005315 }
Romain Guy06882f82009-06-10 13:36:04 -07005316
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005317 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07005318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005319 if (uid != 0 && uid != focus.mSession.mUid) {
5320 if (mContext.checkPermission(
5321 android.Manifest.permission.INJECT_EVENTS, pid, uid)
5322 != PackageManager.PERMISSION_GRANTED) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005323 Slog.w(TAG, "Permission denied: injecting key event from pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005324 + pid + " uid " + uid + " to window " + focus
5325 + " owned by uid " + focus.mSession.mUid);
5326 if (qev != null) {
5327 mQueue.recycleEvent(qev);
5328 }
5329 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005330 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005331 }
5332 }
Romain Guy06882f82009-06-10 13:36:04 -07005333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005334 final long eventTime = ev.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07005335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005336 synchronized(mWindowMap) {
5337 if (qev != null && ev.getAction() == MotionEvent.ACTION_MOVE) {
5338 mKeyWaiter.bindTargetWindowLocked(focus,
5339 KeyWaiter.RETURN_PENDING_TRACKBALL, qev);
5340 // We don't deliver movement events to the client, we hold
5341 // them and wait for them to call back.
5342 ev = null;
5343 } else {
5344 mKeyWaiter.bindTargetWindowLocked(focus);
5345 }
5346 }
Romain Guy06882f82009-06-10 13:36:04 -07005347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005348 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005349 focus.mClient.dispatchTrackball(ev, eventTime, true);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005350 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005351 } catch (android.os.RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005352 Slog.i(TAG, "WINDOW DIED during key dispatch: " + focus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005353 try {
5354 removeWindow(focus.mSession, focus.mClient);
5355 } catch (java.util.NoSuchElementException ex) {
5356 // This will happen if the window has already been
5357 // removed.
5358 }
5359 }
Romain Guy06882f82009-06-10 13:36:04 -07005360
Dianne Hackborncfaef692009-06-15 14:24:44 -07005361 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005362 }
Romain Guy06882f82009-06-10 13:36:04 -07005363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005364 /**
5365 * @return Returns true if event was dispatched, false if it was dropped for any reason
5366 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07005367 private int dispatchKey(KeyEvent event, int pid, int uid) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005368 if (DEBUG_INPUT) Slog.v(TAG, "Dispatch key: " + event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005369
5370 Object focusObj = mKeyWaiter.waitForNextEventTarget(event, null,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005371 null, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005372 if (focusObj == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005373 Slog.w(TAG, "No focus window, dropping: " + event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005374 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005375 }
5376 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005377 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005378 }
Romain Guy06882f82009-06-10 13:36:04 -07005379
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005380 // Okay we have finished waiting for the last event to be processed.
5381 // First off, if this is a repeat event, check to see if there is
5382 // a corresponding up event in the queue. If there is, we will
5383 // just drop the repeat, because it makes no sense to repeat after
5384 // the user has released a key. (This is especially important for
5385 // long presses.)
5386 if (event.getRepeatCount() > 0 && mQueue.hasKeyUpEvent(event)) {
5387 return INJECT_SUCCEEDED;
5388 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005390 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07005391
Joe Onorato8a9b2202010-02-26 18:56:32 -08005392 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005393 TAG, "Dispatching to " + focus + ": " + event);
5394
5395 if (uid != 0 && uid != focus.mSession.mUid) {
5396 if (mContext.checkPermission(
5397 android.Manifest.permission.INJECT_EVENTS, pid, uid)
5398 != PackageManager.PERMISSION_GRANTED) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005399 Slog.w(TAG, "Permission denied: injecting key event from pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005400 + pid + " uid " + uid + " to window " + focus
5401 + " owned by uid " + focus.mSession.mUid);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005402 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005403 }
5404 }
Romain Guy06882f82009-06-10 13:36:04 -07005405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005406 synchronized(mWindowMap) {
5407 mKeyWaiter.bindTargetWindowLocked(focus);
5408 }
5409
5410 // NOSHIP extra state logging
5411 mKeyWaiter.recordDispatchState(event, focus);
5412 // END NOSHIP
Romain Guy06882f82009-06-10 13:36:04 -07005413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005414 try {
5415 if (DEBUG_INPUT || DEBUG_FOCUS) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005416 Slog.v(TAG, "Delivering key " + event.getKeyCode()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005417 + " to " + focus);
5418 }
5419 focus.mClient.dispatchKey(event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005420 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005421 } catch (android.os.RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005422 Slog.i(TAG, "WINDOW DIED during key dispatch: " + focus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005423 try {
5424 removeWindow(focus.mSession, focus.mClient);
5425 } catch (java.util.NoSuchElementException ex) {
5426 // This will happen if the window has already been
5427 // removed.
5428 }
5429 }
Romain Guy06882f82009-06-10 13:36:04 -07005430
Dianne Hackborncfaef692009-06-15 14:24:44 -07005431 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005432 }
Romain Guy06882f82009-06-10 13:36:04 -07005433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005434 public void pauseKeyDispatching(IBinder _token) {
5435 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5436 "pauseKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005437 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005438 }
5439
5440 synchronized (mWindowMap) {
5441 WindowToken token = mTokenMap.get(_token);
5442 if (token != null) {
5443 mKeyWaiter.pauseDispatchingLocked(token);
5444 }
5445 }
5446 }
5447
5448 public void resumeKeyDispatching(IBinder _token) {
5449 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5450 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005451 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005452 }
5453
5454 synchronized (mWindowMap) {
5455 WindowToken token = mTokenMap.get(_token);
5456 if (token != null) {
5457 mKeyWaiter.resumeDispatchingLocked(token);
5458 }
5459 }
5460 }
5461
5462 public void setEventDispatching(boolean enabled) {
5463 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5464 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005465 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005466 }
5467
5468 synchronized (mWindowMap) {
5469 mKeyWaiter.setEventDispatchingLocked(enabled);
5470 }
5471 }
Romain Guy06882f82009-06-10 13:36:04 -07005472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005473 /**
5474 * Injects a keystroke event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005475 *
5476 * @param ev A motion event describing the keystroke action. (Be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005477 * {@link SystemClock#uptimeMillis()} as the timebase.)
5478 * @param sync If true, wait for the event to be completed before returning to the caller.
5479 * @return Returns true if event was dispatched, false if it was dropped for any reason
5480 */
5481 public boolean injectKeyEvent(KeyEvent ev, boolean sync) {
5482 long downTime = ev.getDownTime();
5483 long eventTime = ev.getEventTime();
5484
5485 int action = ev.getAction();
5486 int code = ev.getKeyCode();
5487 int repeatCount = ev.getRepeatCount();
5488 int metaState = ev.getMetaState();
5489 int deviceId = ev.getDeviceId();
5490 int scancode = ev.getScanCode();
5491
5492 if (eventTime == 0) eventTime = SystemClock.uptimeMillis();
5493 if (downTime == 0) downTime = eventTime;
5494
5495 KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
The Android Open Source Project10592532009-03-18 17:39:46 -07005496 deviceId, scancode, KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005497
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005498 final int pid = Binder.getCallingPid();
5499 final int uid = Binder.getCallingUid();
5500 final long ident = Binder.clearCallingIdentity();
5501 final int result = dispatchKey(newEvent, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005502 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005503 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005504 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005505 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005506 switch (result) {
5507 case INJECT_NO_PERMISSION:
5508 throw new SecurityException(
Chander S Pechetty27f3de62010-02-10 22:14:00 +05305509 "Injecting to another application requires INJECT_EVENTS permission");
Dianne Hackborncfaef692009-06-15 14:24:44 -07005510 case INJECT_SUCCEEDED:
5511 return true;
5512 }
5513 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005514 }
5515
5516 /**
5517 * Inject a pointer (touch) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005518 *
5519 * @param ev A motion event describing the pointer (touch) action. (As noted in
5520 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005521 * {@link SystemClock#uptimeMillis()} as the timebase.)
5522 * @param sync If true, wait for the event to be completed before returning to the caller.
5523 * @return Returns true if event was dispatched, false if it was dropped for any reason
5524 */
5525 public boolean injectPointerEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005526 final int pid = Binder.getCallingPid();
5527 final int uid = Binder.getCallingUid();
5528 final long ident = Binder.clearCallingIdentity();
5529 final int result = dispatchPointer(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005530 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005531 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005532 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005533 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005534 switch (result) {
5535 case INJECT_NO_PERMISSION:
5536 throw new SecurityException(
Chander S Pechetty27f3de62010-02-10 22:14:00 +05305537 "Injecting to another application requires INJECT_EVENTS permission");
Dianne Hackborncfaef692009-06-15 14:24:44 -07005538 case INJECT_SUCCEEDED:
5539 return true;
5540 }
5541 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005542 }
Romain Guy06882f82009-06-10 13:36:04 -07005543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005544 /**
5545 * Inject a trackball (navigation device) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005546 *
5547 * @param ev A motion event describing the trackball action. (As noted in
5548 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005549 * {@link SystemClock#uptimeMillis()} as the timebase.)
5550 * @param sync If true, wait for the event to be completed before returning to the caller.
5551 * @return Returns true if event was dispatched, false if it was dropped for any reason
5552 */
5553 public boolean injectTrackballEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005554 final int pid = Binder.getCallingPid();
5555 final int uid = Binder.getCallingUid();
5556 final long ident = Binder.clearCallingIdentity();
5557 final int result = dispatchTrackball(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005558 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005559 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005560 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005561 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005562 switch (result) {
5563 case INJECT_NO_PERMISSION:
5564 throw new SecurityException(
Chander S Pechetty27f3de62010-02-10 22:14:00 +05305565 "Injecting to another application requires INJECT_EVENTS permission");
Dianne Hackborncfaef692009-06-15 14:24:44 -07005566 case INJECT_SUCCEEDED:
5567 return true;
5568 }
5569 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005570 }
Romain Guy06882f82009-06-10 13:36:04 -07005571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005572 private WindowState getFocusedWindow() {
5573 synchronized (mWindowMap) {
5574 return getFocusedWindowLocked();
5575 }
5576 }
5577
5578 private WindowState getFocusedWindowLocked() {
5579 return mCurrentFocus;
5580 }
Romain Guy06882f82009-06-10 13:36:04 -07005581
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005582 /**
5583 * This class holds the state for dispatching key events. This state
5584 * is protected by the KeyWaiter instance, NOT by the window lock. You
5585 * can be holding the main window lock while acquire the KeyWaiter lock,
5586 * but not the other way around.
5587 */
5588 final class KeyWaiter {
5589 // NOSHIP debugging
5590 public class DispatchState {
5591 private KeyEvent event;
5592 private WindowState focus;
5593 private long time;
5594 private WindowState lastWin;
5595 private IBinder lastBinder;
5596 private boolean finished;
5597 private boolean gotFirstWindow;
5598 private boolean eventDispatching;
5599 private long timeToSwitch;
5600 private boolean wasFrozen;
5601 private boolean focusPaused;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005602 private WindowState curFocus;
Romain Guy06882f82009-06-10 13:36:04 -07005603
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005604 DispatchState(KeyEvent theEvent, WindowState theFocus) {
5605 focus = theFocus;
5606 event = theEvent;
5607 time = System.currentTimeMillis();
5608 // snapshot KeyWaiter state
5609 lastWin = mLastWin;
5610 lastBinder = mLastBinder;
5611 finished = mFinished;
5612 gotFirstWindow = mGotFirstWindow;
5613 eventDispatching = mEventDispatching;
5614 timeToSwitch = mTimeToSwitch;
5615 wasFrozen = mWasFrozen;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005616 curFocus = mCurrentFocus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005617 // cache the paused state at ctor time as well
5618 if (theFocus == null || theFocus.mToken == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005619 focusPaused = false;
5620 } else {
5621 focusPaused = theFocus.mToken.paused;
5622 }
5623 }
Romain Guy06882f82009-06-10 13:36:04 -07005624
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005625 public String toString() {
5626 return "{{" + event + " to " + focus + " @ " + time
5627 + " lw=" + lastWin + " lb=" + lastBinder
5628 + " fin=" + finished + " gfw=" + gotFirstWindow
5629 + " ed=" + eventDispatching + " tts=" + timeToSwitch
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005630 + " wf=" + wasFrozen + " fp=" + focusPaused
Christopher Tate46d45252010-02-09 15:48:57 -08005631 + " mcf=" + curFocus + "}}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005632 }
5633 };
5634 private DispatchState mDispatchState = null;
5635 public void recordDispatchState(KeyEvent theEvent, WindowState theFocus) {
5636 mDispatchState = new DispatchState(theEvent, theFocus);
5637 }
5638 // END NOSHIP
5639
5640 public static final int RETURN_NOTHING = 0;
5641 public static final int RETURN_PENDING_POINTER = 1;
5642 public static final int RETURN_PENDING_TRACKBALL = 2;
Romain Guy06882f82009-06-10 13:36:04 -07005643
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005644 final Object SKIP_TARGET_TOKEN = new Object();
5645 final Object CONSUMED_EVENT_TOKEN = new Object();
Romain Guy06882f82009-06-10 13:36:04 -07005646
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005647 private WindowState mLastWin = null;
5648 private IBinder mLastBinder = null;
5649 private boolean mFinished = true;
5650 private boolean mGotFirstWindow = false;
5651 private boolean mEventDispatching = true;
5652 private long mTimeToSwitch = 0;
5653 /* package */ boolean mWasFrozen = false;
Romain Guy06882f82009-06-10 13:36:04 -07005654
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005655 // Target of Motion events
5656 WindowState mMotionTarget;
Romain Guy06882f82009-06-10 13:36:04 -07005657
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005658 // Windows above the target who would like to receive an "outside"
5659 // touch event for any down events outside of them.
5660 WindowState mOutsideTouchTargets;
5661
5662 /**
5663 * Wait for the last event dispatch to complete, then find the next
5664 * target that should receive the given event and wait for that one
5665 * to be ready to receive it.
5666 */
5667 Object waitForNextEventTarget(KeyEvent nextKey, QueuedEvent qev,
5668 MotionEvent nextMotion, boolean isPointerEvent,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005669 boolean failIfTimeout, int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005670 long startTime = SystemClock.uptimeMillis();
5671 long keyDispatchingTimeout = 5 * 1000;
5672 long waitedFor = 0;
5673
5674 while (true) {
5675 // Figure out which window we care about. It is either the
5676 // last window we are waiting to have process the event or,
5677 // if none, then the next window we think the event should go
5678 // to. Note: we retrieve mLastWin outside of the lock, so
5679 // it may change before we lock. Thus we must check it again.
5680 WindowState targetWin = mLastWin;
5681 boolean targetIsNew = targetWin == null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08005682 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005683 TAG, "waitForLastKey: mFinished=" + mFinished +
5684 ", mLastWin=" + mLastWin);
5685 if (targetIsNew) {
5686 Object target = findTargetWindow(nextKey, qev, nextMotion,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005687 isPointerEvent, callingPid, callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005688 if (target == SKIP_TARGET_TOKEN) {
5689 // The user has pressed a special key, and we are
5690 // dropping all pending events before it.
Joe Onorato8a9b2202010-02-26 18:56:32 -08005691 if (DEBUG_INPUT) Slog.v(TAG, "Skipping: " + nextKey
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005692 + " " + nextMotion);
5693 return null;
5694 }
5695 if (target == CONSUMED_EVENT_TOKEN) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005696 if (DEBUG_INPUT) Slog.v(TAG, "Consumed: " + nextKey
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005697 + " " + nextMotion);
5698 return target;
5699 }
5700 targetWin = (WindowState)target;
5701 }
Romain Guy06882f82009-06-10 13:36:04 -07005702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005703 AppWindowToken targetApp = null;
Romain Guy06882f82009-06-10 13:36:04 -07005704
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005705 // Now: is it okay to send the next event to this window?
5706 synchronized (this) {
5707 // First: did we come here based on the last window not
5708 // being null, but it changed by the time we got here?
5709 // If so, try again.
5710 if (!targetIsNew && mLastWin == null) {
5711 continue;
5712 }
Romain Guy06882f82009-06-10 13:36:04 -07005713
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005714 // We never dispatch events if not finished with the
5715 // last one, or the display is frozen.
5716 if (mFinished && !mDisplayFrozen) {
5717 // If event dispatching is disabled, then we
5718 // just consume the events.
5719 if (!mEventDispatching) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005720 if (DEBUG_INPUT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005721 "Skipping event; dispatching disabled: "
5722 + nextKey + " " + nextMotion);
5723 return null;
5724 }
5725 if (targetWin != null) {
5726 // If this is a new target, and that target is not
5727 // paused or unresponsive, then all looks good to
5728 // handle the event.
5729 if (targetIsNew && !targetWin.mToken.paused) {
5730 return targetWin;
5731 }
Romain Guy06882f82009-06-10 13:36:04 -07005732
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005733 // If we didn't find a target window, and there is no
5734 // focused app window, then just eat the events.
5735 } else if (mFocusedApp == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005736 if (DEBUG_INPUT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005737 "Skipping event; no focused app: "
5738 + nextKey + " " + nextMotion);
5739 return null;
5740 }
5741 }
Romain Guy06882f82009-06-10 13:36:04 -07005742
Joe Onorato8a9b2202010-02-26 18:56:32 -08005743 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005744 TAG, "Waiting for last key in " + mLastBinder
5745 + " target=" + targetWin
5746 + " mFinished=" + mFinished
5747 + " mDisplayFrozen=" + mDisplayFrozen
5748 + " targetIsNew=" + targetIsNew
5749 + " paused="
5750 + (targetWin != null ? targetWin.mToken.paused : false)
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005751 + " mFocusedApp=" + mFocusedApp
5752 + " mCurrentFocus=" + mCurrentFocus);
Romain Guy06882f82009-06-10 13:36:04 -07005753
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005754 targetApp = targetWin != null
5755 ? targetWin.mAppToken : mFocusedApp;
Romain Guy06882f82009-06-10 13:36:04 -07005756
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005757 long curTimeout = keyDispatchingTimeout;
5758 if (mTimeToSwitch != 0) {
5759 long now = SystemClock.uptimeMillis();
5760 if (mTimeToSwitch <= now) {
5761 // If an app switch key has been pressed, and we have
5762 // waited too long for the current app to finish
5763 // processing keys, then wait no more!
Christopher Tate136b1f92010-02-11 17:51:24 -08005764 doFinishedKeyLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005765 continue;
5766 }
5767 long switchTimeout = mTimeToSwitch - now;
5768 if (curTimeout > switchTimeout) {
5769 curTimeout = switchTimeout;
5770 }
5771 }
Romain Guy06882f82009-06-10 13:36:04 -07005772
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005773 try {
5774 // after that continue
5775 // processing keys, so we don't get stuck.
Joe Onorato8a9b2202010-02-26 18:56:32 -08005776 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005777 TAG, "Waiting for key dispatch: " + curTimeout);
5778 wait(curTimeout);
Joe Onorato8a9b2202010-02-26 18:56:32 -08005779 if (DEBUG_INPUT) Slog.v(TAG, "Finished waiting @"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005780 + SystemClock.uptimeMillis() + " startTime="
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005781 + startTime + " switchTime=" + mTimeToSwitch
5782 + " target=" + targetWin + " mLW=" + mLastWin
5783 + " mLB=" + mLastBinder + " fin=" + mFinished
5784 + " mCurrentFocus=" + mCurrentFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005785 } catch (InterruptedException e) {
5786 }
5787 }
5788
5789 // If we were frozen during configuration change, restart the
5790 // timeout checks from now; otherwise look at whether we timed
5791 // out before awakening.
5792 if (mWasFrozen) {
5793 waitedFor = 0;
5794 mWasFrozen = false;
5795 } else {
5796 waitedFor = SystemClock.uptimeMillis() - startTime;
5797 }
5798
5799 if (waitedFor >= keyDispatchingTimeout && mTimeToSwitch == 0) {
5800 IApplicationToken at = null;
5801 synchronized (this) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005802 Slog.w(TAG, "Key dispatching timed out sending to " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005803 (targetWin != null ? targetWin.mAttrs.getTitle()
Ken Shirriff8200b202010-02-04 13:34:37 -08005804 : "<null>: no window ready for key dispatch"));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005805 // NOSHIP debugging
Joe Onorato8a9b2202010-02-26 18:56:32 -08005806 Slog.w(TAG, "Previous dispatch state: " + mDispatchState);
5807 Slog.w(TAG, "Current dispatch state: " +
Ken Shirriff8200b202010-02-04 13:34:37 -08005808 new DispatchState(nextKey, targetWin));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005809 // END NOSHIP
5810 //dump();
5811 if (targetWin != null) {
5812 at = targetWin.getAppToken();
5813 } else if (targetApp != null) {
5814 at = targetApp.appToken;
5815 }
5816 }
5817
5818 boolean abort = true;
5819 if (at != null) {
5820 try {
5821 long timeout = at.getKeyDispatchingTimeout();
5822 if (timeout > waitedFor) {
5823 // we did not wait the proper amount of time for this application.
5824 // set the timeout to be the real timeout and wait again.
5825 keyDispatchingTimeout = timeout - waitedFor;
5826 continue;
5827 } else {
5828 abort = at.keyDispatchingTimedOut();
5829 }
5830 } catch (RemoteException ex) {
5831 }
5832 }
5833
5834 synchronized (this) {
5835 if (abort && (mLastWin == targetWin || targetWin == null)) {
5836 mFinished = true;
Romain Guy06882f82009-06-10 13:36:04 -07005837 if (mLastWin != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005838 if (DEBUG_INPUT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005839 "Window " + mLastWin +
5840 " timed out on key input");
5841 if (mLastWin.mToken.paused) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005842 Slog.w(TAG, "Un-pausing dispatching to this window");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005843 mLastWin.mToken.paused = false;
5844 }
5845 }
5846 if (mMotionTarget == targetWin) {
5847 mMotionTarget = null;
5848 }
5849 mLastWin = null;
5850 mLastBinder = null;
5851 if (failIfTimeout || targetWin == null) {
5852 return null;
5853 }
5854 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005855 Slog.w(TAG, "Continuing to wait for key to be dispatched");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005856 startTime = SystemClock.uptimeMillis();
5857 }
5858 }
5859 }
5860 }
5861 }
Romain Guy06882f82009-06-10 13:36:04 -07005862
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005863 Object findTargetWindow(KeyEvent nextKey, QueuedEvent qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005864 MotionEvent nextMotion, boolean isPointerEvent,
5865 int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005866 mOutsideTouchTargets = null;
Romain Guy06882f82009-06-10 13:36:04 -07005867
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005868 if (nextKey != null) {
5869 // Find the target window for a normal key event.
5870 final int keycode = nextKey.getKeyCode();
5871 final int repeatCount = nextKey.getRepeatCount();
5872 final boolean down = nextKey.getAction() != KeyEvent.ACTION_UP;
5873 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(keycode);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005874
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005875 if (!dispatch) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005876 if (callingUid == 0 ||
5877 mContext.checkPermission(
5878 android.Manifest.permission.INJECT_EVENTS,
5879 callingPid, callingUid)
5880 == PackageManager.PERMISSION_GRANTED) {
5881 mPolicy.interceptKeyTi(null, keycode,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005882 nextKey.getMetaState(), down, repeatCount,
5883 nextKey.getFlags());
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005884 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08005885 Slog.w(TAG, "Event timeout during app switch: dropping "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005886 + nextKey);
5887 return SKIP_TARGET_TOKEN;
5888 }
Romain Guy06882f82009-06-10 13:36:04 -07005889
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005890 // System.out.println("##### [" + SystemClock.uptimeMillis() + "] WindowManagerService.dispatchKey(" + keycode + ", " + down + ", " + repeatCount + ")");
Romain Guy06882f82009-06-10 13:36:04 -07005891
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005892 WindowState focus = null;
5893 synchronized(mWindowMap) {
5894 focus = getFocusedWindowLocked();
5895 }
Romain Guy06882f82009-06-10 13:36:04 -07005896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005897 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
Romain Guy06882f82009-06-10 13:36:04 -07005898
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005899 if (callingUid == 0 ||
5900 (focus != null && callingUid == focus.mSession.mUid) ||
5901 mContext.checkPermission(
5902 android.Manifest.permission.INJECT_EVENTS,
5903 callingPid, callingUid)
5904 == PackageManager.PERMISSION_GRANTED) {
5905 if (mPolicy.interceptKeyTi(focus,
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005906 keycode, nextKey.getMetaState(), down, repeatCount,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005907 nextKey.getFlags())) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005908 return CONSUMED_EVENT_TOKEN;
5909 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005910 }
Romain Guy06882f82009-06-10 13:36:04 -07005911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005912 return focus;
Romain Guy06882f82009-06-10 13:36:04 -07005913
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005914 } else if (!isPointerEvent) {
5915 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(-1);
5916 if (!dispatch) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005917 Slog.w(TAG, "Event timeout during app switch: dropping trackball "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005918 + nextMotion);
5919 return SKIP_TARGET_TOKEN;
5920 }
Romain Guy06882f82009-06-10 13:36:04 -07005921
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005922 WindowState focus = null;
5923 synchronized(mWindowMap) {
5924 focus = getFocusedWindowLocked();
5925 }
Romain Guy06882f82009-06-10 13:36:04 -07005926
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005927 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
5928 return focus;
5929 }
Romain Guy06882f82009-06-10 13:36:04 -07005930
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005931 if (nextMotion == null) {
5932 return SKIP_TARGET_TOKEN;
5933 }
Romain Guy06882f82009-06-10 13:36:04 -07005934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005935 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(
5936 KeyEvent.KEYCODE_UNKNOWN);
5937 if (!dispatch) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005938 Slog.w(TAG, "Event timeout during app switch: dropping pointer "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005939 + nextMotion);
5940 return SKIP_TARGET_TOKEN;
5941 }
Romain Guy06882f82009-06-10 13:36:04 -07005942
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005943 // Find the target window for a pointer event.
5944 int action = nextMotion.getAction();
5945 final float xf = nextMotion.getX();
5946 final float yf = nextMotion.getY();
5947 final long eventTime = nextMotion.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07005948
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005949 final boolean screenWasOff = qev != null
5950 && (qev.flags&WindowManagerPolicy.FLAG_BRIGHT_HERE) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07005951
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005952 WindowState target = null;
Romain Guy06882f82009-06-10 13:36:04 -07005953
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005954 synchronized(mWindowMap) {
5955 synchronized (this) {
5956 if (action == MotionEvent.ACTION_DOWN) {
5957 if (mMotionTarget != null) {
5958 // this is weird, we got a pen down, but we thought it was
5959 // already down!
5960 // XXX: We should probably send an ACTION_UP to the current
5961 // target.
Joe Onorato8a9b2202010-02-26 18:56:32 -08005962 Slog.w(TAG, "Pointer down received while already down in: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005963 + mMotionTarget);
5964 mMotionTarget = null;
5965 }
Romain Guy06882f82009-06-10 13:36:04 -07005966
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005967 // ACTION_DOWN is special, because we need to lock next events to
5968 // the window we'll land onto.
5969 final int x = (int)xf;
5970 final int y = (int)yf;
Romain Guy06882f82009-06-10 13:36:04 -07005971
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005972 final ArrayList windows = mWindows;
5973 final int N = windows.size();
5974 WindowState topErrWindow = null;
5975 final Rect tmpRect = mTempRect;
5976 for (int i=N-1; i>=0; i--) {
5977 WindowState child = (WindowState)windows.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08005978 //Slog.i(TAG, "Checking dispatch to: " + child);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005979 final int flags = child.mAttrs.flags;
5980 if ((flags & WindowManager.LayoutParams.FLAG_SYSTEM_ERROR) != 0) {
5981 if (topErrWindow == null) {
5982 topErrWindow = child;
5983 }
5984 }
5985 if (!child.isVisibleLw()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005986 //Slog.i(TAG, "Not visible!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005987 continue;
5988 }
5989 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005990 //Slog.i(TAG, "Not touchable!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005991 if ((flags & WindowManager.LayoutParams
5992 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5993 child.mNextOutsideTouch = mOutsideTouchTargets;
5994 mOutsideTouchTargets = child;
5995 }
5996 continue;
5997 }
5998 tmpRect.set(child.mFrame);
5999 if (child.mTouchableInsets == ViewTreeObserver
6000 .InternalInsetsInfo.TOUCHABLE_INSETS_CONTENT) {
6001 // The touch is inside of the window if it is
6002 // inside the frame, AND the content part of that
6003 // frame that was given by the application.
6004 tmpRect.left += child.mGivenContentInsets.left;
6005 tmpRect.top += child.mGivenContentInsets.top;
6006 tmpRect.right -= child.mGivenContentInsets.right;
6007 tmpRect.bottom -= child.mGivenContentInsets.bottom;
6008 } else if (child.mTouchableInsets == ViewTreeObserver
6009 .InternalInsetsInfo.TOUCHABLE_INSETS_VISIBLE) {
6010 // The touch is inside of the window if it is
6011 // inside the frame, AND the visible part of that
6012 // frame that was given by the application.
6013 tmpRect.left += child.mGivenVisibleInsets.left;
6014 tmpRect.top += child.mGivenVisibleInsets.top;
6015 tmpRect.right -= child.mGivenVisibleInsets.right;
6016 tmpRect.bottom -= child.mGivenVisibleInsets.bottom;
6017 }
6018 final int touchFlags = flags &
6019 (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
6020 |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
6021 if (tmpRect.contains(x, y) || touchFlags == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006022 //Slog.i(TAG, "Using this target!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006023 if (!screenWasOff || (flags &
6024 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING) != 0) {
6025 mMotionTarget = child;
6026 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006027 //Slog.i(TAG, "Waking, skip!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006028 mMotionTarget = null;
6029 }
6030 break;
6031 }
Romain Guy06882f82009-06-10 13:36:04 -07006032
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006033 if ((flags & WindowManager.LayoutParams
6034 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
6035 child.mNextOutsideTouch = mOutsideTouchTargets;
6036 mOutsideTouchTargets = child;
Joe Onorato8a9b2202010-02-26 18:56:32 -08006037 //Slog.i(TAG, "Adding to outside target list: " + child);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006038 }
6039 }
6040
6041 // if there's an error window but it's not accepting
6042 // focus (typically because it is not yet visible) just
6043 // wait for it -- any other focused window may in fact
6044 // be in ANR state.
6045 if (topErrWindow != null && mMotionTarget != topErrWindow) {
6046 mMotionTarget = null;
6047 }
6048 }
Romain Guy06882f82009-06-10 13:36:04 -07006049
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006050 target = mMotionTarget;
6051 }
6052 }
Romain Guy06882f82009-06-10 13:36:04 -07006053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006054 wakeupIfNeeded(target, eventType(nextMotion));
Romain Guy06882f82009-06-10 13:36:04 -07006055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006056 // Pointer events are a little different -- if there isn't a
6057 // target found for any event, then just drop it.
6058 return target != null ? target : SKIP_TARGET_TOKEN;
6059 }
Romain Guy06882f82009-06-10 13:36:04 -07006060
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006061 boolean checkShouldDispatchKey(int keycode) {
6062 synchronized (this) {
6063 if (mPolicy.isAppSwitchKeyTqTiLwLi(keycode)) {
6064 mTimeToSwitch = 0;
6065 return true;
6066 }
6067 if (mTimeToSwitch != 0
6068 && mTimeToSwitch < SystemClock.uptimeMillis()) {
6069 return false;
6070 }
6071 return true;
6072 }
6073 }
Romain Guy06882f82009-06-10 13:36:04 -07006074
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006075 void bindTargetWindowLocked(WindowState win,
6076 int pendingWhat, QueuedEvent pendingMotion) {
6077 synchronized (this) {
6078 bindTargetWindowLockedLocked(win, pendingWhat, pendingMotion);
6079 }
6080 }
Romain Guy06882f82009-06-10 13:36:04 -07006081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006082 void bindTargetWindowLocked(WindowState win) {
6083 synchronized (this) {
6084 bindTargetWindowLockedLocked(win, RETURN_NOTHING, null);
6085 }
6086 }
6087
6088 void bindTargetWindowLockedLocked(WindowState win,
6089 int pendingWhat, QueuedEvent pendingMotion) {
6090 mLastWin = win;
6091 mLastBinder = win.mClient.asBinder();
6092 mFinished = false;
6093 if (pendingMotion != null) {
6094 final Session s = win.mSession;
6095 if (pendingWhat == RETURN_PENDING_POINTER) {
6096 releasePendingPointerLocked(s);
6097 s.mPendingPointerMove = pendingMotion;
6098 s.mPendingPointerWindow = win;
Joe Onorato8a9b2202010-02-26 18:56:32 -08006099 if (DEBUG_INPUT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006100 "bindTargetToWindow " + s.mPendingPointerMove);
6101 } else if (pendingWhat == RETURN_PENDING_TRACKBALL) {
6102 releasePendingTrackballLocked(s);
6103 s.mPendingTrackballMove = pendingMotion;
6104 s.mPendingTrackballWindow = win;
6105 }
6106 }
6107 }
Romain Guy06882f82009-06-10 13:36:04 -07006108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006109 void releasePendingPointerLocked(Session s) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006110 if (DEBUG_INPUT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006111 "releasePendingPointer " + s.mPendingPointerMove);
6112 if (s.mPendingPointerMove != null) {
6113 mQueue.recycleEvent(s.mPendingPointerMove);
6114 s.mPendingPointerMove = null;
6115 }
6116 }
Romain Guy06882f82009-06-10 13:36:04 -07006117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006118 void releasePendingTrackballLocked(Session s) {
6119 if (s.mPendingTrackballMove != null) {
6120 mQueue.recycleEvent(s.mPendingTrackballMove);
6121 s.mPendingTrackballMove = null;
6122 }
6123 }
Romain Guy06882f82009-06-10 13:36:04 -07006124
Maciej Białka26824b72010-03-05 08:40:09 +01006125 void releaseMotionTarget(WindowState win) {
6126 if (mMotionTarget == win) {
6127 mMotionTarget = null;
6128 }
6129 }
6130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006131 MotionEvent finishedKey(Session session, IWindow client, boolean force,
6132 int returnWhat) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006133 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006134 TAG, "finishedKey: client=" + client + ", force=" + force);
6135
6136 if (client == null) {
6137 return null;
6138 }
6139
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07006140 MotionEvent res = null;
6141 QueuedEvent qev = null;
6142 WindowState win = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006144 synchronized (this) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006145 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006146 TAG, "finishedKey: client=" + client.asBinder()
6147 + ", force=" + force + ", last=" + mLastBinder
6148 + " (token=" + (mLastWin != null ? mLastWin.mToken : null) + ")");
6149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006150 if (returnWhat == RETURN_PENDING_POINTER) {
6151 qev = session.mPendingPointerMove;
6152 win = session.mPendingPointerWindow;
6153 session.mPendingPointerMove = null;
6154 session.mPendingPointerWindow = null;
6155 } else if (returnWhat == RETURN_PENDING_TRACKBALL) {
6156 qev = session.mPendingTrackballMove;
6157 win = session.mPendingTrackballWindow;
6158 session.mPendingTrackballMove = null;
6159 session.mPendingTrackballWindow = null;
6160 }
Romain Guy06882f82009-06-10 13:36:04 -07006161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006162 if (mLastBinder == client.asBinder()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006163 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006164 TAG, "finishedKey: last paused="
6165 + ((mLastWin != null) ? mLastWin.mToken.paused : "null"));
6166 if (mLastWin != null && (!mLastWin.mToken.paused || force
6167 || !mEventDispatching)) {
Christopher Tate136b1f92010-02-11 17:51:24 -08006168 doFinishedKeyLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006169 } else {
6170 // Make sure to wake up anyone currently waiting to
6171 // dispatch a key, so they can re-evaluate their
6172 // current situation.
6173 mFinished = true;
6174 notifyAll();
6175 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006176 }
Romain Guy06882f82009-06-10 13:36:04 -07006177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006178 if (qev != null) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07006179 res = (MotionEvent)qev.event;
Joe Onorato8a9b2202010-02-26 18:56:32 -08006180 if (DEBUG_INPUT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006181 "Returning pending motion: " + res);
6182 mQueue.recycleEvent(qev);
6183 if (win != null && returnWhat == RETURN_PENDING_POINTER) {
6184 res.offsetLocation(-win.mFrame.left, -win.mFrame.top);
6185 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006186 }
Christopher Tate2624fbc2009-12-11 12:11:31 -08006187 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006188
Christopher Tate2624fbc2009-12-11 12:11:31 -08006189 if (res != null && returnWhat == RETURN_PENDING_POINTER) {
6190 synchronized (mWindowMap) {
Dianne Hackborn90d2db32010-02-11 22:19:06 -08006191 dispatchPointerElsewhereLocked(win, win, res, res.getEventTime(), false);
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07006192 }
6193 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006194
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07006195 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006196 }
6197
6198 void tickle() {
6199 synchronized (this) {
6200 notifyAll();
6201 }
6202 }
Romain Guy06882f82009-06-10 13:36:04 -07006203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006204 void handleNewWindowLocked(WindowState newWindow) {
6205 if (!newWindow.canReceiveKeys()) {
6206 return;
6207 }
6208 synchronized (this) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006209 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006210 TAG, "New key dispatch window: win="
6211 + newWindow.mClient.asBinder()
6212 + ", last=" + mLastBinder
6213 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
6214 + "), finished=" + mFinished + ", paused="
6215 + newWindow.mToken.paused);
6216
6217 // Displaying a window implicitly causes dispatching to
6218 // be unpaused. (This is to protect against bugs if someone
6219 // pauses dispatching but forgets to resume.)
6220 newWindow.mToken.paused = false;
6221
6222 mGotFirstWindow = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006223
6224 if ((newWindow.mAttrs.flags & FLAG_SYSTEM_ERROR) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006225 if (DEBUG_INPUT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006226 "New SYSTEM_ERROR window; resetting state");
6227 mLastWin = null;
6228 mLastBinder = null;
6229 mMotionTarget = null;
6230 mFinished = true;
6231 } else if (mLastWin != null) {
6232 // If the new window is above the window we are
6233 // waiting on, then stop waiting and let key dispatching
6234 // start on the new guy.
Joe Onorato8a9b2202010-02-26 18:56:32 -08006235 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006236 TAG, "Last win layer=" + mLastWin.mLayer
6237 + ", new win layer=" + newWindow.mLayer);
6238 if (newWindow.mLayer >= mLastWin.mLayer) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006239 // The new window is above the old; finish pending input to the last
6240 // window and start directing it to the new one.
6241 mLastWin.mToken.paused = false;
Christopher Tate136b1f92010-02-11 17:51:24 -08006242 doFinishedKeyLocked(false); // does a notifyAll()
6243 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006244 }
6245 }
6246
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006247 // Now that we've put a new window state in place, make the event waiter
6248 // take notice and retarget its attentions.
6249 notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006250 }
6251 }
6252
6253 void pauseDispatchingLocked(WindowToken token) {
6254 synchronized (this)
6255 {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006256 if (DEBUG_INPUT) Slog.v(TAG, "Pausing WindowToken " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006257 token.paused = true;
6258
6259 /*
6260 if (mLastWin != null && !mFinished && mLastWin.mBaseLayer <= layer) {
6261 mPaused = true;
6262 } else {
6263 if (mLastWin == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006264 Slog.i(TAG, "Key dispatching not paused: no last window.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006265 } else if (mFinished) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006266 Slog.i(TAG, "Key dispatching not paused: finished last key.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006267 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006268 Slog.i(TAG, "Key dispatching not paused: window in higher layer.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006269 }
6270 }
6271 */
6272 }
6273 }
6274
6275 void resumeDispatchingLocked(WindowToken token) {
6276 synchronized (this) {
6277 if (token.paused) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006278 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006279 TAG, "Resuming WindowToken " + token
6280 + ", last=" + mLastBinder
6281 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
6282 + "), finished=" + mFinished + ", paused="
6283 + token.paused);
6284 token.paused = false;
6285 if (mLastWin != null && mLastWin.mToken == token && mFinished) {
Christopher Tate136b1f92010-02-11 17:51:24 -08006286 doFinishedKeyLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006287 } else {
6288 notifyAll();
6289 }
6290 }
6291 }
6292 }
6293
6294 void setEventDispatchingLocked(boolean enabled) {
6295 synchronized (this) {
6296 mEventDispatching = enabled;
6297 notifyAll();
6298 }
6299 }
Romain Guy06882f82009-06-10 13:36:04 -07006300
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006301 void appSwitchComing() {
6302 synchronized (this) {
6303 // Don't wait for more than .5 seconds for app to finish
6304 // processing the pending events.
6305 long now = SystemClock.uptimeMillis() + 500;
Joe Onorato8a9b2202010-02-26 18:56:32 -08006306 if (DEBUG_INPUT) Slog.v(TAG, "appSwitchComing: " + now);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006307 if (mTimeToSwitch == 0 || now < mTimeToSwitch) {
6308 mTimeToSwitch = now;
6309 }
6310 notifyAll();
6311 }
6312 }
Romain Guy06882f82009-06-10 13:36:04 -07006313
Christopher Tate136b1f92010-02-11 17:51:24 -08006314 private final void doFinishedKeyLocked(boolean force) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006315 if (mLastWin != null) {
6316 releasePendingPointerLocked(mLastWin.mSession);
6317 releasePendingTrackballLocked(mLastWin.mSession);
6318 }
Romain Guy06882f82009-06-10 13:36:04 -07006319
Christopher Tate136b1f92010-02-11 17:51:24 -08006320 if (force || mLastWin == null || !mLastWin.mToken.paused
6321 || !mLastWin.isVisibleLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006322 // If the current window has been paused, we aren't -really-
6323 // finished... so let the waiters still wait.
6324 mLastWin = null;
6325 mLastBinder = null;
6326 }
6327 mFinished = true;
6328 notifyAll();
6329 }
6330 }
6331
6332 private class KeyQ extends KeyInputQueue
6333 implements KeyInputQueue.FilterCallback {
6334 PowerManager.WakeLock mHoldingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07006335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006336 KeyQ() {
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07006337 super(mContext, WindowManagerService.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006338 PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
6339 mHoldingScreen = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
6340 "KEEP_SCREEN_ON_FLAG");
6341 mHoldingScreen.setReferenceCounted(false);
6342 }
6343
6344 @Override
6345 boolean preprocessEvent(InputDevice device, RawInputEvent event) {
6346 if (mPolicy.preprocessInputEventTq(event)) {
6347 return true;
6348 }
Romain Guy06882f82009-06-10 13:36:04 -07006349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006350 switch (event.type) {
6351 case RawInputEvent.EV_KEY: {
6352 // XXX begin hack
6353 if (DEBUG) {
6354 if (event.keycode == KeyEvent.KEYCODE_G) {
6355 if (event.value != 0) {
6356 // G down
6357 mPolicy.screenTurnedOff(WindowManagerPolicy.OFF_BECAUSE_OF_USER);
6358 }
6359 return false;
6360 }
6361 if (event.keycode == KeyEvent.KEYCODE_D) {
6362 if (event.value != 0) {
6363 //dump();
6364 }
6365 return false;
6366 }
6367 }
6368 // XXX end hack
Romain Guy06882f82009-06-10 13:36:04 -07006369
Charles Mendis322591c2009-10-29 11:06:59 -07006370 boolean screenIsOff = !mPowerManager.isScreenOn();
6371 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006372 int actions = mPolicy.interceptKeyTq(event, !screenIsOff);
Romain Guy06882f82009-06-10 13:36:04 -07006373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006374 if ((actions & WindowManagerPolicy.ACTION_GO_TO_SLEEP) != 0) {
6375 mPowerManager.goToSleep(event.when);
6376 }
6377
6378 if (screenIsOff) {
6379 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6380 }
6381 if (screenIsDim) {
6382 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6383 }
6384 if ((actions & WindowManagerPolicy.ACTION_POKE_USER_ACTIVITY) != 0) {
6385 mPowerManager.userActivity(event.when, false,
Michael Chane96440f2009-05-06 10:27:36 -07006386 LocalPowerManager.BUTTON_EVENT, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006387 }
Romain Guy06882f82009-06-10 13:36:04 -07006388
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006389 if ((actions & WindowManagerPolicy.ACTION_PASS_TO_USER) != 0) {
6390 if (event.value != 0 && mPolicy.isAppSwitchKeyTqTiLwLi(event.keycode)) {
6391 filterQueue(this);
6392 mKeyWaiter.appSwitchComing();
6393 }
6394 return true;
6395 } else {
6396 return false;
6397 }
6398 }
Romain Guy06882f82009-06-10 13:36:04 -07006399
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006400 case RawInputEvent.EV_REL: {
Charles Mendis322591c2009-10-29 11:06:59 -07006401 boolean screenIsOff = !mPowerManager.isScreenOn();
6402 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006403 if (screenIsOff) {
6404 if (!mPolicy.isWakeRelMovementTq(event.deviceId,
6405 device.classes, event)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006406 //Slog.i(TAG, "dropping because screenIsOff and !isWakeKey");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006407 return false;
6408 }
6409 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6410 }
6411 if (screenIsDim) {
6412 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6413 }
6414 return true;
6415 }
Romain Guy06882f82009-06-10 13:36:04 -07006416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006417 case RawInputEvent.EV_ABS: {
Charles Mendis322591c2009-10-29 11:06:59 -07006418 boolean screenIsOff = !mPowerManager.isScreenOn();
6419 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006420 if (screenIsOff) {
6421 if (!mPolicy.isWakeAbsMovementTq(event.deviceId,
6422 device.classes, event)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006423 //Slog.i(TAG, "dropping because screenIsOff and !isWakeKey");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006424 return false;
6425 }
6426 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6427 }
6428 if (screenIsDim) {
6429 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6430 }
6431 return true;
6432 }
Romain Guy06882f82009-06-10 13:36:04 -07006433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006434 default:
6435 return true;
6436 }
6437 }
6438
6439 public int filterEvent(QueuedEvent ev) {
6440 switch (ev.classType) {
6441 case RawInputEvent.CLASS_KEYBOARD:
6442 KeyEvent ke = (KeyEvent)ev.event;
6443 if (mPolicy.isMovementKeyTi(ke.getKeyCode())) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006444 Slog.w(TAG, "Dropping movement key during app switch: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006445 + ke.getKeyCode() + ", action=" + ke.getAction());
6446 return FILTER_REMOVE;
6447 }
6448 return FILTER_ABORT;
6449 default:
6450 return FILTER_KEEP;
6451 }
6452 }
Romain Guy06882f82009-06-10 13:36:04 -07006453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006454 /**
6455 * Must be called with the main window manager lock held.
6456 */
6457 void setHoldScreenLocked(boolean holding) {
6458 boolean state = mHoldingScreen.isHeld();
6459 if (holding != state) {
6460 if (holding) {
6461 mHoldingScreen.acquire();
6462 } else {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006463 mPolicy.screenOnStoppedLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006464 mHoldingScreen.release();
6465 }
6466 }
6467 }
Michael Chan53071d62009-05-13 17:29:48 -07006468 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006469
6470 public boolean detectSafeMode() {
6471 mSafeMode = mPolicy.detectSafeMode();
6472 return mSafeMode;
6473 }
Romain Guy06882f82009-06-10 13:36:04 -07006474
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006475 public void systemReady() {
6476 mPolicy.systemReady();
6477 }
Romain Guy06882f82009-06-10 13:36:04 -07006478
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006479 private final class InputDispatcherThread extends Thread {
6480 // Time to wait when there is nothing to do: 9999 seconds.
6481 static final int LONG_WAIT=9999*1000;
6482
6483 public InputDispatcherThread() {
6484 super("InputDispatcher");
6485 }
Romain Guy06882f82009-06-10 13:36:04 -07006486
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006487 @Override
6488 public void run() {
6489 while (true) {
6490 try {
6491 process();
6492 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006493 Slog.e(TAG, "Exception in input dispatcher", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006494 }
6495 }
6496 }
Romain Guy06882f82009-06-10 13:36:04 -07006497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006498 private void process() {
6499 android.os.Process.setThreadPriority(
6500 android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -07006501
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006502 // The last key event we saw
6503 KeyEvent lastKey = null;
6504
6505 // Last keydown time for auto-repeating keys
6506 long lastKeyTime = SystemClock.uptimeMillis();
6507 long nextKeyTime = lastKeyTime+LONG_WAIT;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006508 long downTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006509
Romain Guy06882f82009-06-10 13:36:04 -07006510 // How many successive repeats we generated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006511 int keyRepeatCount = 0;
6512
6513 // Need to report that configuration has changed?
6514 boolean configChanged = false;
Romain Guy06882f82009-06-10 13:36:04 -07006515
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006516 while (true) {
6517 long curTime = SystemClock.uptimeMillis();
6518
Joe Onorato8a9b2202010-02-26 18:56:32 -08006519 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006520 TAG, "Waiting for next key: now=" + curTime
6521 + ", repeat @ " + nextKeyTime);
6522
6523 // Retrieve next event, waiting only as long as the next
6524 // repeat timeout. If the configuration has changed, then
6525 // don't wait at all -- we'll report the change as soon as
6526 // we have processed all events.
6527 QueuedEvent ev = mQueue.getEvent(
6528 (int)((!configChanged && curTime < nextKeyTime)
6529 ? (nextKeyTime-curTime) : 0));
6530
Joe Onorato8a9b2202010-02-26 18:56:32 -08006531 if (DEBUG_INPUT && ev != null) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006532 TAG, "Event: type=" + ev.classType + " data=" + ev.event);
6533
Michael Chan53071d62009-05-13 17:29:48 -07006534 if (MEASURE_LATENCY) {
6535 lt.sample("2 got event ", System.nanoTime() - ev.whenNano);
6536 }
6537
Mike Lockwood3d0ea722009-10-21 22:58:29 -04006538 if (lastKey != null && !mPolicy.allowKeyRepeat()) {
6539 // cancel key repeat at the request of the policy.
6540 lastKey = null;
6541 downTime = 0;
6542 lastKeyTime = curTime;
6543 nextKeyTime = curTime + LONG_WAIT;
6544 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006545 try {
6546 if (ev != null) {
Michael Chan53071d62009-05-13 17:29:48 -07006547 curTime = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006548 int eventType;
6549 if (ev.classType == RawInputEvent.CLASS_TOUCHSCREEN) {
6550 eventType = eventType((MotionEvent)ev.event);
6551 } else if (ev.classType == RawInputEvent.CLASS_KEYBOARD ||
6552 ev.classType == RawInputEvent.CLASS_TRACKBALL) {
6553 eventType = LocalPowerManager.BUTTON_EVENT;
6554 } else {
6555 eventType = LocalPowerManager.OTHER_EVENT;
6556 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006557 try {
Michael Chan53071d62009-05-13 17:29:48 -07006558 if ((curTime - mLastBatteryStatsCallTime)
Michael Chane96440f2009-05-06 10:27:36 -07006559 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
Michael Chan53071d62009-05-13 17:29:48 -07006560 mLastBatteryStatsCallTime = curTime;
Michael Chane96440f2009-05-06 10:27:36 -07006561 mBatteryStats.noteInputEvent();
6562 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006563 } catch (RemoteException e) {
6564 // Ignore
6565 }
Michael Chane10de972009-05-18 11:24:50 -07006566
Mike Lockwood5db42402009-11-30 14:51:51 -05006567 if (ev.classType == RawInputEvent.CLASS_CONFIGURATION_CHANGED) {
6568 // do not wake screen in this case
6569 } else if (eventType != TOUCH_EVENT
Michael Chane10de972009-05-18 11:24:50 -07006570 && eventType != LONG_TOUCH_EVENT
6571 && eventType != CHEEK_EVENT) {
6572 mPowerManager.userActivity(curTime, false,
6573 eventType, false);
6574 } else if (mLastTouchEventType != eventType
6575 || (curTime - mLastUserActivityCallTime)
6576 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
6577 mLastUserActivityCallTime = curTime;
6578 mLastTouchEventType = eventType;
6579 mPowerManager.userActivity(curTime, false,
6580 eventType, false);
6581 }
6582
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006583 switch (ev.classType) {
6584 case RawInputEvent.CLASS_KEYBOARD:
6585 KeyEvent ke = (KeyEvent)ev.event;
6586 if (ke.isDown()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006587 lastKeyTime = curTime;
Kristian Dreher133bfdf2010-02-23 08:50:58 +01006588 if (lastKey != null &&
6589 ke.getKeyCode() == lastKey.getKeyCode()) {
6590 keyRepeatCount++;
6591 // Arbitrary long timeout to block
6592 // repeating here since we know that
6593 // the device driver takes care of it.
6594 nextKeyTime = lastKeyTime + LONG_WAIT;
The Android Open Source Project2a9ae012010-05-12 12:33:35 -07006595 if (DEBUG_INPUT) Slog.v(
Kristian Dreher133bfdf2010-02-23 08:50:58 +01006596 TAG, "Received repeated key down");
6597 } else {
6598 downTime = curTime;
6599 keyRepeatCount = 0;
6600 nextKeyTime = lastKeyTime
6601 + ViewConfiguration.getLongPressTimeout();
The Android Open Source Project2a9ae012010-05-12 12:33:35 -07006602 if (DEBUG_INPUT) Slog.v(
Kristian Dreher133bfdf2010-02-23 08:50:58 +01006603 TAG, "Received key down: first repeat @ "
6604 + nextKeyTime);
6605 }
6606 lastKey = ke;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006607 } else {
6608 lastKey = null;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006609 downTime = 0;
Kristian Dreher133bfdf2010-02-23 08:50:58 +01006610 keyRepeatCount = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006611 // Arbitrary long timeout.
6612 lastKeyTime = curTime;
6613 nextKeyTime = curTime + LONG_WAIT;
Joe Onorato8a9b2202010-02-26 18:56:32 -08006614 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006615 TAG, "Received key up: ignore repeat @ "
6616 + nextKeyTime);
6617 }
Kristian Dreher133bfdf2010-02-23 08:50:58 +01006618 if (keyRepeatCount > 0) {
6619 dispatchKey(KeyEvent.changeTimeRepeat(ke,
6620 ke.getEventTime(), keyRepeatCount), 0, 0);
6621 } else {
6622 dispatchKey(ke, 0, 0);
6623 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006624 mQueue.recycleEvent(ev);
6625 break;
6626 case RawInputEvent.CLASS_TOUCHSCREEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08006627 //Slog.i(TAG, "Read next event " + ev);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006628 dispatchPointer(ev, (MotionEvent)ev.event, 0, 0);
6629 break;
6630 case RawInputEvent.CLASS_TRACKBALL:
6631 dispatchTrackball(ev, (MotionEvent)ev.event, 0, 0);
6632 break;
6633 case RawInputEvent.CLASS_CONFIGURATION_CHANGED:
6634 configChanged = true;
6635 break;
6636 default:
6637 mQueue.recycleEvent(ev);
6638 break;
6639 }
Romain Guy06882f82009-06-10 13:36:04 -07006640
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006641 } else if (configChanged) {
6642 configChanged = false;
6643 sendNewConfiguration();
Romain Guy06882f82009-06-10 13:36:04 -07006644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006645 } else if (lastKey != null) {
6646 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006647
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006648 // Timeout occurred while key was down. If it is at or
6649 // past the key repeat time, dispatch the repeat.
Joe Onorato8a9b2202010-02-26 18:56:32 -08006650 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006651 TAG, "Key timeout: repeat=" + nextKeyTime
6652 + ", now=" + curTime);
6653 if (curTime < nextKeyTime) {
6654 continue;
6655 }
Romain Guy06882f82009-06-10 13:36:04 -07006656
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006657 lastKeyTime = nextKeyTime;
6658 nextKeyTime = nextKeyTime + KEY_REPEAT_DELAY;
6659 keyRepeatCount++;
Joe Onorato8a9b2202010-02-26 18:56:32 -08006660 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006661 TAG, "Key repeat: count=" + keyRepeatCount
6662 + ", next @ " + nextKeyTime);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006663 KeyEvent newEvent;
6664 if (downTime != 0 && (downTime
6665 + ViewConfiguration.getLongPressTimeout())
6666 <= curTime) {
6667 newEvent = KeyEvent.changeTimeRepeat(lastKey,
6668 curTime, keyRepeatCount,
6669 lastKey.getFlags() | KeyEvent.FLAG_LONG_PRESS);
6670 downTime = 0;
6671 } else {
6672 newEvent = KeyEvent.changeTimeRepeat(lastKey,
6673 curTime, keyRepeatCount);
6674 }
6675 dispatchKey(newEvent, 0, 0);
Romain Guy06882f82009-06-10 13:36:04 -07006676
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006677 } else {
6678 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006679
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006680 lastKeyTime = curTime;
6681 nextKeyTime = curTime + LONG_WAIT;
6682 }
Romain Guy06882f82009-06-10 13:36:04 -07006683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006684 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006685 Slog.e(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006686 "Input thread received uncaught exception: " + e, e);
6687 }
6688 }
6689 }
6690 }
6691
6692 // -------------------------------------------------------------
6693 // Client Session State
6694 // -------------------------------------------------------------
6695
6696 private final class Session extends IWindowSession.Stub
6697 implements IBinder.DeathRecipient {
6698 final IInputMethodClient mClient;
6699 final IInputContext mInputContext;
6700 final int mUid;
6701 final int mPid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006702 final String mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006703 SurfaceSession mSurfaceSession;
6704 int mNumWindow = 0;
6705 boolean mClientDead = false;
Romain Guy06882f82009-06-10 13:36:04 -07006706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006707 /**
6708 * Current pointer move event being dispatched to client window... must
6709 * hold key lock to access.
6710 */
6711 QueuedEvent mPendingPointerMove;
6712 WindowState mPendingPointerWindow;
Romain Guy06882f82009-06-10 13:36:04 -07006713
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006714 /**
6715 * Current trackball move event being dispatched to client window... must
6716 * hold key lock to access.
6717 */
6718 QueuedEvent mPendingTrackballMove;
6719 WindowState mPendingTrackballWindow;
6720
6721 public Session(IInputMethodClient client, IInputContext inputContext) {
6722 mClient = client;
6723 mInputContext = inputContext;
6724 mUid = Binder.getCallingUid();
6725 mPid = Binder.getCallingPid();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006726 StringBuilder sb = new StringBuilder();
6727 sb.append("Session{");
6728 sb.append(Integer.toHexString(System.identityHashCode(this)));
6729 sb.append(" uid ");
6730 sb.append(mUid);
6731 sb.append("}");
6732 mStringName = sb.toString();
Romain Guy06882f82009-06-10 13:36:04 -07006733
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006734 synchronized (mWindowMap) {
6735 if (mInputMethodManager == null && mHaveInputMethods) {
6736 IBinder b = ServiceManager.getService(
6737 Context.INPUT_METHOD_SERVICE);
6738 mInputMethodManager = IInputMethodManager.Stub.asInterface(b);
6739 }
6740 }
6741 long ident = Binder.clearCallingIdentity();
6742 try {
6743 // Note: it is safe to call in to the input method manager
6744 // here because we are not holding our lock.
6745 if (mInputMethodManager != null) {
6746 mInputMethodManager.addClient(client, inputContext,
6747 mUid, mPid);
6748 } else {
6749 client.setUsingInputMethod(false);
6750 }
6751 client.asBinder().linkToDeath(this, 0);
6752 } catch (RemoteException e) {
6753 // The caller has died, so we can just forget about this.
6754 try {
6755 if (mInputMethodManager != null) {
6756 mInputMethodManager.removeClient(client);
6757 }
6758 } catch (RemoteException ee) {
6759 }
6760 } finally {
6761 Binder.restoreCallingIdentity(ident);
6762 }
6763 }
Romain Guy06882f82009-06-10 13:36:04 -07006764
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006765 @Override
6766 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
6767 throws RemoteException {
6768 try {
6769 return super.onTransact(code, data, reply, flags);
6770 } catch (RuntimeException e) {
6771 // Log all 'real' exceptions thrown to the caller
6772 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006773 Slog.e(TAG, "Window Session Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006774 }
6775 throw e;
6776 }
6777 }
6778
6779 public void binderDied() {
6780 // Note: it is safe to call in to the input method manager
6781 // here because we are not holding our lock.
6782 try {
6783 if (mInputMethodManager != null) {
6784 mInputMethodManager.removeClient(mClient);
6785 }
6786 } catch (RemoteException e) {
6787 }
6788 synchronized(mWindowMap) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07006789 mClient.asBinder().unlinkToDeath(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006790 mClientDead = true;
6791 killSessionLocked();
6792 }
6793 }
6794
6795 public int add(IWindow window, WindowManager.LayoutParams attrs,
6796 int viewVisibility, Rect outContentInsets) {
6797 return addWindow(this, window, attrs, viewVisibility, outContentInsets);
6798 }
Romain Guy06882f82009-06-10 13:36:04 -07006799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006800 public void remove(IWindow window) {
6801 removeWindow(this, window);
6802 }
Romain Guy06882f82009-06-10 13:36:04 -07006803
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006804 public int relayout(IWindow window, WindowManager.LayoutParams attrs,
6805 int requestedWidth, int requestedHeight, int viewFlags,
6806 boolean insetsPending, Rect outFrame, Rect outContentInsets,
Dianne Hackborn694f79b2010-03-17 19:44:59 -07006807 Rect outVisibleInsets, Configuration outConfig, Surface outSurface) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006808 return relayoutWindow(this, window, attrs,
6809 requestedWidth, requestedHeight, viewFlags, insetsPending,
Dianne Hackborn694f79b2010-03-17 19:44:59 -07006810 outFrame, outContentInsets, outVisibleInsets, outConfig, outSurface);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006811 }
Romain Guy06882f82009-06-10 13:36:04 -07006812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006813 public void setTransparentRegion(IWindow window, Region region) {
6814 setTransparentRegionWindow(this, window, region);
6815 }
Romain Guy06882f82009-06-10 13:36:04 -07006816
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006817 public void setInsets(IWindow window, int touchableInsets,
6818 Rect contentInsets, Rect visibleInsets) {
6819 setInsetsWindow(this, window, touchableInsets, contentInsets,
6820 visibleInsets);
6821 }
Romain Guy06882f82009-06-10 13:36:04 -07006822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006823 public void getDisplayFrame(IWindow window, Rect outDisplayFrame) {
6824 getWindowDisplayFrame(this, window, outDisplayFrame);
6825 }
Romain Guy06882f82009-06-10 13:36:04 -07006826
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006827 public void finishDrawing(IWindow window) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006828 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006829 TAG, "IWindow finishDrawing called for " + window);
6830 finishDrawingWindow(this, window);
6831 }
6832
6833 public void finishKey(IWindow window) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006834 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006835 TAG, "IWindow finishKey called for " + window);
6836 mKeyWaiter.finishedKey(this, window, false,
6837 KeyWaiter.RETURN_NOTHING);
6838 }
6839
6840 public MotionEvent getPendingPointerMove(IWindow window) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006841 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006842 TAG, "IWindow getPendingMotionEvent called for " + window);
6843 return mKeyWaiter.finishedKey(this, window, false,
6844 KeyWaiter.RETURN_PENDING_POINTER);
6845 }
Romain Guy06882f82009-06-10 13:36:04 -07006846
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006847 public MotionEvent getPendingTrackballMove(IWindow window) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006848 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006849 TAG, "IWindow getPendingMotionEvent called for " + window);
6850 return mKeyWaiter.finishedKey(this, window, false,
6851 KeyWaiter.RETURN_PENDING_TRACKBALL);
6852 }
6853
6854 public void setInTouchMode(boolean mode) {
6855 synchronized(mWindowMap) {
6856 mInTouchMode = mode;
6857 }
6858 }
6859
6860 public boolean getInTouchMode() {
6861 synchronized(mWindowMap) {
6862 return mInTouchMode;
6863 }
6864 }
6865
6866 public boolean performHapticFeedback(IWindow window, int effectId,
6867 boolean always) {
6868 synchronized(mWindowMap) {
6869 long ident = Binder.clearCallingIdentity();
6870 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006871 return mPolicy.performHapticFeedbackLw(
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006872 windowForClientLocked(this, window, true),
6873 effectId, always);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006874 } finally {
6875 Binder.restoreCallingIdentity(ident);
6876 }
6877 }
6878 }
Romain Guy06882f82009-06-10 13:36:04 -07006879
Marco Nelissenbf6956b2009-11-09 15:21:13 -08006880 public void setWallpaperPosition(IBinder window, float x, float y, float xStep, float yStep) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006881 synchronized(mWindowMap) {
6882 long ident = Binder.clearCallingIdentity();
6883 try {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006884 setWindowWallpaperPositionLocked(
6885 windowForClientLocked(this, window, true),
Marco Nelissenbf6956b2009-11-09 15:21:13 -08006886 x, y, xStep, yStep);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006887 } finally {
6888 Binder.restoreCallingIdentity(ident);
6889 }
6890 }
6891 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006892
Dianne Hackborn19382ac2009-09-11 21:13:37 -07006893 public void wallpaperOffsetsComplete(IBinder window) {
6894 WindowManagerService.this.wallpaperOffsetsComplete(window);
6895 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006896
Dianne Hackborn75804932009-10-20 20:15:20 -07006897 public Bundle sendWallpaperCommand(IBinder window, String action, int x, int y,
6898 int z, Bundle extras, boolean sync) {
6899 synchronized(mWindowMap) {
6900 long ident = Binder.clearCallingIdentity();
6901 try {
6902 return sendWindowWallpaperCommandLocked(
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006903 windowForClientLocked(this, window, true),
Dianne Hackborn75804932009-10-20 20:15:20 -07006904 action, x, y, z, extras, sync);
6905 } finally {
6906 Binder.restoreCallingIdentity(ident);
6907 }
6908 }
6909 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006910
Dianne Hackborn75804932009-10-20 20:15:20 -07006911 public void wallpaperCommandComplete(IBinder window, Bundle result) {
6912 WindowManagerService.this.wallpaperCommandComplete(window, result);
6913 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006914
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006915 void windowAddedLocked() {
6916 if (mSurfaceSession == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006917 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006918 TAG, "First window added to " + this + ", creating SurfaceSession");
6919 mSurfaceSession = new SurfaceSession();
Joe Onorato8a9b2202010-02-26 18:56:32 -08006920 if (SHOW_TRANSACTIONS) Slog.i(
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006921 TAG, " NEW SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006922 mSessions.add(this);
6923 }
6924 mNumWindow++;
6925 }
6926
6927 void windowRemovedLocked() {
6928 mNumWindow--;
6929 killSessionLocked();
6930 }
Romain Guy06882f82009-06-10 13:36:04 -07006931
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006932 void killSessionLocked() {
6933 if (mNumWindow <= 0 && mClientDead) {
6934 mSessions.remove(this);
6935 if (mSurfaceSession != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006936 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006937 TAG, "Last window removed from " + this
6938 + ", destroying " + mSurfaceSession);
Joe Onorato8a9b2202010-02-26 18:56:32 -08006939 if (SHOW_TRANSACTIONS) Slog.i(
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006940 TAG, " KILL SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006941 try {
6942 mSurfaceSession.kill();
6943 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006944 Slog.w(TAG, "Exception thrown when killing surface session "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006945 + mSurfaceSession + " in session " + this
6946 + ": " + e.toString());
6947 }
6948 mSurfaceSession = null;
6949 }
6950 }
6951 }
Romain Guy06882f82009-06-10 13:36:04 -07006952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006953 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006954 pw.print(prefix); pw.print("mNumWindow="); pw.print(mNumWindow);
6955 pw.print(" mClientDead="); pw.print(mClientDead);
6956 pw.print(" mSurfaceSession="); pw.println(mSurfaceSession);
6957 if (mPendingPointerWindow != null || mPendingPointerMove != null) {
6958 pw.print(prefix);
6959 pw.print("mPendingPointerWindow="); pw.print(mPendingPointerWindow);
6960 pw.print(" mPendingPointerMove="); pw.println(mPendingPointerMove);
6961 }
6962 if (mPendingTrackballWindow != null || mPendingTrackballMove != null) {
6963 pw.print(prefix);
6964 pw.print("mPendingTrackballWindow="); pw.print(mPendingTrackballWindow);
6965 pw.print(" mPendingTrackballMove="); pw.println(mPendingTrackballMove);
6966 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006967 }
6968
6969 @Override
6970 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006971 return mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006972 }
6973 }
6974
6975 // -------------------------------------------------------------
6976 // Client Window State
6977 // -------------------------------------------------------------
6978
6979 private final class WindowState implements WindowManagerPolicy.WindowState {
6980 final Session mSession;
6981 final IWindow mClient;
6982 WindowToken mToken;
The Android Open Source Project10592532009-03-18 17:39:46 -07006983 WindowToken mRootToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006984 AppWindowToken mAppToken;
6985 AppWindowToken mTargetAppToken;
6986 final WindowManager.LayoutParams mAttrs = new WindowManager.LayoutParams();
6987 final DeathRecipient mDeathRecipient;
6988 final WindowState mAttachedWindow;
6989 final ArrayList mChildWindows = new ArrayList();
6990 final int mBaseLayer;
6991 final int mSubLayer;
6992 final boolean mLayoutAttached;
6993 final boolean mIsImWindow;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006994 final boolean mIsWallpaper;
6995 final boolean mIsFloatingLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006996 int mViewVisibility;
6997 boolean mPolicyVisibility = true;
6998 boolean mPolicyVisibilityAfterAnim = true;
6999 boolean mAppFreezing;
7000 Surface mSurface;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007001 boolean mReportDestroySurface;
7002 boolean mSurfacePendingDestroy;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007003 boolean mAttachedHidden; // is our parent window hidden?
7004 boolean mLastHidden; // was this window last hidden?
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007005 boolean mWallpaperVisible; // for wallpaper, what was last vis report?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007006 int mRequestedWidth;
7007 int mRequestedHeight;
7008 int mLastRequestedWidth;
7009 int mLastRequestedHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007010 int mLayer;
7011 int mAnimLayer;
7012 int mLastLayer;
7013 boolean mHaveFrame;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007014 boolean mObscured;
Dianne Hackborn93e462b2009-09-15 22:50:40 -07007015 boolean mTurnOnScreen;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007016
7017 WindowState mNextOutsideTouch;
Romain Guy06882f82009-06-10 13:36:04 -07007018
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007019 int mLayoutSeq = -1;
7020
7021 Configuration mConfiguration = null;
7022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007023 // Actual frame shown on-screen (may be modified by animation)
7024 final Rect mShownFrame = new Rect();
7025 final Rect mLastShownFrame = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07007026
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007027 /**
Dianne Hackbornac3587d2010-03-11 11:12:11 -08007028 * Set when we have changed the size of the surface, to know that
7029 * we must tell them application to resize (and thus redraw itself).
7030 */
7031 boolean mSurfaceResized;
7032
7033 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007034 * Insets that determine the actually visible area
7035 */
7036 final Rect mVisibleInsets = new Rect();
7037 final Rect mLastVisibleInsets = new Rect();
7038 boolean mVisibleInsetsChanged;
7039
7040 /**
7041 * Insets that are covered by system windows
7042 */
7043 final Rect mContentInsets = new Rect();
7044 final Rect mLastContentInsets = new Rect();
7045 boolean mContentInsetsChanged;
7046
7047 /**
7048 * Set to true if we are waiting for this window to receive its
7049 * given internal insets before laying out other windows based on it.
7050 */
7051 boolean mGivenInsetsPending;
Romain Guy06882f82009-06-10 13:36:04 -07007052
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007053 /**
7054 * These are the content insets that were given during layout for
7055 * this window, to be applied to windows behind it.
7056 */
7057 final Rect mGivenContentInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07007058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007059 /**
7060 * These are the visible insets that were given during layout for
7061 * this window, to be applied to windows behind it.
7062 */
7063 final Rect mGivenVisibleInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07007064
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007065 /**
7066 * Flag indicating whether the touchable region should be adjusted by
7067 * the visible insets; if false the area outside the visible insets is
7068 * NOT touchable, so we must use those to adjust the frame during hit
7069 * tests.
7070 */
7071 int mTouchableInsets = ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME;
Romain Guy06882f82009-06-10 13:36:04 -07007072
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007073 // Current transformation being applied.
7074 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
7075 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
7076 float mHScale=1, mVScale=1;
7077 float mLastHScale=1, mLastVScale=1;
7078 final Matrix mTmpMatrix = new Matrix();
7079
7080 // "Real" frame that the application sees.
7081 final Rect mFrame = new Rect();
7082 final Rect mLastFrame = new Rect();
7083
7084 final Rect mContainingFrame = new Rect();
7085 final Rect mDisplayFrame = new Rect();
7086 final Rect mContentFrame = new Rect();
7087 final Rect mVisibleFrame = new Rect();
7088
7089 float mShownAlpha = 1;
7090 float mAlpha = 1;
7091 float mLastAlpha = 1;
7092
7093 // Set to true if, when the window gets displayed, it should perform
7094 // an enter animation.
7095 boolean mEnterAnimationPending;
7096
7097 // Currently running animation.
7098 boolean mAnimating;
7099 boolean mLocalAnimating;
7100 Animation mAnimation;
7101 boolean mAnimationIsEntrance;
7102 boolean mHasTransformation;
7103 boolean mHasLocalTransformation;
7104 final Transformation mTransformation = new Transformation();
7105
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07007106 // If a window showing a wallpaper: the requested offset for the
7107 // wallpaper; if a wallpaper window: the currently applied offset.
7108 float mWallpaperX = -1;
7109 float mWallpaperY = -1;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08007110
7111 // If a window showing a wallpaper: what fraction of the offset
7112 // range corresponds to a full virtual screen.
7113 float mWallpaperXStep = -1;
7114 float mWallpaperYStep = -1;
7115
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07007116 // Wallpaper windows: pixels offset based on above variables.
7117 int mXOffset;
7118 int mYOffset;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007120 // This is set after IWindowSession.relayout() has been called at
7121 // least once for the window. It allows us to detect the situation
7122 // where we don't yet have a surface, but should have one soon, so
7123 // we can give the window focus before waiting for the relayout.
7124 boolean mRelayoutCalled;
Romain Guy06882f82009-06-10 13:36:04 -07007125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007126 // This is set after the Surface has been created but before the
7127 // window has been drawn. During this time the surface is hidden.
7128 boolean mDrawPending;
7129
7130 // This is set after the window has finished drawing for the first
7131 // time but before its surface is shown. The surface will be
7132 // displayed when the next layout is run.
7133 boolean mCommitDrawPending;
7134
7135 // This is set during the time after the window's drawing has been
7136 // committed, and before its surface is actually shown. It is used
7137 // to delay showing the surface until all windows in a token are ready
7138 // to be shown.
7139 boolean mReadyToShow;
Romain Guy06882f82009-06-10 13:36:04 -07007140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007141 // Set when the window has been shown in the screen the first time.
7142 boolean mHasDrawn;
7143
7144 // Currently running an exit animation?
7145 boolean mExiting;
7146
7147 // Currently on the mDestroySurface list?
7148 boolean mDestroying;
Romain Guy06882f82009-06-10 13:36:04 -07007149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007150 // Completely remove from window manager after exit animation?
7151 boolean mRemoveOnExit;
7152
7153 // Set when the orientation is changing and this window has not yet
7154 // been updated for the new orientation.
7155 boolean mOrientationChanging;
Romain Guy06882f82009-06-10 13:36:04 -07007156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007157 // Is this window now (or just being) removed?
7158 boolean mRemoved;
Romain Guy06882f82009-06-10 13:36:04 -07007159
Dianne Hackborn16064f92010-03-25 00:47:24 -07007160 // For debugging, this is the last information given to the surface flinger.
7161 boolean mSurfaceShown;
7162 int mSurfaceX, mSurfaceY, mSurfaceW, mSurfaceH;
7163 int mSurfaceLayer;
7164 float mSurfaceAlpha;
7165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007166 WindowState(Session s, IWindow c, WindowToken token,
7167 WindowState attachedWindow, WindowManager.LayoutParams a,
7168 int viewVisibility) {
7169 mSession = s;
7170 mClient = c;
7171 mToken = token;
7172 mAttrs.copyFrom(a);
7173 mViewVisibility = viewVisibility;
7174 DeathRecipient deathRecipient = new DeathRecipient();
7175 mAlpha = a.alpha;
Joe Onorato8a9b2202010-02-26 18:56:32 -08007176 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007177 TAG, "Window " + this + " client=" + c.asBinder()
7178 + " token=" + token + " (" + mAttrs.token + ")");
7179 try {
7180 c.asBinder().linkToDeath(deathRecipient, 0);
7181 } catch (RemoteException e) {
7182 mDeathRecipient = null;
7183 mAttachedWindow = null;
7184 mLayoutAttached = false;
7185 mIsImWindow = false;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007186 mIsWallpaper = false;
7187 mIsFloatingLayer = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007188 mBaseLayer = 0;
7189 mSubLayer = 0;
7190 return;
7191 }
7192 mDeathRecipient = deathRecipient;
Romain Guy06882f82009-06-10 13:36:04 -07007193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007194 if ((mAttrs.type >= FIRST_SUB_WINDOW &&
7195 mAttrs.type <= LAST_SUB_WINDOW)) {
7196 // The multiplier here is to reserve space for multiple
7197 // windows in the same type layer.
7198 mBaseLayer = mPolicy.windowTypeToLayerLw(
7199 attachedWindow.mAttrs.type) * TYPE_LAYER_MULTIPLIER
7200 + TYPE_LAYER_OFFSET;
7201 mSubLayer = mPolicy.subWindowTypeToLayerLw(a.type);
7202 mAttachedWindow = attachedWindow;
7203 mAttachedWindow.mChildWindows.add(this);
7204 mLayoutAttached = mAttrs.type !=
7205 WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
7206 mIsImWindow = attachedWindow.mAttrs.type == TYPE_INPUT_METHOD
7207 || attachedWindow.mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007208 mIsWallpaper = attachedWindow.mAttrs.type == TYPE_WALLPAPER;
7209 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007210 } else {
7211 // The multiplier here is to reserve space for multiple
7212 // windows in the same type layer.
7213 mBaseLayer = mPolicy.windowTypeToLayerLw(a.type)
7214 * TYPE_LAYER_MULTIPLIER
7215 + TYPE_LAYER_OFFSET;
7216 mSubLayer = 0;
7217 mAttachedWindow = null;
7218 mLayoutAttached = false;
7219 mIsImWindow = mAttrs.type == TYPE_INPUT_METHOD
7220 || mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007221 mIsWallpaper = mAttrs.type == TYPE_WALLPAPER;
7222 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007223 }
7224
7225 WindowState appWin = this;
7226 while (appWin.mAttachedWindow != null) {
7227 appWin = mAttachedWindow;
7228 }
7229 WindowToken appToken = appWin.mToken;
7230 while (appToken.appWindowToken == null) {
7231 WindowToken parent = mTokenMap.get(appToken.token);
7232 if (parent == null || appToken == parent) {
7233 break;
7234 }
7235 appToken = parent;
7236 }
The Android Open Source Project10592532009-03-18 17:39:46 -07007237 mRootToken = appToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007238 mAppToken = appToken.appWindowToken;
7239
7240 mSurface = null;
7241 mRequestedWidth = 0;
7242 mRequestedHeight = 0;
7243 mLastRequestedWidth = 0;
7244 mLastRequestedHeight = 0;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007245 mXOffset = 0;
7246 mYOffset = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007247 mLayer = 0;
7248 mAnimLayer = 0;
7249 mLastLayer = 0;
7250 }
7251
7252 void attach() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007253 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007254 TAG, "Attaching " + this + " token=" + mToken
7255 + ", list=" + mToken.windows);
7256 mSession.windowAddedLocked();
7257 }
7258
7259 public void computeFrameLw(Rect pf, Rect df, Rect cf, Rect vf) {
7260 mHaveFrame = true;
7261
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007262 final Rect container = mContainingFrame;
7263 container.set(pf);
7264
7265 final Rect display = mDisplayFrame;
7266 display.set(df);
7267
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007268 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007269 container.intersect(mCompatibleScreenFrame);
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007270 if ((mAttrs.flags & FLAG_LAYOUT_NO_LIMITS) == 0) {
7271 display.intersect(mCompatibleScreenFrame);
7272 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007273 }
7274
7275 final int pw = container.right - container.left;
7276 final int ph = container.bottom - container.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007277
7278 int w,h;
7279 if ((mAttrs.flags & mAttrs.FLAG_SCALED) != 0) {
7280 w = mAttrs.width < 0 ? pw : mAttrs.width;
7281 h = mAttrs.height< 0 ? ph : mAttrs.height;
7282 } else {
Romain Guy980a9382010-01-08 15:06:28 -08007283 w = mAttrs.width == mAttrs.MATCH_PARENT ? pw : mRequestedWidth;
7284 h = mAttrs.height== mAttrs.MATCH_PARENT ? ph : mRequestedHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007285 }
Romain Guy06882f82009-06-10 13:36:04 -07007286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007287 final Rect content = mContentFrame;
7288 content.set(cf);
Romain Guy06882f82009-06-10 13:36:04 -07007289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007290 final Rect visible = mVisibleFrame;
7291 visible.set(vf);
Romain Guy06882f82009-06-10 13:36:04 -07007292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007293 final Rect frame = mFrame;
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007294 final int fw = frame.width();
7295 final int fh = frame.height();
Romain Guy06882f82009-06-10 13:36:04 -07007296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007297 //System.out.println("In: w=" + w + " h=" + h + " container=" +
7298 // container + " x=" + mAttrs.x + " y=" + mAttrs.y);
7299
7300 Gravity.apply(mAttrs.gravity, w, h, container,
7301 (int) (mAttrs.x + mAttrs.horizontalMargin * pw),
7302 (int) (mAttrs.y + mAttrs.verticalMargin * ph), frame);
7303
7304 //System.out.println("Out: " + mFrame);
7305
7306 // Now make sure the window fits in the overall display.
7307 Gravity.applyDisplay(mAttrs.gravity, df, frame);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007309 // Make sure the content and visible frames are inside of the
7310 // final window frame.
7311 if (content.left < frame.left) content.left = frame.left;
7312 if (content.top < frame.top) content.top = frame.top;
7313 if (content.right > frame.right) content.right = frame.right;
7314 if (content.bottom > frame.bottom) content.bottom = frame.bottom;
7315 if (visible.left < frame.left) visible.left = frame.left;
7316 if (visible.top < frame.top) visible.top = frame.top;
7317 if (visible.right > frame.right) visible.right = frame.right;
7318 if (visible.bottom > frame.bottom) visible.bottom = frame.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007320 final Rect contentInsets = mContentInsets;
7321 contentInsets.left = content.left-frame.left;
7322 contentInsets.top = content.top-frame.top;
7323 contentInsets.right = frame.right-content.right;
7324 contentInsets.bottom = frame.bottom-content.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007326 final Rect visibleInsets = mVisibleInsets;
7327 visibleInsets.left = visible.left-frame.left;
7328 visibleInsets.top = visible.top-frame.top;
7329 visibleInsets.right = frame.right-visible.right;
7330 visibleInsets.bottom = frame.bottom-visible.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007331
Dianne Hackborn284ac932009-08-28 10:34:25 -07007332 if (mIsWallpaper && (fw != frame.width() || fh != frame.height())) {
7333 updateWallpaperOffsetLocked(this, mDisplay.getWidth(),
Dianne Hackborn19382ac2009-09-11 21:13:37 -07007334 mDisplay.getHeight(), false);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007335 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007337 if (localLOGV) {
7338 //if ("com.google.android.youtube".equals(mAttrs.packageName)
7339 // && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007340 Slog.v(TAG, "Resolving (mRequestedWidth="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007341 + mRequestedWidth + ", mRequestedheight="
7342 + mRequestedHeight + ") to" + " (pw=" + pw + ", ph=" + ph
7343 + "): frame=" + mFrame.toShortString()
7344 + " ci=" + contentInsets.toShortString()
7345 + " vi=" + visibleInsets.toShortString());
7346 //}
7347 }
7348 }
Romain Guy06882f82009-06-10 13:36:04 -07007349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007350 public Rect getFrameLw() {
7351 return mFrame;
7352 }
7353
7354 public Rect getShownFrameLw() {
7355 return mShownFrame;
7356 }
7357
7358 public Rect getDisplayFrameLw() {
7359 return mDisplayFrame;
7360 }
7361
7362 public Rect getContentFrameLw() {
7363 return mContentFrame;
7364 }
7365
7366 public Rect getVisibleFrameLw() {
7367 return mVisibleFrame;
7368 }
7369
7370 public boolean getGivenInsetsPendingLw() {
7371 return mGivenInsetsPending;
7372 }
7373
7374 public Rect getGivenContentInsetsLw() {
7375 return mGivenContentInsets;
7376 }
Romain Guy06882f82009-06-10 13:36:04 -07007377
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007378 public Rect getGivenVisibleInsetsLw() {
7379 return mGivenVisibleInsets;
7380 }
Romain Guy06882f82009-06-10 13:36:04 -07007381
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007382 public WindowManager.LayoutParams getAttrs() {
7383 return mAttrs;
7384 }
7385
7386 public int getSurfaceLayer() {
7387 return mLayer;
7388 }
Romain Guy06882f82009-06-10 13:36:04 -07007389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007390 public IApplicationToken getAppToken() {
7391 return mAppToken != null ? mAppToken.appToken : null;
7392 }
7393
7394 public boolean hasAppShownWindows() {
7395 return mAppToken != null ? mAppToken.firstWindowDrawn : false;
7396 }
7397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007398 public void setAnimation(Animation anim) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007399 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007400 TAG, "Setting animation in " + this + ": " + anim);
7401 mAnimating = false;
7402 mLocalAnimating = false;
7403 mAnimation = anim;
7404 mAnimation.restrictDuration(MAX_ANIMATION_DURATION);
7405 mAnimation.scaleCurrentDuration(mWindowAnimationScale);
7406 }
7407
7408 public void clearAnimation() {
7409 if (mAnimation != null) {
7410 mAnimating = true;
7411 mLocalAnimating = false;
7412 mAnimation = null;
7413 }
7414 }
Romain Guy06882f82009-06-10 13:36:04 -07007415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007416 Surface createSurfaceLocked() {
7417 if (mSurface == null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007418 mReportDestroySurface = false;
7419 mSurfacePendingDestroy = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007420 mDrawPending = true;
7421 mCommitDrawPending = false;
7422 mReadyToShow = false;
7423 if (mAppToken != null) {
7424 mAppToken.allDrawn = false;
7425 }
7426
7427 int flags = 0;
Mathias Agopian317a6282009-08-13 17:29:02 -07007428 if (mAttrs.memoryType == MEMORY_TYPE_PUSH_BUFFERS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007429 flags |= Surface.PUSH_BUFFERS;
7430 }
7431
7432 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
7433 flags |= Surface.SECURE;
7434 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08007435 if (DEBUG_VISIBILITY) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007436 TAG, "Creating surface in session "
7437 + mSession.mSurfaceSession + " window " + this
7438 + " w=" + mFrame.width()
7439 + " h=" + mFrame.height() + " format="
7440 + mAttrs.format + " flags=" + flags);
7441
7442 int w = mFrame.width();
7443 int h = mFrame.height();
7444 if ((mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
7445 // for a scaled surface, we always want the requested
7446 // size.
7447 w = mRequestedWidth;
7448 h = mRequestedHeight;
7449 }
7450
Romain Guy9825ec62009-10-01 00:58:09 -07007451 // Something is wrong and SurfaceFlinger will not like this,
7452 // try to revert to sane values
7453 if (w <= 0) w = 1;
7454 if (h <= 0) h = 1;
7455
Dianne Hackborn16064f92010-03-25 00:47:24 -07007456 mSurfaceShown = false;
7457 mSurfaceLayer = 0;
7458 mSurfaceAlpha = 1;
7459 mSurfaceX = 0;
7460 mSurfaceY = 0;
7461 mSurfaceW = w;
7462 mSurfaceH = h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007463 try {
7464 mSurface = new Surface(
Romain Guy06882f82009-06-10 13:36:04 -07007465 mSession.mSurfaceSession, mSession.mPid,
Mathias Agopian5d26c1e2010-03-01 16:09:43 -08007466 mAttrs.getTitle().toString(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007467 0, w, h, mAttrs.format, flags);
Joe Onorato8a9b2202010-02-26 18:56:32 -08007468 if (SHOW_TRANSACTIONS) Slog.i(TAG, " CREATE SURFACE "
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007469 + mSurface + " IN SESSION "
7470 + mSession.mSurfaceSession
7471 + ": pid=" + mSession.mPid + " format="
7472 + mAttrs.format + " flags=0x"
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007473 + Integer.toHexString(flags)
7474 + " / " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007475 } catch (Surface.OutOfResourcesException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007476 Slog.w(TAG, "OutOfResourcesException creating surface");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007477 reclaimSomeSurfaceMemoryLocked(this, "create");
7478 return null;
7479 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007480 Slog.e(TAG, "Exception creating surface", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007481 return null;
7482 }
Romain Guy06882f82009-06-10 13:36:04 -07007483
Joe Onorato8a9b2202010-02-26 18:56:32 -08007484 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007485 TAG, "Got surface: " + mSurface
7486 + ", set left=" + mFrame.left + " top=" + mFrame.top
7487 + ", animLayer=" + mAnimLayer);
7488 if (SHOW_TRANSACTIONS) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007489 Slog.i(TAG, ">>> OPEN TRANSACTION");
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007490 if (SHOW_TRANSACTIONS) logSurface(this,
7491 "CREATE pos=(" + mFrame.left + "," + mFrame.top + ") (" +
7492 mFrame.width() + "x" + mFrame.height() + "), layer=" +
7493 mAnimLayer + " HIDE", null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007494 }
7495 Surface.openTransaction();
7496 try {
7497 try {
Dianne Hackborn16064f92010-03-25 00:47:24 -07007498 mSurfaceX = mFrame.left + mXOffset;
Dianne Hackborn529bef62010-03-25 11:48:43 -07007499 mSurfaceY = mFrame.top + mYOffset;
Dianne Hackborn16064f92010-03-25 00:47:24 -07007500 mSurface.setPosition(mSurfaceX, mSurfaceY);
7501 mSurfaceLayer = mAnimLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007502 mSurface.setLayer(mAnimLayer);
Dianne Hackborn16064f92010-03-25 00:47:24 -07007503 mSurfaceShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007504 mSurface.hide();
7505 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007506 if (SHOW_TRANSACTIONS) logSurface(this, "DITHER", null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007507 mSurface.setFlags(Surface.SURFACE_DITHER,
7508 Surface.SURFACE_DITHER);
7509 }
7510 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007511 Slog.w(TAG, "Error creating surface in " + w, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007512 reclaimSomeSurfaceMemoryLocked(this, "create-init");
7513 }
7514 mLastHidden = true;
7515 } finally {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007516 if (SHOW_TRANSACTIONS) Slog.i(TAG, "<<< CLOSE TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007517 Surface.closeTransaction();
7518 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08007519 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007520 TAG, "Created surface " + this);
7521 }
7522 return mSurface;
7523 }
Romain Guy06882f82009-06-10 13:36:04 -07007524
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007525 void destroySurfaceLocked() {
7526 // Window is no longer on-screen, so can no longer receive
7527 // key events... if we were waiting for it to finish
7528 // handling a key event, the wait is over!
7529 mKeyWaiter.finishedKey(mSession, mClient, true,
7530 KeyWaiter.RETURN_NOTHING);
7531 mKeyWaiter.releasePendingPointerLocked(mSession);
7532 mKeyWaiter.releasePendingTrackballLocked(mSession);
7533
7534 if (mAppToken != null && this == mAppToken.startingWindow) {
7535 mAppToken.startingDisplayed = false;
7536 }
Romain Guy06882f82009-06-10 13:36:04 -07007537
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007538 if (mSurface != null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007539 mDrawPending = false;
7540 mCommitDrawPending = false;
7541 mReadyToShow = false;
7542
7543 int i = mChildWindows.size();
7544 while (i > 0) {
7545 i--;
7546 WindowState c = (WindowState)mChildWindows.get(i);
7547 c.mAttachedHidden = true;
7548 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007549
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007550 if (mReportDestroySurface) {
7551 mReportDestroySurface = false;
7552 mSurfacePendingDestroy = true;
7553 try {
7554 mClient.dispatchGetNewSurface();
7555 // We'll really destroy on the next time around.
7556 return;
7557 } catch (RemoteException e) {
7558 }
7559 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007560
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007561 try {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007562 if (DEBUG_VISIBILITY) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007563 RuntimeException e = null;
7564 if (!HIDE_STACK_CRAWLS) {
7565 e = new RuntimeException();
7566 e.fillInStackTrace();
7567 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08007568 Slog.w(TAG, "Window " + this + " destroying surface "
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007569 + mSurface + ", session " + mSession, e);
7570 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007571 if (SHOW_TRANSACTIONS) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007572 RuntimeException e = null;
7573 if (!HIDE_STACK_CRAWLS) {
7574 e = new RuntimeException();
7575 e.fillInStackTrace();
7576 }
7577 if (SHOW_TRANSACTIONS) logSurface(this, "DESTROY", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007578 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007579 mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007580 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007581 Slog.w(TAG, "Exception thrown when destroying Window " + this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007582 + " surface " + mSurface + " session " + mSession
7583 + ": " + e.toString());
7584 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007585
Dianne Hackborn16064f92010-03-25 00:47:24 -07007586 mSurfaceShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007587 mSurface = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007588 }
7589 }
7590
7591 boolean finishDrawingLocked() {
7592 if (mDrawPending) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007593 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007594 TAG, "finishDrawingLocked: " + mSurface);
7595 mCommitDrawPending = true;
7596 mDrawPending = false;
7597 return true;
7598 }
7599 return false;
7600 }
7601
7602 // This must be called while inside a transaction.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007603 boolean commitFinishDrawingLocked(long currentTime) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007604 //Slog.i(TAG, "commitFinishDrawingLocked: " + mSurface);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007605 if (!mCommitDrawPending) {
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007606 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007607 }
7608 mCommitDrawPending = false;
7609 mReadyToShow = true;
7610 final boolean starting = mAttrs.type == TYPE_APPLICATION_STARTING;
7611 final AppWindowToken atoken = mAppToken;
7612 if (atoken == null || atoken.allDrawn || starting) {
7613 performShowLocked();
7614 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007615 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007616 }
7617
7618 // This must be called while inside a transaction.
7619 boolean performShowLocked() {
7620 if (DEBUG_VISIBILITY) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007621 RuntimeException e = null;
7622 if (!HIDE_STACK_CRAWLS) {
7623 e = new RuntimeException();
7624 e.fillInStackTrace();
7625 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08007626 Slog.v(TAG, "performShow on " + this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007627 + ": readyToShow=" + mReadyToShow + " readyForDisplay=" + isReadyForDisplay()
7628 + " starting=" + (mAttrs.type == TYPE_APPLICATION_STARTING), e);
7629 }
7630 if (mReadyToShow && isReadyForDisplay()) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007631 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) logSurface(this,
7632 "SHOW (performShowLocked)", null);
Joe Onorato8a9b2202010-02-26 18:56:32 -08007633 if (DEBUG_VISIBILITY) Slog.v(TAG, "Showing " + this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007634 + " during animation: policyVis=" + mPolicyVisibility
7635 + " attHidden=" + mAttachedHidden
7636 + " tok.hiddenRequested="
7637 + (mAppToken != null ? mAppToken.hiddenRequested : false)
Dianne Hackborn248b1882009-09-16 16:46:44 -07007638 + " tok.hidden="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007639 + (mAppToken != null ? mAppToken.hidden : false)
7640 + " animating=" + mAnimating
7641 + " tok animating="
7642 + (mAppToken != null ? mAppToken.animating : false));
7643 if (!showSurfaceRobustlyLocked(this)) {
7644 return false;
7645 }
7646 mLastAlpha = -1;
7647 mHasDrawn = true;
7648 mLastHidden = false;
7649 mReadyToShow = false;
7650 enableScreenIfNeededLocked();
7651
7652 applyEnterAnimationLocked(this);
Romain Guy06882f82009-06-10 13:36:04 -07007653
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007654 int i = mChildWindows.size();
7655 while (i > 0) {
7656 i--;
7657 WindowState c = (WindowState)mChildWindows.get(i);
Dianne Hackbornf09c1a22010-04-22 15:59:21 -07007658 if (c.mAttachedHidden) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007659 c.mAttachedHidden = false;
Dianne Hackbornf09c1a22010-04-22 15:59:21 -07007660 if (c.mSurface != null) {
7661 c.performShowLocked();
7662 // It hadn't been shown, which means layout not
7663 // performed on it, so now we want to make sure to
7664 // do a layout. If called from within the transaction
7665 // loop, this will cause it to restart with a new
7666 // layout.
7667 mLayoutNeeded = true;
7668 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007669 }
7670 }
Romain Guy06882f82009-06-10 13:36:04 -07007671
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007672 if (mAttrs.type != TYPE_APPLICATION_STARTING
7673 && mAppToken != null) {
7674 mAppToken.firstWindowDrawn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007675
Dianne Hackborn248b1882009-09-16 16:46:44 -07007676 if (mAppToken.startingData != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007677 if (DEBUG_STARTING_WINDOW || DEBUG_ANIM) Slog.v(TAG,
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007678 "Finish starting " + mToken
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007679 + ": first real window is shown, no animation");
Dianne Hackborn248b1882009-09-16 16:46:44 -07007680 // If this initial window is animating, stop it -- we
7681 // will do an animation to reveal it from behind the
7682 // starting window, so there is no need for it to also
7683 // be doing its own stuff.
7684 if (mAnimation != null) {
7685 mAnimation = null;
7686 // Make sure we clean up the animation.
7687 mAnimating = true;
7688 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007689 mFinishedStarting.add(mAppToken);
7690 mH.sendEmptyMessage(H.FINISHED_STARTING);
7691 }
7692 mAppToken.updateReportedVisibilityLocked();
7693 }
7694 }
7695 return true;
7696 }
Romain Guy06882f82009-06-10 13:36:04 -07007697
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007698 // This must be called while inside a transaction. Returns true if
7699 // there is more animation to run.
7700 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
Dianne Hackbornde2606d2009-12-18 16:53:55 -08007701 if (!mDisplayFrozen && mPolicy.isScreenOn()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007702 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07007703
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007704 if (!mDrawPending && !mCommitDrawPending && mAnimation != null) {
7705 mHasTransformation = true;
7706 mHasLocalTransformation = true;
7707 if (!mLocalAnimating) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007708 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007709 TAG, "Starting animation in " + this +
7710 " @ " + currentTime + ": ww=" + mFrame.width() + " wh=" + mFrame.height() +
7711 " dw=" + dw + " dh=" + dh + " scale=" + mWindowAnimationScale);
7712 mAnimation.initialize(mFrame.width(), mFrame.height(), dw, dh);
7713 mAnimation.setStartTime(currentTime);
7714 mLocalAnimating = true;
7715 mAnimating = true;
7716 }
7717 mTransformation.clear();
7718 final boolean more = mAnimation.getTransformation(
7719 currentTime, mTransformation);
Joe Onorato8a9b2202010-02-26 18:56:32 -08007720 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007721 TAG, "Stepped animation in " + this +
7722 ": more=" + more + ", xform=" + mTransformation);
7723 if (more) {
7724 // we're not done!
7725 return true;
7726 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08007727 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007728 TAG, "Finished animation in " + this +
7729 " @ " + currentTime);
7730 mAnimation = null;
7731 //WindowManagerService.this.dump();
7732 }
7733 mHasLocalTransformation = false;
7734 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppToken != null
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007735 && mAppToken.animation != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007736 // When our app token is animating, we kind-of pretend like
7737 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
7738 // part of this check means that we will only do this if
7739 // our window is not currently exiting, or it is not
7740 // locally animating itself. The idea being that one that
7741 // is exiting and doing a local animation should be removed
7742 // once that animation is done.
7743 mAnimating = true;
7744 mHasTransformation = true;
7745 mTransformation.clear();
7746 return false;
7747 } else if (mHasTransformation) {
7748 // Little trick to get through the path below to act like
7749 // we have finished an animation.
7750 mAnimating = true;
7751 } else if (isAnimating()) {
7752 mAnimating = true;
7753 }
7754 } else if (mAnimation != null) {
7755 // If the display is frozen, and there is a pending animation,
7756 // clear it and make sure we run the cleanup code.
7757 mAnimating = true;
7758 mLocalAnimating = true;
7759 mAnimation = null;
7760 }
Romain Guy06882f82009-06-10 13:36:04 -07007761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007762 if (!mAnimating && !mLocalAnimating) {
7763 return false;
7764 }
7765
Joe Onorato8a9b2202010-02-26 18:56:32 -08007766 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007767 TAG, "Animation done in " + this + ": exiting=" + mExiting
7768 + ", reportedVisible="
7769 + (mAppToken != null ? mAppToken.reportedVisible : false));
Romain Guy06882f82009-06-10 13:36:04 -07007770
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007771 mAnimating = false;
7772 mLocalAnimating = false;
7773 mAnimation = null;
7774 mAnimLayer = mLayer;
7775 if (mIsImWindow) {
7776 mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007777 } else if (mIsWallpaper) {
7778 mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007779 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08007780 if (DEBUG_LAYERS) Slog.v(TAG, "Stepping win " + this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007781 + " anim layer: " + mAnimLayer);
7782 mHasTransformation = false;
7783 mHasLocalTransformation = false;
Dianne Hackbornb601ce12010-03-01 23:36:02 -08007784 if (mPolicyVisibility != mPolicyVisibilityAfterAnim) {
7785 if (DEBUG_VISIBILITY) {
7786 Slog.v(TAG, "Policy visibility changing after anim in " + this + ": "
7787 + mPolicyVisibilityAfterAnim);
7788 }
7789 mPolicyVisibility = mPolicyVisibilityAfterAnim;
7790 if (!mPolicyVisibility) {
7791 if (mCurrentFocus == this) {
7792 mFocusMayChange = true;
7793 }
7794 // Window is no longer visible -- make sure if we were waiting
7795 // for it to be displayed before enabling the display, that
7796 // we allow the display to be enabled now.
7797 enableScreenIfNeededLocked();
7798 }
Dianne Hackbornf3bea9c2009-12-09 18:26:21 -08007799 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007800 mTransformation.clear();
7801 if (mHasDrawn
7802 && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
7803 && mAppToken != null
7804 && mAppToken.firstWindowDrawn
7805 && mAppToken.startingData != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007806 if (DEBUG_STARTING_WINDOW) Slog.v(TAG, "Finish starting "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007807 + mToken + ": first real window done animating");
7808 mFinishedStarting.add(mAppToken);
7809 mH.sendEmptyMessage(H.FINISHED_STARTING);
7810 }
Romain Guy06882f82009-06-10 13:36:04 -07007811
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007812 finishExit();
7813
7814 if (mAppToken != null) {
7815 mAppToken.updateReportedVisibilityLocked();
7816 }
7817
7818 return false;
7819 }
7820
7821 void finishExit() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007822 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007823 TAG, "finishExit in " + this
7824 + ": exiting=" + mExiting
7825 + " remove=" + mRemoveOnExit
7826 + " windowAnimating=" + isWindowAnimating());
Romain Guy06882f82009-06-10 13:36:04 -07007827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007828 final int N = mChildWindows.size();
7829 for (int i=0; i<N; i++) {
7830 ((WindowState)mChildWindows.get(i)).finishExit();
7831 }
Romain Guy06882f82009-06-10 13:36:04 -07007832
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007833 if (!mExiting) {
7834 return;
7835 }
Romain Guy06882f82009-06-10 13:36:04 -07007836
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007837 if (isWindowAnimating()) {
7838 return;
7839 }
7840
Joe Onorato8a9b2202010-02-26 18:56:32 -08007841 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007842 TAG, "Exit animation finished in " + this
7843 + ": remove=" + mRemoveOnExit);
7844 if (mSurface != null) {
7845 mDestroySurface.add(this);
7846 mDestroying = true;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007847 if (SHOW_TRANSACTIONS) logSurface(this, "HIDE (finishExit)", null);
Dianne Hackborn16064f92010-03-25 00:47:24 -07007848 mSurfaceShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007849 try {
7850 mSurface.hide();
7851 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007852 Slog.w(TAG, "Error hiding surface in " + this, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007853 }
7854 mLastHidden = true;
7855 mKeyWaiter.releasePendingPointerLocked(mSession);
7856 }
7857 mExiting = false;
7858 if (mRemoveOnExit) {
7859 mPendingRemove.add(this);
7860 mRemoveOnExit = false;
7861 }
7862 }
Romain Guy06882f82009-06-10 13:36:04 -07007863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007864 boolean isIdentityMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
7865 if (dsdx < .99999f || dsdx > 1.00001f) return false;
7866 if (dtdy < .99999f || dtdy > 1.00001f) return false;
7867 if (dtdx < -.000001f || dtdx > .000001f) return false;
7868 if (dsdy < -.000001f || dsdy > .000001f) return false;
7869 return true;
7870 }
Romain Guy06882f82009-06-10 13:36:04 -07007871
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007872 void computeShownFrameLocked() {
7873 final boolean selfTransformation = mHasLocalTransformation;
7874 Transformation attachedTransformation =
7875 (mAttachedWindow != null && mAttachedWindow.mHasLocalTransformation)
7876 ? mAttachedWindow.mTransformation : null;
7877 Transformation appTransformation =
7878 (mAppToken != null && mAppToken.hasTransformation)
7879 ? mAppToken.transformation : null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007880
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007881 // Wallpapers are animated based on the "real" window they
7882 // are currently targeting.
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007883 if (mAttrs.type == TYPE_WALLPAPER && mLowerWallpaperTarget == null
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07007884 && mWallpaperTarget != null) {
Dianne Hackborn5baba162009-09-23 17:01:12 -07007885 if (mWallpaperTarget.mHasLocalTransformation &&
7886 mWallpaperTarget.mAnimation != null &&
7887 !mWallpaperTarget.mAnimation.getDetachWallpaper()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007888 attachedTransformation = mWallpaperTarget.mTransformation;
Dianne Hackborn5baba162009-09-23 17:01:12 -07007889 if (DEBUG_WALLPAPER && attachedTransformation != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007890 Slog.v(TAG, "WP target attached xform: " + attachedTransformation);
Dianne Hackborn5baba162009-09-23 17:01:12 -07007891 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007892 }
7893 if (mWallpaperTarget.mAppToken != null &&
Dianne Hackborn5baba162009-09-23 17:01:12 -07007894 mWallpaperTarget.mAppToken.hasTransformation &&
7895 mWallpaperTarget.mAppToken.animation != null &&
7896 !mWallpaperTarget.mAppToken.animation.getDetachWallpaper()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007897 appTransformation = mWallpaperTarget.mAppToken.transformation;
Dianne Hackborn5baba162009-09-23 17:01:12 -07007898 if (DEBUG_WALLPAPER && appTransformation != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007899 Slog.v(TAG, "WP target app xform: " + appTransformation);
Dianne Hackborn5baba162009-09-23 17:01:12 -07007900 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007901 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007902 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007903
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007904 if (selfTransformation || attachedTransformation != null
7905 || appTransformation != null) {
Romain Guy06882f82009-06-10 13:36:04 -07007906 // cache often used attributes locally
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007907 final Rect frame = mFrame;
7908 final float tmpFloats[] = mTmpFloats;
7909 final Matrix tmpMatrix = mTmpMatrix;
7910
7911 // Compute the desired transformation.
Dianne Hackborn65c23872009-09-18 17:47:02 -07007912 tmpMatrix.setTranslate(0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007913 if (selfTransformation) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007914 tmpMatrix.postConcat(mTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007915 }
Dianne Hackborn65c23872009-09-18 17:47:02 -07007916 tmpMatrix.postTranslate(frame.left, frame.top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007917 if (attachedTransformation != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007918 tmpMatrix.postConcat(attachedTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007919 }
7920 if (appTransformation != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007921 tmpMatrix.postConcat(appTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007922 }
7923
7924 // "convert" it into SurfaceFlinger's format
7925 // (a 2x2 matrix + an offset)
7926 // Here we must not transform the position of the surface
7927 // since it is already included in the transformation.
Joe Onorato8a9b2202010-02-26 18:56:32 -08007928 //Slog.i(TAG, "Transform: " + matrix);
Romain Guy06882f82009-06-10 13:36:04 -07007929
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007930 tmpMatrix.getValues(tmpFloats);
7931 mDsDx = tmpFloats[Matrix.MSCALE_X];
7932 mDtDx = tmpFloats[Matrix.MSKEW_X];
7933 mDsDy = tmpFloats[Matrix.MSKEW_Y];
7934 mDtDy = tmpFloats[Matrix.MSCALE_Y];
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007935 int x = (int)tmpFloats[Matrix.MTRANS_X] + mXOffset;
7936 int y = (int)tmpFloats[Matrix.MTRANS_Y] + mYOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007937 int w = frame.width();
7938 int h = frame.height();
7939 mShownFrame.set(x, y, x+w, y+h);
7940
7941 // Now set the alpha... but because our current hardware
7942 // can't do alpha transformation on a non-opaque surface,
7943 // turn it off if we are running an animation that is also
7944 // transforming since it is more important to have that
7945 // animation be smooth.
7946 mShownAlpha = mAlpha;
7947 if (!mLimitedAlphaCompositing
7948 || (!PixelFormat.formatHasAlpha(mAttrs.format)
7949 || (isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
7950 && x == frame.left && y == frame.top))) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007951 //Slog.i(TAG, "Applying alpha transform");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007952 if (selfTransformation) {
7953 mShownAlpha *= mTransformation.getAlpha();
7954 }
7955 if (attachedTransformation != null) {
7956 mShownAlpha *= attachedTransformation.getAlpha();
7957 }
7958 if (appTransformation != null) {
7959 mShownAlpha *= appTransformation.getAlpha();
7960 }
7961 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007962 //Slog.i(TAG, "Not applying alpha transform");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007963 }
Romain Guy06882f82009-06-10 13:36:04 -07007964
Joe Onorato8a9b2202010-02-26 18:56:32 -08007965 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007966 TAG, "Continuing animation in " + this +
7967 ": " + mShownFrame +
7968 ", alpha=" + mTransformation.getAlpha());
7969 return;
7970 }
Romain Guy06882f82009-06-10 13:36:04 -07007971
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007972 mShownFrame.set(mFrame);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007973 if (mXOffset != 0 || mYOffset != 0) {
7974 mShownFrame.offset(mXOffset, mYOffset);
7975 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007976 mShownAlpha = mAlpha;
7977 mDsDx = 1;
7978 mDtDx = 0;
7979 mDsDy = 0;
7980 mDtDy = 1;
7981 }
Romain Guy06882f82009-06-10 13:36:04 -07007982
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007983 /**
7984 * Is this window visible? It is not visible if there is no
7985 * surface, or we are in the process of running an exit animation
7986 * that will remove the surface, or its app token has been hidden.
7987 */
7988 public boolean isVisibleLw() {
7989 final AppWindowToken atoken = mAppToken;
7990 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7991 && (atoken == null || !atoken.hiddenRequested)
7992 && !mExiting && !mDestroying;
7993 }
7994
7995 /**
Dianne Hackborn3d163f072009-10-07 21:26:57 -07007996 * Like {@link #isVisibleLw}, but also counts a window that is currently
7997 * "hidden" behind the keyguard as visible. This allows us to apply
7998 * things like window flags that impact the keyguard.
7999 * XXX I am starting to think we need to have ANOTHER visibility flag
8000 * for this "hidden behind keyguard" state rather than overloading
8001 * mPolicyVisibility. Ungh.
8002 */
8003 public boolean isVisibleOrBehindKeyguardLw() {
8004 final AppWindowToken atoken = mAppToken;
8005 return mSurface != null && !mAttachedHidden
8006 && (atoken == null ? mPolicyVisibility : !atoken.hiddenRequested)
Dianne Hackborn5943c202010-04-12 21:36:49 -07008007 && (mOrientationChanging || (!mDrawPending && !mCommitDrawPending))
Dianne Hackborn3d163f072009-10-07 21:26:57 -07008008 && !mExiting && !mDestroying;
8009 }
8010
8011 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008012 * Is this window visible, ignoring its app token? It is not visible
8013 * if there is no surface, or we are in the process of running an exit animation
8014 * that will remove the surface.
8015 */
8016 public boolean isWinVisibleLw() {
8017 final AppWindowToken atoken = mAppToken;
8018 return mSurface != null && mPolicyVisibility && !mAttachedHidden
8019 && (atoken == null || !atoken.hiddenRequested || atoken.animating)
8020 && !mExiting && !mDestroying;
8021 }
8022
8023 /**
8024 * The same as isVisible(), but follows the current hidden state of
8025 * the associated app token, not the pending requested hidden state.
8026 */
8027 boolean isVisibleNow() {
8028 return mSurface != null && mPolicyVisibility && !mAttachedHidden
The Android Open Source Project10592532009-03-18 17:39:46 -07008029 && !mRootToken.hidden && !mExiting && !mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008030 }
8031
8032 /**
8033 * Same as isVisible(), but we also count it as visible between the
8034 * call to IWindowSession.add() and the first relayout().
8035 */
8036 boolean isVisibleOrAdding() {
8037 final AppWindowToken atoken = mAppToken;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008038 return ((mSurface != null && !mReportDestroySurface)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008039 || (!mRelayoutCalled && mViewVisibility == View.VISIBLE))
8040 && mPolicyVisibility && !mAttachedHidden
8041 && (atoken == null || !atoken.hiddenRequested)
8042 && !mExiting && !mDestroying;
8043 }
8044
8045 /**
8046 * Is this window currently on-screen? It is on-screen either if it
8047 * is visible or it is currently running an animation before no longer
8048 * being visible.
8049 */
8050 boolean isOnScreen() {
8051 final AppWindowToken atoken = mAppToken;
8052 if (atoken != null) {
8053 return mSurface != null && mPolicyVisibility && !mDestroying
8054 && ((!mAttachedHidden && !atoken.hiddenRequested)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07008055 || mAnimation != null || atoken.animation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008056 } else {
8057 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn0cd48872009-08-13 18:51:59 -07008058 && (!mAttachedHidden || mAnimation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008059 }
8060 }
Romain Guy06882f82009-06-10 13:36:04 -07008061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008062 /**
8063 * Like isOnScreen(), but we don't return true if the window is part
8064 * of a transition that has not yet been started.
8065 */
8066 boolean isReadyForDisplay() {
Dianne Hackborna8f60182009-09-01 19:01:50 -07008067 if (mRootToken.waitingToShow &&
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07008068 mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07008069 return false;
8070 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008071 final AppWindowToken atoken = mAppToken;
Dianne Hackborn0cd48872009-08-13 18:51:59 -07008072 final boolean animating = atoken != null
8073 ? (atoken.animation != null) : false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008074 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07008075 && ((!mAttachedHidden && mViewVisibility == View.VISIBLE
8076 && !mRootToken.hidden)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07008077 || mAnimation != null || animating);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008078 }
8079
8080 /** Is the window or its container currently animating? */
8081 boolean isAnimating() {
8082 final WindowState attached = mAttachedWindow;
8083 final AppWindowToken atoken = mAppToken;
8084 return mAnimation != null
8085 || (attached != null && attached.mAnimation != null)
Romain Guy06882f82009-06-10 13:36:04 -07008086 || (atoken != null &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008087 (atoken.animation != null
8088 || atoken.inPendingTransaction));
8089 }
8090
8091 /** Is this window currently animating? */
8092 boolean isWindowAnimating() {
8093 return mAnimation != null;
8094 }
8095
8096 /**
8097 * Like isOnScreen, but returns false if the surface hasn't yet
8098 * been drawn.
8099 */
8100 public boolean isDisplayedLw() {
8101 final AppWindowToken atoken = mAppToken;
8102 return mSurface != null && mPolicyVisibility && !mDestroying
8103 && !mDrawPending && !mCommitDrawPending
8104 && ((!mAttachedHidden &&
8105 (atoken == null || !atoken.hiddenRequested))
8106 || mAnimating);
8107 }
8108
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008109 /**
8110 * Returns true if the window has a surface that it has drawn a
Dianne Hackborn5943c202010-04-12 21:36:49 -07008111 * complete UI in to. Note that this returns true if the orientation
8112 * is changing even if the window hasn't redrawn because we don't want
8113 * to stop things from executing during that time.
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008114 */
8115 public boolean isDrawnLw() {
8116 final AppWindowToken atoken = mAppToken;
8117 return mSurface != null && !mDestroying
Dianne Hackborn5943c202010-04-12 21:36:49 -07008118 && (mOrientationChanging || (!mDrawPending && !mCommitDrawPending));
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008119 }
8120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008121 public boolean fillsScreenLw(int screenWidth, int screenHeight,
8122 boolean shownFrame, boolean onlyOpaque) {
8123 if (mSurface == null) {
8124 return false;
8125 }
8126 if (mAppToken != null && !mAppToken.appFullscreen) {
8127 return false;
8128 }
8129 if (onlyOpaque && mAttrs.format != PixelFormat.OPAQUE) {
8130 return false;
8131 }
8132 final Rect frame = shownFrame ? mShownFrame : mFrame;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07008133
8134 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
8135 return frame.left <= mCompatibleScreenFrame.left &&
8136 frame.top <= mCompatibleScreenFrame.top &&
8137 frame.right >= mCompatibleScreenFrame.right &&
8138 frame.bottom >= mCompatibleScreenFrame.bottom;
8139 } else {
8140 return frame.left <= 0 && frame.top <= 0
8141 && frame.right >= screenWidth
8142 && frame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008143 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008144 }
Romain Guy06882f82009-06-10 13:36:04 -07008145
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008146 /**
Dianne Hackborn25994b42009-09-04 14:21:19 -07008147 * Return true if the window is opaque and fully drawn. This indicates
8148 * it may obscure windows behind it.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008149 */
8150 boolean isOpaqueDrawn() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07008151 return (mAttrs.format == PixelFormat.OPAQUE
8152 || mAttrs.type == TYPE_WALLPAPER)
8153 && mSurface != null && mAnimation == null
8154 && (mAppToken == null || mAppToken.animation == null)
8155 && !mDrawPending && !mCommitDrawPending;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008156 }
8157
8158 boolean needsBackgroundFiller(int screenWidth, int screenHeight) {
8159 return
8160 // only if the application is requesting compatible window
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008161 (mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0 &&
8162 // only if it's visible
8163 mHasDrawn && mViewVisibility == View.VISIBLE &&
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07008164 // and only if the application fills the compatible screen
8165 mFrame.left <= mCompatibleScreenFrame.left &&
8166 mFrame.top <= mCompatibleScreenFrame.top &&
8167 mFrame.right >= mCompatibleScreenFrame.right &&
8168 mFrame.bottom >= mCompatibleScreenFrame.bottom &&
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008169 // and starting window do not need background filler
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07008170 mAttrs.type != mAttrs.TYPE_APPLICATION_STARTING;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008171 }
8172
8173 boolean isFullscreen(int screenWidth, int screenHeight) {
8174 return mFrame.left <= 0 && mFrame.top <= 0 &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07008175 mFrame.right >= screenWidth && mFrame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008176 }
8177
8178 void removeLocked() {
8179 if (mAttachedWindow != null) {
8180 mAttachedWindow.mChildWindows.remove(this);
8181 }
8182 destroySurfaceLocked();
8183 mSession.windowRemovedLocked();
8184 try {
8185 mClient.asBinder().unlinkToDeath(mDeathRecipient, 0);
8186 } catch (RuntimeException e) {
8187 // Ignore if it has already been removed (usually because
8188 // we are doing this as part of processing a death note.)
8189 }
8190 }
8191
8192 private class DeathRecipient implements IBinder.DeathRecipient {
8193 public void binderDied() {
8194 try {
8195 synchronized(mWindowMap) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08008196 WindowState win = windowForClientLocked(mSession, mClient, false);
Joe Onorato8a9b2202010-02-26 18:56:32 -08008197 Slog.i(TAG, "WIN DEATH: " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008198 if (win != null) {
8199 removeWindowLocked(mSession, win);
8200 }
8201 }
8202 } catch (IllegalArgumentException ex) {
8203 // This will happen if the window has already been
8204 // removed.
8205 }
8206 }
8207 }
8208
8209 /** Returns true if this window desires key events. */
8210 public final boolean canReceiveKeys() {
8211 return isVisibleOrAdding()
8212 && (mViewVisibility == View.VISIBLE)
8213 && ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0);
8214 }
8215
8216 public boolean hasDrawnLw() {
8217 return mHasDrawn;
8218 }
8219
8220 public boolean showLw(boolean doAnimation) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008221 return showLw(doAnimation, true);
8222 }
8223
8224 boolean showLw(boolean doAnimation, boolean requestAnim) {
8225 if (mPolicyVisibility && mPolicyVisibilityAfterAnim) {
8226 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008227 }
Dianne Hackbornb601ce12010-03-01 23:36:02 -08008228 if (DEBUG_VISIBILITY) Slog.v(TAG, "Policy visibility true: " + this);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08008229 if (doAnimation) {
8230 if (DEBUG_VISIBILITY) Slog.v(TAG, "doAnimation: mPolicyVisibility="
8231 + mPolicyVisibility + " mAnimation=" + mAnimation);
8232 if (mDisplayFrozen || !mPolicy.isScreenOn()) {
8233 doAnimation = false;
8234 } else if (mPolicyVisibility && mAnimation == null) {
8235 // Check for the case where we are currently visible and
8236 // not animating; we do not want to do animation at such a
8237 // point to become visible when we already are.
8238 doAnimation = false;
8239 }
8240 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008241 mPolicyVisibility = true;
8242 mPolicyVisibilityAfterAnim = true;
8243 if (doAnimation) {
8244 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_ENTER, true);
8245 }
8246 if (requestAnim) {
8247 requestAnimationLocked(0);
8248 }
8249 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008250 }
8251
8252 public boolean hideLw(boolean doAnimation) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008253 return hideLw(doAnimation, true);
8254 }
8255
8256 boolean hideLw(boolean doAnimation, boolean requestAnim) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08008257 if (doAnimation) {
8258 if (mDisplayFrozen || !mPolicy.isScreenOn()) {
8259 doAnimation = false;
8260 }
8261 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008262 boolean current = doAnimation ? mPolicyVisibilityAfterAnim
8263 : mPolicyVisibility;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008264 if (!current) {
8265 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008266 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008267 if (doAnimation) {
8268 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_EXIT, false);
8269 if (mAnimation == null) {
8270 doAnimation = false;
8271 }
8272 }
8273 if (doAnimation) {
8274 mPolicyVisibilityAfterAnim = false;
8275 } else {
Dianne Hackbornb601ce12010-03-01 23:36:02 -08008276 if (DEBUG_VISIBILITY) Slog.v(TAG, "Policy visibility false: " + this);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008277 mPolicyVisibilityAfterAnim = false;
8278 mPolicyVisibility = false;
Dianne Hackbornf3bea9c2009-12-09 18:26:21 -08008279 // Window is no longer visible -- make sure if we were waiting
8280 // for it to be displayed before enabling the display, that
8281 // we allow the display to be enabled now.
8282 enableScreenIfNeededLocked();
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08008283 if (mCurrentFocus == this) {
8284 mFocusMayChange = true;
8285 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008286 }
8287 if (requestAnim) {
8288 requestAnimationLocked(0);
8289 }
8290 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008291 }
8292
8293 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008294 pw.print(prefix); pw.print("mSession="); pw.print(mSession);
8295 pw.print(" mClient="); pw.println(mClient.asBinder());
8296 pw.print(prefix); pw.print("mAttrs="); pw.println(mAttrs);
8297 if (mAttachedWindow != null || mLayoutAttached) {
8298 pw.print(prefix); pw.print("mAttachedWindow="); pw.print(mAttachedWindow);
8299 pw.print(" mLayoutAttached="); pw.println(mLayoutAttached);
8300 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07008301 if (mIsImWindow || mIsWallpaper || mIsFloatingLayer) {
8302 pw.print(prefix); pw.print("mIsImWindow="); pw.print(mIsImWindow);
8303 pw.print(" mIsWallpaper="); pw.print(mIsWallpaper);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008304 pw.print(" mIsFloatingLayer="); pw.print(mIsFloatingLayer);
8305 pw.print(" mWallpaperVisible="); pw.println(mWallpaperVisible);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008306 }
8307 pw.print(prefix); pw.print("mBaseLayer="); pw.print(mBaseLayer);
8308 pw.print(" mSubLayer="); pw.print(mSubLayer);
8309 pw.print(" mAnimLayer="); pw.print(mLayer); pw.print("+");
8310 pw.print((mTargetAppToken != null ? mTargetAppToken.animLayerAdjustment
8311 : (mAppToken != null ? mAppToken.animLayerAdjustment : 0)));
8312 pw.print("="); pw.print(mAnimLayer);
8313 pw.print(" mLastLayer="); pw.println(mLastLayer);
8314 if (mSurface != null) {
8315 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
Dianne Hackborn16064f92010-03-25 00:47:24 -07008316 pw.print(prefix); pw.print("Surface: shown="); pw.print(mSurfaceShown);
8317 pw.print(" layer="); pw.print(mSurfaceLayer);
8318 pw.print(" alpha="); pw.print(mSurfaceAlpha);
8319 pw.print(" rect=("); pw.print(mSurfaceX);
8320 pw.print(","); pw.print(mSurfaceY);
8321 pw.print(") "); pw.print(mSurfaceW);
8322 pw.print(" x "); pw.println(mSurfaceH);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008323 }
8324 pw.print(prefix); pw.print("mToken="); pw.println(mToken);
8325 pw.print(prefix); pw.print("mRootToken="); pw.println(mRootToken);
8326 if (mAppToken != null) {
8327 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
8328 }
8329 if (mTargetAppToken != null) {
8330 pw.print(prefix); pw.print("mTargetAppToken="); pw.println(mTargetAppToken);
8331 }
8332 pw.print(prefix); pw.print("mViewVisibility=0x");
8333 pw.print(Integer.toHexString(mViewVisibility));
8334 pw.print(" mLastHidden="); pw.print(mLastHidden);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07008335 pw.print(" mHaveFrame="); pw.print(mHaveFrame);
8336 pw.print(" mObscured="); pw.println(mObscured);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008337 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim || mAttachedHidden) {
8338 pw.print(prefix); pw.print("mPolicyVisibility=");
8339 pw.print(mPolicyVisibility);
8340 pw.print(" mPolicyVisibilityAfterAnim=");
8341 pw.print(mPolicyVisibilityAfterAnim);
8342 pw.print(" mAttachedHidden="); pw.println(mAttachedHidden);
8343 }
Dianne Hackborn9b52a212009-12-11 14:51:35 -08008344 if (!mRelayoutCalled) {
8345 pw.print(prefix); pw.print("mRelayoutCalled="); pw.println(mRelayoutCalled);
8346 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008347 pw.print(prefix); pw.print("Requested w="); pw.print(mRequestedWidth);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08008348 pw.print(" h="); pw.print(mRequestedHeight);
8349 pw.print(" mLayoutSeq="); pw.println(mLayoutSeq);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008350 if (mXOffset != 0 || mYOffset != 0) {
8351 pw.print(prefix); pw.print("Offsets x="); pw.print(mXOffset);
8352 pw.print(" y="); pw.println(mYOffset);
8353 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008354 pw.print(prefix); pw.print("mGivenContentInsets=");
8355 mGivenContentInsets.printShortString(pw);
8356 pw.print(" mGivenVisibleInsets=");
8357 mGivenVisibleInsets.printShortString(pw);
8358 pw.println();
8359 if (mTouchableInsets != 0 || mGivenInsetsPending) {
8360 pw.print(prefix); pw.print("mTouchableInsets="); pw.print(mTouchableInsets);
8361 pw.print(" mGivenInsetsPending="); pw.println(mGivenInsetsPending);
8362 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08008363 pw.print(prefix); pw.print("mConfiguration="); pw.println(mConfiguration);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008364 pw.print(prefix); pw.print("mShownFrame=");
8365 mShownFrame.printShortString(pw);
8366 pw.print(" last="); mLastShownFrame.printShortString(pw);
8367 pw.println();
8368 pw.print(prefix); pw.print("mFrame="); mFrame.printShortString(pw);
8369 pw.print(" last="); mLastFrame.printShortString(pw);
8370 pw.println();
8371 pw.print(prefix); pw.print("mContainingFrame=");
8372 mContainingFrame.printShortString(pw);
8373 pw.print(" mDisplayFrame=");
8374 mDisplayFrame.printShortString(pw);
8375 pw.println();
8376 pw.print(prefix); pw.print("mContentFrame="); mContentFrame.printShortString(pw);
8377 pw.print(" mVisibleFrame="); mVisibleFrame.printShortString(pw);
8378 pw.println();
8379 pw.print(prefix); pw.print("mContentInsets="); mContentInsets.printShortString(pw);
8380 pw.print(" last="); mLastContentInsets.printShortString(pw);
8381 pw.print(" mVisibleInsets="); mVisibleInsets.printShortString(pw);
8382 pw.print(" last="); mLastVisibleInsets.printShortString(pw);
8383 pw.println();
8384 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
8385 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
8386 pw.print(" mAlpha="); pw.print(mAlpha);
8387 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
8388 }
8389 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
8390 || mAnimation != null) {
8391 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
8392 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
8393 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
8394 pw.print(" mAnimation="); pw.println(mAnimation);
8395 }
8396 if (mHasTransformation || mHasLocalTransformation) {
8397 pw.print(prefix); pw.print("XForm: has=");
8398 pw.print(mHasTransformation);
8399 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
8400 pw.print(" "); mTransformation.printShortString(pw);
8401 pw.println();
8402 }
8403 pw.print(prefix); pw.print("mDrawPending="); pw.print(mDrawPending);
8404 pw.print(" mCommitDrawPending="); pw.print(mCommitDrawPending);
8405 pw.print(" mReadyToShow="); pw.print(mReadyToShow);
8406 pw.print(" mHasDrawn="); pw.println(mHasDrawn);
8407 if (mExiting || mRemoveOnExit || mDestroying || mRemoved) {
8408 pw.print(prefix); pw.print("mExiting="); pw.print(mExiting);
8409 pw.print(" mRemoveOnExit="); pw.print(mRemoveOnExit);
8410 pw.print(" mDestroying="); pw.print(mDestroying);
8411 pw.print(" mRemoved="); pw.println(mRemoved);
8412 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07008413 if (mOrientationChanging || mAppFreezing || mTurnOnScreen) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008414 pw.print(prefix); pw.print("mOrientationChanging=");
8415 pw.print(mOrientationChanging);
Dianne Hackborn93e462b2009-09-15 22:50:40 -07008416 pw.print(" mAppFreezing="); pw.print(mAppFreezing);
8417 pw.print(" mTurnOnScreen="); pw.println(mTurnOnScreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008418 }
Mitsuru Oshima589cebe2009-07-22 20:38:58 -07008419 if (mHScale != 1 || mVScale != 1) {
8420 pw.print(prefix); pw.print("mHScale="); pw.print(mHScale);
8421 pw.print(" mVScale="); pw.println(mVScale);
8422 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07008423 if (mWallpaperX != -1 || mWallpaperY != -1) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008424 pw.print(prefix); pw.print("mWallpaperX="); pw.print(mWallpaperX);
8425 pw.print(" mWallpaperY="); pw.println(mWallpaperY);
8426 }
Marco Nelissenbf6956b2009-11-09 15:21:13 -08008427 if (mWallpaperXStep != -1 || mWallpaperYStep != -1) {
8428 pw.print(prefix); pw.print("mWallpaperXStep="); pw.print(mWallpaperXStep);
8429 pw.print(" mWallpaperYStep="); pw.println(mWallpaperYStep);
8430 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008431 }
8432
8433 @Override
8434 public String toString() {
8435 return "Window{"
8436 + Integer.toHexString(System.identityHashCode(this))
8437 + " " + mAttrs.getTitle() + " paused=" + mToken.paused + "}";
8438 }
8439 }
Romain Guy06882f82009-06-10 13:36:04 -07008440
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008441 // -------------------------------------------------------------
8442 // Window Token State
8443 // -------------------------------------------------------------
8444
8445 class WindowToken {
8446 // The actual token.
8447 final IBinder token;
8448
8449 // The type of window this token is for, as per WindowManager.LayoutParams.
8450 final int windowType;
Romain Guy06882f82009-06-10 13:36:04 -07008451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008452 // Set if this token was explicitly added by a client, so should
8453 // not be removed when all windows are removed.
8454 final boolean explicit;
Romain Guy06882f82009-06-10 13:36:04 -07008455
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008456 // For printing.
8457 String stringName;
Romain Guy06882f82009-06-10 13:36:04 -07008458
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008459 // If this is an AppWindowToken, this is non-null.
8460 AppWindowToken appWindowToken;
Romain Guy06882f82009-06-10 13:36:04 -07008461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008462 // All of the windows associated with this token.
8463 final ArrayList<WindowState> windows = new ArrayList<WindowState>();
8464
8465 // Is key dispatching paused for this token?
8466 boolean paused = false;
8467
8468 // Should this token's windows be hidden?
8469 boolean hidden;
8470
8471 // Temporary for finding which tokens no longer have visible windows.
8472 boolean hasVisible;
8473
Dianne Hackborna8f60182009-09-01 19:01:50 -07008474 // Set to true when this token is in a pending transaction where it
8475 // will be shown.
8476 boolean waitingToShow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008477
Dianne Hackborna8f60182009-09-01 19:01:50 -07008478 // Set to true when this token is in a pending transaction where it
8479 // will be hidden.
8480 boolean waitingToHide;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008481
Dianne Hackborna8f60182009-09-01 19:01:50 -07008482 // Set to true when this token is in a pending transaction where its
8483 // windows will be put to the bottom of the list.
8484 boolean sendingToBottom;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008485
Dianne Hackborna8f60182009-09-01 19:01:50 -07008486 // Set to true when this token is in a pending transaction where its
8487 // windows will be put to the top of the list.
8488 boolean sendingToTop;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008490 WindowToken(IBinder _token, int type, boolean _explicit) {
8491 token = _token;
8492 windowType = type;
8493 explicit = _explicit;
8494 }
8495
8496 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008497 pw.print(prefix); pw.print("token="); pw.println(token);
8498 pw.print(prefix); pw.print("windows="); pw.println(windows);
8499 pw.print(prefix); pw.print("windowType="); pw.print(windowType);
8500 pw.print(" hidden="); pw.print(hidden);
8501 pw.print(" hasVisible="); pw.println(hasVisible);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008502 if (waitingToShow || waitingToHide || sendingToBottom || sendingToTop) {
8503 pw.print(prefix); pw.print("waitingToShow="); pw.print(waitingToShow);
8504 pw.print(" waitingToHide="); pw.print(waitingToHide);
8505 pw.print(" sendingToBottom="); pw.print(sendingToBottom);
8506 pw.print(" sendingToTop="); pw.println(sendingToTop);
8507 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008508 }
8509
8510 @Override
8511 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008512 if (stringName == null) {
8513 StringBuilder sb = new StringBuilder();
8514 sb.append("WindowToken{");
8515 sb.append(Integer.toHexString(System.identityHashCode(this)));
8516 sb.append(" token="); sb.append(token); sb.append('}');
8517 stringName = sb.toString();
8518 }
8519 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008520 }
8521 };
8522
8523 class AppWindowToken extends WindowToken {
8524 // Non-null only for application tokens.
8525 final IApplicationToken appToken;
8526
8527 // All of the windows and child windows that are included in this
8528 // application token. Note this list is NOT sorted!
8529 final ArrayList<WindowState> allAppWindows = new ArrayList<WindowState>();
8530
8531 int groupId = -1;
8532 boolean appFullscreen;
8533 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Romain Guy06882f82009-06-10 13:36:04 -07008534
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008535 // These are used for determining when all windows associated with
8536 // an activity have been drawn, so they can be made visible together
8537 // at the same time.
8538 int lastTransactionSequence = mTransactionSequence-1;
8539 int numInterestingWindows;
8540 int numDrawnWindows;
8541 boolean inPendingTransaction;
8542 boolean allDrawn;
Romain Guy06882f82009-06-10 13:36:04 -07008543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008544 // Is this token going to be hidden in a little while? If so, it
8545 // won't be taken into account for setting the screen orientation.
8546 boolean willBeHidden;
Romain Guy06882f82009-06-10 13:36:04 -07008547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008548 // Is this window's surface needed? This is almost like hidden, except
8549 // it will sometimes be true a little earlier: when the token has
8550 // been shown, but is still waiting for its app transition to execute
8551 // before making its windows shown.
8552 boolean hiddenRequested;
Romain Guy06882f82009-06-10 13:36:04 -07008553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008554 // Have we told the window clients to hide themselves?
8555 boolean clientHidden;
Romain Guy06882f82009-06-10 13:36:04 -07008556
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008557 // Last visibility state we reported to the app token.
8558 boolean reportedVisible;
8559
8560 // Set to true when the token has been removed from the window mgr.
8561 boolean removed;
8562
8563 // Have we been asked to have this token keep the screen frozen?
8564 boolean freezingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07008565
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008566 boolean animating;
8567 Animation animation;
8568 boolean hasTransformation;
8569 final Transformation transformation = new Transformation();
Romain Guy06882f82009-06-10 13:36:04 -07008570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008571 // Offset to the window of all layers in the token, for use by
8572 // AppWindowToken animations.
8573 int animLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -07008574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008575 // Information about an application starting window if displayed.
8576 StartingData startingData;
8577 WindowState startingWindow;
8578 View startingView;
8579 boolean startingDisplayed;
8580 boolean startingMoved;
8581 boolean firstWindowDrawn;
8582
8583 AppWindowToken(IApplicationToken _token) {
8584 super(_token.asBinder(),
8585 WindowManager.LayoutParams.TYPE_APPLICATION, true);
8586 appWindowToken = this;
8587 appToken = _token;
8588 }
Romain Guy06882f82009-06-10 13:36:04 -07008589
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008590 public void setAnimation(Animation anim) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008591 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008592 TAG, "Setting animation in " + this + ": " + anim);
8593 animation = anim;
8594 animating = false;
8595 anim.restrictDuration(MAX_ANIMATION_DURATION);
8596 anim.scaleCurrentDuration(mTransitionAnimationScale);
8597 int zorder = anim.getZAdjustment();
8598 int adj = 0;
8599 if (zorder == Animation.ZORDER_TOP) {
8600 adj = TYPE_LAYER_OFFSET;
8601 } else if (zorder == Animation.ZORDER_BOTTOM) {
8602 adj = -TYPE_LAYER_OFFSET;
8603 }
Romain Guy06882f82009-06-10 13:36:04 -07008604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008605 if (animLayerAdjustment != adj) {
8606 animLayerAdjustment = adj;
8607 updateLayers();
8608 }
8609 }
Romain Guy06882f82009-06-10 13:36:04 -07008610
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008611 public void setDummyAnimation() {
8612 if (animation == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008613 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008614 TAG, "Setting dummy animation in " + this);
8615 animation = sDummyAnimation;
8616 }
8617 }
8618
8619 public void clearAnimation() {
8620 if (animation != null) {
8621 animation = null;
8622 animating = true;
8623 }
8624 }
Romain Guy06882f82009-06-10 13:36:04 -07008625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008626 void updateLayers() {
8627 final int N = allAppWindows.size();
8628 final int adj = animLayerAdjustment;
8629 for (int i=0; i<N; i++) {
8630 WindowState w = allAppWindows.get(i);
8631 w.mAnimLayer = w.mLayer + adj;
Joe Onorato8a9b2202010-02-26 18:56:32 -08008632 if (DEBUG_LAYERS) Slog.v(TAG, "Updating layer " + w + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008633 + w.mAnimLayer);
8634 if (w == mInputMethodTarget) {
8635 setInputMethodAnimLayerAdjustment(adj);
8636 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008637 if (w == mWallpaperTarget && mLowerWallpaperTarget == null) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008638 setWallpaperAnimLayerAdjustmentLocked(adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008639 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008640 }
8641 }
Romain Guy06882f82009-06-10 13:36:04 -07008642
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008643 void sendAppVisibilityToClients() {
8644 final int N = allAppWindows.size();
8645 for (int i=0; i<N; i++) {
8646 WindowState win = allAppWindows.get(i);
8647 if (win == startingWindow && clientHidden) {
8648 // Don't hide the starting window.
8649 continue;
8650 }
8651 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008652 if (DEBUG_VISIBILITY) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008653 "Setting visibility of " + win + ": " + (!clientHidden));
8654 win.mClient.dispatchAppVisibility(!clientHidden);
8655 } catch (RemoteException e) {
8656 }
8657 }
8658 }
Romain Guy06882f82009-06-10 13:36:04 -07008659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008660 void showAllWindowsLocked() {
8661 final int NW = allAppWindows.size();
8662 for (int i=0; i<NW; i++) {
8663 WindowState w = allAppWindows.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08008664 if (DEBUG_VISIBILITY) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008665 "performing show on: " + w);
8666 w.performShowLocked();
8667 }
8668 }
Romain Guy06882f82009-06-10 13:36:04 -07008669
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008670 // This must be called while inside a transaction.
8671 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
Dianne Hackbornde2606d2009-12-18 16:53:55 -08008672 if (!mDisplayFrozen && mPolicy.isScreenOn()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008673 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07008674
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008675 if (animation == sDummyAnimation) {
8676 // This guy is going to animate, but not yet. For now count
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008677 // it as not animating for purposes of scheduling transactions;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008678 // when it is really time to animate, this will be set to
8679 // a real animation and the next call will execute normally.
8680 return false;
8681 }
Romain Guy06882f82009-06-10 13:36:04 -07008682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008683 if ((allDrawn || animating || startingDisplayed) && animation != null) {
8684 if (!animating) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008685 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008686 TAG, "Starting animation in " + this +
8687 " @ " + currentTime + ": dw=" + dw + " dh=" + dh
8688 + " scale=" + mTransitionAnimationScale
8689 + " allDrawn=" + allDrawn + " animating=" + animating);
8690 animation.initialize(dw, dh, dw, dh);
8691 animation.setStartTime(currentTime);
8692 animating = true;
8693 }
8694 transformation.clear();
8695 final boolean more = animation.getTransformation(
8696 currentTime, transformation);
Joe Onorato8a9b2202010-02-26 18:56:32 -08008697 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008698 TAG, "Stepped animation in " + this +
8699 ": more=" + more + ", xform=" + transformation);
8700 if (more) {
8701 // we're done!
8702 hasTransformation = true;
8703 return true;
8704 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08008705 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008706 TAG, "Finished animation in " + this +
8707 " @ " + currentTime);
8708 animation = null;
8709 }
8710 } else if (animation != null) {
8711 // If the display is frozen, and there is a pending animation,
8712 // clear it and make sure we run the cleanup code.
8713 animating = true;
8714 animation = null;
8715 }
8716
8717 hasTransformation = false;
Romain Guy06882f82009-06-10 13:36:04 -07008718
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008719 if (!animating) {
8720 return false;
8721 }
8722
8723 clearAnimation();
8724 animating = false;
8725 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == this) {
8726 moveInputMethodWindowsIfNeededLocked(true);
8727 }
Romain Guy06882f82009-06-10 13:36:04 -07008728
Joe Onorato8a9b2202010-02-26 18:56:32 -08008729 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008730 TAG, "Animation done in " + this
8731 + ": reportedVisible=" + reportedVisible);
8732
8733 transformation.clear();
8734 if (animLayerAdjustment != 0) {
8735 animLayerAdjustment = 0;
8736 updateLayers();
8737 }
Romain Guy06882f82009-06-10 13:36:04 -07008738
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008739 final int N = windows.size();
8740 for (int i=0; i<N; i++) {
8741 ((WindowState)windows.get(i)).finishExit();
8742 }
8743 updateReportedVisibilityLocked();
Romain Guy06882f82009-06-10 13:36:04 -07008744
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008745 return false;
8746 }
8747
8748 void updateReportedVisibilityLocked() {
8749 if (appToken == null) {
8750 return;
8751 }
Romain Guy06882f82009-06-10 13:36:04 -07008752
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008753 int numInteresting = 0;
8754 int numVisible = 0;
8755 boolean nowGone = true;
Romain Guy06882f82009-06-10 13:36:04 -07008756
Joe Onorato8a9b2202010-02-26 18:56:32 -08008757 if (DEBUG_VISIBILITY) Slog.v(TAG, "Update reported visibility: " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008758 final int N = allAppWindows.size();
8759 for (int i=0; i<N; i++) {
8760 WindowState win = allAppWindows.get(i);
Dianne Hackborn6cf67fa2009-12-21 16:46:34 -08008761 if (win == startingWindow || win.mAppFreezing
The Android Open Source Project727cec02010-04-08 11:35:37 -07008762 || win.mViewVisibility != View.VISIBLE
8763 || win.mAttrs.type == TYPE_APPLICATION_STARTING) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008764 continue;
8765 }
8766 if (DEBUG_VISIBILITY) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008767 Slog.v(TAG, "Win " + win + ": isDrawn="
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008768 + win.isDrawnLw()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008769 + ", isAnimating=" + win.isAnimating());
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008770 if (!win.isDrawnLw()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008771 Slog.v(TAG, "Not displayed: s=" + win.mSurface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008772 + " pv=" + win.mPolicyVisibility
8773 + " dp=" + win.mDrawPending
8774 + " cdp=" + win.mCommitDrawPending
8775 + " ah=" + win.mAttachedHidden
8776 + " th="
8777 + (win.mAppToken != null
8778 ? win.mAppToken.hiddenRequested : false)
8779 + " a=" + win.mAnimating);
8780 }
8781 }
8782 numInteresting++;
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008783 if (win.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008784 if (!win.isAnimating()) {
8785 numVisible++;
8786 }
8787 nowGone = false;
8788 } else if (win.isAnimating()) {
8789 nowGone = false;
8790 }
8791 }
Romain Guy06882f82009-06-10 13:36:04 -07008792
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008793 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
Joe Onorato8a9b2202010-02-26 18:56:32 -08008794 if (DEBUG_VISIBILITY) Slog.v(TAG, "VIS " + this + ": interesting="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008795 + numInteresting + " visible=" + numVisible);
8796 if (nowVisible != reportedVisible) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008797 if (DEBUG_VISIBILITY) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008798 TAG, "Visibility changed in " + this
8799 + ": vis=" + nowVisible);
8800 reportedVisible = nowVisible;
8801 Message m = mH.obtainMessage(
8802 H.REPORT_APPLICATION_TOKEN_WINDOWS,
8803 nowVisible ? 1 : 0,
8804 nowGone ? 1 : 0,
8805 this);
8806 mH.sendMessage(m);
8807 }
8808 }
Romain Guy06882f82009-06-10 13:36:04 -07008809
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07008810 WindowState findMainWindow() {
8811 int j = windows.size();
8812 while (j > 0) {
8813 j--;
8814 WindowState win = windows.get(j);
8815 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
8816 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
8817 return win;
8818 }
8819 }
8820 return null;
8821 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008823 void dump(PrintWriter pw, String prefix) {
8824 super.dump(pw, prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008825 if (appToken != null) {
8826 pw.print(prefix); pw.println("app=true");
8827 }
8828 if (allAppWindows.size() > 0) {
8829 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
8830 }
8831 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008832 pw.print(" appFullscreen="); pw.print(appFullscreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008833 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
8834 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
8835 pw.print(" clientHidden="); pw.print(clientHidden);
8836 pw.print(" willBeHidden="); pw.print(willBeHidden);
8837 pw.print(" reportedVisible="); pw.println(reportedVisible);
8838 if (paused || freezingScreen) {
8839 pw.print(prefix); pw.print("paused="); pw.print(paused);
8840 pw.print(" freezingScreen="); pw.println(freezingScreen);
8841 }
8842 if (numInterestingWindows != 0 || numDrawnWindows != 0
8843 || inPendingTransaction || allDrawn) {
8844 pw.print(prefix); pw.print("numInterestingWindows=");
8845 pw.print(numInterestingWindows);
8846 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
8847 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
8848 pw.print(" allDrawn="); pw.println(allDrawn);
8849 }
8850 if (animating || animation != null) {
8851 pw.print(prefix); pw.print("animating="); pw.print(animating);
8852 pw.print(" animation="); pw.println(animation);
8853 }
8854 if (animLayerAdjustment != 0) {
8855 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
8856 }
8857 if (hasTransformation) {
8858 pw.print(prefix); pw.print("hasTransformation="); pw.print(hasTransformation);
8859 pw.print(" transformation="); transformation.printShortString(pw);
8860 pw.println();
8861 }
8862 if (startingData != null || removed || firstWindowDrawn) {
8863 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
8864 pw.print(" removed="); pw.print(removed);
8865 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
8866 }
8867 if (startingWindow != null || startingView != null
8868 || startingDisplayed || startingMoved) {
8869 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
8870 pw.print(" startingView="); pw.print(startingView);
8871 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
8872 pw.print(" startingMoved"); pw.println(startingMoved);
8873 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008874 }
8875
8876 @Override
8877 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008878 if (stringName == null) {
8879 StringBuilder sb = new StringBuilder();
8880 sb.append("AppWindowToken{");
8881 sb.append(Integer.toHexString(System.identityHashCode(this)));
8882 sb.append(" token="); sb.append(token); sb.append('}');
8883 stringName = sb.toString();
8884 }
8885 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008886 }
8887 }
Romain Guy06882f82009-06-10 13:36:04 -07008888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008889 // -------------------------------------------------------------
8890 // DummyAnimation
8891 // -------------------------------------------------------------
8892
8893 // This is an animation that does nothing: it just immediately finishes
8894 // itself every time it is called. It is used as a stub animation in cases
8895 // where we want to synchronize multiple things that may be animating.
8896 static final class DummyAnimation extends Animation {
8897 public boolean getTransformation(long currentTime, Transformation outTransformation) {
8898 return false;
8899 }
8900 }
8901 static final Animation sDummyAnimation = new DummyAnimation();
Romain Guy06882f82009-06-10 13:36:04 -07008902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008903 // -------------------------------------------------------------
8904 // Async Handler
8905 // -------------------------------------------------------------
8906
8907 static final class StartingData {
8908 final String pkg;
8909 final int theme;
8910 final CharSequence nonLocalizedLabel;
8911 final int labelRes;
8912 final int icon;
Romain Guy06882f82009-06-10 13:36:04 -07008913
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008914 StartingData(String _pkg, int _theme, CharSequence _nonLocalizedLabel,
8915 int _labelRes, int _icon) {
8916 pkg = _pkg;
8917 theme = _theme;
8918 nonLocalizedLabel = _nonLocalizedLabel;
8919 labelRes = _labelRes;
8920 icon = _icon;
8921 }
8922 }
8923
8924 private final class H extends Handler {
8925 public static final int REPORT_FOCUS_CHANGE = 2;
8926 public static final int REPORT_LOSING_FOCUS = 3;
8927 public static final int ANIMATE = 4;
8928 public static final int ADD_STARTING = 5;
8929 public static final int REMOVE_STARTING = 6;
8930 public static final int FINISHED_STARTING = 7;
8931 public static final int REPORT_APPLICATION_TOKEN_WINDOWS = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008932 public static final int WINDOW_FREEZE_TIMEOUT = 11;
8933 public static final int HOLD_SCREEN_CHANGED = 12;
8934 public static final int APP_TRANSITION_TIMEOUT = 13;
8935 public static final int PERSIST_ANIMATION_SCALE = 14;
8936 public static final int FORCE_GC = 15;
8937 public static final int ENABLE_SCREEN = 16;
8938 public static final int APP_FREEZE_TIMEOUT = 17;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08008939 public static final int SEND_NEW_CONFIGURATION = 18;
Romain Guy06882f82009-06-10 13:36:04 -07008940
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008941 private Session mLastReportedHold;
Romain Guy06882f82009-06-10 13:36:04 -07008942
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008943 public H() {
8944 }
Romain Guy06882f82009-06-10 13:36:04 -07008945
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008946 @Override
8947 public void handleMessage(Message msg) {
8948 switch (msg.what) {
8949 case REPORT_FOCUS_CHANGE: {
8950 WindowState lastFocus;
8951 WindowState newFocus;
Romain Guy06882f82009-06-10 13:36:04 -07008952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008953 synchronized(mWindowMap) {
8954 lastFocus = mLastFocus;
8955 newFocus = mCurrentFocus;
8956 if (lastFocus == newFocus) {
8957 // Focus is not changing, so nothing to do.
8958 return;
8959 }
8960 mLastFocus = newFocus;
Joe Onorato8a9b2202010-02-26 18:56:32 -08008961 //Slog.i(TAG, "Focus moving from " + lastFocus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008962 // + " to " + newFocus);
8963 if (newFocus != null && lastFocus != null
8964 && !newFocus.isDisplayedLw()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008965 //Slog.i(TAG, "Delaying loss of focus...");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008966 mLosingFocus.add(lastFocus);
8967 lastFocus = null;
8968 }
8969 }
8970
8971 if (lastFocus != newFocus) {
8972 //System.out.println("Changing focus from " + lastFocus
8973 // + " to " + newFocus);
8974 if (newFocus != null) {
8975 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008976 //Slog.i(TAG, "Gaining focus: " + newFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008977 newFocus.mClient.windowFocusChanged(true, mInTouchMode);
8978 } catch (RemoteException e) {
8979 // Ignore if process has died.
8980 }
8981 }
8982
8983 if (lastFocus != null) {
8984 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008985 //Slog.i(TAG, "Losing focus: " + lastFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008986 lastFocus.mClient.windowFocusChanged(false, mInTouchMode);
8987 } catch (RemoteException e) {
8988 // Ignore if process has died.
8989 }
8990 }
8991 }
8992 } break;
8993
8994 case REPORT_LOSING_FOCUS: {
8995 ArrayList<WindowState> losers;
Romain Guy06882f82009-06-10 13:36:04 -07008996
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008997 synchronized(mWindowMap) {
8998 losers = mLosingFocus;
8999 mLosingFocus = new ArrayList<WindowState>();
9000 }
9001
9002 final int N = losers.size();
9003 for (int i=0; i<N; i++) {
9004 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009005 //Slog.i(TAG, "Losing delayed focus: " + losers.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009006 losers.get(i).mClient.windowFocusChanged(false, mInTouchMode);
9007 } catch (RemoteException e) {
9008 // Ignore if process has died.
9009 }
9010 }
9011 } break;
9012
9013 case ANIMATE: {
9014 synchronized(mWindowMap) {
9015 mAnimationPending = false;
9016 performLayoutAndPlaceSurfacesLocked();
9017 }
9018 } break;
9019
9020 case ADD_STARTING: {
9021 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
9022 final StartingData sd = wtoken.startingData;
9023
9024 if (sd == null) {
9025 // Animation has been canceled... do nothing.
9026 return;
9027 }
Romain Guy06882f82009-06-10 13:36:04 -07009028
Joe Onorato8a9b2202010-02-26 18:56:32 -08009029 if (DEBUG_STARTING_WINDOW) Slog.v(TAG, "Add starting "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009030 + wtoken + ": pkg=" + sd.pkg);
Romain Guy06882f82009-06-10 13:36:04 -07009031
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009032 View view = null;
9033 try {
9034 view = mPolicy.addStartingWindow(
9035 wtoken.token, sd.pkg,
9036 sd.theme, sd.nonLocalizedLabel, sd.labelRes,
9037 sd.icon);
9038 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009039 Slog.w(TAG, "Exception when adding starting window", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009040 }
9041
9042 if (view != null) {
9043 boolean abort = false;
9044
9045 synchronized(mWindowMap) {
9046 if (wtoken.removed || wtoken.startingData == null) {
9047 // If the window was successfully added, then
9048 // we need to remove it.
9049 if (wtoken.startingWindow != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009050 if (DEBUG_STARTING_WINDOW) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009051 "Aborted starting " + wtoken
9052 + ": removed=" + wtoken.removed
9053 + " startingData=" + wtoken.startingData);
9054 wtoken.startingWindow = null;
9055 wtoken.startingData = null;
9056 abort = true;
9057 }
9058 } else {
9059 wtoken.startingView = view;
9060 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08009061 if (DEBUG_STARTING_WINDOW && !abort) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009062 "Added starting " + wtoken
9063 + ": startingWindow="
9064 + wtoken.startingWindow + " startingView="
9065 + wtoken.startingView);
9066 }
9067
9068 if (abort) {
9069 try {
9070 mPolicy.removeStartingWindow(wtoken.token, view);
9071 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009072 Slog.w(TAG, "Exception when removing starting window", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009073 }
9074 }
9075 }
9076 } break;
9077
9078 case REMOVE_STARTING: {
9079 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
9080 IBinder token = null;
9081 View view = null;
9082 synchronized (mWindowMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009083 if (DEBUG_STARTING_WINDOW) Slog.v(TAG, "Remove starting "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009084 + wtoken + ": startingWindow="
9085 + wtoken.startingWindow + " startingView="
9086 + wtoken.startingView);
9087 if (wtoken.startingWindow != null) {
9088 view = wtoken.startingView;
9089 token = wtoken.token;
9090 wtoken.startingData = null;
9091 wtoken.startingView = null;
9092 wtoken.startingWindow = null;
9093 }
9094 }
9095 if (view != null) {
9096 try {
9097 mPolicy.removeStartingWindow(token, view);
9098 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009099 Slog.w(TAG, "Exception when removing starting window", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009100 }
9101 }
9102 } break;
9103
9104 case FINISHED_STARTING: {
9105 IBinder token = null;
9106 View view = null;
9107 while (true) {
9108 synchronized (mWindowMap) {
9109 final int N = mFinishedStarting.size();
9110 if (N <= 0) {
9111 break;
9112 }
9113 AppWindowToken wtoken = mFinishedStarting.remove(N-1);
9114
Joe Onorato8a9b2202010-02-26 18:56:32 -08009115 if (DEBUG_STARTING_WINDOW) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009116 "Finished starting " + wtoken
9117 + ": startingWindow=" + wtoken.startingWindow
9118 + " startingView=" + wtoken.startingView);
9119
9120 if (wtoken.startingWindow == null) {
9121 continue;
9122 }
9123
9124 view = wtoken.startingView;
9125 token = wtoken.token;
9126 wtoken.startingData = null;
9127 wtoken.startingView = null;
9128 wtoken.startingWindow = null;
9129 }
9130
9131 try {
9132 mPolicy.removeStartingWindow(token, view);
9133 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009134 Slog.w(TAG, "Exception when removing starting window", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009135 }
9136 }
9137 } break;
9138
9139 case REPORT_APPLICATION_TOKEN_WINDOWS: {
9140 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
9141
9142 boolean nowVisible = msg.arg1 != 0;
9143 boolean nowGone = msg.arg2 != 0;
9144
9145 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009146 if (DEBUG_VISIBILITY) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009147 TAG, "Reporting visible in " + wtoken
9148 + " visible=" + nowVisible
9149 + " gone=" + nowGone);
9150 if (nowVisible) {
9151 wtoken.appToken.windowsVisible();
9152 } else {
9153 wtoken.appToken.windowsGone();
9154 }
9155 } catch (RemoteException ex) {
9156 }
9157 } break;
Romain Guy06882f82009-06-10 13:36:04 -07009158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009159 case WINDOW_FREEZE_TIMEOUT: {
9160 synchronized (mWindowMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009161 Slog.w(TAG, "Window freeze timeout expired.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009162 int i = mWindows.size();
9163 while (i > 0) {
9164 i--;
9165 WindowState w = (WindowState)mWindows.get(i);
9166 if (w.mOrientationChanging) {
9167 w.mOrientationChanging = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009168 Slog.w(TAG, "Force clearing orientation change: " + w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009169 }
9170 }
9171 performLayoutAndPlaceSurfacesLocked();
9172 }
9173 break;
9174 }
Romain Guy06882f82009-06-10 13:36:04 -07009175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009176 case HOLD_SCREEN_CHANGED: {
9177 Session oldHold;
9178 Session newHold;
9179 synchronized (mWindowMap) {
9180 oldHold = mLastReportedHold;
9181 newHold = (Session)msg.obj;
9182 mLastReportedHold = newHold;
9183 }
Romain Guy06882f82009-06-10 13:36:04 -07009184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009185 if (oldHold != newHold) {
9186 try {
9187 if (oldHold != null) {
9188 mBatteryStats.noteStopWakelock(oldHold.mUid,
9189 "window",
9190 BatteryStats.WAKE_TYPE_WINDOW);
9191 }
9192 if (newHold != null) {
9193 mBatteryStats.noteStartWakelock(newHold.mUid,
9194 "window",
9195 BatteryStats.WAKE_TYPE_WINDOW);
9196 }
9197 } catch (RemoteException e) {
9198 }
9199 }
9200 break;
9201 }
Romain Guy06882f82009-06-10 13:36:04 -07009202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009203 case APP_TRANSITION_TIMEOUT: {
9204 synchronized (mWindowMap) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009205 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009206 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009207 "*** APP TRANSITION TIMEOUT");
9208 mAppTransitionReady = true;
9209 mAppTransitionTimeout = true;
9210 performLayoutAndPlaceSurfacesLocked();
9211 }
9212 }
9213 break;
9214 }
Romain Guy06882f82009-06-10 13:36:04 -07009215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009216 case PERSIST_ANIMATION_SCALE: {
9217 Settings.System.putFloat(mContext.getContentResolver(),
9218 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
9219 Settings.System.putFloat(mContext.getContentResolver(),
9220 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
9221 break;
9222 }
Romain Guy06882f82009-06-10 13:36:04 -07009223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009224 case FORCE_GC: {
9225 synchronized(mWindowMap) {
9226 if (mAnimationPending) {
9227 // If we are animating, don't do the gc now but
9228 // delay a bit so we don't interrupt the animation.
9229 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
9230 2000);
9231 return;
9232 }
9233 // If we are currently rotating the display, it will
9234 // schedule a new message when done.
9235 if (mDisplayFrozen) {
9236 return;
9237 }
9238 mFreezeGcPending = 0;
9239 }
9240 Runtime.getRuntime().gc();
9241 break;
9242 }
Romain Guy06882f82009-06-10 13:36:04 -07009243
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009244 case ENABLE_SCREEN: {
9245 performEnableScreen();
9246 break;
9247 }
Romain Guy06882f82009-06-10 13:36:04 -07009248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009249 case APP_FREEZE_TIMEOUT: {
9250 synchronized (mWindowMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009251 Slog.w(TAG, "App freeze timeout expired.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009252 int i = mAppTokens.size();
9253 while (i > 0) {
9254 i--;
9255 AppWindowToken tok = mAppTokens.get(i);
9256 if (tok.freezingScreen) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009257 Slog.w(TAG, "Force clearing freeze: " + tok);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009258 unsetAppFreezingScreenLocked(tok, true, true);
9259 }
9260 }
9261 }
9262 break;
9263 }
Romain Guy06882f82009-06-10 13:36:04 -07009264
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009265 case SEND_NEW_CONFIGURATION: {
9266 removeMessages(SEND_NEW_CONFIGURATION);
9267 sendNewConfiguration();
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07009268 break;
9269 }
Romain Guy06882f82009-06-10 13:36:04 -07009270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009271 }
9272 }
9273 }
9274
9275 // -------------------------------------------------------------
9276 // IWindowManager API
9277 // -------------------------------------------------------------
9278
9279 public IWindowSession openSession(IInputMethodClient client,
9280 IInputContext inputContext) {
9281 if (client == null) throw new IllegalArgumentException("null client");
9282 if (inputContext == null) throw new IllegalArgumentException("null inputContext");
9283 return new Session(client, inputContext);
9284 }
9285
9286 public boolean inputMethodClientHasFocus(IInputMethodClient client) {
9287 synchronized (mWindowMap) {
9288 // The focus for the client is the window immediately below
9289 // where we would place the input method window.
9290 int idx = findDesiredInputMethodWindowIndexLocked(false);
9291 WindowState imFocus;
9292 if (idx > 0) {
9293 imFocus = (WindowState)mWindows.get(idx-1);
9294 if (imFocus != null) {
9295 if (imFocus.mSession.mClient != null &&
9296 imFocus.mSession.mClient.asBinder() == client.asBinder()) {
9297 return true;
9298 }
9299 }
9300 }
9301 }
9302 return false;
9303 }
Romain Guy06882f82009-06-10 13:36:04 -07009304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009305 // -------------------------------------------------------------
9306 // Internals
9307 // -------------------------------------------------------------
9308
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009309 final WindowState windowForClientLocked(Session session, IWindow client,
9310 boolean throwOnError) {
9311 return windowForClientLocked(session, client.asBinder(), throwOnError);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009312 }
Romain Guy06882f82009-06-10 13:36:04 -07009313
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009314 final WindowState windowForClientLocked(Session session, IBinder client,
9315 boolean throwOnError) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009316 WindowState win = mWindowMap.get(client);
Joe Onorato8a9b2202010-02-26 18:56:32 -08009317 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009318 TAG, "Looking up client " + client + ": " + win);
9319 if (win == null) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009320 RuntimeException ex = new IllegalArgumentException(
9321 "Requested window " + client + " does not exist");
9322 if (throwOnError) {
9323 throw ex;
9324 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08009325 Slog.w(TAG, "Failed looking up window", ex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009326 return null;
9327 }
9328 if (session != null && win.mSession != session) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009329 RuntimeException ex = new IllegalArgumentException(
9330 "Requested window " + client + " is in session " +
9331 win.mSession + ", not " + session);
9332 if (throwOnError) {
9333 throw ex;
9334 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08009335 Slog.w(TAG, "Failed looking up window", ex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009336 return null;
9337 }
9338
9339 return win;
9340 }
9341
Dianne Hackborna8f60182009-09-01 19:01:50 -07009342 final void rebuildAppWindowListLocked() {
9343 int NW = mWindows.size();
9344 int i;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009345 int lastWallpaper = -1;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009346 int numRemoved = 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009347
Dianne Hackborna8f60182009-09-01 19:01:50 -07009348 // First remove all existing app windows.
9349 i=0;
9350 while (i < NW) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009351 WindowState w = (WindowState)mWindows.get(i);
9352 if (w.mAppToken != null) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009353 WindowState win = (WindowState)mWindows.remove(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08009354 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG,
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009355 "Rebuild removing window: " + win);
Dianne Hackborna8f60182009-09-01 19:01:50 -07009356 NW--;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009357 numRemoved++;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009358 continue;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009359 } else if (w.mAttrs.type == WindowManager.LayoutParams.TYPE_WALLPAPER
9360 && lastWallpaper == i-1) {
9361 lastWallpaper = i;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009362 }
9363 i++;
9364 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009365
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009366 // The wallpaper window(s) typically live at the bottom of the stack,
9367 // so skip them before adding app tokens.
9368 lastWallpaper++;
9369 i = lastWallpaper;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009370
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009371 // First add all of the exiting app tokens... these are no longer
9372 // in the main app list, but still have windows shown. We put them
9373 // in the back because now that the animation is over we no longer
9374 // will care about them.
9375 int NT = mExitingAppTokens.size();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009376 for (int j=0; j<NT; j++) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009377 i = reAddAppWindowsLocked(i, mExitingAppTokens.get(j));
9378 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009379
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009380 // And add in the still active app tokens in Z order.
9381 NT = mAppTokens.size();
9382 for (int j=0; j<NT; j++) {
9383 i = reAddAppWindowsLocked(i, mAppTokens.get(j));
Dianne Hackborna8f60182009-09-01 19:01:50 -07009384 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009385
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009386 i -= lastWallpaper;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009387 if (i != numRemoved) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009388 Slog.w(TAG, "Rebuild removed " + numRemoved
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009389 + " windows but added " + i);
9390 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07009391 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009393 private final void assignLayersLocked() {
9394 int N = mWindows.size();
9395 int curBaseLayer = 0;
9396 int curLayer = 0;
9397 int i;
Romain Guy06882f82009-06-10 13:36:04 -07009398
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009399 for (i=0; i<N; i++) {
9400 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009401 if (w.mBaseLayer == curBaseLayer || w.mIsImWindow
9402 || (i > 0 && w.mIsWallpaper)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009403 curLayer += WINDOW_LAYER_MULTIPLIER;
9404 w.mLayer = curLayer;
9405 } else {
9406 curBaseLayer = curLayer = w.mBaseLayer;
9407 w.mLayer = curLayer;
9408 }
9409 if (w.mTargetAppToken != null) {
9410 w.mAnimLayer = w.mLayer + w.mTargetAppToken.animLayerAdjustment;
9411 } else if (w.mAppToken != null) {
9412 w.mAnimLayer = w.mLayer + w.mAppToken.animLayerAdjustment;
9413 } else {
9414 w.mAnimLayer = w.mLayer;
9415 }
9416 if (w.mIsImWindow) {
9417 w.mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07009418 } else if (w.mIsWallpaper) {
9419 w.mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009420 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08009421 if (DEBUG_LAYERS) Slog.v(TAG, "Assign layer " + w + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009422 + w.mAnimLayer);
9423 //System.out.println(
9424 // "Assigned layer " + curLayer + " to " + w.mClient.asBinder());
9425 }
9426 }
9427
9428 private boolean mInLayout = false;
9429 private final void performLayoutAndPlaceSurfacesLocked() {
9430 if (mInLayout) {
Dave Bortcfe65242009-04-09 14:51:04 -07009431 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009432 throw new RuntimeException("Recursive call!");
9433 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08009434 Slog.w(TAG, "performLayoutAndPlaceSurfacesLocked called while in layout");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009435 return;
9436 }
9437
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009438 if (mWaitingForConfig) {
9439 // Our configuration has changed (most likely rotation), but we
9440 // don't yet have the complete configuration to report to
9441 // applications. Don't do any window layout until we have it.
9442 return;
9443 }
9444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009445 boolean recoveringMemory = false;
9446 if (mForceRemoves != null) {
9447 recoveringMemory = true;
9448 // Wait a little it for things to settle down, and off we go.
9449 for (int i=0; i<mForceRemoves.size(); i++) {
9450 WindowState ws = mForceRemoves.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08009451 Slog.i(TAG, "Force removing: " + ws);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009452 removeWindowInnerLocked(ws.mSession, ws);
9453 }
9454 mForceRemoves = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009455 Slog.w(TAG, "Due to memory failure, waiting a bit for next layout");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009456 Object tmp = new Object();
9457 synchronized (tmp) {
9458 try {
9459 tmp.wait(250);
9460 } catch (InterruptedException e) {
9461 }
9462 }
9463 }
Romain Guy06882f82009-06-10 13:36:04 -07009464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009465 mInLayout = true;
9466 try {
9467 performLayoutAndPlaceSurfacesLockedInner(recoveringMemory);
Romain Guy06882f82009-06-10 13:36:04 -07009468
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009469 int i = mPendingRemove.size()-1;
9470 if (i >= 0) {
9471 while (i >= 0) {
9472 WindowState w = mPendingRemove.get(i);
9473 removeWindowInnerLocked(w.mSession, w);
9474 i--;
9475 }
9476 mPendingRemove.clear();
9477
9478 mInLayout = false;
9479 assignLayersLocked();
9480 mLayoutNeeded = true;
9481 performLayoutAndPlaceSurfacesLocked();
9482
9483 } else {
9484 mInLayout = false;
9485 if (mLayoutNeeded) {
9486 requestAnimationLocked(0);
9487 }
9488 }
9489 } catch (RuntimeException e) {
9490 mInLayout = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009491 Slog.e(TAG, "Unhandled exception while layout out windows", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009492 }
9493 }
9494
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009495 private final int performLayoutLockedInner() {
9496 if (!mLayoutNeeded) {
9497 return 0;
9498 }
9499
9500 mLayoutNeeded = false;
9501
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009502 final int dw = mDisplay.getWidth();
9503 final int dh = mDisplay.getHeight();
9504
9505 final int N = mWindows.size();
9506 int i;
9507
Joe Onorato8a9b2202010-02-26 18:56:32 -08009508 if (DEBUG_LAYOUT) Slog.v(TAG, "performLayout: needed="
Dianne Hackborn9b52a212009-12-11 14:51:35 -08009509 + mLayoutNeeded + " dw=" + dw + " dh=" + dh);
9510
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009511 mPolicy.beginLayoutLw(dw, dh);
Romain Guy06882f82009-06-10 13:36:04 -07009512
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009513 int seq = mLayoutSeq+1;
9514 if (seq < 0) seq = 0;
9515 mLayoutSeq = seq;
9516
9517 // First perform layout of any root windows (not attached
9518 // to another window).
9519 int topAttached = -1;
9520 for (i = N-1; i >= 0; i--) {
9521 WindowState win = (WindowState) mWindows.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009522
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009523 // Don't do layout of a window if it is not visible, or
9524 // soon won't be visible, to avoid wasting time and funky
9525 // changes while a window is animating away.
9526 final AppWindowToken atoken = win.mAppToken;
9527 final boolean gone = win.mViewVisibility == View.GONE
9528 || !win.mRelayoutCalled
9529 || win.mRootToken.hidden
9530 || (atoken != null && atoken.hiddenRequested)
9531 || win.mAttachedHidden
9532 || win.mExiting || win.mDestroying;
9533
9534 if (!win.mLayoutAttached) {
9535 if (DEBUG_LAYOUT) Slog.v(TAG, "First pass " + win
9536 + ": gone=" + gone + " mHaveFrame=" + win.mHaveFrame
9537 + " mLayoutAttached=" + win.mLayoutAttached);
9538 if (DEBUG_LAYOUT && gone) Slog.v(TAG, " (mViewVisibility="
9539 + win.mViewVisibility + " mRelayoutCalled="
9540 + win.mRelayoutCalled + " hidden="
9541 + win.mRootToken.hidden + " hiddenRequested="
9542 + (atoken != null && atoken.hiddenRequested)
9543 + " mAttachedHidden=" + win.mAttachedHidden);
9544 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009545
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009546 // If this view is GONE, then skip it -- keep the current
9547 // frame, and let the caller know so they can ignore it
9548 // if they want. (We do the normal layout for INVISIBLE
9549 // windows, since that means "perform layout as normal,
9550 // just don't display").
9551 if (!gone || !win.mHaveFrame) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009552 if (!win.mLayoutAttached) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009553 mPolicy.layoutWindowLw(win, win.mAttrs, null);
9554 win.mLayoutSeq = seq;
9555 if (DEBUG_LAYOUT) Slog.v(TAG, "-> mFrame="
9556 + win.mFrame + " mContainingFrame="
9557 + win.mContainingFrame + " mDisplayFrame="
9558 + win.mDisplayFrame);
9559 } else {
9560 if (topAttached < 0) topAttached = i;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009561 }
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009562 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009563 }
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009564
9565 // Now perform layout of attached windows, which usually
9566 // depend on the position of the window they are attached to.
9567 // XXX does not deal with windows that are attached to windows
9568 // that are themselves attached.
9569 for (i = topAttached; i >= 0; i--) {
9570 WindowState win = (WindowState) mWindows.get(i);
9571
9572 // If this view is GONE, then skip it -- keep the current
9573 // frame, and let the caller know so they can ignore it
9574 // if they want. (We do the normal layout for INVISIBLE
9575 // windows, since that means "perform layout as normal,
9576 // just don't display").
9577 if (win.mLayoutAttached) {
9578 if (DEBUG_LAYOUT) Slog.v(TAG, "Second pass " + win
9579 + " mHaveFrame=" + win.mHaveFrame
9580 + " mViewVisibility=" + win.mViewVisibility
9581 + " mRelayoutCalled=" + win.mRelayoutCalled);
9582 if ((win.mViewVisibility != View.GONE && win.mRelayoutCalled)
9583 || !win.mHaveFrame) {
9584 mPolicy.layoutWindowLw(win, win.mAttrs, win.mAttachedWindow);
9585 win.mLayoutSeq = seq;
9586 if (DEBUG_LAYOUT) Slog.v(TAG, "-> mFrame="
9587 + win.mFrame + " mContainingFrame="
9588 + win.mContainingFrame + " mDisplayFrame="
9589 + win.mDisplayFrame);
9590 }
9591 }
9592 }
9593
9594 return mPolicy.finishLayoutLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009595 }
Romain Guy06882f82009-06-10 13:36:04 -07009596
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009597 private final void performLayoutAndPlaceSurfacesLockedInner(
9598 boolean recoveringMemory) {
9599 final long currentTime = SystemClock.uptimeMillis();
9600 final int dw = mDisplay.getWidth();
9601 final int dh = mDisplay.getHeight();
9602
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009603 int i;
9604
Dianne Hackbornb601ce12010-03-01 23:36:02 -08009605 if (mFocusMayChange) {
9606 mFocusMayChange = false;
9607 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
9608 }
9609
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009610 if (mFxSession == null) {
9611 mFxSession = new SurfaceSession();
9612 }
Romain Guy06882f82009-06-10 13:36:04 -07009613
Joe Onorato8a9b2202010-02-26 18:56:32 -08009614 if (SHOW_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009615
9616 // Initialize state of exiting tokens.
9617 for (i=mExitingTokens.size()-1; i>=0; i--) {
9618 mExitingTokens.get(i).hasVisible = false;
9619 }
9620
9621 // Initialize state of exiting applications.
9622 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
9623 mExitingAppTokens.get(i).hasVisible = false;
9624 }
9625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009626 boolean orientationChangeComplete = true;
9627 Session holdScreen = null;
9628 float screenBrightness = -1;
Mike Lockwoodfb73f792009-11-20 11:31:18 -05009629 float buttonBrightness = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009630 boolean focusDisplayed = false;
9631 boolean animating = false;
9632
9633 Surface.openTransaction();
9634 try {
Dianne Hackbornde2606d2009-12-18 16:53:55 -08009635 boolean wallpaperForceHidingChanged = false;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009636 int repeats = 0;
9637 int changes = 0;
9638
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009639 do {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009640 repeats++;
9641 if (repeats > 6) {
9642 Slog.w(TAG, "Animation repeat aborted after too many iterations");
9643 mLayoutNeeded = false;
9644 break;
9645 }
9646
9647 if ((changes&(WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER
9648 | WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG
9649 | WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT)) != 0) {
9650 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
9651 if ((adjustWallpaperWindowsLocked()&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
9652 assignLayersLocked();
9653 mLayoutNeeded = true;
9654 }
9655 }
9656 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG) != 0) {
9657 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
9658 if (updateOrientationFromAppTokensLocked()) {
9659 mLayoutNeeded = true;
9660 mH.sendEmptyMessage(H.SEND_NEW_CONFIGURATION);
9661 }
9662 }
9663 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT) != 0) {
9664 mLayoutNeeded = true;
9665 }
9666 }
9667
9668 // FIRST LOOP: Perform a layout, if needed.
9669 if (repeats < 4) {
9670 changes = performLayoutLockedInner();
9671 if (changes != 0) {
9672 continue;
9673 }
9674 } else {
9675 Slog.w(TAG, "Layout repeat skipped after too many iterations");
9676 changes = 0;
9677 }
9678
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009679 final int transactionSequence = ++mTransactionSequence;
9680
9681 // Update animations of all applications, including those
9682 // associated with exiting/removed apps
9683 boolean tokensAnimating = false;
9684 final int NAT = mAppTokens.size();
9685 for (i=0; i<NAT; i++) {
9686 if (mAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
9687 tokensAnimating = true;
9688 }
9689 }
9690 final int NEAT = mExitingAppTokens.size();
9691 for (i=0; i<NEAT; i++) {
9692 if (mExitingAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
9693 tokensAnimating = true;
9694 }
9695 }
9696
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009697 // SECOND LOOP: Execute animations and update visibility of windows.
9698
Joe Onorato8a9b2202010-02-26 18:56:32 -08009699 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG, "*** ANIM STEP: seq="
Dianne Hackbornde2606d2009-12-18 16:53:55 -08009700 + transactionSequence + " tokensAnimating="
9701 + tokensAnimating);
9702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009703 animating = tokensAnimating;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009704
9705 boolean tokenMayBeDrawn = false;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009706 boolean wallpaperMayChange = false;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009707 boolean forceHiding = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009708
9709 mPolicy.beginAnimationLw(dw, dh);
9710
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009711 final int N = mWindows.size();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009712
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009713 for (i=N-1; i>=0; i--) {
9714 WindowState w = (WindowState)mWindows.get(i);
9715
9716 final WindowManager.LayoutParams attrs = w.mAttrs;
9717
9718 if (w.mSurface != null) {
9719 // Execute animation.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009720 if (w.commitFinishDrawingLocked(currentTime)) {
9721 if ((w.mAttrs.flags
9722 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009723 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009724 "First draw done in potential wallpaper target " + w);
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009725 wallpaperMayChange = true;
9726 }
9727 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009728
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009729 boolean wasAnimating = w.mAnimating;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009730 if (w.stepAnimationLocked(currentTime, dw, dh)) {
9731 animating = true;
9732 //w.dump(" ");
9733 }
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009734 if (wasAnimating && !w.mAnimating && mWallpaperTarget == w) {
9735 wallpaperMayChange = true;
9736 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009737
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009738 if (mPolicy.doesForceHide(w, attrs)) {
9739 if (!wasAnimating && animating) {
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08009740 if (DEBUG_VISIBILITY) Slog.v(TAG,
9741 "Animation done that could impact force hide: "
9742 + w);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009743 wallpaperForceHidingChanged = true;
Dianne Hackbornb601ce12010-03-01 23:36:02 -08009744 mFocusMayChange = true;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009745 } else if (w.isReadyForDisplay() && w.mAnimation == null) {
9746 forceHiding = true;
9747 }
9748 } else if (mPolicy.canBeForceHidden(w, attrs)) {
9749 boolean changed;
9750 if (forceHiding) {
9751 changed = w.hideLw(false, false);
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08009752 if (DEBUG_VISIBILITY && changed) Slog.v(TAG,
9753 "Now policy hidden: " + w);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009754 } else {
9755 changed = w.showLw(false, false);
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08009756 if (DEBUG_VISIBILITY && changed) Slog.v(TAG,
9757 "Now policy shown: " + w);
9758 if (changed) {
9759 if (wallpaperForceHidingChanged
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009760 && w.isVisibleNow() /*w.isReadyForDisplay()*/) {
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08009761 // Assume we will need to animate. If
9762 // we don't (because the wallpaper will
9763 // stay with the lock screen), then we will
9764 // clean up later.
9765 Animation a = mPolicy.createForceHideEnterAnimation();
9766 if (a != null) {
9767 w.setAnimation(a);
9768 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009769 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08009770 if (mCurrentFocus == null ||
9771 mCurrentFocus.mLayer < w.mLayer) {
9772 // We are showing on to of the current
9773 // focus, so re-evaluate focus to make
9774 // sure it is correct.
9775 mFocusMayChange = true;
9776 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009777 }
9778 }
9779 if (changed && (attrs.flags
9780 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
9781 wallpaperMayChange = true;
9782 }
9783 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009784
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009785 mPolicy.animatingWindowLw(w, attrs);
9786 }
9787
9788 final AppWindowToken atoken = w.mAppToken;
9789 if (atoken != null && (!atoken.allDrawn || atoken.freezingScreen)) {
9790 if (atoken.lastTransactionSequence != transactionSequence) {
9791 atoken.lastTransactionSequence = transactionSequence;
9792 atoken.numInterestingWindows = atoken.numDrawnWindows = 0;
9793 atoken.startingDisplayed = false;
9794 }
9795 if ((w.isOnScreen() || w.mAttrs.type
9796 == WindowManager.LayoutParams.TYPE_BASE_APPLICATION)
9797 && !w.mExiting && !w.mDestroying) {
9798 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009799 Slog.v(TAG, "Eval win " + w + ": isDrawn="
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009800 + w.isDrawnLw()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009801 + ", isAnimating=" + w.isAnimating());
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009802 if (!w.isDrawnLw()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009803 Slog.v(TAG, "Not displayed: s=" + w.mSurface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009804 + " pv=" + w.mPolicyVisibility
9805 + " dp=" + w.mDrawPending
9806 + " cdp=" + w.mCommitDrawPending
9807 + " ah=" + w.mAttachedHidden
9808 + " th=" + atoken.hiddenRequested
9809 + " a=" + w.mAnimating);
9810 }
9811 }
9812 if (w != atoken.startingWindow) {
9813 if (!atoken.freezingScreen || !w.mAppFreezing) {
9814 atoken.numInterestingWindows++;
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009815 if (w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009816 atoken.numDrawnWindows++;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009817 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009818 "tokenMayBeDrawn: " + atoken
9819 + " freezingScreen=" + atoken.freezingScreen
9820 + " mAppFreezing=" + w.mAppFreezing);
9821 tokenMayBeDrawn = true;
9822 }
9823 }
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009824 } else if (w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009825 atoken.startingDisplayed = true;
9826 }
9827 }
9828 } else if (w.mReadyToShow) {
9829 w.performShowLocked();
9830 }
9831 }
9832
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009833 changes |= mPolicy.finishAnimationLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009834
9835 if (tokenMayBeDrawn) {
9836 // See if any windows have been drawn, so they (and others
9837 // associated with them) can now be shown.
9838 final int NT = mTokenList.size();
9839 for (i=0; i<NT; i++) {
9840 AppWindowToken wtoken = mTokenList.get(i).appWindowToken;
9841 if (wtoken == null) {
9842 continue;
9843 }
9844 if (wtoken.freezingScreen) {
9845 int numInteresting = wtoken.numInterestingWindows;
9846 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009847 if (DEBUG_VISIBILITY) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009848 "allDrawn: " + wtoken
9849 + " interesting=" + numInteresting
9850 + " drawn=" + wtoken.numDrawnWindows);
9851 wtoken.showAllWindowsLocked();
9852 unsetAppFreezingScreenLocked(wtoken, false, true);
9853 orientationChangeComplete = true;
9854 }
9855 } else if (!wtoken.allDrawn) {
9856 int numInteresting = wtoken.numInterestingWindows;
9857 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009858 if (DEBUG_VISIBILITY) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009859 "allDrawn: " + wtoken
9860 + " interesting=" + numInteresting
9861 + " drawn=" + wtoken.numDrawnWindows);
9862 wtoken.allDrawn = true;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009863 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_ANIM;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009864
9865 // We can now show all of the drawn windows!
9866 if (!mOpeningApps.contains(wtoken)) {
9867 wtoken.showAllWindowsLocked();
9868 }
9869 }
9870 }
9871 }
9872 }
9873
9874 // If we are ready to perform an app transition, check through
9875 // all of the app tokens to be shown and see if they are ready
9876 // to go.
9877 if (mAppTransitionReady) {
9878 int NN = mOpeningApps.size();
9879 boolean goodToGo = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009880 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009881 "Checking " + NN + " opening apps (frozen="
9882 + mDisplayFrozen + " timeout="
9883 + mAppTransitionTimeout + ")...");
9884 if (!mDisplayFrozen && !mAppTransitionTimeout) {
9885 // If the display isn't frozen, wait to do anything until
9886 // all of the apps are ready. Otherwise just go because
9887 // we'll unfreeze the display when everyone is ready.
9888 for (i=0; i<NN && goodToGo; i++) {
9889 AppWindowToken wtoken = mOpeningApps.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08009890 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009891 "Check opening app" + wtoken + ": allDrawn="
9892 + wtoken.allDrawn + " startingDisplayed="
9893 + wtoken.startingDisplayed);
9894 if (!wtoken.allDrawn && !wtoken.startingDisplayed
9895 && !wtoken.startingMoved) {
9896 goodToGo = false;
9897 }
9898 }
9899 }
9900 if (goodToGo) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009901 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG, "**** GOOD TO GO");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009902 int transit = mNextAppTransition;
9903 if (mSkipAppTransitionAnimation) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009904 transit = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009905 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009906 mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009907 mAppTransitionReady = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009908 mAppTransitionRunning = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009909 mAppTransitionTimeout = false;
9910 mStartingIconInTransition = false;
9911 mSkipAppTransitionAnimation = false;
9912
9913 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
9914
Dianne Hackborna8f60182009-09-01 19:01:50 -07009915 // If there are applications waiting to come to the
9916 // top of the stack, now is the time to move their windows.
9917 // (Note that we don't do apps going to the bottom
9918 // here -- we want to keep their windows in the old
9919 // Z-order until the animation completes.)
9920 if (mToTopApps.size() > 0) {
9921 NN = mAppTokens.size();
9922 for (i=0; i<NN; i++) {
9923 AppWindowToken wtoken = mAppTokens.get(i);
9924 if (wtoken.sendingToTop) {
9925 wtoken.sendingToTop = false;
9926 moveAppWindowsLocked(wtoken, NN, false);
9927 }
9928 }
9929 mToTopApps.clear();
9930 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009931
Dianne Hackborn25994b42009-09-04 14:21:19 -07009932 WindowState oldWallpaper = mWallpaperTarget;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009933
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009934 adjustWallpaperWindowsLocked();
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009935 wallpaperMayChange = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009936
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009937 // The top-most window will supply the layout params,
9938 // and we will determine it below.
9939 LayoutParams animLp = null;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009940 AppWindowToken animToken = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009941 int bestAnimLayer = -1;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009942
Joe Onorato8a9b2202010-02-26 18:56:32 -08009943 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009944 "New wallpaper target=" + mWallpaperTarget
9945 + ", lower target=" + mLowerWallpaperTarget
9946 + ", upper target=" + mUpperWallpaperTarget);
Dianne Hackborn25994b42009-09-04 14:21:19 -07009947 int foundWallpapers = 0;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009948 // Do a first pass through the tokens for two
9949 // things:
9950 // (1) Determine if both the closing and opening
9951 // app token sets are wallpaper targets, in which
9952 // case special animations are needed
9953 // (since the wallpaper needs to stay static
9954 // behind them).
9955 // (2) Find the layout params of the top-most
9956 // application window in the tokens, which is
9957 // what will control the animation theme.
9958 final int NC = mClosingApps.size();
9959 NN = NC + mOpeningApps.size();
9960 for (i=0; i<NN; i++) {
9961 AppWindowToken wtoken;
9962 int mode;
9963 if (i < NC) {
9964 wtoken = mClosingApps.get(i);
9965 mode = 1;
9966 } else {
9967 wtoken = mOpeningApps.get(i-NC);
9968 mode = 2;
9969 }
9970 if (mLowerWallpaperTarget != null) {
9971 if (mLowerWallpaperTarget.mAppToken == wtoken
9972 || mUpperWallpaperTarget.mAppToken == wtoken) {
9973 foundWallpapers |= mode;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009974 }
9975 }
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009976 if (wtoken.appFullscreen) {
9977 WindowState ws = wtoken.findMainWindow();
9978 if (ws != null) {
9979 // If this is a compatibility mode
9980 // window, we will always use its anim.
9981 if ((ws.mAttrs.flags&FLAG_COMPATIBLE_WINDOW) != 0) {
9982 animLp = ws.mAttrs;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009983 animToken = ws.mAppToken;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009984 bestAnimLayer = Integer.MAX_VALUE;
9985 } else if (ws.mLayer > bestAnimLayer) {
9986 animLp = ws.mAttrs;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009987 animToken = ws.mAppToken;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009988 bestAnimLayer = ws.mLayer;
9989 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07009990 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009991 }
9992 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009993
Dianne Hackborn25994b42009-09-04 14:21:19 -07009994 if (foundWallpapers == 3) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009995 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -07009996 "Wallpaper animation!");
9997 switch (transit) {
9998 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
9999 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
10000 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
10001 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN;
10002 break;
10003 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
10004 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
10005 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
10006 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE;
10007 break;
10008 }
Joe Onorato8a9b2202010-02-26 18:56:32 -080010009 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -070010010 "New transit: " + transit);
10011 } else if (oldWallpaper != null) {
10012 // We are transitioning from an activity with
10013 // a wallpaper to one without.
10014 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010015 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -070010016 "New transit away from wallpaper: " + transit);
10017 } else if (mWallpaperTarget != null) {
10018 // We are transitioning from an activity without
10019 // a wallpaper to now showing the wallpaper
10020 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010021 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -070010022 "New transit into wallpaper: " + transit);
10023 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010024
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010025 if ((transit&WindowManagerPolicy.TRANSIT_ENTER_MASK) != 0) {
10026 mLastEnterAnimToken = animToken;
10027 mLastEnterAnimParams = animLp;
10028 } else if (mLastEnterAnimParams != null) {
10029 animLp = mLastEnterAnimParams;
10030 mLastEnterAnimToken = null;
10031 mLastEnterAnimParams = null;
10032 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010033
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010034 // If all closing windows are obscured, then there is
10035 // no need to do an animation. This is the case, for
10036 // example, when this transition is being done behind
10037 // the lock screen.
10038 if (!mPolicy.allowAppAnimationsLw()) {
10039 animLp = null;
10040 }
10041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010042 NN = mOpeningApps.size();
10043 for (i=0; i<NN; i++) {
10044 AppWindowToken wtoken = mOpeningApps.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -080010045 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010046 "Now opening app" + wtoken);
10047 wtoken.reportedVisible = false;
10048 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -070010049 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -070010050 setTokenVisibilityLocked(wtoken, animLp, true, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010051 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -070010052 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010053 wtoken.showAllWindowsLocked();
10054 }
10055 NN = mClosingApps.size();
10056 for (i=0; i<NN; i++) {
10057 AppWindowToken wtoken = mClosingApps.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -080010058 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010059 "Now closing app" + wtoken);
10060 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -070010061 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -070010062 setTokenVisibilityLocked(wtoken, animLp, false, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010063 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -070010064 wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010065 // Force the allDrawn flag, because we want to start
10066 // this guy's animations regardless of whether it's
10067 // gotten drawn.
10068 wtoken.allDrawn = true;
10069 }
10070
Dianne Hackborn8b571a82009-09-25 16:09:43 -070010071 mNextAppTransitionPackage = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010072
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010073 mOpeningApps.clear();
10074 mClosingApps.clear();
10075
10076 // This has changed the visibility of windows, so perform
10077 // a new layout to get them all up-to-date.
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010078 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_LAYOUT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010079 mLayoutNeeded = true;
Dianne Hackborn20583ff2009-07-27 21:51:05 -070010080 if (!moveInputMethodWindowsIfNeededLocked(true)) {
10081 assignLayersLocked();
10082 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010083 updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES);
Dianne Hackbornb601ce12010-03-01 23:36:02 -080010084 mFocusMayChange = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010085 }
10086 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010087
Dianne Hackborn16064f92010-03-25 00:47:24 -070010088 int adjResult = 0;
10089
Dianne Hackborna8f60182009-09-01 19:01:50 -070010090 if (!animating && mAppTransitionRunning) {
10091 // We have finished the animation of an app transition. To do
10092 // this, we have delayed a lot of operations like showing and
10093 // hiding apps, moving apps in Z-order, etc. The app token list
10094 // reflects the correct Z-order, but the window list may now
10095 // be out of sync with it. So here we will just rebuild the
10096 // entire app window list. Fun!
10097 mAppTransitionRunning = false;
10098 // Clear information about apps that were moving.
10099 mToBottomApps.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010100
Dianne Hackborna8f60182009-09-01 19:01:50 -070010101 rebuildAppWindowListLocked();
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010102 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_LAYOUT;
Dianne Hackborn16064f92010-03-25 00:47:24 -070010103 adjResult |= ADJUST_WALLPAPER_LAYERS_CHANGED;
Dianne Hackborna8f60182009-09-01 19:01:50 -070010104 moveInputMethodWindowsIfNeededLocked(false);
10105 wallpaperMayChange = true;
Suchi Amalapurapuc9568e32009-11-05 18:51:16 -080010106 // Since the window list has been rebuilt, focus might
10107 // have to be recomputed since the actual order of windows
10108 // might have changed again.
Dianne Hackbornb601ce12010-03-01 23:36:02 -080010109 mFocusMayChange = true;
Dianne Hackborna8f60182009-09-01 19:01:50 -070010110 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010111
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010112 if (wallpaperForceHidingChanged && changes == 0 && !mAppTransitionReady) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010113 // At this point, there was a window with a wallpaper that
10114 // was force hiding other windows behind it, but now it
10115 // is going away. This may be simple -- just animate
10116 // away the wallpaper and its window -- or it may be
10117 // hard -- the wallpaper now needs to be shown behind
10118 // something that was hidden.
10119 WindowState oldWallpaper = mWallpaperTarget;
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010120 if (mLowerWallpaperTarget != null
10121 && mLowerWallpaperTarget.mAppToken != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010122 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010123 "wallpaperForceHiding changed with lower="
10124 + mLowerWallpaperTarget);
Joe Onorato8a9b2202010-02-26 18:56:32 -080010125 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010126 "hidden=" + mLowerWallpaperTarget.mAppToken.hidden +
10127 " hiddenRequested=" + mLowerWallpaperTarget.mAppToken.hiddenRequested);
10128 if (mLowerWallpaperTarget.mAppToken.hidden) {
10129 // The lower target has become hidden before we
10130 // actually started the animation... let's completely
10131 // re-evaluate everything.
10132 mLowerWallpaperTarget = mUpperWallpaperTarget = null;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010133 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_ANIM;
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010134 }
10135 }
Dianne Hackborn16064f92010-03-25 00:47:24 -070010136 adjResult |= adjustWallpaperWindowsLocked();
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010137 wallpaperMayChange = false;
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010138 wallpaperForceHidingChanged = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010139 if (DEBUG_WALLPAPER) Slog.v(TAG, "****** OLD: " + oldWallpaper
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010140 + " NEW: " + mWallpaperTarget
10141 + " LOWER: " + mLowerWallpaperTarget);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010142 if (mLowerWallpaperTarget == null) {
10143 // Whoops, we don't need a special wallpaper animation.
10144 // Clear them out.
10145 forceHiding = false;
10146 for (i=N-1; i>=0; i--) {
10147 WindowState w = (WindowState)mWindows.get(i);
10148 if (w.mSurface != null) {
10149 final WindowManager.LayoutParams attrs = w.mAttrs;
Suchi Amalapurapuc03d28b2009-10-28 14:32:05 -070010150 if (mPolicy.doesForceHide(w, attrs) && w.isVisibleLw()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010151 if (DEBUG_FOCUS) Slog.i(TAG, "win=" + w + " force hides other windows");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010152 forceHiding = true;
10153 } else if (mPolicy.canBeForceHidden(w, attrs)) {
10154 if (!w.mAnimating) {
10155 // We set the animation above so it
10156 // is not yet running.
10157 w.clearAnimation();
10158 }
10159 }
10160 }
10161 }
10162 }
10163 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010164
Dianne Hackborn6c3f5712009-08-25 18:42:59 -070010165 if (wallpaperMayChange) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010166 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010167 "Wallpaper may change! Adjusting");
Dianne Hackborn16064f92010-03-25 00:47:24 -070010168 adjResult |= adjustWallpaperWindowsLocked();
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010169 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010170
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010171 if ((adjResult&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010172 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010173 "Wallpaper layer changed: assigning layers + relayout");
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010174 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_LAYOUT;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010175 assignLayersLocked();
10176 } else if ((adjResult&ADJUST_WALLPAPER_VISIBILITY_CHANGED) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010177 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010178 "Wallpaper visibility changed: relayout");
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010179 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_LAYOUT;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010180 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010181
Dianne Hackbornb601ce12010-03-01 23:36:02 -080010182 if (mFocusMayChange) {
10183 mFocusMayChange = false;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010184 if (updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES)) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010185 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_ANIM;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010186 adjResult = 0;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -070010187 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010188 }
10189
10190 if (mLayoutNeeded) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010191 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_LAYOUT;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -070010192 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010193
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010194 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG, "*** ANIM STEP: changes=0x"
10195 + Integer.toHexString(changes));
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010196
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010197 } while (changes != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010198
10199 // THIRD LOOP: Update the surfaces of all windows.
10200
10201 final boolean someoneLosingFocus = mLosingFocus.size() != 0;
10202
10203 boolean obscured = false;
10204 boolean blurring = false;
10205 boolean dimming = false;
10206 boolean covered = false;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -070010207 boolean syswin = false;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010208 boolean backgroundFillerShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010209
Dianne Hackbornbdd52b22009-09-02 21:46:19 -070010210 final int N = mWindows.size();
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010212 for (i=N-1; i>=0; i--) {
10213 WindowState w = (WindowState)mWindows.get(i);
10214
10215 boolean displayed = false;
10216 final WindowManager.LayoutParams attrs = w.mAttrs;
10217 final int attrFlags = attrs.flags;
10218
10219 if (w.mSurface != null) {
Dianne Hackbornac3587d2010-03-11 11:12:11 -080010220 // XXX NOTE: The logic here could be improved. We have
10221 // the decision about whether to resize a window separated
10222 // from whether to hide the surface. This can cause us to
10223 // resize a surface even if we are going to hide it. You
10224 // can see this by (1) holding device in landscape mode on
10225 // home screen; (2) tapping browser icon (device will rotate
10226 // to landscape; (3) tap home. The wallpaper will be resized
10227 // in step 2 but then immediately hidden, causing us to
10228 // have to resize and then redraw it again in step 3. It
10229 // would be nice to figure out how to avoid this, but it is
10230 // difficult because we do need to resize surfaces in some
10231 // cases while they are hidden such as when first showing a
10232 // window.
10233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010234 w.computeShownFrameLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -080010235 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010236 TAG, "Placing surface #" + i + " " + w.mSurface
10237 + ": new=" + w.mShownFrame + ", old="
10238 + w.mLastShownFrame);
10239
10240 boolean resize;
10241 int width, height;
10242 if ((w.mAttrs.flags & w.mAttrs.FLAG_SCALED) != 0) {
10243 resize = w.mLastRequestedWidth != w.mRequestedWidth ||
10244 w.mLastRequestedHeight != w.mRequestedHeight;
10245 // for a scaled surface, we just want to use
10246 // the requested size.
10247 width = w.mRequestedWidth;
10248 height = w.mRequestedHeight;
10249 w.mLastRequestedWidth = width;
10250 w.mLastRequestedHeight = height;
10251 w.mLastShownFrame.set(w.mShownFrame);
10252 try {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010253 if (SHOW_TRANSACTIONS) logSurface(w,
10254 "POS " + w.mShownFrame.left
10255 + ", " + w.mShownFrame.top, null);
Dianne Hackborn16064f92010-03-25 00:47:24 -070010256 w.mSurfaceX = w.mShownFrame.left;
10257 w.mSurfaceY = w.mShownFrame.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010258 w.mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
10259 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010260 Slog.w(TAG, "Error positioning surface in " + w, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010261 if (!recoveringMemory) {
10262 reclaimSomeSurfaceMemoryLocked(w, "position");
10263 }
10264 }
10265 } else {
10266 resize = !w.mLastShownFrame.equals(w.mShownFrame);
10267 width = w.mShownFrame.width();
10268 height = w.mShownFrame.height();
10269 w.mLastShownFrame.set(w.mShownFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010270 }
10271
10272 if (resize) {
10273 if (width < 1) width = 1;
10274 if (height < 1) height = 1;
10275 if (w.mSurface != null) {
10276 try {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010277 if (SHOW_TRANSACTIONS) logSurface(w,
10278 "POS " + w.mShownFrame.left + ","
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010279 + w.mShownFrame.top + " SIZE "
10280 + w.mShownFrame.width() + "x"
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010281 + w.mShownFrame.height(), null);
Dianne Hackbornac3587d2010-03-11 11:12:11 -080010282 w.mSurfaceResized = true;
Dianne Hackborn16064f92010-03-25 00:47:24 -070010283 w.mSurfaceW = width;
10284 w.mSurfaceH = height;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010285 w.mSurface.setSize(width, height);
Dianne Hackborn16064f92010-03-25 00:47:24 -070010286 w.mSurfaceX = w.mShownFrame.left;
10287 w.mSurfaceY = w.mShownFrame.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010288 w.mSurface.setPosition(w.mShownFrame.left,
10289 w.mShownFrame.top);
10290 } catch (RuntimeException e) {
10291 // If something goes wrong with the surface (such
10292 // as running out of memory), don't take down the
10293 // entire system.
Joe Onorato8a9b2202010-02-26 18:56:32 -080010294 Slog.e(TAG, "Failure updating surface of " + w
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010295 + "size=(" + width + "x" + height
10296 + "), pos=(" + w.mShownFrame.left
10297 + "," + w.mShownFrame.top + ")", e);
10298 if (!recoveringMemory) {
10299 reclaimSomeSurfaceMemoryLocked(w, "size");
10300 }
10301 }
10302 }
10303 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010304 if (!w.mAppFreezing && w.mLayoutSeq == mLayoutSeq) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010305 w.mContentInsetsChanged =
10306 !w.mLastContentInsets.equals(w.mContentInsets);
10307 w.mVisibleInsetsChanged =
10308 !w.mLastVisibleInsets.equals(w.mVisibleInsets);
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010309 boolean configChanged =
10310 w.mConfiguration != mCurConfiguration
10311 && (w.mConfiguration == null
10312 || mCurConfiguration.diff(w.mConfiguration) != 0);
Dianne Hackborn694f79b2010-03-17 19:44:59 -070010313 if (DEBUG_CONFIGURATION && configChanged) {
10314 Slog.v(TAG, "Win " + w + " config changed: "
10315 + mCurConfiguration);
10316 }
Joe Onorato8a9b2202010-02-26 18:56:32 -080010317 if (localLOGV) Slog.v(TAG, "Resizing " + w
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010318 + ": configChanged=" + configChanged
10319 + " last=" + w.mLastFrame + " frame=" + w.mFrame);
Romain Guy06882f82009-06-10 13:36:04 -070010320 if (!w.mLastFrame.equals(w.mFrame)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010321 || w.mContentInsetsChanged
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010322 || w.mVisibleInsetsChanged
Dianne Hackbornac3587d2010-03-11 11:12:11 -080010323 || w.mSurfaceResized
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010324 || configChanged) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010325 w.mLastFrame.set(w.mFrame);
10326 w.mLastContentInsets.set(w.mContentInsets);
10327 w.mLastVisibleInsets.set(w.mVisibleInsets);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010328 // If the screen is currently frozen, then keep
10329 // it frozen until this window draws at its new
10330 // orientation.
10331 if (mDisplayFrozen) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010332 if (DEBUG_ORIENTATION) Slog.v(TAG,
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010333 "Resizing while display frozen: " + w);
10334 w.mOrientationChanging = true;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010335 if (!mWindowsFreezingScreen) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010336 mWindowsFreezingScreen = true;
10337 // XXX should probably keep timeout from
10338 // when we first froze the display.
10339 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
10340 mH.sendMessageDelayed(mH.obtainMessage(
10341 H.WINDOW_FREEZE_TIMEOUT), 2000);
10342 }
10343 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010344 // If the orientation is changing, then we need to
10345 // hold off on unfreezing the display until this
10346 // window has been redrawn; to do that, we need
10347 // to go through the process of getting informed
10348 // by the application when it has finished drawing.
10349 if (w.mOrientationChanging) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010350 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010351 "Orientation start waiting for draw in "
10352 + w + ", surface " + w.mSurface);
10353 w.mDrawPending = true;
10354 w.mCommitDrawPending = false;
10355 w.mReadyToShow = false;
10356 if (w.mAppToken != null) {
10357 w.mAppToken.allDrawn = false;
10358 }
10359 }
Dianne Hackbornac3587d2010-03-11 11:12:11 -080010360 if (DEBUG_RESIZE || DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010361 "Resizing window " + w + " to " + w.mFrame);
10362 mResizingWindows.add(w);
10363 } else if (w.mOrientationChanging) {
10364 if (!w.mDrawPending && !w.mCommitDrawPending) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010365 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010366 "Orientation not waiting for draw in "
10367 + w + ", surface " + w.mSurface);
10368 w.mOrientationChanging = false;
10369 }
10370 }
10371 }
10372
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010373 if (w.mAttachedHidden || !w.isReadyForDisplay()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010374 if (!w.mLastHidden) {
10375 //dump();
Dianne Hackborn5943c202010-04-12 21:36:49 -070010376 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Window hiding: waitingToShow="
10377 + w.mRootToken.waitingToShow + " polvis="
10378 + w.mPolicyVisibility + " atthid="
10379 + w.mAttachedHidden + " tokhid="
10380 + w.mRootToken.hidden + " vis="
10381 + w.mViewVisibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010382 w.mLastHidden = true;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010383 if (SHOW_TRANSACTIONS) logSurface(w,
10384 "HIDE (performLayout)", null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010385 if (w.mSurface != null) {
Dianne Hackborn16064f92010-03-25 00:47:24 -070010386 w.mSurfaceShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010387 try {
10388 w.mSurface.hide();
10389 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010390 Slog.w(TAG, "Exception hiding surface in " + w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010391 }
10392 }
10393 mKeyWaiter.releasePendingPointerLocked(w.mSession);
10394 }
10395 // If we are waiting for this window to handle an
10396 // orientation change, well, it is hidden, so
10397 // doesn't really matter. Note that this does
10398 // introduce a potential glitch if the window
10399 // becomes unhidden before it has drawn for the
10400 // new orientation.
10401 if (w.mOrientationChanging) {
10402 w.mOrientationChanging = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010403 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010404 "Orientation change skips hidden " + w);
10405 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010406 } else if (w.mLastLayer != w.mAnimLayer
10407 || w.mLastAlpha != w.mShownAlpha
10408 || w.mLastDsDx != w.mDsDx
10409 || w.mLastDtDx != w.mDtDx
10410 || w.mLastDsDy != w.mDsDy
10411 || w.mLastDtDy != w.mDtDy
10412 || w.mLastHScale != w.mHScale
10413 || w.mLastVScale != w.mVScale
10414 || w.mLastHidden) {
10415 displayed = true;
10416 w.mLastAlpha = w.mShownAlpha;
10417 w.mLastLayer = w.mAnimLayer;
10418 w.mLastDsDx = w.mDsDx;
10419 w.mLastDtDx = w.mDtDx;
10420 w.mLastDsDy = w.mDsDy;
10421 w.mLastDtDy = w.mDtDy;
10422 w.mLastHScale = w.mHScale;
10423 w.mLastVScale = w.mVScale;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010424 if (SHOW_TRANSACTIONS) logSurface(w,
10425 "alpha=" + w.mShownAlpha + " layer=" + w.mAnimLayer
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010426 + " matrix=[" + (w.mDsDx*w.mHScale)
10427 + "," + (w.mDtDx*w.mVScale)
10428 + "][" + (w.mDsDy*w.mHScale)
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010429 + "," + (w.mDtDy*w.mVScale) + "]", null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010430 if (w.mSurface != null) {
10431 try {
Dianne Hackborn16064f92010-03-25 00:47:24 -070010432 w.mSurfaceAlpha = w.mShownAlpha;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010433 w.mSurface.setAlpha(w.mShownAlpha);
Dianne Hackborn16064f92010-03-25 00:47:24 -070010434 w.mSurfaceLayer = w.mAnimLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010435 w.mSurface.setLayer(w.mAnimLayer);
10436 w.mSurface.setMatrix(
10437 w.mDsDx*w.mHScale, w.mDtDx*w.mVScale,
10438 w.mDsDy*w.mHScale, w.mDtDy*w.mVScale);
10439 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010440 Slog.w(TAG, "Error updating surface in " + w, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010441 if (!recoveringMemory) {
10442 reclaimSomeSurfaceMemoryLocked(w, "update");
10443 }
10444 }
10445 }
10446
10447 if (w.mLastHidden && !w.mDrawPending
10448 && !w.mCommitDrawPending
10449 && !w.mReadyToShow) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010450 if (SHOW_TRANSACTIONS) logSurface(w,
10451 "SHOW (performLayout)", null);
Joe Onorato8a9b2202010-02-26 18:56:32 -080010452 if (DEBUG_VISIBILITY) Slog.v(TAG, "Showing " + w
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010453 + " during relayout");
10454 if (showSurfaceRobustlyLocked(w)) {
10455 w.mHasDrawn = true;
10456 w.mLastHidden = false;
10457 } else {
10458 w.mOrientationChanging = false;
10459 }
10460 }
10461 if (w.mSurface != null) {
10462 w.mToken.hasVisible = true;
10463 }
10464 } else {
10465 displayed = true;
10466 }
10467
10468 if (displayed) {
10469 if (!covered) {
Romain Guy980a9382010-01-08 15:06:28 -080010470 if (attrs.width == LayoutParams.MATCH_PARENT
10471 && attrs.height == LayoutParams.MATCH_PARENT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010472 covered = true;
10473 }
10474 }
10475 if (w.mOrientationChanging) {
10476 if (w.mDrawPending || w.mCommitDrawPending) {
10477 orientationChangeComplete = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010478 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010479 "Orientation continue waiting for draw in " + w);
10480 } else {
10481 w.mOrientationChanging = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010482 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010483 "Orientation change complete in " + w);
10484 }
10485 }
10486 w.mToken.hasVisible = true;
10487 }
10488 } else if (w.mOrientationChanging) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010489 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010490 "Orientation change skips hidden " + w);
10491 w.mOrientationChanging = false;
10492 }
10493
10494 final boolean canBeSeen = w.isDisplayedLw();
10495
10496 if (someoneLosingFocus && w == mCurrentFocus && canBeSeen) {
10497 focusDisplayed = true;
10498 }
10499
Dianne Hackborne9e9bca2009-08-18 15:08:22 -070010500 final boolean obscuredChanged = w.mObscured != obscured;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010501
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010502 // Update effect.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010503 if (!(w.mObscured=obscured)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010504 if (w.mSurface != null) {
10505 if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
10506 holdScreen = w.mSession;
10507 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -070010508 if (!syswin && w.mAttrs.screenBrightness >= 0
10509 && screenBrightness < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010510 screenBrightness = w.mAttrs.screenBrightness;
10511 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -050010512 if (!syswin && w.mAttrs.buttonBrightness >= 0
10513 && buttonBrightness < 0) {
10514 buttonBrightness = w.mAttrs.buttonBrightness;
10515 }
Mike Lockwood46af6a82010-03-09 08:28:22 -050010516 if (canBeSeen
10517 && (attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG
10518 || attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD
10519 || attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_ERROR)) {
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -070010520 syswin = true;
10521 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010522 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010523
Dianne Hackborn25994b42009-09-04 14:21:19 -070010524 boolean opaqueDrawn = canBeSeen && w.isOpaqueDrawn();
10525 if (opaqueDrawn && w.isFullscreen(dw, dh)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010526 // This window completely covers everything behind it,
10527 // so we want to leave all of them as unblurred (for
10528 // performance reasons).
10529 obscured = true;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010530 } else if (opaqueDrawn && w.needsBackgroundFiller(dw, dh)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010531 if (SHOW_TRANSACTIONS) Slog.d(TAG, "showing background filler");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010532 // This window is in compatibility mode, and needs background filler.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010533 obscured = true;
10534 if (mBackgroundFillerSurface == null) {
10535 try {
10536 mBackgroundFillerSurface = new Surface(mFxSession, 0,
Mathias Agopian5d26c1e2010-03-01 16:09:43 -080010537 "BackGroundFiller",
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010538 0, dw, dh,
10539 PixelFormat.OPAQUE,
10540 Surface.FX_SURFACE_NORMAL);
10541 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010542 Slog.e(TAG, "Exception creating filler surface", e);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010543 }
10544 }
10545 try {
10546 mBackgroundFillerSurface.setPosition(0, 0);
10547 mBackgroundFillerSurface.setSize(dw, dh);
10548 // Using the same layer as Dim because they will never be shown at the
10549 // same time.
10550 mBackgroundFillerSurface.setLayer(w.mAnimLayer - 1);
10551 mBackgroundFillerSurface.show();
10552 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010553 Slog.e(TAG, "Exception showing filler surface");
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010554 }
10555 backgroundFillerShown = true;
10556 mBackgroundFillerShown = true;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010557 } else if (canBeSeen && !obscured &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010558 (attrFlags&FLAG_BLUR_BEHIND|FLAG_DIM_BEHIND) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010559 if (localLOGV) Slog.v(TAG, "Win " + w
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010560 + ": blurring=" + blurring
10561 + " obscured=" + obscured
10562 + " displayed=" + displayed);
10563 if ((attrFlags&FLAG_DIM_BEHIND) != 0) {
10564 if (!dimming) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010565 //Slog.i(TAG, "DIM BEHIND: " + w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010566 dimming = true;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010567 if (mDimAnimator == null) {
10568 mDimAnimator = new DimAnimator(mFxSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010569 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010570 mDimAnimator.show(dw, dh);
Dianne Hackborn16064f92010-03-25 00:47:24 -070010571 mDimAnimator.updateParameters(w, currentTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010572 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010573 }
10574 if ((attrFlags&FLAG_BLUR_BEHIND) != 0) {
10575 if (!blurring) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010576 //Slog.i(TAG, "BLUR BEHIND: " + w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010577 blurring = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010578 if (mBlurSurface == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010579 if (SHOW_TRANSACTIONS) Slog.i(TAG, " BLUR "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010580 + mBlurSurface + ": CREATE");
10581 try {
Romain Guy06882f82009-06-10 13:36:04 -070010582 mBlurSurface = new Surface(mFxSession, 0,
Mathias Agopian5d26c1e2010-03-01 16:09:43 -080010583 "BlurSurface",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010584 -1, 16, 16,
10585 PixelFormat.OPAQUE,
10586 Surface.FX_SURFACE_BLUR);
10587 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010588 Slog.e(TAG, "Exception creating Blur surface", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010589 }
10590 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010591 if (mBlurSurface != null) {
Dianne Hackborn16064f92010-03-25 00:47:24 -070010592 if (SHOW_TRANSACTIONS) Slog.i(TAG, " BLUR "
10593 + mBlurSurface + ": pos=(0,0) (" +
10594 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010595 mBlurSurface.setPosition(0, 0);
10596 mBlurSurface.setSize(dw, dh);
Dianne Hackborn16064f92010-03-25 00:47:24 -070010597 mBlurSurface.setLayer(w.mAnimLayer-2);
10598 if (!mBlurShown) {
10599 try {
10600 if (SHOW_TRANSACTIONS) Slog.i(TAG, " BLUR "
10601 + mBlurSurface + ": SHOW");
10602 mBlurSurface.show();
10603 } catch (RuntimeException e) {
10604 Slog.w(TAG, "Failure showing blur surface", e);
10605 }
10606 mBlurShown = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010607 }
10608 }
10609 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010610 }
10611 }
10612 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010613
Dianne Hackborne9e9bca2009-08-18 15:08:22 -070010614 if (obscuredChanged && mWallpaperTarget == w) {
10615 // This is the wallpaper target and its obscured state
10616 // changed... make sure the current wallaper's visibility
10617 // has been updated accordingly.
10618 updateWallpaperVisibilityLocked();
10619 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010620 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010621
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010622 if (backgroundFillerShown == false && mBackgroundFillerShown) {
10623 mBackgroundFillerShown = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010624 if (SHOW_TRANSACTIONS) Slog.d(TAG, "hiding background filler");
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010625 try {
10626 mBackgroundFillerSurface.hide();
10627 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010628 Slog.e(TAG, "Exception hiding filler surface", e);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010629 }
10630 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010631
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010632 if (mDimAnimator != null && mDimAnimator.mDimShown) {
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010633 animating |= mDimAnimator.updateSurface(dimming, currentTime,
10634 mDisplayFrozen || !mPolicy.isScreenOn());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010635 }
Romain Guy06882f82009-06-10 13:36:04 -070010636
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010637 if (!blurring && mBlurShown) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010638 if (SHOW_TRANSACTIONS) Slog.i(TAG, " BLUR " + mBlurSurface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010639 + ": HIDE");
10640 try {
10641 mBlurSurface.hide();
10642 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010643 Slog.w(TAG, "Illegal argument exception hiding blur surface");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010644 }
10645 mBlurShown = false;
10646 }
10647
Joe Onorato8a9b2202010-02-26 18:56:32 -080010648 if (SHOW_TRANSACTIONS) Slog.i(TAG, "<<< CLOSE TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010649 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010650 Slog.e(TAG, "Unhandled exception in Window Manager", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010651 }
10652
10653 Surface.closeTransaction();
Romain Guy06882f82009-06-10 13:36:04 -070010654
Joe Onorato8a9b2202010-02-26 18:56:32 -080010655 if (DEBUG_ORIENTATION && mDisplayFrozen) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010656 "With display frozen, orientationChangeComplete="
10657 + orientationChangeComplete);
10658 if (orientationChangeComplete) {
10659 if (mWindowsFreezingScreen) {
10660 mWindowsFreezingScreen = false;
10661 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
10662 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010663 stopFreezingDisplayLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010664 }
Romain Guy06882f82009-06-10 13:36:04 -070010665
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010666 i = mResizingWindows.size();
10667 if (i > 0) {
10668 do {
10669 i--;
10670 WindowState win = mResizingWindows.get(i);
10671 try {
Dianne Hackbornac3587d2010-03-11 11:12:11 -080010672 if (DEBUG_RESIZE || DEBUG_ORIENTATION) Slog.v(TAG,
10673 "Reporting new frame to " + win + ": " + win.mFrame);
Dianne Hackborn694f79b2010-03-17 19:44:59 -070010674 int diff = 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010675 boolean configChanged =
10676 win.mConfiguration != mCurConfiguration
10677 && (win.mConfiguration == null
Dianne Hackborn694f79b2010-03-17 19:44:59 -070010678 || (diff=mCurConfiguration.diff(win.mConfiguration)) != 0);
10679 if ((DEBUG_RESIZE || DEBUG_ORIENTATION || DEBUG_CONFIGURATION)
10680 && configChanged) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010681 Slog.i(TAG, "Sending new config to window " + win + ": "
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010682 + win.mFrame.width() + "x" + win.mFrame.height()
Dianne Hackborn694f79b2010-03-17 19:44:59 -070010683 + " / " + mCurConfiguration + " / 0x"
10684 + Integer.toHexString(diff));
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010685 }
Dianne Hackborn694f79b2010-03-17 19:44:59 -070010686 win.mConfiguration = mCurConfiguration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010687 win.mClient.resized(win.mFrame.width(),
10688 win.mFrame.height(), win.mLastContentInsets,
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010689 win.mLastVisibleInsets, win.mDrawPending,
10690 configChanged ? win.mConfiguration : null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010691 win.mContentInsetsChanged = false;
10692 win.mVisibleInsetsChanged = false;
Dianne Hackbornac3587d2010-03-11 11:12:11 -080010693 win.mSurfaceResized = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010694 } catch (RemoteException e) {
10695 win.mOrientationChanging = false;
10696 }
10697 } while (i > 0);
10698 mResizingWindows.clear();
10699 }
Romain Guy06882f82009-06-10 13:36:04 -070010700
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010701 // Destroy the surface of any windows that are no longer visible.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010702 boolean wallpaperDestroyed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010703 i = mDestroySurface.size();
10704 if (i > 0) {
10705 do {
10706 i--;
10707 WindowState win = mDestroySurface.get(i);
10708 win.mDestroying = false;
10709 if (mInputMethodWindow == win) {
10710 mInputMethodWindow = null;
10711 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010712 if (win == mWallpaperTarget) {
10713 wallpaperDestroyed = true;
10714 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010715 win.destroySurfaceLocked();
10716 } while (i > 0);
10717 mDestroySurface.clear();
10718 }
10719
10720 // Time to remove any exiting tokens?
10721 for (i=mExitingTokens.size()-1; i>=0; i--) {
10722 WindowToken token = mExitingTokens.get(i);
10723 if (!token.hasVisible) {
10724 mExitingTokens.remove(i);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070010725 if (token.windowType == TYPE_WALLPAPER) {
10726 mWallpaperTokens.remove(token);
10727 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010728 }
10729 }
10730
10731 // Time to remove any exiting applications?
10732 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
10733 AppWindowToken token = mExitingAppTokens.get(i);
10734 if (!token.hasVisible && !mClosingApps.contains(token)) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -070010735 // Make sure there is no animation running on this token,
10736 // so any windows associated with it will be removed as
10737 // soon as their animations are complete
10738 token.animation = null;
10739 token.animating = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010740 mAppTokens.remove(token);
10741 mExitingAppTokens.remove(i);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010742 if (mLastEnterAnimToken == token) {
10743 mLastEnterAnimToken = null;
10744 mLastEnterAnimParams = null;
10745 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010746 }
10747 }
10748
Dianne Hackborna8f60182009-09-01 19:01:50 -070010749 boolean needRelayout = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010750
Dianne Hackborna8f60182009-09-01 19:01:50 -070010751 if (!animating && mAppTransitionRunning) {
10752 // We have finished the animation of an app transition. To do
10753 // this, we have delayed a lot of operations like showing and
10754 // hiding apps, moving apps in Z-order, etc. The app token list
10755 // reflects the correct Z-order, but the window list may now
10756 // be out of sync with it. So here we will just rebuild the
10757 // entire app window list. Fun!
10758 mAppTransitionRunning = false;
10759 needRelayout = true;
10760 rebuildAppWindowListLocked();
Dianne Hackborn16064f92010-03-25 00:47:24 -070010761 assignLayersLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -070010762 // Clear information about apps that were moving.
10763 mToBottomApps.clear();
10764 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010765
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010766 if (focusDisplayed) {
10767 mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
10768 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010769 if (wallpaperDestroyed) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010770 needRelayout = adjustWallpaperWindowsLocked() != 0;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010771 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070010772 if (needRelayout) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010773 requestAnimationLocked(0);
10774 } else if (animating) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010775 requestAnimationLocked(currentTime+(1000/60)-SystemClock.uptimeMillis());
10776 }
Dianne Hackbornce73c1e2010-04-12 23:11:38 -070010777
10778 if (DEBUG_FREEZE) Slog.v(TAG, "Layout: mDisplayFrozen=" + mDisplayFrozen
10779 + " holdScreen=" + holdScreen);
10780 if (!mDisplayFrozen) {
10781 mQueue.setHoldScreenLocked(holdScreen != null);
10782 if (screenBrightness < 0 || screenBrightness > 1.0f) {
10783 mPowerManager.setScreenBrightnessOverride(-1);
10784 } else {
10785 mPowerManager.setScreenBrightnessOverride((int)
10786 (screenBrightness * Power.BRIGHTNESS_ON));
10787 }
10788 if (buttonBrightness < 0 || buttonBrightness > 1.0f) {
10789 mPowerManager.setButtonBrightnessOverride(-1);
10790 } else {
10791 mPowerManager.setButtonBrightnessOverride((int)
10792 (buttonBrightness * Power.BRIGHTNESS_ON));
10793 }
10794 if (holdScreen != mHoldingScreenOn) {
10795 mHoldingScreenOn = holdScreen;
10796 Message m = mH.obtainMessage(H.HOLD_SCREEN_CHANGED, holdScreen);
10797 mH.sendMessage(m);
10798 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010799 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010800
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010801 if (mTurnOnScreen) {
Dianne Hackbornb601ce12010-03-01 23:36:02 -080010802 if (DEBUG_VISIBILITY) Slog.v(TAG, "Turning screen on after layout!");
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010803 mPowerManager.userActivity(SystemClock.uptimeMillis(), false,
10804 LocalPowerManager.BUTTON_EVENT, true);
10805 mTurnOnScreen = false;
10806 }
Dianne Hackbornf3bea9c2009-12-09 18:26:21 -080010807
10808 // Check to see if we are now in a state where the screen should
10809 // be enabled, because the window obscured flags have changed.
10810 enableScreenIfNeededLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010811 }
10812
10813 void requestAnimationLocked(long delay) {
10814 if (!mAnimationPending) {
10815 mAnimationPending = true;
10816 mH.sendMessageDelayed(mH.obtainMessage(H.ANIMATE), delay);
10817 }
10818 }
Romain Guy06882f82009-06-10 13:36:04 -070010819
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010820 /**
10821 * Have the surface flinger show a surface, robustly dealing with
10822 * error conditions. In particular, if there is not enough memory
10823 * to show the surface, then we will try to get rid of other surfaces
10824 * in order to succeed.
Romain Guy06882f82009-06-10 13:36:04 -070010825 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010826 * @return Returns true if the surface was successfully shown.
10827 */
10828 boolean showSurfaceRobustlyLocked(WindowState win) {
10829 try {
10830 if (win.mSurface != null) {
Dianne Hackborn16064f92010-03-25 00:47:24 -070010831 win.mSurfaceShown = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010832 win.mSurface.show();
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010833 if (win.mTurnOnScreen) {
Dianne Hackbornb601ce12010-03-01 23:36:02 -080010834 if (DEBUG_VISIBILITY) Slog.v(TAG,
10835 "Show surface turning screen on: " + win);
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010836 win.mTurnOnScreen = false;
10837 mTurnOnScreen = true;
10838 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010839 }
10840 return true;
10841 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010842 Slog.w(TAG, "Failure showing surface " + win.mSurface + " in " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010843 }
Romain Guy06882f82009-06-10 13:36:04 -070010844
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010845 reclaimSomeSurfaceMemoryLocked(win, "show");
Romain Guy06882f82009-06-10 13:36:04 -070010846
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010847 return false;
10848 }
Romain Guy06882f82009-06-10 13:36:04 -070010849
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010850 void reclaimSomeSurfaceMemoryLocked(WindowState win, String operation) {
10851 final Surface surface = win.mSurface;
Romain Guy06882f82009-06-10 13:36:04 -070010852
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010853 EventLog.writeEvent(EventLogTags.WM_NO_SURFACE_MEMORY, win.toString(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010854 win.mSession.mPid, operation);
Romain Guy06882f82009-06-10 13:36:04 -070010855
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010856 if (mForceRemoves == null) {
10857 mForceRemoves = new ArrayList<WindowState>();
10858 }
Romain Guy06882f82009-06-10 13:36:04 -070010859
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010860 long callingIdentity = Binder.clearCallingIdentity();
10861 try {
10862 // There was some problem... first, do a sanity check of the
10863 // window list to make sure we haven't left any dangling surfaces
10864 // around.
10865 int N = mWindows.size();
10866 boolean leakedSurface = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010867 Slog.i(TAG, "Out of memory for surface! Looking for leaks...");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010868 for (int i=0; i<N; i++) {
10869 WindowState ws = (WindowState)mWindows.get(i);
10870 if (ws.mSurface != null) {
10871 if (!mSessions.contains(ws.mSession)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010872 Slog.w(TAG, "LEAKED SURFACE (session doesn't exist): "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010873 + ws + " surface=" + ws.mSurface
10874 + " token=" + win.mToken
10875 + " pid=" + ws.mSession.mPid
10876 + " uid=" + ws.mSession.mUid);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010877 ws.mSurface.destroy();
Dianne Hackborn16064f92010-03-25 00:47:24 -070010878 ws.mSurfaceShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010879 ws.mSurface = null;
10880 mForceRemoves.add(ws);
10881 i--;
10882 N--;
10883 leakedSurface = true;
10884 } else if (win.mAppToken != null && win.mAppToken.clientHidden) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010885 Slog.w(TAG, "LEAKED SURFACE (app token hidden): "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010886 + ws + " surface=" + ws.mSurface
10887 + " token=" + win.mAppToken);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010888 ws.mSurface.destroy();
Dianne Hackborn16064f92010-03-25 00:47:24 -070010889 ws.mSurfaceShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010890 ws.mSurface = null;
10891 leakedSurface = true;
10892 }
10893 }
10894 }
Romain Guy06882f82009-06-10 13:36:04 -070010895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010896 boolean killedApps = false;
10897 if (!leakedSurface) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010898 Slog.w(TAG, "No leaked surfaces; killing applicatons!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010899 SparseIntArray pidCandidates = new SparseIntArray();
10900 for (int i=0; i<N; i++) {
10901 WindowState ws = (WindowState)mWindows.get(i);
10902 if (ws.mSurface != null) {
10903 pidCandidates.append(ws.mSession.mPid, ws.mSession.mPid);
10904 }
10905 }
10906 if (pidCandidates.size() > 0) {
10907 int[] pids = new int[pidCandidates.size()];
10908 for (int i=0; i<pids.length; i++) {
10909 pids[i] = pidCandidates.keyAt(i);
10910 }
10911 try {
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -070010912 if (mActivityManager.killPids(pids, "Free memory")) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010913 killedApps = true;
10914 }
10915 } catch (RemoteException e) {
10916 }
10917 }
10918 }
Romain Guy06882f82009-06-10 13:36:04 -070010919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010920 if (leakedSurface || killedApps) {
10921 // We managed to reclaim some memory, so get rid of the trouble
10922 // surface and ask the app to request another one.
Joe Onorato8a9b2202010-02-26 18:56:32 -080010923 Slog.w(TAG, "Looks like we have reclaimed some memory, clearing surface for retry.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010924 if (surface != null) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010925 surface.destroy();
Dianne Hackborn16064f92010-03-25 00:47:24 -070010926 win.mSurfaceShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010927 win.mSurface = null;
10928 }
Romain Guy06882f82009-06-10 13:36:04 -070010929
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010930 try {
10931 win.mClient.dispatchGetNewSurface();
10932 } catch (RemoteException e) {
10933 }
10934 }
10935 } finally {
10936 Binder.restoreCallingIdentity(callingIdentity);
10937 }
10938 }
Romain Guy06882f82009-06-10 13:36:04 -070010939
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010940 private boolean updateFocusedWindowLocked(int mode) {
10941 WindowState newFocus = computeFocusedWindowLocked();
10942 if (mCurrentFocus != newFocus) {
10943 // This check makes sure that we don't already have the focus
10944 // change message pending.
10945 mH.removeMessages(H.REPORT_FOCUS_CHANGE);
10946 mH.sendEmptyMessage(H.REPORT_FOCUS_CHANGE);
Joe Onorato8a9b2202010-02-26 18:56:32 -080010947 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010948 TAG, "Changing focus from " + mCurrentFocus + " to " + newFocus);
10949 final WindowState oldFocus = mCurrentFocus;
10950 mCurrentFocus = newFocus;
10951 mLosingFocus.remove(newFocus);
Romain Guy06882f82009-06-10 13:36:04 -070010952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010953 final WindowState imWindow = mInputMethodWindow;
10954 if (newFocus != imWindow && oldFocus != imWindow) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010955 if (moveInputMethodWindowsIfNeededLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010956 mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS &&
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010957 mode != UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
10958 mLayoutNeeded = true;
10959 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010960 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
10961 performLayoutLockedInner();
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010962 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
10963 // Client will do the layout, but we need to assign layers
10964 // for handleNewWindowLocked() below.
10965 assignLayersLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010966 }
10967 }
Romain Guy06882f82009-06-10 13:36:04 -070010968
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010969 if (newFocus != null && mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
10970 mKeyWaiter.handleNewWindowLocked(newFocus);
10971 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010972 return true;
10973 }
10974 return false;
10975 }
10976
10977 private WindowState computeFocusedWindowLocked() {
10978 WindowState result = null;
10979 WindowState win;
10980
10981 int i = mWindows.size() - 1;
10982 int nextAppIndex = mAppTokens.size()-1;
10983 WindowToken nextApp = nextAppIndex >= 0
10984 ? mAppTokens.get(nextAppIndex) : null;
10985
10986 while (i >= 0) {
10987 win = (WindowState)mWindows.get(i);
10988
Joe Onorato8a9b2202010-02-26 18:56:32 -080010989 if (localLOGV || DEBUG_FOCUS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010990 TAG, "Looking for focus: " + i
10991 + " = " + win
10992 + ", flags=" + win.mAttrs.flags
10993 + ", canReceive=" + win.canReceiveKeys());
10994
10995 AppWindowToken thisApp = win.mAppToken;
Romain Guy06882f82009-06-10 13:36:04 -070010996
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010997 // If this window's application has been removed, just skip it.
10998 if (thisApp != null && thisApp.removed) {
10999 i--;
11000 continue;
11001 }
Romain Guy06882f82009-06-10 13:36:04 -070011002
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011003 // If there is a focused app, don't allow focus to go to any
11004 // windows below it. If this is an application window, step
11005 // through the app tokens until we find its app.
11006 if (thisApp != null && nextApp != null && thisApp != nextApp
11007 && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
11008 int origAppIndex = nextAppIndex;
11009 while (nextAppIndex > 0) {
11010 if (nextApp == mFocusedApp) {
11011 // Whoops, we are below the focused app... no focus
11012 // for you!
Joe Onorato8a9b2202010-02-26 18:56:32 -080011013 if (localLOGV || DEBUG_FOCUS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011014 TAG, "Reached focused app: " + mFocusedApp);
11015 return null;
11016 }
11017 nextAppIndex--;
11018 nextApp = mAppTokens.get(nextAppIndex);
11019 if (nextApp == thisApp) {
11020 break;
11021 }
11022 }
11023 if (thisApp != nextApp) {
11024 // Uh oh, the app token doesn't exist! This shouldn't
11025 // happen, but if it does we can get totally hosed...
11026 // so restart at the original app.
11027 nextAppIndex = origAppIndex;
11028 nextApp = mAppTokens.get(nextAppIndex);
11029 }
11030 }
11031
11032 // Dispatch to this window if it is wants key events.
11033 if (win.canReceiveKeys()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011034 if (DEBUG_FOCUS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011035 TAG, "Found focus @ " + i + " = " + win);
11036 result = win;
11037 break;
11038 }
11039
11040 i--;
11041 }
11042
11043 return result;
11044 }
11045
11046 private void startFreezingDisplayLocked() {
11047 if (mDisplayFrozen) {
Chris Tate2ad63a92009-03-25 17:36:48 -070011048 // Freezing the display also suspends key event delivery, to
11049 // keep events from going astray while the display is reconfigured.
11050 // If someone has changed orientation again while the screen is
11051 // still frozen, the events will continue to be blocked while the
11052 // successive orientation change is processed. To prevent spurious
11053 // ANRs, we reset the event dispatch timeout in this case.
11054 synchronized (mKeyWaiter) {
11055 mKeyWaiter.mWasFrozen = true;
11056 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011057 return;
11058 }
Romain Guy06882f82009-06-10 13:36:04 -070011059
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011060 mScreenFrozenLock.acquire();
Romain Guy06882f82009-06-10 13:36:04 -070011061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011062 long now = SystemClock.uptimeMillis();
Joe Onorato8a9b2202010-02-26 18:56:32 -080011063 //Slog.i(TAG, "Freezing, gc pending: " + mFreezeGcPending + ", now " + now);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011064 if (mFreezeGcPending != 0) {
11065 if (now > (mFreezeGcPending+1000)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011066 //Slog.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011067 mH.removeMessages(H.FORCE_GC);
11068 Runtime.getRuntime().gc();
11069 mFreezeGcPending = now;
11070 }
11071 } else {
11072 mFreezeGcPending = now;
11073 }
Romain Guy06882f82009-06-10 13:36:04 -070011074
Dianne Hackbornce73c1e2010-04-12 23:11:38 -070011075 if (DEBUG_FREEZE) Slog.v(TAG, "*** FREEZING DISPLAY", new RuntimeException());
11076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011077 mDisplayFrozen = true;
Dianne Hackbornbfe319e2009-09-21 00:34:05 -070011078 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
11079 mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070011080 mNextAppTransitionPackage = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011081 mAppTransitionReady = true;
11082 }
Romain Guy06882f82009-06-10 13:36:04 -070011083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011084 if (PROFILE_ORIENTATION) {
11085 File file = new File("/data/system/frozen");
11086 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
11087 }
11088 Surface.freezeDisplay(0);
11089 }
Romain Guy06882f82009-06-10 13:36:04 -070011090
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011091 private void stopFreezingDisplayLocked() {
11092 if (!mDisplayFrozen) {
11093 return;
11094 }
Romain Guy06882f82009-06-10 13:36:04 -070011095
Dianne Hackborne36d6e22010-02-17 19:46:25 -080011096 if (mWaitingForConfig || mAppsFreezingScreen > 0 || mWindowsFreezingScreen) {
11097 return;
11098 }
11099
Dianne Hackbornce73c1e2010-04-12 23:11:38 -070011100 if (DEBUG_FREEZE) Slog.v(TAG, "*** UNFREEZING DISPLAY", new RuntimeException());
11101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011102 mDisplayFrozen = false;
11103 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
11104 if (PROFILE_ORIENTATION) {
11105 Debug.stopMethodTracing();
11106 }
11107 Surface.unfreezeDisplay(0);
Romain Guy06882f82009-06-10 13:36:04 -070011108
Chris Tate2ad63a92009-03-25 17:36:48 -070011109 // Reset the key delivery timeout on unfreeze, too. We force a wakeup here
11110 // too because regular key delivery processing should resume immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011111 synchronized (mKeyWaiter) {
11112 mKeyWaiter.mWasFrozen = true;
11113 mKeyWaiter.notifyAll();
11114 }
11115
Christopher Tateb696aee2010-04-02 19:08:30 -070011116 // While the display is frozen we don't re-compute the orientation
11117 // to avoid inconsistent states. However, something interesting
11118 // could have actually changed during that time so re-evaluate it
11119 // now to catch that.
11120 if (updateOrientationFromAppTokensLocked()) {
11121 mH.sendEmptyMessage(H.SEND_NEW_CONFIGURATION);
11122 }
11123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011124 // A little kludge: a lot could have happened while the
11125 // display was frozen, so now that we are coming back we
11126 // do a gc so that any remote references the system
11127 // processes holds on others can be released if they are
11128 // no longer needed.
11129 mH.removeMessages(H.FORCE_GC);
11130 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
11131 2000);
Romain Guy06882f82009-06-10 13:36:04 -070011132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011133 mScreenFrozenLock.release();
11134 }
Romain Guy06882f82009-06-10 13:36:04 -070011135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011136 @Override
11137 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
11138 if (mContext.checkCallingOrSelfPermission("android.permission.DUMP")
11139 != PackageManager.PERMISSION_GRANTED) {
11140 pw.println("Permission Denial: can't dump WindowManager from from pid="
11141 + Binder.getCallingPid()
11142 + ", uid=" + Binder.getCallingUid());
11143 return;
11144 }
Romain Guy06882f82009-06-10 13:36:04 -070011145
Dianne Hackborna2e92262010-03-02 17:19:29 -080011146 pw.println("Input State:");
11147 mQueue.dump(pw, " ");
11148 pw.println(" ");
11149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011150 synchronized(mWindowMap) {
11151 pw.println("Current Window Manager state:");
11152 for (int i=mWindows.size()-1; i>=0; i--) {
11153 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011154 pw.print(" Window #"); pw.print(i); pw.print(' ');
11155 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011156 w.dump(pw, " ");
11157 }
11158 if (mInputMethodDialogs.size() > 0) {
11159 pw.println(" ");
11160 pw.println(" Input method dialogs:");
11161 for (int i=mInputMethodDialogs.size()-1; i>=0; i--) {
11162 WindowState w = mInputMethodDialogs.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011163 pw.print(" IM Dialog #"); pw.print(i); pw.print(": "); pw.println(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011164 }
11165 }
11166 if (mPendingRemove.size() > 0) {
11167 pw.println(" ");
11168 pw.println(" Remove pending for:");
11169 for (int i=mPendingRemove.size()-1; i>=0; i--) {
11170 WindowState w = mPendingRemove.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011171 pw.print(" Remove #"); pw.print(i); pw.print(' ');
11172 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011173 w.dump(pw, " ");
11174 }
11175 }
11176 if (mForceRemoves != null && mForceRemoves.size() > 0) {
11177 pw.println(" ");
11178 pw.println(" Windows force removing:");
11179 for (int i=mForceRemoves.size()-1; i>=0; i--) {
11180 WindowState w = mForceRemoves.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011181 pw.print(" Removing #"); pw.print(i); pw.print(' ');
11182 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011183 w.dump(pw, " ");
11184 }
11185 }
11186 if (mDestroySurface.size() > 0) {
11187 pw.println(" ");
11188 pw.println(" Windows waiting to destroy their surface:");
11189 for (int i=mDestroySurface.size()-1; i>=0; i--) {
11190 WindowState w = mDestroySurface.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011191 pw.print(" Destroy #"); pw.print(i); pw.print(' ');
11192 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011193 w.dump(pw, " ");
11194 }
11195 }
11196 if (mLosingFocus.size() > 0) {
11197 pw.println(" ");
11198 pw.println(" Windows losing focus:");
11199 for (int i=mLosingFocus.size()-1; i>=0; i--) {
11200 WindowState w = mLosingFocus.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011201 pw.print(" Losing #"); pw.print(i); pw.print(' ');
11202 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011203 w.dump(pw, " ");
11204 }
11205 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070011206 if (mResizingWindows.size() > 0) {
11207 pw.println(" ");
11208 pw.println(" Windows waiting to resize:");
11209 for (int i=mResizingWindows.size()-1; i>=0; i--) {
11210 WindowState w = mResizingWindows.get(i);
11211 pw.print(" Resizing #"); pw.print(i); pw.print(' ');
11212 pw.print(w); pw.println(":");
11213 w.dump(pw, " ");
11214 }
11215 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011216 if (mSessions.size() > 0) {
11217 pw.println(" ");
11218 pw.println(" All active sessions:");
11219 Iterator<Session> it = mSessions.iterator();
11220 while (it.hasNext()) {
11221 Session s = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011222 pw.print(" Session "); pw.print(s); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011223 s.dump(pw, " ");
11224 }
11225 }
11226 if (mTokenMap.size() > 0) {
11227 pw.println(" ");
11228 pw.println(" All tokens:");
11229 Iterator<WindowToken> it = mTokenMap.values().iterator();
11230 while (it.hasNext()) {
11231 WindowToken token = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011232 pw.print(" Token "); pw.print(token.token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011233 token.dump(pw, " ");
11234 }
11235 }
11236 if (mTokenList.size() > 0) {
11237 pw.println(" ");
11238 pw.println(" Window token list:");
11239 for (int i=0; i<mTokenList.size(); i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011240 pw.print(" #"); pw.print(i); pw.print(": ");
11241 pw.println(mTokenList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011242 }
11243 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070011244 if (mWallpaperTokens.size() > 0) {
11245 pw.println(" ");
11246 pw.println(" Wallpaper tokens:");
11247 for (int i=mWallpaperTokens.size()-1; i>=0; i--) {
11248 WindowToken token = mWallpaperTokens.get(i);
11249 pw.print(" Wallpaper #"); pw.print(i);
11250 pw.print(' '); pw.print(token); pw.println(':');
11251 token.dump(pw, " ");
11252 }
11253 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011254 if (mAppTokens.size() > 0) {
11255 pw.println(" ");
11256 pw.println(" Application tokens in Z order:");
11257 for (int i=mAppTokens.size()-1; i>=0; i--) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011258 pw.print(" App #"); pw.print(i); pw.print(": ");
11259 pw.println(mAppTokens.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011260 }
11261 }
11262 if (mFinishedStarting.size() > 0) {
11263 pw.println(" ");
11264 pw.println(" Finishing start of application tokens:");
11265 for (int i=mFinishedStarting.size()-1; i>=0; i--) {
11266 WindowToken token = mFinishedStarting.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011267 pw.print(" Finished Starting #"); pw.print(i);
11268 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011269 token.dump(pw, " ");
11270 }
11271 }
11272 if (mExitingTokens.size() > 0) {
11273 pw.println(" ");
11274 pw.println(" Exiting tokens:");
11275 for (int i=mExitingTokens.size()-1; i>=0; i--) {
11276 WindowToken token = mExitingTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011277 pw.print(" Exiting #"); pw.print(i);
11278 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011279 token.dump(pw, " ");
11280 }
11281 }
11282 if (mExitingAppTokens.size() > 0) {
11283 pw.println(" ");
11284 pw.println(" Exiting application tokens:");
11285 for (int i=mExitingAppTokens.size()-1; i>=0; i--) {
11286 WindowToken token = mExitingAppTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011287 pw.print(" Exiting App #"); pw.print(i);
11288 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011289 token.dump(pw, " ");
11290 }
11291 }
11292 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011293 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
11294 pw.print(" mLastFocus="); pw.println(mLastFocus);
11295 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
11296 pw.print(" mInputMethodTarget="); pw.println(mInputMethodTarget);
11297 pw.print(" mInputMethodWindow="); pw.println(mInputMethodWindow);
Dianne Hackbornf21adf62009-08-13 10:20:21 -070011298 pw.print(" mWallpaperTarget="); pw.println(mWallpaperTarget);
Dianne Hackborn284ac932009-08-28 10:34:25 -070011299 if (mLowerWallpaperTarget != null && mUpperWallpaperTarget != null) {
11300 pw.print(" mLowerWallpaperTarget="); pw.println(mLowerWallpaperTarget);
11301 pw.print(" mUpperWallpaperTarget="); pw.println(mUpperWallpaperTarget);
11302 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -080011303 pw.print(" mCurConfiguration="); pw.println(this.mCurConfiguration);
11304 pw.print(" mInTouchMode="); pw.print(mInTouchMode);
11305 pw.print(" mLayoutSeq="); pw.println(mLayoutSeq);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011306 pw.print(" mSystemBooted="); pw.print(mSystemBooted);
11307 pw.print(" mDisplayEnabled="); pw.println(mDisplayEnabled);
11308 pw.print(" mLayoutNeeded="); pw.print(mLayoutNeeded);
11309 pw.print(" mBlurShown="); pw.println(mBlurShown);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011310 if (mDimAnimator != null) {
11311 mDimAnimator.printTo(pw);
11312 } else {
Dianne Hackborna2e92262010-03-02 17:19:29 -080011313 pw.println( " no DimAnimator ");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011314 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011315 pw.print(" mInputMethodAnimLayerAdjustment=");
Dianne Hackborn759a39e2009-08-09 17:20:27 -070011316 pw.print(mInputMethodAnimLayerAdjustment);
11317 pw.print(" mWallpaperAnimLayerAdjustment=");
11318 pw.println(mWallpaperAnimLayerAdjustment);
Dianne Hackborn284ac932009-08-28 10:34:25 -070011319 pw.print(" mLastWallpaperX="); pw.print(mLastWallpaperX);
11320 pw.print(" mLastWallpaperY="); pw.println(mLastWallpaperY);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011321 pw.print(" mDisplayFrozen="); pw.print(mDisplayFrozen);
11322 pw.print(" mWindowsFreezingScreen="); pw.print(mWindowsFreezingScreen);
Dianne Hackborne36d6e22010-02-17 19:46:25 -080011323 pw.print(" mAppsFreezingScreen="); pw.print(mAppsFreezingScreen);
11324 pw.print(" mWaitingForConfig="); pw.println(mWaitingForConfig);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011325 pw.print(" mRotation="); pw.print(mRotation);
11326 pw.print(", mForcedAppOrientation="); pw.print(mForcedAppOrientation);
11327 pw.print(", mRequestedRotation="); pw.println(mRequestedRotation);
11328 pw.print(" mAnimationPending="); pw.print(mAnimationPending);
11329 pw.print(" mWindowAnimationScale="); pw.print(mWindowAnimationScale);
11330 pw.print(" mTransitionWindowAnimationScale="); pw.println(mTransitionAnimationScale);
11331 pw.print(" mNextAppTransition=0x");
11332 pw.print(Integer.toHexString(mNextAppTransition));
11333 pw.print(", mAppTransitionReady="); pw.print(mAppTransitionReady);
Dianne Hackborna8f60182009-09-01 19:01:50 -070011334 pw.print(", mAppTransitionRunning="); pw.print(mAppTransitionRunning);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011335 pw.print(", mAppTransitionTimeout="); pw.println( mAppTransitionTimeout);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070011336 if (mNextAppTransitionPackage != null) {
11337 pw.print(" mNextAppTransitionPackage=");
11338 pw.print(mNextAppTransitionPackage);
11339 pw.print(", mNextAppTransitionEnter=0x");
11340 pw.print(Integer.toHexString(mNextAppTransitionEnter));
11341 pw.print(", mNextAppTransitionExit=0x");
11342 pw.print(Integer.toHexString(mNextAppTransitionExit));
11343 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011344 pw.print(" mStartingIconInTransition="); pw.print(mStartingIconInTransition);
11345 pw.print(", mSkipAppTransitionAnimation="); pw.println(mSkipAppTransitionAnimation);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070011346 if (mLastEnterAnimToken != null || mLastEnterAnimToken != null) {
11347 pw.print(" mLastEnterAnimToken="); pw.print(mLastEnterAnimToken);
11348 pw.print(", mLastEnterAnimParams="); pw.println(mLastEnterAnimParams);
11349 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011350 if (mOpeningApps.size() > 0) {
11351 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
11352 }
11353 if (mClosingApps.size() > 0) {
11354 pw.print(" mClosingApps="); pw.println(mClosingApps);
11355 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070011356 if (mToTopApps.size() > 0) {
11357 pw.print(" mToTopApps="); pw.println(mToTopApps);
11358 }
11359 if (mToBottomApps.size() > 0) {
11360 pw.print(" mToBottomApps="); pw.println(mToBottomApps);
11361 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011362 pw.print(" DisplayWidth="); pw.print(mDisplay.getWidth());
11363 pw.print(" DisplayHeight="); pw.println(mDisplay.getHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011364 pw.println(" KeyWaiter state:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011365 pw.print(" mLastWin="); pw.print(mKeyWaiter.mLastWin);
11366 pw.print(" mLastBinder="); pw.println(mKeyWaiter.mLastBinder);
11367 pw.print(" mFinished="); pw.print(mKeyWaiter.mFinished);
11368 pw.print(" mGotFirstWindow="); pw.print(mKeyWaiter.mGotFirstWindow);
11369 pw.print(" mEventDispatching="); pw.print(mKeyWaiter.mEventDispatching);
11370 pw.print(" mTimeToSwitch="); pw.println(mKeyWaiter.mTimeToSwitch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011371 }
11372 }
11373
11374 public void monitor() {
11375 synchronized (mWindowMap) { }
Mike Lockwood983ee092009-11-22 01:42:24 -050011376 synchronized (mKeyguardTokenWatcher) { }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011377 synchronized (mKeyWaiter) { }
11378 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011379
Dianne Hackbornddca3ee2009-07-23 19:01:31 -070011380 public void virtualKeyFeedback(KeyEvent event) {
11381 mPolicy.keyFeedbackFromInput(event);
11382 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080011383
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011384 /**
11385 * DimAnimator class that controls the dim animation. This holds the surface and
Doug Zongkerab5c49c2009-12-04 10:31:43 -080011386 * all state used for dim animation.
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011387 */
11388 private static class DimAnimator {
11389 Surface mDimSurface;
11390 boolean mDimShown = false;
11391 float mDimCurrentAlpha;
11392 float mDimTargetAlpha;
11393 float mDimDeltaPerMs;
11394 long mLastDimAnimTime;
Dianne Hackbornf83c5552010-03-31 22:19:32 -070011395
11396 int mLastDimWidth, mLastDimHeight;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011397
11398 DimAnimator (SurfaceSession session) {
11399 if (mDimSurface == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011400 if (SHOW_TRANSACTIONS) Slog.i(TAG, " DIM "
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011401 + mDimSurface + ": CREATE");
11402 try {
Mathias Agopian5d26c1e2010-03-01 16:09:43 -080011403 mDimSurface = new Surface(session, 0,
11404 "DimSurface",
11405 -1, 16, 16, PixelFormat.OPAQUE,
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011406 Surface.FX_SURFACE_DIM);
Maciej Białka9ee5c222010-03-24 10:25:40 +010011407 mDimSurface.setAlpha(0.0f);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011408 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011409 Slog.e(TAG, "Exception creating Dim surface", e);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011410 }
11411 }
11412 }
11413
11414 /**
11415 * Show the dim surface.
11416 */
11417 void show(int dw, int dh) {
Dianne Hackborn16064f92010-03-25 00:47:24 -070011418 if (!mDimShown) {
11419 if (SHOW_TRANSACTIONS) Slog.i(TAG, " DIM " + mDimSurface + ": SHOW pos=(0,0) (" +
11420 dw + "x" + dh + ")");
11421 mDimShown = true;
11422 try {
Dianne Hackbornf83c5552010-03-31 22:19:32 -070011423 mLastDimWidth = dw;
11424 mLastDimHeight = dh;
Dianne Hackborn16064f92010-03-25 00:47:24 -070011425 mDimSurface.setPosition(0, 0);
11426 mDimSurface.setSize(dw, dh);
11427 mDimSurface.show();
11428 } catch (RuntimeException e) {
11429 Slog.w(TAG, "Failure showing dim surface", e);
11430 }
Dianne Hackbornf83c5552010-03-31 22:19:32 -070011431 } else if (mLastDimWidth != dw || mLastDimHeight != dh) {
11432 mLastDimWidth = dw;
11433 mLastDimHeight = dh;
11434 mDimSurface.setSize(dw, dh);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011435 }
11436 }
11437
11438 /**
11439 * Set's the dim surface's layer and update dim parameters that will be used in
11440 * {@link updateSurface} after all windows are examined.
11441 */
11442 void updateParameters(WindowState w, long currentTime) {
11443 mDimSurface.setLayer(w.mAnimLayer-1);
11444
11445 final float target = w.mExiting ? 0 : w.mAttrs.dimAmount;
Joe Onorato8a9b2202010-02-26 18:56:32 -080011446 if (SHOW_TRANSACTIONS) Slog.i(TAG, " DIM " + mDimSurface
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070011447 + ": layer=" + (w.mAnimLayer-1) + " target=" + target);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011448 if (mDimTargetAlpha != target) {
11449 // If the desired dim level has changed, then
11450 // start an animation to it.
11451 mLastDimAnimTime = currentTime;
11452 long duration = (w.mAnimating && w.mAnimation != null)
11453 ? w.mAnimation.computeDurationHint()
11454 : DEFAULT_DIM_DURATION;
11455 if (target > mDimTargetAlpha) {
11456 // This is happening behind the activity UI,
11457 // so we can make it run a little longer to
11458 // give a stronger impression without disrupting
11459 // the user.
11460 duration *= DIM_DURATION_MULTIPLIER;
11461 }
11462 if (duration < 1) {
11463 // Don't divide by zero
11464 duration = 1;
11465 }
11466 mDimTargetAlpha = target;
11467 mDimDeltaPerMs = (mDimTargetAlpha-mDimCurrentAlpha) / duration;
11468 }
11469 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080011470
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011471 /**
11472 * Updating the surface's alpha. Returns true if the animation continues, or returns
11473 * false when the animation is finished and the dim surface is hidden.
11474 */
11475 boolean updateSurface(boolean dimming, long currentTime, boolean displayFrozen) {
11476 if (!dimming) {
11477 if (mDimTargetAlpha != 0) {
11478 mLastDimAnimTime = currentTime;
11479 mDimTargetAlpha = 0;
11480 mDimDeltaPerMs = (-mDimCurrentAlpha) / DEFAULT_DIM_DURATION;
11481 }
11482 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080011483
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011484 boolean animating = false;
11485 if (mLastDimAnimTime != 0) {
11486 mDimCurrentAlpha += mDimDeltaPerMs
11487 * (currentTime-mLastDimAnimTime);
11488 boolean more = true;
11489 if (displayFrozen) {
11490 // If the display is frozen, there is no reason to animate.
11491 more = false;
11492 } else if (mDimDeltaPerMs > 0) {
11493 if (mDimCurrentAlpha > mDimTargetAlpha) {
11494 more = false;
11495 }
11496 } else if (mDimDeltaPerMs < 0) {
11497 if (mDimCurrentAlpha < mDimTargetAlpha) {
11498 more = false;
11499 }
11500 } else {
11501 more = false;
11502 }
11503
11504 // Do we need to continue animating?
11505 if (more) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011506 if (SHOW_TRANSACTIONS) Slog.i(TAG, " DIM "
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011507 + mDimSurface + ": alpha=" + mDimCurrentAlpha);
11508 mLastDimAnimTime = currentTime;
11509 mDimSurface.setAlpha(mDimCurrentAlpha);
11510 animating = true;
11511 } else {
11512 mDimCurrentAlpha = mDimTargetAlpha;
11513 mLastDimAnimTime = 0;
Joe Onorato8a9b2202010-02-26 18:56:32 -080011514 if (SHOW_TRANSACTIONS) Slog.i(TAG, " DIM "
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011515 + mDimSurface + ": final alpha=" + mDimCurrentAlpha);
11516 mDimSurface.setAlpha(mDimCurrentAlpha);
11517 if (!dimming) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011518 if (SHOW_TRANSACTIONS) Slog.i(TAG, " DIM " + mDimSurface
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011519 + ": HIDE");
11520 try {
11521 mDimSurface.hide();
11522 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011523 Slog.w(TAG, "Illegal argument exception hiding dim surface");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011524 }
11525 mDimShown = false;
11526 }
11527 }
11528 }
11529 return animating;
11530 }
11531
11532 public void printTo(PrintWriter pw) {
11533 pw.print(" mDimShown="); pw.print(mDimShown);
11534 pw.print(" current="); pw.print(mDimCurrentAlpha);
11535 pw.print(" target="); pw.print(mDimTargetAlpha);
11536 pw.print(" delta="); pw.print(mDimDeltaPerMs);
11537 pw.print(" lastAnimTime="); pw.println(mLastDimAnimTime);
11538 }
11539 }
11540
11541 /**
11542 * Animation that fade in after 0.5 interpolate time, or fade out in reverse order.
11543 * This is used for opening/closing transition for apps in compatible mode.
11544 */
11545 private static class FadeInOutAnimation extends Animation {
11546 int mWidth;
11547 boolean mFadeIn;
11548
11549 public FadeInOutAnimation(boolean fadeIn) {
11550 setInterpolator(new AccelerateInterpolator());
11551 setDuration(DEFAULT_FADE_IN_OUT_DURATION);
11552 mFadeIn = fadeIn;
11553 }
11554
11555 @Override
11556 protected void applyTransformation(float interpolatedTime, Transformation t) {
11557 float x = interpolatedTime;
11558 if (!mFadeIn) {
11559 x = 1.0f - x; // reverse the interpolation for fade out
11560 }
11561 if (x < 0.5) {
11562 // move the window out of the screen.
11563 t.getMatrix().setTranslate(mWidth, 0);
11564 } else {
11565 t.getMatrix().setTranslate(0, 0);// show
11566 t.setAlpha((x - 0.5f) * 2);
11567 }
11568 }
11569
11570 @Override
11571 public void initialize(int width, int height, int parentWidth, int parentHeight) {
11572 // width is the screen width {@see AppWindowToken#stepAnimatinoLocked}
11573 mWidth = width;
11574 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011575
11576 @Override
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -070011577 public int getZAdjustment() {
11578 return Animation.ZORDER_TOP;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011579 }
11580 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011581}