blob: 95ab5bc56dffffb478a497850bf3ac00bc4e3ce3 [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;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700104import android.view.InputChannel;
105import android.view.InputQueue;
106import android.view.InputTarget;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107import android.view.KeyEvent;
108import android.view.MotionEvent;
109import android.view.RawInputEvent;
110import android.view.Surface;
111import android.view.SurfaceSession;
112import android.view.View;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -0700113import android.view.ViewConfiguration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114import android.view.ViewTreeObserver;
115import android.view.WindowManager;
116import android.view.WindowManagerImpl;
117import android.view.WindowManagerPolicy;
118import android.view.WindowManager.LayoutParams;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700119import android.view.animation.AccelerateInterpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120import android.view.animation.Animation;
121import android.view.animation.AnimationUtils;
122import android.view.animation.Transformation;
123
124import java.io.BufferedWriter;
125import java.io.File;
126import java.io.FileDescriptor;
127import java.io.IOException;
128import java.io.OutputStream;
129import java.io.OutputStreamWriter;
130import java.io.PrintWriter;
131import java.io.StringWriter;
132import java.net.Socket;
133import java.util.ArrayList;
134import java.util.HashMap;
135import java.util.HashSet;
136import java.util.Iterator;
137import java.util.List;
138
139/** {@hide} */
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700140public class WindowManagerService extends IWindowManager.Stub
141 implements Watchdog.Monitor, KeyInputQueue.HapticFeedbackCallback {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 static final String TAG = "WindowManager";
143 static final boolean DEBUG = false;
144 static final boolean DEBUG_FOCUS = false;
145 static final boolean DEBUG_ANIM = false;
Dianne Hackborn9b52a212009-12-11 14:51:35 -0800146 static final boolean DEBUG_LAYOUT = false;
Dianne Hackbornac3587d2010-03-11 11:12:11 -0800147 static final boolean DEBUG_RESIZE = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 static final boolean DEBUG_LAYERS = false;
149 static final boolean DEBUG_INPUT = false;
150 static final boolean DEBUG_INPUT_METHOD = false;
151 static final boolean DEBUG_VISIBILITY = false;
Dianne Hackbornbdd52b22009-09-02 21:46:19 -0700152 static final boolean DEBUG_WINDOW_MOVEMENT = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 static final boolean DEBUG_ORIENTATION = false;
Dianne Hackborn694f79b2010-03-17 19:44:59 -0700154 static final boolean DEBUG_CONFIGURATION = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 static final boolean DEBUG_APP_TRANSITIONS = false;
156 static final boolean DEBUG_STARTING_WINDOW = false;
157 static final boolean DEBUG_REORDER = false;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -0700158 static final boolean DEBUG_WALLPAPER = false;
Dianne Hackbornce73c1e2010-04-12 23:11:38 -0700159 static final boolean DEBUG_FREEZE = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 static final boolean SHOW_TRANSACTIONS = false;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700161 static final boolean HIDE_STACK_CRAWLS = true;
Michael Chan53071d62009-05-13 17:29:48 -0700162 static final boolean MEASURE_LATENCY = false;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700163 static final boolean ENABLE_NATIVE_INPUT_DISPATCH =
164 WindowManagerPolicy.ENABLE_NATIVE_INPUT_DISPATCH;
Michael Chan53071d62009-05-13 17:29:48 -0700165 static private LatencyTimer lt;
166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 static final boolean PROFILE_ORIENTATION = false;
168 static final boolean BLUR = true;
Dave Bortcfe65242009-04-09 14:51:04 -0700169 static final boolean localLOGV = DEBUG;
Romain Guy06882f82009-06-10 13:36:04 -0700170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 /** How long to wait for subsequent key repeats, in milliseconds */
172 static final int KEY_REPEAT_DELAY = 50;
173
174 /** How much to multiply the policy's type layer, to reserve room
175 * for multiple windows of the same type and Z-ordering adjustment
176 * with TYPE_LAYER_OFFSET. */
177 static final int TYPE_LAYER_MULTIPLIER = 10000;
Romain Guy06882f82009-06-10 13:36:04 -0700178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 /** Offset from TYPE_LAYER_MULTIPLIER for moving a group of windows above
180 * or below others in the same layer. */
181 static final int TYPE_LAYER_OFFSET = 1000;
Romain Guy06882f82009-06-10 13:36:04 -0700182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 /** How much to increment the layer for each window, to reserve room
184 * for effect surfaces between them.
185 */
186 static final int WINDOW_LAYER_MULTIPLIER = 5;
Romain Guy06882f82009-06-10 13:36:04 -0700187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 /** The maximum length we will accept for a loaded animation duration:
189 * this is 10 seconds.
190 */
191 static final int MAX_ANIMATION_DURATION = 10*1000;
192
193 /** Amount of time (in milliseconds) to animate the dim surface from one
194 * value to another, when no window animation is driving it.
195 */
196 static final int DEFAULT_DIM_DURATION = 200;
197
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700198 /** Amount of time (in milliseconds) to animate the fade-in-out transition for
199 * compatible windows.
200 */
201 static final int DEFAULT_FADE_IN_OUT_DURATION = 400;
202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 /** Adjustment to time to perform a dim, to make it more dramatic.
204 */
205 static final int DIM_DURATION_MULTIPLIER = 6;
Jeff Brown7fbdc842010-06-17 20:52:56 -0700206
207 // Maximum number of milliseconds to wait for input event injection.
208 // FIXME is this value reasonable?
209 private static final int INJECTION_TIMEOUT_MILLIS = 30 * 1000;
Romain Guy06882f82009-06-10 13:36:04 -0700210
Dianne Hackborncfaef692009-06-15 14:24:44 -0700211 static final int INJECT_FAILED = 0;
212 static final int INJECT_SUCCEEDED = 1;
213 static final int INJECT_NO_PERMISSION = -1;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 static final int UPDATE_FOCUS_NORMAL = 0;
216 static final int UPDATE_FOCUS_WILL_ASSIGN_LAYERS = 1;
217 static final int UPDATE_FOCUS_PLACING_SURFACES = 2;
218 static final int UPDATE_FOCUS_WILL_PLACE_SURFACES = 3;
Romain Guy06882f82009-06-10 13:36:04 -0700219
Michael Chane96440f2009-05-06 10:27:36 -0700220 /** The minimum time between dispatching touch events. */
221 int mMinWaitTimeBetweenTouchEvents = 1000 / 35;
222
223 // Last touch event time
224 long mLastTouchEventTime = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700225
Michael Chane96440f2009-05-06 10:27:36 -0700226 // Last touch event type
227 int mLastTouchEventType = OTHER_EVENT;
Romain Guy06882f82009-06-10 13:36:04 -0700228
Michael Chane96440f2009-05-06 10:27:36 -0700229 // Time to wait before calling useractivity again. This saves CPU usage
230 // when we get a flood of touch events.
231 static final int MIN_TIME_BETWEEN_USERACTIVITIES = 1000;
232
233 // Last time we call user activity
234 long mLastUserActivityCallTime = 0;
235
Romain Guy06882f82009-06-10 13:36:04 -0700236 // Last time we updated battery stats
Michael Chane96440f2009-05-06 10:27:36 -0700237 long mLastBatteryStatsCallTime = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 private static final String SYSTEM_SECURE = "ro.secure";
Romain Guy06882f82009-06-10 13:36:04 -0700240 private static final String SYSTEM_DEBUGGABLE = "ro.debuggable";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241
242 /**
243 * Condition waited on by {@link #reenableKeyguard} to know the call to
244 * the window policy has finished.
Mike Lockwood983ee092009-11-22 01:42:24 -0500245 * This is set to true only if mKeyguardTokenWatcher.acquired() has
246 * actually disabled the keyguard.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 */
Mike Lockwood983ee092009-11-22 01:42:24 -0500248 private boolean mKeyguardDisabled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249
Jim Miller284b62e2010-06-08 14:27:42 -0700250 private static final int ALLOW_DISABLE_YES = 1;
251 private static final int ALLOW_DISABLE_NO = 0;
252 private static final int ALLOW_DISABLE_UNKNOWN = -1; // check with DevicePolicyManager
253 private int mAllowDisableKeyguard = ALLOW_DISABLE_UNKNOWN; // sync'd by mKeyguardTokenWatcher
254
Mike Lockwood983ee092009-11-22 01:42:24 -0500255 final TokenWatcher mKeyguardTokenWatcher = new TokenWatcher(
256 new Handler(), "WindowManagerService.mKeyguardTokenWatcher") {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 public void acquired() {
Jim Miller284b62e2010-06-08 14:27:42 -0700258 if (shouldAllowDisableKeyguard()) {
259 mPolicy.enableKeyguard(false);
260 mKeyguardDisabled = true;
261 } else {
262 Log.v(TAG, "Not disabling keyguard since device policy is enforced");
263 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 }
265 public void released() {
Dianne Hackborna33e3f72009-09-29 17:28:24 -0700266 mPolicy.enableKeyguard(true);
Mike Lockwood983ee092009-11-22 01:42:24 -0500267 synchronized (mKeyguardTokenWatcher) {
268 mKeyguardDisabled = false;
269 mKeyguardTokenWatcher.notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 }
271 }
272 };
273
Jim Miller284b62e2010-06-08 14:27:42 -0700274 final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
275 @Override
276 public void onReceive(Context context, Intent intent) {
277 mPolicy.enableKeyguard(true);
278 synchronized(mKeyguardTokenWatcher) {
279 // lazily evaluate this next time we're asked to disable keyguard
280 mAllowDisableKeyguard = ALLOW_DISABLE_UNKNOWN;
281 mKeyguardDisabled = false;
282 }
283 }
284 };
285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 final Context mContext;
287
288 final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 final boolean mLimitedAlphaCompositing;
Romain Guy06882f82009-06-10 13:36:04 -0700291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 final WindowManagerPolicy mPolicy = PolicyManager.makeNewWindowManager();
293
294 final IActivityManager mActivityManager;
Romain Guy06882f82009-06-10 13:36:04 -0700295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 final IBatteryStats mBatteryStats;
Romain Guy06882f82009-06-10 13:36:04 -0700297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298 /**
299 * All currently active sessions with clients.
300 */
301 final HashSet<Session> mSessions = new HashSet<Session>();
Romain Guy06882f82009-06-10 13:36:04 -0700302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 /**
304 * Mapping from an IWindow IBinder to the server's Window object.
305 * This is also used as the lock for all of our state.
306 */
307 final HashMap<IBinder, WindowState> mWindowMap = new HashMap<IBinder, WindowState>();
308
309 /**
310 * Mapping from a token IBinder to a WindowToken object.
311 */
312 final HashMap<IBinder, WindowToken> mTokenMap =
313 new HashMap<IBinder, WindowToken>();
314
315 /**
316 * The same tokens as mTokenMap, stored in a list for efficient iteration
317 * over them.
318 */
319 final ArrayList<WindowToken> mTokenList = new ArrayList<WindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321 /**
322 * Window tokens that are in the process of exiting, but still
323 * on screen for animations.
324 */
325 final ArrayList<WindowToken> mExitingTokens = new ArrayList<WindowToken>();
326
327 /**
328 * Z-ordered (bottom-most first) list of all application tokens, for
329 * controlling the ordering of windows in different applications. This
330 * contains WindowToken objects.
331 */
332 final ArrayList<AppWindowToken> mAppTokens = new ArrayList<AppWindowToken>();
333
334 /**
335 * Application tokens that are in the process of exiting, but still
336 * on screen for animations.
337 */
338 final ArrayList<AppWindowToken> mExitingAppTokens = new ArrayList<AppWindowToken>();
339
340 /**
341 * List of window tokens that have finished starting their application,
342 * and now need to have the policy remove their windows.
343 */
344 final ArrayList<AppWindowToken> mFinishedStarting = new ArrayList<AppWindowToken>();
345
346 /**
Dianne Hackborn3b3e1452009-09-24 19:22:12 -0700347 * This was the app token that was used to retrieve the last enter
348 * animation. It will be used for the next exit animation.
349 */
350 AppWindowToken mLastEnterAnimToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800351
Dianne Hackborn3b3e1452009-09-24 19:22:12 -0700352 /**
353 * These were the layout params used to retrieve the last enter animation.
354 * They will be used for the next exit animation.
355 */
356 LayoutParams mLastEnterAnimParams;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800357
Dianne Hackborn3b3e1452009-09-24 19:22:12 -0700358 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 * Z-ordered (bottom-most first) list of all Window objects.
360 */
361 final ArrayList mWindows = new ArrayList();
362
363 /**
364 * Windows that are being resized. Used so we can tell the client about
365 * the resize after closing the transaction in which we resized the
366 * underlying surface.
367 */
368 final ArrayList<WindowState> mResizingWindows = new ArrayList<WindowState>();
369
370 /**
371 * Windows whose animations have ended and now must be removed.
372 */
373 final ArrayList<WindowState> mPendingRemove = new ArrayList<WindowState>();
374
375 /**
376 * Windows whose surface should be destroyed.
377 */
378 final ArrayList<WindowState> mDestroySurface = new ArrayList<WindowState>();
379
380 /**
381 * Windows that have lost input focus and are waiting for the new
382 * focus window to be displayed before they are told about this.
383 */
384 ArrayList<WindowState> mLosingFocus = new ArrayList<WindowState>();
385
386 /**
387 * This is set when we have run out of memory, and will either be an empty
388 * list or contain windows that need to be force removed.
389 */
390 ArrayList<WindowState> mForceRemoves;
Romain Guy06882f82009-06-10 13:36:04 -0700391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 IInputMethodManager mInputMethodManager;
Romain Guy06882f82009-06-10 13:36:04 -0700393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 SurfaceSession mFxSession;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700395 private DimAnimator mDimAnimator = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 Surface mBlurSurface;
397 boolean mBlurShown;
Romain Guy06882f82009-06-10 13:36:04 -0700398
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 int mTransactionSequence = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700400
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 final float[] mTmpFloats = new float[9];
402
403 boolean mSafeMode;
404 boolean mDisplayEnabled = false;
405 boolean mSystemBooted = false;
Christopher Tateb696aee2010-04-02 19:08:30 -0700406 int mInitialDisplayWidth = 0;
407 int mInitialDisplayHeight = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 int mRotation = 0;
409 int mRequestedRotation = 0;
410 int mForcedAppOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Dianne Hackborn321ae682009-03-27 16:16:03 -0700411 int mLastRotationFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 ArrayList<IRotationWatcher> mRotationWatchers
413 = new ArrayList<IRotationWatcher>();
Romain Guy06882f82009-06-10 13:36:04 -0700414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 boolean mLayoutNeeded = true;
416 boolean mAnimationPending = false;
417 boolean mDisplayFrozen = false;
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800418 boolean mWaitingForConfig = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 boolean mWindowsFreezingScreen = false;
420 long mFreezeGcPending = 0;
421 int mAppsFreezingScreen = 0;
422
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800423 int mLayoutSeq = 0;
424
Dianne Hackbornb601ce12010-03-01 23:36:02 -0800425 // State while inside of layoutAndPlaceSurfacesLocked().
426 boolean mFocusMayChange;
427
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800428 Configuration mCurConfiguration = new Configuration();
429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 // This is held as long as we have the screen frozen, to give us time to
431 // perform a rotation animation when turning off shows the lock screen which
432 // changes the orientation.
433 PowerManager.WakeLock mScreenFrozenLock;
Romain Guy06882f82009-06-10 13:36:04 -0700434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 // State management of app transitions. When we are preparing for a
436 // transition, mNextAppTransition will be the kind of transition to
437 // perform or TRANSIT_NONE if we are not waiting. If we are waiting,
438 // mOpeningApps and mClosingApps are the lists of tokens that will be
439 // made visible or hidden at the next transition.
Dianne Hackbornbfe319e2009-09-21 00:34:05 -0700440 int mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -0700441 String mNextAppTransitionPackage;
442 int mNextAppTransitionEnter;
443 int mNextAppTransitionExit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 boolean mAppTransitionReady = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -0700445 boolean mAppTransitionRunning = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 boolean mAppTransitionTimeout = false;
447 boolean mStartingIconInTransition = false;
448 boolean mSkipAppTransitionAnimation = false;
449 final ArrayList<AppWindowToken> mOpeningApps = new ArrayList<AppWindowToken>();
450 final ArrayList<AppWindowToken> mClosingApps = new ArrayList<AppWindowToken>();
Dianne Hackborna8f60182009-09-01 19:01:50 -0700451 final ArrayList<AppWindowToken> mToTopApps = new ArrayList<AppWindowToken>();
452 final ArrayList<AppWindowToken> mToBottomApps = new ArrayList<AppWindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 Display mDisplay;
Romain Guy06882f82009-06-10 13:36:04 -0700455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 H mH = new H();
457
458 WindowState mCurrentFocus = null;
459 WindowState mLastFocus = null;
Romain Guy06882f82009-06-10 13:36:04 -0700460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 // This just indicates the window the input method is on top of, not
462 // necessarily the window its input is going to.
463 WindowState mInputMethodTarget = null;
464 WindowState mUpcomingInputMethodTarget = null;
465 boolean mInputMethodTargetWaitingAnim;
466 int mInputMethodAnimLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -0700467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 WindowState mInputMethodWindow = null;
469 final ArrayList<WindowState> mInputMethodDialogs = new ArrayList<WindowState>();
470
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700471 final ArrayList<WindowToken> mWallpaperTokens = new ArrayList<WindowToken>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800472
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700473 // If non-null, this is the currently visible window that is associated
474 // with the wallpaper.
475 WindowState mWallpaperTarget = null;
Dianne Hackborn3be63c02009-08-20 19:31:38 -0700476 // If non-null, we are in the middle of animating from one wallpaper target
477 // to another, and this is the lower one in Z-order.
478 WindowState mLowerWallpaperTarget = null;
479 // If non-null, we are in the middle of animating from one wallpaper target
480 // to another, and this is the higher one in Z-order.
481 WindowState mUpperWallpaperTarget = null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700482 int mWallpaperAnimLayerAdjustment;
Dianne Hackborn73e92b42009-10-15 14:29:19 -0700483 float mLastWallpaperX = -1;
484 float mLastWallpaperY = -1;
Marco Nelissenbf6956b2009-11-09 15:21:13 -0800485 float mLastWallpaperXStep = -1;
486 float mLastWallpaperYStep = -1;
Dianne Hackborn6adba242009-11-10 11:10:09 -0800487 boolean mSendingPointersToWallpaper = false;
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700488 // This is set when we are waiting for a wallpaper to tell us it is done
489 // changing its scroll position.
490 WindowState mWaitingOnWallpaper;
491 // The last time we had a timeout when waiting for a wallpaper.
492 long mLastWallpaperTimeoutTime;
493 // We give a wallpaper up to 150ms to finish scrolling.
494 static final long WALLPAPER_TIMEOUT = 150;
495 // Time we wait after a timeout before trying to wait again.
496 static final long WALLPAPER_TIMEOUT_RECOVERY = 10000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 AppWindowToken mFocusedApp = null;
499
500 PowerManagerService mPowerManager;
Romain Guy06882f82009-06-10 13:36:04 -0700501
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 float mWindowAnimationScale = 1.0f;
503 float mTransitionAnimationScale = 1.0f;
Romain Guy06882f82009-06-10 13:36:04 -0700504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 final KeyWaiter mKeyWaiter = new KeyWaiter();
506 final KeyQ mQueue;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700507 final InputManager mInputManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 final InputDispatcherThread mInputThread;
509
510 // Who is holding the screen on.
511 Session mHoldingScreenOn;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700512 PowerManager.WakeLock mHoldingScreenWakeLock;
Romain Guy06882f82009-06-10 13:36:04 -0700513
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700514 boolean mTurnOnScreen;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800515
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 /**
517 * Whether the UI is currently running in touch mode (not showing
518 * navigational focus because the user is directly pressing the screen).
519 */
520 boolean mInTouchMode = false;
521
522 private ViewServer mViewServer;
523
524 final Rect mTempRect = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -0700525
Dianne Hackbornc485a602009-03-24 22:39:49 -0700526 final Configuration mTempConfiguration = new Configuration();
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700527 int mScreenLayout = Configuration.SCREENLAYOUT_SIZE_UNDEFINED;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -0700528
529 // The frame use to limit the size of the app running in compatibility mode.
530 Rect mCompatibleScreenFrame = new Rect();
531 // The surface used to fill the outer rim of the app running in compatibility mode.
532 Surface mBackgroundFillerSurface = null;
533 boolean mBackgroundFillerShown = false;
534
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 public static WindowManagerService main(Context context,
536 PowerManagerService pm, boolean haveInputMethods) {
537 WMThread thr = new WMThread(context, pm, haveInputMethods);
538 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700539
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 synchronized (thr) {
541 while (thr.mService == null) {
542 try {
543 thr.wait();
544 } catch (InterruptedException e) {
545 }
546 }
547 }
Romain Guy06882f82009-06-10 13:36:04 -0700548
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 return thr.mService;
550 }
Romain Guy06882f82009-06-10 13:36:04 -0700551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 static class WMThread extends Thread {
553 WindowManagerService mService;
Romain Guy06882f82009-06-10 13:36:04 -0700554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 private final Context mContext;
556 private final PowerManagerService mPM;
557 private final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700558
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 public WMThread(Context context, PowerManagerService pm,
560 boolean haveInputMethods) {
561 super("WindowManager");
562 mContext = context;
563 mPM = pm;
564 mHaveInputMethods = haveInputMethods;
565 }
Romain Guy06882f82009-06-10 13:36:04 -0700566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 public void run() {
568 Looper.prepare();
569 WindowManagerService s = new WindowManagerService(mContext, mPM,
570 mHaveInputMethods);
571 android.os.Process.setThreadPriority(
572 android.os.Process.THREAD_PRIORITY_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -0700573
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 synchronized (this) {
575 mService = s;
576 notifyAll();
577 }
Romain Guy06882f82009-06-10 13:36:04 -0700578
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 Looper.loop();
580 }
581 }
582
583 static class PolicyThread extends Thread {
584 private final WindowManagerPolicy mPolicy;
585 private final WindowManagerService mService;
586 private final Context mContext;
587 private final PowerManagerService mPM;
588 boolean mRunning = false;
Romain Guy06882f82009-06-10 13:36:04 -0700589
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800590 public PolicyThread(WindowManagerPolicy policy,
591 WindowManagerService service, Context context,
592 PowerManagerService pm) {
593 super("WindowManagerPolicy");
594 mPolicy = policy;
595 mService = service;
596 mContext = context;
597 mPM = pm;
598 }
Romain Guy06882f82009-06-10 13:36:04 -0700599
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 public void run() {
601 Looper.prepare();
Dianne Hackbornac3587d2010-03-11 11:12:11 -0800602 WindowManagerPolicyThread.set(this, Looper.myLooper());
603
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604 //Looper.myLooper().setMessageLogging(new LogPrinter(
Joe Onorato8a9b2202010-02-26 18:56:32 -0800605 // Log.VERBOSE, "WindowManagerPolicy", Log.LOG_ID_SYSTEM));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 android.os.Process.setThreadPriority(
607 android.os.Process.THREAD_PRIORITY_FOREGROUND);
608 mPolicy.init(mContext, mService, mPM);
Romain Guy06882f82009-06-10 13:36:04 -0700609
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 synchronized (this) {
611 mRunning = true;
612 notifyAll();
613 }
Romain Guy06882f82009-06-10 13:36:04 -0700614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 Looper.loop();
616 }
617 }
618
619 private WindowManagerService(Context context, PowerManagerService pm,
620 boolean haveInputMethods) {
Michael Chan53071d62009-05-13 17:29:48 -0700621 if (MEASURE_LATENCY) {
622 lt = new LatencyTimer(100, 1000);
623 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800624
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800625 mContext = context;
626 mHaveInputMethods = haveInputMethods;
627 mLimitedAlphaCompositing = context.getResources().getBoolean(
628 com.android.internal.R.bool.config_sf_limitedAlpha);
Romain Guy06882f82009-06-10 13:36:04 -0700629
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630 mPowerManager = pm;
631 mPowerManager.setPolicy(mPolicy);
632 PowerManager pmc = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
633 mScreenFrozenLock = pmc.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
634 "SCREEN_FROZEN");
635 mScreenFrozenLock.setReferenceCounted(false);
636
637 mActivityManager = ActivityManagerNative.getDefault();
638 mBatteryStats = BatteryStatsService.getService();
639
640 // Get persisted window scale setting
641 mWindowAnimationScale = Settings.System.getFloat(context.getContentResolver(),
642 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
643 mTransitionAnimationScale = Settings.System.getFloat(context.getContentResolver(),
644 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
Romain Guy06882f82009-06-10 13:36:04 -0700645
Jim Miller284b62e2010-06-08 14:27:42 -0700646 // Track changes to DevicePolicyManager state so we can enable/disable keyguard.
647 IntentFilter filter = new IntentFilter();
648 filter.addAction(DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED);
649 mContext.registerReceiver(mBroadcastReceiver, filter);
650
Michael Chan9f028e62009-08-04 17:37:46 -0700651 int max_events_per_sec = 35;
652 try {
653 max_events_per_sec = Integer.parseInt(SystemProperties
654 .get("windowsmgr.max_events_per_sec"));
655 if (max_events_per_sec < 1) {
656 max_events_per_sec = 35;
657 }
658 } catch (NumberFormatException e) {
659 }
660 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
661
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700662 mHoldingScreenWakeLock = pmc.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
663 "KEEP_SCREEN_ON_FLAG");
664 mHoldingScreenWakeLock.setReferenceCounted(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700666 if (ENABLE_NATIVE_INPUT_DISPATCH) {
667 mInputManager = new InputManager(context, this, mPolicy, pmc, mPowerManager);
668 } else {
669 mInputManager = null;
670 }
671 mQueue = new KeyQ();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 mInputThread = new InputDispatcherThread();
Romain Guy06882f82009-06-10 13:36:04 -0700673
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800674 PolicyThread thr = new PolicyThread(mPolicy, this, context, pm);
675 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700676
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 synchronized (thr) {
678 while (!thr.mRunning) {
679 try {
680 thr.wait();
681 } catch (InterruptedException e) {
682 }
683 }
684 }
Romain Guy06882f82009-06-10 13:36:04 -0700685
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700686 if (ENABLE_NATIVE_INPUT_DISPATCH) {
687 mInputManager.start();
688 } else {
689 mInputThread.start();
690 }
Romain Guy06882f82009-06-10 13:36:04 -0700691
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692 // Add ourself to the Watchdog monitors.
693 Watchdog.getInstance().addMonitor(this);
694 }
695
696 @Override
697 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
698 throws RemoteException {
699 try {
700 return super.onTransact(code, data, reply, flags);
701 } catch (RuntimeException e) {
702 // The window manager only throws security exceptions, so let's
703 // log all others.
704 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800705 Slog.e(TAG, "Window Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 }
707 throw e;
708 }
709 }
710
711 private void placeWindowAfter(Object pos, WindowState window) {
712 final int i = mWindows.indexOf(pos);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800713 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800714 TAG, "Adding window " + window + " at "
715 + (i+1) + " of " + mWindows.size() + " (after " + pos + ")");
716 mWindows.add(i+1, window);
717 }
718
719 private void placeWindowBefore(Object pos, WindowState window) {
720 final int i = mWindows.indexOf(pos);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800721 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722 TAG, "Adding window " + window + " at "
723 + i + " of " + mWindows.size() + " (before " + pos + ")");
724 mWindows.add(i, window);
725 }
726
727 //This method finds out the index of a window that has the same app token as
728 //win. used for z ordering the windows in mWindows
729 private int findIdxBasedOnAppTokens(WindowState win) {
730 //use a local variable to cache mWindows
731 ArrayList localmWindows = mWindows;
732 int jmax = localmWindows.size();
733 if(jmax == 0) {
734 return -1;
735 }
736 for(int j = (jmax-1); j >= 0; j--) {
737 WindowState wentry = (WindowState)localmWindows.get(j);
738 if(wentry.mAppToken == win.mAppToken) {
739 return j;
740 }
741 }
742 return -1;
743 }
Romain Guy06882f82009-06-10 13:36:04 -0700744
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800745 private void addWindowToListInOrderLocked(WindowState win, boolean addToToken) {
746 final IWindow client = win.mClient;
747 final WindowToken token = win.mToken;
748 final ArrayList localmWindows = mWindows;
Romain Guy06882f82009-06-10 13:36:04 -0700749
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800750 final int N = localmWindows.size();
751 final WindowState attached = win.mAttachedWindow;
752 int i;
753 if (attached == null) {
754 int tokenWindowsPos = token.windows.size();
755 if (token.appWindowToken != null) {
756 int index = tokenWindowsPos-1;
757 if (index >= 0) {
758 // If this application has existing windows, we
759 // simply place the new window on top of them... but
760 // keep the starting window on top.
761 if (win.mAttrs.type == TYPE_BASE_APPLICATION) {
762 // Base windows go behind everything else.
763 placeWindowBefore(token.windows.get(0), win);
764 tokenWindowsPos = 0;
765 } else {
766 AppWindowToken atoken = win.mAppToken;
767 if (atoken != null &&
768 token.windows.get(index) == atoken.startingWindow) {
769 placeWindowBefore(token.windows.get(index), win);
770 tokenWindowsPos--;
771 } else {
772 int newIdx = findIdxBasedOnAppTokens(win);
773 if(newIdx != -1) {
Romain Guy06882f82009-06-10 13:36:04 -0700774 //there is a window above this one associated with the same
775 //apptoken note that the window could be a floating window
776 //that was created later or a window at the top of the list of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800777 //windows associated with this token.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800778 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Slog.v(
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700779 TAG, "Adding window " + win + " at "
780 + (newIdx+1) + " of " + N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800781 localmWindows.add(newIdx+1, win);
Romain Guy06882f82009-06-10 13:36:04 -0700782 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783 }
784 }
785 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800786 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800787 TAG, "Figuring out where to add app window "
788 + client.asBinder() + " (token=" + token + ")");
789 // Figure out where the window should go, based on the
790 // order of applications.
791 final int NA = mAppTokens.size();
792 Object pos = null;
793 for (i=NA-1; i>=0; i--) {
794 AppWindowToken t = mAppTokens.get(i);
795 if (t == token) {
796 i--;
797 break;
798 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800799
Dianne Hackborna8f60182009-09-01 19:01:50 -0700800 // We haven't reached the token yet; if this token
801 // is not going to the bottom and has windows, we can
802 // use it as an anchor for when we do reach the token.
803 if (!t.sendingToBottom && t.windows.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804 pos = t.windows.get(0);
805 }
806 }
807 // We now know the index into the apps. If we found
808 // an app window above, that gives us the position; else
809 // we need to look some more.
810 if (pos != null) {
811 // Move behind any windows attached to this one.
Romain Guy06882f82009-06-10 13:36:04 -0700812 WindowToken atoken =
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800813 mTokenMap.get(((WindowState)pos).mClient.asBinder());
814 if (atoken != null) {
815 final int NC = atoken.windows.size();
816 if (NC > 0) {
817 WindowState bottom = atoken.windows.get(0);
818 if (bottom.mSubLayer < 0) {
819 pos = bottom;
820 }
821 }
822 }
823 placeWindowBefore(pos, win);
824 } else {
Dianne Hackborna8f60182009-09-01 19:01:50 -0700825 // Continue looking down until we find the first
826 // token that has windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800827 while (i >= 0) {
828 AppWindowToken t = mAppTokens.get(i);
829 final int NW = t.windows.size();
830 if (NW > 0) {
831 pos = t.windows.get(NW-1);
832 break;
833 }
834 i--;
835 }
836 if (pos != null) {
837 // Move in front of any windows attached to this
838 // one.
839 WindowToken atoken =
840 mTokenMap.get(((WindowState)pos).mClient.asBinder());
841 if (atoken != null) {
842 final int NC = atoken.windows.size();
843 if (NC > 0) {
844 WindowState top = atoken.windows.get(NC-1);
845 if (top.mSubLayer >= 0) {
846 pos = top;
847 }
848 }
849 }
850 placeWindowAfter(pos, win);
851 } else {
852 // Just search for the start of this layer.
853 final int myLayer = win.mBaseLayer;
854 for (i=0; i<N; i++) {
855 WindowState w = (WindowState)localmWindows.get(i);
856 if (w.mBaseLayer > myLayer) {
857 break;
858 }
859 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800860 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Slog.v(
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700861 TAG, "Adding window " + win + " at "
862 + i + " of " + N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863 localmWindows.add(i, win);
864 }
865 }
866 }
867 } else {
868 // Figure out where window should go, based on layer.
869 final int myLayer = win.mBaseLayer;
870 for (i=N-1; i>=0; i--) {
871 if (((WindowState)localmWindows.get(i)).mBaseLayer <= myLayer) {
872 i++;
873 break;
874 }
875 }
876 if (i < 0) i = 0;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800877 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Slog.v(
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700878 TAG, "Adding window " + win + " at "
879 + i + " of " + N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800880 localmWindows.add(i, win);
881 }
882 if (addToToken) {
883 token.windows.add(tokenWindowsPos, win);
884 }
885
886 } else {
887 // Figure out this window's ordering relative to the window
888 // it is attached to.
889 final int NA = token.windows.size();
890 final int sublayer = win.mSubLayer;
891 int largestSublayer = Integer.MIN_VALUE;
892 WindowState windowWithLargestSublayer = null;
893 for (i=0; i<NA; i++) {
894 WindowState w = token.windows.get(i);
895 final int wSublayer = w.mSubLayer;
896 if (wSublayer >= largestSublayer) {
897 largestSublayer = wSublayer;
898 windowWithLargestSublayer = w;
899 }
900 if (sublayer < 0) {
901 // For negative sublayers, we go below all windows
902 // in the same sublayer.
903 if (wSublayer >= sublayer) {
904 if (addToToken) {
905 token.windows.add(i, win);
906 }
907 placeWindowBefore(
908 wSublayer >= 0 ? attached : w, win);
909 break;
910 }
911 } else {
912 // For positive sublayers, we go above all windows
913 // in the same sublayer.
914 if (wSublayer > sublayer) {
915 if (addToToken) {
916 token.windows.add(i, win);
917 }
918 placeWindowBefore(w, win);
919 break;
920 }
921 }
922 }
923 if (i >= NA) {
924 if (addToToken) {
925 token.windows.add(win);
926 }
927 if (sublayer < 0) {
928 placeWindowBefore(attached, win);
929 } else {
930 placeWindowAfter(largestSublayer >= 0
931 ? windowWithLargestSublayer
932 : attached,
933 win);
934 }
935 }
936 }
Romain Guy06882f82009-06-10 13:36:04 -0700937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938 if (win.mAppToken != null && addToToken) {
939 win.mAppToken.allAppWindows.add(win);
940 }
941 }
Romain Guy06882f82009-06-10 13:36:04 -0700942
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800943 static boolean canBeImeTarget(WindowState w) {
944 final int fl = w.mAttrs.flags
945 & (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM);
946 if (fl == 0 || fl == (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM)) {
947 return w.isVisibleOrAdding();
948 }
949 return false;
950 }
Romain Guy06882f82009-06-10 13:36:04 -0700951
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952 int findDesiredInputMethodWindowIndexLocked(boolean willMove) {
953 final ArrayList localmWindows = mWindows;
954 final int N = localmWindows.size();
955 WindowState w = null;
956 int i = N;
957 while (i > 0) {
958 i--;
959 w = (WindowState)localmWindows.get(i);
Romain Guy06882f82009-06-10 13:36:04 -0700960
Joe Onorato8a9b2202010-02-26 18:56:32 -0800961 //Slog.i(TAG, "Checking window @" + i + " " + w + " fl=0x"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962 // + Integer.toHexString(w.mAttrs.flags));
963 if (canBeImeTarget(w)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800964 //Slog.i(TAG, "Putting input method here!");
Romain Guy06882f82009-06-10 13:36:04 -0700965
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800966 // Yet more tricksyness! If this window is a "starting"
967 // window, we do actually want to be on top of it, but
968 // it is not -really- where input will go. So if the caller
969 // is not actually looking to move the IME, look down below
970 // for a real window to target...
971 if (!willMove
972 && w.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
973 && i > 0) {
974 WindowState wb = (WindowState)localmWindows.get(i-1);
975 if (wb.mAppToken == w.mAppToken && canBeImeTarget(wb)) {
976 i--;
977 w = wb;
978 }
979 }
980 break;
981 }
982 }
Romain Guy06882f82009-06-10 13:36:04 -0700983
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984 mUpcomingInputMethodTarget = w;
Romain Guy06882f82009-06-10 13:36:04 -0700985
Joe Onorato8a9b2202010-02-26 18:56:32 -0800986 if (DEBUG_INPUT_METHOD) Slog.v(TAG, "Desired input method target="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800987 + w + " willMove=" + willMove);
Romain Guy06882f82009-06-10 13:36:04 -0700988
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800989 if (willMove && w != null) {
990 final WindowState curTarget = mInputMethodTarget;
991 if (curTarget != null && curTarget.mAppToken != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700992
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800993 // Now some fun for dealing with window animations that
994 // modify the Z order. We need to look at all windows below
995 // the current target that are in this app, finding the highest
996 // visible one in layering.
997 AppWindowToken token = curTarget.mAppToken;
998 WindowState highestTarget = null;
999 int highestPos = 0;
1000 if (token.animating || token.animation != null) {
1001 int pos = 0;
1002 pos = localmWindows.indexOf(curTarget);
1003 while (pos >= 0) {
1004 WindowState win = (WindowState)localmWindows.get(pos);
1005 if (win.mAppToken != token) {
1006 break;
1007 }
1008 if (!win.mRemoved) {
1009 if (highestTarget == null || win.mAnimLayer >
1010 highestTarget.mAnimLayer) {
1011 highestTarget = win;
1012 highestPos = pos;
1013 }
1014 }
1015 pos--;
1016 }
1017 }
Romain Guy06882f82009-06-10 13:36:04 -07001018
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019 if (highestTarget != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001020 if (DEBUG_INPUT_METHOD) Slog.v(TAG, "mNextAppTransition="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 + mNextAppTransition + " " + highestTarget
1022 + " animating=" + highestTarget.isAnimating()
1023 + " layer=" + highestTarget.mAnimLayer
1024 + " new layer=" + w.mAnimLayer);
Romain Guy06882f82009-06-10 13:36:04 -07001025
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07001026 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 // If we are currently setting up for an animation,
1028 // hold everything until we can find out what will happen.
1029 mInputMethodTargetWaitingAnim = true;
1030 mInputMethodTarget = highestTarget;
1031 return highestPos + 1;
1032 } else if (highestTarget.isAnimating() &&
1033 highestTarget.mAnimLayer > w.mAnimLayer) {
1034 // If the window we are currently targeting is involved
1035 // with an animation, and it is on top of the next target
1036 // we will be over, then hold off on moving until
1037 // that is done.
1038 mInputMethodTarget = highestTarget;
1039 return highestPos + 1;
1040 }
1041 }
1042 }
1043 }
Romain Guy06882f82009-06-10 13:36:04 -07001044
Joe Onorato8a9b2202010-02-26 18:56:32 -08001045 //Slog.i(TAG, "Placing input method @" + (i+1));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 if (w != null) {
1047 if (willMove) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08001048 if (DEBUG_INPUT_METHOD) {
1049 RuntimeException e = null;
1050 if (!HIDE_STACK_CRAWLS) {
1051 e = new RuntimeException();
1052 e.fillInStackTrace();
1053 }
1054 Slog.w(TAG, "Moving IM target from "
1055 + mInputMethodTarget + " to " + w, e);
1056 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057 mInputMethodTarget = w;
1058 if (w.mAppToken != null) {
1059 setInputMethodAnimLayerAdjustment(w.mAppToken.animLayerAdjustment);
1060 } else {
1061 setInputMethodAnimLayerAdjustment(0);
1062 }
1063 }
1064 return i+1;
1065 }
1066 if (willMove) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08001067 if (DEBUG_INPUT_METHOD) {
1068 RuntimeException e = null;
1069 if (!HIDE_STACK_CRAWLS) {
1070 e = new RuntimeException();
1071 e.fillInStackTrace();
1072 }
1073 Slog.w(TAG, "Moving IM target from "
1074 + mInputMethodTarget + " to null", e);
1075 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076 mInputMethodTarget = null;
1077 setInputMethodAnimLayerAdjustment(0);
1078 }
1079 return -1;
1080 }
Romain Guy06882f82009-06-10 13:36:04 -07001081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001082 void addInputMethodWindowToListLocked(WindowState win) {
1083 int pos = findDesiredInputMethodWindowIndexLocked(true);
1084 if (pos >= 0) {
1085 win.mTargetAppToken = mInputMethodTarget.mAppToken;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001086 if (DEBUG_WINDOW_MOVEMENT) Slog.v(
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001087 TAG, "Adding input method window " + win + " at " + pos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088 mWindows.add(pos, win);
1089 moveInputMethodDialogsLocked(pos+1);
1090 return;
1091 }
1092 win.mTargetAppToken = null;
1093 addWindowToListInOrderLocked(win, true);
1094 moveInputMethodDialogsLocked(pos);
1095 }
Romain Guy06882f82009-06-10 13:36:04 -07001096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 void setInputMethodAnimLayerAdjustment(int adj) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001098 if (DEBUG_LAYERS) Slog.v(TAG, "Setting im layer adj to " + adj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099 mInputMethodAnimLayerAdjustment = adj;
1100 WindowState imw = mInputMethodWindow;
1101 if (imw != null) {
1102 imw.mAnimLayer = imw.mLayer + adj;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001103 if (DEBUG_LAYERS) Slog.v(TAG, "IM win " + imw
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001104 + " anim layer: " + imw.mAnimLayer);
1105 int wi = imw.mChildWindows.size();
1106 while (wi > 0) {
1107 wi--;
1108 WindowState cw = (WindowState)imw.mChildWindows.get(wi);
1109 cw.mAnimLayer = cw.mLayer + adj;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001110 if (DEBUG_LAYERS) Slog.v(TAG, "IM win " + cw
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 + " anim layer: " + cw.mAnimLayer);
1112 }
1113 }
1114 int di = mInputMethodDialogs.size();
1115 while (di > 0) {
1116 di --;
1117 imw = mInputMethodDialogs.get(di);
1118 imw.mAnimLayer = imw.mLayer + adj;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001119 if (DEBUG_LAYERS) Slog.v(TAG, "IM win " + imw
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120 + " anim layer: " + imw.mAnimLayer);
1121 }
1122 }
Romain Guy06882f82009-06-10 13:36:04 -07001123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001124 private int tmpRemoveWindowLocked(int interestingPos, WindowState win) {
1125 int wpos = mWindows.indexOf(win);
1126 if (wpos >= 0) {
1127 if (wpos < interestingPos) interestingPos--;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001128 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "Temp removing at " + wpos + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001129 mWindows.remove(wpos);
1130 int NC = win.mChildWindows.size();
1131 while (NC > 0) {
1132 NC--;
1133 WindowState cw = (WindowState)win.mChildWindows.get(NC);
1134 int cpos = mWindows.indexOf(cw);
1135 if (cpos >= 0) {
1136 if (cpos < interestingPos) interestingPos--;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001137 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "Temp removing child at "
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001138 + cpos + ": " + cw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001139 mWindows.remove(cpos);
1140 }
1141 }
1142 }
1143 return interestingPos;
1144 }
Romain Guy06882f82009-06-10 13:36:04 -07001145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146 private void reAddWindowToListInOrderLocked(WindowState win) {
1147 addWindowToListInOrderLocked(win, false);
1148 // This is a hack to get all of the child windows added as well
1149 // at the right position. Child windows should be rare and
1150 // this case should be rare, so it shouldn't be that big a deal.
1151 int wpos = mWindows.indexOf(win);
1152 if (wpos >= 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001153 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "ReAdd removing from " + wpos
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001154 + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 mWindows.remove(wpos);
1156 reAddWindowLocked(wpos, win);
1157 }
1158 }
Romain Guy06882f82009-06-10 13:36:04 -07001159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 void logWindowList(String prefix) {
1161 int N = mWindows.size();
1162 while (N > 0) {
1163 N--;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001164 Slog.v(TAG, prefix + "#" + N + ": " + mWindows.get(N));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001165 }
1166 }
Romain Guy06882f82009-06-10 13:36:04 -07001167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001168 void moveInputMethodDialogsLocked(int pos) {
1169 ArrayList<WindowState> dialogs = mInputMethodDialogs;
Romain Guy06882f82009-06-10 13:36:04 -07001170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 final int N = dialogs.size();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001172 if (DEBUG_INPUT_METHOD) Slog.v(TAG, "Removing " + N + " dialogs w/pos=" + pos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 for (int i=0; i<N; i++) {
1174 pos = tmpRemoveWindowLocked(pos, dialogs.get(i));
1175 }
1176 if (DEBUG_INPUT_METHOD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001177 Slog.v(TAG, "Window list w/pos=" + pos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 logWindowList(" ");
1179 }
Romain Guy06882f82009-06-10 13:36:04 -07001180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 if (pos >= 0) {
1182 final AppWindowToken targetAppToken = mInputMethodTarget.mAppToken;
1183 if (pos < mWindows.size()) {
1184 WindowState wp = (WindowState)mWindows.get(pos);
1185 if (wp == mInputMethodWindow) {
1186 pos++;
1187 }
1188 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001189 if (DEBUG_INPUT_METHOD) Slog.v(TAG, "Adding " + N + " dialogs at pos=" + pos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 for (int i=0; i<N; i++) {
1191 WindowState win = dialogs.get(i);
1192 win.mTargetAppToken = targetAppToken;
1193 pos = reAddWindowLocked(pos, win);
1194 }
1195 if (DEBUG_INPUT_METHOD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001196 Slog.v(TAG, "Final window list:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 logWindowList(" ");
1198 }
1199 return;
1200 }
1201 for (int i=0; i<N; i++) {
1202 WindowState win = dialogs.get(i);
1203 win.mTargetAppToken = null;
1204 reAddWindowToListInOrderLocked(win);
1205 if (DEBUG_INPUT_METHOD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001206 Slog.v(TAG, "No IM target, final list:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001207 logWindowList(" ");
1208 }
1209 }
1210 }
Romain Guy06882f82009-06-10 13:36:04 -07001211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 boolean moveInputMethodWindowsIfNeededLocked(boolean needAssignLayers) {
1213 final WindowState imWin = mInputMethodWindow;
1214 final int DN = mInputMethodDialogs.size();
1215 if (imWin == null && DN == 0) {
1216 return false;
1217 }
Romain Guy06882f82009-06-10 13:36:04 -07001218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219 int imPos = findDesiredInputMethodWindowIndexLocked(true);
1220 if (imPos >= 0) {
1221 // In this case, the input method windows are to be placed
1222 // immediately above the window they are targeting.
Romain Guy06882f82009-06-10 13:36:04 -07001223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 // First check to see if the input method windows are already
1225 // located here, and contiguous.
1226 final int N = mWindows.size();
1227 WindowState firstImWin = imPos < N
1228 ? (WindowState)mWindows.get(imPos) : null;
Romain Guy06882f82009-06-10 13:36:04 -07001229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 // Figure out the actual input method window that should be
1231 // at the bottom of their stack.
1232 WindowState baseImWin = imWin != null
1233 ? imWin : mInputMethodDialogs.get(0);
1234 if (baseImWin.mChildWindows.size() > 0) {
1235 WindowState cw = (WindowState)baseImWin.mChildWindows.get(0);
1236 if (cw.mSubLayer < 0) baseImWin = cw;
1237 }
Romain Guy06882f82009-06-10 13:36:04 -07001238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239 if (firstImWin == baseImWin) {
1240 // The windows haven't moved... but are they still contiguous?
1241 // First find the top IM window.
1242 int pos = imPos+1;
1243 while (pos < N) {
1244 if (!((WindowState)mWindows.get(pos)).mIsImWindow) {
1245 break;
1246 }
1247 pos++;
1248 }
1249 pos++;
1250 // Now there should be no more input method windows above.
1251 while (pos < N) {
1252 if (((WindowState)mWindows.get(pos)).mIsImWindow) {
1253 break;
1254 }
1255 pos++;
1256 }
1257 if (pos >= N) {
1258 // All is good!
1259 return false;
1260 }
1261 }
Romain Guy06882f82009-06-10 13:36:04 -07001262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 if (imWin != null) {
1264 if (DEBUG_INPUT_METHOD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001265 Slog.v(TAG, "Moving IM from " + imPos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 logWindowList(" ");
1267 }
1268 imPos = tmpRemoveWindowLocked(imPos, imWin);
1269 if (DEBUG_INPUT_METHOD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001270 Slog.v(TAG, "List after moving with new pos " + imPos + ":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271 logWindowList(" ");
1272 }
1273 imWin.mTargetAppToken = mInputMethodTarget.mAppToken;
1274 reAddWindowLocked(imPos, imWin);
1275 if (DEBUG_INPUT_METHOD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001276 Slog.v(TAG, "List after moving IM to " + imPos + ":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 logWindowList(" ");
1278 }
1279 if (DN > 0) moveInputMethodDialogsLocked(imPos+1);
1280 } else {
1281 moveInputMethodDialogsLocked(imPos);
1282 }
Romain Guy06882f82009-06-10 13:36:04 -07001283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 } else {
1285 // In this case, the input method windows go in a fixed layer,
1286 // because they aren't currently associated with a focus window.
Romain Guy06882f82009-06-10 13:36:04 -07001287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 if (imWin != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001289 if (DEBUG_INPUT_METHOD) Slog.v(TAG, "Moving IM from " + imPos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 tmpRemoveWindowLocked(0, imWin);
1291 imWin.mTargetAppToken = null;
1292 reAddWindowToListInOrderLocked(imWin);
1293 if (DEBUG_INPUT_METHOD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001294 Slog.v(TAG, "List with no IM target:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001295 logWindowList(" ");
1296 }
1297 if (DN > 0) moveInputMethodDialogsLocked(-1);;
1298 } else {
1299 moveInputMethodDialogsLocked(-1);;
1300 }
Romain Guy06882f82009-06-10 13:36:04 -07001301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001302 }
Romain Guy06882f82009-06-10 13:36:04 -07001303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304 if (needAssignLayers) {
1305 assignLayersLocked();
1306 }
Romain Guy06882f82009-06-10 13:36:04 -07001307
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001308 return true;
1309 }
Romain Guy06882f82009-06-10 13:36:04 -07001310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001311 void adjustInputMethodDialogsLocked() {
1312 moveInputMethodDialogsLocked(findDesiredInputMethodWindowIndexLocked(true));
1313 }
Romain Guy06882f82009-06-10 13:36:04 -07001314
Dianne Hackborn25994b42009-09-04 14:21:19 -07001315 final boolean isWallpaperVisible(WindowState wallpaperTarget) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001316 if (DEBUG_WALLPAPER) Slog.v(TAG, "Wallpaper vis: target obscured="
Dianne Hackborn25994b42009-09-04 14:21:19 -07001317 + (wallpaperTarget != null ? Boolean.toString(wallpaperTarget.mObscured) : "??")
1318 + " anim=" + ((wallpaperTarget != null && wallpaperTarget.mAppToken != null)
1319 ? wallpaperTarget.mAppToken.animation : null)
1320 + " upper=" + mUpperWallpaperTarget
1321 + " lower=" + mLowerWallpaperTarget);
1322 return (wallpaperTarget != null
1323 && (!wallpaperTarget.mObscured || (wallpaperTarget.mAppToken != null
1324 && wallpaperTarget.mAppToken.animation != null)))
1325 || mUpperWallpaperTarget != null
1326 || mLowerWallpaperTarget != null;
1327 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001328
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001329 static final int ADJUST_WALLPAPER_LAYERS_CHANGED = 1<<1;
1330 static final int ADJUST_WALLPAPER_VISIBILITY_CHANGED = 1<<2;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001331
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001332 int adjustWallpaperWindowsLocked() {
1333 int changed = 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001334
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001335 final int dw = mDisplay.getWidth();
1336 final int dh = mDisplay.getHeight();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001337
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001338 // First find top-most window that has asked to be on top of the
1339 // wallpaper; all wallpapers go behind it.
1340 final ArrayList localmWindows = mWindows;
1341 int N = localmWindows.size();
1342 WindowState w = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001343 WindowState foundW = null;
1344 int foundI = 0;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001345 WindowState topCurW = null;
1346 int topCurI = 0;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001347 int i = N;
1348 while (i > 0) {
1349 i--;
1350 w = (WindowState)localmWindows.get(i);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001351 if ((w.mAttrs.type == WindowManager.LayoutParams.TYPE_WALLPAPER)) {
1352 if (topCurW == null) {
1353 topCurW = w;
1354 topCurI = i;
1355 }
1356 continue;
1357 }
1358 topCurW = null;
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001359 if (w.mAppToken != null) {
1360 // If this window's app token is hidden and not animating,
1361 // it is of no interest to us.
1362 if (w.mAppToken.hidden && w.mAppToken.animation == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001363 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001364 "Skipping hidden or animating token: " + w);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001365 topCurW = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001366 continue;
1367 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001368 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001369 if (DEBUG_WALLPAPER) Slog.v(TAG, "Win " + w + ": readyfordisplay="
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001370 + w.isReadyForDisplay() + " drawpending=" + w.mDrawPending
1371 + " commitdrawpending=" + w.mCommitDrawPending);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001372 if ((w.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0 && w.isReadyForDisplay()
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07001373 && (mWallpaperTarget == w
1374 || (!w.mDrawPending && !w.mCommitDrawPending))) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001375 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001376 "Found wallpaper activity: #" + i + "=" + w);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001377 foundW = w;
1378 foundI = i;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001379 if (w == mWallpaperTarget && ((w.mAppToken != null
1380 && w.mAppToken.animation != null)
1381 || w.mAnimation != null)) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001382 // The current wallpaper target is animating, so we'll
1383 // look behind it for another possible target and figure
1384 // out what is going on below.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001385 if (DEBUG_WALLPAPER) Slog.v(TAG, "Win " + w
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001386 + ": token animating, looking behind.");
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001387 continue;
1388 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001389 break;
1390 }
1391 }
1392
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07001393 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001394 // If we are currently waiting for an app transition, and either
1395 // the current target or the next target are involved with it,
1396 // then hold off on doing anything with the wallpaper.
1397 // Note that we are checking here for just whether the target
1398 // is part of an app token... which is potentially overly aggressive
1399 // (the app token may not be involved in the transition), but good
1400 // enough (we'll just wait until whatever transition is pending
1401 // executes).
1402 if (mWallpaperTarget != null && mWallpaperTarget.mAppToken != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001403 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001404 "Wallpaper not changing: waiting for app anim in current target");
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001405 return 0;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001406 }
1407 if (foundW != null && foundW.mAppToken != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001408 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001409 "Wallpaper not changing: waiting for app anim in found target");
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001410 return 0;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001411 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001412 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001413
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001414 if (mWallpaperTarget != foundW) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001415 if (DEBUG_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001416 Slog.v(TAG, "New wallpaper target: " + foundW
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001417 + " oldTarget: " + mWallpaperTarget);
1418 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001419
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001420 mLowerWallpaperTarget = null;
1421 mUpperWallpaperTarget = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001422
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001423 WindowState oldW = mWallpaperTarget;
1424 mWallpaperTarget = foundW;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001425
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001426 // Now what is happening... if the current and new targets are
1427 // animating, then we are in our super special mode!
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001428 if (foundW != null && oldW != null) {
1429 boolean oldAnim = oldW.mAnimation != null
1430 || (oldW.mAppToken != null && oldW.mAppToken.animation != null);
1431 boolean foundAnim = foundW.mAnimation != null
1432 || (foundW.mAppToken != null && foundW.mAppToken.animation != null);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001433 if (DEBUG_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001434 Slog.v(TAG, "New animation: " + foundAnim
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001435 + " old animation: " + oldAnim);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001436 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001437 if (foundAnim && oldAnim) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001438 int oldI = localmWindows.indexOf(oldW);
1439 if (DEBUG_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001440 Slog.v(TAG, "New i: " + foundI + " old i: " + oldI);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001441 }
1442 if (oldI >= 0) {
1443 if (DEBUG_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001444 Slog.v(TAG, "Animating wallpapers: old#" + oldI
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001445 + "=" + oldW + "; new#" + foundI
1446 + "=" + foundW);
1447 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001448
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001449 // Set the new target correctly.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001450 if (foundW.mAppToken != null && foundW.mAppToken.hiddenRequested) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001451 if (DEBUG_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001452 Slog.v(TAG, "Old wallpaper still the target.");
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001453 }
1454 mWallpaperTarget = oldW;
1455 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001456
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001457 // Now set the upper and lower wallpaper targets
1458 // correctly, and make sure that we are positioning
1459 // the wallpaper below the lower.
1460 if (foundI > oldI) {
1461 // The new target is on top of the old one.
1462 if (DEBUG_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001463 Slog.v(TAG, "Found target above old target.");
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001464 }
1465 mUpperWallpaperTarget = foundW;
1466 mLowerWallpaperTarget = oldW;
1467 foundW = oldW;
1468 foundI = oldI;
1469 } else {
1470 // The new target is below the old one.
1471 if (DEBUG_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001472 Slog.v(TAG, "Found target below old target.");
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001473 }
1474 mUpperWallpaperTarget = oldW;
1475 mLowerWallpaperTarget = foundW;
1476 }
1477 }
1478 }
1479 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001480
Dianne Hackborn6b1cb352009-09-28 18:27:26 -07001481 } else if (mLowerWallpaperTarget != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001482 // Is it time to stop animating?
Dianne Hackborn6b1cb352009-09-28 18:27:26 -07001483 boolean lowerAnimating = mLowerWallpaperTarget.mAnimation != null
1484 || (mLowerWallpaperTarget.mAppToken != null
1485 && mLowerWallpaperTarget.mAppToken.animation != null);
1486 boolean upperAnimating = mUpperWallpaperTarget.mAnimation != null
1487 || (mUpperWallpaperTarget.mAppToken != null
1488 && mUpperWallpaperTarget.mAppToken.animation != null);
1489 if (!lowerAnimating || !upperAnimating) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001490 if (DEBUG_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001491 Slog.v(TAG, "No longer animating wallpaper targets!");
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001492 }
1493 mLowerWallpaperTarget = null;
1494 mUpperWallpaperTarget = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001495 }
1496 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001497
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001498 boolean visible = foundW != null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001499 if (visible) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001500 // The window is visible to the compositor... but is it visible
1501 // to the user? That is what the wallpaper cares about.
Dianne Hackborn25994b42009-09-04 14:21:19 -07001502 visible = isWallpaperVisible(foundW);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001503 if (DEBUG_WALLPAPER) Slog.v(TAG, "Wallpaper visibility: " + visible);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001504
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001505 // If the wallpaper target is animating, we may need to copy
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001506 // its layer adjustment. Only do this if we are not transfering
1507 // between two wallpaper targets.
1508 mWallpaperAnimLayerAdjustment =
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001509 (mLowerWallpaperTarget == null && foundW.mAppToken != null)
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001510 ? foundW.mAppToken.animLayerAdjustment : 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001511
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001512 final int maxLayer = mPolicy.getMaxWallpaperLayer()
1513 * TYPE_LAYER_MULTIPLIER
1514 + TYPE_LAYER_OFFSET;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001515
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001516 // Now w is the window we are supposed to be behind... but we
1517 // need to be sure to also be behind any of its attached windows,
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001518 // AND any starting window associated with it, AND below the
1519 // maximum layer the policy allows for wallpapers.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001520 while (foundI > 0) {
1521 WindowState wb = (WindowState)localmWindows.get(foundI-1);
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001522 if (wb.mBaseLayer < maxLayer &&
1523 wb.mAttachedWindow != foundW &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001524 (wb.mAttrs.type != TYPE_APPLICATION_STARTING ||
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001525 wb.mToken != foundW.mToken)) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001526 // This window is not related to the previous one in any
1527 // interesting way, so stop here.
1528 break;
1529 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001530 foundW = wb;
1531 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001532 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001533 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001534 if (DEBUG_WALLPAPER) Slog.v(TAG, "No wallpaper target");
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001535 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001536
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001537 if (foundW == null && topCurW != null) {
1538 // There is no wallpaper target, so it goes at the bottom.
1539 // We will assume it is the same place as last time, if known.
1540 foundW = topCurW;
1541 foundI = topCurI+1;
1542 } else {
1543 // Okay i is the position immediately above the wallpaper. Look at
1544 // what is below it for later.
1545 foundW = foundI > 0 ? (WindowState)localmWindows.get(foundI-1) : null;
1546 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001547
Dianne Hackborn284ac932009-08-28 10:34:25 -07001548 if (visible) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001549 if (mWallpaperTarget.mWallpaperX >= 0) {
1550 mLastWallpaperX = mWallpaperTarget.mWallpaperX;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001551 mLastWallpaperXStep = mWallpaperTarget.mWallpaperXStep;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001552 }
1553 if (mWallpaperTarget.mWallpaperY >= 0) {
1554 mLastWallpaperY = mWallpaperTarget.mWallpaperY;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001555 mLastWallpaperYStep = mWallpaperTarget.mWallpaperYStep;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001556 }
Dianne Hackborn284ac932009-08-28 10:34:25 -07001557 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001558
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001559 // Start stepping backwards from here, ensuring that our wallpaper windows
1560 // are correctly placed.
1561 int curTokenIndex = mWallpaperTokens.size();
1562 while (curTokenIndex > 0) {
1563 curTokenIndex--;
1564 WindowToken token = mWallpaperTokens.get(curTokenIndex);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001565 if (token.hidden == visible) {
1566 changed |= ADJUST_WALLPAPER_VISIBILITY_CHANGED;
1567 token.hidden = !visible;
1568 // Need to do a layout to ensure the wallpaper now has the
1569 // correct size.
1570 mLayoutNeeded = true;
1571 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001572
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001573 int curWallpaperIndex = token.windows.size();
1574 while (curWallpaperIndex > 0) {
1575 curWallpaperIndex--;
1576 WindowState wallpaper = token.windows.get(curWallpaperIndex);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001577
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001578 if (visible) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001579 updateWallpaperOffsetLocked(wallpaper, dw, dh, false);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001580 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001581
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001582 // First, make sure the client has the current visibility
1583 // state.
1584 if (wallpaper.mWallpaperVisible != visible) {
1585 wallpaper.mWallpaperVisible = visible;
1586 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001587 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001588 "Setting visibility of wallpaper " + wallpaper
1589 + ": " + visible);
1590 wallpaper.mClient.dispatchAppVisibility(visible);
1591 } catch (RemoteException e) {
1592 }
1593 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001594
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001595 wallpaper.mAnimLayer = wallpaper.mLayer + mWallpaperAnimLayerAdjustment;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001596 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Slog.v(TAG, "Wallpaper win "
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001597 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001598
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001599 // First, if this window is at the current index, then all
1600 // is well.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001601 if (wallpaper == foundW) {
1602 foundI--;
1603 foundW = foundI > 0
1604 ? (WindowState)localmWindows.get(foundI-1) : null;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001605 continue;
1606 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001607
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001608 // The window didn't match... the current wallpaper window,
1609 // wherever it is, is in the wrong place, so make sure it is
1610 // not in the list.
1611 int oldIndex = localmWindows.indexOf(wallpaper);
1612 if (oldIndex >= 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001613 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "Wallpaper removing at "
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001614 + oldIndex + ": " + wallpaper);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001615 localmWindows.remove(oldIndex);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001616 if (oldIndex < foundI) {
1617 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001618 }
1619 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001620
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001621 // Now stick it in.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001622 if (DEBUG_WALLPAPER || DEBUG_WINDOW_MOVEMENT) Slog.v(TAG,
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001623 "Moving wallpaper " + wallpaper
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001624 + " from " + oldIndex + " to " + foundI);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001625
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001626 localmWindows.add(foundI, wallpaper);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001627 changed |= ADJUST_WALLPAPER_LAYERS_CHANGED;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001628 }
1629 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001630
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001631 return changed;
1632 }
1633
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001634 void setWallpaperAnimLayerAdjustmentLocked(int adj) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001635 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001636 "Setting wallpaper layer adj to " + adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001637 mWallpaperAnimLayerAdjustment = adj;
1638 int curTokenIndex = mWallpaperTokens.size();
1639 while (curTokenIndex > 0) {
1640 curTokenIndex--;
1641 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1642 int curWallpaperIndex = token.windows.size();
1643 while (curWallpaperIndex > 0) {
1644 curWallpaperIndex--;
1645 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1646 wallpaper.mAnimLayer = wallpaper.mLayer + adj;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001647 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Slog.v(TAG, "Wallpaper win "
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001648 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001649 }
1650 }
1651 }
1652
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001653 boolean updateWallpaperOffsetLocked(WindowState wallpaperWin, int dw, int dh,
1654 boolean sync) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001655 boolean changed = false;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001656 boolean rawChanged = false;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001657 float wpx = mLastWallpaperX >= 0 ? mLastWallpaperX : 0.5f;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001658 float wpxs = mLastWallpaperXStep >= 0 ? mLastWallpaperXStep : -1.0f;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001659 int availw = wallpaperWin.mFrame.right-wallpaperWin.mFrame.left-dw;
1660 int offset = availw > 0 ? -(int)(availw*wpx+.5f) : 0;
1661 changed = wallpaperWin.mXOffset != offset;
1662 if (changed) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001663 if (DEBUG_WALLPAPER) Slog.v(TAG, "Update wallpaper "
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001664 + wallpaperWin + " x: " + offset);
1665 wallpaperWin.mXOffset = offset;
1666 }
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001667 if (wallpaperWin.mWallpaperX != wpx || wallpaperWin.mWallpaperXStep != wpxs) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001668 wallpaperWin.mWallpaperX = wpx;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001669 wallpaperWin.mWallpaperXStep = wpxs;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001670 rawChanged = true;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001671 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001672
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001673 float wpy = mLastWallpaperY >= 0 ? mLastWallpaperY : 0.5f;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001674 float wpys = mLastWallpaperYStep >= 0 ? mLastWallpaperYStep : -1.0f;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001675 int availh = wallpaperWin.mFrame.bottom-wallpaperWin.mFrame.top-dh;
1676 offset = availh > 0 ? -(int)(availh*wpy+.5f) : 0;
1677 if (wallpaperWin.mYOffset != offset) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001678 if (DEBUG_WALLPAPER) Slog.v(TAG, "Update wallpaper "
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001679 + wallpaperWin + " y: " + offset);
1680 changed = true;
1681 wallpaperWin.mYOffset = offset;
1682 }
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001683 if (wallpaperWin.mWallpaperY != wpy || wallpaperWin.mWallpaperYStep != wpys) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001684 wallpaperWin.mWallpaperY = wpy;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001685 wallpaperWin.mWallpaperYStep = wpys;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001686 rawChanged = true;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001687 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001688
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001689 if (rawChanged) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001690 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001691 if (DEBUG_WALLPAPER) Slog.v(TAG, "Report new wp offset "
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001692 + wallpaperWin + " x=" + wallpaperWin.mWallpaperX
1693 + " y=" + wallpaperWin.mWallpaperY);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001694 if (sync) {
Dianne Hackborn75804932009-10-20 20:15:20 -07001695 mWaitingOnWallpaper = wallpaperWin;
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001696 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001697 wallpaperWin.mClient.dispatchWallpaperOffsets(
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001698 wallpaperWin.mWallpaperX, wallpaperWin.mWallpaperY,
1699 wallpaperWin.mWallpaperXStep, wallpaperWin.mWallpaperYStep, sync);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001700 if (sync) {
Dianne Hackborn75804932009-10-20 20:15:20 -07001701 if (mWaitingOnWallpaper != null) {
1702 long start = SystemClock.uptimeMillis();
1703 if ((mLastWallpaperTimeoutTime+WALLPAPER_TIMEOUT_RECOVERY)
1704 < start) {
1705 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001706 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn75804932009-10-20 20:15:20 -07001707 "Waiting for offset complete...");
1708 mWindowMap.wait(WALLPAPER_TIMEOUT);
1709 } catch (InterruptedException e) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001710 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001711 if (DEBUG_WALLPAPER) Slog.v(TAG, "Offset complete!");
Dianne Hackborn75804932009-10-20 20:15:20 -07001712 if ((start+WALLPAPER_TIMEOUT)
1713 < SystemClock.uptimeMillis()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001714 Slog.i(TAG, "Timeout waiting for wallpaper to offset: "
Dianne Hackborn75804932009-10-20 20:15:20 -07001715 + wallpaperWin);
1716 mLastWallpaperTimeoutTime = start;
1717 }
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001718 }
Dianne Hackborn75804932009-10-20 20:15:20 -07001719 mWaitingOnWallpaper = null;
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001720 }
1721 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001722 } catch (RemoteException e) {
1723 }
1724 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001725
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001726 return changed;
1727 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001728
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001729 void wallpaperOffsetsComplete(IBinder window) {
Dianne Hackborn75804932009-10-20 20:15:20 -07001730 synchronized (mWindowMap) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001731 if (mWaitingOnWallpaper != null &&
1732 mWaitingOnWallpaper.mClient.asBinder() == window) {
1733 mWaitingOnWallpaper = null;
Dianne Hackborn75804932009-10-20 20:15:20 -07001734 mWindowMap.notifyAll();
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001735 }
1736 }
1737 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001738
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001739 boolean updateWallpaperOffsetLocked(WindowState changingTarget, boolean sync) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001740 final int dw = mDisplay.getWidth();
1741 final int dh = mDisplay.getHeight();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001742
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001743 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001744
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001745 WindowState target = mWallpaperTarget;
1746 if (target != null) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001747 if (target.mWallpaperX >= 0) {
1748 mLastWallpaperX = target.mWallpaperX;
1749 } else if (changingTarget.mWallpaperX >= 0) {
1750 mLastWallpaperX = changingTarget.mWallpaperX;
1751 }
1752 if (target.mWallpaperY >= 0) {
1753 mLastWallpaperY = target.mWallpaperY;
1754 } else if (changingTarget.mWallpaperY >= 0) {
1755 mLastWallpaperY = changingTarget.mWallpaperY;
1756 }
1757 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001758
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001759 int curTokenIndex = mWallpaperTokens.size();
1760 while (curTokenIndex > 0) {
1761 curTokenIndex--;
1762 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1763 int curWallpaperIndex = token.windows.size();
1764 while (curWallpaperIndex > 0) {
1765 curWallpaperIndex--;
1766 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1767 if (updateWallpaperOffsetLocked(wallpaper, dw, dh, sync)) {
1768 wallpaper.computeShownFrameLocked();
1769 changed = true;
1770 // We only want to be synchronous with one wallpaper.
1771 sync = false;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001772 }
1773 }
1774 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001775
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001776 return changed;
1777 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001778
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001779 void updateWallpaperVisibilityLocked() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07001780 final boolean visible = isWallpaperVisible(mWallpaperTarget);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001781 final int dw = mDisplay.getWidth();
1782 final int dh = mDisplay.getHeight();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001783
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001784 int curTokenIndex = mWallpaperTokens.size();
1785 while (curTokenIndex > 0) {
1786 curTokenIndex--;
1787 WindowToken token = mWallpaperTokens.get(curTokenIndex);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001788 if (token.hidden == visible) {
1789 token.hidden = !visible;
1790 // Need to do a layout to ensure the wallpaper now has the
1791 // correct size.
1792 mLayoutNeeded = true;
1793 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001794
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001795 int curWallpaperIndex = token.windows.size();
1796 while (curWallpaperIndex > 0) {
1797 curWallpaperIndex--;
1798 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1799 if (visible) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001800 updateWallpaperOffsetLocked(wallpaper, dw, dh, false);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001801 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001802
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001803 if (wallpaper.mWallpaperVisible != visible) {
1804 wallpaper.mWallpaperVisible = visible;
1805 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001806 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -07001807 "Updating visibility of wallpaper " + wallpaper
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001808 + ": " + visible);
1809 wallpaper.mClient.dispatchAppVisibility(visible);
1810 } catch (RemoteException e) {
1811 }
1812 }
1813 }
1814 }
1815 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001816
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001817 void sendPointerToWallpaperLocked(WindowState srcWin,
1818 MotionEvent pointer, long eventTime) {
1819 int curTokenIndex = mWallpaperTokens.size();
1820 while (curTokenIndex > 0) {
1821 curTokenIndex--;
1822 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1823 int curWallpaperIndex = token.windows.size();
1824 while (curWallpaperIndex > 0) {
1825 curWallpaperIndex--;
1826 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1827 if ((wallpaper.mAttrs.flags &
1828 WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
1829 continue;
1830 }
1831 try {
1832 MotionEvent ev = MotionEvent.obtainNoHistory(pointer);
Dianne Hackborn6adba242009-11-10 11:10:09 -08001833 if (srcWin != null) {
1834 ev.offsetLocation(srcWin.mFrame.left-wallpaper.mFrame.left,
1835 srcWin.mFrame.top-wallpaper.mFrame.top);
1836 } else {
1837 ev.offsetLocation(-wallpaper.mFrame.left, -wallpaper.mFrame.top);
1838 }
1839 switch (pointer.getAction()) {
1840 case MotionEvent.ACTION_DOWN:
1841 mSendingPointersToWallpaper = true;
1842 break;
1843 case MotionEvent.ACTION_UP:
1844 mSendingPointersToWallpaper = false;
1845 break;
1846 }
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001847 wallpaper.mClient.dispatchPointer(ev, eventTime, false);
1848 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001849 Slog.w(TAG, "Failure sending pointer to wallpaper", e);
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001850 }
1851 }
1852 }
1853 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001854
Dianne Hackborn90d2db32010-02-11 22:19:06 -08001855 void dispatchPointerElsewhereLocked(WindowState srcWin, WindowState relWin,
1856 MotionEvent pointer, long eventTime, boolean skipped) {
1857 if (relWin != null) {
1858 mPolicy.dispatchedPointerEventLw(pointer, relWin.mFrame.left, relWin.mFrame.top);
1859 } else {
1860 mPolicy.dispatchedPointerEventLw(pointer, 0, 0);
1861 }
1862
1863 // If we sent an initial down to the wallpaper, then continue
1864 // sending events until the final up.
1865 if (mSendingPointersToWallpaper) {
1866 if (skipped) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001867 Slog.i(TAG, "Sending skipped pointer to wallpaper!");
Dianne Hackborn90d2db32010-02-11 22:19:06 -08001868 }
1869 sendPointerToWallpaperLocked(relWin, pointer, eventTime);
1870
1871 // If we are on top of the wallpaper, then the wallpaper also
1872 // gets to see this movement.
1873 } else if (srcWin != null
1874 && pointer.getAction() == MotionEvent.ACTION_DOWN
1875 && mWallpaperTarget == srcWin
1876 && srcWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
1877 sendPointerToWallpaperLocked(relWin, pointer, eventTime);
1878 }
1879 }
1880
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001881 public int addWindow(Session session, IWindow client,
1882 WindowManager.LayoutParams attrs, int viewVisibility,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001883 Rect outContentInsets, InputChannel outInputChannel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001884 int res = mPolicy.checkAddPermission(attrs);
1885 if (res != WindowManagerImpl.ADD_OKAY) {
1886 return res;
1887 }
Romain Guy06882f82009-06-10 13:36:04 -07001888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001889 boolean reportNewConfig = false;
1890 WindowState attachedWindow = null;
1891 WindowState win = null;
Romain Guy06882f82009-06-10 13:36:04 -07001892
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001893 synchronized(mWindowMap) {
1894 // Instantiating a Display requires talking with the simulator,
1895 // so don't do it until we know the system is mostly up and
1896 // running.
1897 if (mDisplay == null) {
1898 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
1899 mDisplay = wm.getDefaultDisplay();
Christopher Tateb696aee2010-04-02 19:08:30 -07001900 mInitialDisplayWidth = mDisplay.getWidth();
1901 mInitialDisplayHeight = mDisplay.getHeight();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001902 if (ENABLE_NATIVE_INPUT_DISPATCH) {
1903 mInputManager.setDisplaySize(0,
1904 mInitialDisplayWidth, mInitialDisplayHeight);
1905 } else {
1906 mQueue.setDisplay(mDisplay);
1907 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001908 reportNewConfig = true;
1909 }
Romain Guy06882f82009-06-10 13:36:04 -07001910
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001911 if (mWindowMap.containsKey(client.asBinder())) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001912 Slog.w(TAG, "Window " + client + " is already added");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001913 return WindowManagerImpl.ADD_DUPLICATE_ADD;
1914 }
1915
1916 if (attrs.type >= FIRST_SUB_WINDOW && attrs.type <= LAST_SUB_WINDOW) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001917 attachedWindow = windowForClientLocked(null, attrs.token, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001918 if (attachedWindow == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001919 Slog.w(TAG, "Attempted to add window with token that is not a window: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001920 + attrs.token + ". Aborting.");
1921 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1922 }
1923 if (attachedWindow.mAttrs.type >= FIRST_SUB_WINDOW
1924 && attachedWindow.mAttrs.type <= LAST_SUB_WINDOW) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001925 Slog.w(TAG, "Attempted to add window with token that is a sub-window: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001926 + attrs.token + ". Aborting.");
1927 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1928 }
1929 }
1930
1931 boolean addToken = false;
1932 WindowToken token = mTokenMap.get(attrs.token);
1933 if (token == null) {
1934 if (attrs.type >= FIRST_APPLICATION_WINDOW
1935 && attrs.type <= LAST_APPLICATION_WINDOW) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001936 Slog.w(TAG, "Attempted to add application window with unknown token "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001937 + attrs.token + ". Aborting.");
1938 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1939 }
1940 if (attrs.type == TYPE_INPUT_METHOD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001941 Slog.w(TAG, "Attempted to add input method window with unknown token "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001942 + attrs.token + ". Aborting.");
1943 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1944 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001945 if (attrs.type == TYPE_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001946 Slog.w(TAG, "Attempted to add wallpaper window with unknown token "
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001947 + attrs.token + ". Aborting.");
1948 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1949 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001950 token = new WindowToken(attrs.token, -1, false);
1951 addToken = true;
1952 } else if (attrs.type >= FIRST_APPLICATION_WINDOW
1953 && attrs.type <= LAST_APPLICATION_WINDOW) {
1954 AppWindowToken atoken = token.appWindowToken;
1955 if (atoken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001956 Slog.w(TAG, "Attempted to add window with non-application token "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001957 + token + ". Aborting.");
1958 return WindowManagerImpl.ADD_NOT_APP_TOKEN;
1959 } else if (atoken.removed) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001960 Slog.w(TAG, "Attempted to add window with exiting application token "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001961 + token + ". Aborting.");
1962 return WindowManagerImpl.ADD_APP_EXITING;
1963 }
1964 if (attrs.type == TYPE_APPLICATION_STARTING && atoken.firstWindowDrawn) {
1965 // No need for this guy!
Joe Onorato8a9b2202010-02-26 18:56:32 -08001966 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001967 TAG, "**** NO NEED TO START: " + attrs.getTitle());
1968 return WindowManagerImpl.ADD_STARTING_NOT_NEEDED;
1969 }
1970 } else if (attrs.type == TYPE_INPUT_METHOD) {
1971 if (token.windowType != TYPE_INPUT_METHOD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001972 Slog.w(TAG, "Attempted to add input method window with bad token "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001973 + attrs.token + ". Aborting.");
1974 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1975 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001976 } else if (attrs.type == TYPE_WALLPAPER) {
1977 if (token.windowType != TYPE_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001978 Slog.w(TAG, "Attempted to add wallpaper window with bad token "
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001979 + attrs.token + ". Aborting.");
1980 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1981 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001982 }
1983
1984 win = new WindowState(session, client, token,
1985 attachedWindow, attrs, viewVisibility);
1986 if (win.mDeathRecipient == null) {
1987 // Client has apparently died, so there is no reason to
1988 // continue.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001989 Slog.w(TAG, "Adding window client " + client.asBinder()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001990 + " that is dead, aborting.");
1991 return WindowManagerImpl.ADD_APP_EXITING;
1992 }
1993
1994 mPolicy.adjustWindowParamsLw(win.mAttrs);
Romain Guy06882f82009-06-10 13:36:04 -07001995
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001996 res = mPolicy.prepareAddWindowLw(win, attrs);
1997 if (res != WindowManagerImpl.ADD_OKAY) {
1998 return res;
1999 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002000
2001 if (ENABLE_NATIVE_INPUT_DISPATCH) {
2002 if (outInputChannel != null) {
2003 String name = win.makeInputChannelName();
2004 InputChannel[] inputChannels = InputChannel.openInputChannelPair(name);
2005 win.mInputChannel = inputChannels[0];
2006 inputChannels[1].transferToBinderOutParameter(outInputChannel);
2007
2008 mInputManager.registerInputChannel(win.mInputChannel);
2009 }
2010 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002011
2012 // From now on, no exceptions or errors allowed!
2013
2014 res = WindowManagerImpl.ADD_OKAY;
Romain Guy06882f82009-06-10 13:36:04 -07002015
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002016 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002017
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002018 if (addToken) {
2019 mTokenMap.put(attrs.token, token);
2020 mTokenList.add(token);
2021 }
2022 win.attach();
2023 mWindowMap.put(client.asBinder(), win);
2024
2025 if (attrs.type == TYPE_APPLICATION_STARTING &&
2026 token.appWindowToken != null) {
2027 token.appWindowToken.startingWindow = win;
2028 }
2029
2030 boolean imMayMove = true;
Romain Guy06882f82009-06-10 13:36:04 -07002031
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002032 if (attrs.type == TYPE_INPUT_METHOD) {
2033 mInputMethodWindow = win;
2034 addInputMethodWindowToListLocked(win);
2035 imMayMove = false;
2036 } else if (attrs.type == TYPE_INPUT_METHOD_DIALOG) {
2037 mInputMethodDialogs.add(win);
2038 addWindowToListInOrderLocked(win, true);
2039 adjustInputMethodDialogsLocked();
2040 imMayMove = false;
2041 } else {
2042 addWindowToListInOrderLocked(win, true);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002043 if (attrs.type == TYPE_WALLPAPER) {
2044 mLastWallpaperTimeoutTime = 0;
2045 adjustWallpaperWindowsLocked();
2046 } else if ((attrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002047 adjustWallpaperWindowsLocked();
2048 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002049 }
Romain Guy06882f82009-06-10 13:36:04 -07002050
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002051 win.mEnterAnimationPending = true;
Romain Guy06882f82009-06-10 13:36:04 -07002052
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002053 mPolicy.getContentInsetHintLw(attrs, outContentInsets);
Romain Guy06882f82009-06-10 13:36:04 -07002054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002055 if (mInTouchMode) {
2056 res |= WindowManagerImpl.ADD_FLAG_IN_TOUCH_MODE;
2057 }
2058 if (win == null || win.mAppToken == null || !win.mAppToken.clientHidden) {
2059 res |= WindowManagerImpl.ADD_FLAG_APP_VISIBLE;
2060 }
Romain Guy06882f82009-06-10 13:36:04 -07002061
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08002062 boolean focusChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002063 if (win.canReceiveKeys()) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08002064 if ((focusChanged=updateFocusedWindowLocked(UPDATE_FOCUS_WILL_ASSIGN_LAYERS))
2065 == true) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002066 imMayMove = false;
2067 }
2068 }
Romain Guy06882f82009-06-10 13:36:04 -07002069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002070 if (imMayMove) {
Romain Guy06882f82009-06-10 13:36:04 -07002071 moveInputMethodWindowsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002072 }
Romain Guy06882f82009-06-10 13:36:04 -07002073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002074 assignLayersLocked();
2075 // Don't do layout here, the window must call
2076 // relayout to be displayed, so we'll do it there.
Romain Guy06882f82009-06-10 13:36:04 -07002077
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002078 //dump();
2079
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08002080 if (focusChanged) {
2081 if (mCurrentFocus != null) {
2082 mKeyWaiter.handleNewWindowLocked(mCurrentFocus);
2083 }
2084 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08002085 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002086 TAG, "New client " + client.asBinder()
2087 + ": window=" + win);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002088
2089 if (win.isVisibleOrAdding() && updateOrientationFromAppTokensLocked()) {
2090 reportNewConfig = true;
2091 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002092 }
2093
2094 // sendNewConfiguration() checks caller permissions so we must call it with
2095 // privilege. updateOrientationFromAppTokens() clears and resets the caller
2096 // identity anyway, so it's safe to just clear & restore around this whole
2097 // block.
2098 final long origId = Binder.clearCallingIdentity();
2099 if (reportNewConfig) {
2100 sendNewConfiguration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002101 }
2102 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07002103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002104 return res;
2105 }
Romain Guy06882f82009-06-10 13:36:04 -07002106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002107 public void removeWindow(Session session, IWindow client) {
2108 synchronized(mWindowMap) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002109 WindowState win = windowForClientLocked(session, client, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002110 if (win == null) {
2111 return;
2112 }
2113 removeWindowLocked(session, win);
2114 }
2115 }
Romain Guy06882f82009-06-10 13:36:04 -07002116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002117 public void removeWindowLocked(Session session, WindowState win) {
2118
Joe Onorato8a9b2202010-02-26 18:56:32 -08002119 if (localLOGV || DEBUG_FOCUS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002120 TAG, "Remove " + win + " client="
2121 + Integer.toHexString(System.identityHashCode(
2122 win.mClient.asBinder()))
2123 + ", surface=" + win.mSurface);
2124
2125 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002126
Joe Onorato8a9b2202010-02-26 18:56:32 -08002127 if (DEBUG_APP_TRANSITIONS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002128 TAG, "Remove " + win + ": mSurface=" + win.mSurface
2129 + " mExiting=" + win.mExiting
2130 + " isAnimating=" + win.isAnimating()
2131 + " app-animation="
2132 + (win.mAppToken != null ? win.mAppToken.animation : null)
2133 + " inPendingTransaction="
2134 + (win.mAppToken != null ? win.mAppToken.inPendingTransaction : false)
2135 + " mDisplayFrozen=" + mDisplayFrozen);
2136 // Visibility of the removed window. Will be used later to update orientation later on.
2137 boolean wasVisible = false;
2138 // First, see if we need to run an animation. If we do, we have
2139 // to hold off on removing the window until the animation is done.
2140 // If the display is frozen, just remove immediately, since the
2141 // animation wouldn't be seen.
Dianne Hackbornde2606d2009-12-18 16:53:55 -08002142 if (win.mSurface != null && !mDisplayFrozen && mPolicy.isScreenOn()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002143 // If we are not currently running the exit animation, we
2144 // need to see about starting one.
2145 if (wasVisible=win.isWinVisibleLw()) {
Romain Guy06882f82009-06-10 13:36:04 -07002146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002147 int transit = WindowManagerPolicy.TRANSIT_EXIT;
2148 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
2149 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
2150 }
2151 // Try starting an animation.
2152 if (applyAnimationLocked(win, transit, false)) {
2153 win.mExiting = true;
2154 }
2155 }
2156 if (win.mExiting || win.isAnimating()) {
2157 // The exit animation is running... wait for it!
Joe Onorato8a9b2202010-02-26 18:56:32 -08002158 //Slog.i(TAG, "*** Running exit animation...");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002159 win.mExiting = true;
2160 win.mRemoveOnExit = true;
2161 mLayoutNeeded = true;
2162 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
2163 performLayoutAndPlaceSurfacesLocked();
2164 if (win.mAppToken != null) {
2165 win.mAppToken.updateReportedVisibilityLocked();
2166 }
2167 //dump();
2168 Binder.restoreCallingIdentity(origId);
2169 return;
2170 }
2171 }
2172
2173 removeWindowInnerLocked(session, win);
2174 // Removing a visible window will effect the computed orientation
2175 // So just update orientation if needed.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002176 if (wasVisible && computeForcedAppOrientationLocked()
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002177 != mForcedAppOrientation
2178 && updateOrientationFromAppTokensLocked()) {
2179 mH.sendEmptyMessage(H.SEND_NEW_CONFIGURATION);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002180 }
2181 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2182 Binder.restoreCallingIdentity(origId);
2183 }
Romain Guy06882f82009-06-10 13:36:04 -07002184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002185 private void removeWindowInnerLocked(Session session, WindowState win) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07002186 mKeyWaiter.finishedKey(session, win.mClient, true,
2187 KeyWaiter.RETURN_NOTHING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002188 mKeyWaiter.releasePendingPointerLocked(win.mSession);
2189 mKeyWaiter.releasePendingTrackballLocked(win.mSession);
Romain Guy06882f82009-06-10 13:36:04 -07002190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002191 win.mRemoved = true;
Romain Guy06882f82009-06-10 13:36:04 -07002192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002193 if (mInputMethodTarget == win) {
2194 moveInputMethodWindowsIfNeededLocked(false);
2195 }
Romain Guy06882f82009-06-10 13:36:04 -07002196
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07002197 if (false) {
2198 RuntimeException e = new RuntimeException("here");
2199 e.fillInStackTrace();
Joe Onorato8a9b2202010-02-26 18:56:32 -08002200 Slog.w(TAG, "Removing window " + win, e);
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07002201 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002203 mPolicy.removeWindowLw(win);
2204 win.removeLocked();
2205
2206 mWindowMap.remove(win.mClient.asBinder());
2207 mWindows.remove(win);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002208 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "Final remove of window: " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002209
2210 if (mInputMethodWindow == win) {
2211 mInputMethodWindow = null;
2212 } else if (win.mAttrs.type == TYPE_INPUT_METHOD_DIALOG) {
2213 mInputMethodDialogs.remove(win);
2214 }
Romain Guy06882f82009-06-10 13:36:04 -07002215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002216 final WindowToken token = win.mToken;
2217 final AppWindowToken atoken = win.mAppToken;
2218 token.windows.remove(win);
2219 if (atoken != null) {
2220 atoken.allAppWindows.remove(win);
2221 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08002222 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002223 TAG, "**** Removing window " + win + ": count="
2224 + token.windows.size());
2225 if (token.windows.size() == 0) {
2226 if (!token.explicit) {
2227 mTokenMap.remove(token.token);
2228 mTokenList.remove(token);
2229 } else if (atoken != null) {
2230 atoken.firstWindowDrawn = false;
2231 }
2232 }
2233
2234 if (atoken != null) {
2235 if (atoken.startingWindow == win) {
2236 atoken.startingWindow = null;
2237 } else if (atoken.allAppWindows.size() == 0 && atoken.startingData != null) {
2238 // If this is the last window and we had requested a starting
2239 // transition window, well there is no point now.
2240 atoken.startingData = null;
2241 } else if (atoken.allAppWindows.size() == 1 && atoken.startingView != null) {
2242 // If this is the last window except for a starting transition
2243 // window, we need to get rid of the starting transition.
2244 if (DEBUG_STARTING_WINDOW) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002245 Slog.v(TAG, "Schedule remove starting " + token
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002246 + ": no more real windows");
2247 }
2248 Message m = mH.obtainMessage(H.REMOVE_STARTING, atoken);
2249 mH.sendMessage(m);
2250 }
2251 }
Romain Guy06882f82009-06-10 13:36:04 -07002252
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002253 if (win.mAttrs.type == TYPE_WALLPAPER) {
2254 mLastWallpaperTimeoutTime = 0;
2255 adjustWallpaperWindowsLocked();
2256 } else if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07002257 adjustWallpaperWindowsLocked();
2258 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002260 if (!mInLayout) {
2261 assignLayersLocked();
2262 mLayoutNeeded = true;
2263 performLayoutAndPlaceSurfacesLocked();
2264 if (win.mAppToken != null) {
2265 win.mAppToken.updateReportedVisibilityLocked();
2266 }
2267 }
2268 }
2269
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08002270 private static void logSurface(WindowState w, String msg, RuntimeException where) {
2271 String str = " SURFACE " + Integer.toHexString(w.hashCode())
2272 + ": " + msg + " / " + w.mAttrs.getTitle();
2273 if (where != null) {
2274 Slog.i(TAG, str, where);
2275 } else {
2276 Slog.i(TAG, str);
2277 }
2278 }
2279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002280 private void setTransparentRegionWindow(Session session, IWindow client, Region region) {
2281 long origId = Binder.clearCallingIdentity();
2282 try {
2283 synchronized (mWindowMap) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002284 WindowState w = windowForClientLocked(session, client, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002285 if ((w != null) && (w.mSurface != null)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002286 if (SHOW_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002287 Surface.openTransaction();
2288 try {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08002289 if (SHOW_TRANSACTIONS) logSurface(w,
2290 "transparentRegionHint=" + region, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002291 w.mSurface.setTransparentRegionHint(region);
2292 } finally {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002293 if (SHOW_TRANSACTIONS) Slog.i(TAG, "<<< CLOSE TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002294 Surface.closeTransaction();
2295 }
2296 }
2297 }
2298 } finally {
2299 Binder.restoreCallingIdentity(origId);
2300 }
2301 }
2302
2303 void setInsetsWindow(Session session, IWindow client,
Romain Guy06882f82009-06-10 13:36:04 -07002304 int touchableInsets, Rect contentInsets,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002305 Rect visibleInsets) {
2306 long origId = Binder.clearCallingIdentity();
2307 try {
2308 synchronized (mWindowMap) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002309 WindowState w = windowForClientLocked(session, client, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002310 if (w != null) {
2311 w.mGivenInsetsPending = false;
2312 w.mGivenContentInsets.set(contentInsets);
2313 w.mGivenVisibleInsets.set(visibleInsets);
2314 w.mTouchableInsets = touchableInsets;
2315 mLayoutNeeded = true;
2316 performLayoutAndPlaceSurfacesLocked();
2317 }
2318 }
2319 } finally {
2320 Binder.restoreCallingIdentity(origId);
2321 }
2322 }
Romain Guy06882f82009-06-10 13:36:04 -07002323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002324 public void getWindowDisplayFrame(Session session, IWindow client,
2325 Rect outDisplayFrame) {
2326 synchronized(mWindowMap) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002327 WindowState win = windowForClientLocked(session, client, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002328 if (win == null) {
2329 outDisplayFrame.setEmpty();
2330 return;
2331 }
2332 outDisplayFrame.set(win.mDisplayFrame);
2333 }
2334 }
2335
Marco Nelissenbf6956b2009-11-09 15:21:13 -08002336 public void setWindowWallpaperPositionLocked(WindowState window, float x, float y,
2337 float xStep, float yStep) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002338 if (window.mWallpaperX != x || window.mWallpaperY != y) {
2339 window.mWallpaperX = x;
2340 window.mWallpaperY = y;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08002341 window.mWallpaperXStep = xStep;
2342 window.mWallpaperYStep = yStep;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07002343 if (updateWallpaperOffsetLocked(window, true)) {
2344 performLayoutAndPlaceSurfacesLocked();
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002345 }
2346 }
2347 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002348
Dianne Hackborn75804932009-10-20 20:15:20 -07002349 void wallpaperCommandComplete(IBinder window, Bundle result) {
2350 synchronized (mWindowMap) {
2351 if (mWaitingOnWallpaper != null &&
2352 mWaitingOnWallpaper.mClient.asBinder() == window) {
2353 mWaitingOnWallpaper = null;
2354 mWindowMap.notifyAll();
2355 }
2356 }
2357 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002358
Dianne Hackborn75804932009-10-20 20:15:20 -07002359 public Bundle sendWindowWallpaperCommandLocked(WindowState window,
2360 String action, int x, int y, int z, Bundle extras, boolean sync) {
2361 if (window == mWallpaperTarget || window == mLowerWallpaperTarget
2362 || window == mUpperWallpaperTarget) {
2363 boolean doWait = sync;
2364 int curTokenIndex = mWallpaperTokens.size();
2365 while (curTokenIndex > 0) {
2366 curTokenIndex--;
2367 WindowToken token = mWallpaperTokens.get(curTokenIndex);
2368 int curWallpaperIndex = token.windows.size();
2369 while (curWallpaperIndex > 0) {
2370 curWallpaperIndex--;
2371 WindowState wallpaper = token.windows.get(curWallpaperIndex);
2372 try {
2373 wallpaper.mClient.dispatchWallpaperCommand(action,
2374 x, y, z, extras, sync);
2375 // We only want to be synchronous with one wallpaper.
2376 sync = false;
2377 } catch (RemoteException e) {
2378 }
2379 }
2380 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002381
Dianne Hackborn75804932009-10-20 20:15:20 -07002382 if (doWait) {
2383 // XXX Need to wait for result.
2384 }
2385 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002386
Dianne Hackborn75804932009-10-20 20:15:20 -07002387 return null;
2388 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002390 public int relayoutWindow(Session session, IWindow client,
2391 WindowManager.LayoutParams attrs, int requestedWidth,
2392 int requestedHeight, int viewVisibility, boolean insetsPending,
2393 Rect outFrame, Rect outContentInsets, Rect outVisibleInsets,
Dianne Hackborn694f79b2010-03-17 19:44:59 -07002394 Configuration outConfig, Surface outSurface) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002395 boolean displayed = false;
2396 boolean inTouchMode;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002397 boolean configChanged;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002398 long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002399
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002400 synchronized(mWindowMap) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002401 WindowState win = windowForClientLocked(session, client, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002402 if (win == null) {
2403 return 0;
2404 }
2405 win.mRequestedWidth = requestedWidth;
2406 win.mRequestedHeight = requestedHeight;
2407
2408 if (attrs != null) {
2409 mPolicy.adjustWindowParamsLw(attrs);
2410 }
Romain Guy06882f82009-06-10 13:36:04 -07002411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002412 int attrChanges = 0;
2413 int flagChanges = 0;
2414 if (attrs != null) {
2415 flagChanges = win.mAttrs.flags ^= attrs.flags;
2416 attrChanges = win.mAttrs.copyFrom(attrs);
2417 }
2418
Joe Onorato8a9b2202010-02-26 18:56:32 -08002419 if (DEBUG_LAYOUT) Slog.v(TAG, "Relayout " + win + ": " + win.mAttrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002420
2421 if ((attrChanges & WindowManager.LayoutParams.ALPHA_CHANGED) != 0) {
2422 win.mAlpha = attrs.alpha;
2423 }
2424
2425 final boolean scaledWindow =
2426 ((win.mAttrs.flags & WindowManager.LayoutParams.FLAG_SCALED) != 0);
2427
2428 if (scaledWindow) {
2429 // requested{Width|Height} Surface's physical size
2430 // attrs.{width|height} Size on screen
2431 win.mHScale = (attrs.width != requestedWidth) ?
2432 (attrs.width / (float)requestedWidth) : 1.0f;
2433 win.mVScale = (attrs.height != requestedHeight) ?
2434 (attrs.height / (float)requestedHeight) : 1.0f;
Dianne Hackborn9b52a212009-12-11 14:51:35 -08002435 } else {
2436 win.mHScale = win.mVScale = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002437 }
2438
2439 boolean imMayMove = (flagChanges&(
2440 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
2441 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07002442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002443 boolean focusMayChange = win.mViewVisibility != viewVisibility
2444 || ((flagChanges&WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) != 0)
2445 || (!win.mRelayoutCalled);
Romain Guy06882f82009-06-10 13:36:04 -07002446
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002447 boolean wallpaperMayMove = win.mViewVisibility != viewVisibility
2448 && (win.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002450 win.mRelayoutCalled = true;
2451 final int oldVisibility = win.mViewVisibility;
2452 win.mViewVisibility = viewVisibility;
2453 if (viewVisibility == View.VISIBLE &&
2454 (win.mAppToken == null || !win.mAppToken.clientHidden)) {
2455 displayed = !win.isVisibleLw();
2456 if (win.mExiting) {
2457 win.mExiting = false;
2458 win.mAnimation = null;
2459 }
2460 if (win.mDestroying) {
2461 win.mDestroying = false;
2462 mDestroySurface.remove(win);
2463 }
2464 if (oldVisibility == View.GONE) {
2465 win.mEnterAnimationPending = true;
2466 }
Dianne Hackborn694f79b2010-03-17 19:44:59 -07002467 if (displayed) {
2468 if (win.mSurface != null && !win.mDrawPending
2469 && !win.mCommitDrawPending && !mDisplayFrozen
2470 && mPolicy.isScreenOn()) {
2471 applyEnterAnimationLocked(win);
2472 }
2473 if ((win.mAttrs.flags
2474 & WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON) != 0) {
2475 if (DEBUG_VISIBILITY) Slog.v(TAG,
2476 "Relayout window turning screen on: " + win);
2477 win.mTurnOnScreen = true;
2478 }
2479 int diff = 0;
2480 if (win.mConfiguration != mCurConfiguration
2481 && (win.mConfiguration == null
2482 || (diff=mCurConfiguration.diff(win.mConfiguration)) != 0)) {
2483 win.mConfiguration = mCurConfiguration;
2484 if (DEBUG_CONFIGURATION) {
2485 Slog.i(TAG, "Window " + win + " visible with new config: "
2486 + win.mConfiguration + " / 0x"
2487 + Integer.toHexString(diff));
2488 }
2489 outConfig.setTo(mCurConfiguration);
2490 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07002491 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002492 if ((attrChanges&WindowManager.LayoutParams.FORMAT_CHANGED) != 0) {
2493 // To change the format, we need to re-build the surface.
2494 win.destroySurfaceLocked();
2495 displayed = true;
2496 }
2497 try {
2498 Surface surface = win.createSurfaceLocked();
2499 if (surface != null) {
2500 outSurface.copyFrom(surface);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002501 win.mReportDestroySurface = false;
2502 win.mSurfacePendingDestroy = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -08002503 if (SHOW_TRANSACTIONS) Slog.i(TAG,
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002504 " OUT SURFACE " + outSurface + ": copied");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002505 } else {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002506 // For some reason there isn't a surface. Clear the
2507 // caller's object so they see the same state.
2508 outSurface.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002509 }
2510 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002511 Slog.w(TAG, "Exception thrown when creating surface for client "
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002512 + client + " (" + win.mAttrs.getTitle() + ")",
2513 e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002514 Binder.restoreCallingIdentity(origId);
2515 return 0;
2516 }
2517 if (displayed) {
2518 focusMayChange = true;
2519 }
2520 if (win.mAttrs.type == TYPE_INPUT_METHOD
2521 && mInputMethodWindow == null) {
2522 mInputMethodWindow = win;
2523 imMayMove = true;
2524 }
Dianne Hackborn558947c2009-12-18 16:02:50 -08002525 if (win.mAttrs.type == TYPE_BASE_APPLICATION
2526 && win.mAppToken != null
2527 && win.mAppToken.startingWindow != null) {
2528 // Special handling of starting window over the base
2529 // window of the app: propagate lock screen flags to it,
2530 // to provide the correct semantics while starting.
2531 final int mask =
2532 WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
Mike Lockwoodef731622010-01-27 17:51:34 -05002533 | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
2534 | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON;
Dianne Hackborn558947c2009-12-18 16:02:50 -08002535 WindowManager.LayoutParams sa = win.mAppToken.startingWindow.mAttrs;
2536 sa.flags = (sa.flags&~mask) | (win.mAttrs.flags&mask);
2537 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002538 } else {
2539 win.mEnterAnimationPending = false;
2540 if (win.mSurface != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002541 if (DEBUG_VISIBILITY) Slog.i(TAG, "Relayout invis " + win
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002542 + ": mExiting=" + win.mExiting
2543 + " mSurfacePendingDestroy=" + win.mSurfacePendingDestroy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002544 // If we are not currently running the exit animation, we
2545 // need to see about starting one.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002546 if (!win.mExiting || win.mSurfacePendingDestroy) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002547 // Try starting an animation; if there isn't one, we
2548 // can destroy the surface right away.
2549 int transit = WindowManagerPolicy.TRANSIT_EXIT;
2550 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
2551 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
2552 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002553 if (!win.mSurfacePendingDestroy && win.isWinVisibleLw() &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002554 applyAnimationLocked(win, transit, false)) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002555 focusMayChange = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002556 win.mExiting = true;
2557 mKeyWaiter.finishedKey(session, client, true,
2558 KeyWaiter.RETURN_NOTHING);
2559 } else if (win.isAnimating()) {
2560 // Currently in a hide animation... turn this into
2561 // an exit.
2562 win.mExiting = true;
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07002563 } else if (win == mWallpaperTarget) {
2564 // If the wallpaper is currently behind this
2565 // window, we need to change both of them inside
2566 // of a transaction to avoid artifacts.
2567 win.mExiting = true;
2568 win.mAnimating = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002569 } else {
2570 if (mInputMethodWindow == win) {
2571 mInputMethodWindow = null;
2572 }
2573 win.destroySurfaceLocked();
2574 }
2575 }
2576 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002577
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002578 if (win.mSurface == null || (win.getAttrs().flags
2579 & WindowManager.LayoutParams.FLAG_KEEP_SURFACE_WHILE_ANIMATING) == 0
2580 || win.mSurfacePendingDestroy) {
2581 // We are being called from a local process, which
2582 // means outSurface holds its current surface. Ensure the
2583 // surface object is cleared, but we don't want it actually
2584 // destroyed at this point.
2585 win.mSurfacePendingDestroy = false;
2586 outSurface.release();
Joe Onorato8a9b2202010-02-26 18:56:32 -08002587 if (DEBUG_VISIBILITY) Slog.i(TAG, "Releasing surface in: " + win);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002588 } else if (win.mSurface != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002589 if (DEBUG_VISIBILITY) Slog.i(TAG,
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002590 "Keeping surface, will report destroy: " + win);
2591 win.mReportDestroySurface = true;
2592 outSurface.copyFrom(win.mSurface);
2593 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002594 }
2595
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002596 if (focusMayChange) {
2597 //System.out.println("Focus may change: " + win.mAttrs.getTitle());
2598 if (updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002599 imMayMove = false;
2600 }
2601 //System.out.println("Relayout " + win + ": focus=" + mCurrentFocus);
2602 }
Romain Guy06882f82009-06-10 13:36:04 -07002603
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08002604 // updateFocusedWindowLocked() already assigned layers so we only need to
2605 // reassign them at this point if the IM window state gets shuffled
2606 boolean assignLayers = false;
Romain Guy06882f82009-06-10 13:36:04 -07002607
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002608 if (imMayMove) {
Dianne Hackborn8abd5f02009-11-20 18:09:03 -08002609 if (moveInputMethodWindowsIfNeededLocked(false) || displayed) {
2610 // Little hack here -- we -should- be able to rely on the
2611 // function to return true if the IME has moved and needs
2612 // its layer recomputed. However, if the IME was hidden
2613 // and isn't actually moved in the list, its layer may be
2614 // out of data so we make sure to recompute it.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002615 assignLayers = true;
2616 }
2617 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002618 if (wallpaperMayMove) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002619 if ((adjustWallpaperWindowsLocked()&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002620 assignLayers = true;
2621 }
2622 }
Romain Guy06882f82009-06-10 13:36:04 -07002623
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002624 mLayoutNeeded = true;
2625 win.mGivenInsetsPending = insetsPending;
2626 if (assignLayers) {
2627 assignLayersLocked();
2628 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002629 configChanged = updateOrientationFromAppTokensLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002630 performLayoutAndPlaceSurfacesLocked();
Dianne Hackborn284ac932009-08-28 10:34:25 -07002631 if (displayed && win.mIsWallpaper) {
2632 updateWallpaperOffsetLocked(win, mDisplay.getWidth(),
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002633 mDisplay.getHeight(), false);
Dianne Hackborn284ac932009-08-28 10:34:25 -07002634 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002635 if (win.mAppToken != null) {
2636 win.mAppToken.updateReportedVisibilityLocked();
2637 }
2638 outFrame.set(win.mFrame);
2639 outContentInsets.set(win.mContentInsets);
2640 outVisibleInsets.set(win.mVisibleInsets);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002641 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002642 TAG, "Relayout given client " + client.asBinder()
Romain Guy06882f82009-06-10 13:36:04 -07002643 + ", requestedWidth=" + requestedWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002644 + ", requestedHeight=" + requestedHeight
2645 + ", viewVisibility=" + viewVisibility
2646 + "\nRelayout returning frame=" + outFrame
2647 + ", surface=" + outSurface);
2648
Joe Onorato8a9b2202010-02-26 18:56:32 -08002649 if (localLOGV || DEBUG_FOCUS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002650 TAG, "Relayout of " + win + ": focusMayChange=" + focusMayChange);
2651
2652 inTouchMode = mInTouchMode;
2653 }
2654
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002655 if (configChanged) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002656 sendNewConfiguration();
2657 }
Romain Guy06882f82009-06-10 13:36:04 -07002658
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002659 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07002660
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002661 return (inTouchMode ? WindowManagerImpl.RELAYOUT_IN_TOUCH_MODE : 0)
2662 | (displayed ? WindowManagerImpl.RELAYOUT_FIRST_TIME : 0);
2663 }
2664
2665 public void finishDrawingWindow(Session session, IWindow client) {
2666 final long origId = Binder.clearCallingIdentity();
2667 synchronized(mWindowMap) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002668 WindowState win = windowForClientLocked(session, client, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002669 if (win != null && win.finishDrawingLocked()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07002670 if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
2671 adjustWallpaperWindowsLocked();
2672 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002673 mLayoutNeeded = true;
2674 performLayoutAndPlaceSurfacesLocked();
2675 }
2676 }
2677 Binder.restoreCallingIdentity(origId);
2678 }
2679
2680 private AttributeCache.Entry getCachedAnimations(WindowManager.LayoutParams lp) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002681 if (DEBUG_ANIM) Slog.v(TAG, "Loading animations: params package="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002682 + (lp != null ? lp.packageName : null)
2683 + " resId=0x" + (lp != null ? Integer.toHexString(lp.windowAnimations) : null));
2684 if (lp != null && lp.windowAnimations != 0) {
2685 // If this is a system resource, don't try to load it from the
2686 // application resources. It is nice to avoid loading application
2687 // resources if we can.
2688 String packageName = lp.packageName != null ? lp.packageName : "android";
2689 int resId = lp.windowAnimations;
2690 if ((resId&0xFF000000) == 0x01000000) {
2691 packageName = "android";
2692 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08002693 if (DEBUG_ANIM) Slog.v(TAG, "Loading animations: picked package="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002694 + packageName);
2695 return AttributeCache.instance().get(packageName, resId,
2696 com.android.internal.R.styleable.WindowAnimation);
2697 }
2698 return null;
2699 }
Romain Guy06882f82009-06-10 13:36:04 -07002700
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002701 private AttributeCache.Entry getCachedAnimations(String packageName, int resId) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002702 if (DEBUG_ANIM) Slog.v(TAG, "Loading animations: params package="
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002703 + packageName + " resId=0x" + Integer.toHexString(resId));
2704 if (packageName != null) {
2705 if ((resId&0xFF000000) == 0x01000000) {
2706 packageName = "android";
2707 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08002708 if (DEBUG_ANIM) Slog.v(TAG, "Loading animations: picked package="
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002709 + packageName);
2710 return AttributeCache.instance().get(packageName, resId,
2711 com.android.internal.R.styleable.WindowAnimation);
2712 }
2713 return null;
2714 }
2715
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002716 private void applyEnterAnimationLocked(WindowState win) {
2717 int transit = WindowManagerPolicy.TRANSIT_SHOW;
2718 if (win.mEnterAnimationPending) {
2719 win.mEnterAnimationPending = false;
2720 transit = WindowManagerPolicy.TRANSIT_ENTER;
2721 }
2722
2723 applyAnimationLocked(win, transit, true);
2724 }
2725
2726 private boolean applyAnimationLocked(WindowState win,
2727 int transit, boolean isEntrance) {
2728 if (win.mLocalAnimating && win.mAnimationIsEntrance == isEntrance) {
2729 // If we are trying to apply an animation, but already running
2730 // an animation of the same type, then just leave that one alone.
2731 return true;
2732 }
Romain Guy06882f82009-06-10 13:36:04 -07002733
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002734 // Only apply an animation if the display isn't frozen. If it is
2735 // frozen, there is no reason to animate and it can cause strange
2736 // artifacts when we unfreeze the display if some different animation
2737 // is running.
Dianne Hackbornde2606d2009-12-18 16:53:55 -08002738 if (!mDisplayFrozen && mPolicy.isScreenOn()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002739 int anim = mPolicy.selectAnimationLw(win, transit);
2740 int attr = -1;
2741 Animation a = null;
2742 if (anim != 0) {
2743 a = AnimationUtils.loadAnimation(mContext, anim);
2744 } else {
2745 switch (transit) {
2746 case WindowManagerPolicy.TRANSIT_ENTER:
2747 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
2748 break;
2749 case WindowManagerPolicy.TRANSIT_EXIT:
2750 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
2751 break;
2752 case WindowManagerPolicy.TRANSIT_SHOW:
2753 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
2754 break;
2755 case WindowManagerPolicy.TRANSIT_HIDE:
2756 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
2757 break;
2758 }
2759 if (attr >= 0) {
2760 a = loadAnimation(win.mAttrs, attr);
2761 }
2762 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08002763 if (DEBUG_ANIM) Slog.v(TAG, "applyAnimation: win=" + win
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002764 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
2765 + " mAnimation=" + win.mAnimation
2766 + " isEntrance=" + isEntrance);
2767 if (a != null) {
2768 if (DEBUG_ANIM) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08002769 RuntimeException e = null;
2770 if (!HIDE_STACK_CRAWLS) {
2771 e = new RuntimeException();
2772 e.fillInStackTrace();
2773 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08002774 Slog.v(TAG, "Loaded animation " + a + " for " + win, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002775 }
2776 win.setAnimation(a);
2777 win.mAnimationIsEntrance = isEntrance;
2778 }
2779 } else {
2780 win.clearAnimation();
2781 }
2782
2783 return win.mAnimation != null;
2784 }
2785
2786 private Animation loadAnimation(WindowManager.LayoutParams lp, int animAttr) {
2787 int anim = 0;
2788 Context context = mContext;
2789 if (animAttr >= 0) {
2790 AttributeCache.Entry ent = getCachedAnimations(lp);
2791 if (ent != null) {
2792 context = ent.context;
2793 anim = ent.array.getResourceId(animAttr, 0);
2794 }
2795 }
2796 if (anim != 0) {
2797 return AnimationUtils.loadAnimation(context, anim);
2798 }
2799 return null;
2800 }
Romain Guy06882f82009-06-10 13:36:04 -07002801
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002802 private Animation loadAnimation(String packageName, int resId) {
2803 int anim = 0;
2804 Context context = mContext;
2805 if (resId >= 0) {
2806 AttributeCache.Entry ent = getCachedAnimations(packageName, resId);
2807 if (ent != null) {
2808 context = ent.context;
2809 anim = resId;
2810 }
2811 }
2812 if (anim != 0) {
2813 return AnimationUtils.loadAnimation(context, anim);
2814 }
2815 return null;
2816 }
2817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002818 private boolean applyAnimationLocked(AppWindowToken wtoken,
2819 WindowManager.LayoutParams lp, int transit, boolean enter) {
2820 // Only apply an animation if the display isn't frozen. If it is
2821 // frozen, there is no reason to animate and it can cause strange
2822 // artifacts when we unfreeze the display if some different animation
2823 // is running.
Dianne Hackbornde2606d2009-12-18 16:53:55 -08002824 if (!mDisplayFrozen && mPolicy.isScreenOn()) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002825 Animation a;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07002826 if (lp != null && (lp.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002827 a = new FadeInOutAnimation(enter);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002828 if (DEBUG_ANIM) Slog.v(TAG,
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002829 "applying FadeInOutAnimation for a window in compatibility mode");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002830 } else if (mNextAppTransitionPackage != null) {
2831 a = loadAnimation(mNextAppTransitionPackage, enter ?
2832 mNextAppTransitionEnter : mNextAppTransitionExit);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002833 } else {
2834 int animAttr = 0;
2835 switch (transit) {
2836 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
2837 animAttr = enter
2838 ? com.android.internal.R.styleable.WindowAnimation_activityOpenEnterAnimation
2839 : com.android.internal.R.styleable.WindowAnimation_activityOpenExitAnimation;
2840 break;
2841 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
2842 animAttr = enter
2843 ? com.android.internal.R.styleable.WindowAnimation_activityCloseEnterAnimation
2844 : com.android.internal.R.styleable.WindowAnimation_activityCloseExitAnimation;
2845 break;
2846 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
2847 animAttr = enter
2848 ? com.android.internal.R.styleable.WindowAnimation_taskOpenEnterAnimation
2849 : com.android.internal.R.styleable.WindowAnimation_taskOpenExitAnimation;
2850 break;
2851 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
2852 animAttr = enter
2853 ? com.android.internal.R.styleable.WindowAnimation_taskCloseEnterAnimation
2854 : com.android.internal.R.styleable.WindowAnimation_taskCloseExitAnimation;
2855 break;
2856 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
2857 animAttr = enter
2858 ? com.android.internal.R.styleable.WindowAnimation_taskToFrontEnterAnimation
2859 : com.android.internal.R.styleable.WindowAnimation_taskToFrontExitAnimation;
2860 break;
2861 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
2862 animAttr = enter
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07002863 ? com.android.internal.R.styleable.WindowAnimation_taskToBackEnterAnimation
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002864 : com.android.internal.R.styleable.WindowAnimation_taskToBackExitAnimation;
2865 break;
Dianne Hackborn25994b42009-09-04 14:21:19 -07002866 case WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN:
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002867 animAttr = enter
Dianne Hackborn25994b42009-09-04 14:21:19 -07002868 ? com.android.internal.R.styleable.WindowAnimation_wallpaperOpenEnterAnimation
2869 : com.android.internal.R.styleable.WindowAnimation_wallpaperOpenExitAnimation;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002870 break;
Dianne Hackborn25994b42009-09-04 14:21:19 -07002871 case WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE:
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002872 animAttr = enter
Dianne Hackborn25994b42009-09-04 14:21:19 -07002873 ? com.android.internal.R.styleable.WindowAnimation_wallpaperCloseEnterAnimation
2874 : com.android.internal.R.styleable.WindowAnimation_wallpaperCloseExitAnimation;
2875 break;
2876 case WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN:
2877 animAttr = enter
2878 ? com.android.internal.R.styleable.WindowAnimation_wallpaperIntraOpenEnterAnimation
2879 : com.android.internal.R.styleable.WindowAnimation_wallpaperIntraOpenExitAnimation;
2880 break;
2881 case WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE:
2882 animAttr = enter
2883 ? com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseEnterAnimation
2884 : com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseExitAnimation;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002885 break;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002886 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07002887 a = animAttr != 0 ? loadAnimation(lp, animAttr) : null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08002888 if (DEBUG_ANIM) Slog.v(TAG, "applyAnimation: wtoken=" + wtoken
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002889 + " anim=" + a
2890 + " animAttr=0x" + Integer.toHexString(animAttr)
2891 + " transit=" + transit);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002892 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002893 if (a != null) {
2894 if (DEBUG_ANIM) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08002895 RuntimeException e = null;
2896 if (!HIDE_STACK_CRAWLS) {
2897 e = new RuntimeException();
2898 e.fillInStackTrace();
2899 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08002900 Slog.v(TAG, "Loaded animation " + a + " for " + wtoken, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002901 }
2902 wtoken.setAnimation(a);
2903 }
2904 } else {
2905 wtoken.clearAnimation();
2906 }
2907
2908 return wtoken.animation != null;
2909 }
2910
2911 // -------------------------------------------------------------
2912 // Application Window Tokens
2913 // -------------------------------------------------------------
2914
2915 public void validateAppTokens(List tokens) {
2916 int v = tokens.size()-1;
2917 int m = mAppTokens.size()-1;
2918 while (v >= 0 && m >= 0) {
2919 AppWindowToken wtoken = mAppTokens.get(m);
2920 if (wtoken.removed) {
2921 m--;
2922 continue;
2923 }
2924 if (tokens.get(v) != wtoken.token) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002925 Slog.w(TAG, "Tokens out of sync: external is " + tokens.get(v)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002926 + " @ " + v + ", internal is " + wtoken.token + " @ " + m);
2927 }
2928 v--;
2929 m--;
2930 }
2931 while (v >= 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002932 Slog.w(TAG, "External token not found: " + tokens.get(v) + " @ " + v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002933 v--;
2934 }
2935 while (m >= 0) {
2936 AppWindowToken wtoken = mAppTokens.get(m);
2937 if (!wtoken.removed) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002938 Slog.w(TAG, "Invalid internal token: " + wtoken.token + " @ " + m);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002939 }
2940 m--;
2941 }
2942 }
2943
2944 boolean checkCallingPermission(String permission, String func) {
2945 // Quick check: if the calling permission is me, it's all okay.
2946 if (Binder.getCallingPid() == Process.myPid()) {
2947 return true;
2948 }
Romain Guy06882f82009-06-10 13:36:04 -07002949
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002950 if (mContext.checkCallingPermission(permission)
2951 == PackageManager.PERMISSION_GRANTED) {
2952 return true;
2953 }
2954 String msg = "Permission Denial: " + func + " from pid="
2955 + Binder.getCallingPid()
2956 + ", uid=" + Binder.getCallingUid()
2957 + " requires " + permission;
Joe Onorato8a9b2202010-02-26 18:56:32 -08002958 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002959 return false;
2960 }
Romain Guy06882f82009-06-10 13:36:04 -07002961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002962 AppWindowToken findAppWindowToken(IBinder token) {
2963 WindowToken wtoken = mTokenMap.get(token);
2964 if (wtoken == null) {
2965 return null;
2966 }
2967 return wtoken.appWindowToken;
2968 }
Romain Guy06882f82009-06-10 13:36:04 -07002969
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002970 public void addWindowToken(IBinder token, int type) {
2971 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2972 "addWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002973 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002974 }
Romain Guy06882f82009-06-10 13:36:04 -07002975
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002976 synchronized(mWindowMap) {
2977 WindowToken wtoken = mTokenMap.get(token);
2978 if (wtoken != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002979 Slog.w(TAG, "Attempted to add existing input method token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002980 return;
2981 }
2982 wtoken = new WindowToken(token, type, true);
2983 mTokenMap.put(token, wtoken);
2984 mTokenList.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002985 if (type == TYPE_WALLPAPER) {
2986 mWallpaperTokens.add(wtoken);
2987 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002988 }
2989 }
Romain Guy06882f82009-06-10 13:36:04 -07002990
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002991 public void removeWindowToken(IBinder token) {
2992 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2993 "removeWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002994 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002995 }
2996
2997 final long origId = Binder.clearCallingIdentity();
2998 synchronized(mWindowMap) {
2999 WindowToken wtoken = mTokenMap.remove(token);
3000 mTokenList.remove(wtoken);
3001 if (wtoken != null) {
3002 boolean delayed = false;
3003 if (!wtoken.hidden) {
3004 wtoken.hidden = true;
Romain Guy06882f82009-06-10 13:36:04 -07003005
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003006 final int N = wtoken.windows.size();
3007 boolean changed = false;
Romain Guy06882f82009-06-10 13:36:04 -07003008
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003009 for (int i=0; i<N; i++) {
3010 WindowState win = wtoken.windows.get(i);
3011
3012 if (win.isAnimating()) {
3013 delayed = true;
3014 }
Romain Guy06882f82009-06-10 13:36:04 -07003015
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003016 if (win.isVisibleNow()) {
3017 applyAnimationLocked(win,
3018 WindowManagerPolicy.TRANSIT_EXIT, false);
3019 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
3020 KeyWaiter.RETURN_NOTHING);
3021 changed = true;
3022 }
3023 }
3024
3025 if (changed) {
3026 mLayoutNeeded = true;
3027 performLayoutAndPlaceSurfacesLocked();
3028 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3029 }
Romain Guy06882f82009-06-10 13:36:04 -07003030
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003031 if (delayed) {
3032 mExitingTokens.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07003033 } else if (wtoken.windowType == TYPE_WALLPAPER) {
3034 mWallpaperTokens.remove(wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003035 }
3036 }
Romain Guy06882f82009-06-10 13:36:04 -07003037
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003038 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003039 Slog.w(TAG, "Attempted to remove non-existing token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003040 }
3041 }
3042 Binder.restoreCallingIdentity(origId);
3043 }
3044
3045 public void addAppToken(int addPos, IApplicationToken token,
3046 int groupId, int requestedOrientation, boolean fullscreen) {
3047 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3048 "addAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003049 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003050 }
Romain Guy06882f82009-06-10 13:36:04 -07003051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003052 synchronized(mWindowMap) {
3053 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
3054 if (wtoken != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003055 Slog.w(TAG, "Attempted to add existing app token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003056 return;
3057 }
3058 wtoken = new AppWindowToken(token);
3059 wtoken.groupId = groupId;
3060 wtoken.appFullscreen = fullscreen;
3061 wtoken.requestedOrientation = requestedOrientation;
3062 mAppTokens.add(addPos, wtoken);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003063 if (localLOGV) Slog.v(TAG, "Adding new app token: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003064 mTokenMap.put(token.asBinder(), wtoken);
3065 mTokenList.add(wtoken);
Romain Guy06882f82009-06-10 13:36:04 -07003066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003067 // Application tokens start out hidden.
3068 wtoken.hidden = true;
3069 wtoken.hiddenRequested = true;
Romain Guy06882f82009-06-10 13:36:04 -07003070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003071 //dump();
3072 }
3073 }
Romain Guy06882f82009-06-10 13:36:04 -07003074
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003075 public void setAppGroupId(IBinder token, int groupId) {
3076 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3077 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003078 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003079 }
3080
3081 synchronized(mWindowMap) {
3082 AppWindowToken wtoken = findAppWindowToken(token);
3083 if (wtoken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003084 Slog.w(TAG, "Attempted to set group id of non-existing app token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003085 return;
3086 }
3087 wtoken.groupId = groupId;
3088 }
3089 }
Romain Guy06882f82009-06-10 13:36:04 -07003090
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003091 public int getOrientationFromWindowsLocked() {
3092 int pos = mWindows.size() - 1;
3093 while (pos >= 0) {
3094 WindowState wtoken = (WindowState) mWindows.get(pos);
3095 pos--;
3096 if (wtoken.mAppToken != null) {
3097 // We hit an application window. so the orientation will be determined by the
3098 // app window. No point in continuing further.
3099 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
3100 }
Christopher Tateb696aee2010-04-02 19:08:30 -07003101 if (!wtoken.isVisibleLw() || !wtoken.mPolicyVisibilityAfterAnim) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003102 continue;
3103 }
3104 int req = wtoken.mAttrs.screenOrientation;
3105 if((req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) ||
3106 (req == ActivityInfo.SCREEN_ORIENTATION_BEHIND)){
3107 continue;
3108 } else {
3109 return req;
3110 }
3111 }
3112 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
3113 }
Romain Guy06882f82009-06-10 13:36:04 -07003114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003115 public int getOrientationFromAppTokensLocked() {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003116 int pos = mAppTokens.size() - 1;
3117 int curGroup = 0;
3118 int lastOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
3119 boolean findingBehind = false;
3120 boolean haveGroup = false;
3121 boolean lastFullscreen = false;
3122 while (pos >= 0) {
3123 AppWindowToken wtoken = mAppTokens.get(pos);
3124 pos--;
3125 // if we're about to tear down this window and not seek for
3126 // the behind activity, don't use it for orientation
3127 if (!findingBehind
3128 && (!wtoken.hidden && wtoken.hiddenRequested)) {
3129 continue;
3130 }
3131
3132 if (!haveGroup) {
3133 // We ignore any hidden applications on the top.
3134 if (wtoken.hiddenRequested || wtoken.willBeHidden) {
The Android Open Source Project10592532009-03-18 17:39:46 -07003135 continue;
3136 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003137 haveGroup = true;
3138 curGroup = wtoken.groupId;
3139 lastOrientation = wtoken.requestedOrientation;
3140 } else if (curGroup != wtoken.groupId) {
3141 // If we have hit a new application group, and the bottom
3142 // of the previous group didn't explicitly say to use
3143 // the orientation behind it, and the last app was
3144 // full screen, then we'll stick with the
3145 // user's orientation.
3146 if (lastOrientation != ActivityInfo.SCREEN_ORIENTATION_BEHIND
3147 && lastFullscreen) {
3148 return lastOrientation;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003149 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003150 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003151 int or = wtoken.requestedOrientation;
3152 // If this application is fullscreen, and didn't explicitly say
3153 // to use the orientation behind it, then just take whatever
3154 // orientation it has and ignores whatever is under it.
3155 lastFullscreen = wtoken.appFullscreen;
3156 if (lastFullscreen
3157 && or != ActivityInfo.SCREEN_ORIENTATION_BEHIND) {
3158 return or;
3159 }
3160 // If this application has requested an explicit orientation,
3161 // then use it.
3162 if (or == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE ||
3163 or == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ||
3164 or == ActivityInfo.SCREEN_ORIENTATION_SENSOR ||
3165 or == ActivityInfo.SCREEN_ORIENTATION_NOSENSOR ||
3166 or == ActivityInfo.SCREEN_ORIENTATION_USER) {
3167 return or;
3168 }
3169 findingBehind |= (or == ActivityInfo.SCREEN_ORIENTATION_BEHIND);
3170 }
3171 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003172 }
Romain Guy06882f82009-06-10 13:36:04 -07003173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003174 public Configuration updateOrientationFromAppTokens(
The Android Open Source Project10592532009-03-18 17:39:46 -07003175 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003176 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3177 "updateOrientationFromAppTokens()")) {
3178 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
3179 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003180
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003181 Configuration config = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003182 long ident = Binder.clearCallingIdentity();
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003183
3184 synchronized(mWindowMap) {
3185 if (updateOrientationFromAppTokensLocked()) {
3186 if (freezeThisOneIfNeeded != null) {
3187 AppWindowToken wtoken = findAppWindowToken(
3188 freezeThisOneIfNeeded);
3189 if (wtoken != null) {
3190 startAppFreezingScreenLocked(wtoken,
3191 ActivityInfo.CONFIG_ORIENTATION);
3192 }
3193 }
3194 config = computeNewConfigurationLocked();
3195
3196 } else if (currentConfig != null) {
3197 // No obvious action we need to take, but if our current
3198 // state mismatches the activity maanager's, update it
3199 mTempConfiguration.setToDefaults();
3200 if (computeNewConfigurationLocked(mTempConfiguration)) {
3201 if (currentConfig.diff(mTempConfiguration) != 0) {
3202 mWaitingForConfig = true;
3203 mLayoutNeeded = true;
3204 startFreezingDisplayLocked();
3205 config = new Configuration(mTempConfiguration);
3206 }
3207 }
3208 }
3209 }
3210
Dianne Hackborncfaef692009-06-15 14:24:44 -07003211 Binder.restoreCallingIdentity(ident);
3212 return config;
3213 }
3214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003215 /*
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003216 * Determine the new desired orientation of the display, returning
3217 * a non-null new Configuration if it has changed from the current
3218 * orientation. IF TRUE IS RETURNED SOMEONE MUST CALL
3219 * setNewConfiguration() TO TELL THE WINDOW MANAGER IT CAN UNFREEZE THE
3220 * SCREEN. This will typically be done for you if you call
3221 * sendNewConfiguration().
3222 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003223 * The orientation is computed from non-application windows first. If none of
3224 * the non-application windows specify orientation, the orientation is computed from
Romain Guy06882f82009-06-10 13:36:04 -07003225 * application tokens.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003226 * @see android.view.IWindowManager#updateOrientationFromAppTokens(
3227 * android.os.IBinder)
3228 */
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003229 boolean updateOrientationFromAppTokensLocked() {
Christopher Tateb696aee2010-04-02 19:08:30 -07003230 if (mDisplayFrozen) {
3231 // If the display is frozen, some activities may be in the middle
3232 // of restarting, and thus have removed their old window. If the
3233 // window has the flag to hide the lock screen, then the lock screen
3234 // can re-appear and inflict its own orientation on us. Keep the
3235 // orientation stable until this all settles down.
3236 return false;
3237 }
3238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003239 boolean changed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003240 long ident = Binder.clearCallingIdentity();
3241 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003242 int req = computeForcedAppOrientationLocked();
Romain Guy06882f82009-06-10 13:36:04 -07003243
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003244 if (req != mForcedAppOrientation) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003245 mForcedAppOrientation = req;
3246 //send a message to Policy indicating orientation change to take
3247 //action like disabling/enabling sensors etc.,
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003248 mPolicy.setCurrentOrientationLw(req);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003249 if (setRotationUncheckedLocked(WindowManagerPolicy.USE_LAST_ROTATION,
3250 mLastRotationFlags | Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE)) {
3251 changed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003252 }
3253 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003254
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003255 return changed;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003256 } finally {
3257 Binder.restoreCallingIdentity(ident);
3258 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003259 }
Romain Guy06882f82009-06-10 13:36:04 -07003260
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003261 int computeForcedAppOrientationLocked() {
3262 int req = getOrientationFromWindowsLocked();
3263 if (req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
3264 req = getOrientationFromAppTokensLocked();
3265 }
3266 return req;
3267 }
Romain Guy06882f82009-06-10 13:36:04 -07003268
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003269 public void setNewConfiguration(Configuration config) {
3270 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3271 "setNewConfiguration()")) {
3272 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
3273 }
3274
3275 synchronized(mWindowMap) {
3276 mCurConfiguration = new Configuration(config);
3277 mWaitingForConfig = false;
3278 performLayoutAndPlaceSurfacesLocked();
3279 }
3280 }
3281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003282 public void setAppOrientation(IApplicationToken token, int requestedOrientation) {
3283 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3284 "setAppOrientation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003285 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003286 }
Romain Guy06882f82009-06-10 13:36:04 -07003287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003288 synchronized(mWindowMap) {
3289 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
3290 if (wtoken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003291 Slog.w(TAG, "Attempted to set orientation of non-existing app token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003292 return;
3293 }
Romain Guy06882f82009-06-10 13:36:04 -07003294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003295 wtoken.requestedOrientation = requestedOrientation;
3296 }
3297 }
Romain Guy06882f82009-06-10 13:36:04 -07003298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003299 public int getAppOrientation(IApplicationToken token) {
3300 synchronized(mWindowMap) {
3301 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
3302 if (wtoken == null) {
3303 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
3304 }
Romain Guy06882f82009-06-10 13:36:04 -07003305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003306 return wtoken.requestedOrientation;
3307 }
3308 }
Romain Guy06882f82009-06-10 13:36:04 -07003309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003310 public void setFocusedApp(IBinder token, boolean moveFocusNow) {
3311 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3312 "setFocusedApp()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003313 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003314 }
3315
3316 synchronized(mWindowMap) {
3317 boolean changed = false;
3318 if (token == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003319 if (DEBUG_FOCUS) Slog.v(TAG, "Clearing focused app, was " + mFocusedApp);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003320 changed = mFocusedApp != null;
3321 mFocusedApp = null;
3322 mKeyWaiter.tickle();
3323 } else {
3324 AppWindowToken newFocus = findAppWindowToken(token);
3325 if (newFocus == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003326 Slog.w(TAG, "Attempted to set focus to non-existing app token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003327 return;
3328 }
3329 changed = mFocusedApp != newFocus;
3330 mFocusedApp = newFocus;
Joe Onorato8a9b2202010-02-26 18:56:32 -08003331 if (DEBUG_FOCUS) Slog.v(TAG, "Set focused app to: " + mFocusedApp);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003332 mKeyWaiter.tickle();
3333 }
3334
3335 if (moveFocusNow && changed) {
3336 final long origId = Binder.clearCallingIdentity();
3337 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3338 Binder.restoreCallingIdentity(origId);
3339 }
3340 }
3341 }
3342
3343 public void prepareAppTransition(int transit) {
3344 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3345 "prepareAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003346 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003347 }
Romain Guy06882f82009-06-10 13:36:04 -07003348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003349 synchronized(mWindowMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003350 if (DEBUG_APP_TRANSITIONS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003351 TAG, "Prepare app transition: transit=" + transit
3352 + " mNextAppTransition=" + mNextAppTransition);
Dianne Hackbornb601ce12010-03-01 23:36:02 -08003353 if (!mDisplayFrozen && mPolicy.isScreenOn()) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003354 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET
3355 || mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003356 mNextAppTransition = transit;
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07003357 } else if (transit == WindowManagerPolicy.TRANSIT_TASK_OPEN
3358 && mNextAppTransition == WindowManagerPolicy.TRANSIT_TASK_CLOSE) {
3359 // Opening a new task always supersedes a close for the anim.
3360 mNextAppTransition = transit;
3361 } else if (transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN
3362 && mNextAppTransition == WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE) {
3363 // Opening a new activity always supersedes a close for the anim.
3364 mNextAppTransition = transit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003365 }
3366 mAppTransitionReady = false;
3367 mAppTransitionTimeout = false;
3368 mStartingIconInTransition = false;
3369 mSkipAppTransitionAnimation = false;
3370 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
3371 mH.sendMessageDelayed(mH.obtainMessage(H.APP_TRANSITION_TIMEOUT),
3372 5000);
3373 }
3374 }
3375 }
3376
3377 public int getPendingAppTransition() {
3378 return mNextAppTransition;
3379 }
Romain Guy06882f82009-06-10 13:36:04 -07003380
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003381 public void overridePendingAppTransition(String packageName,
3382 int enterAnim, int exitAnim) {
Dianne Hackborn8b571a82009-09-25 16:09:43 -07003383 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003384 mNextAppTransitionPackage = packageName;
3385 mNextAppTransitionEnter = enterAnim;
3386 mNextAppTransitionExit = exitAnim;
3387 }
3388 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003390 public void executeAppTransition() {
3391 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3392 "executeAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003393 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003394 }
Romain Guy06882f82009-06-10 13:36:04 -07003395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003396 synchronized(mWindowMap) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003397 if (DEBUG_APP_TRANSITIONS) {
3398 RuntimeException e = new RuntimeException("here");
3399 e.fillInStackTrace();
Joe Onorato8a9b2202010-02-26 18:56:32 -08003400 Slog.w(TAG, "Execute app transition: mNextAppTransition="
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003401 + mNextAppTransition, e);
3402 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003403 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003404 mAppTransitionReady = true;
3405 final long origId = Binder.clearCallingIdentity();
3406 performLayoutAndPlaceSurfacesLocked();
3407 Binder.restoreCallingIdentity(origId);
3408 }
3409 }
3410 }
3411
3412 public void setAppStartingWindow(IBinder token, String pkg,
3413 int theme, CharSequence nonLocalizedLabel, int labelRes, int icon,
3414 IBinder transferFrom, boolean createIfNeeded) {
3415 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3416 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003417 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003418 }
3419
3420 synchronized(mWindowMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003421 if (DEBUG_STARTING_WINDOW) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003422 TAG, "setAppStartingIcon: token=" + token + " pkg=" + pkg
3423 + " transferFrom=" + transferFrom);
Romain Guy06882f82009-06-10 13:36:04 -07003424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003425 AppWindowToken wtoken = findAppWindowToken(token);
3426 if (wtoken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003427 Slog.w(TAG, "Attempted to set icon of non-existing app token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003428 return;
3429 }
3430
3431 // If the display is frozen, we won't do anything until the
3432 // actual window is displayed so there is no reason to put in
3433 // the starting window.
Dianne Hackbornde2606d2009-12-18 16:53:55 -08003434 if (mDisplayFrozen || !mPolicy.isScreenOn()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003435 return;
3436 }
Romain Guy06882f82009-06-10 13:36:04 -07003437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003438 if (wtoken.startingData != null) {
3439 return;
3440 }
Romain Guy06882f82009-06-10 13:36:04 -07003441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003442 if (transferFrom != null) {
3443 AppWindowToken ttoken = findAppWindowToken(transferFrom);
3444 if (ttoken != null) {
3445 WindowState startingWindow = ttoken.startingWindow;
3446 if (startingWindow != null) {
3447 if (mStartingIconInTransition) {
3448 // In this case, the starting icon has already
3449 // been displayed, so start letting windows get
3450 // shown immediately without any more transitions.
3451 mSkipAppTransitionAnimation = true;
3452 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08003453 if (DEBUG_STARTING_WINDOW) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003454 "Moving existing starting from " + ttoken
3455 + " to " + wtoken);
3456 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07003457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003458 // Transfer the starting window over to the new
3459 // token.
3460 wtoken.startingData = ttoken.startingData;
3461 wtoken.startingView = ttoken.startingView;
3462 wtoken.startingWindow = startingWindow;
3463 ttoken.startingData = null;
3464 ttoken.startingView = null;
3465 ttoken.startingWindow = null;
3466 ttoken.startingMoved = true;
3467 startingWindow.mToken = wtoken;
Dianne Hackbornef49c572009-03-24 19:27:32 -07003468 startingWindow.mRootToken = wtoken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003469 startingWindow.mAppToken = wtoken;
Joe Onorato8a9b2202010-02-26 18:56:32 -08003470 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG,
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003471 "Removing starting window: " + startingWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003472 mWindows.remove(startingWindow);
3473 ttoken.windows.remove(startingWindow);
3474 ttoken.allAppWindows.remove(startingWindow);
3475 addWindowToListInOrderLocked(startingWindow, true);
Romain Guy06882f82009-06-10 13:36:04 -07003476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003477 // Propagate other interesting state between the
3478 // tokens. If the old token is displayed, we should
3479 // immediately force the new one to be displayed. If
3480 // it is animating, we need to move that animation to
3481 // the new one.
3482 if (ttoken.allDrawn) {
3483 wtoken.allDrawn = true;
3484 }
3485 if (ttoken.firstWindowDrawn) {
3486 wtoken.firstWindowDrawn = true;
3487 }
3488 if (!ttoken.hidden) {
3489 wtoken.hidden = false;
3490 wtoken.hiddenRequested = false;
3491 wtoken.willBeHidden = false;
3492 }
3493 if (wtoken.clientHidden != ttoken.clientHidden) {
3494 wtoken.clientHidden = ttoken.clientHidden;
3495 wtoken.sendAppVisibilityToClients();
3496 }
3497 if (ttoken.animation != null) {
3498 wtoken.animation = ttoken.animation;
3499 wtoken.animating = ttoken.animating;
3500 wtoken.animLayerAdjustment = ttoken.animLayerAdjustment;
3501 ttoken.animation = null;
3502 ttoken.animLayerAdjustment = 0;
3503 wtoken.updateLayers();
3504 ttoken.updateLayers();
3505 }
Romain Guy06882f82009-06-10 13:36:04 -07003506
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003507 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003508 mLayoutNeeded = true;
3509 performLayoutAndPlaceSurfacesLocked();
3510 Binder.restoreCallingIdentity(origId);
3511 return;
3512 } else if (ttoken.startingData != null) {
3513 // The previous app was getting ready to show a
3514 // starting window, but hasn't yet done so. Steal it!
Joe Onorato8a9b2202010-02-26 18:56:32 -08003515 if (DEBUG_STARTING_WINDOW) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003516 "Moving pending starting from " + ttoken
3517 + " to " + wtoken);
3518 wtoken.startingData = ttoken.startingData;
3519 ttoken.startingData = null;
3520 ttoken.startingMoved = true;
3521 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3522 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3523 // want to process the message ASAP, before any other queued
3524 // messages.
3525 mH.sendMessageAtFrontOfQueue(m);
3526 return;
3527 }
3528 }
3529 }
3530
3531 // There is no existing starting window, and the caller doesn't
3532 // want us to create one, so that's it!
3533 if (!createIfNeeded) {
3534 return;
3535 }
Romain Guy06882f82009-06-10 13:36:04 -07003536
Dianne Hackborn284ac932009-08-28 10:34:25 -07003537 // If this is a translucent or wallpaper window, then don't
3538 // show a starting window -- the current effect (a full-screen
3539 // opaque starting window that fades away to the real contents
3540 // when it is ready) does not work for this.
3541 if (theme != 0) {
3542 AttributeCache.Entry ent = AttributeCache.instance().get(pkg, theme,
3543 com.android.internal.R.styleable.Window);
3544 if (ent.array.getBoolean(
3545 com.android.internal.R.styleable.Window_windowIsTranslucent, false)) {
3546 return;
3547 }
3548 if (ent.array.getBoolean(
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07003549 com.android.internal.R.styleable.Window_windowIsFloating, false)) {
3550 return;
3551 }
3552 if (ent.array.getBoolean(
Dianne Hackborn284ac932009-08-28 10:34:25 -07003553 com.android.internal.R.styleable.Window_windowShowWallpaper, false)) {
3554 return;
3555 }
3556 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003558 mStartingIconInTransition = true;
3559 wtoken.startingData = new StartingData(
3560 pkg, theme, nonLocalizedLabel,
3561 labelRes, icon);
3562 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3563 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3564 // want to process the message ASAP, before any other queued
3565 // messages.
3566 mH.sendMessageAtFrontOfQueue(m);
3567 }
3568 }
3569
3570 public void setAppWillBeHidden(IBinder token) {
3571 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3572 "setAppWillBeHidden()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003573 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003574 }
3575
3576 AppWindowToken wtoken;
3577
3578 synchronized(mWindowMap) {
3579 wtoken = findAppWindowToken(token);
3580 if (wtoken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003581 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 -08003582 return;
3583 }
3584 wtoken.willBeHidden = true;
3585 }
3586 }
Romain Guy06882f82009-06-10 13:36:04 -07003587
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003588 boolean setTokenVisibilityLocked(AppWindowToken wtoken, WindowManager.LayoutParams lp,
3589 boolean visible, int transit, boolean performLayout) {
3590 boolean delayed = false;
3591
3592 if (wtoken.clientHidden == visible) {
3593 wtoken.clientHidden = !visible;
3594 wtoken.sendAppVisibilityToClients();
3595 }
Romain Guy06882f82009-06-10 13:36:04 -07003596
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003597 wtoken.willBeHidden = false;
3598 if (wtoken.hidden == visible) {
3599 final int N = wtoken.allAppWindows.size();
3600 boolean changed = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -08003601 if (DEBUG_APP_TRANSITIONS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003602 TAG, "Changing app " + wtoken + " hidden=" + wtoken.hidden
3603 + " performLayout=" + performLayout);
Romain Guy06882f82009-06-10 13:36:04 -07003604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003605 boolean runningAppAnimation = false;
Romain Guy06882f82009-06-10 13:36:04 -07003606
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003607 if (transit != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003608 if (wtoken.animation == sDummyAnimation) {
3609 wtoken.animation = null;
3610 }
3611 applyAnimationLocked(wtoken, lp, transit, visible);
3612 changed = true;
3613 if (wtoken.animation != null) {
3614 delayed = runningAppAnimation = true;
3615 }
3616 }
Romain Guy06882f82009-06-10 13:36:04 -07003617
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003618 for (int i=0; i<N; i++) {
3619 WindowState win = wtoken.allAppWindows.get(i);
3620 if (win == wtoken.startingWindow) {
3621 continue;
3622 }
3623
3624 if (win.isAnimating()) {
3625 delayed = true;
3626 }
Romain Guy06882f82009-06-10 13:36:04 -07003627
Joe Onorato8a9b2202010-02-26 18:56:32 -08003628 //Slog.i(TAG, "Window " + win + ": vis=" + win.isVisible());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003629 //win.dump(" ");
3630 if (visible) {
3631 if (!win.isVisibleNow()) {
3632 if (!runningAppAnimation) {
3633 applyAnimationLocked(win,
3634 WindowManagerPolicy.TRANSIT_ENTER, true);
3635 }
3636 changed = true;
3637 }
3638 } else if (win.isVisibleNow()) {
3639 if (!runningAppAnimation) {
3640 applyAnimationLocked(win,
3641 WindowManagerPolicy.TRANSIT_EXIT, false);
3642 }
3643 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
3644 KeyWaiter.RETURN_NOTHING);
3645 changed = true;
3646 }
3647 }
3648
3649 wtoken.hidden = wtoken.hiddenRequested = !visible;
3650 if (!visible) {
3651 unsetAppFreezingScreenLocked(wtoken, true, true);
3652 } else {
3653 // If we are being set visible, and the starting window is
3654 // not yet displayed, then make sure it doesn't get displayed.
3655 WindowState swin = wtoken.startingWindow;
3656 if (swin != null && (swin.mDrawPending
3657 || swin.mCommitDrawPending)) {
3658 swin.mPolicyVisibility = false;
3659 swin.mPolicyVisibilityAfterAnim = false;
3660 }
3661 }
Romain Guy06882f82009-06-10 13:36:04 -07003662
Joe Onorato8a9b2202010-02-26 18:56:32 -08003663 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG, "setTokenVisibilityLocked: " + wtoken
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003664 + ": hidden=" + wtoken.hidden + " hiddenRequested="
3665 + wtoken.hiddenRequested);
Romain Guy06882f82009-06-10 13:36:04 -07003666
Dianne Hackborn9b52a212009-12-11 14:51:35 -08003667 if (changed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003668 mLayoutNeeded = true;
Dianne Hackborn9b52a212009-12-11 14:51:35 -08003669 if (performLayout) {
3670 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
3671 performLayoutAndPlaceSurfacesLocked();
3672 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003673 }
3674 }
3675
3676 if (wtoken.animation != null) {
3677 delayed = true;
3678 }
Romain Guy06882f82009-06-10 13:36:04 -07003679
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003680 return delayed;
3681 }
3682
3683 public void setAppVisibility(IBinder token, boolean visible) {
3684 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3685 "setAppVisibility()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003686 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003687 }
3688
3689 AppWindowToken wtoken;
3690
3691 synchronized(mWindowMap) {
3692 wtoken = findAppWindowToken(token);
3693 if (wtoken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003694 Slog.w(TAG, "Attempted to set visibility of non-existing app token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003695 return;
3696 }
3697
3698 if (DEBUG_APP_TRANSITIONS || DEBUG_ORIENTATION) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08003699 RuntimeException e = null;
3700 if (!HIDE_STACK_CRAWLS) {
3701 e = new RuntimeException();
3702 e.fillInStackTrace();
3703 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08003704 Slog.v(TAG, "setAppVisibility(" + token + ", " + visible
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003705 + "): mNextAppTransition=" + mNextAppTransition
3706 + " hidden=" + wtoken.hidden
3707 + " hiddenRequested=" + wtoken.hiddenRequested, e);
3708 }
Romain Guy06882f82009-06-10 13:36:04 -07003709
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003710 // If we are preparing an app transition, then delay changing
3711 // the visibility of this token until we execute that transition.
Dianne Hackbornb601ce12010-03-01 23:36:02 -08003712 if (!mDisplayFrozen && mPolicy.isScreenOn()
3713 && mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003714 // Already in requested state, don't do anything more.
3715 if (wtoken.hiddenRequested != visible) {
3716 return;
3717 }
3718 wtoken.hiddenRequested = !visible;
Romain Guy06882f82009-06-10 13:36:04 -07003719
Joe Onorato8a9b2202010-02-26 18:56:32 -08003720 if (DEBUG_APP_TRANSITIONS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003721 TAG, "Setting dummy animation on: " + wtoken);
3722 wtoken.setDummyAnimation();
3723 mOpeningApps.remove(wtoken);
3724 mClosingApps.remove(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003725 wtoken.waitingToShow = wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003726 wtoken.inPendingTransaction = true;
3727 if (visible) {
3728 mOpeningApps.add(wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003729 wtoken.startingDisplayed = false;
3730 wtoken.startingMoved = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003731
Dianne Hackborn195f6a02009-11-24 11:26:00 -08003732 // If the token is currently hidden (should be the
3733 // common case), then we need to set up to wait for
3734 // its windows to be ready.
3735 if (wtoken.hidden) {
3736 wtoken.allDrawn = false;
3737 wtoken.waitingToShow = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003738
Dianne Hackborn195f6a02009-11-24 11:26:00 -08003739 if (wtoken.clientHidden) {
3740 // In the case where we are making an app visible
3741 // but holding off for a transition, we still need
3742 // to tell the client to make its windows visible so
3743 // they get drawn. Otherwise, we will wait on
3744 // performing the transition until all windows have
3745 // been drawn, they never will be, and we are sad.
3746 wtoken.clientHidden = false;
3747 wtoken.sendAppVisibilityToClients();
3748 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003749 }
3750 } else {
3751 mClosingApps.add(wtoken);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003752
Dianne Hackborn195f6a02009-11-24 11:26:00 -08003753 // If the token is currently visible (should be the
3754 // common case), then set up to wait for it to be hidden.
3755 if (!wtoken.hidden) {
3756 wtoken.waitingToHide = true;
3757 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003758 }
3759 return;
3760 }
Romain Guy06882f82009-06-10 13:36:04 -07003761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003762 final long origId = Binder.clearCallingIdentity();
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003763 setTokenVisibilityLocked(wtoken, null, visible, WindowManagerPolicy.TRANSIT_UNSET, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003764 wtoken.updateReportedVisibilityLocked();
3765 Binder.restoreCallingIdentity(origId);
3766 }
3767 }
3768
3769 void unsetAppFreezingScreenLocked(AppWindowToken wtoken,
3770 boolean unfreezeSurfaceNow, boolean force) {
3771 if (wtoken.freezingScreen) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003772 if (DEBUG_ORIENTATION) Slog.v(TAG, "Clear freezing of " + wtoken
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003773 + " force=" + force);
3774 final int N = wtoken.allAppWindows.size();
3775 boolean unfrozeWindows = false;
3776 for (int i=0; i<N; i++) {
3777 WindowState w = wtoken.allAppWindows.get(i);
3778 if (w.mAppFreezing) {
3779 w.mAppFreezing = false;
3780 if (w.mSurface != null && !w.mOrientationChanging) {
3781 w.mOrientationChanging = true;
3782 }
3783 unfrozeWindows = true;
3784 }
3785 }
3786 if (force || unfrozeWindows) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003787 if (DEBUG_ORIENTATION) Slog.v(TAG, "No longer freezing: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003788 wtoken.freezingScreen = false;
3789 mAppsFreezingScreen--;
3790 }
3791 if (unfreezeSurfaceNow) {
3792 if (unfrozeWindows) {
3793 mLayoutNeeded = true;
3794 performLayoutAndPlaceSurfacesLocked();
3795 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003796 stopFreezingDisplayLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003797 }
3798 }
3799 }
Romain Guy06882f82009-06-10 13:36:04 -07003800
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003801 public void startAppFreezingScreenLocked(AppWindowToken wtoken,
3802 int configChanges) {
3803 if (DEBUG_ORIENTATION) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08003804 RuntimeException e = null;
3805 if (!HIDE_STACK_CRAWLS) {
3806 e = new RuntimeException();
3807 e.fillInStackTrace();
3808 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08003809 Slog.i(TAG, "Set freezing of " + wtoken.appToken
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003810 + ": hidden=" + wtoken.hidden + " freezing="
3811 + wtoken.freezingScreen, e);
3812 }
3813 if (!wtoken.hiddenRequested) {
3814 if (!wtoken.freezingScreen) {
3815 wtoken.freezingScreen = true;
3816 mAppsFreezingScreen++;
3817 if (mAppsFreezingScreen == 1) {
3818 startFreezingDisplayLocked();
3819 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
3820 mH.sendMessageDelayed(mH.obtainMessage(H.APP_FREEZE_TIMEOUT),
3821 5000);
3822 }
3823 }
3824 final int N = wtoken.allAppWindows.size();
3825 for (int i=0; i<N; i++) {
3826 WindowState w = wtoken.allAppWindows.get(i);
3827 w.mAppFreezing = true;
3828 }
3829 }
3830 }
Romain Guy06882f82009-06-10 13:36:04 -07003831
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003832 public void startAppFreezingScreen(IBinder token, int configChanges) {
3833 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3834 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003835 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003836 }
3837
3838 synchronized(mWindowMap) {
Dianne Hackbornb601ce12010-03-01 23:36:02 -08003839 if (configChanges == 0 && !mDisplayFrozen && mPolicy.isScreenOn()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003840 if (DEBUG_ORIENTATION) Slog.v(TAG, "Skipping set freeze of " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003841 return;
3842 }
Romain Guy06882f82009-06-10 13:36:04 -07003843
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003844 AppWindowToken wtoken = findAppWindowToken(token);
3845 if (wtoken == null || wtoken.appToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003846 Slog.w(TAG, "Attempted to freeze screen with non-existing app token: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003847 return;
3848 }
3849 final long origId = Binder.clearCallingIdentity();
3850 startAppFreezingScreenLocked(wtoken, configChanges);
3851 Binder.restoreCallingIdentity(origId);
3852 }
3853 }
Romain Guy06882f82009-06-10 13:36:04 -07003854
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003855 public void stopAppFreezingScreen(IBinder token, boolean force) {
3856 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3857 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003858 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003859 }
3860
3861 synchronized(mWindowMap) {
3862 AppWindowToken wtoken = findAppWindowToken(token);
3863 if (wtoken == null || wtoken.appToken == null) {
3864 return;
3865 }
3866 final long origId = Binder.clearCallingIdentity();
Joe Onorato8a9b2202010-02-26 18:56:32 -08003867 if (DEBUG_ORIENTATION) Slog.v(TAG, "Clear freezing of " + token
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003868 + ": hidden=" + wtoken.hidden + " freezing=" + wtoken.freezingScreen);
3869 unsetAppFreezingScreenLocked(wtoken, true, force);
3870 Binder.restoreCallingIdentity(origId);
3871 }
3872 }
Romain Guy06882f82009-06-10 13:36:04 -07003873
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003874 public void removeAppToken(IBinder token) {
3875 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3876 "removeAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003877 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003878 }
3879
3880 AppWindowToken wtoken = null;
3881 AppWindowToken startingToken = null;
3882 boolean delayed = false;
3883
3884 final long origId = Binder.clearCallingIdentity();
3885 synchronized(mWindowMap) {
3886 WindowToken basewtoken = mTokenMap.remove(token);
3887 mTokenList.remove(basewtoken);
3888 if (basewtoken != null && (wtoken=basewtoken.appWindowToken) != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003889 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG, "Removing app token: " + wtoken);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003890 delayed = setTokenVisibilityLocked(wtoken, null, false, WindowManagerPolicy.TRANSIT_UNSET, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003891 wtoken.inPendingTransaction = false;
3892 mOpeningApps.remove(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003893 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003894 if (mClosingApps.contains(wtoken)) {
3895 delayed = true;
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003896 } else if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003897 mClosingApps.add(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003898 wtoken.waitingToHide = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003899 delayed = true;
3900 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08003901 if (DEBUG_APP_TRANSITIONS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003902 TAG, "Removing app " + wtoken + " delayed=" + delayed
3903 + " animation=" + wtoken.animation
3904 + " animating=" + wtoken.animating);
3905 if (delayed) {
3906 // set the token aside because it has an active animation to be finished
3907 mExitingAppTokens.add(wtoken);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003908 } else {
3909 // Make sure there is no animation running on this token,
3910 // so any windows associated with it will be removed as
3911 // soon as their animations are complete
3912 wtoken.animation = null;
3913 wtoken.animating = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003914 }
3915 mAppTokens.remove(wtoken);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003916 if (mLastEnterAnimToken == wtoken) {
3917 mLastEnterAnimToken = null;
3918 mLastEnterAnimParams = null;
3919 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003920 wtoken.removed = true;
3921 if (wtoken.startingData != null) {
3922 startingToken = wtoken;
3923 }
3924 unsetAppFreezingScreenLocked(wtoken, true, true);
3925 if (mFocusedApp == wtoken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003926 if (DEBUG_FOCUS) Slog.v(TAG, "Removing focused app token:" + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003927 mFocusedApp = null;
3928 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3929 mKeyWaiter.tickle();
3930 }
3931 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003932 Slog.w(TAG, "Attempted to remove non-existing app token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003933 }
Romain Guy06882f82009-06-10 13:36:04 -07003934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003935 if (!delayed && wtoken != null) {
3936 wtoken.updateReportedVisibilityLocked();
3937 }
3938 }
3939 Binder.restoreCallingIdentity(origId);
3940
3941 if (startingToken != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003942 if (DEBUG_STARTING_WINDOW) Slog.v(TAG, "Schedule remove starting "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003943 + startingToken + ": app token removed");
3944 Message m = mH.obtainMessage(H.REMOVE_STARTING, startingToken);
3945 mH.sendMessage(m);
3946 }
3947 }
3948
3949 private boolean tmpRemoveAppWindowsLocked(WindowToken token) {
3950 final int NW = token.windows.size();
3951 for (int i=0; i<NW; i++) {
3952 WindowState win = token.windows.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003953 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "Tmp removing app window " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003954 mWindows.remove(win);
3955 int j = win.mChildWindows.size();
3956 while (j > 0) {
3957 j--;
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003958 WindowState cwin = (WindowState)win.mChildWindows.get(j);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003959 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG,
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003960 "Tmp removing child window " + cwin);
3961 mWindows.remove(cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003962 }
3963 }
3964 return NW > 0;
3965 }
3966
3967 void dumpAppTokensLocked() {
3968 for (int i=mAppTokens.size()-1; i>=0; i--) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003969 Slog.v(TAG, " #" + i + ": " + mAppTokens.get(i).token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003970 }
3971 }
Romain Guy06882f82009-06-10 13:36:04 -07003972
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003973 void dumpWindowsLocked() {
3974 for (int i=mWindows.size()-1; i>=0; i--) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003975 Slog.v(TAG, " #" + i + ": " + mWindows.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003976 }
3977 }
Romain Guy06882f82009-06-10 13:36:04 -07003978
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003979 private int findWindowOffsetLocked(int tokenPos) {
3980 final int NW = mWindows.size();
3981
3982 if (tokenPos >= mAppTokens.size()) {
3983 int i = NW;
3984 while (i > 0) {
3985 i--;
3986 WindowState win = (WindowState)mWindows.get(i);
3987 if (win.getAppToken() != null) {
3988 return i+1;
3989 }
3990 }
3991 }
3992
3993 while (tokenPos > 0) {
3994 // Find the first app token below the new position that has
3995 // a window displayed.
3996 final AppWindowToken wtoken = mAppTokens.get(tokenPos-1);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003997 if (DEBUG_REORDER) Slog.v(TAG, "Looking for lower windows @ "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003998 + tokenPos + " -- " + wtoken.token);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003999 if (wtoken.sendingToBottom) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004000 if (DEBUG_REORDER) Slog.v(TAG,
Dianne Hackborna8f60182009-09-01 19:01:50 -07004001 "Skipping token -- currently sending to bottom");
4002 tokenPos--;
4003 continue;
4004 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004005 int i = wtoken.windows.size();
4006 while (i > 0) {
4007 i--;
4008 WindowState win = wtoken.windows.get(i);
4009 int j = win.mChildWindows.size();
4010 while (j > 0) {
4011 j--;
4012 WindowState cwin = (WindowState)win.mChildWindows.get(j);
Dianne Hackborna8f60182009-09-01 19:01:50 -07004013 if (cwin.mSubLayer >= 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004014 for (int pos=NW-1; pos>=0; pos--) {
4015 if (mWindows.get(pos) == cwin) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004016 if (DEBUG_REORDER) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004017 "Found child win @" + (pos+1));
4018 return pos+1;
4019 }
4020 }
4021 }
4022 }
4023 for (int pos=NW-1; pos>=0; pos--) {
4024 if (mWindows.get(pos) == win) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004025 if (DEBUG_REORDER) Slog.v(TAG, "Found win @" + (pos+1));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004026 return pos+1;
4027 }
4028 }
4029 }
4030 tokenPos--;
4031 }
4032
4033 return 0;
4034 }
4035
4036 private final int reAddWindowLocked(int index, WindowState win) {
4037 final int NCW = win.mChildWindows.size();
4038 boolean added = false;
4039 for (int j=0; j<NCW; j++) {
4040 WindowState cwin = (WindowState)win.mChildWindows.get(j);
4041 if (!added && cwin.mSubLayer >= 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004042 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "Re-adding child window at "
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07004043 + index + ": " + cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004044 mWindows.add(index, win);
4045 index++;
4046 added = true;
4047 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08004048 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "Re-adding window at "
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07004049 + index + ": " + cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004050 mWindows.add(index, cwin);
4051 index++;
4052 }
4053 if (!added) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004054 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "Re-adding window at "
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07004055 + index + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004056 mWindows.add(index, win);
4057 index++;
4058 }
4059 return index;
4060 }
Romain Guy06882f82009-06-10 13:36:04 -07004061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004062 private final int reAddAppWindowsLocked(int index, WindowToken token) {
4063 final int NW = token.windows.size();
4064 for (int i=0; i<NW; i++) {
4065 index = reAddWindowLocked(index, token.windows.get(i));
4066 }
4067 return index;
4068 }
4069
4070 public void moveAppToken(int index, IBinder token) {
4071 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4072 "moveAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004073 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004074 }
4075
4076 synchronized(mWindowMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004077 if (DEBUG_REORDER) Slog.v(TAG, "Initial app tokens:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004078 if (DEBUG_REORDER) dumpAppTokensLocked();
4079 final AppWindowToken wtoken = findAppWindowToken(token);
4080 if (wtoken == null || !mAppTokens.remove(wtoken)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004081 Slog.w(TAG, "Attempting to reorder token that doesn't exist: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004082 + token + " (" + wtoken + ")");
4083 return;
4084 }
4085 mAppTokens.add(index, wtoken);
Joe Onorato8a9b2202010-02-26 18:56:32 -08004086 if (DEBUG_REORDER) Slog.v(TAG, "Moved " + token + " to " + index + ":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004087 if (DEBUG_REORDER) dumpAppTokensLocked();
Romain Guy06882f82009-06-10 13:36:04 -07004088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004089 final long origId = Binder.clearCallingIdentity();
Joe Onorato8a9b2202010-02-26 18:56:32 -08004090 if (DEBUG_REORDER) Slog.v(TAG, "Removing windows in " + token + ":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004091 if (DEBUG_REORDER) dumpWindowsLocked();
4092 if (tmpRemoveAppWindowsLocked(wtoken)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004093 if (DEBUG_REORDER) Slog.v(TAG, "Adding windows back in:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004094 if (DEBUG_REORDER) dumpWindowsLocked();
4095 reAddAppWindowsLocked(findWindowOffsetLocked(index), wtoken);
Joe Onorato8a9b2202010-02-26 18:56:32 -08004096 if (DEBUG_REORDER) Slog.v(TAG, "Final window list:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004097 if (DEBUG_REORDER) dumpWindowsLocked();
4098 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004099 mLayoutNeeded = true;
4100 performLayoutAndPlaceSurfacesLocked();
4101 }
4102 Binder.restoreCallingIdentity(origId);
4103 }
4104 }
4105
4106 private void removeAppTokensLocked(List<IBinder> tokens) {
4107 // XXX This should be done more efficiently!
4108 // (take advantage of the fact that both lists should be
4109 // ordered in the same way.)
4110 int N = tokens.size();
4111 for (int i=0; i<N; i++) {
4112 IBinder token = tokens.get(i);
4113 final AppWindowToken wtoken = findAppWindowToken(token);
4114 if (!mAppTokens.remove(wtoken)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004115 Slog.w(TAG, "Attempting to reorder token that doesn't exist: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004116 + token + " (" + wtoken + ")");
4117 i--;
4118 N--;
4119 }
4120 }
4121 }
4122
Dianne Hackborna8f60182009-09-01 19:01:50 -07004123 private void moveAppWindowsLocked(AppWindowToken wtoken, int tokenPos,
4124 boolean updateFocusAndLayout) {
4125 // First remove all of the windows from the list.
4126 tmpRemoveAppWindowsLocked(wtoken);
4127
4128 // Where to start adding?
4129 int pos = findWindowOffsetLocked(tokenPos);
4130
4131 // And now add them back at the correct place.
4132 pos = reAddAppWindowsLocked(pos, wtoken);
4133
4134 if (updateFocusAndLayout) {
4135 if (!updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
4136 assignLayersLocked();
4137 }
4138 mLayoutNeeded = true;
4139 performLayoutAndPlaceSurfacesLocked();
4140 }
4141 }
4142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004143 private void moveAppWindowsLocked(List<IBinder> tokens, int tokenPos) {
4144 // First remove all of the windows from the list.
4145 final int N = tokens.size();
4146 int i;
4147 for (i=0; i<N; i++) {
4148 WindowToken token = mTokenMap.get(tokens.get(i));
4149 if (token != null) {
4150 tmpRemoveAppWindowsLocked(token);
4151 }
4152 }
4153
4154 // Where to start adding?
4155 int pos = findWindowOffsetLocked(tokenPos);
4156
4157 // And now add them back at the correct place.
4158 for (i=0; i<N; i++) {
4159 WindowToken token = mTokenMap.get(tokens.get(i));
4160 if (token != null) {
4161 pos = reAddAppWindowsLocked(pos, token);
4162 }
4163 }
4164
Dianne Hackborna8f60182009-09-01 19:01:50 -07004165 if (!updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
4166 assignLayersLocked();
4167 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004168 mLayoutNeeded = true;
4169 performLayoutAndPlaceSurfacesLocked();
4170
4171 //dump();
4172 }
4173
4174 public void moveAppTokensToTop(List<IBinder> tokens) {
4175 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4176 "moveAppTokensToTop()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004177 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004178 }
4179
4180 final long origId = Binder.clearCallingIdentity();
4181 synchronized(mWindowMap) {
4182 removeAppTokensLocked(tokens);
4183 final int N = tokens.size();
4184 for (int i=0; i<N; i++) {
4185 AppWindowToken wt = findAppWindowToken(tokens.get(i));
4186 if (wt != null) {
4187 mAppTokens.add(wt);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004188 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004189 mToTopApps.remove(wt);
4190 mToBottomApps.remove(wt);
4191 mToTopApps.add(wt);
4192 wt.sendingToBottom = false;
4193 wt.sendingToTop = true;
4194 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004195 }
4196 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004197
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004198 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004199 moveAppWindowsLocked(tokens, mAppTokens.size());
4200 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004201 }
4202 Binder.restoreCallingIdentity(origId);
4203 }
4204
4205 public void moveAppTokensToBottom(List<IBinder> tokens) {
4206 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4207 "moveAppTokensToBottom()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004208 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004209 }
4210
4211 final long origId = Binder.clearCallingIdentity();
4212 synchronized(mWindowMap) {
4213 removeAppTokensLocked(tokens);
4214 final int N = tokens.size();
4215 int pos = 0;
4216 for (int i=0; i<N; i++) {
4217 AppWindowToken wt = findAppWindowToken(tokens.get(i));
4218 if (wt != null) {
4219 mAppTokens.add(pos, wt);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004220 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004221 mToTopApps.remove(wt);
4222 mToBottomApps.remove(wt);
4223 mToBottomApps.add(i, wt);
4224 wt.sendingToTop = false;
4225 wt.sendingToBottom = true;
4226 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004227 pos++;
4228 }
4229 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004230
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004231 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004232 moveAppWindowsLocked(tokens, 0);
4233 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004234 }
4235 Binder.restoreCallingIdentity(origId);
4236 }
4237
4238 // -------------------------------------------------------------
4239 // Misc IWindowSession methods
4240 // -------------------------------------------------------------
Romain Guy06882f82009-06-10 13:36:04 -07004241
Jim Miller284b62e2010-06-08 14:27:42 -07004242 private boolean shouldAllowDisableKeyguard()
Jim Millerd6b57052010-06-07 17:52:42 -07004243 {
Jim Miller284b62e2010-06-08 14:27:42 -07004244 // We fail safe and prevent disabling keyguard in the unlikely event this gets
4245 // called before DevicePolicyManagerService has started.
4246 if (mAllowDisableKeyguard == ALLOW_DISABLE_UNKNOWN) {
4247 DevicePolicyManager dpm = (DevicePolicyManager) mContext.getSystemService(
4248 Context.DEVICE_POLICY_SERVICE);
4249 if (dpm != null) {
4250 mAllowDisableKeyguard = dpm.getPasswordQuality(null)
4251 == DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED ?
4252 ALLOW_DISABLE_YES : ALLOW_DISABLE_NO;
4253 }
Jim Millerd6b57052010-06-07 17:52:42 -07004254 }
Jim Miller284b62e2010-06-08 14:27:42 -07004255 return mAllowDisableKeyguard == ALLOW_DISABLE_YES;
Jim Millerd6b57052010-06-07 17:52:42 -07004256 }
4257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004258 public void disableKeyguard(IBinder token, String tag) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04004259 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004260 != PackageManager.PERMISSION_GRANTED) {
4261 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
4262 }
Jim Millerd6b57052010-06-07 17:52:42 -07004263
Jim Miller284b62e2010-06-08 14:27:42 -07004264 synchronized (mKeyguardTokenWatcher) {
4265 mKeyguardTokenWatcher.acquire(token, tag);
Mike Lockwooddd884682009-10-11 16:57:08 -04004266 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004267 }
4268
4269 public void reenableKeyguard(IBinder token) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04004270 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004271 != PackageManager.PERMISSION_GRANTED) {
4272 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
4273 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004274
Jim Miller284b62e2010-06-08 14:27:42 -07004275 synchronized (mKeyguardTokenWatcher) {
4276 mKeyguardTokenWatcher.release(token);
Jim Millerd6b57052010-06-07 17:52:42 -07004277
Jim Miller284b62e2010-06-08 14:27:42 -07004278 if (!mKeyguardTokenWatcher.isAcquired()) {
4279 // If we are the last one to reenable the keyguard wait until
4280 // we have actually finished reenabling until returning.
4281 // It is possible that reenableKeyguard() can be called before
4282 // the previous disableKeyguard() is handled, in which case
4283 // neither mKeyguardTokenWatcher.acquired() or released() would
4284 // be called. In that case mKeyguardDisabled will be false here
4285 // and we have nothing to wait for.
4286 while (mKeyguardDisabled) {
4287 try {
4288 mKeyguardTokenWatcher.wait();
4289 } catch (InterruptedException e) {
4290 Thread.currentThread().interrupt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004291 }
4292 }
4293 }
4294 }
4295 }
4296
4297 /**
4298 * @see android.app.KeyguardManager#exitKeyguardSecurely
4299 */
4300 public void exitKeyguardSecurely(final IOnKeyguardExitResult callback) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04004301 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004302 != PackageManager.PERMISSION_GRANTED) {
4303 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
4304 }
4305 mPolicy.exitKeyguardSecurely(new WindowManagerPolicy.OnKeyguardExitResult() {
4306 public void onKeyguardExitResult(boolean success) {
4307 try {
4308 callback.onKeyguardExitResult(success);
4309 } catch (RemoteException e) {
4310 // Client has died, we don't care.
4311 }
4312 }
4313 });
4314 }
4315
4316 public boolean inKeyguardRestrictedInputMode() {
4317 return mPolicy.inKeyguardRestrictedKeyInputMode();
4318 }
Romain Guy06882f82009-06-10 13:36:04 -07004319
Dianne Hackbornffa42482009-09-23 22:20:11 -07004320 public void closeSystemDialogs(String reason) {
4321 synchronized(mWindowMap) {
4322 for (int i=mWindows.size()-1; i>=0; i--) {
4323 WindowState w = (WindowState)mWindows.get(i);
4324 if (w.mSurface != null) {
4325 try {
4326 w.mClient.closeSystemDialogs(reason);
4327 } catch (RemoteException e) {
4328 }
4329 }
4330 }
4331 }
4332 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004334 static float fixScale(float scale) {
4335 if (scale < 0) scale = 0;
4336 else if (scale > 20) scale = 20;
4337 return Math.abs(scale);
4338 }
Romain Guy06882f82009-06-10 13:36:04 -07004339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004340 public void setAnimationScale(int which, float scale) {
4341 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
4342 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004343 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004344 }
4345
4346 if (scale < 0) scale = 0;
4347 else if (scale > 20) scale = 20;
4348 scale = Math.abs(scale);
4349 switch (which) {
4350 case 0: mWindowAnimationScale = fixScale(scale); break;
4351 case 1: mTransitionAnimationScale = fixScale(scale); break;
4352 }
Romain Guy06882f82009-06-10 13:36:04 -07004353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004354 // Persist setting
4355 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
4356 }
Romain Guy06882f82009-06-10 13:36:04 -07004357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004358 public void setAnimationScales(float[] scales) {
4359 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
4360 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004361 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004362 }
4363
4364 if (scales != null) {
4365 if (scales.length >= 1) {
4366 mWindowAnimationScale = fixScale(scales[0]);
4367 }
4368 if (scales.length >= 2) {
4369 mTransitionAnimationScale = fixScale(scales[1]);
4370 }
4371 }
Romain Guy06882f82009-06-10 13:36:04 -07004372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004373 // Persist setting
4374 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
4375 }
Romain Guy06882f82009-06-10 13:36:04 -07004376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004377 public float getAnimationScale(int which) {
4378 switch (which) {
4379 case 0: return mWindowAnimationScale;
4380 case 1: return mTransitionAnimationScale;
4381 }
4382 return 0;
4383 }
Romain Guy06882f82009-06-10 13:36:04 -07004384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004385 public float[] getAnimationScales() {
4386 return new float[] { mWindowAnimationScale, mTransitionAnimationScale };
4387 }
Romain Guy06882f82009-06-10 13:36:04 -07004388
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004389 public int getSwitchState(int sw) {
4390 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4391 "getSwitchState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004392 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004393 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004394 if (ENABLE_NATIVE_INPUT_DISPATCH) {
4395 return mInputManager.getSwitchState(sw);
4396 } else {
4397 return KeyInputQueue.getSwitchState(sw);
4398 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004399 }
Romain Guy06882f82009-06-10 13:36:04 -07004400
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004401 public int getSwitchStateForDevice(int devid, int sw) {
4402 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4403 "getSwitchStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004404 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004405 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004406 if (ENABLE_NATIVE_INPUT_DISPATCH) {
4407 return mInputManager.getSwitchState(devid, sw);
4408 } else {
4409 return KeyInputQueue.getSwitchState(devid, sw);
4410 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004411 }
Romain Guy06882f82009-06-10 13:36:04 -07004412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004413 public int getScancodeState(int sw) {
4414 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4415 "getScancodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004416 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004417 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004418 if (ENABLE_NATIVE_INPUT_DISPATCH) {
4419 return mInputManager.getScancodeState(sw);
4420 } else {
4421 return mQueue.getScancodeState(sw);
4422 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004423 }
Romain Guy06882f82009-06-10 13:36:04 -07004424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004425 public int getScancodeStateForDevice(int devid, int sw) {
4426 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4427 "getScancodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004428 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004429 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004430 if (ENABLE_NATIVE_INPUT_DISPATCH) {
4431 return mInputManager.getScancodeState(devid, sw);
4432 } else {
4433 return mQueue.getScancodeState(devid, sw);
4434 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004435 }
Romain Guy06882f82009-06-10 13:36:04 -07004436
Dianne Hackborn1d62ea92009-11-17 12:49:50 -08004437 public int getTrackballScancodeState(int sw) {
4438 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4439 "getTrackballScancodeState()")) {
4440 throw new SecurityException("Requires READ_INPUT_STATE permission");
4441 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004442 if (ENABLE_NATIVE_INPUT_DISPATCH) {
4443 return mInputManager.getTrackballScancodeState(sw);
4444 } else {
4445 return mQueue.getTrackballScancodeState(sw);
4446 }
Dianne Hackborn1d62ea92009-11-17 12:49:50 -08004447 }
4448
4449 public int getDPadScancodeState(int sw) {
4450 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4451 "getDPadScancodeState()")) {
4452 throw new SecurityException("Requires READ_INPUT_STATE permission");
4453 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004454 if (ENABLE_NATIVE_INPUT_DISPATCH) {
4455 return mInputManager.getDPadScancodeState(sw);
4456 } else {
4457 return mQueue.getDPadScancodeState(sw);
4458 }
Dianne Hackborn1d62ea92009-11-17 12:49:50 -08004459 }
4460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004461 public int getKeycodeState(int sw) {
4462 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4463 "getKeycodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004464 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004465 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004466 if (ENABLE_NATIVE_INPUT_DISPATCH) {
4467 return mInputManager.getKeycodeState(sw);
4468 } else {
4469 return mQueue.getKeycodeState(sw);
4470 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004471 }
Romain Guy06882f82009-06-10 13:36:04 -07004472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004473 public int getKeycodeStateForDevice(int devid, int sw) {
4474 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4475 "getKeycodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004476 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004477 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004478 if (ENABLE_NATIVE_INPUT_DISPATCH) {
4479 return mInputManager.getKeycodeState(devid, sw);
4480 } else {
4481 return mQueue.getKeycodeState(devid, sw);
4482 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004483 }
Romain Guy06882f82009-06-10 13:36:04 -07004484
Dianne Hackborn1d62ea92009-11-17 12:49:50 -08004485 public int getTrackballKeycodeState(int sw) {
4486 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4487 "getTrackballKeycodeState()")) {
4488 throw new SecurityException("Requires READ_INPUT_STATE permission");
4489 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004490 if (ENABLE_NATIVE_INPUT_DISPATCH) {
4491 return mInputManager.getTrackballKeycodeState(sw);
4492 } else {
4493 return mQueue.getTrackballKeycodeState(sw);
4494 }
Dianne Hackborn1d62ea92009-11-17 12:49:50 -08004495 }
4496
4497 public int getDPadKeycodeState(int sw) {
4498 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4499 "getDPadKeycodeState()")) {
4500 throw new SecurityException("Requires READ_INPUT_STATE permission");
4501 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004502 if (ENABLE_NATIVE_INPUT_DISPATCH) {
4503 return mInputManager.getDPadKeycodeState(sw);
4504 } else {
4505 return mQueue.getDPadKeycodeState(sw);
4506 }
Dianne Hackborn1d62ea92009-11-17 12:49:50 -08004507 }
4508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004509 public boolean hasKeys(int[] keycodes, boolean[] keyExists) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004510 if (ENABLE_NATIVE_INPUT_DISPATCH) {
4511 return mInputManager.hasKeys(keycodes, keyExists);
4512 } else {
4513 return KeyInputQueue.hasKeys(keycodes, keyExists);
4514 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004515 }
Romain Guy06882f82009-06-10 13:36:04 -07004516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004517 public void enableScreenAfterBoot() {
4518 synchronized(mWindowMap) {
4519 if (mSystemBooted) {
4520 return;
4521 }
4522 mSystemBooted = true;
4523 }
Romain Guy06882f82009-06-10 13:36:04 -07004524
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004525 performEnableScreen();
4526 }
Romain Guy06882f82009-06-10 13:36:04 -07004527
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004528 public void enableScreenIfNeededLocked() {
4529 if (mDisplayEnabled) {
4530 return;
4531 }
4532 if (!mSystemBooted) {
4533 return;
4534 }
4535 mH.sendMessage(mH.obtainMessage(H.ENABLE_SCREEN));
4536 }
Romain Guy06882f82009-06-10 13:36:04 -07004537
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004538 public void performEnableScreen() {
4539 synchronized(mWindowMap) {
4540 if (mDisplayEnabled) {
4541 return;
4542 }
4543 if (!mSystemBooted) {
4544 return;
4545 }
Romain Guy06882f82009-06-10 13:36:04 -07004546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004547 // Don't enable the screen until all existing windows
4548 // have been drawn.
4549 final int N = mWindows.size();
4550 for (int i=0; i<N; i++) {
4551 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn5943c202010-04-12 21:36:49 -07004552 if (w.isVisibleLw() && !w.mObscured
4553 && (w.mOrientationChanging || !w.isDrawnLw())) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004554 return;
4555 }
4556 }
Romain Guy06882f82009-06-10 13:36:04 -07004557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004558 mDisplayEnabled = true;
4559 if (false) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004560 Slog.i(TAG, "ENABLING SCREEN!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004561 StringWriter sw = new StringWriter();
4562 PrintWriter pw = new PrintWriter(sw);
4563 this.dump(null, pw, null);
Joe Onorato8a9b2202010-02-26 18:56:32 -08004564 Slog.i(TAG, sw.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004565 }
4566 try {
4567 IBinder surfaceFlinger = ServiceManager.getService("SurfaceFlinger");
4568 if (surfaceFlinger != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004569 //Slog.i(TAG, "******* TELLING SURFACE FLINGER WE ARE BOOTED!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004570 Parcel data = Parcel.obtain();
4571 data.writeInterfaceToken("android.ui.ISurfaceComposer");
4572 surfaceFlinger.transact(IBinder.FIRST_CALL_TRANSACTION,
4573 data, null, 0);
4574 data.recycle();
4575 }
4576 } catch (RemoteException ex) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004577 Slog.e(TAG, "Boot completed: SurfaceFlinger is dead!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004578 }
4579 }
Romain Guy06882f82009-06-10 13:36:04 -07004580
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004581 mPolicy.enableScreenAfterBoot();
Romain Guy06882f82009-06-10 13:36:04 -07004582
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004583 // Make sure the last requested orientation has been applied.
Dianne Hackborn321ae682009-03-27 16:16:03 -07004584 setRotationUnchecked(WindowManagerPolicy.USE_LAST_ROTATION, false,
4585 mLastRotationFlags | Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004586 }
Romain Guy06882f82009-06-10 13:36:04 -07004587
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004588 public void setInTouchMode(boolean mode) {
4589 synchronized(mWindowMap) {
4590 mInTouchMode = mode;
4591 }
4592 }
4593
Romain Guy06882f82009-06-10 13:36:04 -07004594 public void setRotation(int rotation,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004595 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004596 if (!checkCallingPermission(android.Manifest.permission.SET_ORIENTATION,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004597 "setRotation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004598 throw new SecurityException("Requires SET_ORIENTATION permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004599 }
4600
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004601 setRotationUnchecked(rotation, alwaysSendConfiguration, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004602 }
Romain Guy06882f82009-06-10 13:36:04 -07004603
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004604 public void setRotationUnchecked(int rotation,
4605 boolean alwaysSendConfiguration, int animFlags) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004606 if(DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004607 "alwaysSendConfiguration set to "+alwaysSendConfiguration);
Romain Guy06882f82009-06-10 13:36:04 -07004608
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004609 long origId = Binder.clearCallingIdentity();
4610 boolean changed;
4611 synchronized(mWindowMap) {
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004612 changed = setRotationUncheckedLocked(rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004613 }
Romain Guy06882f82009-06-10 13:36:04 -07004614
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004615 if (changed || alwaysSendConfiguration) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004616 sendNewConfiguration();
4617 }
Romain Guy06882f82009-06-10 13:36:04 -07004618
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004619 Binder.restoreCallingIdentity(origId);
4620 }
Romain Guy06882f82009-06-10 13:36:04 -07004621
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004622 /**
4623 * Apply a new rotation to the screen, respecting the requests of
4624 * applications. Use WindowManagerPolicy.USE_LAST_ROTATION to simply
4625 * re-evaluate the desired rotation.
4626 *
4627 * Returns null if the rotation has been changed. In this case YOU
4628 * MUST CALL setNewConfiguration() TO UNFREEZE THE SCREEN.
4629 */
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004630 public boolean setRotationUncheckedLocked(int rotation, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004631 boolean changed;
4632 if (rotation == WindowManagerPolicy.USE_LAST_ROTATION) {
4633 rotation = mRequestedRotation;
4634 } else {
4635 mRequestedRotation = rotation;
Dianne Hackborn321ae682009-03-27 16:16:03 -07004636 mLastRotationFlags = animFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004637 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08004638 if (DEBUG_ORIENTATION) Slog.v(TAG, "Overwriting rotation value from " + rotation);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004639 rotation = mPolicy.rotationForOrientationLw(mForcedAppOrientation,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004640 mRotation, mDisplayEnabled);
Joe Onorato8a9b2202010-02-26 18:56:32 -08004641 if (DEBUG_ORIENTATION) Slog.v(TAG, "new rotation is set to " + rotation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004642 changed = mDisplayEnabled && mRotation != rotation;
Romain Guy06882f82009-06-10 13:36:04 -07004643
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004644 if (changed) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004645 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004646 "Rotation changed to " + rotation
4647 + " from " + mRotation
4648 + " (forceApp=" + mForcedAppOrientation
4649 + ", req=" + mRequestedRotation + ")");
4650 mRotation = rotation;
4651 mWindowsFreezingScreen = true;
4652 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
4653 mH.sendMessageDelayed(mH.obtainMessage(H.WINDOW_FREEZE_TIMEOUT),
4654 2000);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004655 mWaitingForConfig = true;
4656 mLayoutNeeded = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004657 startFreezingDisplayLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08004658 Slog.i(TAG, "Setting rotation to " + rotation + ", animFlags=" + animFlags);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004659 if (ENABLE_NATIVE_INPUT_DISPATCH) {
4660 mInputManager.setDisplayOrientation(0, rotation);
4661 } else {
4662 mQueue.setOrientation(rotation);
4663 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004664 if (mDisplayEnabled) {
Dianne Hackborn321ae682009-03-27 16:16:03 -07004665 Surface.setOrientation(0, rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004666 }
4667 for (int i=mWindows.size()-1; i>=0; i--) {
4668 WindowState w = (WindowState)mWindows.get(i);
4669 if (w.mSurface != null) {
4670 w.mOrientationChanging = true;
4671 }
4672 }
4673 for (int i=mRotationWatchers.size()-1; i>=0; i--) {
4674 try {
4675 mRotationWatchers.get(i).onRotationChanged(rotation);
4676 } catch (RemoteException e) {
4677 }
4678 }
4679 } //end if changed
Romain Guy06882f82009-06-10 13:36:04 -07004680
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004681 return changed;
4682 }
Romain Guy06882f82009-06-10 13:36:04 -07004683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004684 public int getRotation() {
4685 return mRotation;
4686 }
4687
4688 public int watchRotation(IRotationWatcher watcher) {
4689 final IBinder watcherBinder = watcher.asBinder();
4690 IBinder.DeathRecipient dr = new IBinder.DeathRecipient() {
4691 public void binderDied() {
4692 synchronized (mWindowMap) {
4693 for (int i=0; i<mRotationWatchers.size(); i++) {
4694 if (watcherBinder == mRotationWatchers.get(i).asBinder()) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07004695 IRotationWatcher removed = mRotationWatchers.remove(i);
4696 if (removed != null) {
4697 removed.asBinder().unlinkToDeath(this, 0);
4698 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004699 i--;
4700 }
4701 }
4702 }
4703 }
4704 };
Romain Guy06882f82009-06-10 13:36:04 -07004705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004706 synchronized (mWindowMap) {
4707 try {
4708 watcher.asBinder().linkToDeath(dr, 0);
4709 mRotationWatchers.add(watcher);
4710 } catch (RemoteException e) {
4711 // Client died, no cleanup needed.
4712 }
Romain Guy06882f82009-06-10 13:36:04 -07004713
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004714 return mRotation;
4715 }
4716 }
4717
4718 /**
4719 * Starts the view server on the specified port.
4720 *
4721 * @param port The port to listener to.
4722 *
4723 * @return True if the server was successfully started, false otherwise.
4724 *
4725 * @see com.android.server.ViewServer
4726 * @see com.android.server.ViewServer#VIEW_SERVER_DEFAULT_PORT
4727 */
4728 public boolean startViewServer(int port) {
Romain Guy06882f82009-06-10 13:36:04 -07004729 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004730 return false;
4731 }
4732
4733 if (!checkCallingPermission(Manifest.permission.DUMP, "startViewServer")) {
4734 return false;
4735 }
4736
4737 if (port < 1024) {
4738 return false;
4739 }
4740
4741 if (mViewServer != null) {
4742 if (!mViewServer.isRunning()) {
4743 try {
4744 return mViewServer.start();
4745 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004746 Slog.w(TAG, "View server did not start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004747 }
4748 }
4749 return false;
4750 }
4751
4752 try {
4753 mViewServer = new ViewServer(this, port);
4754 return mViewServer.start();
4755 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004756 Slog.w(TAG, "View server did not start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004757 }
4758 return false;
4759 }
4760
Romain Guy06882f82009-06-10 13:36:04 -07004761 private boolean isSystemSecure() {
4762 return "1".equals(SystemProperties.get(SYSTEM_SECURE, "1")) &&
4763 "0".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
4764 }
4765
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004766 /**
4767 * Stops the view server if it exists.
4768 *
4769 * @return True if the server stopped, false if it wasn't started or
4770 * couldn't be stopped.
4771 *
4772 * @see com.android.server.ViewServer
4773 */
4774 public boolean stopViewServer() {
Romain Guy06882f82009-06-10 13:36:04 -07004775 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004776 return false;
4777 }
4778
4779 if (!checkCallingPermission(Manifest.permission.DUMP, "stopViewServer")) {
4780 return false;
4781 }
4782
4783 if (mViewServer != null) {
4784 return mViewServer.stop();
4785 }
4786 return false;
4787 }
4788
4789 /**
4790 * Indicates whether the view server is running.
4791 *
4792 * @return True if the server is running, false otherwise.
4793 *
4794 * @see com.android.server.ViewServer
4795 */
4796 public boolean isViewServerRunning() {
Romain Guy06882f82009-06-10 13:36:04 -07004797 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004798 return false;
4799 }
4800
4801 if (!checkCallingPermission(Manifest.permission.DUMP, "isViewServerRunning")) {
4802 return false;
4803 }
4804
4805 return mViewServer != null && mViewServer.isRunning();
4806 }
4807
4808 /**
4809 * Lists all availble windows in the system. The listing is written in the
4810 * specified Socket's output stream with the following syntax:
4811 * windowHashCodeInHexadecimal windowName
4812 * Each line of the ouput represents a different window.
4813 *
4814 * @param client The remote client to send the listing to.
4815 * @return False if an error occured, true otherwise.
4816 */
4817 boolean viewServerListWindows(Socket client) {
Romain Guy06882f82009-06-10 13:36:04 -07004818 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004819 return false;
4820 }
4821
4822 boolean result = true;
4823
4824 Object[] windows;
4825 synchronized (mWindowMap) {
4826 windows = new Object[mWindows.size()];
4827 //noinspection unchecked
4828 windows = mWindows.toArray(windows);
4829 }
4830
4831 BufferedWriter out = null;
4832
4833 // Any uncaught exception will crash the system process
4834 try {
4835 OutputStream clientStream = client.getOutputStream();
4836 out = new BufferedWriter(new OutputStreamWriter(clientStream), 8 * 1024);
4837
4838 final int count = windows.length;
4839 for (int i = 0; i < count; i++) {
4840 final WindowState w = (WindowState) windows[i];
4841 out.write(Integer.toHexString(System.identityHashCode(w)));
4842 out.write(' ');
4843 out.append(w.mAttrs.getTitle());
4844 out.write('\n');
4845 }
4846
4847 out.write("DONE.\n");
4848 out.flush();
4849 } catch (Exception e) {
4850 result = false;
4851 } finally {
4852 if (out != null) {
4853 try {
4854 out.close();
4855 } catch (IOException e) {
4856 result = false;
4857 }
4858 }
4859 }
4860
4861 return result;
4862 }
4863
4864 /**
4865 * Sends a command to a target window. The result of the command, if any, will be
4866 * written in the output stream of the specified socket.
4867 *
4868 * The parameters must follow this syntax:
4869 * windowHashcode extra
4870 *
4871 * Where XX is the length in characeters of the windowTitle.
4872 *
4873 * The first parameter is the target window. The window with the specified hashcode
4874 * will be the target. If no target can be found, nothing happens. The extra parameters
4875 * will be delivered to the target window and as parameters to the command itself.
4876 *
4877 * @param client The remote client to sent the result, if any, to.
4878 * @param command The command to execute.
4879 * @param parameters The command parameters.
4880 *
4881 * @return True if the command was successfully delivered, false otherwise. This does
4882 * not indicate whether the command itself was successful.
4883 */
4884 boolean viewServerWindowCommand(Socket client, String command, String parameters) {
Romain Guy06882f82009-06-10 13:36:04 -07004885 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004886 return false;
4887 }
4888
4889 boolean success = true;
4890 Parcel data = null;
4891 Parcel reply = null;
4892
4893 // Any uncaught exception will crash the system process
4894 try {
4895 // Find the hashcode of the window
4896 int index = parameters.indexOf(' ');
4897 if (index == -1) {
4898 index = parameters.length();
4899 }
4900 final String code = parameters.substring(0, index);
Romain Guy236092a2009-12-14 15:31:48 -08004901 int hashCode = (int) Long.parseLong(code, 16);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004902
4903 // Extract the command's parameter after the window description
4904 if (index < parameters.length()) {
4905 parameters = parameters.substring(index + 1);
4906 } else {
4907 parameters = "";
4908 }
4909
4910 final WindowManagerService.WindowState window = findWindow(hashCode);
4911 if (window == null) {
4912 return false;
4913 }
4914
4915 data = Parcel.obtain();
4916 data.writeInterfaceToken("android.view.IWindow");
4917 data.writeString(command);
4918 data.writeString(parameters);
4919 data.writeInt(1);
4920 ParcelFileDescriptor.fromSocket(client).writeToParcel(data, 0);
4921
4922 reply = Parcel.obtain();
4923
4924 final IBinder binder = window.mClient.asBinder();
4925 // TODO: GET THE TRANSACTION CODE IN A SAFER MANNER
4926 binder.transact(IBinder.FIRST_CALL_TRANSACTION, data, reply, 0);
4927
4928 reply.readException();
4929
4930 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004931 Slog.w(TAG, "Could not send command " + command + " with parameters " + parameters, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004932 success = false;
4933 } finally {
4934 if (data != null) {
4935 data.recycle();
4936 }
4937 if (reply != null) {
4938 reply.recycle();
4939 }
4940 }
4941
4942 return success;
4943 }
4944
4945 private WindowState findWindow(int hashCode) {
4946 if (hashCode == -1) {
4947 return getFocusedWindow();
4948 }
4949
4950 synchronized (mWindowMap) {
4951 final ArrayList windows = mWindows;
4952 final int count = windows.size();
4953
4954 for (int i = 0; i < count; i++) {
4955 WindowState w = (WindowState) windows.get(i);
4956 if (System.identityHashCode(w) == hashCode) {
4957 return w;
4958 }
4959 }
4960 }
4961
4962 return null;
4963 }
4964
4965 /*
4966 * Instruct the Activity Manager to fetch the current configuration and broadcast
4967 * that to config-changed listeners if appropriate.
4968 */
4969 void sendNewConfiguration() {
4970 try {
4971 mActivityManager.updateConfiguration(null);
4972 } catch (RemoteException e) {
4973 }
4974 }
Romain Guy06882f82009-06-10 13:36:04 -07004975
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004976 public Configuration computeNewConfiguration() {
4977 synchronized (mWindowMap) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07004978 return computeNewConfigurationLocked();
4979 }
4980 }
Romain Guy06882f82009-06-10 13:36:04 -07004981
Dianne Hackbornc485a602009-03-24 22:39:49 -07004982 Configuration computeNewConfigurationLocked() {
4983 Configuration config = new Configuration();
4984 if (!computeNewConfigurationLocked(config)) {
4985 return null;
4986 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07004987 return config;
4988 }
Romain Guy06882f82009-06-10 13:36:04 -07004989
Dianne Hackbornc485a602009-03-24 22:39:49 -07004990 boolean computeNewConfigurationLocked(Configuration config) {
4991 if (mDisplay == null) {
4992 return false;
4993 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004994 if (ENABLE_NATIVE_INPUT_DISPATCH) {
4995 mInputManager.getInputConfiguration(config);
4996 } else {
4997 mQueue.getInputConfiguration(config);
4998 }
Christopher Tateb696aee2010-04-02 19:08:30 -07004999
5000 // Use the effective "visual" dimensions based on current rotation
5001 final boolean rotated = (mRotation == Surface.ROTATION_90
5002 || mRotation == Surface.ROTATION_270);
5003 final int dw = rotated ? mInitialDisplayHeight : mInitialDisplayWidth;
5004 final int dh = rotated ? mInitialDisplayWidth : mInitialDisplayHeight;
5005
Dianne Hackbornc485a602009-03-24 22:39:49 -07005006 int orientation = Configuration.ORIENTATION_SQUARE;
5007 if (dw < dh) {
5008 orientation = Configuration.ORIENTATION_PORTRAIT;
5009 } else if (dw > dh) {
5010 orientation = Configuration.ORIENTATION_LANDSCAPE;
5011 }
5012 config.orientation = orientation;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005013
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07005014 DisplayMetrics dm = new DisplayMetrics();
5015 mDisplay.getMetrics(dm);
5016 CompatibilityInfo.updateCompatibleScreenFrame(dm, orientation, mCompatibleScreenFrame);
5017
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07005018 if (mScreenLayout == Configuration.SCREENLAYOUT_SIZE_UNDEFINED) {
Dianne Hackborn723738c2009-06-25 19:48:04 -07005019 // Note we only do this once because at this point we don't
5020 // expect the screen to change in this way at runtime, and want
5021 // to avoid all of this computation for every config change.
Dianne Hackborn723738c2009-06-25 19:48:04 -07005022 int longSize = dw;
5023 int shortSize = dh;
5024 if (longSize < shortSize) {
5025 int tmp = longSize;
5026 longSize = shortSize;
5027 shortSize = tmp;
5028 }
5029 longSize = (int)(longSize/dm.density);
5030 shortSize = (int)(shortSize/dm.density);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07005031
Dianne Hackborn723738c2009-06-25 19:48:04 -07005032 // These semi-magic numbers define our compatibility modes for
5033 // applications with different screens. Don't change unless you
5034 // make sure to test lots and lots of apps!
5035 if (longSize < 470) {
5036 // This is shorter than an HVGA normal density screen (which
5037 // is 480 pixels on its long side).
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07005038 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_SMALL
5039 | Configuration.SCREENLAYOUT_LONG_NO;
Dianne Hackborn723738c2009-06-25 19:48:04 -07005040 } else {
Dianne Hackborn14cee9f2010-04-23 17:51:26 -07005041 // What size is this screen screen?
5042 if (longSize >= 800 && shortSize >= 600) {
5043 // SVGA or larger screens at medium density are the point
5044 // at which we consider it to be an extra large screen.
5045 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_XLARGE;
5046 } else if (longSize >= 640 && shortSize >= 480) {
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07005047 // VGA or larger screens at medium density are the point
5048 // at which we consider it to be a large screen.
5049 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_LARGE;
5050 } else {
5051 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_NORMAL;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005052
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07005053 // If this screen is wider than normal HVGA, or taller
5054 // than FWVGA, then for old apps we want to run in size
5055 // compatibility mode.
5056 if (shortSize > 321 || longSize > 570) {
5057 mScreenLayout |= Configuration.SCREENLAYOUT_COMPAT_NEEDED;
5058 }
5059 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005060
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07005061 // Is this a long screen?
5062 if (((longSize*3)/5) >= (shortSize-1)) {
5063 // Anything wider than WVGA (5:3) is considering to be long.
5064 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_YES;
5065 } else {
5066 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_NO;
5067 }
Dianne Hackborn723738c2009-06-25 19:48:04 -07005068 }
5069 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07005070 config.screenLayout = mScreenLayout;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005071
Dianne Hackbornc485a602009-03-24 22:39:49 -07005072 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
5073 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
5074 mPolicy.adjustConfigurationLw(config);
5075 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005076 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07005077
5078 /* Notifies the window manager about a broken input channel.
5079 *
5080 * Called by the InputManager.
5081 */
5082 public void notifyInputChannelBroken(InputChannel inputChannel) {
5083 synchronized (mWindowMap) {
5084 WindowState windowState = getWindowStateForInputChannelLocked(inputChannel);
5085 if (windowState == null) {
5086 return; // irrelevant
5087 }
5088
5089 Slog.i(TAG, "WINDOW DIED " + windowState);
5090 removeWindowLocked(windowState.mSession, windowState);
5091 }
5092 }
5093
5094 /* Notifies the window manager about a broken input channel.
5095 *
5096 * Called by the InputManager.
5097 */
5098 public long notifyInputChannelANR(InputChannel inputChannel) {
5099 IApplicationToken appToken;
5100 synchronized (mWindowMap) {
5101 WindowState windowState = getWindowStateForInputChannelLocked(inputChannel);
5102 if (windowState == null) {
5103 return -2; // irrelevant, abort dispatching (-2)
5104 }
5105
5106 Slog.i(TAG, "Input event dispatching timed out sending to "
5107 + windowState.mAttrs.getTitle());
5108 appToken = windowState.getAppToken();
5109 }
5110
5111 try {
5112 // Notify the activity manager about the timeout and let it decide whether
5113 // to abort dispatching or keep waiting.
5114 boolean abort = appToken.keyDispatchingTimedOut();
5115 if (abort) {
5116 return -2; // abort dispatching
5117 }
5118
5119 // Return new timeout.
5120 // We use -1 for infinite timeout to avoid clash with -2 magic number.
5121 long newTimeout = appToken.getKeyDispatchingTimeout() * 1000000;
5122 return newTimeout < 0 ? -1 : newTimeout;
5123 } catch (RemoteException ex) {
5124 return -2; // abort dispatching
5125 }
5126 }
5127
5128 /* Notifies the window manager about a broken input channel.
5129 *
5130 * Called by the InputManager.
5131 */
5132 public void notifyInputChannelRecoveredFromANR(InputChannel inputChannel) {
5133 // Nothing to do just now.
5134 // Just wait for the user to dismiss the ANR dialog.
5135
5136 // TODO We could try to automatically dismiss the ANR dialog on recovery
5137 // although that might be disorienting.
5138 }
5139
5140 private WindowState getWindowStateForInputChannelLocked(InputChannel inputChannel) {
5141 int windowCount = mWindows.size();
5142 for (int i = 0; i < windowCount; i++) {
5143 WindowState windowState = (WindowState) mWindows.get(i);
5144 if (windowState.mInputChannel == inputChannel) {
5145 return windowState;
5146 }
5147 }
5148
5149 return null;
5150 }
Romain Guy06882f82009-06-10 13:36:04 -07005151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005152 // -------------------------------------------------------------
5153 // Input Events and Focus Management
5154 // -------------------------------------------------------------
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005155
Jeff Brown7fbdc842010-06-17 20:52:56 -07005156 private boolean checkInjectionPermissionTd(WindowState focus,
5157 int injectorPid, int injectorUid) {
5158 if (injectorUid > 0 && (focus == null || injectorUid != focus.mSession.mUid)) {
5159 if (mContext.checkPermission(
5160 android.Manifest.permission.INJECT_EVENTS, injectorPid, injectorUid)
5161 != PackageManager.PERMISSION_GRANTED) {
5162 Slog.w(TAG, "Permission denied: injecting key event from pid "
5163 + injectorPid + " uid " + injectorUid + " to window " + focus
5164 + " owned by uid " + focus.mSession.mUid);
5165 return false;
5166 }
5167 }
5168 return true;
5169 }
5170
5171 /* Gets the input targets for a key event.
5172 *
5173 * Called by the InputManager on the InputDispatcher thread.
5174 */
5175 public int getKeyEventTargetsTd(InputTargetList inputTargets,
5176 KeyEvent event, int nature, int policyFlags, int injectorPid, int injectorUid) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005177 if (DEBUG_INPUT) Slog.v(TAG, "Dispatch key: " + event);
5178
5179 // TODO what do we do with mDisplayFrozen?
5180 // TODO what do we do with focus.mToken.paused?
5181
5182 WindowState focus = getFocusedWindow();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005183
Jeff Brown7fbdc842010-06-17 20:52:56 -07005184 if (! checkInjectionPermissionTd(focus, injectorPid, injectorUid)) {
5185 return InputManager.INPUT_EVENT_INJECTION_PERMISSION_DENIED;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005186 }
5187
Jeff Brown7fbdc842010-06-17 20:52:56 -07005188 if (mPolicy.interceptKeyTi(focus, event.getKeyCode(), event.getMetaState(),
5189 event.getAction() == KeyEvent.ACTION_DOWN,
5190 event.getRepeatCount(), event.getFlags())) {
5191 // Policy consumed the event.
5192 return InputManager.INPUT_EVENT_INJECTION_SUCCEEDED;
5193 }
5194
5195 if (focus == null) {
5196 return InputManager.INPUT_EVENT_INJECTION_FAILED;
5197 }
5198
5199 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
5200
5201 addInputTargetTd(inputTargets, focus, InputTarget.FLAG_SYNC);
5202 return InputManager.INPUT_EVENT_INJECTION_SUCCEEDED;
5203 }
5204
5205 /* Gets the input targets for a motion event.
5206 *
5207 * Called by the InputManager on the InputDispatcher thread.
5208 */
5209 public int getMotionEventTargetsTd(InputTargetList inputTargets,
5210 MotionEvent event, int nature, int policyFlags, int injectorPid, int injectorUid) {
5211 switch (nature) {
5212 case InputQueue.INPUT_EVENT_NATURE_TRACKBALL:
5213 return getMotionEventTargetsForTrackballTd(inputTargets, event, policyFlags,
5214 injectorPid, injectorUid);
5215
5216 case InputQueue.INPUT_EVENT_NATURE_TOUCH:
5217 return getMotionEventTargetsForTouchTd(inputTargets, event, policyFlags,
5218 injectorPid, injectorUid);
5219
5220 default:
5221 return InputManager.INPUT_EVENT_INJECTION_FAILED;
5222 }
5223 }
5224
5225 /* Gets the input targets for a trackball event.
5226 *
5227 * Called by the InputManager on the InputDispatcher thread.
5228 */
5229 private int getMotionEventTargetsForTrackballTd(InputTargetList inputTargets,
5230 MotionEvent event, int policyFlags, int injectorPid, int injectorUid) {
5231 WindowState focus = getFocusedWindow();
5232
5233 if (! checkInjectionPermissionTd(focus, injectorPid, injectorUid)) {
5234 return InputManager.INPUT_EVENT_INJECTION_PERMISSION_DENIED;
5235 }
5236
5237 if (focus == null) {
5238 return InputManager.INPUT_EVENT_INJECTION_FAILED;
5239 }
5240
5241 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
5242
5243 addInputTargetTd(inputTargets, focus, InputTarget.FLAG_SYNC);
5244 return InputManager.INPUT_EVENT_INJECTION_SUCCEEDED;
5245 }
5246
5247 /* Set to true when a fat touch has been detected during the processing of a touch event.
5248 *
5249 * Only used by getMotionEventTargetsForTouchTd.
5250 * Set to true whenever a fat touch is detected and reset to false on ACTION_UP.
5251 */
5252 private boolean mFatTouch;
5253
5254 /* Set to true when we think the touch event.
5255 *
5256 * Only used by getMotionEventTargetsForTouchTd.
5257 * Set to true on ACTION_DOWN and set to false on ACTION_UP.
5258 */
5259 private boolean mTouchDown;
5260
5261 /* Current target of Motion events.
5262 *
5263 * Only used by getMotionEventTargetsForTouchTd.
5264 * Initialized on ACTION_DOWN and cleared on ACTION_UP.
5265 */
5266 private WindowState mTouchFocus;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005267
Jeff Brown7fbdc842010-06-17 20:52:56 -07005268 /* Windows above the target that would like to receive an "outside" touch event
5269 * for any down events outside of them.
5270 *
5271 * Only used by getMotionEventTargetsForTouchTd.
5272 * Initialized on ACTION_DOWN and cleared immediately afterwards.
5273 */
5274 private ArrayList<WindowState> mOutsideTouchTargets = new ArrayList<WindowState>();
5275
5276 /* Wallpaper windows that are currently receiving touch events.
5277 *
5278 * Only used by getMotionEventTargetsForTouchTd.
5279 * Initialized on ACTION_DOWN and cleared on ACTION_UP.
5280 */
5281 private ArrayList<WindowState> mWallpaperTouchTargets = new ArrayList<WindowState>();
5282
5283 /* Gets the input targets for a touch event.
5284 *
5285 * Called by the InputManager on the InputDispatcher thread.
5286 */
5287 private int getMotionEventTargetsForTouchTd(InputTargetList inputTargets,
5288 MotionEvent event, int policyFlags, int injectorPid, int injectorUid) {
5289 final int action = event.getAction();
5290
5291 if (action == MotionEvent.ACTION_DOWN) {
5292 updateTouchFocusBeforeDownTd(event, policyFlags);
5293 } else {
5294 updateTouchFocusBeforeNonDownTd(event, policyFlags);
5295 }
5296
5297 boolean skipDelivery = false;
5298 int touchTargetFlags = 0;
5299
5300 int injectionResult = InputManager.INPUT_EVENT_INJECTION_SUCCEEDED;
5301 WindowState focusedTouchTarget = mTouchFocus;
5302 if (focusedTouchTarget == null) {
5303 // In this case we are either dropping the event, or have received
5304 // a move or up without a down. It is common to receive move
5305 // events in such a way, since this means the user is moving the
5306 // pointer without actually pressing down. All other cases should
5307 // be atypical, so let's log them.
5308 if (action != MotionEvent.ACTION_MOVE) {
5309 Slog.w(TAG, "No window to dispatch pointer action " + action);
5310 injectionResult = InputManager.INPUT_EVENT_INJECTION_FAILED;
5311 }
5312 } else {
5313 // We have a valid focused touch target.
5314 if (! checkInjectionPermissionTd(focusedTouchTarget, injectorPid, injectorUid)) {
5315 return InputManager.INPUT_EVENT_INJECTION_PERMISSION_DENIED;
5316 }
5317
5318 wakeupIfNeeded(focusedTouchTarget, eventType(event));
5319
5320 if ((focusedTouchTarget.mAttrs.flags &
5321 WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES) != 0) {
5322 // Target wants to ignore fat touch events
5323 boolean cheekPress = mPolicy.isCheekPressedAgainstScreen(event);
5324
5325 if (cheekPress) {
5326 if ((action == MotionEvent.ACTION_DOWN)) {
5327 mFatTouch = true;
5328 skipDelivery = true;
5329 } else {
5330 if (! mFatTouch) {
5331 // cancel the earlier event
5332 touchTargetFlags |= InputTarget.FLAG_CANCEL;
5333 mFatTouch = true;
5334 } else {
5335 skipDelivery = true;
5336 }
5337 }
5338 }
5339 }
5340 }
5341
5342 if (! skipDelivery) {
5343 int outsideTargetCount = mOutsideTouchTargets.size();
5344 for (int i = 0; i < outsideTargetCount; i++) {
5345 WindowState outsideTouchTarget = mOutsideTouchTargets.get(i);
5346 addInputTargetTd(inputTargets, outsideTouchTarget,
5347 InputTarget.FLAG_OUTSIDE | touchTargetFlags);
5348 }
5349
5350 int wallpaperTargetCount = mWallpaperTouchTargets.size();
5351 for (int i = 0; i < wallpaperTargetCount; i++) {
5352 WindowState wallpaperTouchTarget = mWallpaperTouchTargets.get(i);
5353 addInputTargetTd(inputTargets, wallpaperTouchTarget,
5354 touchTargetFlags);
5355 }
5356
5357 if (focusedTouchTarget != null) {
5358 addInputTargetTd(inputTargets, focusedTouchTarget,
5359 InputTarget.FLAG_SYNC | touchTargetFlags);
5360 }
5361 }
5362
5363 if (action == MotionEvent.ACTION_UP) {
5364 updateTouchFocusAfterUpTd(event, policyFlags);
5365 }
5366
5367 return injectionResult;
5368 }
5369
5370 private void updateTouchFocusBeforeDownTd(MotionEvent event, int policyFlags) {
5371 if (mTouchDown) {
5372 // This is weird, we got a down, but we thought it was already down!
5373 // XXX: We should probably send an ACTION_UP to the current target.
5374 Slog.w(TAG, "Pointer down received while already down in: " + mTouchFocus);
5375 updateTouchFocusAfterUpTd(event, policyFlags);
5376 }
5377
5378 mTouchDown = true;
5379 mPowerManager.logPointerDownEvent();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005380
5381 final boolean screenWasOff = (policyFlags & WindowManagerPolicy.FLAG_BRIGHT_HERE) != 0;
Jeff Brown7fbdc842010-06-17 20:52:56 -07005382 synchronized (mWindowMap) {
5383 final int x = (int) event.getX();
5384 final int y = (int) event.getY();
5385
5386 final ArrayList windows = mWindows;
5387 final int N = windows.size();
5388 WindowState topErrWindow = null;
5389 final Rect tmpRect = mTempRect;
5390 for (int i= N - 1; i >= 0; i--) {
5391 WindowState child = (WindowState) windows.get(i);
5392 //Slog.i(TAG, "Checking dispatch to: " + child);
5393
5394 final int flags = child.mAttrs.flags;
5395 if ((flags & WindowManager.LayoutParams.FLAG_SYSTEM_ERROR) != 0) {
5396 if (topErrWindow == null) {
5397 topErrWindow = child;
5398 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005399 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07005400
5401 if (!child.isVisibleLw()) {
5402 //Slog.i(TAG, "Not visible!");
5403 continue;
5404 }
5405
5406 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) == 0) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005407 tmpRect.set(child.mFrame);
5408 if (child.mTouchableInsets == ViewTreeObserver
5409 .InternalInsetsInfo.TOUCHABLE_INSETS_CONTENT) {
5410 // The touch is inside of the window if it is
5411 // inside the frame, AND the content part of that
5412 // frame that was given by the application.
5413 tmpRect.left += child.mGivenContentInsets.left;
5414 tmpRect.top += child.mGivenContentInsets.top;
5415 tmpRect.right -= child.mGivenContentInsets.right;
5416 tmpRect.bottom -= child.mGivenContentInsets.bottom;
5417 } else if (child.mTouchableInsets == ViewTreeObserver
5418 .InternalInsetsInfo.TOUCHABLE_INSETS_VISIBLE) {
5419 // The touch is inside of the window if it is
5420 // inside the frame, AND the visible part of that
5421 // frame that was given by the application.
5422 tmpRect.left += child.mGivenVisibleInsets.left;
5423 tmpRect.top += child.mGivenVisibleInsets.top;
5424 tmpRect.right -= child.mGivenVisibleInsets.right;
5425 tmpRect.bottom -= child.mGivenVisibleInsets.bottom;
5426 }
5427 final int touchFlags = flags &
5428 (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
5429 |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
5430 if (tmpRect.contains(x, y) || touchFlags == 0) {
5431 //Slog.i(TAG, "Using this target!");
Jeff Brown7fbdc842010-06-17 20:52:56 -07005432 if (! screenWasOff || (flags &
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005433 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING) != 0) {
5434 mTouchFocus = child;
5435 } else {
5436 //Slog.i(TAG, "Waking, skip!");
5437 mTouchFocus = null;
5438 }
5439 break;
5440 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005441 }
5442
Jeff Brown7fbdc842010-06-17 20:52:56 -07005443 if ((flags & WindowManager.LayoutParams
5444 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5445 //Slog.i(TAG, "Adding to outside target list: " + child);
5446 mOutsideTouchTargets.add(child);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005447 }
5448 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07005449
5450 // If there's an error window but it's not accepting focus (typically because
5451 // it is not yet visible) just wait for it -- any other focused window may in fact
5452 // be in ANR state.
5453 if (topErrWindow != null && mTouchFocus != topErrWindow) {
5454 mTouchFocus = null;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005455 }
5456
Jeff Brown7fbdc842010-06-17 20:52:56 -07005457 // Drop the touch focus if the window is not visible.
5458 if (mTouchFocus != null && ! mTouchFocus.isVisibleLw()) {
5459 mTouchFocus = null;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005460 }
5461
Jeff Brown7fbdc842010-06-17 20:52:56 -07005462 // Determine wallpaper targets.
5463 if (mTouchFocus != null
5464 && mTouchFocus == mWallpaperTarget
5465 && mTouchFocus.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005466 int curTokenIndex = mWallpaperTokens.size();
5467 while (curTokenIndex > 0) {
5468 curTokenIndex--;
5469 WindowToken token = mWallpaperTokens.get(curTokenIndex);
Jeff Brown7fbdc842010-06-17 20:52:56 -07005470
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005471 int curWallpaperIndex = token.windows.size();
5472 while (curWallpaperIndex > 0) {
5473 curWallpaperIndex--;
5474 WindowState wallpaper = token.windows.get(curWallpaperIndex);
5475 if ((wallpaper.mAttrs.flags &
Jeff Brown7fbdc842010-06-17 20:52:56 -07005476 WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) == 0) {
5477 mWallpaperTouchTargets.add(wallpaper);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005478 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005479 }
5480 }
5481 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07005482 }
5483 }
5484
5485 private void updateTouchFocusBeforeNonDownTd(MotionEvent event, int policyFlags) {
5486 synchronized (mWindowMap) {
5487 // Drop the touch focus if the window is not visible.
5488 if (mTouchFocus != null && ! mTouchFocus.isVisibleLw()) {
5489 mTouchFocus = null;
5490 mWallpaperTouchTargets.clear();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005491 }
5492 }
5493 }
5494
Jeff Brown7fbdc842010-06-17 20:52:56 -07005495 private void updateTouchFocusAfterUpTd(MotionEvent event, int policyFlags) {
5496 mFatTouch = false;
5497 mTouchDown = false;
5498 mTouchFocus = null;
5499 mOutsideTouchTargets.clear();
5500 mWallpaperTouchTargets.clear();
5501
5502 mPowerManager.logPointerUpEvent();
5503 }
5504
5505 /* Adds a window to a list of input targets.
5506 * Do NOT call this method while holding any locks because the call to
5507 * appToken.getKeyDispatchingTimeout() can potentially call into the ActivityManager
5508 * and create a deadlock hazard.
5509 */
5510 private void addInputTargetTd(InputTargetList inputTargets, WindowState window, int flags) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005511 if (window.mInputChannel == null) {
5512 return;
5513 }
5514
5515 long timeoutNanos = -1;
5516 IApplicationToken appToken = window.getAppToken();
5517
5518 if (appToken != null) {
5519 try {
5520 timeoutNanos = appToken.getKeyDispatchingTimeout() * 1000000;
5521 } catch (RemoteException ex) {
5522 Slog.w(TAG, "Could not get key dispatching timeout.", ex);
5523 }
5524 }
5525
5526 inputTargets.add(window.mInputChannel, flags, timeoutNanos,
5527 - window.mFrame.left, - window.mFrame.top);
5528 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005529
5530 private final void wakeupIfNeeded(WindowState targetWin, int eventType) {
Michael Chane96440f2009-05-06 10:27:36 -07005531 long curTime = SystemClock.uptimeMillis();
5532
Michael Chane10de972009-05-18 11:24:50 -07005533 if (eventType == TOUCH_EVENT || eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT) {
Michael Chane96440f2009-05-06 10:27:36 -07005534 if (mLastTouchEventType == eventType &&
5535 (curTime - mLastUserActivityCallTime) < MIN_TIME_BETWEEN_USERACTIVITIES) {
5536 return;
5537 }
5538 mLastUserActivityCallTime = curTime;
5539 mLastTouchEventType = eventType;
5540 }
5541
5542 if (targetWin == null
5543 || targetWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
5544 mPowerManager.userActivity(curTime, false, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005545 }
5546 }
5547
5548 // tells if it's a cheek event or not -- this function is stateful
5549 private static final int EVENT_NONE = 0;
5550 private static final int EVENT_UNKNOWN = 0;
5551 private static final int EVENT_CHEEK = 0;
5552 private static final int EVENT_IGNORE_DURATION = 300; // ms
5553 private static final float CHEEK_THRESHOLD = 0.6f;
5554 private int mEventState = EVENT_NONE;
5555 private float mEventSize;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07005556
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005557 private int eventType(MotionEvent ev) {
5558 float size = ev.getSize();
5559 switch (ev.getAction()) {
5560 case MotionEvent.ACTION_DOWN:
5561 mEventSize = size;
5562 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_EVENT;
5563 case MotionEvent.ACTION_UP:
5564 if (size > mEventSize) mEventSize = size;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07005565 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005566 case MotionEvent.ACTION_MOVE:
5567 final int N = ev.getHistorySize();
5568 if (size > mEventSize) mEventSize = size;
5569 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
5570 for (int i=0; i<N; i++) {
5571 size = ev.getHistoricalSize(i);
5572 if (size > mEventSize) mEventSize = size;
5573 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
5574 }
5575 if (ev.getEventTime() < ev.getDownTime() + EVENT_IGNORE_DURATION) {
5576 return TOUCH_EVENT;
5577 } else {
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07005578 return LONG_TOUCH_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005579 }
5580 default:
5581 // not good
5582 return OTHER_EVENT;
5583 }
5584 }
5585
5586 /**
5587 * @return Returns true if event was dispatched, false if it was dropped for any reason
5588 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07005589 private int dispatchPointer(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005590 if (DEBUG_INPUT || WindowManagerPolicy.WATCH_POINTER) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005591 "dispatchPointer " + ev);
5592
Michael Chan53071d62009-05-13 17:29:48 -07005593 if (MEASURE_LATENCY) {
5594 lt.sample("3 Wait for last dispatch ", System.nanoTime() - qev.whenNano);
5595 }
5596
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005597 Object targetObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005598 ev, true, false, pid, uid);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005599
Michael Chan53071d62009-05-13 17:29:48 -07005600 if (MEASURE_LATENCY) {
5601 lt.sample("3 Last dispatch finished ", System.nanoTime() - qev.whenNano);
5602 }
5603
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005604 int action = ev.getAction();
Romain Guy06882f82009-06-10 13:36:04 -07005605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005606 if (action == MotionEvent.ACTION_UP) {
5607 // let go of our target
5608 mKeyWaiter.mMotionTarget = null;
5609 mPowerManager.logPointerUpEvent();
5610 } else if (action == MotionEvent.ACTION_DOWN) {
5611 mPowerManager.logPointerDownEvent();
5612 }
5613
5614 if (targetObj == null) {
5615 // In this case we are either dropping the event, or have received
5616 // a move or up without a down. It is common to receive move
5617 // events in such a way, since this means the user is moving the
5618 // pointer without actually pressing down. All other cases should
5619 // be atypical, so let's log them.
Michael Chane96440f2009-05-06 10:27:36 -07005620 if (action != MotionEvent.ACTION_MOVE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005621 Slog.w(TAG, "No window to dispatch pointer action " + ev.getAction());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005622 }
Dianne Hackborn6adba242009-11-10 11:10:09 -08005623 synchronized (mWindowMap) {
Dianne Hackborn90d2db32010-02-11 22:19:06 -08005624 dispatchPointerElsewhereLocked(null, null, ev, ev.getEventTime(), true);
Dianne Hackborn6adba242009-11-10 11:10:09 -08005625 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005626 if (qev != null) {
5627 mQueue.recycleEvent(qev);
5628 }
5629 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005630 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005631 }
5632 if (targetObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborn6adba242009-11-10 11:10:09 -08005633 synchronized (mWindowMap) {
Dianne Hackborn90d2db32010-02-11 22:19:06 -08005634 dispatchPointerElsewhereLocked(null, null, ev, ev.getEventTime(), true);
Dianne Hackborn6adba242009-11-10 11:10:09 -08005635 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005636 if (qev != null) {
5637 mQueue.recycleEvent(qev);
5638 }
5639 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005640 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005641 }
Romain Guy06882f82009-06-10 13:36:04 -07005642
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005643 WindowState target = (WindowState)targetObj;
Romain Guy06882f82009-06-10 13:36:04 -07005644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005645 final long eventTime = ev.getEventTime();
Michael Chan53071d62009-05-13 17:29:48 -07005646 final long eventTimeNano = ev.getEventTimeNano();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005647
Joe Onorato8a9b2202010-02-26 18:56:32 -08005648 //Slog.i(TAG, "Sending " + ev + " to " + target);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005649
5650 if (uid != 0 && uid != target.mSession.mUid) {
5651 if (mContext.checkPermission(
5652 android.Manifest.permission.INJECT_EVENTS, pid, uid)
5653 != PackageManager.PERMISSION_GRANTED) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005654 Slog.w(TAG, "Permission denied: injecting pointer event from pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005655 + pid + " uid " + uid + " to window " + target
5656 + " owned by uid " + target.mSession.mUid);
5657 if (qev != null) {
5658 mQueue.recycleEvent(qev);
5659 }
5660 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005661 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005662 }
5663 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005664
Michael Chan53071d62009-05-13 17:29:48 -07005665 if (MEASURE_LATENCY) {
5666 lt.sample("4 in dispatchPointer ", System.nanoTime() - eventTimeNano);
5667 }
5668
Romain Guy06882f82009-06-10 13:36:04 -07005669 if ((target.mAttrs.flags &
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005670 WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES) != 0) {
5671 //target wants to ignore fat touch events
5672 boolean cheekPress = mPolicy.isCheekPressedAgainstScreen(ev);
5673 //explicit flag to return without processing event further
5674 boolean returnFlag = false;
5675 if((action == MotionEvent.ACTION_DOWN)) {
5676 mFatTouch = false;
5677 if(cheekPress) {
5678 mFatTouch = true;
5679 returnFlag = true;
5680 }
5681 } else {
5682 if(action == MotionEvent.ACTION_UP) {
5683 if(mFatTouch) {
5684 //earlier even was invalid doesnt matter if current up is cheekpress or not
5685 mFatTouch = false;
5686 returnFlag = true;
5687 } else if(cheekPress) {
5688 //cancel the earlier event
5689 ev.setAction(MotionEvent.ACTION_CANCEL);
5690 action = MotionEvent.ACTION_CANCEL;
5691 }
5692 } else if(action == MotionEvent.ACTION_MOVE) {
5693 if(mFatTouch) {
5694 //two cases here
5695 //an invalid down followed by 0 or moves(valid or invalid)
Romain Guy06882f82009-06-10 13:36:04 -07005696 //a valid down, invalid move, more moves. want to ignore till up
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005697 returnFlag = true;
5698 } else if(cheekPress) {
5699 //valid down followed by invalid moves
5700 //an invalid move have to cancel earlier action
5701 ev.setAction(MotionEvent.ACTION_CANCEL);
5702 action = MotionEvent.ACTION_CANCEL;
Joe Onorato8a9b2202010-02-26 18:56:32 -08005703 if (DEBUG_INPUT) Slog.v(TAG, "Sending cancel for invalid ACTION_MOVE");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005704 //note that the subsequent invalid moves will not get here
5705 mFatTouch = true;
5706 }
5707 }
5708 } //else if action
5709 if(returnFlag) {
5710 //recycle que, ev
5711 if (qev != null) {
5712 mQueue.recycleEvent(qev);
5713 }
5714 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005715 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005716 }
5717 } //end if target
Michael Chane96440f2009-05-06 10:27:36 -07005718
Michael Chan9f028e62009-08-04 17:37:46 -07005719 // Enable this for testing the "right" value
5720 if (false && action == MotionEvent.ACTION_DOWN) {
Michael Chane96440f2009-05-06 10:27:36 -07005721 int max_events_per_sec = 35;
5722 try {
5723 max_events_per_sec = Integer.parseInt(SystemProperties
5724 .get("windowsmgr.max_events_per_sec"));
5725 if (max_events_per_sec < 1) {
5726 max_events_per_sec = 35;
5727 }
5728 } catch (NumberFormatException e) {
5729 }
5730 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
5731 }
5732
5733 /*
5734 * Throttle events to minimize CPU usage when there's a flood of events
5735 * e.g. constant contact with the screen
5736 */
5737 if (action == MotionEvent.ACTION_MOVE) {
5738 long nextEventTime = mLastTouchEventTime + mMinWaitTimeBetweenTouchEvents;
5739 long now = SystemClock.uptimeMillis();
5740 if (now < nextEventTime) {
5741 try {
5742 Thread.sleep(nextEventTime - now);
5743 } catch (InterruptedException e) {
5744 }
5745 mLastTouchEventTime = nextEventTime;
5746 } else {
5747 mLastTouchEventTime = now;
5748 }
5749 }
5750
Michael Chan53071d62009-05-13 17:29:48 -07005751 if (MEASURE_LATENCY) {
5752 lt.sample("5 in dispatchPointer ", System.nanoTime() - eventTimeNano);
5753 }
5754
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005755 synchronized(mWindowMap) {
Dianne Hackborn6adba242009-11-10 11:10:09 -08005756 if (!target.isVisibleLw()) {
5757 // During this motion dispatch, the target window has become
5758 // invisible.
Dianne Hackborn90d2db32010-02-11 22:19:06 -08005759 dispatchPointerElsewhereLocked(null, null, ev, ev.getEventTime(), false);
Dianne Hackborn6adba242009-11-10 11:10:09 -08005760 if (qev != null) {
5761 mQueue.recycleEvent(qev);
5762 }
5763 ev.recycle();
5764 return INJECT_SUCCEEDED;
5765 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005766
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005767 if (qev != null && action == MotionEvent.ACTION_MOVE) {
5768 mKeyWaiter.bindTargetWindowLocked(target,
5769 KeyWaiter.RETURN_PENDING_POINTER, qev);
5770 ev = null;
5771 } else {
5772 if (action == MotionEvent.ACTION_DOWN) {
5773 WindowState out = mKeyWaiter.mOutsideTouchTargets;
5774 if (out != null) {
5775 MotionEvent oev = MotionEvent.obtain(ev);
5776 oev.setAction(MotionEvent.ACTION_OUTSIDE);
5777 do {
5778 final Rect frame = out.mFrame;
5779 oev.offsetLocation(-(float)frame.left, -(float)frame.top);
5780 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005781 out.mClient.dispatchPointer(oev, eventTime, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005782 } catch (android.os.RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005783 Slog.i(TAG, "WINDOW DIED during outside motion dispatch: " + out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005784 }
5785 oev.offsetLocation((float)frame.left, (float)frame.top);
5786 out = out.mNextOutsideTouch;
5787 } while (out != null);
5788 mKeyWaiter.mOutsideTouchTargets = null;
5789 }
5790 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005791
Dianne Hackborn90d2db32010-02-11 22:19:06 -08005792 dispatchPointerElsewhereLocked(target, null, ev, ev.getEventTime(), false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005793
Dianne Hackborn6adba242009-11-10 11:10:09 -08005794 final Rect frame = target.mFrame;
5795 ev.offsetLocation(-(float)frame.left, -(float)frame.top);
5796 mKeyWaiter.bindTargetWindowLocked(target);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005797 }
5798 }
Romain Guy06882f82009-06-10 13:36:04 -07005799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005800 // finally offset the event to the target's coordinate system and
5801 // dispatch the event.
5802 try {
5803 if (DEBUG_INPUT || DEBUG_FOCUS || WindowManagerPolicy.WATCH_POINTER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005804 Slog.v(TAG, "Delivering pointer " + qev + " to " + target);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005805 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005806
Michael Chan53071d62009-05-13 17:29:48 -07005807 if (MEASURE_LATENCY) {
5808 lt.sample("6 before svr->client ipc ", System.nanoTime() - eventTimeNano);
5809 }
5810
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005811 target.mClient.dispatchPointer(ev, eventTime, true);
Michael Chan53071d62009-05-13 17:29:48 -07005812
5813 if (MEASURE_LATENCY) {
5814 lt.sample("7 after svr->client ipc ", System.nanoTime() - eventTimeNano);
5815 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07005816 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005817 } catch (android.os.RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005818 Slog.i(TAG, "WINDOW DIED during motion dispatch: " + target);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005819 mKeyWaiter.mMotionTarget = null;
5820 try {
5821 removeWindow(target.mSession, target.mClient);
5822 } catch (java.util.NoSuchElementException ex) {
5823 // This will happen if the window has already been
5824 // removed.
5825 }
5826 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07005827 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005828 }
Romain Guy06882f82009-06-10 13:36:04 -07005829
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005830 /**
5831 * @return Returns true if event was dispatched, false if it was dropped for any reason
5832 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07005833 private int dispatchTrackball(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005834 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005835 TAG, "dispatchTrackball [" + ev.getAction() +"] <" + ev.getX() + ", " + ev.getY() + ">");
Romain Guy06882f82009-06-10 13:36:04 -07005836
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005837 Object focusObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005838 ev, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005839 if (focusObj == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005840 Slog.w(TAG, "No focus window, dropping trackball: " + ev);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005841 if (qev != null) {
5842 mQueue.recycleEvent(qev);
5843 }
5844 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005845 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005846 }
5847 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
5848 if (qev != null) {
5849 mQueue.recycleEvent(qev);
5850 }
5851 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005852 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005853 }
Romain Guy06882f82009-06-10 13:36:04 -07005854
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005855 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07005856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005857 if (uid != 0 && uid != focus.mSession.mUid) {
5858 if (mContext.checkPermission(
5859 android.Manifest.permission.INJECT_EVENTS, pid, uid)
5860 != PackageManager.PERMISSION_GRANTED) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005861 Slog.w(TAG, "Permission denied: injecting key event from pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005862 + pid + " uid " + uid + " to window " + focus
5863 + " owned by uid " + focus.mSession.mUid);
5864 if (qev != null) {
5865 mQueue.recycleEvent(qev);
5866 }
5867 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005868 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005869 }
5870 }
Romain Guy06882f82009-06-10 13:36:04 -07005871
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005872 final long eventTime = ev.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07005873
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005874 synchronized(mWindowMap) {
5875 if (qev != null && ev.getAction() == MotionEvent.ACTION_MOVE) {
5876 mKeyWaiter.bindTargetWindowLocked(focus,
5877 KeyWaiter.RETURN_PENDING_TRACKBALL, qev);
5878 // We don't deliver movement events to the client, we hold
5879 // them and wait for them to call back.
5880 ev = null;
5881 } else {
5882 mKeyWaiter.bindTargetWindowLocked(focus);
5883 }
5884 }
Romain Guy06882f82009-06-10 13:36:04 -07005885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005886 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005887 focus.mClient.dispatchTrackball(ev, eventTime, true);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005888 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005889 } catch (android.os.RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005890 Slog.i(TAG, "WINDOW DIED during key dispatch: " + focus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005891 try {
5892 removeWindow(focus.mSession, focus.mClient);
5893 } catch (java.util.NoSuchElementException ex) {
5894 // This will happen if the window has already been
5895 // removed.
5896 }
5897 }
Romain Guy06882f82009-06-10 13:36:04 -07005898
Dianne Hackborncfaef692009-06-15 14:24:44 -07005899 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005900 }
Romain Guy06882f82009-06-10 13:36:04 -07005901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005902 /**
5903 * @return Returns true if event was dispatched, false if it was dropped for any reason
5904 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07005905 private int dispatchKey(KeyEvent event, int pid, int uid) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005906 if (DEBUG_INPUT) Slog.v(TAG, "Dispatch key: " + event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005907
5908 Object focusObj = mKeyWaiter.waitForNextEventTarget(event, null,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005909 null, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005910 if (focusObj == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005911 Slog.w(TAG, "No focus window, dropping: " + event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005912 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005913 }
5914 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005915 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005916 }
Romain Guy06882f82009-06-10 13:36:04 -07005917
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005918 // Okay we have finished waiting for the last event to be processed.
5919 // First off, if this is a repeat event, check to see if there is
5920 // a corresponding up event in the queue. If there is, we will
5921 // just drop the repeat, because it makes no sense to repeat after
5922 // the user has released a key. (This is especially important for
5923 // long presses.)
5924 if (event.getRepeatCount() > 0 && mQueue.hasKeyUpEvent(event)) {
5925 return INJECT_SUCCEEDED;
5926 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005927
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005928 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07005929
Joe Onorato8a9b2202010-02-26 18:56:32 -08005930 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005931 TAG, "Dispatching to " + focus + ": " + event);
5932
5933 if (uid != 0 && uid != focus.mSession.mUid) {
5934 if (mContext.checkPermission(
5935 android.Manifest.permission.INJECT_EVENTS, pid, uid)
5936 != PackageManager.PERMISSION_GRANTED) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005937 Slog.w(TAG, "Permission denied: injecting key event from pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005938 + pid + " uid " + uid + " to window " + focus
5939 + " owned by uid " + focus.mSession.mUid);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005940 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005941 }
5942 }
Romain Guy06882f82009-06-10 13:36:04 -07005943
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005944 synchronized(mWindowMap) {
5945 mKeyWaiter.bindTargetWindowLocked(focus);
5946 }
5947
5948 // NOSHIP extra state logging
5949 mKeyWaiter.recordDispatchState(event, focus);
5950 // END NOSHIP
Romain Guy06882f82009-06-10 13:36:04 -07005951
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005952 try {
5953 if (DEBUG_INPUT || DEBUG_FOCUS) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005954 Slog.v(TAG, "Delivering key " + event.getKeyCode()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005955 + " to " + focus);
5956 }
5957 focus.mClient.dispatchKey(event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005958 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005959 } catch (android.os.RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005960 Slog.i(TAG, "WINDOW DIED during key dispatch: " + focus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005961 try {
5962 removeWindow(focus.mSession, focus.mClient);
5963 } catch (java.util.NoSuchElementException ex) {
5964 // This will happen if the window has already been
5965 // removed.
5966 }
5967 }
Romain Guy06882f82009-06-10 13:36:04 -07005968
Dianne Hackborncfaef692009-06-15 14:24:44 -07005969 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005970 }
Romain Guy06882f82009-06-10 13:36:04 -07005971
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005972 public void pauseKeyDispatching(IBinder _token) {
5973 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5974 "pauseKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005975 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005976 }
5977
5978 synchronized (mWindowMap) {
5979 WindowToken token = mTokenMap.get(_token);
5980 if (token != null) {
5981 mKeyWaiter.pauseDispatchingLocked(token);
5982 }
5983 }
5984 }
5985
5986 public void resumeKeyDispatching(IBinder _token) {
5987 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5988 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005989 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005990 }
5991
5992 synchronized (mWindowMap) {
5993 WindowToken token = mTokenMap.get(_token);
5994 if (token != null) {
5995 mKeyWaiter.resumeDispatchingLocked(token);
5996 }
5997 }
5998 }
5999
6000 public void setEventDispatching(boolean enabled) {
6001 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
6002 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07006003 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006004 }
6005
6006 synchronized (mWindowMap) {
6007 mKeyWaiter.setEventDispatchingLocked(enabled);
6008 }
6009 }
Romain Guy06882f82009-06-10 13:36:04 -07006010
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006011 /**
6012 * Injects a keystroke event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07006013 *
6014 * @param ev A motion event describing the keystroke action. (Be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006015 * {@link SystemClock#uptimeMillis()} as the timebase.)
6016 * @param sync If true, wait for the event to be completed before returning to the caller.
6017 * @return Returns true if event was dispatched, false if it was dropped for any reason
6018 */
6019 public boolean injectKeyEvent(KeyEvent ev, boolean sync) {
6020 long downTime = ev.getDownTime();
6021 long eventTime = ev.getEventTime();
6022
6023 int action = ev.getAction();
6024 int code = ev.getKeyCode();
6025 int repeatCount = ev.getRepeatCount();
6026 int metaState = ev.getMetaState();
6027 int deviceId = ev.getDeviceId();
6028 int scancode = ev.getScanCode();
6029
6030 if (eventTime == 0) eventTime = SystemClock.uptimeMillis();
6031 if (downTime == 0) downTime = eventTime;
6032
6033 KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
The Android Open Source Project10592532009-03-18 17:39:46 -07006034 deviceId, scancode, KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006035
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07006036 final int pid = Binder.getCallingPid();
6037 final int uid = Binder.getCallingUid();
6038 final long ident = Binder.clearCallingIdentity();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07006039
6040 final int result;
6041 if (ENABLE_NATIVE_INPUT_DISPATCH) {
Jeff Brown7fbdc842010-06-17 20:52:56 -07006042 result = mInputManager.injectKeyEvent(newEvent, InputQueue.INPUT_EVENT_NATURE_KEY,
6043 pid, uid, sync, INJECTION_TIMEOUT_MILLIS);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07006044 } else {
6045 result = dispatchKey(newEvent, pid, uid);
6046 if (sync) {
6047 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
6048 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006049 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07006050
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07006051 Binder.restoreCallingIdentity(ident);
Jeff Brown7fbdc842010-06-17 20:52:56 -07006052 return reportInjectionResult(result);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006053 }
6054
6055 /**
6056 * Inject a pointer (touch) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07006057 *
6058 * @param ev A motion event describing the pointer (touch) action. (As noted in
6059 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006060 * {@link SystemClock#uptimeMillis()} as the timebase.)
6061 * @param sync If true, wait for the event to be completed before returning to the caller.
6062 * @return Returns true if event was dispatched, false if it was dropped for any reason
6063 */
6064 public boolean injectPointerEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07006065 final int pid = Binder.getCallingPid();
6066 final int uid = Binder.getCallingUid();
6067 final long ident = Binder.clearCallingIdentity();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07006068
6069 final int result;
6070 if (ENABLE_NATIVE_INPUT_DISPATCH) {
Jeff Brown7fbdc842010-06-17 20:52:56 -07006071 result = mInputManager.injectMotionEvent(ev, InputQueue.INPUT_EVENT_NATURE_TOUCH,
6072 pid, uid, sync, INJECTION_TIMEOUT_MILLIS);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07006073 } else {
6074 result = dispatchPointer(null, ev, pid, uid);
6075 if (sync) {
6076 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
6077 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006078 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07006079
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07006080 Binder.restoreCallingIdentity(ident);
Jeff Brown7fbdc842010-06-17 20:52:56 -07006081 return reportInjectionResult(result);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006082 }
Romain Guy06882f82009-06-10 13:36:04 -07006083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006084 /**
6085 * Inject a trackball (navigation device) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07006086 *
6087 * @param ev A motion event describing the trackball action. (As noted in
6088 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006089 * {@link SystemClock#uptimeMillis()} as the timebase.)
6090 * @param sync If true, wait for the event to be completed before returning to the caller.
6091 * @return Returns true if event was dispatched, false if it was dropped for any reason
6092 */
6093 public boolean injectTrackballEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07006094 final int pid = Binder.getCallingPid();
6095 final int uid = Binder.getCallingUid();
6096 final long ident = Binder.clearCallingIdentity();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07006097
6098 final int result;
6099 if (ENABLE_NATIVE_INPUT_DISPATCH) {
Jeff Brown7fbdc842010-06-17 20:52:56 -07006100 result = mInputManager.injectMotionEvent(ev, InputQueue.INPUT_EVENT_NATURE_TRACKBALL,
6101 pid, uid, sync, INJECTION_TIMEOUT_MILLIS);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07006102 } else {
6103 result = dispatchTrackball(null, ev, pid, uid);
6104 if (sync) {
6105 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
6106 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006107 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07006108
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07006109 Binder.restoreCallingIdentity(ident);
Jeff Brown7fbdc842010-06-17 20:52:56 -07006110 return reportInjectionResult(result);
6111 }
6112
6113 private boolean reportInjectionResult(int result) {
6114 if (ENABLE_NATIVE_INPUT_DISPATCH) {
6115 switch (result) {
6116 case InputManager.INPUT_EVENT_INJECTION_PERMISSION_DENIED:
6117 Slog.w(TAG, "Input event injection permission denied.");
6118 throw new SecurityException(
6119 "Injecting to another application requires INJECT_EVENTS permission");
6120 case InputManager.INPUT_EVENT_INJECTION_SUCCEEDED:
6121 Slog.v(TAG, "Input event injection succeeded.");
6122 return true;
6123 case InputManager.INPUT_EVENT_INJECTION_TIMED_OUT:
6124 Slog.w(TAG, "Input event injection timed out.");
6125 return false;
6126 case InputManager.INPUT_EVENT_INJECTION_FAILED:
6127 default:
6128 Slog.w(TAG, "Input event injection failed.");
6129 return false;
6130 }
6131 } else {
6132 switch (result) {
6133 case INJECT_NO_PERMISSION:
6134 throw new SecurityException(
6135 "Injecting to another application requires INJECT_EVENTS permission");
6136 case INJECT_SUCCEEDED:
6137 return true;
6138 }
6139 return false;
Dianne Hackborncfaef692009-06-15 14:24:44 -07006140 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006141 }
Romain Guy06882f82009-06-10 13:36:04 -07006142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006143 private WindowState getFocusedWindow() {
6144 synchronized (mWindowMap) {
6145 return getFocusedWindowLocked();
6146 }
6147 }
6148
6149 private WindowState getFocusedWindowLocked() {
6150 return mCurrentFocus;
6151 }
Romain Guy06882f82009-06-10 13:36:04 -07006152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006153 /**
6154 * This class holds the state for dispatching key events. This state
6155 * is protected by the KeyWaiter instance, NOT by the window lock. You
6156 * can be holding the main window lock while acquire the KeyWaiter lock,
6157 * but not the other way around.
6158 */
6159 final class KeyWaiter {
6160 // NOSHIP debugging
6161 public class DispatchState {
6162 private KeyEvent event;
6163 private WindowState focus;
6164 private long time;
6165 private WindowState lastWin;
6166 private IBinder lastBinder;
6167 private boolean finished;
6168 private boolean gotFirstWindow;
6169 private boolean eventDispatching;
6170 private long timeToSwitch;
6171 private boolean wasFrozen;
6172 private boolean focusPaused;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006173 private WindowState curFocus;
Romain Guy06882f82009-06-10 13:36:04 -07006174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006175 DispatchState(KeyEvent theEvent, WindowState theFocus) {
6176 focus = theFocus;
6177 event = theEvent;
6178 time = System.currentTimeMillis();
6179 // snapshot KeyWaiter state
6180 lastWin = mLastWin;
6181 lastBinder = mLastBinder;
6182 finished = mFinished;
6183 gotFirstWindow = mGotFirstWindow;
6184 eventDispatching = mEventDispatching;
6185 timeToSwitch = mTimeToSwitch;
6186 wasFrozen = mWasFrozen;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006187 curFocus = mCurrentFocus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006188 // cache the paused state at ctor time as well
6189 if (theFocus == null || theFocus.mToken == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006190 focusPaused = false;
6191 } else {
6192 focusPaused = theFocus.mToken.paused;
6193 }
6194 }
Romain Guy06882f82009-06-10 13:36:04 -07006195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006196 public String toString() {
6197 return "{{" + event + " to " + focus + " @ " + time
6198 + " lw=" + lastWin + " lb=" + lastBinder
6199 + " fin=" + finished + " gfw=" + gotFirstWindow
6200 + " ed=" + eventDispatching + " tts=" + timeToSwitch
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006201 + " wf=" + wasFrozen + " fp=" + focusPaused
Christopher Tate46d45252010-02-09 15:48:57 -08006202 + " mcf=" + curFocus + "}}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006203 }
6204 };
6205 private DispatchState mDispatchState = null;
6206 public void recordDispatchState(KeyEvent theEvent, WindowState theFocus) {
6207 mDispatchState = new DispatchState(theEvent, theFocus);
6208 }
6209 // END NOSHIP
6210
6211 public static final int RETURN_NOTHING = 0;
6212 public static final int RETURN_PENDING_POINTER = 1;
6213 public static final int RETURN_PENDING_TRACKBALL = 2;
Romain Guy06882f82009-06-10 13:36:04 -07006214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006215 final Object SKIP_TARGET_TOKEN = new Object();
6216 final Object CONSUMED_EVENT_TOKEN = new Object();
Romain Guy06882f82009-06-10 13:36:04 -07006217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006218 private WindowState mLastWin = null;
6219 private IBinder mLastBinder = null;
6220 private boolean mFinished = true;
6221 private boolean mGotFirstWindow = false;
6222 private boolean mEventDispatching = true;
6223 private long mTimeToSwitch = 0;
6224 /* package */ boolean mWasFrozen = false;
Romain Guy06882f82009-06-10 13:36:04 -07006225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006226 // Target of Motion events
6227 WindowState mMotionTarget;
Romain Guy06882f82009-06-10 13:36:04 -07006228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006229 // Windows above the target who would like to receive an "outside"
6230 // touch event for any down events outside of them.
6231 WindowState mOutsideTouchTargets;
6232
6233 /**
6234 * Wait for the last event dispatch to complete, then find the next
6235 * target that should receive the given event and wait for that one
6236 * to be ready to receive it.
6237 */
6238 Object waitForNextEventTarget(KeyEvent nextKey, QueuedEvent qev,
6239 MotionEvent nextMotion, boolean isPointerEvent,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07006240 boolean failIfTimeout, int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006241 long startTime = SystemClock.uptimeMillis();
6242 long keyDispatchingTimeout = 5 * 1000;
6243 long waitedFor = 0;
6244
6245 while (true) {
6246 // Figure out which window we care about. It is either the
6247 // last window we are waiting to have process the event or,
6248 // if none, then the next window we think the event should go
6249 // to. Note: we retrieve mLastWin outside of the lock, so
6250 // it may change before we lock. Thus we must check it again.
6251 WindowState targetWin = mLastWin;
6252 boolean targetIsNew = targetWin == null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08006253 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006254 TAG, "waitForLastKey: mFinished=" + mFinished +
6255 ", mLastWin=" + mLastWin);
6256 if (targetIsNew) {
6257 Object target = findTargetWindow(nextKey, qev, nextMotion,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07006258 isPointerEvent, callingPid, callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006259 if (target == SKIP_TARGET_TOKEN) {
6260 // The user has pressed a special key, and we are
6261 // dropping all pending events before it.
Joe Onorato8a9b2202010-02-26 18:56:32 -08006262 if (DEBUG_INPUT) Slog.v(TAG, "Skipping: " + nextKey
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006263 + " " + nextMotion);
6264 return null;
6265 }
6266 if (target == CONSUMED_EVENT_TOKEN) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006267 if (DEBUG_INPUT) Slog.v(TAG, "Consumed: " + nextKey
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006268 + " " + nextMotion);
6269 return target;
6270 }
6271 targetWin = (WindowState)target;
6272 }
Romain Guy06882f82009-06-10 13:36:04 -07006273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006274 AppWindowToken targetApp = null;
Romain Guy06882f82009-06-10 13:36:04 -07006275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006276 // Now: is it okay to send the next event to this window?
6277 synchronized (this) {
6278 // First: did we come here based on the last window not
6279 // being null, but it changed by the time we got here?
6280 // If so, try again.
6281 if (!targetIsNew && mLastWin == null) {
6282 continue;
6283 }
Romain Guy06882f82009-06-10 13:36:04 -07006284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006285 // We never dispatch events if not finished with the
6286 // last one, or the display is frozen.
6287 if (mFinished && !mDisplayFrozen) {
6288 // If event dispatching is disabled, then we
6289 // just consume the events.
6290 if (!mEventDispatching) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006291 if (DEBUG_INPUT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006292 "Skipping event; dispatching disabled: "
6293 + nextKey + " " + nextMotion);
6294 return null;
6295 }
6296 if (targetWin != null) {
6297 // If this is a new target, and that target is not
6298 // paused or unresponsive, then all looks good to
6299 // handle the event.
6300 if (targetIsNew && !targetWin.mToken.paused) {
6301 return targetWin;
6302 }
Romain Guy06882f82009-06-10 13:36:04 -07006303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006304 // If we didn't find a target window, and there is no
6305 // focused app window, then just eat the events.
6306 } else if (mFocusedApp == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006307 if (DEBUG_INPUT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006308 "Skipping event; no focused app: "
6309 + nextKey + " " + nextMotion);
6310 return null;
6311 }
6312 }
Romain Guy06882f82009-06-10 13:36:04 -07006313
Joe Onorato8a9b2202010-02-26 18:56:32 -08006314 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006315 TAG, "Waiting for last key in " + mLastBinder
6316 + " target=" + targetWin
6317 + " mFinished=" + mFinished
6318 + " mDisplayFrozen=" + mDisplayFrozen
6319 + " targetIsNew=" + targetIsNew
6320 + " paused="
6321 + (targetWin != null ? targetWin.mToken.paused : false)
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006322 + " mFocusedApp=" + mFocusedApp
6323 + " mCurrentFocus=" + mCurrentFocus);
Romain Guy06882f82009-06-10 13:36:04 -07006324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006325 targetApp = targetWin != null
6326 ? targetWin.mAppToken : mFocusedApp;
Romain Guy06882f82009-06-10 13:36:04 -07006327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006328 long curTimeout = keyDispatchingTimeout;
6329 if (mTimeToSwitch != 0) {
6330 long now = SystemClock.uptimeMillis();
6331 if (mTimeToSwitch <= now) {
6332 // If an app switch key has been pressed, and we have
6333 // waited too long for the current app to finish
6334 // processing keys, then wait no more!
Christopher Tate136b1f92010-02-11 17:51:24 -08006335 doFinishedKeyLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006336 continue;
6337 }
6338 long switchTimeout = mTimeToSwitch - now;
6339 if (curTimeout > switchTimeout) {
6340 curTimeout = switchTimeout;
6341 }
6342 }
Romain Guy06882f82009-06-10 13:36:04 -07006343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006344 try {
6345 // after that continue
6346 // processing keys, so we don't get stuck.
Joe Onorato8a9b2202010-02-26 18:56:32 -08006347 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006348 TAG, "Waiting for key dispatch: " + curTimeout);
6349 wait(curTimeout);
Joe Onorato8a9b2202010-02-26 18:56:32 -08006350 if (DEBUG_INPUT) Slog.v(TAG, "Finished waiting @"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006351 + SystemClock.uptimeMillis() + " startTime="
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006352 + startTime + " switchTime=" + mTimeToSwitch
6353 + " target=" + targetWin + " mLW=" + mLastWin
6354 + " mLB=" + mLastBinder + " fin=" + mFinished
6355 + " mCurrentFocus=" + mCurrentFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006356 } catch (InterruptedException e) {
6357 }
6358 }
6359
6360 // If we were frozen during configuration change, restart the
6361 // timeout checks from now; otherwise look at whether we timed
6362 // out before awakening.
6363 if (mWasFrozen) {
6364 waitedFor = 0;
6365 mWasFrozen = false;
6366 } else {
6367 waitedFor = SystemClock.uptimeMillis() - startTime;
6368 }
6369
6370 if (waitedFor >= keyDispatchingTimeout && mTimeToSwitch == 0) {
6371 IApplicationToken at = null;
6372 synchronized (this) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006373 Slog.w(TAG, "Key dispatching timed out sending to " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006374 (targetWin != null ? targetWin.mAttrs.getTitle()
Ken Shirriff8200b202010-02-04 13:34:37 -08006375 : "<null>: no window ready for key dispatch"));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006376 // NOSHIP debugging
Joe Onorato8a9b2202010-02-26 18:56:32 -08006377 Slog.w(TAG, "Previous dispatch state: " + mDispatchState);
6378 Slog.w(TAG, "Current dispatch state: " +
Ken Shirriff8200b202010-02-04 13:34:37 -08006379 new DispatchState(nextKey, targetWin));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006380 // END NOSHIP
6381 //dump();
6382 if (targetWin != null) {
6383 at = targetWin.getAppToken();
6384 } else if (targetApp != null) {
6385 at = targetApp.appToken;
6386 }
6387 }
6388
6389 boolean abort = true;
6390 if (at != null) {
6391 try {
6392 long timeout = at.getKeyDispatchingTimeout();
6393 if (timeout > waitedFor) {
6394 // we did not wait the proper amount of time for this application.
6395 // set the timeout to be the real timeout and wait again.
6396 keyDispatchingTimeout = timeout - waitedFor;
6397 continue;
6398 } else {
6399 abort = at.keyDispatchingTimedOut();
6400 }
6401 } catch (RemoteException ex) {
6402 }
6403 }
6404
6405 synchronized (this) {
6406 if (abort && (mLastWin == targetWin || targetWin == null)) {
6407 mFinished = true;
Romain Guy06882f82009-06-10 13:36:04 -07006408 if (mLastWin != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006409 if (DEBUG_INPUT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006410 "Window " + mLastWin +
6411 " timed out on key input");
6412 if (mLastWin.mToken.paused) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006413 Slog.w(TAG, "Un-pausing dispatching to this window");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006414 mLastWin.mToken.paused = false;
6415 }
6416 }
6417 if (mMotionTarget == targetWin) {
6418 mMotionTarget = null;
6419 }
6420 mLastWin = null;
6421 mLastBinder = null;
6422 if (failIfTimeout || targetWin == null) {
6423 return null;
6424 }
6425 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006426 Slog.w(TAG, "Continuing to wait for key to be dispatched");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006427 startTime = SystemClock.uptimeMillis();
6428 }
6429 }
6430 }
6431 }
6432 }
Romain Guy06882f82009-06-10 13:36:04 -07006433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006434 Object findTargetWindow(KeyEvent nextKey, QueuedEvent qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07006435 MotionEvent nextMotion, boolean isPointerEvent,
6436 int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006437 mOutsideTouchTargets = null;
Romain Guy06882f82009-06-10 13:36:04 -07006438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006439 if (nextKey != null) {
6440 // Find the target window for a normal key event.
6441 final int keycode = nextKey.getKeyCode();
6442 final int repeatCount = nextKey.getRepeatCount();
6443 final boolean down = nextKey.getAction() != KeyEvent.ACTION_UP;
6444 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(keycode);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006446 if (!dispatch) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07006447 if (callingUid == 0 ||
6448 mContext.checkPermission(
6449 android.Manifest.permission.INJECT_EVENTS,
6450 callingPid, callingUid)
6451 == PackageManager.PERMISSION_GRANTED) {
6452 mPolicy.interceptKeyTi(null, keycode,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07006453 nextKey.getMetaState(), down, repeatCount,
6454 nextKey.getFlags());
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07006455 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08006456 Slog.w(TAG, "Event timeout during app switch: dropping "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006457 + nextKey);
6458 return SKIP_TARGET_TOKEN;
6459 }
Romain Guy06882f82009-06-10 13:36:04 -07006460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006461 // System.out.println("##### [" + SystemClock.uptimeMillis() + "] WindowManagerService.dispatchKey(" + keycode + ", " + down + ", " + repeatCount + ")");
Romain Guy06882f82009-06-10 13:36:04 -07006462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006463 WindowState focus = null;
6464 synchronized(mWindowMap) {
6465 focus = getFocusedWindowLocked();
6466 }
Romain Guy06882f82009-06-10 13:36:04 -07006467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006468 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
Romain Guy06882f82009-06-10 13:36:04 -07006469
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07006470 if (callingUid == 0 ||
6471 (focus != null && callingUid == focus.mSession.mUid) ||
6472 mContext.checkPermission(
6473 android.Manifest.permission.INJECT_EVENTS,
6474 callingPid, callingUid)
6475 == PackageManager.PERMISSION_GRANTED) {
6476 if (mPolicy.interceptKeyTi(focus,
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006477 keycode, nextKey.getMetaState(), down, repeatCount,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07006478 nextKey.getFlags())) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07006479 return CONSUMED_EVENT_TOKEN;
6480 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006481 }
Romain Guy06882f82009-06-10 13:36:04 -07006482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006483 return focus;
Romain Guy06882f82009-06-10 13:36:04 -07006484
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006485 } else if (!isPointerEvent) {
6486 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(-1);
6487 if (!dispatch) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006488 Slog.w(TAG, "Event timeout during app switch: dropping trackball "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006489 + nextMotion);
6490 return SKIP_TARGET_TOKEN;
6491 }
Romain Guy06882f82009-06-10 13:36:04 -07006492
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006493 WindowState focus = null;
6494 synchronized(mWindowMap) {
6495 focus = getFocusedWindowLocked();
6496 }
Romain Guy06882f82009-06-10 13:36:04 -07006497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006498 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
6499 return focus;
6500 }
Romain Guy06882f82009-06-10 13:36:04 -07006501
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006502 if (nextMotion == null) {
6503 return SKIP_TARGET_TOKEN;
6504 }
Romain Guy06882f82009-06-10 13:36:04 -07006505
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006506 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(
6507 KeyEvent.KEYCODE_UNKNOWN);
6508 if (!dispatch) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006509 Slog.w(TAG, "Event timeout during app switch: dropping pointer "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006510 + nextMotion);
6511 return SKIP_TARGET_TOKEN;
6512 }
Romain Guy06882f82009-06-10 13:36:04 -07006513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006514 // Find the target window for a pointer event.
6515 int action = nextMotion.getAction();
6516 final float xf = nextMotion.getX();
6517 final float yf = nextMotion.getY();
6518 final long eventTime = nextMotion.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07006519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006520 final boolean screenWasOff = qev != null
6521 && (qev.flags&WindowManagerPolicy.FLAG_BRIGHT_HERE) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07006522
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006523 WindowState target = null;
Romain Guy06882f82009-06-10 13:36:04 -07006524
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006525 synchronized(mWindowMap) {
6526 synchronized (this) {
6527 if (action == MotionEvent.ACTION_DOWN) {
6528 if (mMotionTarget != null) {
6529 // this is weird, we got a pen down, but we thought it was
6530 // already down!
6531 // XXX: We should probably send an ACTION_UP to the current
6532 // target.
Joe Onorato8a9b2202010-02-26 18:56:32 -08006533 Slog.w(TAG, "Pointer down received while already down in: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006534 + mMotionTarget);
6535 mMotionTarget = null;
6536 }
Romain Guy06882f82009-06-10 13:36:04 -07006537
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006538 // ACTION_DOWN is special, because we need to lock next events to
6539 // the window we'll land onto.
6540 final int x = (int)xf;
6541 final int y = (int)yf;
Romain Guy06882f82009-06-10 13:36:04 -07006542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006543 final ArrayList windows = mWindows;
6544 final int N = windows.size();
6545 WindowState topErrWindow = null;
6546 final Rect tmpRect = mTempRect;
6547 for (int i=N-1; i>=0; i--) {
6548 WindowState child = (WindowState)windows.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08006549 //Slog.i(TAG, "Checking dispatch to: " + child);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006550 final int flags = child.mAttrs.flags;
6551 if ((flags & WindowManager.LayoutParams.FLAG_SYSTEM_ERROR) != 0) {
6552 if (topErrWindow == null) {
6553 topErrWindow = child;
6554 }
6555 }
6556 if (!child.isVisibleLw()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006557 //Slog.i(TAG, "Not visible!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006558 continue;
6559 }
6560 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006561 //Slog.i(TAG, "Not touchable!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006562 if ((flags & WindowManager.LayoutParams
6563 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
6564 child.mNextOutsideTouch = mOutsideTouchTargets;
6565 mOutsideTouchTargets = child;
6566 }
6567 continue;
6568 }
6569 tmpRect.set(child.mFrame);
6570 if (child.mTouchableInsets == ViewTreeObserver
6571 .InternalInsetsInfo.TOUCHABLE_INSETS_CONTENT) {
6572 // The touch is inside of the window if it is
6573 // inside the frame, AND the content part of that
6574 // frame that was given by the application.
6575 tmpRect.left += child.mGivenContentInsets.left;
6576 tmpRect.top += child.mGivenContentInsets.top;
6577 tmpRect.right -= child.mGivenContentInsets.right;
6578 tmpRect.bottom -= child.mGivenContentInsets.bottom;
6579 } else if (child.mTouchableInsets == ViewTreeObserver
6580 .InternalInsetsInfo.TOUCHABLE_INSETS_VISIBLE) {
6581 // The touch is inside of the window if it is
6582 // inside the frame, AND the visible part of that
6583 // frame that was given by the application.
6584 tmpRect.left += child.mGivenVisibleInsets.left;
6585 tmpRect.top += child.mGivenVisibleInsets.top;
6586 tmpRect.right -= child.mGivenVisibleInsets.right;
6587 tmpRect.bottom -= child.mGivenVisibleInsets.bottom;
6588 }
6589 final int touchFlags = flags &
6590 (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
6591 |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
6592 if (tmpRect.contains(x, y) || touchFlags == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006593 //Slog.i(TAG, "Using this target!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006594 if (!screenWasOff || (flags &
6595 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING) != 0) {
6596 mMotionTarget = child;
6597 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006598 //Slog.i(TAG, "Waking, skip!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006599 mMotionTarget = null;
6600 }
6601 break;
6602 }
Romain Guy06882f82009-06-10 13:36:04 -07006603
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006604 if ((flags & WindowManager.LayoutParams
6605 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
6606 child.mNextOutsideTouch = mOutsideTouchTargets;
6607 mOutsideTouchTargets = child;
Joe Onorato8a9b2202010-02-26 18:56:32 -08006608 //Slog.i(TAG, "Adding to outside target list: " + child);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006609 }
6610 }
6611
6612 // if there's an error window but it's not accepting
6613 // focus (typically because it is not yet visible) just
6614 // wait for it -- any other focused window may in fact
6615 // be in ANR state.
6616 if (topErrWindow != null && mMotionTarget != topErrWindow) {
6617 mMotionTarget = null;
6618 }
6619 }
Romain Guy06882f82009-06-10 13:36:04 -07006620
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006621 target = mMotionTarget;
6622 }
6623 }
Romain Guy06882f82009-06-10 13:36:04 -07006624
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006625 wakeupIfNeeded(target, eventType(nextMotion));
Romain Guy06882f82009-06-10 13:36:04 -07006626
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006627 // Pointer events are a little different -- if there isn't a
6628 // target found for any event, then just drop it.
6629 return target != null ? target : SKIP_TARGET_TOKEN;
6630 }
Romain Guy06882f82009-06-10 13:36:04 -07006631
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006632 boolean checkShouldDispatchKey(int keycode) {
6633 synchronized (this) {
6634 if (mPolicy.isAppSwitchKeyTqTiLwLi(keycode)) {
6635 mTimeToSwitch = 0;
6636 return true;
6637 }
6638 if (mTimeToSwitch != 0
6639 && mTimeToSwitch < SystemClock.uptimeMillis()) {
6640 return false;
6641 }
6642 return true;
6643 }
6644 }
Romain Guy06882f82009-06-10 13:36:04 -07006645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006646 void bindTargetWindowLocked(WindowState win,
6647 int pendingWhat, QueuedEvent pendingMotion) {
6648 synchronized (this) {
6649 bindTargetWindowLockedLocked(win, pendingWhat, pendingMotion);
6650 }
6651 }
Romain Guy06882f82009-06-10 13:36:04 -07006652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006653 void bindTargetWindowLocked(WindowState win) {
6654 synchronized (this) {
6655 bindTargetWindowLockedLocked(win, RETURN_NOTHING, null);
6656 }
6657 }
6658
6659 void bindTargetWindowLockedLocked(WindowState win,
6660 int pendingWhat, QueuedEvent pendingMotion) {
6661 mLastWin = win;
6662 mLastBinder = win.mClient.asBinder();
6663 mFinished = false;
6664 if (pendingMotion != null) {
6665 final Session s = win.mSession;
6666 if (pendingWhat == RETURN_PENDING_POINTER) {
6667 releasePendingPointerLocked(s);
6668 s.mPendingPointerMove = pendingMotion;
6669 s.mPendingPointerWindow = win;
Joe Onorato8a9b2202010-02-26 18:56:32 -08006670 if (DEBUG_INPUT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006671 "bindTargetToWindow " + s.mPendingPointerMove);
6672 } else if (pendingWhat == RETURN_PENDING_TRACKBALL) {
6673 releasePendingTrackballLocked(s);
6674 s.mPendingTrackballMove = pendingMotion;
6675 s.mPendingTrackballWindow = win;
6676 }
6677 }
6678 }
Romain Guy06882f82009-06-10 13:36:04 -07006679
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006680 void releasePendingPointerLocked(Session s) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006681 if (DEBUG_INPUT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006682 "releasePendingPointer " + s.mPendingPointerMove);
6683 if (s.mPendingPointerMove != null) {
6684 mQueue.recycleEvent(s.mPendingPointerMove);
6685 s.mPendingPointerMove = null;
6686 }
6687 }
Romain Guy06882f82009-06-10 13:36:04 -07006688
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006689 void releasePendingTrackballLocked(Session s) {
6690 if (s.mPendingTrackballMove != null) {
6691 mQueue.recycleEvent(s.mPendingTrackballMove);
6692 s.mPendingTrackballMove = null;
6693 }
6694 }
Romain Guy06882f82009-06-10 13:36:04 -07006695
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006696 MotionEvent finishedKey(Session session, IWindow client, boolean force,
6697 int returnWhat) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006698 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006699 TAG, "finishedKey: client=" + client + ", force=" + force);
6700
6701 if (client == null) {
6702 return null;
6703 }
6704
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07006705 MotionEvent res = null;
6706 QueuedEvent qev = null;
6707 WindowState win = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006708
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006709 synchronized (this) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006710 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006711 TAG, "finishedKey: client=" + client.asBinder()
6712 + ", force=" + force + ", last=" + mLastBinder
6713 + " (token=" + (mLastWin != null ? mLastWin.mToken : null) + ")");
6714
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006715 if (returnWhat == RETURN_PENDING_POINTER) {
6716 qev = session.mPendingPointerMove;
6717 win = session.mPendingPointerWindow;
6718 session.mPendingPointerMove = null;
6719 session.mPendingPointerWindow = null;
6720 } else if (returnWhat == RETURN_PENDING_TRACKBALL) {
6721 qev = session.mPendingTrackballMove;
6722 win = session.mPendingTrackballWindow;
6723 session.mPendingTrackballMove = null;
6724 session.mPendingTrackballWindow = null;
6725 }
Romain Guy06882f82009-06-10 13:36:04 -07006726
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006727 if (mLastBinder == client.asBinder()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006728 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006729 TAG, "finishedKey: last paused="
6730 + ((mLastWin != null) ? mLastWin.mToken.paused : "null"));
6731 if (mLastWin != null && (!mLastWin.mToken.paused || force
6732 || !mEventDispatching)) {
Christopher Tate136b1f92010-02-11 17:51:24 -08006733 doFinishedKeyLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006734 } else {
6735 // Make sure to wake up anyone currently waiting to
6736 // dispatch a key, so they can re-evaluate their
6737 // current situation.
6738 mFinished = true;
6739 notifyAll();
6740 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006741 }
Romain Guy06882f82009-06-10 13:36:04 -07006742
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006743 if (qev != null) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07006744 res = (MotionEvent)qev.event;
Joe Onorato8a9b2202010-02-26 18:56:32 -08006745 if (DEBUG_INPUT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006746 "Returning pending motion: " + res);
6747 mQueue.recycleEvent(qev);
6748 if (win != null && returnWhat == RETURN_PENDING_POINTER) {
6749 res.offsetLocation(-win.mFrame.left, -win.mFrame.top);
6750 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006751 }
Christopher Tate2624fbc2009-12-11 12:11:31 -08006752 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006753
Christopher Tate2624fbc2009-12-11 12:11:31 -08006754 if (res != null && returnWhat == RETURN_PENDING_POINTER) {
6755 synchronized (mWindowMap) {
Dianne Hackborn90d2db32010-02-11 22:19:06 -08006756 dispatchPointerElsewhereLocked(win, win, res, res.getEventTime(), false);
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07006757 }
6758 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006759
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07006760 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006761 }
6762
6763 void tickle() {
6764 synchronized (this) {
6765 notifyAll();
6766 }
6767 }
Romain Guy06882f82009-06-10 13:36:04 -07006768
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006769 void handleNewWindowLocked(WindowState newWindow) {
6770 if (!newWindow.canReceiveKeys()) {
6771 return;
6772 }
6773 synchronized (this) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006774 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006775 TAG, "New key dispatch window: win="
6776 + newWindow.mClient.asBinder()
6777 + ", last=" + mLastBinder
6778 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
6779 + "), finished=" + mFinished + ", paused="
6780 + newWindow.mToken.paused);
6781
6782 // Displaying a window implicitly causes dispatching to
6783 // be unpaused. (This is to protect against bugs if someone
6784 // pauses dispatching but forgets to resume.)
6785 newWindow.mToken.paused = false;
6786
6787 mGotFirstWindow = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006788
6789 if ((newWindow.mAttrs.flags & FLAG_SYSTEM_ERROR) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006790 if (DEBUG_INPUT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006791 "New SYSTEM_ERROR window; resetting state");
6792 mLastWin = null;
6793 mLastBinder = null;
6794 mMotionTarget = null;
6795 mFinished = true;
6796 } else if (mLastWin != null) {
6797 // If the new window is above the window we are
6798 // waiting on, then stop waiting and let key dispatching
6799 // start on the new guy.
Joe Onorato8a9b2202010-02-26 18:56:32 -08006800 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006801 TAG, "Last win layer=" + mLastWin.mLayer
6802 + ", new win layer=" + newWindow.mLayer);
6803 if (newWindow.mLayer >= mLastWin.mLayer) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006804 // The new window is above the old; finish pending input to the last
6805 // window and start directing it to the new one.
6806 mLastWin.mToken.paused = false;
Christopher Tate136b1f92010-02-11 17:51:24 -08006807 doFinishedKeyLocked(false); // does a notifyAll()
6808 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006809 }
6810 }
6811
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006812 // Now that we've put a new window state in place, make the event waiter
6813 // take notice and retarget its attentions.
6814 notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006815 }
6816 }
6817
6818 void pauseDispatchingLocked(WindowToken token) {
6819 synchronized (this)
6820 {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006821 if (DEBUG_INPUT) Slog.v(TAG, "Pausing WindowToken " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006822 token.paused = true;
6823
6824 /*
6825 if (mLastWin != null && !mFinished && mLastWin.mBaseLayer <= layer) {
6826 mPaused = true;
6827 } else {
6828 if (mLastWin == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006829 Slog.i(TAG, "Key dispatching not paused: no last window.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006830 } else if (mFinished) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006831 Slog.i(TAG, "Key dispatching not paused: finished last key.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006832 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006833 Slog.i(TAG, "Key dispatching not paused: window in higher layer.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006834 }
6835 }
6836 */
6837 }
6838 }
6839
6840 void resumeDispatchingLocked(WindowToken token) {
6841 synchronized (this) {
6842 if (token.paused) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006843 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006844 TAG, "Resuming WindowToken " + token
6845 + ", last=" + mLastBinder
6846 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
6847 + "), finished=" + mFinished + ", paused="
6848 + token.paused);
6849 token.paused = false;
6850 if (mLastWin != null && mLastWin.mToken == token && mFinished) {
Christopher Tate136b1f92010-02-11 17:51:24 -08006851 doFinishedKeyLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006852 } else {
6853 notifyAll();
6854 }
6855 }
6856 }
6857 }
6858
6859 void setEventDispatchingLocked(boolean enabled) {
6860 synchronized (this) {
6861 mEventDispatching = enabled;
6862 notifyAll();
6863 }
6864 }
Romain Guy06882f82009-06-10 13:36:04 -07006865
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006866 void appSwitchComing() {
6867 synchronized (this) {
6868 // Don't wait for more than .5 seconds for app to finish
6869 // processing the pending events.
6870 long now = SystemClock.uptimeMillis() + 500;
Joe Onorato8a9b2202010-02-26 18:56:32 -08006871 if (DEBUG_INPUT) Slog.v(TAG, "appSwitchComing: " + now);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006872 if (mTimeToSwitch == 0 || now < mTimeToSwitch) {
6873 mTimeToSwitch = now;
6874 }
6875 notifyAll();
6876 }
6877 }
Romain Guy06882f82009-06-10 13:36:04 -07006878
Christopher Tate136b1f92010-02-11 17:51:24 -08006879 private final void doFinishedKeyLocked(boolean force) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006880 if (mLastWin != null) {
6881 releasePendingPointerLocked(mLastWin.mSession);
6882 releasePendingTrackballLocked(mLastWin.mSession);
6883 }
Romain Guy06882f82009-06-10 13:36:04 -07006884
Christopher Tate136b1f92010-02-11 17:51:24 -08006885 if (force || mLastWin == null || !mLastWin.mToken.paused
6886 || !mLastWin.isVisibleLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006887 // If the current window has been paused, we aren't -really-
6888 // finished... so let the waiters still wait.
6889 mLastWin = null;
6890 mLastBinder = null;
6891 }
6892 mFinished = true;
6893 notifyAll();
6894 }
6895 }
6896
6897 private class KeyQ extends KeyInputQueue
6898 implements KeyInputQueue.FilterCallback {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006899 KeyQ() {
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07006900 super(mContext, WindowManagerService.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006901 }
6902
6903 @Override
6904 boolean preprocessEvent(InputDevice device, RawInputEvent event) {
6905 if (mPolicy.preprocessInputEventTq(event)) {
6906 return true;
6907 }
Romain Guy06882f82009-06-10 13:36:04 -07006908
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006909 switch (event.type) {
6910 case RawInputEvent.EV_KEY: {
6911 // XXX begin hack
6912 if (DEBUG) {
6913 if (event.keycode == KeyEvent.KEYCODE_G) {
6914 if (event.value != 0) {
6915 // G down
6916 mPolicy.screenTurnedOff(WindowManagerPolicy.OFF_BECAUSE_OF_USER);
6917 }
6918 return false;
6919 }
6920 if (event.keycode == KeyEvent.KEYCODE_D) {
6921 if (event.value != 0) {
6922 //dump();
6923 }
6924 return false;
6925 }
6926 }
6927 // XXX end hack
Romain Guy06882f82009-06-10 13:36:04 -07006928
Charles Mendis322591c2009-10-29 11:06:59 -07006929 boolean screenIsOff = !mPowerManager.isScreenOn();
6930 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006931 int actions = mPolicy.interceptKeyTq(event, !screenIsOff);
Romain Guy06882f82009-06-10 13:36:04 -07006932
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006933 if ((actions & WindowManagerPolicy.ACTION_GO_TO_SLEEP) != 0) {
6934 mPowerManager.goToSleep(event.when);
6935 }
6936
6937 if (screenIsOff) {
6938 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6939 }
6940 if (screenIsDim) {
6941 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6942 }
6943 if ((actions & WindowManagerPolicy.ACTION_POKE_USER_ACTIVITY) != 0) {
6944 mPowerManager.userActivity(event.when, false,
Michael Chane96440f2009-05-06 10:27:36 -07006945 LocalPowerManager.BUTTON_EVENT, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006946 }
Romain Guy06882f82009-06-10 13:36:04 -07006947
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006948 if ((actions & WindowManagerPolicy.ACTION_PASS_TO_USER) != 0) {
6949 if (event.value != 0 && mPolicy.isAppSwitchKeyTqTiLwLi(event.keycode)) {
6950 filterQueue(this);
6951 mKeyWaiter.appSwitchComing();
6952 }
6953 return true;
6954 } else {
6955 return false;
6956 }
6957 }
Romain Guy06882f82009-06-10 13:36:04 -07006958
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006959 case RawInputEvent.EV_REL: {
Charles Mendis322591c2009-10-29 11:06:59 -07006960 boolean screenIsOff = !mPowerManager.isScreenOn();
6961 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006962 if (screenIsOff) {
6963 if (!mPolicy.isWakeRelMovementTq(event.deviceId,
6964 device.classes, event)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006965 //Slog.i(TAG, "dropping because screenIsOff and !isWakeKey");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006966 return false;
6967 }
6968 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6969 }
6970 if (screenIsDim) {
6971 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6972 }
6973 return true;
6974 }
Romain Guy06882f82009-06-10 13:36:04 -07006975
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006976 case RawInputEvent.EV_ABS: {
Charles Mendis322591c2009-10-29 11:06:59 -07006977 boolean screenIsOff = !mPowerManager.isScreenOn();
6978 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006979 if (screenIsOff) {
6980 if (!mPolicy.isWakeAbsMovementTq(event.deviceId,
6981 device.classes, event)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006982 //Slog.i(TAG, "dropping because screenIsOff and !isWakeKey");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006983 return false;
6984 }
6985 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6986 }
6987 if (screenIsDim) {
6988 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6989 }
6990 return true;
6991 }
Romain Guy06882f82009-06-10 13:36:04 -07006992
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006993 default:
6994 return true;
6995 }
6996 }
6997
6998 public int filterEvent(QueuedEvent ev) {
6999 switch (ev.classType) {
7000 case RawInputEvent.CLASS_KEYBOARD:
7001 KeyEvent ke = (KeyEvent)ev.event;
7002 if (mPolicy.isMovementKeyTi(ke.getKeyCode())) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007003 Slog.w(TAG, "Dropping movement key during app switch: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007004 + ke.getKeyCode() + ", action=" + ke.getAction());
7005 return FILTER_REMOVE;
7006 }
7007 return FILTER_ABORT;
7008 default:
7009 return FILTER_KEEP;
7010 }
7011 }
Michael Chan53071d62009-05-13 17:29:48 -07007012 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007013
7014 public boolean detectSafeMode() {
7015 mSafeMode = mPolicy.detectSafeMode();
7016 return mSafeMode;
7017 }
Romain Guy06882f82009-06-10 13:36:04 -07007018
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007019 public void systemReady() {
7020 mPolicy.systemReady();
7021 }
Romain Guy06882f82009-06-10 13:36:04 -07007022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007023 private final class InputDispatcherThread extends Thread {
7024 // Time to wait when there is nothing to do: 9999 seconds.
7025 static final int LONG_WAIT=9999*1000;
7026
7027 public InputDispatcherThread() {
7028 super("InputDispatcher");
7029 }
Romain Guy06882f82009-06-10 13:36:04 -07007030
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007031 @Override
7032 public void run() {
7033 while (true) {
7034 try {
7035 process();
7036 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007037 Slog.e(TAG, "Exception in input dispatcher", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007038 }
7039 }
7040 }
Romain Guy06882f82009-06-10 13:36:04 -07007041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007042 private void process() {
7043 android.os.Process.setThreadPriority(
7044 android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -07007045
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007046 // The last key event we saw
7047 KeyEvent lastKey = null;
7048
7049 // Last keydown time for auto-repeating keys
7050 long lastKeyTime = SystemClock.uptimeMillis();
7051 long nextKeyTime = lastKeyTime+LONG_WAIT;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007052 long downTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007053
Romain Guy06882f82009-06-10 13:36:04 -07007054 // How many successive repeats we generated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007055 int keyRepeatCount = 0;
7056
7057 // Need to report that configuration has changed?
7058 boolean configChanged = false;
Romain Guy06882f82009-06-10 13:36:04 -07007059
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007060 while (true) {
7061 long curTime = SystemClock.uptimeMillis();
7062
Joe Onorato8a9b2202010-02-26 18:56:32 -08007063 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007064 TAG, "Waiting for next key: now=" + curTime
7065 + ", repeat @ " + nextKeyTime);
7066
7067 // Retrieve next event, waiting only as long as the next
7068 // repeat timeout. If the configuration has changed, then
7069 // don't wait at all -- we'll report the change as soon as
7070 // we have processed all events.
7071 QueuedEvent ev = mQueue.getEvent(
7072 (int)((!configChanged && curTime < nextKeyTime)
7073 ? (nextKeyTime-curTime) : 0));
7074
Joe Onorato8a9b2202010-02-26 18:56:32 -08007075 if (DEBUG_INPUT && ev != null) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007076 TAG, "Event: type=" + ev.classType + " data=" + ev.event);
7077
Michael Chan53071d62009-05-13 17:29:48 -07007078 if (MEASURE_LATENCY) {
7079 lt.sample("2 got event ", System.nanoTime() - ev.whenNano);
7080 }
7081
Mike Lockwood3d0ea722009-10-21 22:58:29 -04007082 if (lastKey != null && !mPolicy.allowKeyRepeat()) {
7083 // cancel key repeat at the request of the policy.
7084 lastKey = null;
7085 downTime = 0;
7086 lastKeyTime = curTime;
7087 nextKeyTime = curTime + LONG_WAIT;
7088 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007089 try {
7090 if (ev != null) {
Michael Chan53071d62009-05-13 17:29:48 -07007091 curTime = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007092 int eventType;
7093 if (ev.classType == RawInputEvent.CLASS_TOUCHSCREEN) {
7094 eventType = eventType((MotionEvent)ev.event);
7095 } else if (ev.classType == RawInputEvent.CLASS_KEYBOARD ||
7096 ev.classType == RawInputEvent.CLASS_TRACKBALL) {
7097 eventType = LocalPowerManager.BUTTON_EVENT;
7098 } else {
7099 eventType = LocalPowerManager.OTHER_EVENT;
7100 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07007101 try {
Michael Chan53071d62009-05-13 17:29:48 -07007102 if ((curTime - mLastBatteryStatsCallTime)
Michael Chane96440f2009-05-06 10:27:36 -07007103 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
Michael Chan53071d62009-05-13 17:29:48 -07007104 mLastBatteryStatsCallTime = curTime;
Michael Chane96440f2009-05-06 10:27:36 -07007105 mBatteryStats.noteInputEvent();
7106 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07007107 } catch (RemoteException e) {
7108 // Ignore
7109 }
Michael Chane10de972009-05-18 11:24:50 -07007110
Mike Lockwood5db42402009-11-30 14:51:51 -05007111 if (ev.classType == RawInputEvent.CLASS_CONFIGURATION_CHANGED) {
7112 // do not wake screen in this case
7113 } else if (eventType != TOUCH_EVENT
Michael Chane10de972009-05-18 11:24:50 -07007114 && eventType != LONG_TOUCH_EVENT
7115 && eventType != CHEEK_EVENT) {
7116 mPowerManager.userActivity(curTime, false,
7117 eventType, false);
7118 } else if (mLastTouchEventType != eventType
7119 || (curTime - mLastUserActivityCallTime)
7120 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
7121 mLastUserActivityCallTime = curTime;
7122 mLastTouchEventType = eventType;
7123 mPowerManager.userActivity(curTime, false,
7124 eventType, false);
7125 }
7126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007127 switch (ev.classType) {
7128 case RawInputEvent.CLASS_KEYBOARD:
7129 KeyEvent ke = (KeyEvent)ev.event;
7130 if (ke.isDown()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007131 lastKeyTime = curTime;
Kristian Dreher133bfdf2010-02-23 08:50:58 +01007132 if (lastKey != null &&
7133 ke.getKeyCode() == lastKey.getKeyCode()) {
7134 keyRepeatCount++;
7135 // Arbitrary long timeout to block
7136 // repeating here since we know that
7137 // the device driver takes care of it.
7138 nextKeyTime = lastKeyTime + LONG_WAIT;
The Android Open Source Project2a9ae012010-05-12 12:33:35 -07007139 if (DEBUG_INPUT) Slog.v(
Kristian Dreher133bfdf2010-02-23 08:50:58 +01007140 TAG, "Received repeated key down");
7141 } else {
7142 downTime = curTime;
7143 keyRepeatCount = 0;
7144 nextKeyTime = lastKeyTime
7145 + ViewConfiguration.getLongPressTimeout();
The Android Open Source Project2a9ae012010-05-12 12:33:35 -07007146 if (DEBUG_INPUT) Slog.v(
Kristian Dreher133bfdf2010-02-23 08:50:58 +01007147 TAG, "Received key down: first repeat @ "
7148 + nextKeyTime);
7149 }
7150 lastKey = ke;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007151 } else {
7152 lastKey = null;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007153 downTime = 0;
Kristian Dreher133bfdf2010-02-23 08:50:58 +01007154 keyRepeatCount = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007155 // Arbitrary long timeout.
7156 lastKeyTime = curTime;
7157 nextKeyTime = curTime + LONG_WAIT;
Joe Onorato8a9b2202010-02-26 18:56:32 -08007158 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007159 TAG, "Received key up: ignore repeat @ "
7160 + nextKeyTime);
7161 }
Kristian Dreher133bfdf2010-02-23 08:50:58 +01007162 if (keyRepeatCount > 0) {
7163 dispatchKey(KeyEvent.changeTimeRepeat(ke,
7164 ke.getEventTime(), keyRepeatCount), 0, 0);
7165 } else {
7166 dispatchKey(ke, 0, 0);
7167 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007168 mQueue.recycleEvent(ev);
7169 break;
7170 case RawInputEvent.CLASS_TOUCHSCREEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08007171 //Slog.i(TAG, "Read next event " + ev);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007172 dispatchPointer(ev, (MotionEvent)ev.event, 0, 0);
7173 break;
7174 case RawInputEvent.CLASS_TRACKBALL:
7175 dispatchTrackball(ev, (MotionEvent)ev.event, 0, 0);
7176 break;
7177 case RawInputEvent.CLASS_CONFIGURATION_CHANGED:
7178 configChanged = true;
7179 break;
7180 default:
7181 mQueue.recycleEvent(ev);
7182 break;
7183 }
Romain Guy06882f82009-06-10 13:36:04 -07007184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007185 } else if (configChanged) {
7186 configChanged = false;
7187 sendNewConfiguration();
Romain Guy06882f82009-06-10 13:36:04 -07007188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007189 } else if (lastKey != null) {
7190 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07007191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007192 // Timeout occurred while key was down. If it is at or
7193 // past the key repeat time, dispatch the repeat.
Joe Onorato8a9b2202010-02-26 18:56:32 -08007194 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007195 TAG, "Key timeout: repeat=" + nextKeyTime
7196 + ", now=" + curTime);
7197 if (curTime < nextKeyTime) {
7198 continue;
7199 }
Romain Guy06882f82009-06-10 13:36:04 -07007200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007201 lastKeyTime = nextKeyTime;
7202 nextKeyTime = nextKeyTime + KEY_REPEAT_DELAY;
7203 keyRepeatCount++;
Joe Onorato8a9b2202010-02-26 18:56:32 -08007204 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007205 TAG, "Key repeat: count=" + keyRepeatCount
7206 + ", next @ " + nextKeyTime);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007207 KeyEvent newEvent;
7208 if (downTime != 0 && (downTime
7209 + ViewConfiguration.getLongPressTimeout())
7210 <= curTime) {
7211 newEvent = KeyEvent.changeTimeRepeat(lastKey,
7212 curTime, keyRepeatCount,
7213 lastKey.getFlags() | KeyEvent.FLAG_LONG_PRESS);
7214 downTime = 0;
7215 } else {
7216 newEvent = KeyEvent.changeTimeRepeat(lastKey,
7217 curTime, keyRepeatCount);
7218 }
7219 dispatchKey(newEvent, 0, 0);
Romain Guy06882f82009-06-10 13:36:04 -07007220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007221 } else {
7222 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07007223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007224 lastKeyTime = curTime;
7225 nextKeyTime = curTime + LONG_WAIT;
7226 }
Romain Guy06882f82009-06-10 13:36:04 -07007227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007228 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007229 Slog.e(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007230 "Input thread received uncaught exception: " + e, e);
7231 }
7232 }
7233 }
7234 }
7235
7236 // -------------------------------------------------------------
7237 // Client Session State
7238 // -------------------------------------------------------------
7239
7240 private final class Session extends IWindowSession.Stub
7241 implements IBinder.DeathRecipient {
7242 final IInputMethodClient mClient;
7243 final IInputContext mInputContext;
7244 final int mUid;
7245 final int mPid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007246 final String mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007247 SurfaceSession mSurfaceSession;
7248 int mNumWindow = 0;
7249 boolean mClientDead = false;
Romain Guy06882f82009-06-10 13:36:04 -07007250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007251 /**
7252 * Current pointer move event being dispatched to client window... must
7253 * hold key lock to access.
7254 */
7255 QueuedEvent mPendingPointerMove;
7256 WindowState mPendingPointerWindow;
Romain Guy06882f82009-06-10 13:36:04 -07007257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007258 /**
7259 * Current trackball move event being dispatched to client window... must
7260 * hold key lock to access.
7261 */
7262 QueuedEvent mPendingTrackballMove;
7263 WindowState mPendingTrackballWindow;
7264
7265 public Session(IInputMethodClient client, IInputContext inputContext) {
7266 mClient = client;
7267 mInputContext = inputContext;
7268 mUid = Binder.getCallingUid();
7269 mPid = Binder.getCallingPid();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007270 StringBuilder sb = new StringBuilder();
7271 sb.append("Session{");
7272 sb.append(Integer.toHexString(System.identityHashCode(this)));
7273 sb.append(" uid ");
7274 sb.append(mUid);
7275 sb.append("}");
7276 mStringName = sb.toString();
Romain Guy06882f82009-06-10 13:36:04 -07007277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007278 synchronized (mWindowMap) {
7279 if (mInputMethodManager == null && mHaveInputMethods) {
7280 IBinder b = ServiceManager.getService(
7281 Context.INPUT_METHOD_SERVICE);
7282 mInputMethodManager = IInputMethodManager.Stub.asInterface(b);
7283 }
7284 }
7285 long ident = Binder.clearCallingIdentity();
7286 try {
7287 // Note: it is safe to call in to the input method manager
7288 // here because we are not holding our lock.
7289 if (mInputMethodManager != null) {
7290 mInputMethodManager.addClient(client, inputContext,
7291 mUid, mPid);
7292 } else {
7293 client.setUsingInputMethod(false);
7294 }
7295 client.asBinder().linkToDeath(this, 0);
7296 } catch (RemoteException e) {
7297 // The caller has died, so we can just forget about this.
7298 try {
7299 if (mInputMethodManager != null) {
7300 mInputMethodManager.removeClient(client);
7301 }
7302 } catch (RemoteException ee) {
7303 }
7304 } finally {
7305 Binder.restoreCallingIdentity(ident);
7306 }
7307 }
Romain Guy06882f82009-06-10 13:36:04 -07007308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007309 @Override
7310 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
7311 throws RemoteException {
7312 try {
7313 return super.onTransact(code, data, reply, flags);
7314 } catch (RuntimeException e) {
7315 // Log all 'real' exceptions thrown to the caller
7316 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007317 Slog.e(TAG, "Window Session Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007318 }
7319 throw e;
7320 }
7321 }
7322
7323 public void binderDied() {
7324 // Note: it is safe to call in to the input method manager
7325 // here because we are not holding our lock.
7326 try {
7327 if (mInputMethodManager != null) {
7328 mInputMethodManager.removeClient(mClient);
7329 }
7330 } catch (RemoteException e) {
7331 }
7332 synchronized(mWindowMap) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07007333 mClient.asBinder().unlinkToDeath(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007334 mClientDead = true;
7335 killSessionLocked();
7336 }
7337 }
7338
7339 public int add(IWindow window, WindowManager.LayoutParams attrs,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07007340 int viewVisibility, Rect outContentInsets, InputChannel outInputChannel) {
7341 return addWindow(this, window, attrs, viewVisibility, outContentInsets,
7342 outInputChannel);
7343 }
7344
7345 public int addWithoutInputChannel(IWindow window, WindowManager.LayoutParams attrs,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007346 int viewVisibility, Rect outContentInsets) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07007347 return addWindow(this, window, attrs, viewVisibility, outContentInsets, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007348 }
Romain Guy06882f82009-06-10 13:36:04 -07007349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007350 public void remove(IWindow window) {
7351 removeWindow(this, window);
7352 }
Romain Guy06882f82009-06-10 13:36:04 -07007353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007354 public int relayout(IWindow window, WindowManager.LayoutParams attrs,
7355 int requestedWidth, int requestedHeight, int viewFlags,
7356 boolean insetsPending, Rect outFrame, Rect outContentInsets,
Dianne Hackborn694f79b2010-03-17 19:44:59 -07007357 Rect outVisibleInsets, Configuration outConfig, Surface outSurface) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007358 return relayoutWindow(this, window, attrs,
7359 requestedWidth, requestedHeight, viewFlags, insetsPending,
Dianne Hackborn694f79b2010-03-17 19:44:59 -07007360 outFrame, outContentInsets, outVisibleInsets, outConfig, outSurface);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007361 }
Romain Guy06882f82009-06-10 13:36:04 -07007362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007363 public void setTransparentRegion(IWindow window, Region region) {
7364 setTransparentRegionWindow(this, window, region);
7365 }
Romain Guy06882f82009-06-10 13:36:04 -07007366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007367 public void setInsets(IWindow window, int touchableInsets,
7368 Rect contentInsets, Rect visibleInsets) {
7369 setInsetsWindow(this, window, touchableInsets, contentInsets,
7370 visibleInsets);
7371 }
Romain Guy06882f82009-06-10 13:36:04 -07007372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007373 public void getDisplayFrame(IWindow window, Rect outDisplayFrame) {
7374 getWindowDisplayFrame(this, window, outDisplayFrame);
7375 }
Romain Guy06882f82009-06-10 13:36:04 -07007376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007377 public void finishDrawing(IWindow window) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007378 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007379 TAG, "IWindow finishDrawing called for " + window);
7380 finishDrawingWindow(this, window);
7381 }
7382
7383 public void finishKey(IWindow window) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007384 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007385 TAG, "IWindow finishKey called for " + window);
7386 mKeyWaiter.finishedKey(this, window, false,
7387 KeyWaiter.RETURN_NOTHING);
7388 }
7389
7390 public MotionEvent getPendingPointerMove(IWindow window) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007391 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007392 TAG, "IWindow getPendingMotionEvent called for " + window);
7393 return mKeyWaiter.finishedKey(this, window, false,
7394 KeyWaiter.RETURN_PENDING_POINTER);
7395 }
Romain Guy06882f82009-06-10 13:36:04 -07007396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007397 public MotionEvent getPendingTrackballMove(IWindow window) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007398 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007399 TAG, "IWindow getPendingMotionEvent called for " + window);
7400 return mKeyWaiter.finishedKey(this, window, false,
7401 KeyWaiter.RETURN_PENDING_TRACKBALL);
7402 }
7403
7404 public void setInTouchMode(boolean mode) {
7405 synchronized(mWindowMap) {
7406 mInTouchMode = mode;
7407 }
7408 }
7409
7410 public boolean getInTouchMode() {
7411 synchronized(mWindowMap) {
7412 return mInTouchMode;
7413 }
7414 }
7415
7416 public boolean performHapticFeedback(IWindow window, int effectId,
7417 boolean always) {
7418 synchronized(mWindowMap) {
7419 long ident = Binder.clearCallingIdentity();
7420 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07007421 return mPolicy.performHapticFeedbackLw(
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007422 windowForClientLocked(this, window, true),
7423 effectId, always);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007424 } finally {
7425 Binder.restoreCallingIdentity(ident);
7426 }
7427 }
7428 }
Romain Guy06882f82009-06-10 13:36:04 -07007429
Marco Nelissenbf6956b2009-11-09 15:21:13 -08007430 public void setWallpaperPosition(IBinder window, float x, float y, float xStep, float yStep) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007431 synchronized(mWindowMap) {
7432 long ident = Binder.clearCallingIdentity();
7433 try {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007434 setWindowWallpaperPositionLocked(
7435 windowForClientLocked(this, window, true),
Marco Nelissenbf6956b2009-11-09 15:21:13 -08007436 x, y, xStep, yStep);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007437 } finally {
7438 Binder.restoreCallingIdentity(ident);
7439 }
7440 }
7441 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007442
Dianne Hackborn19382ac2009-09-11 21:13:37 -07007443 public void wallpaperOffsetsComplete(IBinder window) {
7444 WindowManagerService.this.wallpaperOffsetsComplete(window);
7445 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007446
Dianne Hackborn75804932009-10-20 20:15:20 -07007447 public Bundle sendWallpaperCommand(IBinder window, String action, int x, int y,
7448 int z, Bundle extras, boolean sync) {
7449 synchronized(mWindowMap) {
7450 long ident = Binder.clearCallingIdentity();
7451 try {
7452 return sendWindowWallpaperCommandLocked(
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007453 windowForClientLocked(this, window, true),
Dianne Hackborn75804932009-10-20 20:15:20 -07007454 action, x, y, z, extras, sync);
7455 } finally {
7456 Binder.restoreCallingIdentity(ident);
7457 }
7458 }
7459 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007460
Dianne Hackborn75804932009-10-20 20:15:20 -07007461 public void wallpaperCommandComplete(IBinder window, Bundle result) {
7462 WindowManagerService.this.wallpaperCommandComplete(window, result);
7463 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007465 void windowAddedLocked() {
7466 if (mSurfaceSession == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007467 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007468 TAG, "First window added to " + this + ", creating SurfaceSession");
7469 mSurfaceSession = new SurfaceSession();
Joe Onorato8a9b2202010-02-26 18:56:32 -08007470 if (SHOW_TRANSACTIONS) Slog.i(
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007471 TAG, " NEW SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007472 mSessions.add(this);
7473 }
7474 mNumWindow++;
7475 }
7476
7477 void windowRemovedLocked() {
7478 mNumWindow--;
7479 killSessionLocked();
7480 }
Romain Guy06882f82009-06-10 13:36:04 -07007481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007482 void killSessionLocked() {
7483 if (mNumWindow <= 0 && mClientDead) {
7484 mSessions.remove(this);
7485 if (mSurfaceSession != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007486 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007487 TAG, "Last window removed from " + this
7488 + ", destroying " + mSurfaceSession);
Joe Onorato8a9b2202010-02-26 18:56:32 -08007489 if (SHOW_TRANSACTIONS) Slog.i(
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007490 TAG, " KILL SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007491 try {
7492 mSurfaceSession.kill();
7493 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007494 Slog.w(TAG, "Exception thrown when killing surface session "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007495 + mSurfaceSession + " in session " + this
7496 + ": " + e.toString());
7497 }
7498 mSurfaceSession = null;
7499 }
7500 }
7501 }
Romain Guy06882f82009-06-10 13:36:04 -07007502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007503 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007504 pw.print(prefix); pw.print("mNumWindow="); pw.print(mNumWindow);
7505 pw.print(" mClientDead="); pw.print(mClientDead);
7506 pw.print(" mSurfaceSession="); pw.println(mSurfaceSession);
7507 if (mPendingPointerWindow != null || mPendingPointerMove != null) {
7508 pw.print(prefix);
7509 pw.print("mPendingPointerWindow="); pw.print(mPendingPointerWindow);
7510 pw.print(" mPendingPointerMove="); pw.println(mPendingPointerMove);
7511 }
7512 if (mPendingTrackballWindow != null || mPendingTrackballMove != null) {
7513 pw.print(prefix);
7514 pw.print("mPendingTrackballWindow="); pw.print(mPendingTrackballWindow);
7515 pw.print(" mPendingTrackballMove="); pw.println(mPendingTrackballMove);
7516 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007517 }
7518
7519 @Override
7520 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007521 return mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007522 }
7523 }
7524
7525 // -------------------------------------------------------------
7526 // Client Window State
7527 // -------------------------------------------------------------
7528
7529 private final class WindowState implements WindowManagerPolicy.WindowState {
7530 final Session mSession;
7531 final IWindow mClient;
7532 WindowToken mToken;
The Android Open Source Project10592532009-03-18 17:39:46 -07007533 WindowToken mRootToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007534 AppWindowToken mAppToken;
7535 AppWindowToken mTargetAppToken;
7536 final WindowManager.LayoutParams mAttrs = new WindowManager.LayoutParams();
7537 final DeathRecipient mDeathRecipient;
7538 final WindowState mAttachedWindow;
7539 final ArrayList mChildWindows = new ArrayList();
7540 final int mBaseLayer;
7541 final int mSubLayer;
7542 final boolean mLayoutAttached;
7543 final boolean mIsImWindow;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007544 final boolean mIsWallpaper;
7545 final boolean mIsFloatingLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007546 int mViewVisibility;
7547 boolean mPolicyVisibility = true;
7548 boolean mPolicyVisibilityAfterAnim = true;
7549 boolean mAppFreezing;
7550 Surface mSurface;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007551 boolean mReportDestroySurface;
7552 boolean mSurfacePendingDestroy;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007553 boolean mAttachedHidden; // is our parent window hidden?
7554 boolean mLastHidden; // was this window last hidden?
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007555 boolean mWallpaperVisible; // for wallpaper, what was last vis report?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007556 int mRequestedWidth;
7557 int mRequestedHeight;
7558 int mLastRequestedWidth;
7559 int mLastRequestedHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007560 int mLayer;
7561 int mAnimLayer;
7562 int mLastLayer;
7563 boolean mHaveFrame;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007564 boolean mObscured;
Dianne Hackborn93e462b2009-09-15 22:50:40 -07007565 boolean mTurnOnScreen;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007566
7567 WindowState mNextOutsideTouch;
Romain Guy06882f82009-06-10 13:36:04 -07007568
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007569 int mLayoutSeq = -1;
7570
7571 Configuration mConfiguration = null;
7572
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007573 // Actual frame shown on-screen (may be modified by animation)
7574 final Rect mShownFrame = new Rect();
7575 final Rect mLastShownFrame = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07007576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007577 /**
Dianne Hackbornac3587d2010-03-11 11:12:11 -08007578 * Set when we have changed the size of the surface, to know that
7579 * we must tell them application to resize (and thus redraw itself).
7580 */
7581 boolean mSurfaceResized;
7582
7583 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007584 * Insets that determine the actually visible area
7585 */
7586 final Rect mVisibleInsets = new Rect();
7587 final Rect mLastVisibleInsets = new Rect();
7588 boolean mVisibleInsetsChanged;
7589
7590 /**
7591 * Insets that are covered by system windows
7592 */
7593 final Rect mContentInsets = new Rect();
7594 final Rect mLastContentInsets = new Rect();
7595 boolean mContentInsetsChanged;
7596
7597 /**
7598 * Set to true if we are waiting for this window to receive its
7599 * given internal insets before laying out other windows based on it.
7600 */
7601 boolean mGivenInsetsPending;
Romain Guy06882f82009-06-10 13:36:04 -07007602
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007603 /**
7604 * These are the content insets that were given during layout for
7605 * this window, to be applied to windows behind it.
7606 */
7607 final Rect mGivenContentInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07007608
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007609 /**
7610 * These are the visible insets that were given during layout for
7611 * this window, to be applied to windows behind it.
7612 */
7613 final Rect mGivenVisibleInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07007614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007615 /**
7616 * Flag indicating whether the touchable region should be adjusted by
7617 * the visible insets; if false the area outside the visible insets is
7618 * NOT touchable, so we must use those to adjust the frame during hit
7619 * tests.
7620 */
7621 int mTouchableInsets = ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME;
Romain Guy06882f82009-06-10 13:36:04 -07007622
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007623 // Current transformation being applied.
7624 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
7625 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
7626 float mHScale=1, mVScale=1;
7627 float mLastHScale=1, mLastVScale=1;
7628 final Matrix mTmpMatrix = new Matrix();
7629
7630 // "Real" frame that the application sees.
7631 final Rect mFrame = new Rect();
7632 final Rect mLastFrame = new Rect();
7633
7634 final Rect mContainingFrame = new Rect();
7635 final Rect mDisplayFrame = new Rect();
7636 final Rect mContentFrame = new Rect();
7637 final Rect mVisibleFrame = new Rect();
7638
7639 float mShownAlpha = 1;
7640 float mAlpha = 1;
7641 float mLastAlpha = 1;
7642
7643 // Set to true if, when the window gets displayed, it should perform
7644 // an enter animation.
7645 boolean mEnterAnimationPending;
7646
7647 // Currently running animation.
7648 boolean mAnimating;
7649 boolean mLocalAnimating;
7650 Animation mAnimation;
7651 boolean mAnimationIsEntrance;
7652 boolean mHasTransformation;
7653 boolean mHasLocalTransformation;
7654 final Transformation mTransformation = new Transformation();
7655
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07007656 // If a window showing a wallpaper: the requested offset for the
7657 // wallpaper; if a wallpaper window: the currently applied offset.
7658 float mWallpaperX = -1;
7659 float mWallpaperY = -1;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08007660
7661 // If a window showing a wallpaper: what fraction of the offset
7662 // range corresponds to a full virtual screen.
7663 float mWallpaperXStep = -1;
7664 float mWallpaperYStep = -1;
7665
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07007666 // Wallpaper windows: pixels offset based on above variables.
7667 int mXOffset;
7668 int mYOffset;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007669
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007670 // This is set after IWindowSession.relayout() has been called at
7671 // least once for the window. It allows us to detect the situation
7672 // where we don't yet have a surface, but should have one soon, so
7673 // we can give the window focus before waiting for the relayout.
7674 boolean mRelayoutCalled;
Romain Guy06882f82009-06-10 13:36:04 -07007675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007676 // This is set after the Surface has been created but before the
7677 // window has been drawn. During this time the surface is hidden.
7678 boolean mDrawPending;
7679
7680 // This is set after the window has finished drawing for the first
7681 // time but before its surface is shown. The surface will be
7682 // displayed when the next layout is run.
7683 boolean mCommitDrawPending;
7684
7685 // This is set during the time after the window's drawing has been
7686 // committed, and before its surface is actually shown. It is used
7687 // to delay showing the surface until all windows in a token are ready
7688 // to be shown.
7689 boolean mReadyToShow;
Romain Guy06882f82009-06-10 13:36:04 -07007690
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007691 // Set when the window has been shown in the screen the first time.
7692 boolean mHasDrawn;
7693
7694 // Currently running an exit animation?
7695 boolean mExiting;
7696
7697 // Currently on the mDestroySurface list?
7698 boolean mDestroying;
Romain Guy06882f82009-06-10 13:36:04 -07007699
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007700 // Completely remove from window manager after exit animation?
7701 boolean mRemoveOnExit;
7702
7703 // Set when the orientation is changing and this window has not yet
7704 // been updated for the new orientation.
7705 boolean mOrientationChanging;
Romain Guy06882f82009-06-10 13:36:04 -07007706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007707 // Is this window now (or just being) removed?
7708 boolean mRemoved;
Romain Guy06882f82009-06-10 13:36:04 -07007709
Dianne Hackborn16064f92010-03-25 00:47:24 -07007710 // For debugging, this is the last information given to the surface flinger.
7711 boolean mSurfaceShown;
7712 int mSurfaceX, mSurfaceY, mSurfaceW, mSurfaceH;
7713 int mSurfaceLayer;
7714 float mSurfaceAlpha;
7715
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07007716 // Input channel
7717 InputChannel mInputChannel;
7718
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007719 WindowState(Session s, IWindow c, WindowToken token,
7720 WindowState attachedWindow, WindowManager.LayoutParams a,
7721 int viewVisibility) {
7722 mSession = s;
7723 mClient = c;
7724 mToken = token;
7725 mAttrs.copyFrom(a);
7726 mViewVisibility = viewVisibility;
7727 DeathRecipient deathRecipient = new DeathRecipient();
7728 mAlpha = a.alpha;
Joe Onorato8a9b2202010-02-26 18:56:32 -08007729 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007730 TAG, "Window " + this + " client=" + c.asBinder()
7731 + " token=" + token + " (" + mAttrs.token + ")");
7732 try {
7733 c.asBinder().linkToDeath(deathRecipient, 0);
7734 } catch (RemoteException e) {
7735 mDeathRecipient = null;
7736 mAttachedWindow = null;
7737 mLayoutAttached = false;
7738 mIsImWindow = false;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007739 mIsWallpaper = false;
7740 mIsFloatingLayer = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007741 mBaseLayer = 0;
7742 mSubLayer = 0;
7743 return;
7744 }
7745 mDeathRecipient = deathRecipient;
Romain Guy06882f82009-06-10 13:36:04 -07007746
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007747 if ((mAttrs.type >= FIRST_SUB_WINDOW &&
7748 mAttrs.type <= LAST_SUB_WINDOW)) {
7749 // The multiplier here is to reserve space for multiple
7750 // windows in the same type layer.
7751 mBaseLayer = mPolicy.windowTypeToLayerLw(
7752 attachedWindow.mAttrs.type) * TYPE_LAYER_MULTIPLIER
7753 + TYPE_LAYER_OFFSET;
7754 mSubLayer = mPolicy.subWindowTypeToLayerLw(a.type);
7755 mAttachedWindow = attachedWindow;
7756 mAttachedWindow.mChildWindows.add(this);
7757 mLayoutAttached = mAttrs.type !=
7758 WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
7759 mIsImWindow = attachedWindow.mAttrs.type == TYPE_INPUT_METHOD
7760 || attachedWindow.mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007761 mIsWallpaper = attachedWindow.mAttrs.type == TYPE_WALLPAPER;
7762 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007763 } else {
7764 // The multiplier here is to reserve space for multiple
7765 // windows in the same type layer.
7766 mBaseLayer = mPolicy.windowTypeToLayerLw(a.type)
7767 * TYPE_LAYER_MULTIPLIER
7768 + TYPE_LAYER_OFFSET;
7769 mSubLayer = 0;
7770 mAttachedWindow = null;
7771 mLayoutAttached = false;
7772 mIsImWindow = mAttrs.type == TYPE_INPUT_METHOD
7773 || mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007774 mIsWallpaper = mAttrs.type == TYPE_WALLPAPER;
7775 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007776 }
7777
7778 WindowState appWin = this;
7779 while (appWin.mAttachedWindow != null) {
7780 appWin = mAttachedWindow;
7781 }
7782 WindowToken appToken = appWin.mToken;
7783 while (appToken.appWindowToken == null) {
7784 WindowToken parent = mTokenMap.get(appToken.token);
7785 if (parent == null || appToken == parent) {
7786 break;
7787 }
7788 appToken = parent;
7789 }
The Android Open Source Project10592532009-03-18 17:39:46 -07007790 mRootToken = appToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007791 mAppToken = appToken.appWindowToken;
7792
7793 mSurface = null;
7794 mRequestedWidth = 0;
7795 mRequestedHeight = 0;
7796 mLastRequestedWidth = 0;
7797 mLastRequestedHeight = 0;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007798 mXOffset = 0;
7799 mYOffset = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007800 mLayer = 0;
7801 mAnimLayer = 0;
7802 mLastLayer = 0;
7803 }
7804
7805 void attach() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007806 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007807 TAG, "Attaching " + this + " token=" + mToken
7808 + ", list=" + mToken.windows);
7809 mSession.windowAddedLocked();
7810 }
7811
7812 public void computeFrameLw(Rect pf, Rect df, Rect cf, Rect vf) {
7813 mHaveFrame = true;
7814
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007815 final Rect container = mContainingFrame;
7816 container.set(pf);
7817
7818 final Rect display = mDisplayFrame;
7819 display.set(df);
7820
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007821 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007822 container.intersect(mCompatibleScreenFrame);
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007823 if ((mAttrs.flags & FLAG_LAYOUT_NO_LIMITS) == 0) {
7824 display.intersect(mCompatibleScreenFrame);
7825 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007826 }
7827
7828 final int pw = container.right - container.left;
7829 final int ph = container.bottom - container.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007830
7831 int w,h;
7832 if ((mAttrs.flags & mAttrs.FLAG_SCALED) != 0) {
7833 w = mAttrs.width < 0 ? pw : mAttrs.width;
7834 h = mAttrs.height< 0 ? ph : mAttrs.height;
7835 } else {
Romain Guy980a9382010-01-08 15:06:28 -08007836 w = mAttrs.width == mAttrs.MATCH_PARENT ? pw : mRequestedWidth;
7837 h = mAttrs.height== mAttrs.MATCH_PARENT ? ph : mRequestedHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007838 }
Romain Guy06882f82009-06-10 13:36:04 -07007839
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007840 final Rect content = mContentFrame;
7841 content.set(cf);
Romain Guy06882f82009-06-10 13:36:04 -07007842
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007843 final Rect visible = mVisibleFrame;
7844 visible.set(vf);
Romain Guy06882f82009-06-10 13:36:04 -07007845
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007846 final Rect frame = mFrame;
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007847 final int fw = frame.width();
7848 final int fh = frame.height();
Romain Guy06882f82009-06-10 13:36:04 -07007849
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007850 //System.out.println("In: w=" + w + " h=" + h + " container=" +
7851 // container + " x=" + mAttrs.x + " y=" + mAttrs.y);
7852
7853 Gravity.apply(mAttrs.gravity, w, h, container,
7854 (int) (mAttrs.x + mAttrs.horizontalMargin * pw),
7855 (int) (mAttrs.y + mAttrs.verticalMargin * ph), frame);
7856
7857 //System.out.println("Out: " + mFrame);
7858
7859 // Now make sure the window fits in the overall display.
7860 Gravity.applyDisplay(mAttrs.gravity, df, frame);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007861
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007862 // Make sure the content and visible frames are inside of the
7863 // final window frame.
7864 if (content.left < frame.left) content.left = frame.left;
7865 if (content.top < frame.top) content.top = frame.top;
7866 if (content.right > frame.right) content.right = frame.right;
7867 if (content.bottom > frame.bottom) content.bottom = frame.bottom;
7868 if (visible.left < frame.left) visible.left = frame.left;
7869 if (visible.top < frame.top) visible.top = frame.top;
7870 if (visible.right > frame.right) visible.right = frame.right;
7871 if (visible.bottom > frame.bottom) visible.bottom = frame.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007872
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007873 final Rect contentInsets = mContentInsets;
7874 contentInsets.left = content.left-frame.left;
7875 contentInsets.top = content.top-frame.top;
7876 contentInsets.right = frame.right-content.right;
7877 contentInsets.bottom = frame.bottom-content.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007879 final Rect visibleInsets = mVisibleInsets;
7880 visibleInsets.left = visible.left-frame.left;
7881 visibleInsets.top = visible.top-frame.top;
7882 visibleInsets.right = frame.right-visible.right;
7883 visibleInsets.bottom = frame.bottom-visible.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007884
Dianne Hackborn284ac932009-08-28 10:34:25 -07007885 if (mIsWallpaper && (fw != frame.width() || fh != frame.height())) {
7886 updateWallpaperOffsetLocked(this, mDisplay.getWidth(),
Dianne Hackborn19382ac2009-09-11 21:13:37 -07007887 mDisplay.getHeight(), false);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007888 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007889
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007890 if (localLOGV) {
7891 //if ("com.google.android.youtube".equals(mAttrs.packageName)
7892 // && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007893 Slog.v(TAG, "Resolving (mRequestedWidth="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007894 + mRequestedWidth + ", mRequestedheight="
7895 + mRequestedHeight + ") to" + " (pw=" + pw + ", ph=" + ph
7896 + "): frame=" + mFrame.toShortString()
7897 + " ci=" + contentInsets.toShortString()
7898 + " vi=" + visibleInsets.toShortString());
7899 //}
7900 }
7901 }
Romain Guy06882f82009-06-10 13:36:04 -07007902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007903 public Rect getFrameLw() {
7904 return mFrame;
7905 }
7906
7907 public Rect getShownFrameLw() {
7908 return mShownFrame;
7909 }
7910
7911 public Rect getDisplayFrameLw() {
7912 return mDisplayFrame;
7913 }
7914
7915 public Rect getContentFrameLw() {
7916 return mContentFrame;
7917 }
7918
7919 public Rect getVisibleFrameLw() {
7920 return mVisibleFrame;
7921 }
7922
7923 public boolean getGivenInsetsPendingLw() {
7924 return mGivenInsetsPending;
7925 }
7926
7927 public Rect getGivenContentInsetsLw() {
7928 return mGivenContentInsets;
7929 }
Romain Guy06882f82009-06-10 13:36:04 -07007930
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007931 public Rect getGivenVisibleInsetsLw() {
7932 return mGivenVisibleInsets;
7933 }
Romain Guy06882f82009-06-10 13:36:04 -07007934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007935 public WindowManager.LayoutParams getAttrs() {
7936 return mAttrs;
7937 }
7938
7939 public int getSurfaceLayer() {
7940 return mLayer;
7941 }
Romain Guy06882f82009-06-10 13:36:04 -07007942
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007943 public IApplicationToken getAppToken() {
7944 return mAppToken != null ? mAppToken.appToken : null;
7945 }
7946
7947 public boolean hasAppShownWindows() {
7948 return mAppToken != null ? mAppToken.firstWindowDrawn : false;
7949 }
7950
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007951 public void setAnimation(Animation anim) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007952 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007953 TAG, "Setting animation in " + this + ": " + anim);
7954 mAnimating = false;
7955 mLocalAnimating = false;
7956 mAnimation = anim;
7957 mAnimation.restrictDuration(MAX_ANIMATION_DURATION);
7958 mAnimation.scaleCurrentDuration(mWindowAnimationScale);
7959 }
7960
7961 public void clearAnimation() {
7962 if (mAnimation != null) {
7963 mAnimating = true;
7964 mLocalAnimating = false;
7965 mAnimation = null;
7966 }
7967 }
Romain Guy06882f82009-06-10 13:36:04 -07007968
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007969 Surface createSurfaceLocked() {
7970 if (mSurface == null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007971 mReportDestroySurface = false;
7972 mSurfacePendingDestroy = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007973 mDrawPending = true;
7974 mCommitDrawPending = false;
7975 mReadyToShow = false;
7976 if (mAppToken != null) {
7977 mAppToken.allDrawn = false;
7978 }
7979
7980 int flags = 0;
Mathias Agopian317a6282009-08-13 17:29:02 -07007981 if (mAttrs.memoryType == MEMORY_TYPE_PUSH_BUFFERS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007982 flags |= Surface.PUSH_BUFFERS;
7983 }
7984
7985 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
7986 flags |= Surface.SECURE;
7987 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08007988 if (DEBUG_VISIBILITY) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007989 TAG, "Creating surface in session "
7990 + mSession.mSurfaceSession + " window " + this
7991 + " w=" + mFrame.width()
7992 + " h=" + mFrame.height() + " format="
7993 + mAttrs.format + " flags=" + flags);
7994
7995 int w = mFrame.width();
7996 int h = mFrame.height();
7997 if ((mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
7998 // for a scaled surface, we always want the requested
7999 // size.
8000 w = mRequestedWidth;
8001 h = mRequestedHeight;
8002 }
8003
Romain Guy9825ec62009-10-01 00:58:09 -07008004 // Something is wrong and SurfaceFlinger will not like this,
8005 // try to revert to sane values
8006 if (w <= 0) w = 1;
8007 if (h <= 0) h = 1;
8008
Dianne Hackborn16064f92010-03-25 00:47:24 -07008009 mSurfaceShown = false;
8010 mSurfaceLayer = 0;
8011 mSurfaceAlpha = 1;
8012 mSurfaceX = 0;
8013 mSurfaceY = 0;
8014 mSurfaceW = w;
8015 mSurfaceH = h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008016 try {
8017 mSurface = new Surface(
Romain Guy06882f82009-06-10 13:36:04 -07008018 mSession.mSurfaceSession, mSession.mPid,
Mathias Agopian5d26c1e2010-03-01 16:09:43 -08008019 mAttrs.getTitle().toString(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008020 0, w, h, mAttrs.format, flags);
Joe Onorato8a9b2202010-02-26 18:56:32 -08008021 if (SHOW_TRANSACTIONS) Slog.i(TAG, " CREATE SURFACE "
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07008022 + mSurface + " IN SESSION "
8023 + mSession.mSurfaceSession
8024 + ": pid=" + mSession.mPid + " format="
8025 + mAttrs.format + " flags=0x"
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08008026 + Integer.toHexString(flags)
8027 + " / " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008028 } catch (Surface.OutOfResourcesException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008029 Slog.w(TAG, "OutOfResourcesException creating surface");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008030 reclaimSomeSurfaceMemoryLocked(this, "create");
8031 return null;
8032 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008033 Slog.e(TAG, "Exception creating surface", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008034 return null;
8035 }
Romain Guy06882f82009-06-10 13:36:04 -07008036
Joe Onorato8a9b2202010-02-26 18:56:32 -08008037 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008038 TAG, "Got surface: " + mSurface
8039 + ", set left=" + mFrame.left + " top=" + mFrame.top
8040 + ", animLayer=" + mAnimLayer);
8041 if (SHOW_TRANSACTIONS) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008042 Slog.i(TAG, ">>> OPEN TRANSACTION");
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08008043 if (SHOW_TRANSACTIONS) logSurface(this,
8044 "CREATE pos=(" + mFrame.left + "," + mFrame.top + ") (" +
8045 mFrame.width() + "x" + mFrame.height() + "), layer=" +
8046 mAnimLayer + " HIDE", null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008047 }
8048 Surface.openTransaction();
8049 try {
8050 try {
Dianne Hackborn16064f92010-03-25 00:47:24 -07008051 mSurfaceX = mFrame.left + mXOffset;
Dianne Hackborn529bef62010-03-25 11:48:43 -07008052 mSurfaceY = mFrame.top + mYOffset;
Dianne Hackborn16064f92010-03-25 00:47:24 -07008053 mSurface.setPosition(mSurfaceX, mSurfaceY);
8054 mSurfaceLayer = mAnimLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008055 mSurface.setLayer(mAnimLayer);
Dianne Hackborn16064f92010-03-25 00:47:24 -07008056 mSurfaceShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008057 mSurface.hide();
8058 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08008059 if (SHOW_TRANSACTIONS) logSurface(this, "DITHER", null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008060 mSurface.setFlags(Surface.SURFACE_DITHER,
8061 Surface.SURFACE_DITHER);
8062 }
8063 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008064 Slog.w(TAG, "Error creating surface in " + w, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008065 reclaimSomeSurfaceMemoryLocked(this, "create-init");
8066 }
8067 mLastHidden = true;
8068 } finally {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008069 if (SHOW_TRANSACTIONS) Slog.i(TAG, "<<< CLOSE TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008070 Surface.closeTransaction();
8071 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08008072 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008073 TAG, "Created surface " + this);
8074 }
8075 return mSurface;
8076 }
Romain Guy06882f82009-06-10 13:36:04 -07008077
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008078 void destroySurfaceLocked() {
8079 // Window is no longer on-screen, so can no longer receive
8080 // key events... if we were waiting for it to finish
8081 // handling a key event, the wait is over!
8082 mKeyWaiter.finishedKey(mSession, mClient, true,
8083 KeyWaiter.RETURN_NOTHING);
8084 mKeyWaiter.releasePendingPointerLocked(mSession);
8085 mKeyWaiter.releasePendingTrackballLocked(mSession);
8086
8087 if (mAppToken != null && this == mAppToken.startingWindow) {
8088 mAppToken.startingDisplayed = false;
8089 }
Romain Guy06882f82009-06-10 13:36:04 -07008090
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008091 if (mSurface != null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008092 mDrawPending = false;
8093 mCommitDrawPending = false;
8094 mReadyToShow = false;
8095
8096 int i = mChildWindows.size();
8097 while (i > 0) {
8098 i--;
8099 WindowState c = (WindowState)mChildWindows.get(i);
8100 c.mAttachedHidden = true;
8101 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008102
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008103 if (mReportDestroySurface) {
8104 mReportDestroySurface = false;
8105 mSurfacePendingDestroy = true;
8106 try {
8107 mClient.dispatchGetNewSurface();
8108 // We'll really destroy on the next time around.
8109 return;
8110 } catch (RemoteException e) {
8111 }
8112 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008114 try {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008115 if (DEBUG_VISIBILITY) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08008116 RuntimeException e = null;
8117 if (!HIDE_STACK_CRAWLS) {
8118 e = new RuntimeException();
8119 e.fillInStackTrace();
8120 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08008121 Slog.w(TAG, "Window " + this + " destroying surface "
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008122 + mSurface + ", session " + mSession, e);
8123 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008124 if (SHOW_TRANSACTIONS) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08008125 RuntimeException e = null;
8126 if (!HIDE_STACK_CRAWLS) {
8127 e = new RuntimeException();
8128 e.fillInStackTrace();
8129 }
8130 if (SHOW_TRANSACTIONS) logSurface(this, "DESTROY", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008131 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07008132 mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008133 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008134 Slog.w(TAG, "Exception thrown when destroying Window " + this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008135 + " surface " + mSurface + " session " + mSession
8136 + ": " + e.toString());
8137 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008138
Dianne Hackborn16064f92010-03-25 00:47:24 -07008139 mSurfaceShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008140 mSurface = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008141 }
8142 }
8143
8144 boolean finishDrawingLocked() {
8145 if (mDrawPending) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008146 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008147 TAG, "finishDrawingLocked: " + mSurface);
8148 mCommitDrawPending = true;
8149 mDrawPending = false;
8150 return true;
8151 }
8152 return false;
8153 }
8154
8155 // This must be called while inside a transaction.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07008156 boolean commitFinishDrawingLocked(long currentTime) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008157 //Slog.i(TAG, "commitFinishDrawingLocked: " + mSurface);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008158 if (!mCommitDrawPending) {
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07008159 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008160 }
8161 mCommitDrawPending = false;
8162 mReadyToShow = true;
8163 final boolean starting = mAttrs.type == TYPE_APPLICATION_STARTING;
8164 final AppWindowToken atoken = mAppToken;
8165 if (atoken == null || atoken.allDrawn || starting) {
8166 performShowLocked();
8167 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07008168 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008169 }
8170
8171 // This must be called while inside a transaction.
8172 boolean performShowLocked() {
8173 if (DEBUG_VISIBILITY) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08008174 RuntimeException e = null;
8175 if (!HIDE_STACK_CRAWLS) {
8176 e = new RuntimeException();
8177 e.fillInStackTrace();
8178 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08008179 Slog.v(TAG, "performShow on " + this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008180 + ": readyToShow=" + mReadyToShow + " readyForDisplay=" + isReadyForDisplay()
8181 + " starting=" + (mAttrs.type == TYPE_APPLICATION_STARTING), e);
8182 }
8183 if (mReadyToShow && isReadyForDisplay()) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08008184 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) logSurface(this,
8185 "SHOW (performShowLocked)", null);
Joe Onorato8a9b2202010-02-26 18:56:32 -08008186 if (DEBUG_VISIBILITY) Slog.v(TAG, "Showing " + this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008187 + " during animation: policyVis=" + mPolicyVisibility
8188 + " attHidden=" + mAttachedHidden
8189 + " tok.hiddenRequested="
8190 + (mAppToken != null ? mAppToken.hiddenRequested : false)
Dianne Hackborn248b1882009-09-16 16:46:44 -07008191 + " tok.hidden="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008192 + (mAppToken != null ? mAppToken.hidden : false)
8193 + " animating=" + mAnimating
8194 + " tok animating="
8195 + (mAppToken != null ? mAppToken.animating : false));
8196 if (!showSurfaceRobustlyLocked(this)) {
8197 return false;
8198 }
8199 mLastAlpha = -1;
8200 mHasDrawn = true;
8201 mLastHidden = false;
8202 mReadyToShow = false;
8203 enableScreenIfNeededLocked();
8204
8205 applyEnterAnimationLocked(this);
Romain Guy06882f82009-06-10 13:36:04 -07008206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008207 int i = mChildWindows.size();
8208 while (i > 0) {
8209 i--;
8210 WindowState c = (WindowState)mChildWindows.get(i);
Dianne Hackbornf09c1a22010-04-22 15:59:21 -07008211 if (c.mAttachedHidden) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008212 c.mAttachedHidden = false;
Dianne Hackbornf09c1a22010-04-22 15:59:21 -07008213 if (c.mSurface != null) {
8214 c.performShowLocked();
8215 // It hadn't been shown, which means layout not
8216 // performed on it, so now we want to make sure to
8217 // do a layout. If called from within the transaction
8218 // loop, this will cause it to restart with a new
8219 // layout.
8220 mLayoutNeeded = true;
8221 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008222 }
8223 }
Romain Guy06882f82009-06-10 13:36:04 -07008224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008225 if (mAttrs.type != TYPE_APPLICATION_STARTING
8226 && mAppToken != null) {
8227 mAppToken.firstWindowDrawn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008228
Dianne Hackborn248b1882009-09-16 16:46:44 -07008229 if (mAppToken.startingData != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008230 if (DEBUG_STARTING_WINDOW || DEBUG_ANIM) Slog.v(TAG,
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07008231 "Finish starting " + mToken
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008232 + ": first real window is shown, no animation");
Dianne Hackborn248b1882009-09-16 16:46:44 -07008233 // If this initial window is animating, stop it -- we
8234 // will do an animation to reveal it from behind the
8235 // starting window, so there is no need for it to also
8236 // be doing its own stuff.
8237 if (mAnimation != null) {
8238 mAnimation = null;
8239 // Make sure we clean up the animation.
8240 mAnimating = true;
8241 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008242 mFinishedStarting.add(mAppToken);
8243 mH.sendEmptyMessage(H.FINISHED_STARTING);
8244 }
8245 mAppToken.updateReportedVisibilityLocked();
8246 }
8247 }
8248 return true;
8249 }
Romain Guy06882f82009-06-10 13:36:04 -07008250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008251 // This must be called while inside a transaction. Returns true if
8252 // there is more animation to run.
8253 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
Dianne Hackbornde2606d2009-12-18 16:53:55 -08008254 if (!mDisplayFrozen && mPolicy.isScreenOn()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008255 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07008256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008257 if (!mDrawPending && !mCommitDrawPending && mAnimation != null) {
8258 mHasTransformation = true;
8259 mHasLocalTransformation = true;
8260 if (!mLocalAnimating) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008261 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008262 TAG, "Starting animation in " + this +
8263 " @ " + currentTime + ": ww=" + mFrame.width() + " wh=" + mFrame.height() +
8264 " dw=" + dw + " dh=" + dh + " scale=" + mWindowAnimationScale);
8265 mAnimation.initialize(mFrame.width(), mFrame.height(), dw, dh);
8266 mAnimation.setStartTime(currentTime);
8267 mLocalAnimating = true;
8268 mAnimating = true;
8269 }
8270 mTransformation.clear();
8271 final boolean more = mAnimation.getTransformation(
8272 currentTime, mTransformation);
Joe Onorato8a9b2202010-02-26 18:56:32 -08008273 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008274 TAG, "Stepped animation in " + this +
8275 ": more=" + more + ", xform=" + mTransformation);
8276 if (more) {
8277 // we're not done!
8278 return true;
8279 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08008280 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008281 TAG, "Finished animation in " + this +
8282 " @ " + currentTime);
8283 mAnimation = null;
8284 //WindowManagerService.this.dump();
8285 }
8286 mHasLocalTransformation = false;
8287 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppToken != null
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008288 && mAppToken.animation != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008289 // When our app token is animating, we kind-of pretend like
8290 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
8291 // part of this check means that we will only do this if
8292 // our window is not currently exiting, or it is not
8293 // locally animating itself. The idea being that one that
8294 // is exiting and doing a local animation should be removed
8295 // once that animation is done.
8296 mAnimating = true;
8297 mHasTransformation = true;
8298 mTransformation.clear();
8299 return false;
8300 } else if (mHasTransformation) {
8301 // Little trick to get through the path below to act like
8302 // we have finished an animation.
8303 mAnimating = true;
8304 } else if (isAnimating()) {
8305 mAnimating = true;
8306 }
8307 } else if (mAnimation != null) {
8308 // If the display is frozen, and there is a pending animation,
8309 // clear it and make sure we run the cleanup code.
8310 mAnimating = true;
8311 mLocalAnimating = true;
8312 mAnimation = null;
8313 }
Romain Guy06882f82009-06-10 13:36:04 -07008314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008315 if (!mAnimating && !mLocalAnimating) {
8316 return false;
8317 }
8318
Joe Onorato8a9b2202010-02-26 18:56:32 -08008319 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008320 TAG, "Animation done in " + this + ": exiting=" + mExiting
8321 + ", reportedVisible="
8322 + (mAppToken != null ? mAppToken.reportedVisible : false));
Romain Guy06882f82009-06-10 13:36:04 -07008323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008324 mAnimating = false;
8325 mLocalAnimating = false;
8326 mAnimation = null;
8327 mAnimLayer = mLayer;
8328 if (mIsImWindow) {
8329 mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008330 } else if (mIsWallpaper) {
8331 mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008332 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08008333 if (DEBUG_LAYERS) Slog.v(TAG, "Stepping win " + this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008334 + " anim layer: " + mAnimLayer);
8335 mHasTransformation = false;
8336 mHasLocalTransformation = false;
Dianne Hackbornb601ce12010-03-01 23:36:02 -08008337 if (mPolicyVisibility != mPolicyVisibilityAfterAnim) {
8338 if (DEBUG_VISIBILITY) {
8339 Slog.v(TAG, "Policy visibility changing after anim in " + this + ": "
8340 + mPolicyVisibilityAfterAnim);
8341 }
8342 mPolicyVisibility = mPolicyVisibilityAfterAnim;
8343 if (!mPolicyVisibility) {
8344 if (mCurrentFocus == this) {
8345 mFocusMayChange = true;
8346 }
8347 // Window is no longer visible -- make sure if we were waiting
8348 // for it to be displayed before enabling the display, that
8349 // we allow the display to be enabled now.
8350 enableScreenIfNeededLocked();
8351 }
Dianne Hackbornf3bea9c2009-12-09 18:26:21 -08008352 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008353 mTransformation.clear();
8354 if (mHasDrawn
8355 && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
8356 && mAppToken != null
8357 && mAppToken.firstWindowDrawn
8358 && mAppToken.startingData != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008359 if (DEBUG_STARTING_WINDOW) Slog.v(TAG, "Finish starting "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008360 + mToken + ": first real window done animating");
8361 mFinishedStarting.add(mAppToken);
8362 mH.sendEmptyMessage(H.FINISHED_STARTING);
8363 }
Romain Guy06882f82009-06-10 13:36:04 -07008364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008365 finishExit();
8366
8367 if (mAppToken != null) {
8368 mAppToken.updateReportedVisibilityLocked();
8369 }
8370
8371 return false;
8372 }
8373
8374 void finishExit() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008375 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008376 TAG, "finishExit in " + this
8377 + ": exiting=" + mExiting
8378 + " remove=" + mRemoveOnExit
8379 + " windowAnimating=" + isWindowAnimating());
Romain Guy06882f82009-06-10 13:36:04 -07008380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008381 final int N = mChildWindows.size();
8382 for (int i=0; i<N; i++) {
8383 ((WindowState)mChildWindows.get(i)).finishExit();
8384 }
Romain Guy06882f82009-06-10 13:36:04 -07008385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008386 if (!mExiting) {
8387 return;
8388 }
Romain Guy06882f82009-06-10 13:36:04 -07008389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008390 if (isWindowAnimating()) {
8391 return;
8392 }
8393
Joe Onorato8a9b2202010-02-26 18:56:32 -08008394 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008395 TAG, "Exit animation finished in " + this
8396 + ": remove=" + mRemoveOnExit);
8397 if (mSurface != null) {
8398 mDestroySurface.add(this);
8399 mDestroying = true;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08008400 if (SHOW_TRANSACTIONS) logSurface(this, "HIDE (finishExit)", null);
Dianne Hackborn16064f92010-03-25 00:47:24 -07008401 mSurfaceShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008402 try {
8403 mSurface.hide();
8404 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008405 Slog.w(TAG, "Error hiding surface in " + this, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008406 }
8407 mLastHidden = true;
8408 mKeyWaiter.releasePendingPointerLocked(mSession);
8409 }
8410 mExiting = false;
8411 if (mRemoveOnExit) {
8412 mPendingRemove.add(this);
8413 mRemoveOnExit = false;
8414 }
8415 }
Romain Guy06882f82009-06-10 13:36:04 -07008416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008417 boolean isIdentityMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
8418 if (dsdx < .99999f || dsdx > 1.00001f) return false;
8419 if (dtdy < .99999f || dtdy > 1.00001f) return false;
8420 if (dtdx < -.000001f || dtdx > .000001f) return false;
8421 if (dsdy < -.000001f || dsdy > .000001f) return false;
8422 return true;
8423 }
Romain Guy06882f82009-06-10 13:36:04 -07008424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008425 void computeShownFrameLocked() {
8426 final boolean selfTransformation = mHasLocalTransformation;
8427 Transformation attachedTransformation =
8428 (mAttachedWindow != null && mAttachedWindow.mHasLocalTransformation)
8429 ? mAttachedWindow.mTransformation : null;
8430 Transformation appTransformation =
8431 (mAppToken != null && mAppToken.hasTransformation)
8432 ? mAppToken.transformation : null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008433
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008434 // Wallpapers are animated based on the "real" window they
8435 // are currently targeting.
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008436 if (mAttrs.type == TYPE_WALLPAPER && mLowerWallpaperTarget == null
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07008437 && mWallpaperTarget != null) {
Dianne Hackborn5baba162009-09-23 17:01:12 -07008438 if (mWallpaperTarget.mHasLocalTransformation &&
8439 mWallpaperTarget.mAnimation != null &&
8440 !mWallpaperTarget.mAnimation.getDetachWallpaper()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008441 attachedTransformation = mWallpaperTarget.mTransformation;
Dianne Hackborn5baba162009-09-23 17:01:12 -07008442 if (DEBUG_WALLPAPER && attachedTransformation != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008443 Slog.v(TAG, "WP target attached xform: " + attachedTransformation);
Dianne Hackborn5baba162009-09-23 17:01:12 -07008444 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008445 }
8446 if (mWallpaperTarget.mAppToken != null &&
Dianne Hackborn5baba162009-09-23 17:01:12 -07008447 mWallpaperTarget.mAppToken.hasTransformation &&
8448 mWallpaperTarget.mAppToken.animation != null &&
8449 !mWallpaperTarget.mAppToken.animation.getDetachWallpaper()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008450 appTransformation = mWallpaperTarget.mAppToken.transformation;
Dianne Hackborn5baba162009-09-23 17:01:12 -07008451 if (DEBUG_WALLPAPER && appTransformation != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008452 Slog.v(TAG, "WP target app xform: " + appTransformation);
Dianne Hackborn5baba162009-09-23 17:01:12 -07008453 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07008454 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008455 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008457 if (selfTransformation || attachedTransformation != null
8458 || appTransformation != null) {
Romain Guy06882f82009-06-10 13:36:04 -07008459 // cache often used attributes locally
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008460 final Rect frame = mFrame;
8461 final float tmpFloats[] = mTmpFloats;
8462 final Matrix tmpMatrix = mTmpMatrix;
8463
8464 // Compute the desired transformation.
Dianne Hackborn65c23872009-09-18 17:47:02 -07008465 tmpMatrix.setTranslate(0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008466 if (selfTransformation) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07008467 tmpMatrix.postConcat(mTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008468 }
Dianne Hackborn65c23872009-09-18 17:47:02 -07008469 tmpMatrix.postTranslate(frame.left, frame.top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008470 if (attachedTransformation != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07008471 tmpMatrix.postConcat(attachedTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008472 }
8473 if (appTransformation != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07008474 tmpMatrix.postConcat(appTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008475 }
8476
8477 // "convert" it into SurfaceFlinger's format
8478 // (a 2x2 matrix + an offset)
8479 // Here we must not transform the position of the surface
8480 // since it is already included in the transformation.
Joe Onorato8a9b2202010-02-26 18:56:32 -08008481 //Slog.i(TAG, "Transform: " + matrix);
Romain Guy06882f82009-06-10 13:36:04 -07008482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008483 tmpMatrix.getValues(tmpFloats);
8484 mDsDx = tmpFloats[Matrix.MSCALE_X];
8485 mDtDx = tmpFloats[Matrix.MSKEW_X];
8486 mDsDy = tmpFloats[Matrix.MSKEW_Y];
8487 mDtDy = tmpFloats[Matrix.MSCALE_Y];
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008488 int x = (int)tmpFloats[Matrix.MTRANS_X] + mXOffset;
8489 int y = (int)tmpFloats[Matrix.MTRANS_Y] + mYOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008490 int w = frame.width();
8491 int h = frame.height();
8492 mShownFrame.set(x, y, x+w, y+h);
8493
8494 // Now set the alpha... but because our current hardware
8495 // can't do alpha transformation on a non-opaque surface,
8496 // turn it off if we are running an animation that is also
8497 // transforming since it is more important to have that
8498 // animation be smooth.
8499 mShownAlpha = mAlpha;
8500 if (!mLimitedAlphaCompositing
8501 || (!PixelFormat.formatHasAlpha(mAttrs.format)
8502 || (isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
8503 && x == frame.left && y == frame.top))) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008504 //Slog.i(TAG, "Applying alpha transform");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008505 if (selfTransformation) {
8506 mShownAlpha *= mTransformation.getAlpha();
8507 }
8508 if (attachedTransformation != null) {
8509 mShownAlpha *= attachedTransformation.getAlpha();
8510 }
8511 if (appTransformation != null) {
8512 mShownAlpha *= appTransformation.getAlpha();
8513 }
8514 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008515 //Slog.i(TAG, "Not applying alpha transform");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008516 }
Romain Guy06882f82009-06-10 13:36:04 -07008517
Joe Onorato8a9b2202010-02-26 18:56:32 -08008518 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008519 TAG, "Continuing animation in " + this +
8520 ": " + mShownFrame +
8521 ", alpha=" + mTransformation.getAlpha());
8522 return;
8523 }
Romain Guy06882f82009-06-10 13:36:04 -07008524
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008525 mShownFrame.set(mFrame);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008526 if (mXOffset != 0 || mYOffset != 0) {
8527 mShownFrame.offset(mXOffset, mYOffset);
8528 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008529 mShownAlpha = mAlpha;
8530 mDsDx = 1;
8531 mDtDx = 0;
8532 mDsDy = 0;
8533 mDtDy = 1;
8534 }
Romain Guy06882f82009-06-10 13:36:04 -07008535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008536 /**
8537 * Is this window visible? It is not visible if there is no
8538 * surface, or we are in the process of running an exit animation
8539 * that will remove the surface, or its app token has been hidden.
8540 */
8541 public boolean isVisibleLw() {
8542 final AppWindowToken atoken = mAppToken;
8543 return mSurface != null && mPolicyVisibility && !mAttachedHidden
8544 && (atoken == null || !atoken.hiddenRequested)
8545 && !mExiting && !mDestroying;
8546 }
8547
8548 /**
Dianne Hackborn3d163f072009-10-07 21:26:57 -07008549 * Like {@link #isVisibleLw}, but also counts a window that is currently
8550 * "hidden" behind the keyguard as visible. This allows us to apply
8551 * things like window flags that impact the keyguard.
8552 * XXX I am starting to think we need to have ANOTHER visibility flag
8553 * for this "hidden behind keyguard" state rather than overloading
8554 * mPolicyVisibility. Ungh.
8555 */
8556 public boolean isVisibleOrBehindKeyguardLw() {
8557 final AppWindowToken atoken = mAppToken;
8558 return mSurface != null && !mAttachedHidden
8559 && (atoken == null ? mPolicyVisibility : !atoken.hiddenRequested)
Dianne Hackborn5943c202010-04-12 21:36:49 -07008560 && (mOrientationChanging || (!mDrawPending && !mCommitDrawPending))
Dianne Hackborn3d163f072009-10-07 21:26:57 -07008561 && !mExiting && !mDestroying;
8562 }
8563
8564 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008565 * Is this window visible, ignoring its app token? It is not visible
8566 * if there is no surface, or we are in the process of running an exit animation
8567 * that will remove the surface.
8568 */
8569 public boolean isWinVisibleLw() {
8570 final AppWindowToken atoken = mAppToken;
8571 return mSurface != null && mPolicyVisibility && !mAttachedHidden
8572 && (atoken == null || !atoken.hiddenRequested || atoken.animating)
8573 && !mExiting && !mDestroying;
8574 }
8575
8576 /**
8577 * The same as isVisible(), but follows the current hidden state of
8578 * the associated app token, not the pending requested hidden state.
8579 */
8580 boolean isVisibleNow() {
8581 return mSurface != null && mPolicyVisibility && !mAttachedHidden
The Android Open Source Project10592532009-03-18 17:39:46 -07008582 && !mRootToken.hidden && !mExiting && !mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008583 }
8584
8585 /**
8586 * Same as isVisible(), but we also count it as visible between the
8587 * call to IWindowSession.add() and the first relayout().
8588 */
8589 boolean isVisibleOrAdding() {
8590 final AppWindowToken atoken = mAppToken;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008591 return ((mSurface != null && !mReportDestroySurface)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008592 || (!mRelayoutCalled && mViewVisibility == View.VISIBLE))
8593 && mPolicyVisibility && !mAttachedHidden
8594 && (atoken == null || !atoken.hiddenRequested)
8595 && !mExiting && !mDestroying;
8596 }
8597
8598 /**
8599 * Is this window currently on-screen? It is on-screen either if it
8600 * is visible or it is currently running an animation before no longer
8601 * being visible.
8602 */
8603 boolean isOnScreen() {
8604 final AppWindowToken atoken = mAppToken;
8605 if (atoken != null) {
8606 return mSurface != null && mPolicyVisibility && !mDestroying
8607 && ((!mAttachedHidden && !atoken.hiddenRequested)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07008608 || mAnimation != null || atoken.animation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008609 } else {
8610 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn0cd48872009-08-13 18:51:59 -07008611 && (!mAttachedHidden || mAnimation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008612 }
8613 }
Romain Guy06882f82009-06-10 13:36:04 -07008614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008615 /**
8616 * Like isOnScreen(), but we don't return true if the window is part
8617 * of a transition that has not yet been started.
8618 */
8619 boolean isReadyForDisplay() {
Dianne Hackborna8f60182009-09-01 19:01:50 -07008620 if (mRootToken.waitingToShow &&
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07008621 mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07008622 return false;
8623 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008624 final AppWindowToken atoken = mAppToken;
Dianne Hackborn0cd48872009-08-13 18:51:59 -07008625 final boolean animating = atoken != null
8626 ? (atoken.animation != null) : false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008627 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07008628 && ((!mAttachedHidden && mViewVisibility == View.VISIBLE
8629 && !mRootToken.hidden)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07008630 || mAnimation != null || animating);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008631 }
8632
8633 /** Is the window or its container currently animating? */
8634 boolean isAnimating() {
8635 final WindowState attached = mAttachedWindow;
8636 final AppWindowToken atoken = mAppToken;
8637 return mAnimation != null
8638 || (attached != null && attached.mAnimation != null)
Romain Guy06882f82009-06-10 13:36:04 -07008639 || (atoken != null &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008640 (atoken.animation != null
8641 || atoken.inPendingTransaction));
8642 }
8643
8644 /** Is this window currently animating? */
8645 boolean isWindowAnimating() {
8646 return mAnimation != null;
8647 }
8648
8649 /**
8650 * Like isOnScreen, but returns false if the surface hasn't yet
8651 * been drawn.
8652 */
8653 public boolean isDisplayedLw() {
8654 final AppWindowToken atoken = mAppToken;
8655 return mSurface != null && mPolicyVisibility && !mDestroying
8656 && !mDrawPending && !mCommitDrawPending
8657 && ((!mAttachedHidden &&
8658 (atoken == null || !atoken.hiddenRequested))
8659 || mAnimating);
8660 }
8661
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008662 /**
8663 * Returns true if the window has a surface that it has drawn a
Dianne Hackborn5943c202010-04-12 21:36:49 -07008664 * complete UI in to. Note that this returns true if the orientation
8665 * is changing even if the window hasn't redrawn because we don't want
8666 * to stop things from executing during that time.
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008667 */
8668 public boolean isDrawnLw() {
8669 final AppWindowToken atoken = mAppToken;
8670 return mSurface != null && !mDestroying
Dianne Hackborn5943c202010-04-12 21:36:49 -07008671 && (mOrientationChanging || (!mDrawPending && !mCommitDrawPending));
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008672 }
8673
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008674 public boolean fillsScreenLw(int screenWidth, int screenHeight,
8675 boolean shownFrame, boolean onlyOpaque) {
8676 if (mSurface == null) {
8677 return false;
8678 }
8679 if (mAppToken != null && !mAppToken.appFullscreen) {
8680 return false;
8681 }
8682 if (onlyOpaque && mAttrs.format != PixelFormat.OPAQUE) {
8683 return false;
8684 }
8685 final Rect frame = shownFrame ? mShownFrame : mFrame;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07008686
8687 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
8688 return frame.left <= mCompatibleScreenFrame.left &&
8689 frame.top <= mCompatibleScreenFrame.top &&
8690 frame.right >= mCompatibleScreenFrame.right &&
8691 frame.bottom >= mCompatibleScreenFrame.bottom;
8692 } else {
8693 return frame.left <= 0 && frame.top <= 0
8694 && frame.right >= screenWidth
8695 && frame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008696 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008697 }
Romain Guy06882f82009-06-10 13:36:04 -07008698
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008699 /**
Dianne Hackborn25994b42009-09-04 14:21:19 -07008700 * Return true if the window is opaque and fully drawn. This indicates
8701 * it may obscure windows behind it.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008702 */
8703 boolean isOpaqueDrawn() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07008704 return (mAttrs.format == PixelFormat.OPAQUE
8705 || mAttrs.type == TYPE_WALLPAPER)
8706 && mSurface != null && mAnimation == null
8707 && (mAppToken == null || mAppToken.animation == null)
8708 && !mDrawPending && !mCommitDrawPending;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008709 }
8710
8711 boolean needsBackgroundFiller(int screenWidth, int screenHeight) {
8712 return
8713 // only if the application is requesting compatible window
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008714 (mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0 &&
8715 // only if it's visible
8716 mHasDrawn && mViewVisibility == View.VISIBLE &&
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07008717 // and only if the application fills the compatible screen
8718 mFrame.left <= mCompatibleScreenFrame.left &&
8719 mFrame.top <= mCompatibleScreenFrame.top &&
8720 mFrame.right >= mCompatibleScreenFrame.right &&
8721 mFrame.bottom >= mCompatibleScreenFrame.bottom &&
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008722 // and starting window do not need background filler
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07008723 mAttrs.type != mAttrs.TYPE_APPLICATION_STARTING;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008724 }
8725
8726 boolean isFullscreen(int screenWidth, int screenHeight) {
8727 return mFrame.left <= 0 && mFrame.top <= 0 &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07008728 mFrame.right >= screenWidth && mFrame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008729 }
8730
8731 void removeLocked() {
8732 if (mAttachedWindow != null) {
8733 mAttachedWindow.mChildWindows.remove(this);
8734 }
8735 destroySurfaceLocked();
8736 mSession.windowRemovedLocked();
8737 try {
8738 mClient.asBinder().unlinkToDeath(mDeathRecipient, 0);
8739 } catch (RuntimeException e) {
8740 // Ignore if it has already been removed (usually because
8741 // we are doing this as part of processing a death note.)
8742 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07008743
8744 if (ENABLE_NATIVE_INPUT_DISPATCH) {
8745 if (mInputChannel != null) {
8746 mInputManager.unregisterInputChannel(mInputChannel);
8747
8748 mInputChannel.dispose();
8749 mInputChannel = null;
8750 }
8751 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008752 }
8753
8754 private class DeathRecipient implements IBinder.DeathRecipient {
8755 public void binderDied() {
8756 try {
8757 synchronized(mWindowMap) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08008758 WindowState win = windowForClientLocked(mSession, mClient, false);
Joe Onorato8a9b2202010-02-26 18:56:32 -08008759 Slog.i(TAG, "WIN DEATH: " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008760 if (win != null) {
8761 removeWindowLocked(mSession, win);
8762 }
8763 }
8764 } catch (IllegalArgumentException ex) {
8765 // This will happen if the window has already been
8766 // removed.
8767 }
8768 }
8769 }
8770
8771 /** Returns true if this window desires key events. */
8772 public final boolean canReceiveKeys() {
8773 return isVisibleOrAdding()
8774 && (mViewVisibility == View.VISIBLE)
8775 && ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0);
8776 }
8777
8778 public boolean hasDrawnLw() {
8779 return mHasDrawn;
8780 }
8781
8782 public boolean showLw(boolean doAnimation) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008783 return showLw(doAnimation, true);
8784 }
8785
8786 boolean showLw(boolean doAnimation, boolean requestAnim) {
8787 if (mPolicyVisibility && mPolicyVisibilityAfterAnim) {
8788 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008789 }
Dianne Hackbornb601ce12010-03-01 23:36:02 -08008790 if (DEBUG_VISIBILITY) Slog.v(TAG, "Policy visibility true: " + this);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08008791 if (doAnimation) {
8792 if (DEBUG_VISIBILITY) Slog.v(TAG, "doAnimation: mPolicyVisibility="
8793 + mPolicyVisibility + " mAnimation=" + mAnimation);
8794 if (mDisplayFrozen || !mPolicy.isScreenOn()) {
8795 doAnimation = false;
8796 } else if (mPolicyVisibility && mAnimation == null) {
8797 // Check for the case where we are currently visible and
8798 // not animating; we do not want to do animation at such a
8799 // point to become visible when we already are.
8800 doAnimation = false;
8801 }
8802 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008803 mPolicyVisibility = true;
8804 mPolicyVisibilityAfterAnim = true;
8805 if (doAnimation) {
8806 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_ENTER, true);
8807 }
8808 if (requestAnim) {
8809 requestAnimationLocked(0);
8810 }
8811 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008812 }
8813
8814 public boolean hideLw(boolean doAnimation) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008815 return hideLw(doAnimation, true);
8816 }
8817
8818 boolean hideLw(boolean doAnimation, boolean requestAnim) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08008819 if (doAnimation) {
8820 if (mDisplayFrozen || !mPolicy.isScreenOn()) {
8821 doAnimation = false;
8822 }
8823 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008824 boolean current = doAnimation ? mPolicyVisibilityAfterAnim
8825 : mPolicyVisibility;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008826 if (!current) {
8827 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008828 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008829 if (doAnimation) {
8830 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_EXIT, false);
8831 if (mAnimation == null) {
8832 doAnimation = false;
8833 }
8834 }
8835 if (doAnimation) {
8836 mPolicyVisibilityAfterAnim = false;
8837 } else {
Dianne Hackbornb601ce12010-03-01 23:36:02 -08008838 if (DEBUG_VISIBILITY) Slog.v(TAG, "Policy visibility false: " + this);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008839 mPolicyVisibilityAfterAnim = false;
8840 mPolicyVisibility = false;
Dianne Hackbornf3bea9c2009-12-09 18:26:21 -08008841 // Window is no longer visible -- make sure if we were waiting
8842 // for it to be displayed before enabling the display, that
8843 // we allow the display to be enabled now.
8844 enableScreenIfNeededLocked();
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08008845 if (mCurrentFocus == this) {
8846 mFocusMayChange = true;
8847 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008848 }
8849 if (requestAnim) {
8850 requestAnimationLocked(0);
8851 }
8852 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008853 }
8854
8855 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008856 pw.print(prefix); pw.print("mSession="); pw.print(mSession);
8857 pw.print(" mClient="); pw.println(mClient.asBinder());
8858 pw.print(prefix); pw.print("mAttrs="); pw.println(mAttrs);
8859 if (mAttachedWindow != null || mLayoutAttached) {
8860 pw.print(prefix); pw.print("mAttachedWindow="); pw.print(mAttachedWindow);
8861 pw.print(" mLayoutAttached="); pw.println(mLayoutAttached);
8862 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07008863 if (mIsImWindow || mIsWallpaper || mIsFloatingLayer) {
8864 pw.print(prefix); pw.print("mIsImWindow="); pw.print(mIsImWindow);
8865 pw.print(" mIsWallpaper="); pw.print(mIsWallpaper);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008866 pw.print(" mIsFloatingLayer="); pw.print(mIsFloatingLayer);
8867 pw.print(" mWallpaperVisible="); pw.println(mWallpaperVisible);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008868 }
8869 pw.print(prefix); pw.print("mBaseLayer="); pw.print(mBaseLayer);
8870 pw.print(" mSubLayer="); pw.print(mSubLayer);
8871 pw.print(" mAnimLayer="); pw.print(mLayer); pw.print("+");
8872 pw.print((mTargetAppToken != null ? mTargetAppToken.animLayerAdjustment
8873 : (mAppToken != null ? mAppToken.animLayerAdjustment : 0)));
8874 pw.print("="); pw.print(mAnimLayer);
8875 pw.print(" mLastLayer="); pw.println(mLastLayer);
8876 if (mSurface != null) {
8877 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
Dianne Hackborn16064f92010-03-25 00:47:24 -07008878 pw.print(prefix); pw.print("Surface: shown="); pw.print(mSurfaceShown);
8879 pw.print(" layer="); pw.print(mSurfaceLayer);
8880 pw.print(" alpha="); pw.print(mSurfaceAlpha);
8881 pw.print(" rect=("); pw.print(mSurfaceX);
8882 pw.print(","); pw.print(mSurfaceY);
8883 pw.print(") "); pw.print(mSurfaceW);
8884 pw.print(" x "); pw.println(mSurfaceH);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008885 }
8886 pw.print(prefix); pw.print("mToken="); pw.println(mToken);
8887 pw.print(prefix); pw.print("mRootToken="); pw.println(mRootToken);
8888 if (mAppToken != null) {
8889 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
8890 }
8891 if (mTargetAppToken != null) {
8892 pw.print(prefix); pw.print("mTargetAppToken="); pw.println(mTargetAppToken);
8893 }
8894 pw.print(prefix); pw.print("mViewVisibility=0x");
8895 pw.print(Integer.toHexString(mViewVisibility));
8896 pw.print(" mLastHidden="); pw.print(mLastHidden);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07008897 pw.print(" mHaveFrame="); pw.print(mHaveFrame);
8898 pw.print(" mObscured="); pw.println(mObscured);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008899 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim || mAttachedHidden) {
8900 pw.print(prefix); pw.print("mPolicyVisibility=");
8901 pw.print(mPolicyVisibility);
8902 pw.print(" mPolicyVisibilityAfterAnim=");
8903 pw.print(mPolicyVisibilityAfterAnim);
8904 pw.print(" mAttachedHidden="); pw.println(mAttachedHidden);
8905 }
Dianne Hackborn9b52a212009-12-11 14:51:35 -08008906 if (!mRelayoutCalled) {
8907 pw.print(prefix); pw.print("mRelayoutCalled="); pw.println(mRelayoutCalled);
8908 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008909 pw.print(prefix); pw.print("Requested w="); pw.print(mRequestedWidth);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08008910 pw.print(" h="); pw.print(mRequestedHeight);
8911 pw.print(" mLayoutSeq="); pw.println(mLayoutSeq);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008912 if (mXOffset != 0 || mYOffset != 0) {
8913 pw.print(prefix); pw.print("Offsets x="); pw.print(mXOffset);
8914 pw.print(" y="); pw.println(mYOffset);
8915 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008916 pw.print(prefix); pw.print("mGivenContentInsets=");
8917 mGivenContentInsets.printShortString(pw);
8918 pw.print(" mGivenVisibleInsets=");
8919 mGivenVisibleInsets.printShortString(pw);
8920 pw.println();
8921 if (mTouchableInsets != 0 || mGivenInsetsPending) {
8922 pw.print(prefix); pw.print("mTouchableInsets="); pw.print(mTouchableInsets);
8923 pw.print(" mGivenInsetsPending="); pw.println(mGivenInsetsPending);
8924 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08008925 pw.print(prefix); pw.print("mConfiguration="); pw.println(mConfiguration);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008926 pw.print(prefix); pw.print("mShownFrame=");
8927 mShownFrame.printShortString(pw);
8928 pw.print(" last="); mLastShownFrame.printShortString(pw);
8929 pw.println();
8930 pw.print(prefix); pw.print("mFrame="); mFrame.printShortString(pw);
8931 pw.print(" last="); mLastFrame.printShortString(pw);
8932 pw.println();
8933 pw.print(prefix); pw.print("mContainingFrame=");
8934 mContainingFrame.printShortString(pw);
8935 pw.print(" mDisplayFrame=");
8936 mDisplayFrame.printShortString(pw);
8937 pw.println();
8938 pw.print(prefix); pw.print("mContentFrame="); mContentFrame.printShortString(pw);
8939 pw.print(" mVisibleFrame="); mVisibleFrame.printShortString(pw);
8940 pw.println();
8941 pw.print(prefix); pw.print("mContentInsets="); mContentInsets.printShortString(pw);
8942 pw.print(" last="); mLastContentInsets.printShortString(pw);
8943 pw.print(" mVisibleInsets="); mVisibleInsets.printShortString(pw);
8944 pw.print(" last="); mLastVisibleInsets.printShortString(pw);
8945 pw.println();
8946 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
8947 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
8948 pw.print(" mAlpha="); pw.print(mAlpha);
8949 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
8950 }
8951 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
8952 || mAnimation != null) {
8953 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
8954 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
8955 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
8956 pw.print(" mAnimation="); pw.println(mAnimation);
8957 }
8958 if (mHasTransformation || mHasLocalTransformation) {
8959 pw.print(prefix); pw.print("XForm: has=");
8960 pw.print(mHasTransformation);
8961 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
8962 pw.print(" "); mTransformation.printShortString(pw);
8963 pw.println();
8964 }
8965 pw.print(prefix); pw.print("mDrawPending="); pw.print(mDrawPending);
8966 pw.print(" mCommitDrawPending="); pw.print(mCommitDrawPending);
8967 pw.print(" mReadyToShow="); pw.print(mReadyToShow);
8968 pw.print(" mHasDrawn="); pw.println(mHasDrawn);
8969 if (mExiting || mRemoveOnExit || mDestroying || mRemoved) {
8970 pw.print(prefix); pw.print("mExiting="); pw.print(mExiting);
8971 pw.print(" mRemoveOnExit="); pw.print(mRemoveOnExit);
8972 pw.print(" mDestroying="); pw.print(mDestroying);
8973 pw.print(" mRemoved="); pw.println(mRemoved);
8974 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07008975 if (mOrientationChanging || mAppFreezing || mTurnOnScreen) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008976 pw.print(prefix); pw.print("mOrientationChanging=");
8977 pw.print(mOrientationChanging);
Dianne Hackborn93e462b2009-09-15 22:50:40 -07008978 pw.print(" mAppFreezing="); pw.print(mAppFreezing);
8979 pw.print(" mTurnOnScreen="); pw.println(mTurnOnScreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008980 }
Mitsuru Oshima589cebe2009-07-22 20:38:58 -07008981 if (mHScale != 1 || mVScale != 1) {
8982 pw.print(prefix); pw.print("mHScale="); pw.print(mHScale);
8983 pw.print(" mVScale="); pw.println(mVScale);
8984 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07008985 if (mWallpaperX != -1 || mWallpaperY != -1) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008986 pw.print(prefix); pw.print("mWallpaperX="); pw.print(mWallpaperX);
8987 pw.print(" mWallpaperY="); pw.println(mWallpaperY);
8988 }
Marco Nelissenbf6956b2009-11-09 15:21:13 -08008989 if (mWallpaperXStep != -1 || mWallpaperYStep != -1) {
8990 pw.print(prefix); pw.print("mWallpaperXStep="); pw.print(mWallpaperXStep);
8991 pw.print(" mWallpaperYStep="); pw.println(mWallpaperYStep);
8992 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008993 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07008994
8995 String makeInputChannelName() {
8996 return Integer.toHexString(System.identityHashCode(this))
8997 + " " + mAttrs.getTitle();
8998 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008999
9000 @Override
9001 public String toString() {
9002 return "Window{"
9003 + Integer.toHexString(System.identityHashCode(this))
9004 + " " + mAttrs.getTitle() + " paused=" + mToken.paused + "}";
9005 }
9006 }
Romain Guy06882f82009-06-10 13:36:04 -07009007
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009008 // -------------------------------------------------------------
9009 // Window Token State
9010 // -------------------------------------------------------------
9011
9012 class WindowToken {
9013 // The actual token.
9014 final IBinder token;
9015
9016 // The type of window this token is for, as per WindowManager.LayoutParams.
9017 final int windowType;
Romain Guy06882f82009-06-10 13:36:04 -07009018
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009019 // Set if this token was explicitly added by a client, so should
9020 // not be removed when all windows are removed.
9021 final boolean explicit;
Romain Guy06882f82009-06-10 13:36:04 -07009022
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009023 // For printing.
9024 String stringName;
Romain Guy06882f82009-06-10 13:36:04 -07009025
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009026 // If this is an AppWindowToken, this is non-null.
9027 AppWindowToken appWindowToken;
Romain Guy06882f82009-06-10 13:36:04 -07009028
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009029 // All of the windows associated with this token.
9030 final ArrayList<WindowState> windows = new ArrayList<WindowState>();
9031
9032 // Is key dispatching paused for this token?
9033 boolean paused = false;
9034
9035 // Should this token's windows be hidden?
9036 boolean hidden;
9037
9038 // Temporary for finding which tokens no longer have visible windows.
9039 boolean hasVisible;
9040
Dianne Hackborna8f60182009-09-01 19:01:50 -07009041 // Set to true when this token is in a pending transaction where it
9042 // will be shown.
9043 boolean waitingToShow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009044
Dianne Hackborna8f60182009-09-01 19:01:50 -07009045 // Set to true when this token is in a pending transaction where it
9046 // will be hidden.
9047 boolean waitingToHide;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009048
Dianne Hackborna8f60182009-09-01 19:01:50 -07009049 // Set to true when this token is in a pending transaction where its
9050 // windows will be put to the bottom of the list.
9051 boolean sendingToBottom;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009052
Dianne Hackborna8f60182009-09-01 19:01:50 -07009053 // Set to true when this token is in a pending transaction where its
9054 // windows will be put to the top of the list.
9055 boolean sendingToTop;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009057 WindowToken(IBinder _token, int type, boolean _explicit) {
9058 token = _token;
9059 windowType = type;
9060 explicit = _explicit;
9061 }
9062
9063 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009064 pw.print(prefix); pw.print("token="); pw.println(token);
9065 pw.print(prefix); pw.print("windows="); pw.println(windows);
9066 pw.print(prefix); pw.print("windowType="); pw.print(windowType);
9067 pw.print(" hidden="); pw.print(hidden);
9068 pw.print(" hasVisible="); pw.println(hasVisible);
Dianne Hackborna8f60182009-09-01 19:01:50 -07009069 if (waitingToShow || waitingToHide || sendingToBottom || sendingToTop) {
9070 pw.print(prefix); pw.print("waitingToShow="); pw.print(waitingToShow);
9071 pw.print(" waitingToHide="); pw.print(waitingToHide);
9072 pw.print(" sendingToBottom="); pw.print(sendingToBottom);
9073 pw.print(" sendingToTop="); pw.println(sendingToTop);
9074 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009075 }
9076
9077 @Override
9078 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009079 if (stringName == null) {
9080 StringBuilder sb = new StringBuilder();
9081 sb.append("WindowToken{");
9082 sb.append(Integer.toHexString(System.identityHashCode(this)));
9083 sb.append(" token="); sb.append(token); sb.append('}');
9084 stringName = sb.toString();
9085 }
9086 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009087 }
9088 };
9089
9090 class AppWindowToken extends WindowToken {
9091 // Non-null only for application tokens.
9092 final IApplicationToken appToken;
9093
9094 // All of the windows and child windows that are included in this
9095 // application token. Note this list is NOT sorted!
9096 final ArrayList<WindowState> allAppWindows = new ArrayList<WindowState>();
9097
9098 int groupId = -1;
9099 boolean appFullscreen;
9100 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Romain Guy06882f82009-06-10 13:36:04 -07009101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009102 // These are used for determining when all windows associated with
9103 // an activity have been drawn, so they can be made visible together
9104 // at the same time.
9105 int lastTransactionSequence = mTransactionSequence-1;
9106 int numInterestingWindows;
9107 int numDrawnWindows;
9108 boolean inPendingTransaction;
9109 boolean allDrawn;
Romain Guy06882f82009-06-10 13:36:04 -07009110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009111 // Is this token going to be hidden in a little while? If so, it
9112 // won't be taken into account for setting the screen orientation.
9113 boolean willBeHidden;
Romain Guy06882f82009-06-10 13:36:04 -07009114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009115 // Is this window's surface needed? This is almost like hidden, except
9116 // it will sometimes be true a little earlier: when the token has
9117 // been shown, but is still waiting for its app transition to execute
9118 // before making its windows shown.
9119 boolean hiddenRequested;
Romain Guy06882f82009-06-10 13:36:04 -07009120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009121 // Have we told the window clients to hide themselves?
9122 boolean clientHidden;
Romain Guy06882f82009-06-10 13:36:04 -07009123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009124 // Last visibility state we reported to the app token.
9125 boolean reportedVisible;
9126
9127 // Set to true when the token has been removed from the window mgr.
9128 boolean removed;
9129
9130 // Have we been asked to have this token keep the screen frozen?
9131 boolean freezingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07009132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009133 boolean animating;
9134 Animation animation;
9135 boolean hasTransformation;
9136 final Transformation transformation = new Transformation();
Romain Guy06882f82009-06-10 13:36:04 -07009137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009138 // Offset to the window of all layers in the token, for use by
9139 // AppWindowToken animations.
9140 int animLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -07009141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009142 // Information about an application starting window if displayed.
9143 StartingData startingData;
9144 WindowState startingWindow;
9145 View startingView;
9146 boolean startingDisplayed;
9147 boolean startingMoved;
9148 boolean firstWindowDrawn;
9149
9150 AppWindowToken(IApplicationToken _token) {
9151 super(_token.asBinder(),
9152 WindowManager.LayoutParams.TYPE_APPLICATION, true);
9153 appWindowToken = this;
9154 appToken = _token;
9155 }
Romain Guy06882f82009-06-10 13:36:04 -07009156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009157 public void setAnimation(Animation anim) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009158 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009159 TAG, "Setting animation in " + this + ": " + anim);
9160 animation = anim;
9161 animating = false;
9162 anim.restrictDuration(MAX_ANIMATION_DURATION);
9163 anim.scaleCurrentDuration(mTransitionAnimationScale);
9164 int zorder = anim.getZAdjustment();
9165 int adj = 0;
9166 if (zorder == Animation.ZORDER_TOP) {
9167 adj = TYPE_LAYER_OFFSET;
9168 } else if (zorder == Animation.ZORDER_BOTTOM) {
9169 adj = -TYPE_LAYER_OFFSET;
9170 }
Romain Guy06882f82009-06-10 13:36:04 -07009171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009172 if (animLayerAdjustment != adj) {
9173 animLayerAdjustment = adj;
9174 updateLayers();
9175 }
9176 }
Romain Guy06882f82009-06-10 13:36:04 -07009177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009178 public void setDummyAnimation() {
9179 if (animation == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009180 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009181 TAG, "Setting dummy animation in " + this);
9182 animation = sDummyAnimation;
9183 }
9184 }
9185
9186 public void clearAnimation() {
9187 if (animation != null) {
9188 animation = null;
9189 animating = true;
9190 }
9191 }
Romain Guy06882f82009-06-10 13:36:04 -07009192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009193 void updateLayers() {
9194 final int N = allAppWindows.size();
9195 final int adj = animLayerAdjustment;
9196 for (int i=0; i<N; i++) {
9197 WindowState w = allAppWindows.get(i);
9198 w.mAnimLayer = w.mLayer + adj;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009199 if (DEBUG_LAYERS) Slog.v(TAG, "Updating layer " + w + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009200 + w.mAnimLayer);
9201 if (w == mInputMethodTarget) {
9202 setInputMethodAnimLayerAdjustment(adj);
9203 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009204 if (w == mWallpaperTarget && mLowerWallpaperTarget == null) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07009205 setWallpaperAnimLayerAdjustmentLocked(adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07009206 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009207 }
9208 }
Romain Guy06882f82009-06-10 13:36:04 -07009209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009210 void sendAppVisibilityToClients() {
9211 final int N = allAppWindows.size();
9212 for (int i=0; i<N; i++) {
9213 WindowState win = allAppWindows.get(i);
9214 if (win == startingWindow && clientHidden) {
9215 // Don't hide the starting window.
9216 continue;
9217 }
9218 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009219 if (DEBUG_VISIBILITY) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009220 "Setting visibility of " + win + ": " + (!clientHidden));
9221 win.mClient.dispatchAppVisibility(!clientHidden);
9222 } catch (RemoteException e) {
9223 }
9224 }
9225 }
Romain Guy06882f82009-06-10 13:36:04 -07009226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009227 void showAllWindowsLocked() {
9228 final int NW = allAppWindows.size();
9229 for (int i=0; i<NW; i++) {
9230 WindowState w = allAppWindows.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08009231 if (DEBUG_VISIBILITY) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009232 "performing show on: " + w);
9233 w.performShowLocked();
9234 }
9235 }
Romain Guy06882f82009-06-10 13:36:04 -07009236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009237 // This must be called while inside a transaction.
9238 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
Dianne Hackbornde2606d2009-12-18 16:53:55 -08009239 if (!mDisplayFrozen && mPolicy.isScreenOn()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009240 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07009241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009242 if (animation == sDummyAnimation) {
9243 // This guy is going to animate, but not yet. For now count
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009244 // it as not animating for purposes of scheduling transactions;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009245 // when it is really time to animate, this will be set to
9246 // a real animation and the next call will execute normally.
9247 return false;
9248 }
Romain Guy06882f82009-06-10 13:36:04 -07009249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009250 if ((allDrawn || animating || startingDisplayed) && animation != null) {
9251 if (!animating) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009252 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009253 TAG, "Starting animation in " + this +
9254 " @ " + currentTime + ": dw=" + dw + " dh=" + dh
9255 + " scale=" + mTransitionAnimationScale
9256 + " allDrawn=" + allDrawn + " animating=" + animating);
9257 animation.initialize(dw, dh, dw, dh);
9258 animation.setStartTime(currentTime);
9259 animating = true;
9260 }
9261 transformation.clear();
9262 final boolean more = animation.getTransformation(
9263 currentTime, transformation);
Joe Onorato8a9b2202010-02-26 18:56:32 -08009264 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009265 TAG, "Stepped animation in " + this +
9266 ": more=" + more + ", xform=" + transformation);
9267 if (more) {
9268 // we're done!
9269 hasTransformation = true;
9270 return true;
9271 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08009272 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009273 TAG, "Finished animation in " + this +
9274 " @ " + currentTime);
9275 animation = null;
9276 }
9277 } else if (animation != null) {
9278 // If the display is frozen, and there is a pending animation,
9279 // clear it and make sure we run the cleanup code.
9280 animating = true;
9281 animation = null;
9282 }
9283
9284 hasTransformation = false;
Romain Guy06882f82009-06-10 13:36:04 -07009285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009286 if (!animating) {
9287 return false;
9288 }
9289
9290 clearAnimation();
9291 animating = false;
9292 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == this) {
9293 moveInputMethodWindowsIfNeededLocked(true);
9294 }
Romain Guy06882f82009-06-10 13:36:04 -07009295
Joe Onorato8a9b2202010-02-26 18:56:32 -08009296 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009297 TAG, "Animation done in " + this
9298 + ": reportedVisible=" + reportedVisible);
9299
9300 transformation.clear();
9301 if (animLayerAdjustment != 0) {
9302 animLayerAdjustment = 0;
9303 updateLayers();
9304 }
Romain Guy06882f82009-06-10 13:36:04 -07009305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009306 final int N = windows.size();
9307 for (int i=0; i<N; i++) {
9308 ((WindowState)windows.get(i)).finishExit();
9309 }
9310 updateReportedVisibilityLocked();
Romain Guy06882f82009-06-10 13:36:04 -07009311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009312 return false;
9313 }
9314
9315 void updateReportedVisibilityLocked() {
9316 if (appToken == null) {
9317 return;
9318 }
Romain Guy06882f82009-06-10 13:36:04 -07009319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009320 int numInteresting = 0;
9321 int numVisible = 0;
9322 boolean nowGone = true;
Romain Guy06882f82009-06-10 13:36:04 -07009323
Joe Onorato8a9b2202010-02-26 18:56:32 -08009324 if (DEBUG_VISIBILITY) Slog.v(TAG, "Update reported visibility: " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009325 final int N = allAppWindows.size();
9326 for (int i=0; i<N; i++) {
9327 WindowState win = allAppWindows.get(i);
Dianne Hackborn6cf67fa2009-12-21 16:46:34 -08009328 if (win == startingWindow || win.mAppFreezing
The Android Open Source Project727cec02010-04-08 11:35:37 -07009329 || win.mViewVisibility != View.VISIBLE
9330 || win.mAttrs.type == TYPE_APPLICATION_STARTING) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009331 continue;
9332 }
9333 if (DEBUG_VISIBILITY) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009334 Slog.v(TAG, "Win " + win + ": isDrawn="
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009335 + win.isDrawnLw()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009336 + ", isAnimating=" + win.isAnimating());
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009337 if (!win.isDrawnLw()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009338 Slog.v(TAG, "Not displayed: s=" + win.mSurface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009339 + " pv=" + win.mPolicyVisibility
9340 + " dp=" + win.mDrawPending
9341 + " cdp=" + win.mCommitDrawPending
9342 + " ah=" + win.mAttachedHidden
9343 + " th="
9344 + (win.mAppToken != null
9345 ? win.mAppToken.hiddenRequested : false)
9346 + " a=" + win.mAnimating);
9347 }
9348 }
9349 numInteresting++;
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009350 if (win.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009351 if (!win.isAnimating()) {
9352 numVisible++;
9353 }
9354 nowGone = false;
9355 } else if (win.isAnimating()) {
9356 nowGone = false;
9357 }
9358 }
Romain Guy06882f82009-06-10 13:36:04 -07009359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009360 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009361 if (DEBUG_VISIBILITY) Slog.v(TAG, "VIS " + this + ": interesting="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009362 + numInteresting + " visible=" + numVisible);
9363 if (nowVisible != reportedVisible) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009364 if (DEBUG_VISIBILITY) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009365 TAG, "Visibility changed in " + this
9366 + ": vis=" + nowVisible);
9367 reportedVisible = nowVisible;
9368 Message m = mH.obtainMessage(
9369 H.REPORT_APPLICATION_TOKEN_WINDOWS,
9370 nowVisible ? 1 : 0,
9371 nowGone ? 1 : 0,
9372 this);
9373 mH.sendMessage(m);
9374 }
9375 }
Romain Guy06882f82009-06-10 13:36:04 -07009376
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009377 WindowState findMainWindow() {
9378 int j = windows.size();
9379 while (j > 0) {
9380 j--;
9381 WindowState win = windows.get(j);
9382 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
9383 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
9384 return win;
9385 }
9386 }
9387 return null;
9388 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009390 void dump(PrintWriter pw, String prefix) {
9391 super.dump(pw, prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009392 if (appToken != null) {
9393 pw.print(prefix); pw.println("app=true");
9394 }
9395 if (allAppWindows.size() > 0) {
9396 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
9397 }
9398 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
Dianne Hackborna8f60182009-09-01 19:01:50 -07009399 pw.print(" appFullscreen="); pw.print(appFullscreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009400 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
9401 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
9402 pw.print(" clientHidden="); pw.print(clientHidden);
9403 pw.print(" willBeHidden="); pw.print(willBeHidden);
9404 pw.print(" reportedVisible="); pw.println(reportedVisible);
9405 if (paused || freezingScreen) {
9406 pw.print(prefix); pw.print("paused="); pw.print(paused);
9407 pw.print(" freezingScreen="); pw.println(freezingScreen);
9408 }
9409 if (numInterestingWindows != 0 || numDrawnWindows != 0
9410 || inPendingTransaction || allDrawn) {
9411 pw.print(prefix); pw.print("numInterestingWindows=");
9412 pw.print(numInterestingWindows);
9413 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
9414 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
9415 pw.print(" allDrawn="); pw.println(allDrawn);
9416 }
9417 if (animating || animation != null) {
9418 pw.print(prefix); pw.print("animating="); pw.print(animating);
9419 pw.print(" animation="); pw.println(animation);
9420 }
9421 if (animLayerAdjustment != 0) {
9422 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
9423 }
9424 if (hasTransformation) {
9425 pw.print(prefix); pw.print("hasTransformation="); pw.print(hasTransformation);
9426 pw.print(" transformation="); transformation.printShortString(pw);
9427 pw.println();
9428 }
9429 if (startingData != null || removed || firstWindowDrawn) {
9430 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
9431 pw.print(" removed="); pw.print(removed);
9432 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
9433 }
9434 if (startingWindow != null || startingView != null
9435 || startingDisplayed || startingMoved) {
9436 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
9437 pw.print(" startingView="); pw.print(startingView);
9438 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
9439 pw.print(" startingMoved"); pw.println(startingMoved);
9440 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009441 }
9442
9443 @Override
9444 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009445 if (stringName == null) {
9446 StringBuilder sb = new StringBuilder();
9447 sb.append("AppWindowToken{");
9448 sb.append(Integer.toHexString(System.identityHashCode(this)));
9449 sb.append(" token="); sb.append(token); sb.append('}');
9450 stringName = sb.toString();
9451 }
9452 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009453 }
9454 }
Romain Guy06882f82009-06-10 13:36:04 -07009455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009456 // -------------------------------------------------------------
9457 // DummyAnimation
9458 // -------------------------------------------------------------
9459
9460 // This is an animation that does nothing: it just immediately finishes
9461 // itself every time it is called. It is used as a stub animation in cases
9462 // where we want to synchronize multiple things that may be animating.
9463 static final class DummyAnimation extends Animation {
9464 public boolean getTransformation(long currentTime, Transformation outTransformation) {
9465 return false;
9466 }
9467 }
9468 static final Animation sDummyAnimation = new DummyAnimation();
Romain Guy06882f82009-06-10 13:36:04 -07009469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009470 // -------------------------------------------------------------
9471 // Async Handler
9472 // -------------------------------------------------------------
9473
9474 static final class StartingData {
9475 final String pkg;
9476 final int theme;
9477 final CharSequence nonLocalizedLabel;
9478 final int labelRes;
9479 final int icon;
Romain Guy06882f82009-06-10 13:36:04 -07009480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009481 StartingData(String _pkg, int _theme, CharSequence _nonLocalizedLabel,
9482 int _labelRes, int _icon) {
9483 pkg = _pkg;
9484 theme = _theme;
9485 nonLocalizedLabel = _nonLocalizedLabel;
9486 labelRes = _labelRes;
9487 icon = _icon;
9488 }
9489 }
9490
9491 private final class H extends Handler {
9492 public static final int REPORT_FOCUS_CHANGE = 2;
9493 public static final int REPORT_LOSING_FOCUS = 3;
9494 public static final int ANIMATE = 4;
9495 public static final int ADD_STARTING = 5;
9496 public static final int REMOVE_STARTING = 6;
9497 public static final int FINISHED_STARTING = 7;
9498 public static final int REPORT_APPLICATION_TOKEN_WINDOWS = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009499 public static final int WINDOW_FREEZE_TIMEOUT = 11;
9500 public static final int HOLD_SCREEN_CHANGED = 12;
9501 public static final int APP_TRANSITION_TIMEOUT = 13;
9502 public static final int PERSIST_ANIMATION_SCALE = 14;
9503 public static final int FORCE_GC = 15;
9504 public static final int ENABLE_SCREEN = 16;
9505 public static final int APP_FREEZE_TIMEOUT = 17;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009506 public static final int SEND_NEW_CONFIGURATION = 18;
Romain Guy06882f82009-06-10 13:36:04 -07009507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009508 private Session mLastReportedHold;
Romain Guy06882f82009-06-10 13:36:04 -07009509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009510 public H() {
9511 }
Romain Guy06882f82009-06-10 13:36:04 -07009512
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009513 @Override
9514 public void handleMessage(Message msg) {
9515 switch (msg.what) {
9516 case REPORT_FOCUS_CHANGE: {
9517 WindowState lastFocus;
9518 WindowState newFocus;
Romain Guy06882f82009-06-10 13:36:04 -07009519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009520 synchronized(mWindowMap) {
9521 lastFocus = mLastFocus;
9522 newFocus = mCurrentFocus;
9523 if (lastFocus == newFocus) {
9524 // Focus is not changing, so nothing to do.
9525 return;
9526 }
9527 mLastFocus = newFocus;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009528 //Slog.i(TAG, "Focus moving from " + lastFocus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009529 // + " to " + newFocus);
9530 if (newFocus != null && lastFocus != null
9531 && !newFocus.isDisplayedLw()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009532 //Slog.i(TAG, "Delaying loss of focus...");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009533 mLosingFocus.add(lastFocus);
9534 lastFocus = null;
9535 }
9536 }
9537
9538 if (lastFocus != newFocus) {
9539 //System.out.println("Changing focus from " + lastFocus
9540 // + " to " + newFocus);
9541 if (newFocus != null) {
9542 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009543 //Slog.i(TAG, "Gaining focus: " + newFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009544 newFocus.mClient.windowFocusChanged(true, mInTouchMode);
9545 } catch (RemoteException e) {
9546 // Ignore if process has died.
9547 }
9548 }
9549
9550 if (lastFocus != null) {
9551 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009552 //Slog.i(TAG, "Losing focus: " + lastFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009553 lastFocus.mClient.windowFocusChanged(false, mInTouchMode);
9554 } catch (RemoteException e) {
9555 // Ignore if process has died.
9556 }
9557 }
9558 }
9559 } break;
9560
9561 case REPORT_LOSING_FOCUS: {
9562 ArrayList<WindowState> losers;
Romain Guy06882f82009-06-10 13:36:04 -07009563
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009564 synchronized(mWindowMap) {
9565 losers = mLosingFocus;
9566 mLosingFocus = new ArrayList<WindowState>();
9567 }
9568
9569 final int N = losers.size();
9570 for (int i=0; i<N; i++) {
9571 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009572 //Slog.i(TAG, "Losing delayed focus: " + losers.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009573 losers.get(i).mClient.windowFocusChanged(false, mInTouchMode);
9574 } catch (RemoteException e) {
9575 // Ignore if process has died.
9576 }
9577 }
9578 } break;
9579
9580 case ANIMATE: {
9581 synchronized(mWindowMap) {
9582 mAnimationPending = false;
9583 performLayoutAndPlaceSurfacesLocked();
9584 }
9585 } break;
9586
9587 case ADD_STARTING: {
9588 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
9589 final StartingData sd = wtoken.startingData;
9590
9591 if (sd == null) {
9592 // Animation has been canceled... do nothing.
9593 return;
9594 }
Romain Guy06882f82009-06-10 13:36:04 -07009595
Joe Onorato8a9b2202010-02-26 18:56:32 -08009596 if (DEBUG_STARTING_WINDOW) Slog.v(TAG, "Add starting "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009597 + wtoken + ": pkg=" + sd.pkg);
Romain Guy06882f82009-06-10 13:36:04 -07009598
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009599 View view = null;
9600 try {
9601 view = mPolicy.addStartingWindow(
9602 wtoken.token, sd.pkg,
9603 sd.theme, sd.nonLocalizedLabel, sd.labelRes,
9604 sd.icon);
9605 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009606 Slog.w(TAG, "Exception when adding starting window", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009607 }
9608
9609 if (view != null) {
9610 boolean abort = false;
9611
9612 synchronized(mWindowMap) {
9613 if (wtoken.removed || wtoken.startingData == null) {
9614 // If the window was successfully added, then
9615 // we need to remove it.
9616 if (wtoken.startingWindow != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009617 if (DEBUG_STARTING_WINDOW) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009618 "Aborted starting " + wtoken
9619 + ": removed=" + wtoken.removed
9620 + " startingData=" + wtoken.startingData);
9621 wtoken.startingWindow = null;
9622 wtoken.startingData = null;
9623 abort = true;
9624 }
9625 } else {
9626 wtoken.startingView = view;
9627 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08009628 if (DEBUG_STARTING_WINDOW && !abort) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009629 "Added starting " + wtoken
9630 + ": startingWindow="
9631 + wtoken.startingWindow + " startingView="
9632 + wtoken.startingView);
9633 }
9634
9635 if (abort) {
9636 try {
9637 mPolicy.removeStartingWindow(wtoken.token, view);
9638 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009639 Slog.w(TAG, "Exception when removing starting window", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009640 }
9641 }
9642 }
9643 } break;
9644
9645 case REMOVE_STARTING: {
9646 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
9647 IBinder token = null;
9648 View view = null;
9649 synchronized (mWindowMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009650 if (DEBUG_STARTING_WINDOW) Slog.v(TAG, "Remove starting "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009651 + wtoken + ": startingWindow="
9652 + wtoken.startingWindow + " startingView="
9653 + wtoken.startingView);
9654 if (wtoken.startingWindow != null) {
9655 view = wtoken.startingView;
9656 token = wtoken.token;
9657 wtoken.startingData = null;
9658 wtoken.startingView = null;
9659 wtoken.startingWindow = null;
9660 }
9661 }
9662 if (view != null) {
9663 try {
9664 mPolicy.removeStartingWindow(token, view);
9665 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009666 Slog.w(TAG, "Exception when removing starting window", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009667 }
9668 }
9669 } break;
9670
9671 case FINISHED_STARTING: {
9672 IBinder token = null;
9673 View view = null;
9674 while (true) {
9675 synchronized (mWindowMap) {
9676 final int N = mFinishedStarting.size();
9677 if (N <= 0) {
9678 break;
9679 }
9680 AppWindowToken wtoken = mFinishedStarting.remove(N-1);
9681
Joe Onorato8a9b2202010-02-26 18:56:32 -08009682 if (DEBUG_STARTING_WINDOW) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009683 "Finished starting " + wtoken
9684 + ": startingWindow=" + wtoken.startingWindow
9685 + " startingView=" + wtoken.startingView);
9686
9687 if (wtoken.startingWindow == null) {
9688 continue;
9689 }
9690
9691 view = wtoken.startingView;
9692 token = wtoken.token;
9693 wtoken.startingData = null;
9694 wtoken.startingView = null;
9695 wtoken.startingWindow = null;
9696 }
9697
9698 try {
9699 mPolicy.removeStartingWindow(token, view);
9700 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009701 Slog.w(TAG, "Exception when removing starting window", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009702 }
9703 }
9704 } break;
9705
9706 case REPORT_APPLICATION_TOKEN_WINDOWS: {
9707 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
9708
9709 boolean nowVisible = msg.arg1 != 0;
9710 boolean nowGone = msg.arg2 != 0;
9711
9712 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009713 if (DEBUG_VISIBILITY) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009714 TAG, "Reporting visible in " + wtoken
9715 + " visible=" + nowVisible
9716 + " gone=" + nowGone);
9717 if (nowVisible) {
9718 wtoken.appToken.windowsVisible();
9719 } else {
9720 wtoken.appToken.windowsGone();
9721 }
9722 } catch (RemoteException ex) {
9723 }
9724 } break;
Romain Guy06882f82009-06-10 13:36:04 -07009725
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009726 case WINDOW_FREEZE_TIMEOUT: {
9727 synchronized (mWindowMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009728 Slog.w(TAG, "Window freeze timeout expired.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009729 int i = mWindows.size();
9730 while (i > 0) {
9731 i--;
9732 WindowState w = (WindowState)mWindows.get(i);
9733 if (w.mOrientationChanging) {
9734 w.mOrientationChanging = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009735 Slog.w(TAG, "Force clearing orientation change: " + w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009736 }
9737 }
9738 performLayoutAndPlaceSurfacesLocked();
9739 }
9740 break;
9741 }
Romain Guy06882f82009-06-10 13:36:04 -07009742
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009743 case HOLD_SCREEN_CHANGED: {
9744 Session oldHold;
9745 Session newHold;
9746 synchronized (mWindowMap) {
9747 oldHold = mLastReportedHold;
9748 newHold = (Session)msg.obj;
9749 mLastReportedHold = newHold;
9750 }
Romain Guy06882f82009-06-10 13:36:04 -07009751
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009752 if (oldHold != newHold) {
9753 try {
9754 if (oldHold != null) {
9755 mBatteryStats.noteStopWakelock(oldHold.mUid,
9756 "window",
9757 BatteryStats.WAKE_TYPE_WINDOW);
9758 }
9759 if (newHold != null) {
9760 mBatteryStats.noteStartWakelock(newHold.mUid,
9761 "window",
9762 BatteryStats.WAKE_TYPE_WINDOW);
9763 }
9764 } catch (RemoteException e) {
9765 }
9766 }
9767 break;
9768 }
Romain Guy06882f82009-06-10 13:36:04 -07009769
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009770 case APP_TRANSITION_TIMEOUT: {
9771 synchronized (mWindowMap) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009772 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009773 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009774 "*** APP TRANSITION TIMEOUT");
9775 mAppTransitionReady = true;
9776 mAppTransitionTimeout = true;
9777 performLayoutAndPlaceSurfacesLocked();
9778 }
9779 }
9780 break;
9781 }
Romain Guy06882f82009-06-10 13:36:04 -07009782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009783 case PERSIST_ANIMATION_SCALE: {
9784 Settings.System.putFloat(mContext.getContentResolver(),
9785 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
9786 Settings.System.putFloat(mContext.getContentResolver(),
9787 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
9788 break;
9789 }
Romain Guy06882f82009-06-10 13:36:04 -07009790
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009791 case FORCE_GC: {
9792 synchronized(mWindowMap) {
9793 if (mAnimationPending) {
9794 // If we are animating, don't do the gc now but
9795 // delay a bit so we don't interrupt the animation.
9796 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
9797 2000);
9798 return;
9799 }
9800 // If we are currently rotating the display, it will
9801 // schedule a new message when done.
9802 if (mDisplayFrozen) {
9803 return;
9804 }
9805 mFreezeGcPending = 0;
9806 }
9807 Runtime.getRuntime().gc();
9808 break;
9809 }
Romain Guy06882f82009-06-10 13:36:04 -07009810
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009811 case ENABLE_SCREEN: {
9812 performEnableScreen();
9813 break;
9814 }
Romain Guy06882f82009-06-10 13:36:04 -07009815
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009816 case APP_FREEZE_TIMEOUT: {
9817 synchronized (mWindowMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009818 Slog.w(TAG, "App freeze timeout expired.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009819 int i = mAppTokens.size();
9820 while (i > 0) {
9821 i--;
9822 AppWindowToken tok = mAppTokens.get(i);
9823 if (tok.freezingScreen) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009824 Slog.w(TAG, "Force clearing freeze: " + tok);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009825 unsetAppFreezingScreenLocked(tok, true, true);
9826 }
9827 }
9828 }
9829 break;
9830 }
Romain Guy06882f82009-06-10 13:36:04 -07009831
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009832 case SEND_NEW_CONFIGURATION: {
9833 removeMessages(SEND_NEW_CONFIGURATION);
9834 sendNewConfiguration();
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07009835 break;
9836 }
Romain Guy06882f82009-06-10 13:36:04 -07009837
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009838 }
9839 }
9840 }
9841
9842 // -------------------------------------------------------------
9843 // IWindowManager API
9844 // -------------------------------------------------------------
9845
9846 public IWindowSession openSession(IInputMethodClient client,
9847 IInputContext inputContext) {
9848 if (client == null) throw new IllegalArgumentException("null client");
9849 if (inputContext == null) throw new IllegalArgumentException("null inputContext");
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07009850 Session session = new Session(client, inputContext);
9851 return session;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009852 }
9853
9854 public boolean inputMethodClientHasFocus(IInputMethodClient client) {
9855 synchronized (mWindowMap) {
9856 // The focus for the client is the window immediately below
9857 // where we would place the input method window.
9858 int idx = findDesiredInputMethodWindowIndexLocked(false);
9859 WindowState imFocus;
9860 if (idx > 0) {
9861 imFocus = (WindowState)mWindows.get(idx-1);
9862 if (imFocus != null) {
9863 if (imFocus.mSession.mClient != null &&
9864 imFocus.mSession.mClient.asBinder() == client.asBinder()) {
9865 return true;
9866 }
9867 }
9868 }
9869 }
9870 return false;
9871 }
Romain Guy06882f82009-06-10 13:36:04 -07009872
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009873 // -------------------------------------------------------------
9874 // Internals
9875 // -------------------------------------------------------------
9876
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009877 final WindowState windowForClientLocked(Session session, IWindow client,
9878 boolean throwOnError) {
9879 return windowForClientLocked(session, client.asBinder(), throwOnError);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009880 }
Romain Guy06882f82009-06-10 13:36:04 -07009881
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009882 final WindowState windowForClientLocked(Session session, IBinder client,
9883 boolean throwOnError) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009884 WindowState win = mWindowMap.get(client);
Joe Onorato8a9b2202010-02-26 18:56:32 -08009885 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009886 TAG, "Looking up client " + client + ": " + win);
9887 if (win == null) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009888 RuntimeException ex = new IllegalArgumentException(
9889 "Requested window " + client + " does not exist");
9890 if (throwOnError) {
9891 throw ex;
9892 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08009893 Slog.w(TAG, "Failed looking up window", ex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009894 return null;
9895 }
9896 if (session != null && win.mSession != session) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009897 RuntimeException ex = new IllegalArgumentException(
9898 "Requested window " + client + " is in session " +
9899 win.mSession + ", not " + session);
9900 if (throwOnError) {
9901 throw ex;
9902 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08009903 Slog.w(TAG, "Failed looking up window", ex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009904 return null;
9905 }
9906
9907 return win;
9908 }
9909
Dianne Hackborna8f60182009-09-01 19:01:50 -07009910 final void rebuildAppWindowListLocked() {
9911 int NW = mWindows.size();
9912 int i;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009913 int lastWallpaper = -1;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009914 int numRemoved = 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009915
Dianne Hackborna8f60182009-09-01 19:01:50 -07009916 // First remove all existing app windows.
9917 i=0;
9918 while (i < NW) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009919 WindowState w = (WindowState)mWindows.get(i);
9920 if (w.mAppToken != null) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009921 WindowState win = (WindowState)mWindows.remove(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08009922 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG,
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009923 "Rebuild removing window: " + win);
Dianne Hackborna8f60182009-09-01 19:01:50 -07009924 NW--;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009925 numRemoved++;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009926 continue;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009927 } else if (w.mAttrs.type == WindowManager.LayoutParams.TYPE_WALLPAPER
9928 && lastWallpaper == i-1) {
9929 lastWallpaper = i;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009930 }
9931 i++;
9932 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009933
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009934 // The wallpaper window(s) typically live at the bottom of the stack,
9935 // so skip them before adding app tokens.
9936 lastWallpaper++;
9937 i = lastWallpaper;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009938
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009939 // First add all of the exiting app tokens... these are no longer
9940 // in the main app list, but still have windows shown. We put them
9941 // in the back because now that the animation is over we no longer
9942 // will care about them.
9943 int NT = mExitingAppTokens.size();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009944 for (int j=0; j<NT; j++) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009945 i = reAddAppWindowsLocked(i, mExitingAppTokens.get(j));
9946 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009947
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009948 // And add in the still active app tokens in Z order.
9949 NT = mAppTokens.size();
9950 for (int j=0; j<NT; j++) {
9951 i = reAddAppWindowsLocked(i, mAppTokens.get(j));
Dianne Hackborna8f60182009-09-01 19:01:50 -07009952 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009953
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009954 i -= lastWallpaper;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009955 if (i != numRemoved) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009956 Slog.w(TAG, "Rebuild removed " + numRemoved
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009957 + " windows but added " + i);
9958 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07009959 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009960
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009961 private final void assignLayersLocked() {
9962 int N = mWindows.size();
9963 int curBaseLayer = 0;
9964 int curLayer = 0;
9965 int i;
Romain Guy06882f82009-06-10 13:36:04 -07009966
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009967 for (i=0; i<N; i++) {
9968 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009969 if (w.mBaseLayer == curBaseLayer || w.mIsImWindow
9970 || (i > 0 && w.mIsWallpaper)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009971 curLayer += WINDOW_LAYER_MULTIPLIER;
9972 w.mLayer = curLayer;
9973 } else {
9974 curBaseLayer = curLayer = w.mBaseLayer;
9975 w.mLayer = curLayer;
9976 }
9977 if (w.mTargetAppToken != null) {
9978 w.mAnimLayer = w.mLayer + w.mTargetAppToken.animLayerAdjustment;
9979 } else if (w.mAppToken != null) {
9980 w.mAnimLayer = w.mLayer + w.mAppToken.animLayerAdjustment;
9981 } else {
9982 w.mAnimLayer = w.mLayer;
9983 }
9984 if (w.mIsImWindow) {
9985 w.mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07009986 } else if (w.mIsWallpaper) {
9987 w.mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009988 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08009989 if (DEBUG_LAYERS) Slog.v(TAG, "Assign layer " + w + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009990 + w.mAnimLayer);
9991 //System.out.println(
9992 // "Assigned layer " + curLayer + " to " + w.mClient.asBinder());
9993 }
9994 }
9995
9996 private boolean mInLayout = false;
9997 private final void performLayoutAndPlaceSurfacesLocked() {
9998 if (mInLayout) {
Dave Bortcfe65242009-04-09 14:51:04 -07009999 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010000 throw new RuntimeException("Recursive call!");
10001 }
Joe Onorato8a9b2202010-02-26 18:56:32 -080010002 Slog.w(TAG, "performLayoutAndPlaceSurfacesLocked called while in layout");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010003 return;
10004 }
10005
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010006 if (mWaitingForConfig) {
10007 // Our configuration has changed (most likely rotation), but we
10008 // don't yet have the complete configuration to report to
10009 // applications. Don't do any window layout until we have it.
10010 return;
10011 }
10012
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010013 boolean recoveringMemory = false;
10014 if (mForceRemoves != null) {
10015 recoveringMemory = true;
10016 // Wait a little it for things to settle down, and off we go.
10017 for (int i=0; i<mForceRemoves.size(); i++) {
10018 WindowState ws = mForceRemoves.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -080010019 Slog.i(TAG, "Force removing: " + ws);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010020 removeWindowInnerLocked(ws.mSession, ws);
10021 }
10022 mForceRemoves = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010023 Slog.w(TAG, "Due to memory failure, waiting a bit for next layout");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010024 Object tmp = new Object();
10025 synchronized (tmp) {
10026 try {
10027 tmp.wait(250);
10028 } catch (InterruptedException e) {
10029 }
10030 }
10031 }
Romain Guy06882f82009-06-10 13:36:04 -070010032
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010033 mInLayout = true;
10034 try {
10035 performLayoutAndPlaceSurfacesLockedInner(recoveringMemory);
Romain Guy06882f82009-06-10 13:36:04 -070010036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010037 int i = mPendingRemove.size()-1;
10038 if (i >= 0) {
10039 while (i >= 0) {
10040 WindowState w = mPendingRemove.get(i);
10041 removeWindowInnerLocked(w.mSession, w);
10042 i--;
10043 }
10044 mPendingRemove.clear();
10045
10046 mInLayout = false;
10047 assignLayersLocked();
10048 mLayoutNeeded = true;
10049 performLayoutAndPlaceSurfacesLocked();
10050
10051 } else {
10052 mInLayout = false;
10053 if (mLayoutNeeded) {
10054 requestAnimationLocked(0);
10055 }
10056 }
10057 } catch (RuntimeException e) {
10058 mInLayout = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010059 Slog.e(TAG, "Unhandled exception while layout out windows", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010060 }
10061 }
10062
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010063 private final int performLayoutLockedInner() {
10064 if (!mLayoutNeeded) {
10065 return 0;
10066 }
10067
10068 mLayoutNeeded = false;
10069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010070 final int dw = mDisplay.getWidth();
10071 final int dh = mDisplay.getHeight();
10072
10073 final int N = mWindows.size();
10074 int i;
10075
Joe Onorato8a9b2202010-02-26 18:56:32 -080010076 if (DEBUG_LAYOUT) Slog.v(TAG, "performLayout: needed="
Dianne Hackborn9b52a212009-12-11 14:51:35 -080010077 + mLayoutNeeded + " dw=" + dw + " dh=" + dh);
10078
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010079 mPolicy.beginLayoutLw(dw, dh);
Romain Guy06882f82009-06-10 13:36:04 -070010080
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010081 int seq = mLayoutSeq+1;
10082 if (seq < 0) seq = 0;
10083 mLayoutSeq = seq;
10084
10085 // First perform layout of any root windows (not attached
10086 // to another window).
10087 int topAttached = -1;
10088 for (i = N-1; i >= 0; i--) {
10089 WindowState win = (WindowState) mWindows.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010090
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010091 // Don't do layout of a window if it is not visible, or
10092 // soon won't be visible, to avoid wasting time and funky
10093 // changes while a window is animating away.
10094 final AppWindowToken atoken = win.mAppToken;
10095 final boolean gone = win.mViewVisibility == View.GONE
10096 || !win.mRelayoutCalled
10097 || win.mRootToken.hidden
10098 || (atoken != null && atoken.hiddenRequested)
10099 || win.mAttachedHidden
10100 || win.mExiting || win.mDestroying;
10101
10102 if (!win.mLayoutAttached) {
10103 if (DEBUG_LAYOUT) Slog.v(TAG, "First pass " + win
10104 + ": gone=" + gone + " mHaveFrame=" + win.mHaveFrame
10105 + " mLayoutAttached=" + win.mLayoutAttached);
10106 if (DEBUG_LAYOUT && gone) Slog.v(TAG, " (mViewVisibility="
10107 + win.mViewVisibility + " mRelayoutCalled="
10108 + win.mRelayoutCalled + " hidden="
10109 + win.mRootToken.hidden + " hiddenRequested="
10110 + (atoken != null && atoken.hiddenRequested)
10111 + " mAttachedHidden=" + win.mAttachedHidden);
10112 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010113
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010114 // If this view is GONE, then skip it -- keep the current
10115 // frame, and let the caller know so they can ignore it
10116 // if they want. (We do the normal layout for INVISIBLE
10117 // windows, since that means "perform layout as normal,
10118 // just don't display").
10119 if (!gone || !win.mHaveFrame) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010120 if (!win.mLayoutAttached) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010121 mPolicy.layoutWindowLw(win, win.mAttrs, null);
10122 win.mLayoutSeq = seq;
10123 if (DEBUG_LAYOUT) Slog.v(TAG, "-> mFrame="
10124 + win.mFrame + " mContainingFrame="
10125 + win.mContainingFrame + " mDisplayFrame="
10126 + win.mDisplayFrame);
10127 } else {
10128 if (topAttached < 0) topAttached = i;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -070010129 }
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010130 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010131 }
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010132
10133 // Now perform layout of attached windows, which usually
10134 // depend on the position of the window they are attached to.
10135 // XXX does not deal with windows that are attached to windows
10136 // that are themselves attached.
10137 for (i = topAttached; i >= 0; i--) {
10138 WindowState win = (WindowState) mWindows.get(i);
10139
10140 // If this view is GONE, then skip it -- keep the current
10141 // frame, and let the caller know so they can ignore it
10142 // if they want. (We do the normal layout for INVISIBLE
10143 // windows, since that means "perform layout as normal,
10144 // just don't display").
10145 if (win.mLayoutAttached) {
10146 if (DEBUG_LAYOUT) Slog.v(TAG, "Second pass " + win
10147 + " mHaveFrame=" + win.mHaveFrame
10148 + " mViewVisibility=" + win.mViewVisibility
10149 + " mRelayoutCalled=" + win.mRelayoutCalled);
10150 if ((win.mViewVisibility != View.GONE && win.mRelayoutCalled)
10151 || !win.mHaveFrame) {
10152 mPolicy.layoutWindowLw(win, win.mAttrs, win.mAttachedWindow);
10153 win.mLayoutSeq = seq;
10154 if (DEBUG_LAYOUT) Slog.v(TAG, "-> mFrame="
10155 + win.mFrame + " mContainingFrame="
10156 + win.mContainingFrame + " mDisplayFrame="
10157 + win.mDisplayFrame);
10158 }
10159 }
10160 }
10161
10162 return mPolicy.finishLayoutLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010163 }
Romain Guy06882f82009-06-10 13:36:04 -070010164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010165 private final void performLayoutAndPlaceSurfacesLockedInner(
10166 boolean recoveringMemory) {
10167 final long currentTime = SystemClock.uptimeMillis();
10168 final int dw = mDisplay.getWidth();
10169 final int dh = mDisplay.getHeight();
10170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010171 int i;
10172
Dianne Hackbornb601ce12010-03-01 23:36:02 -080010173 if (mFocusMayChange) {
10174 mFocusMayChange = false;
10175 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
10176 }
10177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010178 if (mFxSession == null) {
10179 mFxSession = new SurfaceSession();
10180 }
Romain Guy06882f82009-06-10 13:36:04 -070010181
Joe Onorato8a9b2202010-02-26 18:56:32 -080010182 if (SHOW_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010183
10184 // Initialize state of exiting tokens.
10185 for (i=mExitingTokens.size()-1; i>=0; i--) {
10186 mExitingTokens.get(i).hasVisible = false;
10187 }
10188
10189 // Initialize state of exiting applications.
10190 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
10191 mExitingAppTokens.get(i).hasVisible = false;
10192 }
10193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010194 boolean orientationChangeComplete = true;
10195 Session holdScreen = null;
10196 float screenBrightness = -1;
Mike Lockwoodfb73f792009-11-20 11:31:18 -050010197 float buttonBrightness = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010198 boolean focusDisplayed = false;
10199 boolean animating = false;
10200
10201 Surface.openTransaction();
10202 try {
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010203 boolean wallpaperForceHidingChanged = false;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010204 int repeats = 0;
10205 int changes = 0;
10206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010207 do {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010208 repeats++;
10209 if (repeats > 6) {
10210 Slog.w(TAG, "Animation repeat aborted after too many iterations");
10211 mLayoutNeeded = false;
10212 break;
10213 }
10214
10215 if ((changes&(WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER
10216 | WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG
10217 | WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT)) != 0) {
10218 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
10219 if ((adjustWallpaperWindowsLocked()&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
10220 assignLayersLocked();
10221 mLayoutNeeded = true;
10222 }
10223 }
10224 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG) != 0) {
10225 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
10226 if (updateOrientationFromAppTokensLocked()) {
10227 mLayoutNeeded = true;
10228 mH.sendEmptyMessage(H.SEND_NEW_CONFIGURATION);
10229 }
10230 }
10231 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT) != 0) {
10232 mLayoutNeeded = true;
10233 }
10234 }
10235
10236 // FIRST LOOP: Perform a layout, if needed.
10237 if (repeats < 4) {
10238 changes = performLayoutLockedInner();
10239 if (changes != 0) {
10240 continue;
10241 }
10242 } else {
10243 Slog.w(TAG, "Layout repeat skipped after too many iterations");
10244 changes = 0;
10245 }
10246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010247 final int transactionSequence = ++mTransactionSequence;
10248
10249 // Update animations of all applications, including those
10250 // associated with exiting/removed apps
10251 boolean tokensAnimating = false;
10252 final int NAT = mAppTokens.size();
10253 for (i=0; i<NAT; i++) {
10254 if (mAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
10255 tokensAnimating = true;
10256 }
10257 }
10258 final int NEAT = mExitingAppTokens.size();
10259 for (i=0; i<NEAT; i++) {
10260 if (mExitingAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
10261 tokensAnimating = true;
10262 }
10263 }
10264
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010265 // SECOND LOOP: Execute animations and update visibility of windows.
10266
Joe Onorato8a9b2202010-02-26 18:56:32 -080010267 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG, "*** ANIM STEP: seq="
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010268 + transactionSequence + " tokensAnimating="
10269 + tokensAnimating);
10270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010271 animating = tokensAnimating;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010272
10273 boolean tokenMayBeDrawn = false;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -070010274 boolean wallpaperMayChange = false;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010275 boolean forceHiding = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010276
10277 mPolicy.beginAnimationLw(dw, dh);
10278
Dianne Hackbornbdd52b22009-09-02 21:46:19 -070010279 final int N = mWindows.size();
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010281 for (i=N-1; i>=0; i--) {
10282 WindowState w = (WindowState)mWindows.get(i);
10283
10284 final WindowManager.LayoutParams attrs = w.mAttrs;
10285
10286 if (w.mSurface != null) {
10287 // Execute animation.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -070010288 if (w.commitFinishDrawingLocked(currentTime)) {
10289 if ((w.mAttrs.flags
10290 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010291 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010292 "First draw done in potential wallpaper target " + w);
Dianne Hackborn6c3f5712009-08-25 18:42:59 -070010293 wallpaperMayChange = true;
10294 }
10295 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010296
Dianne Hackborn6136b7e2009-09-18 01:53:49 -070010297 boolean wasAnimating = w.mAnimating;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010298 if (w.stepAnimationLocked(currentTime, dw, dh)) {
10299 animating = true;
10300 //w.dump(" ");
10301 }
Dianne Hackborn6136b7e2009-09-18 01:53:49 -070010302 if (wasAnimating && !w.mAnimating && mWallpaperTarget == w) {
10303 wallpaperMayChange = true;
10304 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010305
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010306 if (mPolicy.doesForceHide(w, attrs)) {
10307 if (!wasAnimating && animating) {
Dianne Hackborn20cb56e2010-03-04 00:58:29 -080010308 if (DEBUG_VISIBILITY) Slog.v(TAG,
10309 "Animation done that could impact force hide: "
10310 + w);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010311 wallpaperForceHidingChanged = true;
Dianne Hackbornb601ce12010-03-01 23:36:02 -080010312 mFocusMayChange = true;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010313 } else if (w.isReadyForDisplay() && w.mAnimation == null) {
10314 forceHiding = true;
10315 }
10316 } else if (mPolicy.canBeForceHidden(w, attrs)) {
10317 boolean changed;
10318 if (forceHiding) {
10319 changed = w.hideLw(false, false);
Dianne Hackborn20cb56e2010-03-04 00:58:29 -080010320 if (DEBUG_VISIBILITY && changed) Slog.v(TAG,
10321 "Now policy hidden: " + w);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010322 } else {
10323 changed = w.showLw(false, false);
Dianne Hackborn20cb56e2010-03-04 00:58:29 -080010324 if (DEBUG_VISIBILITY && changed) Slog.v(TAG,
10325 "Now policy shown: " + w);
10326 if (changed) {
10327 if (wallpaperForceHidingChanged
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010328 && w.isVisibleNow() /*w.isReadyForDisplay()*/) {
Dianne Hackborn20cb56e2010-03-04 00:58:29 -080010329 // Assume we will need to animate. If
10330 // we don't (because the wallpaper will
10331 // stay with the lock screen), then we will
10332 // clean up later.
10333 Animation a = mPolicy.createForceHideEnterAnimation();
10334 if (a != null) {
10335 w.setAnimation(a);
10336 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010337 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -080010338 if (mCurrentFocus == null ||
10339 mCurrentFocus.mLayer < w.mLayer) {
10340 // We are showing on to of the current
10341 // focus, so re-evaluate focus to make
10342 // sure it is correct.
10343 mFocusMayChange = true;
10344 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010345 }
10346 }
10347 if (changed && (attrs.flags
10348 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
10349 wallpaperMayChange = true;
10350 }
10351 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010353 mPolicy.animatingWindowLw(w, attrs);
10354 }
10355
10356 final AppWindowToken atoken = w.mAppToken;
10357 if (atoken != null && (!atoken.allDrawn || atoken.freezingScreen)) {
10358 if (atoken.lastTransactionSequence != transactionSequence) {
10359 atoken.lastTransactionSequence = transactionSequence;
10360 atoken.numInterestingWindows = atoken.numDrawnWindows = 0;
10361 atoken.startingDisplayed = false;
10362 }
10363 if ((w.isOnScreen() || w.mAttrs.type
10364 == WindowManager.LayoutParams.TYPE_BASE_APPLICATION)
10365 && !w.mExiting && !w.mDestroying) {
10366 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010367 Slog.v(TAG, "Eval win " + w + ": isDrawn="
Dianne Hackborn7433e8a2009-09-27 13:21:20 -070010368 + w.isDrawnLw()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010369 + ", isAnimating=" + w.isAnimating());
Dianne Hackborn7433e8a2009-09-27 13:21:20 -070010370 if (!w.isDrawnLw()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010371 Slog.v(TAG, "Not displayed: s=" + w.mSurface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010372 + " pv=" + w.mPolicyVisibility
10373 + " dp=" + w.mDrawPending
10374 + " cdp=" + w.mCommitDrawPending
10375 + " ah=" + w.mAttachedHidden
10376 + " th=" + atoken.hiddenRequested
10377 + " a=" + w.mAnimating);
10378 }
10379 }
10380 if (w != atoken.startingWindow) {
10381 if (!atoken.freezingScreen || !w.mAppFreezing) {
10382 atoken.numInterestingWindows++;
Dianne Hackborn7433e8a2009-09-27 13:21:20 -070010383 if (w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010384 atoken.numDrawnWindows++;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010385 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010386 "tokenMayBeDrawn: " + atoken
10387 + " freezingScreen=" + atoken.freezingScreen
10388 + " mAppFreezing=" + w.mAppFreezing);
10389 tokenMayBeDrawn = true;
10390 }
10391 }
Dianne Hackborn7433e8a2009-09-27 13:21:20 -070010392 } else if (w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010393 atoken.startingDisplayed = true;
10394 }
10395 }
10396 } else if (w.mReadyToShow) {
10397 w.performShowLocked();
10398 }
10399 }
10400
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010401 changes |= mPolicy.finishAnimationLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010402
10403 if (tokenMayBeDrawn) {
10404 // See if any windows have been drawn, so they (and others
10405 // associated with them) can now be shown.
10406 final int NT = mTokenList.size();
10407 for (i=0; i<NT; i++) {
10408 AppWindowToken wtoken = mTokenList.get(i).appWindowToken;
10409 if (wtoken == null) {
10410 continue;
10411 }
10412 if (wtoken.freezingScreen) {
10413 int numInteresting = wtoken.numInterestingWindows;
10414 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010415 if (DEBUG_VISIBILITY) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010416 "allDrawn: " + wtoken
10417 + " interesting=" + numInteresting
10418 + " drawn=" + wtoken.numDrawnWindows);
10419 wtoken.showAllWindowsLocked();
10420 unsetAppFreezingScreenLocked(wtoken, false, true);
10421 orientationChangeComplete = true;
10422 }
10423 } else if (!wtoken.allDrawn) {
10424 int numInteresting = wtoken.numInterestingWindows;
10425 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010426 if (DEBUG_VISIBILITY) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010427 "allDrawn: " + wtoken
10428 + " interesting=" + numInteresting
10429 + " drawn=" + wtoken.numDrawnWindows);
10430 wtoken.allDrawn = true;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010431 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_ANIM;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010432
10433 // We can now show all of the drawn windows!
10434 if (!mOpeningApps.contains(wtoken)) {
10435 wtoken.showAllWindowsLocked();
10436 }
10437 }
10438 }
10439 }
10440 }
10441
10442 // If we are ready to perform an app transition, check through
10443 // all of the app tokens to be shown and see if they are ready
10444 // to go.
10445 if (mAppTransitionReady) {
10446 int NN = mOpeningApps.size();
10447 boolean goodToGo = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010448 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010449 "Checking " + NN + " opening apps (frozen="
10450 + mDisplayFrozen + " timeout="
10451 + mAppTransitionTimeout + ")...");
10452 if (!mDisplayFrozen && !mAppTransitionTimeout) {
10453 // If the display isn't frozen, wait to do anything until
10454 // all of the apps are ready. Otherwise just go because
10455 // we'll unfreeze the display when everyone is ready.
10456 for (i=0; i<NN && goodToGo; i++) {
10457 AppWindowToken wtoken = mOpeningApps.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -080010458 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010459 "Check opening app" + wtoken + ": allDrawn="
10460 + wtoken.allDrawn + " startingDisplayed="
10461 + wtoken.startingDisplayed);
10462 if (!wtoken.allDrawn && !wtoken.startingDisplayed
10463 && !wtoken.startingMoved) {
10464 goodToGo = false;
10465 }
10466 }
10467 }
10468 if (goodToGo) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010469 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG, "**** GOOD TO GO");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010470 int transit = mNextAppTransition;
10471 if (mSkipAppTransitionAnimation) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -070010472 transit = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010473 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -070010474 mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010475 mAppTransitionReady = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -070010476 mAppTransitionRunning = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010477 mAppTransitionTimeout = false;
10478 mStartingIconInTransition = false;
10479 mSkipAppTransitionAnimation = false;
10480
10481 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
10482
Dianne Hackborna8f60182009-09-01 19:01:50 -070010483 // If there are applications waiting to come to the
10484 // top of the stack, now is the time to move their windows.
10485 // (Note that we don't do apps going to the bottom
10486 // here -- we want to keep their windows in the old
10487 // Z-order until the animation completes.)
10488 if (mToTopApps.size() > 0) {
10489 NN = mAppTokens.size();
10490 for (i=0; i<NN; i++) {
10491 AppWindowToken wtoken = mAppTokens.get(i);
10492 if (wtoken.sendingToTop) {
10493 wtoken.sendingToTop = false;
10494 moveAppWindowsLocked(wtoken, NN, false);
10495 }
10496 }
10497 mToTopApps.clear();
10498 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010499
Dianne Hackborn25994b42009-09-04 14:21:19 -070010500 WindowState oldWallpaper = mWallpaperTarget;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010501
Dianne Hackborn3be63c02009-08-20 19:31:38 -070010502 adjustWallpaperWindowsLocked();
Dianne Hackborn6c3f5712009-08-25 18:42:59 -070010503 wallpaperMayChange = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010504
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -070010505 // The top-most window will supply the layout params,
10506 // and we will determine it below.
10507 LayoutParams animLp = null;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010508 AppWindowToken animToken = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -070010509 int bestAnimLayer = -1;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010510
Joe Onorato8a9b2202010-02-26 18:56:32 -080010511 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -070010512 "New wallpaper target=" + mWallpaperTarget
10513 + ", lower target=" + mLowerWallpaperTarget
10514 + ", upper target=" + mUpperWallpaperTarget);
Dianne Hackborn25994b42009-09-04 14:21:19 -070010515 int foundWallpapers = 0;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -070010516 // Do a first pass through the tokens for two
10517 // things:
10518 // (1) Determine if both the closing and opening
10519 // app token sets are wallpaper targets, in which
10520 // case special animations are needed
10521 // (since the wallpaper needs to stay static
10522 // behind them).
10523 // (2) Find the layout params of the top-most
10524 // application window in the tokens, which is
10525 // what will control the animation theme.
10526 final int NC = mClosingApps.size();
10527 NN = NC + mOpeningApps.size();
10528 for (i=0; i<NN; i++) {
10529 AppWindowToken wtoken;
10530 int mode;
10531 if (i < NC) {
10532 wtoken = mClosingApps.get(i);
10533 mode = 1;
10534 } else {
10535 wtoken = mOpeningApps.get(i-NC);
10536 mode = 2;
10537 }
10538 if (mLowerWallpaperTarget != null) {
10539 if (mLowerWallpaperTarget.mAppToken == wtoken
10540 || mUpperWallpaperTarget.mAppToken == wtoken) {
10541 foundWallpapers |= mode;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -070010542 }
10543 }
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -070010544 if (wtoken.appFullscreen) {
10545 WindowState ws = wtoken.findMainWindow();
10546 if (ws != null) {
10547 // If this is a compatibility mode
10548 // window, we will always use its anim.
10549 if ((ws.mAttrs.flags&FLAG_COMPATIBLE_WINDOW) != 0) {
10550 animLp = ws.mAttrs;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010551 animToken = ws.mAppToken;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -070010552 bestAnimLayer = Integer.MAX_VALUE;
10553 } else if (ws.mLayer > bestAnimLayer) {
10554 animLp = ws.mAttrs;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010555 animToken = ws.mAppToken;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -070010556 bestAnimLayer = ws.mLayer;
10557 }
Dianne Hackborn25994b42009-09-04 14:21:19 -070010558 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -070010559 }
10560 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010561
Dianne Hackborn25994b42009-09-04 14:21:19 -070010562 if (foundWallpapers == 3) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010563 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -070010564 "Wallpaper animation!");
10565 switch (transit) {
10566 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
10567 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
10568 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
10569 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN;
10570 break;
10571 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
10572 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
10573 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
10574 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE;
10575 break;
10576 }
Joe Onorato8a9b2202010-02-26 18:56:32 -080010577 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -070010578 "New transit: " + transit);
10579 } else if (oldWallpaper != null) {
10580 // We are transitioning from an activity with
10581 // a wallpaper to one without.
10582 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010583 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -070010584 "New transit away from wallpaper: " + transit);
10585 } else if (mWallpaperTarget != null) {
10586 // We are transitioning from an activity without
10587 // a wallpaper to now showing the wallpaper
10588 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010589 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -070010590 "New transit into wallpaper: " + transit);
10591 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010592
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010593 if ((transit&WindowManagerPolicy.TRANSIT_ENTER_MASK) != 0) {
10594 mLastEnterAnimToken = animToken;
10595 mLastEnterAnimParams = animLp;
10596 } else if (mLastEnterAnimParams != null) {
10597 animLp = mLastEnterAnimParams;
10598 mLastEnterAnimToken = null;
10599 mLastEnterAnimParams = null;
10600 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010601
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010602 // If all closing windows are obscured, then there is
10603 // no need to do an animation. This is the case, for
10604 // example, when this transition is being done behind
10605 // the lock screen.
10606 if (!mPolicy.allowAppAnimationsLw()) {
10607 animLp = null;
10608 }
10609
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010610 NN = mOpeningApps.size();
10611 for (i=0; i<NN; i++) {
10612 AppWindowToken wtoken = mOpeningApps.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -080010613 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010614 "Now opening app" + wtoken);
10615 wtoken.reportedVisible = false;
10616 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -070010617 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -070010618 setTokenVisibilityLocked(wtoken, animLp, true, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010619 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -070010620 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010621 wtoken.showAllWindowsLocked();
10622 }
10623 NN = mClosingApps.size();
10624 for (i=0; i<NN; i++) {
10625 AppWindowToken wtoken = mClosingApps.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -080010626 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010627 "Now closing app" + wtoken);
10628 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -070010629 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -070010630 setTokenVisibilityLocked(wtoken, animLp, false, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010631 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -070010632 wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010633 // Force the allDrawn flag, because we want to start
10634 // this guy's animations regardless of whether it's
10635 // gotten drawn.
10636 wtoken.allDrawn = true;
10637 }
10638
Dianne Hackborn8b571a82009-09-25 16:09:43 -070010639 mNextAppTransitionPackage = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010640
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010641 mOpeningApps.clear();
10642 mClosingApps.clear();
10643
10644 // This has changed the visibility of windows, so perform
10645 // a new layout to get them all up-to-date.
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010646 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_LAYOUT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010647 mLayoutNeeded = true;
Dianne Hackborn20583ff2009-07-27 21:51:05 -070010648 if (!moveInputMethodWindowsIfNeededLocked(true)) {
10649 assignLayersLocked();
10650 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010651 updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES);
Dianne Hackbornb601ce12010-03-01 23:36:02 -080010652 mFocusMayChange = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010653 }
10654 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010655
Dianne Hackborn16064f92010-03-25 00:47:24 -070010656 int adjResult = 0;
10657
Dianne Hackborna8f60182009-09-01 19:01:50 -070010658 if (!animating && mAppTransitionRunning) {
10659 // We have finished the animation of an app transition. To do
10660 // this, we have delayed a lot of operations like showing and
10661 // hiding apps, moving apps in Z-order, etc. The app token list
10662 // reflects the correct Z-order, but the window list may now
10663 // be out of sync with it. So here we will just rebuild the
10664 // entire app window list. Fun!
10665 mAppTransitionRunning = false;
10666 // Clear information about apps that were moving.
10667 mToBottomApps.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010668
Dianne Hackborna8f60182009-09-01 19:01:50 -070010669 rebuildAppWindowListLocked();
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010670 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_LAYOUT;
Dianne Hackborn16064f92010-03-25 00:47:24 -070010671 adjResult |= ADJUST_WALLPAPER_LAYERS_CHANGED;
Dianne Hackborna8f60182009-09-01 19:01:50 -070010672 moveInputMethodWindowsIfNeededLocked(false);
10673 wallpaperMayChange = true;
Suchi Amalapurapuc9568e32009-11-05 18:51:16 -080010674 // Since the window list has been rebuilt, focus might
10675 // have to be recomputed since the actual order of windows
10676 // might have changed again.
Dianne Hackbornb601ce12010-03-01 23:36:02 -080010677 mFocusMayChange = true;
Dianne Hackborna8f60182009-09-01 19:01:50 -070010678 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010679
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010680 if (wallpaperForceHidingChanged && changes == 0 && !mAppTransitionReady) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010681 // At this point, there was a window with a wallpaper that
10682 // was force hiding other windows behind it, but now it
10683 // is going away. This may be simple -- just animate
10684 // away the wallpaper and its window -- or it may be
10685 // hard -- the wallpaper now needs to be shown behind
10686 // something that was hidden.
10687 WindowState oldWallpaper = mWallpaperTarget;
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010688 if (mLowerWallpaperTarget != null
10689 && mLowerWallpaperTarget.mAppToken != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010690 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010691 "wallpaperForceHiding changed with lower="
10692 + mLowerWallpaperTarget);
Joe Onorato8a9b2202010-02-26 18:56:32 -080010693 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010694 "hidden=" + mLowerWallpaperTarget.mAppToken.hidden +
10695 " hiddenRequested=" + mLowerWallpaperTarget.mAppToken.hiddenRequested);
10696 if (mLowerWallpaperTarget.mAppToken.hidden) {
10697 // The lower target has become hidden before we
10698 // actually started the animation... let's completely
10699 // re-evaluate everything.
10700 mLowerWallpaperTarget = mUpperWallpaperTarget = null;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010701 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_ANIM;
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010702 }
10703 }
Dianne Hackborn16064f92010-03-25 00:47:24 -070010704 adjResult |= adjustWallpaperWindowsLocked();
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010705 wallpaperMayChange = false;
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010706 wallpaperForceHidingChanged = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010707 if (DEBUG_WALLPAPER) Slog.v(TAG, "****** OLD: " + oldWallpaper
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010708 + " NEW: " + mWallpaperTarget
10709 + " LOWER: " + mLowerWallpaperTarget);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010710 if (mLowerWallpaperTarget == null) {
10711 // Whoops, we don't need a special wallpaper animation.
10712 // Clear them out.
10713 forceHiding = false;
10714 for (i=N-1; i>=0; i--) {
10715 WindowState w = (WindowState)mWindows.get(i);
10716 if (w.mSurface != null) {
10717 final WindowManager.LayoutParams attrs = w.mAttrs;
Suchi Amalapurapuc03d28b2009-10-28 14:32:05 -070010718 if (mPolicy.doesForceHide(w, attrs) && w.isVisibleLw()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010719 if (DEBUG_FOCUS) Slog.i(TAG, "win=" + w + " force hides other windows");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010720 forceHiding = true;
10721 } else if (mPolicy.canBeForceHidden(w, attrs)) {
10722 if (!w.mAnimating) {
10723 // We set the animation above so it
10724 // is not yet running.
10725 w.clearAnimation();
10726 }
10727 }
10728 }
10729 }
10730 }
10731 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010732
Dianne Hackborn6c3f5712009-08-25 18:42:59 -070010733 if (wallpaperMayChange) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010734 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010735 "Wallpaper may change! Adjusting");
Dianne Hackborn16064f92010-03-25 00:47:24 -070010736 adjResult |= adjustWallpaperWindowsLocked();
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010737 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010738
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010739 if ((adjResult&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010740 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010741 "Wallpaper layer changed: assigning layers + relayout");
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010742 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_LAYOUT;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010743 assignLayersLocked();
10744 } else if ((adjResult&ADJUST_WALLPAPER_VISIBILITY_CHANGED) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010745 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010746 "Wallpaper visibility changed: relayout");
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010747 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_LAYOUT;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010748 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010749
Dianne Hackbornb601ce12010-03-01 23:36:02 -080010750 if (mFocusMayChange) {
10751 mFocusMayChange = false;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010752 if (updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES)) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010753 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_ANIM;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010754 adjResult = 0;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -070010755 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010756 }
10757
10758 if (mLayoutNeeded) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010759 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_LAYOUT;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -070010760 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010761
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010762 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG, "*** ANIM STEP: changes=0x"
10763 + Integer.toHexString(changes));
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010764
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010765 } while (changes != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010766
10767 // THIRD LOOP: Update the surfaces of all windows.
10768
10769 final boolean someoneLosingFocus = mLosingFocus.size() != 0;
10770
10771 boolean obscured = false;
10772 boolean blurring = false;
10773 boolean dimming = false;
10774 boolean covered = false;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -070010775 boolean syswin = false;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010776 boolean backgroundFillerShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010777
Dianne Hackbornbdd52b22009-09-02 21:46:19 -070010778 final int N = mWindows.size();
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010779
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010780 for (i=N-1; i>=0; i--) {
10781 WindowState w = (WindowState)mWindows.get(i);
10782
10783 boolean displayed = false;
10784 final WindowManager.LayoutParams attrs = w.mAttrs;
10785 final int attrFlags = attrs.flags;
10786
10787 if (w.mSurface != null) {
Dianne Hackbornac3587d2010-03-11 11:12:11 -080010788 // XXX NOTE: The logic here could be improved. We have
10789 // the decision about whether to resize a window separated
10790 // from whether to hide the surface. This can cause us to
10791 // resize a surface even if we are going to hide it. You
10792 // can see this by (1) holding device in landscape mode on
10793 // home screen; (2) tapping browser icon (device will rotate
10794 // to landscape; (3) tap home. The wallpaper will be resized
10795 // in step 2 but then immediately hidden, causing us to
10796 // have to resize and then redraw it again in step 3. It
10797 // would be nice to figure out how to avoid this, but it is
10798 // difficult because we do need to resize surfaces in some
10799 // cases while they are hidden such as when first showing a
10800 // window.
10801
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010802 w.computeShownFrameLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -080010803 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010804 TAG, "Placing surface #" + i + " " + w.mSurface
10805 + ": new=" + w.mShownFrame + ", old="
10806 + w.mLastShownFrame);
10807
10808 boolean resize;
10809 int width, height;
10810 if ((w.mAttrs.flags & w.mAttrs.FLAG_SCALED) != 0) {
10811 resize = w.mLastRequestedWidth != w.mRequestedWidth ||
10812 w.mLastRequestedHeight != w.mRequestedHeight;
10813 // for a scaled surface, we just want to use
10814 // the requested size.
10815 width = w.mRequestedWidth;
10816 height = w.mRequestedHeight;
10817 w.mLastRequestedWidth = width;
10818 w.mLastRequestedHeight = height;
10819 w.mLastShownFrame.set(w.mShownFrame);
10820 try {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010821 if (SHOW_TRANSACTIONS) logSurface(w,
10822 "POS " + w.mShownFrame.left
10823 + ", " + w.mShownFrame.top, null);
Dianne Hackborn16064f92010-03-25 00:47:24 -070010824 w.mSurfaceX = w.mShownFrame.left;
10825 w.mSurfaceY = w.mShownFrame.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010826 w.mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
10827 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010828 Slog.w(TAG, "Error positioning surface in " + w, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010829 if (!recoveringMemory) {
10830 reclaimSomeSurfaceMemoryLocked(w, "position");
10831 }
10832 }
10833 } else {
10834 resize = !w.mLastShownFrame.equals(w.mShownFrame);
10835 width = w.mShownFrame.width();
10836 height = w.mShownFrame.height();
10837 w.mLastShownFrame.set(w.mShownFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010838 }
10839
10840 if (resize) {
10841 if (width < 1) width = 1;
10842 if (height < 1) height = 1;
10843 if (w.mSurface != null) {
10844 try {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010845 if (SHOW_TRANSACTIONS) logSurface(w,
10846 "POS " + w.mShownFrame.left + ","
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010847 + w.mShownFrame.top + " SIZE "
10848 + w.mShownFrame.width() + "x"
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010849 + w.mShownFrame.height(), null);
Dianne Hackbornac3587d2010-03-11 11:12:11 -080010850 w.mSurfaceResized = true;
Dianne Hackborn16064f92010-03-25 00:47:24 -070010851 w.mSurfaceW = width;
10852 w.mSurfaceH = height;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010853 w.mSurface.setSize(width, height);
Dianne Hackborn16064f92010-03-25 00:47:24 -070010854 w.mSurfaceX = w.mShownFrame.left;
10855 w.mSurfaceY = w.mShownFrame.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010856 w.mSurface.setPosition(w.mShownFrame.left,
10857 w.mShownFrame.top);
10858 } catch (RuntimeException e) {
10859 // If something goes wrong with the surface (such
10860 // as running out of memory), don't take down the
10861 // entire system.
Joe Onorato8a9b2202010-02-26 18:56:32 -080010862 Slog.e(TAG, "Failure updating surface of " + w
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010863 + "size=(" + width + "x" + height
10864 + "), pos=(" + w.mShownFrame.left
10865 + "," + w.mShownFrame.top + ")", e);
10866 if (!recoveringMemory) {
10867 reclaimSomeSurfaceMemoryLocked(w, "size");
10868 }
10869 }
10870 }
10871 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010872 if (!w.mAppFreezing && w.mLayoutSeq == mLayoutSeq) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010873 w.mContentInsetsChanged =
10874 !w.mLastContentInsets.equals(w.mContentInsets);
10875 w.mVisibleInsetsChanged =
10876 !w.mLastVisibleInsets.equals(w.mVisibleInsets);
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010877 boolean configChanged =
10878 w.mConfiguration != mCurConfiguration
10879 && (w.mConfiguration == null
10880 || mCurConfiguration.diff(w.mConfiguration) != 0);
Dianne Hackborn694f79b2010-03-17 19:44:59 -070010881 if (DEBUG_CONFIGURATION && configChanged) {
10882 Slog.v(TAG, "Win " + w + " config changed: "
10883 + mCurConfiguration);
10884 }
Joe Onorato8a9b2202010-02-26 18:56:32 -080010885 if (localLOGV) Slog.v(TAG, "Resizing " + w
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010886 + ": configChanged=" + configChanged
10887 + " last=" + w.mLastFrame + " frame=" + w.mFrame);
Romain Guy06882f82009-06-10 13:36:04 -070010888 if (!w.mLastFrame.equals(w.mFrame)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010889 || w.mContentInsetsChanged
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010890 || w.mVisibleInsetsChanged
Dianne Hackbornac3587d2010-03-11 11:12:11 -080010891 || w.mSurfaceResized
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010892 || configChanged) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010893 w.mLastFrame.set(w.mFrame);
10894 w.mLastContentInsets.set(w.mContentInsets);
10895 w.mLastVisibleInsets.set(w.mVisibleInsets);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010896 // If the screen is currently frozen, then keep
10897 // it frozen until this window draws at its new
10898 // orientation.
10899 if (mDisplayFrozen) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010900 if (DEBUG_ORIENTATION) Slog.v(TAG,
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010901 "Resizing while display frozen: " + w);
10902 w.mOrientationChanging = true;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010903 if (!mWindowsFreezingScreen) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010904 mWindowsFreezingScreen = true;
10905 // XXX should probably keep timeout from
10906 // when we first froze the display.
10907 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
10908 mH.sendMessageDelayed(mH.obtainMessage(
10909 H.WINDOW_FREEZE_TIMEOUT), 2000);
10910 }
10911 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010912 // If the orientation is changing, then we need to
10913 // hold off on unfreezing the display until this
10914 // window has been redrawn; to do that, we need
10915 // to go through the process of getting informed
10916 // by the application when it has finished drawing.
10917 if (w.mOrientationChanging) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010918 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010919 "Orientation start waiting for draw in "
10920 + w + ", surface " + w.mSurface);
10921 w.mDrawPending = true;
10922 w.mCommitDrawPending = false;
10923 w.mReadyToShow = false;
10924 if (w.mAppToken != null) {
10925 w.mAppToken.allDrawn = false;
10926 }
10927 }
Dianne Hackbornac3587d2010-03-11 11:12:11 -080010928 if (DEBUG_RESIZE || DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010929 "Resizing window " + w + " to " + w.mFrame);
10930 mResizingWindows.add(w);
10931 } else if (w.mOrientationChanging) {
10932 if (!w.mDrawPending && !w.mCommitDrawPending) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010933 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010934 "Orientation not waiting for draw in "
10935 + w + ", surface " + w.mSurface);
10936 w.mOrientationChanging = false;
10937 }
10938 }
10939 }
10940
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010941 if (w.mAttachedHidden || !w.isReadyForDisplay()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010942 if (!w.mLastHidden) {
10943 //dump();
Dianne Hackborn5943c202010-04-12 21:36:49 -070010944 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Window hiding: waitingToShow="
10945 + w.mRootToken.waitingToShow + " polvis="
10946 + w.mPolicyVisibility + " atthid="
10947 + w.mAttachedHidden + " tokhid="
10948 + w.mRootToken.hidden + " vis="
10949 + w.mViewVisibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010950 w.mLastHidden = true;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010951 if (SHOW_TRANSACTIONS) logSurface(w,
10952 "HIDE (performLayout)", null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010953 if (w.mSurface != null) {
Dianne Hackborn16064f92010-03-25 00:47:24 -070010954 w.mSurfaceShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010955 try {
10956 w.mSurface.hide();
10957 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010958 Slog.w(TAG, "Exception hiding surface in " + w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010959 }
10960 }
10961 mKeyWaiter.releasePendingPointerLocked(w.mSession);
10962 }
10963 // If we are waiting for this window to handle an
10964 // orientation change, well, it is hidden, so
10965 // doesn't really matter. Note that this does
10966 // introduce a potential glitch if the window
10967 // becomes unhidden before it has drawn for the
10968 // new orientation.
10969 if (w.mOrientationChanging) {
10970 w.mOrientationChanging = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010971 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010972 "Orientation change skips hidden " + w);
10973 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010974 } else if (w.mLastLayer != w.mAnimLayer
10975 || w.mLastAlpha != w.mShownAlpha
10976 || w.mLastDsDx != w.mDsDx
10977 || w.mLastDtDx != w.mDtDx
10978 || w.mLastDsDy != w.mDsDy
10979 || w.mLastDtDy != w.mDtDy
10980 || w.mLastHScale != w.mHScale
10981 || w.mLastVScale != w.mVScale
10982 || w.mLastHidden) {
10983 displayed = true;
10984 w.mLastAlpha = w.mShownAlpha;
10985 w.mLastLayer = w.mAnimLayer;
10986 w.mLastDsDx = w.mDsDx;
10987 w.mLastDtDx = w.mDtDx;
10988 w.mLastDsDy = w.mDsDy;
10989 w.mLastDtDy = w.mDtDy;
10990 w.mLastHScale = w.mHScale;
10991 w.mLastVScale = w.mVScale;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010992 if (SHOW_TRANSACTIONS) logSurface(w,
10993 "alpha=" + w.mShownAlpha + " layer=" + w.mAnimLayer
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010994 + " matrix=[" + (w.mDsDx*w.mHScale)
10995 + "," + (w.mDtDx*w.mVScale)
10996 + "][" + (w.mDsDy*w.mHScale)
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010997 + "," + (w.mDtDy*w.mVScale) + "]", null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010998 if (w.mSurface != null) {
10999 try {
Dianne Hackborn16064f92010-03-25 00:47:24 -070011000 w.mSurfaceAlpha = w.mShownAlpha;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011001 w.mSurface.setAlpha(w.mShownAlpha);
Dianne Hackborn16064f92010-03-25 00:47:24 -070011002 w.mSurfaceLayer = w.mAnimLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011003 w.mSurface.setLayer(w.mAnimLayer);
11004 w.mSurface.setMatrix(
11005 w.mDsDx*w.mHScale, w.mDtDx*w.mVScale,
11006 w.mDsDy*w.mHScale, w.mDtDy*w.mVScale);
11007 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011008 Slog.w(TAG, "Error updating surface in " + w, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011009 if (!recoveringMemory) {
11010 reclaimSomeSurfaceMemoryLocked(w, "update");
11011 }
11012 }
11013 }
11014
11015 if (w.mLastHidden && !w.mDrawPending
11016 && !w.mCommitDrawPending
11017 && !w.mReadyToShow) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080011018 if (SHOW_TRANSACTIONS) logSurface(w,
11019 "SHOW (performLayout)", null);
Joe Onorato8a9b2202010-02-26 18:56:32 -080011020 if (DEBUG_VISIBILITY) Slog.v(TAG, "Showing " + w
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011021 + " during relayout");
11022 if (showSurfaceRobustlyLocked(w)) {
11023 w.mHasDrawn = true;
11024 w.mLastHidden = false;
11025 } else {
11026 w.mOrientationChanging = false;
11027 }
11028 }
11029 if (w.mSurface != null) {
11030 w.mToken.hasVisible = true;
11031 }
11032 } else {
11033 displayed = true;
11034 }
11035
11036 if (displayed) {
11037 if (!covered) {
Romain Guy980a9382010-01-08 15:06:28 -080011038 if (attrs.width == LayoutParams.MATCH_PARENT
11039 && attrs.height == LayoutParams.MATCH_PARENT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011040 covered = true;
11041 }
11042 }
11043 if (w.mOrientationChanging) {
11044 if (w.mDrawPending || w.mCommitDrawPending) {
11045 orientationChangeComplete = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -080011046 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011047 "Orientation continue waiting for draw in " + w);
11048 } else {
11049 w.mOrientationChanging = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -080011050 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011051 "Orientation change complete in " + w);
11052 }
11053 }
11054 w.mToken.hasVisible = true;
11055 }
11056 } else if (w.mOrientationChanging) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011057 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011058 "Orientation change skips hidden " + w);
11059 w.mOrientationChanging = false;
11060 }
11061
11062 final boolean canBeSeen = w.isDisplayedLw();
11063
11064 if (someoneLosingFocus && w == mCurrentFocus && canBeSeen) {
11065 focusDisplayed = true;
11066 }
11067
Dianne Hackborne9e9bca2009-08-18 15:08:22 -070011068 final boolean obscuredChanged = w.mObscured != obscured;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080011069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011070 // Update effect.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070011071 if (!(w.mObscured=obscured)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011072 if (w.mSurface != null) {
11073 if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
11074 holdScreen = w.mSession;
11075 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -070011076 if (!syswin && w.mAttrs.screenBrightness >= 0
11077 && screenBrightness < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011078 screenBrightness = w.mAttrs.screenBrightness;
11079 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -050011080 if (!syswin && w.mAttrs.buttonBrightness >= 0
11081 && buttonBrightness < 0) {
11082 buttonBrightness = w.mAttrs.buttonBrightness;
11083 }
Mike Lockwood46af6a82010-03-09 08:28:22 -050011084 if (canBeSeen
11085 && (attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG
11086 || attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD
11087 || attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_ERROR)) {
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -070011088 syswin = true;
11089 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011090 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070011091
Dianne Hackborn25994b42009-09-04 14:21:19 -070011092 boolean opaqueDrawn = canBeSeen && w.isOpaqueDrawn();
11093 if (opaqueDrawn && w.isFullscreen(dw, dh)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011094 // This window completely covers everything behind it,
11095 // so we want to leave all of them as unblurred (for
11096 // performance reasons).
11097 obscured = true;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070011098 } else if (opaqueDrawn && w.needsBackgroundFiller(dw, dh)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011099 if (SHOW_TRANSACTIONS) Slog.d(TAG, "showing background filler");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011100 // This window is in compatibility mode, and needs background filler.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070011101 obscured = true;
11102 if (mBackgroundFillerSurface == null) {
11103 try {
11104 mBackgroundFillerSurface = new Surface(mFxSession, 0,
Mathias Agopian5d26c1e2010-03-01 16:09:43 -080011105 "BackGroundFiller",
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070011106 0, dw, dh,
11107 PixelFormat.OPAQUE,
11108 Surface.FX_SURFACE_NORMAL);
11109 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011110 Slog.e(TAG, "Exception creating filler surface", e);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070011111 }
11112 }
11113 try {
11114 mBackgroundFillerSurface.setPosition(0, 0);
11115 mBackgroundFillerSurface.setSize(dw, dh);
11116 // Using the same layer as Dim because they will never be shown at the
11117 // same time.
11118 mBackgroundFillerSurface.setLayer(w.mAnimLayer - 1);
11119 mBackgroundFillerSurface.show();
11120 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011121 Slog.e(TAG, "Exception showing filler surface");
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070011122 }
11123 backgroundFillerShown = true;
11124 mBackgroundFillerShown = true;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070011125 } else if (canBeSeen && !obscured &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011126 (attrFlags&FLAG_BLUR_BEHIND|FLAG_DIM_BEHIND) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011127 if (localLOGV) Slog.v(TAG, "Win " + w
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011128 + ": blurring=" + blurring
11129 + " obscured=" + obscured
11130 + " displayed=" + displayed);
11131 if ((attrFlags&FLAG_DIM_BEHIND) != 0) {
11132 if (!dimming) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011133 //Slog.i(TAG, "DIM BEHIND: " + w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011134 dimming = true;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011135 if (mDimAnimator == null) {
11136 mDimAnimator = new DimAnimator(mFxSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011137 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011138 mDimAnimator.show(dw, dh);
Dianne Hackborn16064f92010-03-25 00:47:24 -070011139 mDimAnimator.updateParameters(w, currentTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011140 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011141 }
11142 if ((attrFlags&FLAG_BLUR_BEHIND) != 0) {
11143 if (!blurring) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011144 //Slog.i(TAG, "BLUR BEHIND: " + w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011145 blurring = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011146 if (mBlurSurface == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011147 if (SHOW_TRANSACTIONS) Slog.i(TAG, " BLUR "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011148 + mBlurSurface + ": CREATE");
11149 try {
Romain Guy06882f82009-06-10 13:36:04 -070011150 mBlurSurface = new Surface(mFxSession, 0,
Mathias Agopian5d26c1e2010-03-01 16:09:43 -080011151 "BlurSurface",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011152 -1, 16, 16,
11153 PixelFormat.OPAQUE,
11154 Surface.FX_SURFACE_BLUR);
11155 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011156 Slog.e(TAG, "Exception creating Blur surface", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011157 }
11158 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011159 if (mBlurSurface != null) {
Dianne Hackborn16064f92010-03-25 00:47:24 -070011160 if (SHOW_TRANSACTIONS) Slog.i(TAG, " BLUR "
11161 + mBlurSurface + ": pos=(0,0) (" +
11162 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011163 mBlurSurface.setPosition(0, 0);
11164 mBlurSurface.setSize(dw, dh);
Dianne Hackborn16064f92010-03-25 00:47:24 -070011165 mBlurSurface.setLayer(w.mAnimLayer-2);
11166 if (!mBlurShown) {
11167 try {
11168 if (SHOW_TRANSACTIONS) Slog.i(TAG, " BLUR "
11169 + mBlurSurface + ": SHOW");
11170 mBlurSurface.show();
11171 } catch (RuntimeException e) {
11172 Slog.w(TAG, "Failure showing blur surface", e);
11173 }
11174 mBlurShown = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011175 }
11176 }
11177 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011178 }
11179 }
11180 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080011181
Dianne Hackborne9e9bca2009-08-18 15:08:22 -070011182 if (obscuredChanged && mWallpaperTarget == w) {
11183 // This is the wallpaper target and its obscured state
11184 // changed... make sure the current wallaper's visibility
11185 // has been updated accordingly.
11186 updateWallpaperVisibilityLocked();
11187 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011188 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080011189
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070011190 if (backgroundFillerShown == false && mBackgroundFillerShown) {
11191 mBackgroundFillerShown = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -080011192 if (SHOW_TRANSACTIONS) Slog.d(TAG, "hiding background filler");
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070011193 try {
11194 mBackgroundFillerSurface.hide();
11195 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011196 Slog.e(TAG, "Exception hiding filler surface", e);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070011197 }
11198 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011199
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011200 if (mDimAnimator != null && mDimAnimator.mDimShown) {
Dianne Hackbornde2606d2009-12-18 16:53:55 -080011201 animating |= mDimAnimator.updateSurface(dimming, currentTime,
11202 mDisplayFrozen || !mPolicy.isScreenOn());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011203 }
Romain Guy06882f82009-06-10 13:36:04 -070011204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011205 if (!blurring && mBlurShown) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011206 if (SHOW_TRANSACTIONS) Slog.i(TAG, " BLUR " + mBlurSurface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011207 + ": HIDE");
11208 try {
11209 mBlurSurface.hide();
11210 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011211 Slog.w(TAG, "Illegal argument exception hiding blur surface");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011212 }
11213 mBlurShown = false;
11214 }
11215
Joe Onorato8a9b2202010-02-26 18:56:32 -080011216 if (SHOW_TRANSACTIONS) Slog.i(TAG, "<<< CLOSE TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011217 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011218 Slog.e(TAG, "Unhandled exception in Window Manager", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011219 }
11220
11221 Surface.closeTransaction();
Romain Guy06882f82009-06-10 13:36:04 -070011222
Joe Onorato8a9b2202010-02-26 18:56:32 -080011223 if (DEBUG_ORIENTATION && mDisplayFrozen) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011224 "With display frozen, orientationChangeComplete="
11225 + orientationChangeComplete);
11226 if (orientationChangeComplete) {
11227 if (mWindowsFreezingScreen) {
11228 mWindowsFreezingScreen = false;
11229 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
11230 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -080011231 stopFreezingDisplayLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011232 }
Romain Guy06882f82009-06-10 13:36:04 -070011233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011234 i = mResizingWindows.size();
11235 if (i > 0) {
11236 do {
11237 i--;
11238 WindowState win = mResizingWindows.get(i);
11239 try {
Dianne Hackbornac3587d2010-03-11 11:12:11 -080011240 if (DEBUG_RESIZE || DEBUG_ORIENTATION) Slog.v(TAG,
11241 "Reporting new frame to " + win + ": " + win.mFrame);
Dianne Hackborn694f79b2010-03-17 19:44:59 -070011242 int diff = 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080011243 boolean configChanged =
11244 win.mConfiguration != mCurConfiguration
11245 && (win.mConfiguration == null
Dianne Hackborn694f79b2010-03-17 19:44:59 -070011246 || (diff=mCurConfiguration.diff(win.mConfiguration)) != 0);
11247 if ((DEBUG_RESIZE || DEBUG_ORIENTATION || DEBUG_CONFIGURATION)
11248 && configChanged) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011249 Slog.i(TAG, "Sending new config to window " + win + ": "
Dianne Hackborne36d6e22010-02-17 19:46:25 -080011250 + win.mFrame.width() + "x" + win.mFrame.height()
Dianne Hackborn694f79b2010-03-17 19:44:59 -070011251 + " / " + mCurConfiguration + " / 0x"
11252 + Integer.toHexString(diff));
Dianne Hackborne36d6e22010-02-17 19:46:25 -080011253 }
Dianne Hackborn694f79b2010-03-17 19:44:59 -070011254 win.mConfiguration = mCurConfiguration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011255 win.mClient.resized(win.mFrame.width(),
11256 win.mFrame.height(), win.mLastContentInsets,
Dianne Hackborne36d6e22010-02-17 19:46:25 -080011257 win.mLastVisibleInsets, win.mDrawPending,
11258 configChanged ? win.mConfiguration : null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011259 win.mContentInsetsChanged = false;
11260 win.mVisibleInsetsChanged = false;
Dianne Hackbornac3587d2010-03-11 11:12:11 -080011261 win.mSurfaceResized = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011262 } catch (RemoteException e) {
11263 win.mOrientationChanging = false;
11264 }
11265 } while (i > 0);
11266 mResizingWindows.clear();
11267 }
Romain Guy06882f82009-06-10 13:36:04 -070011268
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011269 // Destroy the surface of any windows that are no longer visible.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070011270 boolean wallpaperDestroyed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011271 i = mDestroySurface.size();
11272 if (i > 0) {
11273 do {
11274 i--;
11275 WindowState win = mDestroySurface.get(i);
11276 win.mDestroying = false;
11277 if (mInputMethodWindow == win) {
11278 mInputMethodWindow = null;
11279 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070011280 if (win == mWallpaperTarget) {
11281 wallpaperDestroyed = true;
11282 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011283 win.destroySurfaceLocked();
11284 } while (i > 0);
11285 mDestroySurface.clear();
11286 }
11287
11288 // Time to remove any exiting tokens?
11289 for (i=mExitingTokens.size()-1; i>=0; i--) {
11290 WindowToken token = mExitingTokens.get(i);
11291 if (!token.hasVisible) {
11292 mExitingTokens.remove(i);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070011293 if (token.windowType == TYPE_WALLPAPER) {
11294 mWallpaperTokens.remove(token);
11295 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011296 }
11297 }
11298
11299 // Time to remove any exiting applications?
11300 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
11301 AppWindowToken token = mExitingAppTokens.get(i);
11302 if (!token.hasVisible && !mClosingApps.contains(token)) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -070011303 // Make sure there is no animation running on this token,
11304 // so any windows associated with it will be removed as
11305 // soon as their animations are complete
11306 token.animation = null;
11307 token.animating = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011308 mAppTokens.remove(token);
11309 mExitingAppTokens.remove(i);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070011310 if (mLastEnterAnimToken == token) {
11311 mLastEnterAnimToken = null;
11312 mLastEnterAnimParams = null;
11313 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011314 }
11315 }
11316
Dianne Hackborna8f60182009-09-01 19:01:50 -070011317 boolean needRelayout = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080011318
Dianne Hackborna8f60182009-09-01 19:01:50 -070011319 if (!animating && mAppTransitionRunning) {
11320 // We have finished the animation of an app transition. To do
11321 // this, we have delayed a lot of operations like showing and
11322 // hiding apps, moving apps in Z-order, etc. The app token list
11323 // reflects the correct Z-order, but the window list may now
11324 // be out of sync with it. So here we will just rebuild the
11325 // entire app window list. Fun!
11326 mAppTransitionRunning = false;
11327 needRelayout = true;
11328 rebuildAppWindowListLocked();
Dianne Hackborn16064f92010-03-25 00:47:24 -070011329 assignLayersLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -070011330 // Clear information about apps that were moving.
11331 mToBottomApps.clear();
11332 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080011333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011334 if (focusDisplayed) {
11335 mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
11336 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070011337 if (wallpaperDestroyed) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070011338 needRelayout = adjustWallpaperWindowsLocked() != 0;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070011339 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070011340 if (needRelayout) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070011341 requestAnimationLocked(0);
11342 } else if (animating) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011343 requestAnimationLocked(currentTime+(1000/60)-SystemClock.uptimeMillis());
11344 }
Dianne Hackbornce73c1e2010-04-12 23:11:38 -070011345
11346 if (DEBUG_FREEZE) Slog.v(TAG, "Layout: mDisplayFrozen=" + mDisplayFrozen
11347 + " holdScreen=" + holdScreen);
11348 if (!mDisplayFrozen) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070011349 setHoldScreenLocked(holdScreen != null);
Dianne Hackbornce73c1e2010-04-12 23:11:38 -070011350 if (screenBrightness < 0 || screenBrightness > 1.0f) {
11351 mPowerManager.setScreenBrightnessOverride(-1);
11352 } else {
11353 mPowerManager.setScreenBrightnessOverride((int)
11354 (screenBrightness * Power.BRIGHTNESS_ON));
11355 }
11356 if (buttonBrightness < 0 || buttonBrightness > 1.0f) {
11357 mPowerManager.setButtonBrightnessOverride(-1);
11358 } else {
11359 mPowerManager.setButtonBrightnessOverride((int)
11360 (buttonBrightness * Power.BRIGHTNESS_ON));
11361 }
11362 if (holdScreen != mHoldingScreenOn) {
11363 mHoldingScreenOn = holdScreen;
11364 Message m = mH.obtainMessage(H.HOLD_SCREEN_CHANGED, holdScreen);
11365 mH.sendMessage(m);
11366 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011367 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080011368
Dianne Hackborn93e462b2009-09-15 22:50:40 -070011369 if (mTurnOnScreen) {
Dianne Hackbornb601ce12010-03-01 23:36:02 -080011370 if (DEBUG_VISIBILITY) Slog.v(TAG, "Turning screen on after layout!");
Dianne Hackborn93e462b2009-09-15 22:50:40 -070011371 mPowerManager.userActivity(SystemClock.uptimeMillis(), false,
11372 LocalPowerManager.BUTTON_EVENT, true);
11373 mTurnOnScreen = false;
11374 }
Dianne Hackbornf3bea9c2009-12-09 18:26:21 -080011375
11376 // Check to see if we are now in a state where the screen should
11377 // be enabled, because the window obscured flags have changed.
11378 enableScreenIfNeededLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011379 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070011380
11381 /**
11382 * Must be called with the main window manager lock held.
11383 */
11384 void setHoldScreenLocked(boolean holding) {
11385 boolean state = mHoldingScreenWakeLock.isHeld();
11386 if (holding != state) {
11387 if (holding) {
11388 mHoldingScreenWakeLock.acquire();
11389 } else {
11390 mPolicy.screenOnStoppedLw();
11391 mHoldingScreenWakeLock.release();
11392 }
11393 }
11394 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011395
11396 void requestAnimationLocked(long delay) {
11397 if (!mAnimationPending) {
11398 mAnimationPending = true;
11399 mH.sendMessageDelayed(mH.obtainMessage(H.ANIMATE), delay);
11400 }
11401 }
Romain Guy06882f82009-06-10 13:36:04 -070011402
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011403 /**
11404 * Have the surface flinger show a surface, robustly dealing with
11405 * error conditions. In particular, if there is not enough memory
11406 * to show the surface, then we will try to get rid of other surfaces
11407 * in order to succeed.
Romain Guy06882f82009-06-10 13:36:04 -070011408 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011409 * @return Returns true if the surface was successfully shown.
11410 */
11411 boolean showSurfaceRobustlyLocked(WindowState win) {
11412 try {
11413 if (win.mSurface != null) {
Dianne Hackborn16064f92010-03-25 00:47:24 -070011414 win.mSurfaceShown = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011415 win.mSurface.show();
Dianne Hackborn93e462b2009-09-15 22:50:40 -070011416 if (win.mTurnOnScreen) {
Dianne Hackbornb601ce12010-03-01 23:36:02 -080011417 if (DEBUG_VISIBILITY) Slog.v(TAG,
11418 "Show surface turning screen on: " + win);
Dianne Hackborn93e462b2009-09-15 22:50:40 -070011419 win.mTurnOnScreen = false;
11420 mTurnOnScreen = true;
11421 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011422 }
11423 return true;
11424 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011425 Slog.w(TAG, "Failure showing surface " + win.mSurface + " in " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011426 }
Romain Guy06882f82009-06-10 13:36:04 -070011427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011428 reclaimSomeSurfaceMemoryLocked(win, "show");
Romain Guy06882f82009-06-10 13:36:04 -070011429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011430 return false;
11431 }
Romain Guy06882f82009-06-10 13:36:04 -070011432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011433 void reclaimSomeSurfaceMemoryLocked(WindowState win, String operation) {
11434 final Surface surface = win.mSurface;
Romain Guy06882f82009-06-10 13:36:04 -070011435
Doug Zongkerab5c49c2009-12-04 10:31:43 -080011436 EventLog.writeEvent(EventLogTags.WM_NO_SURFACE_MEMORY, win.toString(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011437 win.mSession.mPid, operation);
Romain Guy06882f82009-06-10 13:36:04 -070011438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011439 if (mForceRemoves == null) {
11440 mForceRemoves = new ArrayList<WindowState>();
11441 }
Romain Guy06882f82009-06-10 13:36:04 -070011442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011443 long callingIdentity = Binder.clearCallingIdentity();
11444 try {
11445 // There was some problem... first, do a sanity check of the
11446 // window list to make sure we haven't left any dangling surfaces
11447 // around.
11448 int N = mWindows.size();
11449 boolean leakedSurface = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -080011450 Slog.i(TAG, "Out of memory for surface! Looking for leaks...");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011451 for (int i=0; i<N; i++) {
11452 WindowState ws = (WindowState)mWindows.get(i);
11453 if (ws.mSurface != null) {
11454 if (!mSessions.contains(ws.mSession)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011455 Slog.w(TAG, "LEAKED SURFACE (session doesn't exist): "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011456 + ws + " surface=" + ws.mSurface
11457 + " token=" + win.mToken
11458 + " pid=" + ws.mSession.mPid
11459 + " uid=" + ws.mSession.mUid);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070011460 ws.mSurface.destroy();
Dianne Hackborn16064f92010-03-25 00:47:24 -070011461 ws.mSurfaceShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011462 ws.mSurface = null;
11463 mForceRemoves.add(ws);
11464 i--;
11465 N--;
11466 leakedSurface = true;
11467 } else if (win.mAppToken != null && win.mAppToken.clientHidden) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011468 Slog.w(TAG, "LEAKED SURFACE (app token hidden): "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011469 + ws + " surface=" + ws.mSurface
11470 + " token=" + win.mAppToken);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070011471 ws.mSurface.destroy();
Dianne Hackborn16064f92010-03-25 00:47:24 -070011472 ws.mSurfaceShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011473 ws.mSurface = null;
11474 leakedSurface = true;
11475 }
11476 }
11477 }
Romain Guy06882f82009-06-10 13:36:04 -070011478
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011479 boolean killedApps = false;
11480 if (!leakedSurface) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011481 Slog.w(TAG, "No leaked surfaces; killing applicatons!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011482 SparseIntArray pidCandidates = new SparseIntArray();
11483 for (int i=0; i<N; i++) {
11484 WindowState ws = (WindowState)mWindows.get(i);
11485 if (ws.mSurface != null) {
11486 pidCandidates.append(ws.mSession.mPid, ws.mSession.mPid);
11487 }
11488 }
11489 if (pidCandidates.size() > 0) {
11490 int[] pids = new int[pidCandidates.size()];
11491 for (int i=0; i<pids.length; i++) {
11492 pids[i] = pidCandidates.keyAt(i);
11493 }
11494 try {
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -070011495 if (mActivityManager.killPids(pids, "Free memory")) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011496 killedApps = true;
11497 }
11498 } catch (RemoteException e) {
11499 }
11500 }
11501 }
Romain Guy06882f82009-06-10 13:36:04 -070011502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011503 if (leakedSurface || killedApps) {
11504 // We managed to reclaim some memory, so get rid of the trouble
11505 // surface and ask the app to request another one.
Joe Onorato8a9b2202010-02-26 18:56:32 -080011506 Slog.w(TAG, "Looks like we have reclaimed some memory, clearing surface for retry.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011507 if (surface != null) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070011508 surface.destroy();
Dianne Hackborn16064f92010-03-25 00:47:24 -070011509 win.mSurfaceShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011510 win.mSurface = null;
11511 }
Romain Guy06882f82009-06-10 13:36:04 -070011512
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011513 try {
11514 win.mClient.dispatchGetNewSurface();
11515 } catch (RemoteException e) {
11516 }
11517 }
11518 } finally {
11519 Binder.restoreCallingIdentity(callingIdentity);
11520 }
11521 }
Romain Guy06882f82009-06-10 13:36:04 -070011522
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011523 private boolean updateFocusedWindowLocked(int mode) {
11524 WindowState newFocus = computeFocusedWindowLocked();
11525 if (mCurrentFocus != newFocus) {
11526 // This check makes sure that we don't already have the focus
11527 // change message pending.
11528 mH.removeMessages(H.REPORT_FOCUS_CHANGE);
11529 mH.sendEmptyMessage(H.REPORT_FOCUS_CHANGE);
Joe Onorato8a9b2202010-02-26 18:56:32 -080011530 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011531 TAG, "Changing focus from " + mCurrentFocus + " to " + newFocus);
11532 final WindowState oldFocus = mCurrentFocus;
11533 mCurrentFocus = newFocus;
11534 mLosingFocus.remove(newFocus);
Romain Guy06882f82009-06-10 13:36:04 -070011535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011536 final WindowState imWindow = mInputMethodWindow;
11537 if (newFocus != imWindow && oldFocus != imWindow) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080011538 if (moveInputMethodWindowsIfNeededLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011539 mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS &&
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080011540 mode != UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
11541 mLayoutNeeded = true;
11542 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011543 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
11544 performLayoutLockedInner();
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080011545 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
11546 // Client will do the layout, but we need to assign layers
11547 // for handleNewWindowLocked() below.
11548 assignLayersLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011549 }
11550 }
Romain Guy06882f82009-06-10 13:36:04 -070011551
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080011552 if (newFocus != null && mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
11553 mKeyWaiter.handleNewWindowLocked(newFocus);
11554 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011555 return true;
11556 }
11557 return false;
11558 }
11559
11560 private WindowState computeFocusedWindowLocked() {
11561 WindowState result = null;
11562 WindowState win;
11563
11564 int i = mWindows.size() - 1;
11565 int nextAppIndex = mAppTokens.size()-1;
11566 WindowToken nextApp = nextAppIndex >= 0
11567 ? mAppTokens.get(nextAppIndex) : null;
11568
11569 while (i >= 0) {
11570 win = (WindowState)mWindows.get(i);
11571
Joe Onorato8a9b2202010-02-26 18:56:32 -080011572 if (localLOGV || DEBUG_FOCUS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011573 TAG, "Looking for focus: " + i
11574 + " = " + win
11575 + ", flags=" + win.mAttrs.flags
11576 + ", canReceive=" + win.canReceiveKeys());
11577
11578 AppWindowToken thisApp = win.mAppToken;
Romain Guy06882f82009-06-10 13:36:04 -070011579
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011580 // If this window's application has been removed, just skip it.
11581 if (thisApp != null && thisApp.removed) {
11582 i--;
11583 continue;
11584 }
Romain Guy06882f82009-06-10 13:36:04 -070011585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011586 // If there is a focused app, don't allow focus to go to any
11587 // windows below it. If this is an application window, step
11588 // through the app tokens until we find its app.
11589 if (thisApp != null && nextApp != null && thisApp != nextApp
11590 && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
11591 int origAppIndex = nextAppIndex;
11592 while (nextAppIndex > 0) {
11593 if (nextApp == mFocusedApp) {
11594 // Whoops, we are below the focused app... no focus
11595 // for you!
Joe Onorato8a9b2202010-02-26 18:56:32 -080011596 if (localLOGV || DEBUG_FOCUS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011597 TAG, "Reached focused app: " + mFocusedApp);
11598 return null;
11599 }
11600 nextAppIndex--;
11601 nextApp = mAppTokens.get(nextAppIndex);
11602 if (nextApp == thisApp) {
11603 break;
11604 }
11605 }
11606 if (thisApp != nextApp) {
11607 // Uh oh, the app token doesn't exist! This shouldn't
11608 // happen, but if it does we can get totally hosed...
11609 // so restart at the original app.
11610 nextAppIndex = origAppIndex;
11611 nextApp = mAppTokens.get(nextAppIndex);
11612 }
11613 }
11614
11615 // Dispatch to this window if it is wants key events.
11616 if (win.canReceiveKeys()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011617 if (DEBUG_FOCUS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011618 TAG, "Found focus @ " + i + " = " + win);
11619 result = win;
11620 break;
11621 }
11622
11623 i--;
11624 }
11625
11626 return result;
11627 }
11628
11629 private void startFreezingDisplayLocked() {
11630 if (mDisplayFrozen) {
Chris Tate2ad63a92009-03-25 17:36:48 -070011631 // Freezing the display also suspends key event delivery, to
11632 // keep events from going astray while the display is reconfigured.
11633 // If someone has changed orientation again while the screen is
11634 // still frozen, the events will continue to be blocked while the
11635 // successive orientation change is processed. To prevent spurious
11636 // ANRs, we reset the event dispatch timeout in this case.
11637 synchronized (mKeyWaiter) {
11638 mKeyWaiter.mWasFrozen = true;
11639 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011640 return;
11641 }
Romain Guy06882f82009-06-10 13:36:04 -070011642
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011643 mScreenFrozenLock.acquire();
Romain Guy06882f82009-06-10 13:36:04 -070011644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011645 long now = SystemClock.uptimeMillis();
Joe Onorato8a9b2202010-02-26 18:56:32 -080011646 //Slog.i(TAG, "Freezing, gc pending: " + mFreezeGcPending + ", now " + now);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011647 if (mFreezeGcPending != 0) {
11648 if (now > (mFreezeGcPending+1000)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011649 //Slog.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011650 mH.removeMessages(H.FORCE_GC);
11651 Runtime.getRuntime().gc();
11652 mFreezeGcPending = now;
11653 }
11654 } else {
11655 mFreezeGcPending = now;
11656 }
Romain Guy06882f82009-06-10 13:36:04 -070011657
Dianne Hackbornce73c1e2010-04-12 23:11:38 -070011658 if (DEBUG_FREEZE) Slog.v(TAG, "*** FREEZING DISPLAY", new RuntimeException());
11659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011660 mDisplayFrozen = true;
Dianne Hackbornbfe319e2009-09-21 00:34:05 -070011661 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
11662 mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070011663 mNextAppTransitionPackage = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011664 mAppTransitionReady = true;
11665 }
Romain Guy06882f82009-06-10 13:36:04 -070011666
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011667 if (PROFILE_ORIENTATION) {
11668 File file = new File("/data/system/frozen");
11669 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
11670 }
11671 Surface.freezeDisplay(0);
11672 }
Romain Guy06882f82009-06-10 13:36:04 -070011673
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011674 private void stopFreezingDisplayLocked() {
11675 if (!mDisplayFrozen) {
11676 return;
11677 }
Romain Guy06882f82009-06-10 13:36:04 -070011678
Dianne Hackborne36d6e22010-02-17 19:46:25 -080011679 if (mWaitingForConfig || mAppsFreezingScreen > 0 || mWindowsFreezingScreen) {
11680 return;
11681 }
11682
Dianne Hackbornce73c1e2010-04-12 23:11:38 -070011683 if (DEBUG_FREEZE) Slog.v(TAG, "*** UNFREEZING DISPLAY", new RuntimeException());
11684
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011685 mDisplayFrozen = false;
11686 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
11687 if (PROFILE_ORIENTATION) {
11688 Debug.stopMethodTracing();
11689 }
11690 Surface.unfreezeDisplay(0);
Romain Guy06882f82009-06-10 13:36:04 -070011691
Chris Tate2ad63a92009-03-25 17:36:48 -070011692 // Reset the key delivery timeout on unfreeze, too. We force a wakeup here
11693 // too because regular key delivery processing should resume immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011694 synchronized (mKeyWaiter) {
11695 mKeyWaiter.mWasFrozen = true;
11696 mKeyWaiter.notifyAll();
11697 }
11698
Christopher Tateb696aee2010-04-02 19:08:30 -070011699 // While the display is frozen we don't re-compute the orientation
11700 // to avoid inconsistent states. However, something interesting
11701 // could have actually changed during that time so re-evaluate it
11702 // now to catch that.
11703 if (updateOrientationFromAppTokensLocked()) {
11704 mH.sendEmptyMessage(H.SEND_NEW_CONFIGURATION);
11705 }
11706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011707 // A little kludge: a lot could have happened while the
11708 // display was frozen, so now that we are coming back we
11709 // do a gc so that any remote references the system
11710 // processes holds on others can be released if they are
11711 // no longer needed.
11712 mH.removeMessages(H.FORCE_GC);
11713 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
11714 2000);
Romain Guy06882f82009-06-10 13:36:04 -070011715
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011716 mScreenFrozenLock.release();
11717 }
Romain Guy06882f82009-06-10 13:36:04 -070011718
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011719 @Override
11720 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
11721 if (mContext.checkCallingOrSelfPermission("android.permission.DUMP")
11722 != PackageManager.PERMISSION_GRANTED) {
11723 pw.println("Permission Denial: can't dump WindowManager from from pid="
11724 + Binder.getCallingPid()
11725 + ", uid=" + Binder.getCallingUid());
11726 return;
11727 }
Romain Guy06882f82009-06-10 13:36:04 -070011728
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070011729 if (ENABLE_NATIVE_INPUT_DISPATCH) {
11730 pw.println("Input Dispatcher State:");
11731 mInputManager.dump(pw);
11732 } else {
11733 pw.println("Input State:");
11734 mQueue.dump(pw, " ");
11735 }
Dianne Hackborna2e92262010-03-02 17:19:29 -080011736 pw.println(" ");
11737
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011738 synchronized(mWindowMap) {
11739 pw.println("Current Window Manager state:");
11740 for (int i=mWindows.size()-1; i>=0; i--) {
11741 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011742 pw.print(" Window #"); pw.print(i); pw.print(' ');
11743 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011744 w.dump(pw, " ");
11745 }
11746 if (mInputMethodDialogs.size() > 0) {
11747 pw.println(" ");
11748 pw.println(" Input method dialogs:");
11749 for (int i=mInputMethodDialogs.size()-1; i>=0; i--) {
11750 WindowState w = mInputMethodDialogs.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011751 pw.print(" IM Dialog #"); pw.print(i); pw.print(": "); pw.println(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011752 }
11753 }
11754 if (mPendingRemove.size() > 0) {
11755 pw.println(" ");
11756 pw.println(" Remove pending for:");
11757 for (int i=mPendingRemove.size()-1; i>=0; i--) {
11758 WindowState w = mPendingRemove.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011759 pw.print(" Remove #"); pw.print(i); pw.print(' ');
11760 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011761 w.dump(pw, " ");
11762 }
11763 }
11764 if (mForceRemoves != null && mForceRemoves.size() > 0) {
11765 pw.println(" ");
11766 pw.println(" Windows force removing:");
11767 for (int i=mForceRemoves.size()-1; i>=0; i--) {
11768 WindowState w = mForceRemoves.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011769 pw.print(" Removing #"); pw.print(i); pw.print(' ');
11770 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011771 w.dump(pw, " ");
11772 }
11773 }
11774 if (mDestroySurface.size() > 0) {
11775 pw.println(" ");
11776 pw.println(" Windows waiting to destroy their surface:");
11777 for (int i=mDestroySurface.size()-1; i>=0; i--) {
11778 WindowState w = mDestroySurface.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011779 pw.print(" Destroy #"); pw.print(i); pw.print(' ');
11780 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011781 w.dump(pw, " ");
11782 }
11783 }
11784 if (mLosingFocus.size() > 0) {
11785 pw.println(" ");
11786 pw.println(" Windows losing focus:");
11787 for (int i=mLosingFocus.size()-1; i>=0; i--) {
11788 WindowState w = mLosingFocus.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011789 pw.print(" Losing #"); pw.print(i); pw.print(' ');
11790 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011791 w.dump(pw, " ");
11792 }
11793 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070011794 if (mResizingWindows.size() > 0) {
11795 pw.println(" ");
11796 pw.println(" Windows waiting to resize:");
11797 for (int i=mResizingWindows.size()-1; i>=0; i--) {
11798 WindowState w = mResizingWindows.get(i);
11799 pw.print(" Resizing #"); pw.print(i); pw.print(' ');
11800 pw.print(w); pw.println(":");
11801 w.dump(pw, " ");
11802 }
11803 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011804 if (mSessions.size() > 0) {
11805 pw.println(" ");
11806 pw.println(" All active sessions:");
11807 Iterator<Session> it = mSessions.iterator();
11808 while (it.hasNext()) {
11809 Session s = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011810 pw.print(" Session "); pw.print(s); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011811 s.dump(pw, " ");
11812 }
11813 }
11814 if (mTokenMap.size() > 0) {
11815 pw.println(" ");
11816 pw.println(" All tokens:");
11817 Iterator<WindowToken> it = mTokenMap.values().iterator();
11818 while (it.hasNext()) {
11819 WindowToken token = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011820 pw.print(" Token "); pw.print(token.token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011821 token.dump(pw, " ");
11822 }
11823 }
11824 if (mTokenList.size() > 0) {
11825 pw.println(" ");
11826 pw.println(" Window token list:");
11827 for (int i=0; i<mTokenList.size(); i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011828 pw.print(" #"); pw.print(i); pw.print(": ");
11829 pw.println(mTokenList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011830 }
11831 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070011832 if (mWallpaperTokens.size() > 0) {
11833 pw.println(" ");
11834 pw.println(" Wallpaper tokens:");
11835 for (int i=mWallpaperTokens.size()-1; i>=0; i--) {
11836 WindowToken token = mWallpaperTokens.get(i);
11837 pw.print(" Wallpaper #"); pw.print(i);
11838 pw.print(' '); pw.print(token); pw.println(':');
11839 token.dump(pw, " ");
11840 }
11841 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011842 if (mAppTokens.size() > 0) {
11843 pw.println(" ");
11844 pw.println(" Application tokens in Z order:");
11845 for (int i=mAppTokens.size()-1; i>=0; i--) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011846 pw.print(" App #"); pw.print(i); pw.print(": ");
11847 pw.println(mAppTokens.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011848 }
11849 }
11850 if (mFinishedStarting.size() > 0) {
11851 pw.println(" ");
11852 pw.println(" Finishing start of application tokens:");
11853 for (int i=mFinishedStarting.size()-1; i>=0; i--) {
11854 WindowToken token = mFinishedStarting.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011855 pw.print(" Finished Starting #"); pw.print(i);
11856 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011857 token.dump(pw, " ");
11858 }
11859 }
11860 if (mExitingTokens.size() > 0) {
11861 pw.println(" ");
11862 pw.println(" Exiting tokens:");
11863 for (int i=mExitingTokens.size()-1; i>=0; i--) {
11864 WindowToken token = mExitingTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011865 pw.print(" Exiting #"); pw.print(i);
11866 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011867 token.dump(pw, " ");
11868 }
11869 }
11870 if (mExitingAppTokens.size() > 0) {
11871 pw.println(" ");
11872 pw.println(" Exiting application tokens:");
11873 for (int i=mExitingAppTokens.size()-1; i>=0; i--) {
11874 WindowToken token = mExitingAppTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011875 pw.print(" Exiting App #"); pw.print(i);
11876 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011877 token.dump(pw, " ");
11878 }
11879 }
11880 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011881 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
11882 pw.print(" mLastFocus="); pw.println(mLastFocus);
11883 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
11884 pw.print(" mInputMethodTarget="); pw.println(mInputMethodTarget);
11885 pw.print(" mInputMethodWindow="); pw.println(mInputMethodWindow);
Dianne Hackbornf21adf62009-08-13 10:20:21 -070011886 pw.print(" mWallpaperTarget="); pw.println(mWallpaperTarget);
Dianne Hackborn284ac932009-08-28 10:34:25 -070011887 if (mLowerWallpaperTarget != null && mUpperWallpaperTarget != null) {
11888 pw.print(" mLowerWallpaperTarget="); pw.println(mLowerWallpaperTarget);
11889 pw.print(" mUpperWallpaperTarget="); pw.println(mUpperWallpaperTarget);
11890 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -080011891 pw.print(" mCurConfiguration="); pw.println(this.mCurConfiguration);
11892 pw.print(" mInTouchMode="); pw.print(mInTouchMode);
11893 pw.print(" mLayoutSeq="); pw.println(mLayoutSeq);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011894 pw.print(" mSystemBooted="); pw.print(mSystemBooted);
11895 pw.print(" mDisplayEnabled="); pw.println(mDisplayEnabled);
11896 pw.print(" mLayoutNeeded="); pw.print(mLayoutNeeded);
11897 pw.print(" mBlurShown="); pw.println(mBlurShown);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011898 if (mDimAnimator != null) {
11899 mDimAnimator.printTo(pw);
11900 } else {
Dianne Hackborna2e92262010-03-02 17:19:29 -080011901 pw.println( " no DimAnimator ");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011902 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011903 pw.print(" mInputMethodAnimLayerAdjustment=");
Dianne Hackborn759a39e2009-08-09 17:20:27 -070011904 pw.print(mInputMethodAnimLayerAdjustment);
11905 pw.print(" mWallpaperAnimLayerAdjustment=");
11906 pw.println(mWallpaperAnimLayerAdjustment);
Dianne Hackborn284ac932009-08-28 10:34:25 -070011907 pw.print(" mLastWallpaperX="); pw.print(mLastWallpaperX);
11908 pw.print(" mLastWallpaperY="); pw.println(mLastWallpaperY);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011909 pw.print(" mDisplayFrozen="); pw.print(mDisplayFrozen);
11910 pw.print(" mWindowsFreezingScreen="); pw.print(mWindowsFreezingScreen);
Dianne Hackborne36d6e22010-02-17 19:46:25 -080011911 pw.print(" mAppsFreezingScreen="); pw.print(mAppsFreezingScreen);
11912 pw.print(" mWaitingForConfig="); pw.println(mWaitingForConfig);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011913 pw.print(" mRotation="); pw.print(mRotation);
11914 pw.print(", mForcedAppOrientation="); pw.print(mForcedAppOrientation);
11915 pw.print(", mRequestedRotation="); pw.println(mRequestedRotation);
11916 pw.print(" mAnimationPending="); pw.print(mAnimationPending);
11917 pw.print(" mWindowAnimationScale="); pw.print(mWindowAnimationScale);
11918 pw.print(" mTransitionWindowAnimationScale="); pw.println(mTransitionAnimationScale);
11919 pw.print(" mNextAppTransition=0x");
11920 pw.print(Integer.toHexString(mNextAppTransition));
11921 pw.print(", mAppTransitionReady="); pw.print(mAppTransitionReady);
Dianne Hackborna8f60182009-09-01 19:01:50 -070011922 pw.print(", mAppTransitionRunning="); pw.print(mAppTransitionRunning);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011923 pw.print(", mAppTransitionTimeout="); pw.println( mAppTransitionTimeout);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070011924 if (mNextAppTransitionPackage != null) {
11925 pw.print(" mNextAppTransitionPackage=");
11926 pw.print(mNextAppTransitionPackage);
11927 pw.print(", mNextAppTransitionEnter=0x");
11928 pw.print(Integer.toHexString(mNextAppTransitionEnter));
11929 pw.print(", mNextAppTransitionExit=0x");
11930 pw.print(Integer.toHexString(mNextAppTransitionExit));
11931 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011932 pw.print(" mStartingIconInTransition="); pw.print(mStartingIconInTransition);
11933 pw.print(", mSkipAppTransitionAnimation="); pw.println(mSkipAppTransitionAnimation);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070011934 if (mLastEnterAnimToken != null || mLastEnterAnimToken != null) {
11935 pw.print(" mLastEnterAnimToken="); pw.print(mLastEnterAnimToken);
11936 pw.print(", mLastEnterAnimParams="); pw.println(mLastEnterAnimParams);
11937 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011938 if (mOpeningApps.size() > 0) {
11939 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
11940 }
11941 if (mClosingApps.size() > 0) {
11942 pw.print(" mClosingApps="); pw.println(mClosingApps);
11943 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070011944 if (mToTopApps.size() > 0) {
11945 pw.print(" mToTopApps="); pw.println(mToTopApps);
11946 }
11947 if (mToBottomApps.size() > 0) {
11948 pw.print(" mToBottomApps="); pw.println(mToBottomApps);
11949 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011950 pw.print(" DisplayWidth="); pw.print(mDisplay.getWidth());
11951 pw.print(" DisplayHeight="); pw.println(mDisplay.getHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011952 pw.println(" KeyWaiter state:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011953 pw.print(" mLastWin="); pw.print(mKeyWaiter.mLastWin);
11954 pw.print(" mLastBinder="); pw.println(mKeyWaiter.mLastBinder);
11955 pw.print(" mFinished="); pw.print(mKeyWaiter.mFinished);
11956 pw.print(" mGotFirstWindow="); pw.print(mKeyWaiter.mGotFirstWindow);
11957 pw.print(" mEventDispatching="); pw.print(mKeyWaiter.mEventDispatching);
11958 pw.print(" mTimeToSwitch="); pw.println(mKeyWaiter.mTimeToSwitch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011959 }
11960 }
11961
11962 public void monitor() {
11963 synchronized (mWindowMap) { }
Mike Lockwood983ee092009-11-22 01:42:24 -050011964 synchronized (mKeyguardTokenWatcher) { }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011965 synchronized (mKeyWaiter) { }
11966 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011967
Dianne Hackbornddca3ee2009-07-23 19:01:31 -070011968 public void virtualKeyFeedback(KeyEvent event) {
11969 mPolicy.keyFeedbackFromInput(event);
11970 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080011971
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011972 /**
11973 * DimAnimator class that controls the dim animation. This holds the surface and
Doug Zongkerab5c49c2009-12-04 10:31:43 -080011974 * all state used for dim animation.
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011975 */
11976 private static class DimAnimator {
11977 Surface mDimSurface;
11978 boolean mDimShown = false;
11979 float mDimCurrentAlpha;
11980 float mDimTargetAlpha;
11981 float mDimDeltaPerMs;
11982 long mLastDimAnimTime;
Dianne Hackbornf83c5552010-03-31 22:19:32 -070011983
11984 int mLastDimWidth, mLastDimHeight;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011985
11986 DimAnimator (SurfaceSession session) {
11987 if (mDimSurface == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011988 if (SHOW_TRANSACTIONS) Slog.i(TAG, " DIM "
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011989 + mDimSurface + ": CREATE");
11990 try {
Mathias Agopian5d26c1e2010-03-01 16:09:43 -080011991 mDimSurface = new Surface(session, 0,
11992 "DimSurface",
11993 -1, 16, 16, PixelFormat.OPAQUE,
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011994 Surface.FX_SURFACE_DIM);
Maciej Białka9ee5c222010-03-24 10:25:40 +010011995 mDimSurface.setAlpha(0.0f);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011996 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011997 Slog.e(TAG, "Exception creating Dim surface", e);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011998 }
11999 }
12000 }
12001
12002 /**
12003 * Show the dim surface.
12004 */
12005 void show(int dw, int dh) {
Dianne Hackborn16064f92010-03-25 00:47:24 -070012006 if (!mDimShown) {
12007 if (SHOW_TRANSACTIONS) Slog.i(TAG, " DIM " + mDimSurface + ": SHOW pos=(0,0) (" +
12008 dw + "x" + dh + ")");
12009 mDimShown = true;
12010 try {
Dianne Hackbornf83c5552010-03-31 22:19:32 -070012011 mLastDimWidth = dw;
12012 mLastDimHeight = dh;
Dianne Hackborn16064f92010-03-25 00:47:24 -070012013 mDimSurface.setPosition(0, 0);
12014 mDimSurface.setSize(dw, dh);
12015 mDimSurface.show();
12016 } catch (RuntimeException e) {
12017 Slog.w(TAG, "Failure showing dim surface", e);
12018 }
Dianne Hackbornf83c5552010-03-31 22:19:32 -070012019 } else if (mLastDimWidth != dw || mLastDimHeight != dh) {
12020 mLastDimWidth = dw;
12021 mLastDimHeight = dh;
12022 mDimSurface.setSize(dw, dh);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070012023 }
12024 }
12025
12026 /**
12027 * Set's the dim surface's layer and update dim parameters that will be used in
12028 * {@link updateSurface} after all windows are examined.
12029 */
12030 void updateParameters(WindowState w, long currentTime) {
12031 mDimSurface.setLayer(w.mAnimLayer-1);
12032
12033 final float target = w.mExiting ? 0 : w.mAttrs.dimAmount;
Joe Onorato8a9b2202010-02-26 18:56:32 -080012034 if (SHOW_TRANSACTIONS) Slog.i(TAG, " DIM " + mDimSurface
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070012035 + ": layer=" + (w.mAnimLayer-1) + " target=" + target);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070012036 if (mDimTargetAlpha != target) {
12037 // If the desired dim level has changed, then
12038 // start an animation to it.
12039 mLastDimAnimTime = currentTime;
12040 long duration = (w.mAnimating && w.mAnimation != null)
12041 ? w.mAnimation.computeDurationHint()
12042 : DEFAULT_DIM_DURATION;
12043 if (target > mDimTargetAlpha) {
12044 // This is happening behind the activity UI,
12045 // so we can make it run a little longer to
12046 // give a stronger impression without disrupting
12047 // the user.
12048 duration *= DIM_DURATION_MULTIPLIER;
12049 }
12050 if (duration < 1) {
12051 // Don't divide by zero
12052 duration = 1;
12053 }
12054 mDimTargetAlpha = target;
12055 mDimDeltaPerMs = (mDimTargetAlpha-mDimCurrentAlpha) / duration;
12056 }
12057 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080012058
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070012059 /**
12060 * Updating the surface's alpha. Returns true if the animation continues, or returns
12061 * false when the animation is finished and the dim surface is hidden.
12062 */
12063 boolean updateSurface(boolean dimming, long currentTime, boolean displayFrozen) {
12064 if (!dimming) {
12065 if (mDimTargetAlpha != 0) {
12066 mLastDimAnimTime = currentTime;
12067 mDimTargetAlpha = 0;
12068 mDimDeltaPerMs = (-mDimCurrentAlpha) / DEFAULT_DIM_DURATION;
12069 }
12070 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080012071
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070012072 boolean animating = false;
12073 if (mLastDimAnimTime != 0) {
12074 mDimCurrentAlpha += mDimDeltaPerMs
12075 * (currentTime-mLastDimAnimTime);
12076 boolean more = true;
12077 if (displayFrozen) {
12078 // If the display is frozen, there is no reason to animate.
12079 more = false;
12080 } else if (mDimDeltaPerMs > 0) {
12081 if (mDimCurrentAlpha > mDimTargetAlpha) {
12082 more = false;
12083 }
12084 } else if (mDimDeltaPerMs < 0) {
12085 if (mDimCurrentAlpha < mDimTargetAlpha) {
12086 more = false;
12087 }
12088 } else {
12089 more = false;
12090 }
12091
12092 // Do we need to continue animating?
12093 if (more) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080012094 if (SHOW_TRANSACTIONS) Slog.i(TAG, " DIM "
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070012095 + mDimSurface + ": alpha=" + mDimCurrentAlpha);
12096 mLastDimAnimTime = currentTime;
12097 mDimSurface.setAlpha(mDimCurrentAlpha);
12098 animating = true;
12099 } else {
12100 mDimCurrentAlpha = mDimTargetAlpha;
12101 mLastDimAnimTime = 0;
Joe Onorato8a9b2202010-02-26 18:56:32 -080012102 if (SHOW_TRANSACTIONS) Slog.i(TAG, " DIM "
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070012103 + mDimSurface + ": final alpha=" + mDimCurrentAlpha);
12104 mDimSurface.setAlpha(mDimCurrentAlpha);
12105 if (!dimming) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080012106 if (SHOW_TRANSACTIONS) Slog.i(TAG, " DIM " + mDimSurface
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070012107 + ": HIDE");
12108 try {
12109 mDimSurface.hide();
12110 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080012111 Slog.w(TAG, "Illegal argument exception hiding dim surface");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070012112 }
12113 mDimShown = false;
12114 }
12115 }
12116 }
12117 return animating;
12118 }
12119
12120 public void printTo(PrintWriter pw) {
12121 pw.print(" mDimShown="); pw.print(mDimShown);
12122 pw.print(" current="); pw.print(mDimCurrentAlpha);
12123 pw.print(" target="); pw.print(mDimTargetAlpha);
12124 pw.print(" delta="); pw.print(mDimDeltaPerMs);
12125 pw.print(" lastAnimTime="); pw.println(mLastDimAnimTime);
12126 }
12127 }
12128
12129 /**
12130 * Animation that fade in after 0.5 interpolate time, or fade out in reverse order.
12131 * This is used for opening/closing transition for apps in compatible mode.
12132 */
12133 private static class FadeInOutAnimation extends Animation {
12134 int mWidth;
12135 boolean mFadeIn;
12136
12137 public FadeInOutAnimation(boolean fadeIn) {
12138 setInterpolator(new AccelerateInterpolator());
12139 setDuration(DEFAULT_FADE_IN_OUT_DURATION);
12140 mFadeIn = fadeIn;
12141 }
12142
12143 @Override
12144 protected void applyTransformation(float interpolatedTime, Transformation t) {
12145 float x = interpolatedTime;
12146 if (!mFadeIn) {
12147 x = 1.0f - x; // reverse the interpolation for fade out
12148 }
12149 if (x < 0.5) {
12150 // move the window out of the screen.
12151 t.getMatrix().setTranslate(mWidth, 0);
12152 } else {
12153 t.getMatrix().setTranslate(0, 0);// show
12154 t.setAlpha((x - 0.5f) * 2);
12155 }
12156 }
12157
12158 @Override
12159 public void initialize(int width, int height, int parentWidth, int parentHeight) {
12160 // width is the screen width {@see AppWindowToken#stepAnimatinoLocked}
12161 mWidth = width;
12162 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070012163
12164 @Override
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -070012165 public int getZAdjustment() {
12166 return Animation.ZORDER_TOP;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070012167 }
12168 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012169}