blob: ac5e3f191ec5e407e2377737ae8c853b7f2402c1 [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();
Romain Guy06882f82009-06-10 13:36:04 -0700670
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 // Add ourself to the Watchdog monitors.
672 Watchdog.getInstance().addMonitor(this);
673 }
674
675 @Override
676 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
677 throws RemoteException {
678 try {
679 return super.onTransact(code, data, reply, flags);
680 } catch (RuntimeException e) {
681 // The window manager only throws security exceptions, so let's
682 // log all others.
683 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800684 Slog.e(TAG, "Window Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800685 }
686 throw e;
687 }
688 }
689
690 private void placeWindowAfter(Object pos, WindowState window) {
691 final int i = mWindows.indexOf(pos);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800692 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693 TAG, "Adding window " + window + " at "
694 + (i+1) + " of " + mWindows.size() + " (after " + pos + ")");
695 mWindows.add(i+1, window);
696 }
697
698 private void placeWindowBefore(Object pos, WindowState window) {
699 final int i = mWindows.indexOf(pos);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800700 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800701 TAG, "Adding window " + window + " at "
702 + i + " of " + mWindows.size() + " (before " + pos + ")");
703 mWindows.add(i, window);
704 }
705
706 //This method finds out the index of a window that has the same app token as
707 //win. used for z ordering the windows in mWindows
708 private int findIdxBasedOnAppTokens(WindowState win) {
709 //use a local variable to cache mWindows
710 ArrayList localmWindows = mWindows;
711 int jmax = localmWindows.size();
712 if(jmax == 0) {
713 return -1;
714 }
715 for(int j = (jmax-1); j >= 0; j--) {
716 WindowState wentry = (WindowState)localmWindows.get(j);
717 if(wentry.mAppToken == win.mAppToken) {
718 return j;
719 }
720 }
721 return -1;
722 }
Romain Guy06882f82009-06-10 13:36:04 -0700723
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724 private void addWindowToListInOrderLocked(WindowState win, boolean addToToken) {
725 final IWindow client = win.mClient;
726 final WindowToken token = win.mToken;
727 final ArrayList localmWindows = mWindows;
Romain Guy06882f82009-06-10 13:36:04 -0700728
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800729 final int N = localmWindows.size();
730 final WindowState attached = win.mAttachedWindow;
731 int i;
732 if (attached == null) {
733 int tokenWindowsPos = token.windows.size();
734 if (token.appWindowToken != null) {
735 int index = tokenWindowsPos-1;
736 if (index >= 0) {
737 // If this application has existing windows, we
738 // simply place the new window on top of them... but
739 // keep the starting window on top.
740 if (win.mAttrs.type == TYPE_BASE_APPLICATION) {
741 // Base windows go behind everything else.
742 placeWindowBefore(token.windows.get(0), win);
743 tokenWindowsPos = 0;
744 } else {
745 AppWindowToken atoken = win.mAppToken;
746 if (atoken != null &&
747 token.windows.get(index) == atoken.startingWindow) {
748 placeWindowBefore(token.windows.get(index), win);
749 tokenWindowsPos--;
750 } else {
751 int newIdx = findIdxBasedOnAppTokens(win);
752 if(newIdx != -1) {
Romain Guy06882f82009-06-10 13:36:04 -0700753 //there is a window above this one associated with the same
754 //apptoken note that the window could be a floating window
755 //that was created later or a window at the top of the list of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 //windows associated with this token.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800757 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Slog.v(
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700758 TAG, "Adding window " + win + " at "
759 + (newIdx+1) + " of " + N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760 localmWindows.add(newIdx+1, win);
Romain Guy06882f82009-06-10 13:36:04 -0700761 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800762 }
763 }
764 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800765 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800766 TAG, "Figuring out where to add app window "
767 + client.asBinder() + " (token=" + token + ")");
768 // Figure out where the window should go, based on the
769 // order of applications.
770 final int NA = mAppTokens.size();
771 Object pos = null;
772 for (i=NA-1; i>=0; i--) {
773 AppWindowToken t = mAppTokens.get(i);
774 if (t == token) {
775 i--;
776 break;
777 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800778
Dianne Hackborna8f60182009-09-01 19:01:50 -0700779 // We haven't reached the token yet; if this token
780 // is not going to the bottom and has windows, we can
781 // use it as an anchor for when we do reach the token.
782 if (!t.sendingToBottom && t.windows.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783 pos = t.windows.get(0);
784 }
785 }
786 // We now know the index into the apps. If we found
787 // an app window above, that gives us the position; else
788 // we need to look some more.
789 if (pos != null) {
790 // Move behind any windows attached to this one.
Romain Guy06882f82009-06-10 13:36:04 -0700791 WindowToken atoken =
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800792 mTokenMap.get(((WindowState)pos).mClient.asBinder());
793 if (atoken != null) {
794 final int NC = atoken.windows.size();
795 if (NC > 0) {
796 WindowState bottom = atoken.windows.get(0);
797 if (bottom.mSubLayer < 0) {
798 pos = bottom;
799 }
800 }
801 }
802 placeWindowBefore(pos, win);
803 } else {
Dianne Hackborna8f60182009-09-01 19:01:50 -0700804 // Continue looking down until we find the first
805 // token that has windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800806 while (i >= 0) {
807 AppWindowToken t = mAppTokens.get(i);
808 final int NW = t.windows.size();
809 if (NW > 0) {
810 pos = t.windows.get(NW-1);
811 break;
812 }
813 i--;
814 }
815 if (pos != null) {
816 // Move in front of any windows attached to this
817 // one.
818 WindowToken atoken =
819 mTokenMap.get(((WindowState)pos).mClient.asBinder());
820 if (atoken != null) {
821 final int NC = atoken.windows.size();
822 if (NC > 0) {
823 WindowState top = atoken.windows.get(NC-1);
824 if (top.mSubLayer >= 0) {
825 pos = top;
826 }
827 }
828 }
829 placeWindowAfter(pos, win);
830 } else {
831 // Just search for the start of this layer.
832 final int myLayer = win.mBaseLayer;
833 for (i=0; i<N; i++) {
834 WindowState w = (WindowState)localmWindows.get(i);
835 if (w.mBaseLayer > myLayer) {
836 break;
837 }
838 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800839 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Slog.v(
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700840 TAG, "Adding window " + win + " at "
841 + i + " of " + N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800842 localmWindows.add(i, win);
843 }
844 }
845 }
846 } else {
847 // Figure out where window should go, based on layer.
848 final int myLayer = win.mBaseLayer;
849 for (i=N-1; i>=0; i--) {
850 if (((WindowState)localmWindows.get(i)).mBaseLayer <= myLayer) {
851 i++;
852 break;
853 }
854 }
855 if (i < 0) i = 0;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800856 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Slog.v(
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700857 TAG, "Adding window " + win + " at "
858 + i + " of " + N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800859 localmWindows.add(i, win);
860 }
861 if (addToToken) {
862 token.windows.add(tokenWindowsPos, win);
863 }
864
865 } else {
866 // Figure out this window's ordering relative to the window
867 // it is attached to.
868 final int NA = token.windows.size();
869 final int sublayer = win.mSubLayer;
870 int largestSublayer = Integer.MIN_VALUE;
871 WindowState windowWithLargestSublayer = null;
872 for (i=0; i<NA; i++) {
873 WindowState w = token.windows.get(i);
874 final int wSublayer = w.mSubLayer;
875 if (wSublayer >= largestSublayer) {
876 largestSublayer = wSublayer;
877 windowWithLargestSublayer = w;
878 }
879 if (sublayer < 0) {
880 // For negative sublayers, we go below all windows
881 // in the same sublayer.
882 if (wSublayer >= sublayer) {
883 if (addToToken) {
884 token.windows.add(i, win);
885 }
886 placeWindowBefore(
887 wSublayer >= 0 ? attached : w, win);
888 break;
889 }
890 } else {
891 // For positive sublayers, we go above all windows
892 // in the same sublayer.
893 if (wSublayer > sublayer) {
894 if (addToToken) {
895 token.windows.add(i, win);
896 }
897 placeWindowBefore(w, win);
898 break;
899 }
900 }
901 }
902 if (i >= NA) {
903 if (addToToken) {
904 token.windows.add(win);
905 }
906 if (sublayer < 0) {
907 placeWindowBefore(attached, win);
908 } else {
909 placeWindowAfter(largestSublayer >= 0
910 ? windowWithLargestSublayer
911 : attached,
912 win);
913 }
914 }
915 }
Romain Guy06882f82009-06-10 13:36:04 -0700916
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 if (win.mAppToken != null && addToToken) {
918 win.mAppToken.allAppWindows.add(win);
919 }
920 }
Romain Guy06882f82009-06-10 13:36:04 -0700921
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800922 static boolean canBeImeTarget(WindowState w) {
923 final int fl = w.mAttrs.flags
924 & (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM);
925 if (fl == 0 || fl == (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM)) {
926 return w.isVisibleOrAdding();
927 }
928 return false;
929 }
Romain Guy06882f82009-06-10 13:36:04 -0700930
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800931 int findDesiredInputMethodWindowIndexLocked(boolean willMove) {
932 final ArrayList localmWindows = mWindows;
933 final int N = localmWindows.size();
934 WindowState w = null;
935 int i = N;
936 while (i > 0) {
937 i--;
938 w = (WindowState)localmWindows.get(i);
Romain Guy06882f82009-06-10 13:36:04 -0700939
Joe Onorato8a9b2202010-02-26 18:56:32 -0800940 //Slog.i(TAG, "Checking window @" + i + " " + w + " fl=0x"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800941 // + Integer.toHexString(w.mAttrs.flags));
942 if (canBeImeTarget(w)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800943 //Slog.i(TAG, "Putting input method here!");
Romain Guy06882f82009-06-10 13:36:04 -0700944
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800945 // Yet more tricksyness! If this window is a "starting"
946 // window, we do actually want to be on top of it, but
947 // it is not -really- where input will go. So if the caller
948 // is not actually looking to move the IME, look down below
949 // for a real window to target...
950 if (!willMove
951 && w.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
952 && i > 0) {
953 WindowState wb = (WindowState)localmWindows.get(i-1);
954 if (wb.mAppToken == w.mAppToken && canBeImeTarget(wb)) {
955 i--;
956 w = wb;
957 }
958 }
959 break;
960 }
961 }
Romain Guy06882f82009-06-10 13:36:04 -0700962
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800963 mUpcomingInputMethodTarget = w;
Romain Guy06882f82009-06-10 13:36:04 -0700964
Joe Onorato8a9b2202010-02-26 18:56:32 -0800965 if (DEBUG_INPUT_METHOD) Slog.v(TAG, "Desired input method target="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800966 + w + " willMove=" + willMove);
Romain Guy06882f82009-06-10 13:36:04 -0700967
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800968 if (willMove && w != null) {
969 final WindowState curTarget = mInputMethodTarget;
970 if (curTarget != null && curTarget.mAppToken != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700971
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800972 // Now some fun for dealing with window animations that
973 // modify the Z order. We need to look at all windows below
974 // the current target that are in this app, finding the highest
975 // visible one in layering.
976 AppWindowToken token = curTarget.mAppToken;
977 WindowState highestTarget = null;
978 int highestPos = 0;
979 if (token.animating || token.animation != null) {
980 int pos = 0;
981 pos = localmWindows.indexOf(curTarget);
982 while (pos >= 0) {
983 WindowState win = (WindowState)localmWindows.get(pos);
984 if (win.mAppToken != token) {
985 break;
986 }
987 if (!win.mRemoved) {
988 if (highestTarget == null || win.mAnimLayer >
989 highestTarget.mAnimLayer) {
990 highestTarget = win;
991 highestPos = pos;
992 }
993 }
994 pos--;
995 }
996 }
Romain Guy06882f82009-06-10 13:36:04 -0700997
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800998 if (highestTarget != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800999 if (DEBUG_INPUT_METHOD) Slog.v(TAG, "mNextAppTransition="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001000 + mNextAppTransition + " " + highestTarget
1001 + " animating=" + highestTarget.isAnimating()
1002 + " layer=" + highestTarget.mAnimLayer
1003 + " new layer=" + w.mAnimLayer);
Romain Guy06882f82009-06-10 13:36:04 -07001004
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07001005 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006 // If we are currently setting up for an animation,
1007 // hold everything until we can find out what will happen.
1008 mInputMethodTargetWaitingAnim = true;
1009 mInputMethodTarget = highestTarget;
1010 return highestPos + 1;
1011 } else if (highestTarget.isAnimating() &&
1012 highestTarget.mAnimLayer > w.mAnimLayer) {
1013 // If the window we are currently targeting is involved
1014 // with an animation, and it is on top of the next target
1015 // we will be over, then hold off on moving until
1016 // that is done.
1017 mInputMethodTarget = highestTarget;
1018 return highestPos + 1;
1019 }
1020 }
1021 }
1022 }
Romain Guy06882f82009-06-10 13:36:04 -07001023
Joe Onorato8a9b2202010-02-26 18:56:32 -08001024 //Slog.i(TAG, "Placing input method @" + (i+1));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 if (w != null) {
1026 if (willMove) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08001027 if (DEBUG_INPUT_METHOD) {
1028 RuntimeException e = null;
1029 if (!HIDE_STACK_CRAWLS) {
1030 e = new RuntimeException();
1031 e.fillInStackTrace();
1032 }
1033 Slog.w(TAG, "Moving IM target from "
1034 + mInputMethodTarget + " to " + w, e);
1035 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036 mInputMethodTarget = w;
1037 if (w.mAppToken != null) {
1038 setInputMethodAnimLayerAdjustment(w.mAppToken.animLayerAdjustment);
1039 } else {
1040 setInputMethodAnimLayerAdjustment(0);
1041 }
1042 }
1043 return i+1;
1044 }
1045 if (willMove) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08001046 if (DEBUG_INPUT_METHOD) {
1047 RuntimeException e = null;
1048 if (!HIDE_STACK_CRAWLS) {
1049 e = new RuntimeException();
1050 e.fillInStackTrace();
1051 }
1052 Slog.w(TAG, "Moving IM target from "
1053 + mInputMethodTarget + " to null", e);
1054 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055 mInputMethodTarget = null;
1056 setInputMethodAnimLayerAdjustment(0);
1057 }
1058 return -1;
1059 }
Romain Guy06882f82009-06-10 13:36:04 -07001060
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001061 void addInputMethodWindowToListLocked(WindowState win) {
1062 int pos = findDesiredInputMethodWindowIndexLocked(true);
1063 if (pos >= 0) {
1064 win.mTargetAppToken = mInputMethodTarget.mAppToken;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001065 if (DEBUG_WINDOW_MOVEMENT) Slog.v(
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001066 TAG, "Adding input method window " + win + " at " + pos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001067 mWindows.add(pos, win);
1068 moveInputMethodDialogsLocked(pos+1);
1069 return;
1070 }
1071 win.mTargetAppToken = null;
1072 addWindowToListInOrderLocked(win, true);
1073 moveInputMethodDialogsLocked(pos);
1074 }
Romain Guy06882f82009-06-10 13:36:04 -07001075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076 void setInputMethodAnimLayerAdjustment(int adj) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001077 if (DEBUG_LAYERS) Slog.v(TAG, "Setting im layer adj to " + adj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078 mInputMethodAnimLayerAdjustment = adj;
1079 WindowState imw = mInputMethodWindow;
1080 if (imw != null) {
1081 imw.mAnimLayer = imw.mLayer + adj;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001082 if (DEBUG_LAYERS) Slog.v(TAG, "IM win " + imw
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 + " anim layer: " + imw.mAnimLayer);
1084 int wi = imw.mChildWindows.size();
1085 while (wi > 0) {
1086 wi--;
1087 WindowState cw = (WindowState)imw.mChildWindows.get(wi);
1088 cw.mAnimLayer = cw.mLayer + adj;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001089 if (DEBUG_LAYERS) Slog.v(TAG, "IM win " + cw
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001090 + " anim layer: " + cw.mAnimLayer);
1091 }
1092 }
1093 int di = mInputMethodDialogs.size();
1094 while (di > 0) {
1095 di --;
1096 imw = mInputMethodDialogs.get(di);
1097 imw.mAnimLayer = imw.mLayer + adj;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001098 if (DEBUG_LAYERS) Slog.v(TAG, "IM win " + imw
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099 + " anim layer: " + imw.mAnimLayer);
1100 }
1101 }
Romain Guy06882f82009-06-10 13:36:04 -07001102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103 private int tmpRemoveWindowLocked(int interestingPos, WindowState win) {
1104 int wpos = mWindows.indexOf(win);
1105 if (wpos >= 0) {
1106 if (wpos < interestingPos) interestingPos--;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001107 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "Temp removing at " + wpos + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 mWindows.remove(wpos);
1109 int NC = win.mChildWindows.size();
1110 while (NC > 0) {
1111 NC--;
1112 WindowState cw = (WindowState)win.mChildWindows.get(NC);
1113 int cpos = mWindows.indexOf(cw);
1114 if (cpos >= 0) {
1115 if (cpos < interestingPos) interestingPos--;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001116 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "Temp removing child at "
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001117 + cpos + ": " + cw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001118 mWindows.remove(cpos);
1119 }
1120 }
1121 }
1122 return interestingPos;
1123 }
Romain Guy06882f82009-06-10 13:36:04 -07001124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001125 private void reAddWindowToListInOrderLocked(WindowState win) {
1126 addWindowToListInOrderLocked(win, false);
1127 // This is a hack to get all of the child windows added as well
1128 // at the right position. Child windows should be rare and
1129 // this case should be rare, so it shouldn't be that big a deal.
1130 int wpos = mWindows.indexOf(win);
1131 if (wpos >= 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001132 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "ReAdd removing from " + wpos
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001133 + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001134 mWindows.remove(wpos);
1135 reAddWindowLocked(wpos, win);
1136 }
1137 }
Romain Guy06882f82009-06-10 13:36:04 -07001138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001139 void logWindowList(String prefix) {
1140 int N = mWindows.size();
1141 while (N > 0) {
1142 N--;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001143 Slog.v(TAG, prefix + "#" + N + ": " + mWindows.get(N));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 }
1145 }
Romain Guy06882f82009-06-10 13:36:04 -07001146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147 void moveInputMethodDialogsLocked(int pos) {
1148 ArrayList<WindowState> dialogs = mInputMethodDialogs;
Romain Guy06882f82009-06-10 13:36:04 -07001149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 final int N = dialogs.size();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001151 if (DEBUG_INPUT_METHOD) Slog.v(TAG, "Removing " + N + " dialogs w/pos=" + pos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152 for (int i=0; i<N; i++) {
1153 pos = tmpRemoveWindowLocked(pos, dialogs.get(i));
1154 }
1155 if (DEBUG_INPUT_METHOD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001156 Slog.v(TAG, "Window list w/pos=" + pos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157 logWindowList(" ");
1158 }
Romain Guy06882f82009-06-10 13:36:04 -07001159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 if (pos >= 0) {
1161 final AppWindowToken targetAppToken = mInputMethodTarget.mAppToken;
1162 if (pos < mWindows.size()) {
1163 WindowState wp = (WindowState)mWindows.get(pos);
1164 if (wp == mInputMethodWindow) {
1165 pos++;
1166 }
1167 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001168 if (DEBUG_INPUT_METHOD) Slog.v(TAG, "Adding " + N + " dialogs at pos=" + pos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 for (int i=0; i<N; i++) {
1170 WindowState win = dialogs.get(i);
1171 win.mTargetAppToken = targetAppToken;
1172 pos = reAddWindowLocked(pos, win);
1173 }
1174 if (DEBUG_INPUT_METHOD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001175 Slog.v(TAG, "Final window list:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001176 logWindowList(" ");
1177 }
1178 return;
1179 }
1180 for (int i=0; i<N; i++) {
1181 WindowState win = dialogs.get(i);
1182 win.mTargetAppToken = null;
1183 reAddWindowToListInOrderLocked(win);
1184 if (DEBUG_INPUT_METHOD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001185 Slog.v(TAG, "No IM target, final list:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 logWindowList(" ");
1187 }
1188 }
1189 }
Romain Guy06882f82009-06-10 13:36:04 -07001190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 boolean moveInputMethodWindowsIfNeededLocked(boolean needAssignLayers) {
1192 final WindowState imWin = mInputMethodWindow;
1193 final int DN = mInputMethodDialogs.size();
1194 if (imWin == null && DN == 0) {
1195 return false;
1196 }
Romain Guy06882f82009-06-10 13:36:04 -07001197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 int imPos = findDesiredInputMethodWindowIndexLocked(true);
1199 if (imPos >= 0) {
1200 // In this case, the input method windows are to be placed
1201 // immediately above the window they are targeting.
Romain Guy06882f82009-06-10 13:36:04 -07001202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 // First check to see if the input method windows are already
1204 // located here, and contiguous.
1205 final int N = mWindows.size();
1206 WindowState firstImWin = imPos < N
1207 ? (WindowState)mWindows.get(imPos) : null;
Romain Guy06882f82009-06-10 13:36:04 -07001208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 // Figure out the actual input method window that should be
1210 // at the bottom of their stack.
1211 WindowState baseImWin = imWin != null
1212 ? imWin : mInputMethodDialogs.get(0);
1213 if (baseImWin.mChildWindows.size() > 0) {
1214 WindowState cw = (WindowState)baseImWin.mChildWindows.get(0);
1215 if (cw.mSubLayer < 0) baseImWin = cw;
1216 }
Romain Guy06882f82009-06-10 13:36:04 -07001217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218 if (firstImWin == baseImWin) {
1219 // The windows haven't moved... but are they still contiguous?
1220 // First find the top IM window.
1221 int pos = imPos+1;
1222 while (pos < N) {
1223 if (!((WindowState)mWindows.get(pos)).mIsImWindow) {
1224 break;
1225 }
1226 pos++;
1227 }
1228 pos++;
1229 // Now there should be no more input method windows above.
1230 while (pos < N) {
1231 if (((WindowState)mWindows.get(pos)).mIsImWindow) {
1232 break;
1233 }
1234 pos++;
1235 }
1236 if (pos >= N) {
1237 // All is good!
1238 return false;
1239 }
1240 }
Romain Guy06882f82009-06-10 13:36:04 -07001241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 if (imWin != null) {
1243 if (DEBUG_INPUT_METHOD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001244 Slog.v(TAG, "Moving IM from " + imPos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 logWindowList(" ");
1246 }
1247 imPos = tmpRemoveWindowLocked(imPos, imWin);
1248 if (DEBUG_INPUT_METHOD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001249 Slog.v(TAG, "List after moving with new pos " + imPos + ":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001250 logWindowList(" ");
1251 }
1252 imWin.mTargetAppToken = mInputMethodTarget.mAppToken;
1253 reAddWindowLocked(imPos, imWin);
1254 if (DEBUG_INPUT_METHOD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001255 Slog.v(TAG, "List after moving IM to " + imPos + ":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001256 logWindowList(" ");
1257 }
1258 if (DN > 0) moveInputMethodDialogsLocked(imPos+1);
1259 } else {
1260 moveInputMethodDialogsLocked(imPos);
1261 }
Romain Guy06882f82009-06-10 13:36:04 -07001262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 } else {
1264 // In this case, the input method windows go in a fixed layer,
1265 // because they aren't currently associated with a focus window.
Romain Guy06882f82009-06-10 13:36:04 -07001266
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001267 if (imWin != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001268 if (DEBUG_INPUT_METHOD) Slog.v(TAG, "Moving IM from " + imPos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001269 tmpRemoveWindowLocked(0, imWin);
1270 imWin.mTargetAppToken = null;
1271 reAddWindowToListInOrderLocked(imWin);
1272 if (DEBUG_INPUT_METHOD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001273 Slog.v(TAG, "List with no IM target:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 logWindowList(" ");
1275 }
1276 if (DN > 0) moveInputMethodDialogsLocked(-1);;
1277 } else {
1278 moveInputMethodDialogsLocked(-1);;
1279 }
Romain Guy06882f82009-06-10 13:36:04 -07001280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001281 }
Romain Guy06882f82009-06-10 13:36:04 -07001282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283 if (needAssignLayers) {
1284 assignLayersLocked();
1285 }
Romain Guy06882f82009-06-10 13:36:04 -07001286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001287 return true;
1288 }
Romain Guy06882f82009-06-10 13:36:04 -07001289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 void adjustInputMethodDialogsLocked() {
1291 moveInputMethodDialogsLocked(findDesiredInputMethodWindowIndexLocked(true));
1292 }
Romain Guy06882f82009-06-10 13:36:04 -07001293
Dianne Hackborn25994b42009-09-04 14:21:19 -07001294 final boolean isWallpaperVisible(WindowState wallpaperTarget) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001295 if (DEBUG_WALLPAPER) Slog.v(TAG, "Wallpaper vis: target obscured="
Dianne Hackborn25994b42009-09-04 14:21:19 -07001296 + (wallpaperTarget != null ? Boolean.toString(wallpaperTarget.mObscured) : "??")
1297 + " anim=" + ((wallpaperTarget != null && wallpaperTarget.mAppToken != null)
1298 ? wallpaperTarget.mAppToken.animation : null)
1299 + " upper=" + mUpperWallpaperTarget
1300 + " lower=" + mLowerWallpaperTarget);
1301 return (wallpaperTarget != null
1302 && (!wallpaperTarget.mObscured || (wallpaperTarget.mAppToken != null
1303 && wallpaperTarget.mAppToken.animation != null)))
1304 || mUpperWallpaperTarget != null
1305 || mLowerWallpaperTarget != null;
1306 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001307
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001308 static final int ADJUST_WALLPAPER_LAYERS_CHANGED = 1<<1;
1309 static final int ADJUST_WALLPAPER_VISIBILITY_CHANGED = 1<<2;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001310
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001311 int adjustWallpaperWindowsLocked() {
1312 int changed = 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001313
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001314 final int dw = mDisplay.getWidth();
1315 final int dh = mDisplay.getHeight();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001316
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001317 // First find top-most window that has asked to be on top of the
1318 // wallpaper; all wallpapers go behind it.
1319 final ArrayList localmWindows = mWindows;
1320 int N = localmWindows.size();
1321 WindowState w = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001322 WindowState foundW = null;
1323 int foundI = 0;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001324 WindowState topCurW = null;
1325 int topCurI = 0;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001326 int i = N;
1327 while (i > 0) {
1328 i--;
1329 w = (WindowState)localmWindows.get(i);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001330 if ((w.mAttrs.type == WindowManager.LayoutParams.TYPE_WALLPAPER)) {
1331 if (topCurW == null) {
1332 topCurW = w;
1333 topCurI = i;
1334 }
1335 continue;
1336 }
1337 topCurW = null;
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001338 if (w.mAppToken != null) {
1339 // If this window's app token is hidden and not animating,
1340 // it is of no interest to us.
1341 if (w.mAppToken.hidden && w.mAppToken.animation == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001342 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001343 "Skipping hidden or animating token: " + w);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001344 topCurW = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001345 continue;
1346 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001347 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001348 if (DEBUG_WALLPAPER) Slog.v(TAG, "Win " + w + ": readyfordisplay="
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001349 + w.isReadyForDisplay() + " drawpending=" + w.mDrawPending
1350 + " commitdrawpending=" + w.mCommitDrawPending);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001351 if ((w.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0 && w.isReadyForDisplay()
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07001352 && (mWallpaperTarget == w
1353 || (!w.mDrawPending && !w.mCommitDrawPending))) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001354 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001355 "Found wallpaper activity: #" + i + "=" + w);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001356 foundW = w;
1357 foundI = i;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001358 if (w == mWallpaperTarget && ((w.mAppToken != null
1359 && w.mAppToken.animation != null)
1360 || w.mAnimation != null)) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001361 // The current wallpaper target is animating, so we'll
1362 // look behind it for another possible target and figure
1363 // out what is going on below.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001364 if (DEBUG_WALLPAPER) Slog.v(TAG, "Win " + w
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001365 + ": token animating, looking behind.");
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001366 continue;
1367 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001368 break;
1369 }
1370 }
1371
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07001372 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001373 // If we are currently waiting for an app transition, and either
1374 // the current target or the next target are involved with it,
1375 // then hold off on doing anything with the wallpaper.
1376 // Note that we are checking here for just whether the target
1377 // is part of an app token... which is potentially overly aggressive
1378 // (the app token may not be involved in the transition), but good
1379 // enough (we'll just wait until whatever transition is pending
1380 // executes).
1381 if (mWallpaperTarget != null && mWallpaperTarget.mAppToken != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001382 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001383 "Wallpaper not changing: waiting for app anim in current target");
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001384 return 0;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001385 }
1386 if (foundW != null && foundW.mAppToken != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001387 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001388 "Wallpaper not changing: waiting for app anim in found target");
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001389 return 0;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001390 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001391 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001392
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001393 if (mWallpaperTarget != foundW) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001394 if (DEBUG_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001395 Slog.v(TAG, "New wallpaper target: " + foundW
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001396 + " oldTarget: " + mWallpaperTarget);
1397 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001398
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001399 mLowerWallpaperTarget = null;
1400 mUpperWallpaperTarget = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001401
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001402 WindowState oldW = mWallpaperTarget;
1403 mWallpaperTarget = foundW;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001404
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001405 // Now what is happening... if the current and new targets are
1406 // animating, then we are in our super special mode!
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001407 if (foundW != null && oldW != null) {
1408 boolean oldAnim = oldW.mAnimation != null
1409 || (oldW.mAppToken != null && oldW.mAppToken.animation != null);
1410 boolean foundAnim = foundW.mAnimation != null
1411 || (foundW.mAppToken != null && foundW.mAppToken.animation != null);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001412 if (DEBUG_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001413 Slog.v(TAG, "New animation: " + foundAnim
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001414 + " old animation: " + oldAnim);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001415 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001416 if (foundAnim && oldAnim) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001417 int oldI = localmWindows.indexOf(oldW);
1418 if (DEBUG_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001419 Slog.v(TAG, "New i: " + foundI + " old i: " + oldI);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001420 }
1421 if (oldI >= 0) {
1422 if (DEBUG_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001423 Slog.v(TAG, "Animating wallpapers: old#" + oldI
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001424 + "=" + oldW + "; new#" + foundI
1425 + "=" + foundW);
1426 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001427
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001428 // Set the new target correctly.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001429 if (foundW.mAppToken != null && foundW.mAppToken.hiddenRequested) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001430 if (DEBUG_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001431 Slog.v(TAG, "Old wallpaper still the target.");
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001432 }
1433 mWallpaperTarget = oldW;
1434 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001435
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001436 // Now set the upper and lower wallpaper targets
1437 // correctly, and make sure that we are positioning
1438 // the wallpaper below the lower.
1439 if (foundI > oldI) {
1440 // The new target is on top of the old one.
1441 if (DEBUG_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001442 Slog.v(TAG, "Found target above old target.");
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001443 }
1444 mUpperWallpaperTarget = foundW;
1445 mLowerWallpaperTarget = oldW;
1446 foundW = oldW;
1447 foundI = oldI;
1448 } else {
1449 // The new target is below the old one.
1450 if (DEBUG_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001451 Slog.v(TAG, "Found target below old target.");
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001452 }
1453 mUpperWallpaperTarget = oldW;
1454 mLowerWallpaperTarget = foundW;
1455 }
1456 }
1457 }
1458 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001459
Dianne Hackborn6b1cb352009-09-28 18:27:26 -07001460 } else if (mLowerWallpaperTarget != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001461 // Is it time to stop animating?
Dianne Hackborn6b1cb352009-09-28 18:27:26 -07001462 boolean lowerAnimating = mLowerWallpaperTarget.mAnimation != null
1463 || (mLowerWallpaperTarget.mAppToken != null
1464 && mLowerWallpaperTarget.mAppToken.animation != null);
1465 boolean upperAnimating = mUpperWallpaperTarget.mAnimation != null
1466 || (mUpperWallpaperTarget.mAppToken != null
1467 && mUpperWallpaperTarget.mAppToken.animation != null);
1468 if (!lowerAnimating || !upperAnimating) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001469 if (DEBUG_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001470 Slog.v(TAG, "No longer animating wallpaper targets!");
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001471 }
1472 mLowerWallpaperTarget = null;
1473 mUpperWallpaperTarget = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001474 }
1475 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001476
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001477 boolean visible = foundW != null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001478 if (visible) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001479 // The window is visible to the compositor... but is it visible
1480 // to the user? That is what the wallpaper cares about.
Dianne Hackborn25994b42009-09-04 14:21:19 -07001481 visible = isWallpaperVisible(foundW);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001482 if (DEBUG_WALLPAPER) Slog.v(TAG, "Wallpaper visibility: " + visible);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001483
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001484 // If the wallpaper target is animating, we may need to copy
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001485 // its layer adjustment. Only do this if we are not transfering
1486 // between two wallpaper targets.
1487 mWallpaperAnimLayerAdjustment =
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001488 (mLowerWallpaperTarget == null && foundW.mAppToken != null)
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001489 ? foundW.mAppToken.animLayerAdjustment : 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001490
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001491 final int maxLayer = mPolicy.getMaxWallpaperLayer()
1492 * TYPE_LAYER_MULTIPLIER
1493 + TYPE_LAYER_OFFSET;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001494
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001495 // Now w is the window we are supposed to be behind... but we
1496 // need to be sure to also be behind any of its attached windows,
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001497 // AND any starting window associated with it, AND below the
1498 // maximum layer the policy allows for wallpapers.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001499 while (foundI > 0) {
1500 WindowState wb = (WindowState)localmWindows.get(foundI-1);
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001501 if (wb.mBaseLayer < maxLayer &&
1502 wb.mAttachedWindow != foundW &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001503 (wb.mAttrs.type != TYPE_APPLICATION_STARTING ||
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001504 wb.mToken != foundW.mToken)) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001505 // This window is not related to the previous one in any
1506 // interesting way, so stop here.
1507 break;
1508 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001509 foundW = wb;
1510 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001511 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001512 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001513 if (DEBUG_WALLPAPER) Slog.v(TAG, "No wallpaper target");
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001514 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001515
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001516 if (foundW == null && topCurW != null) {
1517 // There is no wallpaper target, so it goes at the bottom.
1518 // We will assume it is the same place as last time, if known.
1519 foundW = topCurW;
1520 foundI = topCurI+1;
1521 } else {
1522 // Okay i is the position immediately above the wallpaper. Look at
1523 // what is below it for later.
1524 foundW = foundI > 0 ? (WindowState)localmWindows.get(foundI-1) : null;
1525 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001526
Dianne Hackborn284ac932009-08-28 10:34:25 -07001527 if (visible) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001528 if (mWallpaperTarget.mWallpaperX >= 0) {
1529 mLastWallpaperX = mWallpaperTarget.mWallpaperX;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001530 mLastWallpaperXStep = mWallpaperTarget.mWallpaperXStep;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001531 }
1532 if (mWallpaperTarget.mWallpaperY >= 0) {
1533 mLastWallpaperY = mWallpaperTarget.mWallpaperY;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001534 mLastWallpaperYStep = mWallpaperTarget.mWallpaperYStep;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001535 }
Dianne Hackborn284ac932009-08-28 10:34:25 -07001536 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001537
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001538 // Start stepping backwards from here, ensuring that our wallpaper windows
1539 // are correctly placed.
1540 int curTokenIndex = mWallpaperTokens.size();
1541 while (curTokenIndex > 0) {
1542 curTokenIndex--;
1543 WindowToken token = mWallpaperTokens.get(curTokenIndex);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001544 if (token.hidden == visible) {
1545 changed |= ADJUST_WALLPAPER_VISIBILITY_CHANGED;
1546 token.hidden = !visible;
1547 // Need to do a layout to ensure the wallpaper now has the
1548 // correct size.
1549 mLayoutNeeded = true;
1550 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001551
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001552 int curWallpaperIndex = token.windows.size();
1553 while (curWallpaperIndex > 0) {
1554 curWallpaperIndex--;
1555 WindowState wallpaper = token.windows.get(curWallpaperIndex);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001556
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001557 if (visible) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001558 updateWallpaperOffsetLocked(wallpaper, dw, dh, false);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001559 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001560
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001561 // First, make sure the client has the current visibility
1562 // state.
1563 if (wallpaper.mWallpaperVisible != visible) {
1564 wallpaper.mWallpaperVisible = visible;
1565 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001566 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001567 "Setting visibility of wallpaper " + wallpaper
1568 + ": " + visible);
1569 wallpaper.mClient.dispatchAppVisibility(visible);
1570 } catch (RemoteException e) {
1571 }
1572 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001573
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001574 wallpaper.mAnimLayer = wallpaper.mLayer + mWallpaperAnimLayerAdjustment;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001575 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Slog.v(TAG, "Wallpaper win "
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001576 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001577
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001578 // First, if this window is at the current index, then all
1579 // is well.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001580 if (wallpaper == foundW) {
1581 foundI--;
1582 foundW = foundI > 0
1583 ? (WindowState)localmWindows.get(foundI-1) : null;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001584 continue;
1585 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001586
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001587 // The window didn't match... the current wallpaper window,
1588 // wherever it is, is in the wrong place, so make sure it is
1589 // not in the list.
1590 int oldIndex = localmWindows.indexOf(wallpaper);
1591 if (oldIndex >= 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001592 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "Wallpaper removing at "
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001593 + oldIndex + ": " + wallpaper);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001594 localmWindows.remove(oldIndex);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001595 if (oldIndex < foundI) {
1596 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001597 }
1598 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001599
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001600 // Now stick it in.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001601 if (DEBUG_WALLPAPER || DEBUG_WINDOW_MOVEMENT) Slog.v(TAG,
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001602 "Moving wallpaper " + wallpaper
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001603 + " from " + oldIndex + " to " + foundI);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001604
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001605 localmWindows.add(foundI, wallpaper);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001606 changed |= ADJUST_WALLPAPER_LAYERS_CHANGED;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001607 }
1608 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001609
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001610 return changed;
1611 }
1612
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001613 void setWallpaperAnimLayerAdjustmentLocked(int adj) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001614 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001615 "Setting wallpaper layer adj to " + adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001616 mWallpaperAnimLayerAdjustment = adj;
1617 int curTokenIndex = mWallpaperTokens.size();
1618 while (curTokenIndex > 0) {
1619 curTokenIndex--;
1620 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1621 int curWallpaperIndex = token.windows.size();
1622 while (curWallpaperIndex > 0) {
1623 curWallpaperIndex--;
1624 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1625 wallpaper.mAnimLayer = wallpaper.mLayer + adj;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001626 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Slog.v(TAG, "Wallpaper win "
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001627 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001628 }
1629 }
1630 }
1631
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001632 boolean updateWallpaperOffsetLocked(WindowState wallpaperWin, int dw, int dh,
1633 boolean sync) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001634 boolean changed = false;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001635 boolean rawChanged = false;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001636 float wpx = mLastWallpaperX >= 0 ? mLastWallpaperX : 0.5f;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001637 float wpxs = mLastWallpaperXStep >= 0 ? mLastWallpaperXStep : -1.0f;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001638 int availw = wallpaperWin.mFrame.right-wallpaperWin.mFrame.left-dw;
1639 int offset = availw > 0 ? -(int)(availw*wpx+.5f) : 0;
1640 changed = wallpaperWin.mXOffset != offset;
1641 if (changed) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001642 if (DEBUG_WALLPAPER) Slog.v(TAG, "Update wallpaper "
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001643 + wallpaperWin + " x: " + offset);
1644 wallpaperWin.mXOffset = offset;
1645 }
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001646 if (wallpaperWin.mWallpaperX != wpx || wallpaperWin.mWallpaperXStep != wpxs) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001647 wallpaperWin.mWallpaperX = wpx;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001648 wallpaperWin.mWallpaperXStep = wpxs;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001649 rawChanged = true;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001650 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001651
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001652 float wpy = mLastWallpaperY >= 0 ? mLastWallpaperY : 0.5f;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001653 float wpys = mLastWallpaperYStep >= 0 ? mLastWallpaperYStep : -1.0f;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001654 int availh = wallpaperWin.mFrame.bottom-wallpaperWin.mFrame.top-dh;
1655 offset = availh > 0 ? -(int)(availh*wpy+.5f) : 0;
1656 if (wallpaperWin.mYOffset != offset) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001657 if (DEBUG_WALLPAPER) Slog.v(TAG, "Update wallpaper "
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001658 + wallpaperWin + " y: " + offset);
1659 changed = true;
1660 wallpaperWin.mYOffset = offset;
1661 }
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001662 if (wallpaperWin.mWallpaperY != wpy || wallpaperWin.mWallpaperYStep != wpys) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001663 wallpaperWin.mWallpaperY = wpy;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001664 wallpaperWin.mWallpaperYStep = wpys;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001665 rawChanged = true;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001666 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001667
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001668 if (rawChanged) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001669 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001670 if (DEBUG_WALLPAPER) Slog.v(TAG, "Report new wp offset "
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001671 + wallpaperWin + " x=" + wallpaperWin.mWallpaperX
1672 + " y=" + wallpaperWin.mWallpaperY);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001673 if (sync) {
Dianne Hackborn75804932009-10-20 20:15:20 -07001674 mWaitingOnWallpaper = wallpaperWin;
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001675 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001676 wallpaperWin.mClient.dispatchWallpaperOffsets(
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001677 wallpaperWin.mWallpaperX, wallpaperWin.mWallpaperY,
1678 wallpaperWin.mWallpaperXStep, wallpaperWin.mWallpaperYStep, sync);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001679 if (sync) {
Dianne Hackborn75804932009-10-20 20:15:20 -07001680 if (mWaitingOnWallpaper != null) {
1681 long start = SystemClock.uptimeMillis();
1682 if ((mLastWallpaperTimeoutTime+WALLPAPER_TIMEOUT_RECOVERY)
1683 < start) {
1684 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001685 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn75804932009-10-20 20:15:20 -07001686 "Waiting for offset complete...");
1687 mWindowMap.wait(WALLPAPER_TIMEOUT);
1688 } catch (InterruptedException e) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001689 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001690 if (DEBUG_WALLPAPER) Slog.v(TAG, "Offset complete!");
Dianne Hackborn75804932009-10-20 20:15:20 -07001691 if ((start+WALLPAPER_TIMEOUT)
1692 < SystemClock.uptimeMillis()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001693 Slog.i(TAG, "Timeout waiting for wallpaper to offset: "
Dianne Hackborn75804932009-10-20 20:15:20 -07001694 + wallpaperWin);
1695 mLastWallpaperTimeoutTime = start;
1696 }
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001697 }
Dianne Hackborn75804932009-10-20 20:15:20 -07001698 mWaitingOnWallpaper = null;
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001699 }
1700 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001701 } catch (RemoteException e) {
1702 }
1703 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001704
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001705 return changed;
1706 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001707
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001708 void wallpaperOffsetsComplete(IBinder window) {
Dianne Hackborn75804932009-10-20 20:15:20 -07001709 synchronized (mWindowMap) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001710 if (mWaitingOnWallpaper != null &&
1711 mWaitingOnWallpaper.mClient.asBinder() == window) {
1712 mWaitingOnWallpaper = null;
Dianne Hackborn75804932009-10-20 20:15:20 -07001713 mWindowMap.notifyAll();
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001714 }
1715 }
1716 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001717
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001718 boolean updateWallpaperOffsetLocked(WindowState changingTarget, boolean sync) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001719 final int dw = mDisplay.getWidth();
1720 final int dh = mDisplay.getHeight();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001721
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001722 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001723
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001724 WindowState target = mWallpaperTarget;
1725 if (target != null) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001726 if (target.mWallpaperX >= 0) {
1727 mLastWallpaperX = target.mWallpaperX;
1728 } else if (changingTarget.mWallpaperX >= 0) {
1729 mLastWallpaperX = changingTarget.mWallpaperX;
1730 }
1731 if (target.mWallpaperY >= 0) {
1732 mLastWallpaperY = target.mWallpaperY;
1733 } else if (changingTarget.mWallpaperY >= 0) {
1734 mLastWallpaperY = changingTarget.mWallpaperY;
1735 }
1736 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001737
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001738 int curTokenIndex = mWallpaperTokens.size();
1739 while (curTokenIndex > 0) {
1740 curTokenIndex--;
1741 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1742 int curWallpaperIndex = token.windows.size();
1743 while (curWallpaperIndex > 0) {
1744 curWallpaperIndex--;
1745 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1746 if (updateWallpaperOffsetLocked(wallpaper, dw, dh, sync)) {
1747 wallpaper.computeShownFrameLocked();
1748 changed = true;
1749 // We only want to be synchronous with one wallpaper.
1750 sync = false;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001751 }
1752 }
1753 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001754
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001755 return changed;
1756 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001757
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001758 void updateWallpaperVisibilityLocked() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07001759 final boolean visible = isWallpaperVisible(mWallpaperTarget);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001760 final int dw = mDisplay.getWidth();
1761 final int dh = mDisplay.getHeight();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001762
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001763 int curTokenIndex = mWallpaperTokens.size();
1764 while (curTokenIndex > 0) {
1765 curTokenIndex--;
1766 WindowToken token = mWallpaperTokens.get(curTokenIndex);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001767 if (token.hidden == visible) {
1768 token.hidden = !visible;
1769 // Need to do a layout to ensure the wallpaper now has the
1770 // correct size.
1771 mLayoutNeeded = true;
1772 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001773
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001774 int curWallpaperIndex = token.windows.size();
1775 while (curWallpaperIndex > 0) {
1776 curWallpaperIndex--;
1777 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1778 if (visible) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001779 updateWallpaperOffsetLocked(wallpaper, dw, dh, false);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001780 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001781
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001782 if (wallpaper.mWallpaperVisible != visible) {
1783 wallpaper.mWallpaperVisible = visible;
1784 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001785 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -07001786 "Updating visibility of wallpaper " + wallpaper
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001787 + ": " + visible);
1788 wallpaper.mClient.dispatchAppVisibility(visible);
1789 } catch (RemoteException e) {
1790 }
1791 }
1792 }
1793 }
1794 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001795
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001796 void sendPointerToWallpaperLocked(WindowState srcWin,
1797 MotionEvent pointer, long eventTime) {
1798 int curTokenIndex = mWallpaperTokens.size();
1799 while (curTokenIndex > 0) {
1800 curTokenIndex--;
1801 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1802 int curWallpaperIndex = token.windows.size();
1803 while (curWallpaperIndex > 0) {
1804 curWallpaperIndex--;
1805 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1806 if ((wallpaper.mAttrs.flags &
1807 WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
1808 continue;
1809 }
1810 try {
1811 MotionEvent ev = MotionEvent.obtainNoHistory(pointer);
Dianne Hackborn6adba242009-11-10 11:10:09 -08001812 if (srcWin != null) {
1813 ev.offsetLocation(srcWin.mFrame.left-wallpaper.mFrame.left,
1814 srcWin.mFrame.top-wallpaper.mFrame.top);
1815 } else {
1816 ev.offsetLocation(-wallpaper.mFrame.left, -wallpaper.mFrame.top);
1817 }
1818 switch (pointer.getAction()) {
1819 case MotionEvent.ACTION_DOWN:
1820 mSendingPointersToWallpaper = true;
1821 break;
1822 case MotionEvent.ACTION_UP:
1823 mSendingPointersToWallpaper = false;
1824 break;
1825 }
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001826 wallpaper.mClient.dispatchPointer(ev, eventTime, false);
1827 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001828 Slog.w(TAG, "Failure sending pointer to wallpaper", e);
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001829 }
1830 }
1831 }
1832 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001833
Dianne Hackborn90d2db32010-02-11 22:19:06 -08001834 void dispatchPointerElsewhereLocked(WindowState srcWin, WindowState relWin,
1835 MotionEvent pointer, long eventTime, boolean skipped) {
1836 if (relWin != null) {
1837 mPolicy.dispatchedPointerEventLw(pointer, relWin.mFrame.left, relWin.mFrame.top);
1838 } else {
1839 mPolicy.dispatchedPointerEventLw(pointer, 0, 0);
1840 }
1841
1842 // If we sent an initial down to the wallpaper, then continue
1843 // sending events until the final up.
1844 if (mSendingPointersToWallpaper) {
1845 if (skipped) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001846 Slog.i(TAG, "Sending skipped pointer to wallpaper!");
Dianne Hackborn90d2db32010-02-11 22:19:06 -08001847 }
1848 sendPointerToWallpaperLocked(relWin, pointer, eventTime);
1849
1850 // If we are on top of the wallpaper, then the wallpaper also
1851 // gets to see this movement.
1852 } else if (srcWin != null
1853 && pointer.getAction() == MotionEvent.ACTION_DOWN
1854 && mWallpaperTarget == srcWin
1855 && srcWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
1856 sendPointerToWallpaperLocked(relWin, pointer, eventTime);
1857 }
1858 }
1859
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001860 public int addWindow(Session session, IWindow client,
1861 WindowManager.LayoutParams attrs, int viewVisibility,
1862 Rect outContentInsets) {
1863 int res = mPolicy.checkAddPermission(attrs);
1864 if (res != WindowManagerImpl.ADD_OKAY) {
1865 return res;
1866 }
Romain Guy06882f82009-06-10 13:36:04 -07001867
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001868 boolean reportNewConfig = false;
1869 WindowState attachedWindow = null;
1870 WindowState win = null;
Romain Guy06882f82009-06-10 13:36:04 -07001871
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001872 synchronized(mWindowMap) {
1873 // Instantiating a Display requires talking with the simulator,
1874 // so don't do it until we know the system is mostly up and
1875 // running.
1876 if (mDisplay == null) {
1877 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
1878 mDisplay = wm.getDefaultDisplay();
Christopher Tateb696aee2010-04-02 19:08:30 -07001879 mInitialDisplayWidth = mDisplay.getWidth();
1880 mInitialDisplayHeight = mDisplay.getHeight();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001881 mQueue.setDisplay(mDisplay);
1882 reportNewConfig = true;
1883 }
Romain Guy06882f82009-06-10 13:36:04 -07001884
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001885 if (mWindowMap.containsKey(client.asBinder())) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001886 Slog.w(TAG, "Window " + client + " is already added");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001887 return WindowManagerImpl.ADD_DUPLICATE_ADD;
1888 }
1889
1890 if (attrs.type >= FIRST_SUB_WINDOW && attrs.type <= LAST_SUB_WINDOW) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001891 attachedWindow = windowForClientLocked(null, attrs.token, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001892 if (attachedWindow == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001893 Slog.w(TAG, "Attempted to add window with token that is not a window: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001894 + attrs.token + ". Aborting.");
1895 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1896 }
1897 if (attachedWindow.mAttrs.type >= FIRST_SUB_WINDOW
1898 && attachedWindow.mAttrs.type <= LAST_SUB_WINDOW) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001899 Slog.w(TAG, "Attempted to add window with token that is a sub-window: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001900 + attrs.token + ". Aborting.");
1901 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1902 }
1903 }
1904
1905 boolean addToken = false;
1906 WindowToken token = mTokenMap.get(attrs.token);
1907 if (token == null) {
1908 if (attrs.type >= FIRST_APPLICATION_WINDOW
1909 && attrs.type <= LAST_APPLICATION_WINDOW) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001910 Slog.w(TAG, "Attempted to add application window with unknown token "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001911 + attrs.token + ". Aborting.");
1912 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1913 }
1914 if (attrs.type == TYPE_INPUT_METHOD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001915 Slog.w(TAG, "Attempted to add input method window with unknown token "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001916 + attrs.token + ". Aborting.");
1917 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1918 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001919 if (attrs.type == TYPE_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001920 Slog.w(TAG, "Attempted to add wallpaper window with unknown token "
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001921 + attrs.token + ". Aborting.");
1922 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1923 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001924 token = new WindowToken(attrs.token, -1, false);
1925 addToken = true;
1926 } else if (attrs.type >= FIRST_APPLICATION_WINDOW
1927 && attrs.type <= LAST_APPLICATION_WINDOW) {
1928 AppWindowToken atoken = token.appWindowToken;
1929 if (atoken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001930 Slog.w(TAG, "Attempted to add window with non-application token "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001931 + token + ". Aborting.");
1932 return WindowManagerImpl.ADD_NOT_APP_TOKEN;
1933 } else if (atoken.removed) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001934 Slog.w(TAG, "Attempted to add window with exiting application token "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001935 + token + ". Aborting.");
1936 return WindowManagerImpl.ADD_APP_EXITING;
1937 }
1938 if (attrs.type == TYPE_APPLICATION_STARTING && atoken.firstWindowDrawn) {
1939 // No need for this guy!
Joe Onorato8a9b2202010-02-26 18:56:32 -08001940 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001941 TAG, "**** NO NEED TO START: " + attrs.getTitle());
1942 return WindowManagerImpl.ADD_STARTING_NOT_NEEDED;
1943 }
1944 } else if (attrs.type == TYPE_INPUT_METHOD) {
1945 if (token.windowType != TYPE_INPUT_METHOD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001946 Slog.w(TAG, "Attempted to add input method window with bad token "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001947 + attrs.token + ". Aborting.");
1948 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1949 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001950 } else if (attrs.type == TYPE_WALLPAPER) {
1951 if (token.windowType != TYPE_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001952 Slog.w(TAG, "Attempted to add wallpaper window with bad token "
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001953 + attrs.token + ". Aborting.");
1954 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1955 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001956 }
1957
1958 win = new WindowState(session, client, token,
1959 attachedWindow, attrs, viewVisibility);
1960 if (win.mDeathRecipient == null) {
1961 // Client has apparently died, so there is no reason to
1962 // continue.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001963 Slog.w(TAG, "Adding window client " + client.asBinder()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001964 + " that is dead, aborting.");
1965 return WindowManagerImpl.ADD_APP_EXITING;
1966 }
1967
1968 mPolicy.adjustWindowParamsLw(win.mAttrs);
Romain Guy06882f82009-06-10 13:36:04 -07001969
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001970 res = mPolicy.prepareAddWindowLw(win, attrs);
1971 if (res != WindowManagerImpl.ADD_OKAY) {
1972 return res;
1973 }
1974
1975 // From now on, no exceptions or errors allowed!
1976
1977 res = WindowManagerImpl.ADD_OKAY;
Romain Guy06882f82009-06-10 13:36:04 -07001978
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001979 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001981 if (addToken) {
1982 mTokenMap.put(attrs.token, token);
1983 mTokenList.add(token);
1984 }
1985 win.attach();
1986 mWindowMap.put(client.asBinder(), win);
1987
1988 if (attrs.type == TYPE_APPLICATION_STARTING &&
1989 token.appWindowToken != null) {
1990 token.appWindowToken.startingWindow = win;
1991 }
1992
1993 boolean imMayMove = true;
Romain Guy06882f82009-06-10 13:36:04 -07001994
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001995 if (attrs.type == TYPE_INPUT_METHOD) {
1996 mInputMethodWindow = win;
1997 addInputMethodWindowToListLocked(win);
1998 imMayMove = false;
1999 } else if (attrs.type == TYPE_INPUT_METHOD_DIALOG) {
2000 mInputMethodDialogs.add(win);
2001 addWindowToListInOrderLocked(win, true);
2002 adjustInputMethodDialogsLocked();
2003 imMayMove = false;
2004 } else {
2005 addWindowToListInOrderLocked(win, true);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002006 if (attrs.type == TYPE_WALLPAPER) {
2007 mLastWallpaperTimeoutTime = 0;
2008 adjustWallpaperWindowsLocked();
2009 } else if ((attrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002010 adjustWallpaperWindowsLocked();
2011 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002012 }
Romain Guy06882f82009-06-10 13:36:04 -07002013
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002014 win.mEnterAnimationPending = true;
Romain Guy06882f82009-06-10 13:36:04 -07002015
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002016 mPolicy.getContentInsetHintLw(attrs, outContentInsets);
Romain Guy06882f82009-06-10 13:36:04 -07002017
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002018 if (mInTouchMode) {
2019 res |= WindowManagerImpl.ADD_FLAG_IN_TOUCH_MODE;
2020 }
2021 if (win == null || win.mAppToken == null || !win.mAppToken.clientHidden) {
2022 res |= WindowManagerImpl.ADD_FLAG_APP_VISIBLE;
2023 }
Romain Guy06882f82009-06-10 13:36:04 -07002024
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08002025 boolean focusChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002026 if (win.canReceiveKeys()) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08002027 if ((focusChanged=updateFocusedWindowLocked(UPDATE_FOCUS_WILL_ASSIGN_LAYERS))
2028 == true) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002029 imMayMove = false;
2030 }
2031 }
Romain Guy06882f82009-06-10 13:36:04 -07002032
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002033 if (imMayMove) {
Romain Guy06882f82009-06-10 13:36:04 -07002034 moveInputMethodWindowsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002035 }
Romain Guy06882f82009-06-10 13:36:04 -07002036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002037 assignLayersLocked();
2038 // Don't do layout here, the window must call
2039 // relayout to be displayed, so we'll do it there.
Romain Guy06882f82009-06-10 13:36:04 -07002040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002041 //dump();
2042
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08002043 if (focusChanged) {
2044 if (mCurrentFocus != null) {
2045 mKeyWaiter.handleNewWindowLocked(mCurrentFocus);
2046 }
2047 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08002048 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002049 TAG, "New client " + client.asBinder()
2050 + ": window=" + win);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002051
2052 if (win.isVisibleOrAdding() && updateOrientationFromAppTokensLocked()) {
2053 reportNewConfig = true;
2054 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002055 }
2056
2057 // sendNewConfiguration() checks caller permissions so we must call it with
2058 // privilege. updateOrientationFromAppTokens() clears and resets the caller
2059 // identity anyway, so it's safe to just clear & restore around this whole
2060 // block.
2061 final long origId = Binder.clearCallingIdentity();
2062 if (reportNewConfig) {
2063 sendNewConfiguration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002064 }
2065 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07002066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002067 return res;
2068 }
Romain Guy06882f82009-06-10 13:36:04 -07002069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002070 public void removeWindow(Session session, IWindow client) {
2071 synchronized(mWindowMap) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002072 WindowState win = windowForClientLocked(session, client, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002073 if (win == null) {
2074 return;
2075 }
2076 removeWindowLocked(session, win);
2077 }
2078 }
Romain Guy06882f82009-06-10 13:36:04 -07002079
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002080 public void removeWindowLocked(Session session, WindowState win) {
2081
Joe Onorato8a9b2202010-02-26 18:56:32 -08002082 if (localLOGV || DEBUG_FOCUS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002083 TAG, "Remove " + win + " client="
2084 + Integer.toHexString(System.identityHashCode(
2085 win.mClient.asBinder()))
2086 + ", surface=" + win.mSurface);
2087
2088 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002089
Joe Onorato8a9b2202010-02-26 18:56:32 -08002090 if (DEBUG_APP_TRANSITIONS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002091 TAG, "Remove " + win + ": mSurface=" + win.mSurface
2092 + " mExiting=" + win.mExiting
2093 + " isAnimating=" + win.isAnimating()
2094 + " app-animation="
2095 + (win.mAppToken != null ? win.mAppToken.animation : null)
2096 + " inPendingTransaction="
2097 + (win.mAppToken != null ? win.mAppToken.inPendingTransaction : false)
2098 + " mDisplayFrozen=" + mDisplayFrozen);
2099 // Visibility of the removed window. Will be used later to update orientation later on.
2100 boolean wasVisible = false;
2101 // First, see if we need to run an animation. If we do, we have
2102 // to hold off on removing the window until the animation is done.
2103 // If the display is frozen, just remove immediately, since the
2104 // animation wouldn't be seen.
Dianne Hackbornde2606d2009-12-18 16:53:55 -08002105 if (win.mSurface != null && !mDisplayFrozen && mPolicy.isScreenOn()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002106 // If we are not currently running the exit animation, we
2107 // need to see about starting one.
2108 if (wasVisible=win.isWinVisibleLw()) {
Romain Guy06882f82009-06-10 13:36:04 -07002109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002110 int transit = WindowManagerPolicy.TRANSIT_EXIT;
2111 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
2112 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
2113 }
2114 // Try starting an animation.
2115 if (applyAnimationLocked(win, transit, false)) {
2116 win.mExiting = true;
2117 }
2118 }
2119 if (win.mExiting || win.isAnimating()) {
2120 // The exit animation is running... wait for it!
Joe Onorato8a9b2202010-02-26 18:56:32 -08002121 //Slog.i(TAG, "*** Running exit animation...");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002122 win.mExiting = true;
2123 win.mRemoveOnExit = true;
2124 mLayoutNeeded = true;
2125 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
2126 performLayoutAndPlaceSurfacesLocked();
2127 if (win.mAppToken != null) {
2128 win.mAppToken.updateReportedVisibilityLocked();
2129 }
2130 //dump();
2131 Binder.restoreCallingIdentity(origId);
2132 return;
2133 }
2134 }
2135
2136 removeWindowInnerLocked(session, win);
2137 // Removing a visible window will effect the computed orientation
2138 // So just update orientation if needed.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002139 if (wasVisible && computeForcedAppOrientationLocked()
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002140 != mForcedAppOrientation
2141 && updateOrientationFromAppTokensLocked()) {
2142 mH.sendEmptyMessage(H.SEND_NEW_CONFIGURATION);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002143 }
2144 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2145 Binder.restoreCallingIdentity(origId);
2146 }
Romain Guy06882f82009-06-10 13:36:04 -07002147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002148 private void removeWindowInnerLocked(Session session, WindowState win) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07002149 mKeyWaiter.finishedKey(session, win.mClient, true,
2150 KeyWaiter.RETURN_NOTHING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002151 mKeyWaiter.releasePendingPointerLocked(win.mSession);
2152 mKeyWaiter.releasePendingTrackballLocked(win.mSession);
Romain Guy06882f82009-06-10 13:36:04 -07002153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002154 win.mRemoved = true;
Romain Guy06882f82009-06-10 13:36:04 -07002155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002156 if (mInputMethodTarget == win) {
2157 moveInputMethodWindowsIfNeededLocked(false);
2158 }
Romain Guy06882f82009-06-10 13:36:04 -07002159
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07002160 if (false) {
2161 RuntimeException e = new RuntimeException("here");
2162 e.fillInStackTrace();
Joe Onorato8a9b2202010-02-26 18:56:32 -08002163 Slog.w(TAG, "Removing window " + win, e);
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07002164 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002166 mPolicy.removeWindowLw(win);
2167 win.removeLocked();
2168
2169 mWindowMap.remove(win.mClient.asBinder());
2170 mWindows.remove(win);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002171 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "Final remove of window: " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002172
2173 if (mInputMethodWindow == win) {
2174 mInputMethodWindow = null;
2175 } else if (win.mAttrs.type == TYPE_INPUT_METHOD_DIALOG) {
2176 mInputMethodDialogs.remove(win);
2177 }
Romain Guy06882f82009-06-10 13:36:04 -07002178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002179 final WindowToken token = win.mToken;
2180 final AppWindowToken atoken = win.mAppToken;
2181 token.windows.remove(win);
2182 if (atoken != null) {
2183 atoken.allAppWindows.remove(win);
2184 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08002185 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002186 TAG, "**** Removing window " + win + ": count="
2187 + token.windows.size());
2188 if (token.windows.size() == 0) {
2189 if (!token.explicit) {
2190 mTokenMap.remove(token.token);
2191 mTokenList.remove(token);
2192 } else if (atoken != null) {
2193 atoken.firstWindowDrawn = false;
2194 }
2195 }
2196
2197 if (atoken != null) {
2198 if (atoken.startingWindow == win) {
2199 atoken.startingWindow = null;
2200 } else if (atoken.allAppWindows.size() == 0 && atoken.startingData != null) {
2201 // If this is the last window and we had requested a starting
2202 // transition window, well there is no point now.
2203 atoken.startingData = null;
2204 } else if (atoken.allAppWindows.size() == 1 && atoken.startingView != null) {
2205 // If this is the last window except for a starting transition
2206 // window, we need to get rid of the starting transition.
2207 if (DEBUG_STARTING_WINDOW) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002208 Slog.v(TAG, "Schedule remove starting " + token
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002209 + ": no more real windows");
2210 }
2211 Message m = mH.obtainMessage(H.REMOVE_STARTING, atoken);
2212 mH.sendMessage(m);
2213 }
2214 }
Romain Guy06882f82009-06-10 13:36:04 -07002215
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002216 if (win.mAttrs.type == TYPE_WALLPAPER) {
2217 mLastWallpaperTimeoutTime = 0;
2218 adjustWallpaperWindowsLocked();
2219 } else if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07002220 adjustWallpaperWindowsLocked();
2221 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002223 if (!mInLayout) {
2224 assignLayersLocked();
2225 mLayoutNeeded = true;
2226 performLayoutAndPlaceSurfacesLocked();
2227 if (win.mAppToken != null) {
2228 win.mAppToken.updateReportedVisibilityLocked();
2229 }
2230 }
2231 }
2232
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08002233 private static void logSurface(WindowState w, String msg, RuntimeException where) {
2234 String str = " SURFACE " + Integer.toHexString(w.hashCode())
2235 + ": " + msg + " / " + w.mAttrs.getTitle();
2236 if (where != null) {
2237 Slog.i(TAG, str, where);
2238 } else {
2239 Slog.i(TAG, str);
2240 }
2241 }
2242
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002243 private void setTransparentRegionWindow(Session session, IWindow client, Region region) {
2244 long origId = Binder.clearCallingIdentity();
2245 try {
2246 synchronized (mWindowMap) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002247 WindowState w = windowForClientLocked(session, client, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002248 if ((w != null) && (w.mSurface != null)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002249 if (SHOW_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002250 Surface.openTransaction();
2251 try {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08002252 if (SHOW_TRANSACTIONS) logSurface(w,
2253 "transparentRegionHint=" + region, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002254 w.mSurface.setTransparentRegionHint(region);
2255 } finally {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002256 if (SHOW_TRANSACTIONS) Slog.i(TAG, "<<< CLOSE TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002257 Surface.closeTransaction();
2258 }
2259 }
2260 }
2261 } finally {
2262 Binder.restoreCallingIdentity(origId);
2263 }
2264 }
2265
2266 void setInsetsWindow(Session session, IWindow client,
Romain Guy06882f82009-06-10 13:36:04 -07002267 int touchableInsets, Rect contentInsets,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002268 Rect visibleInsets) {
2269 long origId = Binder.clearCallingIdentity();
2270 try {
2271 synchronized (mWindowMap) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002272 WindowState w = windowForClientLocked(session, client, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002273 if (w != null) {
2274 w.mGivenInsetsPending = false;
2275 w.mGivenContentInsets.set(contentInsets);
2276 w.mGivenVisibleInsets.set(visibleInsets);
2277 w.mTouchableInsets = touchableInsets;
2278 mLayoutNeeded = true;
2279 performLayoutAndPlaceSurfacesLocked();
2280 }
2281 }
2282 } finally {
2283 Binder.restoreCallingIdentity(origId);
2284 }
2285 }
Romain Guy06882f82009-06-10 13:36:04 -07002286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002287 public void getWindowDisplayFrame(Session session, IWindow client,
2288 Rect outDisplayFrame) {
2289 synchronized(mWindowMap) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002290 WindowState win = windowForClientLocked(session, client, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002291 if (win == null) {
2292 outDisplayFrame.setEmpty();
2293 return;
2294 }
2295 outDisplayFrame.set(win.mDisplayFrame);
2296 }
2297 }
2298
Marco Nelissenbf6956b2009-11-09 15:21:13 -08002299 public void setWindowWallpaperPositionLocked(WindowState window, float x, float y,
2300 float xStep, float yStep) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002301 if (window.mWallpaperX != x || window.mWallpaperY != y) {
2302 window.mWallpaperX = x;
2303 window.mWallpaperY = y;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08002304 window.mWallpaperXStep = xStep;
2305 window.mWallpaperYStep = yStep;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07002306 if (updateWallpaperOffsetLocked(window, true)) {
2307 performLayoutAndPlaceSurfacesLocked();
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002308 }
2309 }
2310 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002311
Dianne Hackborn75804932009-10-20 20:15:20 -07002312 void wallpaperCommandComplete(IBinder window, Bundle result) {
2313 synchronized (mWindowMap) {
2314 if (mWaitingOnWallpaper != null &&
2315 mWaitingOnWallpaper.mClient.asBinder() == window) {
2316 mWaitingOnWallpaper = null;
2317 mWindowMap.notifyAll();
2318 }
2319 }
2320 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002321
Dianne Hackborn75804932009-10-20 20:15:20 -07002322 public Bundle sendWindowWallpaperCommandLocked(WindowState window,
2323 String action, int x, int y, int z, Bundle extras, boolean sync) {
2324 if (window == mWallpaperTarget || window == mLowerWallpaperTarget
2325 || window == mUpperWallpaperTarget) {
2326 boolean doWait = sync;
2327 int curTokenIndex = mWallpaperTokens.size();
2328 while (curTokenIndex > 0) {
2329 curTokenIndex--;
2330 WindowToken token = mWallpaperTokens.get(curTokenIndex);
2331 int curWallpaperIndex = token.windows.size();
2332 while (curWallpaperIndex > 0) {
2333 curWallpaperIndex--;
2334 WindowState wallpaper = token.windows.get(curWallpaperIndex);
2335 try {
2336 wallpaper.mClient.dispatchWallpaperCommand(action,
2337 x, y, z, extras, sync);
2338 // We only want to be synchronous with one wallpaper.
2339 sync = false;
2340 } catch (RemoteException e) {
2341 }
2342 }
2343 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002344
Dianne Hackborn75804932009-10-20 20:15:20 -07002345 if (doWait) {
2346 // XXX Need to wait for result.
2347 }
2348 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002349
Dianne Hackborn75804932009-10-20 20:15:20 -07002350 return null;
2351 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002353 public int relayoutWindow(Session session, IWindow client,
2354 WindowManager.LayoutParams attrs, int requestedWidth,
2355 int requestedHeight, int viewVisibility, boolean insetsPending,
2356 Rect outFrame, Rect outContentInsets, Rect outVisibleInsets,
Dianne Hackborn694f79b2010-03-17 19:44:59 -07002357 Configuration outConfig, Surface outSurface) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002358 boolean displayed = false;
2359 boolean inTouchMode;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002360 boolean configChanged;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002361 long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002363 synchronized(mWindowMap) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002364 WindowState win = windowForClientLocked(session, client, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002365 if (win == null) {
2366 return 0;
2367 }
2368 win.mRequestedWidth = requestedWidth;
2369 win.mRequestedHeight = requestedHeight;
2370
2371 if (attrs != null) {
2372 mPolicy.adjustWindowParamsLw(attrs);
2373 }
Romain Guy06882f82009-06-10 13:36:04 -07002374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002375 int attrChanges = 0;
2376 int flagChanges = 0;
2377 if (attrs != null) {
2378 flagChanges = win.mAttrs.flags ^= attrs.flags;
2379 attrChanges = win.mAttrs.copyFrom(attrs);
2380 }
2381
Joe Onorato8a9b2202010-02-26 18:56:32 -08002382 if (DEBUG_LAYOUT) Slog.v(TAG, "Relayout " + win + ": " + win.mAttrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002383
2384 if ((attrChanges & WindowManager.LayoutParams.ALPHA_CHANGED) != 0) {
2385 win.mAlpha = attrs.alpha;
2386 }
2387
2388 final boolean scaledWindow =
2389 ((win.mAttrs.flags & WindowManager.LayoutParams.FLAG_SCALED) != 0);
2390
2391 if (scaledWindow) {
2392 // requested{Width|Height} Surface's physical size
2393 // attrs.{width|height} Size on screen
2394 win.mHScale = (attrs.width != requestedWidth) ?
2395 (attrs.width / (float)requestedWidth) : 1.0f;
2396 win.mVScale = (attrs.height != requestedHeight) ?
2397 (attrs.height / (float)requestedHeight) : 1.0f;
Dianne Hackborn9b52a212009-12-11 14:51:35 -08002398 } else {
2399 win.mHScale = win.mVScale = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002400 }
2401
2402 boolean imMayMove = (flagChanges&(
2403 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
2404 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07002405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002406 boolean focusMayChange = win.mViewVisibility != viewVisibility
2407 || ((flagChanges&WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) != 0)
2408 || (!win.mRelayoutCalled);
Romain Guy06882f82009-06-10 13:36:04 -07002409
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002410 boolean wallpaperMayMove = win.mViewVisibility != viewVisibility
2411 && (win.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002413 win.mRelayoutCalled = true;
2414 final int oldVisibility = win.mViewVisibility;
2415 win.mViewVisibility = viewVisibility;
2416 if (viewVisibility == View.VISIBLE &&
2417 (win.mAppToken == null || !win.mAppToken.clientHidden)) {
2418 displayed = !win.isVisibleLw();
2419 if (win.mExiting) {
2420 win.mExiting = false;
2421 win.mAnimation = null;
2422 }
2423 if (win.mDestroying) {
2424 win.mDestroying = false;
2425 mDestroySurface.remove(win);
2426 }
2427 if (oldVisibility == View.GONE) {
2428 win.mEnterAnimationPending = true;
2429 }
Dianne Hackborn694f79b2010-03-17 19:44:59 -07002430 if (displayed) {
2431 if (win.mSurface != null && !win.mDrawPending
2432 && !win.mCommitDrawPending && !mDisplayFrozen
2433 && mPolicy.isScreenOn()) {
2434 applyEnterAnimationLocked(win);
2435 }
2436 if ((win.mAttrs.flags
2437 & WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON) != 0) {
2438 if (DEBUG_VISIBILITY) Slog.v(TAG,
2439 "Relayout window turning screen on: " + win);
2440 win.mTurnOnScreen = true;
2441 }
2442 int diff = 0;
2443 if (win.mConfiguration != mCurConfiguration
2444 && (win.mConfiguration == null
2445 || (diff=mCurConfiguration.diff(win.mConfiguration)) != 0)) {
2446 win.mConfiguration = mCurConfiguration;
2447 if (DEBUG_CONFIGURATION) {
2448 Slog.i(TAG, "Window " + win + " visible with new config: "
2449 + win.mConfiguration + " / 0x"
2450 + Integer.toHexString(diff));
2451 }
2452 outConfig.setTo(mCurConfiguration);
2453 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07002454 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002455 if ((attrChanges&WindowManager.LayoutParams.FORMAT_CHANGED) != 0) {
2456 // To change the format, we need to re-build the surface.
2457 win.destroySurfaceLocked();
2458 displayed = true;
2459 }
2460 try {
2461 Surface surface = win.createSurfaceLocked();
2462 if (surface != null) {
2463 outSurface.copyFrom(surface);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002464 win.mReportDestroySurface = false;
2465 win.mSurfacePendingDestroy = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -08002466 if (SHOW_TRANSACTIONS) Slog.i(TAG,
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002467 " OUT SURFACE " + outSurface + ": copied");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002468 } else {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002469 // For some reason there isn't a surface. Clear the
2470 // caller's object so they see the same state.
2471 outSurface.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002472 }
2473 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002474 Slog.w(TAG, "Exception thrown when creating surface for client "
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002475 + client + " (" + win.mAttrs.getTitle() + ")",
2476 e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002477 Binder.restoreCallingIdentity(origId);
2478 return 0;
2479 }
2480 if (displayed) {
2481 focusMayChange = true;
2482 }
2483 if (win.mAttrs.type == TYPE_INPUT_METHOD
2484 && mInputMethodWindow == null) {
2485 mInputMethodWindow = win;
2486 imMayMove = true;
2487 }
Dianne Hackborn558947c2009-12-18 16:02:50 -08002488 if (win.mAttrs.type == TYPE_BASE_APPLICATION
2489 && win.mAppToken != null
2490 && win.mAppToken.startingWindow != null) {
2491 // Special handling of starting window over the base
2492 // window of the app: propagate lock screen flags to it,
2493 // to provide the correct semantics while starting.
2494 final int mask =
2495 WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
Mike Lockwoodef731622010-01-27 17:51:34 -05002496 | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
2497 | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON;
Dianne Hackborn558947c2009-12-18 16:02:50 -08002498 WindowManager.LayoutParams sa = win.mAppToken.startingWindow.mAttrs;
2499 sa.flags = (sa.flags&~mask) | (win.mAttrs.flags&mask);
2500 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002501 } else {
2502 win.mEnterAnimationPending = false;
2503 if (win.mSurface != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002504 if (DEBUG_VISIBILITY) Slog.i(TAG, "Relayout invis " + win
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002505 + ": mExiting=" + win.mExiting
2506 + " mSurfacePendingDestroy=" + win.mSurfacePendingDestroy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002507 // If we are not currently running the exit animation, we
2508 // need to see about starting one.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002509 if (!win.mExiting || win.mSurfacePendingDestroy) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002510 // Try starting an animation; if there isn't one, we
2511 // can destroy the surface right away.
2512 int transit = WindowManagerPolicy.TRANSIT_EXIT;
2513 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
2514 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
2515 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002516 if (!win.mSurfacePendingDestroy && win.isWinVisibleLw() &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002517 applyAnimationLocked(win, transit, false)) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002518 focusMayChange = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002519 win.mExiting = true;
2520 mKeyWaiter.finishedKey(session, client, true,
2521 KeyWaiter.RETURN_NOTHING);
2522 } else if (win.isAnimating()) {
2523 // Currently in a hide animation... turn this into
2524 // an exit.
2525 win.mExiting = true;
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07002526 } else if (win == mWallpaperTarget) {
2527 // If the wallpaper is currently behind this
2528 // window, we need to change both of them inside
2529 // of a transaction to avoid artifacts.
2530 win.mExiting = true;
2531 win.mAnimating = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002532 } else {
2533 if (mInputMethodWindow == win) {
2534 mInputMethodWindow = null;
2535 }
2536 win.destroySurfaceLocked();
2537 }
2538 }
2539 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002540
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002541 if (win.mSurface == null || (win.getAttrs().flags
2542 & WindowManager.LayoutParams.FLAG_KEEP_SURFACE_WHILE_ANIMATING) == 0
2543 || win.mSurfacePendingDestroy) {
2544 // We are being called from a local process, which
2545 // means outSurface holds its current surface. Ensure the
2546 // surface object is cleared, but we don't want it actually
2547 // destroyed at this point.
2548 win.mSurfacePendingDestroy = false;
2549 outSurface.release();
Joe Onorato8a9b2202010-02-26 18:56:32 -08002550 if (DEBUG_VISIBILITY) Slog.i(TAG, "Releasing surface in: " + win);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002551 } else if (win.mSurface != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002552 if (DEBUG_VISIBILITY) Slog.i(TAG,
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002553 "Keeping surface, will report destroy: " + win);
2554 win.mReportDestroySurface = true;
2555 outSurface.copyFrom(win.mSurface);
2556 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002557 }
2558
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002559 if (focusMayChange) {
2560 //System.out.println("Focus may change: " + win.mAttrs.getTitle());
2561 if (updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002562 imMayMove = false;
2563 }
2564 //System.out.println("Relayout " + win + ": focus=" + mCurrentFocus);
2565 }
Romain Guy06882f82009-06-10 13:36:04 -07002566
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08002567 // updateFocusedWindowLocked() already assigned layers so we only need to
2568 // reassign them at this point if the IM window state gets shuffled
2569 boolean assignLayers = false;
Romain Guy06882f82009-06-10 13:36:04 -07002570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002571 if (imMayMove) {
Dianne Hackborn8abd5f02009-11-20 18:09:03 -08002572 if (moveInputMethodWindowsIfNeededLocked(false) || displayed) {
2573 // Little hack here -- we -should- be able to rely on the
2574 // function to return true if the IME has moved and needs
2575 // its layer recomputed. However, if the IME was hidden
2576 // and isn't actually moved in the list, its layer may be
2577 // out of data so we make sure to recompute it.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002578 assignLayers = true;
2579 }
2580 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002581 if (wallpaperMayMove) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002582 if ((adjustWallpaperWindowsLocked()&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002583 assignLayers = true;
2584 }
2585 }
Romain Guy06882f82009-06-10 13:36:04 -07002586
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002587 mLayoutNeeded = true;
2588 win.mGivenInsetsPending = insetsPending;
2589 if (assignLayers) {
2590 assignLayersLocked();
2591 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002592 configChanged = updateOrientationFromAppTokensLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002593 performLayoutAndPlaceSurfacesLocked();
Dianne Hackborn284ac932009-08-28 10:34:25 -07002594 if (displayed && win.mIsWallpaper) {
2595 updateWallpaperOffsetLocked(win, mDisplay.getWidth(),
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002596 mDisplay.getHeight(), false);
Dianne Hackborn284ac932009-08-28 10:34:25 -07002597 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002598 if (win.mAppToken != null) {
2599 win.mAppToken.updateReportedVisibilityLocked();
2600 }
2601 outFrame.set(win.mFrame);
2602 outContentInsets.set(win.mContentInsets);
2603 outVisibleInsets.set(win.mVisibleInsets);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002604 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002605 TAG, "Relayout given client " + client.asBinder()
Romain Guy06882f82009-06-10 13:36:04 -07002606 + ", requestedWidth=" + requestedWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002607 + ", requestedHeight=" + requestedHeight
2608 + ", viewVisibility=" + viewVisibility
2609 + "\nRelayout returning frame=" + outFrame
2610 + ", surface=" + outSurface);
2611
Joe Onorato8a9b2202010-02-26 18:56:32 -08002612 if (localLOGV || DEBUG_FOCUS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002613 TAG, "Relayout of " + win + ": focusMayChange=" + focusMayChange);
2614
2615 inTouchMode = mInTouchMode;
2616 }
2617
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002618 if (configChanged) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002619 sendNewConfiguration();
2620 }
Romain Guy06882f82009-06-10 13:36:04 -07002621
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002622 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07002623
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002624 return (inTouchMode ? WindowManagerImpl.RELAYOUT_IN_TOUCH_MODE : 0)
2625 | (displayed ? WindowManagerImpl.RELAYOUT_FIRST_TIME : 0);
2626 }
2627
2628 public void finishDrawingWindow(Session session, IWindow client) {
2629 final long origId = Binder.clearCallingIdentity();
2630 synchronized(mWindowMap) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002631 WindowState win = windowForClientLocked(session, client, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002632 if (win != null && win.finishDrawingLocked()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07002633 if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
2634 adjustWallpaperWindowsLocked();
2635 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002636 mLayoutNeeded = true;
2637 performLayoutAndPlaceSurfacesLocked();
2638 }
2639 }
2640 Binder.restoreCallingIdentity(origId);
2641 }
2642
2643 private AttributeCache.Entry getCachedAnimations(WindowManager.LayoutParams lp) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002644 if (DEBUG_ANIM) Slog.v(TAG, "Loading animations: params package="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002645 + (lp != null ? lp.packageName : null)
2646 + " resId=0x" + (lp != null ? Integer.toHexString(lp.windowAnimations) : null));
2647 if (lp != null && lp.windowAnimations != 0) {
2648 // If this is a system resource, don't try to load it from the
2649 // application resources. It is nice to avoid loading application
2650 // resources if we can.
2651 String packageName = lp.packageName != null ? lp.packageName : "android";
2652 int resId = lp.windowAnimations;
2653 if ((resId&0xFF000000) == 0x01000000) {
2654 packageName = "android";
2655 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08002656 if (DEBUG_ANIM) Slog.v(TAG, "Loading animations: picked package="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002657 + packageName);
2658 return AttributeCache.instance().get(packageName, resId,
2659 com.android.internal.R.styleable.WindowAnimation);
2660 }
2661 return null;
2662 }
Romain Guy06882f82009-06-10 13:36:04 -07002663
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002664 private AttributeCache.Entry getCachedAnimations(String packageName, int resId) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002665 if (DEBUG_ANIM) Slog.v(TAG, "Loading animations: params package="
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002666 + packageName + " resId=0x" + Integer.toHexString(resId));
2667 if (packageName != null) {
2668 if ((resId&0xFF000000) == 0x01000000) {
2669 packageName = "android";
2670 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08002671 if (DEBUG_ANIM) Slog.v(TAG, "Loading animations: picked package="
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002672 + packageName);
2673 return AttributeCache.instance().get(packageName, resId,
2674 com.android.internal.R.styleable.WindowAnimation);
2675 }
2676 return null;
2677 }
2678
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002679 private void applyEnterAnimationLocked(WindowState win) {
2680 int transit = WindowManagerPolicy.TRANSIT_SHOW;
2681 if (win.mEnterAnimationPending) {
2682 win.mEnterAnimationPending = false;
2683 transit = WindowManagerPolicy.TRANSIT_ENTER;
2684 }
2685
2686 applyAnimationLocked(win, transit, true);
2687 }
2688
2689 private boolean applyAnimationLocked(WindowState win,
2690 int transit, boolean isEntrance) {
2691 if (win.mLocalAnimating && win.mAnimationIsEntrance == isEntrance) {
2692 // If we are trying to apply an animation, but already running
2693 // an animation of the same type, then just leave that one alone.
2694 return true;
2695 }
Romain Guy06882f82009-06-10 13:36:04 -07002696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002697 // Only apply an animation if the display isn't frozen. If it is
2698 // frozen, there is no reason to animate and it can cause strange
2699 // artifacts when we unfreeze the display if some different animation
2700 // is running.
Dianne Hackbornde2606d2009-12-18 16:53:55 -08002701 if (!mDisplayFrozen && mPolicy.isScreenOn()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002702 int anim = mPolicy.selectAnimationLw(win, transit);
2703 int attr = -1;
2704 Animation a = null;
2705 if (anim != 0) {
2706 a = AnimationUtils.loadAnimation(mContext, anim);
2707 } else {
2708 switch (transit) {
2709 case WindowManagerPolicy.TRANSIT_ENTER:
2710 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
2711 break;
2712 case WindowManagerPolicy.TRANSIT_EXIT:
2713 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
2714 break;
2715 case WindowManagerPolicy.TRANSIT_SHOW:
2716 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
2717 break;
2718 case WindowManagerPolicy.TRANSIT_HIDE:
2719 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
2720 break;
2721 }
2722 if (attr >= 0) {
2723 a = loadAnimation(win.mAttrs, attr);
2724 }
2725 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08002726 if (DEBUG_ANIM) Slog.v(TAG, "applyAnimation: win=" + win
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002727 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
2728 + " mAnimation=" + win.mAnimation
2729 + " isEntrance=" + isEntrance);
2730 if (a != null) {
2731 if (DEBUG_ANIM) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08002732 RuntimeException e = null;
2733 if (!HIDE_STACK_CRAWLS) {
2734 e = new RuntimeException();
2735 e.fillInStackTrace();
2736 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08002737 Slog.v(TAG, "Loaded animation " + a + " for " + win, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002738 }
2739 win.setAnimation(a);
2740 win.mAnimationIsEntrance = isEntrance;
2741 }
2742 } else {
2743 win.clearAnimation();
2744 }
2745
2746 return win.mAnimation != null;
2747 }
2748
2749 private Animation loadAnimation(WindowManager.LayoutParams lp, int animAttr) {
2750 int anim = 0;
2751 Context context = mContext;
2752 if (animAttr >= 0) {
2753 AttributeCache.Entry ent = getCachedAnimations(lp);
2754 if (ent != null) {
2755 context = ent.context;
2756 anim = ent.array.getResourceId(animAttr, 0);
2757 }
2758 }
2759 if (anim != 0) {
2760 return AnimationUtils.loadAnimation(context, anim);
2761 }
2762 return null;
2763 }
Romain Guy06882f82009-06-10 13:36:04 -07002764
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002765 private Animation loadAnimation(String packageName, int resId) {
2766 int anim = 0;
2767 Context context = mContext;
2768 if (resId >= 0) {
2769 AttributeCache.Entry ent = getCachedAnimations(packageName, resId);
2770 if (ent != null) {
2771 context = ent.context;
2772 anim = resId;
2773 }
2774 }
2775 if (anim != 0) {
2776 return AnimationUtils.loadAnimation(context, anim);
2777 }
2778 return null;
2779 }
2780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002781 private boolean applyAnimationLocked(AppWindowToken wtoken,
2782 WindowManager.LayoutParams lp, int transit, boolean enter) {
2783 // Only apply an animation if the display isn't frozen. If it is
2784 // frozen, there is no reason to animate and it can cause strange
2785 // artifacts when we unfreeze the display if some different animation
2786 // is running.
Dianne Hackbornde2606d2009-12-18 16:53:55 -08002787 if (!mDisplayFrozen && mPolicy.isScreenOn()) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002788 Animation a;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07002789 if (lp != null && (lp.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002790 a = new FadeInOutAnimation(enter);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002791 if (DEBUG_ANIM) Slog.v(TAG,
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002792 "applying FadeInOutAnimation for a window in compatibility mode");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002793 } else if (mNextAppTransitionPackage != null) {
2794 a = loadAnimation(mNextAppTransitionPackage, enter ?
2795 mNextAppTransitionEnter : mNextAppTransitionExit);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002796 } else {
2797 int animAttr = 0;
2798 switch (transit) {
2799 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
2800 animAttr = enter
2801 ? com.android.internal.R.styleable.WindowAnimation_activityOpenEnterAnimation
2802 : com.android.internal.R.styleable.WindowAnimation_activityOpenExitAnimation;
2803 break;
2804 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
2805 animAttr = enter
2806 ? com.android.internal.R.styleable.WindowAnimation_activityCloseEnterAnimation
2807 : com.android.internal.R.styleable.WindowAnimation_activityCloseExitAnimation;
2808 break;
2809 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
2810 animAttr = enter
2811 ? com.android.internal.R.styleable.WindowAnimation_taskOpenEnterAnimation
2812 : com.android.internal.R.styleable.WindowAnimation_taskOpenExitAnimation;
2813 break;
2814 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
2815 animAttr = enter
2816 ? com.android.internal.R.styleable.WindowAnimation_taskCloseEnterAnimation
2817 : com.android.internal.R.styleable.WindowAnimation_taskCloseExitAnimation;
2818 break;
2819 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
2820 animAttr = enter
2821 ? com.android.internal.R.styleable.WindowAnimation_taskToFrontEnterAnimation
2822 : com.android.internal.R.styleable.WindowAnimation_taskToFrontExitAnimation;
2823 break;
2824 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
2825 animAttr = enter
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07002826 ? com.android.internal.R.styleable.WindowAnimation_taskToBackEnterAnimation
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002827 : com.android.internal.R.styleable.WindowAnimation_taskToBackExitAnimation;
2828 break;
Dianne Hackborn25994b42009-09-04 14:21:19 -07002829 case WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN:
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002830 animAttr = enter
Dianne Hackborn25994b42009-09-04 14:21:19 -07002831 ? com.android.internal.R.styleable.WindowAnimation_wallpaperOpenEnterAnimation
2832 : com.android.internal.R.styleable.WindowAnimation_wallpaperOpenExitAnimation;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002833 break;
Dianne Hackborn25994b42009-09-04 14:21:19 -07002834 case WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE:
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002835 animAttr = enter
Dianne Hackborn25994b42009-09-04 14:21:19 -07002836 ? com.android.internal.R.styleable.WindowAnimation_wallpaperCloseEnterAnimation
2837 : com.android.internal.R.styleable.WindowAnimation_wallpaperCloseExitAnimation;
2838 break;
2839 case WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN:
2840 animAttr = enter
2841 ? com.android.internal.R.styleable.WindowAnimation_wallpaperIntraOpenEnterAnimation
2842 : com.android.internal.R.styleable.WindowAnimation_wallpaperIntraOpenExitAnimation;
2843 break;
2844 case WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE:
2845 animAttr = enter
2846 ? com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseEnterAnimation
2847 : com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseExitAnimation;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002848 break;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002849 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07002850 a = animAttr != 0 ? loadAnimation(lp, animAttr) : null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08002851 if (DEBUG_ANIM) Slog.v(TAG, "applyAnimation: wtoken=" + wtoken
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002852 + " anim=" + a
2853 + " animAttr=0x" + Integer.toHexString(animAttr)
2854 + " transit=" + transit);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002855 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002856 if (a != null) {
2857 if (DEBUG_ANIM) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08002858 RuntimeException e = null;
2859 if (!HIDE_STACK_CRAWLS) {
2860 e = new RuntimeException();
2861 e.fillInStackTrace();
2862 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08002863 Slog.v(TAG, "Loaded animation " + a + " for " + wtoken, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002864 }
2865 wtoken.setAnimation(a);
2866 }
2867 } else {
2868 wtoken.clearAnimation();
2869 }
2870
2871 return wtoken.animation != null;
2872 }
2873
2874 // -------------------------------------------------------------
2875 // Application Window Tokens
2876 // -------------------------------------------------------------
2877
2878 public void validateAppTokens(List tokens) {
2879 int v = tokens.size()-1;
2880 int m = mAppTokens.size()-1;
2881 while (v >= 0 && m >= 0) {
2882 AppWindowToken wtoken = mAppTokens.get(m);
2883 if (wtoken.removed) {
2884 m--;
2885 continue;
2886 }
2887 if (tokens.get(v) != wtoken.token) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002888 Slog.w(TAG, "Tokens out of sync: external is " + tokens.get(v)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002889 + " @ " + v + ", internal is " + wtoken.token + " @ " + m);
2890 }
2891 v--;
2892 m--;
2893 }
2894 while (v >= 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002895 Slog.w(TAG, "External token not found: " + tokens.get(v) + " @ " + v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002896 v--;
2897 }
2898 while (m >= 0) {
2899 AppWindowToken wtoken = mAppTokens.get(m);
2900 if (!wtoken.removed) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002901 Slog.w(TAG, "Invalid internal token: " + wtoken.token + " @ " + m);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002902 }
2903 m--;
2904 }
2905 }
2906
2907 boolean checkCallingPermission(String permission, String func) {
2908 // Quick check: if the calling permission is me, it's all okay.
2909 if (Binder.getCallingPid() == Process.myPid()) {
2910 return true;
2911 }
Romain Guy06882f82009-06-10 13:36:04 -07002912
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002913 if (mContext.checkCallingPermission(permission)
2914 == PackageManager.PERMISSION_GRANTED) {
2915 return true;
2916 }
2917 String msg = "Permission Denial: " + func + " from pid="
2918 + Binder.getCallingPid()
2919 + ", uid=" + Binder.getCallingUid()
2920 + " requires " + permission;
Joe Onorato8a9b2202010-02-26 18:56:32 -08002921 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002922 return false;
2923 }
Romain Guy06882f82009-06-10 13:36:04 -07002924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002925 AppWindowToken findAppWindowToken(IBinder token) {
2926 WindowToken wtoken = mTokenMap.get(token);
2927 if (wtoken == null) {
2928 return null;
2929 }
2930 return wtoken.appWindowToken;
2931 }
Romain Guy06882f82009-06-10 13:36:04 -07002932
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002933 public void addWindowToken(IBinder token, int type) {
2934 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2935 "addWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002936 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002937 }
Romain Guy06882f82009-06-10 13:36:04 -07002938
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002939 synchronized(mWindowMap) {
2940 WindowToken wtoken = mTokenMap.get(token);
2941 if (wtoken != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002942 Slog.w(TAG, "Attempted to add existing input method token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002943 return;
2944 }
2945 wtoken = new WindowToken(token, type, true);
2946 mTokenMap.put(token, wtoken);
2947 mTokenList.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002948 if (type == TYPE_WALLPAPER) {
2949 mWallpaperTokens.add(wtoken);
2950 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002951 }
2952 }
Romain Guy06882f82009-06-10 13:36:04 -07002953
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002954 public void removeWindowToken(IBinder token) {
2955 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2956 "removeWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002957 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002958 }
2959
2960 final long origId = Binder.clearCallingIdentity();
2961 synchronized(mWindowMap) {
2962 WindowToken wtoken = mTokenMap.remove(token);
2963 mTokenList.remove(wtoken);
2964 if (wtoken != null) {
2965 boolean delayed = false;
2966 if (!wtoken.hidden) {
2967 wtoken.hidden = true;
Romain Guy06882f82009-06-10 13:36:04 -07002968
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002969 final int N = wtoken.windows.size();
2970 boolean changed = false;
Romain Guy06882f82009-06-10 13:36:04 -07002971
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002972 for (int i=0; i<N; i++) {
2973 WindowState win = wtoken.windows.get(i);
2974
2975 if (win.isAnimating()) {
2976 delayed = true;
2977 }
Romain Guy06882f82009-06-10 13:36:04 -07002978
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002979 if (win.isVisibleNow()) {
2980 applyAnimationLocked(win,
2981 WindowManagerPolicy.TRANSIT_EXIT, false);
2982 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2983 KeyWaiter.RETURN_NOTHING);
2984 changed = true;
2985 }
2986 }
2987
2988 if (changed) {
2989 mLayoutNeeded = true;
2990 performLayoutAndPlaceSurfacesLocked();
2991 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2992 }
Romain Guy06882f82009-06-10 13:36:04 -07002993
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002994 if (delayed) {
2995 mExitingTokens.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002996 } else if (wtoken.windowType == TYPE_WALLPAPER) {
2997 mWallpaperTokens.remove(wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002998 }
2999 }
Romain Guy06882f82009-06-10 13:36:04 -07003000
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003001 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003002 Slog.w(TAG, "Attempted to remove non-existing token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003003 }
3004 }
3005 Binder.restoreCallingIdentity(origId);
3006 }
3007
3008 public void addAppToken(int addPos, IApplicationToken token,
3009 int groupId, int requestedOrientation, boolean fullscreen) {
3010 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3011 "addAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003012 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003013 }
Romain Guy06882f82009-06-10 13:36:04 -07003014
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003015 synchronized(mWindowMap) {
3016 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
3017 if (wtoken != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003018 Slog.w(TAG, "Attempted to add existing app token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003019 return;
3020 }
3021 wtoken = new AppWindowToken(token);
3022 wtoken.groupId = groupId;
3023 wtoken.appFullscreen = fullscreen;
3024 wtoken.requestedOrientation = requestedOrientation;
3025 mAppTokens.add(addPos, wtoken);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003026 if (localLOGV) Slog.v(TAG, "Adding new app token: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003027 mTokenMap.put(token.asBinder(), wtoken);
3028 mTokenList.add(wtoken);
Romain Guy06882f82009-06-10 13:36:04 -07003029
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003030 // Application tokens start out hidden.
3031 wtoken.hidden = true;
3032 wtoken.hiddenRequested = true;
Romain Guy06882f82009-06-10 13:36:04 -07003033
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003034 //dump();
3035 }
3036 }
Romain Guy06882f82009-06-10 13:36:04 -07003037
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003038 public void setAppGroupId(IBinder token, int groupId) {
3039 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3040 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003041 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003042 }
3043
3044 synchronized(mWindowMap) {
3045 AppWindowToken wtoken = findAppWindowToken(token);
3046 if (wtoken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003047 Slog.w(TAG, "Attempted to set group id of non-existing app token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003048 return;
3049 }
3050 wtoken.groupId = groupId;
3051 }
3052 }
Romain Guy06882f82009-06-10 13:36:04 -07003053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003054 public int getOrientationFromWindowsLocked() {
3055 int pos = mWindows.size() - 1;
3056 while (pos >= 0) {
3057 WindowState wtoken = (WindowState) mWindows.get(pos);
3058 pos--;
3059 if (wtoken.mAppToken != null) {
3060 // We hit an application window. so the orientation will be determined by the
3061 // app window. No point in continuing further.
3062 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
3063 }
Christopher Tateb696aee2010-04-02 19:08:30 -07003064 if (!wtoken.isVisibleLw() || !wtoken.mPolicyVisibilityAfterAnim) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003065 continue;
3066 }
3067 int req = wtoken.mAttrs.screenOrientation;
3068 if((req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) ||
3069 (req == ActivityInfo.SCREEN_ORIENTATION_BEHIND)){
3070 continue;
3071 } else {
3072 return req;
3073 }
3074 }
3075 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
3076 }
Romain Guy06882f82009-06-10 13:36:04 -07003077
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003078 public int getOrientationFromAppTokensLocked() {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003079 int pos = mAppTokens.size() - 1;
3080 int curGroup = 0;
3081 int lastOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
3082 boolean findingBehind = false;
3083 boolean haveGroup = false;
3084 boolean lastFullscreen = false;
3085 while (pos >= 0) {
3086 AppWindowToken wtoken = mAppTokens.get(pos);
3087 pos--;
3088 // if we're about to tear down this window and not seek for
3089 // the behind activity, don't use it for orientation
3090 if (!findingBehind
3091 && (!wtoken.hidden && wtoken.hiddenRequested)) {
3092 continue;
3093 }
3094
3095 if (!haveGroup) {
3096 // We ignore any hidden applications on the top.
3097 if (wtoken.hiddenRequested || wtoken.willBeHidden) {
The Android Open Source Project10592532009-03-18 17:39:46 -07003098 continue;
3099 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003100 haveGroup = true;
3101 curGroup = wtoken.groupId;
3102 lastOrientation = wtoken.requestedOrientation;
3103 } else if (curGroup != wtoken.groupId) {
3104 // If we have hit a new application group, and the bottom
3105 // of the previous group didn't explicitly say to use
3106 // the orientation behind it, and the last app was
3107 // full screen, then we'll stick with the
3108 // user's orientation.
3109 if (lastOrientation != ActivityInfo.SCREEN_ORIENTATION_BEHIND
3110 && lastFullscreen) {
3111 return lastOrientation;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003112 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003113 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003114 int or = wtoken.requestedOrientation;
3115 // If this application is fullscreen, and didn't explicitly say
3116 // to use the orientation behind it, then just take whatever
3117 // orientation it has and ignores whatever is under it.
3118 lastFullscreen = wtoken.appFullscreen;
3119 if (lastFullscreen
3120 && or != ActivityInfo.SCREEN_ORIENTATION_BEHIND) {
3121 return or;
3122 }
3123 // If this application has requested an explicit orientation,
3124 // then use it.
3125 if (or == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE ||
3126 or == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ||
3127 or == ActivityInfo.SCREEN_ORIENTATION_SENSOR ||
3128 or == ActivityInfo.SCREEN_ORIENTATION_NOSENSOR ||
3129 or == ActivityInfo.SCREEN_ORIENTATION_USER) {
3130 return or;
3131 }
3132 findingBehind |= (or == ActivityInfo.SCREEN_ORIENTATION_BEHIND);
3133 }
3134 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003135 }
Romain Guy06882f82009-06-10 13:36:04 -07003136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003137 public Configuration updateOrientationFromAppTokens(
The Android Open Source Project10592532009-03-18 17:39:46 -07003138 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003139 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3140 "updateOrientationFromAppTokens()")) {
3141 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
3142 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003143
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003144 Configuration config = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003145 long ident = Binder.clearCallingIdentity();
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003146
3147 synchronized(mWindowMap) {
3148 if (updateOrientationFromAppTokensLocked()) {
3149 if (freezeThisOneIfNeeded != null) {
3150 AppWindowToken wtoken = findAppWindowToken(
3151 freezeThisOneIfNeeded);
3152 if (wtoken != null) {
3153 startAppFreezingScreenLocked(wtoken,
3154 ActivityInfo.CONFIG_ORIENTATION);
3155 }
3156 }
3157 config = computeNewConfigurationLocked();
3158
3159 } else if (currentConfig != null) {
3160 // No obvious action we need to take, but if our current
3161 // state mismatches the activity maanager's, update it
3162 mTempConfiguration.setToDefaults();
3163 if (computeNewConfigurationLocked(mTempConfiguration)) {
3164 if (currentConfig.diff(mTempConfiguration) != 0) {
3165 mWaitingForConfig = true;
3166 mLayoutNeeded = true;
3167 startFreezingDisplayLocked();
3168 config = new Configuration(mTempConfiguration);
3169 }
3170 }
3171 }
3172 }
3173
Dianne Hackborncfaef692009-06-15 14:24:44 -07003174 Binder.restoreCallingIdentity(ident);
3175 return config;
3176 }
3177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003178 /*
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003179 * Determine the new desired orientation of the display, returning
3180 * a non-null new Configuration if it has changed from the current
3181 * orientation. IF TRUE IS RETURNED SOMEONE MUST CALL
3182 * setNewConfiguration() TO TELL THE WINDOW MANAGER IT CAN UNFREEZE THE
3183 * SCREEN. This will typically be done for you if you call
3184 * sendNewConfiguration().
3185 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003186 * The orientation is computed from non-application windows first. If none of
3187 * the non-application windows specify orientation, the orientation is computed from
Romain Guy06882f82009-06-10 13:36:04 -07003188 * application tokens.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003189 * @see android.view.IWindowManager#updateOrientationFromAppTokens(
3190 * android.os.IBinder)
3191 */
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003192 boolean updateOrientationFromAppTokensLocked() {
Christopher Tateb696aee2010-04-02 19:08:30 -07003193 if (mDisplayFrozen) {
3194 // If the display is frozen, some activities may be in the middle
3195 // of restarting, and thus have removed their old window. If the
3196 // window has the flag to hide the lock screen, then the lock screen
3197 // can re-appear and inflict its own orientation on us. Keep the
3198 // orientation stable until this all settles down.
3199 return false;
3200 }
3201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003202 boolean changed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003203 long ident = Binder.clearCallingIdentity();
3204 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003205 int req = computeForcedAppOrientationLocked();
Romain Guy06882f82009-06-10 13:36:04 -07003206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003207 if (req != mForcedAppOrientation) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003208 mForcedAppOrientation = req;
3209 //send a message to Policy indicating orientation change to take
3210 //action like disabling/enabling sensors etc.,
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003211 mPolicy.setCurrentOrientationLw(req);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003212 if (setRotationUncheckedLocked(WindowManagerPolicy.USE_LAST_ROTATION,
3213 mLastRotationFlags | Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE)) {
3214 changed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003215 }
3216 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003217
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003218 return changed;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003219 } finally {
3220 Binder.restoreCallingIdentity(ident);
3221 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003222 }
Romain Guy06882f82009-06-10 13:36:04 -07003223
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003224 int computeForcedAppOrientationLocked() {
3225 int req = getOrientationFromWindowsLocked();
3226 if (req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
3227 req = getOrientationFromAppTokensLocked();
3228 }
3229 return req;
3230 }
Romain Guy06882f82009-06-10 13:36:04 -07003231
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003232 public void setNewConfiguration(Configuration config) {
3233 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3234 "setNewConfiguration()")) {
3235 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
3236 }
3237
3238 synchronized(mWindowMap) {
3239 mCurConfiguration = new Configuration(config);
3240 mWaitingForConfig = false;
3241 performLayoutAndPlaceSurfacesLocked();
3242 }
3243 }
3244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003245 public void setAppOrientation(IApplicationToken token, int requestedOrientation) {
3246 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3247 "setAppOrientation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003248 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003249 }
Romain Guy06882f82009-06-10 13:36:04 -07003250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003251 synchronized(mWindowMap) {
3252 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
3253 if (wtoken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003254 Slog.w(TAG, "Attempted to set orientation of non-existing app token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003255 return;
3256 }
Romain Guy06882f82009-06-10 13:36:04 -07003257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003258 wtoken.requestedOrientation = requestedOrientation;
3259 }
3260 }
Romain Guy06882f82009-06-10 13:36:04 -07003261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003262 public int getAppOrientation(IApplicationToken token) {
3263 synchronized(mWindowMap) {
3264 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
3265 if (wtoken == null) {
3266 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
3267 }
Romain Guy06882f82009-06-10 13:36:04 -07003268
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003269 return wtoken.requestedOrientation;
3270 }
3271 }
Romain Guy06882f82009-06-10 13:36:04 -07003272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003273 public void setFocusedApp(IBinder token, boolean moveFocusNow) {
3274 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3275 "setFocusedApp()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003276 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003277 }
3278
3279 synchronized(mWindowMap) {
3280 boolean changed = false;
3281 if (token == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003282 if (DEBUG_FOCUS) Slog.v(TAG, "Clearing focused app, was " + mFocusedApp);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003283 changed = mFocusedApp != null;
3284 mFocusedApp = null;
3285 mKeyWaiter.tickle();
3286 } else {
3287 AppWindowToken newFocus = findAppWindowToken(token);
3288 if (newFocus == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003289 Slog.w(TAG, "Attempted to set focus to non-existing app token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003290 return;
3291 }
3292 changed = mFocusedApp != newFocus;
3293 mFocusedApp = newFocus;
Joe Onorato8a9b2202010-02-26 18:56:32 -08003294 if (DEBUG_FOCUS) Slog.v(TAG, "Set focused app to: " + mFocusedApp);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003295 mKeyWaiter.tickle();
3296 }
3297
3298 if (moveFocusNow && changed) {
3299 final long origId = Binder.clearCallingIdentity();
3300 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3301 Binder.restoreCallingIdentity(origId);
3302 }
3303 }
3304 }
3305
3306 public void prepareAppTransition(int transit) {
3307 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3308 "prepareAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003309 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003310 }
Romain Guy06882f82009-06-10 13:36:04 -07003311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003312 synchronized(mWindowMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003313 if (DEBUG_APP_TRANSITIONS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003314 TAG, "Prepare app transition: transit=" + transit
3315 + " mNextAppTransition=" + mNextAppTransition);
Dianne Hackbornb601ce12010-03-01 23:36:02 -08003316 if (!mDisplayFrozen && mPolicy.isScreenOn()) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003317 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET
3318 || mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003319 mNextAppTransition = transit;
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07003320 } else if (transit == WindowManagerPolicy.TRANSIT_TASK_OPEN
3321 && mNextAppTransition == WindowManagerPolicy.TRANSIT_TASK_CLOSE) {
3322 // Opening a new task always supersedes a close for the anim.
3323 mNextAppTransition = transit;
3324 } else if (transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN
3325 && mNextAppTransition == WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE) {
3326 // Opening a new activity always supersedes a close for the anim.
3327 mNextAppTransition = transit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003328 }
3329 mAppTransitionReady = false;
3330 mAppTransitionTimeout = false;
3331 mStartingIconInTransition = false;
3332 mSkipAppTransitionAnimation = false;
3333 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
3334 mH.sendMessageDelayed(mH.obtainMessage(H.APP_TRANSITION_TIMEOUT),
3335 5000);
3336 }
3337 }
3338 }
3339
3340 public int getPendingAppTransition() {
3341 return mNextAppTransition;
3342 }
Romain Guy06882f82009-06-10 13:36:04 -07003343
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003344 public void overridePendingAppTransition(String packageName,
3345 int enterAnim, int exitAnim) {
Dianne Hackborn8b571a82009-09-25 16:09:43 -07003346 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003347 mNextAppTransitionPackage = packageName;
3348 mNextAppTransitionEnter = enterAnim;
3349 mNextAppTransitionExit = exitAnim;
3350 }
3351 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003353 public void executeAppTransition() {
3354 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3355 "executeAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003356 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003357 }
Romain Guy06882f82009-06-10 13:36:04 -07003358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003359 synchronized(mWindowMap) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003360 if (DEBUG_APP_TRANSITIONS) {
3361 RuntimeException e = new RuntimeException("here");
3362 e.fillInStackTrace();
Joe Onorato8a9b2202010-02-26 18:56:32 -08003363 Slog.w(TAG, "Execute app transition: mNextAppTransition="
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003364 + mNextAppTransition, e);
3365 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003366 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003367 mAppTransitionReady = true;
3368 final long origId = Binder.clearCallingIdentity();
3369 performLayoutAndPlaceSurfacesLocked();
3370 Binder.restoreCallingIdentity(origId);
3371 }
3372 }
3373 }
3374
3375 public void setAppStartingWindow(IBinder token, String pkg,
3376 int theme, CharSequence nonLocalizedLabel, int labelRes, int icon,
3377 IBinder transferFrom, boolean createIfNeeded) {
3378 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3379 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003380 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003381 }
3382
3383 synchronized(mWindowMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003384 if (DEBUG_STARTING_WINDOW) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003385 TAG, "setAppStartingIcon: token=" + token + " pkg=" + pkg
3386 + " transferFrom=" + transferFrom);
Romain Guy06882f82009-06-10 13:36:04 -07003387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003388 AppWindowToken wtoken = findAppWindowToken(token);
3389 if (wtoken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003390 Slog.w(TAG, "Attempted to set icon of non-existing app token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003391 return;
3392 }
3393
3394 // If the display is frozen, we won't do anything until the
3395 // actual window is displayed so there is no reason to put in
3396 // the starting window.
Dianne Hackbornde2606d2009-12-18 16:53:55 -08003397 if (mDisplayFrozen || !mPolicy.isScreenOn()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003398 return;
3399 }
Romain Guy06882f82009-06-10 13:36:04 -07003400
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003401 if (wtoken.startingData != null) {
3402 return;
3403 }
Romain Guy06882f82009-06-10 13:36:04 -07003404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003405 if (transferFrom != null) {
3406 AppWindowToken ttoken = findAppWindowToken(transferFrom);
3407 if (ttoken != null) {
3408 WindowState startingWindow = ttoken.startingWindow;
3409 if (startingWindow != null) {
3410 if (mStartingIconInTransition) {
3411 // In this case, the starting icon has already
3412 // been displayed, so start letting windows get
3413 // shown immediately without any more transitions.
3414 mSkipAppTransitionAnimation = true;
3415 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08003416 if (DEBUG_STARTING_WINDOW) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003417 "Moving existing starting from " + ttoken
3418 + " to " + wtoken);
3419 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07003420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003421 // Transfer the starting window over to the new
3422 // token.
3423 wtoken.startingData = ttoken.startingData;
3424 wtoken.startingView = ttoken.startingView;
3425 wtoken.startingWindow = startingWindow;
3426 ttoken.startingData = null;
3427 ttoken.startingView = null;
3428 ttoken.startingWindow = null;
3429 ttoken.startingMoved = true;
3430 startingWindow.mToken = wtoken;
Dianne Hackbornef49c572009-03-24 19:27:32 -07003431 startingWindow.mRootToken = wtoken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003432 startingWindow.mAppToken = wtoken;
Joe Onorato8a9b2202010-02-26 18:56:32 -08003433 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG,
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003434 "Removing starting window: " + startingWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003435 mWindows.remove(startingWindow);
3436 ttoken.windows.remove(startingWindow);
3437 ttoken.allAppWindows.remove(startingWindow);
3438 addWindowToListInOrderLocked(startingWindow, true);
Romain Guy06882f82009-06-10 13:36:04 -07003439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003440 // Propagate other interesting state between the
3441 // tokens. If the old token is displayed, we should
3442 // immediately force the new one to be displayed. If
3443 // it is animating, we need to move that animation to
3444 // the new one.
3445 if (ttoken.allDrawn) {
3446 wtoken.allDrawn = true;
3447 }
3448 if (ttoken.firstWindowDrawn) {
3449 wtoken.firstWindowDrawn = true;
3450 }
3451 if (!ttoken.hidden) {
3452 wtoken.hidden = false;
3453 wtoken.hiddenRequested = false;
3454 wtoken.willBeHidden = false;
3455 }
3456 if (wtoken.clientHidden != ttoken.clientHidden) {
3457 wtoken.clientHidden = ttoken.clientHidden;
3458 wtoken.sendAppVisibilityToClients();
3459 }
3460 if (ttoken.animation != null) {
3461 wtoken.animation = ttoken.animation;
3462 wtoken.animating = ttoken.animating;
3463 wtoken.animLayerAdjustment = ttoken.animLayerAdjustment;
3464 ttoken.animation = null;
3465 ttoken.animLayerAdjustment = 0;
3466 wtoken.updateLayers();
3467 ttoken.updateLayers();
3468 }
Romain Guy06882f82009-06-10 13:36:04 -07003469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003470 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003471 mLayoutNeeded = true;
3472 performLayoutAndPlaceSurfacesLocked();
3473 Binder.restoreCallingIdentity(origId);
3474 return;
3475 } else if (ttoken.startingData != null) {
3476 // The previous app was getting ready to show a
3477 // starting window, but hasn't yet done so. Steal it!
Joe Onorato8a9b2202010-02-26 18:56:32 -08003478 if (DEBUG_STARTING_WINDOW) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003479 "Moving pending starting from " + ttoken
3480 + " to " + wtoken);
3481 wtoken.startingData = ttoken.startingData;
3482 ttoken.startingData = null;
3483 ttoken.startingMoved = true;
3484 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3485 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3486 // want to process the message ASAP, before any other queued
3487 // messages.
3488 mH.sendMessageAtFrontOfQueue(m);
3489 return;
3490 }
3491 }
3492 }
3493
3494 // There is no existing starting window, and the caller doesn't
3495 // want us to create one, so that's it!
3496 if (!createIfNeeded) {
3497 return;
3498 }
Romain Guy06882f82009-06-10 13:36:04 -07003499
Dianne Hackborn284ac932009-08-28 10:34:25 -07003500 // If this is a translucent or wallpaper window, then don't
3501 // show a starting window -- the current effect (a full-screen
3502 // opaque starting window that fades away to the real contents
3503 // when it is ready) does not work for this.
3504 if (theme != 0) {
3505 AttributeCache.Entry ent = AttributeCache.instance().get(pkg, theme,
3506 com.android.internal.R.styleable.Window);
3507 if (ent.array.getBoolean(
3508 com.android.internal.R.styleable.Window_windowIsTranslucent, false)) {
3509 return;
3510 }
3511 if (ent.array.getBoolean(
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07003512 com.android.internal.R.styleable.Window_windowIsFloating, false)) {
3513 return;
3514 }
3515 if (ent.array.getBoolean(
Dianne Hackborn284ac932009-08-28 10:34:25 -07003516 com.android.internal.R.styleable.Window_windowShowWallpaper, false)) {
3517 return;
3518 }
3519 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003521 mStartingIconInTransition = true;
3522 wtoken.startingData = new StartingData(
3523 pkg, theme, nonLocalizedLabel,
3524 labelRes, icon);
3525 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3526 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3527 // want to process the message ASAP, before any other queued
3528 // messages.
3529 mH.sendMessageAtFrontOfQueue(m);
3530 }
3531 }
3532
3533 public void setAppWillBeHidden(IBinder token) {
3534 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3535 "setAppWillBeHidden()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003536 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003537 }
3538
3539 AppWindowToken wtoken;
3540
3541 synchronized(mWindowMap) {
3542 wtoken = findAppWindowToken(token);
3543 if (wtoken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003544 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 -08003545 return;
3546 }
3547 wtoken.willBeHidden = true;
3548 }
3549 }
Romain Guy06882f82009-06-10 13:36:04 -07003550
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003551 boolean setTokenVisibilityLocked(AppWindowToken wtoken, WindowManager.LayoutParams lp,
3552 boolean visible, int transit, boolean performLayout) {
3553 boolean delayed = false;
3554
3555 if (wtoken.clientHidden == visible) {
3556 wtoken.clientHidden = !visible;
3557 wtoken.sendAppVisibilityToClients();
3558 }
Romain Guy06882f82009-06-10 13:36:04 -07003559
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003560 wtoken.willBeHidden = false;
3561 if (wtoken.hidden == visible) {
3562 final int N = wtoken.allAppWindows.size();
3563 boolean changed = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -08003564 if (DEBUG_APP_TRANSITIONS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003565 TAG, "Changing app " + wtoken + " hidden=" + wtoken.hidden
3566 + " performLayout=" + performLayout);
Romain Guy06882f82009-06-10 13:36:04 -07003567
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003568 boolean runningAppAnimation = false;
Romain Guy06882f82009-06-10 13:36:04 -07003569
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003570 if (transit != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003571 if (wtoken.animation == sDummyAnimation) {
3572 wtoken.animation = null;
3573 }
3574 applyAnimationLocked(wtoken, lp, transit, visible);
3575 changed = true;
3576 if (wtoken.animation != null) {
3577 delayed = runningAppAnimation = true;
3578 }
3579 }
Romain Guy06882f82009-06-10 13:36:04 -07003580
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003581 for (int i=0; i<N; i++) {
3582 WindowState win = wtoken.allAppWindows.get(i);
3583 if (win == wtoken.startingWindow) {
3584 continue;
3585 }
3586
3587 if (win.isAnimating()) {
3588 delayed = true;
3589 }
Romain Guy06882f82009-06-10 13:36:04 -07003590
Joe Onorato8a9b2202010-02-26 18:56:32 -08003591 //Slog.i(TAG, "Window " + win + ": vis=" + win.isVisible());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003592 //win.dump(" ");
3593 if (visible) {
3594 if (!win.isVisibleNow()) {
3595 if (!runningAppAnimation) {
3596 applyAnimationLocked(win,
3597 WindowManagerPolicy.TRANSIT_ENTER, true);
3598 }
3599 changed = true;
3600 }
3601 } else if (win.isVisibleNow()) {
3602 if (!runningAppAnimation) {
3603 applyAnimationLocked(win,
3604 WindowManagerPolicy.TRANSIT_EXIT, false);
3605 }
3606 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
3607 KeyWaiter.RETURN_NOTHING);
3608 changed = true;
3609 }
3610 }
3611
3612 wtoken.hidden = wtoken.hiddenRequested = !visible;
3613 if (!visible) {
3614 unsetAppFreezingScreenLocked(wtoken, true, true);
3615 } else {
3616 // If we are being set visible, and the starting window is
3617 // not yet displayed, then make sure it doesn't get displayed.
3618 WindowState swin = wtoken.startingWindow;
3619 if (swin != null && (swin.mDrawPending
3620 || swin.mCommitDrawPending)) {
3621 swin.mPolicyVisibility = false;
3622 swin.mPolicyVisibilityAfterAnim = false;
3623 }
3624 }
Romain Guy06882f82009-06-10 13:36:04 -07003625
Joe Onorato8a9b2202010-02-26 18:56:32 -08003626 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG, "setTokenVisibilityLocked: " + wtoken
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003627 + ": hidden=" + wtoken.hidden + " hiddenRequested="
3628 + wtoken.hiddenRequested);
Romain Guy06882f82009-06-10 13:36:04 -07003629
Dianne Hackborn9b52a212009-12-11 14:51:35 -08003630 if (changed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003631 mLayoutNeeded = true;
Dianne Hackborn9b52a212009-12-11 14:51:35 -08003632 if (performLayout) {
3633 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
3634 performLayoutAndPlaceSurfacesLocked();
3635 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003636 }
3637 }
3638
3639 if (wtoken.animation != null) {
3640 delayed = true;
3641 }
Romain Guy06882f82009-06-10 13:36:04 -07003642
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003643 return delayed;
3644 }
3645
3646 public void setAppVisibility(IBinder token, boolean visible) {
3647 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3648 "setAppVisibility()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003649 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003650 }
3651
3652 AppWindowToken wtoken;
3653
3654 synchronized(mWindowMap) {
3655 wtoken = findAppWindowToken(token);
3656 if (wtoken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003657 Slog.w(TAG, "Attempted to set visibility of non-existing app token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003658 return;
3659 }
3660
3661 if (DEBUG_APP_TRANSITIONS || DEBUG_ORIENTATION) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08003662 RuntimeException e = null;
3663 if (!HIDE_STACK_CRAWLS) {
3664 e = new RuntimeException();
3665 e.fillInStackTrace();
3666 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08003667 Slog.v(TAG, "setAppVisibility(" + token + ", " + visible
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003668 + "): mNextAppTransition=" + mNextAppTransition
3669 + " hidden=" + wtoken.hidden
3670 + " hiddenRequested=" + wtoken.hiddenRequested, e);
3671 }
Romain Guy06882f82009-06-10 13:36:04 -07003672
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003673 // If we are preparing an app transition, then delay changing
3674 // the visibility of this token until we execute that transition.
Dianne Hackbornb601ce12010-03-01 23:36:02 -08003675 if (!mDisplayFrozen && mPolicy.isScreenOn()
3676 && mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003677 // Already in requested state, don't do anything more.
3678 if (wtoken.hiddenRequested != visible) {
3679 return;
3680 }
3681 wtoken.hiddenRequested = !visible;
Romain Guy06882f82009-06-10 13:36:04 -07003682
Joe Onorato8a9b2202010-02-26 18:56:32 -08003683 if (DEBUG_APP_TRANSITIONS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003684 TAG, "Setting dummy animation on: " + wtoken);
3685 wtoken.setDummyAnimation();
3686 mOpeningApps.remove(wtoken);
3687 mClosingApps.remove(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003688 wtoken.waitingToShow = wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003689 wtoken.inPendingTransaction = true;
3690 if (visible) {
3691 mOpeningApps.add(wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003692 wtoken.startingDisplayed = false;
3693 wtoken.startingMoved = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003694
Dianne Hackborn195f6a02009-11-24 11:26:00 -08003695 // If the token is currently hidden (should be the
3696 // common case), then we need to set up to wait for
3697 // its windows to be ready.
3698 if (wtoken.hidden) {
3699 wtoken.allDrawn = false;
3700 wtoken.waitingToShow = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003701
Dianne Hackborn195f6a02009-11-24 11:26:00 -08003702 if (wtoken.clientHidden) {
3703 // In the case where we are making an app visible
3704 // but holding off for a transition, we still need
3705 // to tell the client to make its windows visible so
3706 // they get drawn. Otherwise, we will wait on
3707 // performing the transition until all windows have
3708 // been drawn, they never will be, and we are sad.
3709 wtoken.clientHidden = false;
3710 wtoken.sendAppVisibilityToClients();
3711 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003712 }
3713 } else {
3714 mClosingApps.add(wtoken);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003715
Dianne Hackborn195f6a02009-11-24 11:26:00 -08003716 // If the token is currently visible (should be the
3717 // common case), then set up to wait for it to be hidden.
3718 if (!wtoken.hidden) {
3719 wtoken.waitingToHide = true;
3720 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003721 }
3722 return;
3723 }
Romain Guy06882f82009-06-10 13:36:04 -07003724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003725 final long origId = Binder.clearCallingIdentity();
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003726 setTokenVisibilityLocked(wtoken, null, visible, WindowManagerPolicy.TRANSIT_UNSET, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003727 wtoken.updateReportedVisibilityLocked();
3728 Binder.restoreCallingIdentity(origId);
3729 }
3730 }
3731
3732 void unsetAppFreezingScreenLocked(AppWindowToken wtoken,
3733 boolean unfreezeSurfaceNow, boolean force) {
3734 if (wtoken.freezingScreen) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003735 if (DEBUG_ORIENTATION) Slog.v(TAG, "Clear freezing of " + wtoken
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003736 + " force=" + force);
3737 final int N = wtoken.allAppWindows.size();
3738 boolean unfrozeWindows = false;
3739 for (int i=0; i<N; i++) {
3740 WindowState w = wtoken.allAppWindows.get(i);
3741 if (w.mAppFreezing) {
3742 w.mAppFreezing = false;
3743 if (w.mSurface != null && !w.mOrientationChanging) {
3744 w.mOrientationChanging = true;
3745 }
3746 unfrozeWindows = true;
3747 }
3748 }
3749 if (force || unfrozeWindows) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003750 if (DEBUG_ORIENTATION) Slog.v(TAG, "No longer freezing: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003751 wtoken.freezingScreen = false;
3752 mAppsFreezingScreen--;
3753 }
3754 if (unfreezeSurfaceNow) {
3755 if (unfrozeWindows) {
3756 mLayoutNeeded = true;
3757 performLayoutAndPlaceSurfacesLocked();
3758 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003759 stopFreezingDisplayLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003760 }
3761 }
3762 }
Romain Guy06882f82009-06-10 13:36:04 -07003763
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003764 public void startAppFreezingScreenLocked(AppWindowToken wtoken,
3765 int configChanges) {
3766 if (DEBUG_ORIENTATION) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08003767 RuntimeException e = null;
3768 if (!HIDE_STACK_CRAWLS) {
3769 e = new RuntimeException();
3770 e.fillInStackTrace();
3771 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08003772 Slog.i(TAG, "Set freezing of " + wtoken.appToken
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003773 + ": hidden=" + wtoken.hidden + " freezing="
3774 + wtoken.freezingScreen, e);
3775 }
3776 if (!wtoken.hiddenRequested) {
3777 if (!wtoken.freezingScreen) {
3778 wtoken.freezingScreen = true;
3779 mAppsFreezingScreen++;
3780 if (mAppsFreezingScreen == 1) {
3781 startFreezingDisplayLocked();
3782 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
3783 mH.sendMessageDelayed(mH.obtainMessage(H.APP_FREEZE_TIMEOUT),
3784 5000);
3785 }
3786 }
3787 final int N = wtoken.allAppWindows.size();
3788 for (int i=0; i<N; i++) {
3789 WindowState w = wtoken.allAppWindows.get(i);
3790 w.mAppFreezing = true;
3791 }
3792 }
3793 }
Romain Guy06882f82009-06-10 13:36:04 -07003794
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003795 public void startAppFreezingScreen(IBinder token, int configChanges) {
3796 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3797 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003798 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003799 }
3800
3801 synchronized(mWindowMap) {
Dianne Hackbornb601ce12010-03-01 23:36:02 -08003802 if (configChanges == 0 && !mDisplayFrozen && mPolicy.isScreenOn()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003803 if (DEBUG_ORIENTATION) Slog.v(TAG, "Skipping set freeze of " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003804 return;
3805 }
Romain Guy06882f82009-06-10 13:36:04 -07003806
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003807 AppWindowToken wtoken = findAppWindowToken(token);
3808 if (wtoken == null || wtoken.appToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003809 Slog.w(TAG, "Attempted to freeze screen with non-existing app token: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003810 return;
3811 }
3812 final long origId = Binder.clearCallingIdentity();
3813 startAppFreezingScreenLocked(wtoken, configChanges);
3814 Binder.restoreCallingIdentity(origId);
3815 }
3816 }
Romain Guy06882f82009-06-10 13:36:04 -07003817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003818 public void stopAppFreezingScreen(IBinder token, boolean force) {
3819 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3820 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003821 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003822 }
3823
3824 synchronized(mWindowMap) {
3825 AppWindowToken wtoken = findAppWindowToken(token);
3826 if (wtoken == null || wtoken.appToken == null) {
3827 return;
3828 }
3829 final long origId = Binder.clearCallingIdentity();
Joe Onorato8a9b2202010-02-26 18:56:32 -08003830 if (DEBUG_ORIENTATION) Slog.v(TAG, "Clear freezing of " + token
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003831 + ": hidden=" + wtoken.hidden + " freezing=" + wtoken.freezingScreen);
3832 unsetAppFreezingScreenLocked(wtoken, true, force);
3833 Binder.restoreCallingIdentity(origId);
3834 }
3835 }
Romain Guy06882f82009-06-10 13:36:04 -07003836
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003837 public void removeAppToken(IBinder token) {
3838 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3839 "removeAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003840 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003841 }
3842
3843 AppWindowToken wtoken = null;
3844 AppWindowToken startingToken = null;
3845 boolean delayed = false;
3846
3847 final long origId = Binder.clearCallingIdentity();
3848 synchronized(mWindowMap) {
3849 WindowToken basewtoken = mTokenMap.remove(token);
3850 mTokenList.remove(basewtoken);
3851 if (basewtoken != null && (wtoken=basewtoken.appWindowToken) != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003852 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG, "Removing app token: " + wtoken);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003853 delayed = setTokenVisibilityLocked(wtoken, null, false, WindowManagerPolicy.TRANSIT_UNSET, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003854 wtoken.inPendingTransaction = false;
3855 mOpeningApps.remove(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003856 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003857 if (mClosingApps.contains(wtoken)) {
3858 delayed = true;
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003859 } else if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003860 mClosingApps.add(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003861 wtoken.waitingToHide = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003862 delayed = true;
3863 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08003864 if (DEBUG_APP_TRANSITIONS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003865 TAG, "Removing app " + wtoken + " delayed=" + delayed
3866 + " animation=" + wtoken.animation
3867 + " animating=" + wtoken.animating);
3868 if (delayed) {
3869 // set the token aside because it has an active animation to be finished
3870 mExitingAppTokens.add(wtoken);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003871 } else {
3872 // Make sure there is no animation running on this token,
3873 // so any windows associated with it will be removed as
3874 // soon as their animations are complete
3875 wtoken.animation = null;
3876 wtoken.animating = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003877 }
3878 mAppTokens.remove(wtoken);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003879 if (mLastEnterAnimToken == wtoken) {
3880 mLastEnterAnimToken = null;
3881 mLastEnterAnimParams = null;
3882 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003883 wtoken.removed = true;
3884 if (wtoken.startingData != null) {
3885 startingToken = wtoken;
3886 }
3887 unsetAppFreezingScreenLocked(wtoken, true, true);
3888 if (mFocusedApp == wtoken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003889 if (DEBUG_FOCUS) Slog.v(TAG, "Removing focused app token:" + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003890 mFocusedApp = null;
3891 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3892 mKeyWaiter.tickle();
3893 }
3894 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003895 Slog.w(TAG, "Attempted to remove non-existing app token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003896 }
Romain Guy06882f82009-06-10 13:36:04 -07003897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003898 if (!delayed && wtoken != null) {
3899 wtoken.updateReportedVisibilityLocked();
3900 }
3901 }
3902 Binder.restoreCallingIdentity(origId);
3903
3904 if (startingToken != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003905 if (DEBUG_STARTING_WINDOW) Slog.v(TAG, "Schedule remove starting "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003906 + startingToken + ": app token removed");
3907 Message m = mH.obtainMessage(H.REMOVE_STARTING, startingToken);
3908 mH.sendMessage(m);
3909 }
3910 }
3911
3912 private boolean tmpRemoveAppWindowsLocked(WindowToken token) {
3913 final int NW = token.windows.size();
3914 for (int i=0; i<NW; i++) {
3915 WindowState win = token.windows.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003916 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "Tmp removing app window " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003917 mWindows.remove(win);
3918 int j = win.mChildWindows.size();
3919 while (j > 0) {
3920 j--;
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003921 WindowState cwin = (WindowState)win.mChildWindows.get(j);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003922 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG,
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003923 "Tmp removing child window " + cwin);
3924 mWindows.remove(cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003925 }
3926 }
3927 return NW > 0;
3928 }
3929
3930 void dumpAppTokensLocked() {
3931 for (int i=mAppTokens.size()-1; i>=0; i--) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003932 Slog.v(TAG, " #" + i + ": " + mAppTokens.get(i).token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003933 }
3934 }
Romain Guy06882f82009-06-10 13:36:04 -07003935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003936 void dumpWindowsLocked() {
3937 for (int i=mWindows.size()-1; i>=0; i--) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003938 Slog.v(TAG, " #" + i + ": " + mWindows.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003939 }
3940 }
Romain Guy06882f82009-06-10 13:36:04 -07003941
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003942 private int findWindowOffsetLocked(int tokenPos) {
3943 final int NW = mWindows.size();
3944
3945 if (tokenPos >= mAppTokens.size()) {
3946 int i = NW;
3947 while (i > 0) {
3948 i--;
3949 WindowState win = (WindowState)mWindows.get(i);
3950 if (win.getAppToken() != null) {
3951 return i+1;
3952 }
3953 }
3954 }
3955
3956 while (tokenPos > 0) {
3957 // Find the first app token below the new position that has
3958 // a window displayed.
3959 final AppWindowToken wtoken = mAppTokens.get(tokenPos-1);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003960 if (DEBUG_REORDER) Slog.v(TAG, "Looking for lower windows @ "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003961 + tokenPos + " -- " + wtoken.token);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003962 if (wtoken.sendingToBottom) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003963 if (DEBUG_REORDER) Slog.v(TAG,
Dianne Hackborna8f60182009-09-01 19:01:50 -07003964 "Skipping token -- currently sending to bottom");
3965 tokenPos--;
3966 continue;
3967 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003968 int i = wtoken.windows.size();
3969 while (i > 0) {
3970 i--;
3971 WindowState win = wtoken.windows.get(i);
3972 int j = win.mChildWindows.size();
3973 while (j > 0) {
3974 j--;
3975 WindowState cwin = (WindowState)win.mChildWindows.get(j);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003976 if (cwin.mSubLayer >= 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003977 for (int pos=NW-1; pos>=0; pos--) {
3978 if (mWindows.get(pos) == cwin) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003979 if (DEBUG_REORDER) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003980 "Found child win @" + (pos+1));
3981 return pos+1;
3982 }
3983 }
3984 }
3985 }
3986 for (int pos=NW-1; pos>=0; pos--) {
3987 if (mWindows.get(pos) == win) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003988 if (DEBUG_REORDER) Slog.v(TAG, "Found win @" + (pos+1));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003989 return pos+1;
3990 }
3991 }
3992 }
3993 tokenPos--;
3994 }
3995
3996 return 0;
3997 }
3998
3999 private final int reAddWindowLocked(int index, WindowState win) {
4000 final int NCW = win.mChildWindows.size();
4001 boolean added = false;
4002 for (int j=0; j<NCW; j++) {
4003 WindowState cwin = (WindowState)win.mChildWindows.get(j);
4004 if (!added && cwin.mSubLayer >= 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004005 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "Re-adding child window at "
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07004006 + index + ": " + cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004007 mWindows.add(index, win);
4008 index++;
4009 added = true;
4010 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08004011 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "Re-adding window at "
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07004012 + index + ": " + cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004013 mWindows.add(index, cwin);
4014 index++;
4015 }
4016 if (!added) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004017 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "Re-adding window at "
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07004018 + index + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004019 mWindows.add(index, win);
4020 index++;
4021 }
4022 return index;
4023 }
Romain Guy06882f82009-06-10 13:36:04 -07004024
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004025 private final int reAddAppWindowsLocked(int index, WindowToken token) {
4026 final int NW = token.windows.size();
4027 for (int i=0; i<NW; i++) {
4028 index = reAddWindowLocked(index, token.windows.get(i));
4029 }
4030 return index;
4031 }
4032
4033 public void moveAppToken(int index, IBinder token) {
4034 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4035 "moveAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004036 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004037 }
4038
4039 synchronized(mWindowMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004040 if (DEBUG_REORDER) Slog.v(TAG, "Initial app tokens:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004041 if (DEBUG_REORDER) dumpAppTokensLocked();
4042 final AppWindowToken wtoken = findAppWindowToken(token);
4043 if (wtoken == null || !mAppTokens.remove(wtoken)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004044 Slog.w(TAG, "Attempting to reorder token that doesn't exist: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004045 + token + " (" + wtoken + ")");
4046 return;
4047 }
4048 mAppTokens.add(index, wtoken);
Joe Onorato8a9b2202010-02-26 18:56:32 -08004049 if (DEBUG_REORDER) Slog.v(TAG, "Moved " + token + " to " + index + ":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004050 if (DEBUG_REORDER) dumpAppTokensLocked();
Romain Guy06882f82009-06-10 13:36:04 -07004051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004052 final long origId = Binder.clearCallingIdentity();
Joe Onorato8a9b2202010-02-26 18:56:32 -08004053 if (DEBUG_REORDER) Slog.v(TAG, "Removing windows in " + token + ":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004054 if (DEBUG_REORDER) dumpWindowsLocked();
4055 if (tmpRemoveAppWindowsLocked(wtoken)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004056 if (DEBUG_REORDER) Slog.v(TAG, "Adding windows back in:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004057 if (DEBUG_REORDER) dumpWindowsLocked();
4058 reAddAppWindowsLocked(findWindowOffsetLocked(index), wtoken);
Joe Onorato8a9b2202010-02-26 18:56:32 -08004059 if (DEBUG_REORDER) Slog.v(TAG, "Final window list:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004060 if (DEBUG_REORDER) dumpWindowsLocked();
4061 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004062 mLayoutNeeded = true;
4063 performLayoutAndPlaceSurfacesLocked();
4064 }
4065 Binder.restoreCallingIdentity(origId);
4066 }
4067 }
4068
4069 private void removeAppTokensLocked(List<IBinder> tokens) {
4070 // XXX This should be done more efficiently!
4071 // (take advantage of the fact that both lists should be
4072 // ordered in the same way.)
4073 int N = tokens.size();
4074 for (int i=0; i<N; i++) {
4075 IBinder token = tokens.get(i);
4076 final AppWindowToken wtoken = findAppWindowToken(token);
4077 if (!mAppTokens.remove(wtoken)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004078 Slog.w(TAG, "Attempting to reorder token that doesn't exist: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004079 + token + " (" + wtoken + ")");
4080 i--;
4081 N--;
4082 }
4083 }
4084 }
4085
Dianne Hackborna8f60182009-09-01 19:01:50 -07004086 private void moveAppWindowsLocked(AppWindowToken wtoken, int tokenPos,
4087 boolean updateFocusAndLayout) {
4088 // First remove all of the windows from the list.
4089 tmpRemoveAppWindowsLocked(wtoken);
4090
4091 // Where to start adding?
4092 int pos = findWindowOffsetLocked(tokenPos);
4093
4094 // And now add them back at the correct place.
4095 pos = reAddAppWindowsLocked(pos, wtoken);
4096
4097 if (updateFocusAndLayout) {
4098 if (!updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
4099 assignLayersLocked();
4100 }
4101 mLayoutNeeded = true;
4102 performLayoutAndPlaceSurfacesLocked();
4103 }
4104 }
4105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004106 private void moveAppWindowsLocked(List<IBinder> tokens, int tokenPos) {
4107 // First remove all of the windows from the list.
4108 final int N = tokens.size();
4109 int i;
4110 for (i=0; i<N; i++) {
4111 WindowToken token = mTokenMap.get(tokens.get(i));
4112 if (token != null) {
4113 tmpRemoveAppWindowsLocked(token);
4114 }
4115 }
4116
4117 // Where to start adding?
4118 int pos = findWindowOffsetLocked(tokenPos);
4119
4120 // And now add them back at the correct place.
4121 for (i=0; i<N; i++) {
4122 WindowToken token = mTokenMap.get(tokens.get(i));
4123 if (token != null) {
4124 pos = reAddAppWindowsLocked(pos, token);
4125 }
4126 }
4127
Dianne Hackborna8f60182009-09-01 19:01:50 -07004128 if (!updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
4129 assignLayersLocked();
4130 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004131 mLayoutNeeded = true;
4132 performLayoutAndPlaceSurfacesLocked();
4133
4134 //dump();
4135 }
4136
4137 public void moveAppTokensToTop(List<IBinder> tokens) {
4138 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4139 "moveAppTokensToTop()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004140 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004141 }
4142
4143 final long origId = Binder.clearCallingIdentity();
4144 synchronized(mWindowMap) {
4145 removeAppTokensLocked(tokens);
4146 final int N = tokens.size();
4147 for (int i=0; i<N; i++) {
4148 AppWindowToken wt = findAppWindowToken(tokens.get(i));
4149 if (wt != null) {
4150 mAppTokens.add(wt);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004151 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004152 mToTopApps.remove(wt);
4153 mToBottomApps.remove(wt);
4154 mToTopApps.add(wt);
4155 wt.sendingToBottom = false;
4156 wt.sendingToTop = true;
4157 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004158 }
4159 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004160
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004161 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004162 moveAppWindowsLocked(tokens, mAppTokens.size());
4163 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004164 }
4165 Binder.restoreCallingIdentity(origId);
4166 }
4167
4168 public void moveAppTokensToBottom(List<IBinder> tokens) {
4169 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4170 "moveAppTokensToBottom()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004171 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004172 }
4173
4174 final long origId = Binder.clearCallingIdentity();
4175 synchronized(mWindowMap) {
4176 removeAppTokensLocked(tokens);
4177 final int N = tokens.size();
4178 int pos = 0;
4179 for (int i=0; i<N; i++) {
4180 AppWindowToken wt = findAppWindowToken(tokens.get(i));
4181 if (wt != null) {
4182 mAppTokens.add(pos, wt);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004183 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004184 mToTopApps.remove(wt);
4185 mToBottomApps.remove(wt);
4186 mToBottomApps.add(i, wt);
4187 wt.sendingToTop = false;
4188 wt.sendingToBottom = true;
4189 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004190 pos++;
4191 }
4192 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004193
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004194 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004195 moveAppWindowsLocked(tokens, 0);
4196 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004197 }
4198 Binder.restoreCallingIdentity(origId);
4199 }
4200
4201 // -------------------------------------------------------------
4202 // Misc IWindowSession methods
4203 // -------------------------------------------------------------
Romain Guy06882f82009-06-10 13:36:04 -07004204
Jim Miller284b62e2010-06-08 14:27:42 -07004205 private boolean shouldAllowDisableKeyguard()
Jim Millerd6b57052010-06-07 17:52:42 -07004206 {
Jim Miller284b62e2010-06-08 14:27:42 -07004207 // We fail safe and prevent disabling keyguard in the unlikely event this gets
4208 // called before DevicePolicyManagerService has started.
4209 if (mAllowDisableKeyguard == ALLOW_DISABLE_UNKNOWN) {
4210 DevicePolicyManager dpm = (DevicePolicyManager) mContext.getSystemService(
4211 Context.DEVICE_POLICY_SERVICE);
4212 if (dpm != null) {
4213 mAllowDisableKeyguard = dpm.getPasswordQuality(null)
4214 == DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED ?
4215 ALLOW_DISABLE_YES : ALLOW_DISABLE_NO;
4216 }
Jim Millerd6b57052010-06-07 17:52:42 -07004217 }
Jim Miller284b62e2010-06-08 14:27:42 -07004218 return mAllowDisableKeyguard == ALLOW_DISABLE_YES;
Jim Millerd6b57052010-06-07 17:52:42 -07004219 }
4220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004221 public void disableKeyguard(IBinder token, String tag) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04004222 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004223 != PackageManager.PERMISSION_GRANTED) {
4224 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
4225 }
Jim Millerd6b57052010-06-07 17:52:42 -07004226
Jim Miller284b62e2010-06-08 14:27:42 -07004227 synchronized (mKeyguardTokenWatcher) {
4228 mKeyguardTokenWatcher.acquire(token, tag);
Mike Lockwooddd884682009-10-11 16:57:08 -04004229 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004230 }
4231
4232 public void reenableKeyguard(IBinder token) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04004233 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004234 != PackageManager.PERMISSION_GRANTED) {
4235 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
4236 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004237
Jim Miller284b62e2010-06-08 14:27:42 -07004238 synchronized (mKeyguardTokenWatcher) {
4239 mKeyguardTokenWatcher.release(token);
Jim Millerd6b57052010-06-07 17:52:42 -07004240
Jim Miller284b62e2010-06-08 14:27:42 -07004241 if (!mKeyguardTokenWatcher.isAcquired()) {
4242 // If we are the last one to reenable the keyguard wait until
4243 // we have actually finished reenabling until returning.
4244 // It is possible that reenableKeyguard() can be called before
4245 // the previous disableKeyguard() is handled, in which case
4246 // neither mKeyguardTokenWatcher.acquired() or released() would
4247 // be called. In that case mKeyguardDisabled will be false here
4248 // and we have nothing to wait for.
4249 while (mKeyguardDisabled) {
4250 try {
4251 mKeyguardTokenWatcher.wait();
4252 } catch (InterruptedException e) {
4253 Thread.currentThread().interrupt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004254 }
4255 }
4256 }
4257 }
4258 }
4259
4260 /**
4261 * @see android.app.KeyguardManager#exitKeyguardSecurely
4262 */
4263 public void exitKeyguardSecurely(final IOnKeyguardExitResult callback) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04004264 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004265 != PackageManager.PERMISSION_GRANTED) {
4266 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
4267 }
4268 mPolicy.exitKeyguardSecurely(new WindowManagerPolicy.OnKeyguardExitResult() {
4269 public void onKeyguardExitResult(boolean success) {
4270 try {
4271 callback.onKeyguardExitResult(success);
4272 } catch (RemoteException e) {
4273 // Client has died, we don't care.
4274 }
4275 }
4276 });
4277 }
4278
4279 public boolean inKeyguardRestrictedInputMode() {
4280 return mPolicy.inKeyguardRestrictedKeyInputMode();
4281 }
Romain Guy06882f82009-06-10 13:36:04 -07004282
Dianne Hackbornffa42482009-09-23 22:20:11 -07004283 public void closeSystemDialogs(String reason) {
4284 synchronized(mWindowMap) {
4285 for (int i=mWindows.size()-1; i>=0; i--) {
4286 WindowState w = (WindowState)mWindows.get(i);
4287 if (w.mSurface != null) {
4288 try {
4289 w.mClient.closeSystemDialogs(reason);
4290 } catch (RemoteException e) {
4291 }
4292 }
4293 }
4294 }
4295 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004297 static float fixScale(float scale) {
4298 if (scale < 0) scale = 0;
4299 else if (scale > 20) scale = 20;
4300 return Math.abs(scale);
4301 }
Romain Guy06882f82009-06-10 13:36:04 -07004302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004303 public void setAnimationScale(int which, float scale) {
4304 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
4305 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004306 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004307 }
4308
4309 if (scale < 0) scale = 0;
4310 else if (scale > 20) scale = 20;
4311 scale = Math.abs(scale);
4312 switch (which) {
4313 case 0: mWindowAnimationScale = fixScale(scale); break;
4314 case 1: mTransitionAnimationScale = fixScale(scale); break;
4315 }
Romain Guy06882f82009-06-10 13:36:04 -07004316
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004317 // Persist setting
4318 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
4319 }
Romain Guy06882f82009-06-10 13:36:04 -07004320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004321 public void setAnimationScales(float[] scales) {
4322 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
4323 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004324 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004325 }
4326
4327 if (scales != null) {
4328 if (scales.length >= 1) {
4329 mWindowAnimationScale = fixScale(scales[0]);
4330 }
4331 if (scales.length >= 2) {
4332 mTransitionAnimationScale = fixScale(scales[1]);
4333 }
4334 }
Romain Guy06882f82009-06-10 13:36:04 -07004335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004336 // Persist setting
4337 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
4338 }
Romain Guy06882f82009-06-10 13:36:04 -07004339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004340 public float getAnimationScale(int which) {
4341 switch (which) {
4342 case 0: return mWindowAnimationScale;
4343 case 1: return mTransitionAnimationScale;
4344 }
4345 return 0;
4346 }
Romain Guy06882f82009-06-10 13:36:04 -07004347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004348 public float[] getAnimationScales() {
4349 return new float[] { mWindowAnimationScale, mTransitionAnimationScale };
4350 }
Romain Guy06882f82009-06-10 13:36:04 -07004351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004352 public int getSwitchState(int sw) {
4353 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4354 "getSwitchState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004355 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004356 }
4357 return KeyInputQueue.getSwitchState(sw);
4358 }
Romain Guy06882f82009-06-10 13:36:04 -07004359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004360 public int getSwitchStateForDevice(int devid, int sw) {
4361 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4362 "getSwitchStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004363 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004364 }
4365 return KeyInputQueue.getSwitchState(devid, sw);
4366 }
Romain Guy06882f82009-06-10 13:36:04 -07004367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004368 public int getScancodeState(int sw) {
4369 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4370 "getScancodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004371 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004372 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004373 return mQueue.getScancodeState(sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004374 }
Romain Guy06882f82009-06-10 13:36:04 -07004375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004376 public int getScancodeStateForDevice(int devid, int sw) {
4377 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4378 "getScancodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004379 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004380 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004381 return mQueue.getScancodeState(devid, sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004382 }
Romain Guy06882f82009-06-10 13:36:04 -07004383
Dianne Hackborn1d62ea92009-11-17 12:49:50 -08004384 public int getTrackballScancodeState(int sw) {
4385 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4386 "getTrackballScancodeState()")) {
4387 throw new SecurityException("Requires READ_INPUT_STATE permission");
4388 }
4389 return mQueue.getTrackballScancodeState(sw);
4390 }
4391
4392 public int getDPadScancodeState(int sw) {
4393 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4394 "getDPadScancodeState()")) {
4395 throw new SecurityException("Requires READ_INPUT_STATE permission");
4396 }
4397 return mQueue.getDPadScancodeState(sw);
4398 }
4399
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004400 public int getKeycodeState(int sw) {
4401 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4402 "getKeycodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004403 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004404 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004405 return mQueue.getKeycodeState(sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004406 }
Romain Guy06882f82009-06-10 13:36:04 -07004407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004408 public int getKeycodeStateForDevice(int devid, int sw) {
4409 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4410 "getKeycodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004411 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004412 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004413 return mQueue.getKeycodeState(devid, sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004414 }
Romain Guy06882f82009-06-10 13:36:04 -07004415
Dianne Hackborn1d62ea92009-11-17 12:49:50 -08004416 public int getTrackballKeycodeState(int sw) {
4417 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4418 "getTrackballKeycodeState()")) {
4419 throw new SecurityException("Requires READ_INPUT_STATE permission");
4420 }
4421 return mQueue.getTrackballKeycodeState(sw);
4422 }
4423
4424 public int getDPadKeycodeState(int sw) {
4425 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4426 "getDPadKeycodeState()")) {
4427 throw new SecurityException("Requires READ_INPUT_STATE permission");
4428 }
4429 return mQueue.getDPadKeycodeState(sw);
4430 }
4431
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004432 public boolean hasKeys(int[] keycodes, boolean[] keyExists) {
4433 return KeyInputQueue.hasKeys(keycodes, keyExists);
4434 }
Romain Guy06882f82009-06-10 13:36:04 -07004435
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004436 public void enableScreenAfterBoot() {
4437 synchronized(mWindowMap) {
4438 if (mSystemBooted) {
4439 return;
4440 }
4441 mSystemBooted = true;
4442 }
Romain Guy06882f82009-06-10 13:36:04 -07004443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004444 performEnableScreen();
4445 }
Romain Guy06882f82009-06-10 13:36:04 -07004446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004447 public void enableScreenIfNeededLocked() {
4448 if (mDisplayEnabled) {
4449 return;
4450 }
4451 if (!mSystemBooted) {
4452 return;
4453 }
4454 mH.sendMessage(mH.obtainMessage(H.ENABLE_SCREEN));
4455 }
Romain Guy06882f82009-06-10 13:36:04 -07004456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004457 public void performEnableScreen() {
4458 synchronized(mWindowMap) {
4459 if (mDisplayEnabled) {
4460 return;
4461 }
4462 if (!mSystemBooted) {
4463 return;
4464 }
Romain Guy06882f82009-06-10 13:36:04 -07004465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004466 // Don't enable the screen until all existing windows
4467 // have been drawn.
4468 final int N = mWindows.size();
4469 for (int i=0; i<N; i++) {
4470 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn5943c202010-04-12 21:36:49 -07004471 if (w.isVisibleLw() && !w.mObscured
4472 && (w.mOrientationChanging || !w.isDrawnLw())) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004473 return;
4474 }
4475 }
Romain Guy06882f82009-06-10 13:36:04 -07004476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004477 mDisplayEnabled = true;
4478 if (false) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004479 Slog.i(TAG, "ENABLING SCREEN!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004480 StringWriter sw = new StringWriter();
4481 PrintWriter pw = new PrintWriter(sw);
4482 this.dump(null, pw, null);
Joe Onorato8a9b2202010-02-26 18:56:32 -08004483 Slog.i(TAG, sw.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004484 }
4485 try {
4486 IBinder surfaceFlinger = ServiceManager.getService("SurfaceFlinger");
4487 if (surfaceFlinger != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004488 //Slog.i(TAG, "******* TELLING SURFACE FLINGER WE ARE BOOTED!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004489 Parcel data = Parcel.obtain();
4490 data.writeInterfaceToken("android.ui.ISurfaceComposer");
4491 surfaceFlinger.transact(IBinder.FIRST_CALL_TRANSACTION,
4492 data, null, 0);
4493 data.recycle();
4494 }
4495 } catch (RemoteException ex) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004496 Slog.e(TAG, "Boot completed: SurfaceFlinger is dead!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004497 }
4498 }
Romain Guy06882f82009-06-10 13:36:04 -07004499
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004500 mPolicy.enableScreenAfterBoot();
Romain Guy06882f82009-06-10 13:36:04 -07004501
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004502 // Make sure the last requested orientation has been applied.
Dianne Hackborn321ae682009-03-27 16:16:03 -07004503 setRotationUnchecked(WindowManagerPolicy.USE_LAST_ROTATION, false,
4504 mLastRotationFlags | Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004505 }
Romain Guy06882f82009-06-10 13:36:04 -07004506
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004507 public void setInTouchMode(boolean mode) {
4508 synchronized(mWindowMap) {
4509 mInTouchMode = mode;
4510 }
4511 }
4512
Romain Guy06882f82009-06-10 13:36:04 -07004513 public void setRotation(int rotation,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004514 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004515 if (!checkCallingPermission(android.Manifest.permission.SET_ORIENTATION,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004516 "setRotation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004517 throw new SecurityException("Requires SET_ORIENTATION permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004518 }
4519
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004520 setRotationUnchecked(rotation, alwaysSendConfiguration, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004521 }
Romain Guy06882f82009-06-10 13:36:04 -07004522
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004523 public void setRotationUnchecked(int rotation,
4524 boolean alwaysSendConfiguration, int animFlags) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004525 if(DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004526 "alwaysSendConfiguration set to "+alwaysSendConfiguration);
Romain Guy06882f82009-06-10 13:36:04 -07004527
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004528 long origId = Binder.clearCallingIdentity();
4529 boolean changed;
4530 synchronized(mWindowMap) {
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004531 changed = setRotationUncheckedLocked(rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004532 }
Romain Guy06882f82009-06-10 13:36:04 -07004533
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004534 if (changed || alwaysSendConfiguration) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004535 sendNewConfiguration();
4536 }
Romain Guy06882f82009-06-10 13:36:04 -07004537
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004538 Binder.restoreCallingIdentity(origId);
4539 }
Romain Guy06882f82009-06-10 13:36:04 -07004540
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004541 /**
4542 * Apply a new rotation to the screen, respecting the requests of
4543 * applications. Use WindowManagerPolicy.USE_LAST_ROTATION to simply
4544 * re-evaluate the desired rotation.
4545 *
4546 * Returns null if the rotation has been changed. In this case YOU
4547 * MUST CALL setNewConfiguration() TO UNFREEZE THE SCREEN.
4548 */
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004549 public boolean setRotationUncheckedLocked(int rotation, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004550 boolean changed;
4551 if (rotation == WindowManagerPolicy.USE_LAST_ROTATION) {
4552 rotation = mRequestedRotation;
4553 } else {
4554 mRequestedRotation = rotation;
Dianne Hackborn321ae682009-03-27 16:16:03 -07004555 mLastRotationFlags = animFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004556 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08004557 if (DEBUG_ORIENTATION) Slog.v(TAG, "Overwriting rotation value from " + rotation);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004558 rotation = mPolicy.rotationForOrientationLw(mForcedAppOrientation,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004559 mRotation, mDisplayEnabled);
Joe Onorato8a9b2202010-02-26 18:56:32 -08004560 if (DEBUG_ORIENTATION) Slog.v(TAG, "new rotation is set to " + rotation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004561 changed = mDisplayEnabled && mRotation != rotation;
Romain Guy06882f82009-06-10 13:36:04 -07004562
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004563 if (changed) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004564 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004565 "Rotation changed to " + rotation
4566 + " from " + mRotation
4567 + " (forceApp=" + mForcedAppOrientation
4568 + ", req=" + mRequestedRotation + ")");
4569 mRotation = rotation;
4570 mWindowsFreezingScreen = true;
4571 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
4572 mH.sendMessageDelayed(mH.obtainMessage(H.WINDOW_FREEZE_TIMEOUT),
4573 2000);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004574 mWaitingForConfig = true;
4575 mLayoutNeeded = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004576 startFreezingDisplayLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08004577 Slog.i(TAG, "Setting rotation to " + rotation + ", animFlags=" + animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004578 mQueue.setOrientation(rotation);
4579 if (mDisplayEnabled) {
Dianne Hackborn321ae682009-03-27 16:16:03 -07004580 Surface.setOrientation(0, rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004581 }
4582 for (int i=mWindows.size()-1; i>=0; i--) {
4583 WindowState w = (WindowState)mWindows.get(i);
4584 if (w.mSurface != null) {
4585 w.mOrientationChanging = true;
4586 }
4587 }
4588 for (int i=mRotationWatchers.size()-1; i>=0; i--) {
4589 try {
4590 mRotationWatchers.get(i).onRotationChanged(rotation);
4591 } catch (RemoteException e) {
4592 }
4593 }
4594 } //end if changed
Romain Guy06882f82009-06-10 13:36:04 -07004595
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004596 return changed;
4597 }
Romain Guy06882f82009-06-10 13:36:04 -07004598
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004599 public int getRotation() {
4600 return mRotation;
4601 }
4602
4603 public int watchRotation(IRotationWatcher watcher) {
4604 final IBinder watcherBinder = watcher.asBinder();
4605 IBinder.DeathRecipient dr = new IBinder.DeathRecipient() {
4606 public void binderDied() {
4607 synchronized (mWindowMap) {
4608 for (int i=0; i<mRotationWatchers.size(); i++) {
4609 if (watcherBinder == mRotationWatchers.get(i).asBinder()) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07004610 IRotationWatcher removed = mRotationWatchers.remove(i);
4611 if (removed != null) {
4612 removed.asBinder().unlinkToDeath(this, 0);
4613 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004614 i--;
4615 }
4616 }
4617 }
4618 }
4619 };
Romain Guy06882f82009-06-10 13:36:04 -07004620
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004621 synchronized (mWindowMap) {
4622 try {
4623 watcher.asBinder().linkToDeath(dr, 0);
4624 mRotationWatchers.add(watcher);
4625 } catch (RemoteException e) {
4626 // Client died, no cleanup needed.
4627 }
Romain Guy06882f82009-06-10 13:36:04 -07004628
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004629 return mRotation;
4630 }
4631 }
4632
4633 /**
4634 * Starts the view server on the specified port.
4635 *
4636 * @param port The port to listener to.
4637 *
4638 * @return True if the server was successfully started, false otherwise.
4639 *
4640 * @see com.android.server.ViewServer
4641 * @see com.android.server.ViewServer#VIEW_SERVER_DEFAULT_PORT
4642 */
4643 public boolean startViewServer(int port) {
Romain Guy06882f82009-06-10 13:36:04 -07004644 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004645 return false;
4646 }
4647
4648 if (!checkCallingPermission(Manifest.permission.DUMP, "startViewServer")) {
4649 return false;
4650 }
4651
4652 if (port < 1024) {
4653 return false;
4654 }
4655
4656 if (mViewServer != null) {
4657 if (!mViewServer.isRunning()) {
4658 try {
4659 return mViewServer.start();
4660 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004661 Slog.w(TAG, "View server did not start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004662 }
4663 }
4664 return false;
4665 }
4666
4667 try {
4668 mViewServer = new ViewServer(this, port);
4669 return mViewServer.start();
4670 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004671 Slog.w(TAG, "View server did not start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004672 }
4673 return false;
4674 }
4675
Romain Guy06882f82009-06-10 13:36:04 -07004676 private boolean isSystemSecure() {
4677 return "1".equals(SystemProperties.get(SYSTEM_SECURE, "1")) &&
4678 "0".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
4679 }
4680
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004681 /**
4682 * Stops the view server if it exists.
4683 *
4684 * @return True if the server stopped, false if it wasn't started or
4685 * couldn't be stopped.
4686 *
4687 * @see com.android.server.ViewServer
4688 */
4689 public boolean stopViewServer() {
Romain Guy06882f82009-06-10 13:36:04 -07004690 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004691 return false;
4692 }
4693
4694 if (!checkCallingPermission(Manifest.permission.DUMP, "stopViewServer")) {
4695 return false;
4696 }
4697
4698 if (mViewServer != null) {
4699 return mViewServer.stop();
4700 }
4701 return false;
4702 }
4703
4704 /**
4705 * Indicates whether the view server is running.
4706 *
4707 * @return True if the server is running, false otherwise.
4708 *
4709 * @see com.android.server.ViewServer
4710 */
4711 public boolean isViewServerRunning() {
Romain Guy06882f82009-06-10 13:36:04 -07004712 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004713 return false;
4714 }
4715
4716 if (!checkCallingPermission(Manifest.permission.DUMP, "isViewServerRunning")) {
4717 return false;
4718 }
4719
4720 return mViewServer != null && mViewServer.isRunning();
4721 }
4722
4723 /**
4724 * Lists all availble windows in the system. The listing is written in the
4725 * specified Socket's output stream with the following syntax:
4726 * windowHashCodeInHexadecimal windowName
4727 * Each line of the ouput represents a different window.
4728 *
4729 * @param client The remote client to send the listing to.
4730 * @return False if an error occured, true otherwise.
4731 */
4732 boolean viewServerListWindows(Socket client) {
Romain Guy06882f82009-06-10 13:36:04 -07004733 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004734 return false;
4735 }
4736
4737 boolean result = true;
4738
4739 Object[] windows;
4740 synchronized (mWindowMap) {
4741 windows = new Object[mWindows.size()];
4742 //noinspection unchecked
4743 windows = mWindows.toArray(windows);
4744 }
4745
4746 BufferedWriter out = null;
4747
4748 // Any uncaught exception will crash the system process
4749 try {
4750 OutputStream clientStream = client.getOutputStream();
4751 out = new BufferedWriter(new OutputStreamWriter(clientStream), 8 * 1024);
4752
4753 final int count = windows.length;
4754 for (int i = 0; i < count; i++) {
4755 final WindowState w = (WindowState) windows[i];
4756 out.write(Integer.toHexString(System.identityHashCode(w)));
4757 out.write(' ');
4758 out.append(w.mAttrs.getTitle());
4759 out.write('\n');
4760 }
4761
4762 out.write("DONE.\n");
4763 out.flush();
4764 } catch (Exception e) {
4765 result = false;
4766 } finally {
4767 if (out != null) {
4768 try {
4769 out.close();
4770 } catch (IOException e) {
4771 result = false;
4772 }
4773 }
4774 }
4775
4776 return result;
4777 }
4778
4779 /**
4780 * Sends a command to a target window. The result of the command, if any, will be
4781 * written in the output stream of the specified socket.
4782 *
4783 * The parameters must follow this syntax:
4784 * windowHashcode extra
4785 *
4786 * Where XX is the length in characeters of the windowTitle.
4787 *
4788 * The first parameter is the target window. The window with the specified hashcode
4789 * will be the target. If no target can be found, nothing happens. The extra parameters
4790 * will be delivered to the target window and as parameters to the command itself.
4791 *
4792 * @param client The remote client to sent the result, if any, to.
4793 * @param command The command to execute.
4794 * @param parameters The command parameters.
4795 *
4796 * @return True if the command was successfully delivered, false otherwise. This does
4797 * not indicate whether the command itself was successful.
4798 */
4799 boolean viewServerWindowCommand(Socket client, String command, String parameters) {
Romain Guy06882f82009-06-10 13:36:04 -07004800 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004801 return false;
4802 }
4803
4804 boolean success = true;
4805 Parcel data = null;
4806 Parcel reply = null;
4807
4808 // Any uncaught exception will crash the system process
4809 try {
4810 // Find the hashcode of the window
4811 int index = parameters.indexOf(' ');
4812 if (index == -1) {
4813 index = parameters.length();
4814 }
4815 final String code = parameters.substring(0, index);
Romain Guy236092a2009-12-14 15:31:48 -08004816 int hashCode = (int) Long.parseLong(code, 16);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004817
4818 // Extract the command's parameter after the window description
4819 if (index < parameters.length()) {
4820 parameters = parameters.substring(index + 1);
4821 } else {
4822 parameters = "";
4823 }
4824
4825 final WindowManagerService.WindowState window = findWindow(hashCode);
4826 if (window == null) {
4827 return false;
4828 }
4829
4830 data = Parcel.obtain();
4831 data.writeInterfaceToken("android.view.IWindow");
4832 data.writeString(command);
4833 data.writeString(parameters);
4834 data.writeInt(1);
4835 ParcelFileDescriptor.fromSocket(client).writeToParcel(data, 0);
4836
4837 reply = Parcel.obtain();
4838
4839 final IBinder binder = window.mClient.asBinder();
4840 // TODO: GET THE TRANSACTION CODE IN A SAFER MANNER
4841 binder.transact(IBinder.FIRST_CALL_TRANSACTION, data, reply, 0);
4842
4843 reply.readException();
4844
4845 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004846 Slog.w(TAG, "Could not send command " + command + " with parameters " + parameters, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004847 success = false;
4848 } finally {
4849 if (data != null) {
4850 data.recycle();
4851 }
4852 if (reply != null) {
4853 reply.recycle();
4854 }
4855 }
4856
4857 return success;
4858 }
4859
4860 private WindowState findWindow(int hashCode) {
4861 if (hashCode == -1) {
4862 return getFocusedWindow();
4863 }
4864
4865 synchronized (mWindowMap) {
4866 final ArrayList windows = mWindows;
4867 final int count = windows.size();
4868
4869 for (int i = 0; i < count; i++) {
4870 WindowState w = (WindowState) windows.get(i);
4871 if (System.identityHashCode(w) == hashCode) {
4872 return w;
4873 }
4874 }
4875 }
4876
4877 return null;
4878 }
4879
4880 /*
4881 * Instruct the Activity Manager to fetch the current configuration and broadcast
4882 * that to config-changed listeners if appropriate.
4883 */
4884 void sendNewConfiguration() {
4885 try {
4886 mActivityManager.updateConfiguration(null);
4887 } catch (RemoteException e) {
4888 }
4889 }
Romain Guy06882f82009-06-10 13:36:04 -07004890
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004891 public Configuration computeNewConfiguration() {
4892 synchronized (mWindowMap) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07004893 return computeNewConfigurationLocked();
4894 }
4895 }
Romain Guy06882f82009-06-10 13:36:04 -07004896
Dianne Hackbornc485a602009-03-24 22:39:49 -07004897 Configuration computeNewConfigurationLocked() {
4898 Configuration config = new Configuration();
4899 if (!computeNewConfigurationLocked(config)) {
4900 return null;
4901 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07004902 return config;
4903 }
Romain Guy06882f82009-06-10 13:36:04 -07004904
Dianne Hackbornc485a602009-03-24 22:39:49 -07004905 boolean computeNewConfigurationLocked(Configuration config) {
4906 if (mDisplay == null) {
4907 return false;
4908 }
4909 mQueue.getInputConfiguration(config);
Christopher Tateb696aee2010-04-02 19:08:30 -07004910
4911 // Use the effective "visual" dimensions based on current rotation
4912 final boolean rotated = (mRotation == Surface.ROTATION_90
4913 || mRotation == Surface.ROTATION_270);
4914 final int dw = rotated ? mInitialDisplayHeight : mInitialDisplayWidth;
4915 final int dh = rotated ? mInitialDisplayWidth : mInitialDisplayHeight;
4916
Dianne Hackbornc485a602009-03-24 22:39:49 -07004917 int orientation = Configuration.ORIENTATION_SQUARE;
4918 if (dw < dh) {
4919 orientation = Configuration.ORIENTATION_PORTRAIT;
4920 } else if (dw > dh) {
4921 orientation = Configuration.ORIENTATION_LANDSCAPE;
4922 }
4923 config.orientation = orientation;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004924
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004925 DisplayMetrics dm = new DisplayMetrics();
4926 mDisplay.getMetrics(dm);
4927 CompatibilityInfo.updateCompatibleScreenFrame(dm, orientation, mCompatibleScreenFrame);
4928
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004929 if (mScreenLayout == Configuration.SCREENLAYOUT_SIZE_UNDEFINED) {
Dianne Hackborn723738c2009-06-25 19:48:04 -07004930 // Note we only do this once because at this point we don't
4931 // expect the screen to change in this way at runtime, and want
4932 // to avoid all of this computation for every config change.
Dianne Hackborn723738c2009-06-25 19:48:04 -07004933 int longSize = dw;
4934 int shortSize = dh;
4935 if (longSize < shortSize) {
4936 int tmp = longSize;
4937 longSize = shortSize;
4938 shortSize = tmp;
4939 }
4940 longSize = (int)(longSize/dm.density);
4941 shortSize = (int)(shortSize/dm.density);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004942
Dianne Hackborn723738c2009-06-25 19:48:04 -07004943 // These semi-magic numbers define our compatibility modes for
4944 // applications with different screens. Don't change unless you
4945 // make sure to test lots and lots of apps!
4946 if (longSize < 470) {
4947 // This is shorter than an HVGA normal density screen (which
4948 // is 480 pixels on its long side).
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004949 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_SMALL
4950 | Configuration.SCREENLAYOUT_LONG_NO;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004951 } else {
Dianne Hackborn14cee9f2010-04-23 17:51:26 -07004952 // What size is this screen screen?
4953 if (longSize >= 800 && shortSize >= 600) {
4954 // SVGA or larger screens at medium density are the point
4955 // at which we consider it to be an extra large screen.
4956 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_XLARGE;
4957 } else if (longSize >= 640 && shortSize >= 480) {
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004958 // VGA or larger screens at medium density are the point
4959 // at which we consider it to be a large screen.
4960 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_LARGE;
4961 } else {
4962 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_NORMAL;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004963
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004964 // If this screen is wider than normal HVGA, or taller
4965 // than FWVGA, then for old apps we want to run in size
4966 // compatibility mode.
4967 if (shortSize > 321 || longSize > 570) {
4968 mScreenLayout |= Configuration.SCREENLAYOUT_COMPAT_NEEDED;
4969 }
4970 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004971
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004972 // Is this a long screen?
4973 if (((longSize*3)/5) >= (shortSize-1)) {
4974 // Anything wider than WVGA (5:3) is considering to be long.
4975 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_YES;
4976 } else {
4977 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_NO;
4978 }
Dianne Hackborn723738c2009-06-25 19:48:04 -07004979 }
4980 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004981 config.screenLayout = mScreenLayout;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004982
Dianne Hackbornc485a602009-03-24 22:39:49 -07004983 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
4984 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
4985 mPolicy.adjustConfigurationLw(config);
4986 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004987 }
Romain Guy06882f82009-06-10 13:36:04 -07004988
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004989 // -------------------------------------------------------------
4990 // Input Events and Focus Management
4991 // -------------------------------------------------------------
4992
4993 private final void wakeupIfNeeded(WindowState targetWin, int eventType) {
Michael Chane96440f2009-05-06 10:27:36 -07004994 long curTime = SystemClock.uptimeMillis();
4995
Michael Chane10de972009-05-18 11:24:50 -07004996 if (eventType == TOUCH_EVENT || eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT) {
Michael Chane96440f2009-05-06 10:27:36 -07004997 if (mLastTouchEventType == eventType &&
4998 (curTime - mLastUserActivityCallTime) < MIN_TIME_BETWEEN_USERACTIVITIES) {
4999 return;
5000 }
5001 mLastUserActivityCallTime = curTime;
5002 mLastTouchEventType = eventType;
5003 }
5004
5005 if (targetWin == null
5006 || targetWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
5007 mPowerManager.userActivity(curTime, false, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005008 }
5009 }
5010
5011 // tells if it's a cheek event or not -- this function is stateful
5012 private static final int EVENT_NONE = 0;
5013 private static final int EVENT_UNKNOWN = 0;
5014 private static final int EVENT_CHEEK = 0;
5015 private static final int EVENT_IGNORE_DURATION = 300; // ms
5016 private static final float CHEEK_THRESHOLD = 0.6f;
5017 private int mEventState = EVENT_NONE;
5018 private float mEventSize;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07005019
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005020 private int eventType(MotionEvent ev) {
5021 float size = ev.getSize();
5022 switch (ev.getAction()) {
5023 case MotionEvent.ACTION_DOWN:
5024 mEventSize = size;
5025 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_EVENT;
5026 case MotionEvent.ACTION_UP:
5027 if (size > mEventSize) mEventSize = size;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07005028 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005029 case MotionEvent.ACTION_MOVE:
5030 final int N = ev.getHistorySize();
5031 if (size > mEventSize) mEventSize = size;
5032 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
5033 for (int i=0; i<N; i++) {
5034 size = ev.getHistoricalSize(i);
5035 if (size > mEventSize) mEventSize = size;
5036 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
5037 }
5038 if (ev.getEventTime() < ev.getDownTime() + EVENT_IGNORE_DURATION) {
5039 return TOUCH_EVENT;
5040 } else {
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07005041 return LONG_TOUCH_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005042 }
5043 default:
5044 // not good
5045 return OTHER_EVENT;
5046 }
5047 }
5048
5049 /**
5050 * @return Returns true if event was dispatched, false if it was dropped for any reason
5051 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07005052 private int dispatchPointer(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005053 if (DEBUG_INPUT || WindowManagerPolicy.WATCH_POINTER) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005054 "dispatchPointer " + ev);
5055
Michael Chan53071d62009-05-13 17:29:48 -07005056 if (MEASURE_LATENCY) {
5057 lt.sample("3 Wait for last dispatch ", System.nanoTime() - qev.whenNano);
5058 }
5059
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005060 Object targetObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005061 ev, true, false, pid, uid);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005062
Michael Chan53071d62009-05-13 17:29:48 -07005063 if (MEASURE_LATENCY) {
5064 lt.sample("3 Last dispatch finished ", System.nanoTime() - qev.whenNano);
5065 }
5066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005067 int action = ev.getAction();
Romain Guy06882f82009-06-10 13:36:04 -07005068
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005069 if (action == MotionEvent.ACTION_UP) {
5070 // let go of our target
5071 mKeyWaiter.mMotionTarget = null;
5072 mPowerManager.logPointerUpEvent();
5073 } else if (action == MotionEvent.ACTION_DOWN) {
5074 mPowerManager.logPointerDownEvent();
5075 }
5076
5077 if (targetObj == null) {
5078 // In this case we are either dropping the event, or have received
5079 // a move or up without a down. It is common to receive move
5080 // events in such a way, since this means the user is moving the
5081 // pointer without actually pressing down. All other cases should
5082 // be atypical, so let's log them.
Michael Chane96440f2009-05-06 10:27:36 -07005083 if (action != MotionEvent.ACTION_MOVE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005084 Slog.w(TAG, "No window to dispatch pointer action " + ev.getAction());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005085 }
Dianne Hackborn6adba242009-11-10 11:10:09 -08005086 synchronized (mWindowMap) {
Dianne Hackborn90d2db32010-02-11 22:19:06 -08005087 dispatchPointerElsewhereLocked(null, null, ev, ev.getEventTime(), true);
Dianne Hackborn6adba242009-11-10 11:10:09 -08005088 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005089 if (qev != null) {
5090 mQueue.recycleEvent(qev);
5091 }
5092 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005093 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005094 }
5095 if (targetObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborn6adba242009-11-10 11:10:09 -08005096 synchronized (mWindowMap) {
Dianne Hackborn90d2db32010-02-11 22:19:06 -08005097 dispatchPointerElsewhereLocked(null, null, ev, ev.getEventTime(), true);
Dianne Hackborn6adba242009-11-10 11:10:09 -08005098 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005099 if (qev != null) {
5100 mQueue.recycleEvent(qev);
5101 }
5102 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005103 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005104 }
Romain Guy06882f82009-06-10 13:36:04 -07005105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005106 WindowState target = (WindowState)targetObj;
Romain Guy06882f82009-06-10 13:36:04 -07005107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005108 final long eventTime = ev.getEventTime();
Michael Chan53071d62009-05-13 17:29:48 -07005109 final long eventTimeNano = ev.getEventTimeNano();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005110
Joe Onorato8a9b2202010-02-26 18:56:32 -08005111 //Slog.i(TAG, "Sending " + ev + " to " + target);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005112
5113 if (uid != 0 && uid != target.mSession.mUid) {
5114 if (mContext.checkPermission(
5115 android.Manifest.permission.INJECT_EVENTS, pid, uid)
5116 != PackageManager.PERMISSION_GRANTED) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005117 Slog.w(TAG, "Permission denied: injecting pointer event from pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005118 + pid + " uid " + uid + " to window " + target
5119 + " owned by uid " + target.mSession.mUid);
5120 if (qev != null) {
5121 mQueue.recycleEvent(qev);
5122 }
5123 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005124 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005125 }
5126 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005127
Michael Chan53071d62009-05-13 17:29:48 -07005128 if (MEASURE_LATENCY) {
5129 lt.sample("4 in dispatchPointer ", System.nanoTime() - eventTimeNano);
5130 }
5131
Romain Guy06882f82009-06-10 13:36:04 -07005132 if ((target.mAttrs.flags &
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005133 WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES) != 0) {
5134 //target wants to ignore fat touch events
5135 boolean cheekPress = mPolicy.isCheekPressedAgainstScreen(ev);
5136 //explicit flag to return without processing event further
5137 boolean returnFlag = false;
5138 if((action == MotionEvent.ACTION_DOWN)) {
5139 mFatTouch = false;
5140 if(cheekPress) {
5141 mFatTouch = true;
5142 returnFlag = true;
5143 }
5144 } else {
5145 if(action == MotionEvent.ACTION_UP) {
5146 if(mFatTouch) {
5147 //earlier even was invalid doesnt matter if current up is cheekpress or not
5148 mFatTouch = false;
5149 returnFlag = true;
5150 } else if(cheekPress) {
5151 //cancel the earlier event
5152 ev.setAction(MotionEvent.ACTION_CANCEL);
5153 action = MotionEvent.ACTION_CANCEL;
5154 }
5155 } else if(action == MotionEvent.ACTION_MOVE) {
5156 if(mFatTouch) {
5157 //two cases here
5158 //an invalid down followed by 0 or moves(valid or invalid)
Romain Guy06882f82009-06-10 13:36:04 -07005159 //a valid down, invalid move, more moves. want to ignore till up
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005160 returnFlag = true;
5161 } else if(cheekPress) {
5162 //valid down followed by invalid moves
5163 //an invalid move have to cancel earlier action
5164 ev.setAction(MotionEvent.ACTION_CANCEL);
5165 action = MotionEvent.ACTION_CANCEL;
Joe Onorato8a9b2202010-02-26 18:56:32 -08005166 if (DEBUG_INPUT) Slog.v(TAG, "Sending cancel for invalid ACTION_MOVE");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005167 //note that the subsequent invalid moves will not get here
5168 mFatTouch = true;
5169 }
5170 }
5171 } //else if action
5172 if(returnFlag) {
5173 //recycle que, ev
5174 if (qev != null) {
5175 mQueue.recycleEvent(qev);
5176 }
5177 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005178 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005179 }
5180 } //end if target
Michael Chane96440f2009-05-06 10:27:36 -07005181
Michael Chan9f028e62009-08-04 17:37:46 -07005182 // Enable this for testing the "right" value
5183 if (false && action == MotionEvent.ACTION_DOWN) {
Michael Chane96440f2009-05-06 10:27:36 -07005184 int max_events_per_sec = 35;
5185 try {
5186 max_events_per_sec = Integer.parseInt(SystemProperties
5187 .get("windowsmgr.max_events_per_sec"));
5188 if (max_events_per_sec < 1) {
5189 max_events_per_sec = 35;
5190 }
5191 } catch (NumberFormatException e) {
5192 }
5193 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
5194 }
5195
5196 /*
5197 * Throttle events to minimize CPU usage when there's a flood of events
5198 * e.g. constant contact with the screen
5199 */
5200 if (action == MotionEvent.ACTION_MOVE) {
5201 long nextEventTime = mLastTouchEventTime + mMinWaitTimeBetweenTouchEvents;
5202 long now = SystemClock.uptimeMillis();
5203 if (now < nextEventTime) {
5204 try {
5205 Thread.sleep(nextEventTime - now);
5206 } catch (InterruptedException e) {
5207 }
5208 mLastTouchEventTime = nextEventTime;
5209 } else {
5210 mLastTouchEventTime = now;
5211 }
5212 }
5213
Michael Chan53071d62009-05-13 17:29:48 -07005214 if (MEASURE_LATENCY) {
5215 lt.sample("5 in dispatchPointer ", System.nanoTime() - eventTimeNano);
5216 }
5217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005218 synchronized(mWindowMap) {
Dianne Hackborn6adba242009-11-10 11:10:09 -08005219 if (!target.isVisibleLw()) {
5220 // During this motion dispatch, the target window has become
5221 // invisible.
Dianne Hackborn90d2db32010-02-11 22:19:06 -08005222 dispatchPointerElsewhereLocked(null, null, ev, ev.getEventTime(), false);
Dianne Hackborn6adba242009-11-10 11:10:09 -08005223 if (qev != null) {
5224 mQueue.recycleEvent(qev);
5225 }
5226 ev.recycle();
5227 return INJECT_SUCCEEDED;
5228 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005230 if (qev != null && action == MotionEvent.ACTION_MOVE) {
5231 mKeyWaiter.bindTargetWindowLocked(target,
5232 KeyWaiter.RETURN_PENDING_POINTER, qev);
5233 ev = null;
5234 } else {
5235 if (action == MotionEvent.ACTION_DOWN) {
5236 WindowState out = mKeyWaiter.mOutsideTouchTargets;
5237 if (out != null) {
5238 MotionEvent oev = MotionEvent.obtain(ev);
5239 oev.setAction(MotionEvent.ACTION_OUTSIDE);
5240 do {
5241 final Rect frame = out.mFrame;
5242 oev.offsetLocation(-(float)frame.left, -(float)frame.top);
5243 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005244 out.mClient.dispatchPointer(oev, eventTime, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005245 } catch (android.os.RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005246 Slog.i(TAG, "WINDOW DIED during outside motion dispatch: " + out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005247 }
5248 oev.offsetLocation((float)frame.left, (float)frame.top);
5249 out = out.mNextOutsideTouch;
5250 } while (out != null);
5251 mKeyWaiter.mOutsideTouchTargets = null;
5252 }
5253 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005254
Dianne Hackborn90d2db32010-02-11 22:19:06 -08005255 dispatchPointerElsewhereLocked(target, null, ev, ev.getEventTime(), false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005256
Dianne Hackborn6adba242009-11-10 11:10:09 -08005257 final Rect frame = target.mFrame;
5258 ev.offsetLocation(-(float)frame.left, -(float)frame.top);
5259 mKeyWaiter.bindTargetWindowLocked(target);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005260 }
5261 }
Romain Guy06882f82009-06-10 13:36:04 -07005262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005263 // finally offset the event to the target's coordinate system and
5264 // dispatch the event.
5265 try {
5266 if (DEBUG_INPUT || DEBUG_FOCUS || WindowManagerPolicy.WATCH_POINTER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005267 Slog.v(TAG, "Delivering pointer " + qev + " to " + target);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005268 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005269
Michael Chan53071d62009-05-13 17:29:48 -07005270 if (MEASURE_LATENCY) {
5271 lt.sample("6 before svr->client ipc ", System.nanoTime() - eventTimeNano);
5272 }
5273
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005274 target.mClient.dispatchPointer(ev, eventTime, true);
Michael Chan53071d62009-05-13 17:29:48 -07005275
5276 if (MEASURE_LATENCY) {
5277 lt.sample("7 after svr->client ipc ", System.nanoTime() - eventTimeNano);
5278 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07005279 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005280 } catch (android.os.RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005281 Slog.i(TAG, "WINDOW DIED during motion dispatch: " + target);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005282 mKeyWaiter.mMotionTarget = null;
5283 try {
5284 removeWindow(target.mSession, target.mClient);
5285 } catch (java.util.NoSuchElementException ex) {
5286 // This will happen if the window has already been
5287 // removed.
5288 }
5289 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07005290 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005291 }
Romain Guy06882f82009-06-10 13:36:04 -07005292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005293 /**
5294 * @return Returns true if event was dispatched, false if it was dropped for any reason
5295 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07005296 private int dispatchTrackball(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005297 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005298 TAG, "dispatchTrackball [" + ev.getAction() +"] <" + ev.getX() + ", " + ev.getY() + ">");
Romain Guy06882f82009-06-10 13:36:04 -07005299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005300 Object focusObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005301 ev, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005302 if (focusObj == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005303 Slog.w(TAG, "No focus window, dropping trackball: " + ev);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005304 if (qev != null) {
5305 mQueue.recycleEvent(qev);
5306 }
5307 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005308 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005309 }
5310 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
5311 if (qev != null) {
5312 mQueue.recycleEvent(qev);
5313 }
5314 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005315 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005316 }
Romain Guy06882f82009-06-10 13:36:04 -07005317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005318 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07005319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005320 if (uid != 0 && uid != focus.mSession.mUid) {
5321 if (mContext.checkPermission(
5322 android.Manifest.permission.INJECT_EVENTS, pid, uid)
5323 != PackageManager.PERMISSION_GRANTED) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005324 Slog.w(TAG, "Permission denied: injecting key event from pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005325 + pid + " uid " + uid + " to window " + focus
5326 + " owned by uid " + focus.mSession.mUid);
5327 if (qev != null) {
5328 mQueue.recycleEvent(qev);
5329 }
5330 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005331 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005332 }
5333 }
Romain Guy06882f82009-06-10 13:36:04 -07005334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005335 final long eventTime = ev.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07005336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005337 synchronized(mWindowMap) {
5338 if (qev != null && ev.getAction() == MotionEvent.ACTION_MOVE) {
5339 mKeyWaiter.bindTargetWindowLocked(focus,
5340 KeyWaiter.RETURN_PENDING_TRACKBALL, qev);
5341 // We don't deliver movement events to the client, we hold
5342 // them and wait for them to call back.
5343 ev = null;
5344 } else {
5345 mKeyWaiter.bindTargetWindowLocked(focus);
5346 }
5347 }
Romain Guy06882f82009-06-10 13:36:04 -07005348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005349 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005350 focus.mClient.dispatchTrackball(ev, eventTime, true);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005351 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005352 } catch (android.os.RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005353 Slog.i(TAG, "WINDOW DIED during key dispatch: " + focus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005354 try {
5355 removeWindow(focus.mSession, focus.mClient);
5356 } catch (java.util.NoSuchElementException ex) {
5357 // This will happen if the window has already been
5358 // removed.
5359 }
5360 }
Romain Guy06882f82009-06-10 13:36:04 -07005361
Dianne Hackborncfaef692009-06-15 14:24:44 -07005362 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005363 }
Romain Guy06882f82009-06-10 13:36:04 -07005364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005365 /**
5366 * @return Returns true if event was dispatched, false if it was dropped for any reason
5367 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07005368 private int dispatchKey(KeyEvent event, int pid, int uid) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005369 if (DEBUG_INPUT) Slog.v(TAG, "Dispatch key: " + event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005370
5371 Object focusObj = mKeyWaiter.waitForNextEventTarget(event, null,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005372 null, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005373 if (focusObj == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005374 Slog.w(TAG, "No focus window, dropping: " + event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005375 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005376 }
5377 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005378 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005379 }
Romain Guy06882f82009-06-10 13:36:04 -07005380
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005381 // Okay we have finished waiting for the last event to be processed.
5382 // First off, if this is a repeat event, check to see if there is
5383 // a corresponding up event in the queue. If there is, we will
5384 // just drop the repeat, because it makes no sense to repeat after
5385 // the user has released a key. (This is especially important for
5386 // long presses.)
5387 if (event.getRepeatCount() > 0 && mQueue.hasKeyUpEvent(event)) {
5388 return INJECT_SUCCEEDED;
5389 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005391 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07005392
Joe Onorato8a9b2202010-02-26 18:56:32 -08005393 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005394 TAG, "Dispatching to " + focus + ": " + event);
5395
5396 if (uid != 0 && uid != focus.mSession.mUid) {
5397 if (mContext.checkPermission(
5398 android.Manifest.permission.INJECT_EVENTS, pid, uid)
5399 != PackageManager.PERMISSION_GRANTED) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005400 Slog.w(TAG, "Permission denied: injecting key event from pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005401 + pid + " uid " + uid + " to window " + focus
5402 + " owned by uid " + focus.mSession.mUid);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005403 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005404 }
5405 }
Romain Guy06882f82009-06-10 13:36:04 -07005406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005407 synchronized(mWindowMap) {
5408 mKeyWaiter.bindTargetWindowLocked(focus);
5409 }
5410
5411 // NOSHIP extra state logging
5412 mKeyWaiter.recordDispatchState(event, focus);
5413 // END NOSHIP
Romain Guy06882f82009-06-10 13:36:04 -07005414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005415 try {
5416 if (DEBUG_INPUT || DEBUG_FOCUS) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005417 Slog.v(TAG, "Delivering key " + event.getKeyCode()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005418 + " to " + focus);
5419 }
5420 focus.mClient.dispatchKey(event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005421 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005422 } catch (android.os.RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005423 Slog.i(TAG, "WINDOW DIED during key dispatch: " + focus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005424 try {
5425 removeWindow(focus.mSession, focus.mClient);
5426 } catch (java.util.NoSuchElementException ex) {
5427 // This will happen if the window has already been
5428 // removed.
5429 }
5430 }
Romain Guy06882f82009-06-10 13:36:04 -07005431
Dianne Hackborncfaef692009-06-15 14:24:44 -07005432 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005433 }
Romain Guy06882f82009-06-10 13:36:04 -07005434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005435 public void pauseKeyDispatching(IBinder _token) {
5436 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5437 "pauseKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005438 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005439 }
5440
5441 synchronized (mWindowMap) {
5442 WindowToken token = mTokenMap.get(_token);
5443 if (token != null) {
5444 mKeyWaiter.pauseDispatchingLocked(token);
5445 }
5446 }
5447 }
5448
5449 public void resumeKeyDispatching(IBinder _token) {
5450 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5451 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005452 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005453 }
5454
5455 synchronized (mWindowMap) {
5456 WindowToken token = mTokenMap.get(_token);
5457 if (token != null) {
5458 mKeyWaiter.resumeDispatchingLocked(token);
5459 }
5460 }
5461 }
5462
5463 public void setEventDispatching(boolean enabled) {
5464 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5465 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005466 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005467 }
5468
5469 synchronized (mWindowMap) {
5470 mKeyWaiter.setEventDispatchingLocked(enabled);
5471 }
5472 }
Romain Guy06882f82009-06-10 13:36:04 -07005473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005474 /**
5475 * Injects a keystroke event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005476 *
5477 * @param ev A motion event describing the keystroke action. (Be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005478 * {@link SystemClock#uptimeMillis()} as the timebase.)
5479 * @param sync If true, wait for the event to be completed before returning to the caller.
5480 * @return Returns true if event was dispatched, false if it was dropped for any reason
5481 */
5482 public boolean injectKeyEvent(KeyEvent ev, boolean sync) {
5483 long downTime = ev.getDownTime();
5484 long eventTime = ev.getEventTime();
5485
5486 int action = ev.getAction();
5487 int code = ev.getKeyCode();
5488 int repeatCount = ev.getRepeatCount();
5489 int metaState = ev.getMetaState();
5490 int deviceId = ev.getDeviceId();
5491 int scancode = ev.getScanCode();
5492
5493 if (eventTime == 0) eventTime = SystemClock.uptimeMillis();
5494 if (downTime == 0) downTime = eventTime;
5495
5496 KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
The Android Open Source Project10592532009-03-18 17:39:46 -07005497 deviceId, scancode, KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005498
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005499 final int pid = Binder.getCallingPid();
5500 final int uid = Binder.getCallingUid();
5501 final long ident = Binder.clearCallingIdentity();
5502 final int result = dispatchKey(newEvent, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005503 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005504 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005505 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005506 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005507 switch (result) {
5508 case INJECT_NO_PERMISSION:
5509 throw new SecurityException(
Chander S Pechetty27f3de62010-02-10 22:14:00 +05305510 "Injecting to another application requires INJECT_EVENTS permission");
Dianne Hackborncfaef692009-06-15 14:24:44 -07005511 case INJECT_SUCCEEDED:
5512 return true;
5513 }
5514 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005515 }
5516
5517 /**
5518 * Inject a pointer (touch) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005519 *
5520 * @param ev A motion event describing the pointer (touch) action. (As noted in
5521 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005522 * {@link SystemClock#uptimeMillis()} as the timebase.)
5523 * @param sync If true, wait for the event to be completed before returning to the caller.
5524 * @return Returns true if event was dispatched, false if it was dropped for any reason
5525 */
5526 public boolean injectPointerEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005527 final int pid = Binder.getCallingPid();
5528 final int uid = Binder.getCallingUid();
5529 final long ident = Binder.clearCallingIdentity();
5530 final int result = dispatchPointer(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005531 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005532 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005533 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005534 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005535 switch (result) {
5536 case INJECT_NO_PERMISSION:
5537 throw new SecurityException(
Chander S Pechetty27f3de62010-02-10 22:14:00 +05305538 "Injecting to another application requires INJECT_EVENTS permission");
Dianne Hackborncfaef692009-06-15 14:24:44 -07005539 case INJECT_SUCCEEDED:
5540 return true;
5541 }
5542 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005543 }
Romain Guy06882f82009-06-10 13:36:04 -07005544
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005545 /**
5546 * Inject a trackball (navigation device) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005547 *
5548 * @param ev A motion event describing the trackball action. (As noted in
5549 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005550 * {@link SystemClock#uptimeMillis()} as the timebase.)
5551 * @param sync If true, wait for the event to be completed before returning to the caller.
5552 * @return Returns true if event was dispatched, false if it was dropped for any reason
5553 */
5554 public boolean injectTrackballEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005555 final int pid = Binder.getCallingPid();
5556 final int uid = Binder.getCallingUid();
5557 final long ident = Binder.clearCallingIdentity();
5558 final int result = dispatchTrackball(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005559 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005560 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005561 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005562 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005563 switch (result) {
5564 case INJECT_NO_PERMISSION:
5565 throw new SecurityException(
Chander S Pechetty27f3de62010-02-10 22:14:00 +05305566 "Injecting to another application requires INJECT_EVENTS permission");
Dianne Hackborncfaef692009-06-15 14:24:44 -07005567 case INJECT_SUCCEEDED:
5568 return true;
5569 }
5570 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005571 }
Romain Guy06882f82009-06-10 13:36:04 -07005572
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005573 private WindowState getFocusedWindow() {
5574 synchronized (mWindowMap) {
5575 return getFocusedWindowLocked();
5576 }
5577 }
5578
5579 private WindowState getFocusedWindowLocked() {
5580 return mCurrentFocus;
5581 }
Romain Guy06882f82009-06-10 13:36:04 -07005582
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005583 /**
5584 * This class holds the state for dispatching key events. This state
5585 * is protected by the KeyWaiter instance, NOT by the window lock. You
5586 * can be holding the main window lock while acquire the KeyWaiter lock,
5587 * but not the other way around.
5588 */
5589 final class KeyWaiter {
5590 // NOSHIP debugging
5591 public class DispatchState {
5592 private KeyEvent event;
5593 private WindowState focus;
5594 private long time;
5595 private WindowState lastWin;
5596 private IBinder lastBinder;
5597 private boolean finished;
5598 private boolean gotFirstWindow;
5599 private boolean eventDispatching;
5600 private long timeToSwitch;
5601 private boolean wasFrozen;
5602 private boolean focusPaused;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005603 private WindowState curFocus;
Romain Guy06882f82009-06-10 13:36:04 -07005604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005605 DispatchState(KeyEvent theEvent, WindowState theFocus) {
5606 focus = theFocus;
5607 event = theEvent;
5608 time = System.currentTimeMillis();
5609 // snapshot KeyWaiter state
5610 lastWin = mLastWin;
5611 lastBinder = mLastBinder;
5612 finished = mFinished;
5613 gotFirstWindow = mGotFirstWindow;
5614 eventDispatching = mEventDispatching;
5615 timeToSwitch = mTimeToSwitch;
5616 wasFrozen = mWasFrozen;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005617 curFocus = mCurrentFocus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005618 // cache the paused state at ctor time as well
5619 if (theFocus == null || theFocus.mToken == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005620 focusPaused = false;
5621 } else {
5622 focusPaused = theFocus.mToken.paused;
5623 }
5624 }
Romain Guy06882f82009-06-10 13:36:04 -07005625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005626 public String toString() {
5627 return "{{" + event + " to " + focus + " @ " + time
5628 + " lw=" + lastWin + " lb=" + lastBinder
5629 + " fin=" + finished + " gfw=" + gotFirstWindow
5630 + " ed=" + eventDispatching + " tts=" + timeToSwitch
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005631 + " wf=" + wasFrozen + " fp=" + focusPaused
Christopher Tate46d45252010-02-09 15:48:57 -08005632 + " mcf=" + curFocus + "}}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005633 }
5634 };
5635 private DispatchState mDispatchState = null;
5636 public void recordDispatchState(KeyEvent theEvent, WindowState theFocus) {
5637 mDispatchState = new DispatchState(theEvent, theFocus);
5638 }
5639 // END NOSHIP
5640
5641 public static final int RETURN_NOTHING = 0;
5642 public static final int RETURN_PENDING_POINTER = 1;
5643 public static final int RETURN_PENDING_TRACKBALL = 2;
Romain Guy06882f82009-06-10 13:36:04 -07005644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005645 final Object SKIP_TARGET_TOKEN = new Object();
5646 final Object CONSUMED_EVENT_TOKEN = new Object();
Romain Guy06882f82009-06-10 13:36:04 -07005647
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005648 private WindowState mLastWin = null;
5649 private IBinder mLastBinder = null;
5650 private boolean mFinished = true;
5651 private boolean mGotFirstWindow = false;
5652 private boolean mEventDispatching = true;
5653 private long mTimeToSwitch = 0;
5654 /* package */ boolean mWasFrozen = false;
Romain Guy06882f82009-06-10 13:36:04 -07005655
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005656 // Target of Motion events
5657 WindowState mMotionTarget;
Romain Guy06882f82009-06-10 13:36:04 -07005658
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005659 // Windows above the target who would like to receive an "outside"
5660 // touch event for any down events outside of them.
5661 WindowState mOutsideTouchTargets;
5662
5663 /**
5664 * Wait for the last event dispatch to complete, then find the next
5665 * target that should receive the given event and wait for that one
5666 * to be ready to receive it.
5667 */
5668 Object waitForNextEventTarget(KeyEvent nextKey, QueuedEvent qev,
5669 MotionEvent nextMotion, boolean isPointerEvent,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005670 boolean failIfTimeout, int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005671 long startTime = SystemClock.uptimeMillis();
5672 long keyDispatchingTimeout = 5 * 1000;
5673 long waitedFor = 0;
5674
5675 while (true) {
5676 // Figure out which window we care about. It is either the
5677 // last window we are waiting to have process the event or,
5678 // if none, then the next window we think the event should go
5679 // to. Note: we retrieve mLastWin outside of the lock, so
5680 // it may change before we lock. Thus we must check it again.
5681 WindowState targetWin = mLastWin;
5682 boolean targetIsNew = targetWin == null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08005683 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005684 TAG, "waitForLastKey: mFinished=" + mFinished +
5685 ", mLastWin=" + mLastWin);
5686 if (targetIsNew) {
5687 Object target = findTargetWindow(nextKey, qev, nextMotion,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005688 isPointerEvent, callingPid, callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005689 if (target == SKIP_TARGET_TOKEN) {
5690 // The user has pressed a special key, and we are
5691 // dropping all pending events before it.
Joe Onorato8a9b2202010-02-26 18:56:32 -08005692 if (DEBUG_INPUT) Slog.v(TAG, "Skipping: " + nextKey
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005693 + " " + nextMotion);
5694 return null;
5695 }
5696 if (target == CONSUMED_EVENT_TOKEN) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005697 if (DEBUG_INPUT) Slog.v(TAG, "Consumed: " + nextKey
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005698 + " " + nextMotion);
5699 return target;
5700 }
5701 targetWin = (WindowState)target;
5702 }
Romain Guy06882f82009-06-10 13:36:04 -07005703
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005704 AppWindowToken targetApp = null;
Romain Guy06882f82009-06-10 13:36:04 -07005705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005706 // Now: is it okay to send the next event to this window?
5707 synchronized (this) {
5708 // First: did we come here based on the last window not
5709 // being null, but it changed by the time we got here?
5710 // If so, try again.
5711 if (!targetIsNew && mLastWin == null) {
5712 continue;
5713 }
Romain Guy06882f82009-06-10 13:36:04 -07005714
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005715 // We never dispatch events if not finished with the
5716 // last one, or the display is frozen.
5717 if (mFinished && !mDisplayFrozen) {
5718 // If event dispatching is disabled, then we
5719 // just consume the events.
5720 if (!mEventDispatching) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005721 if (DEBUG_INPUT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005722 "Skipping event; dispatching disabled: "
5723 + nextKey + " " + nextMotion);
5724 return null;
5725 }
5726 if (targetWin != null) {
5727 // If this is a new target, and that target is not
5728 // paused or unresponsive, then all looks good to
5729 // handle the event.
5730 if (targetIsNew && !targetWin.mToken.paused) {
5731 return targetWin;
5732 }
Romain Guy06882f82009-06-10 13:36:04 -07005733
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005734 // If we didn't find a target window, and there is no
5735 // focused app window, then just eat the events.
5736 } else if (mFocusedApp == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005737 if (DEBUG_INPUT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005738 "Skipping event; no focused app: "
5739 + nextKey + " " + nextMotion);
5740 return null;
5741 }
5742 }
Romain Guy06882f82009-06-10 13:36:04 -07005743
Joe Onorato8a9b2202010-02-26 18:56:32 -08005744 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005745 TAG, "Waiting for last key in " + mLastBinder
5746 + " target=" + targetWin
5747 + " mFinished=" + mFinished
5748 + " mDisplayFrozen=" + mDisplayFrozen
5749 + " targetIsNew=" + targetIsNew
5750 + " paused="
5751 + (targetWin != null ? targetWin.mToken.paused : false)
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005752 + " mFocusedApp=" + mFocusedApp
5753 + " mCurrentFocus=" + mCurrentFocus);
Romain Guy06882f82009-06-10 13:36:04 -07005754
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005755 targetApp = targetWin != null
5756 ? targetWin.mAppToken : mFocusedApp;
Romain Guy06882f82009-06-10 13:36:04 -07005757
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005758 long curTimeout = keyDispatchingTimeout;
5759 if (mTimeToSwitch != 0) {
5760 long now = SystemClock.uptimeMillis();
5761 if (mTimeToSwitch <= now) {
5762 // If an app switch key has been pressed, and we have
5763 // waited too long for the current app to finish
5764 // processing keys, then wait no more!
Christopher Tate136b1f92010-02-11 17:51:24 -08005765 doFinishedKeyLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005766 continue;
5767 }
5768 long switchTimeout = mTimeToSwitch - now;
5769 if (curTimeout > switchTimeout) {
5770 curTimeout = switchTimeout;
5771 }
5772 }
Romain Guy06882f82009-06-10 13:36:04 -07005773
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005774 try {
5775 // after that continue
5776 // processing keys, so we don't get stuck.
Joe Onorato8a9b2202010-02-26 18:56:32 -08005777 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005778 TAG, "Waiting for key dispatch: " + curTimeout);
5779 wait(curTimeout);
Joe Onorato8a9b2202010-02-26 18:56:32 -08005780 if (DEBUG_INPUT) Slog.v(TAG, "Finished waiting @"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005781 + SystemClock.uptimeMillis() + " startTime="
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005782 + startTime + " switchTime=" + mTimeToSwitch
5783 + " target=" + targetWin + " mLW=" + mLastWin
5784 + " mLB=" + mLastBinder + " fin=" + mFinished
5785 + " mCurrentFocus=" + mCurrentFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005786 } catch (InterruptedException e) {
5787 }
5788 }
5789
5790 // If we were frozen during configuration change, restart the
5791 // timeout checks from now; otherwise look at whether we timed
5792 // out before awakening.
5793 if (mWasFrozen) {
5794 waitedFor = 0;
5795 mWasFrozen = false;
5796 } else {
5797 waitedFor = SystemClock.uptimeMillis() - startTime;
5798 }
5799
5800 if (waitedFor >= keyDispatchingTimeout && mTimeToSwitch == 0) {
5801 IApplicationToken at = null;
5802 synchronized (this) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005803 Slog.w(TAG, "Key dispatching timed out sending to " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005804 (targetWin != null ? targetWin.mAttrs.getTitle()
Ken Shirriff8200b202010-02-04 13:34:37 -08005805 : "<null>: no window ready for key dispatch"));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005806 // NOSHIP debugging
Joe Onorato8a9b2202010-02-26 18:56:32 -08005807 Slog.w(TAG, "Previous dispatch state: " + mDispatchState);
5808 Slog.w(TAG, "Current dispatch state: " +
Ken Shirriff8200b202010-02-04 13:34:37 -08005809 new DispatchState(nextKey, targetWin));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005810 // END NOSHIP
5811 //dump();
5812 if (targetWin != null) {
5813 at = targetWin.getAppToken();
5814 } else if (targetApp != null) {
5815 at = targetApp.appToken;
5816 }
5817 }
5818
5819 boolean abort = true;
5820 if (at != null) {
5821 try {
5822 long timeout = at.getKeyDispatchingTimeout();
5823 if (timeout > waitedFor) {
5824 // we did not wait the proper amount of time for this application.
5825 // set the timeout to be the real timeout and wait again.
5826 keyDispatchingTimeout = timeout - waitedFor;
5827 continue;
5828 } else {
5829 abort = at.keyDispatchingTimedOut();
5830 }
5831 } catch (RemoteException ex) {
5832 }
5833 }
5834
5835 synchronized (this) {
5836 if (abort && (mLastWin == targetWin || targetWin == null)) {
5837 mFinished = true;
Romain Guy06882f82009-06-10 13:36:04 -07005838 if (mLastWin != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005839 if (DEBUG_INPUT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005840 "Window " + mLastWin +
5841 " timed out on key input");
5842 if (mLastWin.mToken.paused) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005843 Slog.w(TAG, "Un-pausing dispatching to this window");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005844 mLastWin.mToken.paused = false;
5845 }
5846 }
5847 if (mMotionTarget == targetWin) {
5848 mMotionTarget = null;
5849 }
5850 mLastWin = null;
5851 mLastBinder = null;
5852 if (failIfTimeout || targetWin == null) {
5853 return null;
5854 }
5855 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005856 Slog.w(TAG, "Continuing to wait for key to be dispatched");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005857 startTime = SystemClock.uptimeMillis();
5858 }
5859 }
5860 }
5861 }
5862 }
Romain Guy06882f82009-06-10 13:36:04 -07005863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005864 Object findTargetWindow(KeyEvent nextKey, QueuedEvent qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005865 MotionEvent nextMotion, boolean isPointerEvent,
5866 int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005867 mOutsideTouchTargets = null;
Romain Guy06882f82009-06-10 13:36:04 -07005868
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005869 if (nextKey != null) {
5870 // Find the target window for a normal key event.
5871 final int keycode = nextKey.getKeyCode();
5872 final int repeatCount = nextKey.getRepeatCount();
5873 final boolean down = nextKey.getAction() != KeyEvent.ACTION_UP;
5874 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(keycode);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005875
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005876 if (!dispatch) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005877 if (callingUid == 0 ||
5878 mContext.checkPermission(
5879 android.Manifest.permission.INJECT_EVENTS,
5880 callingPid, callingUid)
5881 == PackageManager.PERMISSION_GRANTED) {
5882 mPolicy.interceptKeyTi(null, keycode,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005883 nextKey.getMetaState(), down, repeatCount,
5884 nextKey.getFlags());
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005885 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08005886 Slog.w(TAG, "Event timeout during app switch: dropping "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005887 + nextKey);
5888 return SKIP_TARGET_TOKEN;
5889 }
Romain Guy06882f82009-06-10 13:36:04 -07005890
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005891 // System.out.println("##### [" + SystemClock.uptimeMillis() + "] WindowManagerService.dispatchKey(" + keycode + ", " + down + ", " + repeatCount + ")");
Romain Guy06882f82009-06-10 13:36:04 -07005892
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005893 WindowState focus = null;
5894 synchronized(mWindowMap) {
5895 focus = getFocusedWindowLocked();
5896 }
Romain Guy06882f82009-06-10 13:36:04 -07005897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005898 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
Romain Guy06882f82009-06-10 13:36:04 -07005899
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005900 if (callingUid == 0 ||
5901 (focus != null && callingUid == focus.mSession.mUid) ||
5902 mContext.checkPermission(
5903 android.Manifest.permission.INJECT_EVENTS,
5904 callingPid, callingUid)
5905 == PackageManager.PERMISSION_GRANTED) {
5906 if (mPolicy.interceptKeyTi(focus,
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005907 keycode, nextKey.getMetaState(), down, repeatCount,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005908 nextKey.getFlags())) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005909 return CONSUMED_EVENT_TOKEN;
5910 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005911 }
Romain Guy06882f82009-06-10 13:36:04 -07005912
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005913 return focus;
Romain Guy06882f82009-06-10 13:36:04 -07005914
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005915 } else if (!isPointerEvent) {
5916 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(-1);
5917 if (!dispatch) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005918 Slog.w(TAG, "Event timeout during app switch: dropping trackball "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005919 + nextMotion);
5920 return SKIP_TARGET_TOKEN;
5921 }
Romain Guy06882f82009-06-10 13:36:04 -07005922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005923 WindowState focus = null;
5924 synchronized(mWindowMap) {
5925 focus = getFocusedWindowLocked();
5926 }
Romain Guy06882f82009-06-10 13:36:04 -07005927
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005928 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
5929 return focus;
5930 }
Romain Guy06882f82009-06-10 13:36:04 -07005931
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005932 if (nextMotion == null) {
5933 return SKIP_TARGET_TOKEN;
5934 }
Romain Guy06882f82009-06-10 13:36:04 -07005935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005936 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(
5937 KeyEvent.KEYCODE_UNKNOWN);
5938 if (!dispatch) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005939 Slog.w(TAG, "Event timeout during app switch: dropping pointer "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005940 + nextMotion);
5941 return SKIP_TARGET_TOKEN;
5942 }
Romain Guy06882f82009-06-10 13:36:04 -07005943
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005944 // Find the target window for a pointer event.
5945 int action = nextMotion.getAction();
5946 final float xf = nextMotion.getX();
5947 final float yf = nextMotion.getY();
5948 final long eventTime = nextMotion.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07005949
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005950 final boolean screenWasOff = qev != null
5951 && (qev.flags&WindowManagerPolicy.FLAG_BRIGHT_HERE) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07005952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005953 WindowState target = null;
Romain Guy06882f82009-06-10 13:36:04 -07005954
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005955 synchronized(mWindowMap) {
5956 synchronized (this) {
5957 if (action == MotionEvent.ACTION_DOWN) {
5958 if (mMotionTarget != null) {
5959 // this is weird, we got a pen down, but we thought it was
5960 // already down!
5961 // XXX: We should probably send an ACTION_UP to the current
5962 // target.
Joe Onorato8a9b2202010-02-26 18:56:32 -08005963 Slog.w(TAG, "Pointer down received while already down in: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005964 + mMotionTarget);
5965 mMotionTarget = null;
5966 }
Romain Guy06882f82009-06-10 13:36:04 -07005967
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005968 // ACTION_DOWN is special, because we need to lock next events to
5969 // the window we'll land onto.
5970 final int x = (int)xf;
5971 final int y = (int)yf;
Romain Guy06882f82009-06-10 13:36:04 -07005972
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005973 final ArrayList windows = mWindows;
5974 final int N = windows.size();
5975 WindowState topErrWindow = null;
5976 final Rect tmpRect = mTempRect;
5977 for (int i=N-1; i>=0; i--) {
5978 WindowState child = (WindowState)windows.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08005979 //Slog.i(TAG, "Checking dispatch to: " + child);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005980 final int flags = child.mAttrs.flags;
5981 if ((flags & WindowManager.LayoutParams.FLAG_SYSTEM_ERROR) != 0) {
5982 if (topErrWindow == null) {
5983 topErrWindow = child;
5984 }
5985 }
5986 if (!child.isVisibleLw()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005987 //Slog.i(TAG, "Not visible!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005988 continue;
5989 }
5990 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005991 //Slog.i(TAG, "Not touchable!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005992 if ((flags & WindowManager.LayoutParams
5993 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5994 child.mNextOutsideTouch = mOutsideTouchTargets;
5995 mOutsideTouchTargets = child;
5996 }
5997 continue;
5998 }
5999 tmpRect.set(child.mFrame);
6000 if (child.mTouchableInsets == ViewTreeObserver
6001 .InternalInsetsInfo.TOUCHABLE_INSETS_CONTENT) {
6002 // The touch is inside of the window if it is
6003 // inside the frame, AND the content part of that
6004 // frame that was given by the application.
6005 tmpRect.left += child.mGivenContentInsets.left;
6006 tmpRect.top += child.mGivenContentInsets.top;
6007 tmpRect.right -= child.mGivenContentInsets.right;
6008 tmpRect.bottom -= child.mGivenContentInsets.bottom;
6009 } else if (child.mTouchableInsets == ViewTreeObserver
6010 .InternalInsetsInfo.TOUCHABLE_INSETS_VISIBLE) {
6011 // The touch is inside of the window if it is
6012 // inside the frame, AND the visible part of that
6013 // frame that was given by the application.
6014 tmpRect.left += child.mGivenVisibleInsets.left;
6015 tmpRect.top += child.mGivenVisibleInsets.top;
6016 tmpRect.right -= child.mGivenVisibleInsets.right;
6017 tmpRect.bottom -= child.mGivenVisibleInsets.bottom;
6018 }
6019 final int touchFlags = flags &
6020 (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
6021 |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
6022 if (tmpRect.contains(x, y) || touchFlags == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006023 //Slog.i(TAG, "Using this target!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006024 if (!screenWasOff || (flags &
6025 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING) != 0) {
6026 mMotionTarget = child;
6027 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006028 //Slog.i(TAG, "Waking, skip!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006029 mMotionTarget = null;
6030 }
6031 break;
6032 }
Romain Guy06882f82009-06-10 13:36:04 -07006033
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006034 if ((flags & WindowManager.LayoutParams
6035 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
6036 child.mNextOutsideTouch = mOutsideTouchTargets;
6037 mOutsideTouchTargets = child;
Joe Onorato8a9b2202010-02-26 18:56:32 -08006038 //Slog.i(TAG, "Adding to outside target list: " + child);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006039 }
6040 }
6041
6042 // if there's an error window but it's not accepting
6043 // focus (typically because it is not yet visible) just
6044 // wait for it -- any other focused window may in fact
6045 // be in ANR state.
6046 if (topErrWindow != null && mMotionTarget != topErrWindow) {
6047 mMotionTarget = null;
6048 }
6049 }
Romain Guy06882f82009-06-10 13:36:04 -07006050
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006051 target = mMotionTarget;
6052 }
6053 }
Romain Guy06882f82009-06-10 13:36:04 -07006054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006055 wakeupIfNeeded(target, eventType(nextMotion));
Romain Guy06882f82009-06-10 13:36:04 -07006056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006057 // Pointer events are a little different -- if there isn't a
6058 // target found for any event, then just drop it.
6059 return target != null ? target : SKIP_TARGET_TOKEN;
6060 }
Romain Guy06882f82009-06-10 13:36:04 -07006061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006062 boolean checkShouldDispatchKey(int keycode) {
6063 synchronized (this) {
6064 if (mPolicy.isAppSwitchKeyTqTiLwLi(keycode)) {
6065 mTimeToSwitch = 0;
6066 return true;
6067 }
6068 if (mTimeToSwitch != 0
6069 && mTimeToSwitch < SystemClock.uptimeMillis()) {
6070 return false;
6071 }
6072 return true;
6073 }
6074 }
Romain Guy06882f82009-06-10 13:36:04 -07006075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006076 void bindTargetWindowLocked(WindowState win,
6077 int pendingWhat, QueuedEvent pendingMotion) {
6078 synchronized (this) {
6079 bindTargetWindowLockedLocked(win, pendingWhat, pendingMotion);
6080 }
6081 }
Romain Guy06882f82009-06-10 13:36:04 -07006082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006083 void bindTargetWindowLocked(WindowState win) {
6084 synchronized (this) {
6085 bindTargetWindowLockedLocked(win, RETURN_NOTHING, null);
6086 }
6087 }
6088
6089 void bindTargetWindowLockedLocked(WindowState win,
6090 int pendingWhat, QueuedEvent pendingMotion) {
6091 mLastWin = win;
6092 mLastBinder = win.mClient.asBinder();
6093 mFinished = false;
6094 if (pendingMotion != null) {
6095 final Session s = win.mSession;
6096 if (pendingWhat == RETURN_PENDING_POINTER) {
6097 releasePendingPointerLocked(s);
6098 s.mPendingPointerMove = pendingMotion;
6099 s.mPendingPointerWindow = win;
Joe Onorato8a9b2202010-02-26 18:56:32 -08006100 if (DEBUG_INPUT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006101 "bindTargetToWindow " + s.mPendingPointerMove);
6102 } else if (pendingWhat == RETURN_PENDING_TRACKBALL) {
6103 releasePendingTrackballLocked(s);
6104 s.mPendingTrackballMove = pendingMotion;
6105 s.mPendingTrackballWindow = win;
6106 }
6107 }
6108 }
Romain Guy06882f82009-06-10 13:36:04 -07006109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006110 void releasePendingPointerLocked(Session s) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006111 if (DEBUG_INPUT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006112 "releasePendingPointer " + s.mPendingPointerMove);
6113 if (s.mPendingPointerMove != null) {
6114 mQueue.recycleEvent(s.mPendingPointerMove);
6115 s.mPendingPointerMove = null;
6116 }
6117 }
Romain Guy06882f82009-06-10 13:36:04 -07006118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006119 void releasePendingTrackballLocked(Session s) {
6120 if (s.mPendingTrackballMove != null) {
6121 mQueue.recycleEvent(s.mPendingTrackballMove);
6122 s.mPendingTrackballMove = null;
6123 }
6124 }
Romain Guy06882f82009-06-10 13:36:04 -07006125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006126 MotionEvent finishedKey(Session session, IWindow client, boolean force,
6127 int returnWhat) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006128 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006129 TAG, "finishedKey: client=" + client + ", force=" + force);
6130
6131 if (client == null) {
6132 return null;
6133 }
6134
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07006135 MotionEvent res = null;
6136 QueuedEvent qev = null;
6137 WindowState win = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006139 synchronized (this) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006140 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006141 TAG, "finishedKey: client=" + client.asBinder()
6142 + ", force=" + force + ", last=" + mLastBinder
6143 + " (token=" + (mLastWin != null ? mLastWin.mToken : null) + ")");
6144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006145 if (returnWhat == RETURN_PENDING_POINTER) {
6146 qev = session.mPendingPointerMove;
6147 win = session.mPendingPointerWindow;
6148 session.mPendingPointerMove = null;
6149 session.mPendingPointerWindow = null;
6150 } else if (returnWhat == RETURN_PENDING_TRACKBALL) {
6151 qev = session.mPendingTrackballMove;
6152 win = session.mPendingTrackballWindow;
6153 session.mPendingTrackballMove = null;
6154 session.mPendingTrackballWindow = null;
6155 }
Romain Guy06882f82009-06-10 13:36:04 -07006156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006157 if (mLastBinder == client.asBinder()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006158 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006159 TAG, "finishedKey: last paused="
6160 + ((mLastWin != null) ? mLastWin.mToken.paused : "null"));
6161 if (mLastWin != null && (!mLastWin.mToken.paused || force
6162 || !mEventDispatching)) {
Christopher Tate136b1f92010-02-11 17:51:24 -08006163 doFinishedKeyLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006164 } else {
6165 // Make sure to wake up anyone currently waiting to
6166 // dispatch a key, so they can re-evaluate their
6167 // current situation.
6168 mFinished = true;
6169 notifyAll();
6170 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006171 }
Romain Guy06882f82009-06-10 13:36:04 -07006172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006173 if (qev != null) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07006174 res = (MotionEvent)qev.event;
Joe Onorato8a9b2202010-02-26 18:56:32 -08006175 if (DEBUG_INPUT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006176 "Returning pending motion: " + res);
6177 mQueue.recycleEvent(qev);
6178 if (win != null && returnWhat == RETURN_PENDING_POINTER) {
6179 res.offsetLocation(-win.mFrame.left, -win.mFrame.top);
6180 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006181 }
Christopher Tate2624fbc2009-12-11 12:11:31 -08006182 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006183
Christopher Tate2624fbc2009-12-11 12:11:31 -08006184 if (res != null && returnWhat == RETURN_PENDING_POINTER) {
6185 synchronized (mWindowMap) {
Dianne Hackborn90d2db32010-02-11 22:19:06 -08006186 dispatchPointerElsewhereLocked(win, win, res, res.getEventTime(), false);
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07006187 }
6188 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006189
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07006190 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006191 }
6192
6193 void tickle() {
6194 synchronized (this) {
6195 notifyAll();
6196 }
6197 }
Romain Guy06882f82009-06-10 13:36:04 -07006198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006199 void handleNewWindowLocked(WindowState newWindow) {
6200 if (!newWindow.canReceiveKeys()) {
6201 return;
6202 }
6203 synchronized (this) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006204 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006205 TAG, "New key dispatch window: win="
6206 + newWindow.mClient.asBinder()
6207 + ", last=" + mLastBinder
6208 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
6209 + "), finished=" + mFinished + ", paused="
6210 + newWindow.mToken.paused);
6211
6212 // Displaying a window implicitly causes dispatching to
6213 // be unpaused. (This is to protect against bugs if someone
6214 // pauses dispatching but forgets to resume.)
6215 newWindow.mToken.paused = false;
6216
6217 mGotFirstWindow = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006218
6219 if ((newWindow.mAttrs.flags & FLAG_SYSTEM_ERROR) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006220 if (DEBUG_INPUT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006221 "New SYSTEM_ERROR window; resetting state");
6222 mLastWin = null;
6223 mLastBinder = null;
6224 mMotionTarget = null;
6225 mFinished = true;
6226 } else if (mLastWin != null) {
6227 // If the new window is above the window we are
6228 // waiting on, then stop waiting and let key dispatching
6229 // start on the new guy.
Joe Onorato8a9b2202010-02-26 18:56:32 -08006230 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006231 TAG, "Last win layer=" + mLastWin.mLayer
6232 + ", new win layer=" + newWindow.mLayer);
6233 if (newWindow.mLayer >= mLastWin.mLayer) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006234 // The new window is above the old; finish pending input to the last
6235 // window and start directing it to the new one.
6236 mLastWin.mToken.paused = false;
Christopher Tate136b1f92010-02-11 17:51:24 -08006237 doFinishedKeyLocked(false); // does a notifyAll()
6238 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006239 }
6240 }
6241
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006242 // Now that we've put a new window state in place, make the event waiter
6243 // take notice and retarget its attentions.
6244 notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006245 }
6246 }
6247
6248 void pauseDispatchingLocked(WindowToken token) {
6249 synchronized (this)
6250 {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006251 if (DEBUG_INPUT) Slog.v(TAG, "Pausing WindowToken " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006252 token.paused = true;
6253
6254 /*
6255 if (mLastWin != null && !mFinished && mLastWin.mBaseLayer <= layer) {
6256 mPaused = true;
6257 } else {
6258 if (mLastWin == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006259 Slog.i(TAG, "Key dispatching not paused: no last window.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006260 } else if (mFinished) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006261 Slog.i(TAG, "Key dispatching not paused: finished last key.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006262 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006263 Slog.i(TAG, "Key dispatching not paused: window in higher layer.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006264 }
6265 }
6266 */
6267 }
6268 }
6269
6270 void resumeDispatchingLocked(WindowToken token) {
6271 synchronized (this) {
6272 if (token.paused) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006273 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006274 TAG, "Resuming WindowToken " + token
6275 + ", last=" + mLastBinder
6276 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
6277 + "), finished=" + mFinished + ", paused="
6278 + token.paused);
6279 token.paused = false;
6280 if (mLastWin != null && mLastWin.mToken == token && mFinished) {
Christopher Tate136b1f92010-02-11 17:51:24 -08006281 doFinishedKeyLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006282 } else {
6283 notifyAll();
6284 }
6285 }
6286 }
6287 }
6288
6289 void setEventDispatchingLocked(boolean enabled) {
6290 synchronized (this) {
6291 mEventDispatching = enabled;
6292 notifyAll();
6293 }
6294 }
Romain Guy06882f82009-06-10 13:36:04 -07006295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006296 void appSwitchComing() {
6297 synchronized (this) {
6298 // Don't wait for more than .5 seconds for app to finish
6299 // processing the pending events.
6300 long now = SystemClock.uptimeMillis() + 500;
Joe Onorato8a9b2202010-02-26 18:56:32 -08006301 if (DEBUG_INPUT) Slog.v(TAG, "appSwitchComing: " + now);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006302 if (mTimeToSwitch == 0 || now < mTimeToSwitch) {
6303 mTimeToSwitch = now;
6304 }
6305 notifyAll();
6306 }
6307 }
Romain Guy06882f82009-06-10 13:36:04 -07006308
Christopher Tate136b1f92010-02-11 17:51:24 -08006309 private final void doFinishedKeyLocked(boolean force) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006310 if (mLastWin != null) {
6311 releasePendingPointerLocked(mLastWin.mSession);
6312 releasePendingTrackballLocked(mLastWin.mSession);
6313 }
Romain Guy06882f82009-06-10 13:36:04 -07006314
Christopher Tate136b1f92010-02-11 17:51:24 -08006315 if (force || mLastWin == null || !mLastWin.mToken.paused
6316 || !mLastWin.isVisibleLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006317 // If the current window has been paused, we aren't -really-
6318 // finished... so let the waiters still wait.
6319 mLastWin = null;
6320 mLastBinder = null;
6321 }
6322 mFinished = true;
6323 notifyAll();
6324 }
6325 }
6326
6327 private class KeyQ extends KeyInputQueue
6328 implements KeyInputQueue.FilterCallback {
6329 PowerManager.WakeLock mHoldingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07006330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006331 KeyQ() {
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07006332 super(mContext, WindowManagerService.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006333 PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
6334 mHoldingScreen = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
6335 "KEEP_SCREEN_ON_FLAG");
6336 mHoldingScreen.setReferenceCounted(false);
6337 }
6338
6339 @Override
6340 boolean preprocessEvent(InputDevice device, RawInputEvent event) {
6341 if (mPolicy.preprocessInputEventTq(event)) {
6342 return true;
6343 }
Romain Guy06882f82009-06-10 13:36:04 -07006344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006345 switch (event.type) {
6346 case RawInputEvent.EV_KEY: {
6347 // XXX begin hack
6348 if (DEBUG) {
6349 if (event.keycode == KeyEvent.KEYCODE_G) {
6350 if (event.value != 0) {
6351 // G down
6352 mPolicy.screenTurnedOff(WindowManagerPolicy.OFF_BECAUSE_OF_USER);
6353 }
6354 return false;
6355 }
6356 if (event.keycode == KeyEvent.KEYCODE_D) {
6357 if (event.value != 0) {
6358 //dump();
6359 }
6360 return false;
6361 }
6362 }
6363 // XXX end hack
Romain Guy06882f82009-06-10 13:36:04 -07006364
Charles Mendis322591c2009-10-29 11:06:59 -07006365 boolean screenIsOff = !mPowerManager.isScreenOn();
6366 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006367 int actions = mPolicy.interceptKeyTq(event, !screenIsOff);
Romain Guy06882f82009-06-10 13:36:04 -07006368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006369 if ((actions & WindowManagerPolicy.ACTION_GO_TO_SLEEP) != 0) {
6370 mPowerManager.goToSleep(event.when);
6371 }
6372
6373 if (screenIsOff) {
6374 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6375 }
6376 if (screenIsDim) {
6377 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6378 }
6379 if ((actions & WindowManagerPolicy.ACTION_POKE_USER_ACTIVITY) != 0) {
6380 mPowerManager.userActivity(event.when, false,
Michael Chane96440f2009-05-06 10:27:36 -07006381 LocalPowerManager.BUTTON_EVENT, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006382 }
Romain Guy06882f82009-06-10 13:36:04 -07006383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006384 if ((actions & WindowManagerPolicy.ACTION_PASS_TO_USER) != 0) {
6385 if (event.value != 0 && mPolicy.isAppSwitchKeyTqTiLwLi(event.keycode)) {
6386 filterQueue(this);
6387 mKeyWaiter.appSwitchComing();
6388 }
6389 return true;
6390 } else {
6391 return false;
6392 }
6393 }
Romain Guy06882f82009-06-10 13:36:04 -07006394
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006395 case RawInputEvent.EV_REL: {
Charles Mendis322591c2009-10-29 11:06:59 -07006396 boolean screenIsOff = !mPowerManager.isScreenOn();
6397 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006398 if (screenIsOff) {
6399 if (!mPolicy.isWakeRelMovementTq(event.deviceId,
6400 device.classes, event)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006401 //Slog.i(TAG, "dropping because screenIsOff and !isWakeKey");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006402 return false;
6403 }
6404 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6405 }
6406 if (screenIsDim) {
6407 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6408 }
6409 return true;
6410 }
Romain Guy06882f82009-06-10 13:36:04 -07006411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006412 case RawInputEvent.EV_ABS: {
Charles Mendis322591c2009-10-29 11:06:59 -07006413 boolean screenIsOff = !mPowerManager.isScreenOn();
6414 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006415 if (screenIsOff) {
6416 if (!mPolicy.isWakeAbsMovementTq(event.deviceId,
6417 device.classes, event)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006418 //Slog.i(TAG, "dropping because screenIsOff and !isWakeKey");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006419 return false;
6420 }
6421 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6422 }
6423 if (screenIsDim) {
6424 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6425 }
6426 return true;
6427 }
Romain Guy06882f82009-06-10 13:36:04 -07006428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006429 default:
6430 return true;
6431 }
6432 }
6433
6434 public int filterEvent(QueuedEvent ev) {
6435 switch (ev.classType) {
6436 case RawInputEvent.CLASS_KEYBOARD:
6437 KeyEvent ke = (KeyEvent)ev.event;
6438 if (mPolicy.isMovementKeyTi(ke.getKeyCode())) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006439 Slog.w(TAG, "Dropping movement key during app switch: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006440 + ke.getKeyCode() + ", action=" + ke.getAction());
6441 return FILTER_REMOVE;
6442 }
6443 return FILTER_ABORT;
6444 default:
6445 return FILTER_KEEP;
6446 }
6447 }
Romain Guy06882f82009-06-10 13:36:04 -07006448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006449 /**
6450 * Must be called with the main window manager lock held.
6451 */
6452 void setHoldScreenLocked(boolean holding) {
6453 boolean state = mHoldingScreen.isHeld();
6454 if (holding != state) {
6455 if (holding) {
6456 mHoldingScreen.acquire();
6457 } else {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006458 mPolicy.screenOnStoppedLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006459 mHoldingScreen.release();
6460 }
6461 }
6462 }
Michael Chan53071d62009-05-13 17:29:48 -07006463 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006464
6465 public boolean detectSafeMode() {
6466 mSafeMode = mPolicy.detectSafeMode();
6467 return mSafeMode;
6468 }
Romain Guy06882f82009-06-10 13:36:04 -07006469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006470 public void systemReady() {
6471 mPolicy.systemReady();
6472 }
Romain Guy06882f82009-06-10 13:36:04 -07006473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006474 private final class InputDispatcherThread extends Thread {
6475 // Time to wait when there is nothing to do: 9999 seconds.
6476 static final int LONG_WAIT=9999*1000;
6477
6478 public InputDispatcherThread() {
6479 super("InputDispatcher");
6480 }
Romain Guy06882f82009-06-10 13:36:04 -07006481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006482 @Override
6483 public void run() {
6484 while (true) {
6485 try {
6486 process();
6487 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006488 Slog.e(TAG, "Exception in input dispatcher", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006489 }
6490 }
6491 }
Romain Guy06882f82009-06-10 13:36:04 -07006492
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006493 private void process() {
6494 android.os.Process.setThreadPriority(
6495 android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -07006496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006497 // The last key event we saw
6498 KeyEvent lastKey = null;
6499
6500 // Last keydown time for auto-repeating keys
6501 long lastKeyTime = SystemClock.uptimeMillis();
6502 long nextKeyTime = lastKeyTime+LONG_WAIT;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006503 long downTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006504
Romain Guy06882f82009-06-10 13:36:04 -07006505 // How many successive repeats we generated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006506 int keyRepeatCount = 0;
6507
6508 // Need to report that configuration has changed?
6509 boolean configChanged = false;
Romain Guy06882f82009-06-10 13:36:04 -07006510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006511 while (true) {
6512 long curTime = SystemClock.uptimeMillis();
6513
Joe Onorato8a9b2202010-02-26 18:56:32 -08006514 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006515 TAG, "Waiting for next key: now=" + curTime
6516 + ", repeat @ " + nextKeyTime);
6517
6518 // Retrieve next event, waiting only as long as the next
6519 // repeat timeout. If the configuration has changed, then
6520 // don't wait at all -- we'll report the change as soon as
6521 // we have processed all events.
6522 QueuedEvent ev = mQueue.getEvent(
6523 (int)((!configChanged && curTime < nextKeyTime)
6524 ? (nextKeyTime-curTime) : 0));
6525
Joe Onorato8a9b2202010-02-26 18:56:32 -08006526 if (DEBUG_INPUT && ev != null) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006527 TAG, "Event: type=" + ev.classType + " data=" + ev.event);
6528
Michael Chan53071d62009-05-13 17:29:48 -07006529 if (MEASURE_LATENCY) {
6530 lt.sample("2 got event ", System.nanoTime() - ev.whenNano);
6531 }
6532
Mike Lockwood3d0ea722009-10-21 22:58:29 -04006533 if (lastKey != null && !mPolicy.allowKeyRepeat()) {
6534 // cancel key repeat at the request of the policy.
6535 lastKey = null;
6536 downTime = 0;
6537 lastKeyTime = curTime;
6538 nextKeyTime = curTime + LONG_WAIT;
6539 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006540 try {
6541 if (ev != null) {
Michael Chan53071d62009-05-13 17:29:48 -07006542 curTime = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006543 int eventType;
6544 if (ev.classType == RawInputEvent.CLASS_TOUCHSCREEN) {
6545 eventType = eventType((MotionEvent)ev.event);
6546 } else if (ev.classType == RawInputEvent.CLASS_KEYBOARD ||
6547 ev.classType == RawInputEvent.CLASS_TRACKBALL) {
6548 eventType = LocalPowerManager.BUTTON_EVENT;
6549 } else {
6550 eventType = LocalPowerManager.OTHER_EVENT;
6551 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006552 try {
Michael Chan53071d62009-05-13 17:29:48 -07006553 if ((curTime - mLastBatteryStatsCallTime)
Michael Chane96440f2009-05-06 10:27:36 -07006554 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
Michael Chan53071d62009-05-13 17:29:48 -07006555 mLastBatteryStatsCallTime = curTime;
Michael Chane96440f2009-05-06 10:27:36 -07006556 mBatteryStats.noteInputEvent();
6557 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006558 } catch (RemoteException e) {
6559 // Ignore
6560 }
Michael Chane10de972009-05-18 11:24:50 -07006561
Mike Lockwood5db42402009-11-30 14:51:51 -05006562 if (ev.classType == RawInputEvent.CLASS_CONFIGURATION_CHANGED) {
6563 // do not wake screen in this case
6564 } else if (eventType != TOUCH_EVENT
Michael Chane10de972009-05-18 11:24:50 -07006565 && eventType != LONG_TOUCH_EVENT
6566 && eventType != CHEEK_EVENT) {
6567 mPowerManager.userActivity(curTime, false,
6568 eventType, false);
6569 } else if (mLastTouchEventType != eventType
6570 || (curTime - mLastUserActivityCallTime)
6571 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
6572 mLastUserActivityCallTime = curTime;
6573 mLastTouchEventType = eventType;
6574 mPowerManager.userActivity(curTime, false,
6575 eventType, false);
6576 }
6577
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006578 switch (ev.classType) {
6579 case RawInputEvent.CLASS_KEYBOARD:
6580 KeyEvent ke = (KeyEvent)ev.event;
6581 if (ke.isDown()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006582 lastKeyTime = curTime;
Kristian Dreher133bfdf2010-02-23 08:50:58 +01006583 if (lastKey != null &&
6584 ke.getKeyCode() == lastKey.getKeyCode()) {
6585 keyRepeatCount++;
6586 // Arbitrary long timeout to block
6587 // repeating here since we know that
6588 // the device driver takes care of it.
6589 nextKeyTime = lastKeyTime + LONG_WAIT;
The Android Open Source Project2a9ae012010-05-12 12:33:35 -07006590 if (DEBUG_INPUT) Slog.v(
Kristian Dreher133bfdf2010-02-23 08:50:58 +01006591 TAG, "Received repeated key down");
6592 } else {
6593 downTime = curTime;
6594 keyRepeatCount = 0;
6595 nextKeyTime = lastKeyTime
6596 + ViewConfiguration.getLongPressTimeout();
The Android Open Source Project2a9ae012010-05-12 12:33:35 -07006597 if (DEBUG_INPUT) Slog.v(
Kristian Dreher133bfdf2010-02-23 08:50:58 +01006598 TAG, "Received key down: first repeat @ "
6599 + nextKeyTime);
6600 }
6601 lastKey = ke;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006602 } else {
6603 lastKey = null;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006604 downTime = 0;
Kristian Dreher133bfdf2010-02-23 08:50:58 +01006605 keyRepeatCount = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006606 // Arbitrary long timeout.
6607 lastKeyTime = curTime;
6608 nextKeyTime = curTime + LONG_WAIT;
Joe Onorato8a9b2202010-02-26 18:56:32 -08006609 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006610 TAG, "Received key up: ignore repeat @ "
6611 + nextKeyTime);
6612 }
Kristian Dreher133bfdf2010-02-23 08:50:58 +01006613 if (keyRepeatCount > 0) {
6614 dispatchKey(KeyEvent.changeTimeRepeat(ke,
6615 ke.getEventTime(), keyRepeatCount), 0, 0);
6616 } else {
6617 dispatchKey(ke, 0, 0);
6618 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006619 mQueue.recycleEvent(ev);
6620 break;
6621 case RawInputEvent.CLASS_TOUCHSCREEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08006622 //Slog.i(TAG, "Read next event " + ev);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006623 dispatchPointer(ev, (MotionEvent)ev.event, 0, 0);
6624 break;
6625 case RawInputEvent.CLASS_TRACKBALL:
6626 dispatchTrackball(ev, (MotionEvent)ev.event, 0, 0);
6627 break;
6628 case RawInputEvent.CLASS_CONFIGURATION_CHANGED:
6629 configChanged = true;
6630 break;
6631 default:
6632 mQueue.recycleEvent(ev);
6633 break;
6634 }
Romain Guy06882f82009-06-10 13:36:04 -07006635
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006636 } else if (configChanged) {
6637 configChanged = false;
6638 sendNewConfiguration();
Romain Guy06882f82009-06-10 13:36:04 -07006639
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006640 } else if (lastKey != null) {
6641 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006642
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006643 // Timeout occurred while key was down. If it is at or
6644 // past the key repeat time, dispatch the repeat.
Joe Onorato8a9b2202010-02-26 18:56:32 -08006645 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006646 TAG, "Key timeout: repeat=" + nextKeyTime
6647 + ", now=" + curTime);
6648 if (curTime < nextKeyTime) {
6649 continue;
6650 }
Romain Guy06882f82009-06-10 13:36:04 -07006651
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006652 lastKeyTime = nextKeyTime;
6653 nextKeyTime = nextKeyTime + KEY_REPEAT_DELAY;
6654 keyRepeatCount++;
Joe Onorato8a9b2202010-02-26 18:56:32 -08006655 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006656 TAG, "Key repeat: count=" + keyRepeatCount
6657 + ", next @ " + nextKeyTime);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006658 KeyEvent newEvent;
6659 if (downTime != 0 && (downTime
6660 + ViewConfiguration.getLongPressTimeout())
6661 <= curTime) {
6662 newEvent = KeyEvent.changeTimeRepeat(lastKey,
6663 curTime, keyRepeatCount,
6664 lastKey.getFlags() | KeyEvent.FLAG_LONG_PRESS);
6665 downTime = 0;
6666 } else {
6667 newEvent = KeyEvent.changeTimeRepeat(lastKey,
6668 curTime, keyRepeatCount);
6669 }
6670 dispatchKey(newEvent, 0, 0);
Romain Guy06882f82009-06-10 13:36:04 -07006671
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006672 } else {
6673 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006674
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006675 lastKeyTime = curTime;
6676 nextKeyTime = curTime + LONG_WAIT;
6677 }
Romain Guy06882f82009-06-10 13:36:04 -07006678
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006679 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006680 Slog.e(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006681 "Input thread received uncaught exception: " + e, e);
6682 }
6683 }
6684 }
6685 }
6686
6687 // -------------------------------------------------------------
6688 // Client Session State
6689 // -------------------------------------------------------------
6690
6691 private final class Session extends IWindowSession.Stub
6692 implements IBinder.DeathRecipient {
6693 final IInputMethodClient mClient;
6694 final IInputContext mInputContext;
6695 final int mUid;
6696 final int mPid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006697 final String mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006698 SurfaceSession mSurfaceSession;
6699 int mNumWindow = 0;
6700 boolean mClientDead = false;
Romain Guy06882f82009-06-10 13:36:04 -07006701
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006702 /**
6703 * Current pointer move event being dispatched to client window... must
6704 * hold key lock to access.
6705 */
6706 QueuedEvent mPendingPointerMove;
6707 WindowState mPendingPointerWindow;
Romain Guy06882f82009-06-10 13:36:04 -07006708
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006709 /**
6710 * Current trackball move event being dispatched to client window... must
6711 * hold key lock to access.
6712 */
6713 QueuedEvent mPendingTrackballMove;
6714 WindowState mPendingTrackballWindow;
6715
6716 public Session(IInputMethodClient client, IInputContext inputContext) {
6717 mClient = client;
6718 mInputContext = inputContext;
6719 mUid = Binder.getCallingUid();
6720 mPid = Binder.getCallingPid();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006721 StringBuilder sb = new StringBuilder();
6722 sb.append("Session{");
6723 sb.append(Integer.toHexString(System.identityHashCode(this)));
6724 sb.append(" uid ");
6725 sb.append(mUid);
6726 sb.append("}");
6727 mStringName = sb.toString();
Romain Guy06882f82009-06-10 13:36:04 -07006728
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006729 synchronized (mWindowMap) {
6730 if (mInputMethodManager == null && mHaveInputMethods) {
6731 IBinder b = ServiceManager.getService(
6732 Context.INPUT_METHOD_SERVICE);
6733 mInputMethodManager = IInputMethodManager.Stub.asInterface(b);
6734 }
6735 }
6736 long ident = Binder.clearCallingIdentity();
6737 try {
6738 // Note: it is safe to call in to the input method manager
6739 // here because we are not holding our lock.
6740 if (mInputMethodManager != null) {
6741 mInputMethodManager.addClient(client, inputContext,
6742 mUid, mPid);
6743 } else {
6744 client.setUsingInputMethod(false);
6745 }
6746 client.asBinder().linkToDeath(this, 0);
6747 } catch (RemoteException e) {
6748 // The caller has died, so we can just forget about this.
6749 try {
6750 if (mInputMethodManager != null) {
6751 mInputMethodManager.removeClient(client);
6752 }
6753 } catch (RemoteException ee) {
6754 }
6755 } finally {
6756 Binder.restoreCallingIdentity(ident);
6757 }
6758 }
Romain Guy06882f82009-06-10 13:36:04 -07006759
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006760 @Override
6761 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
6762 throws RemoteException {
6763 try {
6764 return super.onTransact(code, data, reply, flags);
6765 } catch (RuntimeException e) {
6766 // Log all 'real' exceptions thrown to the caller
6767 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006768 Slog.e(TAG, "Window Session Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006769 }
6770 throw e;
6771 }
6772 }
6773
6774 public void binderDied() {
6775 // Note: it is safe to call in to the input method manager
6776 // here because we are not holding our lock.
6777 try {
6778 if (mInputMethodManager != null) {
6779 mInputMethodManager.removeClient(mClient);
6780 }
6781 } catch (RemoteException e) {
6782 }
6783 synchronized(mWindowMap) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07006784 mClient.asBinder().unlinkToDeath(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006785 mClientDead = true;
6786 killSessionLocked();
6787 }
6788 }
6789
6790 public int add(IWindow window, WindowManager.LayoutParams attrs,
6791 int viewVisibility, Rect outContentInsets) {
6792 return addWindow(this, window, attrs, viewVisibility, outContentInsets);
6793 }
Romain Guy06882f82009-06-10 13:36:04 -07006794
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006795 public void remove(IWindow window) {
6796 removeWindow(this, window);
6797 }
Romain Guy06882f82009-06-10 13:36:04 -07006798
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006799 public int relayout(IWindow window, WindowManager.LayoutParams attrs,
6800 int requestedWidth, int requestedHeight, int viewFlags,
6801 boolean insetsPending, Rect outFrame, Rect outContentInsets,
Dianne Hackborn694f79b2010-03-17 19:44:59 -07006802 Rect outVisibleInsets, Configuration outConfig, Surface outSurface) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006803 return relayoutWindow(this, window, attrs,
6804 requestedWidth, requestedHeight, viewFlags, insetsPending,
Dianne Hackborn694f79b2010-03-17 19:44:59 -07006805 outFrame, outContentInsets, outVisibleInsets, outConfig, outSurface);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006806 }
Romain Guy06882f82009-06-10 13:36:04 -07006807
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006808 public void setTransparentRegion(IWindow window, Region region) {
6809 setTransparentRegionWindow(this, window, region);
6810 }
Romain Guy06882f82009-06-10 13:36:04 -07006811
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006812 public void setInsets(IWindow window, int touchableInsets,
6813 Rect contentInsets, Rect visibleInsets) {
6814 setInsetsWindow(this, window, touchableInsets, contentInsets,
6815 visibleInsets);
6816 }
Romain Guy06882f82009-06-10 13:36:04 -07006817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006818 public void getDisplayFrame(IWindow window, Rect outDisplayFrame) {
6819 getWindowDisplayFrame(this, window, outDisplayFrame);
6820 }
Romain Guy06882f82009-06-10 13:36:04 -07006821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006822 public void finishDrawing(IWindow window) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006823 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006824 TAG, "IWindow finishDrawing called for " + window);
6825 finishDrawingWindow(this, window);
6826 }
6827
6828 public void finishKey(IWindow window) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006829 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006830 TAG, "IWindow finishKey called for " + window);
6831 mKeyWaiter.finishedKey(this, window, false,
6832 KeyWaiter.RETURN_NOTHING);
6833 }
6834
6835 public MotionEvent getPendingPointerMove(IWindow window) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006836 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006837 TAG, "IWindow getPendingMotionEvent called for " + window);
6838 return mKeyWaiter.finishedKey(this, window, false,
6839 KeyWaiter.RETURN_PENDING_POINTER);
6840 }
Romain Guy06882f82009-06-10 13:36:04 -07006841
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006842 public MotionEvent getPendingTrackballMove(IWindow window) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006843 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006844 TAG, "IWindow getPendingMotionEvent called for " + window);
6845 return mKeyWaiter.finishedKey(this, window, false,
6846 KeyWaiter.RETURN_PENDING_TRACKBALL);
6847 }
6848
6849 public void setInTouchMode(boolean mode) {
6850 synchronized(mWindowMap) {
6851 mInTouchMode = mode;
6852 }
6853 }
6854
6855 public boolean getInTouchMode() {
6856 synchronized(mWindowMap) {
6857 return mInTouchMode;
6858 }
6859 }
6860
6861 public boolean performHapticFeedback(IWindow window, int effectId,
6862 boolean always) {
6863 synchronized(mWindowMap) {
6864 long ident = Binder.clearCallingIdentity();
6865 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006866 return mPolicy.performHapticFeedbackLw(
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006867 windowForClientLocked(this, window, true),
6868 effectId, always);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006869 } finally {
6870 Binder.restoreCallingIdentity(ident);
6871 }
6872 }
6873 }
Romain Guy06882f82009-06-10 13:36:04 -07006874
Marco Nelissenbf6956b2009-11-09 15:21:13 -08006875 public void setWallpaperPosition(IBinder window, float x, float y, float xStep, float yStep) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006876 synchronized(mWindowMap) {
6877 long ident = Binder.clearCallingIdentity();
6878 try {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006879 setWindowWallpaperPositionLocked(
6880 windowForClientLocked(this, window, true),
Marco Nelissenbf6956b2009-11-09 15:21:13 -08006881 x, y, xStep, yStep);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006882 } finally {
6883 Binder.restoreCallingIdentity(ident);
6884 }
6885 }
6886 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006887
Dianne Hackborn19382ac2009-09-11 21:13:37 -07006888 public void wallpaperOffsetsComplete(IBinder window) {
6889 WindowManagerService.this.wallpaperOffsetsComplete(window);
6890 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006891
Dianne Hackborn75804932009-10-20 20:15:20 -07006892 public Bundle sendWallpaperCommand(IBinder window, String action, int x, int y,
6893 int z, Bundle extras, boolean sync) {
6894 synchronized(mWindowMap) {
6895 long ident = Binder.clearCallingIdentity();
6896 try {
6897 return sendWindowWallpaperCommandLocked(
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006898 windowForClientLocked(this, window, true),
Dianne Hackborn75804932009-10-20 20:15:20 -07006899 action, x, y, z, extras, sync);
6900 } finally {
6901 Binder.restoreCallingIdentity(ident);
6902 }
6903 }
6904 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006905
Dianne Hackborn75804932009-10-20 20:15:20 -07006906 public void wallpaperCommandComplete(IBinder window, Bundle result) {
6907 WindowManagerService.this.wallpaperCommandComplete(window, result);
6908 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006909
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006910 void windowAddedLocked() {
6911 if (mSurfaceSession == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006912 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006913 TAG, "First window added to " + this + ", creating SurfaceSession");
6914 mSurfaceSession = new SurfaceSession();
Joe Onorato8a9b2202010-02-26 18:56:32 -08006915 if (SHOW_TRANSACTIONS) Slog.i(
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006916 TAG, " NEW SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006917 mSessions.add(this);
6918 }
6919 mNumWindow++;
6920 }
6921
6922 void windowRemovedLocked() {
6923 mNumWindow--;
6924 killSessionLocked();
6925 }
Romain Guy06882f82009-06-10 13:36:04 -07006926
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006927 void killSessionLocked() {
6928 if (mNumWindow <= 0 && mClientDead) {
6929 mSessions.remove(this);
6930 if (mSurfaceSession != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006931 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006932 TAG, "Last window removed from " + this
6933 + ", destroying " + mSurfaceSession);
Joe Onorato8a9b2202010-02-26 18:56:32 -08006934 if (SHOW_TRANSACTIONS) Slog.i(
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006935 TAG, " KILL SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006936 try {
6937 mSurfaceSession.kill();
6938 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006939 Slog.w(TAG, "Exception thrown when killing surface session "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006940 + mSurfaceSession + " in session " + this
6941 + ": " + e.toString());
6942 }
6943 mSurfaceSession = null;
6944 }
6945 }
6946 }
Romain Guy06882f82009-06-10 13:36:04 -07006947
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006948 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006949 pw.print(prefix); pw.print("mNumWindow="); pw.print(mNumWindow);
6950 pw.print(" mClientDead="); pw.print(mClientDead);
6951 pw.print(" mSurfaceSession="); pw.println(mSurfaceSession);
6952 if (mPendingPointerWindow != null || mPendingPointerMove != null) {
6953 pw.print(prefix);
6954 pw.print("mPendingPointerWindow="); pw.print(mPendingPointerWindow);
6955 pw.print(" mPendingPointerMove="); pw.println(mPendingPointerMove);
6956 }
6957 if (mPendingTrackballWindow != null || mPendingTrackballMove != null) {
6958 pw.print(prefix);
6959 pw.print("mPendingTrackballWindow="); pw.print(mPendingTrackballWindow);
6960 pw.print(" mPendingTrackballMove="); pw.println(mPendingTrackballMove);
6961 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006962 }
6963
6964 @Override
6965 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006966 return mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006967 }
6968 }
6969
6970 // -------------------------------------------------------------
6971 // Client Window State
6972 // -------------------------------------------------------------
6973
6974 private final class WindowState implements WindowManagerPolicy.WindowState {
6975 final Session mSession;
6976 final IWindow mClient;
6977 WindowToken mToken;
The Android Open Source Project10592532009-03-18 17:39:46 -07006978 WindowToken mRootToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006979 AppWindowToken mAppToken;
6980 AppWindowToken mTargetAppToken;
6981 final WindowManager.LayoutParams mAttrs = new WindowManager.LayoutParams();
6982 final DeathRecipient mDeathRecipient;
6983 final WindowState mAttachedWindow;
6984 final ArrayList mChildWindows = new ArrayList();
6985 final int mBaseLayer;
6986 final int mSubLayer;
6987 final boolean mLayoutAttached;
6988 final boolean mIsImWindow;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006989 final boolean mIsWallpaper;
6990 final boolean mIsFloatingLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006991 int mViewVisibility;
6992 boolean mPolicyVisibility = true;
6993 boolean mPolicyVisibilityAfterAnim = true;
6994 boolean mAppFreezing;
6995 Surface mSurface;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07006996 boolean mReportDestroySurface;
6997 boolean mSurfacePendingDestroy;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006998 boolean mAttachedHidden; // is our parent window hidden?
6999 boolean mLastHidden; // was this window last hidden?
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007000 boolean mWallpaperVisible; // for wallpaper, what was last vis report?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007001 int mRequestedWidth;
7002 int mRequestedHeight;
7003 int mLastRequestedWidth;
7004 int mLastRequestedHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007005 int mLayer;
7006 int mAnimLayer;
7007 int mLastLayer;
7008 boolean mHaveFrame;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007009 boolean mObscured;
Dianne Hackborn93e462b2009-09-15 22:50:40 -07007010 boolean mTurnOnScreen;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007011
7012 WindowState mNextOutsideTouch;
Romain Guy06882f82009-06-10 13:36:04 -07007013
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007014 int mLayoutSeq = -1;
7015
7016 Configuration mConfiguration = null;
7017
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007018 // Actual frame shown on-screen (may be modified by animation)
7019 final Rect mShownFrame = new Rect();
7020 final Rect mLastShownFrame = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07007021
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007022 /**
Dianne Hackbornac3587d2010-03-11 11:12:11 -08007023 * Set when we have changed the size of the surface, to know that
7024 * we must tell them application to resize (and thus redraw itself).
7025 */
7026 boolean mSurfaceResized;
7027
7028 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007029 * Insets that determine the actually visible area
7030 */
7031 final Rect mVisibleInsets = new Rect();
7032 final Rect mLastVisibleInsets = new Rect();
7033 boolean mVisibleInsetsChanged;
7034
7035 /**
7036 * Insets that are covered by system windows
7037 */
7038 final Rect mContentInsets = new Rect();
7039 final Rect mLastContentInsets = new Rect();
7040 boolean mContentInsetsChanged;
7041
7042 /**
7043 * Set to true if we are waiting for this window to receive its
7044 * given internal insets before laying out other windows based on it.
7045 */
7046 boolean mGivenInsetsPending;
Romain Guy06882f82009-06-10 13:36:04 -07007047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007048 /**
7049 * These are the content insets that were given during layout for
7050 * this window, to be applied to windows behind it.
7051 */
7052 final Rect mGivenContentInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07007053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007054 /**
7055 * These are the visible insets that were given during layout for
7056 * this window, to be applied to windows behind it.
7057 */
7058 final Rect mGivenVisibleInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07007059
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007060 /**
7061 * Flag indicating whether the touchable region should be adjusted by
7062 * the visible insets; if false the area outside the visible insets is
7063 * NOT touchable, so we must use those to adjust the frame during hit
7064 * tests.
7065 */
7066 int mTouchableInsets = ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME;
Romain Guy06882f82009-06-10 13:36:04 -07007067
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007068 // Current transformation being applied.
7069 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
7070 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
7071 float mHScale=1, mVScale=1;
7072 float mLastHScale=1, mLastVScale=1;
7073 final Matrix mTmpMatrix = new Matrix();
7074
7075 // "Real" frame that the application sees.
7076 final Rect mFrame = new Rect();
7077 final Rect mLastFrame = new Rect();
7078
7079 final Rect mContainingFrame = new Rect();
7080 final Rect mDisplayFrame = new Rect();
7081 final Rect mContentFrame = new Rect();
7082 final Rect mVisibleFrame = new Rect();
7083
7084 float mShownAlpha = 1;
7085 float mAlpha = 1;
7086 float mLastAlpha = 1;
7087
7088 // Set to true if, when the window gets displayed, it should perform
7089 // an enter animation.
7090 boolean mEnterAnimationPending;
7091
7092 // Currently running animation.
7093 boolean mAnimating;
7094 boolean mLocalAnimating;
7095 Animation mAnimation;
7096 boolean mAnimationIsEntrance;
7097 boolean mHasTransformation;
7098 boolean mHasLocalTransformation;
7099 final Transformation mTransformation = new Transformation();
7100
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07007101 // If a window showing a wallpaper: the requested offset for the
7102 // wallpaper; if a wallpaper window: the currently applied offset.
7103 float mWallpaperX = -1;
7104 float mWallpaperY = -1;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08007105
7106 // If a window showing a wallpaper: what fraction of the offset
7107 // range corresponds to a full virtual screen.
7108 float mWallpaperXStep = -1;
7109 float mWallpaperYStep = -1;
7110
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07007111 // Wallpaper windows: pixels offset based on above variables.
7112 int mXOffset;
7113 int mYOffset;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007115 // This is set after IWindowSession.relayout() has been called at
7116 // least once for the window. It allows us to detect the situation
7117 // where we don't yet have a surface, but should have one soon, so
7118 // we can give the window focus before waiting for the relayout.
7119 boolean mRelayoutCalled;
Romain Guy06882f82009-06-10 13:36:04 -07007120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007121 // This is set after the Surface has been created but before the
7122 // window has been drawn. During this time the surface is hidden.
7123 boolean mDrawPending;
7124
7125 // This is set after the window has finished drawing for the first
7126 // time but before its surface is shown. The surface will be
7127 // displayed when the next layout is run.
7128 boolean mCommitDrawPending;
7129
7130 // This is set during the time after the window's drawing has been
7131 // committed, and before its surface is actually shown. It is used
7132 // to delay showing the surface until all windows in a token are ready
7133 // to be shown.
7134 boolean mReadyToShow;
Romain Guy06882f82009-06-10 13:36:04 -07007135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007136 // Set when the window has been shown in the screen the first time.
7137 boolean mHasDrawn;
7138
7139 // Currently running an exit animation?
7140 boolean mExiting;
7141
7142 // Currently on the mDestroySurface list?
7143 boolean mDestroying;
Romain Guy06882f82009-06-10 13:36:04 -07007144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007145 // Completely remove from window manager after exit animation?
7146 boolean mRemoveOnExit;
7147
7148 // Set when the orientation is changing and this window has not yet
7149 // been updated for the new orientation.
7150 boolean mOrientationChanging;
Romain Guy06882f82009-06-10 13:36:04 -07007151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007152 // Is this window now (or just being) removed?
7153 boolean mRemoved;
Romain Guy06882f82009-06-10 13:36:04 -07007154
Dianne Hackborn16064f92010-03-25 00:47:24 -07007155 // For debugging, this is the last information given to the surface flinger.
7156 boolean mSurfaceShown;
7157 int mSurfaceX, mSurfaceY, mSurfaceW, mSurfaceH;
7158 int mSurfaceLayer;
7159 float mSurfaceAlpha;
7160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007161 WindowState(Session s, IWindow c, WindowToken token,
7162 WindowState attachedWindow, WindowManager.LayoutParams a,
7163 int viewVisibility) {
7164 mSession = s;
7165 mClient = c;
7166 mToken = token;
7167 mAttrs.copyFrom(a);
7168 mViewVisibility = viewVisibility;
7169 DeathRecipient deathRecipient = new DeathRecipient();
7170 mAlpha = a.alpha;
Joe Onorato8a9b2202010-02-26 18:56:32 -08007171 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007172 TAG, "Window " + this + " client=" + c.asBinder()
7173 + " token=" + token + " (" + mAttrs.token + ")");
7174 try {
7175 c.asBinder().linkToDeath(deathRecipient, 0);
7176 } catch (RemoteException e) {
7177 mDeathRecipient = null;
7178 mAttachedWindow = null;
7179 mLayoutAttached = false;
7180 mIsImWindow = false;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007181 mIsWallpaper = false;
7182 mIsFloatingLayer = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007183 mBaseLayer = 0;
7184 mSubLayer = 0;
7185 return;
7186 }
7187 mDeathRecipient = deathRecipient;
Romain Guy06882f82009-06-10 13:36:04 -07007188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007189 if ((mAttrs.type >= FIRST_SUB_WINDOW &&
7190 mAttrs.type <= LAST_SUB_WINDOW)) {
7191 // The multiplier here is to reserve space for multiple
7192 // windows in the same type layer.
7193 mBaseLayer = mPolicy.windowTypeToLayerLw(
7194 attachedWindow.mAttrs.type) * TYPE_LAYER_MULTIPLIER
7195 + TYPE_LAYER_OFFSET;
7196 mSubLayer = mPolicy.subWindowTypeToLayerLw(a.type);
7197 mAttachedWindow = attachedWindow;
7198 mAttachedWindow.mChildWindows.add(this);
7199 mLayoutAttached = mAttrs.type !=
7200 WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
7201 mIsImWindow = attachedWindow.mAttrs.type == TYPE_INPUT_METHOD
7202 || attachedWindow.mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007203 mIsWallpaper = attachedWindow.mAttrs.type == TYPE_WALLPAPER;
7204 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007205 } else {
7206 // The multiplier here is to reserve space for multiple
7207 // windows in the same type layer.
7208 mBaseLayer = mPolicy.windowTypeToLayerLw(a.type)
7209 * TYPE_LAYER_MULTIPLIER
7210 + TYPE_LAYER_OFFSET;
7211 mSubLayer = 0;
7212 mAttachedWindow = null;
7213 mLayoutAttached = false;
7214 mIsImWindow = mAttrs.type == TYPE_INPUT_METHOD
7215 || mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007216 mIsWallpaper = mAttrs.type == TYPE_WALLPAPER;
7217 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007218 }
7219
7220 WindowState appWin = this;
7221 while (appWin.mAttachedWindow != null) {
7222 appWin = mAttachedWindow;
7223 }
7224 WindowToken appToken = appWin.mToken;
7225 while (appToken.appWindowToken == null) {
7226 WindowToken parent = mTokenMap.get(appToken.token);
7227 if (parent == null || appToken == parent) {
7228 break;
7229 }
7230 appToken = parent;
7231 }
The Android Open Source Project10592532009-03-18 17:39:46 -07007232 mRootToken = appToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007233 mAppToken = appToken.appWindowToken;
7234
7235 mSurface = null;
7236 mRequestedWidth = 0;
7237 mRequestedHeight = 0;
7238 mLastRequestedWidth = 0;
7239 mLastRequestedHeight = 0;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007240 mXOffset = 0;
7241 mYOffset = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007242 mLayer = 0;
7243 mAnimLayer = 0;
7244 mLastLayer = 0;
7245 }
7246
7247 void attach() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007248 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007249 TAG, "Attaching " + this + " token=" + mToken
7250 + ", list=" + mToken.windows);
7251 mSession.windowAddedLocked();
7252 }
7253
7254 public void computeFrameLw(Rect pf, Rect df, Rect cf, Rect vf) {
7255 mHaveFrame = true;
7256
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007257 final Rect container = mContainingFrame;
7258 container.set(pf);
7259
7260 final Rect display = mDisplayFrame;
7261 display.set(df);
7262
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007263 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007264 container.intersect(mCompatibleScreenFrame);
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007265 if ((mAttrs.flags & FLAG_LAYOUT_NO_LIMITS) == 0) {
7266 display.intersect(mCompatibleScreenFrame);
7267 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007268 }
7269
7270 final int pw = container.right - container.left;
7271 final int ph = container.bottom - container.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007272
7273 int w,h;
7274 if ((mAttrs.flags & mAttrs.FLAG_SCALED) != 0) {
7275 w = mAttrs.width < 0 ? pw : mAttrs.width;
7276 h = mAttrs.height< 0 ? ph : mAttrs.height;
7277 } else {
Romain Guy980a9382010-01-08 15:06:28 -08007278 w = mAttrs.width == mAttrs.MATCH_PARENT ? pw : mRequestedWidth;
7279 h = mAttrs.height== mAttrs.MATCH_PARENT ? ph : mRequestedHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007280 }
Romain Guy06882f82009-06-10 13:36:04 -07007281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007282 final Rect content = mContentFrame;
7283 content.set(cf);
Romain Guy06882f82009-06-10 13:36:04 -07007284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007285 final Rect visible = mVisibleFrame;
7286 visible.set(vf);
Romain Guy06882f82009-06-10 13:36:04 -07007287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007288 final Rect frame = mFrame;
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007289 final int fw = frame.width();
7290 final int fh = frame.height();
Romain Guy06882f82009-06-10 13:36:04 -07007291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007292 //System.out.println("In: w=" + w + " h=" + h + " container=" +
7293 // container + " x=" + mAttrs.x + " y=" + mAttrs.y);
7294
7295 Gravity.apply(mAttrs.gravity, w, h, container,
7296 (int) (mAttrs.x + mAttrs.horizontalMargin * pw),
7297 (int) (mAttrs.y + mAttrs.verticalMargin * ph), frame);
7298
7299 //System.out.println("Out: " + mFrame);
7300
7301 // Now make sure the window fits in the overall display.
7302 Gravity.applyDisplay(mAttrs.gravity, df, frame);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007304 // Make sure the content and visible frames are inside of the
7305 // final window frame.
7306 if (content.left < frame.left) content.left = frame.left;
7307 if (content.top < frame.top) content.top = frame.top;
7308 if (content.right > frame.right) content.right = frame.right;
7309 if (content.bottom > frame.bottom) content.bottom = frame.bottom;
7310 if (visible.left < frame.left) visible.left = frame.left;
7311 if (visible.top < frame.top) visible.top = frame.top;
7312 if (visible.right > frame.right) visible.right = frame.right;
7313 if (visible.bottom > frame.bottom) visible.bottom = frame.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007315 final Rect contentInsets = mContentInsets;
7316 contentInsets.left = content.left-frame.left;
7317 contentInsets.top = content.top-frame.top;
7318 contentInsets.right = frame.right-content.right;
7319 contentInsets.bottom = frame.bottom-content.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007321 final Rect visibleInsets = mVisibleInsets;
7322 visibleInsets.left = visible.left-frame.left;
7323 visibleInsets.top = visible.top-frame.top;
7324 visibleInsets.right = frame.right-visible.right;
7325 visibleInsets.bottom = frame.bottom-visible.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007326
Dianne Hackborn284ac932009-08-28 10:34:25 -07007327 if (mIsWallpaper && (fw != frame.width() || fh != frame.height())) {
7328 updateWallpaperOffsetLocked(this, mDisplay.getWidth(),
Dianne Hackborn19382ac2009-09-11 21:13:37 -07007329 mDisplay.getHeight(), false);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007330 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007332 if (localLOGV) {
7333 //if ("com.google.android.youtube".equals(mAttrs.packageName)
7334 // && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007335 Slog.v(TAG, "Resolving (mRequestedWidth="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007336 + mRequestedWidth + ", mRequestedheight="
7337 + mRequestedHeight + ") to" + " (pw=" + pw + ", ph=" + ph
7338 + "): frame=" + mFrame.toShortString()
7339 + " ci=" + contentInsets.toShortString()
7340 + " vi=" + visibleInsets.toShortString());
7341 //}
7342 }
7343 }
Romain Guy06882f82009-06-10 13:36:04 -07007344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007345 public Rect getFrameLw() {
7346 return mFrame;
7347 }
7348
7349 public Rect getShownFrameLw() {
7350 return mShownFrame;
7351 }
7352
7353 public Rect getDisplayFrameLw() {
7354 return mDisplayFrame;
7355 }
7356
7357 public Rect getContentFrameLw() {
7358 return mContentFrame;
7359 }
7360
7361 public Rect getVisibleFrameLw() {
7362 return mVisibleFrame;
7363 }
7364
7365 public boolean getGivenInsetsPendingLw() {
7366 return mGivenInsetsPending;
7367 }
7368
7369 public Rect getGivenContentInsetsLw() {
7370 return mGivenContentInsets;
7371 }
Romain Guy06882f82009-06-10 13:36:04 -07007372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007373 public Rect getGivenVisibleInsetsLw() {
7374 return mGivenVisibleInsets;
7375 }
Romain Guy06882f82009-06-10 13:36:04 -07007376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007377 public WindowManager.LayoutParams getAttrs() {
7378 return mAttrs;
7379 }
7380
7381 public int getSurfaceLayer() {
7382 return mLayer;
7383 }
Romain Guy06882f82009-06-10 13:36:04 -07007384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007385 public IApplicationToken getAppToken() {
7386 return mAppToken != null ? mAppToken.appToken : null;
7387 }
7388
7389 public boolean hasAppShownWindows() {
7390 return mAppToken != null ? mAppToken.firstWindowDrawn : false;
7391 }
7392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007393 public void setAnimation(Animation anim) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007394 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007395 TAG, "Setting animation in " + this + ": " + anim);
7396 mAnimating = false;
7397 mLocalAnimating = false;
7398 mAnimation = anim;
7399 mAnimation.restrictDuration(MAX_ANIMATION_DURATION);
7400 mAnimation.scaleCurrentDuration(mWindowAnimationScale);
7401 }
7402
7403 public void clearAnimation() {
7404 if (mAnimation != null) {
7405 mAnimating = true;
7406 mLocalAnimating = false;
7407 mAnimation = null;
7408 }
7409 }
Romain Guy06882f82009-06-10 13:36:04 -07007410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007411 Surface createSurfaceLocked() {
7412 if (mSurface == null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007413 mReportDestroySurface = false;
7414 mSurfacePendingDestroy = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007415 mDrawPending = true;
7416 mCommitDrawPending = false;
7417 mReadyToShow = false;
7418 if (mAppToken != null) {
7419 mAppToken.allDrawn = false;
7420 }
7421
7422 int flags = 0;
Mathias Agopian317a6282009-08-13 17:29:02 -07007423 if (mAttrs.memoryType == MEMORY_TYPE_PUSH_BUFFERS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007424 flags |= Surface.PUSH_BUFFERS;
7425 }
7426
7427 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
7428 flags |= Surface.SECURE;
7429 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08007430 if (DEBUG_VISIBILITY) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007431 TAG, "Creating surface in session "
7432 + mSession.mSurfaceSession + " window " + this
7433 + " w=" + mFrame.width()
7434 + " h=" + mFrame.height() + " format="
7435 + mAttrs.format + " flags=" + flags);
7436
7437 int w = mFrame.width();
7438 int h = mFrame.height();
7439 if ((mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
7440 // for a scaled surface, we always want the requested
7441 // size.
7442 w = mRequestedWidth;
7443 h = mRequestedHeight;
7444 }
7445
Romain Guy9825ec62009-10-01 00:58:09 -07007446 // Something is wrong and SurfaceFlinger will not like this,
7447 // try to revert to sane values
7448 if (w <= 0) w = 1;
7449 if (h <= 0) h = 1;
7450
Dianne Hackborn16064f92010-03-25 00:47:24 -07007451 mSurfaceShown = false;
7452 mSurfaceLayer = 0;
7453 mSurfaceAlpha = 1;
7454 mSurfaceX = 0;
7455 mSurfaceY = 0;
7456 mSurfaceW = w;
7457 mSurfaceH = h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007458 try {
7459 mSurface = new Surface(
Romain Guy06882f82009-06-10 13:36:04 -07007460 mSession.mSurfaceSession, mSession.mPid,
Mathias Agopian5d26c1e2010-03-01 16:09:43 -08007461 mAttrs.getTitle().toString(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007462 0, w, h, mAttrs.format, flags);
Joe Onorato8a9b2202010-02-26 18:56:32 -08007463 if (SHOW_TRANSACTIONS) Slog.i(TAG, " CREATE SURFACE "
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007464 + mSurface + " IN SESSION "
7465 + mSession.mSurfaceSession
7466 + ": pid=" + mSession.mPid + " format="
7467 + mAttrs.format + " flags=0x"
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007468 + Integer.toHexString(flags)
7469 + " / " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007470 } catch (Surface.OutOfResourcesException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007471 Slog.w(TAG, "OutOfResourcesException creating surface");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007472 reclaimSomeSurfaceMemoryLocked(this, "create");
7473 return null;
7474 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007475 Slog.e(TAG, "Exception creating surface", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007476 return null;
7477 }
Romain Guy06882f82009-06-10 13:36:04 -07007478
Joe Onorato8a9b2202010-02-26 18:56:32 -08007479 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007480 TAG, "Got surface: " + mSurface
7481 + ", set left=" + mFrame.left + " top=" + mFrame.top
7482 + ", animLayer=" + mAnimLayer);
7483 if (SHOW_TRANSACTIONS) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007484 Slog.i(TAG, ">>> OPEN TRANSACTION");
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007485 if (SHOW_TRANSACTIONS) logSurface(this,
7486 "CREATE pos=(" + mFrame.left + "," + mFrame.top + ") (" +
7487 mFrame.width() + "x" + mFrame.height() + "), layer=" +
7488 mAnimLayer + " HIDE", null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007489 }
7490 Surface.openTransaction();
7491 try {
7492 try {
Dianne Hackborn16064f92010-03-25 00:47:24 -07007493 mSurfaceX = mFrame.left + mXOffset;
Dianne Hackborn529bef62010-03-25 11:48:43 -07007494 mSurfaceY = mFrame.top + mYOffset;
Dianne Hackborn16064f92010-03-25 00:47:24 -07007495 mSurface.setPosition(mSurfaceX, mSurfaceY);
7496 mSurfaceLayer = mAnimLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007497 mSurface.setLayer(mAnimLayer);
Dianne Hackborn16064f92010-03-25 00:47:24 -07007498 mSurfaceShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007499 mSurface.hide();
7500 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007501 if (SHOW_TRANSACTIONS) logSurface(this, "DITHER", null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007502 mSurface.setFlags(Surface.SURFACE_DITHER,
7503 Surface.SURFACE_DITHER);
7504 }
7505 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007506 Slog.w(TAG, "Error creating surface in " + w, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007507 reclaimSomeSurfaceMemoryLocked(this, "create-init");
7508 }
7509 mLastHidden = true;
7510 } finally {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007511 if (SHOW_TRANSACTIONS) Slog.i(TAG, "<<< CLOSE TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007512 Surface.closeTransaction();
7513 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08007514 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007515 TAG, "Created surface " + this);
7516 }
7517 return mSurface;
7518 }
Romain Guy06882f82009-06-10 13:36:04 -07007519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007520 void destroySurfaceLocked() {
7521 // Window is no longer on-screen, so can no longer receive
7522 // key events... if we were waiting for it to finish
7523 // handling a key event, the wait is over!
7524 mKeyWaiter.finishedKey(mSession, mClient, true,
7525 KeyWaiter.RETURN_NOTHING);
7526 mKeyWaiter.releasePendingPointerLocked(mSession);
7527 mKeyWaiter.releasePendingTrackballLocked(mSession);
7528
7529 if (mAppToken != null && this == mAppToken.startingWindow) {
7530 mAppToken.startingDisplayed = false;
7531 }
Romain Guy06882f82009-06-10 13:36:04 -07007532
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007533 if (mSurface != null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007534 mDrawPending = false;
7535 mCommitDrawPending = false;
7536 mReadyToShow = false;
7537
7538 int i = mChildWindows.size();
7539 while (i > 0) {
7540 i--;
7541 WindowState c = (WindowState)mChildWindows.get(i);
7542 c.mAttachedHidden = true;
7543 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007544
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007545 if (mReportDestroySurface) {
7546 mReportDestroySurface = false;
7547 mSurfacePendingDestroy = true;
7548 try {
7549 mClient.dispatchGetNewSurface();
7550 // We'll really destroy on the next time around.
7551 return;
7552 } catch (RemoteException e) {
7553 }
7554 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007555
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007556 try {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007557 if (DEBUG_VISIBILITY) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007558 RuntimeException e = null;
7559 if (!HIDE_STACK_CRAWLS) {
7560 e = new RuntimeException();
7561 e.fillInStackTrace();
7562 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08007563 Slog.w(TAG, "Window " + this + " destroying surface "
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007564 + mSurface + ", session " + mSession, e);
7565 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007566 if (SHOW_TRANSACTIONS) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007567 RuntimeException e = null;
7568 if (!HIDE_STACK_CRAWLS) {
7569 e = new RuntimeException();
7570 e.fillInStackTrace();
7571 }
7572 if (SHOW_TRANSACTIONS) logSurface(this, "DESTROY", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007573 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007574 mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007575 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007576 Slog.w(TAG, "Exception thrown when destroying Window " + this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007577 + " surface " + mSurface + " session " + mSession
7578 + ": " + e.toString());
7579 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007580
Dianne Hackborn16064f92010-03-25 00:47:24 -07007581 mSurfaceShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007582 mSurface = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007583 }
7584 }
7585
7586 boolean finishDrawingLocked() {
7587 if (mDrawPending) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007588 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007589 TAG, "finishDrawingLocked: " + mSurface);
7590 mCommitDrawPending = true;
7591 mDrawPending = false;
7592 return true;
7593 }
7594 return false;
7595 }
7596
7597 // This must be called while inside a transaction.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007598 boolean commitFinishDrawingLocked(long currentTime) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007599 //Slog.i(TAG, "commitFinishDrawingLocked: " + mSurface);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007600 if (!mCommitDrawPending) {
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007601 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007602 }
7603 mCommitDrawPending = false;
7604 mReadyToShow = true;
7605 final boolean starting = mAttrs.type == TYPE_APPLICATION_STARTING;
7606 final AppWindowToken atoken = mAppToken;
7607 if (atoken == null || atoken.allDrawn || starting) {
7608 performShowLocked();
7609 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007610 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007611 }
7612
7613 // This must be called while inside a transaction.
7614 boolean performShowLocked() {
7615 if (DEBUG_VISIBILITY) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007616 RuntimeException e = null;
7617 if (!HIDE_STACK_CRAWLS) {
7618 e = new RuntimeException();
7619 e.fillInStackTrace();
7620 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08007621 Slog.v(TAG, "performShow on " + this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007622 + ": readyToShow=" + mReadyToShow + " readyForDisplay=" + isReadyForDisplay()
7623 + " starting=" + (mAttrs.type == TYPE_APPLICATION_STARTING), e);
7624 }
7625 if (mReadyToShow && isReadyForDisplay()) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007626 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) logSurface(this,
7627 "SHOW (performShowLocked)", null);
Joe Onorato8a9b2202010-02-26 18:56:32 -08007628 if (DEBUG_VISIBILITY) Slog.v(TAG, "Showing " + this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007629 + " during animation: policyVis=" + mPolicyVisibility
7630 + " attHidden=" + mAttachedHidden
7631 + " tok.hiddenRequested="
7632 + (mAppToken != null ? mAppToken.hiddenRequested : false)
Dianne Hackborn248b1882009-09-16 16:46:44 -07007633 + " tok.hidden="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007634 + (mAppToken != null ? mAppToken.hidden : false)
7635 + " animating=" + mAnimating
7636 + " tok animating="
7637 + (mAppToken != null ? mAppToken.animating : false));
7638 if (!showSurfaceRobustlyLocked(this)) {
7639 return false;
7640 }
7641 mLastAlpha = -1;
7642 mHasDrawn = true;
7643 mLastHidden = false;
7644 mReadyToShow = false;
7645 enableScreenIfNeededLocked();
7646
7647 applyEnterAnimationLocked(this);
Romain Guy06882f82009-06-10 13:36:04 -07007648
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007649 int i = mChildWindows.size();
7650 while (i > 0) {
7651 i--;
7652 WindowState c = (WindowState)mChildWindows.get(i);
Dianne Hackbornf09c1a22010-04-22 15:59:21 -07007653 if (c.mAttachedHidden) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007654 c.mAttachedHidden = false;
Dianne Hackbornf09c1a22010-04-22 15:59:21 -07007655 if (c.mSurface != null) {
7656 c.performShowLocked();
7657 // It hadn't been shown, which means layout not
7658 // performed on it, so now we want to make sure to
7659 // do a layout. If called from within the transaction
7660 // loop, this will cause it to restart with a new
7661 // layout.
7662 mLayoutNeeded = true;
7663 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007664 }
7665 }
Romain Guy06882f82009-06-10 13:36:04 -07007666
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007667 if (mAttrs.type != TYPE_APPLICATION_STARTING
7668 && mAppToken != null) {
7669 mAppToken.firstWindowDrawn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007670
Dianne Hackborn248b1882009-09-16 16:46:44 -07007671 if (mAppToken.startingData != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007672 if (DEBUG_STARTING_WINDOW || DEBUG_ANIM) Slog.v(TAG,
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007673 "Finish starting " + mToken
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007674 + ": first real window is shown, no animation");
Dianne Hackborn248b1882009-09-16 16:46:44 -07007675 // If this initial window is animating, stop it -- we
7676 // will do an animation to reveal it from behind the
7677 // starting window, so there is no need for it to also
7678 // be doing its own stuff.
7679 if (mAnimation != null) {
7680 mAnimation = null;
7681 // Make sure we clean up the animation.
7682 mAnimating = true;
7683 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007684 mFinishedStarting.add(mAppToken);
7685 mH.sendEmptyMessage(H.FINISHED_STARTING);
7686 }
7687 mAppToken.updateReportedVisibilityLocked();
7688 }
7689 }
7690 return true;
7691 }
Romain Guy06882f82009-06-10 13:36:04 -07007692
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007693 // This must be called while inside a transaction. Returns true if
7694 // there is more animation to run.
7695 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
Dianne Hackbornde2606d2009-12-18 16:53:55 -08007696 if (!mDisplayFrozen && mPolicy.isScreenOn()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007697 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07007698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007699 if (!mDrawPending && !mCommitDrawPending && mAnimation != null) {
7700 mHasTransformation = true;
7701 mHasLocalTransformation = true;
7702 if (!mLocalAnimating) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007703 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007704 TAG, "Starting animation in " + this +
7705 " @ " + currentTime + ": ww=" + mFrame.width() + " wh=" + mFrame.height() +
7706 " dw=" + dw + " dh=" + dh + " scale=" + mWindowAnimationScale);
7707 mAnimation.initialize(mFrame.width(), mFrame.height(), dw, dh);
7708 mAnimation.setStartTime(currentTime);
7709 mLocalAnimating = true;
7710 mAnimating = true;
7711 }
7712 mTransformation.clear();
7713 final boolean more = mAnimation.getTransformation(
7714 currentTime, mTransformation);
Joe Onorato8a9b2202010-02-26 18:56:32 -08007715 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007716 TAG, "Stepped animation in " + this +
7717 ": more=" + more + ", xform=" + mTransformation);
7718 if (more) {
7719 // we're not done!
7720 return true;
7721 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08007722 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007723 TAG, "Finished animation in " + this +
7724 " @ " + currentTime);
7725 mAnimation = null;
7726 //WindowManagerService.this.dump();
7727 }
7728 mHasLocalTransformation = false;
7729 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppToken != null
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007730 && mAppToken.animation != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007731 // When our app token is animating, we kind-of pretend like
7732 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
7733 // part of this check means that we will only do this if
7734 // our window is not currently exiting, or it is not
7735 // locally animating itself. The idea being that one that
7736 // is exiting and doing a local animation should be removed
7737 // once that animation is done.
7738 mAnimating = true;
7739 mHasTransformation = true;
7740 mTransformation.clear();
7741 return false;
7742 } else if (mHasTransformation) {
7743 // Little trick to get through the path below to act like
7744 // we have finished an animation.
7745 mAnimating = true;
7746 } else if (isAnimating()) {
7747 mAnimating = true;
7748 }
7749 } else if (mAnimation != null) {
7750 // If the display is frozen, and there is a pending animation,
7751 // clear it and make sure we run the cleanup code.
7752 mAnimating = true;
7753 mLocalAnimating = true;
7754 mAnimation = null;
7755 }
Romain Guy06882f82009-06-10 13:36:04 -07007756
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007757 if (!mAnimating && !mLocalAnimating) {
7758 return false;
7759 }
7760
Joe Onorato8a9b2202010-02-26 18:56:32 -08007761 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007762 TAG, "Animation done in " + this + ": exiting=" + mExiting
7763 + ", reportedVisible="
7764 + (mAppToken != null ? mAppToken.reportedVisible : false));
Romain Guy06882f82009-06-10 13:36:04 -07007765
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007766 mAnimating = false;
7767 mLocalAnimating = false;
7768 mAnimation = null;
7769 mAnimLayer = mLayer;
7770 if (mIsImWindow) {
7771 mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007772 } else if (mIsWallpaper) {
7773 mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007774 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08007775 if (DEBUG_LAYERS) Slog.v(TAG, "Stepping win " + this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007776 + " anim layer: " + mAnimLayer);
7777 mHasTransformation = false;
7778 mHasLocalTransformation = false;
Dianne Hackbornb601ce12010-03-01 23:36:02 -08007779 if (mPolicyVisibility != mPolicyVisibilityAfterAnim) {
7780 if (DEBUG_VISIBILITY) {
7781 Slog.v(TAG, "Policy visibility changing after anim in " + this + ": "
7782 + mPolicyVisibilityAfterAnim);
7783 }
7784 mPolicyVisibility = mPolicyVisibilityAfterAnim;
7785 if (!mPolicyVisibility) {
7786 if (mCurrentFocus == this) {
7787 mFocusMayChange = true;
7788 }
7789 // Window is no longer visible -- make sure if we were waiting
7790 // for it to be displayed before enabling the display, that
7791 // we allow the display to be enabled now.
7792 enableScreenIfNeededLocked();
7793 }
Dianne Hackbornf3bea9c2009-12-09 18:26:21 -08007794 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007795 mTransformation.clear();
7796 if (mHasDrawn
7797 && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
7798 && mAppToken != null
7799 && mAppToken.firstWindowDrawn
7800 && mAppToken.startingData != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007801 if (DEBUG_STARTING_WINDOW) Slog.v(TAG, "Finish starting "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007802 + mToken + ": first real window done animating");
7803 mFinishedStarting.add(mAppToken);
7804 mH.sendEmptyMessage(H.FINISHED_STARTING);
7805 }
Romain Guy06882f82009-06-10 13:36:04 -07007806
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007807 finishExit();
7808
7809 if (mAppToken != null) {
7810 mAppToken.updateReportedVisibilityLocked();
7811 }
7812
7813 return false;
7814 }
7815
7816 void finishExit() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007817 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007818 TAG, "finishExit in " + this
7819 + ": exiting=" + mExiting
7820 + " remove=" + mRemoveOnExit
7821 + " windowAnimating=" + isWindowAnimating());
Romain Guy06882f82009-06-10 13:36:04 -07007822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007823 final int N = mChildWindows.size();
7824 for (int i=0; i<N; i++) {
7825 ((WindowState)mChildWindows.get(i)).finishExit();
7826 }
Romain Guy06882f82009-06-10 13:36:04 -07007827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007828 if (!mExiting) {
7829 return;
7830 }
Romain Guy06882f82009-06-10 13:36:04 -07007831
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007832 if (isWindowAnimating()) {
7833 return;
7834 }
7835
Joe Onorato8a9b2202010-02-26 18:56:32 -08007836 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007837 TAG, "Exit animation finished in " + this
7838 + ": remove=" + mRemoveOnExit);
7839 if (mSurface != null) {
7840 mDestroySurface.add(this);
7841 mDestroying = true;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007842 if (SHOW_TRANSACTIONS) logSurface(this, "HIDE (finishExit)", null);
Dianne Hackborn16064f92010-03-25 00:47:24 -07007843 mSurfaceShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007844 try {
7845 mSurface.hide();
7846 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007847 Slog.w(TAG, "Error hiding surface in " + this, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007848 }
7849 mLastHidden = true;
7850 mKeyWaiter.releasePendingPointerLocked(mSession);
7851 }
7852 mExiting = false;
7853 if (mRemoveOnExit) {
7854 mPendingRemove.add(this);
7855 mRemoveOnExit = false;
7856 }
7857 }
Romain Guy06882f82009-06-10 13:36:04 -07007858
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007859 boolean isIdentityMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
7860 if (dsdx < .99999f || dsdx > 1.00001f) return false;
7861 if (dtdy < .99999f || dtdy > 1.00001f) return false;
7862 if (dtdx < -.000001f || dtdx > .000001f) return false;
7863 if (dsdy < -.000001f || dsdy > .000001f) return false;
7864 return true;
7865 }
Romain Guy06882f82009-06-10 13:36:04 -07007866
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007867 void computeShownFrameLocked() {
7868 final boolean selfTransformation = mHasLocalTransformation;
7869 Transformation attachedTransformation =
7870 (mAttachedWindow != null && mAttachedWindow.mHasLocalTransformation)
7871 ? mAttachedWindow.mTransformation : null;
7872 Transformation appTransformation =
7873 (mAppToken != null && mAppToken.hasTransformation)
7874 ? mAppToken.transformation : null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007875
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007876 // Wallpapers are animated based on the "real" window they
7877 // are currently targeting.
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007878 if (mAttrs.type == TYPE_WALLPAPER && mLowerWallpaperTarget == null
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07007879 && mWallpaperTarget != null) {
Dianne Hackborn5baba162009-09-23 17:01:12 -07007880 if (mWallpaperTarget.mHasLocalTransformation &&
7881 mWallpaperTarget.mAnimation != null &&
7882 !mWallpaperTarget.mAnimation.getDetachWallpaper()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007883 attachedTransformation = mWallpaperTarget.mTransformation;
Dianne Hackborn5baba162009-09-23 17:01:12 -07007884 if (DEBUG_WALLPAPER && attachedTransformation != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007885 Slog.v(TAG, "WP target attached xform: " + attachedTransformation);
Dianne Hackborn5baba162009-09-23 17:01:12 -07007886 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007887 }
7888 if (mWallpaperTarget.mAppToken != null &&
Dianne Hackborn5baba162009-09-23 17:01:12 -07007889 mWallpaperTarget.mAppToken.hasTransformation &&
7890 mWallpaperTarget.mAppToken.animation != null &&
7891 !mWallpaperTarget.mAppToken.animation.getDetachWallpaper()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007892 appTransformation = mWallpaperTarget.mAppToken.transformation;
Dianne Hackborn5baba162009-09-23 17:01:12 -07007893 if (DEBUG_WALLPAPER && appTransformation != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007894 Slog.v(TAG, "WP target app xform: " + appTransformation);
Dianne Hackborn5baba162009-09-23 17:01:12 -07007895 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007896 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007897 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007899 if (selfTransformation || attachedTransformation != null
7900 || appTransformation != null) {
Romain Guy06882f82009-06-10 13:36:04 -07007901 // cache often used attributes locally
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007902 final Rect frame = mFrame;
7903 final float tmpFloats[] = mTmpFloats;
7904 final Matrix tmpMatrix = mTmpMatrix;
7905
7906 // Compute the desired transformation.
Dianne Hackborn65c23872009-09-18 17:47:02 -07007907 tmpMatrix.setTranslate(0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007908 if (selfTransformation) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007909 tmpMatrix.postConcat(mTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007910 }
Dianne Hackborn65c23872009-09-18 17:47:02 -07007911 tmpMatrix.postTranslate(frame.left, frame.top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007912 if (attachedTransformation != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007913 tmpMatrix.postConcat(attachedTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007914 }
7915 if (appTransformation != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007916 tmpMatrix.postConcat(appTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007917 }
7918
7919 // "convert" it into SurfaceFlinger's format
7920 // (a 2x2 matrix + an offset)
7921 // Here we must not transform the position of the surface
7922 // since it is already included in the transformation.
Joe Onorato8a9b2202010-02-26 18:56:32 -08007923 //Slog.i(TAG, "Transform: " + matrix);
Romain Guy06882f82009-06-10 13:36:04 -07007924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007925 tmpMatrix.getValues(tmpFloats);
7926 mDsDx = tmpFloats[Matrix.MSCALE_X];
7927 mDtDx = tmpFloats[Matrix.MSKEW_X];
7928 mDsDy = tmpFloats[Matrix.MSKEW_Y];
7929 mDtDy = tmpFloats[Matrix.MSCALE_Y];
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007930 int x = (int)tmpFloats[Matrix.MTRANS_X] + mXOffset;
7931 int y = (int)tmpFloats[Matrix.MTRANS_Y] + mYOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007932 int w = frame.width();
7933 int h = frame.height();
7934 mShownFrame.set(x, y, x+w, y+h);
7935
7936 // Now set the alpha... but because our current hardware
7937 // can't do alpha transformation on a non-opaque surface,
7938 // turn it off if we are running an animation that is also
7939 // transforming since it is more important to have that
7940 // animation be smooth.
7941 mShownAlpha = mAlpha;
7942 if (!mLimitedAlphaCompositing
7943 || (!PixelFormat.formatHasAlpha(mAttrs.format)
7944 || (isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
7945 && x == frame.left && y == frame.top))) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007946 //Slog.i(TAG, "Applying alpha transform");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007947 if (selfTransformation) {
7948 mShownAlpha *= mTransformation.getAlpha();
7949 }
7950 if (attachedTransformation != null) {
7951 mShownAlpha *= attachedTransformation.getAlpha();
7952 }
7953 if (appTransformation != null) {
7954 mShownAlpha *= appTransformation.getAlpha();
7955 }
7956 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007957 //Slog.i(TAG, "Not applying alpha transform");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007958 }
Romain Guy06882f82009-06-10 13:36:04 -07007959
Joe Onorato8a9b2202010-02-26 18:56:32 -08007960 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007961 TAG, "Continuing animation in " + this +
7962 ": " + mShownFrame +
7963 ", alpha=" + mTransformation.getAlpha());
7964 return;
7965 }
Romain Guy06882f82009-06-10 13:36:04 -07007966
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007967 mShownFrame.set(mFrame);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007968 if (mXOffset != 0 || mYOffset != 0) {
7969 mShownFrame.offset(mXOffset, mYOffset);
7970 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007971 mShownAlpha = mAlpha;
7972 mDsDx = 1;
7973 mDtDx = 0;
7974 mDsDy = 0;
7975 mDtDy = 1;
7976 }
Romain Guy06882f82009-06-10 13:36:04 -07007977
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007978 /**
7979 * Is this window visible? It is not visible if there is no
7980 * surface, or we are in the process of running an exit animation
7981 * that will remove the surface, or its app token has been hidden.
7982 */
7983 public boolean isVisibleLw() {
7984 final AppWindowToken atoken = mAppToken;
7985 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7986 && (atoken == null || !atoken.hiddenRequested)
7987 && !mExiting && !mDestroying;
7988 }
7989
7990 /**
Dianne Hackborn3d163f072009-10-07 21:26:57 -07007991 * Like {@link #isVisibleLw}, but also counts a window that is currently
7992 * "hidden" behind the keyguard as visible. This allows us to apply
7993 * things like window flags that impact the keyguard.
7994 * XXX I am starting to think we need to have ANOTHER visibility flag
7995 * for this "hidden behind keyguard" state rather than overloading
7996 * mPolicyVisibility. Ungh.
7997 */
7998 public boolean isVisibleOrBehindKeyguardLw() {
7999 final AppWindowToken atoken = mAppToken;
8000 return mSurface != null && !mAttachedHidden
8001 && (atoken == null ? mPolicyVisibility : !atoken.hiddenRequested)
Dianne Hackborn5943c202010-04-12 21:36:49 -07008002 && (mOrientationChanging || (!mDrawPending && !mCommitDrawPending))
Dianne Hackborn3d163f072009-10-07 21:26:57 -07008003 && !mExiting && !mDestroying;
8004 }
8005
8006 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008007 * Is this window visible, ignoring its app token? It is not visible
8008 * if there is no surface, or we are in the process of running an exit animation
8009 * that will remove the surface.
8010 */
8011 public boolean isWinVisibleLw() {
8012 final AppWindowToken atoken = mAppToken;
8013 return mSurface != null && mPolicyVisibility && !mAttachedHidden
8014 && (atoken == null || !atoken.hiddenRequested || atoken.animating)
8015 && !mExiting && !mDestroying;
8016 }
8017
8018 /**
8019 * The same as isVisible(), but follows the current hidden state of
8020 * the associated app token, not the pending requested hidden state.
8021 */
8022 boolean isVisibleNow() {
8023 return mSurface != null && mPolicyVisibility && !mAttachedHidden
The Android Open Source Project10592532009-03-18 17:39:46 -07008024 && !mRootToken.hidden && !mExiting && !mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008025 }
8026
8027 /**
8028 * Same as isVisible(), but we also count it as visible between the
8029 * call to IWindowSession.add() and the first relayout().
8030 */
8031 boolean isVisibleOrAdding() {
8032 final AppWindowToken atoken = mAppToken;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008033 return ((mSurface != null && !mReportDestroySurface)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008034 || (!mRelayoutCalled && mViewVisibility == View.VISIBLE))
8035 && mPolicyVisibility && !mAttachedHidden
8036 && (atoken == null || !atoken.hiddenRequested)
8037 && !mExiting && !mDestroying;
8038 }
8039
8040 /**
8041 * Is this window currently on-screen? It is on-screen either if it
8042 * is visible or it is currently running an animation before no longer
8043 * being visible.
8044 */
8045 boolean isOnScreen() {
8046 final AppWindowToken atoken = mAppToken;
8047 if (atoken != null) {
8048 return mSurface != null && mPolicyVisibility && !mDestroying
8049 && ((!mAttachedHidden && !atoken.hiddenRequested)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07008050 || mAnimation != null || atoken.animation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008051 } else {
8052 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn0cd48872009-08-13 18:51:59 -07008053 && (!mAttachedHidden || mAnimation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008054 }
8055 }
Romain Guy06882f82009-06-10 13:36:04 -07008056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008057 /**
8058 * Like isOnScreen(), but we don't return true if the window is part
8059 * of a transition that has not yet been started.
8060 */
8061 boolean isReadyForDisplay() {
Dianne Hackborna8f60182009-09-01 19:01:50 -07008062 if (mRootToken.waitingToShow &&
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07008063 mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07008064 return false;
8065 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008066 final AppWindowToken atoken = mAppToken;
Dianne Hackborn0cd48872009-08-13 18:51:59 -07008067 final boolean animating = atoken != null
8068 ? (atoken.animation != null) : false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008069 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07008070 && ((!mAttachedHidden && mViewVisibility == View.VISIBLE
8071 && !mRootToken.hidden)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07008072 || mAnimation != null || animating);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008073 }
8074
8075 /** Is the window or its container currently animating? */
8076 boolean isAnimating() {
8077 final WindowState attached = mAttachedWindow;
8078 final AppWindowToken atoken = mAppToken;
8079 return mAnimation != null
8080 || (attached != null && attached.mAnimation != null)
Romain Guy06882f82009-06-10 13:36:04 -07008081 || (atoken != null &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008082 (atoken.animation != null
8083 || atoken.inPendingTransaction));
8084 }
8085
8086 /** Is this window currently animating? */
8087 boolean isWindowAnimating() {
8088 return mAnimation != null;
8089 }
8090
8091 /**
8092 * Like isOnScreen, but returns false if the surface hasn't yet
8093 * been drawn.
8094 */
8095 public boolean isDisplayedLw() {
8096 final AppWindowToken atoken = mAppToken;
8097 return mSurface != null && mPolicyVisibility && !mDestroying
8098 && !mDrawPending && !mCommitDrawPending
8099 && ((!mAttachedHidden &&
8100 (atoken == null || !atoken.hiddenRequested))
8101 || mAnimating);
8102 }
8103
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008104 /**
8105 * Returns true if the window has a surface that it has drawn a
Dianne Hackborn5943c202010-04-12 21:36:49 -07008106 * complete UI in to. Note that this returns true if the orientation
8107 * is changing even if the window hasn't redrawn because we don't want
8108 * to stop things from executing during that time.
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008109 */
8110 public boolean isDrawnLw() {
8111 final AppWindowToken atoken = mAppToken;
8112 return mSurface != null && !mDestroying
Dianne Hackborn5943c202010-04-12 21:36:49 -07008113 && (mOrientationChanging || (!mDrawPending && !mCommitDrawPending));
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008114 }
8115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008116 public boolean fillsScreenLw(int screenWidth, int screenHeight,
8117 boolean shownFrame, boolean onlyOpaque) {
8118 if (mSurface == null) {
8119 return false;
8120 }
8121 if (mAppToken != null && !mAppToken.appFullscreen) {
8122 return false;
8123 }
8124 if (onlyOpaque && mAttrs.format != PixelFormat.OPAQUE) {
8125 return false;
8126 }
8127 final Rect frame = shownFrame ? mShownFrame : mFrame;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07008128
8129 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
8130 return frame.left <= mCompatibleScreenFrame.left &&
8131 frame.top <= mCompatibleScreenFrame.top &&
8132 frame.right >= mCompatibleScreenFrame.right &&
8133 frame.bottom >= mCompatibleScreenFrame.bottom;
8134 } else {
8135 return frame.left <= 0 && frame.top <= 0
8136 && frame.right >= screenWidth
8137 && frame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008138 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008139 }
Romain Guy06882f82009-06-10 13:36:04 -07008140
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008141 /**
Dianne Hackborn25994b42009-09-04 14:21:19 -07008142 * Return true if the window is opaque and fully drawn. This indicates
8143 * it may obscure windows behind it.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008144 */
8145 boolean isOpaqueDrawn() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07008146 return (mAttrs.format == PixelFormat.OPAQUE
8147 || mAttrs.type == TYPE_WALLPAPER)
8148 && mSurface != null && mAnimation == null
8149 && (mAppToken == null || mAppToken.animation == null)
8150 && !mDrawPending && !mCommitDrawPending;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008151 }
8152
8153 boolean needsBackgroundFiller(int screenWidth, int screenHeight) {
8154 return
8155 // only if the application is requesting compatible window
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008156 (mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0 &&
8157 // only if it's visible
8158 mHasDrawn && mViewVisibility == View.VISIBLE &&
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07008159 // and only if the application fills the compatible screen
8160 mFrame.left <= mCompatibleScreenFrame.left &&
8161 mFrame.top <= mCompatibleScreenFrame.top &&
8162 mFrame.right >= mCompatibleScreenFrame.right &&
8163 mFrame.bottom >= mCompatibleScreenFrame.bottom &&
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008164 // and starting window do not need background filler
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07008165 mAttrs.type != mAttrs.TYPE_APPLICATION_STARTING;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008166 }
8167
8168 boolean isFullscreen(int screenWidth, int screenHeight) {
8169 return mFrame.left <= 0 && mFrame.top <= 0 &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07008170 mFrame.right >= screenWidth && mFrame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008171 }
8172
8173 void removeLocked() {
8174 if (mAttachedWindow != null) {
8175 mAttachedWindow.mChildWindows.remove(this);
8176 }
8177 destroySurfaceLocked();
8178 mSession.windowRemovedLocked();
8179 try {
8180 mClient.asBinder().unlinkToDeath(mDeathRecipient, 0);
8181 } catch (RuntimeException e) {
8182 // Ignore if it has already been removed (usually because
8183 // we are doing this as part of processing a death note.)
8184 }
8185 }
8186
8187 private class DeathRecipient implements IBinder.DeathRecipient {
8188 public void binderDied() {
8189 try {
8190 synchronized(mWindowMap) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08008191 WindowState win = windowForClientLocked(mSession, mClient, false);
Joe Onorato8a9b2202010-02-26 18:56:32 -08008192 Slog.i(TAG, "WIN DEATH: " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008193 if (win != null) {
8194 removeWindowLocked(mSession, win);
8195 }
8196 }
8197 } catch (IllegalArgumentException ex) {
8198 // This will happen if the window has already been
8199 // removed.
8200 }
8201 }
8202 }
8203
8204 /** Returns true if this window desires key events. */
8205 public final boolean canReceiveKeys() {
8206 return isVisibleOrAdding()
8207 && (mViewVisibility == View.VISIBLE)
8208 && ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0);
8209 }
8210
8211 public boolean hasDrawnLw() {
8212 return mHasDrawn;
8213 }
8214
8215 public boolean showLw(boolean doAnimation) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008216 return showLw(doAnimation, true);
8217 }
8218
8219 boolean showLw(boolean doAnimation, boolean requestAnim) {
8220 if (mPolicyVisibility && mPolicyVisibilityAfterAnim) {
8221 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008222 }
Dianne Hackbornb601ce12010-03-01 23:36:02 -08008223 if (DEBUG_VISIBILITY) Slog.v(TAG, "Policy visibility true: " + this);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08008224 if (doAnimation) {
8225 if (DEBUG_VISIBILITY) Slog.v(TAG, "doAnimation: mPolicyVisibility="
8226 + mPolicyVisibility + " mAnimation=" + mAnimation);
8227 if (mDisplayFrozen || !mPolicy.isScreenOn()) {
8228 doAnimation = false;
8229 } else if (mPolicyVisibility && mAnimation == null) {
8230 // Check for the case where we are currently visible and
8231 // not animating; we do not want to do animation at such a
8232 // point to become visible when we already are.
8233 doAnimation = false;
8234 }
8235 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008236 mPolicyVisibility = true;
8237 mPolicyVisibilityAfterAnim = true;
8238 if (doAnimation) {
8239 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_ENTER, true);
8240 }
8241 if (requestAnim) {
8242 requestAnimationLocked(0);
8243 }
8244 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008245 }
8246
8247 public boolean hideLw(boolean doAnimation) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008248 return hideLw(doAnimation, true);
8249 }
8250
8251 boolean hideLw(boolean doAnimation, boolean requestAnim) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08008252 if (doAnimation) {
8253 if (mDisplayFrozen || !mPolicy.isScreenOn()) {
8254 doAnimation = false;
8255 }
8256 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008257 boolean current = doAnimation ? mPolicyVisibilityAfterAnim
8258 : mPolicyVisibility;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008259 if (!current) {
8260 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008261 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008262 if (doAnimation) {
8263 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_EXIT, false);
8264 if (mAnimation == null) {
8265 doAnimation = false;
8266 }
8267 }
8268 if (doAnimation) {
8269 mPolicyVisibilityAfterAnim = false;
8270 } else {
Dianne Hackbornb601ce12010-03-01 23:36:02 -08008271 if (DEBUG_VISIBILITY) Slog.v(TAG, "Policy visibility false: " + this);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008272 mPolicyVisibilityAfterAnim = false;
8273 mPolicyVisibility = false;
Dianne Hackbornf3bea9c2009-12-09 18:26:21 -08008274 // Window is no longer visible -- make sure if we were waiting
8275 // for it to be displayed before enabling the display, that
8276 // we allow the display to be enabled now.
8277 enableScreenIfNeededLocked();
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08008278 if (mCurrentFocus == this) {
8279 mFocusMayChange = true;
8280 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008281 }
8282 if (requestAnim) {
8283 requestAnimationLocked(0);
8284 }
8285 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008286 }
8287
8288 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008289 pw.print(prefix); pw.print("mSession="); pw.print(mSession);
8290 pw.print(" mClient="); pw.println(mClient.asBinder());
8291 pw.print(prefix); pw.print("mAttrs="); pw.println(mAttrs);
8292 if (mAttachedWindow != null || mLayoutAttached) {
8293 pw.print(prefix); pw.print("mAttachedWindow="); pw.print(mAttachedWindow);
8294 pw.print(" mLayoutAttached="); pw.println(mLayoutAttached);
8295 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07008296 if (mIsImWindow || mIsWallpaper || mIsFloatingLayer) {
8297 pw.print(prefix); pw.print("mIsImWindow="); pw.print(mIsImWindow);
8298 pw.print(" mIsWallpaper="); pw.print(mIsWallpaper);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008299 pw.print(" mIsFloatingLayer="); pw.print(mIsFloatingLayer);
8300 pw.print(" mWallpaperVisible="); pw.println(mWallpaperVisible);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008301 }
8302 pw.print(prefix); pw.print("mBaseLayer="); pw.print(mBaseLayer);
8303 pw.print(" mSubLayer="); pw.print(mSubLayer);
8304 pw.print(" mAnimLayer="); pw.print(mLayer); pw.print("+");
8305 pw.print((mTargetAppToken != null ? mTargetAppToken.animLayerAdjustment
8306 : (mAppToken != null ? mAppToken.animLayerAdjustment : 0)));
8307 pw.print("="); pw.print(mAnimLayer);
8308 pw.print(" mLastLayer="); pw.println(mLastLayer);
8309 if (mSurface != null) {
8310 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
Dianne Hackborn16064f92010-03-25 00:47:24 -07008311 pw.print(prefix); pw.print("Surface: shown="); pw.print(mSurfaceShown);
8312 pw.print(" layer="); pw.print(mSurfaceLayer);
8313 pw.print(" alpha="); pw.print(mSurfaceAlpha);
8314 pw.print(" rect=("); pw.print(mSurfaceX);
8315 pw.print(","); pw.print(mSurfaceY);
8316 pw.print(") "); pw.print(mSurfaceW);
8317 pw.print(" x "); pw.println(mSurfaceH);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008318 }
8319 pw.print(prefix); pw.print("mToken="); pw.println(mToken);
8320 pw.print(prefix); pw.print("mRootToken="); pw.println(mRootToken);
8321 if (mAppToken != null) {
8322 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
8323 }
8324 if (mTargetAppToken != null) {
8325 pw.print(prefix); pw.print("mTargetAppToken="); pw.println(mTargetAppToken);
8326 }
8327 pw.print(prefix); pw.print("mViewVisibility=0x");
8328 pw.print(Integer.toHexString(mViewVisibility));
8329 pw.print(" mLastHidden="); pw.print(mLastHidden);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07008330 pw.print(" mHaveFrame="); pw.print(mHaveFrame);
8331 pw.print(" mObscured="); pw.println(mObscured);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008332 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim || mAttachedHidden) {
8333 pw.print(prefix); pw.print("mPolicyVisibility=");
8334 pw.print(mPolicyVisibility);
8335 pw.print(" mPolicyVisibilityAfterAnim=");
8336 pw.print(mPolicyVisibilityAfterAnim);
8337 pw.print(" mAttachedHidden="); pw.println(mAttachedHidden);
8338 }
Dianne Hackborn9b52a212009-12-11 14:51:35 -08008339 if (!mRelayoutCalled) {
8340 pw.print(prefix); pw.print("mRelayoutCalled="); pw.println(mRelayoutCalled);
8341 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008342 pw.print(prefix); pw.print("Requested w="); pw.print(mRequestedWidth);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08008343 pw.print(" h="); pw.print(mRequestedHeight);
8344 pw.print(" mLayoutSeq="); pw.println(mLayoutSeq);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008345 if (mXOffset != 0 || mYOffset != 0) {
8346 pw.print(prefix); pw.print("Offsets x="); pw.print(mXOffset);
8347 pw.print(" y="); pw.println(mYOffset);
8348 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008349 pw.print(prefix); pw.print("mGivenContentInsets=");
8350 mGivenContentInsets.printShortString(pw);
8351 pw.print(" mGivenVisibleInsets=");
8352 mGivenVisibleInsets.printShortString(pw);
8353 pw.println();
8354 if (mTouchableInsets != 0 || mGivenInsetsPending) {
8355 pw.print(prefix); pw.print("mTouchableInsets="); pw.print(mTouchableInsets);
8356 pw.print(" mGivenInsetsPending="); pw.println(mGivenInsetsPending);
8357 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08008358 pw.print(prefix); pw.print("mConfiguration="); pw.println(mConfiguration);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008359 pw.print(prefix); pw.print("mShownFrame=");
8360 mShownFrame.printShortString(pw);
8361 pw.print(" last="); mLastShownFrame.printShortString(pw);
8362 pw.println();
8363 pw.print(prefix); pw.print("mFrame="); mFrame.printShortString(pw);
8364 pw.print(" last="); mLastFrame.printShortString(pw);
8365 pw.println();
8366 pw.print(prefix); pw.print("mContainingFrame=");
8367 mContainingFrame.printShortString(pw);
8368 pw.print(" mDisplayFrame=");
8369 mDisplayFrame.printShortString(pw);
8370 pw.println();
8371 pw.print(prefix); pw.print("mContentFrame="); mContentFrame.printShortString(pw);
8372 pw.print(" mVisibleFrame="); mVisibleFrame.printShortString(pw);
8373 pw.println();
8374 pw.print(prefix); pw.print("mContentInsets="); mContentInsets.printShortString(pw);
8375 pw.print(" last="); mLastContentInsets.printShortString(pw);
8376 pw.print(" mVisibleInsets="); mVisibleInsets.printShortString(pw);
8377 pw.print(" last="); mLastVisibleInsets.printShortString(pw);
8378 pw.println();
8379 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
8380 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
8381 pw.print(" mAlpha="); pw.print(mAlpha);
8382 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
8383 }
8384 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
8385 || mAnimation != null) {
8386 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
8387 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
8388 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
8389 pw.print(" mAnimation="); pw.println(mAnimation);
8390 }
8391 if (mHasTransformation || mHasLocalTransformation) {
8392 pw.print(prefix); pw.print("XForm: has=");
8393 pw.print(mHasTransformation);
8394 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
8395 pw.print(" "); mTransformation.printShortString(pw);
8396 pw.println();
8397 }
8398 pw.print(prefix); pw.print("mDrawPending="); pw.print(mDrawPending);
8399 pw.print(" mCommitDrawPending="); pw.print(mCommitDrawPending);
8400 pw.print(" mReadyToShow="); pw.print(mReadyToShow);
8401 pw.print(" mHasDrawn="); pw.println(mHasDrawn);
8402 if (mExiting || mRemoveOnExit || mDestroying || mRemoved) {
8403 pw.print(prefix); pw.print("mExiting="); pw.print(mExiting);
8404 pw.print(" mRemoveOnExit="); pw.print(mRemoveOnExit);
8405 pw.print(" mDestroying="); pw.print(mDestroying);
8406 pw.print(" mRemoved="); pw.println(mRemoved);
8407 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07008408 if (mOrientationChanging || mAppFreezing || mTurnOnScreen) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008409 pw.print(prefix); pw.print("mOrientationChanging=");
8410 pw.print(mOrientationChanging);
Dianne Hackborn93e462b2009-09-15 22:50:40 -07008411 pw.print(" mAppFreezing="); pw.print(mAppFreezing);
8412 pw.print(" mTurnOnScreen="); pw.println(mTurnOnScreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008413 }
Mitsuru Oshima589cebe2009-07-22 20:38:58 -07008414 if (mHScale != 1 || mVScale != 1) {
8415 pw.print(prefix); pw.print("mHScale="); pw.print(mHScale);
8416 pw.print(" mVScale="); pw.println(mVScale);
8417 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07008418 if (mWallpaperX != -1 || mWallpaperY != -1) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008419 pw.print(prefix); pw.print("mWallpaperX="); pw.print(mWallpaperX);
8420 pw.print(" mWallpaperY="); pw.println(mWallpaperY);
8421 }
Marco Nelissenbf6956b2009-11-09 15:21:13 -08008422 if (mWallpaperXStep != -1 || mWallpaperYStep != -1) {
8423 pw.print(prefix); pw.print("mWallpaperXStep="); pw.print(mWallpaperXStep);
8424 pw.print(" mWallpaperYStep="); pw.println(mWallpaperYStep);
8425 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008426 }
8427
8428 @Override
8429 public String toString() {
8430 return "Window{"
8431 + Integer.toHexString(System.identityHashCode(this))
8432 + " " + mAttrs.getTitle() + " paused=" + mToken.paused + "}";
8433 }
8434 }
Romain Guy06882f82009-06-10 13:36:04 -07008435
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008436 // -------------------------------------------------------------
8437 // Window Token State
8438 // -------------------------------------------------------------
8439
8440 class WindowToken {
8441 // The actual token.
8442 final IBinder token;
8443
8444 // The type of window this token is for, as per WindowManager.LayoutParams.
8445 final int windowType;
Romain Guy06882f82009-06-10 13:36:04 -07008446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008447 // Set if this token was explicitly added by a client, so should
8448 // not be removed when all windows are removed.
8449 final boolean explicit;
Romain Guy06882f82009-06-10 13:36:04 -07008450
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008451 // For printing.
8452 String stringName;
Romain Guy06882f82009-06-10 13:36:04 -07008453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008454 // If this is an AppWindowToken, this is non-null.
8455 AppWindowToken appWindowToken;
Romain Guy06882f82009-06-10 13:36:04 -07008456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008457 // All of the windows associated with this token.
8458 final ArrayList<WindowState> windows = new ArrayList<WindowState>();
8459
8460 // Is key dispatching paused for this token?
8461 boolean paused = false;
8462
8463 // Should this token's windows be hidden?
8464 boolean hidden;
8465
8466 // Temporary for finding which tokens no longer have visible windows.
8467 boolean hasVisible;
8468
Dianne Hackborna8f60182009-09-01 19:01:50 -07008469 // Set to true when this token is in a pending transaction where it
8470 // will be shown.
8471 boolean waitingToShow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008472
Dianne Hackborna8f60182009-09-01 19:01:50 -07008473 // Set to true when this token is in a pending transaction where it
8474 // will be hidden.
8475 boolean waitingToHide;
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 its
8478 // windows will be put to the bottom of the list.
8479 boolean sendingToBottom;
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 top of the list.
8483 boolean sendingToTop;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008484
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008485 WindowToken(IBinder _token, int type, boolean _explicit) {
8486 token = _token;
8487 windowType = type;
8488 explicit = _explicit;
8489 }
8490
8491 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008492 pw.print(prefix); pw.print("token="); pw.println(token);
8493 pw.print(prefix); pw.print("windows="); pw.println(windows);
8494 pw.print(prefix); pw.print("windowType="); pw.print(windowType);
8495 pw.print(" hidden="); pw.print(hidden);
8496 pw.print(" hasVisible="); pw.println(hasVisible);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008497 if (waitingToShow || waitingToHide || sendingToBottom || sendingToTop) {
8498 pw.print(prefix); pw.print("waitingToShow="); pw.print(waitingToShow);
8499 pw.print(" waitingToHide="); pw.print(waitingToHide);
8500 pw.print(" sendingToBottom="); pw.print(sendingToBottom);
8501 pw.print(" sendingToTop="); pw.println(sendingToTop);
8502 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008503 }
8504
8505 @Override
8506 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008507 if (stringName == null) {
8508 StringBuilder sb = new StringBuilder();
8509 sb.append("WindowToken{");
8510 sb.append(Integer.toHexString(System.identityHashCode(this)));
8511 sb.append(" token="); sb.append(token); sb.append('}');
8512 stringName = sb.toString();
8513 }
8514 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008515 }
8516 };
8517
8518 class AppWindowToken extends WindowToken {
8519 // Non-null only for application tokens.
8520 final IApplicationToken appToken;
8521
8522 // All of the windows and child windows that are included in this
8523 // application token. Note this list is NOT sorted!
8524 final ArrayList<WindowState> allAppWindows = new ArrayList<WindowState>();
8525
8526 int groupId = -1;
8527 boolean appFullscreen;
8528 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Romain Guy06882f82009-06-10 13:36:04 -07008529
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008530 // These are used for determining when all windows associated with
8531 // an activity have been drawn, so they can be made visible together
8532 // at the same time.
8533 int lastTransactionSequence = mTransactionSequence-1;
8534 int numInterestingWindows;
8535 int numDrawnWindows;
8536 boolean inPendingTransaction;
8537 boolean allDrawn;
Romain Guy06882f82009-06-10 13:36:04 -07008538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008539 // Is this token going to be hidden in a little while? If so, it
8540 // won't be taken into account for setting the screen orientation.
8541 boolean willBeHidden;
Romain Guy06882f82009-06-10 13:36:04 -07008542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008543 // Is this window's surface needed? This is almost like hidden, except
8544 // it will sometimes be true a little earlier: when the token has
8545 // been shown, but is still waiting for its app transition to execute
8546 // before making its windows shown.
8547 boolean hiddenRequested;
Romain Guy06882f82009-06-10 13:36:04 -07008548
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008549 // Have we told the window clients to hide themselves?
8550 boolean clientHidden;
Romain Guy06882f82009-06-10 13:36:04 -07008551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008552 // Last visibility state we reported to the app token.
8553 boolean reportedVisible;
8554
8555 // Set to true when the token has been removed from the window mgr.
8556 boolean removed;
8557
8558 // Have we been asked to have this token keep the screen frozen?
8559 boolean freezingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07008560
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008561 boolean animating;
8562 Animation animation;
8563 boolean hasTransformation;
8564 final Transformation transformation = new Transformation();
Romain Guy06882f82009-06-10 13:36:04 -07008565
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008566 // Offset to the window of all layers in the token, for use by
8567 // AppWindowToken animations.
8568 int animLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -07008569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008570 // Information about an application starting window if displayed.
8571 StartingData startingData;
8572 WindowState startingWindow;
8573 View startingView;
8574 boolean startingDisplayed;
8575 boolean startingMoved;
8576 boolean firstWindowDrawn;
8577
8578 AppWindowToken(IApplicationToken _token) {
8579 super(_token.asBinder(),
8580 WindowManager.LayoutParams.TYPE_APPLICATION, true);
8581 appWindowToken = this;
8582 appToken = _token;
8583 }
Romain Guy06882f82009-06-10 13:36:04 -07008584
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008585 public void setAnimation(Animation anim) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008586 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008587 TAG, "Setting animation in " + this + ": " + anim);
8588 animation = anim;
8589 animating = false;
8590 anim.restrictDuration(MAX_ANIMATION_DURATION);
8591 anim.scaleCurrentDuration(mTransitionAnimationScale);
8592 int zorder = anim.getZAdjustment();
8593 int adj = 0;
8594 if (zorder == Animation.ZORDER_TOP) {
8595 adj = TYPE_LAYER_OFFSET;
8596 } else if (zorder == Animation.ZORDER_BOTTOM) {
8597 adj = -TYPE_LAYER_OFFSET;
8598 }
Romain Guy06882f82009-06-10 13:36:04 -07008599
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008600 if (animLayerAdjustment != adj) {
8601 animLayerAdjustment = adj;
8602 updateLayers();
8603 }
8604 }
Romain Guy06882f82009-06-10 13:36:04 -07008605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008606 public void setDummyAnimation() {
8607 if (animation == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008608 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008609 TAG, "Setting dummy animation in " + this);
8610 animation = sDummyAnimation;
8611 }
8612 }
8613
8614 public void clearAnimation() {
8615 if (animation != null) {
8616 animation = null;
8617 animating = true;
8618 }
8619 }
Romain Guy06882f82009-06-10 13:36:04 -07008620
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008621 void updateLayers() {
8622 final int N = allAppWindows.size();
8623 final int adj = animLayerAdjustment;
8624 for (int i=0; i<N; i++) {
8625 WindowState w = allAppWindows.get(i);
8626 w.mAnimLayer = w.mLayer + adj;
Joe Onorato8a9b2202010-02-26 18:56:32 -08008627 if (DEBUG_LAYERS) Slog.v(TAG, "Updating layer " + w + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008628 + w.mAnimLayer);
8629 if (w == mInputMethodTarget) {
8630 setInputMethodAnimLayerAdjustment(adj);
8631 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008632 if (w == mWallpaperTarget && mLowerWallpaperTarget == null) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008633 setWallpaperAnimLayerAdjustmentLocked(adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008634 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008635 }
8636 }
Romain Guy06882f82009-06-10 13:36:04 -07008637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008638 void sendAppVisibilityToClients() {
8639 final int N = allAppWindows.size();
8640 for (int i=0; i<N; i++) {
8641 WindowState win = allAppWindows.get(i);
8642 if (win == startingWindow && clientHidden) {
8643 // Don't hide the starting window.
8644 continue;
8645 }
8646 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008647 if (DEBUG_VISIBILITY) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008648 "Setting visibility of " + win + ": " + (!clientHidden));
8649 win.mClient.dispatchAppVisibility(!clientHidden);
8650 } catch (RemoteException e) {
8651 }
8652 }
8653 }
Romain Guy06882f82009-06-10 13:36:04 -07008654
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008655 void showAllWindowsLocked() {
8656 final int NW = allAppWindows.size();
8657 for (int i=0; i<NW; i++) {
8658 WindowState w = allAppWindows.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08008659 if (DEBUG_VISIBILITY) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008660 "performing show on: " + w);
8661 w.performShowLocked();
8662 }
8663 }
Romain Guy06882f82009-06-10 13:36:04 -07008664
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008665 // This must be called while inside a transaction.
8666 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
Dianne Hackbornde2606d2009-12-18 16:53:55 -08008667 if (!mDisplayFrozen && mPolicy.isScreenOn()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008668 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07008669
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008670 if (animation == sDummyAnimation) {
8671 // This guy is going to animate, but not yet. For now count
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008672 // it as not animating for purposes of scheduling transactions;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008673 // when it is really time to animate, this will be set to
8674 // a real animation and the next call will execute normally.
8675 return false;
8676 }
Romain Guy06882f82009-06-10 13:36:04 -07008677
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008678 if ((allDrawn || animating || startingDisplayed) && animation != null) {
8679 if (!animating) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008680 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008681 TAG, "Starting animation in " + this +
8682 " @ " + currentTime + ": dw=" + dw + " dh=" + dh
8683 + " scale=" + mTransitionAnimationScale
8684 + " allDrawn=" + allDrawn + " animating=" + animating);
8685 animation.initialize(dw, dh, dw, dh);
8686 animation.setStartTime(currentTime);
8687 animating = true;
8688 }
8689 transformation.clear();
8690 final boolean more = animation.getTransformation(
8691 currentTime, transformation);
Joe Onorato8a9b2202010-02-26 18:56:32 -08008692 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008693 TAG, "Stepped animation in " + this +
8694 ": more=" + more + ", xform=" + transformation);
8695 if (more) {
8696 // we're done!
8697 hasTransformation = true;
8698 return true;
8699 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08008700 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008701 TAG, "Finished animation in " + this +
8702 " @ " + currentTime);
8703 animation = null;
8704 }
8705 } else if (animation != null) {
8706 // If the display is frozen, and there is a pending animation,
8707 // clear it and make sure we run the cleanup code.
8708 animating = true;
8709 animation = null;
8710 }
8711
8712 hasTransformation = false;
Romain Guy06882f82009-06-10 13:36:04 -07008713
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008714 if (!animating) {
8715 return false;
8716 }
8717
8718 clearAnimation();
8719 animating = false;
8720 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == this) {
8721 moveInputMethodWindowsIfNeededLocked(true);
8722 }
Romain Guy06882f82009-06-10 13:36:04 -07008723
Joe Onorato8a9b2202010-02-26 18:56:32 -08008724 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008725 TAG, "Animation done in " + this
8726 + ": reportedVisible=" + reportedVisible);
8727
8728 transformation.clear();
8729 if (animLayerAdjustment != 0) {
8730 animLayerAdjustment = 0;
8731 updateLayers();
8732 }
Romain Guy06882f82009-06-10 13:36:04 -07008733
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008734 final int N = windows.size();
8735 for (int i=0; i<N; i++) {
8736 ((WindowState)windows.get(i)).finishExit();
8737 }
8738 updateReportedVisibilityLocked();
Romain Guy06882f82009-06-10 13:36:04 -07008739
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008740 return false;
8741 }
8742
8743 void updateReportedVisibilityLocked() {
8744 if (appToken == null) {
8745 return;
8746 }
Romain Guy06882f82009-06-10 13:36:04 -07008747
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008748 int numInteresting = 0;
8749 int numVisible = 0;
8750 boolean nowGone = true;
Romain Guy06882f82009-06-10 13:36:04 -07008751
Joe Onorato8a9b2202010-02-26 18:56:32 -08008752 if (DEBUG_VISIBILITY) Slog.v(TAG, "Update reported visibility: " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008753 final int N = allAppWindows.size();
8754 for (int i=0; i<N; i++) {
8755 WindowState win = allAppWindows.get(i);
Dianne Hackborn6cf67fa2009-12-21 16:46:34 -08008756 if (win == startingWindow || win.mAppFreezing
The Android Open Source Project727cec02010-04-08 11:35:37 -07008757 || win.mViewVisibility != View.VISIBLE
8758 || win.mAttrs.type == TYPE_APPLICATION_STARTING) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008759 continue;
8760 }
8761 if (DEBUG_VISIBILITY) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008762 Slog.v(TAG, "Win " + win + ": isDrawn="
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008763 + win.isDrawnLw()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008764 + ", isAnimating=" + win.isAnimating());
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008765 if (!win.isDrawnLw()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008766 Slog.v(TAG, "Not displayed: s=" + win.mSurface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008767 + " pv=" + win.mPolicyVisibility
8768 + " dp=" + win.mDrawPending
8769 + " cdp=" + win.mCommitDrawPending
8770 + " ah=" + win.mAttachedHidden
8771 + " th="
8772 + (win.mAppToken != null
8773 ? win.mAppToken.hiddenRequested : false)
8774 + " a=" + win.mAnimating);
8775 }
8776 }
8777 numInteresting++;
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008778 if (win.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008779 if (!win.isAnimating()) {
8780 numVisible++;
8781 }
8782 nowGone = false;
8783 } else if (win.isAnimating()) {
8784 nowGone = false;
8785 }
8786 }
Romain Guy06882f82009-06-10 13:36:04 -07008787
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008788 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
Joe Onorato8a9b2202010-02-26 18:56:32 -08008789 if (DEBUG_VISIBILITY) Slog.v(TAG, "VIS " + this + ": interesting="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008790 + numInteresting + " visible=" + numVisible);
8791 if (nowVisible != reportedVisible) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008792 if (DEBUG_VISIBILITY) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008793 TAG, "Visibility changed in " + this
8794 + ": vis=" + nowVisible);
8795 reportedVisible = nowVisible;
8796 Message m = mH.obtainMessage(
8797 H.REPORT_APPLICATION_TOKEN_WINDOWS,
8798 nowVisible ? 1 : 0,
8799 nowGone ? 1 : 0,
8800 this);
8801 mH.sendMessage(m);
8802 }
8803 }
Romain Guy06882f82009-06-10 13:36:04 -07008804
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07008805 WindowState findMainWindow() {
8806 int j = windows.size();
8807 while (j > 0) {
8808 j--;
8809 WindowState win = windows.get(j);
8810 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
8811 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
8812 return win;
8813 }
8814 }
8815 return null;
8816 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008818 void dump(PrintWriter pw, String prefix) {
8819 super.dump(pw, prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008820 if (appToken != null) {
8821 pw.print(prefix); pw.println("app=true");
8822 }
8823 if (allAppWindows.size() > 0) {
8824 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
8825 }
8826 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008827 pw.print(" appFullscreen="); pw.print(appFullscreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008828 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
8829 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
8830 pw.print(" clientHidden="); pw.print(clientHidden);
8831 pw.print(" willBeHidden="); pw.print(willBeHidden);
8832 pw.print(" reportedVisible="); pw.println(reportedVisible);
8833 if (paused || freezingScreen) {
8834 pw.print(prefix); pw.print("paused="); pw.print(paused);
8835 pw.print(" freezingScreen="); pw.println(freezingScreen);
8836 }
8837 if (numInterestingWindows != 0 || numDrawnWindows != 0
8838 || inPendingTransaction || allDrawn) {
8839 pw.print(prefix); pw.print("numInterestingWindows=");
8840 pw.print(numInterestingWindows);
8841 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
8842 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
8843 pw.print(" allDrawn="); pw.println(allDrawn);
8844 }
8845 if (animating || animation != null) {
8846 pw.print(prefix); pw.print("animating="); pw.print(animating);
8847 pw.print(" animation="); pw.println(animation);
8848 }
8849 if (animLayerAdjustment != 0) {
8850 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
8851 }
8852 if (hasTransformation) {
8853 pw.print(prefix); pw.print("hasTransformation="); pw.print(hasTransformation);
8854 pw.print(" transformation="); transformation.printShortString(pw);
8855 pw.println();
8856 }
8857 if (startingData != null || removed || firstWindowDrawn) {
8858 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
8859 pw.print(" removed="); pw.print(removed);
8860 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
8861 }
8862 if (startingWindow != null || startingView != null
8863 || startingDisplayed || startingMoved) {
8864 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
8865 pw.print(" startingView="); pw.print(startingView);
8866 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
8867 pw.print(" startingMoved"); pw.println(startingMoved);
8868 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008869 }
8870
8871 @Override
8872 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008873 if (stringName == null) {
8874 StringBuilder sb = new StringBuilder();
8875 sb.append("AppWindowToken{");
8876 sb.append(Integer.toHexString(System.identityHashCode(this)));
8877 sb.append(" token="); sb.append(token); sb.append('}');
8878 stringName = sb.toString();
8879 }
8880 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008881 }
8882 }
Romain Guy06882f82009-06-10 13:36:04 -07008883
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008884 // -------------------------------------------------------------
8885 // DummyAnimation
8886 // -------------------------------------------------------------
8887
8888 // This is an animation that does nothing: it just immediately finishes
8889 // itself every time it is called. It is used as a stub animation in cases
8890 // where we want to synchronize multiple things that may be animating.
8891 static final class DummyAnimation extends Animation {
8892 public boolean getTransformation(long currentTime, Transformation outTransformation) {
8893 return false;
8894 }
8895 }
8896 static final Animation sDummyAnimation = new DummyAnimation();
Romain Guy06882f82009-06-10 13:36:04 -07008897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008898 // -------------------------------------------------------------
8899 // Async Handler
8900 // -------------------------------------------------------------
8901
8902 static final class StartingData {
8903 final String pkg;
8904 final int theme;
8905 final CharSequence nonLocalizedLabel;
8906 final int labelRes;
8907 final int icon;
Romain Guy06882f82009-06-10 13:36:04 -07008908
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008909 StartingData(String _pkg, int _theme, CharSequence _nonLocalizedLabel,
8910 int _labelRes, int _icon) {
8911 pkg = _pkg;
8912 theme = _theme;
8913 nonLocalizedLabel = _nonLocalizedLabel;
8914 labelRes = _labelRes;
8915 icon = _icon;
8916 }
8917 }
8918
8919 private final class H extends Handler {
8920 public static final int REPORT_FOCUS_CHANGE = 2;
8921 public static final int REPORT_LOSING_FOCUS = 3;
8922 public static final int ANIMATE = 4;
8923 public static final int ADD_STARTING = 5;
8924 public static final int REMOVE_STARTING = 6;
8925 public static final int FINISHED_STARTING = 7;
8926 public static final int REPORT_APPLICATION_TOKEN_WINDOWS = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008927 public static final int WINDOW_FREEZE_TIMEOUT = 11;
8928 public static final int HOLD_SCREEN_CHANGED = 12;
8929 public static final int APP_TRANSITION_TIMEOUT = 13;
8930 public static final int PERSIST_ANIMATION_SCALE = 14;
8931 public static final int FORCE_GC = 15;
8932 public static final int ENABLE_SCREEN = 16;
8933 public static final int APP_FREEZE_TIMEOUT = 17;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08008934 public static final int SEND_NEW_CONFIGURATION = 18;
Romain Guy06882f82009-06-10 13:36:04 -07008935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008936 private Session mLastReportedHold;
Romain Guy06882f82009-06-10 13:36:04 -07008937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008938 public H() {
8939 }
Romain Guy06882f82009-06-10 13:36:04 -07008940
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008941 @Override
8942 public void handleMessage(Message msg) {
8943 switch (msg.what) {
8944 case REPORT_FOCUS_CHANGE: {
8945 WindowState lastFocus;
8946 WindowState newFocus;
Romain Guy06882f82009-06-10 13:36:04 -07008947
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008948 synchronized(mWindowMap) {
8949 lastFocus = mLastFocus;
8950 newFocus = mCurrentFocus;
8951 if (lastFocus == newFocus) {
8952 // Focus is not changing, so nothing to do.
8953 return;
8954 }
8955 mLastFocus = newFocus;
Joe Onorato8a9b2202010-02-26 18:56:32 -08008956 //Slog.i(TAG, "Focus moving from " + lastFocus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008957 // + " to " + newFocus);
8958 if (newFocus != null && lastFocus != null
8959 && !newFocus.isDisplayedLw()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008960 //Slog.i(TAG, "Delaying loss of focus...");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008961 mLosingFocus.add(lastFocus);
8962 lastFocus = null;
8963 }
8964 }
8965
8966 if (lastFocus != newFocus) {
8967 //System.out.println("Changing focus from " + lastFocus
8968 // + " to " + newFocus);
8969 if (newFocus != null) {
8970 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008971 //Slog.i(TAG, "Gaining focus: " + newFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008972 newFocus.mClient.windowFocusChanged(true, mInTouchMode);
8973 } catch (RemoteException e) {
8974 // Ignore if process has died.
8975 }
8976 }
8977
8978 if (lastFocus != null) {
8979 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008980 //Slog.i(TAG, "Losing focus: " + lastFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008981 lastFocus.mClient.windowFocusChanged(false, mInTouchMode);
8982 } catch (RemoteException e) {
8983 // Ignore if process has died.
8984 }
8985 }
8986 }
8987 } break;
8988
8989 case REPORT_LOSING_FOCUS: {
8990 ArrayList<WindowState> losers;
Romain Guy06882f82009-06-10 13:36:04 -07008991
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008992 synchronized(mWindowMap) {
8993 losers = mLosingFocus;
8994 mLosingFocus = new ArrayList<WindowState>();
8995 }
8996
8997 final int N = losers.size();
8998 for (int i=0; i<N; i++) {
8999 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009000 //Slog.i(TAG, "Losing delayed focus: " + losers.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009001 losers.get(i).mClient.windowFocusChanged(false, mInTouchMode);
9002 } catch (RemoteException e) {
9003 // Ignore if process has died.
9004 }
9005 }
9006 } break;
9007
9008 case ANIMATE: {
9009 synchronized(mWindowMap) {
9010 mAnimationPending = false;
9011 performLayoutAndPlaceSurfacesLocked();
9012 }
9013 } break;
9014
9015 case ADD_STARTING: {
9016 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
9017 final StartingData sd = wtoken.startingData;
9018
9019 if (sd == null) {
9020 // Animation has been canceled... do nothing.
9021 return;
9022 }
Romain Guy06882f82009-06-10 13:36:04 -07009023
Joe Onorato8a9b2202010-02-26 18:56:32 -08009024 if (DEBUG_STARTING_WINDOW) Slog.v(TAG, "Add starting "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009025 + wtoken + ": pkg=" + sd.pkg);
Romain Guy06882f82009-06-10 13:36:04 -07009026
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009027 View view = null;
9028 try {
9029 view = mPolicy.addStartingWindow(
9030 wtoken.token, sd.pkg,
9031 sd.theme, sd.nonLocalizedLabel, sd.labelRes,
9032 sd.icon);
9033 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009034 Slog.w(TAG, "Exception when adding starting window", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009035 }
9036
9037 if (view != null) {
9038 boolean abort = false;
9039
9040 synchronized(mWindowMap) {
9041 if (wtoken.removed || wtoken.startingData == null) {
9042 // If the window was successfully added, then
9043 // we need to remove it.
9044 if (wtoken.startingWindow != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009045 if (DEBUG_STARTING_WINDOW) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009046 "Aborted starting " + wtoken
9047 + ": removed=" + wtoken.removed
9048 + " startingData=" + wtoken.startingData);
9049 wtoken.startingWindow = null;
9050 wtoken.startingData = null;
9051 abort = true;
9052 }
9053 } else {
9054 wtoken.startingView = view;
9055 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08009056 if (DEBUG_STARTING_WINDOW && !abort) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009057 "Added starting " + wtoken
9058 + ": startingWindow="
9059 + wtoken.startingWindow + " startingView="
9060 + wtoken.startingView);
9061 }
9062
9063 if (abort) {
9064 try {
9065 mPolicy.removeStartingWindow(wtoken.token, view);
9066 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009067 Slog.w(TAG, "Exception when removing starting window", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009068 }
9069 }
9070 }
9071 } break;
9072
9073 case REMOVE_STARTING: {
9074 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
9075 IBinder token = null;
9076 View view = null;
9077 synchronized (mWindowMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009078 if (DEBUG_STARTING_WINDOW) Slog.v(TAG, "Remove starting "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009079 + wtoken + ": startingWindow="
9080 + wtoken.startingWindow + " startingView="
9081 + wtoken.startingView);
9082 if (wtoken.startingWindow != null) {
9083 view = wtoken.startingView;
9084 token = wtoken.token;
9085 wtoken.startingData = null;
9086 wtoken.startingView = null;
9087 wtoken.startingWindow = null;
9088 }
9089 }
9090 if (view != null) {
9091 try {
9092 mPolicy.removeStartingWindow(token, view);
9093 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009094 Slog.w(TAG, "Exception when removing starting window", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009095 }
9096 }
9097 } break;
9098
9099 case FINISHED_STARTING: {
9100 IBinder token = null;
9101 View view = null;
9102 while (true) {
9103 synchronized (mWindowMap) {
9104 final int N = mFinishedStarting.size();
9105 if (N <= 0) {
9106 break;
9107 }
9108 AppWindowToken wtoken = mFinishedStarting.remove(N-1);
9109
Joe Onorato8a9b2202010-02-26 18:56:32 -08009110 if (DEBUG_STARTING_WINDOW) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009111 "Finished starting " + wtoken
9112 + ": startingWindow=" + wtoken.startingWindow
9113 + " startingView=" + wtoken.startingView);
9114
9115 if (wtoken.startingWindow == null) {
9116 continue;
9117 }
9118
9119 view = wtoken.startingView;
9120 token = wtoken.token;
9121 wtoken.startingData = null;
9122 wtoken.startingView = null;
9123 wtoken.startingWindow = null;
9124 }
9125
9126 try {
9127 mPolicy.removeStartingWindow(token, view);
9128 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009129 Slog.w(TAG, "Exception when removing starting window", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009130 }
9131 }
9132 } break;
9133
9134 case REPORT_APPLICATION_TOKEN_WINDOWS: {
9135 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
9136
9137 boolean nowVisible = msg.arg1 != 0;
9138 boolean nowGone = msg.arg2 != 0;
9139
9140 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009141 if (DEBUG_VISIBILITY) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009142 TAG, "Reporting visible in " + wtoken
9143 + " visible=" + nowVisible
9144 + " gone=" + nowGone);
9145 if (nowVisible) {
9146 wtoken.appToken.windowsVisible();
9147 } else {
9148 wtoken.appToken.windowsGone();
9149 }
9150 } catch (RemoteException ex) {
9151 }
9152 } break;
Romain Guy06882f82009-06-10 13:36:04 -07009153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009154 case WINDOW_FREEZE_TIMEOUT: {
9155 synchronized (mWindowMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009156 Slog.w(TAG, "Window freeze timeout expired.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009157 int i = mWindows.size();
9158 while (i > 0) {
9159 i--;
9160 WindowState w = (WindowState)mWindows.get(i);
9161 if (w.mOrientationChanging) {
9162 w.mOrientationChanging = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009163 Slog.w(TAG, "Force clearing orientation change: " + w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009164 }
9165 }
9166 performLayoutAndPlaceSurfacesLocked();
9167 }
9168 break;
9169 }
Romain Guy06882f82009-06-10 13:36:04 -07009170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009171 case HOLD_SCREEN_CHANGED: {
9172 Session oldHold;
9173 Session newHold;
9174 synchronized (mWindowMap) {
9175 oldHold = mLastReportedHold;
9176 newHold = (Session)msg.obj;
9177 mLastReportedHold = newHold;
9178 }
Romain Guy06882f82009-06-10 13:36:04 -07009179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009180 if (oldHold != newHold) {
9181 try {
9182 if (oldHold != null) {
9183 mBatteryStats.noteStopWakelock(oldHold.mUid,
9184 "window",
9185 BatteryStats.WAKE_TYPE_WINDOW);
9186 }
9187 if (newHold != null) {
9188 mBatteryStats.noteStartWakelock(newHold.mUid,
9189 "window",
9190 BatteryStats.WAKE_TYPE_WINDOW);
9191 }
9192 } catch (RemoteException e) {
9193 }
9194 }
9195 break;
9196 }
Romain Guy06882f82009-06-10 13:36:04 -07009197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009198 case APP_TRANSITION_TIMEOUT: {
9199 synchronized (mWindowMap) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009200 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009201 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009202 "*** APP TRANSITION TIMEOUT");
9203 mAppTransitionReady = true;
9204 mAppTransitionTimeout = true;
9205 performLayoutAndPlaceSurfacesLocked();
9206 }
9207 }
9208 break;
9209 }
Romain Guy06882f82009-06-10 13:36:04 -07009210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009211 case PERSIST_ANIMATION_SCALE: {
9212 Settings.System.putFloat(mContext.getContentResolver(),
9213 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
9214 Settings.System.putFloat(mContext.getContentResolver(),
9215 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
9216 break;
9217 }
Romain Guy06882f82009-06-10 13:36:04 -07009218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009219 case FORCE_GC: {
9220 synchronized(mWindowMap) {
9221 if (mAnimationPending) {
9222 // If we are animating, don't do the gc now but
9223 // delay a bit so we don't interrupt the animation.
9224 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
9225 2000);
9226 return;
9227 }
9228 // If we are currently rotating the display, it will
9229 // schedule a new message when done.
9230 if (mDisplayFrozen) {
9231 return;
9232 }
9233 mFreezeGcPending = 0;
9234 }
9235 Runtime.getRuntime().gc();
9236 break;
9237 }
Romain Guy06882f82009-06-10 13:36:04 -07009238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009239 case ENABLE_SCREEN: {
9240 performEnableScreen();
9241 break;
9242 }
Romain Guy06882f82009-06-10 13:36:04 -07009243
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009244 case APP_FREEZE_TIMEOUT: {
9245 synchronized (mWindowMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009246 Slog.w(TAG, "App freeze timeout expired.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009247 int i = mAppTokens.size();
9248 while (i > 0) {
9249 i--;
9250 AppWindowToken tok = mAppTokens.get(i);
9251 if (tok.freezingScreen) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009252 Slog.w(TAG, "Force clearing freeze: " + tok);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009253 unsetAppFreezingScreenLocked(tok, true, true);
9254 }
9255 }
9256 }
9257 break;
9258 }
Romain Guy06882f82009-06-10 13:36:04 -07009259
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009260 case SEND_NEW_CONFIGURATION: {
9261 removeMessages(SEND_NEW_CONFIGURATION);
9262 sendNewConfiguration();
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07009263 break;
9264 }
Romain Guy06882f82009-06-10 13:36:04 -07009265
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009266 }
9267 }
9268 }
9269
9270 // -------------------------------------------------------------
9271 // IWindowManager API
9272 // -------------------------------------------------------------
9273
9274 public IWindowSession openSession(IInputMethodClient client,
9275 IInputContext inputContext) {
9276 if (client == null) throw new IllegalArgumentException("null client");
9277 if (inputContext == null) throw new IllegalArgumentException("null inputContext");
9278 return new Session(client, inputContext);
9279 }
9280
9281 public boolean inputMethodClientHasFocus(IInputMethodClient client) {
9282 synchronized (mWindowMap) {
9283 // The focus for the client is the window immediately below
9284 // where we would place the input method window.
9285 int idx = findDesiredInputMethodWindowIndexLocked(false);
9286 WindowState imFocus;
9287 if (idx > 0) {
9288 imFocus = (WindowState)mWindows.get(idx-1);
9289 if (imFocus != null) {
9290 if (imFocus.mSession.mClient != null &&
9291 imFocus.mSession.mClient.asBinder() == client.asBinder()) {
9292 return true;
9293 }
9294 }
9295 }
9296 }
9297 return false;
9298 }
Romain Guy06882f82009-06-10 13:36:04 -07009299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009300 // -------------------------------------------------------------
9301 // Internals
9302 // -------------------------------------------------------------
9303
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009304 final WindowState windowForClientLocked(Session session, IWindow client,
9305 boolean throwOnError) {
9306 return windowForClientLocked(session, client.asBinder(), throwOnError);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009307 }
Romain Guy06882f82009-06-10 13:36:04 -07009308
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009309 final WindowState windowForClientLocked(Session session, IBinder client,
9310 boolean throwOnError) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009311 WindowState win = mWindowMap.get(client);
Joe Onorato8a9b2202010-02-26 18:56:32 -08009312 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009313 TAG, "Looking up client " + client + ": " + win);
9314 if (win == null) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009315 RuntimeException ex = new IllegalArgumentException(
9316 "Requested window " + client + " does not exist");
9317 if (throwOnError) {
9318 throw ex;
9319 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08009320 Slog.w(TAG, "Failed looking up window", ex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009321 return null;
9322 }
9323 if (session != null && win.mSession != session) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009324 RuntimeException ex = new IllegalArgumentException(
9325 "Requested window " + client + " is in session " +
9326 win.mSession + ", not " + session);
9327 if (throwOnError) {
9328 throw ex;
9329 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08009330 Slog.w(TAG, "Failed looking up window", ex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009331 return null;
9332 }
9333
9334 return win;
9335 }
9336
Dianne Hackborna8f60182009-09-01 19:01:50 -07009337 final void rebuildAppWindowListLocked() {
9338 int NW = mWindows.size();
9339 int i;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009340 int lastWallpaper = -1;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009341 int numRemoved = 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009342
Dianne Hackborna8f60182009-09-01 19:01:50 -07009343 // First remove all existing app windows.
9344 i=0;
9345 while (i < NW) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009346 WindowState w = (WindowState)mWindows.get(i);
9347 if (w.mAppToken != null) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009348 WindowState win = (WindowState)mWindows.remove(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08009349 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG,
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009350 "Rebuild removing window: " + win);
Dianne Hackborna8f60182009-09-01 19:01:50 -07009351 NW--;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009352 numRemoved++;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009353 continue;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009354 } else if (w.mAttrs.type == WindowManager.LayoutParams.TYPE_WALLPAPER
9355 && lastWallpaper == i-1) {
9356 lastWallpaper = i;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009357 }
9358 i++;
9359 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009360
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009361 // The wallpaper window(s) typically live at the bottom of the stack,
9362 // so skip them before adding app tokens.
9363 lastWallpaper++;
9364 i = lastWallpaper;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009365
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009366 // First add all of the exiting app tokens... these are no longer
9367 // in the main app list, but still have windows shown. We put them
9368 // in the back because now that the animation is over we no longer
9369 // will care about them.
9370 int NT = mExitingAppTokens.size();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009371 for (int j=0; j<NT; j++) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009372 i = reAddAppWindowsLocked(i, mExitingAppTokens.get(j));
9373 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009374
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009375 // And add in the still active app tokens in Z order.
9376 NT = mAppTokens.size();
9377 for (int j=0; j<NT; j++) {
9378 i = reAddAppWindowsLocked(i, mAppTokens.get(j));
Dianne Hackborna8f60182009-09-01 19:01:50 -07009379 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009380
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009381 i -= lastWallpaper;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009382 if (i != numRemoved) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009383 Slog.w(TAG, "Rebuild removed " + numRemoved
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009384 + " windows but added " + i);
9385 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07009386 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009388 private final void assignLayersLocked() {
9389 int N = mWindows.size();
9390 int curBaseLayer = 0;
9391 int curLayer = 0;
9392 int i;
Romain Guy06882f82009-06-10 13:36:04 -07009393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009394 for (i=0; i<N; i++) {
9395 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009396 if (w.mBaseLayer == curBaseLayer || w.mIsImWindow
9397 || (i > 0 && w.mIsWallpaper)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009398 curLayer += WINDOW_LAYER_MULTIPLIER;
9399 w.mLayer = curLayer;
9400 } else {
9401 curBaseLayer = curLayer = w.mBaseLayer;
9402 w.mLayer = curLayer;
9403 }
9404 if (w.mTargetAppToken != null) {
9405 w.mAnimLayer = w.mLayer + w.mTargetAppToken.animLayerAdjustment;
9406 } else if (w.mAppToken != null) {
9407 w.mAnimLayer = w.mLayer + w.mAppToken.animLayerAdjustment;
9408 } else {
9409 w.mAnimLayer = w.mLayer;
9410 }
9411 if (w.mIsImWindow) {
9412 w.mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07009413 } else if (w.mIsWallpaper) {
9414 w.mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009415 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08009416 if (DEBUG_LAYERS) Slog.v(TAG, "Assign layer " + w + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009417 + w.mAnimLayer);
9418 //System.out.println(
9419 // "Assigned layer " + curLayer + " to " + w.mClient.asBinder());
9420 }
9421 }
9422
9423 private boolean mInLayout = false;
9424 private final void performLayoutAndPlaceSurfacesLocked() {
9425 if (mInLayout) {
Dave Bortcfe65242009-04-09 14:51:04 -07009426 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009427 throw new RuntimeException("Recursive call!");
9428 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08009429 Slog.w(TAG, "performLayoutAndPlaceSurfacesLocked called while in layout");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009430 return;
9431 }
9432
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009433 if (mWaitingForConfig) {
9434 // Our configuration has changed (most likely rotation), but we
9435 // don't yet have the complete configuration to report to
9436 // applications. Don't do any window layout until we have it.
9437 return;
9438 }
9439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009440 boolean recoveringMemory = false;
9441 if (mForceRemoves != null) {
9442 recoveringMemory = true;
9443 // Wait a little it for things to settle down, and off we go.
9444 for (int i=0; i<mForceRemoves.size(); i++) {
9445 WindowState ws = mForceRemoves.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08009446 Slog.i(TAG, "Force removing: " + ws);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009447 removeWindowInnerLocked(ws.mSession, ws);
9448 }
9449 mForceRemoves = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009450 Slog.w(TAG, "Due to memory failure, waiting a bit for next layout");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009451 Object tmp = new Object();
9452 synchronized (tmp) {
9453 try {
9454 tmp.wait(250);
9455 } catch (InterruptedException e) {
9456 }
9457 }
9458 }
Romain Guy06882f82009-06-10 13:36:04 -07009459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009460 mInLayout = true;
9461 try {
9462 performLayoutAndPlaceSurfacesLockedInner(recoveringMemory);
Romain Guy06882f82009-06-10 13:36:04 -07009463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009464 int i = mPendingRemove.size()-1;
9465 if (i >= 0) {
9466 while (i >= 0) {
9467 WindowState w = mPendingRemove.get(i);
9468 removeWindowInnerLocked(w.mSession, w);
9469 i--;
9470 }
9471 mPendingRemove.clear();
9472
9473 mInLayout = false;
9474 assignLayersLocked();
9475 mLayoutNeeded = true;
9476 performLayoutAndPlaceSurfacesLocked();
9477
9478 } else {
9479 mInLayout = false;
9480 if (mLayoutNeeded) {
9481 requestAnimationLocked(0);
9482 }
9483 }
9484 } catch (RuntimeException e) {
9485 mInLayout = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009486 Slog.e(TAG, "Unhandled exception while layout out windows", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009487 }
9488 }
9489
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009490 private final int performLayoutLockedInner() {
9491 if (!mLayoutNeeded) {
9492 return 0;
9493 }
9494
9495 mLayoutNeeded = false;
9496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009497 final int dw = mDisplay.getWidth();
9498 final int dh = mDisplay.getHeight();
9499
9500 final int N = mWindows.size();
9501 int i;
9502
Joe Onorato8a9b2202010-02-26 18:56:32 -08009503 if (DEBUG_LAYOUT) Slog.v(TAG, "performLayout: needed="
Dianne Hackborn9b52a212009-12-11 14:51:35 -08009504 + mLayoutNeeded + " dw=" + dw + " dh=" + dh);
9505
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009506 mPolicy.beginLayoutLw(dw, dh);
Romain Guy06882f82009-06-10 13:36:04 -07009507
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009508 int seq = mLayoutSeq+1;
9509 if (seq < 0) seq = 0;
9510 mLayoutSeq = seq;
9511
9512 // First perform layout of any root windows (not attached
9513 // to another window).
9514 int topAttached = -1;
9515 for (i = N-1; i >= 0; i--) {
9516 WindowState win = (WindowState) mWindows.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009517
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009518 // Don't do layout of a window if it is not visible, or
9519 // soon won't be visible, to avoid wasting time and funky
9520 // changes while a window is animating away.
9521 final AppWindowToken atoken = win.mAppToken;
9522 final boolean gone = win.mViewVisibility == View.GONE
9523 || !win.mRelayoutCalled
9524 || win.mRootToken.hidden
9525 || (atoken != null && atoken.hiddenRequested)
9526 || win.mAttachedHidden
9527 || win.mExiting || win.mDestroying;
9528
9529 if (!win.mLayoutAttached) {
9530 if (DEBUG_LAYOUT) Slog.v(TAG, "First pass " + win
9531 + ": gone=" + gone + " mHaveFrame=" + win.mHaveFrame
9532 + " mLayoutAttached=" + win.mLayoutAttached);
9533 if (DEBUG_LAYOUT && gone) Slog.v(TAG, " (mViewVisibility="
9534 + win.mViewVisibility + " mRelayoutCalled="
9535 + win.mRelayoutCalled + " hidden="
9536 + win.mRootToken.hidden + " hiddenRequested="
9537 + (atoken != null && atoken.hiddenRequested)
9538 + " mAttachedHidden=" + win.mAttachedHidden);
9539 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009540
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009541 // If this view is GONE, then skip it -- keep the current
9542 // frame, and let the caller know so they can ignore it
9543 // if they want. (We do the normal layout for INVISIBLE
9544 // windows, since that means "perform layout as normal,
9545 // just don't display").
9546 if (!gone || !win.mHaveFrame) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009547 if (!win.mLayoutAttached) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009548 mPolicy.layoutWindowLw(win, win.mAttrs, null);
9549 win.mLayoutSeq = seq;
9550 if (DEBUG_LAYOUT) Slog.v(TAG, "-> mFrame="
9551 + win.mFrame + " mContainingFrame="
9552 + win.mContainingFrame + " mDisplayFrame="
9553 + win.mDisplayFrame);
9554 } else {
9555 if (topAttached < 0) topAttached = i;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009556 }
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009557 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009558 }
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009559
9560 // Now perform layout of attached windows, which usually
9561 // depend on the position of the window they are attached to.
9562 // XXX does not deal with windows that are attached to windows
9563 // that are themselves attached.
9564 for (i = topAttached; i >= 0; i--) {
9565 WindowState win = (WindowState) mWindows.get(i);
9566
9567 // If this view is GONE, then skip it -- keep the current
9568 // frame, and let the caller know so they can ignore it
9569 // if they want. (We do the normal layout for INVISIBLE
9570 // windows, since that means "perform layout as normal,
9571 // just don't display").
9572 if (win.mLayoutAttached) {
9573 if (DEBUG_LAYOUT) Slog.v(TAG, "Second pass " + win
9574 + " mHaveFrame=" + win.mHaveFrame
9575 + " mViewVisibility=" + win.mViewVisibility
9576 + " mRelayoutCalled=" + win.mRelayoutCalled);
9577 if ((win.mViewVisibility != View.GONE && win.mRelayoutCalled)
9578 || !win.mHaveFrame) {
9579 mPolicy.layoutWindowLw(win, win.mAttrs, win.mAttachedWindow);
9580 win.mLayoutSeq = seq;
9581 if (DEBUG_LAYOUT) Slog.v(TAG, "-> mFrame="
9582 + win.mFrame + " mContainingFrame="
9583 + win.mContainingFrame + " mDisplayFrame="
9584 + win.mDisplayFrame);
9585 }
9586 }
9587 }
9588
9589 return mPolicy.finishLayoutLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009590 }
Romain Guy06882f82009-06-10 13:36:04 -07009591
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009592 private final void performLayoutAndPlaceSurfacesLockedInner(
9593 boolean recoveringMemory) {
9594 final long currentTime = SystemClock.uptimeMillis();
9595 final int dw = mDisplay.getWidth();
9596 final int dh = mDisplay.getHeight();
9597
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009598 int i;
9599
Dianne Hackbornb601ce12010-03-01 23:36:02 -08009600 if (mFocusMayChange) {
9601 mFocusMayChange = false;
9602 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
9603 }
9604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009605 if (mFxSession == null) {
9606 mFxSession = new SurfaceSession();
9607 }
Romain Guy06882f82009-06-10 13:36:04 -07009608
Joe Onorato8a9b2202010-02-26 18:56:32 -08009609 if (SHOW_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009610
9611 // Initialize state of exiting tokens.
9612 for (i=mExitingTokens.size()-1; i>=0; i--) {
9613 mExitingTokens.get(i).hasVisible = false;
9614 }
9615
9616 // Initialize state of exiting applications.
9617 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
9618 mExitingAppTokens.get(i).hasVisible = false;
9619 }
9620
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009621 boolean orientationChangeComplete = true;
9622 Session holdScreen = null;
9623 float screenBrightness = -1;
Mike Lockwoodfb73f792009-11-20 11:31:18 -05009624 float buttonBrightness = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009625 boolean focusDisplayed = false;
9626 boolean animating = false;
9627
9628 Surface.openTransaction();
9629 try {
Dianne Hackbornde2606d2009-12-18 16:53:55 -08009630 boolean wallpaperForceHidingChanged = false;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009631 int repeats = 0;
9632 int changes = 0;
9633
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009634 do {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009635 repeats++;
9636 if (repeats > 6) {
9637 Slog.w(TAG, "Animation repeat aborted after too many iterations");
9638 mLayoutNeeded = false;
9639 break;
9640 }
9641
9642 if ((changes&(WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER
9643 | WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG
9644 | WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT)) != 0) {
9645 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
9646 if ((adjustWallpaperWindowsLocked()&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
9647 assignLayersLocked();
9648 mLayoutNeeded = true;
9649 }
9650 }
9651 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG) != 0) {
9652 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
9653 if (updateOrientationFromAppTokensLocked()) {
9654 mLayoutNeeded = true;
9655 mH.sendEmptyMessage(H.SEND_NEW_CONFIGURATION);
9656 }
9657 }
9658 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT) != 0) {
9659 mLayoutNeeded = true;
9660 }
9661 }
9662
9663 // FIRST LOOP: Perform a layout, if needed.
9664 if (repeats < 4) {
9665 changes = performLayoutLockedInner();
9666 if (changes != 0) {
9667 continue;
9668 }
9669 } else {
9670 Slog.w(TAG, "Layout repeat skipped after too many iterations");
9671 changes = 0;
9672 }
9673
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009674 final int transactionSequence = ++mTransactionSequence;
9675
9676 // Update animations of all applications, including those
9677 // associated with exiting/removed apps
9678 boolean tokensAnimating = false;
9679 final int NAT = mAppTokens.size();
9680 for (i=0; i<NAT; i++) {
9681 if (mAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
9682 tokensAnimating = true;
9683 }
9684 }
9685 final int NEAT = mExitingAppTokens.size();
9686 for (i=0; i<NEAT; i++) {
9687 if (mExitingAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
9688 tokensAnimating = true;
9689 }
9690 }
9691
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009692 // SECOND LOOP: Execute animations and update visibility of windows.
9693
Joe Onorato8a9b2202010-02-26 18:56:32 -08009694 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG, "*** ANIM STEP: seq="
Dianne Hackbornde2606d2009-12-18 16:53:55 -08009695 + transactionSequence + " tokensAnimating="
9696 + tokensAnimating);
9697
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009698 animating = tokensAnimating;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009699
9700 boolean tokenMayBeDrawn = false;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009701 boolean wallpaperMayChange = false;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009702 boolean forceHiding = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009703
9704 mPolicy.beginAnimationLw(dw, dh);
9705
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009706 final int N = mWindows.size();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009707
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009708 for (i=N-1; i>=0; i--) {
9709 WindowState w = (WindowState)mWindows.get(i);
9710
9711 final WindowManager.LayoutParams attrs = w.mAttrs;
9712
9713 if (w.mSurface != null) {
9714 // Execute animation.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009715 if (w.commitFinishDrawingLocked(currentTime)) {
9716 if ((w.mAttrs.flags
9717 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009718 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009719 "First draw done in potential wallpaper target " + w);
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009720 wallpaperMayChange = true;
9721 }
9722 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009723
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009724 boolean wasAnimating = w.mAnimating;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009725 if (w.stepAnimationLocked(currentTime, dw, dh)) {
9726 animating = true;
9727 //w.dump(" ");
9728 }
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009729 if (wasAnimating && !w.mAnimating && mWallpaperTarget == w) {
9730 wallpaperMayChange = true;
9731 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009732
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009733 if (mPolicy.doesForceHide(w, attrs)) {
9734 if (!wasAnimating && animating) {
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08009735 if (DEBUG_VISIBILITY) Slog.v(TAG,
9736 "Animation done that could impact force hide: "
9737 + w);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009738 wallpaperForceHidingChanged = true;
Dianne Hackbornb601ce12010-03-01 23:36:02 -08009739 mFocusMayChange = true;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009740 } else if (w.isReadyForDisplay() && w.mAnimation == null) {
9741 forceHiding = true;
9742 }
9743 } else if (mPolicy.canBeForceHidden(w, attrs)) {
9744 boolean changed;
9745 if (forceHiding) {
9746 changed = w.hideLw(false, false);
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08009747 if (DEBUG_VISIBILITY && changed) Slog.v(TAG,
9748 "Now policy hidden: " + w);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009749 } else {
9750 changed = w.showLw(false, false);
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08009751 if (DEBUG_VISIBILITY && changed) Slog.v(TAG,
9752 "Now policy shown: " + w);
9753 if (changed) {
9754 if (wallpaperForceHidingChanged
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009755 && w.isVisibleNow() /*w.isReadyForDisplay()*/) {
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08009756 // Assume we will need to animate. If
9757 // we don't (because the wallpaper will
9758 // stay with the lock screen), then we will
9759 // clean up later.
9760 Animation a = mPolicy.createForceHideEnterAnimation();
9761 if (a != null) {
9762 w.setAnimation(a);
9763 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009764 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08009765 if (mCurrentFocus == null ||
9766 mCurrentFocus.mLayer < w.mLayer) {
9767 // We are showing on to of the current
9768 // focus, so re-evaluate focus to make
9769 // sure it is correct.
9770 mFocusMayChange = true;
9771 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009772 }
9773 }
9774 if (changed && (attrs.flags
9775 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
9776 wallpaperMayChange = true;
9777 }
9778 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009779
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009780 mPolicy.animatingWindowLw(w, attrs);
9781 }
9782
9783 final AppWindowToken atoken = w.mAppToken;
9784 if (atoken != null && (!atoken.allDrawn || atoken.freezingScreen)) {
9785 if (atoken.lastTransactionSequence != transactionSequence) {
9786 atoken.lastTransactionSequence = transactionSequence;
9787 atoken.numInterestingWindows = atoken.numDrawnWindows = 0;
9788 atoken.startingDisplayed = false;
9789 }
9790 if ((w.isOnScreen() || w.mAttrs.type
9791 == WindowManager.LayoutParams.TYPE_BASE_APPLICATION)
9792 && !w.mExiting && !w.mDestroying) {
9793 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009794 Slog.v(TAG, "Eval win " + w + ": isDrawn="
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009795 + w.isDrawnLw()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009796 + ", isAnimating=" + w.isAnimating());
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009797 if (!w.isDrawnLw()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009798 Slog.v(TAG, "Not displayed: s=" + w.mSurface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009799 + " pv=" + w.mPolicyVisibility
9800 + " dp=" + w.mDrawPending
9801 + " cdp=" + w.mCommitDrawPending
9802 + " ah=" + w.mAttachedHidden
9803 + " th=" + atoken.hiddenRequested
9804 + " a=" + w.mAnimating);
9805 }
9806 }
9807 if (w != atoken.startingWindow) {
9808 if (!atoken.freezingScreen || !w.mAppFreezing) {
9809 atoken.numInterestingWindows++;
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009810 if (w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009811 atoken.numDrawnWindows++;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009812 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009813 "tokenMayBeDrawn: " + atoken
9814 + " freezingScreen=" + atoken.freezingScreen
9815 + " mAppFreezing=" + w.mAppFreezing);
9816 tokenMayBeDrawn = true;
9817 }
9818 }
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009819 } else if (w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009820 atoken.startingDisplayed = true;
9821 }
9822 }
9823 } else if (w.mReadyToShow) {
9824 w.performShowLocked();
9825 }
9826 }
9827
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009828 changes |= mPolicy.finishAnimationLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009829
9830 if (tokenMayBeDrawn) {
9831 // See if any windows have been drawn, so they (and others
9832 // associated with them) can now be shown.
9833 final int NT = mTokenList.size();
9834 for (i=0; i<NT; i++) {
9835 AppWindowToken wtoken = mTokenList.get(i).appWindowToken;
9836 if (wtoken == null) {
9837 continue;
9838 }
9839 if (wtoken.freezingScreen) {
9840 int numInteresting = wtoken.numInterestingWindows;
9841 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009842 if (DEBUG_VISIBILITY) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009843 "allDrawn: " + wtoken
9844 + " interesting=" + numInteresting
9845 + " drawn=" + wtoken.numDrawnWindows);
9846 wtoken.showAllWindowsLocked();
9847 unsetAppFreezingScreenLocked(wtoken, false, true);
9848 orientationChangeComplete = true;
9849 }
9850 } else if (!wtoken.allDrawn) {
9851 int numInteresting = wtoken.numInterestingWindows;
9852 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009853 if (DEBUG_VISIBILITY) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009854 "allDrawn: " + wtoken
9855 + " interesting=" + numInteresting
9856 + " drawn=" + wtoken.numDrawnWindows);
9857 wtoken.allDrawn = true;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009858 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_ANIM;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009859
9860 // We can now show all of the drawn windows!
9861 if (!mOpeningApps.contains(wtoken)) {
9862 wtoken.showAllWindowsLocked();
9863 }
9864 }
9865 }
9866 }
9867 }
9868
9869 // If we are ready to perform an app transition, check through
9870 // all of the app tokens to be shown and see if they are ready
9871 // to go.
9872 if (mAppTransitionReady) {
9873 int NN = mOpeningApps.size();
9874 boolean goodToGo = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009875 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009876 "Checking " + NN + " opening apps (frozen="
9877 + mDisplayFrozen + " timeout="
9878 + mAppTransitionTimeout + ")...");
9879 if (!mDisplayFrozen && !mAppTransitionTimeout) {
9880 // If the display isn't frozen, wait to do anything until
9881 // all of the apps are ready. Otherwise just go because
9882 // we'll unfreeze the display when everyone is ready.
9883 for (i=0; i<NN && goodToGo; i++) {
9884 AppWindowToken wtoken = mOpeningApps.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08009885 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009886 "Check opening app" + wtoken + ": allDrawn="
9887 + wtoken.allDrawn + " startingDisplayed="
9888 + wtoken.startingDisplayed);
9889 if (!wtoken.allDrawn && !wtoken.startingDisplayed
9890 && !wtoken.startingMoved) {
9891 goodToGo = false;
9892 }
9893 }
9894 }
9895 if (goodToGo) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009896 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG, "**** GOOD TO GO");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009897 int transit = mNextAppTransition;
9898 if (mSkipAppTransitionAnimation) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009899 transit = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009900 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009901 mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009902 mAppTransitionReady = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009903 mAppTransitionRunning = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009904 mAppTransitionTimeout = false;
9905 mStartingIconInTransition = false;
9906 mSkipAppTransitionAnimation = false;
9907
9908 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
9909
Dianne Hackborna8f60182009-09-01 19:01:50 -07009910 // If there are applications waiting to come to the
9911 // top of the stack, now is the time to move their windows.
9912 // (Note that we don't do apps going to the bottom
9913 // here -- we want to keep their windows in the old
9914 // Z-order until the animation completes.)
9915 if (mToTopApps.size() > 0) {
9916 NN = mAppTokens.size();
9917 for (i=0; i<NN; i++) {
9918 AppWindowToken wtoken = mAppTokens.get(i);
9919 if (wtoken.sendingToTop) {
9920 wtoken.sendingToTop = false;
9921 moveAppWindowsLocked(wtoken, NN, false);
9922 }
9923 }
9924 mToTopApps.clear();
9925 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009926
Dianne Hackborn25994b42009-09-04 14:21:19 -07009927 WindowState oldWallpaper = mWallpaperTarget;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009928
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009929 adjustWallpaperWindowsLocked();
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009930 wallpaperMayChange = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009931
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009932 // The top-most window will supply the layout params,
9933 // and we will determine it below.
9934 LayoutParams animLp = null;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009935 AppWindowToken animToken = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009936 int bestAnimLayer = -1;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009937
Joe Onorato8a9b2202010-02-26 18:56:32 -08009938 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009939 "New wallpaper target=" + mWallpaperTarget
9940 + ", lower target=" + mLowerWallpaperTarget
9941 + ", upper target=" + mUpperWallpaperTarget);
Dianne Hackborn25994b42009-09-04 14:21:19 -07009942 int foundWallpapers = 0;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009943 // Do a first pass through the tokens for two
9944 // things:
9945 // (1) Determine if both the closing and opening
9946 // app token sets are wallpaper targets, in which
9947 // case special animations are needed
9948 // (since the wallpaper needs to stay static
9949 // behind them).
9950 // (2) Find the layout params of the top-most
9951 // application window in the tokens, which is
9952 // what will control the animation theme.
9953 final int NC = mClosingApps.size();
9954 NN = NC + mOpeningApps.size();
9955 for (i=0; i<NN; i++) {
9956 AppWindowToken wtoken;
9957 int mode;
9958 if (i < NC) {
9959 wtoken = mClosingApps.get(i);
9960 mode = 1;
9961 } else {
9962 wtoken = mOpeningApps.get(i-NC);
9963 mode = 2;
9964 }
9965 if (mLowerWallpaperTarget != null) {
9966 if (mLowerWallpaperTarget.mAppToken == wtoken
9967 || mUpperWallpaperTarget.mAppToken == wtoken) {
9968 foundWallpapers |= mode;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009969 }
9970 }
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009971 if (wtoken.appFullscreen) {
9972 WindowState ws = wtoken.findMainWindow();
9973 if (ws != null) {
9974 // If this is a compatibility mode
9975 // window, we will always use its anim.
9976 if ((ws.mAttrs.flags&FLAG_COMPATIBLE_WINDOW) != 0) {
9977 animLp = ws.mAttrs;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009978 animToken = ws.mAppToken;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009979 bestAnimLayer = Integer.MAX_VALUE;
9980 } else if (ws.mLayer > bestAnimLayer) {
9981 animLp = ws.mAttrs;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009982 animToken = ws.mAppToken;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009983 bestAnimLayer = ws.mLayer;
9984 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07009985 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009986 }
9987 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009988
Dianne Hackborn25994b42009-09-04 14:21:19 -07009989 if (foundWallpapers == 3) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009990 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -07009991 "Wallpaper animation!");
9992 switch (transit) {
9993 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
9994 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
9995 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
9996 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN;
9997 break;
9998 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
9999 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
10000 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
10001 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE;
10002 break;
10003 }
Joe Onorato8a9b2202010-02-26 18:56:32 -080010004 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -070010005 "New transit: " + transit);
10006 } else if (oldWallpaper != null) {
10007 // We are transitioning from an activity with
10008 // a wallpaper to one without.
10009 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010010 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -070010011 "New transit away from wallpaper: " + transit);
10012 } else if (mWallpaperTarget != null) {
10013 // We are transitioning from an activity without
10014 // a wallpaper to now showing the wallpaper
10015 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010016 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -070010017 "New transit into wallpaper: " + transit);
10018 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010019
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010020 if ((transit&WindowManagerPolicy.TRANSIT_ENTER_MASK) != 0) {
10021 mLastEnterAnimToken = animToken;
10022 mLastEnterAnimParams = animLp;
10023 } else if (mLastEnterAnimParams != null) {
10024 animLp = mLastEnterAnimParams;
10025 mLastEnterAnimToken = null;
10026 mLastEnterAnimParams = null;
10027 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010028
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010029 // If all closing windows are obscured, then there is
10030 // no need to do an animation. This is the case, for
10031 // example, when this transition is being done behind
10032 // the lock screen.
10033 if (!mPolicy.allowAppAnimationsLw()) {
10034 animLp = null;
10035 }
10036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010037 NN = mOpeningApps.size();
10038 for (i=0; i<NN; i++) {
10039 AppWindowToken wtoken = mOpeningApps.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -080010040 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010041 "Now opening app" + wtoken);
10042 wtoken.reportedVisible = false;
10043 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -070010044 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -070010045 setTokenVisibilityLocked(wtoken, animLp, true, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010046 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -070010047 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010048 wtoken.showAllWindowsLocked();
10049 }
10050 NN = mClosingApps.size();
10051 for (i=0; i<NN; i++) {
10052 AppWindowToken wtoken = mClosingApps.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -080010053 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010054 "Now closing app" + wtoken);
10055 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -070010056 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -070010057 setTokenVisibilityLocked(wtoken, animLp, false, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010058 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -070010059 wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010060 // Force the allDrawn flag, because we want to start
10061 // this guy's animations regardless of whether it's
10062 // gotten drawn.
10063 wtoken.allDrawn = true;
10064 }
10065
Dianne Hackborn8b571a82009-09-25 16:09:43 -070010066 mNextAppTransitionPackage = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010067
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010068 mOpeningApps.clear();
10069 mClosingApps.clear();
10070
10071 // This has changed the visibility of windows, so perform
10072 // a new layout to get them all up-to-date.
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010073 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_LAYOUT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010074 mLayoutNeeded = true;
Dianne Hackborn20583ff2009-07-27 21:51:05 -070010075 if (!moveInputMethodWindowsIfNeededLocked(true)) {
10076 assignLayersLocked();
10077 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010078 updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES);
Dianne Hackbornb601ce12010-03-01 23:36:02 -080010079 mFocusMayChange = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010080 }
10081 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010082
Dianne Hackborn16064f92010-03-25 00:47:24 -070010083 int adjResult = 0;
10084
Dianne Hackborna8f60182009-09-01 19:01:50 -070010085 if (!animating && mAppTransitionRunning) {
10086 // We have finished the animation of an app transition. To do
10087 // this, we have delayed a lot of operations like showing and
10088 // hiding apps, moving apps in Z-order, etc. The app token list
10089 // reflects the correct Z-order, but the window list may now
10090 // be out of sync with it. So here we will just rebuild the
10091 // entire app window list. Fun!
10092 mAppTransitionRunning = false;
10093 // Clear information about apps that were moving.
10094 mToBottomApps.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010095
Dianne Hackborna8f60182009-09-01 19:01:50 -070010096 rebuildAppWindowListLocked();
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010097 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_LAYOUT;
Dianne Hackborn16064f92010-03-25 00:47:24 -070010098 adjResult |= ADJUST_WALLPAPER_LAYERS_CHANGED;
Dianne Hackborna8f60182009-09-01 19:01:50 -070010099 moveInputMethodWindowsIfNeededLocked(false);
10100 wallpaperMayChange = true;
Suchi Amalapurapuc9568e32009-11-05 18:51:16 -080010101 // Since the window list has been rebuilt, focus might
10102 // have to be recomputed since the actual order of windows
10103 // might have changed again.
Dianne Hackbornb601ce12010-03-01 23:36:02 -080010104 mFocusMayChange = true;
Dianne Hackborna8f60182009-09-01 19:01:50 -070010105 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010106
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010107 if (wallpaperForceHidingChanged && changes == 0 && !mAppTransitionReady) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010108 // At this point, there was a window with a wallpaper that
10109 // was force hiding other windows behind it, but now it
10110 // is going away. This may be simple -- just animate
10111 // away the wallpaper and its window -- or it may be
10112 // hard -- the wallpaper now needs to be shown behind
10113 // something that was hidden.
10114 WindowState oldWallpaper = mWallpaperTarget;
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010115 if (mLowerWallpaperTarget != null
10116 && mLowerWallpaperTarget.mAppToken != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010117 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010118 "wallpaperForceHiding changed with lower="
10119 + mLowerWallpaperTarget);
Joe Onorato8a9b2202010-02-26 18:56:32 -080010120 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010121 "hidden=" + mLowerWallpaperTarget.mAppToken.hidden +
10122 " hiddenRequested=" + mLowerWallpaperTarget.mAppToken.hiddenRequested);
10123 if (mLowerWallpaperTarget.mAppToken.hidden) {
10124 // The lower target has become hidden before we
10125 // actually started the animation... let's completely
10126 // re-evaluate everything.
10127 mLowerWallpaperTarget = mUpperWallpaperTarget = null;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010128 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_ANIM;
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010129 }
10130 }
Dianne Hackborn16064f92010-03-25 00:47:24 -070010131 adjResult |= adjustWallpaperWindowsLocked();
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010132 wallpaperMayChange = false;
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010133 wallpaperForceHidingChanged = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010134 if (DEBUG_WALLPAPER) Slog.v(TAG, "****** OLD: " + oldWallpaper
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010135 + " NEW: " + mWallpaperTarget
10136 + " LOWER: " + mLowerWallpaperTarget);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010137 if (mLowerWallpaperTarget == null) {
10138 // Whoops, we don't need a special wallpaper animation.
10139 // Clear them out.
10140 forceHiding = false;
10141 for (i=N-1; i>=0; i--) {
10142 WindowState w = (WindowState)mWindows.get(i);
10143 if (w.mSurface != null) {
10144 final WindowManager.LayoutParams attrs = w.mAttrs;
Suchi Amalapurapuc03d28b2009-10-28 14:32:05 -070010145 if (mPolicy.doesForceHide(w, attrs) && w.isVisibleLw()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010146 if (DEBUG_FOCUS) Slog.i(TAG, "win=" + w + " force hides other windows");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010147 forceHiding = true;
10148 } else if (mPolicy.canBeForceHidden(w, attrs)) {
10149 if (!w.mAnimating) {
10150 // We set the animation above so it
10151 // is not yet running.
10152 w.clearAnimation();
10153 }
10154 }
10155 }
10156 }
10157 }
10158 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010159
Dianne Hackborn6c3f5712009-08-25 18:42:59 -070010160 if (wallpaperMayChange) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010161 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010162 "Wallpaper may change! Adjusting");
Dianne Hackborn16064f92010-03-25 00:47:24 -070010163 adjResult |= adjustWallpaperWindowsLocked();
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010164 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010165
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010166 if ((adjResult&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010167 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010168 "Wallpaper layer changed: assigning layers + relayout");
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010169 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_LAYOUT;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010170 assignLayersLocked();
10171 } else if ((adjResult&ADJUST_WALLPAPER_VISIBILITY_CHANGED) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010172 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010173 "Wallpaper visibility changed: relayout");
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010174 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_LAYOUT;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010175 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010176
Dianne Hackbornb601ce12010-03-01 23:36:02 -080010177 if (mFocusMayChange) {
10178 mFocusMayChange = false;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010179 if (updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES)) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010180 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_ANIM;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010181 adjResult = 0;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -070010182 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010183 }
10184
10185 if (mLayoutNeeded) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010186 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_LAYOUT;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -070010187 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010188
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010189 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG, "*** ANIM STEP: changes=0x"
10190 + Integer.toHexString(changes));
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010191
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010192 } while (changes != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010193
10194 // THIRD LOOP: Update the surfaces of all windows.
10195
10196 final boolean someoneLosingFocus = mLosingFocus.size() != 0;
10197
10198 boolean obscured = false;
10199 boolean blurring = false;
10200 boolean dimming = false;
10201 boolean covered = false;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -070010202 boolean syswin = false;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010203 boolean backgroundFillerShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010204
Dianne Hackbornbdd52b22009-09-02 21:46:19 -070010205 final int N = mWindows.size();
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010207 for (i=N-1; i>=0; i--) {
10208 WindowState w = (WindowState)mWindows.get(i);
10209
10210 boolean displayed = false;
10211 final WindowManager.LayoutParams attrs = w.mAttrs;
10212 final int attrFlags = attrs.flags;
10213
10214 if (w.mSurface != null) {
Dianne Hackbornac3587d2010-03-11 11:12:11 -080010215 // XXX NOTE: The logic here could be improved. We have
10216 // the decision about whether to resize a window separated
10217 // from whether to hide the surface. This can cause us to
10218 // resize a surface even if we are going to hide it. You
10219 // can see this by (1) holding device in landscape mode on
10220 // home screen; (2) tapping browser icon (device will rotate
10221 // to landscape; (3) tap home. The wallpaper will be resized
10222 // in step 2 but then immediately hidden, causing us to
10223 // have to resize and then redraw it again in step 3. It
10224 // would be nice to figure out how to avoid this, but it is
10225 // difficult because we do need to resize surfaces in some
10226 // cases while they are hidden such as when first showing a
10227 // window.
10228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010229 w.computeShownFrameLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -080010230 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010231 TAG, "Placing surface #" + i + " " + w.mSurface
10232 + ": new=" + w.mShownFrame + ", old="
10233 + w.mLastShownFrame);
10234
10235 boolean resize;
10236 int width, height;
10237 if ((w.mAttrs.flags & w.mAttrs.FLAG_SCALED) != 0) {
10238 resize = w.mLastRequestedWidth != w.mRequestedWidth ||
10239 w.mLastRequestedHeight != w.mRequestedHeight;
10240 // for a scaled surface, we just want to use
10241 // the requested size.
10242 width = w.mRequestedWidth;
10243 height = w.mRequestedHeight;
10244 w.mLastRequestedWidth = width;
10245 w.mLastRequestedHeight = height;
10246 w.mLastShownFrame.set(w.mShownFrame);
10247 try {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010248 if (SHOW_TRANSACTIONS) logSurface(w,
10249 "POS " + w.mShownFrame.left
10250 + ", " + w.mShownFrame.top, null);
Dianne Hackborn16064f92010-03-25 00:47:24 -070010251 w.mSurfaceX = w.mShownFrame.left;
10252 w.mSurfaceY = w.mShownFrame.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010253 w.mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
10254 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010255 Slog.w(TAG, "Error positioning surface in " + w, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010256 if (!recoveringMemory) {
10257 reclaimSomeSurfaceMemoryLocked(w, "position");
10258 }
10259 }
10260 } else {
10261 resize = !w.mLastShownFrame.equals(w.mShownFrame);
10262 width = w.mShownFrame.width();
10263 height = w.mShownFrame.height();
10264 w.mLastShownFrame.set(w.mShownFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010265 }
10266
10267 if (resize) {
10268 if (width < 1) width = 1;
10269 if (height < 1) height = 1;
10270 if (w.mSurface != null) {
10271 try {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010272 if (SHOW_TRANSACTIONS) logSurface(w,
10273 "POS " + w.mShownFrame.left + ","
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010274 + w.mShownFrame.top + " SIZE "
10275 + w.mShownFrame.width() + "x"
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010276 + w.mShownFrame.height(), null);
Dianne Hackbornac3587d2010-03-11 11:12:11 -080010277 w.mSurfaceResized = true;
Dianne Hackborn16064f92010-03-25 00:47:24 -070010278 w.mSurfaceW = width;
10279 w.mSurfaceH = height;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010280 w.mSurface.setSize(width, height);
Dianne Hackborn16064f92010-03-25 00:47:24 -070010281 w.mSurfaceX = w.mShownFrame.left;
10282 w.mSurfaceY = w.mShownFrame.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010283 w.mSurface.setPosition(w.mShownFrame.left,
10284 w.mShownFrame.top);
10285 } catch (RuntimeException e) {
10286 // If something goes wrong with the surface (such
10287 // as running out of memory), don't take down the
10288 // entire system.
Joe Onorato8a9b2202010-02-26 18:56:32 -080010289 Slog.e(TAG, "Failure updating surface of " + w
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010290 + "size=(" + width + "x" + height
10291 + "), pos=(" + w.mShownFrame.left
10292 + "," + w.mShownFrame.top + ")", e);
10293 if (!recoveringMemory) {
10294 reclaimSomeSurfaceMemoryLocked(w, "size");
10295 }
10296 }
10297 }
10298 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010299 if (!w.mAppFreezing && w.mLayoutSeq == mLayoutSeq) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010300 w.mContentInsetsChanged =
10301 !w.mLastContentInsets.equals(w.mContentInsets);
10302 w.mVisibleInsetsChanged =
10303 !w.mLastVisibleInsets.equals(w.mVisibleInsets);
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010304 boolean configChanged =
10305 w.mConfiguration != mCurConfiguration
10306 && (w.mConfiguration == null
10307 || mCurConfiguration.diff(w.mConfiguration) != 0);
Dianne Hackborn694f79b2010-03-17 19:44:59 -070010308 if (DEBUG_CONFIGURATION && configChanged) {
10309 Slog.v(TAG, "Win " + w + " config changed: "
10310 + mCurConfiguration);
10311 }
Joe Onorato8a9b2202010-02-26 18:56:32 -080010312 if (localLOGV) Slog.v(TAG, "Resizing " + w
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010313 + ": configChanged=" + configChanged
10314 + " last=" + w.mLastFrame + " frame=" + w.mFrame);
Romain Guy06882f82009-06-10 13:36:04 -070010315 if (!w.mLastFrame.equals(w.mFrame)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010316 || w.mContentInsetsChanged
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010317 || w.mVisibleInsetsChanged
Dianne Hackbornac3587d2010-03-11 11:12:11 -080010318 || w.mSurfaceResized
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010319 || configChanged) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010320 w.mLastFrame.set(w.mFrame);
10321 w.mLastContentInsets.set(w.mContentInsets);
10322 w.mLastVisibleInsets.set(w.mVisibleInsets);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010323 // If the screen is currently frozen, then keep
10324 // it frozen until this window draws at its new
10325 // orientation.
10326 if (mDisplayFrozen) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010327 if (DEBUG_ORIENTATION) Slog.v(TAG,
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010328 "Resizing while display frozen: " + w);
10329 w.mOrientationChanging = true;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010330 if (!mWindowsFreezingScreen) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010331 mWindowsFreezingScreen = true;
10332 // XXX should probably keep timeout from
10333 // when we first froze the display.
10334 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
10335 mH.sendMessageDelayed(mH.obtainMessage(
10336 H.WINDOW_FREEZE_TIMEOUT), 2000);
10337 }
10338 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010339 // If the orientation is changing, then we need to
10340 // hold off on unfreezing the display until this
10341 // window has been redrawn; to do that, we need
10342 // to go through the process of getting informed
10343 // by the application when it has finished drawing.
10344 if (w.mOrientationChanging) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010345 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010346 "Orientation start waiting for draw in "
10347 + w + ", surface " + w.mSurface);
10348 w.mDrawPending = true;
10349 w.mCommitDrawPending = false;
10350 w.mReadyToShow = false;
10351 if (w.mAppToken != null) {
10352 w.mAppToken.allDrawn = false;
10353 }
10354 }
Dianne Hackbornac3587d2010-03-11 11:12:11 -080010355 if (DEBUG_RESIZE || DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010356 "Resizing window " + w + " to " + w.mFrame);
10357 mResizingWindows.add(w);
10358 } else if (w.mOrientationChanging) {
10359 if (!w.mDrawPending && !w.mCommitDrawPending) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010360 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010361 "Orientation not waiting for draw in "
10362 + w + ", surface " + w.mSurface);
10363 w.mOrientationChanging = false;
10364 }
10365 }
10366 }
10367
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010368 if (w.mAttachedHidden || !w.isReadyForDisplay()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010369 if (!w.mLastHidden) {
10370 //dump();
Dianne Hackborn5943c202010-04-12 21:36:49 -070010371 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Window hiding: waitingToShow="
10372 + w.mRootToken.waitingToShow + " polvis="
10373 + w.mPolicyVisibility + " atthid="
10374 + w.mAttachedHidden + " tokhid="
10375 + w.mRootToken.hidden + " vis="
10376 + w.mViewVisibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010377 w.mLastHidden = true;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010378 if (SHOW_TRANSACTIONS) logSurface(w,
10379 "HIDE (performLayout)", null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010380 if (w.mSurface != null) {
Dianne Hackborn16064f92010-03-25 00:47:24 -070010381 w.mSurfaceShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010382 try {
10383 w.mSurface.hide();
10384 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010385 Slog.w(TAG, "Exception hiding surface in " + w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010386 }
10387 }
10388 mKeyWaiter.releasePendingPointerLocked(w.mSession);
10389 }
10390 // If we are waiting for this window to handle an
10391 // orientation change, well, it is hidden, so
10392 // doesn't really matter. Note that this does
10393 // introduce a potential glitch if the window
10394 // becomes unhidden before it has drawn for the
10395 // new orientation.
10396 if (w.mOrientationChanging) {
10397 w.mOrientationChanging = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010398 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010399 "Orientation change skips hidden " + w);
10400 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010401 } else if (w.mLastLayer != w.mAnimLayer
10402 || w.mLastAlpha != w.mShownAlpha
10403 || w.mLastDsDx != w.mDsDx
10404 || w.mLastDtDx != w.mDtDx
10405 || w.mLastDsDy != w.mDsDy
10406 || w.mLastDtDy != w.mDtDy
10407 || w.mLastHScale != w.mHScale
10408 || w.mLastVScale != w.mVScale
10409 || w.mLastHidden) {
10410 displayed = true;
10411 w.mLastAlpha = w.mShownAlpha;
10412 w.mLastLayer = w.mAnimLayer;
10413 w.mLastDsDx = w.mDsDx;
10414 w.mLastDtDx = w.mDtDx;
10415 w.mLastDsDy = w.mDsDy;
10416 w.mLastDtDy = w.mDtDy;
10417 w.mLastHScale = w.mHScale;
10418 w.mLastVScale = w.mVScale;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010419 if (SHOW_TRANSACTIONS) logSurface(w,
10420 "alpha=" + w.mShownAlpha + " layer=" + w.mAnimLayer
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010421 + " matrix=[" + (w.mDsDx*w.mHScale)
10422 + "," + (w.mDtDx*w.mVScale)
10423 + "][" + (w.mDsDy*w.mHScale)
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010424 + "," + (w.mDtDy*w.mVScale) + "]", null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010425 if (w.mSurface != null) {
10426 try {
Dianne Hackborn16064f92010-03-25 00:47:24 -070010427 w.mSurfaceAlpha = w.mShownAlpha;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010428 w.mSurface.setAlpha(w.mShownAlpha);
Dianne Hackborn16064f92010-03-25 00:47:24 -070010429 w.mSurfaceLayer = w.mAnimLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010430 w.mSurface.setLayer(w.mAnimLayer);
10431 w.mSurface.setMatrix(
10432 w.mDsDx*w.mHScale, w.mDtDx*w.mVScale,
10433 w.mDsDy*w.mHScale, w.mDtDy*w.mVScale);
10434 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010435 Slog.w(TAG, "Error updating surface in " + w, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010436 if (!recoveringMemory) {
10437 reclaimSomeSurfaceMemoryLocked(w, "update");
10438 }
10439 }
10440 }
10441
10442 if (w.mLastHidden && !w.mDrawPending
10443 && !w.mCommitDrawPending
10444 && !w.mReadyToShow) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010445 if (SHOW_TRANSACTIONS) logSurface(w,
10446 "SHOW (performLayout)", null);
Joe Onorato8a9b2202010-02-26 18:56:32 -080010447 if (DEBUG_VISIBILITY) Slog.v(TAG, "Showing " + w
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010448 + " during relayout");
10449 if (showSurfaceRobustlyLocked(w)) {
10450 w.mHasDrawn = true;
10451 w.mLastHidden = false;
10452 } else {
10453 w.mOrientationChanging = false;
10454 }
10455 }
10456 if (w.mSurface != null) {
10457 w.mToken.hasVisible = true;
10458 }
10459 } else {
10460 displayed = true;
10461 }
10462
10463 if (displayed) {
10464 if (!covered) {
Romain Guy980a9382010-01-08 15:06:28 -080010465 if (attrs.width == LayoutParams.MATCH_PARENT
10466 && attrs.height == LayoutParams.MATCH_PARENT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010467 covered = true;
10468 }
10469 }
10470 if (w.mOrientationChanging) {
10471 if (w.mDrawPending || w.mCommitDrawPending) {
10472 orientationChangeComplete = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010473 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010474 "Orientation continue waiting for draw in " + w);
10475 } else {
10476 w.mOrientationChanging = 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 change complete in " + w);
10479 }
10480 }
10481 w.mToken.hasVisible = true;
10482 }
10483 } else if (w.mOrientationChanging) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010484 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010485 "Orientation change skips hidden " + w);
10486 w.mOrientationChanging = false;
10487 }
10488
10489 final boolean canBeSeen = w.isDisplayedLw();
10490
10491 if (someoneLosingFocus && w == mCurrentFocus && canBeSeen) {
10492 focusDisplayed = true;
10493 }
10494
Dianne Hackborne9e9bca2009-08-18 15:08:22 -070010495 final boolean obscuredChanged = w.mObscured != obscured;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010497 // Update effect.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010498 if (!(w.mObscured=obscured)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010499 if (w.mSurface != null) {
10500 if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
10501 holdScreen = w.mSession;
10502 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -070010503 if (!syswin && w.mAttrs.screenBrightness >= 0
10504 && screenBrightness < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010505 screenBrightness = w.mAttrs.screenBrightness;
10506 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -050010507 if (!syswin && w.mAttrs.buttonBrightness >= 0
10508 && buttonBrightness < 0) {
10509 buttonBrightness = w.mAttrs.buttonBrightness;
10510 }
Mike Lockwood46af6a82010-03-09 08:28:22 -050010511 if (canBeSeen
10512 && (attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG
10513 || attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD
10514 || attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_ERROR)) {
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -070010515 syswin = true;
10516 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010517 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010518
Dianne Hackborn25994b42009-09-04 14:21:19 -070010519 boolean opaqueDrawn = canBeSeen && w.isOpaqueDrawn();
10520 if (opaqueDrawn && w.isFullscreen(dw, dh)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010521 // This window completely covers everything behind it,
10522 // so we want to leave all of them as unblurred (for
10523 // performance reasons).
10524 obscured = true;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010525 } else if (opaqueDrawn && w.needsBackgroundFiller(dw, dh)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010526 if (SHOW_TRANSACTIONS) Slog.d(TAG, "showing background filler");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010527 // This window is in compatibility mode, and needs background filler.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010528 obscured = true;
10529 if (mBackgroundFillerSurface == null) {
10530 try {
10531 mBackgroundFillerSurface = new Surface(mFxSession, 0,
Mathias Agopian5d26c1e2010-03-01 16:09:43 -080010532 "BackGroundFiller",
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010533 0, dw, dh,
10534 PixelFormat.OPAQUE,
10535 Surface.FX_SURFACE_NORMAL);
10536 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010537 Slog.e(TAG, "Exception creating filler surface", e);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010538 }
10539 }
10540 try {
10541 mBackgroundFillerSurface.setPosition(0, 0);
10542 mBackgroundFillerSurface.setSize(dw, dh);
10543 // Using the same layer as Dim because they will never be shown at the
10544 // same time.
10545 mBackgroundFillerSurface.setLayer(w.mAnimLayer - 1);
10546 mBackgroundFillerSurface.show();
10547 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010548 Slog.e(TAG, "Exception showing filler surface");
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010549 }
10550 backgroundFillerShown = true;
10551 mBackgroundFillerShown = true;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010552 } else if (canBeSeen && !obscured &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010553 (attrFlags&FLAG_BLUR_BEHIND|FLAG_DIM_BEHIND) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010554 if (localLOGV) Slog.v(TAG, "Win " + w
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010555 + ": blurring=" + blurring
10556 + " obscured=" + obscured
10557 + " displayed=" + displayed);
10558 if ((attrFlags&FLAG_DIM_BEHIND) != 0) {
10559 if (!dimming) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010560 //Slog.i(TAG, "DIM BEHIND: " + w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010561 dimming = true;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010562 if (mDimAnimator == null) {
10563 mDimAnimator = new DimAnimator(mFxSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010564 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010565 mDimAnimator.show(dw, dh);
Dianne Hackborn16064f92010-03-25 00:47:24 -070010566 mDimAnimator.updateParameters(w, currentTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010567 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010568 }
10569 if ((attrFlags&FLAG_BLUR_BEHIND) != 0) {
10570 if (!blurring) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010571 //Slog.i(TAG, "BLUR BEHIND: " + w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010572 blurring = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010573 if (mBlurSurface == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010574 if (SHOW_TRANSACTIONS) Slog.i(TAG, " BLUR "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010575 + mBlurSurface + ": CREATE");
10576 try {
Romain Guy06882f82009-06-10 13:36:04 -070010577 mBlurSurface = new Surface(mFxSession, 0,
Mathias Agopian5d26c1e2010-03-01 16:09:43 -080010578 "BlurSurface",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010579 -1, 16, 16,
10580 PixelFormat.OPAQUE,
10581 Surface.FX_SURFACE_BLUR);
10582 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010583 Slog.e(TAG, "Exception creating Blur surface", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010584 }
10585 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010586 if (mBlurSurface != null) {
Dianne Hackborn16064f92010-03-25 00:47:24 -070010587 if (SHOW_TRANSACTIONS) Slog.i(TAG, " BLUR "
10588 + mBlurSurface + ": pos=(0,0) (" +
10589 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010590 mBlurSurface.setPosition(0, 0);
10591 mBlurSurface.setSize(dw, dh);
Dianne Hackborn16064f92010-03-25 00:47:24 -070010592 mBlurSurface.setLayer(w.mAnimLayer-2);
10593 if (!mBlurShown) {
10594 try {
10595 if (SHOW_TRANSACTIONS) Slog.i(TAG, " BLUR "
10596 + mBlurSurface + ": SHOW");
10597 mBlurSurface.show();
10598 } catch (RuntimeException e) {
10599 Slog.w(TAG, "Failure showing blur surface", e);
10600 }
10601 mBlurShown = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010602 }
10603 }
10604 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010605 }
10606 }
10607 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010608
Dianne Hackborne9e9bca2009-08-18 15:08:22 -070010609 if (obscuredChanged && mWallpaperTarget == w) {
10610 // This is the wallpaper target and its obscured state
10611 // changed... make sure the current wallaper's visibility
10612 // has been updated accordingly.
10613 updateWallpaperVisibilityLocked();
10614 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010615 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010616
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010617 if (backgroundFillerShown == false && mBackgroundFillerShown) {
10618 mBackgroundFillerShown = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010619 if (SHOW_TRANSACTIONS) Slog.d(TAG, "hiding background filler");
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010620 try {
10621 mBackgroundFillerSurface.hide();
10622 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010623 Slog.e(TAG, "Exception hiding filler surface", e);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010624 }
10625 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010626
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010627 if (mDimAnimator != null && mDimAnimator.mDimShown) {
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010628 animating |= mDimAnimator.updateSurface(dimming, currentTime,
10629 mDisplayFrozen || !mPolicy.isScreenOn());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010630 }
Romain Guy06882f82009-06-10 13:36:04 -070010631
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010632 if (!blurring && mBlurShown) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010633 if (SHOW_TRANSACTIONS) Slog.i(TAG, " BLUR " + mBlurSurface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010634 + ": HIDE");
10635 try {
10636 mBlurSurface.hide();
10637 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010638 Slog.w(TAG, "Illegal argument exception hiding blur surface");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010639 }
10640 mBlurShown = false;
10641 }
10642
Joe Onorato8a9b2202010-02-26 18:56:32 -080010643 if (SHOW_TRANSACTIONS) Slog.i(TAG, "<<< CLOSE TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010644 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010645 Slog.e(TAG, "Unhandled exception in Window Manager", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010646 }
10647
10648 Surface.closeTransaction();
Romain Guy06882f82009-06-10 13:36:04 -070010649
Joe Onorato8a9b2202010-02-26 18:56:32 -080010650 if (DEBUG_ORIENTATION && mDisplayFrozen) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010651 "With display frozen, orientationChangeComplete="
10652 + orientationChangeComplete);
10653 if (orientationChangeComplete) {
10654 if (mWindowsFreezingScreen) {
10655 mWindowsFreezingScreen = false;
10656 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
10657 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010658 stopFreezingDisplayLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010659 }
Romain Guy06882f82009-06-10 13:36:04 -070010660
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010661 i = mResizingWindows.size();
10662 if (i > 0) {
10663 do {
10664 i--;
10665 WindowState win = mResizingWindows.get(i);
10666 try {
Dianne Hackbornac3587d2010-03-11 11:12:11 -080010667 if (DEBUG_RESIZE || DEBUG_ORIENTATION) Slog.v(TAG,
10668 "Reporting new frame to " + win + ": " + win.mFrame);
Dianne Hackborn694f79b2010-03-17 19:44:59 -070010669 int diff = 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010670 boolean configChanged =
10671 win.mConfiguration != mCurConfiguration
10672 && (win.mConfiguration == null
Dianne Hackborn694f79b2010-03-17 19:44:59 -070010673 || (diff=mCurConfiguration.diff(win.mConfiguration)) != 0);
10674 if ((DEBUG_RESIZE || DEBUG_ORIENTATION || DEBUG_CONFIGURATION)
10675 && configChanged) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010676 Slog.i(TAG, "Sending new config to window " + win + ": "
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010677 + win.mFrame.width() + "x" + win.mFrame.height()
Dianne Hackborn694f79b2010-03-17 19:44:59 -070010678 + " / " + mCurConfiguration + " / 0x"
10679 + Integer.toHexString(diff));
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010680 }
Dianne Hackborn694f79b2010-03-17 19:44:59 -070010681 win.mConfiguration = mCurConfiguration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010682 win.mClient.resized(win.mFrame.width(),
10683 win.mFrame.height(), win.mLastContentInsets,
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010684 win.mLastVisibleInsets, win.mDrawPending,
10685 configChanged ? win.mConfiguration : null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010686 win.mContentInsetsChanged = false;
10687 win.mVisibleInsetsChanged = false;
Dianne Hackbornac3587d2010-03-11 11:12:11 -080010688 win.mSurfaceResized = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010689 } catch (RemoteException e) {
10690 win.mOrientationChanging = false;
10691 }
10692 } while (i > 0);
10693 mResizingWindows.clear();
10694 }
Romain Guy06882f82009-06-10 13:36:04 -070010695
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010696 // Destroy the surface of any windows that are no longer visible.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010697 boolean wallpaperDestroyed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010698 i = mDestroySurface.size();
10699 if (i > 0) {
10700 do {
10701 i--;
10702 WindowState win = mDestroySurface.get(i);
10703 win.mDestroying = false;
10704 if (mInputMethodWindow == win) {
10705 mInputMethodWindow = null;
10706 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010707 if (win == mWallpaperTarget) {
10708 wallpaperDestroyed = true;
10709 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010710 win.destroySurfaceLocked();
10711 } while (i > 0);
10712 mDestroySurface.clear();
10713 }
10714
10715 // Time to remove any exiting tokens?
10716 for (i=mExitingTokens.size()-1; i>=0; i--) {
10717 WindowToken token = mExitingTokens.get(i);
10718 if (!token.hasVisible) {
10719 mExitingTokens.remove(i);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070010720 if (token.windowType == TYPE_WALLPAPER) {
10721 mWallpaperTokens.remove(token);
10722 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010723 }
10724 }
10725
10726 // Time to remove any exiting applications?
10727 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
10728 AppWindowToken token = mExitingAppTokens.get(i);
10729 if (!token.hasVisible && !mClosingApps.contains(token)) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -070010730 // Make sure there is no animation running on this token,
10731 // so any windows associated with it will be removed as
10732 // soon as their animations are complete
10733 token.animation = null;
10734 token.animating = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010735 mAppTokens.remove(token);
10736 mExitingAppTokens.remove(i);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010737 if (mLastEnterAnimToken == token) {
10738 mLastEnterAnimToken = null;
10739 mLastEnterAnimParams = null;
10740 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010741 }
10742 }
10743
Dianne Hackborna8f60182009-09-01 19:01:50 -070010744 boolean needRelayout = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010745
Dianne Hackborna8f60182009-09-01 19:01:50 -070010746 if (!animating && mAppTransitionRunning) {
10747 // We have finished the animation of an app transition. To do
10748 // this, we have delayed a lot of operations like showing and
10749 // hiding apps, moving apps in Z-order, etc. The app token list
10750 // reflects the correct Z-order, but the window list may now
10751 // be out of sync with it. So here we will just rebuild the
10752 // entire app window list. Fun!
10753 mAppTransitionRunning = false;
10754 needRelayout = true;
10755 rebuildAppWindowListLocked();
Dianne Hackborn16064f92010-03-25 00:47:24 -070010756 assignLayersLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -070010757 // Clear information about apps that were moving.
10758 mToBottomApps.clear();
10759 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010760
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010761 if (focusDisplayed) {
10762 mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
10763 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010764 if (wallpaperDestroyed) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010765 needRelayout = adjustWallpaperWindowsLocked() != 0;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010766 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070010767 if (needRelayout) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010768 requestAnimationLocked(0);
10769 } else if (animating) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010770 requestAnimationLocked(currentTime+(1000/60)-SystemClock.uptimeMillis());
10771 }
Dianne Hackbornce73c1e2010-04-12 23:11:38 -070010772
10773 if (DEBUG_FREEZE) Slog.v(TAG, "Layout: mDisplayFrozen=" + mDisplayFrozen
10774 + " holdScreen=" + holdScreen);
10775 if (!mDisplayFrozen) {
10776 mQueue.setHoldScreenLocked(holdScreen != null);
10777 if (screenBrightness < 0 || screenBrightness > 1.0f) {
10778 mPowerManager.setScreenBrightnessOverride(-1);
10779 } else {
10780 mPowerManager.setScreenBrightnessOverride((int)
10781 (screenBrightness * Power.BRIGHTNESS_ON));
10782 }
10783 if (buttonBrightness < 0 || buttonBrightness > 1.0f) {
10784 mPowerManager.setButtonBrightnessOverride(-1);
10785 } else {
10786 mPowerManager.setButtonBrightnessOverride((int)
10787 (buttonBrightness * Power.BRIGHTNESS_ON));
10788 }
10789 if (holdScreen != mHoldingScreenOn) {
10790 mHoldingScreenOn = holdScreen;
10791 Message m = mH.obtainMessage(H.HOLD_SCREEN_CHANGED, holdScreen);
10792 mH.sendMessage(m);
10793 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010794 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010795
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010796 if (mTurnOnScreen) {
Dianne Hackbornb601ce12010-03-01 23:36:02 -080010797 if (DEBUG_VISIBILITY) Slog.v(TAG, "Turning screen on after layout!");
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010798 mPowerManager.userActivity(SystemClock.uptimeMillis(), false,
10799 LocalPowerManager.BUTTON_EVENT, true);
10800 mTurnOnScreen = false;
10801 }
Dianne Hackbornf3bea9c2009-12-09 18:26:21 -080010802
10803 // Check to see if we are now in a state where the screen should
10804 // be enabled, because the window obscured flags have changed.
10805 enableScreenIfNeededLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010806 }
10807
10808 void requestAnimationLocked(long delay) {
10809 if (!mAnimationPending) {
10810 mAnimationPending = true;
10811 mH.sendMessageDelayed(mH.obtainMessage(H.ANIMATE), delay);
10812 }
10813 }
Romain Guy06882f82009-06-10 13:36:04 -070010814
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010815 /**
10816 * Have the surface flinger show a surface, robustly dealing with
10817 * error conditions. In particular, if there is not enough memory
10818 * to show the surface, then we will try to get rid of other surfaces
10819 * in order to succeed.
Romain Guy06882f82009-06-10 13:36:04 -070010820 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010821 * @return Returns true if the surface was successfully shown.
10822 */
10823 boolean showSurfaceRobustlyLocked(WindowState win) {
10824 try {
10825 if (win.mSurface != null) {
Dianne Hackborn16064f92010-03-25 00:47:24 -070010826 win.mSurfaceShown = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010827 win.mSurface.show();
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010828 if (win.mTurnOnScreen) {
Dianne Hackbornb601ce12010-03-01 23:36:02 -080010829 if (DEBUG_VISIBILITY) Slog.v(TAG,
10830 "Show surface turning screen on: " + win);
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010831 win.mTurnOnScreen = false;
10832 mTurnOnScreen = true;
10833 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010834 }
10835 return true;
10836 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010837 Slog.w(TAG, "Failure showing surface " + win.mSurface + " in " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010838 }
Romain Guy06882f82009-06-10 13:36:04 -070010839
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010840 reclaimSomeSurfaceMemoryLocked(win, "show");
Romain Guy06882f82009-06-10 13:36:04 -070010841
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010842 return false;
10843 }
Romain Guy06882f82009-06-10 13:36:04 -070010844
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010845 void reclaimSomeSurfaceMemoryLocked(WindowState win, String operation) {
10846 final Surface surface = win.mSurface;
Romain Guy06882f82009-06-10 13:36:04 -070010847
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010848 EventLog.writeEvent(EventLogTags.WM_NO_SURFACE_MEMORY, win.toString(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010849 win.mSession.mPid, operation);
Romain Guy06882f82009-06-10 13:36:04 -070010850
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010851 if (mForceRemoves == null) {
10852 mForceRemoves = new ArrayList<WindowState>();
10853 }
Romain Guy06882f82009-06-10 13:36:04 -070010854
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010855 long callingIdentity = Binder.clearCallingIdentity();
10856 try {
10857 // There was some problem... first, do a sanity check of the
10858 // window list to make sure we haven't left any dangling surfaces
10859 // around.
10860 int N = mWindows.size();
10861 boolean leakedSurface = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010862 Slog.i(TAG, "Out of memory for surface! Looking for leaks...");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010863 for (int i=0; i<N; i++) {
10864 WindowState ws = (WindowState)mWindows.get(i);
10865 if (ws.mSurface != null) {
10866 if (!mSessions.contains(ws.mSession)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010867 Slog.w(TAG, "LEAKED SURFACE (session doesn't exist): "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010868 + ws + " surface=" + ws.mSurface
10869 + " token=" + win.mToken
10870 + " pid=" + ws.mSession.mPid
10871 + " uid=" + ws.mSession.mUid);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010872 ws.mSurface.destroy();
Dianne Hackborn16064f92010-03-25 00:47:24 -070010873 ws.mSurfaceShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010874 ws.mSurface = null;
10875 mForceRemoves.add(ws);
10876 i--;
10877 N--;
10878 leakedSurface = true;
10879 } else if (win.mAppToken != null && win.mAppToken.clientHidden) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010880 Slog.w(TAG, "LEAKED SURFACE (app token hidden): "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010881 + ws + " surface=" + ws.mSurface
10882 + " token=" + win.mAppToken);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010883 ws.mSurface.destroy();
Dianne Hackborn16064f92010-03-25 00:47:24 -070010884 ws.mSurfaceShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010885 ws.mSurface = null;
10886 leakedSurface = true;
10887 }
10888 }
10889 }
Romain Guy06882f82009-06-10 13:36:04 -070010890
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010891 boolean killedApps = false;
10892 if (!leakedSurface) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010893 Slog.w(TAG, "No leaked surfaces; killing applicatons!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010894 SparseIntArray pidCandidates = new SparseIntArray();
10895 for (int i=0; i<N; i++) {
10896 WindowState ws = (WindowState)mWindows.get(i);
10897 if (ws.mSurface != null) {
10898 pidCandidates.append(ws.mSession.mPid, ws.mSession.mPid);
10899 }
10900 }
10901 if (pidCandidates.size() > 0) {
10902 int[] pids = new int[pidCandidates.size()];
10903 for (int i=0; i<pids.length; i++) {
10904 pids[i] = pidCandidates.keyAt(i);
10905 }
10906 try {
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -070010907 if (mActivityManager.killPids(pids, "Free memory")) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010908 killedApps = true;
10909 }
10910 } catch (RemoteException e) {
10911 }
10912 }
10913 }
Romain Guy06882f82009-06-10 13:36:04 -070010914
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010915 if (leakedSurface || killedApps) {
10916 // We managed to reclaim some memory, so get rid of the trouble
10917 // surface and ask the app to request another one.
Joe Onorato8a9b2202010-02-26 18:56:32 -080010918 Slog.w(TAG, "Looks like we have reclaimed some memory, clearing surface for retry.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010919 if (surface != null) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010920 surface.destroy();
Dianne Hackborn16064f92010-03-25 00:47:24 -070010921 win.mSurfaceShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010922 win.mSurface = null;
10923 }
Romain Guy06882f82009-06-10 13:36:04 -070010924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010925 try {
10926 win.mClient.dispatchGetNewSurface();
10927 } catch (RemoteException e) {
10928 }
10929 }
10930 } finally {
10931 Binder.restoreCallingIdentity(callingIdentity);
10932 }
10933 }
Romain Guy06882f82009-06-10 13:36:04 -070010934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010935 private boolean updateFocusedWindowLocked(int mode) {
10936 WindowState newFocus = computeFocusedWindowLocked();
10937 if (mCurrentFocus != newFocus) {
10938 // This check makes sure that we don't already have the focus
10939 // change message pending.
10940 mH.removeMessages(H.REPORT_FOCUS_CHANGE);
10941 mH.sendEmptyMessage(H.REPORT_FOCUS_CHANGE);
Joe Onorato8a9b2202010-02-26 18:56:32 -080010942 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010943 TAG, "Changing focus from " + mCurrentFocus + " to " + newFocus);
10944 final WindowState oldFocus = mCurrentFocus;
10945 mCurrentFocus = newFocus;
10946 mLosingFocus.remove(newFocus);
Romain Guy06882f82009-06-10 13:36:04 -070010947
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010948 final WindowState imWindow = mInputMethodWindow;
10949 if (newFocus != imWindow && oldFocus != imWindow) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010950 if (moveInputMethodWindowsIfNeededLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010951 mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS &&
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010952 mode != UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
10953 mLayoutNeeded = true;
10954 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010955 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
10956 performLayoutLockedInner();
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010957 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
10958 // Client will do the layout, but we need to assign layers
10959 // for handleNewWindowLocked() below.
10960 assignLayersLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010961 }
10962 }
Romain Guy06882f82009-06-10 13:36:04 -070010963
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010964 if (newFocus != null && mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
10965 mKeyWaiter.handleNewWindowLocked(newFocus);
10966 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010967 return true;
10968 }
10969 return false;
10970 }
10971
10972 private WindowState computeFocusedWindowLocked() {
10973 WindowState result = null;
10974 WindowState win;
10975
10976 int i = mWindows.size() - 1;
10977 int nextAppIndex = mAppTokens.size()-1;
10978 WindowToken nextApp = nextAppIndex >= 0
10979 ? mAppTokens.get(nextAppIndex) : null;
10980
10981 while (i >= 0) {
10982 win = (WindowState)mWindows.get(i);
10983
Joe Onorato8a9b2202010-02-26 18:56:32 -080010984 if (localLOGV || DEBUG_FOCUS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010985 TAG, "Looking for focus: " + i
10986 + " = " + win
10987 + ", flags=" + win.mAttrs.flags
10988 + ", canReceive=" + win.canReceiveKeys());
10989
10990 AppWindowToken thisApp = win.mAppToken;
Romain Guy06882f82009-06-10 13:36:04 -070010991
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010992 // If this window's application has been removed, just skip it.
10993 if (thisApp != null && thisApp.removed) {
10994 i--;
10995 continue;
10996 }
Romain Guy06882f82009-06-10 13:36:04 -070010997
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010998 // If there is a focused app, don't allow focus to go to any
10999 // windows below it. If this is an application window, step
11000 // through the app tokens until we find its app.
11001 if (thisApp != null && nextApp != null && thisApp != nextApp
11002 && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
11003 int origAppIndex = nextAppIndex;
11004 while (nextAppIndex > 0) {
11005 if (nextApp == mFocusedApp) {
11006 // Whoops, we are below the focused app... no focus
11007 // for you!
Joe Onorato8a9b2202010-02-26 18:56:32 -080011008 if (localLOGV || DEBUG_FOCUS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011009 TAG, "Reached focused app: " + mFocusedApp);
11010 return null;
11011 }
11012 nextAppIndex--;
11013 nextApp = mAppTokens.get(nextAppIndex);
11014 if (nextApp == thisApp) {
11015 break;
11016 }
11017 }
11018 if (thisApp != nextApp) {
11019 // Uh oh, the app token doesn't exist! This shouldn't
11020 // happen, but if it does we can get totally hosed...
11021 // so restart at the original app.
11022 nextAppIndex = origAppIndex;
11023 nextApp = mAppTokens.get(nextAppIndex);
11024 }
11025 }
11026
11027 // Dispatch to this window if it is wants key events.
11028 if (win.canReceiveKeys()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011029 if (DEBUG_FOCUS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011030 TAG, "Found focus @ " + i + " = " + win);
11031 result = win;
11032 break;
11033 }
11034
11035 i--;
11036 }
11037
11038 return result;
11039 }
11040
11041 private void startFreezingDisplayLocked() {
11042 if (mDisplayFrozen) {
Chris Tate2ad63a92009-03-25 17:36:48 -070011043 // Freezing the display also suspends key event delivery, to
11044 // keep events from going astray while the display is reconfigured.
11045 // If someone has changed orientation again while the screen is
11046 // still frozen, the events will continue to be blocked while the
11047 // successive orientation change is processed. To prevent spurious
11048 // ANRs, we reset the event dispatch timeout in this case.
11049 synchronized (mKeyWaiter) {
11050 mKeyWaiter.mWasFrozen = true;
11051 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011052 return;
11053 }
Romain Guy06882f82009-06-10 13:36:04 -070011054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011055 mScreenFrozenLock.acquire();
Romain Guy06882f82009-06-10 13:36:04 -070011056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011057 long now = SystemClock.uptimeMillis();
Joe Onorato8a9b2202010-02-26 18:56:32 -080011058 //Slog.i(TAG, "Freezing, gc pending: " + mFreezeGcPending + ", now " + now);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011059 if (mFreezeGcPending != 0) {
11060 if (now > (mFreezeGcPending+1000)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011061 //Slog.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011062 mH.removeMessages(H.FORCE_GC);
11063 Runtime.getRuntime().gc();
11064 mFreezeGcPending = now;
11065 }
11066 } else {
11067 mFreezeGcPending = now;
11068 }
Romain Guy06882f82009-06-10 13:36:04 -070011069
Dianne Hackbornce73c1e2010-04-12 23:11:38 -070011070 if (DEBUG_FREEZE) Slog.v(TAG, "*** FREEZING DISPLAY", new RuntimeException());
11071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011072 mDisplayFrozen = true;
Dianne Hackbornbfe319e2009-09-21 00:34:05 -070011073 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
11074 mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070011075 mNextAppTransitionPackage = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011076 mAppTransitionReady = true;
11077 }
Romain Guy06882f82009-06-10 13:36:04 -070011078
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011079 if (PROFILE_ORIENTATION) {
11080 File file = new File("/data/system/frozen");
11081 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
11082 }
11083 Surface.freezeDisplay(0);
11084 }
Romain Guy06882f82009-06-10 13:36:04 -070011085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011086 private void stopFreezingDisplayLocked() {
11087 if (!mDisplayFrozen) {
11088 return;
11089 }
Romain Guy06882f82009-06-10 13:36:04 -070011090
Dianne Hackborne36d6e22010-02-17 19:46:25 -080011091 if (mWaitingForConfig || mAppsFreezingScreen > 0 || mWindowsFreezingScreen) {
11092 return;
11093 }
11094
Dianne Hackbornce73c1e2010-04-12 23:11:38 -070011095 if (DEBUG_FREEZE) Slog.v(TAG, "*** UNFREEZING DISPLAY", new RuntimeException());
11096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011097 mDisplayFrozen = false;
11098 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
11099 if (PROFILE_ORIENTATION) {
11100 Debug.stopMethodTracing();
11101 }
11102 Surface.unfreezeDisplay(0);
Romain Guy06882f82009-06-10 13:36:04 -070011103
Chris Tate2ad63a92009-03-25 17:36:48 -070011104 // Reset the key delivery timeout on unfreeze, too. We force a wakeup here
11105 // too because regular key delivery processing should resume immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011106 synchronized (mKeyWaiter) {
11107 mKeyWaiter.mWasFrozen = true;
11108 mKeyWaiter.notifyAll();
11109 }
11110
Christopher Tateb696aee2010-04-02 19:08:30 -070011111 // While the display is frozen we don't re-compute the orientation
11112 // to avoid inconsistent states. However, something interesting
11113 // could have actually changed during that time so re-evaluate it
11114 // now to catch that.
11115 if (updateOrientationFromAppTokensLocked()) {
11116 mH.sendEmptyMessage(H.SEND_NEW_CONFIGURATION);
11117 }
11118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011119 // A little kludge: a lot could have happened while the
11120 // display was frozen, so now that we are coming back we
11121 // do a gc so that any remote references the system
11122 // processes holds on others can be released if they are
11123 // no longer needed.
11124 mH.removeMessages(H.FORCE_GC);
11125 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
11126 2000);
Romain Guy06882f82009-06-10 13:36:04 -070011127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011128 mScreenFrozenLock.release();
11129 }
Romain Guy06882f82009-06-10 13:36:04 -070011130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011131 @Override
11132 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
11133 if (mContext.checkCallingOrSelfPermission("android.permission.DUMP")
11134 != PackageManager.PERMISSION_GRANTED) {
11135 pw.println("Permission Denial: can't dump WindowManager from from pid="
11136 + Binder.getCallingPid()
11137 + ", uid=" + Binder.getCallingUid());
11138 return;
11139 }
Romain Guy06882f82009-06-10 13:36:04 -070011140
Dianne Hackborna2e92262010-03-02 17:19:29 -080011141 pw.println("Input State:");
11142 mQueue.dump(pw, " ");
11143 pw.println(" ");
11144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011145 synchronized(mWindowMap) {
11146 pw.println("Current Window Manager state:");
11147 for (int i=mWindows.size()-1; i>=0; i--) {
11148 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011149 pw.print(" Window #"); pw.print(i); pw.print(' ');
11150 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011151 w.dump(pw, " ");
11152 }
11153 if (mInputMethodDialogs.size() > 0) {
11154 pw.println(" ");
11155 pw.println(" Input method dialogs:");
11156 for (int i=mInputMethodDialogs.size()-1; i>=0; i--) {
11157 WindowState w = mInputMethodDialogs.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011158 pw.print(" IM Dialog #"); pw.print(i); pw.print(": "); pw.println(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011159 }
11160 }
11161 if (mPendingRemove.size() > 0) {
11162 pw.println(" ");
11163 pw.println(" Remove pending for:");
11164 for (int i=mPendingRemove.size()-1; i>=0; i--) {
11165 WindowState w = mPendingRemove.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011166 pw.print(" Remove #"); pw.print(i); pw.print(' ');
11167 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011168 w.dump(pw, " ");
11169 }
11170 }
11171 if (mForceRemoves != null && mForceRemoves.size() > 0) {
11172 pw.println(" ");
11173 pw.println(" Windows force removing:");
11174 for (int i=mForceRemoves.size()-1; i>=0; i--) {
11175 WindowState w = mForceRemoves.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011176 pw.print(" Removing #"); pw.print(i); pw.print(' ');
11177 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011178 w.dump(pw, " ");
11179 }
11180 }
11181 if (mDestroySurface.size() > 0) {
11182 pw.println(" ");
11183 pw.println(" Windows waiting to destroy their surface:");
11184 for (int i=mDestroySurface.size()-1; i>=0; i--) {
11185 WindowState w = mDestroySurface.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011186 pw.print(" Destroy #"); pw.print(i); pw.print(' ');
11187 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011188 w.dump(pw, " ");
11189 }
11190 }
11191 if (mLosingFocus.size() > 0) {
11192 pw.println(" ");
11193 pw.println(" Windows losing focus:");
11194 for (int i=mLosingFocus.size()-1; i>=0; i--) {
11195 WindowState w = mLosingFocus.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011196 pw.print(" Losing #"); pw.print(i); pw.print(' ');
11197 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011198 w.dump(pw, " ");
11199 }
11200 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070011201 if (mResizingWindows.size() > 0) {
11202 pw.println(" ");
11203 pw.println(" Windows waiting to resize:");
11204 for (int i=mResizingWindows.size()-1; i>=0; i--) {
11205 WindowState w = mResizingWindows.get(i);
11206 pw.print(" Resizing #"); pw.print(i); pw.print(' ');
11207 pw.print(w); pw.println(":");
11208 w.dump(pw, " ");
11209 }
11210 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011211 if (mSessions.size() > 0) {
11212 pw.println(" ");
11213 pw.println(" All active sessions:");
11214 Iterator<Session> it = mSessions.iterator();
11215 while (it.hasNext()) {
11216 Session s = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011217 pw.print(" Session "); pw.print(s); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011218 s.dump(pw, " ");
11219 }
11220 }
11221 if (mTokenMap.size() > 0) {
11222 pw.println(" ");
11223 pw.println(" All tokens:");
11224 Iterator<WindowToken> it = mTokenMap.values().iterator();
11225 while (it.hasNext()) {
11226 WindowToken token = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011227 pw.print(" Token "); pw.print(token.token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011228 token.dump(pw, " ");
11229 }
11230 }
11231 if (mTokenList.size() > 0) {
11232 pw.println(" ");
11233 pw.println(" Window token list:");
11234 for (int i=0; i<mTokenList.size(); i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011235 pw.print(" #"); pw.print(i); pw.print(": ");
11236 pw.println(mTokenList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011237 }
11238 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070011239 if (mWallpaperTokens.size() > 0) {
11240 pw.println(" ");
11241 pw.println(" Wallpaper tokens:");
11242 for (int i=mWallpaperTokens.size()-1; i>=0; i--) {
11243 WindowToken token = mWallpaperTokens.get(i);
11244 pw.print(" Wallpaper #"); pw.print(i);
11245 pw.print(' '); pw.print(token); pw.println(':');
11246 token.dump(pw, " ");
11247 }
11248 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011249 if (mAppTokens.size() > 0) {
11250 pw.println(" ");
11251 pw.println(" Application tokens in Z order:");
11252 for (int i=mAppTokens.size()-1; i>=0; i--) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011253 pw.print(" App #"); pw.print(i); pw.print(": ");
11254 pw.println(mAppTokens.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011255 }
11256 }
11257 if (mFinishedStarting.size() > 0) {
11258 pw.println(" ");
11259 pw.println(" Finishing start of application tokens:");
11260 for (int i=mFinishedStarting.size()-1; i>=0; i--) {
11261 WindowToken token = mFinishedStarting.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011262 pw.print(" Finished Starting #"); pw.print(i);
11263 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011264 token.dump(pw, " ");
11265 }
11266 }
11267 if (mExitingTokens.size() > 0) {
11268 pw.println(" ");
11269 pw.println(" Exiting tokens:");
11270 for (int i=mExitingTokens.size()-1; i>=0; i--) {
11271 WindowToken token = mExitingTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011272 pw.print(" Exiting #"); pw.print(i);
11273 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011274 token.dump(pw, " ");
11275 }
11276 }
11277 if (mExitingAppTokens.size() > 0) {
11278 pw.println(" ");
11279 pw.println(" Exiting application tokens:");
11280 for (int i=mExitingAppTokens.size()-1; i>=0; i--) {
11281 WindowToken token = mExitingAppTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011282 pw.print(" Exiting App #"); pw.print(i);
11283 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011284 token.dump(pw, " ");
11285 }
11286 }
11287 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011288 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
11289 pw.print(" mLastFocus="); pw.println(mLastFocus);
11290 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
11291 pw.print(" mInputMethodTarget="); pw.println(mInputMethodTarget);
11292 pw.print(" mInputMethodWindow="); pw.println(mInputMethodWindow);
Dianne Hackbornf21adf62009-08-13 10:20:21 -070011293 pw.print(" mWallpaperTarget="); pw.println(mWallpaperTarget);
Dianne Hackborn284ac932009-08-28 10:34:25 -070011294 if (mLowerWallpaperTarget != null && mUpperWallpaperTarget != null) {
11295 pw.print(" mLowerWallpaperTarget="); pw.println(mLowerWallpaperTarget);
11296 pw.print(" mUpperWallpaperTarget="); pw.println(mUpperWallpaperTarget);
11297 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -080011298 pw.print(" mCurConfiguration="); pw.println(this.mCurConfiguration);
11299 pw.print(" mInTouchMode="); pw.print(mInTouchMode);
11300 pw.print(" mLayoutSeq="); pw.println(mLayoutSeq);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011301 pw.print(" mSystemBooted="); pw.print(mSystemBooted);
11302 pw.print(" mDisplayEnabled="); pw.println(mDisplayEnabled);
11303 pw.print(" mLayoutNeeded="); pw.print(mLayoutNeeded);
11304 pw.print(" mBlurShown="); pw.println(mBlurShown);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011305 if (mDimAnimator != null) {
11306 mDimAnimator.printTo(pw);
11307 } else {
Dianne Hackborna2e92262010-03-02 17:19:29 -080011308 pw.println( " no DimAnimator ");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011309 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011310 pw.print(" mInputMethodAnimLayerAdjustment=");
Dianne Hackborn759a39e2009-08-09 17:20:27 -070011311 pw.print(mInputMethodAnimLayerAdjustment);
11312 pw.print(" mWallpaperAnimLayerAdjustment=");
11313 pw.println(mWallpaperAnimLayerAdjustment);
Dianne Hackborn284ac932009-08-28 10:34:25 -070011314 pw.print(" mLastWallpaperX="); pw.print(mLastWallpaperX);
11315 pw.print(" mLastWallpaperY="); pw.println(mLastWallpaperY);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011316 pw.print(" mDisplayFrozen="); pw.print(mDisplayFrozen);
11317 pw.print(" mWindowsFreezingScreen="); pw.print(mWindowsFreezingScreen);
Dianne Hackborne36d6e22010-02-17 19:46:25 -080011318 pw.print(" mAppsFreezingScreen="); pw.print(mAppsFreezingScreen);
11319 pw.print(" mWaitingForConfig="); pw.println(mWaitingForConfig);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011320 pw.print(" mRotation="); pw.print(mRotation);
11321 pw.print(", mForcedAppOrientation="); pw.print(mForcedAppOrientation);
11322 pw.print(", mRequestedRotation="); pw.println(mRequestedRotation);
11323 pw.print(" mAnimationPending="); pw.print(mAnimationPending);
11324 pw.print(" mWindowAnimationScale="); pw.print(mWindowAnimationScale);
11325 pw.print(" mTransitionWindowAnimationScale="); pw.println(mTransitionAnimationScale);
11326 pw.print(" mNextAppTransition=0x");
11327 pw.print(Integer.toHexString(mNextAppTransition));
11328 pw.print(", mAppTransitionReady="); pw.print(mAppTransitionReady);
Dianne Hackborna8f60182009-09-01 19:01:50 -070011329 pw.print(", mAppTransitionRunning="); pw.print(mAppTransitionRunning);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011330 pw.print(", mAppTransitionTimeout="); pw.println( mAppTransitionTimeout);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070011331 if (mNextAppTransitionPackage != null) {
11332 pw.print(" mNextAppTransitionPackage=");
11333 pw.print(mNextAppTransitionPackage);
11334 pw.print(", mNextAppTransitionEnter=0x");
11335 pw.print(Integer.toHexString(mNextAppTransitionEnter));
11336 pw.print(", mNextAppTransitionExit=0x");
11337 pw.print(Integer.toHexString(mNextAppTransitionExit));
11338 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011339 pw.print(" mStartingIconInTransition="); pw.print(mStartingIconInTransition);
11340 pw.print(", mSkipAppTransitionAnimation="); pw.println(mSkipAppTransitionAnimation);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070011341 if (mLastEnterAnimToken != null || mLastEnterAnimToken != null) {
11342 pw.print(" mLastEnterAnimToken="); pw.print(mLastEnterAnimToken);
11343 pw.print(", mLastEnterAnimParams="); pw.println(mLastEnterAnimParams);
11344 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011345 if (mOpeningApps.size() > 0) {
11346 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
11347 }
11348 if (mClosingApps.size() > 0) {
11349 pw.print(" mClosingApps="); pw.println(mClosingApps);
11350 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070011351 if (mToTopApps.size() > 0) {
11352 pw.print(" mToTopApps="); pw.println(mToTopApps);
11353 }
11354 if (mToBottomApps.size() > 0) {
11355 pw.print(" mToBottomApps="); pw.println(mToBottomApps);
11356 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011357 pw.print(" DisplayWidth="); pw.print(mDisplay.getWidth());
11358 pw.print(" DisplayHeight="); pw.println(mDisplay.getHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011359 pw.println(" KeyWaiter state:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011360 pw.print(" mLastWin="); pw.print(mKeyWaiter.mLastWin);
11361 pw.print(" mLastBinder="); pw.println(mKeyWaiter.mLastBinder);
11362 pw.print(" mFinished="); pw.print(mKeyWaiter.mFinished);
11363 pw.print(" mGotFirstWindow="); pw.print(mKeyWaiter.mGotFirstWindow);
11364 pw.print(" mEventDispatching="); pw.print(mKeyWaiter.mEventDispatching);
11365 pw.print(" mTimeToSwitch="); pw.println(mKeyWaiter.mTimeToSwitch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011366 }
11367 }
11368
11369 public void monitor() {
11370 synchronized (mWindowMap) { }
Mike Lockwood983ee092009-11-22 01:42:24 -050011371 synchronized (mKeyguardTokenWatcher) { }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011372 synchronized (mKeyWaiter) { }
11373 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011374
Dianne Hackbornddca3ee2009-07-23 19:01:31 -070011375 public void virtualKeyFeedback(KeyEvent event) {
11376 mPolicy.keyFeedbackFromInput(event);
11377 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080011378
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011379 /**
11380 * DimAnimator class that controls the dim animation. This holds the surface and
Doug Zongkerab5c49c2009-12-04 10:31:43 -080011381 * all state used for dim animation.
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011382 */
11383 private static class DimAnimator {
11384 Surface mDimSurface;
11385 boolean mDimShown = false;
11386 float mDimCurrentAlpha;
11387 float mDimTargetAlpha;
11388 float mDimDeltaPerMs;
11389 long mLastDimAnimTime;
Dianne Hackbornf83c5552010-03-31 22:19:32 -070011390
11391 int mLastDimWidth, mLastDimHeight;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011392
11393 DimAnimator (SurfaceSession session) {
11394 if (mDimSurface == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011395 if (SHOW_TRANSACTIONS) Slog.i(TAG, " DIM "
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011396 + mDimSurface + ": CREATE");
11397 try {
Mathias Agopian5d26c1e2010-03-01 16:09:43 -080011398 mDimSurface = new Surface(session, 0,
11399 "DimSurface",
11400 -1, 16, 16, PixelFormat.OPAQUE,
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011401 Surface.FX_SURFACE_DIM);
Maciej Białka9ee5c222010-03-24 10:25:40 +010011402 mDimSurface.setAlpha(0.0f);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011403 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011404 Slog.e(TAG, "Exception creating Dim surface", e);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011405 }
11406 }
11407 }
11408
11409 /**
11410 * Show the dim surface.
11411 */
11412 void show(int dw, int dh) {
Dianne Hackborn16064f92010-03-25 00:47:24 -070011413 if (!mDimShown) {
11414 if (SHOW_TRANSACTIONS) Slog.i(TAG, " DIM " + mDimSurface + ": SHOW pos=(0,0) (" +
11415 dw + "x" + dh + ")");
11416 mDimShown = true;
11417 try {
Dianne Hackbornf83c5552010-03-31 22:19:32 -070011418 mLastDimWidth = dw;
11419 mLastDimHeight = dh;
Dianne Hackborn16064f92010-03-25 00:47:24 -070011420 mDimSurface.setPosition(0, 0);
11421 mDimSurface.setSize(dw, dh);
11422 mDimSurface.show();
11423 } catch (RuntimeException e) {
11424 Slog.w(TAG, "Failure showing dim surface", e);
11425 }
Dianne Hackbornf83c5552010-03-31 22:19:32 -070011426 } else if (mLastDimWidth != dw || mLastDimHeight != dh) {
11427 mLastDimWidth = dw;
11428 mLastDimHeight = dh;
11429 mDimSurface.setSize(dw, dh);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011430 }
11431 }
11432
11433 /**
11434 * Set's the dim surface's layer and update dim parameters that will be used in
11435 * {@link updateSurface} after all windows are examined.
11436 */
11437 void updateParameters(WindowState w, long currentTime) {
11438 mDimSurface.setLayer(w.mAnimLayer-1);
11439
11440 final float target = w.mExiting ? 0 : w.mAttrs.dimAmount;
Joe Onorato8a9b2202010-02-26 18:56:32 -080011441 if (SHOW_TRANSACTIONS) Slog.i(TAG, " DIM " + mDimSurface
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070011442 + ": layer=" + (w.mAnimLayer-1) + " target=" + target);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011443 if (mDimTargetAlpha != target) {
11444 // If the desired dim level has changed, then
11445 // start an animation to it.
11446 mLastDimAnimTime = currentTime;
11447 long duration = (w.mAnimating && w.mAnimation != null)
11448 ? w.mAnimation.computeDurationHint()
11449 : DEFAULT_DIM_DURATION;
11450 if (target > mDimTargetAlpha) {
11451 // This is happening behind the activity UI,
11452 // so we can make it run a little longer to
11453 // give a stronger impression without disrupting
11454 // the user.
11455 duration *= DIM_DURATION_MULTIPLIER;
11456 }
11457 if (duration < 1) {
11458 // Don't divide by zero
11459 duration = 1;
11460 }
11461 mDimTargetAlpha = target;
11462 mDimDeltaPerMs = (mDimTargetAlpha-mDimCurrentAlpha) / duration;
11463 }
11464 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080011465
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011466 /**
11467 * Updating the surface's alpha. Returns true if the animation continues, or returns
11468 * false when the animation is finished and the dim surface is hidden.
11469 */
11470 boolean updateSurface(boolean dimming, long currentTime, boolean displayFrozen) {
11471 if (!dimming) {
11472 if (mDimTargetAlpha != 0) {
11473 mLastDimAnimTime = currentTime;
11474 mDimTargetAlpha = 0;
11475 mDimDeltaPerMs = (-mDimCurrentAlpha) / DEFAULT_DIM_DURATION;
11476 }
11477 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080011478
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011479 boolean animating = false;
11480 if (mLastDimAnimTime != 0) {
11481 mDimCurrentAlpha += mDimDeltaPerMs
11482 * (currentTime-mLastDimAnimTime);
11483 boolean more = true;
11484 if (displayFrozen) {
11485 // If the display is frozen, there is no reason to animate.
11486 more = false;
11487 } else if (mDimDeltaPerMs > 0) {
11488 if (mDimCurrentAlpha > mDimTargetAlpha) {
11489 more = false;
11490 }
11491 } else if (mDimDeltaPerMs < 0) {
11492 if (mDimCurrentAlpha < mDimTargetAlpha) {
11493 more = false;
11494 }
11495 } else {
11496 more = false;
11497 }
11498
11499 // Do we need to continue animating?
11500 if (more) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011501 if (SHOW_TRANSACTIONS) Slog.i(TAG, " DIM "
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011502 + mDimSurface + ": alpha=" + mDimCurrentAlpha);
11503 mLastDimAnimTime = currentTime;
11504 mDimSurface.setAlpha(mDimCurrentAlpha);
11505 animating = true;
11506 } else {
11507 mDimCurrentAlpha = mDimTargetAlpha;
11508 mLastDimAnimTime = 0;
Joe Onorato8a9b2202010-02-26 18:56:32 -080011509 if (SHOW_TRANSACTIONS) Slog.i(TAG, " DIM "
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011510 + mDimSurface + ": final alpha=" + mDimCurrentAlpha);
11511 mDimSurface.setAlpha(mDimCurrentAlpha);
11512 if (!dimming) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011513 if (SHOW_TRANSACTIONS) Slog.i(TAG, " DIM " + mDimSurface
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011514 + ": HIDE");
11515 try {
11516 mDimSurface.hide();
11517 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011518 Slog.w(TAG, "Illegal argument exception hiding dim surface");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011519 }
11520 mDimShown = false;
11521 }
11522 }
11523 }
11524 return animating;
11525 }
11526
11527 public void printTo(PrintWriter pw) {
11528 pw.print(" mDimShown="); pw.print(mDimShown);
11529 pw.print(" current="); pw.print(mDimCurrentAlpha);
11530 pw.print(" target="); pw.print(mDimTargetAlpha);
11531 pw.print(" delta="); pw.print(mDimDeltaPerMs);
11532 pw.print(" lastAnimTime="); pw.println(mLastDimAnimTime);
11533 }
11534 }
11535
11536 /**
11537 * Animation that fade in after 0.5 interpolate time, or fade out in reverse order.
11538 * This is used for opening/closing transition for apps in compatible mode.
11539 */
11540 private static class FadeInOutAnimation extends Animation {
11541 int mWidth;
11542 boolean mFadeIn;
11543
11544 public FadeInOutAnimation(boolean fadeIn) {
11545 setInterpolator(new AccelerateInterpolator());
11546 setDuration(DEFAULT_FADE_IN_OUT_DURATION);
11547 mFadeIn = fadeIn;
11548 }
11549
11550 @Override
11551 protected void applyTransformation(float interpolatedTime, Transformation t) {
11552 float x = interpolatedTime;
11553 if (!mFadeIn) {
11554 x = 1.0f - x; // reverse the interpolation for fade out
11555 }
11556 if (x < 0.5) {
11557 // move the window out of the screen.
11558 t.getMatrix().setTranslate(mWidth, 0);
11559 } else {
11560 t.getMatrix().setTranslate(0, 0);// show
11561 t.setAlpha((x - 0.5f) * 2);
11562 }
11563 }
11564
11565 @Override
11566 public void initialize(int width, int height, int parentWidth, int parentHeight) {
11567 // width is the screen width {@see AppWindowToken#stepAnimatinoLocked}
11568 mWidth = width;
11569 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011570
11571 @Override
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -070011572 public int getZAdjustment() {
11573 return Animation.ZORDER_TOP;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011574 }
11575 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011576}