blob: 6e1b63ae535fab0d08cb07910f1db4bda09ec141 [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();
Mike Playlec6ded102010-11-29 16:01:03 +00005491 int flags = ev.getFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005492
5493 if (eventTime == 0) eventTime = SystemClock.uptimeMillis();
5494 if (downTime == 0) downTime = eventTime;
5495
5496 KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
Mike Playlec6ded102010-11-29 16:01:03 +00005497 deviceId, scancode, flags | KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005498
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005499 final int pid = Binder.getCallingPid();
5500 final int uid = Binder.getCallingUid();
5501 final long ident = Binder.clearCallingIdentity();
5502 final int result = dispatchKey(newEvent, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005503 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005504 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005505 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005506 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005507 switch (result) {
5508 case INJECT_NO_PERMISSION:
5509 throw new SecurityException(
Chander S Pechetty27f3de62010-02-10 22:14:00 +05305510 "Injecting to another application requires INJECT_EVENTS permission");
Dianne Hackborncfaef692009-06-15 14:24:44 -07005511 case INJECT_SUCCEEDED:
5512 return true;
5513 }
5514 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005515 }
5516
5517 /**
5518 * Inject a pointer (touch) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005519 *
5520 * @param ev A motion event describing the pointer (touch) action. (As noted in
5521 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005522 * {@link SystemClock#uptimeMillis()} as the timebase.)
5523 * @param sync If true, wait for the event to be completed before returning to the caller.
5524 * @return Returns true if event was dispatched, false if it was dropped for any reason
5525 */
5526 public boolean injectPointerEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005527 final int pid = Binder.getCallingPid();
5528 final int uid = Binder.getCallingUid();
5529 final long ident = Binder.clearCallingIdentity();
5530 final int result = dispatchPointer(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005531 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005532 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005533 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005534 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005535 switch (result) {
5536 case INJECT_NO_PERMISSION:
5537 throw new SecurityException(
Chander S Pechetty27f3de62010-02-10 22:14:00 +05305538 "Injecting to another application requires INJECT_EVENTS permission");
Dianne Hackborncfaef692009-06-15 14:24:44 -07005539 case INJECT_SUCCEEDED:
5540 return true;
5541 }
5542 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005543 }
Romain Guy06882f82009-06-10 13:36:04 -07005544
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005545 /**
5546 * Inject a trackball (navigation device) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005547 *
5548 * @param ev A motion event describing the trackball action. (As noted in
5549 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005550 * {@link SystemClock#uptimeMillis()} as the timebase.)
5551 * @param sync If true, wait for the event to be completed before returning to the caller.
5552 * @return Returns true if event was dispatched, false if it was dropped for any reason
5553 */
5554 public boolean injectTrackballEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005555 final int pid = Binder.getCallingPid();
5556 final int uid = Binder.getCallingUid();
5557 final long ident = Binder.clearCallingIdentity();
5558 final int result = dispatchTrackball(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005559 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005560 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005561 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005562 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005563 switch (result) {
5564 case INJECT_NO_PERMISSION:
5565 throw new SecurityException(
Chander S Pechetty27f3de62010-02-10 22:14:00 +05305566 "Injecting to another application requires INJECT_EVENTS permission");
Dianne Hackborncfaef692009-06-15 14:24:44 -07005567 case INJECT_SUCCEEDED:
5568 return true;
5569 }
5570 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005571 }
Romain Guy06882f82009-06-10 13:36:04 -07005572
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005573 private WindowState getFocusedWindow() {
5574 synchronized (mWindowMap) {
5575 return getFocusedWindowLocked();
5576 }
5577 }
5578
5579 private WindowState getFocusedWindowLocked() {
5580 return mCurrentFocus;
5581 }
Romain Guy06882f82009-06-10 13:36:04 -07005582
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005583 /**
5584 * This class holds the state for dispatching key events. This state
5585 * is protected by the KeyWaiter instance, NOT by the window lock. You
5586 * can be holding the main window lock while acquire the KeyWaiter lock,
5587 * but not the other way around.
5588 */
5589 final class KeyWaiter {
5590 // NOSHIP debugging
5591 public class DispatchState {
5592 private KeyEvent event;
5593 private WindowState focus;
5594 private long time;
5595 private WindowState lastWin;
5596 private IBinder lastBinder;
5597 private boolean finished;
5598 private boolean gotFirstWindow;
5599 private boolean eventDispatching;
5600 private long timeToSwitch;
5601 private boolean wasFrozen;
5602 private boolean focusPaused;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005603 private WindowState curFocus;
Romain Guy06882f82009-06-10 13:36:04 -07005604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005605 DispatchState(KeyEvent theEvent, WindowState theFocus) {
5606 focus = theFocus;
5607 event = theEvent;
5608 time = System.currentTimeMillis();
5609 // snapshot KeyWaiter state
5610 lastWin = mLastWin;
5611 lastBinder = mLastBinder;
5612 finished = mFinished;
5613 gotFirstWindow = mGotFirstWindow;
5614 eventDispatching = mEventDispatching;
5615 timeToSwitch = mTimeToSwitch;
5616 wasFrozen = mWasFrozen;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005617 curFocus = mCurrentFocus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005618 // cache the paused state at ctor time as well
5619 if (theFocus == null || theFocus.mToken == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005620 focusPaused = false;
5621 } else {
5622 focusPaused = theFocus.mToken.paused;
5623 }
5624 }
Romain Guy06882f82009-06-10 13:36:04 -07005625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005626 public String toString() {
5627 return "{{" + event + " to " + focus + " @ " + time
5628 + " lw=" + lastWin + " lb=" + lastBinder
5629 + " fin=" + finished + " gfw=" + gotFirstWindow
5630 + " ed=" + eventDispatching + " tts=" + timeToSwitch
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005631 + " wf=" + wasFrozen + " fp=" + focusPaused
Christopher Tate46d45252010-02-09 15:48:57 -08005632 + " mcf=" + curFocus + "}}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005633 }
5634 };
5635 private DispatchState mDispatchState = null;
5636 public void recordDispatchState(KeyEvent theEvent, WindowState theFocus) {
5637 mDispatchState = new DispatchState(theEvent, theFocus);
5638 }
5639 // END NOSHIP
5640
5641 public static final int RETURN_NOTHING = 0;
5642 public static final int RETURN_PENDING_POINTER = 1;
5643 public static final int RETURN_PENDING_TRACKBALL = 2;
Romain Guy06882f82009-06-10 13:36:04 -07005644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005645 final Object SKIP_TARGET_TOKEN = new Object();
5646 final Object CONSUMED_EVENT_TOKEN = new Object();
Romain Guy06882f82009-06-10 13:36:04 -07005647
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005648 private WindowState mLastWin = null;
5649 private IBinder mLastBinder = null;
5650 private boolean mFinished = true;
5651 private boolean mGotFirstWindow = false;
5652 private boolean mEventDispatching = true;
5653 private long mTimeToSwitch = 0;
5654 /* package */ boolean mWasFrozen = false;
Romain Guy06882f82009-06-10 13:36:04 -07005655
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005656 // Target of Motion events
5657 WindowState mMotionTarget;
Romain Guy06882f82009-06-10 13:36:04 -07005658
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005659 // Windows above the target who would like to receive an "outside"
5660 // touch event for any down events outside of them.
5661 WindowState mOutsideTouchTargets;
5662
5663 /**
5664 * Wait for the last event dispatch to complete, then find the next
5665 * target that should receive the given event and wait for that one
5666 * to be ready to receive it.
5667 */
5668 Object waitForNextEventTarget(KeyEvent nextKey, QueuedEvent qev,
5669 MotionEvent nextMotion, boolean isPointerEvent,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005670 boolean failIfTimeout, int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005671 long startTime = SystemClock.uptimeMillis();
5672 long keyDispatchingTimeout = 5 * 1000;
5673 long waitedFor = 0;
5674
5675 while (true) {
5676 // Figure out which window we care about. It is either the
5677 // last window we are waiting to have process the event or,
5678 // if none, then the next window we think the event should go
5679 // to. Note: we retrieve mLastWin outside of the lock, so
5680 // it may change before we lock. Thus we must check it again.
5681 WindowState targetWin = mLastWin;
5682 boolean targetIsNew = targetWin == null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08005683 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005684 TAG, "waitForLastKey: mFinished=" + mFinished +
5685 ", mLastWin=" + mLastWin);
5686 if (targetIsNew) {
5687 Object target = findTargetWindow(nextKey, qev, nextMotion,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005688 isPointerEvent, callingPid, callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005689 if (target == SKIP_TARGET_TOKEN) {
5690 // The user has pressed a special key, and we are
5691 // dropping all pending events before it.
Joe Onorato8a9b2202010-02-26 18:56:32 -08005692 if (DEBUG_INPUT) Slog.v(TAG, "Skipping: " + nextKey
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005693 + " " + nextMotion);
5694 return null;
5695 }
5696 if (target == CONSUMED_EVENT_TOKEN) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005697 if (DEBUG_INPUT) Slog.v(TAG, "Consumed: " + nextKey
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005698 + " " + nextMotion);
5699 return target;
5700 }
5701 targetWin = (WindowState)target;
5702 }
Romain Guy06882f82009-06-10 13:36:04 -07005703
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005704 AppWindowToken targetApp = null;
Romain Guy06882f82009-06-10 13:36:04 -07005705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005706 // Now: is it okay to send the next event to this window?
5707 synchronized (this) {
5708 // First: did we come here based on the last window not
5709 // being null, but it changed by the time we got here?
5710 // If so, try again.
5711 if (!targetIsNew && mLastWin == null) {
5712 continue;
5713 }
Romain Guy06882f82009-06-10 13:36:04 -07005714
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005715 // We never dispatch events if not finished with the
5716 // last one, or the display is frozen.
5717 if (mFinished && !mDisplayFrozen) {
5718 // If event dispatching is disabled, then we
5719 // just consume the events.
5720 if (!mEventDispatching) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005721 if (DEBUG_INPUT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005722 "Skipping event; dispatching disabled: "
5723 + nextKey + " " + nextMotion);
5724 return null;
5725 }
5726 if (targetWin != null) {
5727 // If this is a new target, and that target is not
5728 // paused or unresponsive, then all looks good to
5729 // handle the event.
5730 if (targetIsNew && !targetWin.mToken.paused) {
5731 return targetWin;
5732 }
Romain Guy06882f82009-06-10 13:36:04 -07005733
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005734 // If we didn't find a target window, and there is no
5735 // focused app window, then just eat the events.
5736 } else if (mFocusedApp == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005737 if (DEBUG_INPUT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005738 "Skipping event; no focused app: "
5739 + nextKey + " " + nextMotion);
5740 return null;
5741 }
5742 }
Romain Guy06882f82009-06-10 13:36:04 -07005743
Joe Onorato8a9b2202010-02-26 18:56:32 -08005744 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005745 TAG, "Waiting for last key in " + mLastBinder
5746 + " target=" + targetWin
5747 + " mFinished=" + mFinished
5748 + " mDisplayFrozen=" + mDisplayFrozen
5749 + " targetIsNew=" + targetIsNew
5750 + " paused="
5751 + (targetWin != null ? targetWin.mToken.paused : false)
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005752 + " mFocusedApp=" + mFocusedApp
5753 + " mCurrentFocus=" + mCurrentFocus);
Romain Guy06882f82009-06-10 13:36:04 -07005754
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005755 targetApp = targetWin != null
5756 ? targetWin.mAppToken : mFocusedApp;
Romain Guy06882f82009-06-10 13:36:04 -07005757
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005758 long curTimeout = keyDispatchingTimeout;
5759 if (mTimeToSwitch != 0) {
5760 long now = SystemClock.uptimeMillis();
5761 if (mTimeToSwitch <= now) {
5762 // If an app switch key has been pressed, and we have
5763 // waited too long for the current app to finish
5764 // processing keys, then wait no more!
Christopher Tate136b1f92010-02-11 17:51:24 -08005765 doFinishedKeyLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005766 continue;
5767 }
5768 long switchTimeout = mTimeToSwitch - now;
5769 if (curTimeout > switchTimeout) {
5770 curTimeout = switchTimeout;
5771 }
5772 }
Romain Guy06882f82009-06-10 13:36:04 -07005773
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005774 try {
5775 // after that continue
5776 // processing keys, so we don't get stuck.
Joe Onorato8a9b2202010-02-26 18:56:32 -08005777 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005778 TAG, "Waiting for key dispatch: " + curTimeout);
5779 wait(curTimeout);
Joe Onorato8a9b2202010-02-26 18:56:32 -08005780 if (DEBUG_INPUT) Slog.v(TAG, "Finished waiting @"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005781 + SystemClock.uptimeMillis() + " startTime="
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005782 + startTime + " switchTime=" + mTimeToSwitch
5783 + " target=" + targetWin + " mLW=" + mLastWin
5784 + " mLB=" + mLastBinder + " fin=" + mFinished
5785 + " mCurrentFocus=" + mCurrentFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005786 } catch (InterruptedException e) {
5787 }
5788 }
5789
5790 // If we were frozen during configuration change, restart the
5791 // timeout checks from now; otherwise look at whether we timed
5792 // out before awakening.
5793 if (mWasFrozen) {
5794 waitedFor = 0;
5795 mWasFrozen = false;
5796 } else {
5797 waitedFor = SystemClock.uptimeMillis() - startTime;
5798 }
5799
5800 if (waitedFor >= keyDispatchingTimeout && mTimeToSwitch == 0) {
5801 IApplicationToken at = null;
5802 synchronized (this) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005803 Slog.w(TAG, "Key dispatching timed out sending to " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005804 (targetWin != null ? targetWin.mAttrs.getTitle()
Ken Shirriff8200b202010-02-04 13:34:37 -08005805 : "<null>: no window ready for key dispatch"));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005806 // NOSHIP debugging
Joe Onorato8a9b2202010-02-26 18:56:32 -08005807 Slog.w(TAG, "Previous dispatch state: " + mDispatchState);
5808 Slog.w(TAG, "Current dispatch state: " +
Ken Shirriff8200b202010-02-04 13:34:37 -08005809 new DispatchState(nextKey, targetWin));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005810 // END NOSHIP
5811 //dump();
5812 if (targetWin != null) {
5813 at = targetWin.getAppToken();
5814 } else if (targetApp != null) {
5815 at = targetApp.appToken;
5816 }
5817 }
5818
5819 boolean abort = true;
5820 if (at != null) {
5821 try {
5822 long timeout = at.getKeyDispatchingTimeout();
5823 if (timeout > waitedFor) {
5824 // we did not wait the proper amount of time for this application.
5825 // set the timeout to be the real timeout and wait again.
5826 keyDispatchingTimeout = timeout - waitedFor;
5827 continue;
5828 } else {
5829 abort = at.keyDispatchingTimedOut();
5830 }
5831 } catch (RemoteException ex) {
5832 }
5833 }
5834
5835 synchronized (this) {
5836 if (abort && (mLastWin == targetWin || targetWin == null)) {
5837 mFinished = true;
Romain Guy06882f82009-06-10 13:36:04 -07005838 if (mLastWin != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005839 if (DEBUG_INPUT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005840 "Window " + mLastWin +
5841 " timed out on key input");
5842 if (mLastWin.mToken.paused) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005843 Slog.w(TAG, "Un-pausing dispatching to this window");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005844 mLastWin.mToken.paused = false;
5845 }
5846 }
5847 if (mMotionTarget == targetWin) {
5848 mMotionTarget = null;
5849 }
5850 mLastWin = null;
5851 mLastBinder = null;
5852 if (failIfTimeout || targetWin == null) {
5853 return null;
5854 }
5855 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005856 Slog.w(TAG, "Continuing to wait for key to be dispatched");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005857 startTime = SystemClock.uptimeMillis();
5858 }
5859 }
5860 }
5861 }
5862 }
Romain Guy06882f82009-06-10 13:36:04 -07005863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005864 Object findTargetWindow(KeyEvent nextKey, QueuedEvent qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005865 MotionEvent nextMotion, boolean isPointerEvent,
5866 int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005867 mOutsideTouchTargets = null;
Romain Guy06882f82009-06-10 13:36:04 -07005868
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005869 if (nextKey != null) {
5870 // Find the target window for a normal key event.
5871 final int keycode = nextKey.getKeyCode();
5872 final int repeatCount = nextKey.getRepeatCount();
5873 final boolean down = nextKey.getAction() != KeyEvent.ACTION_UP;
5874 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(keycode);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005875
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005876 if (!dispatch) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005877 if (callingUid == 0 ||
5878 mContext.checkPermission(
5879 android.Manifest.permission.INJECT_EVENTS,
5880 callingPid, callingUid)
5881 == PackageManager.PERMISSION_GRANTED) {
5882 mPolicy.interceptKeyTi(null, keycode,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005883 nextKey.getMetaState(), down, repeatCount,
5884 nextKey.getFlags());
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005885 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08005886 Slog.w(TAG, "Event timeout during app switch: dropping "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005887 + nextKey);
5888 return SKIP_TARGET_TOKEN;
5889 }
Romain Guy06882f82009-06-10 13:36:04 -07005890
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005891 // System.out.println("##### [" + SystemClock.uptimeMillis() + "] WindowManagerService.dispatchKey(" + keycode + ", " + down + ", " + repeatCount + ")");
Romain Guy06882f82009-06-10 13:36:04 -07005892
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005893 WindowState focus = null;
5894 synchronized(mWindowMap) {
5895 focus = getFocusedWindowLocked();
5896 }
Romain Guy06882f82009-06-10 13:36:04 -07005897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005898 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
Romain Guy06882f82009-06-10 13:36:04 -07005899
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005900 if (callingUid == 0 ||
5901 (focus != null && callingUid == focus.mSession.mUid) ||
5902 mContext.checkPermission(
5903 android.Manifest.permission.INJECT_EVENTS,
5904 callingPid, callingUid)
5905 == PackageManager.PERMISSION_GRANTED) {
5906 if (mPolicy.interceptKeyTi(focus,
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005907 keycode, nextKey.getMetaState(), down, repeatCount,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005908 nextKey.getFlags())) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005909 return CONSUMED_EVENT_TOKEN;
5910 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005911 }
Romain Guy06882f82009-06-10 13:36:04 -07005912
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005913 return focus;
Romain Guy06882f82009-06-10 13:36:04 -07005914
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005915 } else if (!isPointerEvent) {
5916 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(-1);
5917 if (!dispatch) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005918 Slog.w(TAG, "Event timeout during app switch: dropping trackball "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005919 + nextMotion);
5920 return SKIP_TARGET_TOKEN;
5921 }
Romain Guy06882f82009-06-10 13:36:04 -07005922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005923 WindowState focus = null;
5924 synchronized(mWindowMap) {
5925 focus = getFocusedWindowLocked();
5926 }
Romain Guy06882f82009-06-10 13:36:04 -07005927
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005928 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
5929 return focus;
5930 }
Romain Guy06882f82009-06-10 13:36:04 -07005931
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005932 if (nextMotion == null) {
5933 return SKIP_TARGET_TOKEN;
5934 }
Romain Guy06882f82009-06-10 13:36:04 -07005935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005936 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(
5937 KeyEvent.KEYCODE_UNKNOWN);
5938 if (!dispatch) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005939 Slog.w(TAG, "Event timeout during app switch: dropping pointer "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005940 + nextMotion);
5941 return SKIP_TARGET_TOKEN;
5942 }
Romain Guy06882f82009-06-10 13:36:04 -07005943
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005944 // Find the target window for a pointer event.
5945 int action = nextMotion.getAction();
5946 final float xf = nextMotion.getX();
5947 final float yf = nextMotion.getY();
5948 final long eventTime = nextMotion.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07005949
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005950 final boolean screenWasOff = qev != null
5951 && (qev.flags&WindowManagerPolicy.FLAG_BRIGHT_HERE) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07005952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005953 WindowState target = null;
Romain Guy06882f82009-06-10 13:36:04 -07005954
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005955 synchronized(mWindowMap) {
5956 synchronized (this) {
5957 if (action == MotionEvent.ACTION_DOWN) {
5958 if (mMotionTarget != null) {
5959 // this is weird, we got a pen down, but we thought it was
5960 // already down!
5961 // XXX: We should probably send an ACTION_UP to the current
5962 // target.
Joe Onorato8a9b2202010-02-26 18:56:32 -08005963 Slog.w(TAG, "Pointer down received while already down in: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005964 + mMotionTarget);
5965 mMotionTarget = null;
5966 }
Romain Guy06882f82009-06-10 13:36:04 -07005967
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005968 // ACTION_DOWN is special, because we need to lock next events to
5969 // the window we'll land onto.
5970 final int x = (int)xf;
5971 final int y = (int)yf;
Romain Guy06882f82009-06-10 13:36:04 -07005972
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005973 final ArrayList windows = mWindows;
5974 final int N = windows.size();
5975 WindowState topErrWindow = null;
5976 final Rect tmpRect = mTempRect;
5977 for (int i=N-1; i>=0; i--) {
5978 WindowState child = (WindowState)windows.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08005979 //Slog.i(TAG, "Checking dispatch to: " + child);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005980 final int flags = child.mAttrs.flags;
5981 if ((flags & WindowManager.LayoutParams.FLAG_SYSTEM_ERROR) != 0) {
5982 if (topErrWindow == null) {
5983 topErrWindow = child;
5984 }
5985 }
5986 if (!child.isVisibleLw()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005987 //Slog.i(TAG, "Not visible!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005988 continue;
5989 }
5990 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005991 //Slog.i(TAG, "Not touchable!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005992 if ((flags & WindowManager.LayoutParams
5993 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5994 child.mNextOutsideTouch = mOutsideTouchTargets;
5995 mOutsideTouchTargets = child;
5996 }
5997 continue;
5998 }
5999 tmpRect.set(child.mFrame);
6000 if (child.mTouchableInsets == ViewTreeObserver
6001 .InternalInsetsInfo.TOUCHABLE_INSETS_CONTENT) {
6002 // The touch is inside of the window if it is
6003 // inside the frame, AND the content part of that
6004 // frame that was given by the application.
6005 tmpRect.left += child.mGivenContentInsets.left;
6006 tmpRect.top += child.mGivenContentInsets.top;
6007 tmpRect.right -= child.mGivenContentInsets.right;
6008 tmpRect.bottom -= child.mGivenContentInsets.bottom;
6009 } else if (child.mTouchableInsets == ViewTreeObserver
6010 .InternalInsetsInfo.TOUCHABLE_INSETS_VISIBLE) {
6011 // The touch is inside of the window if it is
6012 // inside the frame, AND the visible part of that
6013 // frame that was given by the application.
6014 tmpRect.left += child.mGivenVisibleInsets.left;
6015 tmpRect.top += child.mGivenVisibleInsets.top;
6016 tmpRect.right -= child.mGivenVisibleInsets.right;
6017 tmpRect.bottom -= child.mGivenVisibleInsets.bottom;
6018 }
6019 final int touchFlags = flags &
6020 (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
6021 |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
6022 if (tmpRect.contains(x, y) || touchFlags == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006023 //Slog.i(TAG, "Using this target!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006024 if (!screenWasOff || (flags &
6025 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING) != 0) {
6026 mMotionTarget = child;
6027 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006028 //Slog.i(TAG, "Waking, skip!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006029 mMotionTarget = null;
6030 }
6031 break;
6032 }
Romain Guy06882f82009-06-10 13:36:04 -07006033
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006034 if ((flags & WindowManager.LayoutParams
6035 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
6036 child.mNextOutsideTouch = mOutsideTouchTargets;
6037 mOutsideTouchTargets = child;
Joe Onorato8a9b2202010-02-26 18:56:32 -08006038 //Slog.i(TAG, "Adding to outside target list: " + child);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006039 }
6040 }
6041
6042 // if there's an error window but it's not accepting
6043 // focus (typically because it is not yet visible) just
6044 // wait for it -- any other focused window may in fact
6045 // be in ANR state.
6046 if (topErrWindow != null && mMotionTarget != topErrWindow) {
6047 mMotionTarget = null;
6048 }
6049 }
Romain Guy06882f82009-06-10 13:36:04 -07006050
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006051 target = mMotionTarget;
6052 }
6053 }
Romain Guy06882f82009-06-10 13:36:04 -07006054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006055 wakeupIfNeeded(target, eventType(nextMotion));
Romain Guy06882f82009-06-10 13:36:04 -07006056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006057 // Pointer events are a little different -- if there isn't a
6058 // target found for any event, then just drop it.
6059 return target != null ? target : SKIP_TARGET_TOKEN;
6060 }
Romain Guy06882f82009-06-10 13:36:04 -07006061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006062 boolean checkShouldDispatchKey(int keycode) {
6063 synchronized (this) {
6064 if (mPolicy.isAppSwitchKeyTqTiLwLi(keycode)) {
6065 mTimeToSwitch = 0;
6066 return true;
6067 }
6068 if (mTimeToSwitch != 0
6069 && mTimeToSwitch < SystemClock.uptimeMillis()) {
6070 return false;
6071 }
6072 return true;
6073 }
6074 }
Romain Guy06882f82009-06-10 13:36:04 -07006075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006076 void bindTargetWindowLocked(WindowState win,
6077 int pendingWhat, QueuedEvent pendingMotion) {
6078 synchronized (this) {
6079 bindTargetWindowLockedLocked(win, pendingWhat, pendingMotion);
6080 }
6081 }
Romain Guy06882f82009-06-10 13:36:04 -07006082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006083 void bindTargetWindowLocked(WindowState win) {
6084 synchronized (this) {
6085 bindTargetWindowLockedLocked(win, RETURN_NOTHING, null);
6086 }
6087 }
6088
6089 void bindTargetWindowLockedLocked(WindowState win,
6090 int pendingWhat, QueuedEvent pendingMotion) {
6091 mLastWin = win;
6092 mLastBinder = win.mClient.asBinder();
6093 mFinished = false;
6094 if (pendingMotion != null) {
6095 final Session s = win.mSession;
6096 if (pendingWhat == RETURN_PENDING_POINTER) {
6097 releasePendingPointerLocked(s);
6098 s.mPendingPointerMove = pendingMotion;
6099 s.mPendingPointerWindow = win;
Joe Onorato8a9b2202010-02-26 18:56:32 -08006100 if (DEBUG_INPUT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006101 "bindTargetToWindow " + s.mPendingPointerMove);
6102 } else if (pendingWhat == RETURN_PENDING_TRACKBALL) {
6103 releasePendingTrackballLocked(s);
6104 s.mPendingTrackballMove = pendingMotion;
6105 s.mPendingTrackballWindow = win;
6106 }
6107 }
6108 }
Romain Guy06882f82009-06-10 13:36:04 -07006109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006110 void releasePendingPointerLocked(Session s) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006111 if (DEBUG_INPUT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006112 "releasePendingPointer " + s.mPendingPointerMove);
6113 if (s.mPendingPointerMove != null) {
6114 mQueue.recycleEvent(s.mPendingPointerMove);
6115 s.mPendingPointerMove = null;
6116 }
6117 }
Romain Guy06882f82009-06-10 13:36:04 -07006118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006119 void releasePendingTrackballLocked(Session s) {
6120 if (s.mPendingTrackballMove != null) {
6121 mQueue.recycleEvent(s.mPendingTrackballMove);
6122 s.mPendingTrackballMove = null;
6123 }
6124 }
Romain Guy06882f82009-06-10 13:36:04 -07006125
Maciej Białka26824b72010-03-05 08:40:09 +01006126 void releaseMotionTarget(WindowState win) {
6127 if (mMotionTarget == win) {
6128 mMotionTarget = null;
6129 }
6130 }
6131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006132 MotionEvent finishedKey(Session session, IWindow client, boolean force,
6133 int returnWhat) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006134 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006135 TAG, "finishedKey: client=" + client + ", force=" + force);
6136
6137 if (client == null) {
6138 return null;
6139 }
6140
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07006141 MotionEvent res = null;
6142 QueuedEvent qev = null;
6143 WindowState win = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006145 synchronized (this) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006146 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006147 TAG, "finishedKey: client=" + client.asBinder()
6148 + ", force=" + force + ", last=" + mLastBinder
6149 + " (token=" + (mLastWin != null ? mLastWin.mToken : null) + ")");
6150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006151 if (returnWhat == RETURN_PENDING_POINTER) {
6152 qev = session.mPendingPointerMove;
6153 win = session.mPendingPointerWindow;
6154 session.mPendingPointerMove = null;
6155 session.mPendingPointerWindow = null;
6156 } else if (returnWhat == RETURN_PENDING_TRACKBALL) {
6157 qev = session.mPendingTrackballMove;
6158 win = session.mPendingTrackballWindow;
6159 session.mPendingTrackballMove = null;
6160 session.mPendingTrackballWindow = null;
6161 }
Romain Guy06882f82009-06-10 13:36:04 -07006162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006163 if (mLastBinder == client.asBinder()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006164 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006165 TAG, "finishedKey: last paused="
6166 + ((mLastWin != null) ? mLastWin.mToken.paused : "null"));
6167 if (mLastWin != null && (!mLastWin.mToken.paused || force
6168 || !mEventDispatching)) {
Christopher Tate136b1f92010-02-11 17:51:24 -08006169 doFinishedKeyLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006170 } else {
6171 // Make sure to wake up anyone currently waiting to
6172 // dispatch a key, so they can re-evaluate their
6173 // current situation.
6174 mFinished = true;
6175 notifyAll();
6176 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006177 }
Romain Guy06882f82009-06-10 13:36:04 -07006178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006179 if (qev != null) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07006180 res = (MotionEvent)qev.event;
Joe Onorato8a9b2202010-02-26 18:56:32 -08006181 if (DEBUG_INPUT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006182 "Returning pending motion: " + res);
6183 mQueue.recycleEvent(qev);
6184 if (win != null && returnWhat == RETURN_PENDING_POINTER) {
6185 res.offsetLocation(-win.mFrame.left, -win.mFrame.top);
6186 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006187 }
Christopher Tate2624fbc2009-12-11 12:11:31 -08006188 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006189
Christopher Tate2624fbc2009-12-11 12:11:31 -08006190 if (res != null && returnWhat == RETURN_PENDING_POINTER) {
6191 synchronized (mWindowMap) {
Dianne Hackborn90d2db32010-02-11 22:19:06 -08006192 dispatchPointerElsewhereLocked(win, win, res, res.getEventTime(), false);
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07006193 }
6194 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006195
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07006196 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006197 }
6198
6199 void tickle() {
6200 synchronized (this) {
6201 notifyAll();
6202 }
6203 }
Romain Guy06882f82009-06-10 13:36:04 -07006204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006205 void handleNewWindowLocked(WindowState newWindow) {
6206 if (!newWindow.canReceiveKeys()) {
6207 return;
6208 }
6209 synchronized (this) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006210 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006211 TAG, "New key dispatch window: win="
6212 + newWindow.mClient.asBinder()
6213 + ", last=" + mLastBinder
6214 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
6215 + "), finished=" + mFinished + ", paused="
6216 + newWindow.mToken.paused);
6217
6218 // Displaying a window implicitly causes dispatching to
6219 // be unpaused. (This is to protect against bugs if someone
6220 // pauses dispatching but forgets to resume.)
6221 newWindow.mToken.paused = false;
6222
6223 mGotFirstWindow = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006224
6225 if ((newWindow.mAttrs.flags & FLAG_SYSTEM_ERROR) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006226 if (DEBUG_INPUT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006227 "New SYSTEM_ERROR window; resetting state");
6228 mLastWin = null;
6229 mLastBinder = null;
6230 mMotionTarget = null;
6231 mFinished = true;
6232 } else if (mLastWin != null) {
6233 // If the new window is above the window we are
6234 // waiting on, then stop waiting and let key dispatching
6235 // start on the new guy.
Joe Onorato8a9b2202010-02-26 18:56:32 -08006236 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006237 TAG, "Last win layer=" + mLastWin.mLayer
6238 + ", new win layer=" + newWindow.mLayer);
6239 if (newWindow.mLayer >= mLastWin.mLayer) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006240 // The new window is above the old; finish pending input to the last
6241 // window and start directing it to the new one.
6242 mLastWin.mToken.paused = false;
Christopher Tate136b1f92010-02-11 17:51:24 -08006243 doFinishedKeyLocked(false); // does a notifyAll()
6244 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006245 }
6246 }
6247
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006248 // Now that we've put a new window state in place, make the event waiter
6249 // take notice and retarget its attentions.
6250 notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006251 }
6252 }
6253
6254 void pauseDispatchingLocked(WindowToken token) {
6255 synchronized (this)
6256 {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006257 if (DEBUG_INPUT) Slog.v(TAG, "Pausing WindowToken " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006258 token.paused = true;
6259
6260 /*
6261 if (mLastWin != null && !mFinished && mLastWin.mBaseLayer <= layer) {
6262 mPaused = true;
6263 } else {
6264 if (mLastWin == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006265 Slog.i(TAG, "Key dispatching not paused: no last window.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006266 } else if (mFinished) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006267 Slog.i(TAG, "Key dispatching not paused: finished last key.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006268 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006269 Slog.i(TAG, "Key dispatching not paused: window in higher layer.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006270 }
6271 }
6272 */
6273 }
6274 }
6275
6276 void resumeDispatchingLocked(WindowToken token) {
6277 synchronized (this) {
6278 if (token.paused) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006279 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006280 TAG, "Resuming WindowToken " + token
6281 + ", last=" + mLastBinder
6282 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
6283 + "), finished=" + mFinished + ", paused="
6284 + token.paused);
6285 token.paused = false;
6286 if (mLastWin != null && mLastWin.mToken == token && mFinished) {
Christopher Tate136b1f92010-02-11 17:51:24 -08006287 doFinishedKeyLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006288 } else {
6289 notifyAll();
6290 }
6291 }
6292 }
6293 }
6294
6295 void setEventDispatchingLocked(boolean enabled) {
6296 synchronized (this) {
6297 mEventDispatching = enabled;
6298 notifyAll();
6299 }
6300 }
Romain Guy06882f82009-06-10 13:36:04 -07006301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006302 void appSwitchComing() {
6303 synchronized (this) {
6304 // Don't wait for more than .5 seconds for app to finish
6305 // processing the pending events.
6306 long now = SystemClock.uptimeMillis() + 500;
Joe Onorato8a9b2202010-02-26 18:56:32 -08006307 if (DEBUG_INPUT) Slog.v(TAG, "appSwitchComing: " + now);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006308 if (mTimeToSwitch == 0 || now < mTimeToSwitch) {
6309 mTimeToSwitch = now;
6310 }
6311 notifyAll();
6312 }
6313 }
Romain Guy06882f82009-06-10 13:36:04 -07006314
Christopher Tate136b1f92010-02-11 17:51:24 -08006315 private final void doFinishedKeyLocked(boolean force) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006316 if (mLastWin != null) {
6317 releasePendingPointerLocked(mLastWin.mSession);
6318 releasePendingTrackballLocked(mLastWin.mSession);
6319 }
Romain Guy06882f82009-06-10 13:36:04 -07006320
Christopher Tate136b1f92010-02-11 17:51:24 -08006321 if (force || mLastWin == null || !mLastWin.mToken.paused
6322 || !mLastWin.isVisibleLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006323 // If the current window has been paused, we aren't -really-
6324 // finished... so let the waiters still wait.
6325 mLastWin = null;
6326 mLastBinder = null;
6327 }
6328 mFinished = true;
6329 notifyAll();
6330 }
6331 }
6332
6333 private class KeyQ extends KeyInputQueue
6334 implements KeyInputQueue.FilterCallback {
6335 PowerManager.WakeLock mHoldingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07006336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006337 KeyQ() {
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07006338 super(mContext, WindowManagerService.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006339 PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
6340 mHoldingScreen = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
6341 "KEEP_SCREEN_ON_FLAG");
6342 mHoldingScreen.setReferenceCounted(false);
6343 }
6344
6345 @Override
6346 boolean preprocessEvent(InputDevice device, RawInputEvent event) {
6347 if (mPolicy.preprocessInputEventTq(event)) {
6348 return true;
6349 }
Romain Guy06882f82009-06-10 13:36:04 -07006350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006351 switch (event.type) {
6352 case RawInputEvent.EV_KEY: {
6353 // XXX begin hack
6354 if (DEBUG) {
6355 if (event.keycode == KeyEvent.KEYCODE_G) {
6356 if (event.value != 0) {
6357 // G down
6358 mPolicy.screenTurnedOff(WindowManagerPolicy.OFF_BECAUSE_OF_USER);
6359 }
6360 return false;
6361 }
6362 if (event.keycode == KeyEvent.KEYCODE_D) {
6363 if (event.value != 0) {
6364 //dump();
6365 }
6366 return false;
6367 }
6368 }
6369 // XXX end hack
Romain Guy06882f82009-06-10 13:36:04 -07006370
Charles Mendis322591c2009-10-29 11:06:59 -07006371 boolean screenIsOff = !mPowerManager.isScreenOn();
6372 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006373 int actions = mPolicy.interceptKeyTq(event, !screenIsOff);
Romain Guy06882f82009-06-10 13:36:04 -07006374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006375 if ((actions & WindowManagerPolicy.ACTION_GO_TO_SLEEP) != 0) {
6376 mPowerManager.goToSleep(event.when);
6377 }
6378
6379 if (screenIsOff) {
6380 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6381 }
6382 if (screenIsDim) {
6383 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6384 }
6385 if ((actions & WindowManagerPolicy.ACTION_POKE_USER_ACTIVITY) != 0) {
6386 mPowerManager.userActivity(event.when, false,
Michael Chane96440f2009-05-06 10:27:36 -07006387 LocalPowerManager.BUTTON_EVENT, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006388 }
Romain Guy06882f82009-06-10 13:36:04 -07006389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006390 if ((actions & WindowManagerPolicy.ACTION_PASS_TO_USER) != 0) {
6391 if (event.value != 0 && mPolicy.isAppSwitchKeyTqTiLwLi(event.keycode)) {
6392 filterQueue(this);
6393 mKeyWaiter.appSwitchComing();
6394 }
6395 return true;
6396 } else {
6397 return false;
6398 }
6399 }
Romain Guy06882f82009-06-10 13:36:04 -07006400
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006401 case RawInputEvent.EV_REL: {
Charles Mendis322591c2009-10-29 11:06:59 -07006402 boolean screenIsOff = !mPowerManager.isScreenOn();
6403 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006404 if (screenIsOff) {
6405 if (!mPolicy.isWakeRelMovementTq(event.deviceId,
6406 device.classes, event)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006407 //Slog.i(TAG, "dropping because screenIsOff and !isWakeKey");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006408 return false;
6409 }
6410 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6411 }
6412 if (screenIsDim) {
6413 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6414 }
6415 return true;
6416 }
Romain Guy06882f82009-06-10 13:36:04 -07006417
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006418 case RawInputEvent.EV_ABS: {
Charles Mendis322591c2009-10-29 11:06:59 -07006419 boolean screenIsOff = !mPowerManager.isScreenOn();
6420 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006421 if (screenIsOff) {
6422 if (!mPolicy.isWakeAbsMovementTq(event.deviceId,
6423 device.classes, event)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006424 //Slog.i(TAG, "dropping because screenIsOff and !isWakeKey");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006425 return false;
6426 }
6427 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6428 }
6429 if (screenIsDim) {
6430 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6431 }
6432 return true;
6433 }
Romain Guy06882f82009-06-10 13:36:04 -07006434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006435 default:
6436 return true;
6437 }
6438 }
6439
6440 public int filterEvent(QueuedEvent ev) {
6441 switch (ev.classType) {
6442 case RawInputEvent.CLASS_KEYBOARD:
6443 KeyEvent ke = (KeyEvent)ev.event;
6444 if (mPolicy.isMovementKeyTi(ke.getKeyCode())) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006445 Slog.w(TAG, "Dropping movement key during app switch: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006446 + ke.getKeyCode() + ", action=" + ke.getAction());
6447 return FILTER_REMOVE;
6448 }
6449 return FILTER_ABORT;
6450 default:
6451 return FILTER_KEEP;
6452 }
6453 }
Romain Guy06882f82009-06-10 13:36:04 -07006454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006455 /**
6456 * Must be called with the main window manager lock held.
6457 */
6458 void setHoldScreenLocked(boolean holding) {
6459 boolean state = mHoldingScreen.isHeld();
6460 if (holding != state) {
6461 if (holding) {
6462 mHoldingScreen.acquire();
6463 } else {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006464 mPolicy.screenOnStoppedLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006465 mHoldingScreen.release();
6466 }
6467 }
6468 }
Michael Chan53071d62009-05-13 17:29:48 -07006469 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006470
6471 public boolean detectSafeMode() {
6472 mSafeMode = mPolicy.detectSafeMode();
6473 return mSafeMode;
6474 }
Romain Guy06882f82009-06-10 13:36:04 -07006475
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006476 public void systemReady() {
6477 mPolicy.systemReady();
6478 }
Romain Guy06882f82009-06-10 13:36:04 -07006479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006480 private final class InputDispatcherThread extends Thread {
6481 // Time to wait when there is nothing to do: 9999 seconds.
6482 static final int LONG_WAIT=9999*1000;
6483
6484 public InputDispatcherThread() {
6485 super("InputDispatcher");
6486 }
Romain Guy06882f82009-06-10 13:36:04 -07006487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006488 @Override
6489 public void run() {
6490 while (true) {
6491 try {
6492 process();
6493 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006494 Slog.e(TAG, "Exception in input dispatcher", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006495 }
6496 }
6497 }
Romain Guy06882f82009-06-10 13:36:04 -07006498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006499 private void process() {
6500 android.os.Process.setThreadPriority(
6501 android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -07006502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006503 // The last key event we saw
6504 KeyEvent lastKey = null;
6505
6506 // Last keydown time for auto-repeating keys
6507 long lastKeyTime = SystemClock.uptimeMillis();
6508 long nextKeyTime = lastKeyTime+LONG_WAIT;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006509 long downTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006510
Romain Guy06882f82009-06-10 13:36:04 -07006511 // How many successive repeats we generated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006512 int keyRepeatCount = 0;
6513
6514 // Need to report that configuration has changed?
6515 boolean configChanged = false;
Romain Guy06882f82009-06-10 13:36:04 -07006516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006517 while (true) {
6518 long curTime = SystemClock.uptimeMillis();
6519
Joe Onorato8a9b2202010-02-26 18:56:32 -08006520 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006521 TAG, "Waiting for next key: now=" + curTime
6522 + ", repeat @ " + nextKeyTime);
6523
6524 // Retrieve next event, waiting only as long as the next
6525 // repeat timeout. If the configuration has changed, then
6526 // don't wait at all -- we'll report the change as soon as
6527 // we have processed all events.
6528 QueuedEvent ev = mQueue.getEvent(
6529 (int)((!configChanged && curTime < nextKeyTime)
6530 ? (nextKeyTime-curTime) : 0));
6531
Joe Onorato8a9b2202010-02-26 18:56:32 -08006532 if (DEBUG_INPUT && ev != null) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006533 TAG, "Event: type=" + ev.classType + " data=" + ev.event);
6534
Michael Chan53071d62009-05-13 17:29:48 -07006535 if (MEASURE_LATENCY) {
6536 lt.sample("2 got event ", System.nanoTime() - ev.whenNano);
6537 }
6538
Mike Lockwood3d0ea722009-10-21 22:58:29 -04006539 if (lastKey != null && !mPolicy.allowKeyRepeat()) {
6540 // cancel key repeat at the request of the policy.
6541 lastKey = null;
6542 downTime = 0;
6543 lastKeyTime = curTime;
6544 nextKeyTime = curTime + LONG_WAIT;
6545 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006546 try {
6547 if (ev != null) {
Michael Chan53071d62009-05-13 17:29:48 -07006548 curTime = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006549 int eventType;
6550 if (ev.classType == RawInputEvent.CLASS_TOUCHSCREEN) {
6551 eventType = eventType((MotionEvent)ev.event);
6552 } else if (ev.classType == RawInputEvent.CLASS_KEYBOARD ||
6553 ev.classType == RawInputEvent.CLASS_TRACKBALL) {
6554 eventType = LocalPowerManager.BUTTON_EVENT;
6555 } else {
6556 eventType = LocalPowerManager.OTHER_EVENT;
6557 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006558 try {
Michael Chan53071d62009-05-13 17:29:48 -07006559 if ((curTime - mLastBatteryStatsCallTime)
Michael Chane96440f2009-05-06 10:27:36 -07006560 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
Michael Chan53071d62009-05-13 17:29:48 -07006561 mLastBatteryStatsCallTime = curTime;
Michael Chane96440f2009-05-06 10:27:36 -07006562 mBatteryStats.noteInputEvent();
6563 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006564 } catch (RemoteException e) {
6565 // Ignore
6566 }
Michael Chane10de972009-05-18 11:24:50 -07006567
Mike Lockwood5db42402009-11-30 14:51:51 -05006568 if (ev.classType == RawInputEvent.CLASS_CONFIGURATION_CHANGED) {
6569 // do not wake screen in this case
6570 } else if (eventType != TOUCH_EVENT
Michael Chane10de972009-05-18 11:24:50 -07006571 && eventType != LONG_TOUCH_EVENT
6572 && eventType != CHEEK_EVENT) {
6573 mPowerManager.userActivity(curTime, false,
6574 eventType, false);
6575 } else if (mLastTouchEventType != eventType
6576 || (curTime - mLastUserActivityCallTime)
6577 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
6578 mLastUserActivityCallTime = curTime;
6579 mLastTouchEventType = eventType;
6580 mPowerManager.userActivity(curTime, false,
6581 eventType, false);
6582 }
6583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006584 switch (ev.classType) {
6585 case RawInputEvent.CLASS_KEYBOARD:
6586 KeyEvent ke = (KeyEvent)ev.event;
6587 if (ke.isDown()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006588 lastKeyTime = curTime;
Kristian Dreher133bfdf2010-02-23 08:50:58 +01006589 if (lastKey != null &&
6590 ke.getKeyCode() == lastKey.getKeyCode()) {
6591 keyRepeatCount++;
6592 // Arbitrary long timeout to block
6593 // repeating here since we know that
6594 // the device driver takes care of it.
6595 nextKeyTime = lastKeyTime + LONG_WAIT;
The Android Open Source Project2a9ae012010-05-12 12:33:35 -07006596 if (DEBUG_INPUT) Slog.v(
Kristian Dreher133bfdf2010-02-23 08:50:58 +01006597 TAG, "Received repeated key down");
6598 } else {
6599 downTime = curTime;
6600 keyRepeatCount = 0;
6601 nextKeyTime = lastKeyTime
6602 + ViewConfiguration.getLongPressTimeout();
The Android Open Source Project2a9ae012010-05-12 12:33:35 -07006603 if (DEBUG_INPUT) Slog.v(
Kristian Dreher133bfdf2010-02-23 08:50:58 +01006604 TAG, "Received key down: first repeat @ "
6605 + nextKeyTime);
6606 }
6607 lastKey = ke;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006608 } else {
6609 lastKey = null;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006610 downTime = 0;
Kristian Dreher133bfdf2010-02-23 08:50:58 +01006611 keyRepeatCount = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006612 // Arbitrary long timeout.
6613 lastKeyTime = curTime;
6614 nextKeyTime = curTime + LONG_WAIT;
Joe Onorato8a9b2202010-02-26 18:56:32 -08006615 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006616 TAG, "Received key up: ignore repeat @ "
6617 + nextKeyTime);
6618 }
Kristian Dreher133bfdf2010-02-23 08:50:58 +01006619 if (keyRepeatCount > 0) {
6620 dispatchKey(KeyEvent.changeTimeRepeat(ke,
6621 ke.getEventTime(), keyRepeatCount), 0, 0);
6622 } else {
6623 dispatchKey(ke, 0, 0);
6624 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006625 mQueue.recycleEvent(ev);
6626 break;
6627 case RawInputEvent.CLASS_TOUCHSCREEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08006628 //Slog.i(TAG, "Read next event " + ev);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006629 dispatchPointer(ev, (MotionEvent)ev.event, 0, 0);
6630 break;
6631 case RawInputEvent.CLASS_TRACKBALL:
6632 dispatchTrackball(ev, (MotionEvent)ev.event, 0, 0);
6633 break;
6634 case RawInputEvent.CLASS_CONFIGURATION_CHANGED:
6635 configChanged = true;
6636 break;
6637 default:
6638 mQueue.recycleEvent(ev);
6639 break;
6640 }
Romain Guy06882f82009-06-10 13:36:04 -07006641
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006642 } else if (configChanged) {
6643 configChanged = false;
6644 sendNewConfiguration();
Romain Guy06882f82009-06-10 13:36:04 -07006645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006646 } else if (lastKey != null) {
6647 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006648
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006649 // Timeout occurred while key was down. If it is at or
6650 // past the key repeat time, dispatch the repeat.
Joe Onorato8a9b2202010-02-26 18:56:32 -08006651 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006652 TAG, "Key timeout: repeat=" + nextKeyTime
6653 + ", now=" + curTime);
6654 if (curTime < nextKeyTime) {
6655 continue;
6656 }
Romain Guy06882f82009-06-10 13:36:04 -07006657
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006658 lastKeyTime = nextKeyTime;
6659 nextKeyTime = nextKeyTime + KEY_REPEAT_DELAY;
6660 keyRepeatCount++;
Joe Onorato8a9b2202010-02-26 18:56:32 -08006661 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006662 TAG, "Key repeat: count=" + keyRepeatCount
6663 + ", next @ " + nextKeyTime);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006664 KeyEvent newEvent;
6665 if (downTime != 0 && (downTime
6666 + ViewConfiguration.getLongPressTimeout())
6667 <= curTime) {
6668 newEvent = KeyEvent.changeTimeRepeat(lastKey,
6669 curTime, keyRepeatCount,
6670 lastKey.getFlags() | KeyEvent.FLAG_LONG_PRESS);
6671 downTime = 0;
6672 } else {
6673 newEvent = KeyEvent.changeTimeRepeat(lastKey,
6674 curTime, keyRepeatCount);
6675 }
6676 dispatchKey(newEvent, 0, 0);
Romain Guy06882f82009-06-10 13:36:04 -07006677
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006678 } else {
6679 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006680
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006681 lastKeyTime = curTime;
6682 nextKeyTime = curTime + LONG_WAIT;
6683 }
Romain Guy06882f82009-06-10 13:36:04 -07006684
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006685 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006686 Slog.e(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006687 "Input thread received uncaught exception: " + e, e);
6688 }
6689 }
6690 }
6691 }
6692
6693 // -------------------------------------------------------------
6694 // Client Session State
6695 // -------------------------------------------------------------
6696
6697 private final class Session extends IWindowSession.Stub
6698 implements IBinder.DeathRecipient {
6699 final IInputMethodClient mClient;
6700 final IInputContext mInputContext;
6701 final int mUid;
6702 final int mPid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006703 final String mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006704 SurfaceSession mSurfaceSession;
6705 int mNumWindow = 0;
6706 boolean mClientDead = false;
Romain Guy06882f82009-06-10 13:36:04 -07006707
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006708 /**
6709 * Current pointer move event being dispatched to client window... must
6710 * hold key lock to access.
6711 */
6712 QueuedEvent mPendingPointerMove;
6713 WindowState mPendingPointerWindow;
Romain Guy06882f82009-06-10 13:36:04 -07006714
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006715 /**
6716 * Current trackball move event being dispatched to client window... must
6717 * hold key lock to access.
6718 */
6719 QueuedEvent mPendingTrackballMove;
6720 WindowState mPendingTrackballWindow;
6721
6722 public Session(IInputMethodClient client, IInputContext inputContext) {
6723 mClient = client;
6724 mInputContext = inputContext;
6725 mUid = Binder.getCallingUid();
6726 mPid = Binder.getCallingPid();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006727 StringBuilder sb = new StringBuilder();
6728 sb.append("Session{");
6729 sb.append(Integer.toHexString(System.identityHashCode(this)));
6730 sb.append(" uid ");
6731 sb.append(mUid);
6732 sb.append("}");
6733 mStringName = sb.toString();
Romain Guy06882f82009-06-10 13:36:04 -07006734
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006735 synchronized (mWindowMap) {
6736 if (mInputMethodManager == null && mHaveInputMethods) {
6737 IBinder b = ServiceManager.getService(
6738 Context.INPUT_METHOD_SERVICE);
6739 mInputMethodManager = IInputMethodManager.Stub.asInterface(b);
6740 }
6741 }
6742 long ident = Binder.clearCallingIdentity();
6743 try {
6744 // Note: it is safe to call in to the input method manager
6745 // here because we are not holding our lock.
6746 if (mInputMethodManager != null) {
6747 mInputMethodManager.addClient(client, inputContext,
6748 mUid, mPid);
6749 } else {
6750 client.setUsingInputMethod(false);
6751 }
6752 client.asBinder().linkToDeath(this, 0);
6753 } catch (RemoteException e) {
6754 // The caller has died, so we can just forget about this.
6755 try {
6756 if (mInputMethodManager != null) {
6757 mInputMethodManager.removeClient(client);
6758 }
6759 } catch (RemoteException ee) {
6760 }
6761 } finally {
6762 Binder.restoreCallingIdentity(ident);
6763 }
6764 }
Romain Guy06882f82009-06-10 13:36:04 -07006765
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006766 @Override
6767 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
6768 throws RemoteException {
6769 try {
6770 return super.onTransact(code, data, reply, flags);
6771 } catch (RuntimeException e) {
6772 // Log all 'real' exceptions thrown to the caller
6773 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006774 Slog.e(TAG, "Window Session Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006775 }
6776 throw e;
6777 }
6778 }
6779
6780 public void binderDied() {
6781 // Note: it is safe to call in to the input method manager
6782 // here because we are not holding our lock.
6783 try {
6784 if (mInputMethodManager != null) {
6785 mInputMethodManager.removeClient(mClient);
6786 }
6787 } catch (RemoteException e) {
6788 }
6789 synchronized(mWindowMap) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07006790 mClient.asBinder().unlinkToDeath(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006791 mClientDead = true;
6792 killSessionLocked();
6793 }
6794 }
6795
6796 public int add(IWindow window, WindowManager.LayoutParams attrs,
6797 int viewVisibility, Rect outContentInsets) {
6798 return addWindow(this, window, attrs, viewVisibility, outContentInsets);
6799 }
Romain Guy06882f82009-06-10 13:36:04 -07006800
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006801 public void remove(IWindow window) {
6802 removeWindow(this, window);
6803 }
Romain Guy06882f82009-06-10 13:36:04 -07006804
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006805 public int relayout(IWindow window, WindowManager.LayoutParams attrs,
6806 int requestedWidth, int requestedHeight, int viewFlags,
6807 boolean insetsPending, Rect outFrame, Rect outContentInsets,
Dianne Hackborn694f79b2010-03-17 19:44:59 -07006808 Rect outVisibleInsets, Configuration outConfig, Surface outSurface) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006809 return relayoutWindow(this, window, attrs,
6810 requestedWidth, requestedHeight, viewFlags, insetsPending,
Dianne Hackborn694f79b2010-03-17 19:44:59 -07006811 outFrame, outContentInsets, outVisibleInsets, outConfig, outSurface);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006812 }
Romain Guy06882f82009-06-10 13:36:04 -07006813
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006814 public void setTransparentRegion(IWindow window, Region region) {
6815 setTransparentRegionWindow(this, window, region);
6816 }
Romain Guy06882f82009-06-10 13:36:04 -07006817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006818 public void setInsets(IWindow window, int touchableInsets,
6819 Rect contentInsets, Rect visibleInsets) {
6820 setInsetsWindow(this, window, touchableInsets, contentInsets,
6821 visibleInsets);
6822 }
Romain Guy06882f82009-06-10 13:36:04 -07006823
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006824 public void getDisplayFrame(IWindow window, Rect outDisplayFrame) {
6825 getWindowDisplayFrame(this, window, outDisplayFrame);
6826 }
Romain Guy06882f82009-06-10 13:36:04 -07006827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006828 public void finishDrawing(IWindow window) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006829 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006830 TAG, "IWindow finishDrawing called for " + window);
6831 finishDrawingWindow(this, window);
6832 }
6833
6834 public void finishKey(IWindow window) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006835 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006836 TAG, "IWindow finishKey called for " + window);
6837 mKeyWaiter.finishedKey(this, window, false,
6838 KeyWaiter.RETURN_NOTHING);
6839 }
6840
6841 public MotionEvent getPendingPointerMove(IWindow window) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006842 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006843 TAG, "IWindow getPendingMotionEvent called for " + window);
6844 return mKeyWaiter.finishedKey(this, window, false,
6845 KeyWaiter.RETURN_PENDING_POINTER);
6846 }
Romain Guy06882f82009-06-10 13:36:04 -07006847
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006848 public MotionEvent getPendingTrackballMove(IWindow window) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006849 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006850 TAG, "IWindow getPendingMotionEvent called for " + window);
6851 return mKeyWaiter.finishedKey(this, window, false,
6852 KeyWaiter.RETURN_PENDING_TRACKBALL);
6853 }
6854
6855 public void setInTouchMode(boolean mode) {
6856 synchronized(mWindowMap) {
6857 mInTouchMode = mode;
6858 }
6859 }
6860
6861 public boolean getInTouchMode() {
6862 synchronized(mWindowMap) {
6863 return mInTouchMode;
6864 }
6865 }
6866
6867 public boolean performHapticFeedback(IWindow window, int effectId,
6868 boolean always) {
6869 synchronized(mWindowMap) {
6870 long ident = Binder.clearCallingIdentity();
6871 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006872 return mPolicy.performHapticFeedbackLw(
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006873 windowForClientLocked(this, window, true),
6874 effectId, always);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006875 } finally {
6876 Binder.restoreCallingIdentity(ident);
6877 }
6878 }
6879 }
Romain Guy06882f82009-06-10 13:36:04 -07006880
Marco Nelissenbf6956b2009-11-09 15:21:13 -08006881 public void setWallpaperPosition(IBinder window, float x, float y, float xStep, float yStep) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006882 synchronized(mWindowMap) {
6883 long ident = Binder.clearCallingIdentity();
6884 try {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006885 setWindowWallpaperPositionLocked(
6886 windowForClientLocked(this, window, true),
Marco Nelissenbf6956b2009-11-09 15:21:13 -08006887 x, y, xStep, yStep);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006888 } finally {
6889 Binder.restoreCallingIdentity(ident);
6890 }
6891 }
6892 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006893
Dianne Hackborn19382ac2009-09-11 21:13:37 -07006894 public void wallpaperOffsetsComplete(IBinder window) {
6895 WindowManagerService.this.wallpaperOffsetsComplete(window);
6896 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006897
Dianne Hackborn75804932009-10-20 20:15:20 -07006898 public Bundle sendWallpaperCommand(IBinder window, String action, int x, int y,
6899 int z, Bundle extras, boolean sync) {
6900 synchronized(mWindowMap) {
6901 long ident = Binder.clearCallingIdentity();
6902 try {
6903 return sendWindowWallpaperCommandLocked(
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006904 windowForClientLocked(this, window, true),
Dianne Hackborn75804932009-10-20 20:15:20 -07006905 action, x, y, z, extras, sync);
6906 } finally {
6907 Binder.restoreCallingIdentity(ident);
6908 }
6909 }
6910 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006911
Dianne Hackborn75804932009-10-20 20:15:20 -07006912 public void wallpaperCommandComplete(IBinder window, Bundle result) {
6913 WindowManagerService.this.wallpaperCommandComplete(window, result);
6914 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006916 void windowAddedLocked() {
6917 if (mSurfaceSession == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006918 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006919 TAG, "First window added to " + this + ", creating SurfaceSession");
6920 mSurfaceSession = new SurfaceSession();
Joe Onorato8a9b2202010-02-26 18:56:32 -08006921 if (SHOW_TRANSACTIONS) Slog.i(
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006922 TAG, " NEW SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006923 mSessions.add(this);
6924 }
6925 mNumWindow++;
6926 }
6927
6928 void windowRemovedLocked() {
6929 mNumWindow--;
6930 killSessionLocked();
6931 }
Romain Guy06882f82009-06-10 13:36:04 -07006932
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006933 void killSessionLocked() {
6934 if (mNumWindow <= 0 && mClientDead) {
6935 mSessions.remove(this);
6936 if (mSurfaceSession != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006937 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006938 TAG, "Last window removed from " + this
6939 + ", destroying " + mSurfaceSession);
Joe Onorato8a9b2202010-02-26 18:56:32 -08006940 if (SHOW_TRANSACTIONS) Slog.i(
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006941 TAG, " KILL SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006942 try {
6943 mSurfaceSession.kill();
6944 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006945 Slog.w(TAG, "Exception thrown when killing surface session "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006946 + mSurfaceSession + " in session " + this
6947 + ": " + e.toString());
6948 }
6949 mSurfaceSession = null;
6950 }
6951 }
6952 }
Romain Guy06882f82009-06-10 13:36:04 -07006953
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006954 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006955 pw.print(prefix); pw.print("mNumWindow="); pw.print(mNumWindow);
6956 pw.print(" mClientDead="); pw.print(mClientDead);
6957 pw.print(" mSurfaceSession="); pw.println(mSurfaceSession);
6958 if (mPendingPointerWindow != null || mPendingPointerMove != null) {
6959 pw.print(prefix);
6960 pw.print("mPendingPointerWindow="); pw.print(mPendingPointerWindow);
6961 pw.print(" mPendingPointerMove="); pw.println(mPendingPointerMove);
6962 }
6963 if (mPendingTrackballWindow != null || mPendingTrackballMove != null) {
6964 pw.print(prefix);
6965 pw.print("mPendingTrackballWindow="); pw.print(mPendingTrackballWindow);
6966 pw.print(" mPendingTrackballMove="); pw.println(mPendingTrackballMove);
6967 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006968 }
6969
6970 @Override
6971 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006972 return mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006973 }
6974 }
6975
6976 // -------------------------------------------------------------
6977 // Client Window State
6978 // -------------------------------------------------------------
6979
6980 private final class WindowState implements WindowManagerPolicy.WindowState {
6981 final Session mSession;
6982 final IWindow mClient;
6983 WindowToken mToken;
The Android Open Source Project10592532009-03-18 17:39:46 -07006984 WindowToken mRootToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006985 AppWindowToken mAppToken;
6986 AppWindowToken mTargetAppToken;
6987 final WindowManager.LayoutParams mAttrs = new WindowManager.LayoutParams();
6988 final DeathRecipient mDeathRecipient;
6989 final WindowState mAttachedWindow;
6990 final ArrayList mChildWindows = new ArrayList();
6991 final int mBaseLayer;
6992 final int mSubLayer;
6993 final boolean mLayoutAttached;
6994 final boolean mIsImWindow;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006995 final boolean mIsWallpaper;
6996 final boolean mIsFloatingLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006997 int mViewVisibility;
6998 boolean mPolicyVisibility = true;
6999 boolean mPolicyVisibilityAfterAnim = true;
7000 boolean mAppFreezing;
7001 Surface mSurface;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007002 boolean mReportDestroySurface;
7003 boolean mSurfacePendingDestroy;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007004 boolean mAttachedHidden; // is our parent window hidden?
7005 boolean mLastHidden; // was this window last hidden?
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007006 boolean mWallpaperVisible; // for wallpaper, what was last vis report?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007007 int mRequestedWidth;
7008 int mRequestedHeight;
7009 int mLastRequestedWidth;
7010 int mLastRequestedHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007011 int mLayer;
7012 int mAnimLayer;
7013 int mLastLayer;
7014 boolean mHaveFrame;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007015 boolean mObscured;
Dianne Hackborn93e462b2009-09-15 22:50:40 -07007016 boolean mTurnOnScreen;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007017
7018 WindowState mNextOutsideTouch;
Romain Guy06882f82009-06-10 13:36:04 -07007019
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007020 int mLayoutSeq = -1;
7021
7022 Configuration mConfiguration = null;
7023
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007024 // Actual frame shown on-screen (may be modified by animation)
7025 final Rect mShownFrame = new Rect();
7026 final Rect mLastShownFrame = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07007027
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007028 /**
Dianne Hackbornac3587d2010-03-11 11:12:11 -08007029 * Set when we have changed the size of the surface, to know that
7030 * we must tell them application to resize (and thus redraw itself).
7031 */
7032 boolean mSurfaceResized;
7033
7034 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007035 * Insets that determine the actually visible area
7036 */
7037 final Rect mVisibleInsets = new Rect();
7038 final Rect mLastVisibleInsets = new Rect();
7039 boolean mVisibleInsetsChanged;
7040
7041 /**
7042 * Insets that are covered by system windows
7043 */
7044 final Rect mContentInsets = new Rect();
7045 final Rect mLastContentInsets = new Rect();
7046 boolean mContentInsetsChanged;
7047
7048 /**
7049 * Set to true if we are waiting for this window to receive its
7050 * given internal insets before laying out other windows based on it.
7051 */
7052 boolean mGivenInsetsPending;
Romain Guy06882f82009-06-10 13:36:04 -07007053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007054 /**
7055 * These are the content insets that were given during layout for
7056 * this window, to be applied to windows behind it.
7057 */
7058 final Rect mGivenContentInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07007059
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007060 /**
7061 * These are the visible insets that were given during layout for
7062 * this window, to be applied to windows behind it.
7063 */
7064 final Rect mGivenVisibleInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07007065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007066 /**
7067 * Flag indicating whether the touchable region should be adjusted by
7068 * the visible insets; if false the area outside the visible insets is
7069 * NOT touchable, so we must use those to adjust the frame during hit
7070 * tests.
7071 */
7072 int mTouchableInsets = ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME;
Romain Guy06882f82009-06-10 13:36:04 -07007073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007074 // Current transformation being applied.
7075 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
7076 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
7077 float mHScale=1, mVScale=1;
7078 float mLastHScale=1, mLastVScale=1;
7079 final Matrix mTmpMatrix = new Matrix();
7080
7081 // "Real" frame that the application sees.
7082 final Rect mFrame = new Rect();
7083 final Rect mLastFrame = new Rect();
7084
7085 final Rect mContainingFrame = new Rect();
7086 final Rect mDisplayFrame = new Rect();
7087 final Rect mContentFrame = new Rect();
7088 final Rect mVisibleFrame = new Rect();
7089
7090 float mShownAlpha = 1;
7091 float mAlpha = 1;
7092 float mLastAlpha = 1;
7093
7094 // Set to true if, when the window gets displayed, it should perform
7095 // an enter animation.
7096 boolean mEnterAnimationPending;
7097
7098 // Currently running animation.
7099 boolean mAnimating;
7100 boolean mLocalAnimating;
7101 Animation mAnimation;
7102 boolean mAnimationIsEntrance;
7103 boolean mHasTransformation;
7104 boolean mHasLocalTransformation;
7105 final Transformation mTransformation = new Transformation();
7106
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07007107 // If a window showing a wallpaper: the requested offset for the
7108 // wallpaper; if a wallpaper window: the currently applied offset.
7109 float mWallpaperX = -1;
7110 float mWallpaperY = -1;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08007111
7112 // If a window showing a wallpaper: what fraction of the offset
7113 // range corresponds to a full virtual screen.
7114 float mWallpaperXStep = -1;
7115 float mWallpaperYStep = -1;
7116
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07007117 // Wallpaper windows: pixels offset based on above variables.
7118 int mXOffset;
7119 int mYOffset;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007121 // This is set after IWindowSession.relayout() has been called at
7122 // least once for the window. It allows us to detect the situation
7123 // where we don't yet have a surface, but should have one soon, so
7124 // we can give the window focus before waiting for the relayout.
7125 boolean mRelayoutCalled;
Romain Guy06882f82009-06-10 13:36:04 -07007126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007127 // This is set after the Surface has been created but before the
7128 // window has been drawn. During this time the surface is hidden.
7129 boolean mDrawPending;
7130
7131 // This is set after the window has finished drawing for the first
7132 // time but before its surface is shown. The surface will be
7133 // displayed when the next layout is run.
7134 boolean mCommitDrawPending;
7135
7136 // This is set during the time after the window's drawing has been
7137 // committed, and before its surface is actually shown. It is used
7138 // to delay showing the surface until all windows in a token are ready
7139 // to be shown.
7140 boolean mReadyToShow;
Romain Guy06882f82009-06-10 13:36:04 -07007141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007142 // Set when the window has been shown in the screen the first time.
7143 boolean mHasDrawn;
7144
7145 // Currently running an exit animation?
7146 boolean mExiting;
7147
7148 // Currently on the mDestroySurface list?
7149 boolean mDestroying;
Romain Guy06882f82009-06-10 13:36:04 -07007150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007151 // Completely remove from window manager after exit animation?
7152 boolean mRemoveOnExit;
7153
7154 // Set when the orientation is changing and this window has not yet
7155 // been updated for the new orientation.
7156 boolean mOrientationChanging;
Romain Guy06882f82009-06-10 13:36:04 -07007157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007158 // Is this window now (or just being) removed?
7159 boolean mRemoved;
Romain Guy06882f82009-06-10 13:36:04 -07007160
Dianne Hackborn16064f92010-03-25 00:47:24 -07007161 // For debugging, this is the last information given to the surface flinger.
7162 boolean mSurfaceShown;
7163 int mSurfaceX, mSurfaceY, mSurfaceW, mSurfaceH;
7164 int mSurfaceLayer;
7165 float mSurfaceAlpha;
7166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007167 WindowState(Session s, IWindow c, WindowToken token,
7168 WindowState attachedWindow, WindowManager.LayoutParams a,
7169 int viewVisibility) {
7170 mSession = s;
7171 mClient = c;
7172 mToken = token;
7173 mAttrs.copyFrom(a);
7174 mViewVisibility = viewVisibility;
7175 DeathRecipient deathRecipient = new DeathRecipient();
7176 mAlpha = a.alpha;
Joe Onorato8a9b2202010-02-26 18:56:32 -08007177 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007178 TAG, "Window " + this + " client=" + c.asBinder()
7179 + " token=" + token + " (" + mAttrs.token + ")");
7180 try {
7181 c.asBinder().linkToDeath(deathRecipient, 0);
7182 } catch (RemoteException e) {
7183 mDeathRecipient = null;
7184 mAttachedWindow = null;
7185 mLayoutAttached = false;
7186 mIsImWindow = false;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007187 mIsWallpaper = false;
7188 mIsFloatingLayer = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007189 mBaseLayer = 0;
7190 mSubLayer = 0;
7191 return;
7192 }
7193 mDeathRecipient = deathRecipient;
Romain Guy06882f82009-06-10 13:36:04 -07007194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007195 if ((mAttrs.type >= FIRST_SUB_WINDOW &&
7196 mAttrs.type <= LAST_SUB_WINDOW)) {
7197 // The multiplier here is to reserve space for multiple
7198 // windows in the same type layer.
7199 mBaseLayer = mPolicy.windowTypeToLayerLw(
7200 attachedWindow.mAttrs.type) * TYPE_LAYER_MULTIPLIER
7201 + TYPE_LAYER_OFFSET;
7202 mSubLayer = mPolicy.subWindowTypeToLayerLw(a.type);
7203 mAttachedWindow = attachedWindow;
7204 mAttachedWindow.mChildWindows.add(this);
7205 mLayoutAttached = mAttrs.type !=
7206 WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
7207 mIsImWindow = attachedWindow.mAttrs.type == TYPE_INPUT_METHOD
7208 || attachedWindow.mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007209 mIsWallpaper = attachedWindow.mAttrs.type == TYPE_WALLPAPER;
7210 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007211 } else {
7212 // The multiplier here is to reserve space for multiple
7213 // windows in the same type layer.
7214 mBaseLayer = mPolicy.windowTypeToLayerLw(a.type)
7215 * TYPE_LAYER_MULTIPLIER
7216 + TYPE_LAYER_OFFSET;
7217 mSubLayer = 0;
7218 mAttachedWindow = null;
7219 mLayoutAttached = false;
7220 mIsImWindow = mAttrs.type == TYPE_INPUT_METHOD
7221 || mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007222 mIsWallpaper = mAttrs.type == TYPE_WALLPAPER;
7223 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007224 }
7225
7226 WindowState appWin = this;
7227 while (appWin.mAttachedWindow != null) {
7228 appWin = mAttachedWindow;
7229 }
7230 WindowToken appToken = appWin.mToken;
7231 while (appToken.appWindowToken == null) {
7232 WindowToken parent = mTokenMap.get(appToken.token);
7233 if (parent == null || appToken == parent) {
7234 break;
7235 }
7236 appToken = parent;
7237 }
The Android Open Source Project10592532009-03-18 17:39:46 -07007238 mRootToken = appToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007239 mAppToken = appToken.appWindowToken;
7240
7241 mSurface = null;
7242 mRequestedWidth = 0;
7243 mRequestedHeight = 0;
7244 mLastRequestedWidth = 0;
7245 mLastRequestedHeight = 0;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007246 mXOffset = 0;
7247 mYOffset = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007248 mLayer = 0;
7249 mAnimLayer = 0;
7250 mLastLayer = 0;
7251 }
7252
7253 void attach() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007254 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007255 TAG, "Attaching " + this + " token=" + mToken
7256 + ", list=" + mToken.windows);
7257 mSession.windowAddedLocked();
7258 }
7259
7260 public void computeFrameLw(Rect pf, Rect df, Rect cf, Rect vf) {
7261 mHaveFrame = true;
7262
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007263 final Rect container = mContainingFrame;
7264 container.set(pf);
7265
7266 final Rect display = mDisplayFrame;
7267 display.set(df);
7268
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007269 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007270 container.intersect(mCompatibleScreenFrame);
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007271 if ((mAttrs.flags & FLAG_LAYOUT_NO_LIMITS) == 0) {
7272 display.intersect(mCompatibleScreenFrame);
7273 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007274 }
7275
7276 final int pw = container.right - container.left;
7277 final int ph = container.bottom - container.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007278
7279 int w,h;
7280 if ((mAttrs.flags & mAttrs.FLAG_SCALED) != 0) {
7281 w = mAttrs.width < 0 ? pw : mAttrs.width;
7282 h = mAttrs.height< 0 ? ph : mAttrs.height;
7283 } else {
Romain Guy980a9382010-01-08 15:06:28 -08007284 w = mAttrs.width == mAttrs.MATCH_PARENT ? pw : mRequestedWidth;
7285 h = mAttrs.height== mAttrs.MATCH_PARENT ? ph : mRequestedHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007286 }
Romain Guy06882f82009-06-10 13:36:04 -07007287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007288 final Rect content = mContentFrame;
7289 content.set(cf);
Romain Guy06882f82009-06-10 13:36:04 -07007290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007291 final Rect visible = mVisibleFrame;
7292 visible.set(vf);
Romain Guy06882f82009-06-10 13:36:04 -07007293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007294 final Rect frame = mFrame;
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007295 final int fw = frame.width();
7296 final int fh = frame.height();
Romain Guy06882f82009-06-10 13:36:04 -07007297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007298 //System.out.println("In: w=" + w + " h=" + h + " container=" +
7299 // container + " x=" + mAttrs.x + " y=" + mAttrs.y);
7300
7301 Gravity.apply(mAttrs.gravity, w, h, container,
7302 (int) (mAttrs.x + mAttrs.horizontalMargin * pw),
7303 (int) (mAttrs.y + mAttrs.verticalMargin * ph), frame);
7304
7305 //System.out.println("Out: " + mFrame);
7306
7307 // Now make sure the window fits in the overall display.
7308 Gravity.applyDisplay(mAttrs.gravity, df, frame);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007310 // Make sure the content and visible frames are inside of the
7311 // final window frame.
7312 if (content.left < frame.left) content.left = frame.left;
7313 if (content.top < frame.top) content.top = frame.top;
7314 if (content.right > frame.right) content.right = frame.right;
7315 if (content.bottom > frame.bottom) content.bottom = frame.bottom;
7316 if (visible.left < frame.left) visible.left = frame.left;
7317 if (visible.top < frame.top) visible.top = frame.top;
7318 if (visible.right > frame.right) visible.right = frame.right;
7319 if (visible.bottom > frame.bottom) visible.bottom = frame.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007321 final Rect contentInsets = mContentInsets;
7322 contentInsets.left = content.left-frame.left;
7323 contentInsets.top = content.top-frame.top;
7324 contentInsets.right = frame.right-content.right;
7325 contentInsets.bottom = frame.bottom-content.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007327 final Rect visibleInsets = mVisibleInsets;
7328 visibleInsets.left = visible.left-frame.left;
7329 visibleInsets.top = visible.top-frame.top;
7330 visibleInsets.right = frame.right-visible.right;
7331 visibleInsets.bottom = frame.bottom-visible.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007332
Dianne Hackborn284ac932009-08-28 10:34:25 -07007333 if (mIsWallpaper && (fw != frame.width() || fh != frame.height())) {
7334 updateWallpaperOffsetLocked(this, mDisplay.getWidth(),
Dianne Hackborn19382ac2009-09-11 21:13:37 -07007335 mDisplay.getHeight(), false);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007336 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007338 if (localLOGV) {
7339 //if ("com.google.android.youtube".equals(mAttrs.packageName)
7340 // && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007341 Slog.v(TAG, "Resolving (mRequestedWidth="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007342 + mRequestedWidth + ", mRequestedheight="
7343 + mRequestedHeight + ") to" + " (pw=" + pw + ", ph=" + ph
7344 + "): frame=" + mFrame.toShortString()
7345 + " ci=" + contentInsets.toShortString()
7346 + " vi=" + visibleInsets.toShortString());
7347 //}
7348 }
7349 }
Romain Guy06882f82009-06-10 13:36:04 -07007350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007351 public Rect getFrameLw() {
7352 return mFrame;
7353 }
7354
7355 public Rect getShownFrameLw() {
7356 return mShownFrame;
7357 }
7358
7359 public Rect getDisplayFrameLw() {
7360 return mDisplayFrame;
7361 }
7362
7363 public Rect getContentFrameLw() {
7364 return mContentFrame;
7365 }
7366
7367 public Rect getVisibleFrameLw() {
7368 return mVisibleFrame;
7369 }
7370
7371 public boolean getGivenInsetsPendingLw() {
7372 return mGivenInsetsPending;
7373 }
7374
7375 public Rect getGivenContentInsetsLw() {
7376 return mGivenContentInsets;
7377 }
Romain Guy06882f82009-06-10 13:36:04 -07007378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007379 public Rect getGivenVisibleInsetsLw() {
7380 return mGivenVisibleInsets;
7381 }
Romain Guy06882f82009-06-10 13:36:04 -07007382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007383 public WindowManager.LayoutParams getAttrs() {
7384 return mAttrs;
7385 }
7386
7387 public int getSurfaceLayer() {
7388 return mLayer;
7389 }
Romain Guy06882f82009-06-10 13:36:04 -07007390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007391 public IApplicationToken getAppToken() {
7392 return mAppToken != null ? mAppToken.appToken : null;
7393 }
7394
7395 public boolean hasAppShownWindows() {
7396 return mAppToken != null ? mAppToken.firstWindowDrawn : false;
7397 }
7398
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007399 public void setAnimation(Animation anim) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007400 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007401 TAG, "Setting animation in " + this + ": " + anim);
7402 mAnimating = false;
7403 mLocalAnimating = false;
7404 mAnimation = anim;
7405 mAnimation.restrictDuration(MAX_ANIMATION_DURATION);
7406 mAnimation.scaleCurrentDuration(mWindowAnimationScale);
7407 }
7408
7409 public void clearAnimation() {
7410 if (mAnimation != null) {
7411 mAnimating = true;
7412 mLocalAnimating = false;
7413 mAnimation = null;
7414 }
7415 }
Romain Guy06882f82009-06-10 13:36:04 -07007416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007417 Surface createSurfaceLocked() {
7418 if (mSurface == null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007419 mReportDestroySurface = false;
7420 mSurfacePendingDestroy = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007421 mDrawPending = true;
7422 mCommitDrawPending = false;
7423 mReadyToShow = false;
7424 if (mAppToken != null) {
7425 mAppToken.allDrawn = false;
7426 }
7427
7428 int flags = 0;
Mathias Agopian317a6282009-08-13 17:29:02 -07007429 if (mAttrs.memoryType == MEMORY_TYPE_PUSH_BUFFERS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007430 flags |= Surface.PUSH_BUFFERS;
7431 }
7432
7433 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
7434 flags |= Surface.SECURE;
7435 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08007436 if (DEBUG_VISIBILITY) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007437 TAG, "Creating surface in session "
7438 + mSession.mSurfaceSession + " window " + this
7439 + " w=" + mFrame.width()
7440 + " h=" + mFrame.height() + " format="
7441 + mAttrs.format + " flags=" + flags);
7442
7443 int w = mFrame.width();
7444 int h = mFrame.height();
7445 if ((mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
7446 // for a scaled surface, we always want the requested
7447 // size.
7448 w = mRequestedWidth;
7449 h = mRequestedHeight;
7450 }
7451
Romain Guy9825ec62009-10-01 00:58:09 -07007452 // Something is wrong and SurfaceFlinger will not like this,
7453 // try to revert to sane values
7454 if (w <= 0) w = 1;
7455 if (h <= 0) h = 1;
7456
Dianne Hackborn16064f92010-03-25 00:47:24 -07007457 mSurfaceShown = false;
7458 mSurfaceLayer = 0;
7459 mSurfaceAlpha = 1;
7460 mSurfaceX = 0;
7461 mSurfaceY = 0;
7462 mSurfaceW = w;
7463 mSurfaceH = h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007464 try {
7465 mSurface = new Surface(
Romain Guy06882f82009-06-10 13:36:04 -07007466 mSession.mSurfaceSession, mSession.mPid,
Mathias Agopian5d26c1e2010-03-01 16:09:43 -08007467 mAttrs.getTitle().toString(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007468 0, w, h, mAttrs.format, flags);
Joe Onorato8a9b2202010-02-26 18:56:32 -08007469 if (SHOW_TRANSACTIONS) Slog.i(TAG, " CREATE SURFACE "
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007470 + mSurface + " IN SESSION "
7471 + mSession.mSurfaceSession
7472 + ": pid=" + mSession.mPid + " format="
7473 + mAttrs.format + " flags=0x"
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007474 + Integer.toHexString(flags)
7475 + " / " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007476 } catch (Surface.OutOfResourcesException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007477 Slog.w(TAG, "OutOfResourcesException creating surface");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007478 reclaimSomeSurfaceMemoryLocked(this, "create");
7479 return null;
7480 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007481 Slog.e(TAG, "Exception creating surface", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007482 return null;
7483 }
Romain Guy06882f82009-06-10 13:36:04 -07007484
Joe Onorato8a9b2202010-02-26 18:56:32 -08007485 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007486 TAG, "Got surface: " + mSurface
7487 + ", set left=" + mFrame.left + " top=" + mFrame.top
7488 + ", animLayer=" + mAnimLayer);
7489 if (SHOW_TRANSACTIONS) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007490 Slog.i(TAG, ">>> OPEN TRANSACTION");
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007491 if (SHOW_TRANSACTIONS) logSurface(this,
7492 "CREATE pos=(" + mFrame.left + "," + mFrame.top + ") (" +
7493 mFrame.width() + "x" + mFrame.height() + "), layer=" +
7494 mAnimLayer + " HIDE", null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007495 }
7496 Surface.openTransaction();
7497 try {
7498 try {
Dianne Hackborn16064f92010-03-25 00:47:24 -07007499 mSurfaceX = mFrame.left + mXOffset;
Dianne Hackborn529bef62010-03-25 11:48:43 -07007500 mSurfaceY = mFrame.top + mYOffset;
Dianne Hackborn16064f92010-03-25 00:47:24 -07007501 mSurface.setPosition(mSurfaceX, mSurfaceY);
7502 mSurfaceLayer = mAnimLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007503 mSurface.setLayer(mAnimLayer);
Dianne Hackborn16064f92010-03-25 00:47:24 -07007504 mSurfaceShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007505 mSurface.hide();
7506 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007507 if (SHOW_TRANSACTIONS) logSurface(this, "DITHER", null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007508 mSurface.setFlags(Surface.SURFACE_DITHER,
7509 Surface.SURFACE_DITHER);
7510 }
7511 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007512 Slog.w(TAG, "Error creating surface in " + w, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007513 reclaimSomeSurfaceMemoryLocked(this, "create-init");
7514 }
7515 mLastHidden = true;
7516 } finally {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007517 if (SHOW_TRANSACTIONS) Slog.i(TAG, "<<< CLOSE TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007518 Surface.closeTransaction();
7519 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08007520 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007521 TAG, "Created surface " + this);
7522 }
7523 return mSurface;
7524 }
Romain Guy06882f82009-06-10 13:36:04 -07007525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007526 void destroySurfaceLocked() {
7527 // Window is no longer on-screen, so can no longer receive
7528 // key events... if we were waiting for it to finish
7529 // handling a key event, the wait is over!
7530 mKeyWaiter.finishedKey(mSession, mClient, true,
7531 KeyWaiter.RETURN_NOTHING);
7532 mKeyWaiter.releasePendingPointerLocked(mSession);
7533 mKeyWaiter.releasePendingTrackballLocked(mSession);
7534
7535 if (mAppToken != null && this == mAppToken.startingWindow) {
7536 mAppToken.startingDisplayed = false;
7537 }
Romain Guy06882f82009-06-10 13:36:04 -07007538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007539 if (mSurface != null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007540 mDrawPending = false;
7541 mCommitDrawPending = false;
7542 mReadyToShow = false;
7543
7544 int i = mChildWindows.size();
7545 while (i > 0) {
7546 i--;
7547 WindowState c = (WindowState)mChildWindows.get(i);
7548 c.mAttachedHidden = true;
7549 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007550
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007551 if (mReportDestroySurface) {
7552 mReportDestroySurface = false;
7553 mSurfacePendingDestroy = true;
7554 try {
7555 mClient.dispatchGetNewSurface();
7556 // We'll really destroy on the next time around.
7557 return;
7558 } catch (RemoteException e) {
7559 }
7560 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007561
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007562 try {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007563 if (DEBUG_VISIBILITY) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007564 RuntimeException e = null;
7565 if (!HIDE_STACK_CRAWLS) {
7566 e = new RuntimeException();
7567 e.fillInStackTrace();
7568 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08007569 Slog.w(TAG, "Window " + this + " destroying surface "
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007570 + mSurface + ", session " + mSession, e);
7571 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007572 if (SHOW_TRANSACTIONS) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007573 RuntimeException e = null;
7574 if (!HIDE_STACK_CRAWLS) {
7575 e = new RuntimeException();
7576 e.fillInStackTrace();
7577 }
7578 if (SHOW_TRANSACTIONS) logSurface(this, "DESTROY", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007579 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007580 mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007581 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007582 Slog.w(TAG, "Exception thrown when destroying Window " + this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007583 + " surface " + mSurface + " session " + mSession
7584 + ": " + e.toString());
7585 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007586
Dianne Hackborn16064f92010-03-25 00:47:24 -07007587 mSurfaceShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007588 mSurface = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007589 }
7590 }
7591
7592 boolean finishDrawingLocked() {
7593 if (mDrawPending) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007594 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007595 TAG, "finishDrawingLocked: " + mSurface);
7596 mCommitDrawPending = true;
7597 mDrawPending = false;
7598 return true;
7599 }
7600 return false;
7601 }
7602
7603 // This must be called while inside a transaction.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007604 boolean commitFinishDrawingLocked(long currentTime) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007605 //Slog.i(TAG, "commitFinishDrawingLocked: " + mSurface);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007606 if (!mCommitDrawPending) {
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007607 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007608 }
7609 mCommitDrawPending = false;
7610 mReadyToShow = true;
7611 final boolean starting = mAttrs.type == TYPE_APPLICATION_STARTING;
7612 final AppWindowToken atoken = mAppToken;
7613 if (atoken == null || atoken.allDrawn || starting) {
7614 performShowLocked();
7615 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007616 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007617 }
7618
7619 // This must be called while inside a transaction.
7620 boolean performShowLocked() {
7621 if (DEBUG_VISIBILITY) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007622 RuntimeException e = null;
7623 if (!HIDE_STACK_CRAWLS) {
7624 e = new RuntimeException();
7625 e.fillInStackTrace();
7626 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08007627 Slog.v(TAG, "performShow on " + this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007628 + ": readyToShow=" + mReadyToShow + " readyForDisplay=" + isReadyForDisplay()
7629 + " starting=" + (mAttrs.type == TYPE_APPLICATION_STARTING), e);
7630 }
7631 if (mReadyToShow && isReadyForDisplay()) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007632 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) logSurface(this,
7633 "SHOW (performShowLocked)", null);
Joe Onorato8a9b2202010-02-26 18:56:32 -08007634 if (DEBUG_VISIBILITY) Slog.v(TAG, "Showing " + this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007635 + " during animation: policyVis=" + mPolicyVisibility
7636 + " attHidden=" + mAttachedHidden
7637 + " tok.hiddenRequested="
7638 + (mAppToken != null ? mAppToken.hiddenRequested : false)
Dianne Hackborn248b1882009-09-16 16:46:44 -07007639 + " tok.hidden="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007640 + (mAppToken != null ? mAppToken.hidden : false)
7641 + " animating=" + mAnimating
7642 + " tok animating="
7643 + (mAppToken != null ? mAppToken.animating : false));
7644 if (!showSurfaceRobustlyLocked(this)) {
7645 return false;
7646 }
7647 mLastAlpha = -1;
7648 mHasDrawn = true;
7649 mLastHidden = false;
7650 mReadyToShow = false;
7651 enableScreenIfNeededLocked();
7652
7653 applyEnterAnimationLocked(this);
Romain Guy06882f82009-06-10 13:36:04 -07007654
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007655 int i = mChildWindows.size();
7656 while (i > 0) {
7657 i--;
7658 WindowState c = (WindowState)mChildWindows.get(i);
Dianne Hackbornf09c1a22010-04-22 15:59:21 -07007659 if (c.mAttachedHidden) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007660 c.mAttachedHidden = false;
Dianne Hackbornf09c1a22010-04-22 15:59:21 -07007661 if (c.mSurface != null) {
7662 c.performShowLocked();
7663 // It hadn't been shown, which means layout not
7664 // performed on it, so now we want to make sure to
7665 // do a layout. If called from within the transaction
7666 // loop, this will cause it to restart with a new
7667 // layout.
7668 mLayoutNeeded = true;
7669 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007670 }
7671 }
Romain Guy06882f82009-06-10 13:36:04 -07007672
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007673 if (mAttrs.type != TYPE_APPLICATION_STARTING
7674 && mAppToken != null) {
7675 mAppToken.firstWindowDrawn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007676
Dianne Hackborn248b1882009-09-16 16:46:44 -07007677 if (mAppToken.startingData != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007678 if (DEBUG_STARTING_WINDOW || DEBUG_ANIM) Slog.v(TAG,
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007679 "Finish starting " + mToken
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007680 + ": first real window is shown, no animation");
Dianne Hackborn248b1882009-09-16 16:46:44 -07007681 // If this initial window is animating, stop it -- we
7682 // will do an animation to reveal it from behind the
7683 // starting window, so there is no need for it to also
7684 // be doing its own stuff.
7685 if (mAnimation != null) {
7686 mAnimation = null;
7687 // Make sure we clean up the animation.
7688 mAnimating = true;
7689 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007690 mFinishedStarting.add(mAppToken);
7691 mH.sendEmptyMessage(H.FINISHED_STARTING);
7692 }
7693 mAppToken.updateReportedVisibilityLocked();
7694 }
7695 }
7696 return true;
7697 }
Romain Guy06882f82009-06-10 13:36:04 -07007698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007699 // This must be called while inside a transaction. Returns true if
7700 // there is more animation to run.
7701 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
Dianne Hackbornde2606d2009-12-18 16:53:55 -08007702 if (!mDisplayFrozen && mPolicy.isScreenOn()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007703 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07007704
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007705 if (!mDrawPending && !mCommitDrawPending && mAnimation != null) {
7706 mHasTransformation = true;
7707 mHasLocalTransformation = true;
7708 if (!mLocalAnimating) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007709 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007710 TAG, "Starting animation in " + this +
7711 " @ " + currentTime + ": ww=" + mFrame.width() + " wh=" + mFrame.height() +
7712 " dw=" + dw + " dh=" + dh + " scale=" + mWindowAnimationScale);
7713 mAnimation.initialize(mFrame.width(), mFrame.height(), dw, dh);
7714 mAnimation.setStartTime(currentTime);
7715 mLocalAnimating = true;
7716 mAnimating = true;
7717 }
7718 mTransformation.clear();
7719 final boolean more = mAnimation.getTransformation(
7720 currentTime, mTransformation);
Joe Onorato8a9b2202010-02-26 18:56:32 -08007721 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007722 TAG, "Stepped animation in " + this +
7723 ": more=" + more + ", xform=" + mTransformation);
7724 if (more) {
7725 // we're not done!
7726 return true;
7727 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08007728 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007729 TAG, "Finished animation in " + this +
7730 " @ " + currentTime);
7731 mAnimation = null;
7732 //WindowManagerService.this.dump();
7733 }
7734 mHasLocalTransformation = false;
7735 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppToken != null
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007736 && mAppToken.animation != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007737 // When our app token is animating, we kind-of pretend like
7738 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
7739 // part of this check means that we will only do this if
7740 // our window is not currently exiting, or it is not
7741 // locally animating itself. The idea being that one that
7742 // is exiting and doing a local animation should be removed
7743 // once that animation is done.
7744 mAnimating = true;
7745 mHasTransformation = true;
7746 mTransformation.clear();
7747 return false;
7748 } else if (mHasTransformation) {
7749 // Little trick to get through the path below to act like
7750 // we have finished an animation.
7751 mAnimating = true;
7752 } else if (isAnimating()) {
7753 mAnimating = true;
7754 }
7755 } else if (mAnimation != null) {
7756 // If the display is frozen, and there is a pending animation,
7757 // clear it and make sure we run the cleanup code.
7758 mAnimating = true;
7759 mLocalAnimating = true;
7760 mAnimation = null;
7761 }
Romain Guy06882f82009-06-10 13:36:04 -07007762
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007763 if (!mAnimating && !mLocalAnimating) {
7764 return false;
7765 }
7766
Joe Onorato8a9b2202010-02-26 18:56:32 -08007767 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007768 TAG, "Animation done in " + this + ": exiting=" + mExiting
7769 + ", reportedVisible="
7770 + (mAppToken != null ? mAppToken.reportedVisible : false));
Romain Guy06882f82009-06-10 13:36:04 -07007771
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007772 mAnimating = false;
7773 mLocalAnimating = false;
7774 mAnimation = null;
7775 mAnimLayer = mLayer;
7776 if (mIsImWindow) {
7777 mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007778 } else if (mIsWallpaper) {
7779 mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007780 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08007781 if (DEBUG_LAYERS) Slog.v(TAG, "Stepping win " + this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007782 + " anim layer: " + mAnimLayer);
7783 mHasTransformation = false;
7784 mHasLocalTransformation = false;
Dianne Hackbornb601ce12010-03-01 23:36:02 -08007785 if (mPolicyVisibility != mPolicyVisibilityAfterAnim) {
7786 if (DEBUG_VISIBILITY) {
7787 Slog.v(TAG, "Policy visibility changing after anim in " + this + ": "
7788 + mPolicyVisibilityAfterAnim);
7789 }
7790 mPolicyVisibility = mPolicyVisibilityAfterAnim;
7791 if (!mPolicyVisibility) {
7792 if (mCurrentFocus == this) {
7793 mFocusMayChange = true;
7794 }
7795 // Window is no longer visible -- make sure if we were waiting
7796 // for it to be displayed before enabling the display, that
7797 // we allow the display to be enabled now.
7798 enableScreenIfNeededLocked();
7799 }
Dianne Hackbornf3bea9c2009-12-09 18:26:21 -08007800 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007801 mTransformation.clear();
7802 if (mHasDrawn
7803 && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
7804 && mAppToken != null
7805 && mAppToken.firstWindowDrawn
7806 && mAppToken.startingData != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007807 if (DEBUG_STARTING_WINDOW) Slog.v(TAG, "Finish starting "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007808 + mToken + ": first real window done animating");
7809 mFinishedStarting.add(mAppToken);
7810 mH.sendEmptyMessage(H.FINISHED_STARTING);
7811 }
Romain Guy06882f82009-06-10 13:36:04 -07007812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007813 finishExit();
7814
7815 if (mAppToken != null) {
7816 mAppToken.updateReportedVisibilityLocked();
7817 }
7818
7819 return false;
7820 }
7821
7822 void finishExit() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007823 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007824 TAG, "finishExit in " + this
7825 + ": exiting=" + mExiting
7826 + " remove=" + mRemoveOnExit
7827 + " windowAnimating=" + isWindowAnimating());
Romain Guy06882f82009-06-10 13:36:04 -07007828
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007829 final int N = mChildWindows.size();
7830 for (int i=0; i<N; i++) {
7831 ((WindowState)mChildWindows.get(i)).finishExit();
7832 }
Romain Guy06882f82009-06-10 13:36:04 -07007833
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007834 if (!mExiting) {
7835 return;
7836 }
Romain Guy06882f82009-06-10 13:36:04 -07007837
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007838 if (isWindowAnimating()) {
7839 return;
7840 }
7841
Joe Onorato8a9b2202010-02-26 18:56:32 -08007842 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007843 TAG, "Exit animation finished in " + this
7844 + ": remove=" + mRemoveOnExit);
7845 if (mSurface != null) {
7846 mDestroySurface.add(this);
7847 mDestroying = true;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007848 if (SHOW_TRANSACTIONS) logSurface(this, "HIDE (finishExit)", null);
Dianne Hackborn16064f92010-03-25 00:47:24 -07007849 mSurfaceShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007850 try {
7851 mSurface.hide();
7852 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007853 Slog.w(TAG, "Error hiding surface in " + this, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007854 }
7855 mLastHidden = true;
7856 mKeyWaiter.releasePendingPointerLocked(mSession);
7857 }
7858 mExiting = false;
7859 if (mRemoveOnExit) {
7860 mPendingRemove.add(this);
7861 mRemoveOnExit = false;
7862 }
7863 }
Romain Guy06882f82009-06-10 13:36:04 -07007864
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007865 boolean isIdentityMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
7866 if (dsdx < .99999f || dsdx > 1.00001f) return false;
7867 if (dtdy < .99999f || dtdy > 1.00001f) return false;
7868 if (dtdx < -.000001f || dtdx > .000001f) return false;
7869 if (dsdy < -.000001f || dsdy > .000001f) return false;
7870 return true;
7871 }
Romain Guy06882f82009-06-10 13:36:04 -07007872
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007873 void computeShownFrameLocked() {
7874 final boolean selfTransformation = mHasLocalTransformation;
7875 Transformation attachedTransformation =
7876 (mAttachedWindow != null && mAttachedWindow.mHasLocalTransformation)
7877 ? mAttachedWindow.mTransformation : null;
7878 Transformation appTransformation =
7879 (mAppToken != null && mAppToken.hasTransformation)
7880 ? mAppToken.transformation : null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007881
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007882 // Wallpapers are animated based on the "real" window they
7883 // are currently targeting.
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007884 if (mAttrs.type == TYPE_WALLPAPER && mLowerWallpaperTarget == null
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07007885 && mWallpaperTarget != null) {
Dianne Hackborn5baba162009-09-23 17:01:12 -07007886 if (mWallpaperTarget.mHasLocalTransformation &&
7887 mWallpaperTarget.mAnimation != null &&
7888 !mWallpaperTarget.mAnimation.getDetachWallpaper()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007889 attachedTransformation = mWallpaperTarget.mTransformation;
Dianne Hackborn5baba162009-09-23 17:01:12 -07007890 if (DEBUG_WALLPAPER && attachedTransformation != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007891 Slog.v(TAG, "WP target attached xform: " + attachedTransformation);
Dianne Hackborn5baba162009-09-23 17:01:12 -07007892 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007893 }
7894 if (mWallpaperTarget.mAppToken != null &&
Dianne Hackborn5baba162009-09-23 17:01:12 -07007895 mWallpaperTarget.mAppToken.hasTransformation &&
7896 mWallpaperTarget.mAppToken.animation != null &&
7897 !mWallpaperTarget.mAppToken.animation.getDetachWallpaper()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007898 appTransformation = mWallpaperTarget.mAppToken.transformation;
Dianne Hackborn5baba162009-09-23 17:01:12 -07007899 if (DEBUG_WALLPAPER && appTransformation != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007900 Slog.v(TAG, "WP target app xform: " + appTransformation);
Dianne Hackborn5baba162009-09-23 17:01:12 -07007901 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007902 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007903 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007904
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007905 if (selfTransformation || attachedTransformation != null
7906 || appTransformation != null) {
Romain Guy06882f82009-06-10 13:36:04 -07007907 // cache often used attributes locally
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007908 final Rect frame = mFrame;
7909 final float tmpFloats[] = mTmpFloats;
7910 final Matrix tmpMatrix = mTmpMatrix;
7911
7912 // Compute the desired transformation.
Dianne Hackborn65c23872009-09-18 17:47:02 -07007913 tmpMatrix.setTranslate(0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007914 if (selfTransformation) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007915 tmpMatrix.postConcat(mTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007916 }
Dianne Hackborn65c23872009-09-18 17:47:02 -07007917 tmpMatrix.postTranslate(frame.left, frame.top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007918 if (attachedTransformation != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007919 tmpMatrix.postConcat(attachedTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007920 }
7921 if (appTransformation != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007922 tmpMatrix.postConcat(appTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007923 }
7924
7925 // "convert" it into SurfaceFlinger's format
7926 // (a 2x2 matrix + an offset)
7927 // Here we must not transform the position of the surface
7928 // since it is already included in the transformation.
Joe Onorato8a9b2202010-02-26 18:56:32 -08007929 //Slog.i(TAG, "Transform: " + matrix);
Romain Guy06882f82009-06-10 13:36:04 -07007930
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007931 tmpMatrix.getValues(tmpFloats);
7932 mDsDx = tmpFloats[Matrix.MSCALE_X];
7933 mDtDx = tmpFloats[Matrix.MSKEW_X];
7934 mDsDy = tmpFloats[Matrix.MSKEW_Y];
7935 mDtDy = tmpFloats[Matrix.MSCALE_Y];
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007936 int x = (int)tmpFloats[Matrix.MTRANS_X] + mXOffset;
7937 int y = (int)tmpFloats[Matrix.MTRANS_Y] + mYOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007938 int w = frame.width();
7939 int h = frame.height();
7940 mShownFrame.set(x, y, x+w, y+h);
7941
7942 // Now set the alpha... but because our current hardware
7943 // can't do alpha transformation on a non-opaque surface,
7944 // turn it off if we are running an animation that is also
7945 // transforming since it is more important to have that
7946 // animation be smooth.
7947 mShownAlpha = mAlpha;
7948 if (!mLimitedAlphaCompositing
7949 || (!PixelFormat.formatHasAlpha(mAttrs.format)
7950 || (isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
7951 && x == frame.left && y == frame.top))) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007952 //Slog.i(TAG, "Applying alpha transform");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007953 if (selfTransformation) {
7954 mShownAlpha *= mTransformation.getAlpha();
7955 }
7956 if (attachedTransformation != null) {
7957 mShownAlpha *= attachedTransformation.getAlpha();
7958 }
7959 if (appTransformation != null) {
7960 mShownAlpha *= appTransformation.getAlpha();
7961 }
7962 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007963 //Slog.i(TAG, "Not applying alpha transform");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007964 }
Romain Guy06882f82009-06-10 13:36:04 -07007965
Joe Onorato8a9b2202010-02-26 18:56:32 -08007966 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007967 TAG, "Continuing animation in " + this +
7968 ": " + mShownFrame +
7969 ", alpha=" + mTransformation.getAlpha());
7970 return;
7971 }
Romain Guy06882f82009-06-10 13:36:04 -07007972
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007973 mShownFrame.set(mFrame);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007974 if (mXOffset != 0 || mYOffset != 0) {
7975 mShownFrame.offset(mXOffset, mYOffset);
7976 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007977 mShownAlpha = mAlpha;
7978 mDsDx = 1;
7979 mDtDx = 0;
7980 mDsDy = 0;
7981 mDtDy = 1;
7982 }
Romain Guy06882f82009-06-10 13:36:04 -07007983
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007984 /**
7985 * Is this window visible? It is not visible if there is no
7986 * surface, or we are in the process of running an exit animation
7987 * that will remove the surface, or its app token has been hidden.
7988 */
7989 public boolean isVisibleLw() {
7990 final AppWindowToken atoken = mAppToken;
7991 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7992 && (atoken == null || !atoken.hiddenRequested)
7993 && !mExiting && !mDestroying;
7994 }
7995
7996 /**
Dianne Hackborn3d163f072009-10-07 21:26:57 -07007997 * Like {@link #isVisibleLw}, but also counts a window that is currently
7998 * "hidden" behind the keyguard as visible. This allows us to apply
7999 * things like window flags that impact the keyguard.
8000 * XXX I am starting to think we need to have ANOTHER visibility flag
8001 * for this "hidden behind keyguard" state rather than overloading
8002 * mPolicyVisibility. Ungh.
8003 */
8004 public boolean isVisibleOrBehindKeyguardLw() {
8005 final AppWindowToken atoken = mAppToken;
8006 return mSurface != null && !mAttachedHidden
8007 && (atoken == null ? mPolicyVisibility : !atoken.hiddenRequested)
Dianne Hackborn5943c202010-04-12 21:36:49 -07008008 && (mOrientationChanging || (!mDrawPending && !mCommitDrawPending))
Dianne Hackborn3d163f072009-10-07 21:26:57 -07008009 && !mExiting && !mDestroying;
8010 }
8011
8012 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008013 * Is this window visible, ignoring its app token? It is not visible
8014 * if there is no surface, or we are in the process of running an exit animation
8015 * that will remove the surface.
8016 */
8017 public boolean isWinVisibleLw() {
8018 final AppWindowToken atoken = mAppToken;
8019 return mSurface != null && mPolicyVisibility && !mAttachedHidden
8020 && (atoken == null || !atoken.hiddenRequested || atoken.animating)
8021 && !mExiting && !mDestroying;
8022 }
8023
8024 /**
8025 * The same as isVisible(), but follows the current hidden state of
8026 * the associated app token, not the pending requested hidden state.
8027 */
8028 boolean isVisibleNow() {
8029 return mSurface != null && mPolicyVisibility && !mAttachedHidden
The Android Open Source Project10592532009-03-18 17:39:46 -07008030 && !mRootToken.hidden && !mExiting && !mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008031 }
8032
8033 /**
8034 * Same as isVisible(), but we also count it as visible between the
8035 * call to IWindowSession.add() and the first relayout().
8036 */
8037 boolean isVisibleOrAdding() {
8038 final AppWindowToken atoken = mAppToken;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008039 return ((mSurface != null && !mReportDestroySurface)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008040 || (!mRelayoutCalled && mViewVisibility == View.VISIBLE))
8041 && mPolicyVisibility && !mAttachedHidden
8042 && (atoken == null || !atoken.hiddenRequested)
8043 && !mExiting && !mDestroying;
8044 }
8045
8046 /**
8047 * Is this window currently on-screen? It is on-screen either if it
8048 * is visible or it is currently running an animation before no longer
8049 * being visible.
8050 */
8051 boolean isOnScreen() {
8052 final AppWindowToken atoken = mAppToken;
8053 if (atoken != null) {
8054 return mSurface != null && mPolicyVisibility && !mDestroying
8055 && ((!mAttachedHidden && !atoken.hiddenRequested)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07008056 || mAnimation != null || atoken.animation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008057 } else {
8058 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn0cd48872009-08-13 18:51:59 -07008059 && (!mAttachedHidden || mAnimation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008060 }
8061 }
Romain Guy06882f82009-06-10 13:36:04 -07008062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008063 /**
8064 * Like isOnScreen(), but we don't return true if the window is part
8065 * of a transition that has not yet been started.
8066 */
8067 boolean isReadyForDisplay() {
Dianne Hackborna8f60182009-09-01 19:01:50 -07008068 if (mRootToken.waitingToShow &&
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07008069 mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07008070 return false;
8071 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008072 final AppWindowToken atoken = mAppToken;
Dianne Hackborn0cd48872009-08-13 18:51:59 -07008073 final boolean animating = atoken != null
8074 ? (atoken.animation != null) : false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008075 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07008076 && ((!mAttachedHidden && mViewVisibility == View.VISIBLE
8077 && !mRootToken.hidden)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07008078 || mAnimation != null || animating);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008079 }
8080
8081 /** Is the window or its container currently animating? */
8082 boolean isAnimating() {
8083 final WindowState attached = mAttachedWindow;
8084 final AppWindowToken atoken = mAppToken;
8085 return mAnimation != null
8086 || (attached != null && attached.mAnimation != null)
Romain Guy06882f82009-06-10 13:36:04 -07008087 || (atoken != null &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008088 (atoken.animation != null
8089 || atoken.inPendingTransaction));
8090 }
8091
8092 /** Is this window currently animating? */
8093 boolean isWindowAnimating() {
8094 return mAnimation != null;
8095 }
8096
8097 /**
8098 * Like isOnScreen, but returns false if the surface hasn't yet
8099 * been drawn.
8100 */
8101 public boolean isDisplayedLw() {
8102 final AppWindowToken atoken = mAppToken;
8103 return mSurface != null && mPolicyVisibility && !mDestroying
8104 && !mDrawPending && !mCommitDrawPending
8105 && ((!mAttachedHidden &&
8106 (atoken == null || !atoken.hiddenRequested))
8107 || mAnimating);
8108 }
8109
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008110 /**
8111 * Returns true if the window has a surface that it has drawn a
Dianne Hackborn5943c202010-04-12 21:36:49 -07008112 * complete UI in to. Note that this returns true if the orientation
8113 * is changing even if the window hasn't redrawn because we don't want
8114 * to stop things from executing during that time.
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008115 */
8116 public boolean isDrawnLw() {
8117 final AppWindowToken atoken = mAppToken;
8118 return mSurface != null && !mDestroying
Dianne Hackborn5943c202010-04-12 21:36:49 -07008119 && (mOrientationChanging || (!mDrawPending && !mCommitDrawPending));
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008120 }
8121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008122 public boolean fillsScreenLw(int screenWidth, int screenHeight,
8123 boolean shownFrame, boolean onlyOpaque) {
8124 if (mSurface == null) {
8125 return false;
8126 }
8127 if (mAppToken != null && !mAppToken.appFullscreen) {
8128 return false;
8129 }
8130 if (onlyOpaque && mAttrs.format != PixelFormat.OPAQUE) {
8131 return false;
8132 }
8133 final Rect frame = shownFrame ? mShownFrame : mFrame;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07008134
8135 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
8136 return frame.left <= mCompatibleScreenFrame.left &&
8137 frame.top <= mCompatibleScreenFrame.top &&
8138 frame.right >= mCompatibleScreenFrame.right &&
8139 frame.bottom >= mCompatibleScreenFrame.bottom;
8140 } else {
8141 return frame.left <= 0 && frame.top <= 0
8142 && frame.right >= screenWidth
8143 && frame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008144 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008145 }
Romain Guy06882f82009-06-10 13:36:04 -07008146
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008147 /**
Dianne Hackborn25994b42009-09-04 14:21:19 -07008148 * Return true if the window is opaque and fully drawn. This indicates
8149 * it may obscure windows behind it.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008150 */
8151 boolean isOpaqueDrawn() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07008152 return (mAttrs.format == PixelFormat.OPAQUE
8153 || mAttrs.type == TYPE_WALLPAPER)
8154 && mSurface != null && mAnimation == null
8155 && (mAppToken == null || mAppToken.animation == null)
8156 && !mDrawPending && !mCommitDrawPending;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008157 }
8158
8159 boolean needsBackgroundFiller(int screenWidth, int screenHeight) {
8160 return
8161 // only if the application is requesting compatible window
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008162 (mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0 &&
8163 // only if it's visible
8164 mHasDrawn && mViewVisibility == View.VISIBLE &&
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07008165 // and only if the application fills the compatible screen
8166 mFrame.left <= mCompatibleScreenFrame.left &&
8167 mFrame.top <= mCompatibleScreenFrame.top &&
8168 mFrame.right >= mCompatibleScreenFrame.right &&
8169 mFrame.bottom >= mCompatibleScreenFrame.bottom &&
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008170 // and starting window do not need background filler
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07008171 mAttrs.type != mAttrs.TYPE_APPLICATION_STARTING;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008172 }
8173
8174 boolean isFullscreen(int screenWidth, int screenHeight) {
8175 return mFrame.left <= 0 && mFrame.top <= 0 &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07008176 mFrame.right >= screenWidth && mFrame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008177 }
8178
8179 void removeLocked() {
8180 if (mAttachedWindow != null) {
8181 mAttachedWindow.mChildWindows.remove(this);
8182 }
8183 destroySurfaceLocked();
8184 mSession.windowRemovedLocked();
8185 try {
8186 mClient.asBinder().unlinkToDeath(mDeathRecipient, 0);
8187 } catch (RuntimeException e) {
8188 // Ignore if it has already been removed (usually because
8189 // we are doing this as part of processing a death note.)
8190 }
8191 }
8192
8193 private class DeathRecipient implements IBinder.DeathRecipient {
8194 public void binderDied() {
8195 try {
8196 synchronized(mWindowMap) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08008197 WindowState win = windowForClientLocked(mSession, mClient, false);
Joe Onorato8a9b2202010-02-26 18:56:32 -08008198 Slog.i(TAG, "WIN DEATH: " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008199 if (win != null) {
8200 removeWindowLocked(mSession, win);
8201 }
8202 }
8203 } catch (IllegalArgumentException ex) {
8204 // This will happen if the window has already been
8205 // removed.
8206 }
8207 }
8208 }
8209
8210 /** Returns true if this window desires key events. */
8211 public final boolean canReceiveKeys() {
8212 return isVisibleOrAdding()
8213 && (mViewVisibility == View.VISIBLE)
8214 && ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0);
8215 }
8216
8217 public boolean hasDrawnLw() {
8218 return mHasDrawn;
8219 }
8220
8221 public boolean showLw(boolean doAnimation) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008222 return showLw(doAnimation, true);
8223 }
8224
8225 boolean showLw(boolean doAnimation, boolean requestAnim) {
8226 if (mPolicyVisibility && mPolicyVisibilityAfterAnim) {
8227 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008228 }
Dianne Hackbornb601ce12010-03-01 23:36:02 -08008229 if (DEBUG_VISIBILITY) Slog.v(TAG, "Policy visibility true: " + this);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08008230 if (doAnimation) {
8231 if (DEBUG_VISIBILITY) Slog.v(TAG, "doAnimation: mPolicyVisibility="
8232 + mPolicyVisibility + " mAnimation=" + mAnimation);
8233 if (mDisplayFrozen || !mPolicy.isScreenOn()) {
8234 doAnimation = false;
8235 } else if (mPolicyVisibility && mAnimation == null) {
8236 // Check for the case where we are currently visible and
8237 // not animating; we do not want to do animation at such a
8238 // point to become visible when we already are.
8239 doAnimation = false;
8240 }
8241 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008242 mPolicyVisibility = true;
8243 mPolicyVisibilityAfterAnim = true;
8244 if (doAnimation) {
8245 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_ENTER, true);
8246 }
8247 if (requestAnim) {
8248 requestAnimationLocked(0);
8249 }
8250 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008251 }
8252
8253 public boolean hideLw(boolean doAnimation) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008254 return hideLw(doAnimation, true);
8255 }
8256
8257 boolean hideLw(boolean doAnimation, boolean requestAnim) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08008258 if (doAnimation) {
8259 if (mDisplayFrozen || !mPolicy.isScreenOn()) {
8260 doAnimation = false;
8261 }
8262 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008263 boolean current = doAnimation ? mPolicyVisibilityAfterAnim
8264 : mPolicyVisibility;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008265 if (!current) {
8266 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008267 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008268 if (doAnimation) {
8269 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_EXIT, false);
8270 if (mAnimation == null) {
8271 doAnimation = false;
8272 }
8273 }
8274 if (doAnimation) {
8275 mPolicyVisibilityAfterAnim = false;
8276 } else {
Dianne Hackbornb601ce12010-03-01 23:36:02 -08008277 if (DEBUG_VISIBILITY) Slog.v(TAG, "Policy visibility false: " + this);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008278 mPolicyVisibilityAfterAnim = false;
8279 mPolicyVisibility = false;
Dianne Hackbornf3bea9c2009-12-09 18:26:21 -08008280 // Window is no longer visible -- make sure if we were waiting
8281 // for it to be displayed before enabling the display, that
8282 // we allow the display to be enabled now.
8283 enableScreenIfNeededLocked();
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08008284 if (mCurrentFocus == this) {
8285 mFocusMayChange = true;
8286 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008287 }
8288 if (requestAnim) {
8289 requestAnimationLocked(0);
8290 }
8291 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008292 }
8293
8294 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008295 pw.print(prefix); pw.print("mSession="); pw.print(mSession);
8296 pw.print(" mClient="); pw.println(mClient.asBinder());
8297 pw.print(prefix); pw.print("mAttrs="); pw.println(mAttrs);
8298 if (mAttachedWindow != null || mLayoutAttached) {
8299 pw.print(prefix); pw.print("mAttachedWindow="); pw.print(mAttachedWindow);
8300 pw.print(" mLayoutAttached="); pw.println(mLayoutAttached);
8301 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07008302 if (mIsImWindow || mIsWallpaper || mIsFloatingLayer) {
8303 pw.print(prefix); pw.print("mIsImWindow="); pw.print(mIsImWindow);
8304 pw.print(" mIsWallpaper="); pw.print(mIsWallpaper);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008305 pw.print(" mIsFloatingLayer="); pw.print(mIsFloatingLayer);
8306 pw.print(" mWallpaperVisible="); pw.println(mWallpaperVisible);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008307 }
8308 pw.print(prefix); pw.print("mBaseLayer="); pw.print(mBaseLayer);
8309 pw.print(" mSubLayer="); pw.print(mSubLayer);
8310 pw.print(" mAnimLayer="); pw.print(mLayer); pw.print("+");
8311 pw.print((mTargetAppToken != null ? mTargetAppToken.animLayerAdjustment
8312 : (mAppToken != null ? mAppToken.animLayerAdjustment : 0)));
8313 pw.print("="); pw.print(mAnimLayer);
8314 pw.print(" mLastLayer="); pw.println(mLastLayer);
8315 if (mSurface != null) {
8316 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
Dianne Hackborn16064f92010-03-25 00:47:24 -07008317 pw.print(prefix); pw.print("Surface: shown="); pw.print(mSurfaceShown);
8318 pw.print(" layer="); pw.print(mSurfaceLayer);
8319 pw.print(" alpha="); pw.print(mSurfaceAlpha);
8320 pw.print(" rect=("); pw.print(mSurfaceX);
8321 pw.print(","); pw.print(mSurfaceY);
8322 pw.print(") "); pw.print(mSurfaceW);
8323 pw.print(" x "); pw.println(mSurfaceH);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008324 }
8325 pw.print(prefix); pw.print("mToken="); pw.println(mToken);
8326 pw.print(prefix); pw.print("mRootToken="); pw.println(mRootToken);
8327 if (mAppToken != null) {
8328 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
8329 }
8330 if (mTargetAppToken != null) {
8331 pw.print(prefix); pw.print("mTargetAppToken="); pw.println(mTargetAppToken);
8332 }
8333 pw.print(prefix); pw.print("mViewVisibility=0x");
8334 pw.print(Integer.toHexString(mViewVisibility));
8335 pw.print(" mLastHidden="); pw.print(mLastHidden);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07008336 pw.print(" mHaveFrame="); pw.print(mHaveFrame);
8337 pw.print(" mObscured="); pw.println(mObscured);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008338 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim || mAttachedHidden) {
8339 pw.print(prefix); pw.print("mPolicyVisibility=");
8340 pw.print(mPolicyVisibility);
8341 pw.print(" mPolicyVisibilityAfterAnim=");
8342 pw.print(mPolicyVisibilityAfterAnim);
8343 pw.print(" mAttachedHidden="); pw.println(mAttachedHidden);
8344 }
Dianne Hackborn9b52a212009-12-11 14:51:35 -08008345 if (!mRelayoutCalled) {
8346 pw.print(prefix); pw.print("mRelayoutCalled="); pw.println(mRelayoutCalled);
8347 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008348 pw.print(prefix); pw.print("Requested w="); pw.print(mRequestedWidth);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08008349 pw.print(" h="); pw.print(mRequestedHeight);
8350 pw.print(" mLayoutSeq="); pw.println(mLayoutSeq);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008351 if (mXOffset != 0 || mYOffset != 0) {
8352 pw.print(prefix); pw.print("Offsets x="); pw.print(mXOffset);
8353 pw.print(" y="); pw.println(mYOffset);
8354 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008355 pw.print(prefix); pw.print("mGivenContentInsets=");
8356 mGivenContentInsets.printShortString(pw);
8357 pw.print(" mGivenVisibleInsets=");
8358 mGivenVisibleInsets.printShortString(pw);
8359 pw.println();
8360 if (mTouchableInsets != 0 || mGivenInsetsPending) {
8361 pw.print(prefix); pw.print("mTouchableInsets="); pw.print(mTouchableInsets);
8362 pw.print(" mGivenInsetsPending="); pw.println(mGivenInsetsPending);
8363 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08008364 pw.print(prefix); pw.print("mConfiguration="); pw.println(mConfiguration);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008365 pw.print(prefix); pw.print("mShownFrame=");
8366 mShownFrame.printShortString(pw);
8367 pw.print(" last="); mLastShownFrame.printShortString(pw);
8368 pw.println();
8369 pw.print(prefix); pw.print("mFrame="); mFrame.printShortString(pw);
8370 pw.print(" last="); mLastFrame.printShortString(pw);
8371 pw.println();
8372 pw.print(prefix); pw.print("mContainingFrame=");
8373 mContainingFrame.printShortString(pw);
8374 pw.print(" mDisplayFrame=");
8375 mDisplayFrame.printShortString(pw);
8376 pw.println();
8377 pw.print(prefix); pw.print("mContentFrame="); mContentFrame.printShortString(pw);
8378 pw.print(" mVisibleFrame="); mVisibleFrame.printShortString(pw);
8379 pw.println();
8380 pw.print(prefix); pw.print("mContentInsets="); mContentInsets.printShortString(pw);
8381 pw.print(" last="); mLastContentInsets.printShortString(pw);
8382 pw.print(" mVisibleInsets="); mVisibleInsets.printShortString(pw);
8383 pw.print(" last="); mLastVisibleInsets.printShortString(pw);
8384 pw.println();
8385 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
8386 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
8387 pw.print(" mAlpha="); pw.print(mAlpha);
8388 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
8389 }
8390 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
8391 || mAnimation != null) {
8392 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
8393 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
8394 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
8395 pw.print(" mAnimation="); pw.println(mAnimation);
8396 }
8397 if (mHasTransformation || mHasLocalTransformation) {
8398 pw.print(prefix); pw.print("XForm: has=");
8399 pw.print(mHasTransformation);
8400 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
8401 pw.print(" "); mTransformation.printShortString(pw);
8402 pw.println();
8403 }
8404 pw.print(prefix); pw.print("mDrawPending="); pw.print(mDrawPending);
8405 pw.print(" mCommitDrawPending="); pw.print(mCommitDrawPending);
8406 pw.print(" mReadyToShow="); pw.print(mReadyToShow);
8407 pw.print(" mHasDrawn="); pw.println(mHasDrawn);
8408 if (mExiting || mRemoveOnExit || mDestroying || mRemoved) {
8409 pw.print(prefix); pw.print("mExiting="); pw.print(mExiting);
8410 pw.print(" mRemoveOnExit="); pw.print(mRemoveOnExit);
8411 pw.print(" mDestroying="); pw.print(mDestroying);
8412 pw.print(" mRemoved="); pw.println(mRemoved);
8413 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07008414 if (mOrientationChanging || mAppFreezing || mTurnOnScreen) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008415 pw.print(prefix); pw.print("mOrientationChanging=");
8416 pw.print(mOrientationChanging);
Dianne Hackborn93e462b2009-09-15 22:50:40 -07008417 pw.print(" mAppFreezing="); pw.print(mAppFreezing);
8418 pw.print(" mTurnOnScreen="); pw.println(mTurnOnScreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008419 }
Mitsuru Oshima589cebe2009-07-22 20:38:58 -07008420 if (mHScale != 1 || mVScale != 1) {
8421 pw.print(prefix); pw.print("mHScale="); pw.print(mHScale);
8422 pw.print(" mVScale="); pw.println(mVScale);
8423 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07008424 if (mWallpaperX != -1 || mWallpaperY != -1) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008425 pw.print(prefix); pw.print("mWallpaperX="); pw.print(mWallpaperX);
8426 pw.print(" mWallpaperY="); pw.println(mWallpaperY);
8427 }
Marco Nelissenbf6956b2009-11-09 15:21:13 -08008428 if (mWallpaperXStep != -1 || mWallpaperYStep != -1) {
8429 pw.print(prefix); pw.print("mWallpaperXStep="); pw.print(mWallpaperXStep);
8430 pw.print(" mWallpaperYStep="); pw.println(mWallpaperYStep);
8431 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008432 }
8433
8434 @Override
8435 public String toString() {
8436 return "Window{"
8437 + Integer.toHexString(System.identityHashCode(this))
8438 + " " + mAttrs.getTitle() + " paused=" + mToken.paused + "}";
8439 }
8440 }
Romain Guy06882f82009-06-10 13:36:04 -07008441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008442 // -------------------------------------------------------------
8443 // Window Token State
8444 // -------------------------------------------------------------
8445
8446 class WindowToken {
8447 // The actual token.
8448 final IBinder token;
8449
8450 // The type of window this token is for, as per WindowManager.LayoutParams.
8451 final int windowType;
Romain Guy06882f82009-06-10 13:36:04 -07008452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008453 // Set if this token was explicitly added by a client, so should
8454 // not be removed when all windows are removed.
8455 final boolean explicit;
Romain Guy06882f82009-06-10 13:36:04 -07008456
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008457 // For printing.
8458 String stringName;
Romain Guy06882f82009-06-10 13:36:04 -07008459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008460 // If this is an AppWindowToken, this is non-null.
8461 AppWindowToken appWindowToken;
Romain Guy06882f82009-06-10 13:36:04 -07008462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008463 // All of the windows associated with this token.
8464 final ArrayList<WindowState> windows = new ArrayList<WindowState>();
8465
8466 // Is key dispatching paused for this token?
8467 boolean paused = false;
8468
8469 // Should this token's windows be hidden?
8470 boolean hidden;
8471
8472 // Temporary for finding which tokens no longer have visible windows.
8473 boolean hasVisible;
8474
Dianne Hackborna8f60182009-09-01 19:01:50 -07008475 // Set to true when this token is in a pending transaction where it
8476 // will be shown.
8477 boolean waitingToShow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008478
Dianne Hackborna8f60182009-09-01 19:01:50 -07008479 // Set to true when this token is in a pending transaction where it
8480 // will be hidden.
8481 boolean waitingToHide;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008482
Dianne Hackborna8f60182009-09-01 19:01:50 -07008483 // Set to true when this token is in a pending transaction where its
8484 // windows will be put to the bottom of the list.
8485 boolean sendingToBottom;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008486
Dianne Hackborna8f60182009-09-01 19:01:50 -07008487 // Set to true when this token is in a pending transaction where its
8488 // windows will be put to the top of the list.
8489 boolean sendingToTop;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008491 WindowToken(IBinder _token, int type, boolean _explicit) {
8492 token = _token;
8493 windowType = type;
8494 explicit = _explicit;
8495 }
8496
8497 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008498 pw.print(prefix); pw.print("token="); pw.println(token);
8499 pw.print(prefix); pw.print("windows="); pw.println(windows);
8500 pw.print(prefix); pw.print("windowType="); pw.print(windowType);
8501 pw.print(" hidden="); pw.print(hidden);
8502 pw.print(" hasVisible="); pw.println(hasVisible);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008503 if (waitingToShow || waitingToHide || sendingToBottom || sendingToTop) {
8504 pw.print(prefix); pw.print("waitingToShow="); pw.print(waitingToShow);
8505 pw.print(" waitingToHide="); pw.print(waitingToHide);
8506 pw.print(" sendingToBottom="); pw.print(sendingToBottom);
8507 pw.print(" sendingToTop="); pw.println(sendingToTop);
8508 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008509 }
8510
8511 @Override
8512 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008513 if (stringName == null) {
8514 StringBuilder sb = new StringBuilder();
8515 sb.append("WindowToken{");
8516 sb.append(Integer.toHexString(System.identityHashCode(this)));
8517 sb.append(" token="); sb.append(token); sb.append('}');
8518 stringName = sb.toString();
8519 }
8520 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008521 }
8522 };
8523
8524 class AppWindowToken extends WindowToken {
8525 // Non-null only for application tokens.
8526 final IApplicationToken appToken;
8527
8528 // All of the windows and child windows that are included in this
8529 // application token. Note this list is NOT sorted!
8530 final ArrayList<WindowState> allAppWindows = new ArrayList<WindowState>();
8531
8532 int groupId = -1;
8533 boolean appFullscreen;
8534 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Romain Guy06882f82009-06-10 13:36:04 -07008535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008536 // These are used for determining when all windows associated with
8537 // an activity have been drawn, so they can be made visible together
8538 // at the same time.
8539 int lastTransactionSequence = mTransactionSequence-1;
8540 int numInterestingWindows;
8541 int numDrawnWindows;
8542 boolean inPendingTransaction;
8543 boolean allDrawn;
Romain Guy06882f82009-06-10 13:36:04 -07008544
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008545 // Is this token going to be hidden in a little while? If so, it
8546 // won't be taken into account for setting the screen orientation.
8547 boolean willBeHidden;
Romain Guy06882f82009-06-10 13:36:04 -07008548
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008549 // Is this window's surface needed? This is almost like hidden, except
8550 // it will sometimes be true a little earlier: when the token has
8551 // been shown, but is still waiting for its app transition to execute
8552 // before making its windows shown.
8553 boolean hiddenRequested;
Romain Guy06882f82009-06-10 13:36:04 -07008554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008555 // Have we told the window clients to hide themselves?
8556 boolean clientHidden;
Romain Guy06882f82009-06-10 13:36:04 -07008557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008558 // Last visibility state we reported to the app token.
8559 boolean reportedVisible;
8560
8561 // Set to true when the token has been removed from the window mgr.
8562 boolean removed;
8563
8564 // Have we been asked to have this token keep the screen frozen?
8565 boolean freezingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07008566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008567 boolean animating;
8568 Animation animation;
8569 boolean hasTransformation;
8570 final Transformation transformation = new Transformation();
Romain Guy06882f82009-06-10 13:36:04 -07008571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008572 // Offset to the window of all layers in the token, for use by
8573 // AppWindowToken animations.
8574 int animLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -07008575
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008576 // Information about an application starting window if displayed.
8577 StartingData startingData;
8578 WindowState startingWindow;
8579 View startingView;
8580 boolean startingDisplayed;
8581 boolean startingMoved;
8582 boolean firstWindowDrawn;
8583
8584 AppWindowToken(IApplicationToken _token) {
8585 super(_token.asBinder(),
8586 WindowManager.LayoutParams.TYPE_APPLICATION, true);
8587 appWindowToken = this;
8588 appToken = _token;
8589 }
Romain Guy06882f82009-06-10 13:36:04 -07008590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008591 public void setAnimation(Animation anim) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008592 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008593 TAG, "Setting animation in " + this + ": " + anim);
8594 animation = anim;
8595 animating = false;
8596 anim.restrictDuration(MAX_ANIMATION_DURATION);
8597 anim.scaleCurrentDuration(mTransitionAnimationScale);
8598 int zorder = anim.getZAdjustment();
8599 int adj = 0;
8600 if (zorder == Animation.ZORDER_TOP) {
8601 adj = TYPE_LAYER_OFFSET;
8602 } else if (zorder == Animation.ZORDER_BOTTOM) {
8603 adj = -TYPE_LAYER_OFFSET;
8604 }
Romain Guy06882f82009-06-10 13:36:04 -07008605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008606 if (animLayerAdjustment != adj) {
8607 animLayerAdjustment = adj;
8608 updateLayers();
8609 }
8610 }
Romain Guy06882f82009-06-10 13:36:04 -07008611
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008612 public void setDummyAnimation() {
8613 if (animation == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008614 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008615 TAG, "Setting dummy animation in " + this);
8616 animation = sDummyAnimation;
8617 }
8618 }
8619
8620 public void clearAnimation() {
8621 if (animation != null) {
8622 animation = null;
8623 animating = true;
8624 }
8625 }
Romain Guy06882f82009-06-10 13:36:04 -07008626
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008627 void updateLayers() {
8628 final int N = allAppWindows.size();
8629 final int adj = animLayerAdjustment;
8630 for (int i=0; i<N; i++) {
8631 WindowState w = allAppWindows.get(i);
8632 w.mAnimLayer = w.mLayer + adj;
Joe Onorato8a9b2202010-02-26 18:56:32 -08008633 if (DEBUG_LAYERS) Slog.v(TAG, "Updating layer " + w + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008634 + w.mAnimLayer);
8635 if (w == mInputMethodTarget) {
8636 setInputMethodAnimLayerAdjustment(adj);
8637 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008638 if (w == mWallpaperTarget && mLowerWallpaperTarget == null) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008639 setWallpaperAnimLayerAdjustmentLocked(adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008640 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008641 }
8642 }
Romain Guy06882f82009-06-10 13:36:04 -07008643
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008644 void sendAppVisibilityToClients() {
8645 final int N = allAppWindows.size();
8646 for (int i=0; i<N; i++) {
8647 WindowState win = allAppWindows.get(i);
8648 if (win == startingWindow && clientHidden) {
8649 // Don't hide the starting window.
8650 continue;
8651 }
8652 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008653 if (DEBUG_VISIBILITY) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008654 "Setting visibility of " + win + ": " + (!clientHidden));
8655 win.mClient.dispatchAppVisibility(!clientHidden);
8656 } catch (RemoteException e) {
8657 }
8658 }
8659 }
Romain Guy06882f82009-06-10 13:36:04 -07008660
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008661 void showAllWindowsLocked() {
8662 final int NW = allAppWindows.size();
8663 for (int i=0; i<NW; i++) {
8664 WindowState w = allAppWindows.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08008665 if (DEBUG_VISIBILITY) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008666 "performing show on: " + w);
8667 w.performShowLocked();
8668 }
8669 }
Romain Guy06882f82009-06-10 13:36:04 -07008670
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008671 // This must be called while inside a transaction.
8672 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
Dianne Hackbornde2606d2009-12-18 16:53:55 -08008673 if (!mDisplayFrozen && mPolicy.isScreenOn()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008674 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07008675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008676 if (animation == sDummyAnimation) {
8677 // This guy is going to animate, but not yet. For now count
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008678 // it as not animating for purposes of scheduling transactions;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008679 // when it is really time to animate, this will be set to
8680 // a real animation and the next call will execute normally.
8681 return false;
8682 }
Romain Guy06882f82009-06-10 13:36:04 -07008683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008684 if ((allDrawn || animating || startingDisplayed) && animation != null) {
8685 if (!animating) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008686 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008687 TAG, "Starting animation in " + this +
8688 " @ " + currentTime + ": dw=" + dw + " dh=" + dh
8689 + " scale=" + mTransitionAnimationScale
8690 + " allDrawn=" + allDrawn + " animating=" + animating);
8691 animation.initialize(dw, dh, dw, dh);
8692 animation.setStartTime(currentTime);
8693 animating = true;
8694 }
8695 transformation.clear();
8696 final boolean more = animation.getTransformation(
8697 currentTime, transformation);
Joe Onorato8a9b2202010-02-26 18:56:32 -08008698 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008699 TAG, "Stepped animation in " + this +
8700 ": more=" + more + ", xform=" + transformation);
8701 if (more) {
8702 // we're done!
8703 hasTransformation = true;
8704 return true;
8705 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08008706 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008707 TAG, "Finished animation in " + this +
8708 " @ " + currentTime);
8709 animation = null;
8710 }
8711 } else if (animation != null) {
8712 // If the display is frozen, and there is a pending animation,
8713 // clear it and make sure we run the cleanup code.
8714 animating = true;
8715 animation = null;
8716 }
8717
8718 hasTransformation = false;
Romain Guy06882f82009-06-10 13:36:04 -07008719
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008720 if (!animating) {
8721 return false;
8722 }
8723
8724 clearAnimation();
8725 animating = false;
8726 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == this) {
8727 moveInputMethodWindowsIfNeededLocked(true);
8728 }
Romain Guy06882f82009-06-10 13:36:04 -07008729
Joe Onorato8a9b2202010-02-26 18:56:32 -08008730 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008731 TAG, "Animation done in " + this
8732 + ": reportedVisible=" + reportedVisible);
8733
8734 transformation.clear();
8735 if (animLayerAdjustment != 0) {
8736 animLayerAdjustment = 0;
8737 updateLayers();
8738 }
Romain Guy06882f82009-06-10 13:36:04 -07008739
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008740 final int N = windows.size();
8741 for (int i=0; i<N; i++) {
8742 ((WindowState)windows.get(i)).finishExit();
8743 }
8744 updateReportedVisibilityLocked();
Romain Guy06882f82009-06-10 13:36:04 -07008745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008746 return false;
8747 }
8748
8749 void updateReportedVisibilityLocked() {
8750 if (appToken == null) {
8751 return;
8752 }
Romain Guy06882f82009-06-10 13:36:04 -07008753
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008754 int numInteresting = 0;
8755 int numVisible = 0;
8756 boolean nowGone = true;
Romain Guy06882f82009-06-10 13:36:04 -07008757
Joe Onorato8a9b2202010-02-26 18:56:32 -08008758 if (DEBUG_VISIBILITY) Slog.v(TAG, "Update reported visibility: " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008759 final int N = allAppWindows.size();
8760 for (int i=0; i<N; i++) {
8761 WindowState win = allAppWindows.get(i);
Dianne Hackborn6cf67fa2009-12-21 16:46:34 -08008762 if (win == startingWindow || win.mAppFreezing
The Android Open Source Project727cec02010-04-08 11:35:37 -07008763 || win.mViewVisibility != View.VISIBLE
Ulf Rosdahl39357702010-09-29 12:34:38 +02008764 || win.mAttrs.type == TYPE_APPLICATION_STARTING
8765 || win.mDestroying) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008766 continue;
8767 }
8768 if (DEBUG_VISIBILITY) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008769 Slog.v(TAG, "Win " + win + ": isDrawn="
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008770 + win.isDrawnLw()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008771 + ", isAnimating=" + win.isAnimating());
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008772 if (!win.isDrawnLw()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008773 Slog.v(TAG, "Not displayed: s=" + win.mSurface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008774 + " pv=" + win.mPolicyVisibility
8775 + " dp=" + win.mDrawPending
8776 + " cdp=" + win.mCommitDrawPending
8777 + " ah=" + win.mAttachedHidden
8778 + " th="
8779 + (win.mAppToken != null
8780 ? win.mAppToken.hiddenRequested : false)
8781 + " a=" + win.mAnimating);
8782 }
8783 }
8784 numInteresting++;
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008785 if (win.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008786 if (!win.isAnimating()) {
8787 numVisible++;
8788 }
8789 nowGone = false;
8790 } else if (win.isAnimating()) {
8791 nowGone = false;
8792 }
8793 }
Romain Guy06882f82009-06-10 13:36:04 -07008794
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008795 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
Joe Onorato8a9b2202010-02-26 18:56:32 -08008796 if (DEBUG_VISIBILITY) Slog.v(TAG, "VIS " + this + ": interesting="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008797 + numInteresting + " visible=" + numVisible);
8798 if (nowVisible != reportedVisible) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008799 if (DEBUG_VISIBILITY) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008800 TAG, "Visibility changed in " + this
8801 + ": vis=" + nowVisible);
8802 reportedVisible = nowVisible;
8803 Message m = mH.obtainMessage(
8804 H.REPORT_APPLICATION_TOKEN_WINDOWS,
8805 nowVisible ? 1 : 0,
8806 nowGone ? 1 : 0,
8807 this);
8808 mH.sendMessage(m);
8809 }
8810 }
Romain Guy06882f82009-06-10 13:36:04 -07008811
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07008812 WindowState findMainWindow() {
8813 int j = windows.size();
8814 while (j > 0) {
8815 j--;
8816 WindowState win = windows.get(j);
8817 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
8818 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
8819 return win;
8820 }
8821 }
8822 return null;
8823 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008824
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008825 void dump(PrintWriter pw, String prefix) {
8826 super.dump(pw, prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008827 if (appToken != null) {
8828 pw.print(prefix); pw.println("app=true");
8829 }
8830 if (allAppWindows.size() > 0) {
8831 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
8832 }
8833 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008834 pw.print(" appFullscreen="); pw.print(appFullscreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008835 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
8836 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
8837 pw.print(" clientHidden="); pw.print(clientHidden);
8838 pw.print(" willBeHidden="); pw.print(willBeHidden);
8839 pw.print(" reportedVisible="); pw.println(reportedVisible);
8840 if (paused || freezingScreen) {
8841 pw.print(prefix); pw.print("paused="); pw.print(paused);
8842 pw.print(" freezingScreen="); pw.println(freezingScreen);
8843 }
8844 if (numInterestingWindows != 0 || numDrawnWindows != 0
8845 || inPendingTransaction || allDrawn) {
8846 pw.print(prefix); pw.print("numInterestingWindows=");
8847 pw.print(numInterestingWindows);
8848 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
8849 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
8850 pw.print(" allDrawn="); pw.println(allDrawn);
8851 }
8852 if (animating || animation != null) {
8853 pw.print(prefix); pw.print("animating="); pw.print(animating);
8854 pw.print(" animation="); pw.println(animation);
8855 }
8856 if (animLayerAdjustment != 0) {
8857 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
8858 }
8859 if (hasTransformation) {
8860 pw.print(prefix); pw.print("hasTransformation="); pw.print(hasTransformation);
8861 pw.print(" transformation="); transformation.printShortString(pw);
8862 pw.println();
8863 }
8864 if (startingData != null || removed || firstWindowDrawn) {
8865 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
8866 pw.print(" removed="); pw.print(removed);
8867 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
8868 }
8869 if (startingWindow != null || startingView != null
8870 || startingDisplayed || startingMoved) {
8871 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
8872 pw.print(" startingView="); pw.print(startingView);
8873 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
8874 pw.print(" startingMoved"); pw.println(startingMoved);
8875 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008876 }
8877
8878 @Override
8879 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008880 if (stringName == null) {
8881 StringBuilder sb = new StringBuilder();
8882 sb.append("AppWindowToken{");
8883 sb.append(Integer.toHexString(System.identityHashCode(this)));
8884 sb.append(" token="); sb.append(token); sb.append('}');
8885 stringName = sb.toString();
8886 }
8887 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008888 }
8889 }
Romain Guy06882f82009-06-10 13:36:04 -07008890
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008891 // -------------------------------------------------------------
8892 // DummyAnimation
8893 // -------------------------------------------------------------
8894
8895 // This is an animation that does nothing: it just immediately finishes
8896 // itself every time it is called. It is used as a stub animation in cases
8897 // where we want to synchronize multiple things that may be animating.
8898 static final class DummyAnimation extends Animation {
8899 public boolean getTransformation(long currentTime, Transformation outTransformation) {
8900 return false;
8901 }
8902 }
8903 static final Animation sDummyAnimation = new DummyAnimation();
Romain Guy06882f82009-06-10 13:36:04 -07008904
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008905 // -------------------------------------------------------------
8906 // Async Handler
8907 // -------------------------------------------------------------
8908
8909 static final class StartingData {
8910 final String pkg;
8911 final int theme;
8912 final CharSequence nonLocalizedLabel;
8913 final int labelRes;
8914 final int icon;
Romain Guy06882f82009-06-10 13:36:04 -07008915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008916 StartingData(String _pkg, int _theme, CharSequence _nonLocalizedLabel,
8917 int _labelRes, int _icon) {
8918 pkg = _pkg;
8919 theme = _theme;
8920 nonLocalizedLabel = _nonLocalizedLabel;
8921 labelRes = _labelRes;
8922 icon = _icon;
8923 }
8924 }
8925
8926 private final class H extends Handler {
8927 public static final int REPORT_FOCUS_CHANGE = 2;
8928 public static final int REPORT_LOSING_FOCUS = 3;
8929 public static final int ANIMATE = 4;
8930 public static final int ADD_STARTING = 5;
8931 public static final int REMOVE_STARTING = 6;
8932 public static final int FINISHED_STARTING = 7;
8933 public static final int REPORT_APPLICATION_TOKEN_WINDOWS = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008934 public static final int WINDOW_FREEZE_TIMEOUT = 11;
8935 public static final int HOLD_SCREEN_CHANGED = 12;
8936 public static final int APP_TRANSITION_TIMEOUT = 13;
8937 public static final int PERSIST_ANIMATION_SCALE = 14;
8938 public static final int FORCE_GC = 15;
8939 public static final int ENABLE_SCREEN = 16;
8940 public static final int APP_FREEZE_TIMEOUT = 17;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08008941 public static final int SEND_NEW_CONFIGURATION = 18;
Romain Guy06882f82009-06-10 13:36:04 -07008942
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008943 private Session mLastReportedHold;
Romain Guy06882f82009-06-10 13:36:04 -07008944
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008945 public H() {
8946 }
Romain Guy06882f82009-06-10 13:36:04 -07008947
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008948 @Override
8949 public void handleMessage(Message msg) {
8950 switch (msg.what) {
8951 case REPORT_FOCUS_CHANGE: {
8952 WindowState lastFocus;
8953 WindowState newFocus;
Romain Guy06882f82009-06-10 13:36:04 -07008954
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008955 synchronized(mWindowMap) {
8956 lastFocus = mLastFocus;
8957 newFocus = mCurrentFocus;
8958 if (lastFocus == newFocus) {
8959 // Focus is not changing, so nothing to do.
8960 return;
8961 }
8962 mLastFocus = newFocus;
Joe Onorato8a9b2202010-02-26 18:56:32 -08008963 //Slog.i(TAG, "Focus moving from " + lastFocus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008964 // + " to " + newFocus);
8965 if (newFocus != null && lastFocus != null
8966 && !newFocus.isDisplayedLw()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008967 //Slog.i(TAG, "Delaying loss of focus...");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008968 mLosingFocus.add(lastFocus);
8969 lastFocus = null;
8970 }
8971 }
8972
8973 if (lastFocus != newFocus) {
8974 //System.out.println("Changing focus from " + lastFocus
8975 // + " to " + newFocus);
8976 if (newFocus != null) {
8977 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008978 //Slog.i(TAG, "Gaining focus: " + newFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008979 newFocus.mClient.windowFocusChanged(true, mInTouchMode);
8980 } catch (RemoteException e) {
8981 // Ignore if process has died.
8982 }
8983 }
8984
8985 if (lastFocus != null) {
8986 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008987 //Slog.i(TAG, "Losing focus: " + lastFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008988 lastFocus.mClient.windowFocusChanged(false, mInTouchMode);
8989 } catch (RemoteException e) {
8990 // Ignore if process has died.
8991 }
8992 }
8993 }
8994 } break;
8995
8996 case REPORT_LOSING_FOCUS: {
8997 ArrayList<WindowState> losers;
Romain Guy06882f82009-06-10 13:36:04 -07008998
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008999 synchronized(mWindowMap) {
9000 losers = mLosingFocus;
9001 mLosingFocus = new ArrayList<WindowState>();
9002 }
9003
9004 final int N = losers.size();
9005 for (int i=0; i<N; i++) {
9006 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009007 //Slog.i(TAG, "Losing delayed focus: " + losers.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009008 losers.get(i).mClient.windowFocusChanged(false, mInTouchMode);
9009 } catch (RemoteException e) {
9010 // Ignore if process has died.
9011 }
9012 }
9013 } break;
9014
9015 case ANIMATE: {
9016 synchronized(mWindowMap) {
9017 mAnimationPending = false;
9018 performLayoutAndPlaceSurfacesLocked();
9019 }
9020 } break;
9021
9022 case ADD_STARTING: {
9023 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
9024 final StartingData sd = wtoken.startingData;
9025
9026 if (sd == null) {
9027 // Animation has been canceled... do nothing.
9028 return;
9029 }
Romain Guy06882f82009-06-10 13:36:04 -07009030
Joe Onorato8a9b2202010-02-26 18:56:32 -08009031 if (DEBUG_STARTING_WINDOW) Slog.v(TAG, "Add starting "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009032 + wtoken + ": pkg=" + sd.pkg);
Romain Guy06882f82009-06-10 13:36:04 -07009033
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009034 View view = null;
9035 try {
9036 view = mPolicy.addStartingWindow(
9037 wtoken.token, sd.pkg,
9038 sd.theme, sd.nonLocalizedLabel, sd.labelRes,
9039 sd.icon);
9040 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009041 Slog.w(TAG, "Exception when adding starting window", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009042 }
9043
9044 if (view != null) {
9045 boolean abort = false;
9046
9047 synchronized(mWindowMap) {
9048 if (wtoken.removed || wtoken.startingData == null) {
9049 // If the window was successfully added, then
9050 // we need to remove it.
9051 if (wtoken.startingWindow != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009052 if (DEBUG_STARTING_WINDOW) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009053 "Aborted starting " + wtoken
9054 + ": removed=" + wtoken.removed
9055 + " startingData=" + wtoken.startingData);
9056 wtoken.startingWindow = null;
9057 wtoken.startingData = null;
9058 abort = true;
9059 }
9060 } else {
9061 wtoken.startingView = view;
9062 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08009063 if (DEBUG_STARTING_WINDOW && !abort) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009064 "Added starting " + wtoken
9065 + ": startingWindow="
9066 + wtoken.startingWindow + " startingView="
9067 + wtoken.startingView);
9068 }
9069
9070 if (abort) {
9071 try {
9072 mPolicy.removeStartingWindow(wtoken.token, view);
9073 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009074 Slog.w(TAG, "Exception when removing starting window", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009075 }
9076 }
9077 }
9078 } break;
9079
9080 case REMOVE_STARTING: {
9081 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
9082 IBinder token = null;
9083 View view = null;
9084 synchronized (mWindowMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009085 if (DEBUG_STARTING_WINDOW) Slog.v(TAG, "Remove starting "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009086 + wtoken + ": startingWindow="
9087 + wtoken.startingWindow + " startingView="
9088 + wtoken.startingView);
9089 if (wtoken.startingWindow != null) {
9090 view = wtoken.startingView;
9091 token = wtoken.token;
9092 wtoken.startingData = null;
9093 wtoken.startingView = null;
9094 wtoken.startingWindow = null;
9095 }
9096 }
9097 if (view != null) {
9098 try {
9099 mPolicy.removeStartingWindow(token, view);
9100 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009101 Slog.w(TAG, "Exception when removing starting window", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009102 }
9103 }
9104 } break;
9105
9106 case FINISHED_STARTING: {
9107 IBinder token = null;
9108 View view = null;
9109 while (true) {
9110 synchronized (mWindowMap) {
9111 final int N = mFinishedStarting.size();
9112 if (N <= 0) {
9113 break;
9114 }
9115 AppWindowToken wtoken = mFinishedStarting.remove(N-1);
9116
Joe Onorato8a9b2202010-02-26 18:56:32 -08009117 if (DEBUG_STARTING_WINDOW) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009118 "Finished starting " + wtoken
9119 + ": startingWindow=" + wtoken.startingWindow
9120 + " startingView=" + wtoken.startingView);
9121
9122 if (wtoken.startingWindow == null) {
9123 continue;
9124 }
9125
9126 view = wtoken.startingView;
9127 token = wtoken.token;
9128 wtoken.startingData = null;
9129 wtoken.startingView = null;
9130 wtoken.startingWindow = null;
9131 }
9132
9133 try {
9134 mPolicy.removeStartingWindow(token, view);
9135 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009136 Slog.w(TAG, "Exception when removing starting window", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009137 }
9138 }
9139 } break;
9140
9141 case REPORT_APPLICATION_TOKEN_WINDOWS: {
9142 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
9143
9144 boolean nowVisible = msg.arg1 != 0;
9145 boolean nowGone = msg.arg2 != 0;
9146
9147 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009148 if (DEBUG_VISIBILITY) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009149 TAG, "Reporting visible in " + wtoken
9150 + " visible=" + nowVisible
9151 + " gone=" + nowGone);
9152 if (nowVisible) {
9153 wtoken.appToken.windowsVisible();
9154 } else {
9155 wtoken.appToken.windowsGone();
9156 }
9157 } catch (RemoteException ex) {
9158 }
9159 } break;
Romain Guy06882f82009-06-10 13:36:04 -07009160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009161 case WINDOW_FREEZE_TIMEOUT: {
9162 synchronized (mWindowMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009163 Slog.w(TAG, "Window freeze timeout expired.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009164 int i = mWindows.size();
9165 while (i > 0) {
9166 i--;
9167 WindowState w = (WindowState)mWindows.get(i);
9168 if (w.mOrientationChanging) {
9169 w.mOrientationChanging = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009170 Slog.w(TAG, "Force clearing orientation change: " + w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009171 }
9172 }
9173 performLayoutAndPlaceSurfacesLocked();
9174 }
9175 break;
9176 }
Romain Guy06882f82009-06-10 13:36:04 -07009177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009178 case HOLD_SCREEN_CHANGED: {
9179 Session oldHold;
9180 Session newHold;
9181 synchronized (mWindowMap) {
9182 oldHold = mLastReportedHold;
9183 newHold = (Session)msg.obj;
9184 mLastReportedHold = newHold;
9185 }
Romain Guy06882f82009-06-10 13:36:04 -07009186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009187 if (oldHold != newHold) {
9188 try {
9189 if (oldHold != null) {
9190 mBatteryStats.noteStopWakelock(oldHold.mUid,
9191 "window",
9192 BatteryStats.WAKE_TYPE_WINDOW);
9193 }
9194 if (newHold != null) {
9195 mBatteryStats.noteStartWakelock(newHold.mUid,
9196 "window",
9197 BatteryStats.WAKE_TYPE_WINDOW);
9198 }
9199 } catch (RemoteException e) {
9200 }
9201 }
9202 break;
9203 }
Romain Guy06882f82009-06-10 13:36:04 -07009204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009205 case APP_TRANSITION_TIMEOUT: {
9206 synchronized (mWindowMap) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009207 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009208 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009209 "*** APP TRANSITION TIMEOUT");
9210 mAppTransitionReady = true;
9211 mAppTransitionTimeout = true;
9212 performLayoutAndPlaceSurfacesLocked();
9213 }
9214 }
9215 break;
9216 }
Romain Guy06882f82009-06-10 13:36:04 -07009217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009218 case PERSIST_ANIMATION_SCALE: {
9219 Settings.System.putFloat(mContext.getContentResolver(),
9220 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
9221 Settings.System.putFloat(mContext.getContentResolver(),
9222 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
9223 break;
9224 }
Romain Guy06882f82009-06-10 13:36:04 -07009225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009226 case FORCE_GC: {
9227 synchronized(mWindowMap) {
9228 if (mAnimationPending) {
9229 // If we are animating, don't do the gc now but
9230 // delay a bit so we don't interrupt the animation.
9231 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
9232 2000);
9233 return;
9234 }
9235 // If we are currently rotating the display, it will
9236 // schedule a new message when done.
9237 if (mDisplayFrozen) {
9238 return;
9239 }
9240 mFreezeGcPending = 0;
9241 }
9242 Runtime.getRuntime().gc();
9243 break;
9244 }
Romain Guy06882f82009-06-10 13:36:04 -07009245
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009246 case ENABLE_SCREEN: {
9247 performEnableScreen();
9248 break;
9249 }
Romain Guy06882f82009-06-10 13:36:04 -07009250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009251 case APP_FREEZE_TIMEOUT: {
9252 synchronized (mWindowMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009253 Slog.w(TAG, "App freeze timeout expired.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009254 int i = mAppTokens.size();
9255 while (i > 0) {
9256 i--;
9257 AppWindowToken tok = mAppTokens.get(i);
9258 if (tok.freezingScreen) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009259 Slog.w(TAG, "Force clearing freeze: " + tok);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009260 unsetAppFreezingScreenLocked(tok, true, true);
9261 }
9262 }
9263 }
9264 break;
9265 }
Romain Guy06882f82009-06-10 13:36:04 -07009266
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009267 case SEND_NEW_CONFIGURATION: {
9268 removeMessages(SEND_NEW_CONFIGURATION);
9269 sendNewConfiguration();
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07009270 break;
9271 }
Romain Guy06882f82009-06-10 13:36:04 -07009272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009273 }
9274 }
9275 }
9276
9277 // -------------------------------------------------------------
9278 // IWindowManager API
9279 // -------------------------------------------------------------
9280
9281 public IWindowSession openSession(IInputMethodClient client,
9282 IInputContext inputContext) {
9283 if (client == null) throw new IllegalArgumentException("null client");
9284 if (inputContext == null) throw new IllegalArgumentException("null inputContext");
9285 return new Session(client, inputContext);
9286 }
9287
9288 public boolean inputMethodClientHasFocus(IInputMethodClient client) {
9289 synchronized (mWindowMap) {
9290 // The focus for the client is the window immediately below
9291 // where we would place the input method window.
9292 int idx = findDesiredInputMethodWindowIndexLocked(false);
9293 WindowState imFocus;
9294 if (idx > 0) {
9295 imFocus = (WindowState)mWindows.get(idx-1);
9296 if (imFocus != null) {
9297 if (imFocus.mSession.mClient != null &&
9298 imFocus.mSession.mClient.asBinder() == client.asBinder()) {
9299 return true;
9300 }
9301 }
9302 }
9303 }
9304 return false;
9305 }
Romain Guy06882f82009-06-10 13:36:04 -07009306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009307 // -------------------------------------------------------------
9308 // Internals
9309 // -------------------------------------------------------------
9310
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009311 final WindowState windowForClientLocked(Session session, IWindow client,
9312 boolean throwOnError) {
9313 return windowForClientLocked(session, client.asBinder(), throwOnError);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009314 }
Romain Guy06882f82009-06-10 13:36:04 -07009315
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009316 final WindowState windowForClientLocked(Session session, IBinder client,
9317 boolean throwOnError) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009318 WindowState win = mWindowMap.get(client);
Joe Onorato8a9b2202010-02-26 18:56:32 -08009319 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009320 TAG, "Looking up client " + client + ": " + win);
9321 if (win == null) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009322 RuntimeException ex = new IllegalArgumentException(
9323 "Requested window " + client + " does not exist");
9324 if (throwOnError) {
9325 throw ex;
9326 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08009327 Slog.w(TAG, "Failed looking up window", ex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009328 return null;
9329 }
9330 if (session != null && win.mSession != session) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009331 RuntimeException ex = new IllegalArgumentException(
9332 "Requested window " + client + " is in session " +
9333 win.mSession + ", not " + session);
9334 if (throwOnError) {
9335 throw ex;
9336 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08009337 Slog.w(TAG, "Failed looking up window", ex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009338 return null;
9339 }
9340
9341 return win;
9342 }
9343
Dianne Hackborna8f60182009-09-01 19:01:50 -07009344 final void rebuildAppWindowListLocked() {
9345 int NW = mWindows.size();
9346 int i;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009347 int lastWallpaper = -1;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009348 int numRemoved = 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009349
Dianne Hackborna8f60182009-09-01 19:01:50 -07009350 // First remove all existing app windows.
9351 i=0;
9352 while (i < NW) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009353 WindowState w = (WindowState)mWindows.get(i);
9354 if (w.mAppToken != null) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009355 WindowState win = (WindowState)mWindows.remove(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08009356 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG,
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009357 "Rebuild removing window: " + win);
Dianne Hackborna8f60182009-09-01 19:01:50 -07009358 NW--;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009359 numRemoved++;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009360 continue;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009361 } else if (w.mAttrs.type == WindowManager.LayoutParams.TYPE_WALLPAPER
9362 && lastWallpaper == i-1) {
9363 lastWallpaper = i;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009364 }
9365 i++;
9366 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009367
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009368 // The wallpaper window(s) typically live at the bottom of the stack,
9369 // so skip them before adding app tokens.
9370 lastWallpaper++;
9371 i = lastWallpaper;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009372
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009373 // First add all of the exiting app tokens... these are no longer
9374 // in the main app list, but still have windows shown. We put them
9375 // in the back because now that the animation is over we no longer
9376 // will care about them.
9377 int NT = mExitingAppTokens.size();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009378 for (int j=0; j<NT; j++) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009379 i = reAddAppWindowsLocked(i, mExitingAppTokens.get(j));
9380 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009381
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009382 // And add in the still active app tokens in Z order.
9383 NT = mAppTokens.size();
9384 for (int j=0; j<NT; j++) {
9385 i = reAddAppWindowsLocked(i, mAppTokens.get(j));
Dianne Hackborna8f60182009-09-01 19:01:50 -07009386 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009387
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009388 i -= lastWallpaper;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009389 if (i != numRemoved) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009390 Slog.w(TAG, "Rebuild removed " + numRemoved
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009391 + " windows but added " + i);
9392 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07009393 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009394
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009395 private final void assignLayersLocked() {
9396 int N = mWindows.size();
9397 int curBaseLayer = 0;
9398 int curLayer = 0;
9399 int i;
Romain Guy06882f82009-06-10 13:36:04 -07009400
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009401 for (i=0; i<N; i++) {
9402 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009403 if (w.mBaseLayer == curBaseLayer || w.mIsImWindow
9404 || (i > 0 && w.mIsWallpaper)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009405 curLayer += WINDOW_LAYER_MULTIPLIER;
9406 w.mLayer = curLayer;
9407 } else {
9408 curBaseLayer = curLayer = w.mBaseLayer;
9409 w.mLayer = curLayer;
9410 }
9411 if (w.mTargetAppToken != null) {
9412 w.mAnimLayer = w.mLayer + w.mTargetAppToken.animLayerAdjustment;
9413 } else if (w.mAppToken != null) {
9414 w.mAnimLayer = w.mLayer + w.mAppToken.animLayerAdjustment;
9415 } else {
9416 w.mAnimLayer = w.mLayer;
9417 }
9418 if (w.mIsImWindow) {
9419 w.mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07009420 } else if (w.mIsWallpaper) {
9421 w.mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009422 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08009423 if (DEBUG_LAYERS) Slog.v(TAG, "Assign layer " + w + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009424 + w.mAnimLayer);
9425 //System.out.println(
9426 // "Assigned layer " + curLayer + " to " + w.mClient.asBinder());
9427 }
9428 }
9429
9430 private boolean mInLayout = false;
9431 private final void performLayoutAndPlaceSurfacesLocked() {
9432 if (mInLayout) {
Dave Bortcfe65242009-04-09 14:51:04 -07009433 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009434 throw new RuntimeException("Recursive call!");
9435 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08009436 Slog.w(TAG, "performLayoutAndPlaceSurfacesLocked called while in layout");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009437 return;
9438 }
9439
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009440 if (mWaitingForConfig) {
9441 // Our configuration has changed (most likely rotation), but we
9442 // don't yet have the complete configuration to report to
9443 // applications. Don't do any window layout until we have it.
9444 return;
9445 }
9446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009447 boolean recoveringMemory = false;
9448 if (mForceRemoves != null) {
9449 recoveringMemory = true;
9450 // Wait a little it for things to settle down, and off we go.
9451 for (int i=0; i<mForceRemoves.size(); i++) {
9452 WindowState ws = mForceRemoves.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08009453 Slog.i(TAG, "Force removing: " + ws);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009454 removeWindowInnerLocked(ws.mSession, ws);
9455 }
9456 mForceRemoves = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009457 Slog.w(TAG, "Due to memory failure, waiting a bit for next layout");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009458 Object tmp = new Object();
9459 synchronized (tmp) {
9460 try {
9461 tmp.wait(250);
9462 } catch (InterruptedException e) {
9463 }
9464 }
9465 }
Romain Guy06882f82009-06-10 13:36:04 -07009466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009467 mInLayout = true;
9468 try {
9469 performLayoutAndPlaceSurfacesLockedInner(recoveringMemory);
Romain Guy06882f82009-06-10 13:36:04 -07009470
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009471 int i = mPendingRemove.size()-1;
9472 if (i >= 0) {
9473 while (i >= 0) {
9474 WindowState w = mPendingRemove.get(i);
9475 removeWindowInnerLocked(w.mSession, w);
9476 i--;
9477 }
9478 mPendingRemove.clear();
9479
9480 mInLayout = false;
9481 assignLayersLocked();
9482 mLayoutNeeded = true;
9483 performLayoutAndPlaceSurfacesLocked();
9484
9485 } else {
9486 mInLayout = false;
9487 if (mLayoutNeeded) {
9488 requestAnimationLocked(0);
9489 }
9490 }
9491 } catch (RuntimeException e) {
9492 mInLayout = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009493 Slog.e(TAG, "Unhandled exception while layout out windows", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009494 }
9495 }
9496
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009497 private final int performLayoutLockedInner() {
9498 if (!mLayoutNeeded) {
9499 return 0;
9500 }
9501
9502 mLayoutNeeded = false;
9503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009504 final int dw = mDisplay.getWidth();
9505 final int dh = mDisplay.getHeight();
9506
9507 final int N = mWindows.size();
9508 int i;
9509
Joe Onorato8a9b2202010-02-26 18:56:32 -08009510 if (DEBUG_LAYOUT) Slog.v(TAG, "performLayout: needed="
Dianne Hackborn9b52a212009-12-11 14:51:35 -08009511 + mLayoutNeeded + " dw=" + dw + " dh=" + dh);
9512
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009513 mPolicy.beginLayoutLw(dw, dh);
Romain Guy06882f82009-06-10 13:36:04 -07009514
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009515 int seq = mLayoutSeq+1;
9516 if (seq < 0) seq = 0;
9517 mLayoutSeq = seq;
9518
9519 // First perform layout of any root windows (not attached
9520 // to another window).
9521 int topAttached = -1;
9522 for (i = N-1; i >= 0; i--) {
9523 WindowState win = (WindowState) mWindows.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009524
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009525 // Don't do layout of a window if it is not visible, or
9526 // soon won't be visible, to avoid wasting time and funky
9527 // changes while a window is animating away.
9528 final AppWindowToken atoken = win.mAppToken;
9529 final boolean gone = win.mViewVisibility == View.GONE
9530 || !win.mRelayoutCalled
9531 || win.mRootToken.hidden
9532 || (atoken != null && atoken.hiddenRequested)
9533 || win.mAttachedHidden
9534 || win.mExiting || win.mDestroying;
9535
9536 if (!win.mLayoutAttached) {
9537 if (DEBUG_LAYOUT) Slog.v(TAG, "First pass " + win
9538 + ": gone=" + gone + " mHaveFrame=" + win.mHaveFrame
9539 + " mLayoutAttached=" + win.mLayoutAttached);
9540 if (DEBUG_LAYOUT && gone) Slog.v(TAG, " (mViewVisibility="
9541 + win.mViewVisibility + " mRelayoutCalled="
9542 + win.mRelayoutCalled + " hidden="
9543 + win.mRootToken.hidden + " hiddenRequested="
9544 + (atoken != null && atoken.hiddenRequested)
9545 + " mAttachedHidden=" + win.mAttachedHidden);
9546 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009547
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009548 // If this view is GONE, then skip it -- keep the current
9549 // frame, and let the caller know so they can ignore it
9550 // if they want. (We do the normal layout for INVISIBLE
9551 // windows, since that means "perform layout as normal,
9552 // just don't display").
9553 if (!gone || !win.mHaveFrame) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009554 if (!win.mLayoutAttached) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009555 mPolicy.layoutWindowLw(win, win.mAttrs, null);
9556 win.mLayoutSeq = seq;
9557 if (DEBUG_LAYOUT) Slog.v(TAG, "-> mFrame="
9558 + win.mFrame + " mContainingFrame="
9559 + win.mContainingFrame + " mDisplayFrame="
9560 + win.mDisplayFrame);
9561 } else {
9562 if (topAttached < 0) topAttached = i;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009563 }
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009564 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009565 }
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009566
9567 // Now perform layout of attached windows, which usually
9568 // depend on the position of the window they are attached to.
9569 // XXX does not deal with windows that are attached to windows
9570 // that are themselves attached.
9571 for (i = topAttached; i >= 0; i--) {
9572 WindowState win = (WindowState) mWindows.get(i);
9573
9574 // If this view is GONE, then skip it -- keep the current
9575 // frame, and let the caller know so they can ignore it
9576 // if they want. (We do the normal layout for INVISIBLE
9577 // windows, since that means "perform layout as normal,
9578 // just don't display").
9579 if (win.mLayoutAttached) {
9580 if (DEBUG_LAYOUT) Slog.v(TAG, "Second pass " + win
9581 + " mHaveFrame=" + win.mHaveFrame
9582 + " mViewVisibility=" + win.mViewVisibility
9583 + " mRelayoutCalled=" + win.mRelayoutCalled);
9584 if ((win.mViewVisibility != View.GONE && win.mRelayoutCalled)
9585 || !win.mHaveFrame) {
9586 mPolicy.layoutWindowLw(win, win.mAttrs, win.mAttachedWindow);
9587 win.mLayoutSeq = seq;
9588 if (DEBUG_LAYOUT) Slog.v(TAG, "-> mFrame="
9589 + win.mFrame + " mContainingFrame="
9590 + win.mContainingFrame + " mDisplayFrame="
9591 + win.mDisplayFrame);
9592 }
9593 }
9594 }
9595
9596 return mPolicy.finishLayoutLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009597 }
Romain Guy06882f82009-06-10 13:36:04 -07009598
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009599 private final void performLayoutAndPlaceSurfacesLockedInner(
9600 boolean recoveringMemory) {
9601 final long currentTime = SystemClock.uptimeMillis();
9602 final int dw = mDisplay.getWidth();
9603 final int dh = mDisplay.getHeight();
9604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009605 int i;
9606
Dianne Hackbornb601ce12010-03-01 23:36:02 -08009607 if (mFocusMayChange) {
9608 mFocusMayChange = false;
9609 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
9610 }
9611
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009612 if (mFxSession == null) {
9613 mFxSession = new SurfaceSession();
9614 }
Romain Guy06882f82009-06-10 13:36:04 -07009615
Joe Onorato8a9b2202010-02-26 18:56:32 -08009616 if (SHOW_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009617
9618 // Initialize state of exiting tokens.
9619 for (i=mExitingTokens.size()-1; i>=0; i--) {
9620 mExitingTokens.get(i).hasVisible = false;
9621 }
9622
9623 // Initialize state of exiting applications.
9624 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
9625 mExitingAppTokens.get(i).hasVisible = false;
9626 }
9627
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009628 boolean orientationChangeComplete = true;
9629 Session holdScreen = null;
9630 float screenBrightness = -1;
Mike Lockwoodfb73f792009-11-20 11:31:18 -05009631 float buttonBrightness = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009632 boolean focusDisplayed = false;
9633 boolean animating = false;
9634
9635 Surface.openTransaction();
9636 try {
Dianne Hackbornde2606d2009-12-18 16:53:55 -08009637 boolean wallpaperForceHidingChanged = false;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009638 int repeats = 0;
9639 int changes = 0;
9640
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009641 do {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009642 repeats++;
9643 if (repeats > 6) {
9644 Slog.w(TAG, "Animation repeat aborted after too many iterations");
9645 mLayoutNeeded = false;
9646 break;
9647 }
9648
9649 if ((changes&(WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER
9650 | WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG
9651 | WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT)) != 0) {
9652 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
9653 if ((adjustWallpaperWindowsLocked()&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
9654 assignLayersLocked();
9655 mLayoutNeeded = true;
9656 }
9657 }
9658 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG) != 0) {
9659 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
9660 if (updateOrientationFromAppTokensLocked()) {
9661 mLayoutNeeded = true;
9662 mH.sendEmptyMessage(H.SEND_NEW_CONFIGURATION);
9663 }
9664 }
9665 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT) != 0) {
9666 mLayoutNeeded = true;
9667 }
9668 }
9669
9670 // FIRST LOOP: Perform a layout, if needed.
9671 if (repeats < 4) {
9672 changes = performLayoutLockedInner();
9673 if (changes != 0) {
9674 continue;
9675 }
9676 } else {
9677 Slog.w(TAG, "Layout repeat skipped after too many iterations");
9678 changes = 0;
9679 }
9680
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009681 final int transactionSequence = ++mTransactionSequence;
9682
9683 // Update animations of all applications, including those
9684 // associated with exiting/removed apps
9685 boolean tokensAnimating = false;
9686 final int NAT = mAppTokens.size();
9687 for (i=0; i<NAT; i++) {
9688 if (mAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
9689 tokensAnimating = true;
9690 }
9691 }
9692 final int NEAT = mExitingAppTokens.size();
9693 for (i=0; i<NEAT; i++) {
9694 if (mExitingAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
9695 tokensAnimating = true;
9696 }
9697 }
9698
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009699 // SECOND LOOP: Execute animations and update visibility of windows.
9700
Joe Onorato8a9b2202010-02-26 18:56:32 -08009701 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG, "*** ANIM STEP: seq="
Dianne Hackbornde2606d2009-12-18 16:53:55 -08009702 + transactionSequence + " tokensAnimating="
9703 + tokensAnimating);
9704
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009705 animating = tokensAnimating;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009706
9707 boolean tokenMayBeDrawn = false;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009708 boolean wallpaperMayChange = false;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009709 boolean forceHiding = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009710
9711 mPolicy.beginAnimationLw(dw, dh);
9712
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009713 final int N = mWindows.size();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009714
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009715 for (i=N-1; i>=0; i--) {
9716 WindowState w = (WindowState)mWindows.get(i);
9717
9718 final WindowManager.LayoutParams attrs = w.mAttrs;
9719
9720 if (w.mSurface != null) {
9721 // Execute animation.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009722 if (w.commitFinishDrawingLocked(currentTime)) {
9723 if ((w.mAttrs.flags
9724 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009725 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009726 "First draw done in potential wallpaper target " + w);
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009727 wallpaperMayChange = true;
9728 }
9729 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009730
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009731 boolean wasAnimating = w.mAnimating;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009732 if (w.stepAnimationLocked(currentTime, dw, dh)) {
9733 animating = true;
9734 //w.dump(" ");
9735 }
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009736 if (wasAnimating && !w.mAnimating && mWallpaperTarget == w) {
9737 wallpaperMayChange = true;
9738 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009739
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009740 if (mPolicy.doesForceHide(w, attrs)) {
9741 if (!wasAnimating && animating) {
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08009742 if (DEBUG_VISIBILITY) Slog.v(TAG,
9743 "Animation done that could impact force hide: "
9744 + w);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009745 wallpaperForceHidingChanged = true;
Dianne Hackbornb601ce12010-03-01 23:36:02 -08009746 mFocusMayChange = true;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009747 } else if (w.isReadyForDisplay() && w.mAnimation == null) {
9748 forceHiding = true;
9749 }
9750 } else if (mPolicy.canBeForceHidden(w, attrs)) {
9751 boolean changed;
9752 if (forceHiding) {
9753 changed = w.hideLw(false, false);
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08009754 if (DEBUG_VISIBILITY && changed) Slog.v(TAG,
9755 "Now policy hidden: " + w);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009756 } else {
9757 changed = w.showLw(false, false);
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08009758 if (DEBUG_VISIBILITY && changed) Slog.v(TAG,
9759 "Now policy shown: " + w);
9760 if (changed) {
9761 if (wallpaperForceHidingChanged
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009762 && w.isVisibleNow() /*w.isReadyForDisplay()*/) {
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08009763 // Assume we will need to animate. If
9764 // we don't (because the wallpaper will
9765 // stay with the lock screen), then we will
9766 // clean up later.
9767 Animation a = mPolicy.createForceHideEnterAnimation();
9768 if (a != null) {
9769 w.setAnimation(a);
9770 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009771 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08009772 if (mCurrentFocus == null ||
9773 mCurrentFocus.mLayer < w.mLayer) {
9774 // We are showing on to of the current
9775 // focus, so re-evaluate focus to make
9776 // sure it is correct.
9777 mFocusMayChange = true;
9778 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009779 }
9780 }
9781 if (changed && (attrs.flags
9782 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
9783 wallpaperMayChange = true;
9784 }
9785 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009786
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009787 mPolicy.animatingWindowLw(w, attrs);
9788 }
9789
9790 final AppWindowToken atoken = w.mAppToken;
9791 if (atoken != null && (!atoken.allDrawn || atoken.freezingScreen)) {
9792 if (atoken.lastTransactionSequence != transactionSequence) {
9793 atoken.lastTransactionSequence = transactionSequence;
9794 atoken.numInterestingWindows = atoken.numDrawnWindows = 0;
9795 atoken.startingDisplayed = false;
9796 }
9797 if ((w.isOnScreen() || w.mAttrs.type
9798 == WindowManager.LayoutParams.TYPE_BASE_APPLICATION)
9799 && !w.mExiting && !w.mDestroying) {
9800 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009801 Slog.v(TAG, "Eval win " + w + ": isDrawn="
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009802 + w.isDrawnLw()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009803 + ", isAnimating=" + w.isAnimating());
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009804 if (!w.isDrawnLw()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009805 Slog.v(TAG, "Not displayed: s=" + w.mSurface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009806 + " pv=" + w.mPolicyVisibility
9807 + " dp=" + w.mDrawPending
9808 + " cdp=" + w.mCommitDrawPending
9809 + " ah=" + w.mAttachedHidden
9810 + " th=" + atoken.hiddenRequested
9811 + " a=" + w.mAnimating);
9812 }
9813 }
9814 if (w != atoken.startingWindow) {
9815 if (!atoken.freezingScreen || !w.mAppFreezing) {
9816 atoken.numInterestingWindows++;
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009817 if (w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009818 atoken.numDrawnWindows++;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009819 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009820 "tokenMayBeDrawn: " + atoken
9821 + " freezingScreen=" + atoken.freezingScreen
9822 + " mAppFreezing=" + w.mAppFreezing);
9823 tokenMayBeDrawn = true;
9824 }
9825 }
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009826 } else if (w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009827 atoken.startingDisplayed = true;
9828 }
9829 }
9830 } else if (w.mReadyToShow) {
9831 w.performShowLocked();
9832 }
9833 }
9834
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009835 changes |= mPolicy.finishAnimationLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009836
9837 if (tokenMayBeDrawn) {
9838 // See if any windows have been drawn, so they (and others
9839 // associated with them) can now be shown.
9840 final int NT = mTokenList.size();
9841 for (i=0; i<NT; i++) {
9842 AppWindowToken wtoken = mTokenList.get(i).appWindowToken;
9843 if (wtoken == null) {
9844 continue;
9845 }
9846 if (wtoken.freezingScreen) {
9847 int numInteresting = wtoken.numInterestingWindows;
9848 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009849 if (DEBUG_VISIBILITY) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009850 "allDrawn: " + wtoken
9851 + " interesting=" + numInteresting
9852 + " drawn=" + wtoken.numDrawnWindows);
9853 wtoken.showAllWindowsLocked();
9854 unsetAppFreezingScreenLocked(wtoken, false, true);
9855 orientationChangeComplete = true;
9856 }
9857 } else if (!wtoken.allDrawn) {
9858 int numInteresting = wtoken.numInterestingWindows;
9859 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009860 if (DEBUG_VISIBILITY) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009861 "allDrawn: " + wtoken
9862 + " interesting=" + numInteresting
9863 + " drawn=" + wtoken.numDrawnWindows);
9864 wtoken.allDrawn = true;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009865 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_ANIM;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009866
9867 // We can now show all of the drawn windows!
9868 if (!mOpeningApps.contains(wtoken)) {
9869 wtoken.showAllWindowsLocked();
9870 }
9871 }
9872 }
9873 }
9874 }
9875
9876 // If we are ready to perform an app transition, check through
9877 // all of the app tokens to be shown and see if they are ready
9878 // to go.
9879 if (mAppTransitionReady) {
9880 int NN = mOpeningApps.size();
9881 boolean goodToGo = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009882 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009883 "Checking " + NN + " opening apps (frozen="
9884 + mDisplayFrozen + " timeout="
9885 + mAppTransitionTimeout + ")...");
9886 if (!mDisplayFrozen && !mAppTransitionTimeout) {
9887 // If the display isn't frozen, wait to do anything until
9888 // all of the apps are ready. Otherwise just go because
9889 // we'll unfreeze the display when everyone is ready.
9890 for (i=0; i<NN && goodToGo; i++) {
9891 AppWindowToken wtoken = mOpeningApps.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08009892 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009893 "Check opening app" + wtoken + ": allDrawn="
9894 + wtoken.allDrawn + " startingDisplayed="
9895 + wtoken.startingDisplayed);
9896 if (!wtoken.allDrawn && !wtoken.startingDisplayed
9897 && !wtoken.startingMoved) {
9898 goodToGo = false;
9899 }
9900 }
9901 }
9902 if (goodToGo) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009903 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG, "**** GOOD TO GO");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009904 int transit = mNextAppTransition;
9905 if (mSkipAppTransitionAnimation) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009906 transit = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009907 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009908 mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009909 mAppTransitionReady = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009910 mAppTransitionRunning = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009911 mAppTransitionTimeout = false;
9912 mStartingIconInTransition = false;
9913 mSkipAppTransitionAnimation = false;
9914
9915 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
9916
Dianne Hackborna8f60182009-09-01 19:01:50 -07009917 // If there are applications waiting to come to the
9918 // top of the stack, now is the time to move their windows.
9919 // (Note that we don't do apps going to the bottom
9920 // here -- we want to keep their windows in the old
9921 // Z-order until the animation completes.)
9922 if (mToTopApps.size() > 0) {
9923 NN = mAppTokens.size();
9924 for (i=0; i<NN; i++) {
9925 AppWindowToken wtoken = mAppTokens.get(i);
9926 if (wtoken.sendingToTop) {
9927 wtoken.sendingToTop = false;
9928 moveAppWindowsLocked(wtoken, NN, false);
9929 }
9930 }
9931 mToTopApps.clear();
9932 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009933
Dianne Hackborn25994b42009-09-04 14:21:19 -07009934 WindowState oldWallpaper = mWallpaperTarget;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009935
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009936 adjustWallpaperWindowsLocked();
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009937 wallpaperMayChange = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009938
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009939 // The top-most window will supply the layout params,
9940 // and we will determine it below.
9941 LayoutParams animLp = null;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009942 AppWindowToken animToken = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009943 int bestAnimLayer = -1;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009944
Joe Onorato8a9b2202010-02-26 18:56:32 -08009945 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009946 "New wallpaper target=" + mWallpaperTarget
9947 + ", lower target=" + mLowerWallpaperTarget
9948 + ", upper target=" + mUpperWallpaperTarget);
Dianne Hackborn25994b42009-09-04 14:21:19 -07009949 int foundWallpapers = 0;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009950 // Do a first pass through the tokens for two
9951 // things:
9952 // (1) Determine if both the closing and opening
9953 // app token sets are wallpaper targets, in which
9954 // case special animations are needed
9955 // (since the wallpaper needs to stay static
9956 // behind them).
9957 // (2) Find the layout params of the top-most
9958 // application window in the tokens, which is
9959 // what will control the animation theme.
9960 final int NC = mClosingApps.size();
9961 NN = NC + mOpeningApps.size();
9962 for (i=0; i<NN; i++) {
9963 AppWindowToken wtoken;
9964 int mode;
9965 if (i < NC) {
9966 wtoken = mClosingApps.get(i);
9967 mode = 1;
9968 } else {
9969 wtoken = mOpeningApps.get(i-NC);
9970 mode = 2;
9971 }
9972 if (mLowerWallpaperTarget != null) {
9973 if (mLowerWallpaperTarget.mAppToken == wtoken
9974 || mUpperWallpaperTarget.mAppToken == wtoken) {
9975 foundWallpapers |= mode;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009976 }
9977 }
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009978 if (wtoken.appFullscreen) {
9979 WindowState ws = wtoken.findMainWindow();
9980 if (ws != null) {
9981 // If this is a compatibility mode
9982 // window, we will always use its anim.
9983 if ((ws.mAttrs.flags&FLAG_COMPATIBLE_WINDOW) != 0) {
9984 animLp = ws.mAttrs;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009985 animToken = ws.mAppToken;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009986 bestAnimLayer = Integer.MAX_VALUE;
9987 } else if (ws.mLayer > bestAnimLayer) {
9988 animLp = ws.mAttrs;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009989 animToken = ws.mAppToken;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009990 bestAnimLayer = ws.mLayer;
9991 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07009992 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009993 }
9994 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009995
Dianne Hackborn25994b42009-09-04 14:21:19 -07009996 if (foundWallpapers == 3) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009997 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -07009998 "Wallpaper animation!");
9999 switch (transit) {
10000 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
10001 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
10002 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
10003 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN;
10004 break;
10005 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
10006 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
10007 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
10008 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE;
10009 break;
10010 }
Joe Onorato8a9b2202010-02-26 18:56:32 -080010011 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -070010012 "New transit: " + transit);
10013 } else if (oldWallpaper != null) {
10014 // We are transitioning from an activity with
10015 // a wallpaper to one without.
10016 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010017 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -070010018 "New transit away from wallpaper: " + transit);
10019 } else if (mWallpaperTarget != null) {
10020 // We are transitioning from an activity without
10021 // a wallpaper to now showing the wallpaper
10022 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010023 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -070010024 "New transit into wallpaper: " + transit);
10025 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010026
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010027 if ((transit&WindowManagerPolicy.TRANSIT_ENTER_MASK) != 0) {
10028 mLastEnterAnimToken = animToken;
10029 mLastEnterAnimParams = animLp;
10030 } else if (mLastEnterAnimParams != null) {
10031 animLp = mLastEnterAnimParams;
10032 mLastEnterAnimToken = null;
10033 mLastEnterAnimParams = null;
10034 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010035
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010036 // If all closing windows are obscured, then there is
10037 // no need to do an animation. This is the case, for
10038 // example, when this transition is being done behind
10039 // the lock screen.
10040 if (!mPolicy.allowAppAnimationsLw()) {
10041 animLp = null;
10042 }
10043
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010044 NN = mOpeningApps.size();
10045 for (i=0; i<NN; i++) {
10046 AppWindowToken wtoken = mOpeningApps.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -080010047 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010048 "Now opening app" + wtoken);
10049 wtoken.reportedVisible = false;
10050 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -070010051 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -070010052 setTokenVisibilityLocked(wtoken, animLp, true, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010053 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -070010054 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010055 wtoken.showAllWindowsLocked();
10056 }
10057 NN = mClosingApps.size();
10058 for (i=0; i<NN; i++) {
10059 AppWindowToken wtoken = mClosingApps.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -080010060 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010061 "Now closing app" + wtoken);
10062 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -070010063 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -070010064 setTokenVisibilityLocked(wtoken, animLp, false, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010065 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -070010066 wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010067 // Force the allDrawn flag, because we want to start
10068 // this guy's animations regardless of whether it's
10069 // gotten drawn.
10070 wtoken.allDrawn = true;
10071 }
10072
Dianne Hackborn8b571a82009-09-25 16:09:43 -070010073 mNextAppTransitionPackage = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010074
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010075 mOpeningApps.clear();
10076 mClosingApps.clear();
10077
10078 // This has changed the visibility of windows, so perform
10079 // a new layout to get them all up-to-date.
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010080 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_LAYOUT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010081 mLayoutNeeded = true;
Dianne Hackborn20583ff2009-07-27 21:51:05 -070010082 if (!moveInputMethodWindowsIfNeededLocked(true)) {
10083 assignLayersLocked();
10084 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010085 updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES);
Dianne Hackbornb601ce12010-03-01 23:36:02 -080010086 mFocusMayChange = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010087 }
10088 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010089
Dianne Hackborn16064f92010-03-25 00:47:24 -070010090 int adjResult = 0;
10091
Dianne Hackborna8f60182009-09-01 19:01:50 -070010092 if (!animating && mAppTransitionRunning) {
10093 // We have finished the animation of an app transition. To do
10094 // this, we have delayed a lot of operations like showing and
10095 // hiding apps, moving apps in Z-order, etc. The app token list
10096 // reflects the correct Z-order, but the window list may now
10097 // be out of sync with it. So here we will just rebuild the
10098 // entire app window list. Fun!
10099 mAppTransitionRunning = false;
10100 // Clear information about apps that were moving.
10101 mToBottomApps.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010102
Dianne Hackborna8f60182009-09-01 19:01:50 -070010103 rebuildAppWindowListLocked();
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010104 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_LAYOUT;
Dianne Hackborn16064f92010-03-25 00:47:24 -070010105 adjResult |= ADJUST_WALLPAPER_LAYERS_CHANGED;
Dianne Hackborna8f60182009-09-01 19:01:50 -070010106 moveInputMethodWindowsIfNeededLocked(false);
10107 wallpaperMayChange = true;
Suchi Amalapurapuc9568e32009-11-05 18:51:16 -080010108 // Since the window list has been rebuilt, focus might
10109 // have to be recomputed since the actual order of windows
10110 // might have changed again.
Dianne Hackbornb601ce12010-03-01 23:36:02 -080010111 mFocusMayChange = true;
Dianne Hackborna8f60182009-09-01 19:01:50 -070010112 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010113
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010114 if (wallpaperForceHidingChanged && changes == 0 && !mAppTransitionReady) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010115 // At this point, there was a window with a wallpaper that
10116 // was force hiding other windows behind it, but now it
10117 // is going away. This may be simple -- just animate
10118 // away the wallpaper and its window -- or it may be
10119 // hard -- the wallpaper now needs to be shown behind
10120 // something that was hidden.
10121 WindowState oldWallpaper = mWallpaperTarget;
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010122 if (mLowerWallpaperTarget != null
10123 && mLowerWallpaperTarget.mAppToken != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010124 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010125 "wallpaperForceHiding changed with lower="
10126 + mLowerWallpaperTarget);
Joe Onorato8a9b2202010-02-26 18:56:32 -080010127 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010128 "hidden=" + mLowerWallpaperTarget.mAppToken.hidden +
10129 " hiddenRequested=" + mLowerWallpaperTarget.mAppToken.hiddenRequested);
10130 if (mLowerWallpaperTarget.mAppToken.hidden) {
10131 // The lower target has become hidden before we
10132 // actually started the animation... let's completely
10133 // re-evaluate everything.
10134 mLowerWallpaperTarget = mUpperWallpaperTarget = null;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010135 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_ANIM;
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010136 }
10137 }
Dianne Hackborn16064f92010-03-25 00:47:24 -070010138 adjResult |= adjustWallpaperWindowsLocked();
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010139 wallpaperMayChange = false;
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010140 wallpaperForceHidingChanged = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010141 if (DEBUG_WALLPAPER) Slog.v(TAG, "****** OLD: " + oldWallpaper
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010142 + " NEW: " + mWallpaperTarget
10143 + " LOWER: " + mLowerWallpaperTarget);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010144 if (mLowerWallpaperTarget == null) {
10145 // Whoops, we don't need a special wallpaper animation.
10146 // Clear them out.
10147 forceHiding = false;
10148 for (i=N-1; i>=0; i--) {
10149 WindowState w = (WindowState)mWindows.get(i);
10150 if (w.mSurface != null) {
10151 final WindowManager.LayoutParams attrs = w.mAttrs;
Suchi Amalapurapuc03d28b2009-10-28 14:32:05 -070010152 if (mPolicy.doesForceHide(w, attrs) && w.isVisibleLw()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010153 if (DEBUG_FOCUS) Slog.i(TAG, "win=" + w + " force hides other windows");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010154 forceHiding = true;
10155 } else if (mPolicy.canBeForceHidden(w, attrs)) {
10156 if (!w.mAnimating) {
10157 // We set the animation above so it
10158 // is not yet running.
10159 w.clearAnimation();
10160 }
10161 }
10162 }
10163 }
10164 }
10165 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010166
Dianne Hackborn6c3f5712009-08-25 18:42:59 -070010167 if (wallpaperMayChange) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010168 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010169 "Wallpaper may change! Adjusting");
Dianne Hackborn16064f92010-03-25 00:47:24 -070010170 adjResult |= adjustWallpaperWindowsLocked();
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010171 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010172
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010173 if ((adjResult&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010174 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010175 "Wallpaper layer changed: assigning layers + relayout");
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010176 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_LAYOUT;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010177 assignLayersLocked();
10178 } else if ((adjResult&ADJUST_WALLPAPER_VISIBILITY_CHANGED) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010179 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010180 "Wallpaper visibility changed: relayout");
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010181 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_LAYOUT;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010182 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010183
Dianne Hackbornb601ce12010-03-01 23:36:02 -080010184 if (mFocusMayChange) {
10185 mFocusMayChange = false;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010186 if (updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES)) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010187 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_ANIM;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010188 adjResult = 0;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -070010189 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010190 }
10191
10192 if (mLayoutNeeded) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010193 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_LAYOUT;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -070010194 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010195
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010196 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG, "*** ANIM STEP: changes=0x"
10197 + Integer.toHexString(changes));
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010198
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010199 } while (changes != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010200
10201 // THIRD LOOP: Update the surfaces of all windows.
10202
10203 final boolean someoneLosingFocus = mLosingFocus.size() != 0;
10204
10205 boolean obscured = false;
10206 boolean blurring = false;
10207 boolean dimming = false;
10208 boolean covered = false;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -070010209 boolean syswin = false;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010210 boolean backgroundFillerShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010211
Dianne Hackbornbdd52b22009-09-02 21:46:19 -070010212 final int N = mWindows.size();
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010214 for (i=N-1; i>=0; i--) {
10215 WindowState w = (WindowState)mWindows.get(i);
10216
10217 boolean displayed = false;
10218 final WindowManager.LayoutParams attrs = w.mAttrs;
10219 final int attrFlags = attrs.flags;
10220
10221 if (w.mSurface != null) {
Dianne Hackbornac3587d2010-03-11 11:12:11 -080010222 // XXX NOTE: The logic here could be improved. We have
10223 // the decision about whether to resize a window separated
10224 // from whether to hide the surface. This can cause us to
10225 // resize a surface even if we are going to hide it. You
10226 // can see this by (1) holding device in landscape mode on
10227 // home screen; (2) tapping browser icon (device will rotate
10228 // to landscape; (3) tap home. The wallpaper will be resized
10229 // in step 2 but then immediately hidden, causing us to
10230 // have to resize and then redraw it again in step 3. It
10231 // would be nice to figure out how to avoid this, but it is
10232 // difficult because we do need to resize surfaces in some
10233 // cases while they are hidden such as when first showing a
10234 // window.
10235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010236 w.computeShownFrameLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -080010237 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010238 TAG, "Placing surface #" + i + " " + w.mSurface
10239 + ": new=" + w.mShownFrame + ", old="
10240 + w.mLastShownFrame);
10241
10242 boolean resize;
10243 int width, height;
10244 if ((w.mAttrs.flags & w.mAttrs.FLAG_SCALED) != 0) {
10245 resize = w.mLastRequestedWidth != w.mRequestedWidth ||
10246 w.mLastRequestedHeight != w.mRequestedHeight;
10247 // for a scaled surface, we just want to use
10248 // the requested size.
10249 width = w.mRequestedWidth;
10250 height = w.mRequestedHeight;
10251 w.mLastRequestedWidth = width;
10252 w.mLastRequestedHeight = height;
10253 w.mLastShownFrame.set(w.mShownFrame);
10254 try {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010255 if (SHOW_TRANSACTIONS) logSurface(w,
10256 "POS " + w.mShownFrame.left
10257 + ", " + w.mShownFrame.top, null);
Dianne Hackborn16064f92010-03-25 00:47:24 -070010258 w.mSurfaceX = w.mShownFrame.left;
10259 w.mSurfaceY = w.mShownFrame.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010260 w.mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
10261 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010262 Slog.w(TAG, "Error positioning surface in " + w, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010263 if (!recoveringMemory) {
10264 reclaimSomeSurfaceMemoryLocked(w, "position");
10265 }
10266 }
10267 } else {
10268 resize = !w.mLastShownFrame.equals(w.mShownFrame);
10269 width = w.mShownFrame.width();
10270 height = w.mShownFrame.height();
10271 w.mLastShownFrame.set(w.mShownFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010272 }
10273
10274 if (resize) {
10275 if (width < 1) width = 1;
10276 if (height < 1) height = 1;
10277 if (w.mSurface != null) {
10278 try {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010279 if (SHOW_TRANSACTIONS) logSurface(w,
10280 "POS " + w.mShownFrame.left + ","
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010281 + w.mShownFrame.top + " SIZE "
10282 + w.mShownFrame.width() + "x"
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010283 + w.mShownFrame.height(), null);
Dianne Hackbornac3587d2010-03-11 11:12:11 -080010284 w.mSurfaceResized = true;
Dianne Hackborn16064f92010-03-25 00:47:24 -070010285 w.mSurfaceW = width;
10286 w.mSurfaceH = height;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010287 w.mSurface.setSize(width, height);
Dianne Hackborn16064f92010-03-25 00:47:24 -070010288 w.mSurfaceX = w.mShownFrame.left;
10289 w.mSurfaceY = w.mShownFrame.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010290 w.mSurface.setPosition(w.mShownFrame.left,
10291 w.mShownFrame.top);
10292 } catch (RuntimeException e) {
10293 // If something goes wrong with the surface (such
10294 // as running out of memory), don't take down the
10295 // entire system.
Joe Onorato8a9b2202010-02-26 18:56:32 -080010296 Slog.e(TAG, "Failure updating surface of " + w
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010297 + "size=(" + width + "x" + height
10298 + "), pos=(" + w.mShownFrame.left
10299 + "," + w.mShownFrame.top + ")", e);
10300 if (!recoveringMemory) {
10301 reclaimSomeSurfaceMemoryLocked(w, "size");
10302 }
10303 }
10304 }
10305 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010306 if (!w.mAppFreezing && w.mLayoutSeq == mLayoutSeq) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010307 w.mContentInsetsChanged =
10308 !w.mLastContentInsets.equals(w.mContentInsets);
10309 w.mVisibleInsetsChanged =
10310 !w.mLastVisibleInsets.equals(w.mVisibleInsets);
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010311 boolean configChanged =
10312 w.mConfiguration != mCurConfiguration
10313 && (w.mConfiguration == null
10314 || mCurConfiguration.diff(w.mConfiguration) != 0);
Dianne Hackborn694f79b2010-03-17 19:44:59 -070010315 if (DEBUG_CONFIGURATION && configChanged) {
10316 Slog.v(TAG, "Win " + w + " config changed: "
10317 + mCurConfiguration);
10318 }
Joe Onorato8a9b2202010-02-26 18:56:32 -080010319 if (localLOGV) Slog.v(TAG, "Resizing " + w
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010320 + ": configChanged=" + configChanged
10321 + " last=" + w.mLastFrame + " frame=" + w.mFrame);
Romain Guy06882f82009-06-10 13:36:04 -070010322 if (!w.mLastFrame.equals(w.mFrame)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010323 || w.mContentInsetsChanged
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010324 || w.mVisibleInsetsChanged
Dianne Hackbornac3587d2010-03-11 11:12:11 -080010325 || w.mSurfaceResized
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010326 || configChanged) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010327 w.mLastFrame.set(w.mFrame);
10328 w.mLastContentInsets.set(w.mContentInsets);
10329 w.mLastVisibleInsets.set(w.mVisibleInsets);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010330 // If the screen is currently frozen, then keep
10331 // it frozen until this window draws at its new
10332 // orientation.
10333 if (mDisplayFrozen) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010334 if (DEBUG_ORIENTATION) Slog.v(TAG,
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010335 "Resizing while display frozen: " + w);
10336 w.mOrientationChanging = true;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010337 if (!mWindowsFreezingScreen) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010338 mWindowsFreezingScreen = true;
10339 // XXX should probably keep timeout from
10340 // when we first froze the display.
10341 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
10342 mH.sendMessageDelayed(mH.obtainMessage(
10343 H.WINDOW_FREEZE_TIMEOUT), 2000);
10344 }
10345 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010346 // If the orientation is changing, then we need to
10347 // hold off on unfreezing the display until this
10348 // window has been redrawn; to do that, we need
10349 // to go through the process of getting informed
10350 // by the application when it has finished drawing.
10351 if (w.mOrientationChanging) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010352 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010353 "Orientation start waiting for draw in "
10354 + w + ", surface " + w.mSurface);
10355 w.mDrawPending = true;
10356 w.mCommitDrawPending = false;
10357 w.mReadyToShow = false;
10358 if (w.mAppToken != null) {
10359 w.mAppToken.allDrawn = false;
10360 }
10361 }
Dianne Hackbornac3587d2010-03-11 11:12:11 -080010362 if (DEBUG_RESIZE || DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010363 "Resizing window " + w + " to " + w.mFrame);
10364 mResizingWindows.add(w);
10365 } else if (w.mOrientationChanging) {
10366 if (!w.mDrawPending && !w.mCommitDrawPending) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010367 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010368 "Orientation not waiting for draw in "
10369 + w + ", surface " + w.mSurface);
10370 w.mOrientationChanging = false;
10371 }
10372 }
10373 }
10374
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010375 if (w.mAttachedHidden || !w.isReadyForDisplay()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010376 if (!w.mLastHidden) {
10377 //dump();
Dianne Hackborn5943c202010-04-12 21:36:49 -070010378 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Window hiding: waitingToShow="
10379 + w.mRootToken.waitingToShow + " polvis="
10380 + w.mPolicyVisibility + " atthid="
10381 + w.mAttachedHidden + " tokhid="
10382 + w.mRootToken.hidden + " vis="
10383 + w.mViewVisibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010384 w.mLastHidden = true;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010385 if (SHOW_TRANSACTIONS) logSurface(w,
10386 "HIDE (performLayout)", null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010387 if (w.mSurface != null) {
Dianne Hackborn16064f92010-03-25 00:47:24 -070010388 w.mSurfaceShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010389 try {
10390 w.mSurface.hide();
10391 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010392 Slog.w(TAG, "Exception hiding surface in " + w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010393 }
10394 }
10395 mKeyWaiter.releasePendingPointerLocked(w.mSession);
10396 }
10397 // If we are waiting for this window to handle an
10398 // orientation change, well, it is hidden, so
10399 // doesn't really matter. Note that this does
10400 // introduce a potential glitch if the window
10401 // becomes unhidden before it has drawn for the
10402 // new orientation.
10403 if (w.mOrientationChanging) {
10404 w.mOrientationChanging = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010405 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010406 "Orientation change skips hidden " + w);
10407 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010408 } else if (w.mLastLayer != w.mAnimLayer
10409 || w.mLastAlpha != w.mShownAlpha
10410 || w.mLastDsDx != w.mDsDx
10411 || w.mLastDtDx != w.mDtDx
10412 || w.mLastDsDy != w.mDsDy
10413 || w.mLastDtDy != w.mDtDy
10414 || w.mLastHScale != w.mHScale
10415 || w.mLastVScale != w.mVScale
10416 || w.mLastHidden) {
10417 displayed = true;
10418 w.mLastAlpha = w.mShownAlpha;
10419 w.mLastLayer = w.mAnimLayer;
10420 w.mLastDsDx = w.mDsDx;
10421 w.mLastDtDx = w.mDtDx;
10422 w.mLastDsDy = w.mDsDy;
10423 w.mLastDtDy = w.mDtDy;
10424 w.mLastHScale = w.mHScale;
10425 w.mLastVScale = w.mVScale;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010426 if (SHOW_TRANSACTIONS) logSurface(w,
10427 "alpha=" + w.mShownAlpha + " layer=" + w.mAnimLayer
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010428 + " matrix=[" + (w.mDsDx*w.mHScale)
10429 + "," + (w.mDtDx*w.mVScale)
10430 + "][" + (w.mDsDy*w.mHScale)
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010431 + "," + (w.mDtDy*w.mVScale) + "]", null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010432 if (w.mSurface != null) {
10433 try {
Dianne Hackborn16064f92010-03-25 00:47:24 -070010434 w.mSurfaceAlpha = w.mShownAlpha;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010435 w.mSurface.setAlpha(w.mShownAlpha);
Dianne Hackborn16064f92010-03-25 00:47:24 -070010436 w.mSurfaceLayer = w.mAnimLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010437 w.mSurface.setLayer(w.mAnimLayer);
10438 w.mSurface.setMatrix(
10439 w.mDsDx*w.mHScale, w.mDtDx*w.mVScale,
10440 w.mDsDy*w.mHScale, w.mDtDy*w.mVScale);
10441 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010442 Slog.w(TAG, "Error updating surface in " + w, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010443 if (!recoveringMemory) {
10444 reclaimSomeSurfaceMemoryLocked(w, "update");
10445 }
10446 }
10447 }
10448
10449 if (w.mLastHidden && !w.mDrawPending
10450 && !w.mCommitDrawPending
10451 && !w.mReadyToShow) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010452 if (SHOW_TRANSACTIONS) logSurface(w,
10453 "SHOW (performLayout)", null);
Joe Onorato8a9b2202010-02-26 18:56:32 -080010454 if (DEBUG_VISIBILITY) Slog.v(TAG, "Showing " + w
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010455 + " during relayout");
10456 if (showSurfaceRobustlyLocked(w)) {
10457 w.mHasDrawn = true;
10458 w.mLastHidden = false;
10459 } else {
10460 w.mOrientationChanging = false;
10461 }
10462 }
10463 if (w.mSurface != null) {
10464 w.mToken.hasVisible = true;
10465 }
10466 } else {
10467 displayed = true;
10468 }
10469
10470 if (displayed) {
10471 if (!covered) {
Romain Guy980a9382010-01-08 15:06:28 -080010472 if (attrs.width == LayoutParams.MATCH_PARENT
10473 && attrs.height == LayoutParams.MATCH_PARENT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010474 covered = true;
10475 }
10476 }
10477 if (w.mOrientationChanging) {
10478 if (w.mDrawPending || w.mCommitDrawPending) {
10479 orientationChangeComplete = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010480 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010481 "Orientation continue waiting for draw in " + w);
10482 } else {
10483 w.mOrientationChanging = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010484 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010485 "Orientation change complete in " + w);
10486 }
10487 }
10488 w.mToken.hasVisible = true;
10489 }
10490 } else if (w.mOrientationChanging) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010491 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010492 "Orientation change skips hidden " + w);
10493 w.mOrientationChanging = false;
10494 }
10495
10496 final boolean canBeSeen = w.isDisplayedLw();
10497
10498 if (someoneLosingFocus && w == mCurrentFocus && canBeSeen) {
10499 focusDisplayed = true;
10500 }
10501
Dianne Hackborne9e9bca2009-08-18 15:08:22 -070010502 final boolean obscuredChanged = w.mObscured != obscured;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010504 // Update effect.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010505 if (!(w.mObscured=obscured)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010506 if (w.mSurface != null) {
10507 if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
10508 holdScreen = w.mSession;
10509 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -070010510 if (!syswin && w.mAttrs.screenBrightness >= 0
10511 && screenBrightness < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010512 screenBrightness = w.mAttrs.screenBrightness;
10513 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -050010514 if (!syswin && w.mAttrs.buttonBrightness >= 0
10515 && buttonBrightness < 0) {
10516 buttonBrightness = w.mAttrs.buttonBrightness;
10517 }
Mike Lockwood46af6a82010-03-09 08:28:22 -050010518 if (canBeSeen
10519 && (attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG
10520 || attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD
10521 || attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_ERROR)) {
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -070010522 syswin = true;
10523 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010524 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010525
Dianne Hackborn25994b42009-09-04 14:21:19 -070010526 boolean opaqueDrawn = canBeSeen && w.isOpaqueDrawn();
10527 if (opaqueDrawn && w.isFullscreen(dw, dh)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010528 // This window completely covers everything behind it,
10529 // so we want to leave all of them as unblurred (for
10530 // performance reasons).
10531 obscured = true;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010532 } else if (opaqueDrawn && w.needsBackgroundFiller(dw, dh)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010533 if (SHOW_TRANSACTIONS) Slog.d(TAG, "showing background filler");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010534 // This window is in compatibility mode, and needs background filler.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010535 obscured = true;
10536 if (mBackgroundFillerSurface == null) {
10537 try {
10538 mBackgroundFillerSurface = new Surface(mFxSession, 0,
Mathias Agopian5d26c1e2010-03-01 16:09:43 -080010539 "BackGroundFiller",
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010540 0, dw, dh,
10541 PixelFormat.OPAQUE,
10542 Surface.FX_SURFACE_NORMAL);
10543 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010544 Slog.e(TAG, "Exception creating filler surface", e);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010545 }
10546 }
10547 try {
10548 mBackgroundFillerSurface.setPosition(0, 0);
10549 mBackgroundFillerSurface.setSize(dw, dh);
10550 // Using the same layer as Dim because they will never be shown at the
10551 // same time.
10552 mBackgroundFillerSurface.setLayer(w.mAnimLayer - 1);
10553 mBackgroundFillerSurface.show();
10554 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010555 Slog.e(TAG, "Exception showing filler surface");
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010556 }
10557 backgroundFillerShown = true;
10558 mBackgroundFillerShown = true;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010559 } else if (canBeSeen && !obscured &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010560 (attrFlags&FLAG_BLUR_BEHIND|FLAG_DIM_BEHIND) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010561 if (localLOGV) Slog.v(TAG, "Win " + w
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010562 + ": blurring=" + blurring
10563 + " obscured=" + obscured
10564 + " displayed=" + displayed);
10565 if ((attrFlags&FLAG_DIM_BEHIND) != 0) {
10566 if (!dimming) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010567 //Slog.i(TAG, "DIM BEHIND: " + w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010568 dimming = true;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010569 if (mDimAnimator == null) {
10570 mDimAnimator = new DimAnimator(mFxSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010571 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010572 mDimAnimator.show(dw, dh);
Dianne Hackborn16064f92010-03-25 00:47:24 -070010573 mDimAnimator.updateParameters(w, currentTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010574 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010575 }
10576 if ((attrFlags&FLAG_BLUR_BEHIND) != 0) {
10577 if (!blurring) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010578 //Slog.i(TAG, "BLUR BEHIND: " + w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010579 blurring = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010580 if (mBlurSurface == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010581 if (SHOW_TRANSACTIONS) Slog.i(TAG, " BLUR "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010582 + mBlurSurface + ": CREATE");
10583 try {
Romain Guy06882f82009-06-10 13:36:04 -070010584 mBlurSurface = new Surface(mFxSession, 0,
Mathias Agopian5d26c1e2010-03-01 16:09:43 -080010585 "BlurSurface",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010586 -1, 16, 16,
10587 PixelFormat.OPAQUE,
10588 Surface.FX_SURFACE_BLUR);
10589 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010590 Slog.e(TAG, "Exception creating Blur surface", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010591 }
10592 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010593 if (mBlurSurface != null) {
Dianne Hackborn16064f92010-03-25 00:47:24 -070010594 if (SHOW_TRANSACTIONS) Slog.i(TAG, " BLUR "
10595 + mBlurSurface + ": pos=(0,0) (" +
10596 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010597 mBlurSurface.setPosition(0, 0);
10598 mBlurSurface.setSize(dw, dh);
Dianne Hackborn16064f92010-03-25 00:47:24 -070010599 mBlurSurface.setLayer(w.mAnimLayer-2);
10600 if (!mBlurShown) {
10601 try {
10602 if (SHOW_TRANSACTIONS) Slog.i(TAG, " BLUR "
10603 + mBlurSurface + ": SHOW");
10604 mBlurSurface.show();
10605 } catch (RuntimeException e) {
10606 Slog.w(TAG, "Failure showing blur surface", e);
10607 }
10608 mBlurShown = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010609 }
10610 }
10611 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010612 }
10613 }
10614 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010615
Dianne Hackborne9e9bca2009-08-18 15:08:22 -070010616 if (obscuredChanged && mWallpaperTarget == w) {
10617 // This is the wallpaper target and its obscured state
10618 // changed... make sure the current wallaper's visibility
10619 // has been updated accordingly.
10620 updateWallpaperVisibilityLocked();
10621 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010622 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010623
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010624 if (backgroundFillerShown == false && mBackgroundFillerShown) {
10625 mBackgroundFillerShown = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010626 if (SHOW_TRANSACTIONS) Slog.d(TAG, "hiding background filler");
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010627 try {
10628 mBackgroundFillerSurface.hide();
10629 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010630 Slog.e(TAG, "Exception hiding filler surface", e);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010631 }
10632 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010633
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010634 if (mDimAnimator != null && mDimAnimator.mDimShown) {
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010635 animating |= mDimAnimator.updateSurface(dimming, currentTime,
10636 mDisplayFrozen || !mPolicy.isScreenOn());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010637 }
Romain Guy06882f82009-06-10 13:36:04 -070010638
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010639 if (!blurring && mBlurShown) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010640 if (SHOW_TRANSACTIONS) Slog.i(TAG, " BLUR " + mBlurSurface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010641 + ": HIDE");
10642 try {
10643 mBlurSurface.hide();
10644 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010645 Slog.w(TAG, "Illegal argument exception hiding blur surface");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010646 }
10647 mBlurShown = false;
10648 }
10649
Joe Onorato8a9b2202010-02-26 18:56:32 -080010650 if (SHOW_TRANSACTIONS) Slog.i(TAG, "<<< CLOSE TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010651 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010652 Slog.e(TAG, "Unhandled exception in Window Manager", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010653 }
10654
10655 Surface.closeTransaction();
Romain Guy06882f82009-06-10 13:36:04 -070010656
Joe Onorato8a9b2202010-02-26 18:56:32 -080010657 if (DEBUG_ORIENTATION && mDisplayFrozen) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010658 "With display frozen, orientationChangeComplete="
10659 + orientationChangeComplete);
10660 if (orientationChangeComplete) {
10661 if (mWindowsFreezingScreen) {
10662 mWindowsFreezingScreen = false;
10663 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
10664 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010665 stopFreezingDisplayLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010666 }
Romain Guy06882f82009-06-10 13:36:04 -070010667
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010668 i = mResizingWindows.size();
10669 if (i > 0) {
10670 do {
10671 i--;
10672 WindowState win = mResizingWindows.get(i);
10673 try {
Dianne Hackbornac3587d2010-03-11 11:12:11 -080010674 if (DEBUG_RESIZE || DEBUG_ORIENTATION) Slog.v(TAG,
10675 "Reporting new frame to " + win + ": " + win.mFrame);
Dianne Hackborn694f79b2010-03-17 19:44:59 -070010676 int diff = 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010677 boolean configChanged =
10678 win.mConfiguration != mCurConfiguration
10679 && (win.mConfiguration == null
Dianne Hackborn694f79b2010-03-17 19:44:59 -070010680 || (diff=mCurConfiguration.diff(win.mConfiguration)) != 0);
10681 if ((DEBUG_RESIZE || DEBUG_ORIENTATION || DEBUG_CONFIGURATION)
10682 && configChanged) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010683 Slog.i(TAG, "Sending new config to window " + win + ": "
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010684 + win.mFrame.width() + "x" + win.mFrame.height()
Dianne Hackborn694f79b2010-03-17 19:44:59 -070010685 + " / " + mCurConfiguration + " / 0x"
10686 + Integer.toHexString(diff));
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010687 }
Dianne Hackborn694f79b2010-03-17 19:44:59 -070010688 win.mConfiguration = mCurConfiguration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010689 win.mClient.resized(win.mFrame.width(),
10690 win.mFrame.height(), win.mLastContentInsets,
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010691 win.mLastVisibleInsets, win.mDrawPending,
10692 configChanged ? win.mConfiguration : null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010693 win.mContentInsetsChanged = false;
10694 win.mVisibleInsetsChanged = false;
Dianne Hackbornac3587d2010-03-11 11:12:11 -080010695 win.mSurfaceResized = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010696 } catch (RemoteException e) {
10697 win.mOrientationChanging = false;
10698 }
10699 } while (i > 0);
10700 mResizingWindows.clear();
10701 }
Romain Guy06882f82009-06-10 13:36:04 -070010702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010703 // Destroy the surface of any windows that are no longer visible.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010704 boolean wallpaperDestroyed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010705 i = mDestroySurface.size();
10706 if (i > 0) {
10707 do {
10708 i--;
10709 WindowState win = mDestroySurface.get(i);
10710 win.mDestroying = false;
10711 if (mInputMethodWindow == win) {
10712 mInputMethodWindow = null;
10713 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010714 if (win == mWallpaperTarget) {
10715 wallpaperDestroyed = true;
10716 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010717 win.destroySurfaceLocked();
10718 } while (i > 0);
10719 mDestroySurface.clear();
10720 }
10721
10722 // Time to remove any exiting tokens?
10723 for (i=mExitingTokens.size()-1; i>=0; i--) {
10724 WindowToken token = mExitingTokens.get(i);
10725 if (!token.hasVisible) {
10726 mExitingTokens.remove(i);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070010727 if (token.windowType == TYPE_WALLPAPER) {
10728 mWallpaperTokens.remove(token);
10729 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010730 }
10731 }
10732
10733 // Time to remove any exiting applications?
10734 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
10735 AppWindowToken token = mExitingAppTokens.get(i);
10736 if (!token.hasVisible && !mClosingApps.contains(token)) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -070010737 // Make sure there is no animation running on this token,
10738 // so any windows associated with it will be removed as
10739 // soon as their animations are complete
10740 token.animation = null;
10741 token.animating = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010742 mAppTokens.remove(token);
10743 mExitingAppTokens.remove(i);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010744 if (mLastEnterAnimToken == token) {
10745 mLastEnterAnimToken = null;
10746 mLastEnterAnimParams = null;
10747 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010748 }
10749 }
10750
Dianne Hackborna8f60182009-09-01 19:01:50 -070010751 boolean needRelayout = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010752
Dianne Hackborna8f60182009-09-01 19:01:50 -070010753 if (!animating && mAppTransitionRunning) {
10754 // We have finished the animation of an app transition. To do
10755 // this, we have delayed a lot of operations like showing and
10756 // hiding apps, moving apps in Z-order, etc. The app token list
10757 // reflects the correct Z-order, but the window list may now
10758 // be out of sync with it. So here we will just rebuild the
10759 // entire app window list. Fun!
10760 mAppTransitionRunning = false;
10761 needRelayout = true;
10762 rebuildAppWindowListLocked();
Dianne Hackborn16064f92010-03-25 00:47:24 -070010763 assignLayersLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -070010764 // Clear information about apps that were moving.
10765 mToBottomApps.clear();
10766 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010767
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010768 if (focusDisplayed) {
10769 mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
10770 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010771 if (wallpaperDestroyed) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010772 needRelayout = adjustWallpaperWindowsLocked() != 0;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010773 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070010774 if (needRelayout) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010775 requestAnimationLocked(0);
10776 } else if (animating) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010777 requestAnimationLocked(currentTime+(1000/60)-SystemClock.uptimeMillis());
10778 }
Dianne Hackbornce73c1e2010-04-12 23:11:38 -070010779
10780 if (DEBUG_FREEZE) Slog.v(TAG, "Layout: mDisplayFrozen=" + mDisplayFrozen
10781 + " holdScreen=" + holdScreen);
10782 if (!mDisplayFrozen) {
10783 mQueue.setHoldScreenLocked(holdScreen != null);
10784 if (screenBrightness < 0 || screenBrightness > 1.0f) {
10785 mPowerManager.setScreenBrightnessOverride(-1);
10786 } else {
10787 mPowerManager.setScreenBrightnessOverride((int)
10788 (screenBrightness * Power.BRIGHTNESS_ON));
10789 }
10790 if (buttonBrightness < 0 || buttonBrightness > 1.0f) {
10791 mPowerManager.setButtonBrightnessOverride(-1);
10792 } else {
10793 mPowerManager.setButtonBrightnessOverride((int)
10794 (buttonBrightness * Power.BRIGHTNESS_ON));
10795 }
10796 if (holdScreen != mHoldingScreenOn) {
10797 mHoldingScreenOn = holdScreen;
10798 Message m = mH.obtainMessage(H.HOLD_SCREEN_CHANGED, holdScreen);
10799 mH.sendMessage(m);
10800 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010801 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010802
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010803 if (mTurnOnScreen) {
Dianne Hackbornb601ce12010-03-01 23:36:02 -080010804 if (DEBUG_VISIBILITY) Slog.v(TAG, "Turning screen on after layout!");
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010805 mPowerManager.userActivity(SystemClock.uptimeMillis(), false,
10806 LocalPowerManager.BUTTON_EVENT, true);
10807 mTurnOnScreen = false;
10808 }
Dianne Hackbornf3bea9c2009-12-09 18:26:21 -080010809
10810 // Check to see if we are now in a state where the screen should
10811 // be enabled, because the window obscured flags have changed.
10812 enableScreenIfNeededLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010813 }
10814
10815 void requestAnimationLocked(long delay) {
10816 if (!mAnimationPending) {
10817 mAnimationPending = true;
10818 mH.sendMessageDelayed(mH.obtainMessage(H.ANIMATE), delay);
10819 }
10820 }
Romain Guy06882f82009-06-10 13:36:04 -070010821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010822 /**
10823 * Have the surface flinger show a surface, robustly dealing with
10824 * error conditions. In particular, if there is not enough memory
10825 * to show the surface, then we will try to get rid of other surfaces
10826 * in order to succeed.
Romain Guy06882f82009-06-10 13:36:04 -070010827 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010828 * @return Returns true if the surface was successfully shown.
10829 */
10830 boolean showSurfaceRobustlyLocked(WindowState win) {
10831 try {
10832 if (win.mSurface != null) {
Dianne Hackborn16064f92010-03-25 00:47:24 -070010833 win.mSurfaceShown = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010834 win.mSurface.show();
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010835 if (win.mTurnOnScreen) {
Dianne Hackbornb601ce12010-03-01 23:36:02 -080010836 if (DEBUG_VISIBILITY) Slog.v(TAG,
10837 "Show surface turning screen on: " + win);
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010838 win.mTurnOnScreen = false;
10839 mTurnOnScreen = true;
10840 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010841 }
10842 return true;
10843 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010844 Slog.w(TAG, "Failure showing surface " + win.mSurface + " in " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010845 }
Romain Guy06882f82009-06-10 13:36:04 -070010846
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010847 reclaimSomeSurfaceMemoryLocked(win, "show");
Romain Guy06882f82009-06-10 13:36:04 -070010848
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010849 return false;
10850 }
Romain Guy06882f82009-06-10 13:36:04 -070010851
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010852 void reclaimSomeSurfaceMemoryLocked(WindowState win, String operation) {
10853 final Surface surface = win.mSurface;
Romain Guy06882f82009-06-10 13:36:04 -070010854
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010855 EventLog.writeEvent(EventLogTags.WM_NO_SURFACE_MEMORY, win.toString(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010856 win.mSession.mPid, operation);
Romain Guy06882f82009-06-10 13:36:04 -070010857
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010858 if (mForceRemoves == null) {
10859 mForceRemoves = new ArrayList<WindowState>();
10860 }
Romain Guy06882f82009-06-10 13:36:04 -070010861
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010862 long callingIdentity = Binder.clearCallingIdentity();
10863 try {
10864 // There was some problem... first, do a sanity check of the
10865 // window list to make sure we haven't left any dangling surfaces
10866 // around.
10867 int N = mWindows.size();
10868 boolean leakedSurface = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010869 Slog.i(TAG, "Out of memory for surface! Looking for leaks...");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010870 for (int i=0; i<N; i++) {
10871 WindowState ws = (WindowState)mWindows.get(i);
10872 if (ws.mSurface != null) {
10873 if (!mSessions.contains(ws.mSession)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010874 Slog.w(TAG, "LEAKED SURFACE (session doesn't exist): "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010875 + ws + " surface=" + ws.mSurface
10876 + " token=" + win.mToken
10877 + " pid=" + ws.mSession.mPid
10878 + " uid=" + ws.mSession.mUid);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010879 ws.mSurface.destroy();
Dianne Hackborn16064f92010-03-25 00:47:24 -070010880 ws.mSurfaceShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010881 ws.mSurface = null;
10882 mForceRemoves.add(ws);
10883 i--;
10884 N--;
10885 leakedSurface = true;
10886 } else if (win.mAppToken != null && win.mAppToken.clientHidden) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010887 Slog.w(TAG, "LEAKED SURFACE (app token hidden): "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010888 + ws + " surface=" + ws.mSurface
10889 + " token=" + win.mAppToken);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010890 ws.mSurface.destroy();
Dianne Hackborn16064f92010-03-25 00:47:24 -070010891 ws.mSurfaceShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010892 ws.mSurface = null;
10893 leakedSurface = true;
10894 }
10895 }
10896 }
Romain Guy06882f82009-06-10 13:36:04 -070010897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010898 boolean killedApps = false;
10899 if (!leakedSurface) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010900 Slog.w(TAG, "No leaked surfaces; killing applicatons!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010901 SparseIntArray pidCandidates = new SparseIntArray();
10902 for (int i=0; i<N; i++) {
10903 WindowState ws = (WindowState)mWindows.get(i);
10904 if (ws.mSurface != null) {
10905 pidCandidates.append(ws.mSession.mPid, ws.mSession.mPid);
10906 }
10907 }
10908 if (pidCandidates.size() > 0) {
10909 int[] pids = new int[pidCandidates.size()];
10910 for (int i=0; i<pids.length; i++) {
10911 pids[i] = pidCandidates.keyAt(i);
10912 }
10913 try {
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -070010914 if (mActivityManager.killPids(pids, "Free memory")) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010915 killedApps = true;
10916 }
10917 } catch (RemoteException e) {
10918 }
10919 }
10920 }
Romain Guy06882f82009-06-10 13:36:04 -070010921
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010922 if (leakedSurface || killedApps) {
10923 // We managed to reclaim some memory, so get rid of the trouble
10924 // surface and ask the app to request another one.
Joe Onorato8a9b2202010-02-26 18:56:32 -080010925 Slog.w(TAG, "Looks like we have reclaimed some memory, clearing surface for retry.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010926 if (surface != null) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010927 surface.destroy();
Dianne Hackborn16064f92010-03-25 00:47:24 -070010928 win.mSurfaceShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010929 win.mSurface = null;
10930 }
Romain Guy06882f82009-06-10 13:36:04 -070010931
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010932 try {
10933 win.mClient.dispatchGetNewSurface();
10934 } catch (RemoteException e) {
10935 }
10936 }
10937 } finally {
10938 Binder.restoreCallingIdentity(callingIdentity);
10939 }
10940 }
Romain Guy06882f82009-06-10 13:36:04 -070010941
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010942 private boolean updateFocusedWindowLocked(int mode) {
10943 WindowState newFocus = computeFocusedWindowLocked();
10944 if (mCurrentFocus != newFocus) {
10945 // This check makes sure that we don't already have the focus
10946 // change message pending.
10947 mH.removeMessages(H.REPORT_FOCUS_CHANGE);
10948 mH.sendEmptyMessage(H.REPORT_FOCUS_CHANGE);
Joe Onorato8a9b2202010-02-26 18:56:32 -080010949 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010950 TAG, "Changing focus from " + mCurrentFocus + " to " + newFocus);
10951 final WindowState oldFocus = mCurrentFocus;
10952 mCurrentFocus = newFocus;
10953 mLosingFocus.remove(newFocus);
Romain Guy06882f82009-06-10 13:36:04 -070010954
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010955 final WindowState imWindow = mInputMethodWindow;
10956 if (newFocus != imWindow && oldFocus != imWindow) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010957 if (moveInputMethodWindowsIfNeededLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010958 mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS &&
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010959 mode != UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
10960 mLayoutNeeded = true;
10961 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010962 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
10963 performLayoutLockedInner();
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010964 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
10965 // Client will do the layout, but we need to assign layers
10966 // for handleNewWindowLocked() below.
10967 assignLayersLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010968 }
10969 }
Romain Guy06882f82009-06-10 13:36:04 -070010970
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010971 if (newFocus != null && mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
10972 mKeyWaiter.handleNewWindowLocked(newFocus);
10973 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010974 return true;
10975 }
10976 return false;
10977 }
10978
10979 private WindowState computeFocusedWindowLocked() {
10980 WindowState result = null;
10981 WindowState win;
10982
10983 int i = mWindows.size() - 1;
10984 int nextAppIndex = mAppTokens.size()-1;
10985 WindowToken nextApp = nextAppIndex >= 0
10986 ? mAppTokens.get(nextAppIndex) : null;
10987
10988 while (i >= 0) {
10989 win = (WindowState)mWindows.get(i);
10990
Joe Onorato8a9b2202010-02-26 18:56:32 -080010991 if (localLOGV || DEBUG_FOCUS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010992 TAG, "Looking for focus: " + i
10993 + " = " + win
10994 + ", flags=" + win.mAttrs.flags
10995 + ", canReceive=" + win.canReceiveKeys());
10996
10997 AppWindowToken thisApp = win.mAppToken;
Romain Guy06882f82009-06-10 13:36:04 -070010998
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010999 // If this window's application has been removed, just skip it.
11000 if (thisApp != null && thisApp.removed) {
11001 i--;
11002 continue;
11003 }
Romain Guy06882f82009-06-10 13:36:04 -070011004
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011005 // If there is a focused app, don't allow focus to go to any
11006 // windows below it. If this is an application window, step
11007 // through the app tokens until we find its app.
11008 if (thisApp != null && nextApp != null && thisApp != nextApp
11009 && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
11010 int origAppIndex = nextAppIndex;
11011 while (nextAppIndex > 0) {
11012 if (nextApp == mFocusedApp) {
11013 // Whoops, we are below the focused app... no focus
11014 // for you!
Joe Onorato8a9b2202010-02-26 18:56:32 -080011015 if (localLOGV || DEBUG_FOCUS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011016 TAG, "Reached focused app: " + mFocusedApp);
11017 return null;
11018 }
11019 nextAppIndex--;
11020 nextApp = mAppTokens.get(nextAppIndex);
11021 if (nextApp == thisApp) {
11022 break;
11023 }
11024 }
11025 if (thisApp != nextApp) {
11026 // Uh oh, the app token doesn't exist! This shouldn't
11027 // happen, but if it does we can get totally hosed...
11028 // so restart at the original app.
11029 nextAppIndex = origAppIndex;
11030 nextApp = mAppTokens.get(nextAppIndex);
11031 }
11032 }
11033
11034 // Dispatch to this window if it is wants key events.
11035 if (win.canReceiveKeys()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011036 if (DEBUG_FOCUS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011037 TAG, "Found focus @ " + i + " = " + win);
11038 result = win;
11039 break;
11040 }
11041
11042 i--;
11043 }
11044
11045 return result;
11046 }
11047
11048 private void startFreezingDisplayLocked() {
11049 if (mDisplayFrozen) {
Chris Tate2ad63a92009-03-25 17:36:48 -070011050 // Freezing the display also suspends key event delivery, to
11051 // keep events from going astray while the display is reconfigured.
11052 // If someone has changed orientation again while the screen is
11053 // still frozen, the events will continue to be blocked while the
11054 // successive orientation change is processed. To prevent spurious
11055 // ANRs, we reset the event dispatch timeout in this case.
11056 synchronized (mKeyWaiter) {
11057 mKeyWaiter.mWasFrozen = true;
11058 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011059 return;
11060 }
Romain Guy06882f82009-06-10 13:36:04 -070011061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011062 mScreenFrozenLock.acquire();
Romain Guy06882f82009-06-10 13:36:04 -070011063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011064 long now = SystemClock.uptimeMillis();
Joe Onorato8a9b2202010-02-26 18:56:32 -080011065 //Slog.i(TAG, "Freezing, gc pending: " + mFreezeGcPending + ", now " + now);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011066 if (mFreezeGcPending != 0) {
11067 if (now > (mFreezeGcPending+1000)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011068 //Slog.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011069 mH.removeMessages(H.FORCE_GC);
11070 Runtime.getRuntime().gc();
11071 mFreezeGcPending = now;
11072 }
11073 } else {
11074 mFreezeGcPending = now;
11075 }
Romain Guy06882f82009-06-10 13:36:04 -070011076
Dianne Hackbornce73c1e2010-04-12 23:11:38 -070011077 if (DEBUG_FREEZE) Slog.v(TAG, "*** FREEZING DISPLAY", new RuntimeException());
11078
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011079 mDisplayFrozen = true;
Dianne Hackbornbfe319e2009-09-21 00:34:05 -070011080 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
11081 mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070011082 mNextAppTransitionPackage = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011083 mAppTransitionReady = true;
11084 }
Romain Guy06882f82009-06-10 13:36:04 -070011085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011086 if (PROFILE_ORIENTATION) {
11087 File file = new File("/data/system/frozen");
11088 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
11089 }
11090 Surface.freezeDisplay(0);
11091 }
Romain Guy06882f82009-06-10 13:36:04 -070011092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011093 private void stopFreezingDisplayLocked() {
11094 if (!mDisplayFrozen) {
11095 return;
11096 }
Romain Guy06882f82009-06-10 13:36:04 -070011097
Dianne Hackborne36d6e22010-02-17 19:46:25 -080011098 if (mWaitingForConfig || mAppsFreezingScreen > 0 || mWindowsFreezingScreen) {
11099 return;
11100 }
11101
Dianne Hackbornce73c1e2010-04-12 23:11:38 -070011102 if (DEBUG_FREEZE) Slog.v(TAG, "*** UNFREEZING DISPLAY", new RuntimeException());
11103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011104 mDisplayFrozen = false;
11105 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
11106 if (PROFILE_ORIENTATION) {
11107 Debug.stopMethodTracing();
11108 }
11109 Surface.unfreezeDisplay(0);
Romain Guy06882f82009-06-10 13:36:04 -070011110
Chris Tate2ad63a92009-03-25 17:36:48 -070011111 // Reset the key delivery timeout on unfreeze, too. We force a wakeup here
11112 // too because regular key delivery processing should resume immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011113 synchronized (mKeyWaiter) {
11114 mKeyWaiter.mWasFrozen = true;
11115 mKeyWaiter.notifyAll();
11116 }
11117
Christopher Tateb696aee2010-04-02 19:08:30 -070011118 // While the display is frozen we don't re-compute the orientation
11119 // to avoid inconsistent states. However, something interesting
11120 // could have actually changed during that time so re-evaluate it
11121 // now to catch that.
11122 if (updateOrientationFromAppTokensLocked()) {
11123 mH.sendEmptyMessage(H.SEND_NEW_CONFIGURATION);
11124 }
11125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011126 // A little kludge: a lot could have happened while the
11127 // display was frozen, so now that we are coming back we
11128 // do a gc so that any remote references the system
11129 // processes holds on others can be released if they are
11130 // no longer needed.
11131 mH.removeMessages(H.FORCE_GC);
11132 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
11133 2000);
Romain Guy06882f82009-06-10 13:36:04 -070011134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011135 mScreenFrozenLock.release();
11136 }
Romain Guy06882f82009-06-10 13:36:04 -070011137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011138 @Override
11139 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
11140 if (mContext.checkCallingOrSelfPermission("android.permission.DUMP")
11141 != PackageManager.PERMISSION_GRANTED) {
11142 pw.println("Permission Denial: can't dump WindowManager from from pid="
11143 + Binder.getCallingPid()
11144 + ", uid=" + Binder.getCallingUid());
11145 return;
11146 }
Romain Guy06882f82009-06-10 13:36:04 -070011147
Dianne Hackborna2e92262010-03-02 17:19:29 -080011148 pw.println("Input State:");
11149 mQueue.dump(pw, " ");
11150 pw.println(" ");
11151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011152 synchronized(mWindowMap) {
11153 pw.println("Current Window Manager state:");
11154 for (int i=mWindows.size()-1; i>=0; i--) {
11155 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011156 pw.print(" Window #"); pw.print(i); pw.print(' ');
11157 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011158 w.dump(pw, " ");
11159 }
11160 if (mInputMethodDialogs.size() > 0) {
11161 pw.println(" ");
11162 pw.println(" Input method dialogs:");
11163 for (int i=mInputMethodDialogs.size()-1; i>=0; i--) {
11164 WindowState w = mInputMethodDialogs.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011165 pw.print(" IM Dialog #"); pw.print(i); pw.print(": "); pw.println(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011166 }
11167 }
11168 if (mPendingRemove.size() > 0) {
11169 pw.println(" ");
11170 pw.println(" Remove pending for:");
11171 for (int i=mPendingRemove.size()-1; i>=0; i--) {
11172 WindowState w = mPendingRemove.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011173 pw.print(" Remove #"); pw.print(i); pw.print(' ');
11174 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011175 w.dump(pw, " ");
11176 }
11177 }
11178 if (mForceRemoves != null && mForceRemoves.size() > 0) {
11179 pw.println(" ");
11180 pw.println(" Windows force removing:");
11181 for (int i=mForceRemoves.size()-1; i>=0; i--) {
11182 WindowState w = mForceRemoves.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011183 pw.print(" Removing #"); pw.print(i); pw.print(' ');
11184 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011185 w.dump(pw, " ");
11186 }
11187 }
11188 if (mDestroySurface.size() > 0) {
11189 pw.println(" ");
11190 pw.println(" Windows waiting to destroy their surface:");
11191 for (int i=mDestroySurface.size()-1; i>=0; i--) {
11192 WindowState w = mDestroySurface.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011193 pw.print(" Destroy #"); pw.print(i); pw.print(' ');
11194 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011195 w.dump(pw, " ");
11196 }
11197 }
11198 if (mLosingFocus.size() > 0) {
11199 pw.println(" ");
11200 pw.println(" Windows losing focus:");
11201 for (int i=mLosingFocus.size()-1; i>=0; i--) {
11202 WindowState w = mLosingFocus.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011203 pw.print(" Losing #"); pw.print(i); pw.print(' ');
11204 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011205 w.dump(pw, " ");
11206 }
11207 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070011208 if (mResizingWindows.size() > 0) {
11209 pw.println(" ");
11210 pw.println(" Windows waiting to resize:");
11211 for (int i=mResizingWindows.size()-1; i>=0; i--) {
11212 WindowState w = mResizingWindows.get(i);
11213 pw.print(" Resizing #"); pw.print(i); pw.print(' ');
11214 pw.print(w); pw.println(":");
11215 w.dump(pw, " ");
11216 }
11217 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011218 if (mSessions.size() > 0) {
11219 pw.println(" ");
11220 pw.println(" All active sessions:");
11221 Iterator<Session> it = mSessions.iterator();
11222 while (it.hasNext()) {
11223 Session s = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011224 pw.print(" Session "); pw.print(s); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011225 s.dump(pw, " ");
11226 }
11227 }
11228 if (mTokenMap.size() > 0) {
11229 pw.println(" ");
11230 pw.println(" All tokens:");
11231 Iterator<WindowToken> it = mTokenMap.values().iterator();
11232 while (it.hasNext()) {
11233 WindowToken token = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011234 pw.print(" Token "); pw.print(token.token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011235 token.dump(pw, " ");
11236 }
11237 }
11238 if (mTokenList.size() > 0) {
11239 pw.println(" ");
11240 pw.println(" Window token list:");
11241 for (int i=0; i<mTokenList.size(); i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011242 pw.print(" #"); pw.print(i); pw.print(": ");
11243 pw.println(mTokenList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011244 }
11245 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070011246 if (mWallpaperTokens.size() > 0) {
11247 pw.println(" ");
11248 pw.println(" Wallpaper tokens:");
11249 for (int i=mWallpaperTokens.size()-1; i>=0; i--) {
11250 WindowToken token = mWallpaperTokens.get(i);
11251 pw.print(" Wallpaper #"); pw.print(i);
11252 pw.print(' '); pw.print(token); pw.println(':');
11253 token.dump(pw, " ");
11254 }
11255 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011256 if (mAppTokens.size() > 0) {
11257 pw.println(" ");
11258 pw.println(" Application tokens in Z order:");
11259 for (int i=mAppTokens.size()-1; i>=0; i--) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011260 pw.print(" App #"); pw.print(i); pw.print(": ");
11261 pw.println(mAppTokens.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011262 }
11263 }
11264 if (mFinishedStarting.size() > 0) {
11265 pw.println(" ");
11266 pw.println(" Finishing start of application tokens:");
11267 for (int i=mFinishedStarting.size()-1; i>=0; i--) {
11268 WindowToken token = mFinishedStarting.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011269 pw.print(" Finished Starting #"); pw.print(i);
11270 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011271 token.dump(pw, " ");
11272 }
11273 }
11274 if (mExitingTokens.size() > 0) {
11275 pw.println(" ");
11276 pw.println(" Exiting tokens:");
11277 for (int i=mExitingTokens.size()-1; i>=0; i--) {
11278 WindowToken token = mExitingTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011279 pw.print(" Exiting #"); pw.print(i);
11280 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011281 token.dump(pw, " ");
11282 }
11283 }
11284 if (mExitingAppTokens.size() > 0) {
11285 pw.println(" ");
11286 pw.println(" Exiting application tokens:");
11287 for (int i=mExitingAppTokens.size()-1; i>=0; i--) {
11288 WindowToken token = mExitingAppTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011289 pw.print(" Exiting App #"); pw.print(i);
11290 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011291 token.dump(pw, " ");
11292 }
11293 }
11294 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011295 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
11296 pw.print(" mLastFocus="); pw.println(mLastFocus);
11297 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
11298 pw.print(" mInputMethodTarget="); pw.println(mInputMethodTarget);
11299 pw.print(" mInputMethodWindow="); pw.println(mInputMethodWindow);
Dianne Hackbornf21adf62009-08-13 10:20:21 -070011300 pw.print(" mWallpaperTarget="); pw.println(mWallpaperTarget);
Dianne Hackborn284ac932009-08-28 10:34:25 -070011301 if (mLowerWallpaperTarget != null && mUpperWallpaperTarget != null) {
11302 pw.print(" mLowerWallpaperTarget="); pw.println(mLowerWallpaperTarget);
11303 pw.print(" mUpperWallpaperTarget="); pw.println(mUpperWallpaperTarget);
11304 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -080011305 pw.print(" mCurConfiguration="); pw.println(this.mCurConfiguration);
11306 pw.print(" mInTouchMode="); pw.print(mInTouchMode);
11307 pw.print(" mLayoutSeq="); pw.println(mLayoutSeq);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011308 pw.print(" mSystemBooted="); pw.print(mSystemBooted);
11309 pw.print(" mDisplayEnabled="); pw.println(mDisplayEnabled);
11310 pw.print(" mLayoutNeeded="); pw.print(mLayoutNeeded);
11311 pw.print(" mBlurShown="); pw.println(mBlurShown);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011312 if (mDimAnimator != null) {
11313 mDimAnimator.printTo(pw);
11314 } else {
Dianne Hackborna2e92262010-03-02 17:19:29 -080011315 pw.println( " no DimAnimator ");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011316 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011317 pw.print(" mInputMethodAnimLayerAdjustment=");
Dianne Hackborn759a39e2009-08-09 17:20:27 -070011318 pw.print(mInputMethodAnimLayerAdjustment);
11319 pw.print(" mWallpaperAnimLayerAdjustment=");
11320 pw.println(mWallpaperAnimLayerAdjustment);
Dianne Hackborn284ac932009-08-28 10:34:25 -070011321 pw.print(" mLastWallpaperX="); pw.print(mLastWallpaperX);
11322 pw.print(" mLastWallpaperY="); pw.println(mLastWallpaperY);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011323 pw.print(" mDisplayFrozen="); pw.print(mDisplayFrozen);
11324 pw.print(" mWindowsFreezingScreen="); pw.print(mWindowsFreezingScreen);
Dianne Hackborne36d6e22010-02-17 19:46:25 -080011325 pw.print(" mAppsFreezingScreen="); pw.print(mAppsFreezingScreen);
11326 pw.print(" mWaitingForConfig="); pw.println(mWaitingForConfig);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011327 pw.print(" mRotation="); pw.print(mRotation);
11328 pw.print(", mForcedAppOrientation="); pw.print(mForcedAppOrientation);
11329 pw.print(", mRequestedRotation="); pw.println(mRequestedRotation);
11330 pw.print(" mAnimationPending="); pw.print(mAnimationPending);
11331 pw.print(" mWindowAnimationScale="); pw.print(mWindowAnimationScale);
11332 pw.print(" mTransitionWindowAnimationScale="); pw.println(mTransitionAnimationScale);
11333 pw.print(" mNextAppTransition=0x");
11334 pw.print(Integer.toHexString(mNextAppTransition));
11335 pw.print(", mAppTransitionReady="); pw.print(mAppTransitionReady);
Dianne Hackborna8f60182009-09-01 19:01:50 -070011336 pw.print(", mAppTransitionRunning="); pw.print(mAppTransitionRunning);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011337 pw.print(", mAppTransitionTimeout="); pw.println( mAppTransitionTimeout);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070011338 if (mNextAppTransitionPackage != null) {
11339 pw.print(" mNextAppTransitionPackage=");
11340 pw.print(mNextAppTransitionPackage);
11341 pw.print(", mNextAppTransitionEnter=0x");
11342 pw.print(Integer.toHexString(mNextAppTransitionEnter));
11343 pw.print(", mNextAppTransitionExit=0x");
11344 pw.print(Integer.toHexString(mNextAppTransitionExit));
11345 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011346 pw.print(" mStartingIconInTransition="); pw.print(mStartingIconInTransition);
11347 pw.print(", mSkipAppTransitionAnimation="); pw.println(mSkipAppTransitionAnimation);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070011348 if (mLastEnterAnimToken != null || mLastEnterAnimToken != null) {
11349 pw.print(" mLastEnterAnimToken="); pw.print(mLastEnterAnimToken);
11350 pw.print(", mLastEnterAnimParams="); pw.println(mLastEnterAnimParams);
11351 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011352 if (mOpeningApps.size() > 0) {
11353 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
11354 }
11355 if (mClosingApps.size() > 0) {
11356 pw.print(" mClosingApps="); pw.println(mClosingApps);
11357 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070011358 if (mToTopApps.size() > 0) {
11359 pw.print(" mToTopApps="); pw.println(mToTopApps);
11360 }
11361 if (mToBottomApps.size() > 0) {
11362 pw.print(" mToBottomApps="); pw.println(mToBottomApps);
11363 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011364 pw.print(" DisplayWidth="); pw.print(mDisplay.getWidth());
11365 pw.print(" DisplayHeight="); pw.println(mDisplay.getHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011366 pw.println(" KeyWaiter state:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011367 pw.print(" mLastWin="); pw.print(mKeyWaiter.mLastWin);
11368 pw.print(" mLastBinder="); pw.println(mKeyWaiter.mLastBinder);
11369 pw.print(" mFinished="); pw.print(mKeyWaiter.mFinished);
11370 pw.print(" mGotFirstWindow="); pw.print(mKeyWaiter.mGotFirstWindow);
11371 pw.print(" mEventDispatching="); pw.print(mKeyWaiter.mEventDispatching);
11372 pw.print(" mTimeToSwitch="); pw.println(mKeyWaiter.mTimeToSwitch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011373 }
11374 }
11375
11376 public void monitor() {
11377 synchronized (mWindowMap) { }
Mike Lockwood983ee092009-11-22 01:42:24 -050011378 synchronized (mKeyguardTokenWatcher) { }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011379 synchronized (mKeyWaiter) { }
11380 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011381
Dianne Hackbornddca3ee2009-07-23 19:01:31 -070011382 public void virtualKeyFeedback(KeyEvent event) {
11383 mPolicy.keyFeedbackFromInput(event);
11384 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080011385
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011386 /**
11387 * DimAnimator class that controls the dim animation. This holds the surface and
Doug Zongkerab5c49c2009-12-04 10:31:43 -080011388 * all state used for dim animation.
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011389 */
11390 private static class DimAnimator {
11391 Surface mDimSurface;
11392 boolean mDimShown = false;
11393 float mDimCurrentAlpha;
11394 float mDimTargetAlpha;
11395 float mDimDeltaPerMs;
11396 long mLastDimAnimTime;
Dianne Hackbornf83c5552010-03-31 22:19:32 -070011397
11398 int mLastDimWidth, mLastDimHeight;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011399
11400 DimAnimator (SurfaceSession session) {
11401 if (mDimSurface == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011402 if (SHOW_TRANSACTIONS) Slog.i(TAG, " DIM "
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011403 + mDimSurface + ": CREATE");
11404 try {
Mathias Agopian5d26c1e2010-03-01 16:09:43 -080011405 mDimSurface = new Surface(session, 0,
11406 "DimSurface",
11407 -1, 16, 16, PixelFormat.OPAQUE,
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011408 Surface.FX_SURFACE_DIM);
Maciej Białka9ee5c222010-03-24 10:25:40 +010011409 mDimSurface.setAlpha(0.0f);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011410 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011411 Slog.e(TAG, "Exception creating Dim surface", e);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011412 }
11413 }
11414 }
11415
11416 /**
11417 * Show the dim surface.
11418 */
11419 void show(int dw, int dh) {
Dianne Hackborn16064f92010-03-25 00:47:24 -070011420 if (!mDimShown) {
11421 if (SHOW_TRANSACTIONS) Slog.i(TAG, " DIM " + mDimSurface + ": SHOW pos=(0,0) (" +
11422 dw + "x" + dh + ")");
11423 mDimShown = true;
11424 try {
Dianne Hackbornf83c5552010-03-31 22:19:32 -070011425 mLastDimWidth = dw;
11426 mLastDimHeight = dh;
Dianne Hackborn16064f92010-03-25 00:47:24 -070011427 mDimSurface.setPosition(0, 0);
11428 mDimSurface.setSize(dw, dh);
11429 mDimSurface.show();
11430 } catch (RuntimeException e) {
11431 Slog.w(TAG, "Failure showing dim surface", e);
11432 }
Dianne Hackbornf83c5552010-03-31 22:19:32 -070011433 } else if (mLastDimWidth != dw || mLastDimHeight != dh) {
11434 mLastDimWidth = dw;
11435 mLastDimHeight = dh;
11436 mDimSurface.setSize(dw, dh);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011437 }
11438 }
11439
11440 /**
11441 * Set's the dim surface's layer and update dim parameters that will be used in
11442 * {@link updateSurface} after all windows are examined.
11443 */
11444 void updateParameters(WindowState w, long currentTime) {
11445 mDimSurface.setLayer(w.mAnimLayer-1);
11446
11447 final float target = w.mExiting ? 0 : w.mAttrs.dimAmount;
Joe Onorato8a9b2202010-02-26 18:56:32 -080011448 if (SHOW_TRANSACTIONS) Slog.i(TAG, " DIM " + mDimSurface
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070011449 + ": layer=" + (w.mAnimLayer-1) + " target=" + target);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011450 if (mDimTargetAlpha != target) {
11451 // If the desired dim level has changed, then
11452 // start an animation to it.
11453 mLastDimAnimTime = currentTime;
11454 long duration = (w.mAnimating && w.mAnimation != null)
11455 ? w.mAnimation.computeDurationHint()
11456 : DEFAULT_DIM_DURATION;
11457 if (target > mDimTargetAlpha) {
11458 // This is happening behind the activity UI,
11459 // so we can make it run a little longer to
11460 // give a stronger impression without disrupting
11461 // the user.
11462 duration *= DIM_DURATION_MULTIPLIER;
11463 }
11464 if (duration < 1) {
11465 // Don't divide by zero
11466 duration = 1;
11467 }
11468 mDimTargetAlpha = target;
11469 mDimDeltaPerMs = (mDimTargetAlpha-mDimCurrentAlpha) / duration;
11470 }
11471 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080011472
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011473 /**
11474 * Updating the surface's alpha. Returns true if the animation continues, or returns
11475 * false when the animation is finished and the dim surface is hidden.
11476 */
11477 boolean updateSurface(boolean dimming, long currentTime, boolean displayFrozen) {
11478 if (!dimming) {
11479 if (mDimTargetAlpha != 0) {
11480 mLastDimAnimTime = currentTime;
11481 mDimTargetAlpha = 0;
11482 mDimDeltaPerMs = (-mDimCurrentAlpha) / DEFAULT_DIM_DURATION;
11483 }
11484 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080011485
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011486 boolean animating = false;
11487 if (mLastDimAnimTime != 0) {
11488 mDimCurrentAlpha += mDimDeltaPerMs
11489 * (currentTime-mLastDimAnimTime);
11490 boolean more = true;
11491 if (displayFrozen) {
11492 // If the display is frozen, there is no reason to animate.
11493 more = false;
11494 } else if (mDimDeltaPerMs > 0) {
11495 if (mDimCurrentAlpha > mDimTargetAlpha) {
11496 more = false;
11497 }
11498 } else if (mDimDeltaPerMs < 0) {
11499 if (mDimCurrentAlpha < mDimTargetAlpha) {
11500 more = false;
11501 }
11502 } else {
11503 more = false;
11504 }
11505
11506 // Do we need to continue animating?
11507 if (more) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011508 if (SHOW_TRANSACTIONS) Slog.i(TAG, " DIM "
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011509 + mDimSurface + ": alpha=" + mDimCurrentAlpha);
11510 mLastDimAnimTime = currentTime;
11511 mDimSurface.setAlpha(mDimCurrentAlpha);
11512 animating = true;
11513 } else {
11514 mDimCurrentAlpha = mDimTargetAlpha;
11515 mLastDimAnimTime = 0;
Joe Onorato8a9b2202010-02-26 18:56:32 -080011516 if (SHOW_TRANSACTIONS) Slog.i(TAG, " DIM "
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011517 + mDimSurface + ": final alpha=" + mDimCurrentAlpha);
11518 mDimSurface.setAlpha(mDimCurrentAlpha);
11519 if (!dimming) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011520 if (SHOW_TRANSACTIONS) Slog.i(TAG, " DIM " + mDimSurface
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011521 + ": HIDE");
11522 try {
11523 mDimSurface.hide();
11524 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011525 Slog.w(TAG, "Illegal argument exception hiding dim surface");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011526 }
11527 mDimShown = false;
11528 }
11529 }
11530 }
11531 return animating;
11532 }
11533
11534 public void printTo(PrintWriter pw) {
11535 pw.print(" mDimShown="); pw.print(mDimShown);
11536 pw.print(" current="); pw.print(mDimCurrentAlpha);
11537 pw.print(" target="); pw.print(mDimTargetAlpha);
11538 pw.print(" delta="); pw.print(mDimDeltaPerMs);
11539 pw.print(" lastAnimTime="); pw.println(mLastDimAnimTime);
11540 }
11541 }
11542
11543 /**
11544 * Animation that fade in after 0.5 interpolate time, or fade out in reverse order.
11545 * This is used for opening/closing transition for apps in compatible mode.
11546 */
11547 private static class FadeInOutAnimation extends Animation {
11548 int mWidth;
11549 boolean mFadeIn;
11550
11551 public FadeInOutAnimation(boolean fadeIn) {
11552 setInterpolator(new AccelerateInterpolator());
11553 setDuration(DEFAULT_FADE_IN_OUT_DURATION);
11554 mFadeIn = fadeIn;
11555 }
11556
11557 @Override
11558 protected void applyTransformation(float interpolatedTime, Transformation t) {
11559 float x = interpolatedTime;
11560 if (!mFadeIn) {
11561 x = 1.0f - x; // reverse the interpolation for fade out
11562 }
11563 if (x < 0.5) {
11564 // move the window out of the screen.
11565 t.getMatrix().setTranslate(mWidth, 0);
11566 } else {
11567 t.getMatrix().setTranslate(0, 0);// show
11568 t.setAlpha((x - 0.5f) * 2);
11569 }
11570 }
11571
11572 @Override
11573 public void initialize(int width, int height, int parentWidth, int parentHeight) {
11574 // width is the screen width {@see AppWindowToken#stepAnimatinoLocked}
11575 mWidth = width;
11576 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011577
11578 @Override
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -070011579 public int getZAdjustment() {
11580 return Animation.ZORDER_TOP;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011581 }
11582 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011583}