blob: 68787cd07ab4b2b077adb379131f9b5622e8c151 [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 &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001504 (wb.mAttrs.type != TYPE_APPLICATION_STARTING ||
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001505 wb.mToken != foundW.mToken)) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001506 // This window is not related to the previous one in any
1507 // interesting way, so stop here.
1508 break;
1509 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001510 foundW = wb;
1511 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001512 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001513 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001514 if (DEBUG_WALLPAPER) Slog.v(TAG, "No wallpaper target");
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001515 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001516
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001517 if (foundW == null && topCurW != null) {
1518 // There is no wallpaper target, so it goes at the bottom.
1519 // We will assume it is the same place as last time, if known.
1520 foundW = topCurW;
1521 foundI = topCurI+1;
1522 } else {
1523 // Okay i is the position immediately above the wallpaper. Look at
1524 // what is below it for later.
1525 foundW = foundI > 0 ? (WindowState)localmWindows.get(foundI-1) : null;
1526 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001527
Dianne Hackborn284ac932009-08-28 10:34:25 -07001528 if (visible) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001529 if (mWallpaperTarget.mWallpaperX >= 0) {
1530 mLastWallpaperX = mWallpaperTarget.mWallpaperX;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001531 mLastWallpaperXStep = mWallpaperTarget.mWallpaperXStep;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001532 }
1533 if (mWallpaperTarget.mWallpaperY >= 0) {
1534 mLastWallpaperY = mWallpaperTarget.mWallpaperY;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001535 mLastWallpaperYStep = mWallpaperTarget.mWallpaperYStep;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001536 }
Dianne Hackborn284ac932009-08-28 10:34:25 -07001537 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001538
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001539 // Start stepping backwards from here, ensuring that our wallpaper windows
1540 // are correctly placed.
1541 int curTokenIndex = mWallpaperTokens.size();
1542 while (curTokenIndex > 0) {
1543 curTokenIndex--;
1544 WindowToken token = mWallpaperTokens.get(curTokenIndex);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001545 if (token.hidden == visible) {
1546 changed |= ADJUST_WALLPAPER_VISIBILITY_CHANGED;
1547 token.hidden = !visible;
1548 // Need to do a layout to ensure the wallpaper now has the
1549 // correct size.
1550 mLayoutNeeded = true;
1551 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001552
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001553 int curWallpaperIndex = token.windows.size();
1554 while (curWallpaperIndex > 0) {
1555 curWallpaperIndex--;
1556 WindowState wallpaper = token.windows.get(curWallpaperIndex);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001557
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001558 if (visible) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001559 updateWallpaperOffsetLocked(wallpaper, dw, dh, false);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001560 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001561
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001562 // First, make sure the client has the current visibility
1563 // state.
1564 if (wallpaper.mWallpaperVisible != visible) {
1565 wallpaper.mWallpaperVisible = visible;
1566 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001567 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001568 "Setting visibility of wallpaper " + wallpaper
1569 + ": " + visible);
1570 wallpaper.mClient.dispatchAppVisibility(visible);
1571 } catch (RemoteException e) {
1572 }
1573 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001574
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001575 wallpaper.mAnimLayer = wallpaper.mLayer + mWallpaperAnimLayerAdjustment;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001576 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Slog.v(TAG, "Wallpaper win "
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001577 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001578
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001579 // First, if this window is at the current index, then all
1580 // is well.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001581 if (wallpaper == foundW) {
1582 foundI--;
1583 foundW = foundI > 0
1584 ? (WindowState)localmWindows.get(foundI-1) : null;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001585 continue;
1586 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001587
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001588 // The window didn't match... the current wallpaper window,
1589 // wherever it is, is in the wrong place, so make sure it is
1590 // not in the list.
1591 int oldIndex = localmWindows.indexOf(wallpaper);
1592 if (oldIndex >= 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001593 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "Wallpaper removing at "
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001594 + oldIndex + ": " + wallpaper);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001595 localmWindows.remove(oldIndex);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001596 if (oldIndex < foundI) {
1597 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001598 }
1599 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001600
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001601 // Now stick it in.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001602 if (DEBUG_WALLPAPER || DEBUG_WINDOW_MOVEMENT) Slog.v(TAG,
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001603 "Moving wallpaper " + wallpaper
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001604 + " from " + oldIndex + " to " + foundI);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001605
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001606 localmWindows.add(foundI, wallpaper);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001607 changed |= ADJUST_WALLPAPER_LAYERS_CHANGED;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001608 }
1609 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001610
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001611 return changed;
1612 }
1613
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001614 void setWallpaperAnimLayerAdjustmentLocked(int adj) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001615 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001616 "Setting wallpaper layer adj to " + adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001617 mWallpaperAnimLayerAdjustment = adj;
1618 int curTokenIndex = mWallpaperTokens.size();
1619 while (curTokenIndex > 0) {
1620 curTokenIndex--;
1621 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1622 int curWallpaperIndex = token.windows.size();
1623 while (curWallpaperIndex > 0) {
1624 curWallpaperIndex--;
1625 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1626 wallpaper.mAnimLayer = wallpaper.mLayer + adj;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001627 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Slog.v(TAG, "Wallpaper win "
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001628 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001629 }
1630 }
1631 }
1632
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001633 boolean updateWallpaperOffsetLocked(WindowState wallpaperWin, int dw, int dh,
1634 boolean sync) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001635 boolean changed = false;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001636 boolean rawChanged = false;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001637 float wpx = mLastWallpaperX >= 0 ? mLastWallpaperX : 0.5f;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001638 float wpxs = mLastWallpaperXStep >= 0 ? mLastWallpaperXStep : -1.0f;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001639 int availw = wallpaperWin.mFrame.right-wallpaperWin.mFrame.left-dw;
1640 int offset = availw > 0 ? -(int)(availw*wpx+.5f) : 0;
1641 changed = wallpaperWin.mXOffset != offset;
1642 if (changed) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001643 if (DEBUG_WALLPAPER) Slog.v(TAG, "Update wallpaper "
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001644 + wallpaperWin + " x: " + offset);
1645 wallpaperWin.mXOffset = offset;
1646 }
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001647 if (wallpaperWin.mWallpaperX != wpx || wallpaperWin.mWallpaperXStep != wpxs) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001648 wallpaperWin.mWallpaperX = wpx;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001649 wallpaperWin.mWallpaperXStep = wpxs;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001650 rawChanged = true;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001651 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001652
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001653 float wpy = mLastWallpaperY >= 0 ? mLastWallpaperY : 0.5f;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001654 float wpys = mLastWallpaperYStep >= 0 ? mLastWallpaperYStep : -1.0f;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001655 int availh = wallpaperWin.mFrame.bottom-wallpaperWin.mFrame.top-dh;
1656 offset = availh > 0 ? -(int)(availh*wpy+.5f) : 0;
1657 if (wallpaperWin.mYOffset != offset) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001658 if (DEBUG_WALLPAPER) Slog.v(TAG, "Update wallpaper "
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001659 + wallpaperWin + " y: " + offset);
1660 changed = true;
1661 wallpaperWin.mYOffset = offset;
1662 }
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001663 if (wallpaperWin.mWallpaperY != wpy || wallpaperWin.mWallpaperYStep != wpys) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001664 wallpaperWin.mWallpaperY = wpy;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001665 wallpaperWin.mWallpaperYStep = wpys;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001666 rawChanged = true;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001667 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001668
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001669 if (rawChanged) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001670 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001671 if (DEBUG_WALLPAPER) Slog.v(TAG, "Report new wp offset "
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001672 + wallpaperWin + " x=" + wallpaperWin.mWallpaperX
1673 + " y=" + wallpaperWin.mWallpaperY);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001674 if (sync) {
Dianne Hackborn75804932009-10-20 20:15:20 -07001675 mWaitingOnWallpaper = wallpaperWin;
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001676 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001677 wallpaperWin.mClient.dispatchWallpaperOffsets(
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001678 wallpaperWin.mWallpaperX, wallpaperWin.mWallpaperY,
1679 wallpaperWin.mWallpaperXStep, wallpaperWin.mWallpaperYStep, sync);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001680 if (sync) {
Dianne Hackborn75804932009-10-20 20:15:20 -07001681 if (mWaitingOnWallpaper != null) {
1682 long start = SystemClock.uptimeMillis();
1683 if ((mLastWallpaperTimeoutTime+WALLPAPER_TIMEOUT_RECOVERY)
1684 < start) {
1685 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001686 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn75804932009-10-20 20:15:20 -07001687 "Waiting for offset complete...");
1688 mWindowMap.wait(WALLPAPER_TIMEOUT);
1689 } catch (InterruptedException e) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001690 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001691 if (DEBUG_WALLPAPER) Slog.v(TAG, "Offset complete!");
Dianne Hackborn75804932009-10-20 20:15:20 -07001692 if ((start+WALLPAPER_TIMEOUT)
1693 < SystemClock.uptimeMillis()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001694 Slog.i(TAG, "Timeout waiting for wallpaper to offset: "
Dianne Hackborn75804932009-10-20 20:15:20 -07001695 + wallpaperWin);
1696 mLastWallpaperTimeoutTime = start;
1697 }
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001698 }
Dianne Hackborn75804932009-10-20 20:15:20 -07001699 mWaitingOnWallpaper = null;
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001700 }
1701 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001702 } catch (RemoteException e) {
1703 }
1704 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001705
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001706 return changed;
1707 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001708
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001709 void wallpaperOffsetsComplete(IBinder window) {
Dianne Hackborn75804932009-10-20 20:15:20 -07001710 synchronized (mWindowMap) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001711 if (mWaitingOnWallpaper != null &&
1712 mWaitingOnWallpaper.mClient.asBinder() == window) {
1713 mWaitingOnWallpaper = null;
Dianne Hackborn75804932009-10-20 20:15:20 -07001714 mWindowMap.notifyAll();
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001715 }
1716 }
1717 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001718
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001719 boolean updateWallpaperOffsetLocked(WindowState changingTarget, boolean sync) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001720 final int dw = mDisplay.getWidth();
1721 final int dh = mDisplay.getHeight();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001722
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001723 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001724
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001725 WindowState target = mWallpaperTarget;
1726 if (target != null) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001727 if (target.mWallpaperX >= 0) {
1728 mLastWallpaperX = target.mWallpaperX;
1729 } else if (changingTarget.mWallpaperX >= 0) {
1730 mLastWallpaperX = changingTarget.mWallpaperX;
1731 }
1732 if (target.mWallpaperY >= 0) {
1733 mLastWallpaperY = target.mWallpaperY;
1734 } else if (changingTarget.mWallpaperY >= 0) {
1735 mLastWallpaperY = changingTarget.mWallpaperY;
1736 }
1737 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001738
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001739 int curTokenIndex = mWallpaperTokens.size();
1740 while (curTokenIndex > 0) {
1741 curTokenIndex--;
1742 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1743 int curWallpaperIndex = token.windows.size();
1744 while (curWallpaperIndex > 0) {
1745 curWallpaperIndex--;
1746 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1747 if (updateWallpaperOffsetLocked(wallpaper, dw, dh, sync)) {
1748 wallpaper.computeShownFrameLocked();
1749 changed = true;
1750 // We only want to be synchronous with one wallpaper.
1751 sync = false;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001752 }
1753 }
1754 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001755
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001756 return changed;
1757 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001758
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001759 void updateWallpaperVisibilityLocked() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07001760 final boolean visible = isWallpaperVisible(mWallpaperTarget);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001761 final int dw = mDisplay.getWidth();
1762 final int dh = mDisplay.getHeight();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001763
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001764 int curTokenIndex = mWallpaperTokens.size();
1765 while (curTokenIndex > 0) {
1766 curTokenIndex--;
1767 WindowToken token = mWallpaperTokens.get(curTokenIndex);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001768 if (token.hidden == visible) {
1769 token.hidden = !visible;
1770 // Need to do a layout to ensure the wallpaper now has the
1771 // correct size.
1772 mLayoutNeeded = true;
1773 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001774
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001775 int curWallpaperIndex = token.windows.size();
1776 while (curWallpaperIndex > 0) {
1777 curWallpaperIndex--;
1778 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1779 if (visible) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001780 updateWallpaperOffsetLocked(wallpaper, dw, dh, false);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001781 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001782
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001783 if (wallpaper.mWallpaperVisible != visible) {
1784 wallpaper.mWallpaperVisible = visible;
1785 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001786 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -07001787 "Updating visibility of wallpaper " + wallpaper
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001788 + ": " + visible);
1789 wallpaper.mClient.dispatchAppVisibility(visible);
1790 } catch (RemoteException e) {
1791 }
1792 }
1793 }
1794 }
1795 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001796
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001797 void sendPointerToWallpaperLocked(WindowState srcWin,
1798 MotionEvent pointer, long eventTime) {
1799 int curTokenIndex = mWallpaperTokens.size();
1800 while (curTokenIndex > 0) {
1801 curTokenIndex--;
1802 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1803 int curWallpaperIndex = token.windows.size();
1804 while (curWallpaperIndex > 0) {
1805 curWallpaperIndex--;
1806 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1807 if ((wallpaper.mAttrs.flags &
1808 WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
1809 continue;
1810 }
1811 try {
1812 MotionEvent ev = MotionEvent.obtainNoHistory(pointer);
Dianne Hackborn6adba242009-11-10 11:10:09 -08001813 if (srcWin != null) {
1814 ev.offsetLocation(srcWin.mFrame.left-wallpaper.mFrame.left,
1815 srcWin.mFrame.top-wallpaper.mFrame.top);
1816 } else {
1817 ev.offsetLocation(-wallpaper.mFrame.left, -wallpaper.mFrame.top);
1818 }
1819 switch (pointer.getAction()) {
1820 case MotionEvent.ACTION_DOWN:
1821 mSendingPointersToWallpaper = true;
1822 break;
1823 case MotionEvent.ACTION_UP:
1824 mSendingPointersToWallpaper = false;
1825 break;
1826 }
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001827 wallpaper.mClient.dispatchPointer(ev, eventTime, false);
1828 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001829 Slog.w(TAG, "Failure sending pointer to wallpaper", e);
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001830 }
1831 }
1832 }
1833 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001834
Dianne Hackborn90d2db32010-02-11 22:19:06 -08001835 void dispatchPointerElsewhereLocked(WindowState srcWin, WindowState relWin,
1836 MotionEvent pointer, long eventTime, boolean skipped) {
1837 if (relWin != null) {
1838 mPolicy.dispatchedPointerEventLw(pointer, relWin.mFrame.left, relWin.mFrame.top);
1839 } else {
1840 mPolicy.dispatchedPointerEventLw(pointer, 0, 0);
1841 }
1842
1843 // If we sent an initial down to the wallpaper, then continue
1844 // sending events until the final up.
1845 if (mSendingPointersToWallpaper) {
1846 if (skipped) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001847 Slog.i(TAG, "Sending skipped pointer to wallpaper!");
Dianne Hackborn90d2db32010-02-11 22:19:06 -08001848 }
1849 sendPointerToWallpaperLocked(relWin, pointer, eventTime);
1850
1851 // If we are on top of the wallpaper, then the wallpaper also
1852 // gets to see this movement.
1853 } else if (srcWin != null
1854 && pointer.getAction() == MotionEvent.ACTION_DOWN
1855 && mWallpaperTarget == srcWin
1856 && srcWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
1857 sendPointerToWallpaperLocked(relWin, pointer, eventTime);
1858 }
1859 }
1860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001861 public int addWindow(Session session, IWindow client,
1862 WindowManager.LayoutParams attrs, int viewVisibility,
1863 Rect outContentInsets) {
1864 int res = mPolicy.checkAddPermission(attrs);
1865 if (res != WindowManagerImpl.ADD_OKAY) {
1866 return res;
1867 }
Romain Guy06882f82009-06-10 13:36:04 -07001868
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001869 boolean reportNewConfig = false;
1870 WindowState attachedWindow = null;
1871 WindowState win = null;
Romain Guy06882f82009-06-10 13:36:04 -07001872
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001873 synchronized(mWindowMap) {
1874 // Instantiating a Display requires talking with the simulator,
1875 // so don't do it until we know the system is mostly up and
1876 // running.
1877 if (mDisplay == null) {
1878 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
1879 mDisplay = wm.getDefaultDisplay();
Christopher Tateb696aee2010-04-02 19:08:30 -07001880 mInitialDisplayWidth = mDisplay.getWidth();
1881 mInitialDisplayHeight = mDisplay.getHeight();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001882 mQueue.setDisplay(mDisplay);
1883 reportNewConfig = true;
1884 }
Romain Guy06882f82009-06-10 13:36:04 -07001885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001886 if (mWindowMap.containsKey(client.asBinder())) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001887 Slog.w(TAG, "Window " + client + " is already added");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001888 return WindowManagerImpl.ADD_DUPLICATE_ADD;
1889 }
1890
1891 if (attrs.type >= FIRST_SUB_WINDOW && attrs.type <= LAST_SUB_WINDOW) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001892 attachedWindow = windowForClientLocked(null, attrs.token, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001893 if (attachedWindow == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001894 Slog.w(TAG, "Attempted to add window with token that is not a window: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001895 + attrs.token + ". Aborting.");
1896 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1897 }
1898 if (attachedWindow.mAttrs.type >= FIRST_SUB_WINDOW
1899 && attachedWindow.mAttrs.type <= LAST_SUB_WINDOW) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001900 Slog.w(TAG, "Attempted to add window with token that is a sub-window: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001901 + attrs.token + ". Aborting.");
1902 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1903 }
1904 }
1905
1906 boolean addToken = false;
1907 WindowToken token = mTokenMap.get(attrs.token);
1908 if (token == null) {
1909 if (attrs.type >= FIRST_APPLICATION_WINDOW
1910 && attrs.type <= LAST_APPLICATION_WINDOW) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001911 Slog.w(TAG, "Attempted to add application window with unknown token "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001912 + attrs.token + ". Aborting.");
1913 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1914 }
1915 if (attrs.type == TYPE_INPUT_METHOD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001916 Slog.w(TAG, "Attempted to add input method window with unknown token "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001917 + attrs.token + ". Aborting.");
1918 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1919 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001920 if (attrs.type == TYPE_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001921 Slog.w(TAG, "Attempted to add wallpaper window with unknown token "
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001922 + attrs.token + ". Aborting.");
1923 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1924 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001925 token = new WindowToken(attrs.token, -1, false);
1926 addToken = true;
1927 } else if (attrs.type >= FIRST_APPLICATION_WINDOW
1928 && attrs.type <= LAST_APPLICATION_WINDOW) {
1929 AppWindowToken atoken = token.appWindowToken;
1930 if (atoken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001931 Slog.w(TAG, "Attempted to add window with non-application token "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001932 + token + ". Aborting.");
1933 return WindowManagerImpl.ADD_NOT_APP_TOKEN;
1934 } else if (atoken.removed) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001935 Slog.w(TAG, "Attempted to add window with exiting application token "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001936 + token + ". Aborting.");
1937 return WindowManagerImpl.ADD_APP_EXITING;
1938 }
1939 if (attrs.type == TYPE_APPLICATION_STARTING && atoken.firstWindowDrawn) {
1940 // No need for this guy!
Joe Onorato8a9b2202010-02-26 18:56:32 -08001941 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001942 TAG, "**** NO NEED TO START: " + attrs.getTitle());
1943 return WindowManagerImpl.ADD_STARTING_NOT_NEEDED;
1944 }
1945 } else if (attrs.type == TYPE_INPUT_METHOD) {
1946 if (token.windowType != TYPE_INPUT_METHOD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001947 Slog.w(TAG, "Attempted to add input method window with bad token "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001948 + attrs.token + ". Aborting.");
1949 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1950 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001951 } else if (attrs.type == TYPE_WALLPAPER) {
1952 if (token.windowType != TYPE_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001953 Slog.w(TAG, "Attempted to add wallpaper window with bad token "
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001954 + attrs.token + ". Aborting.");
1955 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1956 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001957 }
1958
1959 win = new WindowState(session, client, token,
1960 attachedWindow, attrs, viewVisibility);
1961 if (win.mDeathRecipient == null) {
1962 // Client has apparently died, so there is no reason to
1963 // continue.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001964 Slog.w(TAG, "Adding window client " + client.asBinder()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001965 + " that is dead, aborting.");
1966 return WindowManagerImpl.ADD_APP_EXITING;
1967 }
1968
1969 mPolicy.adjustWindowParamsLw(win.mAttrs);
Romain Guy06882f82009-06-10 13:36:04 -07001970
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001971 res = mPolicy.prepareAddWindowLw(win, attrs);
1972 if (res != WindowManagerImpl.ADD_OKAY) {
1973 return res;
1974 }
1975
1976 // From now on, no exceptions or errors allowed!
1977
1978 res = WindowManagerImpl.ADD_OKAY;
Romain Guy06882f82009-06-10 13:36:04 -07001979
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001980 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001981
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001982 if (addToken) {
1983 mTokenMap.put(attrs.token, token);
1984 mTokenList.add(token);
1985 }
1986 win.attach();
1987 mWindowMap.put(client.asBinder(), win);
1988
1989 if (attrs.type == TYPE_APPLICATION_STARTING &&
1990 token.appWindowToken != null) {
1991 token.appWindowToken.startingWindow = win;
1992 }
1993
1994 boolean imMayMove = true;
Romain Guy06882f82009-06-10 13:36:04 -07001995
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001996 if (attrs.type == TYPE_INPUT_METHOD) {
1997 mInputMethodWindow = win;
1998 addInputMethodWindowToListLocked(win);
1999 imMayMove = false;
2000 } else if (attrs.type == TYPE_INPUT_METHOD_DIALOG) {
2001 mInputMethodDialogs.add(win);
2002 addWindowToListInOrderLocked(win, true);
2003 adjustInputMethodDialogsLocked();
2004 imMayMove = false;
2005 } else {
2006 addWindowToListInOrderLocked(win, true);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002007 if (attrs.type == TYPE_WALLPAPER) {
2008 mLastWallpaperTimeoutTime = 0;
2009 adjustWallpaperWindowsLocked();
2010 } else if ((attrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002011 adjustWallpaperWindowsLocked();
2012 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002013 }
Romain Guy06882f82009-06-10 13:36:04 -07002014
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002015 win.mEnterAnimationPending = true;
Romain Guy06882f82009-06-10 13:36:04 -07002016
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002017 mPolicy.getContentInsetHintLw(attrs, outContentInsets);
Romain Guy06882f82009-06-10 13:36:04 -07002018
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002019 if (mInTouchMode) {
2020 res |= WindowManagerImpl.ADD_FLAG_IN_TOUCH_MODE;
2021 }
2022 if (win == null || win.mAppToken == null || !win.mAppToken.clientHidden) {
2023 res |= WindowManagerImpl.ADD_FLAG_APP_VISIBLE;
2024 }
Romain Guy06882f82009-06-10 13:36:04 -07002025
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08002026 boolean focusChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002027 if (win.canReceiveKeys()) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08002028 if ((focusChanged=updateFocusedWindowLocked(UPDATE_FOCUS_WILL_ASSIGN_LAYERS))
2029 == true) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002030 imMayMove = false;
2031 }
2032 }
Romain Guy06882f82009-06-10 13:36:04 -07002033
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002034 if (imMayMove) {
Romain Guy06882f82009-06-10 13:36:04 -07002035 moveInputMethodWindowsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002036 }
Romain Guy06882f82009-06-10 13:36:04 -07002037
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002038 assignLayersLocked();
2039 // Don't do layout here, the window must call
2040 // relayout to be displayed, so we'll do it there.
Romain Guy06882f82009-06-10 13:36:04 -07002041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002042 //dump();
2043
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08002044 if (focusChanged) {
2045 if (mCurrentFocus != null) {
2046 mKeyWaiter.handleNewWindowLocked(mCurrentFocus);
2047 }
2048 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08002049 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002050 TAG, "New client " + client.asBinder()
2051 + ": window=" + win);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002052
2053 if (win.isVisibleOrAdding() && updateOrientationFromAppTokensLocked()) {
2054 reportNewConfig = true;
2055 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002056 }
2057
2058 // sendNewConfiguration() checks caller permissions so we must call it with
2059 // privilege. updateOrientationFromAppTokens() clears and resets the caller
2060 // identity anyway, so it's safe to just clear & restore around this whole
2061 // block.
2062 final long origId = Binder.clearCallingIdentity();
2063 if (reportNewConfig) {
2064 sendNewConfiguration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002065 }
2066 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07002067
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002068 return res;
2069 }
Romain Guy06882f82009-06-10 13:36:04 -07002070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002071 public void removeWindow(Session session, IWindow client) {
2072 synchronized(mWindowMap) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002073 WindowState win = windowForClientLocked(session, client, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002074 if (win == null) {
2075 return;
2076 }
2077 removeWindowLocked(session, win);
2078 }
2079 }
Romain Guy06882f82009-06-10 13:36:04 -07002080
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002081 public void removeWindowLocked(Session session, WindowState win) {
2082
Joe Onorato8a9b2202010-02-26 18:56:32 -08002083 if (localLOGV || DEBUG_FOCUS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002084 TAG, "Remove " + win + " client="
2085 + Integer.toHexString(System.identityHashCode(
2086 win.mClient.asBinder()))
2087 + ", surface=" + win.mSurface);
2088
2089 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002090
Joe Onorato8a9b2202010-02-26 18:56:32 -08002091 if (DEBUG_APP_TRANSITIONS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002092 TAG, "Remove " + win + ": mSurface=" + win.mSurface
2093 + " mExiting=" + win.mExiting
2094 + " isAnimating=" + win.isAnimating()
2095 + " app-animation="
2096 + (win.mAppToken != null ? win.mAppToken.animation : null)
2097 + " inPendingTransaction="
2098 + (win.mAppToken != null ? win.mAppToken.inPendingTransaction : false)
2099 + " mDisplayFrozen=" + mDisplayFrozen);
2100 // Visibility of the removed window. Will be used later to update orientation later on.
2101 boolean wasVisible = false;
2102 // First, see if we need to run an animation. If we do, we have
2103 // to hold off on removing the window until the animation is done.
2104 // If the display is frozen, just remove immediately, since the
2105 // animation wouldn't be seen.
Dianne Hackbornde2606d2009-12-18 16:53:55 -08002106 if (win.mSurface != null && !mDisplayFrozen && mPolicy.isScreenOn()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002107 // If we are not currently running the exit animation, we
2108 // need to see about starting one.
2109 if (wasVisible=win.isWinVisibleLw()) {
Romain Guy06882f82009-06-10 13:36:04 -07002110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002111 int transit = WindowManagerPolicy.TRANSIT_EXIT;
2112 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
2113 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
2114 }
2115 // Try starting an animation.
2116 if (applyAnimationLocked(win, transit, false)) {
2117 win.mExiting = true;
2118 }
2119 }
2120 if (win.mExiting || win.isAnimating()) {
2121 // The exit animation is running... wait for it!
Joe Onorato8a9b2202010-02-26 18:56:32 -08002122 //Slog.i(TAG, "*** Running exit animation...");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002123 win.mExiting = true;
2124 win.mRemoveOnExit = true;
2125 mLayoutNeeded = true;
2126 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
2127 performLayoutAndPlaceSurfacesLocked();
2128 if (win.mAppToken != null) {
2129 win.mAppToken.updateReportedVisibilityLocked();
2130 }
2131 //dump();
2132 Binder.restoreCallingIdentity(origId);
2133 return;
2134 }
2135 }
2136
2137 removeWindowInnerLocked(session, win);
2138 // Removing a visible window will effect the computed orientation
2139 // So just update orientation if needed.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002140 if (wasVisible && computeForcedAppOrientationLocked()
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002141 != mForcedAppOrientation
2142 && updateOrientationFromAppTokensLocked()) {
2143 mH.sendEmptyMessage(H.SEND_NEW_CONFIGURATION);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002144 }
2145 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2146 Binder.restoreCallingIdentity(origId);
2147 }
Romain Guy06882f82009-06-10 13:36:04 -07002148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002149 private void removeWindowInnerLocked(Session session, WindowState win) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07002150 mKeyWaiter.finishedKey(session, win.mClient, true,
2151 KeyWaiter.RETURN_NOTHING);
Maciej Białka26824b72010-03-05 08:40:09 +01002152 mKeyWaiter.releaseMotionTarget(win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002153 mKeyWaiter.releasePendingPointerLocked(win.mSession);
2154 mKeyWaiter.releasePendingTrackballLocked(win.mSession);
Romain Guy06882f82009-06-10 13:36:04 -07002155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002156 win.mRemoved = true;
Romain Guy06882f82009-06-10 13:36:04 -07002157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002158 if (mInputMethodTarget == win) {
2159 moveInputMethodWindowsIfNeededLocked(false);
2160 }
Romain Guy06882f82009-06-10 13:36:04 -07002161
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07002162 if (false) {
2163 RuntimeException e = new RuntimeException("here");
2164 e.fillInStackTrace();
Joe Onorato8a9b2202010-02-26 18:56:32 -08002165 Slog.w(TAG, "Removing window " + win, e);
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07002166 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002168 mPolicy.removeWindowLw(win);
2169 win.removeLocked();
2170
2171 mWindowMap.remove(win.mClient.asBinder());
2172 mWindows.remove(win);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002173 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "Final remove of window: " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002174
2175 if (mInputMethodWindow == win) {
2176 mInputMethodWindow = null;
2177 } else if (win.mAttrs.type == TYPE_INPUT_METHOD_DIALOG) {
2178 mInputMethodDialogs.remove(win);
2179 }
Romain Guy06882f82009-06-10 13:36:04 -07002180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002181 final WindowToken token = win.mToken;
2182 final AppWindowToken atoken = win.mAppToken;
2183 token.windows.remove(win);
2184 if (atoken != null) {
2185 atoken.allAppWindows.remove(win);
2186 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08002187 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002188 TAG, "**** Removing window " + win + ": count="
2189 + token.windows.size());
2190 if (token.windows.size() == 0) {
2191 if (!token.explicit) {
2192 mTokenMap.remove(token.token);
2193 mTokenList.remove(token);
2194 } else if (atoken != null) {
2195 atoken.firstWindowDrawn = false;
2196 }
2197 }
2198
2199 if (atoken != null) {
2200 if (atoken.startingWindow == win) {
2201 atoken.startingWindow = null;
2202 } else if (atoken.allAppWindows.size() == 0 && atoken.startingData != null) {
2203 // If this is the last window and we had requested a starting
2204 // transition window, well there is no point now.
2205 atoken.startingData = null;
2206 } else if (atoken.allAppWindows.size() == 1 && atoken.startingView != null) {
2207 // If this is the last window except for a starting transition
2208 // window, we need to get rid of the starting transition.
2209 if (DEBUG_STARTING_WINDOW) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002210 Slog.v(TAG, "Schedule remove starting " + token
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002211 + ": no more real windows");
2212 }
2213 Message m = mH.obtainMessage(H.REMOVE_STARTING, atoken);
2214 mH.sendMessage(m);
2215 }
2216 }
Romain Guy06882f82009-06-10 13:36:04 -07002217
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002218 if (win.mAttrs.type == TYPE_WALLPAPER) {
2219 mLastWallpaperTimeoutTime = 0;
2220 adjustWallpaperWindowsLocked();
2221 } else if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07002222 adjustWallpaperWindowsLocked();
2223 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002225 if (!mInLayout) {
2226 assignLayersLocked();
2227 mLayoutNeeded = true;
2228 performLayoutAndPlaceSurfacesLocked();
2229 if (win.mAppToken != null) {
2230 win.mAppToken.updateReportedVisibilityLocked();
2231 }
2232 }
2233 }
2234
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08002235 private static void logSurface(WindowState w, String msg, RuntimeException where) {
2236 String str = " SURFACE " + Integer.toHexString(w.hashCode())
2237 + ": " + msg + " / " + w.mAttrs.getTitle();
2238 if (where != null) {
2239 Slog.i(TAG, str, where);
2240 } else {
2241 Slog.i(TAG, str);
2242 }
2243 }
2244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002245 private void setTransparentRegionWindow(Session session, IWindow client, Region region) {
2246 long origId = Binder.clearCallingIdentity();
2247 try {
2248 synchronized (mWindowMap) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002249 WindowState w = windowForClientLocked(session, client, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002250 if ((w != null) && (w.mSurface != null)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002251 if (SHOW_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002252 Surface.openTransaction();
2253 try {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08002254 if (SHOW_TRANSACTIONS) logSurface(w,
2255 "transparentRegionHint=" + region, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002256 w.mSurface.setTransparentRegionHint(region);
2257 } finally {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002258 if (SHOW_TRANSACTIONS) Slog.i(TAG, "<<< CLOSE TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002259 Surface.closeTransaction();
2260 }
2261 }
2262 }
2263 } finally {
2264 Binder.restoreCallingIdentity(origId);
2265 }
2266 }
2267
2268 void setInsetsWindow(Session session, IWindow client,
Romain Guy06882f82009-06-10 13:36:04 -07002269 int touchableInsets, Rect contentInsets,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002270 Rect visibleInsets) {
2271 long origId = Binder.clearCallingIdentity();
2272 try {
2273 synchronized (mWindowMap) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002274 WindowState w = windowForClientLocked(session, client, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002275 if (w != null) {
2276 w.mGivenInsetsPending = false;
2277 w.mGivenContentInsets.set(contentInsets);
2278 w.mGivenVisibleInsets.set(visibleInsets);
2279 w.mTouchableInsets = touchableInsets;
2280 mLayoutNeeded = true;
2281 performLayoutAndPlaceSurfacesLocked();
2282 }
2283 }
2284 } finally {
2285 Binder.restoreCallingIdentity(origId);
2286 }
2287 }
Romain Guy06882f82009-06-10 13:36:04 -07002288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002289 public void getWindowDisplayFrame(Session session, IWindow client,
2290 Rect outDisplayFrame) {
2291 synchronized(mWindowMap) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002292 WindowState win = windowForClientLocked(session, client, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002293 if (win == null) {
2294 outDisplayFrame.setEmpty();
2295 return;
2296 }
2297 outDisplayFrame.set(win.mDisplayFrame);
2298 }
2299 }
2300
Marco Nelissenbf6956b2009-11-09 15:21:13 -08002301 public void setWindowWallpaperPositionLocked(WindowState window, float x, float y,
2302 float xStep, float yStep) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002303 if (window.mWallpaperX != x || window.mWallpaperY != y) {
2304 window.mWallpaperX = x;
2305 window.mWallpaperY = y;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08002306 window.mWallpaperXStep = xStep;
2307 window.mWallpaperYStep = yStep;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07002308 if (updateWallpaperOffsetLocked(window, true)) {
2309 performLayoutAndPlaceSurfacesLocked();
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002310 }
2311 }
2312 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002313
Dianne Hackborn75804932009-10-20 20:15:20 -07002314 void wallpaperCommandComplete(IBinder window, Bundle result) {
2315 synchronized (mWindowMap) {
2316 if (mWaitingOnWallpaper != null &&
2317 mWaitingOnWallpaper.mClient.asBinder() == window) {
2318 mWaitingOnWallpaper = null;
2319 mWindowMap.notifyAll();
2320 }
2321 }
2322 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002323
Dianne Hackborn75804932009-10-20 20:15:20 -07002324 public Bundle sendWindowWallpaperCommandLocked(WindowState window,
2325 String action, int x, int y, int z, Bundle extras, boolean sync) {
2326 if (window == mWallpaperTarget || window == mLowerWallpaperTarget
2327 || window == mUpperWallpaperTarget) {
2328 boolean doWait = sync;
2329 int curTokenIndex = mWallpaperTokens.size();
2330 while (curTokenIndex > 0) {
2331 curTokenIndex--;
2332 WindowToken token = mWallpaperTokens.get(curTokenIndex);
2333 int curWallpaperIndex = token.windows.size();
2334 while (curWallpaperIndex > 0) {
2335 curWallpaperIndex--;
2336 WindowState wallpaper = token.windows.get(curWallpaperIndex);
2337 try {
2338 wallpaper.mClient.dispatchWallpaperCommand(action,
2339 x, y, z, extras, sync);
2340 // We only want to be synchronous with one wallpaper.
2341 sync = false;
2342 } catch (RemoteException e) {
2343 }
2344 }
2345 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002346
Dianne Hackborn75804932009-10-20 20:15:20 -07002347 if (doWait) {
2348 // XXX Need to wait for result.
2349 }
2350 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002351
Dianne Hackborn75804932009-10-20 20:15:20 -07002352 return null;
2353 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002355 public int relayoutWindow(Session session, IWindow client,
2356 WindowManager.LayoutParams attrs, int requestedWidth,
2357 int requestedHeight, int viewVisibility, boolean insetsPending,
2358 Rect outFrame, Rect outContentInsets, Rect outVisibleInsets,
Dianne Hackborn694f79b2010-03-17 19:44:59 -07002359 Configuration outConfig, Surface outSurface) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002360 boolean displayed = false;
2361 boolean inTouchMode;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002362 boolean configChanged;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002363 long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002365 synchronized(mWindowMap) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002366 WindowState win = windowForClientLocked(session, client, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002367 if (win == null) {
2368 return 0;
2369 }
2370 win.mRequestedWidth = requestedWidth;
2371 win.mRequestedHeight = requestedHeight;
2372
2373 if (attrs != null) {
2374 mPolicy.adjustWindowParamsLw(attrs);
2375 }
Romain Guy06882f82009-06-10 13:36:04 -07002376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002377 int attrChanges = 0;
2378 int flagChanges = 0;
2379 if (attrs != null) {
2380 flagChanges = win.mAttrs.flags ^= attrs.flags;
2381 attrChanges = win.mAttrs.copyFrom(attrs);
2382 }
2383
Joe Onorato8a9b2202010-02-26 18:56:32 -08002384 if (DEBUG_LAYOUT) Slog.v(TAG, "Relayout " + win + ": " + win.mAttrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002385
2386 if ((attrChanges & WindowManager.LayoutParams.ALPHA_CHANGED) != 0) {
2387 win.mAlpha = attrs.alpha;
2388 }
2389
2390 final boolean scaledWindow =
2391 ((win.mAttrs.flags & WindowManager.LayoutParams.FLAG_SCALED) != 0);
2392
2393 if (scaledWindow) {
2394 // requested{Width|Height} Surface's physical size
2395 // attrs.{width|height} Size on screen
2396 win.mHScale = (attrs.width != requestedWidth) ?
2397 (attrs.width / (float)requestedWidth) : 1.0f;
2398 win.mVScale = (attrs.height != requestedHeight) ?
2399 (attrs.height / (float)requestedHeight) : 1.0f;
Dianne Hackborn9b52a212009-12-11 14:51:35 -08002400 } else {
2401 win.mHScale = win.mVScale = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002402 }
2403
2404 boolean imMayMove = (flagChanges&(
2405 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
2406 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07002407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002408 boolean focusMayChange = win.mViewVisibility != viewVisibility
2409 || ((flagChanges&WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) != 0)
2410 || (!win.mRelayoutCalled);
Romain Guy06882f82009-06-10 13:36:04 -07002411
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002412 boolean wallpaperMayMove = win.mViewVisibility != viewVisibility
2413 && (win.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002415 win.mRelayoutCalled = true;
2416 final int oldVisibility = win.mViewVisibility;
2417 win.mViewVisibility = viewVisibility;
2418 if (viewVisibility == View.VISIBLE &&
2419 (win.mAppToken == null || !win.mAppToken.clientHidden)) {
2420 displayed = !win.isVisibleLw();
2421 if (win.mExiting) {
2422 win.mExiting = false;
2423 win.mAnimation = null;
2424 }
2425 if (win.mDestroying) {
2426 win.mDestroying = false;
2427 mDestroySurface.remove(win);
2428 }
2429 if (oldVisibility == View.GONE) {
2430 win.mEnterAnimationPending = true;
2431 }
Dianne Hackborn694f79b2010-03-17 19:44:59 -07002432 if (displayed) {
2433 if (win.mSurface != null && !win.mDrawPending
2434 && !win.mCommitDrawPending && !mDisplayFrozen
2435 && mPolicy.isScreenOn()) {
2436 applyEnterAnimationLocked(win);
2437 }
2438 if ((win.mAttrs.flags
2439 & WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON) != 0) {
2440 if (DEBUG_VISIBILITY) Slog.v(TAG,
2441 "Relayout window turning screen on: " + win);
2442 win.mTurnOnScreen = true;
2443 }
2444 int diff = 0;
2445 if (win.mConfiguration != mCurConfiguration
2446 && (win.mConfiguration == null
2447 || (diff=mCurConfiguration.diff(win.mConfiguration)) != 0)) {
2448 win.mConfiguration = mCurConfiguration;
2449 if (DEBUG_CONFIGURATION) {
2450 Slog.i(TAG, "Window " + win + " visible with new config: "
2451 + win.mConfiguration + " / 0x"
2452 + Integer.toHexString(diff));
2453 }
2454 outConfig.setTo(mCurConfiguration);
2455 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07002456 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002457 if ((attrChanges&WindowManager.LayoutParams.FORMAT_CHANGED) != 0) {
2458 // To change the format, we need to re-build the surface.
2459 win.destroySurfaceLocked();
2460 displayed = true;
2461 }
2462 try {
2463 Surface surface = win.createSurfaceLocked();
2464 if (surface != null) {
2465 outSurface.copyFrom(surface);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002466 win.mReportDestroySurface = false;
2467 win.mSurfacePendingDestroy = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -08002468 if (SHOW_TRANSACTIONS) Slog.i(TAG,
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002469 " OUT SURFACE " + outSurface + ": copied");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002470 } else {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002471 // For some reason there isn't a surface. Clear the
2472 // caller's object so they see the same state.
2473 outSurface.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002474 }
2475 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002476 Slog.w(TAG, "Exception thrown when creating surface for client "
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002477 + client + " (" + win.mAttrs.getTitle() + ")",
2478 e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002479 Binder.restoreCallingIdentity(origId);
2480 return 0;
2481 }
2482 if (displayed) {
2483 focusMayChange = true;
2484 }
2485 if (win.mAttrs.type == TYPE_INPUT_METHOD
2486 && mInputMethodWindow == null) {
2487 mInputMethodWindow = win;
2488 imMayMove = true;
2489 }
Dianne Hackborn558947c2009-12-18 16:02:50 -08002490 if (win.mAttrs.type == TYPE_BASE_APPLICATION
2491 && win.mAppToken != null
2492 && win.mAppToken.startingWindow != null) {
2493 // Special handling of starting window over the base
2494 // window of the app: propagate lock screen flags to it,
2495 // to provide the correct semantics while starting.
2496 final int mask =
2497 WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
Mike Lockwoodef731622010-01-27 17:51:34 -05002498 | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
2499 | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON;
Dianne Hackborn558947c2009-12-18 16:02:50 -08002500 WindowManager.LayoutParams sa = win.mAppToken.startingWindow.mAttrs;
2501 sa.flags = (sa.flags&~mask) | (win.mAttrs.flags&mask);
2502 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002503 } else {
2504 win.mEnterAnimationPending = false;
2505 if (win.mSurface != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002506 if (DEBUG_VISIBILITY) Slog.i(TAG, "Relayout invis " + win
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002507 + ": mExiting=" + win.mExiting
2508 + " mSurfacePendingDestroy=" + win.mSurfacePendingDestroy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002509 // If we are not currently running the exit animation, we
2510 // need to see about starting one.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002511 if (!win.mExiting || win.mSurfacePendingDestroy) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002512 // Try starting an animation; if there isn't one, we
2513 // can destroy the surface right away.
2514 int transit = WindowManagerPolicy.TRANSIT_EXIT;
2515 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
2516 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
2517 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002518 if (!win.mSurfacePendingDestroy && win.isWinVisibleLw() &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002519 applyAnimationLocked(win, transit, false)) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002520 focusMayChange = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002521 win.mExiting = true;
2522 mKeyWaiter.finishedKey(session, client, true,
2523 KeyWaiter.RETURN_NOTHING);
2524 } else if (win.isAnimating()) {
2525 // Currently in a hide animation... turn this into
2526 // an exit.
2527 win.mExiting = true;
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07002528 } else if (win == mWallpaperTarget) {
2529 // If the wallpaper is currently behind this
2530 // window, we need to change both of them inside
2531 // of a transaction to avoid artifacts.
2532 win.mExiting = true;
2533 win.mAnimating = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002534 } else {
2535 if (mInputMethodWindow == win) {
2536 mInputMethodWindow = null;
2537 }
2538 win.destroySurfaceLocked();
2539 }
2540 }
2541 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002542
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002543 if (win.mSurface == null || (win.getAttrs().flags
2544 & WindowManager.LayoutParams.FLAG_KEEP_SURFACE_WHILE_ANIMATING) == 0
2545 || win.mSurfacePendingDestroy) {
2546 // We are being called from a local process, which
2547 // means outSurface holds its current surface. Ensure the
2548 // surface object is cleared, but we don't want it actually
2549 // destroyed at this point.
2550 win.mSurfacePendingDestroy = false;
2551 outSurface.release();
Joe Onorato8a9b2202010-02-26 18:56:32 -08002552 if (DEBUG_VISIBILITY) Slog.i(TAG, "Releasing surface in: " + win);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002553 } else if (win.mSurface != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002554 if (DEBUG_VISIBILITY) Slog.i(TAG,
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002555 "Keeping surface, will report destroy: " + win);
2556 win.mReportDestroySurface = true;
2557 outSurface.copyFrom(win.mSurface);
2558 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002559 }
2560
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002561 if (focusMayChange) {
2562 //System.out.println("Focus may change: " + win.mAttrs.getTitle());
2563 if (updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002564 imMayMove = false;
2565 }
2566 //System.out.println("Relayout " + win + ": focus=" + mCurrentFocus);
2567 }
Romain Guy06882f82009-06-10 13:36:04 -07002568
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08002569 // updateFocusedWindowLocked() already assigned layers so we only need to
2570 // reassign them at this point if the IM window state gets shuffled
2571 boolean assignLayers = false;
Romain Guy06882f82009-06-10 13:36:04 -07002572
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002573 if (imMayMove) {
Dianne Hackborn8abd5f02009-11-20 18:09:03 -08002574 if (moveInputMethodWindowsIfNeededLocked(false) || displayed) {
2575 // Little hack here -- we -should- be able to rely on the
2576 // function to return true if the IME has moved and needs
2577 // its layer recomputed. However, if the IME was hidden
2578 // and isn't actually moved in the list, its layer may be
2579 // out of data so we make sure to recompute it.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002580 assignLayers = true;
2581 }
2582 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002583 if (wallpaperMayMove) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002584 if ((adjustWallpaperWindowsLocked()&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002585 assignLayers = true;
2586 }
2587 }
Romain Guy06882f82009-06-10 13:36:04 -07002588
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002589 mLayoutNeeded = true;
2590 win.mGivenInsetsPending = insetsPending;
2591 if (assignLayers) {
2592 assignLayersLocked();
2593 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002594 configChanged = updateOrientationFromAppTokensLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002595 performLayoutAndPlaceSurfacesLocked();
Dianne Hackborn284ac932009-08-28 10:34:25 -07002596 if (displayed && win.mIsWallpaper) {
2597 updateWallpaperOffsetLocked(win, mDisplay.getWidth(),
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002598 mDisplay.getHeight(), false);
Dianne Hackborn284ac932009-08-28 10:34:25 -07002599 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002600 if (win.mAppToken != null) {
2601 win.mAppToken.updateReportedVisibilityLocked();
2602 }
2603 outFrame.set(win.mFrame);
2604 outContentInsets.set(win.mContentInsets);
2605 outVisibleInsets.set(win.mVisibleInsets);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002606 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002607 TAG, "Relayout given client " + client.asBinder()
Romain Guy06882f82009-06-10 13:36:04 -07002608 + ", requestedWidth=" + requestedWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002609 + ", requestedHeight=" + requestedHeight
2610 + ", viewVisibility=" + viewVisibility
2611 + "\nRelayout returning frame=" + outFrame
2612 + ", surface=" + outSurface);
2613
Joe Onorato8a9b2202010-02-26 18:56:32 -08002614 if (localLOGV || DEBUG_FOCUS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002615 TAG, "Relayout of " + win + ": focusMayChange=" + focusMayChange);
2616
2617 inTouchMode = mInTouchMode;
2618 }
2619
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002620 if (configChanged) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002621 sendNewConfiguration();
2622 }
Romain Guy06882f82009-06-10 13:36:04 -07002623
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002624 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07002625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002626 return (inTouchMode ? WindowManagerImpl.RELAYOUT_IN_TOUCH_MODE : 0)
2627 | (displayed ? WindowManagerImpl.RELAYOUT_FIRST_TIME : 0);
2628 }
2629
2630 public void finishDrawingWindow(Session session, IWindow client) {
2631 final long origId = Binder.clearCallingIdentity();
2632 synchronized(mWindowMap) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002633 WindowState win = windowForClientLocked(session, client, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002634 if (win != null && win.finishDrawingLocked()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07002635 if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
2636 adjustWallpaperWindowsLocked();
2637 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002638 mLayoutNeeded = true;
2639 performLayoutAndPlaceSurfacesLocked();
2640 }
2641 }
2642 Binder.restoreCallingIdentity(origId);
2643 }
2644
2645 private AttributeCache.Entry getCachedAnimations(WindowManager.LayoutParams lp) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002646 if (DEBUG_ANIM) Slog.v(TAG, "Loading animations: params package="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002647 + (lp != null ? lp.packageName : null)
2648 + " resId=0x" + (lp != null ? Integer.toHexString(lp.windowAnimations) : null));
2649 if (lp != null && lp.windowAnimations != 0) {
2650 // If this is a system resource, don't try to load it from the
2651 // application resources. It is nice to avoid loading application
2652 // resources if we can.
2653 String packageName = lp.packageName != null ? lp.packageName : "android";
2654 int resId = lp.windowAnimations;
2655 if ((resId&0xFF000000) == 0x01000000) {
2656 packageName = "android";
2657 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08002658 if (DEBUG_ANIM) Slog.v(TAG, "Loading animations: picked package="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002659 + packageName);
2660 return AttributeCache.instance().get(packageName, resId,
2661 com.android.internal.R.styleable.WindowAnimation);
2662 }
2663 return null;
2664 }
Romain Guy06882f82009-06-10 13:36:04 -07002665
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002666 private AttributeCache.Entry getCachedAnimations(String packageName, int resId) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002667 if (DEBUG_ANIM) Slog.v(TAG, "Loading animations: params package="
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002668 + packageName + " resId=0x" + Integer.toHexString(resId));
2669 if (packageName != null) {
2670 if ((resId&0xFF000000) == 0x01000000) {
2671 packageName = "android";
2672 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08002673 if (DEBUG_ANIM) Slog.v(TAG, "Loading animations: picked package="
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002674 + packageName);
2675 return AttributeCache.instance().get(packageName, resId,
2676 com.android.internal.R.styleable.WindowAnimation);
2677 }
2678 return null;
2679 }
2680
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002681 private void applyEnterAnimationLocked(WindowState win) {
2682 int transit = WindowManagerPolicy.TRANSIT_SHOW;
2683 if (win.mEnterAnimationPending) {
2684 win.mEnterAnimationPending = false;
2685 transit = WindowManagerPolicy.TRANSIT_ENTER;
2686 }
2687
2688 applyAnimationLocked(win, transit, true);
2689 }
2690
2691 private boolean applyAnimationLocked(WindowState win,
2692 int transit, boolean isEntrance) {
2693 if (win.mLocalAnimating && win.mAnimationIsEntrance == isEntrance) {
2694 // If we are trying to apply an animation, but already running
2695 // an animation of the same type, then just leave that one alone.
2696 return true;
2697 }
Romain Guy06882f82009-06-10 13:36:04 -07002698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002699 // Only apply an animation if the display isn't frozen. If it is
2700 // frozen, there is no reason to animate and it can cause strange
2701 // artifacts when we unfreeze the display if some different animation
2702 // is running.
Dianne Hackbornde2606d2009-12-18 16:53:55 -08002703 if (!mDisplayFrozen && mPolicy.isScreenOn()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002704 int anim = mPolicy.selectAnimationLw(win, transit);
2705 int attr = -1;
2706 Animation a = null;
2707 if (anim != 0) {
2708 a = AnimationUtils.loadAnimation(mContext, anim);
2709 } else {
2710 switch (transit) {
2711 case WindowManagerPolicy.TRANSIT_ENTER:
2712 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
2713 break;
2714 case WindowManagerPolicy.TRANSIT_EXIT:
2715 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
2716 break;
2717 case WindowManagerPolicy.TRANSIT_SHOW:
2718 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
2719 break;
2720 case WindowManagerPolicy.TRANSIT_HIDE:
2721 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
2722 break;
2723 }
2724 if (attr >= 0) {
2725 a = loadAnimation(win.mAttrs, attr);
2726 }
2727 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08002728 if (DEBUG_ANIM) Slog.v(TAG, "applyAnimation: win=" + win
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002729 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
2730 + " mAnimation=" + win.mAnimation
2731 + " isEntrance=" + isEntrance);
2732 if (a != null) {
2733 if (DEBUG_ANIM) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08002734 RuntimeException e = null;
2735 if (!HIDE_STACK_CRAWLS) {
2736 e = new RuntimeException();
2737 e.fillInStackTrace();
2738 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08002739 Slog.v(TAG, "Loaded animation " + a + " for " + win, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002740 }
2741 win.setAnimation(a);
2742 win.mAnimationIsEntrance = isEntrance;
2743 }
2744 } else {
2745 win.clearAnimation();
2746 }
2747
2748 return win.mAnimation != null;
2749 }
2750
2751 private Animation loadAnimation(WindowManager.LayoutParams lp, int animAttr) {
2752 int anim = 0;
2753 Context context = mContext;
2754 if (animAttr >= 0) {
2755 AttributeCache.Entry ent = getCachedAnimations(lp);
2756 if (ent != null) {
2757 context = ent.context;
2758 anim = ent.array.getResourceId(animAttr, 0);
2759 }
2760 }
2761 if (anim != 0) {
2762 return AnimationUtils.loadAnimation(context, anim);
2763 }
2764 return null;
2765 }
Romain Guy06882f82009-06-10 13:36:04 -07002766
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002767 private Animation loadAnimation(String packageName, int resId) {
2768 int anim = 0;
2769 Context context = mContext;
2770 if (resId >= 0) {
2771 AttributeCache.Entry ent = getCachedAnimations(packageName, resId);
2772 if (ent != null) {
2773 context = ent.context;
2774 anim = resId;
2775 }
2776 }
2777 if (anim != 0) {
2778 return AnimationUtils.loadAnimation(context, anim);
2779 }
2780 return null;
2781 }
2782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002783 private boolean applyAnimationLocked(AppWindowToken wtoken,
2784 WindowManager.LayoutParams lp, int transit, boolean enter) {
2785 // Only apply an animation if the display isn't frozen. If it is
2786 // frozen, there is no reason to animate and it can cause strange
2787 // artifacts when we unfreeze the display if some different animation
2788 // is running.
Dianne Hackbornde2606d2009-12-18 16:53:55 -08002789 if (!mDisplayFrozen && mPolicy.isScreenOn()) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002790 Animation a;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07002791 if (lp != null && (lp.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002792 a = new FadeInOutAnimation(enter);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002793 if (DEBUG_ANIM) Slog.v(TAG,
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002794 "applying FadeInOutAnimation for a window in compatibility mode");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002795 } else if (mNextAppTransitionPackage != null) {
2796 a = loadAnimation(mNextAppTransitionPackage, enter ?
2797 mNextAppTransitionEnter : mNextAppTransitionExit);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002798 } else {
2799 int animAttr = 0;
2800 switch (transit) {
2801 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
2802 animAttr = enter
2803 ? com.android.internal.R.styleable.WindowAnimation_activityOpenEnterAnimation
2804 : com.android.internal.R.styleable.WindowAnimation_activityOpenExitAnimation;
2805 break;
2806 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
2807 animAttr = enter
2808 ? com.android.internal.R.styleable.WindowAnimation_activityCloseEnterAnimation
2809 : com.android.internal.R.styleable.WindowAnimation_activityCloseExitAnimation;
2810 break;
2811 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
2812 animAttr = enter
2813 ? com.android.internal.R.styleable.WindowAnimation_taskOpenEnterAnimation
2814 : com.android.internal.R.styleable.WindowAnimation_taskOpenExitAnimation;
2815 break;
2816 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
2817 animAttr = enter
2818 ? com.android.internal.R.styleable.WindowAnimation_taskCloseEnterAnimation
2819 : com.android.internal.R.styleable.WindowAnimation_taskCloseExitAnimation;
2820 break;
2821 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
2822 animAttr = enter
2823 ? com.android.internal.R.styleable.WindowAnimation_taskToFrontEnterAnimation
2824 : com.android.internal.R.styleable.WindowAnimation_taskToFrontExitAnimation;
2825 break;
2826 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
2827 animAttr = enter
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07002828 ? com.android.internal.R.styleable.WindowAnimation_taskToBackEnterAnimation
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002829 : com.android.internal.R.styleable.WindowAnimation_taskToBackExitAnimation;
2830 break;
Dianne Hackborn25994b42009-09-04 14:21:19 -07002831 case WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN:
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002832 animAttr = enter
Dianne Hackborn25994b42009-09-04 14:21:19 -07002833 ? com.android.internal.R.styleable.WindowAnimation_wallpaperOpenEnterAnimation
2834 : com.android.internal.R.styleable.WindowAnimation_wallpaperOpenExitAnimation;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002835 break;
Dianne Hackborn25994b42009-09-04 14:21:19 -07002836 case WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE:
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002837 animAttr = enter
Dianne Hackborn25994b42009-09-04 14:21:19 -07002838 ? com.android.internal.R.styleable.WindowAnimation_wallpaperCloseEnterAnimation
2839 : com.android.internal.R.styleable.WindowAnimation_wallpaperCloseExitAnimation;
2840 break;
2841 case WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN:
2842 animAttr = enter
2843 ? com.android.internal.R.styleable.WindowAnimation_wallpaperIntraOpenEnterAnimation
2844 : com.android.internal.R.styleable.WindowAnimation_wallpaperIntraOpenExitAnimation;
2845 break;
2846 case WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE:
2847 animAttr = enter
2848 ? com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseEnterAnimation
2849 : com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseExitAnimation;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002850 break;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002851 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07002852 a = animAttr != 0 ? loadAnimation(lp, animAttr) : null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08002853 if (DEBUG_ANIM) Slog.v(TAG, "applyAnimation: wtoken=" + wtoken
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002854 + " anim=" + a
2855 + " animAttr=0x" + Integer.toHexString(animAttr)
2856 + " transit=" + transit);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002857 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002858 if (a != null) {
2859 if (DEBUG_ANIM) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08002860 RuntimeException e = null;
2861 if (!HIDE_STACK_CRAWLS) {
2862 e = new RuntimeException();
2863 e.fillInStackTrace();
2864 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08002865 Slog.v(TAG, "Loaded animation " + a + " for " + wtoken, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002866 }
2867 wtoken.setAnimation(a);
2868 }
2869 } else {
2870 wtoken.clearAnimation();
2871 }
2872
2873 return wtoken.animation != null;
2874 }
2875
2876 // -------------------------------------------------------------
2877 // Application Window Tokens
2878 // -------------------------------------------------------------
2879
2880 public void validateAppTokens(List tokens) {
2881 int v = tokens.size()-1;
2882 int m = mAppTokens.size()-1;
2883 while (v >= 0 && m >= 0) {
2884 AppWindowToken wtoken = mAppTokens.get(m);
2885 if (wtoken.removed) {
2886 m--;
2887 continue;
2888 }
2889 if (tokens.get(v) != wtoken.token) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002890 Slog.w(TAG, "Tokens out of sync: external is " + tokens.get(v)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002891 + " @ " + v + ", internal is " + wtoken.token + " @ " + m);
2892 }
2893 v--;
2894 m--;
2895 }
2896 while (v >= 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002897 Slog.w(TAG, "External token not found: " + tokens.get(v) + " @ " + v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002898 v--;
2899 }
2900 while (m >= 0) {
2901 AppWindowToken wtoken = mAppTokens.get(m);
2902 if (!wtoken.removed) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002903 Slog.w(TAG, "Invalid internal token: " + wtoken.token + " @ " + m);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002904 }
2905 m--;
2906 }
2907 }
2908
2909 boolean checkCallingPermission(String permission, String func) {
2910 // Quick check: if the calling permission is me, it's all okay.
2911 if (Binder.getCallingPid() == Process.myPid()) {
2912 return true;
2913 }
Romain Guy06882f82009-06-10 13:36:04 -07002914
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002915 if (mContext.checkCallingPermission(permission)
2916 == PackageManager.PERMISSION_GRANTED) {
2917 return true;
2918 }
2919 String msg = "Permission Denial: " + func + " from pid="
2920 + Binder.getCallingPid()
2921 + ", uid=" + Binder.getCallingUid()
2922 + " requires " + permission;
Joe Onorato8a9b2202010-02-26 18:56:32 -08002923 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002924 return false;
2925 }
Romain Guy06882f82009-06-10 13:36:04 -07002926
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002927 AppWindowToken findAppWindowToken(IBinder token) {
2928 WindowToken wtoken = mTokenMap.get(token);
2929 if (wtoken == null) {
2930 return null;
2931 }
2932 return wtoken.appWindowToken;
2933 }
Romain Guy06882f82009-06-10 13:36:04 -07002934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002935 public void addWindowToken(IBinder token, int type) {
2936 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2937 "addWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002938 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002939 }
Romain Guy06882f82009-06-10 13:36:04 -07002940
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002941 synchronized(mWindowMap) {
2942 WindowToken wtoken = mTokenMap.get(token);
2943 if (wtoken != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002944 Slog.w(TAG, "Attempted to add existing input method token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002945 return;
2946 }
2947 wtoken = new WindowToken(token, type, true);
2948 mTokenMap.put(token, wtoken);
2949 mTokenList.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002950 if (type == TYPE_WALLPAPER) {
2951 mWallpaperTokens.add(wtoken);
2952 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002953 }
2954 }
Romain Guy06882f82009-06-10 13:36:04 -07002955
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002956 public void removeWindowToken(IBinder token) {
2957 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2958 "removeWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002959 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002960 }
2961
2962 final long origId = Binder.clearCallingIdentity();
2963 synchronized(mWindowMap) {
2964 WindowToken wtoken = mTokenMap.remove(token);
2965 mTokenList.remove(wtoken);
2966 if (wtoken != null) {
2967 boolean delayed = false;
2968 if (!wtoken.hidden) {
2969 wtoken.hidden = true;
Romain Guy06882f82009-06-10 13:36:04 -07002970
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002971 final int N = wtoken.windows.size();
2972 boolean changed = false;
Romain Guy06882f82009-06-10 13:36:04 -07002973
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002974 for (int i=0; i<N; i++) {
2975 WindowState win = wtoken.windows.get(i);
2976
2977 if (win.isAnimating()) {
2978 delayed = true;
2979 }
Romain Guy06882f82009-06-10 13:36:04 -07002980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002981 if (win.isVisibleNow()) {
2982 applyAnimationLocked(win,
2983 WindowManagerPolicy.TRANSIT_EXIT, false);
2984 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2985 KeyWaiter.RETURN_NOTHING);
2986 changed = true;
2987 }
2988 }
2989
2990 if (changed) {
2991 mLayoutNeeded = true;
2992 performLayoutAndPlaceSurfacesLocked();
2993 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2994 }
Romain Guy06882f82009-06-10 13:36:04 -07002995
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002996 if (delayed) {
2997 mExitingTokens.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002998 } else if (wtoken.windowType == TYPE_WALLPAPER) {
2999 mWallpaperTokens.remove(wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003000 }
3001 }
Romain Guy06882f82009-06-10 13:36:04 -07003002
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003003 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003004 Slog.w(TAG, "Attempted to remove non-existing token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003005 }
3006 }
3007 Binder.restoreCallingIdentity(origId);
3008 }
3009
3010 public void addAppToken(int addPos, IApplicationToken token,
3011 int groupId, int requestedOrientation, boolean fullscreen) {
3012 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3013 "addAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003014 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003015 }
Romain Guy06882f82009-06-10 13:36:04 -07003016
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003017 synchronized(mWindowMap) {
3018 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
3019 if (wtoken != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003020 Slog.w(TAG, "Attempted to add existing app token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003021 return;
3022 }
3023 wtoken = new AppWindowToken(token);
3024 wtoken.groupId = groupId;
3025 wtoken.appFullscreen = fullscreen;
3026 wtoken.requestedOrientation = requestedOrientation;
3027 mAppTokens.add(addPos, wtoken);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003028 if (localLOGV) Slog.v(TAG, "Adding new app token: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003029 mTokenMap.put(token.asBinder(), wtoken);
3030 mTokenList.add(wtoken);
Romain Guy06882f82009-06-10 13:36:04 -07003031
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003032 // Application tokens start out hidden.
3033 wtoken.hidden = true;
3034 wtoken.hiddenRequested = true;
Romain Guy06882f82009-06-10 13:36:04 -07003035
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003036 //dump();
3037 }
3038 }
Romain Guy06882f82009-06-10 13:36:04 -07003039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003040 public void setAppGroupId(IBinder token, int groupId) {
3041 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3042 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003043 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003044 }
3045
3046 synchronized(mWindowMap) {
3047 AppWindowToken wtoken = findAppWindowToken(token);
3048 if (wtoken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003049 Slog.w(TAG, "Attempted to set group id of non-existing app token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003050 return;
3051 }
3052 wtoken.groupId = groupId;
3053 }
3054 }
Romain Guy06882f82009-06-10 13:36:04 -07003055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003056 public int getOrientationFromWindowsLocked() {
3057 int pos = mWindows.size() - 1;
3058 while (pos >= 0) {
3059 WindowState wtoken = (WindowState) mWindows.get(pos);
3060 pos--;
3061 if (wtoken.mAppToken != null) {
3062 // We hit an application window. so the orientation will be determined by the
3063 // app window. No point in continuing further.
3064 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
3065 }
Christopher Tateb696aee2010-04-02 19:08:30 -07003066 if (!wtoken.isVisibleLw() || !wtoken.mPolicyVisibilityAfterAnim) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003067 continue;
3068 }
3069 int req = wtoken.mAttrs.screenOrientation;
3070 if((req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) ||
3071 (req == ActivityInfo.SCREEN_ORIENTATION_BEHIND)){
3072 continue;
3073 } else {
3074 return req;
3075 }
3076 }
3077 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
3078 }
Romain Guy06882f82009-06-10 13:36:04 -07003079
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003080 public int getOrientationFromAppTokensLocked() {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003081 int pos = mAppTokens.size() - 1;
3082 int curGroup = 0;
3083 int lastOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
3084 boolean findingBehind = false;
3085 boolean haveGroup = false;
3086 boolean lastFullscreen = false;
3087 while (pos >= 0) {
3088 AppWindowToken wtoken = mAppTokens.get(pos);
3089 pos--;
3090 // if we're about to tear down this window and not seek for
3091 // the behind activity, don't use it for orientation
3092 if (!findingBehind
3093 && (!wtoken.hidden && wtoken.hiddenRequested)) {
3094 continue;
3095 }
3096
3097 if (!haveGroup) {
3098 // We ignore any hidden applications on the top.
3099 if (wtoken.hiddenRequested || wtoken.willBeHidden) {
The Android Open Source Project10592532009-03-18 17:39:46 -07003100 continue;
3101 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003102 haveGroup = true;
3103 curGroup = wtoken.groupId;
3104 lastOrientation = wtoken.requestedOrientation;
3105 } else if (curGroup != wtoken.groupId) {
3106 // If we have hit a new application group, and the bottom
3107 // of the previous group didn't explicitly say to use
3108 // the orientation behind it, and the last app was
3109 // full screen, then we'll stick with the
3110 // user's orientation.
3111 if (lastOrientation != ActivityInfo.SCREEN_ORIENTATION_BEHIND
3112 && lastFullscreen) {
3113 return lastOrientation;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003114 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003115 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003116 int or = wtoken.requestedOrientation;
3117 // If this application is fullscreen, and didn't explicitly say
3118 // to use the orientation behind it, then just take whatever
3119 // orientation it has and ignores whatever is under it.
3120 lastFullscreen = wtoken.appFullscreen;
3121 if (lastFullscreen
3122 && or != ActivityInfo.SCREEN_ORIENTATION_BEHIND) {
3123 return or;
3124 }
3125 // If this application has requested an explicit orientation,
3126 // then use it.
3127 if (or == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE ||
3128 or == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ||
3129 or == ActivityInfo.SCREEN_ORIENTATION_SENSOR ||
3130 or == ActivityInfo.SCREEN_ORIENTATION_NOSENSOR ||
3131 or == ActivityInfo.SCREEN_ORIENTATION_USER) {
3132 return or;
3133 }
3134 findingBehind |= (or == ActivityInfo.SCREEN_ORIENTATION_BEHIND);
3135 }
3136 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003137 }
Romain Guy06882f82009-06-10 13:36:04 -07003138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003139 public Configuration updateOrientationFromAppTokens(
The Android Open Source Project10592532009-03-18 17:39:46 -07003140 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003141 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3142 "updateOrientationFromAppTokens()")) {
3143 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
3144 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003145
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003146 Configuration config = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003147 long ident = Binder.clearCallingIdentity();
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003148
3149 synchronized(mWindowMap) {
3150 if (updateOrientationFromAppTokensLocked()) {
3151 if (freezeThisOneIfNeeded != null) {
3152 AppWindowToken wtoken = findAppWindowToken(
3153 freezeThisOneIfNeeded);
3154 if (wtoken != null) {
3155 startAppFreezingScreenLocked(wtoken,
3156 ActivityInfo.CONFIG_ORIENTATION);
3157 }
3158 }
3159 config = computeNewConfigurationLocked();
3160
3161 } else if (currentConfig != null) {
3162 // No obvious action we need to take, but if our current
3163 // state mismatches the activity maanager's, update it
3164 mTempConfiguration.setToDefaults();
3165 if (computeNewConfigurationLocked(mTempConfiguration)) {
3166 if (currentConfig.diff(mTempConfiguration) != 0) {
3167 mWaitingForConfig = true;
3168 mLayoutNeeded = true;
3169 startFreezingDisplayLocked();
3170 config = new Configuration(mTempConfiguration);
3171 }
3172 }
3173 }
3174 }
3175
Dianne Hackborncfaef692009-06-15 14:24:44 -07003176 Binder.restoreCallingIdentity(ident);
3177 return config;
3178 }
3179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003180 /*
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003181 * Determine the new desired orientation of the display, returning
3182 * a non-null new Configuration if it has changed from the current
3183 * orientation. IF TRUE IS RETURNED SOMEONE MUST CALL
3184 * setNewConfiguration() TO TELL THE WINDOW MANAGER IT CAN UNFREEZE THE
3185 * SCREEN. This will typically be done for you if you call
3186 * sendNewConfiguration().
3187 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003188 * The orientation is computed from non-application windows first. If none of
3189 * the non-application windows specify orientation, the orientation is computed from
Romain Guy06882f82009-06-10 13:36:04 -07003190 * application tokens.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003191 * @see android.view.IWindowManager#updateOrientationFromAppTokens(
3192 * android.os.IBinder)
3193 */
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003194 boolean updateOrientationFromAppTokensLocked() {
Christopher Tateb696aee2010-04-02 19:08:30 -07003195 if (mDisplayFrozen) {
3196 // If the display is frozen, some activities may be in the middle
3197 // of restarting, and thus have removed their old window. If the
3198 // window has the flag to hide the lock screen, then the lock screen
3199 // can re-appear and inflict its own orientation on us. Keep the
3200 // orientation stable until this all settles down.
3201 return false;
3202 }
3203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003204 boolean changed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003205 long ident = Binder.clearCallingIdentity();
3206 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003207 int req = computeForcedAppOrientationLocked();
Romain Guy06882f82009-06-10 13:36:04 -07003208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003209 if (req != mForcedAppOrientation) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003210 mForcedAppOrientation = req;
3211 //send a message to Policy indicating orientation change to take
3212 //action like disabling/enabling sensors etc.,
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003213 mPolicy.setCurrentOrientationLw(req);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003214 if (setRotationUncheckedLocked(WindowManagerPolicy.USE_LAST_ROTATION,
3215 mLastRotationFlags | Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE)) {
3216 changed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003217 }
3218 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003219
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003220 return changed;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003221 } finally {
3222 Binder.restoreCallingIdentity(ident);
3223 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003224 }
Romain Guy06882f82009-06-10 13:36:04 -07003225
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003226 int computeForcedAppOrientationLocked() {
3227 int req = getOrientationFromWindowsLocked();
3228 if (req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
3229 req = getOrientationFromAppTokensLocked();
3230 }
3231 return req;
3232 }
Romain Guy06882f82009-06-10 13:36:04 -07003233
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003234 public void setNewConfiguration(Configuration config) {
3235 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3236 "setNewConfiguration()")) {
3237 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
3238 }
3239
3240 synchronized(mWindowMap) {
3241 mCurConfiguration = new Configuration(config);
3242 mWaitingForConfig = false;
3243 performLayoutAndPlaceSurfacesLocked();
3244 }
3245 }
3246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003247 public void setAppOrientation(IApplicationToken token, int requestedOrientation) {
3248 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3249 "setAppOrientation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003250 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003251 }
Romain Guy06882f82009-06-10 13:36:04 -07003252
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003253 synchronized(mWindowMap) {
3254 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
3255 if (wtoken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003256 Slog.w(TAG, "Attempted to set orientation of non-existing app token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003257 return;
3258 }
Romain Guy06882f82009-06-10 13:36:04 -07003259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003260 wtoken.requestedOrientation = requestedOrientation;
3261 }
3262 }
Romain Guy06882f82009-06-10 13:36:04 -07003263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003264 public int getAppOrientation(IApplicationToken token) {
3265 synchronized(mWindowMap) {
3266 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
3267 if (wtoken == null) {
3268 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
3269 }
Romain Guy06882f82009-06-10 13:36:04 -07003270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003271 return wtoken.requestedOrientation;
3272 }
3273 }
Romain Guy06882f82009-06-10 13:36:04 -07003274
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003275 public void setFocusedApp(IBinder token, boolean moveFocusNow) {
3276 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3277 "setFocusedApp()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003278 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003279 }
3280
3281 synchronized(mWindowMap) {
3282 boolean changed = false;
3283 if (token == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003284 if (DEBUG_FOCUS) Slog.v(TAG, "Clearing focused app, was " + mFocusedApp);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003285 changed = mFocusedApp != null;
3286 mFocusedApp = null;
3287 mKeyWaiter.tickle();
3288 } else {
3289 AppWindowToken newFocus = findAppWindowToken(token);
3290 if (newFocus == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003291 Slog.w(TAG, "Attempted to set focus to non-existing app token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003292 return;
3293 }
3294 changed = mFocusedApp != newFocus;
3295 mFocusedApp = newFocus;
Joe Onorato8a9b2202010-02-26 18:56:32 -08003296 if (DEBUG_FOCUS) Slog.v(TAG, "Set focused app to: " + mFocusedApp);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003297 mKeyWaiter.tickle();
3298 }
3299
3300 if (moveFocusNow && changed) {
3301 final long origId = Binder.clearCallingIdentity();
3302 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3303 Binder.restoreCallingIdentity(origId);
3304 }
3305 }
3306 }
3307
3308 public void prepareAppTransition(int transit) {
3309 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3310 "prepareAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003311 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003312 }
Romain Guy06882f82009-06-10 13:36:04 -07003313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003314 synchronized(mWindowMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003315 if (DEBUG_APP_TRANSITIONS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003316 TAG, "Prepare app transition: transit=" + transit
3317 + " mNextAppTransition=" + mNextAppTransition);
Dianne Hackbornb601ce12010-03-01 23:36:02 -08003318 if (!mDisplayFrozen && mPolicy.isScreenOn()) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003319 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET
3320 || mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003321 mNextAppTransition = transit;
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07003322 } else if (transit == WindowManagerPolicy.TRANSIT_TASK_OPEN
3323 && mNextAppTransition == WindowManagerPolicy.TRANSIT_TASK_CLOSE) {
3324 // Opening a new task always supersedes a close for the anim.
3325 mNextAppTransition = transit;
3326 } else if (transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN
3327 && mNextAppTransition == WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE) {
3328 // Opening a new activity always supersedes a close for the anim.
3329 mNextAppTransition = transit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003330 }
3331 mAppTransitionReady = false;
3332 mAppTransitionTimeout = false;
3333 mStartingIconInTransition = false;
3334 mSkipAppTransitionAnimation = false;
3335 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
3336 mH.sendMessageDelayed(mH.obtainMessage(H.APP_TRANSITION_TIMEOUT),
3337 5000);
3338 }
3339 }
3340 }
3341
3342 public int getPendingAppTransition() {
3343 return mNextAppTransition;
3344 }
Romain Guy06882f82009-06-10 13:36:04 -07003345
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003346 public void overridePendingAppTransition(String packageName,
3347 int enterAnim, int exitAnim) {
Dianne Hackborn8b571a82009-09-25 16:09:43 -07003348 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003349 mNextAppTransitionPackage = packageName;
3350 mNextAppTransitionEnter = enterAnim;
3351 mNextAppTransitionExit = exitAnim;
3352 }
3353 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003355 public void executeAppTransition() {
3356 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3357 "executeAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003358 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003359 }
Romain Guy06882f82009-06-10 13:36:04 -07003360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003361 synchronized(mWindowMap) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003362 if (DEBUG_APP_TRANSITIONS) {
3363 RuntimeException e = new RuntimeException("here");
3364 e.fillInStackTrace();
Joe Onorato8a9b2202010-02-26 18:56:32 -08003365 Slog.w(TAG, "Execute app transition: mNextAppTransition="
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003366 + mNextAppTransition, e);
3367 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003368 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003369 mAppTransitionReady = true;
3370 final long origId = Binder.clearCallingIdentity();
3371 performLayoutAndPlaceSurfacesLocked();
3372 Binder.restoreCallingIdentity(origId);
3373 }
3374 }
3375 }
3376
3377 public void setAppStartingWindow(IBinder token, String pkg,
3378 int theme, CharSequence nonLocalizedLabel, int labelRes, int icon,
3379 IBinder transferFrom, boolean createIfNeeded) {
3380 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3381 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003382 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003383 }
3384
3385 synchronized(mWindowMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003386 if (DEBUG_STARTING_WINDOW) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003387 TAG, "setAppStartingIcon: token=" + token + " pkg=" + pkg
3388 + " transferFrom=" + transferFrom);
Romain Guy06882f82009-06-10 13:36:04 -07003389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003390 AppWindowToken wtoken = findAppWindowToken(token);
3391 if (wtoken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003392 Slog.w(TAG, "Attempted to set icon of non-existing app token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003393 return;
3394 }
3395
3396 // If the display is frozen, we won't do anything until the
3397 // actual window is displayed so there is no reason to put in
3398 // the starting window.
Dianne Hackbornde2606d2009-12-18 16:53:55 -08003399 if (mDisplayFrozen || !mPolicy.isScreenOn()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003400 return;
3401 }
Romain Guy06882f82009-06-10 13:36:04 -07003402
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003403 if (wtoken.startingData != null) {
3404 return;
3405 }
Romain Guy06882f82009-06-10 13:36:04 -07003406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003407 if (transferFrom != null) {
3408 AppWindowToken ttoken = findAppWindowToken(transferFrom);
3409 if (ttoken != null) {
3410 WindowState startingWindow = ttoken.startingWindow;
3411 if (startingWindow != null) {
3412 if (mStartingIconInTransition) {
3413 // In this case, the starting icon has already
3414 // been displayed, so start letting windows get
3415 // shown immediately without any more transitions.
3416 mSkipAppTransitionAnimation = true;
3417 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08003418 if (DEBUG_STARTING_WINDOW) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003419 "Moving existing starting from " + ttoken
3420 + " to " + wtoken);
3421 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07003422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003423 // Transfer the starting window over to the new
3424 // token.
3425 wtoken.startingData = ttoken.startingData;
3426 wtoken.startingView = ttoken.startingView;
3427 wtoken.startingWindow = startingWindow;
3428 ttoken.startingData = null;
3429 ttoken.startingView = null;
3430 ttoken.startingWindow = null;
3431 ttoken.startingMoved = true;
3432 startingWindow.mToken = wtoken;
Dianne Hackbornef49c572009-03-24 19:27:32 -07003433 startingWindow.mRootToken = wtoken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003434 startingWindow.mAppToken = wtoken;
Joe Onorato8a9b2202010-02-26 18:56:32 -08003435 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG,
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003436 "Removing starting window: " + startingWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003437 mWindows.remove(startingWindow);
3438 ttoken.windows.remove(startingWindow);
3439 ttoken.allAppWindows.remove(startingWindow);
3440 addWindowToListInOrderLocked(startingWindow, true);
Romain Guy06882f82009-06-10 13:36:04 -07003441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003442 // Propagate other interesting state between the
3443 // tokens. If the old token is displayed, we should
3444 // immediately force the new one to be displayed. If
3445 // it is animating, we need to move that animation to
3446 // the new one.
3447 if (ttoken.allDrawn) {
3448 wtoken.allDrawn = true;
3449 }
3450 if (ttoken.firstWindowDrawn) {
3451 wtoken.firstWindowDrawn = true;
3452 }
3453 if (!ttoken.hidden) {
3454 wtoken.hidden = false;
3455 wtoken.hiddenRequested = false;
3456 wtoken.willBeHidden = false;
3457 }
3458 if (wtoken.clientHidden != ttoken.clientHidden) {
3459 wtoken.clientHidden = ttoken.clientHidden;
3460 wtoken.sendAppVisibilityToClients();
3461 }
3462 if (ttoken.animation != null) {
3463 wtoken.animation = ttoken.animation;
3464 wtoken.animating = ttoken.animating;
3465 wtoken.animLayerAdjustment = ttoken.animLayerAdjustment;
3466 ttoken.animation = null;
3467 ttoken.animLayerAdjustment = 0;
3468 wtoken.updateLayers();
3469 ttoken.updateLayers();
3470 }
Romain Guy06882f82009-06-10 13:36:04 -07003471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003472 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003473 mLayoutNeeded = true;
3474 performLayoutAndPlaceSurfacesLocked();
3475 Binder.restoreCallingIdentity(origId);
3476 return;
3477 } else if (ttoken.startingData != null) {
3478 // The previous app was getting ready to show a
3479 // starting window, but hasn't yet done so. Steal it!
Joe Onorato8a9b2202010-02-26 18:56:32 -08003480 if (DEBUG_STARTING_WINDOW) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003481 "Moving pending starting from " + ttoken
3482 + " to " + wtoken);
3483 wtoken.startingData = ttoken.startingData;
3484 ttoken.startingData = null;
3485 ttoken.startingMoved = true;
3486 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3487 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3488 // want to process the message ASAP, before any other queued
3489 // messages.
3490 mH.sendMessageAtFrontOfQueue(m);
3491 return;
3492 }
3493 }
3494 }
3495
3496 // There is no existing starting window, and the caller doesn't
3497 // want us to create one, so that's it!
3498 if (!createIfNeeded) {
3499 return;
3500 }
Romain Guy06882f82009-06-10 13:36:04 -07003501
Dianne Hackborn284ac932009-08-28 10:34:25 -07003502 // If this is a translucent or wallpaper window, then don't
3503 // show a starting window -- the current effect (a full-screen
3504 // opaque starting window that fades away to the real contents
3505 // when it is ready) does not work for this.
3506 if (theme != 0) {
3507 AttributeCache.Entry ent = AttributeCache.instance().get(pkg, theme,
3508 com.android.internal.R.styleable.Window);
3509 if (ent.array.getBoolean(
3510 com.android.internal.R.styleable.Window_windowIsTranslucent, false)) {
3511 return;
3512 }
3513 if (ent.array.getBoolean(
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07003514 com.android.internal.R.styleable.Window_windowIsFloating, false)) {
3515 return;
3516 }
3517 if (ent.array.getBoolean(
Dianne Hackborn284ac932009-08-28 10:34:25 -07003518 com.android.internal.R.styleable.Window_windowShowWallpaper, false)) {
3519 return;
3520 }
3521 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003522
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003523 mStartingIconInTransition = true;
3524 wtoken.startingData = new StartingData(
3525 pkg, theme, nonLocalizedLabel,
3526 labelRes, icon);
3527 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3528 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3529 // want to process the message ASAP, before any other queued
3530 // messages.
3531 mH.sendMessageAtFrontOfQueue(m);
3532 }
3533 }
3534
3535 public void setAppWillBeHidden(IBinder token) {
3536 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3537 "setAppWillBeHidden()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003538 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003539 }
3540
3541 AppWindowToken wtoken;
3542
3543 synchronized(mWindowMap) {
3544 wtoken = findAppWindowToken(token);
3545 if (wtoken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003546 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 -08003547 return;
3548 }
3549 wtoken.willBeHidden = true;
3550 }
3551 }
Romain Guy06882f82009-06-10 13:36:04 -07003552
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003553 boolean setTokenVisibilityLocked(AppWindowToken wtoken, WindowManager.LayoutParams lp,
3554 boolean visible, int transit, boolean performLayout) {
3555 boolean delayed = false;
3556
3557 if (wtoken.clientHidden == visible) {
3558 wtoken.clientHidden = !visible;
3559 wtoken.sendAppVisibilityToClients();
3560 }
Romain Guy06882f82009-06-10 13:36:04 -07003561
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003562 wtoken.willBeHidden = false;
3563 if (wtoken.hidden == visible) {
3564 final int N = wtoken.allAppWindows.size();
3565 boolean changed = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -08003566 if (DEBUG_APP_TRANSITIONS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003567 TAG, "Changing app " + wtoken + " hidden=" + wtoken.hidden
3568 + " performLayout=" + performLayout);
Romain Guy06882f82009-06-10 13:36:04 -07003569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003570 boolean runningAppAnimation = false;
Romain Guy06882f82009-06-10 13:36:04 -07003571
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003572 if (transit != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003573 if (wtoken.animation == sDummyAnimation) {
3574 wtoken.animation = null;
3575 }
3576 applyAnimationLocked(wtoken, lp, transit, visible);
3577 changed = true;
3578 if (wtoken.animation != null) {
3579 delayed = runningAppAnimation = true;
3580 }
3581 }
Romain Guy06882f82009-06-10 13:36:04 -07003582
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003583 for (int i=0; i<N; i++) {
3584 WindowState win = wtoken.allAppWindows.get(i);
3585 if (win == wtoken.startingWindow) {
3586 continue;
3587 }
3588
3589 if (win.isAnimating()) {
3590 delayed = true;
3591 }
Romain Guy06882f82009-06-10 13:36:04 -07003592
Joe Onorato8a9b2202010-02-26 18:56:32 -08003593 //Slog.i(TAG, "Window " + win + ": vis=" + win.isVisible());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003594 //win.dump(" ");
3595 if (visible) {
3596 if (!win.isVisibleNow()) {
3597 if (!runningAppAnimation) {
3598 applyAnimationLocked(win,
3599 WindowManagerPolicy.TRANSIT_ENTER, true);
3600 }
3601 changed = true;
3602 }
3603 } else if (win.isVisibleNow()) {
3604 if (!runningAppAnimation) {
3605 applyAnimationLocked(win,
3606 WindowManagerPolicy.TRANSIT_EXIT, false);
3607 }
3608 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
3609 KeyWaiter.RETURN_NOTHING);
3610 changed = true;
3611 }
3612 }
3613
3614 wtoken.hidden = wtoken.hiddenRequested = !visible;
3615 if (!visible) {
3616 unsetAppFreezingScreenLocked(wtoken, true, true);
3617 } else {
3618 // If we are being set visible, and the starting window is
3619 // not yet displayed, then make sure it doesn't get displayed.
3620 WindowState swin = wtoken.startingWindow;
3621 if (swin != null && (swin.mDrawPending
3622 || swin.mCommitDrawPending)) {
3623 swin.mPolicyVisibility = false;
3624 swin.mPolicyVisibilityAfterAnim = false;
3625 }
3626 }
Romain Guy06882f82009-06-10 13:36:04 -07003627
Joe Onorato8a9b2202010-02-26 18:56:32 -08003628 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG, "setTokenVisibilityLocked: " + wtoken
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003629 + ": hidden=" + wtoken.hidden + " hiddenRequested="
3630 + wtoken.hiddenRequested);
Romain Guy06882f82009-06-10 13:36:04 -07003631
Dianne Hackborn9b52a212009-12-11 14:51:35 -08003632 if (changed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003633 mLayoutNeeded = true;
Dianne Hackborn9b52a212009-12-11 14:51:35 -08003634 if (performLayout) {
3635 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
3636 performLayoutAndPlaceSurfacesLocked();
3637 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003638 }
3639 }
3640
3641 if (wtoken.animation != null) {
3642 delayed = true;
3643 }
Romain Guy06882f82009-06-10 13:36:04 -07003644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003645 return delayed;
3646 }
3647
3648 public void setAppVisibility(IBinder token, boolean visible) {
3649 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3650 "setAppVisibility()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003651 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003652 }
3653
3654 AppWindowToken wtoken;
3655
3656 synchronized(mWindowMap) {
3657 wtoken = findAppWindowToken(token);
3658 if (wtoken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003659 Slog.w(TAG, "Attempted to set visibility of non-existing app token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003660 return;
3661 }
3662
3663 if (DEBUG_APP_TRANSITIONS || DEBUG_ORIENTATION) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08003664 RuntimeException e = null;
3665 if (!HIDE_STACK_CRAWLS) {
3666 e = new RuntimeException();
3667 e.fillInStackTrace();
3668 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08003669 Slog.v(TAG, "setAppVisibility(" + token + ", " + visible
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003670 + "): mNextAppTransition=" + mNextAppTransition
3671 + " hidden=" + wtoken.hidden
3672 + " hiddenRequested=" + wtoken.hiddenRequested, e);
3673 }
Romain Guy06882f82009-06-10 13:36:04 -07003674
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003675 // If we are preparing an app transition, then delay changing
3676 // the visibility of this token until we execute that transition.
Dianne Hackbornb601ce12010-03-01 23:36:02 -08003677 if (!mDisplayFrozen && mPolicy.isScreenOn()
3678 && mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003679 // Already in requested state, don't do anything more.
3680 if (wtoken.hiddenRequested != visible) {
3681 return;
3682 }
3683 wtoken.hiddenRequested = !visible;
Romain Guy06882f82009-06-10 13:36:04 -07003684
Joe Onorato8a9b2202010-02-26 18:56:32 -08003685 if (DEBUG_APP_TRANSITIONS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003686 TAG, "Setting dummy animation on: " + wtoken);
3687 wtoken.setDummyAnimation();
3688 mOpeningApps.remove(wtoken);
3689 mClosingApps.remove(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003690 wtoken.waitingToShow = wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003691 wtoken.inPendingTransaction = true;
3692 if (visible) {
3693 mOpeningApps.add(wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003694 wtoken.startingDisplayed = false;
3695 wtoken.startingMoved = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003696
Dianne Hackborn195f6a02009-11-24 11:26:00 -08003697 // If the token is currently hidden (should be the
3698 // common case), then we need to set up to wait for
3699 // its windows to be ready.
3700 if (wtoken.hidden) {
3701 wtoken.allDrawn = false;
3702 wtoken.waitingToShow = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003703
Dianne Hackborn195f6a02009-11-24 11:26:00 -08003704 if (wtoken.clientHidden) {
3705 // In the case where we are making an app visible
3706 // but holding off for a transition, we still need
3707 // to tell the client to make its windows visible so
3708 // they get drawn. Otherwise, we will wait on
3709 // performing the transition until all windows have
3710 // been drawn, they never will be, and we are sad.
3711 wtoken.clientHidden = false;
3712 wtoken.sendAppVisibilityToClients();
3713 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003714 }
3715 } else {
3716 mClosingApps.add(wtoken);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003717
Dianne Hackborn195f6a02009-11-24 11:26:00 -08003718 // If the token is currently visible (should be the
3719 // common case), then set up to wait for it to be hidden.
3720 if (!wtoken.hidden) {
3721 wtoken.waitingToHide = true;
3722 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003723 }
3724 return;
3725 }
Romain Guy06882f82009-06-10 13:36:04 -07003726
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003727 final long origId = Binder.clearCallingIdentity();
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003728 setTokenVisibilityLocked(wtoken, null, visible, WindowManagerPolicy.TRANSIT_UNSET, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003729 wtoken.updateReportedVisibilityLocked();
3730 Binder.restoreCallingIdentity(origId);
3731 }
3732 }
3733
3734 void unsetAppFreezingScreenLocked(AppWindowToken wtoken,
3735 boolean unfreezeSurfaceNow, boolean force) {
3736 if (wtoken.freezingScreen) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003737 if (DEBUG_ORIENTATION) Slog.v(TAG, "Clear freezing of " + wtoken
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003738 + " force=" + force);
3739 final int N = wtoken.allAppWindows.size();
3740 boolean unfrozeWindows = false;
3741 for (int i=0; i<N; i++) {
3742 WindowState w = wtoken.allAppWindows.get(i);
3743 if (w.mAppFreezing) {
3744 w.mAppFreezing = false;
3745 if (w.mSurface != null && !w.mOrientationChanging) {
3746 w.mOrientationChanging = true;
3747 }
3748 unfrozeWindows = true;
3749 }
3750 }
3751 if (force || unfrozeWindows) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003752 if (DEBUG_ORIENTATION) Slog.v(TAG, "No longer freezing: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003753 wtoken.freezingScreen = false;
3754 mAppsFreezingScreen--;
3755 }
3756 if (unfreezeSurfaceNow) {
3757 if (unfrozeWindows) {
3758 mLayoutNeeded = true;
3759 performLayoutAndPlaceSurfacesLocked();
3760 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003761 stopFreezingDisplayLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003762 }
3763 }
3764 }
Romain Guy06882f82009-06-10 13:36:04 -07003765
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003766 public void startAppFreezingScreenLocked(AppWindowToken wtoken,
3767 int configChanges) {
3768 if (DEBUG_ORIENTATION) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08003769 RuntimeException e = null;
3770 if (!HIDE_STACK_CRAWLS) {
3771 e = new RuntimeException();
3772 e.fillInStackTrace();
3773 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08003774 Slog.i(TAG, "Set freezing of " + wtoken.appToken
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003775 + ": hidden=" + wtoken.hidden + " freezing="
3776 + wtoken.freezingScreen, e);
3777 }
3778 if (!wtoken.hiddenRequested) {
3779 if (!wtoken.freezingScreen) {
3780 wtoken.freezingScreen = true;
3781 mAppsFreezingScreen++;
3782 if (mAppsFreezingScreen == 1) {
3783 startFreezingDisplayLocked();
3784 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
3785 mH.sendMessageDelayed(mH.obtainMessage(H.APP_FREEZE_TIMEOUT),
3786 5000);
3787 }
3788 }
3789 final int N = wtoken.allAppWindows.size();
3790 for (int i=0; i<N; i++) {
3791 WindowState w = wtoken.allAppWindows.get(i);
3792 w.mAppFreezing = true;
3793 }
3794 }
3795 }
Romain Guy06882f82009-06-10 13:36:04 -07003796
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003797 public void startAppFreezingScreen(IBinder token, int configChanges) {
3798 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3799 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003800 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003801 }
3802
3803 synchronized(mWindowMap) {
Dianne Hackbornb601ce12010-03-01 23:36:02 -08003804 if (configChanges == 0 && !mDisplayFrozen && mPolicy.isScreenOn()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003805 if (DEBUG_ORIENTATION) Slog.v(TAG, "Skipping set freeze of " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003806 return;
3807 }
Romain Guy06882f82009-06-10 13:36:04 -07003808
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003809 AppWindowToken wtoken = findAppWindowToken(token);
3810 if (wtoken == null || wtoken.appToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003811 Slog.w(TAG, "Attempted to freeze screen with non-existing app token: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003812 return;
3813 }
3814 final long origId = Binder.clearCallingIdentity();
3815 startAppFreezingScreenLocked(wtoken, configChanges);
3816 Binder.restoreCallingIdentity(origId);
3817 }
3818 }
Romain Guy06882f82009-06-10 13:36:04 -07003819
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003820 public void stopAppFreezingScreen(IBinder token, boolean force) {
3821 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3822 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003823 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003824 }
3825
3826 synchronized(mWindowMap) {
3827 AppWindowToken wtoken = findAppWindowToken(token);
3828 if (wtoken == null || wtoken.appToken == null) {
3829 return;
3830 }
3831 final long origId = Binder.clearCallingIdentity();
Joe Onorato8a9b2202010-02-26 18:56:32 -08003832 if (DEBUG_ORIENTATION) Slog.v(TAG, "Clear freezing of " + token
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003833 + ": hidden=" + wtoken.hidden + " freezing=" + wtoken.freezingScreen);
3834 unsetAppFreezingScreenLocked(wtoken, true, force);
3835 Binder.restoreCallingIdentity(origId);
3836 }
3837 }
Romain Guy06882f82009-06-10 13:36:04 -07003838
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003839 public void removeAppToken(IBinder token) {
3840 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3841 "removeAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003842 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003843 }
3844
3845 AppWindowToken wtoken = null;
3846 AppWindowToken startingToken = null;
3847 boolean delayed = false;
3848
3849 final long origId = Binder.clearCallingIdentity();
3850 synchronized(mWindowMap) {
3851 WindowToken basewtoken = mTokenMap.remove(token);
3852 mTokenList.remove(basewtoken);
3853 if (basewtoken != null && (wtoken=basewtoken.appWindowToken) != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003854 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG, "Removing app token: " + wtoken);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003855 delayed = setTokenVisibilityLocked(wtoken, null, false, WindowManagerPolicy.TRANSIT_UNSET, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003856 wtoken.inPendingTransaction = false;
3857 mOpeningApps.remove(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003858 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003859 if (mClosingApps.contains(wtoken)) {
3860 delayed = true;
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003861 } else if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003862 mClosingApps.add(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003863 wtoken.waitingToHide = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003864 delayed = true;
3865 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08003866 if (DEBUG_APP_TRANSITIONS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003867 TAG, "Removing app " + wtoken + " delayed=" + delayed
3868 + " animation=" + wtoken.animation
3869 + " animating=" + wtoken.animating);
3870 if (delayed) {
3871 // set the token aside because it has an active animation to be finished
3872 mExitingAppTokens.add(wtoken);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003873 } else {
3874 // Make sure there is no animation running on this token,
3875 // so any windows associated with it will be removed as
3876 // soon as their animations are complete
3877 wtoken.animation = null;
3878 wtoken.animating = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003879 }
3880 mAppTokens.remove(wtoken);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003881 if (mLastEnterAnimToken == wtoken) {
3882 mLastEnterAnimToken = null;
3883 mLastEnterAnimParams = null;
3884 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003885 wtoken.removed = true;
3886 if (wtoken.startingData != null) {
3887 startingToken = wtoken;
3888 }
3889 unsetAppFreezingScreenLocked(wtoken, true, true);
3890 if (mFocusedApp == wtoken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003891 if (DEBUG_FOCUS) Slog.v(TAG, "Removing focused app token:" + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003892 mFocusedApp = null;
3893 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3894 mKeyWaiter.tickle();
3895 }
3896 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003897 Slog.w(TAG, "Attempted to remove non-existing app token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003898 }
Romain Guy06882f82009-06-10 13:36:04 -07003899
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003900 if (!delayed && wtoken != null) {
3901 wtoken.updateReportedVisibilityLocked();
3902 }
3903 }
3904 Binder.restoreCallingIdentity(origId);
3905
3906 if (startingToken != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003907 if (DEBUG_STARTING_WINDOW) Slog.v(TAG, "Schedule remove starting "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003908 + startingToken + ": app token removed");
3909 Message m = mH.obtainMessage(H.REMOVE_STARTING, startingToken);
3910 mH.sendMessage(m);
3911 }
3912 }
3913
3914 private boolean tmpRemoveAppWindowsLocked(WindowToken token) {
3915 final int NW = token.windows.size();
3916 for (int i=0; i<NW; i++) {
3917 WindowState win = token.windows.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003918 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "Tmp removing app window " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003919 mWindows.remove(win);
3920 int j = win.mChildWindows.size();
3921 while (j > 0) {
3922 j--;
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003923 WindowState cwin = (WindowState)win.mChildWindows.get(j);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003924 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG,
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003925 "Tmp removing child window " + cwin);
3926 mWindows.remove(cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003927 }
3928 }
3929 return NW > 0;
3930 }
3931
3932 void dumpAppTokensLocked() {
3933 for (int i=mAppTokens.size()-1; i>=0; i--) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003934 Slog.v(TAG, " #" + i + ": " + mAppTokens.get(i).token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003935 }
3936 }
Romain Guy06882f82009-06-10 13:36:04 -07003937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003938 void dumpWindowsLocked() {
3939 for (int i=mWindows.size()-1; i>=0; i--) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003940 Slog.v(TAG, " #" + i + ": " + mWindows.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003941 }
3942 }
Romain Guy06882f82009-06-10 13:36:04 -07003943
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003944 private int findWindowOffsetLocked(int tokenPos) {
3945 final int NW = mWindows.size();
3946
3947 if (tokenPos >= mAppTokens.size()) {
3948 int i = NW;
3949 while (i > 0) {
3950 i--;
3951 WindowState win = (WindowState)mWindows.get(i);
3952 if (win.getAppToken() != null) {
3953 return i+1;
3954 }
3955 }
3956 }
3957
3958 while (tokenPos > 0) {
3959 // Find the first app token below the new position that has
3960 // a window displayed.
3961 final AppWindowToken wtoken = mAppTokens.get(tokenPos-1);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003962 if (DEBUG_REORDER) Slog.v(TAG, "Looking for lower windows @ "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003963 + tokenPos + " -- " + wtoken.token);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003964 if (wtoken.sendingToBottom) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003965 if (DEBUG_REORDER) Slog.v(TAG,
Dianne Hackborna8f60182009-09-01 19:01:50 -07003966 "Skipping token -- currently sending to bottom");
3967 tokenPos--;
3968 continue;
3969 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003970 int i = wtoken.windows.size();
3971 while (i > 0) {
3972 i--;
3973 WindowState win = wtoken.windows.get(i);
3974 int j = win.mChildWindows.size();
3975 while (j > 0) {
3976 j--;
3977 WindowState cwin = (WindowState)win.mChildWindows.get(j);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003978 if (cwin.mSubLayer >= 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003979 for (int pos=NW-1; pos>=0; pos--) {
3980 if (mWindows.get(pos) == cwin) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003981 if (DEBUG_REORDER) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003982 "Found child win @" + (pos+1));
3983 return pos+1;
3984 }
3985 }
3986 }
3987 }
3988 for (int pos=NW-1; pos>=0; pos--) {
3989 if (mWindows.get(pos) == win) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003990 if (DEBUG_REORDER) Slog.v(TAG, "Found win @" + (pos+1));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003991 return pos+1;
3992 }
3993 }
3994 }
3995 tokenPos--;
3996 }
3997
3998 return 0;
3999 }
4000
4001 private final int reAddWindowLocked(int index, WindowState win) {
4002 final int NCW = win.mChildWindows.size();
4003 boolean added = false;
4004 for (int j=0; j<NCW; j++) {
4005 WindowState cwin = (WindowState)win.mChildWindows.get(j);
4006 if (!added && cwin.mSubLayer >= 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004007 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "Re-adding child window at "
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07004008 + index + ": " + cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004009 mWindows.add(index, win);
4010 index++;
4011 added = true;
4012 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08004013 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "Re-adding window at "
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07004014 + index + ": " + cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004015 mWindows.add(index, cwin);
4016 index++;
4017 }
4018 if (!added) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004019 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "Re-adding window at "
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07004020 + index + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004021 mWindows.add(index, win);
4022 index++;
4023 }
4024 return index;
4025 }
Romain Guy06882f82009-06-10 13:36:04 -07004026
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004027 private final int reAddAppWindowsLocked(int index, WindowToken token) {
4028 final int NW = token.windows.size();
4029 for (int i=0; i<NW; i++) {
4030 index = reAddWindowLocked(index, token.windows.get(i));
4031 }
4032 return index;
4033 }
4034
4035 public void moveAppToken(int index, IBinder token) {
4036 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4037 "moveAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004038 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004039 }
4040
4041 synchronized(mWindowMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004042 if (DEBUG_REORDER) Slog.v(TAG, "Initial app tokens:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004043 if (DEBUG_REORDER) dumpAppTokensLocked();
4044 final AppWindowToken wtoken = findAppWindowToken(token);
4045 if (wtoken == null || !mAppTokens.remove(wtoken)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004046 Slog.w(TAG, "Attempting to reorder token that doesn't exist: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004047 + token + " (" + wtoken + ")");
4048 return;
4049 }
4050 mAppTokens.add(index, wtoken);
Joe Onorato8a9b2202010-02-26 18:56:32 -08004051 if (DEBUG_REORDER) Slog.v(TAG, "Moved " + token + " to " + index + ":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004052 if (DEBUG_REORDER) dumpAppTokensLocked();
Romain Guy06882f82009-06-10 13:36:04 -07004053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004054 final long origId = Binder.clearCallingIdentity();
Joe Onorato8a9b2202010-02-26 18:56:32 -08004055 if (DEBUG_REORDER) Slog.v(TAG, "Removing windows in " + token + ":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004056 if (DEBUG_REORDER) dumpWindowsLocked();
4057 if (tmpRemoveAppWindowsLocked(wtoken)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004058 if (DEBUG_REORDER) Slog.v(TAG, "Adding windows back in:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004059 if (DEBUG_REORDER) dumpWindowsLocked();
4060 reAddAppWindowsLocked(findWindowOffsetLocked(index), wtoken);
Joe Onorato8a9b2202010-02-26 18:56:32 -08004061 if (DEBUG_REORDER) Slog.v(TAG, "Final window list:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004062 if (DEBUG_REORDER) dumpWindowsLocked();
4063 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004064 mLayoutNeeded = true;
4065 performLayoutAndPlaceSurfacesLocked();
4066 }
4067 Binder.restoreCallingIdentity(origId);
4068 }
4069 }
4070
4071 private void removeAppTokensLocked(List<IBinder> tokens) {
4072 // XXX This should be done more efficiently!
4073 // (take advantage of the fact that both lists should be
4074 // ordered in the same way.)
4075 int N = tokens.size();
4076 for (int i=0; i<N; i++) {
4077 IBinder token = tokens.get(i);
4078 final AppWindowToken wtoken = findAppWindowToken(token);
4079 if (!mAppTokens.remove(wtoken)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004080 Slog.w(TAG, "Attempting to reorder token that doesn't exist: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004081 + token + " (" + wtoken + ")");
4082 i--;
4083 N--;
4084 }
4085 }
4086 }
4087
Dianne Hackborna8f60182009-09-01 19:01:50 -07004088 private void moveAppWindowsLocked(AppWindowToken wtoken, int tokenPos,
4089 boolean updateFocusAndLayout) {
4090 // First remove all of the windows from the list.
4091 tmpRemoveAppWindowsLocked(wtoken);
4092
4093 // Where to start adding?
4094 int pos = findWindowOffsetLocked(tokenPos);
4095
4096 // And now add them back at the correct place.
4097 pos = reAddAppWindowsLocked(pos, wtoken);
4098
4099 if (updateFocusAndLayout) {
4100 if (!updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
4101 assignLayersLocked();
4102 }
4103 mLayoutNeeded = true;
4104 performLayoutAndPlaceSurfacesLocked();
4105 }
4106 }
4107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004108 private void moveAppWindowsLocked(List<IBinder> tokens, int tokenPos) {
4109 // First remove all of the windows from the list.
4110 final int N = tokens.size();
4111 int i;
4112 for (i=0; i<N; i++) {
4113 WindowToken token = mTokenMap.get(tokens.get(i));
4114 if (token != null) {
4115 tmpRemoveAppWindowsLocked(token);
4116 }
4117 }
4118
4119 // Where to start adding?
4120 int pos = findWindowOffsetLocked(tokenPos);
4121
4122 // And now add them back at the correct place.
4123 for (i=0; i<N; i++) {
4124 WindowToken token = mTokenMap.get(tokens.get(i));
4125 if (token != null) {
4126 pos = reAddAppWindowsLocked(pos, token);
4127 }
4128 }
4129
Dianne Hackborna8f60182009-09-01 19:01:50 -07004130 if (!updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
4131 assignLayersLocked();
4132 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004133 mLayoutNeeded = true;
4134 performLayoutAndPlaceSurfacesLocked();
4135
4136 //dump();
4137 }
4138
4139 public void moveAppTokensToTop(List<IBinder> tokens) {
4140 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4141 "moveAppTokensToTop()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004142 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004143 }
4144
4145 final long origId = Binder.clearCallingIdentity();
4146 synchronized(mWindowMap) {
4147 removeAppTokensLocked(tokens);
4148 final int N = tokens.size();
4149 for (int i=0; i<N; i++) {
4150 AppWindowToken wt = findAppWindowToken(tokens.get(i));
4151 if (wt != null) {
4152 mAppTokens.add(wt);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004153 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004154 mToTopApps.remove(wt);
4155 mToBottomApps.remove(wt);
4156 mToTopApps.add(wt);
4157 wt.sendingToBottom = false;
4158 wt.sendingToTop = true;
4159 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004160 }
4161 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004162
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004163 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004164 moveAppWindowsLocked(tokens, mAppTokens.size());
4165 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004166 }
4167 Binder.restoreCallingIdentity(origId);
4168 }
4169
4170 public void moveAppTokensToBottom(List<IBinder> tokens) {
4171 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4172 "moveAppTokensToBottom()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004173 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004174 }
4175
4176 final long origId = Binder.clearCallingIdentity();
4177 synchronized(mWindowMap) {
4178 removeAppTokensLocked(tokens);
4179 final int N = tokens.size();
4180 int pos = 0;
4181 for (int i=0; i<N; i++) {
4182 AppWindowToken wt = findAppWindowToken(tokens.get(i));
4183 if (wt != null) {
4184 mAppTokens.add(pos, wt);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004185 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004186 mToTopApps.remove(wt);
4187 mToBottomApps.remove(wt);
4188 mToBottomApps.add(i, wt);
4189 wt.sendingToTop = false;
4190 wt.sendingToBottom = true;
4191 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004192 pos++;
4193 }
4194 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004195
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004196 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004197 moveAppWindowsLocked(tokens, 0);
4198 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004199 }
4200 Binder.restoreCallingIdentity(origId);
4201 }
4202
4203 // -------------------------------------------------------------
4204 // Misc IWindowSession methods
4205 // -------------------------------------------------------------
Romain Guy06882f82009-06-10 13:36:04 -07004206
Jim Miller284b62e2010-06-08 14:27:42 -07004207 private boolean shouldAllowDisableKeyguard()
Jim Millerd6b57052010-06-07 17:52:42 -07004208 {
Jim Miller284b62e2010-06-08 14:27:42 -07004209 // We fail safe and prevent disabling keyguard in the unlikely event this gets
4210 // called before DevicePolicyManagerService has started.
4211 if (mAllowDisableKeyguard == ALLOW_DISABLE_UNKNOWN) {
4212 DevicePolicyManager dpm = (DevicePolicyManager) mContext.getSystemService(
4213 Context.DEVICE_POLICY_SERVICE);
4214 if (dpm != null) {
4215 mAllowDisableKeyguard = dpm.getPasswordQuality(null)
4216 == DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED ?
4217 ALLOW_DISABLE_YES : ALLOW_DISABLE_NO;
4218 }
Jim Millerd6b57052010-06-07 17:52:42 -07004219 }
Jim Miller284b62e2010-06-08 14:27:42 -07004220 return mAllowDisableKeyguard == ALLOW_DISABLE_YES;
Jim Millerd6b57052010-06-07 17:52:42 -07004221 }
4222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004223 public void disableKeyguard(IBinder token, String tag) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04004224 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004225 != PackageManager.PERMISSION_GRANTED) {
4226 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
4227 }
Jim Millerd6b57052010-06-07 17:52:42 -07004228
Jim Miller284b62e2010-06-08 14:27:42 -07004229 synchronized (mKeyguardTokenWatcher) {
4230 mKeyguardTokenWatcher.acquire(token, tag);
Mike Lockwooddd884682009-10-11 16:57:08 -04004231 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004232 }
4233
4234 public void reenableKeyguard(IBinder token) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04004235 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004236 != PackageManager.PERMISSION_GRANTED) {
4237 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
4238 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004239
Jim Miller284b62e2010-06-08 14:27:42 -07004240 synchronized (mKeyguardTokenWatcher) {
4241 mKeyguardTokenWatcher.release(token);
Jim Millerd6b57052010-06-07 17:52:42 -07004242
Jim Miller284b62e2010-06-08 14:27:42 -07004243 if (!mKeyguardTokenWatcher.isAcquired()) {
4244 // If we are the last one to reenable the keyguard wait until
4245 // we have actually finished reenabling until returning.
4246 // It is possible that reenableKeyguard() can be called before
4247 // the previous disableKeyguard() is handled, in which case
4248 // neither mKeyguardTokenWatcher.acquired() or released() would
4249 // be called. In that case mKeyguardDisabled will be false here
4250 // and we have nothing to wait for.
4251 while (mKeyguardDisabled) {
4252 try {
4253 mKeyguardTokenWatcher.wait();
4254 } catch (InterruptedException e) {
4255 Thread.currentThread().interrupt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004256 }
4257 }
4258 }
4259 }
4260 }
4261
4262 /**
4263 * @see android.app.KeyguardManager#exitKeyguardSecurely
4264 */
4265 public void exitKeyguardSecurely(final IOnKeyguardExitResult callback) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04004266 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004267 != PackageManager.PERMISSION_GRANTED) {
4268 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
4269 }
4270 mPolicy.exitKeyguardSecurely(new WindowManagerPolicy.OnKeyguardExitResult() {
4271 public void onKeyguardExitResult(boolean success) {
4272 try {
4273 callback.onKeyguardExitResult(success);
4274 } catch (RemoteException e) {
4275 // Client has died, we don't care.
4276 }
4277 }
4278 });
4279 }
4280
4281 public boolean inKeyguardRestrictedInputMode() {
4282 return mPolicy.inKeyguardRestrictedKeyInputMode();
4283 }
Romain Guy06882f82009-06-10 13:36:04 -07004284
Dianne Hackbornffa42482009-09-23 22:20:11 -07004285 public void closeSystemDialogs(String reason) {
4286 synchronized(mWindowMap) {
4287 for (int i=mWindows.size()-1; i>=0; i--) {
4288 WindowState w = (WindowState)mWindows.get(i);
4289 if (w.mSurface != null) {
4290 try {
4291 w.mClient.closeSystemDialogs(reason);
4292 } catch (RemoteException e) {
4293 }
4294 }
4295 }
4296 }
4297 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004299 static float fixScale(float scale) {
4300 if (scale < 0) scale = 0;
4301 else if (scale > 20) scale = 20;
4302 return Math.abs(scale);
4303 }
Romain Guy06882f82009-06-10 13:36:04 -07004304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004305 public void setAnimationScale(int which, float scale) {
4306 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
4307 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004308 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004309 }
4310
4311 if (scale < 0) scale = 0;
4312 else if (scale > 20) scale = 20;
4313 scale = Math.abs(scale);
4314 switch (which) {
4315 case 0: mWindowAnimationScale = fixScale(scale); break;
4316 case 1: mTransitionAnimationScale = fixScale(scale); break;
4317 }
Romain Guy06882f82009-06-10 13:36:04 -07004318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004319 // Persist setting
4320 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
4321 }
Romain Guy06882f82009-06-10 13:36:04 -07004322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004323 public void setAnimationScales(float[] scales) {
4324 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
4325 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004326 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004327 }
4328
4329 if (scales != null) {
4330 if (scales.length >= 1) {
4331 mWindowAnimationScale = fixScale(scales[0]);
4332 }
4333 if (scales.length >= 2) {
4334 mTransitionAnimationScale = fixScale(scales[1]);
4335 }
4336 }
Romain Guy06882f82009-06-10 13:36:04 -07004337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004338 // Persist setting
4339 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
4340 }
Romain Guy06882f82009-06-10 13:36:04 -07004341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004342 public float getAnimationScale(int which) {
4343 switch (which) {
4344 case 0: return mWindowAnimationScale;
4345 case 1: return mTransitionAnimationScale;
4346 }
4347 return 0;
4348 }
Romain Guy06882f82009-06-10 13:36:04 -07004349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004350 public float[] getAnimationScales() {
4351 return new float[] { mWindowAnimationScale, mTransitionAnimationScale };
4352 }
Romain Guy06882f82009-06-10 13:36:04 -07004353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004354 public int getSwitchState(int sw) {
4355 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4356 "getSwitchState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004357 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004358 }
4359 return KeyInputQueue.getSwitchState(sw);
4360 }
Romain Guy06882f82009-06-10 13:36:04 -07004361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004362 public int getSwitchStateForDevice(int devid, int sw) {
4363 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4364 "getSwitchStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004365 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004366 }
4367 return KeyInputQueue.getSwitchState(devid, sw);
4368 }
Romain Guy06882f82009-06-10 13:36:04 -07004369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004370 public int getScancodeState(int sw) {
4371 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4372 "getScancodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004373 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004374 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004375 return mQueue.getScancodeState(sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004376 }
Romain Guy06882f82009-06-10 13:36:04 -07004377
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004378 public int getScancodeStateForDevice(int devid, int sw) {
4379 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4380 "getScancodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004381 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004382 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004383 return mQueue.getScancodeState(devid, sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004384 }
Romain Guy06882f82009-06-10 13:36:04 -07004385
Dianne Hackborn1d62ea92009-11-17 12:49:50 -08004386 public int getTrackballScancodeState(int sw) {
4387 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4388 "getTrackballScancodeState()")) {
4389 throw new SecurityException("Requires READ_INPUT_STATE permission");
4390 }
4391 return mQueue.getTrackballScancodeState(sw);
4392 }
4393
4394 public int getDPadScancodeState(int sw) {
4395 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4396 "getDPadScancodeState()")) {
4397 throw new SecurityException("Requires READ_INPUT_STATE permission");
4398 }
4399 return mQueue.getDPadScancodeState(sw);
4400 }
4401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004402 public int getKeycodeState(int sw) {
4403 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4404 "getKeycodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004405 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004406 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004407 return mQueue.getKeycodeState(sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004408 }
Romain Guy06882f82009-06-10 13:36:04 -07004409
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004410 public int getKeycodeStateForDevice(int devid, int sw) {
4411 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4412 "getKeycodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004413 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004414 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004415 return mQueue.getKeycodeState(devid, sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004416 }
Romain Guy06882f82009-06-10 13:36:04 -07004417
Dianne Hackborn1d62ea92009-11-17 12:49:50 -08004418 public int getTrackballKeycodeState(int sw) {
4419 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4420 "getTrackballKeycodeState()")) {
4421 throw new SecurityException("Requires READ_INPUT_STATE permission");
4422 }
4423 return mQueue.getTrackballKeycodeState(sw);
4424 }
4425
4426 public int getDPadKeycodeState(int sw) {
4427 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4428 "getDPadKeycodeState()")) {
4429 throw new SecurityException("Requires READ_INPUT_STATE permission");
4430 }
4431 return mQueue.getDPadKeycodeState(sw);
4432 }
4433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004434 public boolean hasKeys(int[] keycodes, boolean[] keyExists) {
4435 return KeyInputQueue.hasKeys(keycodes, keyExists);
4436 }
Romain Guy06882f82009-06-10 13:36:04 -07004437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004438 public void enableScreenAfterBoot() {
4439 synchronized(mWindowMap) {
4440 if (mSystemBooted) {
4441 return;
4442 }
4443 mSystemBooted = true;
4444 }
Romain Guy06882f82009-06-10 13:36:04 -07004445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004446 performEnableScreen();
4447 }
Romain Guy06882f82009-06-10 13:36:04 -07004448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004449 public void enableScreenIfNeededLocked() {
4450 if (mDisplayEnabled) {
4451 return;
4452 }
4453 if (!mSystemBooted) {
4454 return;
4455 }
4456 mH.sendMessage(mH.obtainMessage(H.ENABLE_SCREEN));
4457 }
Romain Guy06882f82009-06-10 13:36:04 -07004458
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004459 public void performEnableScreen() {
4460 synchronized(mWindowMap) {
4461 if (mDisplayEnabled) {
4462 return;
4463 }
4464 if (!mSystemBooted) {
4465 return;
4466 }
Romain Guy06882f82009-06-10 13:36:04 -07004467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004468 // Don't enable the screen until all existing windows
4469 // have been drawn.
4470 final int N = mWindows.size();
4471 for (int i=0; i<N; i++) {
4472 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn5943c202010-04-12 21:36:49 -07004473 if (w.isVisibleLw() && !w.mObscured
4474 && (w.mOrientationChanging || !w.isDrawnLw())) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004475 return;
4476 }
4477 }
Romain Guy06882f82009-06-10 13:36:04 -07004478
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004479 mDisplayEnabled = true;
4480 if (false) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004481 Slog.i(TAG, "ENABLING SCREEN!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004482 StringWriter sw = new StringWriter();
4483 PrintWriter pw = new PrintWriter(sw);
4484 this.dump(null, pw, null);
Joe Onorato8a9b2202010-02-26 18:56:32 -08004485 Slog.i(TAG, sw.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004486 }
4487 try {
4488 IBinder surfaceFlinger = ServiceManager.getService("SurfaceFlinger");
4489 if (surfaceFlinger != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004490 //Slog.i(TAG, "******* TELLING SURFACE FLINGER WE ARE BOOTED!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004491 Parcel data = Parcel.obtain();
4492 data.writeInterfaceToken("android.ui.ISurfaceComposer");
4493 surfaceFlinger.transact(IBinder.FIRST_CALL_TRANSACTION,
4494 data, null, 0);
4495 data.recycle();
4496 }
4497 } catch (RemoteException ex) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004498 Slog.e(TAG, "Boot completed: SurfaceFlinger is dead!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004499 }
4500 }
Romain Guy06882f82009-06-10 13:36:04 -07004501
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004502 mPolicy.enableScreenAfterBoot();
Romain Guy06882f82009-06-10 13:36:04 -07004503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004504 // Make sure the last requested orientation has been applied.
Dianne Hackborn321ae682009-03-27 16:16:03 -07004505 setRotationUnchecked(WindowManagerPolicy.USE_LAST_ROTATION, false,
4506 mLastRotationFlags | Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004507 }
Romain Guy06882f82009-06-10 13:36:04 -07004508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004509 public void setInTouchMode(boolean mode) {
4510 synchronized(mWindowMap) {
4511 mInTouchMode = mode;
4512 }
4513 }
4514
Romain Guy06882f82009-06-10 13:36:04 -07004515 public void setRotation(int rotation,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004516 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004517 if (!checkCallingPermission(android.Manifest.permission.SET_ORIENTATION,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004518 "setRotation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004519 throw new SecurityException("Requires SET_ORIENTATION permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004520 }
4521
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004522 setRotationUnchecked(rotation, alwaysSendConfiguration, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004523 }
Romain Guy06882f82009-06-10 13:36:04 -07004524
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004525 public void setRotationUnchecked(int rotation,
4526 boolean alwaysSendConfiguration, int animFlags) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004527 if(DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004528 "alwaysSendConfiguration set to "+alwaysSendConfiguration);
Romain Guy06882f82009-06-10 13:36:04 -07004529
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004530 long origId = Binder.clearCallingIdentity();
4531 boolean changed;
4532 synchronized(mWindowMap) {
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004533 changed = setRotationUncheckedLocked(rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004534 }
Romain Guy06882f82009-06-10 13:36:04 -07004535
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004536 if (changed || alwaysSendConfiguration) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004537 sendNewConfiguration();
4538 }
Romain Guy06882f82009-06-10 13:36:04 -07004539
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004540 Binder.restoreCallingIdentity(origId);
4541 }
Romain Guy06882f82009-06-10 13:36:04 -07004542
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004543 /**
4544 * Apply a new rotation to the screen, respecting the requests of
4545 * applications. Use WindowManagerPolicy.USE_LAST_ROTATION to simply
4546 * re-evaluate the desired rotation.
4547 *
4548 * Returns null if the rotation has been changed. In this case YOU
4549 * MUST CALL setNewConfiguration() TO UNFREEZE THE SCREEN.
4550 */
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004551 public boolean setRotationUncheckedLocked(int rotation, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004552 boolean changed;
4553 if (rotation == WindowManagerPolicy.USE_LAST_ROTATION) {
4554 rotation = mRequestedRotation;
4555 } else {
4556 mRequestedRotation = rotation;
Dianne Hackborn321ae682009-03-27 16:16:03 -07004557 mLastRotationFlags = animFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004558 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08004559 if (DEBUG_ORIENTATION) Slog.v(TAG, "Overwriting rotation value from " + rotation);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004560 rotation = mPolicy.rotationForOrientationLw(mForcedAppOrientation,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004561 mRotation, mDisplayEnabled);
Joe Onorato8a9b2202010-02-26 18:56:32 -08004562 if (DEBUG_ORIENTATION) Slog.v(TAG, "new rotation is set to " + rotation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004563 changed = mDisplayEnabled && mRotation != rotation;
Romain Guy06882f82009-06-10 13:36:04 -07004564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004565 if (changed) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004566 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004567 "Rotation changed to " + rotation
4568 + " from " + mRotation
4569 + " (forceApp=" + mForcedAppOrientation
4570 + ", req=" + mRequestedRotation + ")");
4571 mRotation = rotation;
4572 mWindowsFreezingScreen = true;
4573 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
4574 mH.sendMessageDelayed(mH.obtainMessage(H.WINDOW_FREEZE_TIMEOUT),
4575 2000);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004576 mWaitingForConfig = true;
4577 mLayoutNeeded = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004578 startFreezingDisplayLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08004579 Slog.i(TAG, "Setting rotation to " + rotation + ", animFlags=" + animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004580 mQueue.setOrientation(rotation);
4581 if (mDisplayEnabled) {
Dianne Hackborn321ae682009-03-27 16:16:03 -07004582 Surface.setOrientation(0, rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004583 }
4584 for (int i=mWindows.size()-1; i>=0; i--) {
4585 WindowState w = (WindowState)mWindows.get(i);
4586 if (w.mSurface != null) {
4587 w.mOrientationChanging = true;
4588 }
4589 }
4590 for (int i=mRotationWatchers.size()-1; i>=0; i--) {
4591 try {
4592 mRotationWatchers.get(i).onRotationChanged(rotation);
4593 } catch (RemoteException e) {
4594 }
4595 }
4596 } //end if changed
Romain Guy06882f82009-06-10 13:36:04 -07004597
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004598 return changed;
4599 }
Romain Guy06882f82009-06-10 13:36:04 -07004600
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004601 public int getRotation() {
4602 return mRotation;
4603 }
4604
4605 public int watchRotation(IRotationWatcher watcher) {
4606 final IBinder watcherBinder = watcher.asBinder();
4607 IBinder.DeathRecipient dr = new IBinder.DeathRecipient() {
4608 public void binderDied() {
4609 synchronized (mWindowMap) {
4610 for (int i=0; i<mRotationWatchers.size(); i++) {
4611 if (watcherBinder == mRotationWatchers.get(i).asBinder()) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07004612 IRotationWatcher removed = mRotationWatchers.remove(i);
4613 if (removed != null) {
4614 removed.asBinder().unlinkToDeath(this, 0);
4615 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004616 i--;
4617 }
4618 }
4619 }
4620 }
4621 };
Romain Guy06882f82009-06-10 13:36:04 -07004622
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004623 synchronized (mWindowMap) {
4624 try {
4625 watcher.asBinder().linkToDeath(dr, 0);
4626 mRotationWatchers.add(watcher);
4627 } catch (RemoteException e) {
4628 // Client died, no cleanup needed.
4629 }
Romain Guy06882f82009-06-10 13:36:04 -07004630
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004631 return mRotation;
4632 }
4633 }
4634
4635 /**
4636 * Starts the view server on the specified port.
4637 *
4638 * @param port The port to listener to.
4639 *
4640 * @return True if the server was successfully started, false otherwise.
4641 *
4642 * @see com.android.server.ViewServer
4643 * @see com.android.server.ViewServer#VIEW_SERVER_DEFAULT_PORT
4644 */
4645 public boolean startViewServer(int port) {
Romain Guy06882f82009-06-10 13:36:04 -07004646 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004647 return false;
4648 }
4649
4650 if (!checkCallingPermission(Manifest.permission.DUMP, "startViewServer")) {
4651 return false;
4652 }
4653
4654 if (port < 1024) {
4655 return false;
4656 }
4657
4658 if (mViewServer != null) {
4659 if (!mViewServer.isRunning()) {
4660 try {
4661 return mViewServer.start();
4662 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004663 Slog.w(TAG, "View server did not start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004664 }
4665 }
4666 return false;
4667 }
4668
4669 try {
4670 mViewServer = new ViewServer(this, port);
4671 return mViewServer.start();
4672 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004673 Slog.w(TAG, "View server did not start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004674 }
4675 return false;
4676 }
4677
Romain Guy06882f82009-06-10 13:36:04 -07004678 private boolean isSystemSecure() {
4679 return "1".equals(SystemProperties.get(SYSTEM_SECURE, "1")) &&
4680 "0".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
4681 }
4682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004683 /**
4684 * Stops the view server if it exists.
4685 *
4686 * @return True if the server stopped, false if it wasn't started or
4687 * couldn't be stopped.
4688 *
4689 * @see com.android.server.ViewServer
4690 */
4691 public boolean stopViewServer() {
Romain Guy06882f82009-06-10 13:36:04 -07004692 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004693 return false;
4694 }
4695
4696 if (!checkCallingPermission(Manifest.permission.DUMP, "stopViewServer")) {
4697 return false;
4698 }
4699
4700 if (mViewServer != null) {
4701 return mViewServer.stop();
4702 }
4703 return false;
4704 }
4705
4706 /**
4707 * Indicates whether the view server is running.
4708 *
4709 * @return True if the server is running, false otherwise.
4710 *
4711 * @see com.android.server.ViewServer
4712 */
4713 public boolean isViewServerRunning() {
Romain Guy06882f82009-06-10 13:36:04 -07004714 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004715 return false;
4716 }
4717
4718 if (!checkCallingPermission(Manifest.permission.DUMP, "isViewServerRunning")) {
4719 return false;
4720 }
4721
4722 return mViewServer != null && mViewServer.isRunning();
4723 }
4724
4725 /**
4726 * Lists all availble windows in the system. The listing is written in the
4727 * specified Socket's output stream with the following syntax:
4728 * windowHashCodeInHexadecimal windowName
4729 * Each line of the ouput represents a different window.
4730 *
4731 * @param client The remote client to send the listing to.
4732 * @return False if an error occured, true otherwise.
4733 */
4734 boolean viewServerListWindows(Socket client) {
Romain Guy06882f82009-06-10 13:36:04 -07004735 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004736 return false;
4737 }
4738
4739 boolean result = true;
4740
4741 Object[] windows;
4742 synchronized (mWindowMap) {
4743 windows = new Object[mWindows.size()];
4744 //noinspection unchecked
4745 windows = mWindows.toArray(windows);
4746 }
4747
4748 BufferedWriter out = null;
4749
4750 // Any uncaught exception will crash the system process
4751 try {
4752 OutputStream clientStream = client.getOutputStream();
4753 out = new BufferedWriter(new OutputStreamWriter(clientStream), 8 * 1024);
4754
4755 final int count = windows.length;
4756 for (int i = 0; i < count; i++) {
4757 final WindowState w = (WindowState) windows[i];
4758 out.write(Integer.toHexString(System.identityHashCode(w)));
4759 out.write(' ');
4760 out.append(w.mAttrs.getTitle());
4761 out.write('\n');
4762 }
4763
4764 out.write("DONE.\n");
4765 out.flush();
4766 } catch (Exception e) {
4767 result = false;
4768 } finally {
4769 if (out != null) {
4770 try {
4771 out.close();
4772 } catch (IOException e) {
4773 result = false;
4774 }
4775 }
4776 }
4777
4778 return result;
4779 }
4780
4781 /**
4782 * Sends a command to a target window. The result of the command, if any, will be
4783 * written in the output stream of the specified socket.
4784 *
4785 * The parameters must follow this syntax:
4786 * windowHashcode extra
4787 *
4788 * Where XX is the length in characeters of the windowTitle.
4789 *
4790 * The first parameter is the target window. The window with the specified hashcode
4791 * will be the target. If no target can be found, nothing happens. The extra parameters
4792 * will be delivered to the target window and as parameters to the command itself.
4793 *
4794 * @param client The remote client to sent the result, if any, to.
4795 * @param command The command to execute.
4796 * @param parameters The command parameters.
4797 *
4798 * @return True if the command was successfully delivered, false otherwise. This does
4799 * not indicate whether the command itself was successful.
4800 */
4801 boolean viewServerWindowCommand(Socket client, String command, String parameters) {
Romain Guy06882f82009-06-10 13:36:04 -07004802 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004803 return false;
4804 }
4805
4806 boolean success = true;
4807 Parcel data = null;
4808 Parcel reply = null;
4809
4810 // Any uncaught exception will crash the system process
4811 try {
4812 // Find the hashcode of the window
4813 int index = parameters.indexOf(' ');
4814 if (index == -1) {
4815 index = parameters.length();
4816 }
4817 final String code = parameters.substring(0, index);
Romain Guy236092a2009-12-14 15:31:48 -08004818 int hashCode = (int) Long.parseLong(code, 16);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004819
4820 // Extract the command's parameter after the window description
4821 if (index < parameters.length()) {
4822 parameters = parameters.substring(index + 1);
4823 } else {
4824 parameters = "";
4825 }
4826
4827 final WindowManagerService.WindowState window = findWindow(hashCode);
4828 if (window == null) {
4829 return false;
4830 }
4831
4832 data = Parcel.obtain();
4833 data.writeInterfaceToken("android.view.IWindow");
4834 data.writeString(command);
4835 data.writeString(parameters);
4836 data.writeInt(1);
4837 ParcelFileDescriptor.fromSocket(client).writeToParcel(data, 0);
4838
4839 reply = Parcel.obtain();
4840
4841 final IBinder binder = window.mClient.asBinder();
4842 // TODO: GET THE TRANSACTION CODE IN A SAFER MANNER
4843 binder.transact(IBinder.FIRST_CALL_TRANSACTION, data, reply, 0);
4844
4845 reply.readException();
4846
4847 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004848 Slog.w(TAG, "Could not send command " + command + " with parameters " + parameters, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004849 success = false;
4850 } finally {
4851 if (data != null) {
4852 data.recycle();
4853 }
4854 if (reply != null) {
4855 reply.recycle();
4856 }
4857 }
4858
4859 return success;
4860 }
4861
4862 private WindowState findWindow(int hashCode) {
4863 if (hashCode == -1) {
4864 return getFocusedWindow();
4865 }
4866
4867 synchronized (mWindowMap) {
4868 final ArrayList windows = mWindows;
4869 final int count = windows.size();
4870
4871 for (int i = 0; i < count; i++) {
4872 WindowState w = (WindowState) windows.get(i);
4873 if (System.identityHashCode(w) == hashCode) {
4874 return w;
4875 }
4876 }
4877 }
4878
4879 return null;
4880 }
4881
4882 /*
4883 * Instruct the Activity Manager to fetch the current configuration and broadcast
4884 * that to config-changed listeners if appropriate.
4885 */
4886 void sendNewConfiguration() {
4887 try {
4888 mActivityManager.updateConfiguration(null);
4889 } catch (RemoteException e) {
4890 }
4891 }
Romain Guy06882f82009-06-10 13:36:04 -07004892
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004893 public Configuration computeNewConfiguration() {
4894 synchronized (mWindowMap) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07004895 return computeNewConfigurationLocked();
4896 }
4897 }
Romain Guy06882f82009-06-10 13:36:04 -07004898
Dianne Hackbornc485a602009-03-24 22:39:49 -07004899 Configuration computeNewConfigurationLocked() {
4900 Configuration config = new Configuration();
4901 if (!computeNewConfigurationLocked(config)) {
4902 return null;
4903 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07004904 return config;
4905 }
Romain Guy06882f82009-06-10 13:36:04 -07004906
Dianne Hackbornc485a602009-03-24 22:39:49 -07004907 boolean computeNewConfigurationLocked(Configuration config) {
4908 if (mDisplay == null) {
4909 return false;
4910 }
4911 mQueue.getInputConfiguration(config);
Christopher Tateb696aee2010-04-02 19:08:30 -07004912
4913 // Use the effective "visual" dimensions based on current rotation
4914 final boolean rotated = (mRotation == Surface.ROTATION_90
4915 || mRotation == Surface.ROTATION_270);
4916 final int dw = rotated ? mInitialDisplayHeight : mInitialDisplayWidth;
4917 final int dh = rotated ? mInitialDisplayWidth : mInitialDisplayHeight;
4918
Dianne Hackbornc485a602009-03-24 22:39:49 -07004919 int orientation = Configuration.ORIENTATION_SQUARE;
4920 if (dw < dh) {
4921 orientation = Configuration.ORIENTATION_PORTRAIT;
4922 } else if (dw > dh) {
4923 orientation = Configuration.ORIENTATION_LANDSCAPE;
4924 }
4925 config.orientation = orientation;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004926
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004927 DisplayMetrics dm = new DisplayMetrics();
4928 mDisplay.getMetrics(dm);
4929 CompatibilityInfo.updateCompatibleScreenFrame(dm, orientation, mCompatibleScreenFrame);
4930
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004931 if (mScreenLayout == Configuration.SCREENLAYOUT_SIZE_UNDEFINED) {
Dianne Hackborn723738c2009-06-25 19:48:04 -07004932 // Note we only do this once because at this point we don't
4933 // expect the screen to change in this way at runtime, and want
4934 // to avoid all of this computation for every config change.
Dianne Hackborn723738c2009-06-25 19:48:04 -07004935 int longSize = dw;
4936 int shortSize = dh;
4937 if (longSize < shortSize) {
4938 int tmp = longSize;
4939 longSize = shortSize;
4940 shortSize = tmp;
4941 }
4942 longSize = (int)(longSize/dm.density);
4943 shortSize = (int)(shortSize/dm.density);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004944
Dianne Hackborn723738c2009-06-25 19:48:04 -07004945 // These semi-magic numbers define our compatibility modes for
4946 // applications with different screens. Don't change unless you
4947 // make sure to test lots and lots of apps!
4948 if (longSize < 470) {
4949 // This is shorter than an HVGA normal density screen (which
4950 // is 480 pixels on its long side).
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004951 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_SMALL
4952 | Configuration.SCREENLAYOUT_LONG_NO;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004953 } else {
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004954 // Is this a large screen?
4955 if (longSize > 640 && shortSize >= 480) {
4956 // VGA or larger screens at medium density are the point
4957 // at which we consider it to be a large screen.
4958 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_LARGE;
4959 } else {
4960 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_NORMAL;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004961
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004962 // If this screen is wider than normal HVGA, or taller
4963 // than FWVGA, then for old apps we want to run in size
4964 // compatibility mode.
4965 if (shortSize > 321 || longSize > 570) {
4966 mScreenLayout |= Configuration.SCREENLAYOUT_COMPAT_NEEDED;
4967 }
4968 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004969
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004970 // Is this a long screen?
4971 if (((longSize*3)/5) >= (shortSize-1)) {
4972 // Anything wider than WVGA (5:3) is considering to be long.
4973 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_YES;
4974 } else {
4975 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_NO;
4976 }
Dianne Hackborn723738c2009-06-25 19:48:04 -07004977 }
4978 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004979 config.screenLayout = mScreenLayout;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004980
Dianne Hackbornc485a602009-03-24 22:39:49 -07004981 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
4982 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
4983 mPolicy.adjustConfigurationLw(config);
4984 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004985 }
Romain Guy06882f82009-06-10 13:36:04 -07004986
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004987 // -------------------------------------------------------------
4988 // Input Events and Focus Management
4989 // -------------------------------------------------------------
4990
4991 private final void wakeupIfNeeded(WindowState targetWin, int eventType) {
Michael Chane96440f2009-05-06 10:27:36 -07004992 long curTime = SystemClock.uptimeMillis();
4993
Michael Chane10de972009-05-18 11:24:50 -07004994 if (eventType == TOUCH_EVENT || eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT) {
Michael Chane96440f2009-05-06 10:27:36 -07004995 if (mLastTouchEventType == eventType &&
4996 (curTime - mLastUserActivityCallTime) < MIN_TIME_BETWEEN_USERACTIVITIES) {
4997 return;
4998 }
4999 mLastUserActivityCallTime = curTime;
5000 mLastTouchEventType = eventType;
5001 }
5002
5003 if (targetWin == null
5004 || targetWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
5005 mPowerManager.userActivity(curTime, false, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005006 }
5007 }
5008
5009 // tells if it's a cheek event or not -- this function is stateful
5010 private static final int EVENT_NONE = 0;
5011 private static final int EVENT_UNKNOWN = 0;
5012 private static final int EVENT_CHEEK = 0;
5013 private static final int EVENT_IGNORE_DURATION = 300; // ms
5014 private static final float CHEEK_THRESHOLD = 0.6f;
5015 private int mEventState = EVENT_NONE;
5016 private float mEventSize;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07005017
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005018 private int eventType(MotionEvent ev) {
5019 float size = ev.getSize();
5020 switch (ev.getAction()) {
5021 case MotionEvent.ACTION_DOWN:
5022 mEventSize = size;
5023 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_EVENT;
5024 case MotionEvent.ACTION_UP:
5025 if (size > mEventSize) mEventSize = size;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07005026 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005027 case MotionEvent.ACTION_MOVE:
5028 final int N = ev.getHistorySize();
5029 if (size > mEventSize) mEventSize = size;
5030 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
5031 for (int i=0; i<N; i++) {
5032 size = ev.getHistoricalSize(i);
5033 if (size > mEventSize) mEventSize = size;
5034 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
5035 }
5036 if (ev.getEventTime() < ev.getDownTime() + EVENT_IGNORE_DURATION) {
5037 return TOUCH_EVENT;
5038 } else {
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07005039 return LONG_TOUCH_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005040 }
5041 default:
5042 // not good
5043 return OTHER_EVENT;
5044 }
5045 }
5046
5047 /**
5048 * @return Returns true if event was dispatched, false if it was dropped for any reason
5049 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07005050 private int dispatchPointer(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005051 if (DEBUG_INPUT || WindowManagerPolicy.WATCH_POINTER) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005052 "dispatchPointer " + ev);
5053
Michael Chan53071d62009-05-13 17:29:48 -07005054 if (MEASURE_LATENCY) {
5055 lt.sample("3 Wait for last dispatch ", System.nanoTime() - qev.whenNano);
5056 }
5057
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005058 Object targetObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005059 ev, true, false, pid, uid);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005060
Michael Chan53071d62009-05-13 17:29:48 -07005061 if (MEASURE_LATENCY) {
5062 lt.sample("3 Last dispatch finished ", System.nanoTime() - qev.whenNano);
5063 }
5064
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005065 int action = ev.getAction();
Romain Guy06882f82009-06-10 13:36:04 -07005066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005067 if (action == MotionEvent.ACTION_UP) {
5068 // let go of our target
5069 mKeyWaiter.mMotionTarget = null;
5070 mPowerManager.logPointerUpEvent();
5071 } else if (action == MotionEvent.ACTION_DOWN) {
5072 mPowerManager.logPointerDownEvent();
5073 }
5074
5075 if (targetObj == null) {
5076 // In this case we are either dropping the event, or have received
5077 // a move or up without a down. It is common to receive move
5078 // events in such a way, since this means the user is moving the
5079 // pointer without actually pressing down. All other cases should
5080 // be atypical, so let's log them.
Michael Chane96440f2009-05-06 10:27:36 -07005081 if (action != MotionEvent.ACTION_MOVE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005082 Slog.w(TAG, "No window to dispatch pointer action " + ev.getAction());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005083 }
Dianne Hackborn6adba242009-11-10 11:10:09 -08005084 synchronized (mWindowMap) {
Dianne Hackborn90d2db32010-02-11 22:19:06 -08005085 dispatchPointerElsewhereLocked(null, null, ev, ev.getEventTime(), true);
Dianne Hackborn6adba242009-11-10 11:10:09 -08005086 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005087 if (qev != null) {
5088 mQueue.recycleEvent(qev);
5089 }
5090 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005091 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005092 }
5093 if (targetObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborn6adba242009-11-10 11:10:09 -08005094 synchronized (mWindowMap) {
Dianne Hackborn90d2db32010-02-11 22:19:06 -08005095 dispatchPointerElsewhereLocked(null, null, ev, ev.getEventTime(), true);
Dianne Hackborn6adba242009-11-10 11:10:09 -08005096 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005097 if (qev != null) {
5098 mQueue.recycleEvent(qev);
5099 }
5100 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005101 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005102 }
Romain Guy06882f82009-06-10 13:36:04 -07005103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005104 WindowState target = (WindowState)targetObj;
Romain Guy06882f82009-06-10 13:36:04 -07005105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005106 final long eventTime = ev.getEventTime();
Michael Chan53071d62009-05-13 17:29:48 -07005107 final long eventTimeNano = ev.getEventTimeNano();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005108
Joe Onorato8a9b2202010-02-26 18:56:32 -08005109 //Slog.i(TAG, "Sending " + ev + " to " + target);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005110
5111 if (uid != 0 && uid != target.mSession.mUid) {
5112 if (mContext.checkPermission(
5113 android.Manifest.permission.INJECT_EVENTS, pid, uid)
5114 != PackageManager.PERMISSION_GRANTED) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005115 Slog.w(TAG, "Permission denied: injecting pointer event from pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005116 + pid + " uid " + uid + " to window " + target
5117 + " owned by uid " + target.mSession.mUid);
5118 if (qev != null) {
5119 mQueue.recycleEvent(qev);
5120 }
5121 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005122 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005123 }
5124 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005125
Michael Chan53071d62009-05-13 17:29:48 -07005126 if (MEASURE_LATENCY) {
5127 lt.sample("4 in dispatchPointer ", System.nanoTime() - eventTimeNano);
5128 }
5129
Romain Guy06882f82009-06-10 13:36:04 -07005130 if ((target.mAttrs.flags &
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005131 WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES) != 0) {
5132 //target wants to ignore fat touch events
5133 boolean cheekPress = mPolicy.isCheekPressedAgainstScreen(ev);
5134 //explicit flag to return without processing event further
5135 boolean returnFlag = false;
5136 if((action == MotionEvent.ACTION_DOWN)) {
5137 mFatTouch = false;
5138 if(cheekPress) {
5139 mFatTouch = true;
5140 returnFlag = true;
5141 }
5142 } else {
5143 if(action == MotionEvent.ACTION_UP) {
5144 if(mFatTouch) {
5145 //earlier even was invalid doesnt matter if current up is cheekpress or not
5146 mFatTouch = false;
5147 returnFlag = true;
5148 } else if(cheekPress) {
5149 //cancel the earlier event
5150 ev.setAction(MotionEvent.ACTION_CANCEL);
5151 action = MotionEvent.ACTION_CANCEL;
5152 }
5153 } else if(action == MotionEvent.ACTION_MOVE) {
5154 if(mFatTouch) {
5155 //two cases here
5156 //an invalid down followed by 0 or moves(valid or invalid)
Romain Guy06882f82009-06-10 13:36:04 -07005157 //a valid down, invalid move, more moves. want to ignore till up
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005158 returnFlag = true;
5159 } else if(cheekPress) {
5160 //valid down followed by invalid moves
5161 //an invalid move have to cancel earlier action
5162 ev.setAction(MotionEvent.ACTION_CANCEL);
5163 action = MotionEvent.ACTION_CANCEL;
Joe Onorato8a9b2202010-02-26 18:56:32 -08005164 if (DEBUG_INPUT) Slog.v(TAG, "Sending cancel for invalid ACTION_MOVE");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005165 //note that the subsequent invalid moves will not get here
5166 mFatTouch = true;
5167 }
5168 }
5169 } //else if action
5170 if(returnFlag) {
5171 //recycle que, ev
5172 if (qev != null) {
5173 mQueue.recycleEvent(qev);
5174 }
5175 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005176 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005177 }
5178 } //end if target
Michael Chane96440f2009-05-06 10:27:36 -07005179
Michael Chan9f028e62009-08-04 17:37:46 -07005180 // Enable this for testing the "right" value
5181 if (false && action == MotionEvent.ACTION_DOWN) {
Michael Chane96440f2009-05-06 10:27:36 -07005182 int max_events_per_sec = 35;
5183 try {
5184 max_events_per_sec = Integer.parseInt(SystemProperties
5185 .get("windowsmgr.max_events_per_sec"));
5186 if (max_events_per_sec < 1) {
5187 max_events_per_sec = 35;
5188 }
5189 } catch (NumberFormatException e) {
5190 }
5191 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
5192 }
5193
5194 /*
5195 * Throttle events to minimize CPU usage when there's a flood of events
5196 * e.g. constant contact with the screen
5197 */
5198 if (action == MotionEvent.ACTION_MOVE) {
5199 long nextEventTime = mLastTouchEventTime + mMinWaitTimeBetweenTouchEvents;
5200 long now = SystemClock.uptimeMillis();
5201 if (now < nextEventTime) {
5202 try {
5203 Thread.sleep(nextEventTime - now);
5204 } catch (InterruptedException e) {
5205 }
5206 mLastTouchEventTime = nextEventTime;
5207 } else {
5208 mLastTouchEventTime = now;
5209 }
5210 }
5211
Michael Chan53071d62009-05-13 17:29:48 -07005212 if (MEASURE_LATENCY) {
5213 lt.sample("5 in dispatchPointer ", System.nanoTime() - eventTimeNano);
5214 }
5215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005216 synchronized(mWindowMap) {
Dianne Hackborn6adba242009-11-10 11:10:09 -08005217 if (!target.isVisibleLw()) {
5218 // During this motion dispatch, the target window has become
5219 // invisible.
Dianne Hackborn90d2db32010-02-11 22:19:06 -08005220 dispatchPointerElsewhereLocked(null, null, ev, ev.getEventTime(), false);
Dianne Hackborn6adba242009-11-10 11:10:09 -08005221 if (qev != null) {
5222 mQueue.recycleEvent(qev);
5223 }
5224 ev.recycle();
5225 return INJECT_SUCCEEDED;
5226 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005228 if (qev != null && action == MotionEvent.ACTION_MOVE) {
5229 mKeyWaiter.bindTargetWindowLocked(target,
5230 KeyWaiter.RETURN_PENDING_POINTER, qev);
5231 ev = null;
5232 } else {
5233 if (action == MotionEvent.ACTION_DOWN) {
5234 WindowState out = mKeyWaiter.mOutsideTouchTargets;
5235 if (out != null) {
5236 MotionEvent oev = MotionEvent.obtain(ev);
5237 oev.setAction(MotionEvent.ACTION_OUTSIDE);
5238 do {
5239 final Rect frame = out.mFrame;
5240 oev.offsetLocation(-(float)frame.left, -(float)frame.top);
5241 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005242 out.mClient.dispatchPointer(oev, eventTime, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005243 } catch (android.os.RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005244 Slog.i(TAG, "WINDOW DIED during outside motion dispatch: " + out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005245 }
5246 oev.offsetLocation((float)frame.left, (float)frame.top);
5247 out = out.mNextOutsideTouch;
5248 } while (out != null);
5249 mKeyWaiter.mOutsideTouchTargets = null;
5250 }
5251 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005252
Dianne Hackborn90d2db32010-02-11 22:19:06 -08005253 dispatchPointerElsewhereLocked(target, null, ev, ev.getEventTime(), false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005254
Dianne Hackborn6adba242009-11-10 11:10:09 -08005255 final Rect frame = target.mFrame;
5256 ev.offsetLocation(-(float)frame.left, -(float)frame.top);
5257 mKeyWaiter.bindTargetWindowLocked(target);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005258 }
5259 }
Romain Guy06882f82009-06-10 13:36:04 -07005260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005261 // finally offset the event to the target's coordinate system and
5262 // dispatch the event.
5263 try {
5264 if (DEBUG_INPUT || DEBUG_FOCUS || WindowManagerPolicy.WATCH_POINTER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005265 Slog.v(TAG, "Delivering pointer " + qev + " to " + target);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005266 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005267
Michael Chan53071d62009-05-13 17:29:48 -07005268 if (MEASURE_LATENCY) {
5269 lt.sample("6 before svr->client ipc ", System.nanoTime() - eventTimeNano);
5270 }
5271
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005272 target.mClient.dispatchPointer(ev, eventTime, true);
Michael Chan53071d62009-05-13 17:29:48 -07005273
5274 if (MEASURE_LATENCY) {
5275 lt.sample("7 after svr->client ipc ", System.nanoTime() - eventTimeNano);
5276 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07005277 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005278 } catch (android.os.RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005279 Slog.i(TAG, "WINDOW DIED during motion dispatch: " + target);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005280 mKeyWaiter.mMotionTarget = null;
5281 try {
5282 removeWindow(target.mSession, target.mClient);
5283 } catch (java.util.NoSuchElementException ex) {
5284 // This will happen if the window has already been
5285 // removed.
5286 }
5287 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07005288 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005289 }
Romain Guy06882f82009-06-10 13:36:04 -07005290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005291 /**
5292 * @return Returns true if event was dispatched, false if it was dropped for any reason
5293 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07005294 private int dispatchTrackball(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005295 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005296 TAG, "dispatchTrackball [" + ev.getAction() +"] <" + ev.getX() + ", " + ev.getY() + ">");
Romain Guy06882f82009-06-10 13:36:04 -07005297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005298 Object focusObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005299 ev, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005300 if (focusObj == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005301 Slog.w(TAG, "No focus window, dropping trackball: " + ev);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005302 if (qev != null) {
5303 mQueue.recycleEvent(qev);
5304 }
5305 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005306 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005307 }
5308 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
5309 if (qev != null) {
5310 mQueue.recycleEvent(qev);
5311 }
5312 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005313 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005314 }
Romain Guy06882f82009-06-10 13:36:04 -07005315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005316 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07005317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005318 if (uid != 0 && uid != focus.mSession.mUid) {
5319 if (mContext.checkPermission(
5320 android.Manifest.permission.INJECT_EVENTS, pid, uid)
5321 != PackageManager.PERMISSION_GRANTED) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005322 Slog.w(TAG, "Permission denied: injecting key event from pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005323 + pid + " uid " + uid + " to window " + focus
5324 + " owned by uid " + focus.mSession.mUid);
5325 if (qev != null) {
5326 mQueue.recycleEvent(qev);
5327 }
5328 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005329 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005330 }
5331 }
Romain Guy06882f82009-06-10 13:36:04 -07005332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005333 final long eventTime = ev.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07005334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005335 synchronized(mWindowMap) {
5336 if (qev != null && ev.getAction() == MotionEvent.ACTION_MOVE) {
5337 mKeyWaiter.bindTargetWindowLocked(focus,
5338 KeyWaiter.RETURN_PENDING_TRACKBALL, qev);
5339 // We don't deliver movement events to the client, we hold
5340 // them and wait for them to call back.
5341 ev = null;
5342 } else {
5343 mKeyWaiter.bindTargetWindowLocked(focus);
5344 }
5345 }
Romain Guy06882f82009-06-10 13:36:04 -07005346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005347 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005348 focus.mClient.dispatchTrackball(ev, eventTime, true);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005349 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005350 } catch (android.os.RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005351 Slog.i(TAG, "WINDOW DIED during key dispatch: " + focus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005352 try {
5353 removeWindow(focus.mSession, focus.mClient);
5354 } catch (java.util.NoSuchElementException ex) {
5355 // This will happen if the window has already been
5356 // removed.
5357 }
5358 }
Romain Guy06882f82009-06-10 13:36:04 -07005359
Dianne Hackborncfaef692009-06-15 14:24:44 -07005360 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005361 }
Romain Guy06882f82009-06-10 13:36:04 -07005362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005363 /**
5364 * @return Returns true if event was dispatched, false if it was dropped for any reason
5365 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07005366 private int dispatchKey(KeyEvent event, int pid, int uid) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005367 if (DEBUG_INPUT) Slog.v(TAG, "Dispatch key: " + event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005368
5369 Object focusObj = mKeyWaiter.waitForNextEventTarget(event, null,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005370 null, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005371 if (focusObj == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005372 Slog.w(TAG, "No focus window, dropping: " + event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005373 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005374 }
5375 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005376 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005377 }
Romain Guy06882f82009-06-10 13:36:04 -07005378
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005379 // Okay we have finished waiting for the last event to be processed.
5380 // First off, if this is a repeat event, check to see if there is
5381 // a corresponding up event in the queue. If there is, we will
5382 // just drop the repeat, because it makes no sense to repeat after
5383 // the user has released a key. (This is especially important for
5384 // long presses.)
5385 if (event.getRepeatCount() > 0 && mQueue.hasKeyUpEvent(event)) {
5386 return INJECT_SUCCEEDED;
5387 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005388
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005389 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07005390
Joe Onorato8a9b2202010-02-26 18:56:32 -08005391 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005392 TAG, "Dispatching to " + focus + ": " + event);
5393
5394 if (uid != 0 && uid != focus.mSession.mUid) {
5395 if (mContext.checkPermission(
5396 android.Manifest.permission.INJECT_EVENTS, pid, uid)
5397 != PackageManager.PERMISSION_GRANTED) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005398 Slog.w(TAG, "Permission denied: injecting key event from pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005399 + pid + " uid " + uid + " to window " + focus
5400 + " owned by uid " + focus.mSession.mUid);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005401 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005402 }
5403 }
Romain Guy06882f82009-06-10 13:36:04 -07005404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005405 synchronized(mWindowMap) {
5406 mKeyWaiter.bindTargetWindowLocked(focus);
5407 }
5408
5409 // NOSHIP extra state logging
5410 mKeyWaiter.recordDispatchState(event, focus);
5411 // END NOSHIP
Romain Guy06882f82009-06-10 13:36:04 -07005412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005413 try {
5414 if (DEBUG_INPUT || DEBUG_FOCUS) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005415 Slog.v(TAG, "Delivering key " + event.getKeyCode()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005416 + " to " + focus);
5417 }
5418 focus.mClient.dispatchKey(event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005419 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005420 } catch (android.os.RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005421 Slog.i(TAG, "WINDOW DIED during key dispatch: " + focus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005422 try {
5423 removeWindow(focus.mSession, focus.mClient);
5424 } catch (java.util.NoSuchElementException ex) {
5425 // This will happen if the window has already been
5426 // removed.
5427 }
5428 }
Romain Guy06882f82009-06-10 13:36:04 -07005429
Dianne Hackborncfaef692009-06-15 14:24:44 -07005430 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005431 }
Romain Guy06882f82009-06-10 13:36:04 -07005432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005433 public void pauseKeyDispatching(IBinder _token) {
5434 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5435 "pauseKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005436 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005437 }
5438
5439 synchronized (mWindowMap) {
5440 WindowToken token = mTokenMap.get(_token);
5441 if (token != null) {
5442 mKeyWaiter.pauseDispatchingLocked(token);
5443 }
5444 }
5445 }
5446
5447 public void resumeKeyDispatching(IBinder _token) {
5448 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5449 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005450 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005451 }
5452
5453 synchronized (mWindowMap) {
5454 WindowToken token = mTokenMap.get(_token);
5455 if (token != null) {
5456 mKeyWaiter.resumeDispatchingLocked(token);
5457 }
5458 }
5459 }
5460
5461 public void setEventDispatching(boolean enabled) {
5462 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5463 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005464 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005465 }
5466
5467 synchronized (mWindowMap) {
5468 mKeyWaiter.setEventDispatchingLocked(enabled);
5469 }
5470 }
Romain Guy06882f82009-06-10 13:36:04 -07005471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005472 /**
5473 * Injects a keystroke event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005474 *
5475 * @param ev A motion event describing the keystroke action. (Be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005476 * {@link SystemClock#uptimeMillis()} as the timebase.)
5477 * @param sync If true, wait for the event to be completed before returning to the caller.
5478 * @return Returns true if event was dispatched, false if it was dropped for any reason
5479 */
5480 public boolean injectKeyEvent(KeyEvent ev, boolean sync) {
5481 long downTime = ev.getDownTime();
5482 long eventTime = ev.getEventTime();
5483
5484 int action = ev.getAction();
5485 int code = ev.getKeyCode();
5486 int repeatCount = ev.getRepeatCount();
5487 int metaState = ev.getMetaState();
5488 int deviceId = ev.getDeviceId();
5489 int scancode = ev.getScanCode();
5490
5491 if (eventTime == 0) eventTime = SystemClock.uptimeMillis();
5492 if (downTime == 0) downTime = eventTime;
5493
5494 KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
The Android Open Source Project10592532009-03-18 17:39:46 -07005495 deviceId, scancode, KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005496
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005497 final int pid = Binder.getCallingPid();
5498 final int uid = Binder.getCallingUid();
5499 final long ident = Binder.clearCallingIdentity();
5500 final int result = dispatchKey(newEvent, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005501 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005502 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005503 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005504 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005505 switch (result) {
5506 case INJECT_NO_PERMISSION:
5507 throw new SecurityException(
Chander S Pechetty27f3de62010-02-10 22:14:00 +05305508 "Injecting to another application requires INJECT_EVENTS permission");
Dianne Hackborncfaef692009-06-15 14:24:44 -07005509 case INJECT_SUCCEEDED:
5510 return true;
5511 }
5512 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005513 }
5514
5515 /**
5516 * Inject a pointer (touch) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005517 *
5518 * @param ev A motion event describing the pointer (touch) action. (As noted in
5519 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005520 * {@link SystemClock#uptimeMillis()} as the timebase.)
5521 * @param sync If true, wait for the event to be completed before returning to the caller.
5522 * @return Returns true if event was dispatched, false if it was dropped for any reason
5523 */
5524 public boolean injectPointerEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005525 final int pid = Binder.getCallingPid();
5526 final int uid = Binder.getCallingUid();
5527 final long ident = Binder.clearCallingIdentity();
5528 final int result = dispatchPointer(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005529 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005530 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005531 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005532 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005533 switch (result) {
5534 case INJECT_NO_PERMISSION:
5535 throw new SecurityException(
Chander S Pechetty27f3de62010-02-10 22:14:00 +05305536 "Injecting to another application requires INJECT_EVENTS permission");
Dianne Hackborncfaef692009-06-15 14:24:44 -07005537 case INJECT_SUCCEEDED:
5538 return true;
5539 }
5540 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005541 }
Romain Guy06882f82009-06-10 13:36:04 -07005542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005543 /**
5544 * Inject a trackball (navigation device) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005545 *
5546 * @param ev A motion event describing the trackball action. (As noted in
5547 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005548 * {@link SystemClock#uptimeMillis()} as the timebase.)
5549 * @param sync If true, wait for the event to be completed before returning to the caller.
5550 * @return Returns true if event was dispatched, false if it was dropped for any reason
5551 */
5552 public boolean injectTrackballEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005553 final int pid = Binder.getCallingPid();
5554 final int uid = Binder.getCallingUid();
5555 final long ident = Binder.clearCallingIdentity();
5556 final int result = dispatchTrackball(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005557 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005558 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005559 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005560 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005561 switch (result) {
5562 case INJECT_NO_PERMISSION:
5563 throw new SecurityException(
Chander S Pechetty27f3de62010-02-10 22:14:00 +05305564 "Injecting to another application requires INJECT_EVENTS permission");
Dianne Hackborncfaef692009-06-15 14:24:44 -07005565 case INJECT_SUCCEEDED:
5566 return true;
5567 }
5568 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005569 }
Romain Guy06882f82009-06-10 13:36:04 -07005570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005571 private WindowState getFocusedWindow() {
5572 synchronized (mWindowMap) {
5573 return getFocusedWindowLocked();
5574 }
5575 }
5576
5577 private WindowState getFocusedWindowLocked() {
5578 return mCurrentFocus;
5579 }
Romain Guy06882f82009-06-10 13:36:04 -07005580
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005581 /**
5582 * This class holds the state for dispatching key events. This state
5583 * is protected by the KeyWaiter instance, NOT by the window lock. You
5584 * can be holding the main window lock while acquire the KeyWaiter lock,
5585 * but not the other way around.
5586 */
5587 final class KeyWaiter {
5588 // NOSHIP debugging
5589 public class DispatchState {
5590 private KeyEvent event;
5591 private WindowState focus;
5592 private long time;
5593 private WindowState lastWin;
5594 private IBinder lastBinder;
5595 private boolean finished;
5596 private boolean gotFirstWindow;
5597 private boolean eventDispatching;
5598 private long timeToSwitch;
5599 private boolean wasFrozen;
5600 private boolean focusPaused;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005601 private WindowState curFocus;
Romain Guy06882f82009-06-10 13:36:04 -07005602
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005603 DispatchState(KeyEvent theEvent, WindowState theFocus) {
5604 focus = theFocus;
5605 event = theEvent;
5606 time = System.currentTimeMillis();
5607 // snapshot KeyWaiter state
5608 lastWin = mLastWin;
5609 lastBinder = mLastBinder;
5610 finished = mFinished;
5611 gotFirstWindow = mGotFirstWindow;
5612 eventDispatching = mEventDispatching;
5613 timeToSwitch = mTimeToSwitch;
5614 wasFrozen = mWasFrozen;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005615 curFocus = mCurrentFocus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005616 // cache the paused state at ctor time as well
5617 if (theFocus == null || theFocus.mToken == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005618 focusPaused = false;
5619 } else {
5620 focusPaused = theFocus.mToken.paused;
5621 }
5622 }
Romain Guy06882f82009-06-10 13:36:04 -07005623
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005624 public String toString() {
5625 return "{{" + event + " to " + focus + " @ " + time
5626 + " lw=" + lastWin + " lb=" + lastBinder
5627 + " fin=" + finished + " gfw=" + gotFirstWindow
5628 + " ed=" + eventDispatching + " tts=" + timeToSwitch
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005629 + " wf=" + wasFrozen + " fp=" + focusPaused
Christopher Tate46d45252010-02-09 15:48:57 -08005630 + " mcf=" + curFocus + "}}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005631 }
5632 };
5633 private DispatchState mDispatchState = null;
5634 public void recordDispatchState(KeyEvent theEvent, WindowState theFocus) {
5635 mDispatchState = new DispatchState(theEvent, theFocus);
5636 }
5637 // END NOSHIP
5638
5639 public static final int RETURN_NOTHING = 0;
5640 public static final int RETURN_PENDING_POINTER = 1;
5641 public static final int RETURN_PENDING_TRACKBALL = 2;
Romain Guy06882f82009-06-10 13:36:04 -07005642
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005643 final Object SKIP_TARGET_TOKEN = new Object();
5644 final Object CONSUMED_EVENT_TOKEN = new Object();
Romain Guy06882f82009-06-10 13:36:04 -07005645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005646 private WindowState mLastWin = null;
5647 private IBinder mLastBinder = null;
5648 private boolean mFinished = true;
5649 private boolean mGotFirstWindow = false;
5650 private boolean mEventDispatching = true;
5651 private long mTimeToSwitch = 0;
5652 /* package */ boolean mWasFrozen = false;
Romain Guy06882f82009-06-10 13:36:04 -07005653
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005654 // Target of Motion events
5655 WindowState mMotionTarget;
Romain Guy06882f82009-06-10 13:36:04 -07005656
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005657 // Windows above the target who would like to receive an "outside"
5658 // touch event for any down events outside of them.
5659 WindowState mOutsideTouchTargets;
5660
5661 /**
5662 * Wait for the last event dispatch to complete, then find the next
5663 * target that should receive the given event and wait for that one
5664 * to be ready to receive it.
5665 */
5666 Object waitForNextEventTarget(KeyEvent nextKey, QueuedEvent qev,
5667 MotionEvent nextMotion, boolean isPointerEvent,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005668 boolean failIfTimeout, int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005669 long startTime = SystemClock.uptimeMillis();
5670 long keyDispatchingTimeout = 5 * 1000;
5671 long waitedFor = 0;
5672
5673 while (true) {
5674 // Figure out which window we care about. It is either the
5675 // last window we are waiting to have process the event or,
5676 // if none, then the next window we think the event should go
5677 // to. Note: we retrieve mLastWin outside of the lock, so
5678 // it may change before we lock. Thus we must check it again.
5679 WindowState targetWin = mLastWin;
5680 boolean targetIsNew = targetWin == null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08005681 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005682 TAG, "waitForLastKey: mFinished=" + mFinished +
5683 ", mLastWin=" + mLastWin);
5684 if (targetIsNew) {
5685 Object target = findTargetWindow(nextKey, qev, nextMotion,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005686 isPointerEvent, callingPid, callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005687 if (target == SKIP_TARGET_TOKEN) {
5688 // The user has pressed a special key, and we are
5689 // dropping all pending events before it.
Joe Onorato8a9b2202010-02-26 18:56:32 -08005690 if (DEBUG_INPUT) Slog.v(TAG, "Skipping: " + nextKey
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005691 + " " + nextMotion);
5692 return null;
5693 }
5694 if (target == CONSUMED_EVENT_TOKEN) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005695 if (DEBUG_INPUT) Slog.v(TAG, "Consumed: " + nextKey
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005696 + " " + nextMotion);
5697 return target;
5698 }
5699 targetWin = (WindowState)target;
5700 }
Romain Guy06882f82009-06-10 13:36:04 -07005701
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005702 AppWindowToken targetApp = null;
Romain Guy06882f82009-06-10 13:36:04 -07005703
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005704 // Now: is it okay to send the next event to this window?
5705 synchronized (this) {
5706 // First: did we come here based on the last window not
5707 // being null, but it changed by the time we got here?
5708 // If so, try again.
5709 if (!targetIsNew && mLastWin == null) {
5710 continue;
5711 }
Romain Guy06882f82009-06-10 13:36:04 -07005712
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005713 // We never dispatch events if not finished with the
5714 // last one, or the display is frozen.
5715 if (mFinished && !mDisplayFrozen) {
5716 // If event dispatching is disabled, then we
5717 // just consume the events.
5718 if (!mEventDispatching) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005719 if (DEBUG_INPUT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005720 "Skipping event; dispatching disabled: "
5721 + nextKey + " " + nextMotion);
5722 return null;
5723 }
5724 if (targetWin != null) {
5725 // If this is a new target, and that target is not
5726 // paused or unresponsive, then all looks good to
5727 // handle the event.
5728 if (targetIsNew && !targetWin.mToken.paused) {
5729 return targetWin;
5730 }
Romain Guy06882f82009-06-10 13:36:04 -07005731
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005732 // If we didn't find a target window, and there is no
5733 // focused app window, then just eat the events.
5734 } else if (mFocusedApp == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005735 if (DEBUG_INPUT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005736 "Skipping event; no focused app: "
5737 + nextKey + " " + nextMotion);
5738 return null;
5739 }
5740 }
Romain Guy06882f82009-06-10 13:36:04 -07005741
Joe Onorato8a9b2202010-02-26 18:56:32 -08005742 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005743 TAG, "Waiting for last key in " + mLastBinder
5744 + " target=" + targetWin
5745 + " mFinished=" + mFinished
5746 + " mDisplayFrozen=" + mDisplayFrozen
5747 + " targetIsNew=" + targetIsNew
5748 + " paused="
5749 + (targetWin != null ? targetWin.mToken.paused : false)
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005750 + " mFocusedApp=" + mFocusedApp
5751 + " mCurrentFocus=" + mCurrentFocus);
Romain Guy06882f82009-06-10 13:36:04 -07005752
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005753 targetApp = targetWin != null
5754 ? targetWin.mAppToken : mFocusedApp;
Romain Guy06882f82009-06-10 13:36:04 -07005755
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005756 long curTimeout = keyDispatchingTimeout;
5757 if (mTimeToSwitch != 0) {
5758 long now = SystemClock.uptimeMillis();
5759 if (mTimeToSwitch <= now) {
5760 // If an app switch key has been pressed, and we have
5761 // waited too long for the current app to finish
5762 // processing keys, then wait no more!
Christopher Tate136b1f92010-02-11 17:51:24 -08005763 doFinishedKeyLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005764 continue;
5765 }
5766 long switchTimeout = mTimeToSwitch - now;
5767 if (curTimeout > switchTimeout) {
5768 curTimeout = switchTimeout;
5769 }
5770 }
Romain Guy06882f82009-06-10 13:36:04 -07005771
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005772 try {
5773 // after that continue
5774 // processing keys, so we don't get stuck.
Joe Onorato8a9b2202010-02-26 18:56:32 -08005775 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005776 TAG, "Waiting for key dispatch: " + curTimeout);
5777 wait(curTimeout);
Joe Onorato8a9b2202010-02-26 18:56:32 -08005778 if (DEBUG_INPUT) Slog.v(TAG, "Finished waiting @"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005779 + SystemClock.uptimeMillis() + " startTime="
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005780 + startTime + " switchTime=" + mTimeToSwitch
5781 + " target=" + targetWin + " mLW=" + mLastWin
5782 + " mLB=" + mLastBinder + " fin=" + mFinished
5783 + " mCurrentFocus=" + mCurrentFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005784 } catch (InterruptedException e) {
5785 }
5786 }
5787
5788 // If we were frozen during configuration change, restart the
5789 // timeout checks from now; otherwise look at whether we timed
5790 // out before awakening.
5791 if (mWasFrozen) {
5792 waitedFor = 0;
5793 mWasFrozen = false;
5794 } else {
5795 waitedFor = SystemClock.uptimeMillis() - startTime;
5796 }
5797
5798 if (waitedFor >= keyDispatchingTimeout && mTimeToSwitch == 0) {
5799 IApplicationToken at = null;
5800 synchronized (this) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005801 Slog.w(TAG, "Key dispatching timed out sending to " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005802 (targetWin != null ? targetWin.mAttrs.getTitle()
Ken Shirriff8200b202010-02-04 13:34:37 -08005803 : "<null>: no window ready for key dispatch"));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005804 // NOSHIP debugging
Joe Onorato8a9b2202010-02-26 18:56:32 -08005805 Slog.w(TAG, "Previous dispatch state: " + mDispatchState);
5806 Slog.w(TAG, "Current dispatch state: " +
Ken Shirriff8200b202010-02-04 13:34:37 -08005807 new DispatchState(nextKey, targetWin));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005808 // END NOSHIP
5809 //dump();
5810 if (targetWin != null) {
5811 at = targetWin.getAppToken();
5812 } else if (targetApp != null) {
5813 at = targetApp.appToken;
5814 }
5815 }
5816
5817 boolean abort = true;
5818 if (at != null) {
5819 try {
5820 long timeout = at.getKeyDispatchingTimeout();
5821 if (timeout > waitedFor) {
5822 // we did not wait the proper amount of time for this application.
5823 // set the timeout to be the real timeout and wait again.
5824 keyDispatchingTimeout = timeout - waitedFor;
5825 continue;
5826 } else {
5827 abort = at.keyDispatchingTimedOut();
5828 }
5829 } catch (RemoteException ex) {
5830 }
5831 }
5832
5833 synchronized (this) {
5834 if (abort && (mLastWin == targetWin || targetWin == null)) {
5835 mFinished = true;
Romain Guy06882f82009-06-10 13:36:04 -07005836 if (mLastWin != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005837 if (DEBUG_INPUT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005838 "Window " + mLastWin +
5839 " timed out on key input");
5840 if (mLastWin.mToken.paused) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005841 Slog.w(TAG, "Un-pausing dispatching to this window");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005842 mLastWin.mToken.paused = false;
5843 }
5844 }
5845 if (mMotionTarget == targetWin) {
5846 mMotionTarget = null;
5847 }
5848 mLastWin = null;
5849 mLastBinder = null;
5850 if (failIfTimeout || targetWin == null) {
5851 return null;
5852 }
5853 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005854 Slog.w(TAG, "Continuing to wait for key to be dispatched");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005855 startTime = SystemClock.uptimeMillis();
5856 }
5857 }
5858 }
5859 }
5860 }
Romain Guy06882f82009-06-10 13:36:04 -07005861
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005862 Object findTargetWindow(KeyEvent nextKey, QueuedEvent qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005863 MotionEvent nextMotion, boolean isPointerEvent,
5864 int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005865 mOutsideTouchTargets = null;
Romain Guy06882f82009-06-10 13:36:04 -07005866
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005867 if (nextKey != null) {
5868 // Find the target window for a normal key event.
5869 final int keycode = nextKey.getKeyCode();
5870 final int repeatCount = nextKey.getRepeatCount();
5871 final boolean down = nextKey.getAction() != KeyEvent.ACTION_UP;
5872 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(keycode);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005873
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005874 if (!dispatch) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005875 if (callingUid == 0 ||
5876 mContext.checkPermission(
5877 android.Manifest.permission.INJECT_EVENTS,
5878 callingPid, callingUid)
5879 == PackageManager.PERMISSION_GRANTED) {
5880 mPolicy.interceptKeyTi(null, keycode,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005881 nextKey.getMetaState(), down, repeatCount,
5882 nextKey.getFlags());
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005883 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08005884 Slog.w(TAG, "Event timeout during app switch: dropping "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005885 + nextKey);
5886 return SKIP_TARGET_TOKEN;
5887 }
Romain Guy06882f82009-06-10 13:36:04 -07005888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005889 // System.out.println("##### [" + SystemClock.uptimeMillis() + "] WindowManagerService.dispatchKey(" + keycode + ", " + down + ", " + repeatCount + ")");
Romain Guy06882f82009-06-10 13:36:04 -07005890
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005891 WindowState focus = null;
5892 synchronized(mWindowMap) {
5893 focus = getFocusedWindowLocked();
5894 }
Romain Guy06882f82009-06-10 13:36:04 -07005895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005896 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
Romain Guy06882f82009-06-10 13:36:04 -07005897
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005898 if (callingUid == 0 ||
5899 (focus != null && callingUid == focus.mSession.mUid) ||
5900 mContext.checkPermission(
5901 android.Manifest.permission.INJECT_EVENTS,
5902 callingPid, callingUid)
5903 == PackageManager.PERMISSION_GRANTED) {
5904 if (mPolicy.interceptKeyTi(focus,
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005905 keycode, nextKey.getMetaState(), down, repeatCount,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005906 nextKey.getFlags())) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005907 return CONSUMED_EVENT_TOKEN;
5908 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005909 }
Romain Guy06882f82009-06-10 13:36:04 -07005910
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005911 return focus;
Romain Guy06882f82009-06-10 13:36:04 -07005912
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005913 } else if (!isPointerEvent) {
5914 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(-1);
5915 if (!dispatch) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005916 Slog.w(TAG, "Event timeout during app switch: dropping trackball "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005917 + nextMotion);
5918 return SKIP_TARGET_TOKEN;
5919 }
Romain Guy06882f82009-06-10 13:36:04 -07005920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005921 WindowState focus = null;
5922 synchronized(mWindowMap) {
5923 focus = getFocusedWindowLocked();
5924 }
Romain Guy06882f82009-06-10 13:36:04 -07005925
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005926 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
5927 return focus;
5928 }
Romain Guy06882f82009-06-10 13:36:04 -07005929
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005930 if (nextMotion == null) {
5931 return SKIP_TARGET_TOKEN;
5932 }
Romain Guy06882f82009-06-10 13:36:04 -07005933
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005934 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(
5935 KeyEvent.KEYCODE_UNKNOWN);
5936 if (!dispatch) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005937 Slog.w(TAG, "Event timeout during app switch: dropping pointer "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005938 + nextMotion);
5939 return SKIP_TARGET_TOKEN;
5940 }
Romain Guy06882f82009-06-10 13:36:04 -07005941
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005942 // Find the target window for a pointer event.
5943 int action = nextMotion.getAction();
5944 final float xf = nextMotion.getX();
5945 final float yf = nextMotion.getY();
5946 final long eventTime = nextMotion.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07005947
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005948 final boolean screenWasOff = qev != null
5949 && (qev.flags&WindowManagerPolicy.FLAG_BRIGHT_HERE) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07005950
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005951 WindowState target = null;
Romain Guy06882f82009-06-10 13:36:04 -07005952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005953 synchronized(mWindowMap) {
5954 synchronized (this) {
5955 if (action == MotionEvent.ACTION_DOWN) {
5956 if (mMotionTarget != null) {
5957 // this is weird, we got a pen down, but we thought it was
5958 // already down!
5959 // XXX: We should probably send an ACTION_UP to the current
5960 // target.
Joe Onorato8a9b2202010-02-26 18:56:32 -08005961 Slog.w(TAG, "Pointer down received while already down in: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005962 + mMotionTarget);
5963 mMotionTarget = null;
5964 }
Romain Guy06882f82009-06-10 13:36:04 -07005965
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005966 // ACTION_DOWN is special, because we need to lock next events to
5967 // the window we'll land onto.
5968 final int x = (int)xf;
5969 final int y = (int)yf;
Romain Guy06882f82009-06-10 13:36:04 -07005970
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005971 final ArrayList windows = mWindows;
5972 final int N = windows.size();
5973 WindowState topErrWindow = null;
5974 final Rect tmpRect = mTempRect;
5975 for (int i=N-1; i>=0; i--) {
5976 WindowState child = (WindowState)windows.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08005977 //Slog.i(TAG, "Checking dispatch to: " + child);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005978 final int flags = child.mAttrs.flags;
5979 if ((flags & WindowManager.LayoutParams.FLAG_SYSTEM_ERROR) != 0) {
5980 if (topErrWindow == null) {
5981 topErrWindow = child;
5982 }
5983 }
5984 if (!child.isVisibleLw()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005985 //Slog.i(TAG, "Not visible!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005986 continue;
5987 }
5988 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005989 //Slog.i(TAG, "Not touchable!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005990 if ((flags & WindowManager.LayoutParams
5991 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5992 child.mNextOutsideTouch = mOutsideTouchTargets;
5993 mOutsideTouchTargets = child;
5994 }
5995 continue;
5996 }
5997 tmpRect.set(child.mFrame);
5998 if (child.mTouchableInsets == ViewTreeObserver
5999 .InternalInsetsInfo.TOUCHABLE_INSETS_CONTENT) {
6000 // The touch is inside of the window if it is
6001 // inside the frame, AND the content part of that
6002 // frame that was given by the application.
6003 tmpRect.left += child.mGivenContentInsets.left;
6004 tmpRect.top += child.mGivenContentInsets.top;
6005 tmpRect.right -= child.mGivenContentInsets.right;
6006 tmpRect.bottom -= child.mGivenContentInsets.bottom;
6007 } else if (child.mTouchableInsets == ViewTreeObserver
6008 .InternalInsetsInfo.TOUCHABLE_INSETS_VISIBLE) {
6009 // The touch is inside of the window if it is
6010 // inside the frame, AND the visible part of that
6011 // frame that was given by the application.
6012 tmpRect.left += child.mGivenVisibleInsets.left;
6013 tmpRect.top += child.mGivenVisibleInsets.top;
6014 tmpRect.right -= child.mGivenVisibleInsets.right;
6015 tmpRect.bottom -= child.mGivenVisibleInsets.bottom;
6016 }
6017 final int touchFlags = flags &
6018 (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
6019 |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
6020 if (tmpRect.contains(x, y) || touchFlags == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006021 //Slog.i(TAG, "Using this target!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006022 if (!screenWasOff || (flags &
6023 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING) != 0) {
6024 mMotionTarget = child;
6025 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006026 //Slog.i(TAG, "Waking, skip!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006027 mMotionTarget = null;
6028 }
6029 break;
6030 }
Romain Guy06882f82009-06-10 13:36:04 -07006031
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006032 if ((flags & WindowManager.LayoutParams
6033 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
6034 child.mNextOutsideTouch = mOutsideTouchTargets;
6035 mOutsideTouchTargets = child;
Joe Onorato8a9b2202010-02-26 18:56:32 -08006036 //Slog.i(TAG, "Adding to outside target list: " + child);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006037 }
6038 }
6039
6040 // if there's an error window but it's not accepting
6041 // focus (typically because it is not yet visible) just
6042 // wait for it -- any other focused window may in fact
6043 // be in ANR state.
6044 if (topErrWindow != null && mMotionTarget != topErrWindow) {
6045 mMotionTarget = null;
6046 }
6047 }
Romain Guy06882f82009-06-10 13:36:04 -07006048
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006049 target = mMotionTarget;
6050 }
6051 }
Romain Guy06882f82009-06-10 13:36:04 -07006052
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006053 wakeupIfNeeded(target, eventType(nextMotion));
Romain Guy06882f82009-06-10 13:36:04 -07006054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006055 // Pointer events are a little different -- if there isn't a
6056 // target found for any event, then just drop it.
6057 return target != null ? target : SKIP_TARGET_TOKEN;
6058 }
Romain Guy06882f82009-06-10 13:36:04 -07006059
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006060 boolean checkShouldDispatchKey(int keycode) {
6061 synchronized (this) {
6062 if (mPolicy.isAppSwitchKeyTqTiLwLi(keycode)) {
6063 mTimeToSwitch = 0;
6064 return true;
6065 }
6066 if (mTimeToSwitch != 0
6067 && mTimeToSwitch < SystemClock.uptimeMillis()) {
6068 return false;
6069 }
6070 return true;
6071 }
6072 }
Romain Guy06882f82009-06-10 13:36:04 -07006073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006074 void bindTargetWindowLocked(WindowState win,
6075 int pendingWhat, QueuedEvent pendingMotion) {
6076 synchronized (this) {
6077 bindTargetWindowLockedLocked(win, pendingWhat, pendingMotion);
6078 }
6079 }
Romain Guy06882f82009-06-10 13:36:04 -07006080
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006081 void bindTargetWindowLocked(WindowState win) {
6082 synchronized (this) {
6083 bindTargetWindowLockedLocked(win, RETURN_NOTHING, null);
6084 }
6085 }
6086
6087 void bindTargetWindowLockedLocked(WindowState win,
6088 int pendingWhat, QueuedEvent pendingMotion) {
6089 mLastWin = win;
6090 mLastBinder = win.mClient.asBinder();
6091 mFinished = false;
6092 if (pendingMotion != null) {
6093 final Session s = win.mSession;
6094 if (pendingWhat == RETURN_PENDING_POINTER) {
6095 releasePendingPointerLocked(s);
6096 s.mPendingPointerMove = pendingMotion;
6097 s.mPendingPointerWindow = win;
Joe Onorato8a9b2202010-02-26 18:56:32 -08006098 if (DEBUG_INPUT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006099 "bindTargetToWindow " + s.mPendingPointerMove);
6100 } else if (pendingWhat == RETURN_PENDING_TRACKBALL) {
6101 releasePendingTrackballLocked(s);
6102 s.mPendingTrackballMove = pendingMotion;
6103 s.mPendingTrackballWindow = win;
6104 }
6105 }
6106 }
Romain Guy06882f82009-06-10 13:36:04 -07006107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006108 void releasePendingPointerLocked(Session s) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006109 if (DEBUG_INPUT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006110 "releasePendingPointer " + s.mPendingPointerMove);
6111 if (s.mPendingPointerMove != null) {
6112 mQueue.recycleEvent(s.mPendingPointerMove);
6113 s.mPendingPointerMove = null;
6114 }
6115 }
Romain Guy06882f82009-06-10 13:36:04 -07006116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006117 void releasePendingTrackballLocked(Session s) {
6118 if (s.mPendingTrackballMove != null) {
6119 mQueue.recycleEvent(s.mPendingTrackballMove);
6120 s.mPendingTrackballMove = null;
6121 }
6122 }
Romain Guy06882f82009-06-10 13:36:04 -07006123
Maciej Białka26824b72010-03-05 08:40:09 +01006124 void releaseMotionTarget(WindowState win) {
6125 if (mMotionTarget == win) {
6126 mMotionTarget = null;
6127 }
6128 }
6129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006130 MotionEvent finishedKey(Session session, IWindow client, boolean force,
6131 int returnWhat) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006132 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006133 TAG, "finishedKey: client=" + client + ", force=" + force);
6134
6135 if (client == null) {
6136 return null;
6137 }
6138
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07006139 MotionEvent res = null;
6140 QueuedEvent qev = null;
6141 WindowState win = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006143 synchronized (this) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006144 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006145 TAG, "finishedKey: client=" + client.asBinder()
6146 + ", force=" + force + ", last=" + mLastBinder
6147 + " (token=" + (mLastWin != null ? mLastWin.mToken : null) + ")");
6148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006149 if (returnWhat == RETURN_PENDING_POINTER) {
6150 qev = session.mPendingPointerMove;
6151 win = session.mPendingPointerWindow;
6152 session.mPendingPointerMove = null;
6153 session.mPendingPointerWindow = null;
6154 } else if (returnWhat == RETURN_PENDING_TRACKBALL) {
6155 qev = session.mPendingTrackballMove;
6156 win = session.mPendingTrackballWindow;
6157 session.mPendingTrackballMove = null;
6158 session.mPendingTrackballWindow = null;
6159 }
Romain Guy06882f82009-06-10 13:36:04 -07006160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006161 if (mLastBinder == client.asBinder()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006162 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006163 TAG, "finishedKey: last paused="
6164 + ((mLastWin != null) ? mLastWin.mToken.paused : "null"));
6165 if (mLastWin != null && (!mLastWin.mToken.paused || force
6166 || !mEventDispatching)) {
Christopher Tate136b1f92010-02-11 17:51:24 -08006167 doFinishedKeyLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006168 } else {
6169 // Make sure to wake up anyone currently waiting to
6170 // dispatch a key, so they can re-evaluate their
6171 // current situation.
6172 mFinished = true;
6173 notifyAll();
6174 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006175 }
Romain Guy06882f82009-06-10 13:36:04 -07006176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006177 if (qev != null) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07006178 res = (MotionEvent)qev.event;
Joe Onorato8a9b2202010-02-26 18:56:32 -08006179 if (DEBUG_INPUT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006180 "Returning pending motion: " + res);
6181 mQueue.recycleEvent(qev);
6182 if (win != null && returnWhat == RETURN_PENDING_POINTER) {
6183 res.offsetLocation(-win.mFrame.left, -win.mFrame.top);
6184 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006185 }
Christopher Tate2624fbc2009-12-11 12:11:31 -08006186 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006187
Christopher Tate2624fbc2009-12-11 12:11:31 -08006188 if (res != null && returnWhat == RETURN_PENDING_POINTER) {
6189 synchronized (mWindowMap) {
Dianne Hackborn90d2db32010-02-11 22:19:06 -08006190 dispatchPointerElsewhereLocked(win, win, res, res.getEventTime(), false);
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07006191 }
6192 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006193
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07006194 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006195 }
6196
6197 void tickle() {
6198 synchronized (this) {
6199 notifyAll();
6200 }
6201 }
Romain Guy06882f82009-06-10 13:36:04 -07006202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006203 void handleNewWindowLocked(WindowState newWindow) {
6204 if (!newWindow.canReceiveKeys()) {
6205 return;
6206 }
6207 synchronized (this) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006208 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006209 TAG, "New key dispatch window: win="
6210 + newWindow.mClient.asBinder()
6211 + ", last=" + mLastBinder
6212 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
6213 + "), finished=" + mFinished + ", paused="
6214 + newWindow.mToken.paused);
6215
6216 // Displaying a window implicitly causes dispatching to
6217 // be unpaused. (This is to protect against bugs if someone
6218 // pauses dispatching but forgets to resume.)
6219 newWindow.mToken.paused = false;
6220
6221 mGotFirstWindow = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006222
6223 if ((newWindow.mAttrs.flags & FLAG_SYSTEM_ERROR) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006224 if (DEBUG_INPUT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006225 "New SYSTEM_ERROR window; resetting state");
6226 mLastWin = null;
6227 mLastBinder = null;
6228 mMotionTarget = null;
6229 mFinished = true;
6230 } else if (mLastWin != null) {
6231 // If the new window is above the window we are
6232 // waiting on, then stop waiting and let key dispatching
6233 // start on the new guy.
Joe Onorato8a9b2202010-02-26 18:56:32 -08006234 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006235 TAG, "Last win layer=" + mLastWin.mLayer
6236 + ", new win layer=" + newWindow.mLayer);
6237 if (newWindow.mLayer >= mLastWin.mLayer) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006238 // The new window is above the old; finish pending input to the last
6239 // window and start directing it to the new one.
6240 mLastWin.mToken.paused = false;
Christopher Tate136b1f92010-02-11 17:51:24 -08006241 doFinishedKeyLocked(false); // does a notifyAll()
6242 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006243 }
6244 }
6245
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006246 // Now that we've put a new window state in place, make the event waiter
6247 // take notice and retarget its attentions.
6248 notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006249 }
6250 }
6251
6252 void pauseDispatchingLocked(WindowToken token) {
6253 synchronized (this)
6254 {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006255 if (DEBUG_INPUT) Slog.v(TAG, "Pausing WindowToken " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006256 token.paused = true;
6257
6258 /*
6259 if (mLastWin != null && !mFinished && mLastWin.mBaseLayer <= layer) {
6260 mPaused = true;
6261 } else {
6262 if (mLastWin == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006263 Slog.i(TAG, "Key dispatching not paused: no last window.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006264 } else if (mFinished) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006265 Slog.i(TAG, "Key dispatching not paused: finished last key.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006266 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006267 Slog.i(TAG, "Key dispatching not paused: window in higher layer.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006268 }
6269 }
6270 */
6271 }
6272 }
6273
6274 void resumeDispatchingLocked(WindowToken token) {
6275 synchronized (this) {
6276 if (token.paused) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006277 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006278 TAG, "Resuming WindowToken " + token
6279 + ", last=" + mLastBinder
6280 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
6281 + "), finished=" + mFinished + ", paused="
6282 + token.paused);
6283 token.paused = false;
6284 if (mLastWin != null && mLastWin.mToken == token && mFinished) {
Christopher Tate136b1f92010-02-11 17:51:24 -08006285 doFinishedKeyLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006286 } else {
6287 notifyAll();
6288 }
6289 }
6290 }
6291 }
6292
6293 void setEventDispatchingLocked(boolean enabled) {
6294 synchronized (this) {
6295 mEventDispatching = enabled;
6296 notifyAll();
6297 }
6298 }
Romain Guy06882f82009-06-10 13:36:04 -07006299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006300 void appSwitchComing() {
6301 synchronized (this) {
6302 // Don't wait for more than .5 seconds for app to finish
6303 // processing the pending events.
6304 long now = SystemClock.uptimeMillis() + 500;
Joe Onorato8a9b2202010-02-26 18:56:32 -08006305 if (DEBUG_INPUT) Slog.v(TAG, "appSwitchComing: " + now);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006306 if (mTimeToSwitch == 0 || now < mTimeToSwitch) {
6307 mTimeToSwitch = now;
6308 }
6309 notifyAll();
6310 }
6311 }
Romain Guy06882f82009-06-10 13:36:04 -07006312
Christopher Tate136b1f92010-02-11 17:51:24 -08006313 private final void doFinishedKeyLocked(boolean force) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006314 if (mLastWin != null) {
6315 releasePendingPointerLocked(mLastWin.mSession);
6316 releasePendingTrackballLocked(mLastWin.mSession);
6317 }
Romain Guy06882f82009-06-10 13:36:04 -07006318
Christopher Tate136b1f92010-02-11 17:51:24 -08006319 if (force || mLastWin == null || !mLastWin.mToken.paused
6320 || !mLastWin.isVisibleLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006321 // If the current window has been paused, we aren't -really-
6322 // finished... so let the waiters still wait.
6323 mLastWin = null;
6324 mLastBinder = null;
6325 }
6326 mFinished = true;
6327 notifyAll();
6328 }
6329 }
6330
6331 private class KeyQ extends KeyInputQueue
6332 implements KeyInputQueue.FilterCallback {
6333 PowerManager.WakeLock mHoldingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07006334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006335 KeyQ() {
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07006336 super(mContext, WindowManagerService.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006337 PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
6338 mHoldingScreen = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
6339 "KEEP_SCREEN_ON_FLAG");
6340 mHoldingScreen.setReferenceCounted(false);
6341 }
6342
6343 @Override
6344 boolean preprocessEvent(InputDevice device, RawInputEvent event) {
6345 if (mPolicy.preprocessInputEventTq(event)) {
6346 return true;
6347 }
Romain Guy06882f82009-06-10 13:36:04 -07006348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006349 switch (event.type) {
6350 case RawInputEvent.EV_KEY: {
6351 // XXX begin hack
6352 if (DEBUG) {
6353 if (event.keycode == KeyEvent.KEYCODE_G) {
6354 if (event.value != 0) {
6355 // G down
6356 mPolicy.screenTurnedOff(WindowManagerPolicy.OFF_BECAUSE_OF_USER);
6357 }
6358 return false;
6359 }
6360 if (event.keycode == KeyEvent.KEYCODE_D) {
6361 if (event.value != 0) {
6362 //dump();
6363 }
6364 return false;
6365 }
6366 }
6367 // XXX end hack
Romain Guy06882f82009-06-10 13:36:04 -07006368
Charles Mendis322591c2009-10-29 11:06:59 -07006369 boolean screenIsOff = !mPowerManager.isScreenOn();
6370 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006371 int actions = mPolicy.interceptKeyTq(event, !screenIsOff);
Romain Guy06882f82009-06-10 13:36:04 -07006372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006373 if ((actions & WindowManagerPolicy.ACTION_GO_TO_SLEEP) != 0) {
6374 mPowerManager.goToSleep(event.when);
6375 }
6376
6377 if (screenIsOff) {
6378 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6379 }
6380 if (screenIsDim) {
6381 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6382 }
6383 if ((actions & WindowManagerPolicy.ACTION_POKE_USER_ACTIVITY) != 0) {
6384 mPowerManager.userActivity(event.when, false,
Michael Chane96440f2009-05-06 10:27:36 -07006385 LocalPowerManager.BUTTON_EVENT, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006386 }
Romain Guy06882f82009-06-10 13:36:04 -07006387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006388 if ((actions & WindowManagerPolicy.ACTION_PASS_TO_USER) != 0) {
6389 if (event.value != 0 && mPolicy.isAppSwitchKeyTqTiLwLi(event.keycode)) {
6390 filterQueue(this);
6391 mKeyWaiter.appSwitchComing();
6392 }
6393 return true;
6394 } else {
6395 return false;
6396 }
6397 }
Romain Guy06882f82009-06-10 13:36:04 -07006398
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006399 case RawInputEvent.EV_REL: {
Charles Mendis322591c2009-10-29 11:06:59 -07006400 boolean screenIsOff = !mPowerManager.isScreenOn();
6401 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006402 if (screenIsOff) {
6403 if (!mPolicy.isWakeRelMovementTq(event.deviceId,
6404 device.classes, event)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006405 //Slog.i(TAG, "dropping because screenIsOff and !isWakeKey");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006406 return false;
6407 }
6408 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6409 }
6410 if (screenIsDim) {
6411 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6412 }
6413 return true;
6414 }
Romain Guy06882f82009-06-10 13:36:04 -07006415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006416 case RawInputEvent.EV_ABS: {
Charles Mendis322591c2009-10-29 11:06:59 -07006417 boolean screenIsOff = !mPowerManager.isScreenOn();
6418 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006419 if (screenIsOff) {
6420 if (!mPolicy.isWakeAbsMovementTq(event.deviceId,
6421 device.classes, event)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006422 //Slog.i(TAG, "dropping because screenIsOff and !isWakeKey");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006423 return false;
6424 }
6425 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6426 }
6427 if (screenIsDim) {
6428 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6429 }
6430 return true;
6431 }
Romain Guy06882f82009-06-10 13:36:04 -07006432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006433 default:
6434 return true;
6435 }
6436 }
6437
6438 public int filterEvent(QueuedEvent ev) {
6439 switch (ev.classType) {
6440 case RawInputEvent.CLASS_KEYBOARD:
6441 KeyEvent ke = (KeyEvent)ev.event;
6442 if (mPolicy.isMovementKeyTi(ke.getKeyCode())) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006443 Slog.w(TAG, "Dropping movement key during app switch: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006444 + ke.getKeyCode() + ", action=" + ke.getAction());
6445 return FILTER_REMOVE;
6446 }
6447 return FILTER_ABORT;
6448 default:
6449 return FILTER_KEEP;
6450 }
6451 }
Romain Guy06882f82009-06-10 13:36:04 -07006452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006453 /**
6454 * Must be called with the main window manager lock held.
6455 */
6456 void setHoldScreenLocked(boolean holding) {
6457 boolean state = mHoldingScreen.isHeld();
6458 if (holding != state) {
6459 if (holding) {
6460 mHoldingScreen.acquire();
6461 } else {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006462 mPolicy.screenOnStoppedLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006463 mHoldingScreen.release();
6464 }
6465 }
6466 }
Michael Chan53071d62009-05-13 17:29:48 -07006467 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006468
6469 public boolean detectSafeMode() {
6470 mSafeMode = mPolicy.detectSafeMode();
6471 return mSafeMode;
6472 }
Romain Guy06882f82009-06-10 13:36:04 -07006473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006474 public void systemReady() {
6475 mPolicy.systemReady();
6476 }
Romain Guy06882f82009-06-10 13:36:04 -07006477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006478 private final class InputDispatcherThread extends Thread {
6479 // Time to wait when there is nothing to do: 9999 seconds.
6480 static final int LONG_WAIT=9999*1000;
6481
6482 public InputDispatcherThread() {
6483 super("InputDispatcher");
6484 }
Romain Guy06882f82009-06-10 13:36:04 -07006485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006486 @Override
6487 public void run() {
6488 while (true) {
6489 try {
6490 process();
6491 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006492 Slog.e(TAG, "Exception in input dispatcher", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006493 }
6494 }
6495 }
Romain Guy06882f82009-06-10 13:36:04 -07006496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006497 private void process() {
6498 android.os.Process.setThreadPriority(
6499 android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -07006500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006501 // The last key event we saw
6502 KeyEvent lastKey = null;
6503
6504 // Last keydown time for auto-repeating keys
6505 long lastKeyTime = SystemClock.uptimeMillis();
6506 long nextKeyTime = lastKeyTime+LONG_WAIT;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006507 long downTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006508
Romain Guy06882f82009-06-10 13:36:04 -07006509 // How many successive repeats we generated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006510 int keyRepeatCount = 0;
6511
6512 // Need to report that configuration has changed?
6513 boolean configChanged = false;
Romain Guy06882f82009-06-10 13:36:04 -07006514
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006515 while (true) {
6516 long curTime = SystemClock.uptimeMillis();
6517
Joe Onorato8a9b2202010-02-26 18:56:32 -08006518 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006519 TAG, "Waiting for next key: now=" + curTime
6520 + ", repeat @ " + nextKeyTime);
6521
6522 // Retrieve next event, waiting only as long as the next
6523 // repeat timeout. If the configuration has changed, then
6524 // don't wait at all -- we'll report the change as soon as
6525 // we have processed all events.
6526 QueuedEvent ev = mQueue.getEvent(
6527 (int)((!configChanged && curTime < nextKeyTime)
6528 ? (nextKeyTime-curTime) : 0));
6529
Joe Onorato8a9b2202010-02-26 18:56:32 -08006530 if (DEBUG_INPUT && ev != null) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006531 TAG, "Event: type=" + ev.classType + " data=" + ev.event);
6532
Michael Chan53071d62009-05-13 17:29:48 -07006533 if (MEASURE_LATENCY) {
6534 lt.sample("2 got event ", System.nanoTime() - ev.whenNano);
6535 }
6536
Mike Lockwood3d0ea722009-10-21 22:58:29 -04006537 if (lastKey != null && !mPolicy.allowKeyRepeat()) {
6538 // cancel key repeat at the request of the policy.
6539 lastKey = null;
6540 downTime = 0;
6541 lastKeyTime = curTime;
6542 nextKeyTime = curTime + LONG_WAIT;
6543 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006544 try {
6545 if (ev != null) {
Michael Chan53071d62009-05-13 17:29:48 -07006546 curTime = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006547 int eventType;
6548 if (ev.classType == RawInputEvent.CLASS_TOUCHSCREEN) {
6549 eventType = eventType((MotionEvent)ev.event);
6550 } else if (ev.classType == RawInputEvent.CLASS_KEYBOARD ||
6551 ev.classType == RawInputEvent.CLASS_TRACKBALL) {
6552 eventType = LocalPowerManager.BUTTON_EVENT;
6553 } else {
6554 eventType = LocalPowerManager.OTHER_EVENT;
6555 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006556 try {
Michael Chan53071d62009-05-13 17:29:48 -07006557 if ((curTime - mLastBatteryStatsCallTime)
Michael Chane96440f2009-05-06 10:27:36 -07006558 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
Michael Chan53071d62009-05-13 17:29:48 -07006559 mLastBatteryStatsCallTime = curTime;
Michael Chane96440f2009-05-06 10:27:36 -07006560 mBatteryStats.noteInputEvent();
6561 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006562 } catch (RemoteException e) {
6563 // Ignore
6564 }
Michael Chane10de972009-05-18 11:24:50 -07006565
Mike Lockwood5db42402009-11-30 14:51:51 -05006566 if (ev.classType == RawInputEvent.CLASS_CONFIGURATION_CHANGED) {
6567 // do not wake screen in this case
6568 } else if (eventType != TOUCH_EVENT
Michael Chane10de972009-05-18 11:24:50 -07006569 && eventType != LONG_TOUCH_EVENT
6570 && eventType != CHEEK_EVENT) {
6571 mPowerManager.userActivity(curTime, false,
6572 eventType, false);
6573 } else if (mLastTouchEventType != eventType
6574 || (curTime - mLastUserActivityCallTime)
6575 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
6576 mLastUserActivityCallTime = curTime;
6577 mLastTouchEventType = eventType;
6578 mPowerManager.userActivity(curTime, false,
6579 eventType, false);
6580 }
6581
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006582 switch (ev.classType) {
6583 case RawInputEvent.CLASS_KEYBOARD:
6584 KeyEvent ke = (KeyEvent)ev.event;
6585 if (ke.isDown()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006586 lastKeyTime = curTime;
Kristian Dreher133bfdf2010-02-23 08:50:58 +01006587 if (lastKey != null &&
6588 ke.getKeyCode() == lastKey.getKeyCode()) {
6589 keyRepeatCount++;
6590 // Arbitrary long timeout to block
6591 // repeating here since we know that
6592 // the device driver takes care of it.
6593 nextKeyTime = lastKeyTime + LONG_WAIT;
The Android Open Source Project2a9ae012010-05-12 12:33:35 -07006594 if (DEBUG_INPUT) Slog.v(
Kristian Dreher133bfdf2010-02-23 08:50:58 +01006595 TAG, "Received repeated key down");
6596 } else {
6597 downTime = curTime;
6598 keyRepeatCount = 0;
6599 nextKeyTime = lastKeyTime
6600 + ViewConfiguration.getLongPressTimeout();
The Android Open Source Project2a9ae012010-05-12 12:33:35 -07006601 if (DEBUG_INPUT) Slog.v(
Kristian Dreher133bfdf2010-02-23 08:50:58 +01006602 TAG, "Received key down: first repeat @ "
6603 + nextKeyTime);
6604 }
6605 lastKey = ke;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006606 } else {
6607 lastKey = null;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006608 downTime = 0;
Kristian Dreher133bfdf2010-02-23 08:50:58 +01006609 keyRepeatCount = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006610 // Arbitrary long timeout.
6611 lastKeyTime = curTime;
6612 nextKeyTime = curTime + LONG_WAIT;
Joe Onorato8a9b2202010-02-26 18:56:32 -08006613 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006614 TAG, "Received key up: ignore repeat @ "
6615 + nextKeyTime);
6616 }
Kristian Dreher133bfdf2010-02-23 08:50:58 +01006617 if (keyRepeatCount > 0) {
6618 dispatchKey(KeyEvent.changeTimeRepeat(ke,
6619 ke.getEventTime(), keyRepeatCount), 0, 0);
6620 } else {
6621 dispatchKey(ke, 0, 0);
6622 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006623 mQueue.recycleEvent(ev);
6624 break;
6625 case RawInputEvent.CLASS_TOUCHSCREEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08006626 //Slog.i(TAG, "Read next event " + ev);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006627 dispatchPointer(ev, (MotionEvent)ev.event, 0, 0);
6628 break;
6629 case RawInputEvent.CLASS_TRACKBALL:
6630 dispatchTrackball(ev, (MotionEvent)ev.event, 0, 0);
6631 break;
6632 case RawInputEvent.CLASS_CONFIGURATION_CHANGED:
6633 configChanged = true;
6634 break;
6635 default:
6636 mQueue.recycleEvent(ev);
6637 break;
6638 }
Romain Guy06882f82009-06-10 13:36:04 -07006639
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006640 } else if (configChanged) {
6641 configChanged = false;
6642 sendNewConfiguration();
Romain Guy06882f82009-06-10 13:36:04 -07006643
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006644 } else if (lastKey != null) {
6645 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006646
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006647 // Timeout occurred while key was down. If it is at or
6648 // past the key repeat time, dispatch the repeat.
Joe Onorato8a9b2202010-02-26 18:56:32 -08006649 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006650 TAG, "Key timeout: repeat=" + nextKeyTime
6651 + ", now=" + curTime);
6652 if (curTime < nextKeyTime) {
6653 continue;
6654 }
Romain Guy06882f82009-06-10 13:36:04 -07006655
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006656 lastKeyTime = nextKeyTime;
6657 nextKeyTime = nextKeyTime + KEY_REPEAT_DELAY;
6658 keyRepeatCount++;
Joe Onorato8a9b2202010-02-26 18:56:32 -08006659 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006660 TAG, "Key repeat: count=" + keyRepeatCount
6661 + ", next @ " + nextKeyTime);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006662 KeyEvent newEvent;
6663 if (downTime != 0 && (downTime
6664 + ViewConfiguration.getLongPressTimeout())
6665 <= curTime) {
6666 newEvent = KeyEvent.changeTimeRepeat(lastKey,
6667 curTime, keyRepeatCount,
6668 lastKey.getFlags() | KeyEvent.FLAG_LONG_PRESS);
6669 downTime = 0;
6670 } else {
6671 newEvent = KeyEvent.changeTimeRepeat(lastKey,
6672 curTime, keyRepeatCount);
6673 }
6674 dispatchKey(newEvent, 0, 0);
Romain Guy06882f82009-06-10 13:36:04 -07006675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006676 } else {
6677 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006678
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006679 lastKeyTime = curTime;
6680 nextKeyTime = curTime + LONG_WAIT;
6681 }
Romain Guy06882f82009-06-10 13:36:04 -07006682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006683 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006684 Slog.e(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006685 "Input thread received uncaught exception: " + e, e);
6686 }
6687 }
6688 }
6689 }
6690
6691 // -------------------------------------------------------------
6692 // Client Session State
6693 // -------------------------------------------------------------
6694
6695 private final class Session extends IWindowSession.Stub
6696 implements IBinder.DeathRecipient {
6697 final IInputMethodClient mClient;
6698 final IInputContext mInputContext;
6699 final int mUid;
6700 final int mPid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006701 final String mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006702 SurfaceSession mSurfaceSession;
6703 int mNumWindow = 0;
6704 boolean mClientDead = false;
Romain Guy06882f82009-06-10 13:36:04 -07006705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006706 /**
6707 * Current pointer move event being dispatched to client window... must
6708 * hold key lock to access.
6709 */
6710 QueuedEvent mPendingPointerMove;
6711 WindowState mPendingPointerWindow;
Romain Guy06882f82009-06-10 13:36:04 -07006712
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006713 /**
6714 * Current trackball move event being dispatched to client window... must
6715 * hold key lock to access.
6716 */
6717 QueuedEvent mPendingTrackballMove;
6718 WindowState mPendingTrackballWindow;
6719
6720 public Session(IInputMethodClient client, IInputContext inputContext) {
6721 mClient = client;
6722 mInputContext = inputContext;
6723 mUid = Binder.getCallingUid();
6724 mPid = Binder.getCallingPid();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006725 StringBuilder sb = new StringBuilder();
6726 sb.append("Session{");
6727 sb.append(Integer.toHexString(System.identityHashCode(this)));
6728 sb.append(" uid ");
6729 sb.append(mUid);
6730 sb.append("}");
6731 mStringName = sb.toString();
Romain Guy06882f82009-06-10 13:36:04 -07006732
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006733 synchronized (mWindowMap) {
6734 if (mInputMethodManager == null && mHaveInputMethods) {
6735 IBinder b = ServiceManager.getService(
6736 Context.INPUT_METHOD_SERVICE);
6737 mInputMethodManager = IInputMethodManager.Stub.asInterface(b);
6738 }
6739 }
6740 long ident = Binder.clearCallingIdentity();
6741 try {
6742 // Note: it is safe to call in to the input method manager
6743 // here because we are not holding our lock.
6744 if (mInputMethodManager != null) {
6745 mInputMethodManager.addClient(client, inputContext,
6746 mUid, mPid);
6747 } else {
6748 client.setUsingInputMethod(false);
6749 }
6750 client.asBinder().linkToDeath(this, 0);
6751 } catch (RemoteException e) {
6752 // The caller has died, so we can just forget about this.
6753 try {
6754 if (mInputMethodManager != null) {
6755 mInputMethodManager.removeClient(client);
6756 }
6757 } catch (RemoteException ee) {
6758 }
6759 } finally {
6760 Binder.restoreCallingIdentity(ident);
6761 }
6762 }
Romain Guy06882f82009-06-10 13:36:04 -07006763
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006764 @Override
6765 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
6766 throws RemoteException {
6767 try {
6768 return super.onTransact(code, data, reply, flags);
6769 } catch (RuntimeException e) {
6770 // Log all 'real' exceptions thrown to the caller
6771 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006772 Slog.e(TAG, "Window Session Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006773 }
6774 throw e;
6775 }
6776 }
6777
6778 public void binderDied() {
6779 // Note: it is safe to call in to the input method manager
6780 // here because we are not holding our lock.
6781 try {
6782 if (mInputMethodManager != null) {
6783 mInputMethodManager.removeClient(mClient);
6784 }
6785 } catch (RemoteException e) {
6786 }
6787 synchronized(mWindowMap) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07006788 mClient.asBinder().unlinkToDeath(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006789 mClientDead = true;
6790 killSessionLocked();
6791 }
6792 }
6793
6794 public int add(IWindow window, WindowManager.LayoutParams attrs,
6795 int viewVisibility, Rect outContentInsets) {
6796 return addWindow(this, window, attrs, viewVisibility, outContentInsets);
6797 }
Romain Guy06882f82009-06-10 13:36:04 -07006798
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006799 public void remove(IWindow window) {
6800 removeWindow(this, window);
6801 }
Romain Guy06882f82009-06-10 13:36:04 -07006802
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006803 public int relayout(IWindow window, WindowManager.LayoutParams attrs,
6804 int requestedWidth, int requestedHeight, int viewFlags,
6805 boolean insetsPending, Rect outFrame, Rect outContentInsets,
Dianne Hackborn694f79b2010-03-17 19:44:59 -07006806 Rect outVisibleInsets, Configuration outConfig, Surface outSurface) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006807 return relayoutWindow(this, window, attrs,
6808 requestedWidth, requestedHeight, viewFlags, insetsPending,
Dianne Hackborn694f79b2010-03-17 19:44:59 -07006809 outFrame, outContentInsets, outVisibleInsets, outConfig, outSurface);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006810 }
Romain Guy06882f82009-06-10 13:36:04 -07006811
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006812 public void setTransparentRegion(IWindow window, Region region) {
6813 setTransparentRegionWindow(this, window, region);
6814 }
Romain Guy06882f82009-06-10 13:36:04 -07006815
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006816 public void setInsets(IWindow window, int touchableInsets,
6817 Rect contentInsets, Rect visibleInsets) {
6818 setInsetsWindow(this, window, touchableInsets, contentInsets,
6819 visibleInsets);
6820 }
Romain Guy06882f82009-06-10 13:36:04 -07006821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006822 public void getDisplayFrame(IWindow window, Rect outDisplayFrame) {
6823 getWindowDisplayFrame(this, window, outDisplayFrame);
6824 }
Romain Guy06882f82009-06-10 13:36:04 -07006825
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006826 public void finishDrawing(IWindow window) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006827 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006828 TAG, "IWindow finishDrawing called for " + window);
6829 finishDrawingWindow(this, window);
6830 }
6831
6832 public void finishKey(IWindow window) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006833 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006834 TAG, "IWindow finishKey called for " + window);
6835 mKeyWaiter.finishedKey(this, window, false,
6836 KeyWaiter.RETURN_NOTHING);
6837 }
6838
6839 public MotionEvent getPendingPointerMove(IWindow window) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006840 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006841 TAG, "IWindow getPendingMotionEvent called for " + window);
6842 return mKeyWaiter.finishedKey(this, window, false,
6843 KeyWaiter.RETURN_PENDING_POINTER);
6844 }
Romain Guy06882f82009-06-10 13:36:04 -07006845
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006846 public MotionEvent getPendingTrackballMove(IWindow window) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006847 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006848 TAG, "IWindow getPendingMotionEvent called for " + window);
6849 return mKeyWaiter.finishedKey(this, window, false,
6850 KeyWaiter.RETURN_PENDING_TRACKBALL);
6851 }
6852
6853 public void setInTouchMode(boolean mode) {
6854 synchronized(mWindowMap) {
6855 mInTouchMode = mode;
6856 }
6857 }
6858
6859 public boolean getInTouchMode() {
6860 synchronized(mWindowMap) {
6861 return mInTouchMode;
6862 }
6863 }
6864
6865 public boolean performHapticFeedback(IWindow window, int effectId,
6866 boolean always) {
6867 synchronized(mWindowMap) {
6868 long ident = Binder.clearCallingIdentity();
6869 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006870 return mPolicy.performHapticFeedbackLw(
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006871 windowForClientLocked(this, window, true),
6872 effectId, always);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006873 } finally {
6874 Binder.restoreCallingIdentity(ident);
6875 }
6876 }
6877 }
Romain Guy06882f82009-06-10 13:36:04 -07006878
Marco Nelissenbf6956b2009-11-09 15:21:13 -08006879 public void setWallpaperPosition(IBinder window, float x, float y, float xStep, float yStep) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006880 synchronized(mWindowMap) {
6881 long ident = Binder.clearCallingIdentity();
6882 try {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006883 setWindowWallpaperPositionLocked(
6884 windowForClientLocked(this, window, true),
Marco Nelissenbf6956b2009-11-09 15:21:13 -08006885 x, y, xStep, yStep);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006886 } finally {
6887 Binder.restoreCallingIdentity(ident);
6888 }
6889 }
6890 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006891
Dianne Hackborn19382ac2009-09-11 21:13:37 -07006892 public void wallpaperOffsetsComplete(IBinder window) {
6893 WindowManagerService.this.wallpaperOffsetsComplete(window);
6894 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006895
Dianne Hackborn75804932009-10-20 20:15:20 -07006896 public Bundle sendWallpaperCommand(IBinder window, String action, int x, int y,
6897 int z, Bundle extras, boolean sync) {
6898 synchronized(mWindowMap) {
6899 long ident = Binder.clearCallingIdentity();
6900 try {
6901 return sendWindowWallpaperCommandLocked(
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006902 windowForClientLocked(this, window, true),
Dianne Hackborn75804932009-10-20 20:15:20 -07006903 action, x, y, z, extras, sync);
6904 } finally {
6905 Binder.restoreCallingIdentity(ident);
6906 }
6907 }
6908 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006909
Dianne Hackborn75804932009-10-20 20:15:20 -07006910 public void wallpaperCommandComplete(IBinder window, Bundle result) {
6911 WindowManagerService.this.wallpaperCommandComplete(window, result);
6912 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006913
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006914 void windowAddedLocked() {
6915 if (mSurfaceSession == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006916 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006917 TAG, "First window added to " + this + ", creating SurfaceSession");
6918 mSurfaceSession = new SurfaceSession();
Joe Onorato8a9b2202010-02-26 18:56:32 -08006919 if (SHOW_TRANSACTIONS) Slog.i(
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006920 TAG, " NEW SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006921 mSessions.add(this);
6922 }
6923 mNumWindow++;
6924 }
6925
6926 void windowRemovedLocked() {
6927 mNumWindow--;
6928 killSessionLocked();
6929 }
Romain Guy06882f82009-06-10 13:36:04 -07006930
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006931 void killSessionLocked() {
6932 if (mNumWindow <= 0 && mClientDead) {
6933 mSessions.remove(this);
6934 if (mSurfaceSession != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006935 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006936 TAG, "Last window removed from " + this
6937 + ", destroying " + mSurfaceSession);
Joe Onorato8a9b2202010-02-26 18:56:32 -08006938 if (SHOW_TRANSACTIONS) Slog.i(
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006939 TAG, " KILL SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006940 try {
6941 mSurfaceSession.kill();
6942 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006943 Slog.w(TAG, "Exception thrown when killing surface session "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006944 + mSurfaceSession + " in session " + this
6945 + ": " + e.toString());
6946 }
6947 mSurfaceSession = null;
6948 }
6949 }
6950 }
Romain Guy06882f82009-06-10 13:36:04 -07006951
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006952 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006953 pw.print(prefix); pw.print("mNumWindow="); pw.print(mNumWindow);
6954 pw.print(" mClientDead="); pw.print(mClientDead);
6955 pw.print(" mSurfaceSession="); pw.println(mSurfaceSession);
6956 if (mPendingPointerWindow != null || mPendingPointerMove != null) {
6957 pw.print(prefix);
6958 pw.print("mPendingPointerWindow="); pw.print(mPendingPointerWindow);
6959 pw.print(" mPendingPointerMove="); pw.println(mPendingPointerMove);
6960 }
6961 if (mPendingTrackballWindow != null || mPendingTrackballMove != null) {
6962 pw.print(prefix);
6963 pw.print("mPendingTrackballWindow="); pw.print(mPendingTrackballWindow);
6964 pw.print(" mPendingTrackballMove="); pw.println(mPendingTrackballMove);
6965 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006966 }
6967
6968 @Override
6969 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006970 return mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006971 }
6972 }
6973
6974 // -------------------------------------------------------------
6975 // Client Window State
6976 // -------------------------------------------------------------
6977
6978 private final class WindowState implements WindowManagerPolicy.WindowState {
6979 final Session mSession;
6980 final IWindow mClient;
6981 WindowToken mToken;
The Android Open Source Project10592532009-03-18 17:39:46 -07006982 WindowToken mRootToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006983 AppWindowToken mAppToken;
6984 AppWindowToken mTargetAppToken;
6985 final WindowManager.LayoutParams mAttrs = new WindowManager.LayoutParams();
6986 final DeathRecipient mDeathRecipient;
6987 final WindowState mAttachedWindow;
6988 final ArrayList mChildWindows = new ArrayList();
6989 final int mBaseLayer;
6990 final int mSubLayer;
6991 final boolean mLayoutAttached;
6992 final boolean mIsImWindow;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006993 final boolean mIsWallpaper;
6994 final boolean mIsFloatingLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006995 int mViewVisibility;
6996 boolean mPolicyVisibility = true;
6997 boolean mPolicyVisibilityAfterAnim = true;
6998 boolean mAppFreezing;
6999 Surface mSurface;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007000 boolean mReportDestroySurface;
7001 boolean mSurfacePendingDestroy;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007002 boolean mAttachedHidden; // is our parent window hidden?
7003 boolean mLastHidden; // was this window last hidden?
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007004 boolean mWallpaperVisible; // for wallpaper, what was last vis report?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007005 int mRequestedWidth;
7006 int mRequestedHeight;
7007 int mLastRequestedWidth;
7008 int mLastRequestedHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007009 int mLayer;
7010 int mAnimLayer;
7011 int mLastLayer;
7012 boolean mHaveFrame;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007013 boolean mObscured;
Dianne Hackborn93e462b2009-09-15 22:50:40 -07007014 boolean mTurnOnScreen;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007015
7016 WindowState mNextOutsideTouch;
Romain Guy06882f82009-06-10 13:36:04 -07007017
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007018 int mLayoutSeq = -1;
7019
7020 Configuration mConfiguration = null;
7021
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007022 // Actual frame shown on-screen (may be modified by animation)
7023 final Rect mShownFrame = new Rect();
7024 final Rect mLastShownFrame = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07007025
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007026 /**
Dianne Hackbornac3587d2010-03-11 11:12:11 -08007027 * Set when we have changed the size of the surface, to know that
7028 * we must tell them application to resize (and thus redraw itself).
7029 */
7030 boolean mSurfaceResized;
7031
7032 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007033 * Insets that determine the actually visible area
7034 */
7035 final Rect mVisibleInsets = new Rect();
7036 final Rect mLastVisibleInsets = new Rect();
7037 boolean mVisibleInsetsChanged;
7038
7039 /**
7040 * Insets that are covered by system windows
7041 */
7042 final Rect mContentInsets = new Rect();
7043 final Rect mLastContentInsets = new Rect();
7044 boolean mContentInsetsChanged;
7045
7046 /**
7047 * Set to true if we are waiting for this window to receive its
7048 * given internal insets before laying out other windows based on it.
7049 */
7050 boolean mGivenInsetsPending;
Romain Guy06882f82009-06-10 13:36:04 -07007051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007052 /**
7053 * These are the content insets that were given during layout for
7054 * this window, to be applied to windows behind it.
7055 */
7056 final Rect mGivenContentInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07007057
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007058 /**
7059 * These are the visible insets that were given during layout for
7060 * this window, to be applied to windows behind it.
7061 */
7062 final Rect mGivenVisibleInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07007063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007064 /**
7065 * Flag indicating whether the touchable region should be adjusted by
7066 * the visible insets; if false the area outside the visible insets is
7067 * NOT touchable, so we must use those to adjust the frame during hit
7068 * tests.
7069 */
7070 int mTouchableInsets = ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME;
Romain Guy06882f82009-06-10 13:36:04 -07007071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007072 // Current transformation being applied.
7073 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
7074 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
7075 float mHScale=1, mVScale=1;
7076 float mLastHScale=1, mLastVScale=1;
7077 final Matrix mTmpMatrix = new Matrix();
7078
7079 // "Real" frame that the application sees.
7080 final Rect mFrame = new Rect();
7081 final Rect mLastFrame = new Rect();
7082
7083 final Rect mContainingFrame = new Rect();
7084 final Rect mDisplayFrame = new Rect();
7085 final Rect mContentFrame = new Rect();
7086 final Rect mVisibleFrame = new Rect();
7087
7088 float mShownAlpha = 1;
7089 float mAlpha = 1;
7090 float mLastAlpha = 1;
7091
7092 // Set to true if, when the window gets displayed, it should perform
7093 // an enter animation.
7094 boolean mEnterAnimationPending;
7095
7096 // Currently running animation.
7097 boolean mAnimating;
7098 boolean mLocalAnimating;
7099 Animation mAnimation;
7100 boolean mAnimationIsEntrance;
7101 boolean mHasTransformation;
7102 boolean mHasLocalTransformation;
7103 final Transformation mTransformation = new Transformation();
7104
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07007105 // If a window showing a wallpaper: the requested offset for the
7106 // wallpaper; if a wallpaper window: the currently applied offset.
7107 float mWallpaperX = -1;
7108 float mWallpaperY = -1;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08007109
7110 // If a window showing a wallpaper: what fraction of the offset
7111 // range corresponds to a full virtual screen.
7112 float mWallpaperXStep = -1;
7113 float mWallpaperYStep = -1;
7114
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07007115 // Wallpaper windows: pixels offset based on above variables.
7116 int mXOffset;
7117 int mYOffset;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007119 // This is set after IWindowSession.relayout() has been called at
7120 // least once for the window. It allows us to detect the situation
7121 // where we don't yet have a surface, but should have one soon, so
7122 // we can give the window focus before waiting for the relayout.
7123 boolean mRelayoutCalled;
Romain Guy06882f82009-06-10 13:36:04 -07007124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007125 // This is set after the Surface has been created but before the
7126 // window has been drawn. During this time the surface is hidden.
7127 boolean mDrawPending;
7128
7129 // This is set after the window has finished drawing for the first
7130 // time but before its surface is shown. The surface will be
7131 // displayed when the next layout is run.
7132 boolean mCommitDrawPending;
7133
7134 // This is set during the time after the window's drawing has been
7135 // committed, and before its surface is actually shown. It is used
7136 // to delay showing the surface until all windows in a token are ready
7137 // to be shown.
7138 boolean mReadyToShow;
Romain Guy06882f82009-06-10 13:36:04 -07007139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007140 // Set when the window has been shown in the screen the first time.
7141 boolean mHasDrawn;
7142
7143 // Currently running an exit animation?
7144 boolean mExiting;
7145
7146 // Currently on the mDestroySurface list?
7147 boolean mDestroying;
Romain Guy06882f82009-06-10 13:36:04 -07007148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007149 // Completely remove from window manager after exit animation?
7150 boolean mRemoveOnExit;
7151
7152 // Set when the orientation is changing and this window has not yet
7153 // been updated for the new orientation.
7154 boolean mOrientationChanging;
Romain Guy06882f82009-06-10 13:36:04 -07007155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007156 // Is this window now (or just being) removed?
7157 boolean mRemoved;
Romain Guy06882f82009-06-10 13:36:04 -07007158
Dianne Hackborn16064f92010-03-25 00:47:24 -07007159 // For debugging, this is the last information given to the surface flinger.
7160 boolean mSurfaceShown;
7161 int mSurfaceX, mSurfaceY, mSurfaceW, mSurfaceH;
7162 int mSurfaceLayer;
7163 float mSurfaceAlpha;
7164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007165 WindowState(Session s, IWindow c, WindowToken token,
7166 WindowState attachedWindow, WindowManager.LayoutParams a,
7167 int viewVisibility) {
7168 mSession = s;
7169 mClient = c;
7170 mToken = token;
7171 mAttrs.copyFrom(a);
7172 mViewVisibility = viewVisibility;
7173 DeathRecipient deathRecipient = new DeathRecipient();
7174 mAlpha = a.alpha;
Joe Onorato8a9b2202010-02-26 18:56:32 -08007175 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007176 TAG, "Window " + this + " client=" + c.asBinder()
7177 + " token=" + token + " (" + mAttrs.token + ")");
7178 try {
7179 c.asBinder().linkToDeath(deathRecipient, 0);
7180 } catch (RemoteException e) {
7181 mDeathRecipient = null;
7182 mAttachedWindow = null;
7183 mLayoutAttached = false;
7184 mIsImWindow = false;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007185 mIsWallpaper = false;
7186 mIsFloatingLayer = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007187 mBaseLayer = 0;
7188 mSubLayer = 0;
7189 return;
7190 }
7191 mDeathRecipient = deathRecipient;
Romain Guy06882f82009-06-10 13:36:04 -07007192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007193 if ((mAttrs.type >= FIRST_SUB_WINDOW &&
7194 mAttrs.type <= LAST_SUB_WINDOW)) {
7195 // The multiplier here is to reserve space for multiple
7196 // windows in the same type layer.
7197 mBaseLayer = mPolicy.windowTypeToLayerLw(
7198 attachedWindow.mAttrs.type) * TYPE_LAYER_MULTIPLIER
7199 + TYPE_LAYER_OFFSET;
7200 mSubLayer = mPolicy.subWindowTypeToLayerLw(a.type);
7201 mAttachedWindow = attachedWindow;
7202 mAttachedWindow.mChildWindows.add(this);
7203 mLayoutAttached = mAttrs.type !=
7204 WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
7205 mIsImWindow = attachedWindow.mAttrs.type == TYPE_INPUT_METHOD
7206 || attachedWindow.mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007207 mIsWallpaper = attachedWindow.mAttrs.type == TYPE_WALLPAPER;
7208 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007209 } else {
7210 // The multiplier here is to reserve space for multiple
7211 // windows in the same type layer.
7212 mBaseLayer = mPolicy.windowTypeToLayerLw(a.type)
7213 * TYPE_LAYER_MULTIPLIER
7214 + TYPE_LAYER_OFFSET;
7215 mSubLayer = 0;
7216 mAttachedWindow = null;
7217 mLayoutAttached = false;
7218 mIsImWindow = mAttrs.type == TYPE_INPUT_METHOD
7219 || mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007220 mIsWallpaper = mAttrs.type == TYPE_WALLPAPER;
7221 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007222 }
7223
7224 WindowState appWin = this;
7225 while (appWin.mAttachedWindow != null) {
7226 appWin = mAttachedWindow;
7227 }
7228 WindowToken appToken = appWin.mToken;
7229 while (appToken.appWindowToken == null) {
7230 WindowToken parent = mTokenMap.get(appToken.token);
7231 if (parent == null || appToken == parent) {
7232 break;
7233 }
7234 appToken = parent;
7235 }
The Android Open Source Project10592532009-03-18 17:39:46 -07007236 mRootToken = appToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007237 mAppToken = appToken.appWindowToken;
7238
7239 mSurface = null;
7240 mRequestedWidth = 0;
7241 mRequestedHeight = 0;
7242 mLastRequestedWidth = 0;
7243 mLastRequestedHeight = 0;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007244 mXOffset = 0;
7245 mYOffset = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007246 mLayer = 0;
7247 mAnimLayer = 0;
7248 mLastLayer = 0;
7249 }
7250
7251 void attach() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007252 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007253 TAG, "Attaching " + this + " token=" + mToken
7254 + ", list=" + mToken.windows);
7255 mSession.windowAddedLocked();
7256 }
7257
7258 public void computeFrameLw(Rect pf, Rect df, Rect cf, Rect vf) {
7259 mHaveFrame = true;
7260
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007261 final Rect container = mContainingFrame;
7262 container.set(pf);
7263
7264 final Rect display = mDisplayFrame;
7265 display.set(df);
7266
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007267 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007268 container.intersect(mCompatibleScreenFrame);
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007269 if ((mAttrs.flags & FLAG_LAYOUT_NO_LIMITS) == 0) {
7270 display.intersect(mCompatibleScreenFrame);
7271 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007272 }
7273
7274 final int pw = container.right - container.left;
7275 final int ph = container.bottom - container.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007276
7277 int w,h;
7278 if ((mAttrs.flags & mAttrs.FLAG_SCALED) != 0) {
7279 w = mAttrs.width < 0 ? pw : mAttrs.width;
7280 h = mAttrs.height< 0 ? ph : mAttrs.height;
7281 } else {
Romain Guy980a9382010-01-08 15:06:28 -08007282 w = mAttrs.width == mAttrs.MATCH_PARENT ? pw : mRequestedWidth;
7283 h = mAttrs.height== mAttrs.MATCH_PARENT ? ph : mRequestedHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007284 }
Romain Guy06882f82009-06-10 13:36:04 -07007285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007286 final Rect content = mContentFrame;
7287 content.set(cf);
Romain Guy06882f82009-06-10 13:36:04 -07007288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007289 final Rect visible = mVisibleFrame;
7290 visible.set(vf);
Romain Guy06882f82009-06-10 13:36:04 -07007291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007292 final Rect frame = mFrame;
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007293 final int fw = frame.width();
7294 final int fh = frame.height();
Romain Guy06882f82009-06-10 13:36:04 -07007295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007296 //System.out.println("In: w=" + w + " h=" + h + " container=" +
7297 // container + " x=" + mAttrs.x + " y=" + mAttrs.y);
7298
7299 Gravity.apply(mAttrs.gravity, w, h, container,
7300 (int) (mAttrs.x + mAttrs.horizontalMargin * pw),
7301 (int) (mAttrs.y + mAttrs.verticalMargin * ph), frame);
7302
7303 //System.out.println("Out: " + mFrame);
7304
7305 // Now make sure the window fits in the overall display.
7306 Gravity.applyDisplay(mAttrs.gravity, df, frame);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007307
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007308 // Make sure the content and visible frames are inside of the
7309 // final window frame.
7310 if (content.left < frame.left) content.left = frame.left;
7311 if (content.top < frame.top) content.top = frame.top;
7312 if (content.right > frame.right) content.right = frame.right;
7313 if (content.bottom > frame.bottom) content.bottom = frame.bottom;
7314 if (visible.left < frame.left) visible.left = frame.left;
7315 if (visible.top < frame.top) visible.top = frame.top;
7316 if (visible.right > frame.right) visible.right = frame.right;
7317 if (visible.bottom > frame.bottom) visible.bottom = frame.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007319 final Rect contentInsets = mContentInsets;
7320 contentInsets.left = content.left-frame.left;
7321 contentInsets.top = content.top-frame.top;
7322 contentInsets.right = frame.right-content.right;
7323 contentInsets.bottom = frame.bottom-content.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007325 final Rect visibleInsets = mVisibleInsets;
7326 visibleInsets.left = visible.left-frame.left;
7327 visibleInsets.top = visible.top-frame.top;
7328 visibleInsets.right = frame.right-visible.right;
7329 visibleInsets.bottom = frame.bottom-visible.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007330
Dianne Hackborn284ac932009-08-28 10:34:25 -07007331 if (mIsWallpaper && (fw != frame.width() || fh != frame.height())) {
7332 updateWallpaperOffsetLocked(this, mDisplay.getWidth(),
Dianne Hackborn19382ac2009-09-11 21:13:37 -07007333 mDisplay.getHeight(), false);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007334 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007336 if (localLOGV) {
7337 //if ("com.google.android.youtube".equals(mAttrs.packageName)
7338 // && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007339 Slog.v(TAG, "Resolving (mRequestedWidth="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007340 + mRequestedWidth + ", mRequestedheight="
7341 + mRequestedHeight + ") to" + " (pw=" + pw + ", ph=" + ph
7342 + "): frame=" + mFrame.toShortString()
7343 + " ci=" + contentInsets.toShortString()
7344 + " vi=" + visibleInsets.toShortString());
7345 //}
7346 }
7347 }
Romain Guy06882f82009-06-10 13:36:04 -07007348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007349 public Rect getFrameLw() {
7350 return mFrame;
7351 }
7352
7353 public Rect getShownFrameLw() {
7354 return mShownFrame;
7355 }
7356
7357 public Rect getDisplayFrameLw() {
7358 return mDisplayFrame;
7359 }
7360
7361 public Rect getContentFrameLw() {
7362 return mContentFrame;
7363 }
7364
7365 public Rect getVisibleFrameLw() {
7366 return mVisibleFrame;
7367 }
7368
7369 public boolean getGivenInsetsPendingLw() {
7370 return mGivenInsetsPending;
7371 }
7372
7373 public Rect getGivenContentInsetsLw() {
7374 return mGivenContentInsets;
7375 }
Romain Guy06882f82009-06-10 13:36:04 -07007376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007377 public Rect getGivenVisibleInsetsLw() {
7378 return mGivenVisibleInsets;
7379 }
Romain Guy06882f82009-06-10 13:36:04 -07007380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007381 public WindowManager.LayoutParams getAttrs() {
7382 return mAttrs;
7383 }
7384
7385 public int getSurfaceLayer() {
7386 return mLayer;
7387 }
Romain Guy06882f82009-06-10 13:36:04 -07007388
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007389 public IApplicationToken getAppToken() {
7390 return mAppToken != null ? mAppToken.appToken : null;
7391 }
7392
7393 public boolean hasAppShownWindows() {
7394 return mAppToken != null ? mAppToken.firstWindowDrawn : false;
7395 }
7396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007397 public void setAnimation(Animation anim) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007398 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007399 TAG, "Setting animation in " + this + ": " + anim);
7400 mAnimating = false;
7401 mLocalAnimating = false;
7402 mAnimation = anim;
7403 mAnimation.restrictDuration(MAX_ANIMATION_DURATION);
7404 mAnimation.scaleCurrentDuration(mWindowAnimationScale);
7405 }
7406
7407 public void clearAnimation() {
7408 if (mAnimation != null) {
7409 mAnimating = true;
7410 mLocalAnimating = false;
7411 mAnimation = null;
7412 }
7413 }
Romain Guy06882f82009-06-10 13:36:04 -07007414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007415 Surface createSurfaceLocked() {
7416 if (mSurface == null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007417 mReportDestroySurface = false;
7418 mSurfacePendingDestroy = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007419 mDrawPending = true;
7420 mCommitDrawPending = false;
7421 mReadyToShow = false;
7422 if (mAppToken != null) {
7423 mAppToken.allDrawn = false;
7424 }
7425
7426 int flags = 0;
Mathias Agopian317a6282009-08-13 17:29:02 -07007427 if (mAttrs.memoryType == MEMORY_TYPE_PUSH_BUFFERS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007428 flags |= Surface.PUSH_BUFFERS;
7429 }
7430
7431 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
7432 flags |= Surface.SECURE;
7433 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08007434 if (DEBUG_VISIBILITY) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007435 TAG, "Creating surface in session "
7436 + mSession.mSurfaceSession + " window " + this
7437 + " w=" + mFrame.width()
7438 + " h=" + mFrame.height() + " format="
7439 + mAttrs.format + " flags=" + flags);
7440
7441 int w = mFrame.width();
7442 int h = mFrame.height();
7443 if ((mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
7444 // for a scaled surface, we always want the requested
7445 // size.
7446 w = mRequestedWidth;
7447 h = mRequestedHeight;
7448 }
7449
Romain Guy9825ec62009-10-01 00:58:09 -07007450 // Something is wrong and SurfaceFlinger will not like this,
7451 // try to revert to sane values
7452 if (w <= 0) w = 1;
7453 if (h <= 0) h = 1;
7454
Dianne Hackborn16064f92010-03-25 00:47:24 -07007455 mSurfaceShown = false;
7456 mSurfaceLayer = 0;
7457 mSurfaceAlpha = 1;
7458 mSurfaceX = 0;
7459 mSurfaceY = 0;
7460 mSurfaceW = w;
7461 mSurfaceH = h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007462 try {
7463 mSurface = new Surface(
Romain Guy06882f82009-06-10 13:36:04 -07007464 mSession.mSurfaceSession, mSession.mPid,
Mathias Agopian5d26c1e2010-03-01 16:09:43 -08007465 mAttrs.getTitle().toString(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007466 0, w, h, mAttrs.format, flags);
Joe Onorato8a9b2202010-02-26 18:56:32 -08007467 if (SHOW_TRANSACTIONS) Slog.i(TAG, " CREATE SURFACE "
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007468 + mSurface + " IN SESSION "
7469 + mSession.mSurfaceSession
7470 + ": pid=" + mSession.mPid + " format="
7471 + mAttrs.format + " flags=0x"
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007472 + Integer.toHexString(flags)
7473 + " / " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007474 } catch (Surface.OutOfResourcesException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007475 Slog.w(TAG, "OutOfResourcesException creating surface");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007476 reclaimSomeSurfaceMemoryLocked(this, "create");
7477 return null;
7478 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007479 Slog.e(TAG, "Exception creating surface", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007480 return null;
7481 }
Romain Guy06882f82009-06-10 13:36:04 -07007482
Joe Onorato8a9b2202010-02-26 18:56:32 -08007483 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007484 TAG, "Got surface: " + mSurface
7485 + ", set left=" + mFrame.left + " top=" + mFrame.top
7486 + ", animLayer=" + mAnimLayer);
7487 if (SHOW_TRANSACTIONS) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007488 Slog.i(TAG, ">>> OPEN TRANSACTION");
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007489 if (SHOW_TRANSACTIONS) logSurface(this,
7490 "CREATE pos=(" + mFrame.left + "," + mFrame.top + ") (" +
7491 mFrame.width() + "x" + mFrame.height() + "), layer=" +
7492 mAnimLayer + " HIDE", null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007493 }
7494 Surface.openTransaction();
7495 try {
7496 try {
Dianne Hackborn16064f92010-03-25 00:47:24 -07007497 mSurfaceX = mFrame.left + mXOffset;
Dianne Hackborn529bef62010-03-25 11:48:43 -07007498 mSurfaceY = mFrame.top + mYOffset;
Dianne Hackborn16064f92010-03-25 00:47:24 -07007499 mSurface.setPosition(mSurfaceX, mSurfaceY);
7500 mSurfaceLayer = mAnimLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007501 mSurface.setLayer(mAnimLayer);
Dianne Hackborn16064f92010-03-25 00:47:24 -07007502 mSurfaceShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007503 mSurface.hide();
7504 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007505 if (SHOW_TRANSACTIONS) logSurface(this, "DITHER", null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007506 mSurface.setFlags(Surface.SURFACE_DITHER,
7507 Surface.SURFACE_DITHER);
7508 }
7509 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007510 Slog.w(TAG, "Error creating surface in " + w, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007511 reclaimSomeSurfaceMemoryLocked(this, "create-init");
7512 }
7513 mLastHidden = true;
7514 } finally {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007515 if (SHOW_TRANSACTIONS) Slog.i(TAG, "<<< CLOSE TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007516 Surface.closeTransaction();
7517 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08007518 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007519 TAG, "Created surface " + this);
7520 }
7521 return mSurface;
7522 }
Romain Guy06882f82009-06-10 13:36:04 -07007523
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007524 void destroySurfaceLocked() {
7525 // Window is no longer on-screen, so can no longer receive
7526 // key events... if we were waiting for it to finish
7527 // handling a key event, the wait is over!
7528 mKeyWaiter.finishedKey(mSession, mClient, true,
7529 KeyWaiter.RETURN_NOTHING);
7530 mKeyWaiter.releasePendingPointerLocked(mSession);
7531 mKeyWaiter.releasePendingTrackballLocked(mSession);
7532
7533 if (mAppToken != null && this == mAppToken.startingWindow) {
7534 mAppToken.startingDisplayed = false;
7535 }
Romain Guy06882f82009-06-10 13:36:04 -07007536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007537 if (mSurface != null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007538 mDrawPending = false;
7539 mCommitDrawPending = false;
7540 mReadyToShow = false;
7541
7542 int i = mChildWindows.size();
7543 while (i > 0) {
7544 i--;
7545 WindowState c = (WindowState)mChildWindows.get(i);
7546 c.mAttachedHidden = true;
7547 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007548
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007549 if (mReportDestroySurface) {
7550 mReportDestroySurface = false;
7551 mSurfacePendingDestroy = true;
7552 try {
7553 mClient.dispatchGetNewSurface();
7554 // We'll really destroy on the next time around.
7555 return;
7556 } catch (RemoteException e) {
7557 }
7558 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007559
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007560 try {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007561 if (DEBUG_VISIBILITY) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007562 RuntimeException e = null;
7563 if (!HIDE_STACK_CRAWLS) {
7564 e = new RuntimeException();
7565 e.fillInStackTrace();
7566 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08007567 Slog.w(TAG, "Window " + this + " destroying surface "
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007568 + mSurface + ", session " + mSession, e);
7569 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007570 if (SHOW_TRANSACTIONS) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007571 RuntimeException e = null;
7572 if (!HIDE_STACK_CRAWLS) {
7573 e = new RuntimeException();
7574 e.fillInStackTrace();
7575 }
7576 if (SHOW_TRANSACTIONS) logSurface(this, "DESTROY", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007577 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007578 mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007579 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007580 Slog.w(TAG, "Exception thrown when destroying Window " + this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007581 + " surface " + mSurface + " session " + mSession
7582 + ": " + e.toString());
7583 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007584
Dianne Hackborn16064f92010-03-25 00:47:24 -07007585 mSurfaceShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007586 mSurface = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007587 }
7588 }
7589
7590 boolean finishDrawingLocked() {
7591 if (mDrawPending) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007592 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007593 TAG, "finishDrawingLocked: " + mSurface);
7594 mCommitDrawPending = true;
7595 mDrawPending = false;
7596 return true;
7597 }
7598 return false;
7599 }
7600
7601 // This must be called while inside a transaction.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007602 boolean commitFinishDrawingLocked(long currentTime) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007603 //Slog.i(TAG, "commitFinishDrawingLocked: " + mSurface);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007604 if (!mCommitDrawPending) {
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007605 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007606 }
7607 mCommitDrawPending = false;
7608 mReadyToShow = true;
7609 final boolean starting = mAttrs.type == TYPE_APPLICATION_STARTING;
7610 final AppWindowToken atoken = mAppToken;
7611 if (atoken == null || atoken.allDrawn || starting) {
7612 performShowLocked();
7613 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007614 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007615 }
7616
7617 // This must be called while inside a transaction.
7618 boolean performShowLocked() {
7619 if (DEBUG_VISIBILITY) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007620 RuntimeException e = null;
7621 if (!HIDE_STACK_CRAWLS) {
7622 e = new RuntimeException();
7623 e.fillInStackTrace();
7624 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08007625 Slog.v(TAG, "performShow on " + this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007626 + ": readyToShow=" + mReadyToShow + " readyForDisplay=" + isReadyForDisplay()
7627 + " starting=" + (mAttrs.type == TYPE_APPLICATION_STARTING), e);
7628 }
7629 if (mReadyToShow && isReadyForDisplay()) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007630 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) logSurface(this,
7631 "SHOW (performShowLocked)", null);
Joe Onorato8a9b2202010-02-26 18:56:32 -08007632 if (DEBUG_VISIBILITY) Slog.v(TAG, "Showing " + this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007633 + " during animation: policyVis=" + mPolicyVisibility
7634 + " attHidden=" + mAttachedHidden
7635 + " tok.hiddenRequested="
7636 + (mAppToken != null ? mAppToken.hiddenRequested : false)
Dianne Hackborn248b1882009-09-16 16:46:44 -07007637 + " tok.hidden="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007638 + (mAppToken != null ? mAppToken.hidden : false)
7639 + " animating=" + mAnimating
7640 + " tok animating="
7641 + (mAppToken != null ? mAppToken.animating : false));
7642 if (!showSurfaceRobustlyLocked(this)) {
7643 return false;
7644 }
7645 mLastAlpha = -1;
7646 mHasDrawn = true;
7647 mLastHidden = false;
7648 mReadyToShow = false;
7649 enableScreenIfNeededLocked();
7650
7651 applyEnterAnimationLocked(this);
Romain Guy06882f82009-06-10 13:36:04 -07007652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007653 int i = mChildWindows.size();
7654 while (i > 0) {
7655 i--;
7656 WindowState c = (WindowState)mChildWindows.get(i);
Dianne Hackbornf09c1a22010-04-22 15:59:21 -07007657 if (c.mAttachedHidden) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007658 c.mAttachedHidden = false;
Dianne Hackbornf09c1a22010-04-22 15:59:21 -07007659 if (c.mSurface != null) {
7660 c.performShowLocked();
7661 // It hadn't been shown, which means layout not
7662 // performed on it, so now we want to make sure to
7663 // do a layout. If called from within the transaction
7664 // loop, this will cause it to restart with a new
7665 // layout.
7666 mLayoutNeeded = true;
7667 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007668 }
7669 }
Romain Guy06882f82009-06-10 13:36:04 -07007670
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007671 if (mAttrs.type != TYPE_APPLICATION_STARTING
7672 && mAppToken != null) {
7673 mAppToken.firstWindowDrawn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007674
Dianne Hackborn248b1882009-09-16 16:46:44 -07007675 if (mAppToken.startingData != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007676 if (DEBUG_STARTING_WINDOW || DEBUG_ANIM) Slog.v(TAG,
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007677 "Finish starting " + mToken
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007678 + ": first real window is shown, no animation");
Dianne Hackborn248b1882009-09-16 16:46:44 -07007679 // If this initial window is animating, stop it -- we
7680 // will do an animation to reveal it from behind the
7681 // starting window, so there is no need for it to also
7682 // be doing its own stuff.
7683 if (mAnimation != null) {
7684 mAnimation = null;
7685 // Make sure we clean up the animation.
7686 mAnimating = true;
7687 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007688 mFinishedStarting.add(mAppToken);
7689 mH.sendEmptyMessage(H.FINISHED_STARTING);
7690 }
7691 mAppToken.updateReportedVisibilityLocked();
7692 }
7693 }
7694 return true;
7695 }
Romain Guy06882f82009-06-10 13:36:04 -07007696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007697 // This must be called while inside a transaction. Returns true if
7698 // there is more animation to run.
7699 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
Dianne Hackbornde2606d2009-12-18 16:53:55 -08007700 if (!mDisplayFrozen && mPolicy.isScreenOn()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007701 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07007702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007703 if (!mDrawPending && !mCommitDrawPending && mAnimation != null) {
7704 mHasTransformation = true;
7705 mHasLocalTransformation = true;
7706 if (!mLocalAnimating) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007707 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007708 TAG, "Starting animation in " + this +
7709 " @ " + currentTime + ": ww=" + mFrame.width() + " wh=" + mFrame.height() +
7710 " dw=" + dw + " dh=" + dh + " scale=" + mWindowAnimationScale);
7711 mAnimation.initialize(mFrame.width(), mFrame.height(), dw, dh);
7712 mAnimation.setStartTime(currentTime);
7713 mLocalAnimating = true;
7714 mAnimating = true;
7715 }
7716 mTransformation.clear();
7717 final boolean more = mAnimation.getTransformation(
7718 currentTime, mTransformation);
Joe Onorato8a9b2202010-02-26 18:56:32 -08007719 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007720 TAG, "Stepped animation in " + this +
7721 ": more=" + more + ", xform=" + mTransformation);
7722 if (more) {
7723 // we're not done!
7724 return true;
7725 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08007726 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007727 TAG, "Finished animation in " + this +
7728 " @ " + currentTime);
7729 mAnimation = null;
7730 //WindowManagerService.this.dump();
7731 }
7732 mHasLocalTransformation = false;
7733 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppToken != null
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007734 && mAppToken.animation != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007735 // When our app token is animating, we kind-of pretend like
7736 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
7737 // part of this check means that we will only do this if
7738 // our window is not currently exiting, or it is not
7739 // locally animating itself. The idea being that one that
7740 // is exiting and doing a local animation should be removed
7741 // once that animation is done.
7742 mAnimating = true;
7743 mHasTransformation = true;
7744 mTransformation.clear();
7745 return false;
7746 } else if (mHasTransformation) {
7747 // Little trick to get through the path below to act like
7748 // we have finished an animation.
7749 mAnimating = true;
7750 } else if (isAnimating()) {
7751 mAnimating = true;
7752 }
7753 } else if (mAnimation != null) {
7754 // If the display is frozen, and there is a pending animation,
7755 // clear it and make sure we run the cleanup code.
7756 mAnimating = true;
7757 mLocalAnimating = true;
7758 mAnimation = null;
7759 }
Romain Guy06882f82009-06-10 13:36:04 -07007760
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007761 if (!mAnimating && !mLocalAnimating) {
7762 return false;
7763 }
7764
Joe Onorato8a9b2202010-02-26 18:56:32 -08007765 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007766 TAG, "Animation done in " + this + ": exiting=" + mExiting
7767 + ", reportedVisible="
7768 + (mAppToken != null ? mAppToken.reportedVisible : false));
Romain Guy06882f82009-06-10 13:36:04 -07007769
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007770 mAnimating = false;
7771 mLocalAnimating = false;
7772 mAnimation = null;
7773 mAnimLayer = mLayer;
7774 if (mIsImWindow) {
7775 mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007776 } else if (mIsWallpaper) {
7777 mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007778 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08007779 if (DEBUG_LAYERS) Slog.v(TAG, "Stepping win " + this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007780 + " anim layer: " + mAnimLayer);
7781 mHasTransformation = false;
7782 mHasLocalTransformation = false;
Dianne Hackbornb601ce12010-03-01 23:36:02 -08007783 if (mPolicyVisibility != mPolicyVisibilityAfterAnim) {
7784 if (DEBUG_VISIBILITY) {
7785 Slog.v(TAG, "Policy visibility changing after anim in " + this + ": "
7786 + mPolicyVisibilityAfterAnim);
7787 }
7788 mPolicyVisibility = mPolicyVisibilityAfterAnim;
7789 if (!mPolicyVisibility) {
7790 if (mCurrentFocus == this) {
7791 mFocusMayChange = true;
7792 }
7793 // Window is no longer visible -- make sure if we were waiting
7794 // for it to be displayed before enabling the display, that
7795 // we allow the display to be enabled now.
7796 enableScreenIfNeededLocked();
7797 }
Dianne Hackbornf3bea9c2009-12-09 18:26:21 -08007798 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007799 mTransformation.clear();
7800 if (mHasDrawn
7801 && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
7802 && mAppToken != null
7803 && mAppToken.firstWindowDrawn
7804 && mAppToken.startingData != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007805 if (DEBUG_STARTING_WINDOW) Slog.v(TAG, "Finish starting "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007806 + mToken + ": first real window done animating");
7807 mFinishedStarting.add(mAppToken);
7808 mH.sendEmptyMessage(H.FINISHED_STARTING);
7809 }
Romain Guy06882f82009-06-10 13:36:04 -07007810
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007811 finishExit();
7812
7813 if (mAppToken != null) {
7814 mAppToken.updateReportedVisibilityLocked();
7815 }
7816
7817 return false;
7818 }
7819
7820 void finishExit() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007821 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007822 TAG, "finishExit in " + this
7823 + ": exiting=" + mExiting
7824 + " remove=" + mRemoveOnExit
7825 + " windowAnimating=" + isWindowAnimating());
Romain Guy06882f82009-06-10 13:36:04 -07007826
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007827 final int N = mChildWindows.size();
7828 for (int i=0; i<N; i++) {
7829 ((WindowState)mChildWindows.get(i)).finishExit();
7830 }
Romain Guy06882f82009-06-10 13:36:04 -07007831
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007832 if (!mExiting) {
7833 return;
7834 }
Romain Guy06882f82009-06-10 13:36:04 -07007835
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007836 if (isWindowAnimating()) {
7837 return;
7838 }
7839
Joe Onorato8a9b2202010-02-26 18:56:32 -08007840 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007841 TAG, "Exit animation finished in " + this
7842 + ": remove=" + mRemoveOnExit);
7843 if (mSurface != null) {
7844 mDestroySurface.add(this);
7845 mDestroying = true;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007846 if (SHOW_TRANSACTIONS) logSurface(this, "HIDE (finishExit)", null);
Dianne Hackborn16064f92010-03-25 00:47:24 -07007847 mSurfaceShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007848 try {
7849 mSurface.hide();
7850 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007851 Slog.w(TAG, "Error hiding surface in " + this, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007852 }
7853 mLastHidden = true;
7854 mKeyWaiter.releasePendingPointerLocked(mSession);
7855 }
7856 mExiting = false;
7857 if (mRemoveOnExit) {
7858 mPendingRemove.add(this);
7859 mRemoveOnExit = false;
7860 }
7861 }
Romain Guy06882f82009-06-10 13:36:04 -07007862
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007863 boolean isIdentityMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
7864 if (dsdx < .99999f || dsdx > 1.00001f) return false;
7865 if (dtdy < .99999f || dtdy > 1.00001f) return false;
7866 if (dtdx < -.000001f || dtdx > .000001f) return false;
7867 if (dsdy < -.000001f || dsdy > .000001f) return false;
7868 return true;
7869 }
Romain Guy06882f82009-06-10 13:36:04 -07007870
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007871 void computeShownFrameLocked() {
7872 final boolean selfTransformation = mHasLocalTransformation;
7873 Transformation attachedTransformation =
7874 (mAttachedWindow != null && mAttachedWindow.mHasLocalTransformation)
7875 ? mAttachedWindow.mTransformation : null;
7876 Transformation appTransformation =
7877 (mAppToken != null && mAppToken.hasTransformation)
7878 ? mAppToken.transformation : null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007879
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007880 // Wallpapers are animated based on the "real" window they
7881 // are currently targeting.
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007882 if (mAttrs.type == TYPE_WALLPAPER && mLowerWallpaperTarget == null
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07007883 && mWallpaperTarget != null) {
Dianne Hackborn5baba162009-09-23 17:01:12 -07007884 if (mWallpaperTarget.mHasLocalTransformation &&
7885 mWallpaperTarget.mAnimation != null &&
7886 !mWallpaperTarget.mAnimation.getDetachWallpaper()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007887 attachedTransformation = mWallpaperTarget.mTransformation;
Dianne Hackborn5baba162009-09-23 17:01:12 -07007888 if (DEBUG_WALLPAPER && attachedTransformation != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007889 Slog.v(TAG, "WP target attached xform: " + attachedTransformation);
Dianne Hackborn5baba162009-09-23 17:01:12 -07007890 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007891 }
7892 if (mWallpaperTarget.mAppToken != null &&
Dianne Hackborn5baba162009-09-23 17:01:12 -07007893 mWallpaperTarget.mAppToken.hasTransformation &&
7894 mWallpaperTarget.mAppToken.animation != null &&
7895 !mWallpaperTarget.mAppToken.animation.getDetachWallpaper()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007896 appTransformation = mWallpaperTarget.mAppToken.transformation;
Dianne Hackborn5baba162009-09-23 17:01:12 -07007897 if (DEBUG_WALLPAPER && appTransformation != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007898 Slog.v(TAG, "WP target app xform: " + appTransformation);
Dianne Hackborn5baba162009-09-23 17:01:12 -07007899 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007900 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007901 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007903 if (selfTransformation || attachedTransformation != null
7904 || appTransformation != null) {
Romain Guy06882f82009-06-10 13:36:04 -07007905 // cache often used attributes locally
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007906 final Rect frame = mFrame;
7907 final float tmpFloats[] = mTmpFloats;
7908 final Matrix tmpMatrix = mTmpMatrix;
7909
7910 // Compute the desired transformation.
Dianne Hackborn65c23872009-09-18 17:47:02 -07007911 tmpMatrix.setTranslate(0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007912 if (selfTransformation) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007913 tmpMatrix.postConcat(mTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007914 }
Dianne Hackborn65c23872009-09-18 17:47:02 -07007915 tmpMatrix.postTranslate(frame.left, frame.top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007916 if (attachedTransformation != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007917 tmpMatrix.postConcat(attachedTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007918 }
7919 if (appTransformation != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007920 tmpMatrix.postConcat(appTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007921 }
7922
7923 // "convert" it into SurfaceFlinger's format
7924 // (a 2x2 matrix + an offset)
7925 // Here we must not transform the position of the surface
7926 // since it is already included in the transformation.
Joe Onorato8a9b2202010-02-26 18:56:32 -08007927 //Slog.i(TAG, "Transform: " + matrix);
Romain Guy06882f82009-06-10 13:36:04 -07007928
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007929 tmpMatrix.getValues(tmpFloats);
7930 mDsDx = tmpFloats[Matrix.MSCALE_X];
7931 mDtDx = tmpFloats[Matrix.MSKEW_X];
7932 mDsDy = tmpFloats[Matrix.MSKEW_Y];
7933 mDtDy = tmpFloats[Matrix.MSCALE_Y];
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007934 int x = (int)tmpFloats[Matrix.MTRANS_X] + mXOffset;
7935 int y = (int)tmpFloats[Matrix.MTRANS_Y] + mYOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007936 int w = frame.width();
7937 int h = frame.height();
7938 mShownFrame.set(x, y, x+w, y+h);
7939
7940 // Now set the alpha... but because our current hardware
7941 // can't do alpha transformation on a non-opaque surface,
7942 // turn it off if we are running an animation that is also
7943 // transforming since it is more important to have that
7944 // animation be smooth.
7945 mShownAlpha = mAlpha;
7946 if (!mLimitedAlphaCompositing
7947 || (!PixelFormat.formatHasAlpha(mAttrs.format)
7948 || (isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
7949 && x == frame.left && y == frame.top))) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007950 //Slog.i(TAG, "Applying alpha transform");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007951 if (selfTransformation) {
7952 mShownAlpha *= mTransformation.getAlpha();
7953 }
7954 if (attachedTransformation != null) {
7955 mShownAlpha *= attachedTransformation.getAlpha();
7956 }
7957 if (appTransformation != null) {
7958 mShownAlpha *= appTransformation.getAlpha();
7959 }
7960 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007961 //Slog.i(TAG, "Not applying alpha transform");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007962 }
Romain Guy06882f82009-06-10 13:36:04 -07007963
Joe Onorato8a9b2202010-02-26 18:56:32 -08007964 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007965 TAG, "Continuing animation in " + this +
7966 ": " + mShownFrame +
7967 ", alpha=" + mTransformation.getAlpha());
7968 return;
7969 }
Romain Guy06882f82009-06-10 13:36:04 -07007970
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007971 mShownFrame.set(mFrame);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007972 if (mXOffset != 0 || mYOffset != 0) {
7973 mShownFrame.offset(mXOffset, mYOffset);
7974 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007975 mShownAlpha = mAlpha;
7976 mDsDx = 1;
7977 mDtDx = 0;
7978 mDsDy = 0;
7979 mDtDy = 1;
7980 }
Romain Guy06882f82009-06-10 13:36:04 -07007981
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007982 /**
7983 * Is this window visible? It is not visible if there is no
7984 * surface, or we are in the process of running an exit animation
7985 * that will remove the surface, or its app token has been hidden.
7986 */
7987 public boolean isVisibleLw() {
7988 final AppWindowToken atoken = mAppToken;
7989 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7990 && (atoken == null || !atoken.hiddenRequested)
7991 && !mExiting && !mDestroying;
7992 }
7993
7994 /**
Dianne Hackborn3d163f072009-10-07 21:26:57 -07007995 * Like {@link #isVisibleLw}, but also counts a window that is currently
7996 * "hidden" behind the keyguard as visible. This allows us to apply
7997 * things like window flags that impact the keyguard.
7998 * XXX I am starting to think we need to have ANOTHER visibility flag
7999 * for this "hidden behind keyguard" state rather than overloading
8000 * mPolicyVisibility. Ungh.
8001 */
8002 public boolean isVisibleOrBehindKeyguardLw() {
8003 final AppWindowToken atoken = mAppToken;
8004 return mSurface != null && !mAttachedHidden
8005 && (atoken == null ? mPolicyVisibility : !atoken.hiddenRequested)
Dianne Hackborn5943c202010-04-12 21:36:49 -07008006 && (mOrientationChanging || (!mDrawPending && !mCommitDrawPending))
Dianne Hackborn3d163f072009-10-07 21:26:57 -07008007 && !mExiting && !mDestroying;
8008 }
8009
8010 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008011 * Is this window visible, ignoring its app token? It is not visible
8012 * if there is no surface, or we are in the process of running an exit animation
8013 * that will remove the surface.
8014 */
8015 public boolean isWinVisibleLw() {
8016 final AppWindowToken atoken = mAppToken;
8017 return mSurface != null && mPolicyVisibility && !mAttachedHidden
8018 && (atoken == null || !atoken.hiddenRequested || atoken.animating)
8019 && !mExiting && !mDestroying;
8020 }
8021
8022 /**
8023 * The same as isVisible(), but follows the current hidden state of
8024 * the associated app token, not the pending requested hidden state.
8025 */
8026 boolean isVisibleNow() {
8027 return mSurface != null && mPolicyVisibility && !mAttachedHidden
The Android Open Source Project10592532009-03-18 17:39:46 -07008028 && !mRootToken.hidden && !mExiting && !mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008029 }
8030
8031 /**
8032 * Same as isVisible(), but we also count it as visible between the
8033 * call to IWindowSession.add() and the first relayout().
8034 */
8035 boolean isVisibleOrAdding() {
8036 final AppWindowToken atoken = mAppToken;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008037 return ((mSurface != null && !mReportDestroySurface)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008038 || (!mRelayoutCalled && mViewVisibility == View.VISIBLE))
8039 && mPolicyVisibility && !mAttachedHidden
8040 && (atoken == null || !atoken.hiddenRequested)
8041 && !mExiting && !mDestroying;
8042 }
8043
8044 /**
8045 * Is this window currently on-screen? It is on-screen either if it
8046 * is visible or it is currently running an animation before no longer
8047 * being visible.
8048 */
8049 boolean isOnScreen() {
8050 final AppWindowToken atoken = mAppToken;
8051 if (atoken != null) {
8052 return mSurface != null && mPolicyVisibility && !mDestroying
8053 && ((!mAttachedHidden && !atoken.hiddenRequested)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07008054 || mAnimation != null || atoken.animation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008055 } else {
8056 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn0cd48872009-08-13 18:51:59 -07008057 && (!mAttachedHidden || mAnimation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008058 }
8059 }
Romain Guy06882f82009-06-10 13:36:04 -07008060
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008061 /**
8062 * Like isOnScreen(), but we don't return true if the window is part
8063 * of a transition that has not yet been started.
8064 */
8065 boolean isReadyForDisplay() {
Dianne Hackborna8f60182009-09-01 19:01:50 -07008066 if (mRootToken.waitingToShow &&
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07008067 mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07008068 return false;
8069 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008070 final AppWindowToken atoken = mAppToken;
Dianne Hackborn0cd48872009-08-13 18:51:59 -07008071 final boolean animating = atoken != null
8072 ? (atoken.animation != null) : false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008073 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07008074 && ((!mAttachedHidden && mViewVisibility == View.VISIBLE
8075 && !mRootToken.hidden)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07008076 || mAnimation != null || animating);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008077 }
8078
8079 /** Is the window or its container currently animating? */
8080 boolean isAnimating() {
8081 final WindowState attached = mAttachedWindow;
8082 final AppWindowToken atoken = mAppToken;
8083 return mAnimation != null
8084 || (attached != null && attached.mAnimation != null)
Romain Guy06882f82009-06-10 13:36:04 -07008085 || (atoken != null &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008086 (atoken.animation != null
8087 || atoken.inPendingTransaction));
8088 }
8089
8090 /** Is this window currently animating? */
8091 boolean isWindowAnimating() {
8092 return mAnimation != null;
8093 }
8094
8095 /**
8096 * Like isOnScreen, but returns false if the surface hasn't yet
8097 * been drawn.
8098 */
8099 public boolean isDisplayedLw() {
8100 final AppWindowToken atoken = mAppToken;
8101 return mSurface != null && mPolicyVisibility && !mDestroying
8102 && !mDrawPending && !mCommitDrawPending
8103 && ((!mAttachedHidden &&
8104 (atoken == null || !atoken.hiddenRequested))
8105 || mAnimating);
8106 }
8107
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008108 /**
8109 * Returns true if the window has a surface that it has drawn a
Dianne Hackborn5943c202010-04-12 21:36:49 -07008110 * complete UI in to. Note that this returns true if the orientation
8111 * is changing even if the window hasn't redrawn because we don't want
8112 * to stop things from executing during that time.
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008113 */
8114 public boolean isDrawnLw() {
8115 final AppWindowToken atoken = mAppToken;
8116 return mSurface != null && !mDestroying
Dianne Hackborn5943c202010-04-12 21:36:49 -07008117 && (mOrientationChanging || (!mDrawPending && !mCommitDrawPending));
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008118 }
8119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008120 public boolean fillsScreenLw(int screenWidth, int screenHeight,
8121 boolean shownFrame, boolean onlyOpaque) {
8122 if (mSurface == null) {
8123 return false;
8124 }
8125 if (mAppToken != null && !mAppToken.appFullscreen) {
8126 return false;
8127 }
8128 if (onlyOpaque && mAttrs.format != PixelFormat.OPAQUE) {
8129 return false;
8130 }
8131 final Rect frame = shownFrame ? mShownFrame : mFrame;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07008132
8133 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
8134 return frame.left <= mCompatibleScreenFrame.left &&
8135 frame.top <= mCompatibleScreenFrame.top &&
8136 frame.right >= mCompatibleScreenFrame.right &&
8137 frame.bottom >= mCompatibleScreenFrame.bottom;
8138 } else {
8139 return frame.left <= 0 && frame.top <= 0
8140 && frame.right >= screenWidth
8141 && frame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008142 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008143 }
Romain Guy06882f82009-06-10 13:36:04 -07008144
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008145 /**
Dianne Hackborn25994b42009-09-04 14:21:19 -07008146 * Return true if the window is opaque and fully drawn. This indicates
8147 * it may obscure windows behind it.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008148 */
8149 boolean isOpaqueDrawn() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07008150 return (mAttrs.format == PixelFormat.OPAQUE
8151 || mAttrs.type == TYPE_WALLPAPER)
8152 && mSurface != null && mAnimation == null
8153 && (mAppToken == null || mAppToken.animation == null)
8154 && !mDrawPending && !mCommitDrawPending;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008155 }
8156
8157 boolean needsBackgroundFiller(int screenWidth, int screenHeight) {
8158 return
8159 // only if the application is requesting compatible window
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008160 (mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0 &&
8161 // only if it's visible
8162 mHasDrawn && mViewVisibility == View.VISIBLE &&
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07008163 // and only if the application fills the compatible screen
8164 mFrame.left <= mCompatibleScreenFrame.left &&
8165 mFrame.top <= mCompatibleScreenFrame.top &&
8166 mFrame.right >= mCompatibleScreenFrame.right &&
8167 mFrame.bottom >= mCompatibleScreenFrame.bottom &&
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008168 // and starting window do not need background filler
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07008169 mAttrs.type != mAttrs.TYPE_APPLICATION_STARTING;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008170 }
8171
8172 boolean isFullscreen(int screenWidth, int screenHeight) {
8173 return mFrame.left <= 0 && mFrame.top <= 0 &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07008174 mFrame.right >= screenWidth && mFrame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008175 }
8176
8177 void removeLocked() {
8178 if (mAttachedWindow != null) {
8179 mAttachedWindow.mChildWindows.remove(this);
8180 }
8181 destroySurfaceLocked();
8182 mSession.windowRemovedLocked();
8183 try {
8184 mClient.asBinder().unlinkToDeath(mDeathRecipient, 0);
8185 } catch (RuntimeException e) {
8186 // Ignore if it has already been removed (usually because
8187 // we are doing this as part of processing a death note.)
8188 }
8189 }
8190
8191 private class DeathRecipient implements IBinder.DeathRecipient {
8192 public void binderDied() {
8193 try {
8194 synchronized(mWindowMap) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08008195 WindowState win = windowForClientLocked(mSession, mClient, false);
Joe Onorato8a9b2202010-02-26 18:56:32 -08008196 Slog.i(TAG, "WIN DEATH: " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008197 if (win != null) {
8198 removeWindowLocked(mSession, win);
8199 }
8200 }
8201 } catch (IllegalArgumentException ex) {
8202 // This will happen if the window has already been
8203 // removed.
8204 }
8205 }
8206 }
8207
8208 /** Returns true if this window desires key events. */
8209 public final boolean canReceiveKeys() {
8210 return isVisibleOrAdding()
8211 && (mViewVisibility == View.VISIBLE)
8212 && ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0);
8213 }
8214
8215 public boolean hasDrawnLw() {
8216 return mHasDrawn;
8217 }
8218
8219 public boolean showLw(boolean doAnimation) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008220 return showLw(doAnimation, true);
8221 }
8222
8223 boolean showLw(boolean doAnimation, boolean requestAnim) {
8224 if (mPolicyVisibility && mPolicyVisibilityAfterAnim) {
8225 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008226 }
Dianne Hackbornb601ce12010-03-01 23:36:02 -08008227 if (DEBUG_VISIBILITY) Slog.v(TAG, "Policy visibility true: " + this);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08008228 if (doAnimation) {
8229 if (DEBUG_VISIBILITY) Slog.v(TAG, "doAnimation: mPolicyVisibility="
8230 + mPolicyVisibility + " mAnimation=" + mAnimation);
8231 if (mDisplayFrozen || !mPolicy.isScreenOn()) {
8232 doAnimation = false;
8233 } else if (mPolicyVisibility && mAnimation == null) {
8234 // Check for the case where we are currently visible and
8235 // not animating; we do not want to do animation at such a
8236 // point to become visible when we already are.
8237 doAnimation = false;
8238 }
8239 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008240 mPolicyVisibility = true;
8241 mPolicyVisibilityAfterAnim = true;
8242 if (doAnimation) {
8243 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_ENTER, true);
8244 }
8245 if (requestAnim) {
8246 requestAnimationLocked(0);
8247 }
8248 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008249 }
8250
8251 public boolean hideLw(boolean doAnimation) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008252 return hideLw(doAnimation, true);
8253 }
8254
8255 boolean hideLw(boolean doAnimation, boolean requestAnim) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08008256 if (doAnimation) {
8257 if (mDisplayFrozen || !mPolicy.isScreenOn()) {
8258 doAnimation = false;
8259 }
8260 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008261 boolean current = doAnimation ? mPolicyVisibilityAfterAnim
8262 : mPolicyVisibility;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008263 if (!current) {
8264 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008265 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008266 if (doAnimation) {
8267 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_EXIT, false);
8268 if (mAnimation == null) {
8269 doAnimation = false;
8270 }
8271 }
8272 if (doAnimation) {
8273 mPolicyVisibilityAfterAnim = false;
8274 } else {
Dianne Hackbornb601ce12010-03-01 23:36:02 -08008275 if (DEBUG_VISIBILITY) Slog.v(TAG, "Policy visibility false: " + this);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008276 mPolicyVisibilityAfterAnim = false;
8277 mPolicyVisibility = false;
Dianne Hackbornf3bea9c2009-12-09 18:26:21 -08008278 // Window is no longer visible -- make sure if we were waiting
8279 // for it to be displayed before enabling the display, that
8280 // we allow the display to be enabled now.
8281 enableScreenIfNeededLocked();
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08008282 if (mCurrentFocus == this) {
8283 mFocusMayChange = true;
8284 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008285 }
8286 if (requestAnim) {
8287 requestAnimationLocked(0);
8288 }
8289 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008290 }
8291
8292 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008293 pw.print(prefix); pw.print("mSession="); pw.print(mSession);
8294 pw.print(" mClient="); pw.println(mClient.asBinder());
8295 pw.print(prefix); pw.print("mAttrs="); pw.println(mAttrs);
8296 if (mAttachedWindow != null || mLayoutAttached) {
8297 pw.print(prefix); pw.print("mAttachedWindow="); pw.print(mAttachedWindow);
8298 pw.print(" mLayoutAttached="); pw.println(mLayoutAttached);
8299 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07008300 if (mIsImWindow || mIsWallpaper || mIsFloatingLayer) {
8301 pw.print(prefix); pw.print("mIsImWindow="); pw.print(mIsImWindow);
8302 pw.print(" mIsWallpaper="); pw.print(mIsWallpaper);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008303 pw.print(" mIsFloatingLayer="); pw.print(mIsFloatingLayer);
8304 pw.print(" mWallpaperVisible="); pw.println(mWallpaperVisible);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008305 }
8306 pw.print(prefix); pw.print("mBaseLayer="); pw.print(mBaseLayer);
8307 pw.print(" mSubLayer="); pw.print(mSubLayer);
8308 pw.print(" mAnimLayer="); pw.print(mLayer); pw.print("+");
8309 pw.print((mTargetAppToken != null ? mTargetAppToken.animLayerAdjustment
8310 : (mAppToken != null ? mAppToken.animLayerAdjustment : 0)));
8311 pw.print("="); pw.print(mAnimLayer);
8312 pw.print(" mLastLayer="); pw.println(mLastLayer);
8313 if (mSurface != null) {
8314 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
Dianne Hackborn16064f92010-03-25 00:47:24 -07008315 pw.print(prefix); pw.print("Surface: shown="); pw.print(mSurfaceShown);
8316 pw.print(" layer="); pw.print(mSurfaceLayer);
8317 pw.print(" alpha="); pw.print(mSurfaceAlpha);
8318 pw.print(" rect=("); pw.print(mSurfaceX);
8319 pw.print(","); pw.print(mSurfaceY);
8320 pw.print(") "); pw.print(mSurfaceW);
8321 pw.print(" x "); pw.println(mSurfaceH);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008322 }
8323 pw.print(prefix); pw.print("mToken="); pw.println(mToken);
8324 pw.print(prefix); pw.print("mRootToken="); pw.println(mRootToken);
8325 if (mAppToken != null) {
8326 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
8327 }
8328 if (mTargetAppToken != null) {
8329 pw.print(prefix); pw.print("mTargetAppToken="); pw.println(mTargetAppToken);
8330 }
8331 pw.print(prefix); pw.print("mViewVisibility=0x");
8332 pw.print(Integer.toHexString(mViewVisibility));
8333 pw.print(" mLastHidden="); pw.print(mLastHidden);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07008334 pw.print(" mHaveFrame="); pw.print(mHaveFrame);
8335 pw.print(" mObscured="); pw.println(mObscured);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008336 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim || mAttachedHidden) {
8337 pw.print(prefix); pw.print("mPolicyVisibility=");
8338 pw.print(mPolicyVisibility);
8339 pw.print(" mPolicyVisibilityAfterAnim=");
8340 pw.print(mPolicyVisibilityAfterAnim);
8341 pw.print(" mAttachedHidden="); pw.println(mAttachedHidden);
8342 }
Dianne Hackborn9b52a212009-12-11 14:51:35 -08008343 if (!mRelayoutCalled) {
8344 pw.print(prefix); pw.print("mRelayoutCalled="); pw.println(mRelayoutCalled);
8345 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008346 pw.print(prefix); pw.print("Requested w="); pw.print(mRequestedWidth);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08008347 pw.print(" h="); pw.print(mRequestedHeight);
8348 pw.print(" mLayoutSeq="); pw.println(mLayoutSeq);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008349 if (mXOffset != 0 || mYOffset != 0) {
8350 pw.print(prefix); pw.print("Offsets x="); pw.print(mXOffset);
8351 pw.print(" y="); pw.println(mYOffset);
8352 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008353 pw.print(prefix); pw.print("mGivenContentInsets=");
8354 mGivenContentInsets.printShortString(pw);
8355 pw.print(" mGivenVisibleInsets=");
8356 mGivenVisibleInsets.printShortString(pw);
8357 pw.println();
8358 if (mTouchableInsets != 0 || mGivenInsetsPending) {
8359 pw.print(prefix); pw.print("mTouchableInsets="); pw.print(mTouchableInsets);
8360 pw.print(" mGivenInsetsPending="); pw.println(mGivenInsetsPending);
8361 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08008362 pw.print(prefix); pw.print("mConfiguration="); pw.println(mConfiguration);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008363 pw.print(prefix); pw.print("mShownFrame=");
8364 mShownFrame.printShortString(pw);
8365 pw.print(" last="); mLastShownFrame.printShortString(pw);
8366 pw.println();
8367 pw.print(prefix); pw.print("mFrame="); mFrame.printShortString(pw);
8368 pw.print(" last="); mLastFrame.printShortString(pw);
8369 pw.println();
8370 pw.print(prefix); pw.print("mContainingFrame=");
8371 mContainingFrame.printShortString(pw);
8372 pw.print(" mDisplayFrame=");
8373 mDisplayFrame.printShortString(pw);
8374 pw.println();
8375 pw.print(prefix); pw.print("mContentFrame="); mContentFrame.printShortString(pw);
8376 pw.print(" mVisibleFrame="); mVisibleFrame.printShortString(pw);
8377 pw.println();
8378 pw.print(prefix); pw.print("mContentInsets="); mContentInsets.printShortString(pw);
8379 pw.print(" last="); mLastContentInsets.printShortString(pw);
8380 pw.print(" mVisibleInsets="); mVisibleInsets.printShortString(pw);
8381 pw.print(" last="); mLastVisibleInsets.printShortString(pw);
8382 pw.println();
8383 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
8384 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
8385 pw.print(" mAlpha="); pw.print(mAlpha);
8386 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
8387 }
8388 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
8389 || mAnimation != null) {
8390 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
8391 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
8392 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
8393 pw.print(" mAnimation="); pw.println(mAnimation);
8394 }
8395 if (mHasTransformation || mHasLocalTransformation) {
8396 pw.print(prefix); pw.print("XForm: has=");
8397 pw.print(mHasTransformation);
8398 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
8399 pw.print(" "); mTransformation.printShortString(pw);
8400 pw.println();
8401 }
8402 pw.print(prefix); pw.print("mDrawPending="); pw.print(mDrawPending);
8403 pw.print(" mCommitDrawPending="); pw.print(mCommitDrawPending);
8404 pw.print(" mReadyToShow="); pw.print(mReadyToShow);
8405 pw.print(" mHasDrawn="); pw.println(mHasDrawn);
8406 if (mExiting || mRemoveOnExit || mDestroying || mRemoved) {
8407 pw.print(prefix); pw.print("mExiting="); pw.print(mExiting);
8408 pw.print(" mRemoveOnExit="); pw.print(mRemoveOnExit);
8409 pw.print(" mDestroying="); pw.print(mDestroying);
8410 pw.print(" mRemoved="); pw.println(mRemoved);
8411 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07008412 if (mOrientationChanging || mAppFreezing || mTurnOnScreen) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008413 pw.print(prefix); pw.print("mOrientationChanging=");
8414 pw.print(mOrientationChanging);
Dianne Hackborn93e462b2009-09-15 22:50:40 -07008415 pw.print(" mAppFreezing="); pw.print(mAppFreezing);
8416 pw.print(" mTurnOnScreen="); pw.println(mTurnOnScreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008417 }
Mitsuru Oshima589cebe2009-07-22 20:38:58 -07008418 if (mHScale != 1 || mVScale != 1) {
8419 pw.print(prefix); pw.print("mHScale="); pw.print(mHScale);
8420 pw.print(" mVScale="); pw.println(mVScale);
8421 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07008422 if (mWallpaperX != -1 || mWallpaperY != -1) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008423 pw.print(prefix); pw.print("mWallpaperX="); pw.print(mWallpaperX);
8424 pw.print(" mWallpaperY="); pw.println(mWallpaperY);
8425 }
Marco Nelissenbf6956b2009-11-09 15:21:13 -08008426 if (mWallpaperXStep != -1 || mWallpaperYStep != -1) {
8427 pw.print(prefix); pw.print("mWallpaperXStep="); pw.print(mWallpaperXStep);
8428 pw.print(" mWallpaperYStep="); pw.println(mWallpaperYStep);
8429 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008430 }
8431
8432 @Override
8433 public String toString() {
8434 return "Window{"
8435 + Integer.toHexString(System.identityHashCode(this))
8436 + " " + mAttrs.getTitle() + " paused=" + mToken.paused + "}";
8437 }
8438 }
Romain Guy06882f82009-06-10 13:36:04 -07008439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008440 // -------------------------------------------------------------
8441 // Window Token State
8442 // -------------------------------------------------------------
8443
8444 class WindowToken {
8445 // The actual token.
8446 final IBinder token;
8447
8448 // The type of window this token is for, as per WindowManager.LayoutParams.
8449 final int windowType;
Romain Guy06882f82009-06-10 13:36:04 -07008450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008451 // Set if this token was explicitly added by a client, so should
8452 // not be removed when all windows are removed.
8453 final boolean explicit;
Romain Guy06882f82009-06-10 13:36:04 -07008454
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008455 // For printing.
8456 String stringName;
Romain Guy06882f82009-06-10 13:36:04 -07008457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008458 // If this is an AppWindowToken, this is non-null.
8459 AppWindowToken appWindowToken;
Romain Guy06882f82009-06-10 13:36:04 -07008460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008461 // All of the windows associated with this token.
8462 final ArrayList<WindowState> windows = new ArrayList<WindowState>();
8463
8464 // Is key dispatching paused for this token?
8465 boolean paused = false;
8466
8467 // Should this token's windows be hidden?
8468 boolean hidden;
8469
8470 // Temporary for finding which tokens no longer have visible windows.
8471 boolean hasVisible;
8472
Dianne Hackborna8f60182009-09-01 19:01:50 -07008473 // Set to true when this token is in a pending transaction where it
8474 // will be shown.
8475 boolean waitingToShow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008476
Dianne Hackborna8f60182009-09-01 19:01:50 -07008477 // Set to true when this token is in a pending transaction where it
8478 // will be hidden.
8479 boolean waitingToHide;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008480
Dianne Hackborna8f60182009-09-01 19:01:50 -07008481 // Set to true when this token is in a pending transaction where its
8482 // windows will be put to the bottom of the list.
8483 boolean sendingToBottom;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008484
Dianne Hackborna8f60182009-09-01 19:01:50 -07008485 // Set to true when this token is in a pending transaction where its
8486 // windows will be put to the top of the list.
8487 boolean sendingToTop;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008488
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008489 WindowToken(IBinder _token, int type, boolean _explicit) {
8490 token = _token;
8491 windowType = type;
8492 explicit = _explicit;
8493 }
8494
8495 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008496 pw.print(prefix); pw.print("token="); pw.println(token);
8497 pw.print(prefix); pw.print("windows="); pw.println(windows);
8498 pw.print(prefix); pw.print("windowType="); pw.print(windowType);
8499 pw.print(" hidden="); pw.print(hidden);
8500 pw.print(" hasVisible="); pw.println(hasVisible);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008501 if (waitingToShow || waitingToHide || sendingToBottom || sendingToTop) {
8502 pw.print(prefix); pw.print("waitingToShow="); pw.print(waitingToShow);
8503 pw.print(" waitingToHide="); pw.print(waitingToHide);
8504 pw.print(" sendingToBottom="); pw.print(sendingToBottom);
8505 pw.print(" sendingToTop="); pw.println(sendingToTop);
8506 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008507 }
8508
8509 @Override
8510 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008511 if (stringName == null) {
8512 StringBuilder sb = new StringBuilder();
8513 sb.append("WindowToken{");
8514 sb.append(Integer.toHexString(System.identityHashCode(this)));
8515 sb.append(" token="); sb.append(token); sb.append('}');
8516 stringName = sb.toString();
8517 }
8518 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008519 }
8520 };
8521
8522 class AppWindowToken extends WindowToken {
8523 // Non-null only for application tokens.
8524 final IApplicationToken appToken;
8525
8526 // All of the windows and child windows that are included in this
8527 // application token. Note this list is NOT sorted!
8528 final ArrayList<WindowState> allAppWindows = new ArrayList<WindowState>();
8529
8530 int groupId = -1;
8531 boolean appFullscreen;
8532 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Romain Guy06882f82009-06-10 13:36:04 -07008533
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008534 // These are used for determining when all windows associated with
8535 // an activity have been drawn, so they can be made visible together
8536 // at the same time.
8537 int lastTransactionSequence = mTransactionSequence-1;
8538 int numInterestingWindows;
8539 int numDrawnWindows;
8540 boolean inPendingTransaction;
8541 boolean allDrawn;
Romain Guy06882f82009-06-10 13:36:04 -07008542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008543 // Is this token going to be hidden in a little while? If so, it
8544 // won't be taken into account for setting the screen orientation.
8545 boolean willBeHidden;
Romain Guy06882f82009-06-10 13:36:04 -07008546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008547 // Is this window's surface needed? This is almost like hidden, except
8548 // it will sometimes be true a little earlier: when the token has
8549 // been shown, but is still waiting for its app transition to execute
8550 // before making its windows shown.
8551 boolean hiddenRequested;
Romain Guy06882f82009-06-10 13:36:04 -07008552
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008553 // Have we told the window clients to hide themselves?
8554 boolean clientHidden;
Romain Guy06882f82009-06-10 13:36:04 -07008555
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008556 // Last visibility state we reported to the app token.
8557 boolean reportedVisible;
8558
8559 // Set to true when the token has been removed from the window mgr.
8560 boolean removed;
8561
8562 // Have we been asked to have this token keep the screen frozen?
8563 boolean freezingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07008564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008565 boolean animating;
8566 Animation animation;
8567 boolean hasTransformation;
8568 final Transformation transformation = new Transformation();
Romain Guy06882f82009-06-10 13:36:04 -07008569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008570 // Offset to the window of all layers in the token, for use by
8571 // AppWindowToken animations.
8572 int animLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -07008573
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008574 // Information about an application starting window if displayed.
8575 StartingData startingData;
8576 WindowState startingWindow;
8577 View startingView;
8578 boolean startingDisplayed;
8579 boolean startingMoved;
8580 boolean firstWindowDrawn;
8581
8582 AppWindowToken(IApplicationToken _token) {
8583 super(_token.asBinder(),
8584 WindowManager.LayoutParams.TYPE_APPLICATION, true);
8585 appWindowToken = this;
8586 appToken = _token;
8587 }
Romain Guy06882f82009-06-10 13:36:04 -07008588
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008589 public void setAnimation(Animation anim) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008590 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008591 TAG, "Setting animation in " + this + ": " + anim);
8592 animation = anim;
8593 animating = false;
8594 anim.restrictDuration(MAX_ANIMATION_DURATION);
8595 anim.scaleCurrentDuration(mTransitionAnimationScale);
8596 int zorder = anim.getZAdjustment();
8597 int adj = 0;
8598 if (zorder == Animation.ZORDER_TOP) {
8599 adj = TYPE_LAYER_OFFSET;
8600 } else if (zorder == Animation.ZORDER_BOTTOM) {
8601 adj = -TYPE_LAYER_OFFSET;
8602 }
Romain Guy06882f82009-06-10 13:36:04 -07008603
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008604 if (animLayerAdjustment != adj) {
8605 animLayerAdjustment = adj;
8606 updateLayers();
8607 }
8608 }
Romain Guy06882f82009-06-10 13:36:04 -07008609
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008610 public void setDummyAnimation() {
8611 if (animation == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008612 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008613 TAG, "Setting dummy animation in " + this);
8614 animation = sDummyAnimation;
8615 }
8616 }
8617
8618 public void clearAnimation() {
8619 if (animation != null) {
8620 animation = null;
8621 animating = true;
8622 }
8623 }
Romain Guy06882f82009-06-10 13:36:04 -07008624
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008625 void updateLayers() {
8626 final int N = allAppWindows.size();
8627 final int adj = animLayerAdjustment;
8628 for (int i=0; i<N; i++) {
8629 WindowState w = allAppWindows.get(i);
8630 w.mAnimLayer = w.mLayer + adj;
Joe Onorato8a9b2202010-02-26 18:56:32 -08008631 if (DEBUG_LAYERS) Slog.v(TAG, "Updating layer " + w + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008632 + w.mAnimLayer);
8633 if (w == mInputMethodTarget) {
8634 setInputMethodAnimLayerAdjustment(adj);
8635 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008636 if (w == mWallpaperTarget && mLowerWallpaperTarget == null) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008637 setWallpaperAnimLayerAdjustmentLocked(adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008638 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008639 }
8640 }
Romain Guy06882f82009-06-10 13:36:04 -07008641
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008642 void sendAppVisibilityToClients() {
8643 final int N = allAppWindows.size();
8644 for (int i=0; i<N; i++) {
8645 WindowState win = allAppWindows.get(i);
8646 if (win == startingWindow && clientHidden) {
8647 // Don't hide the starting window.
8648 continue;
8649 }
8650 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008651 if (DEBUG_VISIBILITY) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008652 "Setting visibility of " + win + ": " + (!clientHidden));
8653 win.mClient.dispatchAppVisibility(!clientHidden);
8654 } catch (RemoteException e) {
8655 }
8656 }
8657 }
Romain Guy06882f82009-06-10 13:36:04 -07008658
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008659 void showAllWindowsLocked() {
8660 final int NW = allAppWindows.size();
8661 for (int i=0; i<NW; i++) {
8662 WindowState w = allAppWindows.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08008663 if (DEBUG_VISIBILITY) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008664 "performing show on: " + w);
8665 w.performShowLocked();
8666 }
8667 }
Romain Guy06882f82009-06-10 13:36:04 -07008668
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008669 // This must be called while inside a transaction.
8670 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
Dianne Hackbornde2606d2009-12-18 16:53:55 -08008671 if (!mDisplayFrozen && mPolicy.isScreenOn()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008672 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07008673
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008674 if (animation == sDummyAnimation) {
8675 // This guy is going to animate, but not yet. For now count
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008676 // it as not animating for purposes of scheduling transactions;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008677 // when it is really time to animate, this will be set to
8678 // a real animation and the next call will execute normally.
8679 return false;
8680 }
Romain Guy06882f82009-06-10 13:36:04 -07008681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008682 if ((allDrawn || animating || startingDisplayed) && animation != null) {
8683 if (!animating) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008684 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008685 TAG, "Starting animation in " + this +
8686 " @ " + currentTime + ": dw=" + dw + " dh=" + dh
8687 + " scale=" + mTransitionAnimationScale
8688 + " allDrawn=" + allDrawn + " animating=" + animating);
8689 animation.initialize(dw, dh, dw, dh);
8690 animation.setStartTime(currentTime);
8691 animating = true;
8692 }
8693 transformation.clear();
8694 final boolean more = animation.getTransformation(
8695 currentTime, transformation);
Joe Onorato8a9b2202010-02-26 18:56:32 -08008696 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008697 TAG, "Stepped animation in " + this +
8698 ": more=" + more + ", xform=" + transformation);
8699 if (more) {
8700 // we're done!
8701 hasTransformation = true;
8702 return true;
8703 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08008704 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008705 TAG, "Finished animation in " + this +
8706 " @ " + currentTime);
8707 animation = null;
8708 }
8709 } else if (animation != null) {
8710 // If the display is frozen, and there is a pending animation,
8711 // clear it and make sure we run the cleanup code.
8712 animating = true;
8713 animation = null;
8714 }
8715
8716 hasTransformation = false;
Romain Guy06882f82009-06-10 13:36:04 -07008717
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008718 if (!animating) {
8719 return false;
8720 }
8721
8722 clearAnimation();
8723 animating = false;
8724 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == this) {
8725 moveInputMethodWindowsIfNeededLocked(true);
8726 }
Romain Guy06882f82009-06-10 13:36:04 -07008727
Joe Onorato8a9b2202010-02-26 18:56:32 -08008728 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008729 TAG, "Animation done in " + this
8730 + ": reportedVisible=" + reportedVisible);
8731
8732 transformation.clear();
8733 if (animLayerAdjustment != 0) {
8734 animLayerAdjustment = 0;
8735 updateLayers();
8736 }
Romain Guy06882f82009-06-10 13:36:04 -07008737
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008738 final int N = windows.size();
8739 for (int i=0; i<N; i++) {
8740 ((WindowState)windows.get(i)).finishExit();
8741 }
8742 updateReportedVisibilityLocked();
Romain Guy06882f82009-06-10 13:36:04 -07008743
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008744 return false;
8745 }
8746
8747 void updateReportedVisibilityLocked() {
8748 if (appToken == null) {
8749 return;
8750 }
Romain Guy06882f82009-06-10 13:36:04 -07008751
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008752 int numInteresting = 0;
8753 int numVisible = 0;
8754 boolean nowGone = true;
Romain Guy06882f82009-06-10 13:36:04 -07008755
Joe Onorato8a9b2202010-02-26 18:56:32 -08008756 if (DEBUG_VISIBILITY) Slog.v(TAG, "Update reported visibility: " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008757 final int N = allAppWindows.size();
8758 for (int i=0; i<N; i++) {
8759 WindowState win = allAppWindows.get(i);
Dianne Hackborn6cf67fa2009-12-21 16:46:34 -08008760 if (win == startingWindow || win.mAppFreezing
The Android Open Source Project727cec02010-04-08 11:35:37 -07008761 || win.mViewVisibility != View.VISIBLE
8762 || win.mAttrs.type == TYPE_APPLICATION_STARTING) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008763 continue;
8764 }
8765 if (DEBUG_VISIBILITY) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008766 Slog.v(TAG, "Win " + win + ": isDrawn="
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008767 + win.isDrawnLw()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008768 + ", isAnimating=" + win.isAnimating());
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008769 if (!win.isDrawnLw()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008770 Slog.v(TAG, "Not displayed: s=" + win.mSurface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008771 + " pv=" + win.mPolicyVisibility
8772 + " dp=" + win.mDrawPending
8773 + " cdp=" + win.mCommitDrawPending
8774 + " ah=" + win.mAttachedHidden
8775 + " th="
8776 + (win.mAppToken != null
8777 ? win.mAppToken.hiddenRequested : false)
8778 + " a=" + win.mAnimating);
8779 }
8780 }
8781 numInteresting++;
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008782 if (win.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008783 if (!win.isAnimating()) {
8784 numVisible++;
8785 }
8786 nowGone = false;
8787 } else if (win.isAnimating()) {
8788 nowGone = false;
8789 }
8790 }
Romain Guy06882f82009-06-10 13:36:04 -07008791
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008792 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
Joe Onorato8a9b2202010-02-26 18:56:32 -08008793 if (DEBUG_VISIBILITY) Slog.v(TAG, "VIS " + this + ": interesting="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008794 + numInteresting + " visible=" + numVisible);
8795 if (nowVisible != reportedVisible) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008796 if (DEBUG_VISIBILITY) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008797 TAG, "Visibility changed in " + this
8798 + ": vis=" + nowVisible);
8799 reportedVisible = nowVisible;
8800 Message m = mH.obtainMessage(
8801 H.REPORT_APPLICATION_TOKEN_WINDOWS,
8802 nowVisible ? 1 : 0,
8803 nowGone ? 1 : 0,
8804 this);
8805 mH.sendMessage(m);
8806 }
8807 }
Romain Guy06882f82009-06-10 13:36:04 -07008808
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07008809 WindowState findMainWindow() {
8810 int j = windows.size();
8811 while (j > 0) {
8812 j--;
8813 WindowState win = windows.get(j);
8814 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
8815 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
8816 return win;
8817 }
8818 }
8819 return null;
8820 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008822 void dump(PrintWriter pw, String prefix) {
8823 super.dump(pw, prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008824 if (appToken != null) {
8825 pw.print(prefix); pw.println("app=true");
8826 }
8827 if (allAppWindows.size() > 0) {
8828 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
8829 }
8830 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008831 pw.print(" appFullscreen="); pw.print(appFullscreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008832 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
8833 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
8834 pw.print(" clientHidden="); pw.print(clientHidden);
8835 pw.print(" willBeHidden="); pw.print(willBeHidden);
8836 pw.print(" reportedVisible="); pw.println(reportedVisible);
8837 if (paused || freezingScreen) {
8838 pw.print(prefix); pw.print("paused="); pw.print(paused);
8839 pw.print(" freezingScreen="); pw.println(freezingScreen);
8840 }
8841 if (numInterestingWindows != 0 || numDrawnWindows != 0
8842 || inPendingTransaction || allDrawn) {
8843 pw.print(prefix); pw.print("numInterestingWindows=");
8844 pw.print(numInterestingWindows);
8845 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
8846 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
8847 pw.print(" allDrawn="); pw.println(allDrawn);
8848 }
8849 if (animating || animation != null) {
8850 pw.print(prefix); pw.print("animating="); pw.print(animating);
8851 pw.print(" animation="); pw.println(animation);
8852 }
8853 if (animLayerAdjustment != 0) {
8854 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
8855 }
8856 if (hasTransformation) {
8857 pw.print(prefix); pw.print("hasTransformation="); pw.print(hasTransformation);
8858 pw.print(" transformation="); transformation.printShortString(pw);
8859 pw.println();
8860 }
8861 if (startingData != null || removed || firstWindowDrawn) {
8862 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
8863 pw.print(" removed="); pw.print(removed);
8864 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
8865 }
8866 if (startingWindow != null || startingView != null
8867 || startingDisplayed || startingMoved) {
8868 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
8869 pw.print(" startingView="); pw.print(startingView);
8870 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
8871 pw.print(" startingMoved"); pw.println(startingMoved);
8872 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008873 }
8874
8875 @Override
8876 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008877 if (stringName == null) {
8878 StringBuilder sb = new StringBuilder();
8879 sb.append("AppWindowToken{");
8880 sb.append(Integer.toHexString(System.identityHashCode(this)));
8881 sb.append(" token="); sb.append(token); sb.append('}');
8882 stringName = sb.toString();
8883 }
8884 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008885 }
8886 }
Romain Guy06882f82009-06-10 13:36:04 -07008887
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008888 // -------------------------------------------------------------
8889 // DummyAnimation
8890 // -------------------------------------------------------------
8891
8892 // This is an animation that does nothing: it just immediately finishes
8893 // itself every time it is called. It is used as a stub animation in cases
8894 // where we want to synchronize multiple things that may be animating.
8895 static final class DummyAnimation extends Animation {
8896 public boolean getTransformation(long currentTime, Transformation outTransformation) {
8897 return false;
8898 }
8899 }
8900 static final Animation sDummyAnimation = new DummyAnimation();
Romain Guy06882f82009-06-10 13:36:04 -07008901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008902 // -------------------------------------------------------------
8903 // Async Handler
8904 // -------------------------------------------------------------
8905
8906 static final class StartingData {
8907 final String pkg;
8908 final int theme;
8909 final CharSequence nonLocalizedLabel;
8910 final int labelRes;
8911 final int icon;
Romain Guy06882f82009-06-10 13:36:04 -07008912
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008913 StartingData(String _pkg, int _theme, CharSequence _nonLocalizedLabel,
8914 int _labelRes, int _icon) {
8915 pkg = _pkg;
8916 theme = _theme;
8917 nonLocalizedLabel = _nonLocalizedLabel;
8918 labelRes = _labelRes;
8919 icon = _icon;
8920 }
8921 }
8922
8923 private final class H extends Handler {
8924 public static final int REPORT_FOCUS_CHANGE = 2;
8925 public static final int REPORT_LOSING_FOCUS = 3;
8926 public static final int ANIMATE = 4;
8927 public static final int ADD_STARTING = 5;
8928 public static final int REMOVE_STARTING = 6;
8929 public static final int FINISHED_STARTING = 7;
8930 public static final int REPORT_APPLICATION_TOKEN_WINDOWS = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008931 public static final int WINDOW_FREEZE_TIMEOUT = 11;
8932 public static final int HOLD_SCREEN_CHANGED = 12;
8933 public static final int APP_TRANSITION_TIMEOUT = 13;
8934 public static final int PERSIST_ANIMATION_SCALE = 14;
8935 public static final int FORCE_GC = 15;
8936 public static final int ENABLE_SCREEN = 16;
8937 public static final int APP_FREEZE_TIMEOUT = 17;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08008938 public static final int SEND_NEW_CONFIGURATION = 18;
Romain Guy06882f82009-06-10 13:36:04 -07008939
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008940 private Session mLastReportedHold;
Romain Guy06882f82009-06-10 13:36:04 -07008941
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008942 public H() {
8943 }
Romain Guy06882f82009-06-10 13:36:04 -07008944
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008945 @Override
8946 public void handleMessage(Message msg) {
8947 switch (msg.what) {
8948 case REPORT_FOCUS_CHANGE: {
8949 WindowState lastFocus;
8950 WindowState newFocus;
Romain Guy06882f82009-06-10 13:36:04 -07008951
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008952 synchronized(mWindowMap) {
8953 lastFocus = mLastFocus;
8954 newFocus = mCurrentFocus;
8955 if (lastFocus == newFocus) {
8956 // Focus is not changing, so nothing to do.
8957 return;
8958 }
8959 mLastFocus = newFocus;
Joe Onorato8a9b2202010-02-26 18:56:32 -08008960 //Slog.i(TAG, "Focus moving from " + lastFocus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008961 // + " to " + newFocus);
8962 if (newFocus != null && lastFocus != null
8963 && !newFocus.isDisplayedLw()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008964 //Slog.i(TAG, "Delaying loss of focus...");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008965 mLosingFocus.add(lastFocus);
8966 lastFocus = null;
8967 }
8968 }
8969
8970 if (lastFocus != newFocus) {
8971 //System.out.println("Changing focus from " + lastFocus
8972 // + " to " + newFocus);
8973 if (newFocus != null) {
8974 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008975 //Slog.i(TAG, "Gaining focus: " + newFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008976 newFocus.mClient.windowFocusChanged(true, mInTouchMode);
8977 } catch (RemoteException e) {
8978 // Ignore if process has died.
8979 }
8980 }
8981
8982 if (lastFocus != null) {
8983 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008984 //Slog.i(TAG, "Losing focus: " + lastFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008985 lastFocus.mClient.windowFocusChanged(false, mInTouchMode);
8986 } catch (RemoteException e) {
8987 // Ignore if process has died.
8988 }
8989 }
8990 }
8991 } break;
8992
8993 case REPORT_LOSING_FOCUS: {
8994 ArrayList<WindowState> losers;
Romain Guy06882f82009-06-10 13:36:04 -07008995
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008996 synchronized(mWindowMap) {
8997 losers = mLosingFocus;
8998 mLosingFocus = new ArrayList<WindowState>();
8999 }
9000
9001 final int N = losers.size();
9002 for (int i=0; i<N; i++) {
9003 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009004 //Slog.i(TAG, "Losing delayed focus: " + losers.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009005 losers.get(i).mClient.windowFocusChanged(false, mInTouchMode);
9006 } catch (RemoteException e) {
9007 // Ignore if process has died.
9008 }
9009 }
9010 } break;
9011
9012 case ANIMATE: {
9013 synchronized(mWindowMap) {
9014 mAnimationPending = false;
9015 performLayoutAndPlaceSurfacesLocked();
9016 }
9017 } break;
9018
9019 case ADD_STARTING: {
9020 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
9021 final StartingData sd = wtoken.startingData;
9022
9023 if (sd == null) {
9024 // Animation has been canceled... do nothing.
9025 return;
9026 }
Romain Guy06882f82009-06-10 13:36:04 -07009027
Joe Onorato8a9b2202010-02-26 18:56:32 -08009028 if (DEBUG_STARTING_WINDOW) Slog.v(TAG, "Add starting "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009029 + wtoken + ": pkg=" + sd.pkg);
Romain Guy06882f82009-06-10 13:36:04 -07009030
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009031 View view = null;
9032 try {
9033 view = mPolicy.addStartingWindow(
9034 wtoken.token, sd.pkg,
9035 sd.theme, sd.nonLocalizedLabel, sd.labelRes,
9036 sd.icon);
9037 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009038 Slog.w(TAG, "Exception when adding starting window", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009039 }
9040
9041 if (view != null) {
9042 boolean abort = false;
9043
9044 synchronized(mWindowMap) {
9045 if (wtoken.removed || wtoken.startingData == null) {
9046 // If the window was successfully added, then
9047 // we need to remove it.
9048 if (wtoken.startingWindow != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009049 if (DEBUG_STARTING_WINDOW) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009050 "Aborted starting " + wtoken
9051 + ": removed=" + wtoken.removed
9052 + " startingData=" + wtoken.startingData);
9053 wtoken.startingWindow = null;
9054 wtoken.startingData = null;
9055 abort = true;
9056 }
9057 } else {
9058 wtoken.startingView = view;
9059 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08009060 if (DEBUG_STARTING_WINDOW && !abort) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009061 "Added starting " + wtoken
9062 + ": startingWindow="
9063 + wtoken.startingWindow + " startingView="
9064 + wtoken.startingView);
9065 }
9066
9067 if (abort) {
9068 try {
9069 mPolicy.removeStartingWindow(wtoken.token, view);
9070 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009071 Slog.w(TAG, "Exception when removing starting window", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009072 }
9073 }
9074 }
9075 } break;
9076
9077 case REMOVE_STARTING: {
9078 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
9079 IBinder token = null;
9080 View view = null;
9081 synchronized (mWindowMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009082 if (DEBUG_STARTING_WINDOW) Slog.v(TAG, "Remove starting "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009083 + wtoken + ": startingWindow="
9084 + wtoken.startingWindow + " startingView="
9085 + wtoken.startingView);
9086 if (wtoken.startingWindow != null) {
9087 view = wtoken.startingView;
9088 token = wtoken.token;
9089 wtoken.startingData = null;
9090 wtoken.startingView = null;
9091 wtoken.startingWindow = null;
9092 }
9093 }
9094 if (view != null) {
9095 try {
9096 mPolicy.removeStartingWindow(token, view);
9097 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009098 Slog.w(TAG, "Exception when removing starting window", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009099 }
9100 }
9101 } break;
9102
9103 case FINISHED_STARTING: {
9104 IBinder token = null;
9105 View view = null;
9106 while (true) {
9107 synchronized (mWindowMap) {
9108 final int N = mFinishedStarting.size();
9109 if (N <= 0) {
9110 break;
9111 }
9112 AppWindowToken wtoken = mFinishedStarting.remove(N-1);
9113
Joe Onorato8a9b2202010-02-26 18:56:32 -08009114 if (DEBUG_STARTING_WINDOW) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009115 "Finished starting " + wtoken
9116 + ": startingWindow=" + wtoken.startingWindow
9117 + " startingView=" + wtoken.startingView);
9118
9119 if (wtoken.startingWindow == null) {
9120 continue;
9121 }
9122
9123 view = wtoken.startingView;
9124 token = wtoken.token;
9125 wtoken.startingData = null;
9126 wtoken.startingView = null;
9127 wtoken.startingWindow = null;
9128 }
9129
9130 try {
9131 mPolicy.removeStartingWindow(token, view);
9132 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009133 Slog.w(TAG, "Exception when removing starting window", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009134 }
9135 }
9136 } break;
9137
9138 case REPORT_APPLICATION_TOKEN_WINDOWS: {
9139 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
9140
9141 boolean nowVisible = msg.arg1 != 0;
9142 boolean nowGone = msg.arg2 != 0;
9143
9144 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009145 if (DEBUG_VISIBILITY) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009146 TAG, "Reporting visible in " + wtoken
9147 + " visible=" + nowVisible
9148 + " gone=" + nowGone);
9149 if (nowVisible) {
9150 wtoken.appToken.windowsVisible();
9151 } else {
9152 wtoken.appToken.windowsGone();
9153 }
9154 } catch (RemoteException ex) {
9155 }
9156 } break;
Romain Guy06882f82009-06-10 13:36:04 -07009157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009158 case WINDOW_FREEZE_TIMEOUT: {
9159 synchronized (mWindowMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009160 Slog.w(TAG, "Window freeze timeout expired.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009161 int i = mWindows.size();
9162 while (i > 0) {
9163 i--;
9164 WindowState w = (WindowState)mWindows.get(i);
9165 if (w.mOrientationChanging) {
9166 w.mOrientationChanging = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009167 Slog.w(TAG, "Force clearing orientation change: " + w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009168 }
9169 }
9170 performLayoutAndPlaceSurfacesLocked();
9171 }
9172 break;
9173 }
Romain Guy06882f82009-06-10 13:36:04 -07009174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009175 case HOLD_SCREEN_CHANGED: {
9176 Session oldHold;
9177 Session newHold;
9178 synchronized (mWindowMap) {
9179 oldHold = mLastReportedHold;
9180 newHold = (Session)msg.obj;
9181 mLastReportedHold = newHold;
9182 }
Romain Guy06882f82009-06-10 13:36:04 -07009183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009184 if (oldHold != newHold) {
9185 try {
9186 if (oldHold != null) {
9187 mBatteryStats.noteStopWakelock(oldHold.mUid,
9188 "window",
9189 BatteryStats.WAKE_TYPE_WINDOW);
9190 }
9191 if (newHold != null) {
9192 mBatteryStats.noteStartWakelock(newHold.mUid,
9193 "window",
9194 BatteryStats.WAKE_TYPE_WINDOW);
9195 }
9196 } catch (RemoteException e) {
9197 }
9198 }
9199 break;
9200 }
Romain Guy06882f82009-06-10 13:36:04 -07009201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009202 case APP_TRANSITION_TIMEOUT: {
9203 synchronized (mWindowMap) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009204 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009205 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009206 "*** APP TRANSITION TIMEOUT");
9207 mAppTransitionReady = true;
9208 mAppTransitionTimeout = true;
9209 performLayoutAndPlaceSurfacesLocked();
9210 }
9211 }
9212 break;
9213 }
Romain Guy06882f82009-06-10 13:36:04 -07009214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009215 case PERSIST_ANIMATION_SCALE: {
9216 Settings.System.putFloat(mContext.getContentResolver(),
9217 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
9218 Settings.System.putFloat(mContext.getContentResolver(),
9219 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
9220 break;
9221 }
Romain Guy06882f82009-06-10 13:36:04 -07009222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009223 case FORCE_GC: {
9224 synchronized(mWindowMap) {
9225 if (mAnimationPending) {
9226 // If we are animating, don't do the gc now but
9227 // delay a bit so we don't interrupt the animation.
9228 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
9229 2000);
9230 return;
9231 }
9232 // If we are currently rotating the display, it will
9233 // schedule a new message when done.
9234 if (mDisplayFrozen) {
9235 return;
9236 }
9237 mFreezeGcPending = 0;
9238 }
9239 Runtime.getRuntime().gc();
9240 break;
9241 }
Romain Guy06882f82009-06-10 13:36:04 -07009242
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009243 case ENABLE_SCREEN: {
9244 performEnableScreen();
9245 break;
9246 }
Romain Guy06882f82009-06-10 13:36:04 -07009247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009248 case APP_FREEZE_TIMEOUT: {
9249 synchronized (mWindowMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009250 Slog.w(TAG, "App freeze timeout expired.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009251 int i = mAppTokens.size();
9252 while (i > 0) {
9253 i--;
9254 AppWindowToken tok = mAppTokens.get(i);
9255 if (tok.freezingScreen) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009256 Slog.w(TAG, "Force clearing freeze: " + tok);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009257 unsetAppFreezingScreenLocked(tok, true, true);
9258 }
9259 }
9260 }
9261 break;
9262 }
Romain Guy06882f82009-06-10 13:36:04 -07009263
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009264 case SEND_NEW_CONFIGURATION: {
9265 removeMessages(SEND_NEW_CONFIGURATION);
9266 sendNewConfiguration();
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07009267 break;
9268 }
Romain Guy06882f82009-06-10 13:36:04 -07009269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009270 }
9271 }
9272 }
9273
9274 // -------------------------------------------------------------
9275 // IWindowManager API
9276 // -------------------------------------------------------------
9277
9278 public IWindowSession openSession(IInputMethodClient client,
9279 IInputContext inputContext) {
9280 if (client == null) throw new IllegalArgumentException("null client");
9281 if (inputContext == null) throw new IllegalArgumentException("null inputContext");
9282 return new Session(client, inputContext);
9283 }
9284
9285 public boolean inputMethodClientHasFocus(IInputMethodClient client) {
9286 synchronized (mWindowMap) {
9287 // The focus for the client is the window immediately below
9288 // where we would place the input method window.
9289 int idx = findDesiredInputMethodWindowIndexLocked(false);
9290 WindowState imFocus;
9291 if (idx > 0) {
9292 imFocus = (WindowState)mWindows.get(idx-1);
9293 if (imFocus != null) {
9294 if (imFocus.mSession.mClient != null &&
9295 imFocus.mSession.mClient.asBinder() == client.asBinder()) {
9296 return true;
9297 }
9298 }
9299 }
9300 }
9301 return false;
9302 }
Romain Guy06882f82009-06-10 13:36:04 -07009303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009304 // -------------------------------------------------------------
9305 // Internals
9306 // -------------------------------------------------------------
9307
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009308 final WindowState windowForClientLocked(Session session, IWindow client,
9309 boolean throwOnError) {
9310 return windowForClientLocked(session, client.asBinder(), throwOnError);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009311 }
Romain Guy06882f82009-06-10 13:36:04 -07009312
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009313 final WindowState windowForClientLocked(Session session, IBinder client,
9314 boolean throwOnError) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009315 WindowState win = mWindowMap.get(client);
Joe Onorato8a9b2202010-02-26 18:56:32 -08009316 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009317 TAG, "Looking up client " + client + ": " + win);
9318 if (win == null) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009319 RuntimeException ex = new IllegalArgumentException(
9320 "Requested window " + client + " does not exist");
9321 if (throwOnError) {
9322 throw ex;
9323 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08009324 Slog.w(TAG, "Failed looking up window", ex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009325 return null;
9326 }
9327 if (session != null && win.mSession != session) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009328 RuntimeException ex = new IllegalArgumentException(
9329 "Requested window " + client + " is in session " +
9330 win.mSession + ", not " + session);
9331 if (throwOnError) {
9332 throw ex;
9333 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08009334 Slog.w(TAG, "Failed looking up window", ex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009335 return null;
9336 }
9337
9338 return win;
9339 }
9340
Dianne Hackborna8f60182009-09-01 19:01:50 -07009341 final void rebuildAppWindowListLocked() {
9342 int NW = mWindows.size();
9343 int i;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009344 int lastWallpaper = -1;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009345 int numRemoved = 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009346
Dianne Hackborna8f60182009-09-01 19:01:50 -07009347 // First remove all existing app windows.
9348 i=0;
9349 while (i < NW) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009350 WindowState w = (WindowState)mWindows.get(i);
9351 if (w.mAppToken != null) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009352 WindowState win = (WindowState)mWindows.remove(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08009353 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG,
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009354 "Rebuild removing window: " + win);
Dianne Hackborna8f60182009-09-01 19:01:50 -07009355 NW--;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009356 numRemoved++;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009357 continue;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009358 } else if (w.mAttrs.type == WindowManager.LayoutParams.TYPE_WALLPAPER
9359 && lastWallpaper == i-1) {
9360 lastWallpaper = i;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009361 }
9362 i++;
9363 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009364
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009365 // The wallpaper window(s) typically live at the bottom of the stack,
9366 // so skip them before adding app tokens.
9367 lastWallpaper++;
9368 i = lastWallpaper;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009369
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009370 // First add all of the exiting app tokens... these are no longer
9371 // in the main app list, but still have windows shown. We put them
9372 // in the back because now that the animation is over we no longer
9373 // will care about them.
9374 int NT = mExitingAppTokens.size();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009375 for (int j=0; j<NT; j++) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009376 i = reAddAppWindowsLocked(i, mExitingAppTokens.get(j));
9377 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009378
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009379 // And add in the still active app tokens in Z order.
9380 NT = mAppTokens.size();
9381 for (int j=0; j<NT; j++) {
9382 i = reAddAppWindowsLocked(i, mAppTokens.get(j));
Dianne Hackborna8f60182009-09-01 19:01:50 -07009383 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009384
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009385 i -= lastWallpaper;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009386 if (i != numRemoved) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009387 Slog.w(TAG, "Rebuild removed " + numRemoved
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009388 + " windows but added " + i);
9389 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07009390 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009392 private final void assignLayersLocked() {
9393 int N = mWindows.size();
9394 int curBaseLayer = 0;
9395 int curLayer = 0;
9396 int i;
Romain Guy06882f82009-06-10 13:36:04 -07009397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009398 for (i=0; i<N; i++) {
9399 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009400 if (w.mBaseLayer == curBaseLayer || w.mIsImWindow
9401 || (i > 0 && w.mIsWallpaper)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009402 curLayer += WINDOW_LAYER_MULTIPLIER;
9403 w.mLayer = curLayer;
9404 } else {
9405 curBaseLayer = curLayer = w.mBaseLayer;
9406 w.mLayer = curLayer;
9407 }
9408 if (w.mTargetAppToken != null) {
9409 w.mAnimLayer = w.mLayer + w.mTargetAppToken.animLayerAdjustment;
9410 } else if (w.mAppToken != null) {
9411 w.mAnimLayer = w.mLayer + w.mAppToken.animLayerAdjustment;
9412 } else {
9413 w.mAnimLayer = w.mLayer;
9414 }
9415 if (w.mIsImWindow) {
9416 w.mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07009417 } else if (w.mIsWallpaper) {
9418 w.mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009419 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08009420 if (DEBUG_LAYERS) Slog.v(TAG, "Assign layer " + w + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009421 + w.mAnimLayer);
9422 //System.out.println(
9423 // "Assigned layer " + curLayer + " to " + w.mClient.asBinder());
9424 }
9425 }
9426
9427 private boolean mInLayout = false;
9428 private final void performLayoutAndPlaceSurfacesLocked() {
9429 if (mInLayout) {
Dave Bortcfe65242009-04-09 14:51:04 -07009430 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009431 throw new RuntimeException("Recursive call!");
9432 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08009433 Slog.w(TAG, "performLayoutAndPlaceSurfacesLocked called while in layout");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009434 return;
9435 }
9436
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009437 if (mWaitingForConfig) {
9438 // Our configuration has changed (most likely rotation), but we
9439 // don't yet have the complete configuration to report to
9440 // applications. Don't do any window layout until we have it.
9441 return;
9442 }
9443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009444 boolean recoveringMemory = false;
9445 if (mForceRemoves != null) {
9446 recoveringMemory = true;
9447 // Wait a little it for things to settle down, and off we go.
9448 for (int i=0; i<mForceRemoves.size(); i++) {
9449 WindowState ws = mForceRemoves.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08009450 Slog.i(TAG, "Force removing: " + ws);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009451 removeWindowInnerLocked(ws.mSession, ws);
9452 }
9453 mForceRemoves = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009454 Slog.w(TAG, "Due to memory failure, waiting a bit for next layout");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009455 Object tmp = new Object();
9456 synchronized (tmp) {
9457 try {
9458 tmp.wait(250);
9459 } catch (InterruptedException e) {
9460 }
9461 }
9462 }
Romain Guy06882f82009-06-10 13:36:04 -07009463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009464 mInLayout = true;
9465 try {
9466 performLayoutAndPlaceSurfacesLockedInner(recoveringMemory);
Romain Guy06882f82009-06-10 13:36:04 -07009467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009468 int i = mPendingRemove.size()-1;
9469 if (i >= 0) {
9470 while (i >= 0) {
9471 WindowState w = mPendingRemove.get(i);
9472 removeWindowInnerLocked(w.mSession, w);
9473 i--;
9474 }
9475 mPendingRemove.clear();
9476
9477 mInLayout = false;
9478 assignLayersLocked();
9479 mLayoutNeeded = true;
9480 performLayoutAndPlaceSurfacesLocked();
9481
9482 } else {
9483 mInLayout = false;
9484 if (mLayoutNeeded) {
9485 requestAnimationLocked(0);
9486 }
9487 }
9488 } catch (RuntimeException e) {
9489 mInLayout = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009490 Slog.e(TAG, "Unhandled exception while layout out windows", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009491 }
9492 }
9493
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009494 private final int performLayoutLockedInner() {
9495 if (!mLayoutNeeded) {
9496 return 0;
9497 }
9498
9499 mLayoutNeeded = false;
9500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009501 final int dw = mDisplay.getWidth();
9502 final int dh = mDisplay.getHeight();
9503
9504 final int N = mWindows.size();
9505 int i;
9506
Joe Onorato8a9b2202010-02-26 18:56:32 -08009507 if (DEBUG_LAYOUT) Slog.v(TAG, "performLayout: needed="
Dianne Hackborn9b52a212009-12-11 14:51:35 -08009508 + mLayoutNeeded + " dw=" + dw + " dh=" + dh);
9509
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009510 mPolicy.beginLayoutLw(dw, dh);
Romain Guy06882f82009-06-10 13:36:04 -07009511
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009512 int seq = mLayoutSeq+1;
9513 if (seq < 0) seq = 0;
9514 mLayoutSeq = seq;
9515
9516 // First perform layout of any root windows (not attached
9517 // to another window).
9518 int topAttached = -1;
9519 for (i = N-1; i >= 0; i--) {
9520 WindowState win = (WindowState) mWindows.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009521
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009522 // Don't do layout of a window if it is not visible, or
9523 // soon won't be visible, to avoid wasting time and funky
9524 // changes while a window is animating away.
9525 final AppWindowToken atoken = win.mAppToken;
9526 final boolean gone = win.mViewVisibility == View.GONE
9527 || !win.mRelayoutCalled
9528 || win.mRootToken.hidden
9529 || (atoken != null && atoken.hiddenRequested)
9530 || win.mAttachedHidden
9531 || win.mExiting || win.mDestroying;
9532
9533 if (!win.mLayoutAttached) {
9534 if (DEBUG_LAYOUT) Slog.v(TAG, "First pass " + win
9535 + ": gone=" + gone + " mHaveFrame=" + win.mHaveFrame
9536 + " mLayoutAttached=" + win.mLayoutAttached);
9537 if (DEBUG_LAYOUT && gone) Slog.v(TAG, " (mViewVisibility="
9538 + win.mViewVisibility + " mRelayoutCalled="
9539 + win.mRelayoutCalled + " hidden="
9540 + win.mRootToken.hidden + " hiddenRequested="
9541 + (atoken != null && atoken.hiddenRequested)
9542 + " mAttachedHidden=" + win.mAttachedHidden);
9543 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009544
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009545 // If this view is GONE, then skip it -- keep the current
9546 // frame, and let the caller know so they can ignore it
9547 // if they want. (We do the normal layout for INVISIBLE
9548 // windows, since that means "perform layout as normal,
9549 // just don't display").
9550 if (!gone || !win.mHaveFrame) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009551 if (!win.mLayoutAttached) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009552 mPolicy.layoutWindowLw(win, win.mAttrs, null);
9553 win.mLayoutSeq = seq;
9554 if (DEBUG_LAYOUT) Slog.v(TAG, "-> mFrame="
9555 + win.mFrame + " mContainingFrame="
9556 + win.mContainingFrame + " mDisplayFrame="
9557 + win.mDisplayFrame);
9558 } else {
9559 if (topAttached < 0) topAttached = i;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009560 }
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009561 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009562 }
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009563
9564 // Now perform layout of attached windows, which usually
9565 // depend on the position of the window they are attached to.
9566 // XXX does not deal with windows that are attached to windows
9567 // that are themselves attached.
9568 for (i = topAttached; i >= 0; i--) {
9569 WindowState win = (WindowState) mWindows.get(i);
9570
9571 // If this view is GONE, then skip it -- keep the current
9572 // frame, and let the caller know so they can ignore it
9573 // if they want. (We do the normal layout for INVISIBLE
9574 // windows, since that means "perform layout as normal,
9575 // just don't display").
9576 if (win.mLayoutAttached) {
9577 if (DEBUG_LAYOUT) Slog.v(TAG, "Second pass " + win
9578 + " mHaveFrame=" + win.mHaveFrame
9579 + " mViewVisibility=" + win.mViewVisibility
9580 + " mRelayoutCalled=" + win.mRelayoutCalled);
9581 if ((win.mViewVisibility != View.GONE && win.mRelayoutCalled)
9582 || !win.mHaveFrame) {
9583 mPolicy.layoutWindowLw(win, win.mAttrs, win.mAttachedWindow);
9584 win.mLayoutSeq = seq;
9585 if (DEBUG_LAYOUT) Slog.v(TAG, "-> mFrame="
9586 + win.mFrame + " mContainingFrame="
9587 + win.mContainingFrame + " mDisplayFrame="
9588 + win.mDisplayFrame);
9589 }
9590 }
9591 }
9592
9593 return mPolicy.finishLayoutLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009594 }
Romain Guy06882f82009-06-10 13:36:04 -07009595
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009596 private final void performLayoutAndPlaceSurfacesLockedInner(
9597 boolean recoveringMemory) {
9598 final long currentTime = SystemClock.uptimeMillis();
9599 final int dw = mDisplay.getWidth();
9600 final int dh = mDisplay.getHeight();
9601
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009602 int i;
9603
Dianne Hackbornb601ce12010-03-01 23:36:02 -08009604 if (mFocusMayChange) {
9605 mFocusMayChange = false;
9606 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
9607 }
9608
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009609 if (mFxSession == null) {
9610 mFxSession = new SurfaceSession();
9611 }
Romain Guy06882f82009-06-10 13:36:04 -07009612
Joe Onorato8a9b2202010-02-26 18:56:32 -08009613 if (SHOW_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009614
9615 // Initialize state of exiting tokens.
9616 for (i=mExitingTokens.size()-1; i>=0; i--) {
9617 mExitingTokens.get(i).hasVisible = false;
9618 }
9619
9620 // Initialize state of exiting applications.
9621 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
9622 mExitingAppTokens.get(i).hasVisible = false;
9623 }
9624
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009625 boolean orientationChangeComplete = true;
9626 Session holdScreen = null;
9627 float screenBrightness = -1;
Mike Lockwoodfb73f792009-11-20 11:31:18 -05009628 float buttonBrightness = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009629 boolean focusDisplayed = false;
9630 boolean animating = false;
9631
9632 Surface.openTransaction();
9633 try {
Dianne Hackbornde2606d2009-12-18 16:53:55 -08009634 boolean wallpaperForceHidingChanged = false;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009635 int repeats = 0;
9636 int changes = 0;
9637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009638 do {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009639 repeats++;
9640 if (repeats > 6) {
9641 Slog.w(TAG, "Animation repeat aborted after too many iterations");
9642 mLayoutNeeded = false;
9643 break;
9644 }
9645
9646 if ((changes&(WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER
9647 | WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG
9648 | WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT)) != 0) {
9649 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
9650 if ((adjustWallpaperWindowsLocked()&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
9651 assignLayersLocked();
9652 mLayoutNeeded = true;
9653 }
9654 }
9655 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG) != 0) {
9656 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
9657 if (updateOrientationFromAppTokensLocked()) {
9658 mLayoutNeeded = true;
9659 mH.sendEmptyMessage(H.SEND_NEW_CONFIGURATION);
9660 }
9661 }
9662 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT) != 0) {
9663 mLayoutNeeded = true;
9664 }
9665 }
9666
9667 // FIRST LOOP: Perform a layout, if needed.
9668 if (repeats < 4) {
9669 changes = performLayoutLockedInner();
9670 if (changes != 0) {
9671 continue;
9672 }
9673 } else {
9674 Slog.w(TAG, "Layout repeat skipped after too many iterations");
9675 changes = 0;
9676 }
9677
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009678 final int transactionSequence = ++mTransactionSequence;
9679
9680 // Update animations of all applications, including those
9681 // associated with exiting/removed apps
9682 boolean tokensAnimating = false;
9683 final int NAT = mAppTokens.size();
9684 for (i=0; i<NAT; i++) {
9685 if (mAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
9686 tokensAnimating = true;
9687 }
9688 }
9689 final int NEAT = mExitingAppTokens.size();
9690 for (i=0; i<NEAT; i++) {
9691 if (mExitingAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
9692 tokensAnimating = true;
9693 }
9694 }
9695
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009696 // SECOND LOOP: Execute animations and update visibility of windows.
9697
Joe Onorato8a9b2202010-02-26 18:56:32 -08009698 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG, "*** ANIM STEP: seq="
Dianne Hackbornde2606d2009-12-18 16:53:55 -08009699 + transactionSequence + " tokensAnimating="
9700 + tokensAnimating);
9701
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009702 animating = tokensAnimating;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009703
9704 boolean tokenMayBeDrawn = false;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009705 boolean wallpaperMayChange = false;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009706 boolean forceHiding = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009707
9708 mPolicy.beginAnimationLw(dw, dh);
9709
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009710 final int N = mWindows.size();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009711
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009712 for (i=N-1; i>=0; i--) {
9713 WindowState w = (WindowState)mWindows.get(i);
9714
9715 final WindowManager.LayoutParams attrs = w.mAttrs;
9716
9717 if (w.mSurface != null) {
9718 // Execute animation.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009719 if (w.commitFinishDrawingLocked(currentTime)) {
9720 if ((w.mAttrs.flags
9721 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009722 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009723 "First draw done in potential wallpaper target " + w);
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009724 wallpaperMayChange = true;
9725 }
9726 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009727
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009728 boolean wasAnimating = w.mAnimating;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009729 if (w.stepAnimationLocked(currentTime, dw, dh)) {
9730 animating = true;
9731 //w.dump(" ");
9732 }
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009733 if (wasAnimating && !w.mAnimating && mWallpaperTarget == w) {
9734 wallpaperMayChange = true;
9735 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009736
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009737 if (mPolicy.doesForceHide(w, attrs)) {
9738 if (!wasAnimating && animating) {
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08009739 if (DEBUG_VISIBILITY) Slog.v(TAG,
9740 "Animation done that could impact force hide: "
9741 + w);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009742 wallpaperForceHidingChanged = true;
Dianne Hackbornb601ce12010-03-01 23:36:02 -08009743 mFocusMayChange = true;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009744 } else if (w.isReadyForDisplay() && w.mAnimation == null) {
9745 forceHiding = true;
9746 }
9747 } else if (mPolicy.canBeForceHidden(w, attrs)) {
9748 boolean changed;
9749 if (forceHiding) {
9750 changed = w.hideLw(false, false);
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08009751 if (DEBUG_VISIBILITY && changed) Slog.v(TAG,
9752 "Now policy hidden: " + w);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009753 } else {
9754 changed = w.showLw(false, false);
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08009755 if (DEBUG_VISIBILITY && changed) Slog.v(TAG,
9756 "Now policy shown: " + w);
9757 if (changed) {
9758 if (wallpaperForceHidingChanged
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009759 && w.isVisibleNow() /*w.isReadyForDisplay()*/) {
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08009760 // Assume we will need to animate. If
9761 // we don't (because the wallpaper will
9762 // stay with the lock screen), then we will
9763 // clean up later.
9764 Animation a = mPolicy.createForceHideEnterAnimation();
9765 if (a != null) {
9766 w.setAnimation(a);
9767 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009768 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08009769 if (mCurrentFocus == null ||
9770 mCurrentFocus.mLayer < w.mLayer) {
9771 // We are showing on to of the current
9772 // focus, so re-evaluate focus to make
9773 // sure it is correct.
9774 mFocusMayChange = true;
9775 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009776 }
9777 }
9778 if (changed && (attrs.flags
9779 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
9780 wallpaperMayChange = true;
9781 }
9782 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009783
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009784 mPolicy.animatingWindowLw(w, attrs);
9785 }
9786
9787 final AppWindowToken atoken = w.mAppToken;
9788 if (atoken != null && (!atoken.allDrawn || atoken.freezingScreen)) {
9789 if (atoken.lastTransactionSequence != transactionSequence) {
9790 atoken.lastTransactionSequence = transactionSequence;
9791 atoken.numInterestingWindows = atoken.numDrawnWindows = 0;
9792 atoken.startingDisplayed = false;
9793 }
9794 if ((w.isOnScreen() || w.mAttrs.type
9795 == WindowManager.LayoutParams.TYPE_BASE_APPLICATION)
9796 && !w.mExiting && !w.mDestroying) {
9797 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009798 Slog.v(TAG, "Eval win " + w + ": isDrawn="
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009799 + w.isDrawnLw()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009800 + ", isAnimating=" + w.isAnimating());
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009801 if (!w.isDrawnLw()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009802 Slog.v(TAG, "Not displayed: s=" + w.mSurface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009803 + " pv=" + w.mPolicyVisibility
9804 + " dp=" + w.mDrawPending
9805 + " cdp=" + w.mCommitDrawPending
9806 + " ah=" + w.mAttachedHidden
9807 + " th=" + atoken.hiddenRequested
9808 + " a=" + w.mAnimating);
9809 }
9810 }
9811 if (w != atoken.startingWindow) {
9812 if (!atoken.freezingScreen || !w.mAppFreezing) {
9813 atoken.numInterestingWindows++;
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009814 if (w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009815 atoken.numDrawnWindows++;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009816 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009817 "tokenMayBeDrawn: " + atoken
9818 + " freezingScreen=" + atoken.freezingScreen
9819 + " mAppFreezing=" + w.mAppFreezing);
9820 tokenMayBeDrawn = true;
9821 }
9822 }
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009823 } else if (w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009824 atoken.startingDisplayed = true;
9825 }
9826 }
9827 } else if (w.mReadyToShow) {
9828 w.performShowLocked();
9829 }
9830 }
9831
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009832 changes |= mPolicy.finishAnimationLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009833
9834 if (tokenMayBeDrawn) {
9835 // See if any windows have been drawn, so they (and others
9836 // associated with them) can now be shown.
9837 final int NT = mTokenList.size();
9838 for (i=0; i<NT; i++) {
9839 AppWindowToken wtoken = mTokenList.get(i).appWindowToken;
9840 if (wtoken == null) {
9841 continue;
9842 }
9843 if (wtoken.freezingScreen) {
9844 int numInteresting = wtoken.numInterestingWindows;
9845 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009846 if (DEBUG_VISIBILITY) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009847 "allDrawn: " + wtoken
9848 + " interesting=" + numInteresting
9849 + " drawn=" + wtoken.numDrawnWindows);
9850 wtoken.showAllWindowsLocked();
9851 unsetAppFreezingScreenLocked(wtoken, false, true);
9852 orientationChangeComplete = true;
9853 }
9854 } else if (!wtoken.allDrawn) {
9855 int numInteresting = wtoken.numInterestingWindows;
9856 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009857 if (DEBUG_VISIBILITY) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009858 "allDrawn: " + wtoken
9859 + " interesting=" + numInteresting
9860 + " drawn=" + wtoken.numDrawnWindows);
9861 wtoken.allDrawn = true;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009862 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_ANIM;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009863
9864 // We can now show all of the drawn windows!
9865 if (!mOpeningApps.contains(wtoken)) {
9866 wtoken.showAllWindowsLocked();
9867 }
9868 }
9869 }
9870 }
9871 }
9872
9873 // If we are ready to perform an app transition, check through
9874 // all of the app tokens to be shown and see if they are ready
9875 // to go.
9876 if (mAppTransitionReady) {
9877 int NN = mOpeningApps.size();
9878 boolean goodToGo = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009879 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009880 "Checking " + NN + " opening apps (frozen="
9881 + mDisplayFrozen + " timeout="
9882 + mAppTransitionTimeout + ")...");
9883 if (!mDisplayFrozen && !mAppTransitionTimeout) {
9884 // If the display isn't frozen, wait to do anything until
9885 // all of the apps are ready. Otherwise just go because
9886 // we'll unfreeze the display when everyone is ready.
9887 for (i=0; i<NN && goodToGo; i++) {
9888 AppWindowToken wtoken = mOpeningApps.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08009889 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009890 "Check opening app" + wtoken + ": allDrawn="
9891 + wtoken.allDrawn + " startingDisplayed="
9892 + wtoken.startingDisplayed);
9893 if (!wtoken.allDrawn && !wtoken.startingDisplayed
9894 && !wtoken.startingMoved) {
9895 goodToGo = false;
9896 }
9897 }
9898 }
9899 if (goodToGo) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009900 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG, "**** GOOD TO GO");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009901 int transit = mNextAppTransition;
9902 if (mSkipAppTransitionAnimation) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009903 transit = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009904 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009905 mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009906 mAppTransitionReady = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009907 mAppTransitionRunning = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009908 mAppTransitionTimeout = false;
9909 mStartingIconInTransition = false;
9910 mSkipAppTransitionAnimation = false;
9911
9912 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
9913
Dianne Hackborna8f60182009-09-01 19:01:50 -07009914 // If there are applications waiting to come to the
9915 // top of the stack, now is the time to move their windows.
9916 // (Note that we don't do apps going to the bottom
9917 // here -- we want to keep their windows in the old
9918 // Z-order until the animation completes.)
9919 if (mToTopApps.size() > 0) {
9920 NN = mAppTokens.size();
9921 for (i=0; i<NN; i++) {
9922 AppWindowToken wtoken = mAppTokens.get(i);
9923 if (wtoken.sendingToTop) {
9924 wtoken.sendingToTop = false;
9925 moveAppWindowsLocked(wtoken, NN, false);
9926 }
9927 }
9928 mToTopApps.clear();
9929 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009930
Dianne Hackborn25994b42009-09-04 14:21:19 -07009931 WindowState oldWallpaper = mWallpaperTarget;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009932
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009933 adjustWallpaperWindowsLocked();
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009934 wallpaperMayChange = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009935
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009936 // The top-most window will supply the layout params,
9937 // and we will determine it below.
9938 LayoutParams animLp = null;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009939 AppWindowToken animToken = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009940 int bestAnimLayer = -1;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009941
Joe Onorato8a9b2202010-02-26 18:56:32 -08009942 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009943 "New wallpaper target=" + mWallpaperTarget
9944 + ", lower target=" + mLowerWallpaperTarget
9945 + ", upper target=" + mUpperWallpaperTarget);
Dianne Hackborn25994b42009-09-04 14:21:19 -07009946 int foundWallpapers = 0;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009947 // Do a first pass through the tokens for two
9948 // things:
9949 // (1) Determine if both the closing and opening
9950 // app token sets are wallpaper targets, in which
9951 // case special animations are needed
9952 // (since the wallpaper needs to stay static
9953 // behind them).
9954 // (2) Find the layout params of the top-most
9955 // application window in the tokens, which is
9956 // what will control the animation theme.
9957 final int NC = mClosingApps.size();
9958 NN = NC + mOpeningApps.size();
9959 for (i=0; i<NN; i++) {
9960 AppWindowToken wtoken;
9961 int mode;
9962 if (i < NC) {
9963 wtoken = mClosingApps.get(i);
9964 mode = 1;
9965 } else {
9966 wtoken = mOpeningApps.get(i-NC);
9967 mode = 2;
9968 }
9969 if (mLowerWallpaperTarget != null) {
9970 if (mLowerWallpaperTarget.mAppToken == wtoken
9971 || mUpperWallpaperTarget.mAppToken == wtoken) {
9972 foundWallpapers |= mode;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009973 }
9974 }
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009975 if (wtoken.appFullscreen) {
9976 WindowState ws = wtoken.findMainWindow();
9977 if (ws != null) {
9978 // If this is a compatibility mode
9979 // window, we will always use its anim.
9980 if ((ws.mAttrs.flags&FLAG_COMPATIBLE_WINDOW) != 0) {
9981 animLp = ws.mAttrs;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009982 animToken = ws.mAppToken;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009983 bestAnimLayer = Integer.MAX_VALUE;
9984 } else if (ws.mLayer > bestAnimLayer) {
9985 animLp = ws.mAttrs;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009986 animToken = ws.mAppToken;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009987 bestAnimLayer = ws.mLayer;
9988 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07009989 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009990 }
9991 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009992
Dianne Hackborn25994b42009-09-04 14:21:19 -07009993 if (foundWallpapers == 3) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009994 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -07009995 "Wallpaper animation!");
9996 switch (transit) {
9997 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
9998 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
9999 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
10000 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN;
10001 break;
10002 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
10003 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
10004 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
10005 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE;
10006 break;
10007 }
Joe Onorato8a9b2202010-02-26 18:56:32 -080010008 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -070010009 "New transit: " + transit);
10010 } else if (oldWallpaper != null) {
10011 // We are transitioning from an activity with
10012 // a wallpaper to one without.
10013 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010014 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -070010015 "New transit away from wallpaper: " + transit);
10016 } else if (mWallpaperTarget != null) {
10017 // We are transitioning from an activity without
10018 // a wallpaper to now showing the wallpaper
10019 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010020 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -070010021 "New transit into wallpaper: " + transit);
10022 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010023
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010024 if ((transit&WindowManagerPolicy.TRANSIT_ENTER_MASK) != 0) {
10025 mLastEnterAnimToken = animToken;
10026 mLastEnterAnimParams = animLp;
10027 } else if (mLastEnterAnimParams != null) {
10028 animLp = mLastEnterAnimParams;
10029 mLastEnterAnimToken = null;
10030 mLastEnterAnimParams = null;
10031 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010032
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010033 // If all closing windows are obscured, then there is
10034 // no need to do an animation. This is the case, for
10035 // example, when this transition is being done behind
10036 // the lock screen.
10037 if (!mPolicy.allowAppAnimationsLw()) {
10038 animLp = null;
10039 }
10040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010041 NN = mOpeningApps.size();
10042 for (i=0; i<NN; i++) {
10043 AppWindowToken wtoken = mOpeningApps.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -080010044 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010045 "Now opening app" + wtoken);
10046 wtoken.reportedVisible = false;
10047 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -070010048 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -070010049 setTokenVisibilityLocked(wtoken, animLp, true, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010050 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -070010051 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010052 wtoken.showAllWindowsLocked();
10053 }
10054 NN = mClosingApps.size();
10055 for (i=0; i<NN; i++) {
10056 AppWindowToken wtoken = mClosingApps.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -080010057 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010058 "Now closing app" + wtoken);
10059 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -070010060 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -070010061 setTokenVisibilityLocked(wtoken, animLp, false, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010062 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -070010063 wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010064 // Force the allDrawn flag, because we want to start
10065 // this guy's animations regardless of whether it's
10066 // gotten drawn.
10067 wtoken.allDrawn = true;
10068 }
10069
Dianne Hackborn8b571a82009-09-25 16:09:43 -070010070 mNextAppTransitionPackage = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010072 mOpeningApps.clear();
10073 mClosingApps.clear();
10074
10075 // This has changed the visibility of windows, so perform
10076 // a new layout to get them all up-to-date.
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010077 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_LAYOUT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010078 mLayoutNeeded = true;
Dianne Hackborn20583ff2009-07-27 21:51:05 -070010079 if (!moveInputMethodWindowsIfNeededLocked(true)) {
10080 assignLayersLocked();
10081 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010082 updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES);
Dianne Hackbornb601ce12010-03-01 23:36:02 -080010083 mFocusMayChange = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010084 }
10085 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010086
Dianne Hackborn16064f92010-03-25 00:47:24 -070010087 int adjResult = 0;
10088
Dianne Hackborna8f60182009-09-01 19:01:50 -070010089 if (!animating && mAppTransitionRunning) {
10090 // We have finished the animation of an app transition. To do
10091 // this, we have delayed a lot of operations like showing and
10092 // hiding apps, moving apps in Z-order, etc. The app token list
10093 // reflects the correct Z-order, but the window list may now
10094 // be out of sync with it. So here we will just rebuild the
10095 // entire app window list. Fun!
10096 mAppTransitionRunning = false;
10097 // Clear information about apps that were moving.
10098 mToBottomApps.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010099
Dianne Hackborna8f60182009-09-01 19:01:50 -070010100 rebuildAppWindowListLocked();
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010101 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_LAYOUT;
Dianne Hackborn16064f92010-03-25 00:47:24 -070010102 adjResult |= ADJUST_WALLPAPER_LAYERS_CHANGED;
Dianne Hackborna8f60182009-09-01 19:01:50 -070010103 moveInputMethodWindowsIfNeededLocked(false);
10104 wallpaperMayChange = true;
Suchi Amalapurapuc9568e32009-11-05 18:51:16 -080010105 // Since the window list has been rebuilt, focus might
10106 // have to be recomputed since the actual order of windows
10107 // might have changed again.
Dianne Hackbornb601ce12010-03-01 23:36:02 -080010108 mFocusMayChange = true;
Dianne Hackborna8f60182009-09-01 19:01:50 -070010109 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010110
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010111 if (wallpaperForceHidingChanged && changes == 0 && !mAppTransitionReady) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010112 // At this point, there was a window with a wallpaper that
10113 // was force hiding other windows behind it, but now it
10114 // is going away. This may be simple -- just animate
10115 // away the wallpaper and its window -- or it may be
10116 // hard -- the wallpaper now needs to be shown behind
10117 // something that was hidden.
10118 WindowState oldWallpaper = mWallpaperTarget;
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010119 if (mLowerWallpaperTarget != null
10120 && mLowerWallpaperTarget.mAppToken != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010121 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010122 "wallpaperForceHiding changed with lower="
10123 + mLowerWallpaperTarget);
Joe Onorato8a9b2202010-02-26 18:56:32 -080010124 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010125 "hidden=" + mLowerWallpaperTarget.mAppToken.hidden +
10126 " hiddenRequested=" + mLowerWallpaperTarget.mAppToken.hiddenRequested);
10127 if (mLowerWallpaperTarget.mAppToken.hidden) {
10128 // The lower target has become hidden before we
10129 // actually started the animation... let's completely
10130 // re-evaluate everything.
10131 mLowerWallpaperTarget = mUpperWallpaperTarget = null;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010132 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_ANIM;
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010133 }
10134 }
Dianne Hackborn16064f92010-03-25 00:47:24 -070010135 adjResult |= adjustWallpaperWindowsLocked();
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010136 wallpaperMayChange = false;
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010137 wallpaperForceHidingChanged = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010138 if (DEBUG_WALLPAPER) Slog.v(TAG, "****** OLD: " + oldWallpaper
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010139 + " NEW: " + mWallpaperTarget
10140 + " LOWER: " + mLowerWallpaperTarget);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010141 if (mLowerWallpaperTarget == null) {
10142 // Whoops, we don't need a special wallpaper animation.
10143 // Clear them out.
10144 forceHiding = false;
10145 for (i=N-1; i>=0; i--) {
10146 WindowState w = (WindowState)mWindows.get(i);
10147 if (w.mSurface != null) {
10148 final WindowManager.LayoutParams attrs = w.mAttrs;
Suchi Amalapurapuc03d28b2009-10-28 14:32:05 -070010149 if (mPolicy.doesForceHide(w, attrs) && w.isVisibleLw()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010150 if (DEBUG_FOCUS) Slog.i(TAG, "win=" + w + " force hides other windows");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010151 forceHiding = true;
10152 } else if (mPolicy.canBeForceHidden(w, attrs)) {
10153 if (!w.mAnimating) {
10154 // We set the animation above so it
10155 // is not yet running.
10156 w.clearAnimation();
10157 }
10158 }
10159 }
10160 }
10161 }
10162 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010163
Dianne Hackborn6c3f5712009-08-25 18:42:59 -070010164 if (wallpaperMayChange) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010165 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010166 "Wallpaper may change! Adjusting");
Dianne Hackborn16064f92010-03-25 00:47:24 -070010167 adjResult |= adjustWallpaperWindowsLocked();
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010168 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010169
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010170 if ((adjResult&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010171 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010172 "Wallpaper layer changed: assigning layers + relayout");
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010173 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_LAYOUT;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010174 assignLayersLocked();
10175 } else if ((adjResult&ADJUST_WALLPAPER_VISIBILITY_CHANGED) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010176 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010177 "Wallpaper visibility changed: relayout");
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010178 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_LAYOUT;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010179 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010180
Dianne Hackbornb601ce12010-03-01 23:36:02 -080010181 if (mFocusMayChange) {
10182 mFocusMayChange = false;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010183 if (updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES)) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010184 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_ANIM;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010185 adjResult = 0;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -070010186 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010187 }
10188
10189 if (mLayoutNeeded) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010190 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_LAYOUT;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -070010191 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010192
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010193 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG, "*** ANIM STEP: changes=0x"
10194 + Integer.toHexString(changes));
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010195
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010196 } while (changes != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010197
10198 // THIRD LOOP: Update the surfaces of all windows.
10199
10200 final boolean someoneLosingFocus = mLosingFocus.size() != 0;
10201
10202 boolean obscured = false;
10203 boolean blurring = false;
10204 boolean dimming = false;
10205 boolean covered = false;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -070010206 boolean syswin = false;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010207 boolean backgroundFillerShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010208
Dianne Hackbornbdd52b22009-09-02 21:46:19 -070010209 final int N = mWindows.size();
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010211 for (i=N-1; i>=0; i--) {
10212 WindowState w = (WindowState)mWindows.get(i);
10213
10214 boolean displayed = false;
10215 final WindowManager.LayoutParams attrs = w.mAttrs;
10216 final int attrFlags = attrs.flags;
10217
10218 if (w.mSurface != null) {
Dianne Hackbornac3587d2010-03-11 11:12:11 -080010219 // XXX NOTE: The logic here could be improved. We have
10220 // the decision about whether to resize a window separated
10221 // from whether to hide the surface. This can cause us to
10222 // resize a surface even if we are going to hide it. You
10223 // can see this by (1) holding device in landscape mode on
10224 // home screen; (2) tapping browser icon (device will rotate
10225 // to landscape; (3) tap home. The wallpaper will be resized
10226 // in step 2 but then immediately hidden, causing us to
10227 // have to resize and then redraw it again in step 3. It
10228 // would be nice to figure out how to avoid this, but it is
10229 // difficult because we do need to resize surfaces in some
10230 // cases while they are hidden such as when first showing a
10231 // window.
10232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010233 w.computeShownFrameLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -080010234 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010235 TAG, "Placing surface #" + i + " " + w.mSurface
10236 + ": new=" + w.mShownFrame + ", old="
10237 + w.mLastShownFrame);
10238
10239 boolean resize;
10240 int width, height;
10241 if ((w.mAttrs.flags & w.mAttrs.FLAG_SCALED) != 0) {
10242 resize = w.mLastRequestedWidth != w.mRequestedWidth ||
10243 w.mLastRequestedHeight != w.mRequestedHeight;
10244 // for a scaled surface, we just want to use
10245 // the requested size.
10246 width = w.mRequestedWidth;
10247 height = w.mRequestedHeight;
10248 w.mLastRequestedWidth = width;
10249 w.mLastRequestedHeight = height;
10250 w.mLastShownFrame.set(w.mShownFrame);
10251 try {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010252 if (SHOW_TRANSACTIONS) logSurface(w,
10253 "POS " + w.mShownFrame.left
10254 + ", " + w.mShownFrame.top, null);
Dianne Hackborn16064f92010-03-25 00:47:24 -070010255 w.mSurfaceX = w.mShownFrame.left;
10256 w.mSurfaceY = w.mShownFrame.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010257 w.mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
10258 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010259 Slog.w(TAG, "Error positioning surface in " + w, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010260 if (!recoveringMemory) {
10261 reclaimSomeSurfaceMemoryLocked(w, "position");
10262 }
10263 }
10264 } else {
10265 resize = !w.mLastShownFrame.equals(w.mShownFrame);
10266 width = w.mShownFrame.width();
10267 height = w.mShownFrame.height();
10268 w.mLastShownFrame.set(w.mShownFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010269 }
10270
10271 if (resize) {
10272 if (width < 1) width = 1;
10273 if (height < 1) height = 1;
10274 if (w.mSurface != null) {
10275 try {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010276 if (SHOW_TRANSACTIONS) logSurface(w,
10277 "POS " + w.mShownFrame.left + ","
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010278 + w.mShownFrame.top + " SIZE "
10279 + w.mShownFrame.width() + "x"
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010280 + w.mShownFrame.height(), null);
Dianne Hackbornac3587d2010-03-11 11:12:11 -080010281 w.mSurfaceResized = true;
Dianne Hackborn16064f92010-03-25 00:47:24 -070010282 w.mSurfaceW = width;
10283 w.mSurfaceH = height;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010284 w.mSurface.setSize(width, height);
Dianne Hackborn16064f92010-03-25 00:47:24 -070010285 w.mSurfaceX = w.mShownFrame.left;
10286 w.mSurfaceY = w.mShownFrame.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010287 w.mSurface.setPosition(w.mShownFrame.left,
10288 w.mShownFrame.top);
10289 } catch (RuntimeException e) {
10290 // If something goes wrong with the surface (such
10291 // as running out of memory), don't take down the
10292 // entire system.
Joe Onorato8a9b2202010-02-26 18:56:32 -080010293 Slog.e(TAG, "Failure updating surface of " + w
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010294 + "size=(" + width + "x" + height
10295 + "), pos=(" + w.mShownFrame.left
10296 + "," + w.mShownFrame.top + ")", e);
10297 if (!recoveringMemory) {
10298 reclaimSomeSurfaceMemoryLocked(w, "size");
10299 }
10300 }
10301 }
10302 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010303 if (!w.mAppFreezing && w.mLayoutSeq == mLayoutSeq) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010304 w.mContentInsetsChanged =
10305 !w.mLastContentInsets.equals(w.mContentInsets);
10306 w.mVisibleInsetsChanged =
10307 !w.mLastVisibleInsets.equals(w.mVisibleInsets);
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010308 boolean configChanged =
10309 w.mConfiguration != mCurConfiguration
10310 && (w.mConfiguration == null
10311 || mCurConfiguration.diff(w.mConfiguration) != 0);
Dianne Hackborn694f79b2010-03-17 19:44:59 -070010312 if (DEBUG_CONFIGURATION && configChanged) {
10313 Slog.v(TAG, "Win " + w + " config changed: "
10314 + mCurConfiguration);
10315 }
Joe Onorato8a9b2202010-02-26 18:56:32 -080010316 if (localLOGV) Slog.v(TAG, "Resizing " + w
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010317 + ": configChanged=" + configChanged
10318 + " last=" + w.mLastFrame + " frame=" + w.mFrame);
Romain Guy06882f82009-06-10 13:36:04 -070010319 if (!w.mLastFrame.equals(w.mFrame)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010320 || w.mContentInsetsChanged
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010321 || w.mVisibleInsetsChanged
Dianne Hackbornac3587d2010-03-11 11:12:11 -080010322 || w.mSurfaceResized
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010323 || configChanged) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010324 w.mLastFrame.set(w.mFrame);
10325 w.mLastContentInsets.set(w.mContentInsets);
10326 w.mLastVisibleInsets.set(w.mVisibleInsets);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010327 // If the screen is currently frozen, then keep
10328 // it frozen until this window draws at its new
10329 // orientation.
10330 if (mDisplayFrozen) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010331 if (DEBUG_ORIENTATION) Slog.v(TAG,
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010332 "Resizing while display frozen: " + w);
10333 w.mOrientationChanging = true;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010334 if (!mWindowsFreezingScreen) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010335 mWindowsFreezingScreen = true;
10336 // XXX should probably keep timeout from
10337 // when we first froze the display.
10338 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
10339 mH.sendMessageDelayed(mH.obtainMessage(
10340 H.WINDOW_FREEZE_TIMEOUT), 2000);
10341 }
10342 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010343 // If the orientation is changing, then we need to
10344 // hold off on unfreezing the display until this
10345 // window has been redrawn; to do that, we need
10346 // to go through the process of getting informed
10347 // by the application when it has finished drawing.
10348 if (w.mOrientationChanging) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010349 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010350 "Orientation start waiting for draw in "
10351 + w + ", surface " + w.mSurface);
10352 w.mDrawPending = true;
10353 w.mCommitDrawPending = false;
10354 w.mReadyToShow = false;
10355 if (w.mAppToken != null) {
10356 w.mAppToken.allDrawn = false;
10357 }
10358 }
Dianne Hackbornac3587d2010-03-11 11:12:11 -080010359 if (DEBUG_RESIZE || DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010360 "Resizing window " + w + " to " + w.mFrame);
10361 mResizingWindows.add(w);
10362 } else if (w.mOrientationChanging) {
10363 if (!w.mDrawPending && !w.mCommitDrawPending) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010364 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010365 "Orientation not waiting for draw in "
10366 + w + ", surface " + w.mSurface);
10367 w.mOrientationChanging = false;
10368 }
10369 }
10370 }
10371
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010372 if (w.mAttachedHidden || !w.isReadyForDisplay()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010373 if (!w.mLastHidden) {
10374 //dump();
Dianne Hackborn5943c202010-04-12 21:36:49 -070010375 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Window hiding: waitingToShow="
10376 + w.mRootToken.waitingToShow + " polvis="
10377 + w.mPolicyVisibility + " atthid="
10378 + w.mAttachedHidden + " tokhid="
10379 + w.mRootToken.hidden + " vis="
10380 + w.mViewVisibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010381 w.mLastHidden = true;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010382 if (SHOW_TRANSACTIONS) logSurface(w,
10383 "HIDE (performLayout)", null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010384 if (w.mSurface != null) {
Dianne Hackborn16064f92010-03-25 00:47:24 -070010385 w.mSurfaceShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010386 try {
10387 w.mSurface.hide();
10388 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010389 Slog.w(TAG, "Exception hiding surface in " + w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010390 }
10391 }
10392 mKeyWaiter.releasePendingPointerLocked(w.mSession);
10393 }
10394 // If we are waiting for this window to handle an
10395 // orientation change, well, it is hidden, so
10396 // doesn't really matter. Note that this does
10397 // introduce a potential glitch if the window
10398 // becomes unhidden before it has drawn for the
10399 // new orientation.
10400 if (w.mOrientationChanging) {
10401 w.mOrientationChanging = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010402 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010403 "Orientation change skips hidden " + w);
10404 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010405 } else if (w.mLastLayer != w.mAnimLayer
10406 || w.mLastAlpha != w.mShownAlpha
10407 || w.mLastDsDx != w.mDsDx
10408 || w.mLastDtDx != w.mDtDx
10409 || w.mLastDsDy != w.mDsDy
10410 || w.mLastDtDy != w.mDtDy
10411 || w.mLastHScale != w.mHScale
10412 || w.mLastVScale != w.mVScale
10413 || w.mLastHidden) {
10414 displayed = true;
10415 w.mLastAlpha = w.mShownAlpha;
10416 w.mLastLayer = w.mAnimLayer;
10417 w.mLastDsDx = w.mDsDx;
10418 w.mLastDtDx = w.mDtDx;
10419 w.mLastDsDy = w.mDsDy;
10420 w.mLastDtDy = w.mDtDy;
10421 w.mLastHScale = w.mHScale;
10422 w.mLastVScale = w.mVScale;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010423 if (SHOW_TRANSACTIONS) logSurface(w,
10424 "alpha=" + w.mShownAlpha + " layer=" + w.mAnimLayer
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010425 + " matrix=[" + (w.mDsDx*w.mHScale)
10426 + "," + (w.mDtDx*w.mVScale)
10427 + "][" + (w.mDsDy*w.mHScale)
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010428 + "," + (w.mDtDy*w.mVScale) + "]", null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010429 if (w.mSurface != null) {
10430 try {
Dianne Hackborn16064f92010-03-25 00:47:24 -070010431 w.mSurfaceAlpha = w.mShownAlpha;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010432 w.mSurface.setAlpha(w.mShownAlpha);
Dianne Hackborn16064f92010-03-25 00:47:24 -070010433 w.mSurfaceLayer = w.mAnimLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010434 w.mSurface.setLayer(w.mAnimLayer);
10435 w.mSurface.setMatrix(
10436 w.mDsDx*w.mHScale, w.mDtDx*w.mVScale,
10437 w.mDsDy*w.mHScale, w.mDtDy*w.mVScale);
10438 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010439 Slog.w(TAG, "Error updating surface in " + w, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010440 if (!recoveringMemory) {
10441 reclaimSomeSurfaceMemoryLocked(w, "update");
10442 }
10443 }
10444 }
10445
10446 if (w.mLastHidden && !w.mDrawPending
10447 && !w.mCommitDrawPending
10448 && !w.mReadyToShow) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010449 if (SHOW_TRANSACTIONS) logSurface(w,
10450 "SHOW (performLayout)", null);
Joe Onorato8a9b2202010-02-26 18:56:32 -080010451 if (DEBUG_VISIBILITY) Slog.v(TAG, "Showing " + w
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010452 + " during relayout");
10453 if (showSurfaceRobustlyLocked(w)) {
10454 w.mHasDrawn = true;
10455 w.mLastHidden = false;
10456 } else {
10457 w.mOrientationChanging = false;
10458 }
10459 }
10460 if (w.mSurface != null) {
10461 w.mToken.hasVisible = true;
10462 }
10463 } else {
10464 displayed = true;
10465 }
10466
10467 if (displayed) {
10468 if (!covered) {
Romain Guy980a9382010-01-08 15:06:28 -080010469 if (attrs.width == LayoutParams.MATCH_PARENT
10470 && attrs.height == LayoutParams.MATCH_PARENT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010471 covered = true;
10472 }
10473 }
10474 if (w.mOrientationChanging) {
10475 if (w.mDrawPending || w.mCommitDrawPending) {
10476 orientationChangeComplete = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010477 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010478 "Orientation continue waiting for draw in " + w);
10479 } else {
10480 w.mOrientationChanging = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010481 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010482 "Orientation change complete in " + w);
10483 }
10484 }
10485 w.mToken.hasVisible = true;
10486 }
10487 } else if (w.mOrientationChanging) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010488 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010489 "Orientation change skips hidden " + w);
10490 w.mOrientationChanging = false;
10491 }
10492
10493 final boolean canBeSeen = w.isDisplayedLw();
10494
10495 if (someoneLosingFocus && w == mCurrentFocus && canBeSeen) {
10496 focusDisplayed = true;
10497 }
10498
Dianne Hackborne9e9bca2009-08-18 15:08:22 -070010499 final boolean obscuredChanged = w.mObscured != obscured;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010501 // Update effect.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010502 if (!(w.mObscured=obscured)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010503 if (w.mSurface != null) {
10504 if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
10505 holdScreen = w.mSession;
10506 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -070010507 if (!syswin && w.mAttrs.screenBrightness >= 0
10508 && screenBrightness < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010509 screenBrightness = w.mAttrs.screenBrightness;
10510 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -050010511 if (!syswin && w.mAttrs.buttonBrightness >= 0
10512 && buttonBrightness < 0) {
10513 buttonBrightness = w.mAttrs.buttonBrightness;
10514 }
Mike Lockwood46af6a82010-03-09 08:28:22 -050010515 if (canBeSeen
10516 && (attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG
10517 || attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD
10518 || attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_ERROR)) {
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -070010519 syswin = true;
10520 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010521 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010522
Dianne Hackborn25994b42009-09-04 14:21:19 -070010523 boolean opaqueDrawn = canBeSeen && w.isOpaqueDrawn();
10524 if (opaqueDrawn && w.isFullscreen(dw, dh)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010525 // This window completely covers everything behind it,
10526 // so we want to leave all of them as unblurred (for
10527 // performance reasons).
10528 obscured = true;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010529 } else if (opaqueDrawn && w.needsBackgroundFiller(dw, dh)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010530 if (SHOW_TRANSACTIONS) Slog.d(TAG, "showing background filler");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010531 // This window is in compatibility mode, and needs background filler.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010532 obscured = true;
10533 if (mBackgroundFillerSurface == null) {
10534 try {
10535 mBackgroundFillerSurface = new Surface(mFxSession, 0,
Mathias Agopian5d26c1e2010-03-01 16:09:43 -080010536 "BackGroundFiller",
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010537 0, dw, dh,
10538 PixelFormat.OPAQUE,
10539 Surface.FX_SURFACE_NORMAL);
10540 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010541 Slog.e(TAG, "Exception creating filler surface", e);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010542 }
10543 }
10544 try {
10545 mBackgroundFillerSurface.setPosition(0, 0);
10546 mBackgroundFillerSurface.setSize(dw, dh);
10547 // Using the same layer as Dim because they will never be shown at the
10548 // same time.
10549 mBackgroundFillerSurface.setLayer(w.mAnimLayer - 1);
10550 mBackgroundFillerSurface.show();
10551 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010552 Slog.e(TAG, "Exception showing filler surface");
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010553 }
10554 backgroundFillerShown = true;
10555 mBackgroundFillerShown = true;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010556 } else if (canBeSeen && !obscured &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010557 (attrFlags&FLAG_BLUR_BEHIND|FLAG_DIM_BEHIND) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010558 if (localLOGV) Slog.v(TAG, "Win " + w
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010559 + ": blurring=" + blurring
10560 + " obscured=" + obscured
10561 + " displayed=" + displayed);
10562 if ((attrFlags&FLAG_DIM_BEHIND) != 0) {
10563 if (!dimming) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010564 //Slog.i(TAG, "DIM BEHIND: " + w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010565 dimming = true;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010566 if (mDimAnimator == null) {
10567 mDimAnimator = new DimAnimator(mFxSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010568 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010569 mDimAnimator.show(dw, dh);
Dianne Hackborn16064f92010-03-25 00:47:24 -070010570 mDimAnimator.updateParameters(w, currentTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010571 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010572 }
10573 if ((attrFlags&FLAG_BLUR_BEHIND) != 0) {
10574 if (!blurring) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010575 //Slog.i(TAG, "BLUR BEHIND: " + w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010576 blurring = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010577 if (mBlurSurface == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010578 if (SHOW_TRANSACTIONS) Slog.i(TAG, " BLUR "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010579 + mBlurSurface + ": CREATE");
10580 try {
Romain Guy06882f82009-06-10 13:36:04 -070010581 mBlurSurface = new Surface(mFxSession, 0,
Mathias Agopian5d26c1e2010-03-01 16:09:43 -080010582 "BlurSurface",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010583 -1, 16, 16,
10584 PixelFormat.OPAQUE,
10585 Surface.FX_SURFACE_BLUR);
10586 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010587 Slog.e(TAG, "Exception creating Blur surface", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010588 }
10589 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010590 if (mBlurSurface != null) {
Dianne Hackborn16064f92010-03-25 00:47:24 -070010591 if (SHOW_TRANSACTIONS) Slog.i(TAG, " BLUR "
10592 + mBlurSurface + ": pos=(0,0) (" +
10593 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010594 mBlurSurface.setPosition(0, 0);
10595 mBlurSurface.setSize(dw, dh);
Dianne Hackborn16064f92010-03-25 00:47:24 -070010596 mBlurSurface.setLayer(w.mAnimLayer-2);
10597 if (!mBlurShown) {
10598 try {
10599 if (SHOW_TRANSACTIONS) Slog.i(TAG, " BLUR "
10600 + mBlurSurface + ": SHOW");
10601 mBlurSurface.show();
10602 } catch (RuntimeException e) {
10603 Slog.w(TAG, "Failure showing blur surface", e);
10604 }
10605 mBlurShown = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010606 }
10607 }
10608 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010609 }
10610 }
10611 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010612
Dianne Hackborne9e9bca2009-08-18 15:08:22 -070010613 if (obscuredChanged && mWallpaperTarget == w) {
10614 // This is the wallpaper target and its obscured state
10615 // changed... make sure the current wallaper's visibility
10616 // has been updated accordingly.
10617 updateWallpaperVisibilityLocked();
10618 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010619 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010620
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010621 if (backgroundFillerShown == false && mBackgroundFillerShown) {
10622 mBackgroundFillerShown = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010623 if (SHOW_TRANSACTIONS) Slog.d(TAG, "hiding background filler");
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010624 try {
10625 mBackgroundFillerSurface.hide();
10626 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010627 Slog.e(TAG, "Exception hiding filler surface", e);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010628 }
10629 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010630
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010631 if (mDimAnimator != null && mDimAnimator.mDimShown) {
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010632 animating |= mDimAnimator.updateSurface(dimming, currentTime,
10633 mDisplayFrozen || !mPolicy.isScreenOn());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010634 }
Romain Guy06882f82009-06-10 13:36:04 -070010635
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010636 if (!blurring && mBlurShown) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010637 if (SHOW_TRANSACTIONS) Slog.i(TAG, " BLUR " + mBlurSurface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010638 + ": HIDE");
10639 try {
10640 mBlurSurface.hide();
10641 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010642 Slog.w(TAG, "Illegal argument exception hiding blur surface");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010643 }
10644 mBlurShown = false;
10645 }
10646
Joe Onorato8a9b2202010-02-26 18:56:32 -080010647 if (SHOW_TRANSACTIONS) Slog.i(TAG, "<<< CLOSE TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010648 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010649 Slog.e(TAG, "Unhandled exception in Window Manager", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010650 }
10651
10652 Surface.closeTransaction();
Romain Guy06882f82009-06-10 13:36:04 -070010653
Joe Onorato8a9b2202010-02-26 18:56:32 -080010654 if (DEBUG_ORIENTATION && mDisplayFrozen) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010655 "With display frozen, orientationChangeComplete="
10656 + orientationChangeComplete);
10657 if (orientationChangeComplete) {
10658 if (mWindowsFreezingScreen) {
10659 mWindowsFreezingScreen = false;
10660 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
10661 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010662 stopFreezingDisplayLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010663 }
Romain Guy06882f82009-06-10 13:36:04 -070010664
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010665 i = mResizingWindows.size();
10666 if (i > 0) {
10667 do {
10668 i--;
10669 WindowState win = mResizingWindows.get(i);
10670 try {
Dianne Hackbornac3587d2010-03-11 11:12:11 -080010671 if (DEBUG_RESIZE || DEBUG_ORIENTATION) Slog.v(TAG,
10672 "Reporting new frame to " + win + ": " + win.mFrame);
Dianne Hackborn694f79b2010-03-17 19:44:59 -070010673 int diff = 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010674 boolean configChanged =
10675 win.mConfiguration != mCurConfiguration
10676 && (win.mConfiguration == null
Dianne Hackborn694f79b2010-03-17 19:44:59 -070010677 || (diff=mCurConfiguration.diff(win.mConfiguration)) != 0);
10678 if ((DEBUG_RESIZE || DEBUG_ORIENTATION || DEBUG_CONFIGURATION)
10679 && configChanged) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010680 Slog.i(TAG, "Sending new config to window " + win + ": "
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010681 + win.mFrame.width() + "x" + win.mFrame.height()
Dianne Hackborn694f79b2010-03-17 19:44:59 -070010682 + " / " + mCurConfiguration + " / 0x"
10683 + Integer.toHexString(diff));
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010684 }
Dianne Hackborn694f79b2010-03-17 19:44:59 -070010685 win.mConfiguration = mCurConfiguration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010686 win.mClient.resized(win.mFrame.width(),
10687 win.mFrame.height(), win.mLastContentInsets,
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010688 win.mLastVisibleInsets, win.mDrawPending,
10689 configChanged ? win.mConfiguration : null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010690 win.mContentInsetsChanged = false;
10691 win.mVisibleInsetsChanged = false;
Dianne Hackbornac3587d2010-03-11 11:12:11 -080010692 win.mSurfaceResized = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010693 } catch (RemoteException e) {
10694 win.mOrientationChanging = false;
10695 }
10696 } while (i > 0);
10697 mResizingWindows.clear();
10698 }
Romain Guy06882f82009-06-10 13:36:04 -070010699
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010700 // Destroy the surface of any windows that are no longer visible.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010701 boolean wallpaperDestroyed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010702 i = mDestroySurface.size();
10703 if (i > 0) {
10704 do {
10705 i--;
10706 WindowState win = mDestroySurface.get(i);
10707 win.mDestroying = false;
10708 if (mInputMethodWindow == win) {
10709 mInputMethodWindow = null;
10710 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010711 if (win == mWallpaperTarget) {
10712 wallpaperDestroyed = true;
10713 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010714 win.destroySurfaceLocked();
10715 } while (i > 0);
10716 mDestroySurface.clear();
10717 }
10718
10719 // Time to remove any exiting tokens?
10720 for (i=mExitingTokens.size()-1; i>=0; i--) {
10721 WindowToken token = mExitingTokens.get(i);
10722 if (!token.hasVisible) {
10723 mExitingTokens.remove(i);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070010724 if (token.windowType == TYPE_WALLPAPER) {
10725 mWallpaperTokens.remove(token);
10726 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010727 }
10728 }
10729
10730 // Time to remove any exiting applications?
10731 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
10732 AppWindowToken token = mExitingAppTokens.get(i);
10733 if (!token.hasVisible && !mClosingApps.contains(token)) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -070010734 // Make sure there is no animation running on this token,
10735 // so any windows associated with it will be removed as
10736 // soon as their animations are complete
10737 token.animation = null;
10738 token.animating = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010739 mAppTokens.remove(token);
10740 mExitingAppTokens.remove(i);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010741 if (mLastEnterAnimToken == token) {
10742 mLastEnterAnimToken = null;
10743 mLastEnterAnimParams = null;
10744 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010745 }
10746 }
10747
Dianne Hackborna8f60182009-09-01 19:01:50 -070010748 boolean needRelayout = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010749
Dianne Hackborna8f60182009-09-01 19:01:50 -070010750 if (!animating && mAppTransitionRunning) {
10751 // We have finished the animation of an app transition. To do
10752 // this, we have delayed a lot of operations like showing and
10753 // hiding apps, moving apps in Z-order, etc. The app token list
10754 // reflects the correct Z-order, but the window list may now
10755 // be out of sync with it. So here we will just rebuild the
10756 // entire app window list. Fun!
10757 mAppTransitionRunning = false;
10758 needRelayout = true;
10759 rebuildAppWindowListLocked();
Dianne Hackborn16064f92010-03-25 00:47:24 -070010760 assignLayersLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -070010761 // Clear information about apps that were moving.
10762 mToBottomApps.clear();
10763 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010764
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010765 if (focusDisplayed) {
10766 mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
10767 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010768 if (wallpaperDestroyed) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010769 needRelayout = adjustWallpaperWindowsLocked() != 0;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010770 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070010771 if (needRelayout) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010772 requestAnimationLocked(0);
10773 } else if (animating) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010774 requestAnimationLocked(currentTime+(1000/60)-SystemClock.uptimeMillis());
10775 }
Dianne Hackbornce73c1e2010-04-12 23:11:38 -070010776
10777 if (DEBUG_FREEZE) Slog.v(TAG, "Layout: mDisplayFrozen=" + mDisplayFrozen
10778 + " holdScreen=" + holdScreen);
10779 if (!mDisplayFrozen) {
10780 mQueue.setHoldScreenLocked(holdScreen != null);
10781 if (screenBrightness < 0 || screenBrightness > 1.0f) {
10782 mPowerManager.setScreenBrightnessOverride(-1);
10783 } else {
10784 mPowerManager.setScreenBrightnessOverride((int)
10785 (screenBrightness * Power.BRIGHTNESS_ON));
10786 }
10787 if (buttonBrightness < 0 || buttonBrightness > 1.0f) {
10788 mPowerManager.setButtonBrightnessOverride(-1);
10789 } else {
10790 mPowerManager.setButtonBrightnessOverride((int)
10791 (buttonBrightness * Power.BRIGHTNESS_ON));
10792 }
10793 if (holdScreen != mHoldingScreenOn) {
10794 mHoldingScreenOn = holdScreen;
10795 Message m = mH.obtainMessage(H.HOLD_SCREEN_CHANGED, holdScreen);
10796 mH.sendMessage(m);
10797 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010798 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010799
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010800 if (mTurnOnScreen) {
Dianne Hackbornb601ce12010-03-01 23:36:02 -080010801 if (DEBUG_VISIBILITY) Slog.v(TAG, "Turning screen on after layout!");
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010802 mPowerManager.userActivity(SystemClock.uptimeMillis(), false,
10803 LocalPowerManager.BUTTON_EVENT, true);
10804 mTurnOnScreen = false;
10805 }
Dianne Hackbornf3bea9c2009-12-09 18:26:21 -080010806
10807 // Check to see if we are now in a state where the screen should
10808 // be enabled, because the window obscured flags have changed.
10809 enableScreenIfNeededLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010810 }
10811
10812 void requestAnimationLocked(long delay) {
10813 if (!mAnimationPending) {
10814 mAnimationPending = true;
10815 mH.sendMessageDelayed(mH.obtainMessage(H.ANIMATE), delay);
10816 }
10817 }
Romain Guy06882f82009-06-10 13:36:04 -070010818
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010819 /**
10820 * Have the surface flinger show a surface, robustly dealing with
10821 * error conditions. In particular, if there is not enough memory
10822 * to show the surface, then we will try to get rid of other surfaces
10823 * in order to succeed.
Romain Guy06882f82009-06-10 13:36:04 -070010824 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010825 * @return Returns true if the surface was successfully shown.
10826 */
10827 boolean showSurfaceRobustlyLocked(WindowState win) {
10828 try {
10829 if (win.mSurface != null) {
Dianne Hackborn16064f92010-03-25 00:47:24 -070010830 win.mSurfaceShown = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010831 win.mSurface.show();
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010832 if (win.mTurnOnScreen) {
Dianne Hackbornb601ce12010-03-01 23:36:02 -080010833 if (DEBUG_VISIBILITY) Slog.v(TAG,
10834 "Show surface turning screen on: " + win);
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010835 win.mTurnOnScreen = false;
10836 mTurnOnScreen = true;
10837 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010838 }
10839 return true;
10840 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010841 Slog.w(TAG, "Failure showing surface " + win.mSurface + " in " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010842 }
Romain Guy06882f82009-06-10 13:36:04 -070010843
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010844 reclaimSomeSurfaceMemoryLocked(win, "show");
Romain Guy06882f82009-06-10 13:36:04 -070010845
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010846 return false;
10847 }
Romain Guy06882f82009-06-10 13:36:04 -070010848
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010849 void reclaimSomeSurfaceMemoryLocked(WindowState win, String operation) {
10850 final Surface surface = win.mSurface;
Romain Guy06882f82009-06-10 13:36:04 -070010851
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010852 EventLog.writeEvent(EventLogTags.WM_NO_SURFACE_MEMORY, win.toString(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010853 win.mSession.mPid, operation);
Romain Guy06882f82009-06-10 13:36:04 -070010854
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010855 if (mForceRemoves == null) {
10856 mForceRemoves = new ArrayList<WindowState>();
10857 }
Romain Guy06882f82009-06-10 13:36:04 -070010858
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010859 long callingIdentity = Binder.clearCallingIdentity();
10860 try {
10861 // There was some problem... first, do a sanity check of the
10862 // window list to make sure we haven't left any dangling surfaces
10863 // around.
10864 int N = mWindows.size();
10865 boolean leakedSurface = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010866 Slog.i(TAG, "Out of memory for surface! Looking for leaks...");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010867 for (int i=0; i<N; i++) {
10868 WindowState ws = (WindowState)mWindows.get(i);
10869 if (ws.mSurface != null) {
10870 if (!mSessions.contains(ws.mSession)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010871 Slog.w(TAG, "LEAKED SURFACE (session doesn't exist): "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010872 + ws + " surface=" + ws.mSurface
10873 + " token=" + win.mToken
10874 + " pid=" + ws.mSession.mPid
10875 + " uid=" + ws.mSession.mUid);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010876 ws.mSurface.destroy();
Dianne Hackborn16064f92010-03-25 00:47:24 -070010877 ws.mSurfaceShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010878 ws.mSurface = null;
10879 mForceRemoves.add(ws);
10880 i--;
10881 N--;
10882 leakedSurface = true;
10883 } else if (win.mAppToken != null && win.mAppToken.clientHidden) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010884 Slog.w(TAG, "LEAKED SURFACE (app token hidden): "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010885 + ws + " surface=" + ws.mSurface
10886 + " token=" + win.mAppToken);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010887 ws.mSurface.destroy();
Dianne Hackborn16064f92010-03-25 00:47:24 -070010888 ws.mSurfaceShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010889 ws.mSurface = null;
10890 leakedSurface = true;
10891 }
10892 }
10893 }
Romain Guy06882f82009-06-10 13:36:04 -070010894
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010895 boolean killedApps = false;
10896 if (!leakedSurface) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010897 Slog.w(TAG, "No leaked surfaces; killing applicatons!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010898 SparseIntArray pidCandidates = new SparseIntArray();
10899 for (int i=0; i<N; i++) {
10900 WindowState ws = (WindowState)mWindows.get(i);
10901 if (ws.mSurface != null) {
10902 pidCandidates.append(ws.mSession.mPid, ws.mSession.mPid);
10903 }
10904 }
10905 if (pidCandidates.size() > 0) {
10906 int[] pids = new int[pidCandidates.size()];
10907 for (int i=0; i<pids.length; i++) {
10908 pids[i] = pidCandidates.keyAt(i);
10909 }
10910 try {
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -070010911 if (mActivityManager.killPids(pids, "Free memory")) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010912 killedApps = true;
10913 }
10914 } catch (RemoteException e) {
10915 }
10916 }
10917 }
Romain Guy06882f82009-06-10 13:36:04 -070010918
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010919 if (leakedSurface || killedApps) {
10920 // We managed to reclaim some memory, so get rid of the trouble
10921 // surface and ask the app to request another one.
Joe Onorato8a9b2202010-02-26 18:56:32 -080010922 Slog.w(TAG, "Looks like we have reclaimed some memory, clearing surface for retry.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010923 if (surface != null) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010924 surface.destroy();
Dianne Hackborn16064f92010-03-25 00:47:24 -070010925 win.mSurfaceShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010926 win.mSurface = null;
10927 }
Romain Guy06882f82009-06-10 13:36:04 -070010928
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010929 try {
10930 win.mClient.dispatchGetNewSurface();
10931 } catch (RemoteException e) {
10932 }
10933 }
10934 } finally {
10935 Binder.restoreCallingIdentity(callingIdentity);
10936 }
10937 }
Romain Guy06882f82009-06-10 13:36:04 -070010938
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010939 private boolean updateFocusedWindowLocked(int mode) {
10940 WindowState newFocus = computeFocusedWindowLocked();
10941 if (mCurrentFocus != newFocus) {
10942 // This check makes sure that we don't already have the focus
10943 // change message pending.
10944 mH.removeMessages(H.REPORT_FOCUS_CHANGE);
10945 mH.sendEmptyMessage(H.REPORT_FOCUS_CHANGE);
Joe Onorato8a9b2202010-02-26 18:56:32 -080010946 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010947 TAG, "Changing focus from " + mCurrentFocus + " to " + newFocus);
10948 final WindowState oldFocus = mCurrentFocus;
10949 mCurrentFocus = newFocus;
10950 mLosingFocus.remove(newFocus);
Romain Guy06882f82009-06-10 13:36:04 -070010951
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010952 final WindowState imWindow = mInputMethodWindow;
10953 if (newFocus != imWindow && oldFocus != imWindow) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010954 if (moveInputMethodWindowsIfNeededLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010955 mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS &&
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010956 mode != UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
10957 mLayoutNeeded = true;
10958 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010959 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
10960 performLayoutLockedInner();
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010961 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
10962 // Client will do the layout, but we need to assign layers
10963 // for handleNewWindowLocked() below.
10964 assignLayersLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010965 }
10966 }
Romain Guy06882f82009-06-10 13:36:04 -070010967
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010968 if (newFocus != null && mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
10969 mKeyWaiter.handleNewWindowLocked(newFocus);
10970 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010971 return true;
10972 }
10973 return false;
10974 }
10975
10976 private WindowState computeFocusedWindowLocked() {
10977 WindowState result = null;
10978 WindowState win;
10979
10980 int i = mWindows.size() - 1;
10981 int nextAppIndex = mAppTokens.size()-1;
10982 WindowToken nextApp = nextAppIndex >= 0
10983 ? mAppTokens.get(nextAppIndex) : null;
10984
10985 while (i >= 0) {
10986 win = (WindowState)mWindows.get(i);
10987
Joe Onorato8a9b2202010-02-26 18:56:32 -080010988 if (localLOGV || DEBUG_FOCUS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010989 TAG, "Looking for focus: " + i
10990 + " = " + win
10991 + ", flags=" + win.mAttrs.flags
10992 + ", canReceive=" + win.canReceiveKeys());
10993
10994 AppWindowToken thisApp = win.mAppToken;
Romain Guy06882f82009-06-10 13:36:04 -070010995
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010996 // If this window's application has been removed, just skip it.
10997 if (thisApp != null && thisApp.removed) {
10998 i--;
10999 continue;
11000 }
Romain Guy06882f82009-06-10 13:36:04 -070011001
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011002 // If there is a focused app, don't allow focus to go to any
11003 // windows below it. If this is an application window, step
11004 // through the app tokens until we find its app.
11005 if (thisApp != null && nextApp != null && thisApp != nextApp
11006 && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
11007 int origAppIndex = nextAppIndex;
11008 while (nextAppIndex > 0) {
11009 if (nextApp == mFocusedApp) {
11010 // Whoops, we are below the focused app... no focus
11011 // for you!
Joe Onorato8a9b2202010-02-26 18:56:32 -080011012 if (localLOGV || DEBUG_FOCUS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011013 TAG, "Reached focused app: " + mFocusedApp);
11014 return null;
11015 }
11016 nextAppIndex--;
11017 nextApp = mAppTokens.get(nextAppIndex);
11018 if (nextApp == thisApp) {
11019 break;
11020 }
11021 }
11022 if (thisApp != nextApp) {
11023 // Uh oh, the app token doesn't exist! This shouldn't
11024 // happen, but if it does we can get totally hosed...
11025 // so restart at the original app.
11026 nextAppIndex = origAppIndex;
11027 nextApp = mAppTokens.get(nextAppIndex);
11028 }
11029 }
11030
11031 // Dispatch to this window if it is wants key events.
11032 if (win.canReceiveKeys()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011033 if (DEBUG_FOCUS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011034 TAG, "Found focus @ " + i + " = " + win);
11035 result = win;
11036 break;
11037 }
11038
11039 i--;
11040 }
11041
11042 return result;
11043 }
11044
11045 private void startFreezingDisplayLocked() {
11046 if (mDisplayFrozen) {
Chris Tate2ad63a92009-03-25 17:36:48 -070011047 // Freezing the display also suspends key event delivery, to
11048 // keep events from going astray while the display is reconfigured.
11049 // If someone has changed orientation again while the screen is
11050 // still frozen, the events will continue to be blocked while the
11051 // successive orientation change is processed. To prevent spurious
11052 // ANRs, we reset the event dispatch timeout in this case.
11053 synchronized (mKeyWaiter) {
11054 mKeyWaiter.mWasFrozen = true;
11055 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011056 return;
11057 }
Romain Guy06882f82009-06-10 13:36:04 -070011058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011059 mScreenFrozenLock.acquire();
Romain Guy06882f82009-06-10 13:36:04 -070011060
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011061 long now = SystemClock.uptimeMillis();
Joe Onorato8a9b2202010-02-26 18:56:32 -080011062 //Slog.i(TAG, "Freezing, gc pending: " + mFreezeGcPending + ", now " + now);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011063 if (mFreezeGcPending != 0) {
11064 if (now > (mFreezeGcPending+1000)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011065 //Slog.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011066 mH.removeMessages(H.FORCE_GC);
11067 Runtime.getRuntime().gc();
11068 mFreezeGcPending = now;
11069 }
11070 } else {
11071 mFreezeGcPending = now;
11072 }
Romain Guy06882f82009-06-10 13:36:04 -070011073
Dianne Hackbornce73c1e2010-04-12 23:11:38 -070011074 if (DEBUG_FREEZE) Slog.v(TAG, "*** FREEZING DISPLAY", new RuntimeException());
11075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011076 mDisplayFrozen = true;
Dianne Hackbornbfe319e2009-09-21 00:34:05 -070011077 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
11078 mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070011079 mNextAppTransitionPackage = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011080 mAppTransitionReady = true;
11081 }
Romain Guy06882f82009-06-10 13:36:04 -070011082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011083 if (PROFILE_ORIENTATION) {
11084 File file = new File("/data/system/frozen");
11085 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
11086 }
11087 Surface.freezeDisplay(0);
11088 }
Romain Guy06882f82009-06-10 13:36:04 -070011089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011090 private void stopFreezingDisplayLocked() {
11091 if (!mDisplayFrozen) {
11092 return;
11093 }
Romain Guy06882f82009-06-10 13:36:04 -070011094
Dianne Hackborne36d6e22010-02-17 19:46:25 -080011095 if (mWaitingForConfig || mAppsFreezingScreen > 0 || mWindowsFreezingScreen) {
11096 return;
11097 }
11098
Dianne Hackbornce73c1e2010-04-12 23:11:38 -070011099 if (DEBUG_FREEZE) Slog.v(TAG, "*** UNFREEZING DISPLAY", new RuntimeException());
11100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011101 mDisplayFrozen = false;
11102 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
11103 if (PROFILE_ORIENTATION) {
11104 Debug.stopMethodTracing();
11105 }
11106 Surface.unfreezeDisplay(0);
Romain Guy06882f82009-06-10 13:36:04 -070011107
Chris Tate2ad63a92009-03-25 17:36:48 -070011108 // Reset the key delivery timeout on unfreeze, too. We force a wakeup here
11109 // too because regular key delivery processing should resume immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011110 synchronized (mKeyWaiter) {
11111 mKeyWaiter.mWasFrozen = true;
11112 mKeyWaiter.notifyAll();
11113 }
11114
Christopher Tateb696aee2010-04-02 19:08:30 -070011115 // While the display is frozen we don't re-compute the orientation
11116 // to avoid inconsistent states. However, something interesting
11117 // could have actually changed during that time so re-evaluate it
11118 // now to catch that.
11119 if (updateOrientationFromAppTokensLocked()) {
11120 mH.sendEmptyMessage(H.SEND_NEW_CONFIGURATION);
11121 }
11122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011123 // A little kludge: a lot could have happened while the
11124 // display was frozen, so now that we are coming back we
11125 // do a gc so that any remote references the system
11126 // processes holds on others can be released if they are
11127 // no longer needed.
11128 mH.removeMessages(H.FORCE_GC);
11129 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
11130 2000);
Romain Guy06882f82009-06-10 13:36:04 -070011131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011132 mScreenFrozenLock.release();
11133 }
Romain Guy06882f82009-06-10 13:36:04 -070011134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011135 @Override
11136 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
11137 if (mContext.checkCallingOrSelfPermission("android.permission.DUMP")
11138 != PackageManager.PERMISSION_GRANTED) {
11139 pw.println("Permission Denial: can't dump WindowManager from from pid="
11140 + Binder.getCallingPid()
11141 + ", uid=" + Binder.getCallingUid());
11142 return;
11143 }
Romain Guy06882f82009-06-10 13:36:04 -070011144
Dianne Hackborna2e92262010-03-02 17:19:29 -080011145 pw.println("Input State:");
11146 mQueue.dump(pw, " ");
11147 pw.println(" ");
11148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011149 synchronized(mWindowMap) {
11150 pw.println("Current Window Manager state:");
11151 for (int i=mWindows.size()-1; i>=0; i--) {
11152 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011153 pw.print(" Window #"); pw.print(i); pw.print(' ');
11154 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011155 w.dump(pw, " ");
11156 }
11157 if (mInputMethodDialogs.size() > 0) {
11158 pw.println(" ");
11159 pw.println(" Input method dialogs:");
11160 for (int i=mInputMethodDialogs.size()-1; i>=0; i--) {
11161 WindowState w = mInputMethodDialogs.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011162 pw.print(" IM Dialog #"); pw.print(i); pw.print(": "); pw.println(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011163 }
11164 }
11165 if (mPendingRemove.size() > 0) {
11166 pw.println(" ");
11167 pw.println(" Remove pending for:");
11168 for (int i=mPendingRemove.size()-1; i>=0; i--) {
11169 WindowState w = mPendingRemove.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011170 pw.print(" Remove #"); pw.print(i); pw.print(' ');
11171 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011172 w.dump(pw, " ");
11173 }
11174 }
11175 if (mForceRemoves != null && mForceRemoves.size() > 0) {
11176 pw.println(" ");
11177 pw.println(" Windows force removing:");
11178 for (int i=mForceRemoves.size()-1; i>=0; i--) {
11179 WindowState w = mForceRemoves.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011180 pw.print(" Removing #"); pw.print(i); pw.print(' ');
11181 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011182 w.dump(pw, " ");
11183 }
11184 }
11185 if (mDestroySurface.size() > 0) {
11186 pw.println(" ");
11187 pw.println(" Windows waiting to destroy their surface:");
11188 for (int i=mDestroySurface.size()-1; i>=0; i--) {
11189 WindowState w = mDestroySurface.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011190 pw.print(" Destroy #"); pw.print(i); pw.print(' ');
11191 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011192 w.dump(pw, " ");
11193 }
11194 }
11195 if (mLosingFocus.size() > 0) {
11196 pw.println(" ");
11197 pw.println(" Windows losing focus:");
11198 for (int i=mLosingFocus.size()-1; i>=0; i--) {
11199 WindowState w = mLosingFocus.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011200 pw.print(" Losing #"); pw.print(i); pw.print(' ');
11201 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011202 w.dump(pw, " ");
11203 }
11204 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070011205 if (mResizingWindows.size() > 0) {
11206 pw.println(" ");
11207 pw.println(" Windows waiting to resize:");
11208 for (int i=mResizingWindows.size()-1; i>=0; i--) {
11209 WindowState w = mResizingWindows.get(i);
11210 pw.print(" Resizing #"); pw.print(i); pw.print(' ');
11211 pw.print(w); pw.println(":");
11212 w.dump(pw, " ");
11213 }
11214 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011215 if (mSessions.size() > 0) {
11216 pw.println(" ");
11217 pw.println(" All active sessions:");
11218 Iterator<Session> it = mSessions.iterator();
11219 while (it.hasNext()) {
11220 Session s = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011221 pw.print(" Session "); pw.print(s); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011222 s.dump(pw, " ");
11223 }
11224 }
11225 if (mTokenMap.size() > 0) {
11226 pw.println(" ");
11227 pw.println(" All tokens:");
11228 Iterator<WindowToken> it = mTokenMap.values().iterator();
11229 while (it.hasNext()) {
11230 WindowToken token = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011231 pw.print(" Token "); pw.print(token.token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011232 token.dump(pw, " ");
11233 }
11234 }
11235 if (mTokenList.size() > 0) {
11236 pw.println(" ");
11237 pw.println(" Window token list:");
11238 for (int i=0; i<mTokenList.size(); i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011239 pw.print(" #"); pw.print(i); pw.print(": ");
11240 pw.println(mTokenList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011241 }
11242 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070011243 if (mWallpaperTokens.size() > 0) {
11244 pw.println(" ");
11245 pw.println(" Wallpaper tokens:");
11246 for (int i=mWallpaperTokens.size()-1; i>=0; i--) {
11247 WindowToken token = mWallpaperTokens.get(i);
11248 pw.print(" Wallpaper #"); pw.print(i);
11249 pw.print(' '); pw.print(token); pw.println(':');
11250 token.dump(pw, " ");
11251 }
11252 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011253 if (mAppTokens.size() > 0) {
11254 pw.println(" ");
11255 pw.println(" Application tokens in Z order:");
11256 for (int i=mAppTokens.size()-1; i>=0; i--) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011257 pw.print(" App #"); pw.print(i); pw.print(": ");
11258 pw.println(mAppTokens.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011259 }
11260 }
11261 if (mFinishedStarting.size() > 0) {
11262 pw.println(" ");
11263 pw.println(" Finishing start of application tokens:");
11264 for (int i=mFinishedStarting.size()-1; i>=0; i--) {
11265 WindowToken token = mFinishedStarting.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011266 pw.print(" Finished Starting #"); pw.print(i);
11267 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011268 token.dump(pw, " ");
11269 }
11270 }
11271 if (mExitingTokens.size() > 0) {
11272 pw.println(" ");
11273 pw.println(" Exiting tokens:");
11274 for (int i=mExitingTokens.size()-1; i>=0; i--) {
11275 WindowToken token = mExitingTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011276 pw.print(" Exiting #"); pw.print(i);
11277 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011278 token.dump(pw, " ");
11279 }
11280 }
11281 if (mExitingAppTokens.size() > 0) {
11282 pw.println(" ");
11283 pw.println(" Exiting application tokens:");
11284 for (int i=mExitingAppTokens.size()-1; i>=0; i--) {
11285 WindowToken token = mExitingAppTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011286 pw.print(" Exiting App #"); pw.print(i);
11287 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011288 token.dump(pw, " ");
11289 }
11290 }
11291 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011292 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
11293 pw.print(" mLastFocus="); pw.println(mLastFocus);
11294 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
11295 pw.print(" mInputMethodTarget="); pw.println(mInputMethodTarget);
11296 pw.print(" mInputMethodWindow="); pw.println(mInputMethodWindow);
Dianne Hackbornf21adf62009-08-13 10:20:21 -070011297 pw.print(" mWallpaperTarget="); pw.println(mWallpaperTarget);
Dianne Hackborn284ac932009-08-28 10:34:25 -070011298 if (mLowerWallpaperTarget != null && mUpperWallpaperTarget != null) {
11299 pw.print(" mLowerWallpaperTarget="); pw.println(mLowerWallpaperTarget);
11300 pw.print(" mUpperWallpaperTarget="); pw.println(mUpperWallpaperTarget);
11301 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -080011302 pw.print(" mCurConfiguration="); pw.println(this.mCurConfiguration);
11303 pw.print(" mInTouchMode="); pw.print(mInTouchMode);
11304 pw.print(" mLayoutSeq="); pw.println(mLayoutSeq);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011305 pw.print(" mSystemBooted="); pw.print(mSystemBooted);
11306 pw.print(" mDisplayEnabled="); pw.println(mDisplayEnabled);
11307 pw.print(" mLayoutNeeded="); pw.print(mLayoutNeeded);
11308 pw.print(" mBlurShown="); pw.println(mBlurShown);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011309 if (mDimAnimator != null) {
11310 mDimAnimator.printTo(pw);
11311 } else {
Dianne Hackborna2e92262010-03-02 17:19:29 -080011312 pw.println( " no DimAnimator ");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011313 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011314 pw.print(" mInputMethodAnimLayerAdjustment=");
Dianne Hackborn759a39e2009-08-09 17:20:27 -070011315 pw.print(mInputMethodAnimLayerAdjustment);
11316 pw.print(" mWallpaperAnimLayerAdjustment=");
11317 pw.println(mWallpaperAnimLayerAdjustment);
Dianne Hackborn284ac932009-08-28 10:34:25 -070011318 pw.print(" mLastWallpaperX="); pw.print(mLastWallpaperX);
11319 pw.print(" mLastWallpaperY="); pw.println(mLastWallpaperY);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011320 pw.print(" mDisplayFrozen="); pw.print(mDisplayFrozen);
11321 pw.print(" mWindowsFreezingScreen="); pw.print(mWindowsFreezingScreen);
Dianne Hackborne36d6e22010-02-17 19:46:25 -080011322 pw.print(" mAppsFreezingScreen="); pw.print(mAppsFreezingScreen);
11323 pw.print(" mWaitingForConfig="); pw.println(mWaitingForConfig);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011324 pw.print(" mRotation="); pw.print(mRotation);
11325 pw.print(", mForcedAppOrientation="); pw.print(mForcedAppOrientation);
11326 pw.print(", mRequestedRotation="); pw.println(mRequestedRotation);
11327 pw.print(" mAnimationPending="); pw.print(mAnimationPending);
11328 pw.print(" mWindowAnimationScale="); pw.print(mWindowAnimationScale);
11329 pw.print(" mTransitionWindowAnimationScale="); pw.println(mTransitionAnimationScale);
11330 pw.print(" mNextAppTransition=0x");
11331 pw.print(Integer.toHexString(mNextAppTransition));
11332 pw.print(", mAppTransitionReady="); pw.print(mAppTransitionReady);
Dianne Hackborna8f60182009-09-01 19:01:50 -070011333 pw.print(", mAppTransitionRunning="); pw.print(mAppTransitionRunning);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011334 pw.print(", mAppTransitionTimeout="); pw.println( mAppTransitionTimeout);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070011335 if (mNextAppTransitionPackage != null) {
11336 pw.print(" mNextAppTransitionPackage=");
11337 pw.print(mNextAppTransitionPackage);
11338 pw.print(", mNextAppTransitionEnter=0x");
11339 pw.print(Integer.toHexString(mNextAppTransitionEnter));
11340 pw.print(", mNextAppTransitionExit=0x");
11341 pw.print(Integer.toHexString(mNextAppTransitionExit));
11342 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011343 pw.print(" mStartingIconInTransition="); pw.print(mStartingIconInTransition);
11344 pw.print(", mSkipAppTransitionAnimation="); pw.println(mSkipAppTransitionAnimation);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070011345 if (mLastEnterAnimToken != null || mLastEnterAnimToken != null) {
11346 pw.print(" mLastEnterAnimToken="); pw.print(mLastEnterAnimToken);
11347 pw.print(", mLastEnterAnimParams="); pw.println(mLastEnterAnimParams);
11348 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011349 if (mOpeningApps.size() > 0) {
11350 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
11351 }
11352 if (mClosingApps.size() > 0) {
11353 pw.print(" mClosingApps="); pw.println(mClosingApps);
11354 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070011355 if (mToTopApps.size() > 0) {
11356 pw.print(" mToTopApps="); pw.println(mToTopApps);
11357 }
11358 if (mToBottomApps.size() > 0) {
11359 pw.print(" mToBottomApps="); pw.println(mToBottomApps);
11360 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011361 pw.print(" DisplayWidth="); pw.print(mDisplay.getWidth());
11362 pw.print(" DisplayHeight="); pw.println(mDisplay.getHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011363 pw.println(" KeyWaiter state:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011364 pw.print(" mLastWin="); pw.print(mKeyWaiter.mLastWin);
11365 pw.print(" mLastBinder="); pw.println(mKeyWaiter.mLastBinder);
11366 pw.print(" mFinished="); pw.print(mKeyWaiter.mFinished);
11367 pw.print(" mGotFirstWindow="); pw.print(mKeyWaiter.mGotFirstWindow);
11368 pw.print(" mEventDispatching="); pw.print(mKeyWaiter.mEventDispatching);
11369 pw.print(" mTimeToSwitch="); pw.println(mKeyWaiter.mTimeToSwitch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011370 }
11371 }
11372
11373 public void monitor() {
11374 synchronized (mWindowMap) { }
Mike Lockwood983ee092009-11-22 01:42:24 -050011375 synchronized (mKeyguardTokenWatcher) { }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011376 synchronized (mKeyWaiter) { }
11377 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011378
Dianne Hackbornddca3ee2009-07-23 19:01:31 -070011379 public void virtualKeyFeedback(KeyEvent event) {
11380 mPolicy.keyFeedbackFromInput(event);
11381 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080011382
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011383 /**
11384 * DimAnimator class that controls the dim animation. This holds the surface and
Doug Zongkerab5c49c2009-12-04 10:31:43 -080011385 * all state used for dim animation.
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011386 */
11387 private static class DimAnimator {
11388 Surface mDimSurface;
11389 boolean mDimShown = false;
11390 float mDimCurrentAlpha;
11391 float mDimTargetAlpha;
11392 float mDimDeltaPerMs;
11393 long mLastDimAnimTime;
Dianne Hackbornf83c5552010-03-31 22:19:32 -070011394
11395 int mLastDimWidth, mLastDimHeight;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011396
11397 DimAnimator (SurfaceSession session) {
11398 if (mDimSurface == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011399 if (SHOW_TRANSACTIONS) Slog.i(TAG, " DIM "
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011400 + mDimSurface + ": CREATE");
11401 try {
Mathias Agopian5d26c1e2010-03-01 16:09:43 -080011402 mDimSurface = new Surface(session, 0,
11403 "DimSurface",
11404 -1, 16, 16, PixelFormat.OPAQUE,
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011405 Surface.FX_SURFACE_DIM);
Maciej Białka9ee5c222010-03-24 10:25:40 +010011406 mDimSurface.setAlpha(0.0f);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011407 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011408 Slog.e(TAG, "Exception creating Dim surface", e);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011409 }
11410 }
11411 }
11412
11413 /**
11414 * Show the dim surface.
11415 */
11416 void show(int dw, int dh) {
Dianne Hackborn16064f92010-03-25 00:47:24 -070011417 if (!mDimShown) {
11418 if (SHOW_TRANSACTIONS) Slog.i(TAG, " DIM " + mDimSurface + ": SHOW pos=(0,0) (" +
11419 dw + "x" + dh + ")");
11420 mDimShown = true;
11421 try {
Dianne Hackbornf83c5552010-03-31 22:19:32 -070011422 mLastDimWidth = dw;
11423 mLastDimHeight = dh;
Dianne Hackborn16064f92010-03-25 00:47:24 -070011424 mDimSurface.setPosition(0, 0);
11425 mDimSurface.setSize(dw, dh);
11426 mDimSurface.show();
11427 } catch (RuntimeException e) {
11428 Slog.w(TAG, "Failure showing dim surface", e);
11429 }
Dianne Hackbornf83c5552010-03-31 22:19:32 -070011430 } else if (mLastDimWidth != dw || mLastDimHeight != dh) {
11431 mLastDimWidth = dw;
11432 mLastDimHeight = dh;
11433 mDimSurface.setSize(dw, dh);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011434 }
11435 }
11436
11437 /**
11438 * Set's the dim surface's layer and update dim parameters that will be used in
11439 * {@link updateSurface} after all windows are examined.
11440 */
11441 void updateParameters(WindowState w, long currentTime) {
11442 mDimSurface.setLayer(w.mAnimLayer-1);
11443
11444 final float target = w.mExiting ? 0 : w.mAttrs.dimAmount;
Joe Onorato8a9b2202010-02-26 18:56:32 -080011445 if (SHOW_TRANSACTIONS) Slog.i(TAG, " DIM " + mDimSurface
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070011446 + ": layer=" + (w.mAnimLayer-1) + " target=" + target);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011447 if (mDimTargetAlpha != target) {
11448 // If the desired dim level has changed, then
11449 // start an animation to it.
11450 mLastDimAnimTime = currentTime;
11451 long duration = (w.mAnimating && w.mAnimation != null)
11452 ? w.mAnimation.computeDurationHint()
11453 : DEFAULT_DIM_DURATION;
11454 if (target > mDimTargetAlpha) {
11455 // This is happening behind the activity UI,
11456 // so we can make it run a little longer to
11457 // give a stronger impression without disrupting
11458 // the user.
11459 duration *= DIM_DURATION_MULTIPLIER;
11460 }
11461 if (duration < 1) {
11462 // Don't divide by zero
11463 duration = 1;
11464 }
11465 mDimTargetAlpha = target;
11466 mDimDeltaPerMs = (mDimTargetAlpha-mDimCurrentAlpha) / duration;
11467 }
11468 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080011469
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011470 /**
11471 * Updating the surface's alpha. Returns true if the animation continues, or returns
11472 * false when the animation is finished and the dim surface is hidden.
11473 */
11474 boolean updateSurface(boolean dimming, long currentTime, boolean displayFrozen) {
11475 if (!dimming) {
11476 if (mDimTargetAlpha != 0) {
11477 mLastDimAnimTime = currentTime;
11478 mDimTargetAlpha = 0;
11479 mDimDeltaPerMs = (-mDimCurrentAlpha) / DEFAULT_DIM_DURATION;
11480 }
11481 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080011482
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011483 boolean animating = false;
11484 if (mLastDimAnimTime != 0) {
11485 mDimCurrentAlpha += mDimDeltaPerMs
11486 * (currentTime-mLastDimAnimTime);
11487 boolean more = true;
11488 if (displayFrozen) {
11489 // If the display is frozen, there is no reason to animate.
11490 more = false;
11491 } else if (mDimDeltaPerMs > 0) {
11492 if (mDimCurrentAlpha > mDimTargetAlpha) {
11493 more = false;
11494 }
11495 } else if (mDimDeltaPerMs < 0) {
11496 if (mDimCurrentAlpha < mDimTargetAlpha) {
11497 more = false;
11498 }
11499 } else {
11500 more = false;
11501 }
11502
11503 // Do we need to continue animating?
11504 if (more) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011505 if (SHOW_TRANSACTIONS) Slog.i(TAG, " DIM "
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011506 + mDimSurface + ": alpha=" + mDimCurrentAlpha);
11507 mLastDimAnimTime = currentTime;
11508 mDimSurface.setAlpha(mDimCurrentAlpha);
11509 animating = true;
11510 } else {
11511 mDimCurrentAlpha = mDimTargetAlpha;
11512 mLastDimAnimTime = 0;
Joe Onorato8a9b2202010-02-26 18:56:32 -080011513 if (SHOW_TRANSACTIONS) Slog.i(TAG, " DIM "
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011514 + mDimSurface + ": final alpha=" + mDimCurrentAlpha);
11515 mDimSurface.setAlpha(mDimCurrentAlpha);
11516 if (!dimming) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011517 if (SHOW_TRANSACTIONS) Slog.i(TAG, " DIM " + mDimSurface
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011518 + ": HIDE");
11519 try {
11520 mDimSurface.hide();
11521 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011522 Slog.w(TAG, "Illegal argument exception hiding dim surface");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011523 }
11524 mDimShown = false;
11525 }
11526 }
11527 }
11528 return animating;
11529 }
11530
11531 public void printTo(PrintWriter pw) {
11532 pw.print(" mDimShown="); pw.print(mDimShown);
11533 pw.print(" current="); pw.print(mDimCurrentAlpha);
11534 pw.print(" target="); pw.print(mDimTargetAlpha);
11535 pw.print(" delta="); pw.print(mDimDeltaPerMs);
11536 pw.print(" lastAnimTime="); pw.println(mLastDimAnimTime);
11537 }
11538 }
11539
11540 /**
11541 * Animation that fade in after 0.5 interpolate time, or fade out in reverse order.
11542 * This is used for opening/closing transition for apps in compatible mode.
11543 */
11544 private static class FadeInOutAnimation extends Animation {
11545 int mWidth;
11546 boolean mFadeIn;
11547
11548 public FadeInOutAnimation(boolean fadeIn) {
11549 setInterpolator(new AccelerateInterpolator());
11550 setDuration(DEFAULT_FADE_IN_OUT_DURATION);
11551 mFadeIn = fadeIn;
11552 }
11553
11554 @Override
11555 protected void applyTransformation(float interpolatedTime, Transformation t) {
11556 float x = interpolatedTime;
11557 if (!mFadeIn) {
11558 x = 1.0f - x; // reverse the interpolation for fade out
11559 }
11560 if (x < 0.5) {
11561 // move the window out of the screen.
11562 t.getMatrix().setTranslate(mWidth, 0);
11563 } else {
11564 t.getMatrix().setTranslate(0, 0);// show
11565 t.setAlpha((x - 0.5f) * 2);
11566 }
11567 }
11568
11569 @Override
11570 public void initialize(int width, int height, int parentWidth, int parentHeight) {
11571 // width is the screen width {@see AppWindowToken#stepAnimatinoLocked}
11572 mWidth = width;
11573 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011574
11575 @Override
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -070011576 public int getZAdjustment() {
11577 return Animation.ZORDER_TOP;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011578 }
11579 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011580}