blob: f750f23d7cbc80a843460e90ac18ff83c9415f24 [file] [log] [blame]
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001/*
2 * Copyright (C) 2006 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.internal.policy.impl;
18
19import android.app.Activity;
20import android.app.ActivityManagerNative;
21import android.app.IActivityManager;
22import android.app.IStatusBar;
Dianne Hackborn78968392010-03-04 20:47:56 -080023import android.app.IUiModeManager;
24import android.app.UiModeManager;
Dianne Hackborn39c2d712009-09-22 11:41:31 -070025import android.content.ActivityNotFoundException;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080026import android.content.BroadcastReceiver;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080027import android.content.ContentResolver;
28import android.content.Context;
29import android.content.Intent;
Mike Lockwood1753f7f2009-08-24 14:49:07 -070030import android.content.IntentFilter;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080031import android.content.pm.ActivityInfo;
32import android.content.pm.PackageManager;
33import android.content.res.Configuration;
34import android.content.res.Resources;
35import android.database.ContentObserver;
Dianne Hackbornc777e072010-02-12 13:07:59 -080036import android.graphics.PixelFormat;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080037import android.graphics.Rect;
38import android.os.Handler;
39import android.os.IBinder;
40import android.os.LocalPowerManager;
41import android.os.PowerManager;
42import android.os.RemoteException;
43import android.os.ServiceManager;
44import android.os.SystemClock;
45import android.os.SystemProperties;
46import android.os.Vibrator;
47import android.provider.Settings;
48
49import com.android.internal.policy.PolicyManager;
50import com.android.internal.telephony.ITelephony;
Dianne Hackborn6019c9d2010-03-01 21:43:11 -080051import com.android.internal.widget.PointerLocationView;
52
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080053import android.util.Config;
54import android.util.EventLog;
55import android.util.Log;
Mitsuru Oshima831d0d92009-06-16 18:27:18 -070056import android.view.Display;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080057import android.view.Gravity;
58import android.view.HapticFeedbackConstants;
59import android.view.IWindowManager;
60import android.view.KeyEvent;
61import android.view.MotionEvent;
62import android.view.WindowOrientationListener;
63import android.view.RawInputEvent;
64import android.view.Surface;
65import android.view.View;
66import android.view.ViewConfiguration;
67import android.view.Window;
68import android.view.WindowManager;
69import static android.view.WindowManager.LayoutParams.FIRST_APPLICATION_WINDOW;
70import static android.view.WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN;
71import static android.view.WindowManager.LayoutParams.FLAG_FULLSCREEN;
72import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
73import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR;
74import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
Suchi Amalapurapu9cdc9032009-05-14 18:01:07 -070075import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
Dianne Hackborn39c2d712009-09-22 11:41:31 -070076import static android.view.WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD;
Mike Lockwood28569302010-01-28 11:54:40 -050077import static android.view.WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080078import static android.view.WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST;
79import static android.view.WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;
80import static android.view.WindowManager.LayoutParams.LAST_APPLICATION_WINDOW;
81import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA;
Dianne Hackborn5cb8d792009-05-21 17:34:15 -070082import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA_OVERLAY;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080083import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_PANEL;
84import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL;
85import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
86import static android.view.WindowManager.LayoutParams.TYPE_KEYGUARD;
87import static android.view.WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG;
88import static android.view.WindowManager.LayoutParams.TYPE_PHONE;
89import static android.view.WindowManager.LayoutParams.TYPE_PRIORITY_PHONE;
90import static android.view.WindowManager.LayoutParams.TYPE_SEARCH_BAR;
91import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR;
92import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR_PANEL;
Dianne Hackborn6a294ce2009-12-03 11:37:44 -080093import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080094import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
95import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
96import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
97import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
98import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
99import static android.view.WindowManager.LayoutParams.TYPE_TOAST;
Dianne Hackbornb7cd8b92009-08-08 20:53:03 -0700100import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800101import android.view.WindowManagerImpl;
102import android.view.WindowManagerPolicy;
Dianne Hackborn01ad2f42009-09-24 19:24:56 -0700103import android.view.animation.Animation;
104import android.view.animation.AnimationUtils;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800105import android.media.IAudioService;
106import android.media.AudioManager;
107
Dianne Hackborn08743722009-12-21 12:16:51 -0800108import java.util.ArrayList;
109
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800110/**
The Android Open Source Project0727d222009-03-11 12:11:58 -0700111 * WindowManagerPolicy implementation for the Android phone UI. This
112 * introduces a new method suffix, Lp, for an internal lock of the
113 * PhoneWindowManager. This is used to protect some internal state, and
114 * can be acquired with either thw Lw and Li lock held, so has the restrictions
115 * of both of those when held.
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800116 */
117public class PhoneWindowManager implements WindowManagerPolicy {
118 static final String TAG = "WindowManager";
119 static final boolean DEBUG = false;
120 static final boolean localLOGV = DEBUG ? Config.LOGD : Config.LOGV;
The Android Open Source Project11267662009-03-18 17:39:47 -0700121 static final boolean DEBUG_LAYOUT = false;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800122 static final boolean SHOW_STARTING_ANIMATIONS = true;
123 static final boolean SHOW_PROCESSES_ON_ALT_MENU = false;
124
Dianne Hackbornb7cd8b92009-08-08 20:53:03 -0700125 // wallpaper is at the bottom, though the window manager may move it.
Dianne Hackborn3c3bd442009-08-19 17:09:46 -0700126 static final int WALLPAPER_LAYER = 2;
Dianne Hackbornb7cd8b92009-08-08 20:53:03 -0700127 static final int APPLICATION_LAYER = 2;
128 static final int PHONE_LAYER = 3;
129 static final int SEARCH_BAR_LAYER = 4;
130 static final int STATUS_BAR_PANEL_LAYER = 5;
Dianne Hackborn6a294ce2009-12-03 11:37:44 -0800131 static final int SYSTEM_DIALOG_LAYER = 6;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800132 // toasts and the plugged-in battery thing
Dianne Hackborn6a294ce2009-12-03 11:37:44 -0800133 static final int TOAST_LAYER = 7;
134 static final int STATUS_BAR_LAYER = 8;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800135 // SIM errors and unlock. Not sure if this really should be in a high layer.
Dianne Hackborn6a294ce2009-12-03 11:37:44 -0800136 static final int PRIORITY_PHONE_LAYER = 9;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800137 // like the ANR / app crashed dialogs
Dianne Hackborn6a294ce2009-12-03 11:37:44 -0800138 static final int SYSTEM_ALERT_LAYER = 10;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800139 // system-level error dialogs
Dianne Hackborn6a294ce2009-12-03 11:37:44 -0800140 static final int SYSTEM_ERROR_LAYER = 11;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800141 // on-screen keyboards and other such input method user interfaces go here.
Dianne Hackborn6a294ce2009-12-03 11:37:44 -0800142 static final int INPUT_METHOD_LAYER = 12;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800143 // on-screen keyboards and other such input method user interfaces go here.
Dianne Hackborn6a294ce2009-12-03 11:37:44 -0800144 static final int INPUT_METHOD_DIALOG_LAYER = 13;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800145 // the keyguard; nothing on top of these can take focus, since they are
146 // responsible for power management when displayed.
Dianne Hackborn6a294ce2009-12-03 11:37:44 -0800147 static final int KEYGUARD_LAYER = 14;
148 static final int KEYGUARD_DIALOG_LAYER = 15;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800149 // things in here CAN NOT take focus, but are shown on top of everything else.
Dianne Hackborn6a294ce2009-12-03 11:37:44 -0800150 static final int SYSTEM_OVERLAY_LAYER = 16;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800151
Dianne Hackborn5cb8d792009-05-21 17:34:15 -0700152 static final int APPLICATION_MEDIA_SUBLAYER = -2;
153 static final int APPLICATION_MEDIA_OVERLAY_SUBLAYER = -1;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800154 static final int APPLICATION_PANEL_SUBLAYER = 1;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800155 static final int APPLICATION_SUB_PANEL_SUBLAYER = 2;
156
157 static final float SLIDE_TOUCH_EVENT_SIZE_LIMIT = 0.6f;
158
159 // Debugging: set this to have the system act like there is no hard keyboard.
160 static final boolean KEYBOARD_ALWAYS_HIDDEN = false;
161
162 static public final String SYSTEM_DIALOG_REASON_KEY = "reason";
163 static public final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions";
164 static public final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";
Joe Onoratof275f0c2009-11-24 16:11:13 -0500165 static public final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800166
The Android Open Source Project0727d222009-03-11 12:11:58 -0700167 final Object mLock = new Object();
168
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800169 Context mContext;
170 IWindowManager mWindowManager;
171 LocalPowerManager mPowerManager;
172 Vibrator mVibrator; // Vibrator for giving feedback of orientation changes
173
Dianne Hackborn181ceb52009-08-27 22:16:40 -0700174 // Vibrator pattern for haptic feedback of a long press.
175 long[] mLongPressVibePattern;
176
177 // Vibrator pattern for haptic feedback of virtual key press.
178 long[] mVirtualKeyVibePattern;
179
Amith Yamasanic33cb712010-02-10 15:21:49 -0800180 // Vibrator pattern for a short vibration.
181 long[] mKeyboardTapVibePattern;
182
Dianne Hackbornb1a79802009-09-29 15:18:31 -0700183 // Vibrator pattern for haptic feedback during boot when safe mode is disabled.
184 long[] mSafeModeDisabledVibePattern;
185
186 // Vibrator pattern for haptic feedback during boot when safe mode is enabled.
187 long[] mSafeModeEnabledVibePattern;
188
Adam Powell9d86faf2010-02-10 11:39:57 -0800189 // Vibrator pattern for haptic feedback when the user hits a touch scroll barrier.
190 long[] mScrollBarrierVibePattern;
191
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800192 /** If true, hitting shift & menu will broadcast Intent.ACTION_BUG_REPORT */
193 boolean mEnableShiftMenuBugReports = false;
194
195 boolean mSafeMode;
196 WindowState mStatusBar = null;
Dianne Hackborn08743722009-12-21 12:16:51 -0800197 final ArrayList<WindowState> mStatusBarPanels = new ArrayList<WindowState>();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800198 WindowState mKeyguard = null;
199 KeyguardViewMediator mKeyguardMediator;
200 GlobalActions mGlobalActions;
201 boolean mShouldTurnOffOnKeyUp;
202 RecentApplicationsDialog mRecentAppsDialog;
203 Handler mHandler;
Dianne Hackborn05726582009-09-22 17:28:24 -0700204
Dianne Hackbornc777e072010-02-12 13:07:59 -0800205 boolean mSystemReady;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800206 boolean mLidOpen;
Dianne Hackborn78968392010-03-04 20:47:56 -0800207 int mUiMode = Configuration.UI_MODE_TYPE_NORMAL;
Mike Lockwood1753f7f2009-08-24 14:49:07 -0700208 int mLidOpenRotation;
Mike Lockwood2d1efbd2009-09-16 12:15:53 -0400209 int mCarDockRotation;
210 int mDeskDockRotation;
Mike Lockwoode9867d22009-09-20 01:59:02 -0400211 boolean mCarDockEnablesAccelerometer;
212 boolean mDeskDockEnablesAccelerometer;
Dianne Hackborn2862fff2009-09-15 22:56:29 -0700213 int mLidKeyboardAccessibility;
214 int mLidNavigationAccessibility;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800215 boolean mScreenOn = false;
216 boolean mOrientationSensorEnabled = false;
217 int mCurrentAppOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
218 static final int DEFAULT_ACCELEROMETER_ROTATION = 0;
219 int mAccelerometerDefault = DEFAULT_ACCELEROMETER_ROTATION;
220 boolean mHasSoftInput = false;
221
Dianne Hackbornc777e072010-02-12 13:07:59 -0800222 int mPointerLocationMode = 0;
223 PointerLocationView mPointerLocationView = null;
224
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800225 // The current size of the screen.
226 int mW, mH;
227 // During layout, the current screen borders with all outer decoration
228 // (status bar, input method dock) accounted for.
229 int mCurLeft, mCurTop, mCurRight, mCurBottom;
230 // During layout, the frame in which content should be displayed
231 // to the user, accounting for all screen decoration except for any
232 // space they deem as available for other content. This is usually
233 // the same as mCur*, but may be larger if the screen decor has supplied
234 // content insets.
235 int mContentLeft, mContentTop, mContentRight, mContentBottom;
236 // During layout, the current screen borders along with input method
237 // windows are placed.
238 int mDockLeft, mDockTop, mDockRight, mDockBottom;
239 // During layout, the layer at which the doc window is placed.
240 int mDockLayer;
241
242 static final Rect mTmpParentFrame = new Rect();
243 static final Rect mTmpDisplayFrame = new Rect();
244 static final Rect mTmpContentFrame = new Rect();
245 static final Rect mTmpVisibleFrame = new Rect();
246
247 WindowState mTopFullscreenOpaqueWindowState;
248 boolean mForceStatusBar;
Dianne Hackborn39c2d712009-09-22 11:41:31 -0700249 boolean mHideLockScreen;
250 boolean mDismissKeyguard;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800251 boolean mHomePressed;
252 Intent mHomeIntent;
Dianne Hackborn39c2d712009-09-22 11:41:31 -0700253 Intent mCarDockIntent;
254 Intent mDeskDockIntent;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800255 boolean mSearchKeyPressed;
256 boolean mConsumeSearchKeyUp;
257
Mike Lockwood28569302010-01-28 11:54:40 -0500258 // support for activating the lock screen while the screen is on
259 boolean mAllowLockscreenWhenOn;
260 int mLockScreenTimeout;
261 boolean mLockScreenTimerActive;
262
David Brownbaf8d092010-03-08 21:52:59 -0800263 // Behavior of ENDCALL Button. (See Settings.System.END_BUTTON_BEHAVIOR.)
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800264 int mEndcallBehavior;
David Brownbaf8d092010-03-08 21:52:59 -0800265
266 // Behavior of POWER button while in-call and screen on.
267 // (See Settings.Secure.INCALL_POWER_BUTTON_BEHAVIOR.)
268 int mIncallPowerBehavior;
269
Mitsuru Oshima831d0d92009-06-16 18:27:18 -0700270 int mLandscapeRotation = -1;
271 int mPortraitRotation = -1;
272
Dianne Hackborn03759ed2009-03-27 16:04:08 -0700273 // Nothing to see here, move along...
274 int mFancyRotationAnimation;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800275
276 ShortcutManager mShortcutManager;
277 PowerManager.WakeLock mBroadcastWakeLock;
278
279 class SettingsObserver extends ContentObserver {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800280 SettingsObserver(Handler handler) {
281 super(handler);
282 }
David Brownbaf8d092010-03-08 21:52:59 -0800283
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800284 void observe() {
285 ContentResolver resolver = mContext.getContentResolver();
286 resolver.registerContentObserver(Settings.System.getUriFor(
287 Settings.System.END_BUTTON_BEHAVIOR), false, this);
David Brownbaf8d092010-03-08 21:52:59 -0800288 resolver.registerContentObserver(Settings.Secure.getUriFor(
289 Settings.Secure.INCALL_POWER_BUTTON_BEHAVIOR), false, this);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800290 resolver.registerContentObserver(Settings.System.getUriFor(
291 Settings.System.ACCELEROMETER_ROTATION), false, this);
Mike Lockwood28569302010-01-28 11:54:40 -0500292 resolver.registerContentObserver(Settings.System.getUriFor(
293 Settings.System.SCREEN_OFF_TIMEOUT), false, this);
Dianne Hackbornc777e072010-02-12 13:07:59 -0800294 resolver.registerContentObserver(Settings.System.getUriFor(
295 Settings.System.POINTER_LOCATION), false, this);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800296 resolver.registerContentObserver(Settings.Secure.getUriFor(
297 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
Dianne Hackborn03759ed2009-03-27 16:04:08 -0700298 resolver.registerContentObserver(Settings.System.getUriFor(
299 "fancy_rotation_anim"), false, this);
Dianne Hackbornc777e072010-02-12 13:07:59 -0800300 updateSettings();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800301 }
302
303 @Override public void onChange(boolean selfChange) {
Dianne Hackbornc777e072010-02-12 13:07:59 -0800304 updateSettings();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800305 try {
Dianne Hackborn32bc91d2009-03-27 16:16:03 -0700306 mWindowManager.setRotation(USE_LAST_ROTATION, false,
307 mFancyRotationAnimation);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800308 } catch (RemoteException e) {
309 // Ignore
310 }
311 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800312 }
313
314 class MyOrientationListener extends WindowOrientationListener {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800315 MyOrientationListener(Context context) {
316 super(context);
317 }
318
319 @Override
The Android Open Source Projectd06b0972009-03-13 13:04:23 -0700320 public void onOrientationChanged(int rotation) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800321 // Send updates based on orientation value
Dianne Hackborn05726582009-09-22 17:28:24 -0700322 if (localLOGV) Log.v(TAG, "onOrientationChanged, rotation changed to " +rotation);
Dianne Hackborn03759ed2009-03-27 16:04:08 -0700323 try {
Dianne Hackborn32bc91d2009-03-27 16:16:03 -0700324 mWindowManager.setRotation(rotation, false,
325 mFancyRotationAnimation);
Dianne Hackborn03759ed2009-03-27 16:04:08 -0700326 } catch (RemoteException e) {
327 // Ignore
Wink Saville37c124c2009-04-02 01:37:02 -0700328
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800329 }
330 }
331 }
332 MyOrientationListener mOrientationListener;
333
Dianne Hackborn03759ed2009-03-27 16:04:08 -0700334 boolean useSensorForOrientationLp(int appOrientation) {
Joe Onorato1b58b3e2009-09-22 16:44:16 -0700335 // The app says use the sensor.
Dianne Hackborn03759ed2009-03-27 16:04:08 -0700336 if (appOrientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800337 return true;
338 }
Joe Onorato1b58b3e2009-09-22 16:44:16 -0700339 // The user preference says we can rotate, and the app is willing to rotate.
340 if (mAccelerometerDefault != 0 &&
341 (appOrientation == ActivityInfo.SCREEN_ORIENTATION_USER
342 || appOrientation == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800343 return true;
344 }
Joe Onorato1b58b3e2009-09-22 16:44:16 -0700345 // We're in a dock that has a rotation affinity, an the app is willing to rotate.
Dianne Hackborn78968392010-03-04 20:47:56 -0800346 if ((mCarDockEnablesAccelerometer && mUiMode == Configuration.UI_MODE_TYPE_CAR)
347 || (mDeskDockEnablesAccelerometer && mUiMode == Configuration.UI_MODE_TYPE_DESK)) {
Joe Onorato1b58b3e2009-09-22 16:44:16 -0700348 // Note we override the nosensor flag here.
349 if (appOrientation == ActivityInfo.SCREEN_ORIENTATION_USER
350 || appOrientation == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
351 || appOrientation == ActivityInfo.SCREEN_ORIENTATION_NOSENSOR) {
352 return true;
353 }
354 }
355 // Else, don't use the sensor.
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800356 return false;
357 }
358
Suchi Amalapurapua9e1b162009-03-24 22:24:47 -0700359 /*
360 * We always let the sensor be switched on by default except when
361 * the user has explicitly disabled sensor based rotation or when the
362 * screen is switched off.
363 */
The Android Open Source Project0727d222009-03-11 12:11:58 -0700364 boolean needSensorRunningLp() {
365 if (mCurrentAppOrientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800366 // If the application has explicitly requested to follow the
367 // orientation, then we need to turn the sensor or.
368 return true;
369 }
Dianne Hackborn78968392010-03-04 20:47:56 -0800370 if ((mCarDockEnablesAccelerometer && mUiMode == Configuration.UI_MODE_TYPE_CAR) ||
371 (mDeskDockEnablesAccelerometer && mUiMode == Configuration.UI_MODE_TYPE_DESK)) {
Mike Lockwoode9867d22009-09-20 01:59:02 -0400372 // enable accelerometer if we are docked in a dock that enables accelerometer
373 // orientation management,
374 return true;
375 }
Suchi Amalapurapua9e1b162009-03-24 22:24:47 -0700376 if (mAccelerometerDefault == 0) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800377 // If the setting for using the sensor by default is enabled, then
378 // we will always leave it on. Note that the user could go to
379 // a window that forces an orientation that does not use the
380 // sensor and in theory we could turn it off... however, when next
381 // turning it on we won't have a good value for the current
382 // orientation for a little bit, which can cause orientation
383 // changes to lag, so we'd like to keep it always on. (It will
384 // still be turned off when the screen is off.)
Suchi Amalapurapua9e1b162009-03-24 22:24:47 -0700385 return false;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800386 }
Suchi Amalapurapua9e1b162009-03-24 22:24:47 -0700387 return true;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800388 }
389
390 /*
391 * Various use cases for invoking this function
392 * screen turning off, should always disable listeners if already enabled
393 * screen turned on and current app has sensor based orientation, enable listeners
394 * if not already enabled
395 * screen turned on and current app does not have sensor orientation, disable listeners if
396 * already enabled
397 * screen turning on and current app has sensor based orientation, enable listeners if needed
398 * screen turning on and current app has nosensor based orientation, do nothing
399 */
The Android Open Source Project0727d222009-03-11 12:11:58 -0700400 void updateOrientationListenerLp() {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800401 if (!mOrientationListener.canDetectOrientation()) {
402 // If sensor is turned off or nonexistent for some reason
403 return;
404 }
405 //Could have been invoked due to screen turning on or off or
406 //change of the currently visible window's orientation
Dianne Hackborn05726582009-09-22 17:28:24 -0700407 if (localLOGV) Log.v(TAG, "Screen status="+mScreenOn+
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800408 ", current orientation="+mCurrentAppOrientation+
409 ", SensorEnabled="+mOrientationSensorEnabled);
410 boolean disable = true;
The Android Open Source Project0727d222009-03-11 12:11:58 -0700411 if (mScreenOn) {
412 if (needSensorRunningLp()) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800413 disable = false;
414 //enable listener if not already enabled
The Android Open Source Project0727d222009-03-11 12:11:58 -0700415 if (!mOrientationSensorEnabled) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800416 mOrientationListener.enable();
Dianne Hackborn05726582009-09-22 17:28:24 -0700417 if(localLOGV) Log.v(TAG, "Enabling listeners");
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800418 mOrientationSensorEnabled = true;
419 }
420 }
421 }
422 //check if sensors need to be disabled
The Android Open Source Project0727d222009-03-11 12:11:58 -0700423 if (disable && mOrientationSensorEnabled) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800424 mOrientationListener.disable();
Dianne Hackborn05726582009-09-22 17:28:24 -0700425 if(localLOGV) Log.v(TAG, "Disabling listeners");
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800426 mOrientationSensorEnabled = false;
427 }
428 }
429
Dianne Hackborn0041e972009-07-24 17:14:43 -0700430 Runnable mPowerLongPress = new Runnable() {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800431 public void run() {
432 mShouldTurnOffOnKeyUp = false;
The Android Open Source Project0727d222009-03-11 12:11:58 -0700433 performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800434 sendCloseSystemWindows(SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS);
435 showGlobalActionsDialog();
436 }
437 };
438
439 void showGlobalActionsDialog() {
440 if (mGlobalActions == null) {
441 mGlobalActions = new GlobalActions(mContext);
442 }
Dianne Hackborn9dc06cc2009-11-17 18:19:23 -0800443 final boolean keyguardShowing = mKeyguardMediator.isShowingAndNotHidden();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800444 mGlobalActions.showDialog(keyguardShowing, isDeviceProvisioned());
445 if (keyguardShowing) {
446 // since it took two seconds of long press to bring this up,
447 // poke the wake lock so they have some time to see the dialog.
448 mKeyguardMediator.pokeWakelock();
449 }
450 }
451
452 boolean isDeviceProvisioned() {
453 return Settings.Secure.getInt(
454 mContext.getContentResolver(), Settings.Secure.DEVICE_PROVISIONED, 0) != 0;
455 }
456
457 /**
458 * When a home-key longpress expires, close other system windows and launch the recent apps
459 */
460 Runnable mHomeLongPress = new Runnable() {
461 public void run() {
462 /*
463 * Eat the longpress so it won't dismiss the recent apps dialog when
464 * the user lets go of the home key
465 */
466 mHomePressed = false;
The Android Open Source Project0727d222009-03-11 12:11:58 -0700467 performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800468 sendCloseSystemWindows(SYSTEM_DIALOG_REASON_RECENT_APPS);
469 showRecentAppsDialog();
470 }
471 };
472
473 /**
474 * Create (if necessary) and launch the recent apps dialog
475 */
476 void showRecentAppsDialog() {
477 if (mRecentAppsDialog == null) {
478 mRecentAppsDialog = new RecentApplicationsDialog(mContext);
479 }
480 mRecentAppsDialog.show();
481 }
482
483 /** {@inheritDoc} */
484 public void init(Context context, IWindowManager windowManager,
485 LocalPowerManager powerManager) {
486 mContext = context;
487 mWindowManager = windowManager;
488 mPowerManager = powerManager;
489 mKeyguardMediator = new KeyguardViewMediator(context, this, powerManager);
490 mHandler = new Handler();
491 mOrientationListener = new MyOrientationListener(mContext);
492 SettingsObserver settingsObserver = new SettingsObserver(mHandler);
493 settingsObserver.observe();
494 mShortcutManager = new ShortcutManager(context, mHandler);
495 mShortcutManager.observe();
496 mHomeIntent = new Intent(Intent.ACTION_MAIN, null);
497 mHomeIntent.addCategory(Intent.CATEGORY_HOME);
498 mHomeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
499 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Dianne Hackborn39c2d712009-09-22 11:41:31 -0700500 mCarDockIntent = new Intent(Intent.ACTION_MAIN, null);
501 mCarDockIntent.addCategory(Intent.CATEGORY_CAR_DOCK);
502 mCarDockIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
503 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
504 mDeskDockIntent = new Intent(Intent.ACTION_MAIN, null);
505 mDeskDockIntent.addCategory(Intent.CATEGORY_DESK_DOCK);
506 mDeskDockIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
507 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800508 PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
509 mBroadcastWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
510 "PhoneWindowManager.mBroadcastWakeLock");
511 mEnableShiftMenuBugReports = "1".equals(SystemProperties.get("ro.debuggable"));
Dianne Hackborn2862fff2009-09-15 22:56:29 -0700512 mLidOpenRotation = readRotation(
Mike Lockwood2d1efbd2009-09-16 12:15:53 -0400513 com.android.internal.R.integer.config_lidOpenRotation);
514 mCarDockRotation = readRotation(
515 com.android.internal.R.integer.config_carDockRotation);
516 mDeskDockRotation = readRotation(
517 com.android.internal.R.integer.config_deskDockRotation);
Mike Lockwoode9867d22009-09-20 01:59:02 -0400518 mCarDockEnablesAccelerometer = mContext.getResources().getBoolean(
519 com.android.internal.R.bool.config_carDockEnablesAccelerometer);
520 mDeskDockEnablesAccelerometer = mContext.getResources().getBoolean(
521 com.android.internal.R.bool.config_deskDockEnablesAccelerometer);
Dianne Hackborn2862fff2009-09-15 22:56:29 -0700522 mLidKeyboardAccessibility = mContext.getResources().getInteger(
523 com.android.internal.R.integer.config_lidKeyboardAccessibility);
524 mLidNavigationAccessibility = mContext.getResources().getInteger(
525 com.android.internal.R.integer.config_lidNavigationAccessibility);
Mike Lockwood1753f7f2009-08-24 14:49:07 -0700526 // register for dock events
Dianne Hackborn78968392010-03-04 20:47:56 -0800527 IntentFilter filter = new IntentFilter();
528 filter.addAction(UiModeManager.ACTION_ENTER_CAR_MODE);
529 filter.addAction(UiModeManager.ACTION_EXIT_CAR_MODE);
530 filter.addAction(UiModeManager.ACTION_ENTER_DESK_MODE);
531 filter.addAction(UiModeManager.ACTION_EXIT_DESK_MODE);
532 context.registerReceiver(mDockReceiver, filter);
Dianne Hackbornb1a79802009-09-29 15:18:31 -0700533 mVibrator = new Vibrator();
534 mLongPressVibePattern = getLongIntArray(mContext.getResources(),
535 com.android.internal.R.array.config_longPressVibePattern);
536 mVirtualKeyVibePattern = getLongIntArray(mContext.getResources(),
537 com.android.internal.R.array.config_virtualKeyVibePattern);
Amith Yamasanic33cb712010-02-10 15:21:49 -0800538 mKeyboardTapVibePattern = getLongIntArray(mContext.getResources(),
539 com.android.internal.R.array.config_keyboardTapVibePattern);
Dianne Hackbornb1a79802009-09-29 15:18:31 -0700540 mSafeModeDisabledVibePattern = getLongIntArray(mContext.getResources(),
541 com.android.internal.R.array.config_safeModeDisabledVibePattern);
542 mSafeModeEnabledVibePattern = getLongIntArray(mContext.getResources(),
543 com.android.internal.R.array.config_safeModeEnabledVibePattern);
Adam Powell9d86faf2010-02-10 11:39:57 -0800544 mScrollBarrierVibePattern = getLongIntArray(mContext.getResources(),
545 com.android.internal.R.array.config_scrollBarrierVibePattern);
Mike Lockwood1753f7f2009-08-24 14:49:07 -0700546 }
547
Dianne Hackbornc777e072010-02-12 13:07:59 -0800548 public void updateSettings() {
549 ContentResolver resolver = mContext.getContentResolver();
550 boolean updateRotation = false;
551 View addView = null;
552 View removeView = null;
553 synchronized (mLock) {
554 mEndcallBehavior = Settings.System.getInt(resolver,
David Brownbaf8d092010-03-08 21:52:59 -0800555 Settings.System.END_BUTTON_BEHAVIOR,
556 Settings.System.END_BUTTON_BEHAVIOR_DEFAULT);
557 mIncallPowerBehavior = Settings.Secure.getInt(resolver,
558 Settings.Secure.INCALL_POWER_BUTTON_BEHAVIOR,
559 Settings.Secure.INCALL_POWER_BUTTON_BEHAVIOR_DEFAULT);
Dianne Hackbornc777e072010-02-12 13:07:59 -0800560 mFancyRotationAnimation = Settings.System.getInt(resolver,
561 "fancy_rotation_anim", 0) != 0 ? 0x80 : 0;
562 int accelerometerDefault = Settings.System.getInt(resolver,
563 Settings.System.ACCELEROMETER_ROTATION, DEFAULT_ACCELEROMETER_ROTATION);
564 if (mAccelerometerDefault != accelerometerDefault) {
565 mAccelerometerDefault = accelerometerDefault;
566 updateOrientationListenerLp();
567 }
568 if (mSystemReady) {
569 int pointerLocation = Settings.System.getInt(resolver,
570 Settings.System.POINTER_LOCATION, 0);
571 if (mPointerLocationMode != pointerLocation) {
572 mPointerLocationMode = pointerLocation;
573 if (pointerLocation != 0) {
574 if (mPointerLocationView == null) {
575 mPointerLocationView = new PointerLocationView(mContext);
576 mPointerLocationView.setPrintCoords(false);
577 addView = mPointerLocationView;
578 }
579 } else {
580 removeView = mPointerLocationView;
581 mPointerLocationView = null;
582 }
583 }
584 }
585 // use screen off timeout setting as the timeout for the lockscreen
586 mLockScreenTimeout = Settings.System.getInt(resolver,
587 Settings.System.SCREEN_OFF_TIMEOUT, 0);
588 String imId = Settings.Secure.getString(resolver,
589 Settings.Secure.DEFAULT_INPUT_METHOD);
590 boolean hasSoftInput = imId != null && imId.length() > 0;
591 if (mHasSoftInput != hasSoftInput) {
592 mHasSoftInput = hasSoftInput;
593 updateRotation = true;
594 }
595 }
596 if (updateRotation) {
597 updateRotation(0);
598 }
599 if (addView != null) {
600 WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
601 WindowManager.LayoutParams.MATCH_PARENT,
602 WindowManager.LayoutParams.MATCH_PARENT);
603 lp.type = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
604 lp.flags =
605 WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE|
606 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|
607 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
608 lp.format = PixelFormat.TRANSLUCENT;
609 lp.setTitle("PointerLocation");
610 WindowManagerImpl wm = (WindowManagerImpl)
611 mContext.getSystemService(Context.WINDOW_SERVICE);
612 wm.addView(addView, lp);
613 }
614 if (removeView != null) {
615 WindowManagerImpl wm = (WindowManagerImpl)
616 mContext.getSystemService(Context.WINDOW_SERVICE);
617 wm.removeView(removeView);
618 }
619 }
620
Mike Lockwood2d1efbd2009-09-16 12:15:53 -0400621 private int readRotation(int resID) {
Mike Lockwood1753f7f2009-08-24 14:49:07 -0700622 try {
623 int rotation = mContext.getResources().getInteger(resID);
624 switch (rotation) {
625 case 0:
626 return Surface.ROTATION_0;
627 case 90:
628 return Surface.ROTATION_90;
629 case 180:
630 return Surface.ROTATION_180;
631 case 270:
632 return Surface.ROTATION_270;
Mike Lockwood1753f7f2009-08-24 14:49:07 -0700633 }
634 } catch (Resources.NotFoundException e) {
Mike Lockwood2d1efbd2009-09-16 12:15:53 -0400635 // fall through
Mike Lockwood1753f7f2009-08-24 14:49:07 -0700636 }
Mike Lockwood2d1efbd2009-09-16 12:15:53 -0400637 return -1;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800638 }
639
640 /** {@inheritDoc} */
641 public int checkAddPermission(WindowManager.LayoutParams attrs) {
642 int type = attrs.type;
Mitsuru Oshima831d0d92009-06-16 18:27:18 -0700643
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800644 if (type < WindowManager.LayoutParams.FIRST_SYSTEM_WINDOW
645 || type > WindowManager.LayoutParams.LAST_SYSTEM_WINDOW) {
646 return WindowManagerImpl.ADD_OKAY;
647 }
648 String permission = null;
649 switch (type) {
650 case TYPE_TOAST:
651 // XXX right now the app process has complete control over
652 // this... should introduce a token to let the system
653 // monitor/control what they are doing.
654 break;
655 case TYPE_INPUT_METHOD:
Dianne Hackbornb7cd8b92009-08-08 20:53:03 -0700656 case TYPE_WALLPAPER:
657 // The window manager will check these.
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800658 break;
659 case TYPE_PHONE:
660 case TYPE_PRIORITY_PHONE:
661 case TYPE_SYSTEM_ALERT:
662 case TYPE_SYSTEM_ERROR:
663 case TYPE_SYSTEM_OVERLAY:
664 permission = android.Manifest.permission.SYSTEM_ALERT_WINDOW;
665 break;
666 default:
667 permission = android.Manifest.permission.INTERNAL_SYSTEM_WINDOW;
668 }
669 if (permission != null) {
670 if (mContext.checkCallingOrSelfPermission(permission)
671 != PackageManager.PERMISSION_GRANTED) {
672 return WindowManagerImpl.ADD_PERMISSION_DENIED;
673 }
674 }
675 return WindowManagerImpl.ADD_OKAY;
676 }
677
678 public void adjustWindowParamsLw(WindowManager.LayoutParams attrs) {
679 switch (attrs.type) {
680 case TYPE_SYSTEM_OVERLAY:
681 case TYPE_TOAST:
682 // These types of windows can't receive input events.
683 attrs.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
684 | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
685 break;
686 }
687 }
688
689 void readLidState() {
690 try {
Mike Lockwood1753f7f2009-08-24 14:49:07 -0700691 int sw = mWindowManager.getSwitchState(RawInputEvent.SW_LID);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800692 if (sw >= 0) {
693 mLidOpen = sw == 0;
694 }
695 } catch (RemoteException e) {
696 // Ignore
697 }
698 }
699
Dianne Hackborn2862fff2009-09-15 22:56:29 -0700700 private int determineHiddenState(boolean lidOpen,
701 int mode, int hiddenValue, int visibleValue) {
702 switch (mode) {
703 case 1:
704 return lidOpen ? visibleValue : hiddenValue;
705 case 2:
706 return lidOpen ? hiddenValue : visibleValue;
707 }
708 return visibleValue;
709 }
710
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800711 /** {@inheritDoc} */
712 public void adjustConfigurationLw(Configuration config) {
713 readLidState();
714 final boolean lidOpen = !KEYBOARD_ALWAYS_HIDDEN && mLidOpen;
715 mPowerManager.setKeyboardVisibility(lidOpen);
Dianne Hackborn2862fff2009-09-15 22:56:29 -0700716 config.hardKeyboardHidden = determineHiddenState(lidOpen,
717 mLidKeyboardAccessibility, Configuration.HARDKEYBOARDHIDDEN_YES,
718 Configuration.HARDKEYBOARDHIDDEN_NO);
719 config.navigationHidden = determineHiddenState(lidOpen,
720 mLidNavigationAccessibility, Configuration.NAVIGATIONHIDDEN_YES,
721 Configuration.NAVIGATIONHIDDEN_NO);
722 config.keyboardHidden = (config.hardKeyboardHidden
723 == Configuration.HARDKEYBOARDHIDDEN_NO || mHasSoftInput)
724 ? Configuration.KEYBOARDHIDDEN_NO
725 : Configuration.KEYBOARDHIDDEN_YES;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800726 }
727
728 public boolean isCheekPressedAgainstScreen(MotionEvent ev) {
729 if(ev.getSize() > SLIDE_TOUCH_EVENT_SIZE_LIMIT) {
730 return true;
731 }
732 int size = ev.getHistorySize();
733 for(int i = 0; i < size; i++) {
734 if(ev.getHistoricalSize(i) > SLIDE_TOUCH_EVENT_SIZE_LIMIT) {
735 return true;
736 }
737 }
738 return false;
739 }
740
Dianne Hackbornc777e072010-02-12 13:07:59 -0800741 public void dispatchedPointerEventLw(MotionEvent ev, int targetX, int targetY) {
742 if (mPointerLocationView == null) {
743 return;
744 }
745 synchronized (mLock) {
746 if (mPointerLocationView == null) {
747 return;
748 }
749 ev.offsetLocation(targetX, targetY);
750 mPointerLocationView.addTouchEvent(ev);
751 ev.offsetLocation(-targetX, -targetY);
752 }
753 }
754
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800755 /** {@inheritDoc} */
756 public int windowTypeToLayerLw(int type) {
757 if (type >= FIRST_APPLICATION_WINDOW && type <= LAST_APPLICATION_WINDOW) {
758 return APPLICATION_LAYER;
759 }
760 switch (type) {
761 case TYPE_STATUS_BAR:
762 return STATUS_BAR_LAYER;
763 case TYPE_STATUS_BAR_PANEL:
764 return STATUS_BAR_PANEL_LAYER;
Dianne Hackborn6a294ce2009-12-03 11:37:44 -0800765 case TYPE_SYSTEM_DIALOG:
766 return SYSTEM_DIALOG_LAYER;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800767 case TYPE_SEARCH_BAR:
768 return SEARCH_BAR_LAYER;
769 case TYPE_PHONE:
770 return PHONE_LAYER;
771 case TYPE_KEYGUARD:
772 return KEYGUARD_LAYER;
773 case TYPE_KEYGUARD_DIALOG:
774 return KEYGUARD_DIALOG_LAYER;
775 case TYPE_SYSTEM_ALERT:
776 return SYSTEM_ALERT_LAYER;
777 case TYPE_SYSTEM_ERROR:
778 return SYSTEM_ERROR_LAYER;
779 case TYPE_INPUT_METHOD:
780 return INPUT_METHOD_LAYER;
781 case TYPE_INPUT_METHOD_DIALOG:
782 return INPUT_METHOD_DIALOG_LAYER;
783 case TYPE_SYSTEM_OVERLAY:
784 return SYSTEM_OVERLAY_LAYER;
785 case TYPE_PRIORITY_PHONE:
786 return PRIORITY_PHONE_LAYER;
787 case TYPE_TOAST:
788 return TOAST_LAYER;
Dianne Hackbornb7cd8b92009-08-08 20:53:03 -0700789 case TYPE_WALLPAPER:
790 return WALLPAPER_LAYER;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800791 }
792 Log.e(TAG, "Unknown window type: " + type);
793 return APPLICATION_LAYER;
794 }
795
796 /** {@inheritDoc} */
797 public int subWindowTypeToLayerLw(int type) {
798 switch (type) {
799 case TYPE_APPLICATION_PANEL:
800 case TYPE_APPLICATION_ATTACHED_DIALOG:
801 return APPLICATION_PANEL_SUBLAYER;
802 case TYPE_APPLICATION_MEDIA:
803 return APPLICATION_MEDIA_SUBLAYER;
Dianne Hackborn5cb8d792009-05-21 17:34:15 -0700804 case TYPE_APPLICATION_MEDIA_OVERLAY:
805 return APPLICATION_MEDIA_OVERLAY_SUBLAYER;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800806 case TYPE_APPLICATION_SUB_PANEL:
807 return APPLICATION_SUB_PANEL_SUBLAYER;
808 }
809 Log.e(TAG, "Unknown sub-window type: " + type);
810 return 0;
811 }
812
Dianne Hackbornabe0da02009-09-18 01:55:39 -0700813 public int getMaxWallpaperLayer() {
814 return STATUS_BAR_LAYER;
815 }
816
Dianne Hackborn01ad2f42009-09-24 19:24:56 -0700817 public boolean doesForceHide(WindowState win, WindowManager.LayoutParams attrs) {
818 return attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD;
819 }
820
821 public boolean canBeForceHidden(WindowState win, WindowManager.LayoutParams attrs) {
822 return attrs.type != WindowManager.LayoutParams.TYPE_STATUS_BAR
823 && attrs.type != WindowManager.LayoutParams.TYPE_WALLPAPER;
824 }
825
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800826 /** {@inheritDoc} */
827 public View addStartingWindow(IBinder appToken, String packageName,
828 int theme, CharSequence nonLocalizedLabel,
829 int labelRes, int icon) {
830 if (!SHOW_STARTING_ANIMATIONS) {
831 return null;
832 }
833 if (packageName == null) {
834 return null;
835 }
836
837 Context context = mContext;
838 boolean setTheme = false;
839 //Log.i(TAG, "addStartingWindow " + packageName + ": nonLocalizedLabel="
840 // + nonLocalizedLabel + " theme=" + Integer.toHexString(theme));
841 if (theme != 0 || labelRes != 0) {
842 try {
843 context = context.createPackageContext(packageName, 0);
844 if (theme != 0) {
845 context.setTheme(theme);
846 setTheme = true;
847 }
848 } catch (PackageManager.NameNotFoundException e) {
849 // Ignore
850 }
851 }
852 if (!setTheme) {
853 context.setTheme(com.android.internal.R.style.Theme);
854 }
855
856 Window win = PolicyManager.makeNewWindow(context);
857 if (win.getWindowStyle().getBoolean(
858 com.android.internal.R.styleable.Window_windowDisablePreview, false)) {
859 return null;
860 }
861
862 Resources r = context.getResources();
863 win.setTitle(r.getText(labelRes, nonLocalizedLabel));
864
865 win.setType(
866 WindowManager.LayoutParams.TYPE_APPLICATION_STARTING);
867 // Force the window flags: this is a fake window, so it is not really
868 // touchable or focusable by the user. We also add in the ALT_FOCUSABLE_IM
869 // flag because we do know that the next window will take input
870 // focus, so we want to get the IME window up on top of us right away.
871 win.setFlags(
872 WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE|
873 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|
874 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
875 WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE|
876 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|
877 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
878
Romain Guycc6828c2010-01-08 15:06:37 -0800879 win.setLayout(WindowManager.LayoutParams.MATCH_PARENT,
880 WindowManager.LayoutParams.MATCH_PARENT);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800881
882 final WindowManager.LayoutParams params = win.getAttributes();
883 params.token = appToken;
884 params.packageName = packageName;
885 params.windowAnimations = win.getWindowStyle().getResourceId(
886 com.android.internal.R.styleable.Window_windowAnimationStyle, 0);
887 params.setTitle("Starting " + packageName);
888
889 try {
890 WindowManagerImpl wm = (WindowManagerImpl)
891 context.getSystemService(Context.WINDOW_SERVICE);
892 View view = win.getDecorView();
893
894 if (win.isFloating()) {
895 // Whoops, there is no way to display an animation/preview
896 // of such a thing! After all that work... let's skip it.
897 // (Note that we must do this here because it is in
898 // getDecorView() where the theme is evaluated... maybe
899 // we should peek the floating attribute from the theme
900 // earlier.)
901 return null;
902 }
903
904 if (localLOGV) Log.v(
905 TAG, "Adding starting window for " + packageName
906 + " / " + appToken + ": "
907 + (view.getParent() != null ? view : null));
908
909 wm.addView(view, params);
910
911 // Only return the view if it was successfully added to the
912 // window manager... which we can tell by it having a parent.
913 return view.getParent() != null ? view : null;
914 } catch (WindowManagerImpl.BadTokenException e) {
915 // ignore
916 Log.w(TAG, appToken + " already running, starting window not displayed");
917 }
918
919 return null;
920 }
921
922 /** {@inheritDoc} */
923 public void removeStartingWindow(IBinder appToken, View window) {
924 // RuntimeException e = new RuntimeException();
925 // Log.i(TAG, "remove " + appToken + " " + window, e);
926
927 if (localLOGV) Log.v(
928 TAG, "Removing starting window for " + appToken + ": " + window);
929
930 if (window != null) {
931 WindowManagerImpl wm = (WindowManagerImpl) mContext.getSystemService(Context.WINDOW_SERVICE);
932 wm.removeView(window);
933 }
934 }
935
936 /**
937 * Preflight adding a window to the system.
938 *
939 * Currently enforces that three window types are singletons:
940 * <ul>
941 * <li>STATUS_BAR_TYPE</li>
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800942 * <li>KEYGUARD_TYPE</li>
943 * </ul>
944 *
945 * @param win The window to be added
946 * @param attrs Information about the window to be added
947 *
948 * @return If ok, WindowManagerImpl.ADD_OKAY. If too many singletons, WindowManagerImpl.ADD_MULTIPLE_SINGLETON
949 */
950 public int prepareAddWindowLw(WindowState win, WindowManager.LayoutParams attrs) {
951 switch (attrs.type) {
952 case TYPE_STATUS_BAR:
953 if (mStatusBar != null) {
954 return WindowManagerImpl.ADD_MULTIPLE_SINGLETON;
955 }
956 mStatusBar = win;
957 break;
Dianne Hackborn08743722009-12-21 12:16:51 -0800958 case TYPE_STATUS_BAR_PANEL:
959 mStatusBarPanels.add(win);
960 break;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800961 case TYPE_KEYGUARD:
962 if (mKeyguard != null) {
963 return WindowManagerImpl.ADD_MULTIPLE_SINGLETON;
964 }
965 mKeyguard = win;
966 break;
967 }
968 return WindowManagerImpl.ADD_OKAY;
969 }
970
971 /** {@inheritDoc} */
972 public void removeWindowLw(WindowState win) {
973 if (mStatusBar == win) {
974 mStatusBar = null;
975 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800976 else if (mKeyguard == win) {
977 mKeyguard = null;
Dianne Hackborn08743722009-12-21 12:16:51 -0800978 } else {
979 mStatusBarPanels.remove(win);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800980 }
981 }
982
983 static final boolean PRINT_ANIM = false;
984
985 /** {@inheritDoc} */
986 public int selectAnimationLw(WindowState win, int transit) {
987 if (PRINT_ANIM) Log.i(TAG, "selectAnimation in " + win
988 + ": transit=" + transit);
989 if (transit == TRANSIT_PREVIEW_DONE) {
990 if (win.hasAppShownWindows()) {
991 if (PRINT_ANIM) Log.i(TAG, "**** STARTING EXIT");
992 return com.android.internal.R.anim.app_starting_exit;
993 }
994 }
995
996 return 0;
997 }
998
Dianne Hackborn01ad2f42009-09-24 19:24:56 -0700999 public Animation createForceHideEnterAnimation() {
1000 return AnimationUtils.loadAnimation(mContext,
1001 com.android.internal.R.anim.lock_screen_behind_enter);
1002 }
1003
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001004 static ITelephony getPhoneInterface() {
1005 return ITelephony.Stub.asInterface(ServiceManager.checkService(Context.TELEPHONY_SERVICE));
1006 }
1007
1008 static IAudioService getAudioInterface() {
1009 return IAudioService.Stub.asInterface(ServiceManager.checkService(Context.AUDIO_SERVICE));
1010 }
1011
1012 boolean keyguardOn() {
1013 return keyguardIsShowingTq() || inKeyguardRestrictedKeyInputMode();
1014 }
1015
1016 private static final int[] WINDOW_TYPES_WHERE_HOME_DOESNT_WORK = {
1017 WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
1018 WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
1019 };
1020
1021 /** {@inheritDoc} */
1022 public boolean interceptKeyTi(WindowState win, int code, int metaKeys, boolean down,
Dianne Hackborn0041e972009-07-24 17:14:43 -07001023 int repeatCount, int flags) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001024 boolean keyguardOn = keyguardOn();
1025
1026 if (false) {
1027 Log.d(TAG, "interceptKeyTi code=" + code + " down=" + down + " repeatCount="
1028 + repeatCount + " keyguardOn=" + keyguardOn + " mHomePressed=" + mHomePressed);
1029 }
1030
1031 // Clear a pending HOME longpress if the user releases Home
1032 // TODO: This could probably be inside the next bit of logic, but that code
1033 // turned out to be a bit fragile so I'm doing it here explicitly, for now.
1034 if ((code == KeyEvent.KEYCODE_HOME) && !down) {
1035 mHandler.removeCallbacks(mHomeLongPress);
1036 }
1037
1038 // If the HOME button is currently being held, then we do special
1039 // chording with it.
1040 if (mHomePressed) {
1041
1042 // If we have released the home key, and didn't do anything else
1043 // while it was pressed, then it is time to go home!
1044 if (code == KeyEvent.KEYCODE_HOME) {
1045 if (!down) {
1046 mHomePressed = false;
1047
Dianne Hackborn0041e972009-07-24 17:14:43 -07001048 if ((flags&KeyEvent.FLAG_CANCELED) == 0) {
1049 // If an incoming call is ringing, HOME is totally disabled.
1050 // (The user is already on the InCallScreen at this point,
1051 // and his ONLY options are to answer or reject the call.)
1052 boolean incomingRinging = false;
1053 try {
1054 ITelephony phoneServ = getPhoneInterface();
1055 if (phoneServ != null) {
1056 incomingRinging = phoneServ.isRinging();
1057 } else {
1058 Log.w(TAG, "Unable to find ITelephony interface");
1059 }
1060 } catch (RemoteException ex) {
1061 Log.w(TAG, "RemoteException from getPhoneInterface()", ex);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001062 }
Dianne Hackborn0041e972009-07-24 17:14:43 -07001063
1064 if (incomingRinging) {
1065 Log.i(TAG, "Ignoring HOME; there's a ringing incoming call.");
1066 } else {
1067 launchHomeFromHotKey();
1068 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001069 } else {
Dianne Hackborn0041e972009-07-24 17:14:43 -07001070 Log.i(TAG, "Ignoring HOME; event canceled.");
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001071 }
1072 }
1073 }
1074
1075 return true;
1076 }
Dianne Hackborn0041e972009-07-24 17:14:43 -07001077
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001078 // First we always handle the home key here, so applications
1079 // can never break it, although if keyguard is on, we do let
1080 // it handle it, because that gives us the correct 5 second
1081 // timeout.
1082 if (code == KeyEvent.KEYCODE_HOME) {
1083
1084 // If a system window has focus, then it doesn't make sense
1085 // right now to interact with applications.
1086 WindowManager.LayoutParams attrs = win != null ? win.getAttrs() : null;
1087 if (attrs != null) {
1088 final int type = attrs.type;
1089 if (type == WindowManager.LayoutParams.TYPE_KEYGUARD
1090 || type == WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG) {
1091 // the "app" is keyguard, so give it the key
1092 return false;
1093 }
1094 final int typeCount = WINDOW_TYPES_WHERE_HOME_DOESNT_WORK.length;
1095 for (int i=0; i<typeCount; i++) {
1096 if (type == WINDOW_TYPES_WHERE_HOME_DOESNT_WORK[i]) {
1097 // don't do anything, but also don't pass it to the app
1098 return true;
1099 }
1100 }
1101 }
1102
1103 if (down && repeatCount == 0) {
1104 if (!keyguardOn) {
1105 mHandler.postDelayed(mHomeLongPress, ViewConfiguration.getGlobalActionKeyTimeout());
1106 }
1107 mHomePressed = true;
1108 }
1109 return true;
1110 } else if (code == KeyEvent.KEYCODE_MENU) {
1111 // Hijack modified menu keys for debugging features
1112 final int chordBug = KeyEvent.META_SHIFT_ON;
1113
1114 if (down && repeatCount == 0) {
1115 if (mEnableShiftMenuBugReports && (metaKeys & chordBug) == chordBug) {
1116 Intent intent = new Intent(Intent.ACTION_BUG_REPORT);
1117 mContext.sendOrderedBroadcast(intent, null);
1118 return true;
1119 } else if (SHOW_PROCESSES_ON_ALT_MENU &&
1120 (metaKeys & KeyEvent.META_ALT_ON) == KeyEvent.META_ALT_ON) {
1121 Intent service = new Intent();
1122 service.setClassName(mContext, "com.android.server.LoadAverageService");
1123 ContentResolver res = mContext.getContentResolver();
1124 boolean shown = Settings.System.getInt(
1125 res, Settings.System.SHOW_PROCESSES, 0) != 0;
1126 if (!shown) {
1127 mContext.startService(service);
1128 } else {
1129 mContext.stopService(service);
1130 }
1131 Settings.System.putInt(
1132 res, Settings.System.SHOW_PROCESSES, shown ? 0 : 1);
1133 return true;
1134 }
1135 }
1136 } else if (code == KeyEvent.KEYCODE_NOTIFICATION) {
1137 if (down) {
1138 // this key doesn't exist on current hardware, but if a device
1139 // didn't have a touchscreen, it would want one of these to open
1140 // the status bar.
1141 IStatusBar sbs = IStatusBar.Stub.asInterface(ServiceManager.getService("statusbar"));
1142 if (sbs != null) {
1143 try {
1144 sbs.toggle();
1145 } catch (RemoteException e) {
1146 // we're screwed anyway, since it's in this process
1147 throw new RuntimeException(e);
1148 }
1149 }
1150 }
1151 return true;
1152 } else if (code == KeyEvent.KEYCODE_SEARCH) {
1153 if (down) {
1154 if (repeatCount == 0) {
1155 mSearchKeyPressed = true;
1156 }
1157 } else {
1158 mSearchKeyPressed = false;
1159
1160 if (mConsumeSearchKeyUp) {
1161 // Consume the up-event
1162 mConsumeSearchKeyUp = false;
1163 return true;
1164 }
1165 }
1166 }
1167
1168 // Shortcuts are invoked through Search+key, so intercept those here
1169 if (mSearchKeyPressed) {
1170 if (down && repeatCount == 0 && !keyguardOn) {
1171 Intent shortcutIntent = mShortcutManager.getIntent(code, metaKeys);
1172 if (shortcutIntent != null) {
1173 shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1174 mContext.startActivity(shortcutIntent);
1175
1176 /*
1177 * We launched an app, so the up-event of the search key
1178 * should be consumed
1179 */
1180 mConsumeSearchKeyUp = true;
1181 return true;
1182 }
1183 }
1184 }
1185
1186 return false;
1187 }
1188
1189 /**
1190 * A home key -> launch home action was detected. Take the appropriate action
1191 * given the situation with the keyguard.
1192 */
1193 void launchHomeFromHotKey() {
Dianne Hackborn9dc06cc2009-11-17 18:19:23 -08001194 if (mKeyguardMediator.isShowingAndNotHidden()) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001195 // don't launch home if keyguard showing
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001196 } else if (!mHideLockScreen && mKeyguardMediator.isInputRestricted()) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001197 // when in keyguard restricted mode, must first verify unlock
1198 // before launching home
1199 mKeyguardMediator.verifyUnlock(new OnKeyguardExitResult() {
1200 public void onKeyguardExitResult(boolean success) {
1201 if (success) {
Dianne Hackborn256dd3b2009-05-19 18:51:21 -07001202 try {
1203 ActivityManagerNative.getDefault().stopAppSwitches();
1204 } catch (RemoteException e) {
1205 }
Joe Onoratof275f0c2009-11-24 16:11:13 -05001206 sendCloseSystemWindows(SYSTEM_DIALOG_REASON_HOME_KEY);
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001207 startDockOrHome();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001208 }
1209 }
1210 });
1211 } else {
1212 // no keyguard stuff to worry about, just launch home!
Dianne Hackborn256dd3b2009-05-19 18:51:21 -07001213 try {
1214 ActivityManagerNative.getDefault().stopAppSwitches();
1215 } catch (RemoteException e) {
1216 }
Joe Onoratof275f0c2009-11-24 16:11:13 -05001217 sendCloseSystemWindows(SYSTEM_DIALOG_REASON_HOME_KEY);
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001218 startDockOrHome();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001219 }
1220 }
1221
1222 public void getContentInsetHintLw(WindowManager.LayoutParams attrs, Rect contentInset) {
1223 final int fl = attrs.flags;
1224
1225 if ((fl &
1226 (FLAG_LAYOUT_IN_SCREEN | FLAG_FULLSCREEN | FLAG_LAYOUT_INSET_DECOR))
1227 == (FLAG_LAYOUT_IN_SCREEN | FLAG_LAYOUT_INSET_DECOR)) {
1228 contentInset.set(mCurLeft, mCurTop, mW - mCurRight, mH - mCurBottom);
1229 } else {
1230 contentInset.setEmpty();
1231 }
1232 }
1233
1234 /** {@inheritDoc} */
1235 public void beginLayoutLw(int displayWidth, int displayHeight) {
1236 mW = displayWidth;
1237 mH = displayHeight;
1238 mDockLeft = mContentLeft = mCurLeft = 0;
1239 mDockTop = mContentTop = mCurTop = 0;
1240 mDockRight = mContentRight = mCurRight = displayWidth;
1241 mDockBottom = mContentBottom = mCurBottom = displayHeight;
1242 mDockLayer = 0x10000000;
1243
1244 // decide where the status bar goes ahead of time
1245 if (mStatusBar != null) {
1246 final Rect pf = mTmpParentFrame;
1247 final Rect df = mTmpDisplayFrame;
1248 final Rect vf = mTmpVisibleFrame;
1249 pf.left = df.left = vf.left = 0;
1250 pf.top = df.top = vf.top = 0;
1251 pf.right = df.right = vf.right = displayWidth;
1252 pf.bottom = df.bottom = vf.bottom = displayHeight;
1253
1254 mStatusBar.computeFrameLw(pf, df, vf, vf);
1255 if (mStatusBar.isVisibleLw()) {
1256 // If the status bar is hidden, we don't want to cause
1257 // windows behind it to scroll.
1258 mDockTop = mContentTop = mCurTop = mStatusBar.getFrameLw().bottom;
The Android Open Source Project11267662009-03-18 17:39:47 -07001259 if (DEBUG_LAYOUT) Log.v(TAG, "Status bar: mDockBottom="
1260 + mDockBottom + " mContentBottom="
1261 + mContentBottom + " mCurBottom=" + mCurBottom);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001262 }
1263 }
1264 }
1265
1266 void setAttachedWindowFrames(WindowState win, int fl, int sim,
1267 WindowState attached, boolean insetDecors, Rect pf, Rect df, Rect cf, Rect vf) {
1268 if (win.getSurfaceLayer() > mDockLayer && attached.getSurfaceLayer() < mDockLayer) {
1269 // Here's a special case: if this attached window is a panel that is
1270 // above the dock window, and the window it is attached to is below
1271 // the dock window, then the frames we computed for the window it is
1272 // attached to can not be used because the dock is effectively part
1273 // of the underlying window and the attached window is floating on top
1274 // of the whole thing. So, we ignore the attached window and explicitly
1275 // compute the frames that would be appropriate without the dock.
1276 df.left = cf.left = vf.left = mDockLeft;
1277 df.top = cf.top = vf.top = mDockTop;
1278 df.right = cf.right = vf.right = mDockRight;
1279 df.bottom = cf.bottom = vf.bottom = mDockBottom;
1280 } else {
1281 // The effective display frame of the attached window depends on
1282 // whether it is taking care of insetting its content. If not,
1283 // we need to use the parent's content frame so that the entire
1284 // window is positioned within that content. Otherwise we can use
1285 // the display frame and let the attached window take care of
1286 // positioning its content appropriately.
1287 if ((sim & SOFT_INPUT_MASK_ADJUST) != SOFT_INPUT_ADJUST_RESIZE) {
1288 cf.set(attached.getDisplayFrameLw());
1289 } else {
1290 // If the window is resizing, then we want to base the content
1291 // frame on our attached content frame to resize... however,
1292 // things can be tricky if the attached window is NOT in resize
1293 // mode, in which case its content frame will be larger.
1294 // Ungh. So to deal with that, make sure the content frame
1295 // we end up using is not covering the IM dock.
1296 cf.set(attached.getContentFrameLw());
1297 if (attached.getSurfaceLayer() < mDockLayer) {
1298 if (cf.left < mContentLeft) cf.left = mContentLeft;
1299 if (cf.top < mContentTop) cf.top = mContentTop;
1300 if (cf.right > mContentRight) cf.right = mContentRight;
1301 if (cf.bottom > mContentBottom) cf.bottom = mContentBottom;
1302 }
1303 }
1304 df.set(insetDecors ? attached.getDisplayFrameLw() : cf);
1305 vf.set(attached.getVisibleFrameLw());
1306 }
1307 // The LAYOUT_IN_SCREEN flag is used to determine whether the attached
1308 // window should be positioned relative to its parent or the entire
1309 // screen.
1310 pf.set((fl & FLAG_LAYOUT_IN_SCREEN) == 0
1311 ? attached.getFrameLw() : df);
1312 }
1313
1314 /** {@inheritDoc} */
Dianne Hackborn0ecadf72009-03-31 18:00:37 -07001315 public void layoutWindowLw(WindowState win, WindowManager.LayoutParams attrs,
1316 WindowState attached) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001317 // we've already done the status bar
1318 if (win == mStatusBar) {
1319 return;
1320 }
1321
1322 if (false) {
1323 if ("com.google.android.youtube".equals(attrs.packageName)
1324 && attrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
1325 Log.i(TAG, "GOTCHA!");
1326 }
1327 }
1328
1329 final int fl = attrs.flags;
1330 final int sim = attrs.softInputMode;
1331
1332 final Rect pf = mTmpParentFrame;
1333 final Rect df = mTmpDisplayFrame;
1334 final Rect cf = mTmpContentFrame;
1335 final Rect vf = mTmpVisibleFrame;
1336
1337 if (attrs.type == TYPE_INPUT_METHOD) {
1338 pf.left = df.left = cf.left = vf.left = mDockLeft;
1339 pf.top = df.top = cf.top = vf.top = mDockTop;
1340 pf.right = df.right = cf.right = vf.right = mDockRight;
1341 pf.bottom = df.bottom = cf.bottom = vf.bottom = mDockBottom;
1342 // IM dock windows always go to the bottom of the screen.
1343 attrs.gravity = Gravity.BOTTOM;
1344 mDockLayer = win.getSurfaceLayer();
1345 } else {
1346 if ((fl &
1347 (FLAG_LAYOUT_IN_SCREEN | FLAG_FULLSCREEN | FLAG_LAYOUT_INSET_DECOR))
1348 == (FLAG_LAYOUT_IN_SCREEN | FLAG_LAYOUT_INSET_DECOR)) {
1349 // This is the case for a normal activity window: we want it
1350 // to cover all of the screen space, and it can take care of
1351 // moving its contents to account for screen decorations that
1352 // intrude into that space.
1353 if (attached != null) {
1354 // If this window is attached to another, our display
1355 // frame is the same as the one we are attached to.
1356 setAttachedWindowFrames(win, fl, sim, attached, true, pf, df, cf, vf);
1357 } else {
1358 pf.left = df.left = 0;
1359 pf.top = df.top = 0;
1360 pf.right = df.right = mW;
1361 pf.bottom = df.bottom = mH;
1362 if ((sim & SOFT_INPUT_MASK_ADJUST) != SOFT_INPUT_ADJUST_RESIZE) {
1363 cf.left = mDockLeft;
1364 cf.top = mDockTop;
1365 cf.right = mDockRight;
1366 cf.bottom = mDockBottom;
1367 } else {
1368 cf.left = mContentLeft;
1369 cf.top = mContentTop;
1370 cf.right = mContentRight;
1371 cf.bottom = mContentBottom;
1372 }
1373 vf.left = mCurLeft;
1374 vf.top = mCurTop;
1375 vf.right = mCurRight;
1376 vf.bottom = mCurBottom;
1377 }
1378 } else if ((fl & FLAG_LAYOUT_IN_SCREEN) != 0) {
1379 // A window that has requested to fill the entire screen just
1380 // gets everything, period.
1381 pf.left = df.left = cf.left = 0;
1382 pf.top = df.top = cf.top = 0;
1383 pf.right = df.right = cf.right = mW;
1384 pf.bottom = df.bottom = cf.bottom = mH;
1385 vf.left = mCurLeft;
1386 vf.top = mCurTop;
1387 vf.right = mCurRight;
1388 vf.bottom = mCurBottom;
1389 } else if (attached != null) {
1390 // A child window should be placed inside of the same visible
1391 // frame that its parent had.
1392 setAttachedWindowFrames(win, fl, sim, attached, false, pf, df, cf, vf);
1393 } else {
1394 // Otherwise, a normal window must be placed inside the content
1395 // of all screen decorations.
1396 pf.left = mContentLeft;
1397 pf.top = mContentTop;
1398 pf.right = mContentRight;
1399 pf.bottom = mContentBottom;
1400 if ((sim & SOFT_INPUT_MASK_ADJUST) != SOFT_INPUT_ADJUST_RESIZE) {
1401 df.left = cf.left = mDockLeft;
1402 df.top = cf.top = mDockTop;
1403 df.right = cf.right = mDockRight;
1404 df.bottom = cf.bottom = mDockBottom;
1405 } else {
1406 df.left = cf.left = mContentLeft;
1407 df.top = cf.top = mContentTop;
1408 df.right = cf.right = mContentRight;
1409 df.bottom = cf.bottom = mContentBottom;
1410 }
1411 vf.left = mCurLeft;
1412 vf.top = mCurTop;
1413 vf.right = mCurRight;
1414 vf.bottom = mCurBottom;
1415 }
1416 }
1417
1418 if ((fl & FLAG_LAYOUT_NO_LIMITS) != 0) {
1419 df.left = df.top = cf.left = cf.top = vf.left = vf.top = -10000;
1420 df.right = df.bottom = cf.right = cf.bottom = vf.right = vf.bottom = 10000;
1421 }
1422
The Android Open Source Project11267662009-03-18 17:39:47 -07001423 if (DEBUG_LAYOUT) Log.v(TAG, "Compute frame " + attrs.getTitle()
1424 + ": sim=#" + Integer.toHexString(sim)
1425 + " pf=" + pf.toShortString() + " df=" + df.toShortString()
1426 + " cf=" + cf.toShortString() + " vf=" + vf.toShortString());
1427
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001428 if (false) {
1429 if ("com.google.android.youtube".equals(attrs.packageName)
1430 && attrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
1431 if (true || localLOGV) Log.v(TAG, "Computing frame of " + win +
The Android Open Source Project11267662009-03-18 17:39:47 -07001432 ": sim=#" + Integer.toHexString(sim)
1433 + " pf=" + pf.toShortString() + " df=" + df.toShortString()
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001434 + " cf=" + cf.toShortString() + " vf=" + vf.toShortString());
1435 }
1436 }
1437
1438 win.computeFrameLw(pf, df, cf, vf);
1439
1440 // Dock windows carve out the bottom of the screen, so normal windows
1441 // can't appear underneath them.
1442 if (attrs.type == TYPE_INPUT_METHOD && !win.getGivenInsetsPendingLw()) {
1443 int top = win.getContentFrameLw().top;
1444 top += win.getGivenContentInsetsLw().top;
1445 if (mContentBottom > top) {
1446 mContentBottom = top;
1447 }
1448 top = win.getVisibleFrameLw().top;
1449 top += win.getGivenVisibleInsetsLw().top;
1450 if (mCurBottom > top) {
1451 mCurBottom = top;
1452 }
The Android Open Source Project11267662009-03-18 17:39:47 -07001453 if (DEBUG_LAYOUT) Log.v(TAG, "Input method: mDockBottom="
1454 + mDockBottom + " mContentBottom="
1455 + mContentBottom + " mCurBottom=" + mCurBottom);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001456 }
1457 }
1458
1459 /** {@inheritDoc} */
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001460 public int finishLayoutLw() {
Dianne Hackborn5f3063e2010-03-10 16:01:58 -08001461 return 0;
1462 }
1463
1464 /** {@inheritDoc} */
1465 public void beginAnimationLw(int displayWidth, int displayHeight) {
1466 mTopFullscreenOpaqueWindowState = null;
1467 mForceStatusBar = false;
1468
1469 mHideLockScreen = false;
1470 mAllowLockscreenWhenOn = false;
1471 mDismissKeyguard = false;
1472 }
1473
1474 /** {@inheritDoc} */
1475 public void animatingWindowLw(WindowState win,
1476 WindowManager.LayoutParams attrs) {
1477 if (mTopFullscreenOpaqueWindowState == null &&
1478 win.isVisibleOrBehindKeyguardLw()) {
1479 if ((attrs.flags & FLAG_FORCE_NOT_FULLSCREEN) != 0) {
1480 mForceStatusBar = true;
1481 }
1482 if (attrs.type >= FIRST_APPLICATION_WINDOW
1483 && attrs.type <= LAST_APPLICATION_WINDOW
1484 && win.fillsScreenLw(mW, mH, false, false)) {
1485 if (DEBUG_LAYOUT) Log.v(TAG, "Fullscreen window: " + win);
1486 mTopFullscreenOpaqueWindowState = win;
1487 if ((attrs.flags & FLAG_SHOW_WHEN_LOCKED) != 0) {
1488 if (localLOGV) Log.v(TAG, "Setting mHideLockScreen to true by win " + win);
1489 mHideLockScreen = true;
1490 }
1491 if ((attrs.flags & FLAG_DISMISS_KEYGUARD) != 0) {
1492 if (localLOGV) Log.v(TAG, "Setting mDismissKeyguard to true by win " + win);
1493 mDismissKeyguard = true;
1494 }
1495 if ((attrs.flags & FLAG_ALLOW_LOCK_WHILE_SCREEN_ON) != 0) {
1496 mAllowLockscreenWhenOn = true;
1497 }
1498 }
1499 }
1500 }
1501
1502 /** {@inheritDoc} */
1503 public int finishAnimationLw() {
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001504 int changes = 0;
Dianne Hackborn5f3063e2010-03-10 16:01:58 -08001505
The Android Open Source Projectc84bf282009-03-09 11:52:14 -07001506 boolean hiding = false;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001507 if (mStatusBar != null) {
Dianne Hackbornd70bc2f2009-10-06 23:25:51 -07001508 if (localLOGV) Log.i(TAG, "force=" + mForceStatusBar
1509 + " top=" + mTopFullscreenOpaqueWindowState);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001510 if (mForceStatusBar) {
Dianne Hackborn0ecadf72009-03-31 18:00:37 -07001511 if (DEBUG_LAYOUT) Log.v(TAG, "Showing status bar");
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001512 if (mStatusBar.showLw(true)) changes |= FINISH_LAYOUT_REDO_LAYOUT;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001513 } else if (mTopFullscreenOpaqueWindowState != null) {
1514 //Log.i(TAG, "frame: " + mTopFullscreenOpaqueWindowState.getFrameLw()
1515 // + " shown frame: " + mTopFullscreenOpaqueWindowState.getShownFrameLw());
1516 //Log.i(TAG, "attr: " + mTopFullscreenOpaqueWindowState.getAttrs());
1517 WindowManager.LayoutParams lp =
1518 mTopFullscreenOpaqueWindowState.getAttrs();
1519 boolean hideStatusBar =
1520 (lp.flags & WindowManager.LayoutParams.FLAG_FULLSCREEN) != 0;
1521 if (hideStatusBar) {
Dianne Hackborn0ecadf72009-03-31 18:00:37 -07001522 if (DEBUG_LAYOUT) Log.v(TAG, "Hiding status bar");
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001523 if (mStatusBar.hideLw(true)) changes |= FINISH_LAYOUT_REDO_LAYOUT;
The Android Open Source Projectc84bf282009-03-09 11:52:14 -07001524 hiding = true;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001525 } else {
Dianne Hackborn0ecadf72009-03-31 18:00:37 -07001526 if (DEBUG_LAYOUT) Log.v(TAG, "Showing status bar");
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001527 if (mStatusBar.showLw(true)) changes |= FINISH_LAYOUT_REDO_LAYOUT;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001528 }
1529 }
1530 }
Dianne Hackborn5f3063e2010-03-10 16:01:58 -08001531
1532 if (changes != 0 && hiding) {
1533 IStatusBar sbs = IStatusBar.Stub.asInterface(ServiceManager.getService("statusbar"));
1534 if (sbs != null) {
1535 try {
1536 // Make sure the window shade is hidden.
1537 sbs.deactivate();
1538 } catch (RemoteException e) {
1539 }
1540 }
1541 }
1542
Suchi Amalapurapu9cdc9032009-05-14 18:01:07 -07001543 // Hide the key guard if a visible window explicitly specifies that it wants to be displayed
1544 // when the screen is locked
1545 if (mKeyguard != null) {
Dianne Hackborn05726582009-09-22 17:28:24 -07001546 if (localLOGV) Log.v(TAG, "finishLayoutLw::mHideKeyguard="+mHideLockScreen);
1547 if (mDismissKeyguard && !mKeyguardMediator.isSecure()) {
Dianne Hackborn08743722009-12-21 12:16:51 -08001548 if (mKeyguard.hideLw(true)) {
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001549 changes |= FINISH_LAYOUT_REDO_LAYOUT
1550 | FINISH_LAYOUT_REDO_CONFIG
1551 | FINISH_LAYOUT_REDO_WALLPAPER;
1552 }
Dianne Hackborn05726582009-09-22 17:28:24 -07001553 if (mKeyguardMediator.isShowing()) {
Dianne Hackbornb446e972009-09-20 15:23:25 -07001554 mHandler.post(new Runnable() {
1555 public void run() {
Dianne Hackborn05726582009-09-22 17:28:24 -07001556 mKeyguardMediator.keyguardDone(false, false);
Dianne Hackbornb446e972009-09-20 15:23:25 -07001557 }
1558 });
1559 }
Dianne Hackborn05726582009-09-22 17:28:24 -07001560 } else if (mHideLockScreen) {
Dianne Hackborn08743722009-12-21 12:16:51 -08001561 if (mKeyguard.hideLw(true)) {
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001562 changes |= FINISH_LAYOUT_REDO_LAYOUT
1563 | FINISH_LAYOUT_REDO_CONFIG
1564 | FINISH_LAYOUT_REDO_WALLPAPER;
1565 }
Mike Lockwoodf3bfed52010-01-21 16:38:44 -05001566 mKeyguardMediator.setHidden(true);
Suchi Amalapurapu9cdc9032009-05-14 18:01:07 -07001567 } else {
Dianne Hackborn08743722009-12-21 12:16:51 -08001568 if (mKeyguard.showLw(true)) {
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001569 changes |= FINISH_LAYOUT_REDO_LAYOUT
1570 | FINISH_LAYOUT_REDO_CONFIG
1571 | FINISH_LAYOUT_REDO_WALLPAPER;
1572 }
Mike Lockwoodf3bfed52010-01-21 16:38:44 -05001573 mKeyguardMediator.setHidden(false);
Suchi Amalapurapu9cdc9032009-05-14 18:01:07 -07001574 }
1575 }
The Android Open Source Projectc84bf282009-03-09 11:52:14 -07001576
Mike Lockwood28569302010-01-28 11:54:40 -05001577 // update since mAllowLockscreenWhenOn might have changed
1578 updateLockScreenTimeout();
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001579 return changes;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001580 }
1581
Dianne Hackborn08743722009-12-21 12:16:51 -08001582 public boolean allowAppAnimationsLw() {
1583 if (mKeyguard != null && mKeyguard.isVisibleLw()) {
1584 // If keyguard is currently visible, no reason to animate
1585 // behind it.
1586 return false;
1587 }
1588 if (mStatusBar != null && mStatusBar.isVisibleLw()) {
1589 Rect rect = new Rect(mStatusBar.getShownFrameLw());
1590 for (int i=mStatusBarPanels.size()-1; i>=0; i--) {
1591 WindowState w = mStatusBarPanels.get(i);
1592 if (w.isVisibleLw()) {
1593 rect.union(w.getShownFrameLw());
1594 }
1595 }
1596 final int insetw = mW/10;
1597 final int inseth = mH/10;
1598 if (rect.contains(insetw, inseth, mW-insetw, mH-inseth)) {
1599 // All of the status bar windows put together cover the
1600 // screen, so the app can't be seen. (Note this test doesn't
1601 // work if the rects of these windows are at off offsets or
1602 // sizes, causing gaps in the rect union we have computed.)
1603 return false;
1604 }
1605 }
1606 return true;
1607 }
1608
Dianne Hackborn0ecadf72009-03-31 18:00:37 -07001609 /** {@inheritDoc} */
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001610 public boolean preprocessInputEventTq(RawInputEvent event) {
1611 switch (event.type) {
1612 case RawInputEvent.EV_SW:
Mike Lockwood1753f7f2009-08-24 14:49:07 -07001613 if (event.keycode == RawInputEvent.SW_LID) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001614 // lid changed state
1615 mLidOpen = event.value == 0;
Dianne Hackbornb446e972009-09-20 15:23:25 -07001616 boolean awakeNow = mKeyguardMediator.doLidChangeTq(mLidOpen);
Dianne Hackborn32bc91d2009-03-27 16:16:03 -07001617 updateRotation(Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
Dianne Hackbornb446e972009-09-20 15:23:25 -07001618 if (awakeNow) {
1619 // If the lid opening and we don't have to keep the
1620 // keyguard up, then we can turn on the screen
1621 // immediately.
1622 mKeyguardMediator.pokeWakelock();
1623 } else if (keyguardIsShowingTq()) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001624 if (mLidOpen) {
Dianne Hackbornb446e972009-09-20 15:23:25 -07001625 // If we are opening the lid and not hiding the
1626 // keyguard, then we need to have it turn on the
1627 // screen once it is shown.
1628 mKeyguardMediator.onWakeKeyWhenKeyguardShowingTq(
1629 KeyEvent.KEYCODE_POWER);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001630 }
1631 } else {
1632 // Light up the keyboard if we are sliding up.
1633 if (mLidOpen) {
1634 mPowerManager.userActivity(SystemClock.uptimeMillis(), false,
1635 LocalPowerManager.BUTTON_EVENT);
1636 } else {
1637 mPowerManager.userActivity(SystemClock.uptimeMillis(), false,
1638 LocalPowerManager.OTHER_EVENT);
1639 }
1640 }
1641 }
1642 }
1643 return false;
1644 }
1645
1646
1647 /** {@inheritDoc} */
1648 public boolean isAppSwitchKeyTqTiLwLi(int keycode) {
1649 return keycode == KeyEvent.KEYCODE_HOME
1650 || keycode == KeyEvent.KEYCODE_ENDCALL;
1651 }
1652
1653 /** {@inheritDoc} */
1654 public boolean isMovementKeyTi(int keycode) {
1655 switch (keycode) {
1656 case KeyEvent.KEYCODE_DPAD_UP:
1657 case KeyEvent.KEYCODE_DPAD_DOWN:
1658 case KeyEvent.KEYCODE_DPAD_LEFT:
1659 case KeyEvent.KEYCODE_DPAD_RIGHT:
1660 return true;
1661 }
1662 return false;
1663 }
1664
1665
1666 /**
1667 * @return Whether a telephone call is in progress right now.
1668 */
1669 boolean isInCall() {
1670 final ITelephony phone = getPhoneInterface();
1671 if (phone == null) {
1672 Log.w(TAG, "couldn't get ITelephony reference");
1673 return false;
1674 }
1675 try {
1676 return phone.isOffhook();
1677 } catch (RemoteException e) {
1678 Log.w(TAG, "ITelephony.isOffhhook threw RemoteException " + e);
1679 return false;
1680 }
1681 }
1682
1683 /**
1684 * @return Whether music is being played right now.
1685 */
1686 boolean isMusicActive() {
Eric Laurent413cb9d2009-07-17 11:52:43 -07001687 final AudioManager am = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
1688 if (am == null) {
1689 Log.w(TAG, "isMusicActive: couldn't get AudioManager reference");
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001690 return false;
1691 }
Eric Laurent413cb9d2009-07-17 11:52:43 -07001692 return am.isMusicActive();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001693 }
1694
1695 /**
1696 * Tell the audio service to adjust the volume appropriate to the event.
1697 * @param keycode
1698 */
Mike Lockwooda53e3812009-09-25 10:51:39 -04001699 void handleVolumeKey(int stream, int keycode) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001700 final IAudioService audio = getAudioInterface();
1701 if (audio == null) {
Mike Lockwooda53e3812009-09-25 10:51:39 -04001702 Log.w(TAG, "handleVolumeKey: couldn't get IAudioService reference");
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001703 return;
1704 }
1705 try {
1706 // since audio is playing, we shouldn't have to hold a wake lock
1707 // during the call, but we do it as a precaution for the rare possibility
1708 // that the music stops right before we call this
1709 mBroadcastWakeLock.acquire();
Mike Lockwooda53e3812009-09-25 10:51:39 -04001710 audio.adjustStreamVolume(stream,
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001711 keycode == KeyEvent.KEYCODE_VOLUME_UP
1712 ? AudioManager.ADJUST_RAISE
1713 : AudioManager.ADJUST_LOWER,
1714 0);
1715 } catch (RemoteException e) {
1716 Log.w(TAG, "IAudioService.adjustStreamVolume() threw RemoteException " + e);
1717 } finally {
1718 mBroadcastWakeLock.release();
1719 }
1720 }
1721
1722 static boolean isMediaKey(int code) {
1723 if (code == KeyEvent.KEYCODE_HEADSETHOOK ||
Andy Stadler8b89d692009-04-10 16:24:49 -07001724 code == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE ||
1725 code == KeyEvent.KEYCODE_MEDIA_STOP ||
1726 code == KeyEvent.KEYCODE_MEDIA_NEXT ||
1727 code == KeyEvent.KEYCODE_MEDIA_PREVIOUS ||
Johan Redestig2e2a3d02010-01-05 08:45:33 +01001728 code == KeyEvent.KEYCODE_MEDIA_REWIND ||
Andy Stadler8b89d692009-04-10 16:24:49 -07001729 code == KeyEvent.KEYCODE_MEDIA_FAST_FORWARD) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001730 return true;
1731 }
1732 return false;
1733 }
1734
1735 /** {@inheritDoc} */
1736 public int interceptKeyTq(RawInputEvent event, boolean screenIsOn) {
1737 int result = ACTION_PASS_TO_USER;
1738 final boolean isWakeKey = isWakeKeyTq(event);
Mike Lockwoodc72b15f2009-11-28 20:58:22 -05001739 // If screen is off then we treat the case where the keyguard is open but hidden
1740 // the same as if it were open and in front.
1741 // This will prevent any keys other than the power button from waking the screen
1742 // when the keyguard is hidden by another activity.
1743 final boolean keyguardActive = (screenIsOn ?
1744 mKeyguardMediator.isShowingAndNotHidden() :
1745 mKeyguardMediator.isShowing());
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001746
1747 if (false) {
1748 Log.d(TAG, "interceptKeyTq event=" + event + " keycode=" + event.keycode
Mike Lockwoodc72b15f2009-11-28 20:58:22 -05001749 + " screenIsOn=" + screenIsOn + " keyguardActive=" + keyguardActive);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001750 }
1751
Mike Lockwoodc72b15f2009-11-28 20:58:22 -05001752 if (keyguardActive) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001753 if (screenIsOn) {
1754 // when the screen is on, always give the event to the keyguard
1755 result |= ACTION_PASS_TO_USER;
1756 } else {
1757 // otherwise, don't pass it to the user
1758 result &= ~ACTION_PASS_TO_USER;
1759
1760 final boolean isKeyDown =
1761 (event.type == RawInputEvent.EV_KEY) && (event.value != 0);
1762 if (isWakeKey && isKeyDown) {
1763
1764 // tell the mediator about a wake key, it may decide to
1765 // turn on the screen depending on whether the key is
1766 // appropriate.
1767 if (!mKeyguardMediator.onWakeKeyWhenKeyguardShowingTq(event.keycode)
1768 && (event.keycode == KeyEvent.KEYCODE_VOLUME_DOWN
1769 || event.keycode == KeyEvent.KEYCODE_VOLUME_UP)) {
Mike Lockwood99ff0a82009-12-02 01:37:38 -05001770 // when keyguard is showing and screen off, we need
1771 // to handle the volume key for calls and music here
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001772 if (isInCall()) {
Mike Lockwood99ff0a82009-12-02 01:37:38 -05001773 handleVolumeKey(AudioManager.STREAM_VOICE_CALL, event.keycode);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001774 } else if (isMusicActive()) {
Mike Lockwooda53e3812009-09-25 10:51:39 -04001775 handleVolumeKey(AudioManager.STREAM_MUSIC, event.keycode);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001776 }
1777 }
1778 }
1779 }
1780 } else if (!screenIsOn) {
Mike Lockwooda53e3812009-09-25 10:51:39 -04001781 // If we are in-call with screen off and keyguard is not showing,
1782 // then handle the volume key ourselves.
1783 // This is necessary because the phone app will disable the keyguard
1784 // when the proximity sensor is in use.
1785 if (isInCall() && event.type == RawInputEvent.EV_KEY &&
1786 (event.keycode == KeyEvent.KEYCODE_VOLUME_DOWN
1787 || event.keycode == KeyEvent.KEYCODE_VOLUME_UP)) {
1788 result &= ~ACTION_PASS_TO_USER;
1789 handleVolumeKey(AudioManager.STREAM_VOICE_CALL, event.keycode);
1790 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001791 if (isWakeKey) {
1792 // a wake key has a sole purpose of waking the device; don't pass
1793 // it to the user
1794 result |= ACTION_POKE_USER_ACTIVITY;
1795 result &= ~ACTION_PASS_TO_USER;
1796 }
1797 }
1798
1799 int type = event.type;
1800 int code = event.keycode;
1801 boolean down = event.value != 0;
1802
1803 if (type == RawInputEvent.EV_KEY) {
Dianne Hackborn0041e972009-07-24 17:14:43 -07001804 if (code == KeyEvent.KEYCODE_ENDCALL
1805 || code == KeyEvent.KEYCODE_POWER) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001806 if (down) {
Mike Lockwood2eb47962009-10-23 11:29:52 -04001807 boolean handled = false;
David Brownbaf8d092010-03-08 21:52:59 -08001808 boolean hungUp = false;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001809 // key repeats are generated by the window manager, and we don't see them
1810 // here, so unless the driver is doing something it shouldn't be, we know
1811 // this is the real press event.
Mike Lockwood75d3caa2009-10-22 13:14:31 -04001812 ITelephony phoneServ = getPhoneInterface();
1813 if (phoneServ != null) {
Dianne Hackborn0041e972009-07-24 17:14:43 -07001814 try {
Mike Lockwood2eb47962009-10-23 11:29:52 -04001815 if (code == KeyEvent.KEYCODE_ENDCALL) {
David Brownbaf8d092010-03-08 21:52:59 -08001816 handled = hungUp = phoneServ.endCall();
1817 } else if (code == KeyEvent.KEYCODE_POWER) {
1818 if (phoneServ.isRinging()) {
1819 // Pressing Power while there's a ringing incoming
1820 // call should silence the ringer.
1821 phoneServ.silenceRinger();
1822 handled = true;
1823 } else if (phoneServ.isOffhook() &&
1824 ((mIncallPowerBehavior
1825 & Settings.Secure.INCALL_POWER_BUTTON_BEHAVIOR_HANGUP)
1826 != 0)) {
1827 // Otherwise, if "Power button ends call" is enabled,
1828 // the Power button will hang up any current active call.
1829 handled = hungUp = phoneServ.endCall();
1830 }
Dianne Hackborn0041e972009-07-24 17:14:43 -07001831 }
1832 } catch (RemoteException ex) {
Mike Lockwood75d3caa2009-10-22 13:14:31 -04001833 Log.w(TAG, "ITelephony threw RemoteException" + ex);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001834 }
Mike Lockwood75d3caa2009-10-22 13:14:31 -04001835 } else {
1836 Log.w(TAG, "!!! Unable to find ITelephony interface !!!");
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001837 }
David Brownbaf8d092010-03-08 21:52:59 -08001838
1839 if (!screenIsOn
1840 || (handled && code != KeyEvent.KEYCODE_POWER)
1841 || (handled && hungUp && code == KeyEvent.KEYCODE_POWER)) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001842 mShouldTurnOffOnKeyUp = false;
1843 } else {
1844 // only try to turn off the screen if we didn't already hang up
1845 mShouldTurnOffOnKeyUp = true;
Dianne Hackborn0041e972009-07-24 17:14:43 -07001846 mHandler.postDelayed(mPowerLongPress,
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001847 ViewConfiguration.getGlobalActionKeyTimeout());
1848 result &= ~ACTION_PASS_TO_USER;
1849 }
1850 } else {
Dianne Hackborn0041e972009-07-24 17:14:43 -07001851 mHandler.removeCallbacks(mPowerLongPress);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001852 if (mShouldTurnOffOnKeyUp) {
1853 mShouldTurnOffOnKeyUp = false;
Mike Lockwoodb4e2d1c2009-11-23 15:54:10 -05001854 boolean gohome, sleeps;
1855 if (code == KeyEvent.KEYCODE_ENDCALL) {
David Brownbaf8d092010-03-08 21:52:59 -08001856 gohome = (mEndcallBehavior
1857 & Settings.System.END_BUTTON_BEHAVIOR_HOME) != 0;
1858 sleeps = (mEndcallBehavior
1859 & Settings.System.END_BUTTON_BEHAVIOR_SLEEP) != 0;
Mike Lockwoodb4e2d1c2009-11-23 15:54:10 -05001860 } else {
1861 gohome = false;
1862 sleeps = true;
1863 }
Mike Lockwoodc72b15f2009-11-28 20:58:22 -05001864 if (keyguardActive
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001865 || (sleeps && !gohome)
1866 || (gohome && !goHome() && sleeps)) {
1867 // they must already be on the keyguad or home screen,
1868 // go to sleep instead
1869 Log.d(TAG, "I'm tired mEndcallBehavior=0x"
1870 + Integer.toHexString(mEndcallBehavior));
1871 result &= ~ACTION_POKE_USER_ACTIVITY;
1872 result |= ACTION_GO_TO_SLEEP;
1873 }
1874 result &= ~ACTION_PASS_TO_USER;
1875 }
1876 }
1877 } else if (isMediaKey(code)) {
1878 // This key needs to be handled even if the screen is off.
1879 // If others need to be handled while it's off, this is a reasonable
1880 // pattern to follow.
1881 if ((result & ACTION_PASS_TO_USER) == 0) {
1882 // Only do this if we would otherwise not pass it to the user. In that
1883 // case, the PhoneWindow class will do the same thing, except it will
1884 // only do it if the showing app doesn't process the key on its own.
1885 KeyEvent keyEvent = new KeyEvent(event.when, event.when,
1886 down ? KeyEvent.ACTION_DOWN : KeyEvent.ACTION_UP,
1887 code, 0);
1888 mBroadcastWakeLock.acquire();
1889 mHandler.post(new PassHeadsetKey(keyEvent));
1890 }
1891 } else if (code == KeyEvent.KEYCODE_CALL) {
1892 // If an incoming call is ringing, answer it!
1893 // (We handle this key here, rather than in the InCallScreen, to make
1894 // sure we'll respond to the key even if the InCallScreen hasn't come to
1895 // the foreground yet.)
1896
1897 // We answer the call on the DOWN event, to agree with
1898 // the "fallback" behavior in the InCallScreen.
1899 if (down) {
1900 try {
1901 ITelephony phoneServ = getPhoneInterface();
1902 if (phoneServ != null) {
1903 if (phoneServ.isRinging()) {
1904 Log.i(TAG, "interceptKeyTq:"
1905 + " CALL key-down while ringing: Answer the call!");
1906 phoneServ.answerRingingCall();
1907
1908 // And *don't* pass this key thru to the current activity
1909 // (which is presumably the InCallScreen.)
1910 result &= ~ACTION_PASS_TO_USER;
1911 }
1912 } else {
1913 Log.w(TAG, "CALL button: Unable to find ITelephony interface");
1914 }
1915 } catch (RemoteException ex) {
1916 Log.w(TAG, "CALL button: RemoteException from getPhoneInterface()", ex);
1917 }
1918 }
1919 } else if ((code == KeyEvent.KEYCODE_VOLUME_UP)
1920 || (code == KeyEvent.KEYCODE_VOLUME_DOWN)) {
1921 // If an incoming call is ringing, either VOLUME key means
1922 // "silence ringer". We handle these keys here, rather than
1923 // in the InCallScreen, to make sure we'll respond to them
1924 // even if the InCallScreen hasn't come to the foreground yet.
1925
1926 // Look for the DOWN event here, to agree with the "fallback"
1927 // behavior in the InCallScreen.
1928 if (down) {
1929 try {
1930 ITelephony phoneServ = getPhoneInterface();
1931 if (phoneServ != null) {
1932 if (phoneServ.isRinging()) {
1933 Log.i(TAG, "interceptKeyTq:"
1934 + " VOLUME key-down while ringing: Silence ringer!");
1935 // Silence the ringer. (It's safe to call this
1936 // even if the ringer has already been silenced.)
1937 phoneServ.silenceRinger();
1938
1939 // And *don't* pass this key thru to the current activity
1940 // (which is probably the InCallScreen.)
1941 result &= ~ACTION_PASS_TO_USER;
1942 }
1943 } else {
1944 Log.w(TAG, "VOLUME button: Unable to find ITelephony interface");
1945 }
1946 } catch (RemoteException ex) {
1947 Log.w(TAG, "VOLUME button: RemoteException from getPhoneInterface()", ex);
1948 }
1949 }
1950 }
1951 }
1952
1953 return result;
1954 }
1955
1956 class PassHeadsetKey implements Runnable {
1957 KeyEvent mKeyEvent;
1958
1959 PassHeadsetKey(KeyEvent keyEvent) {
1960 mKeyEvent = keyEvent;
1961 }
1962
1963 public void run() {
1964 if (ActivityManagerNative.isSystemReady()) {
1965 Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
1966 intent.putExtra(Intent.EXTRA_KEY_EVENT, mKeyEvent);
1967 mContext.sendOrderedBroadcast(intent, null, mBroadcastDone,
1968 mHandler, Activity.RESULT_OK, null, null);
1969 }
1970 }
1971 }
1972
1973 BroadcastReceiver mBroadcastDone = new BroadcastReceiver() {
1974 public void onReceive(Context context, Intent intent) {
1975 mBroadcastWakeLock.release();
1976 }
1977 };
Mike Lockwood1753f7f2009-08-24 14:49:07 -07001978
1979 BroadcastReceiver mDockReceiver = new BroadcastReceiver() {
1980 public void onReceive(Context context, Intent intent) {
Dianne Hackborn78968392010-03-04 20:47:56 -08001981 try {
1982 IUiModeManager uiModeService = IUiModeManager.Stub.asInterface(
1983 ServiceManager.getService(Context.UI_MODE_SERVICE));
1984 mUiMode = uiModeService.getCurrentModeType();
Dianne Hackborn78968392010-03-04 20:47:56 -08001985 } catch (RemoteException e) {
Dianne Hackborn80fa1662009-10-07 14:02:10 -07001986 }
Mike Lockwood1753f7f2009-08-24 14:49:07 -07001987 updateRotation(Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
Mike Lockwoode9867d22009-09-20 01:59:02 -04001988 updateOrientationListenerLp();
Mike Lockwood1753f7f2009-08-24 14:49:07 -07001989 }
1990 };
1991
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001992 /** {@inheritDoc} */
1993 public boolean isWakeRelMovementTq(int device, int classes,
1994 RawInputEvent event) {
1995 // if it's tagged with one of the wake bits, it wakes up the device
1996 return ((event.flags & (FLAG_WAKE | FLAG_WAKE_DROPPED)) != 0);
1997 }
1998
1999 /** {@inheritDoc} */
2000 public boolean isWakeAbsMovementTq(int device, int classes,
2001 RawInputEvent event) {
2002 // if it's tagged with one of the wake bits, it wakes up the device
2003 return ((event.flags & (FLAG_WAKE | FLAG_WAKE_DROPPED)) != 0);
2004 }
2005
2006 /**
2007 * Given the current state of the world, should this key wake up the device?
2008 */
2009 protected boolean isWakeKeyTq(RawInputEvent event) {
2010 // There are not key maps for trackball devices, but we'd still
2011 // like to have pressing it wake the device up, so force it here.
2012 int keycode = event.keycode;
2013 int flags = event.flags;
2014 if (keycode == RawInputEvent.BTN_MOUSE) {
2015 flags |= WindowManagerPolicy.FLAG_WAKE;
2016 }
2017 return (flags
2018 & (WindowManagerPolicy.FLAG_WAKE | WindowManagerPolicy.FLAG_WAKE_DROPPED)) != 0;
2019 }
2020
2021 /** {@inheritDoc} */
2022 public void screenTurnedOff(int why) {
Dianne Hackborn74489012009-03-24 20:50:09 -07002023 EventLog.writeEvent(70000, 0);
2024 mKeyguardMediator.onScreenTurnedOff(why);
The Android Open Source Project0727d222009-03-11 12:11:58 -07002025 synchronized (mLock) {
The Android Open Source Project0727d222009-03-11 12:11:58 -07002026 mScreenOn = false;
2027 updateOrientationListenerLp();
Mike Lockwood28569302010-01-28 11:54:40 -05002028 updateLockScreenTimeout();
The Android Open Source Project0727d222009-03-11 12:11:58 -07002029 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002030 }
2031
2032 /** {@inheritDoc} */
2033 public void screenTurnedOn() {
Dianne Hackborn74489012009-03-24 20:50:09 -07002034 EventLog.writeEvent(70000, 1);
2035 mKeyguardMediator.onScreenTurnedOn();
The Android Open Source Project0727d222009-03-11 12:11:58 -07002036 synchronized (mLock) {
The Android Open Source Project0727d222009-03-11 12:11:58 -07002037 mScreenOn = true;
2038 updateOrientationListenerLp();
Mike Lockwood28569302010-01-28 11:54:40 -05002039 updateLockScreenTimeout();
The Android Open Source Project0727d222009-03-11 12:11:58 -07002040 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002041 }
2042
2043 /** {@inheritDoc} */
Dianne Hackborn08743722009-12-21 12:16:51 -08002044 public boolean isScreenOn() {
2045 return mScreenOn;
2046 }
2047
2048 /** {@inheritDoc} */
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002049 public void enableKeyguard(boolean enabled) {
2050 mKeyguardMediator.setKeyguardEnabled(enabled);
2051 }
2052
2053 /** {@inheritDoc} */
2054 public void exitKeyguardSecurely(OnKeyguardExitResult callback) {
2055 mKeyguardMediator.verifyUnlock(callback);
2056 }
2057
Mike Lockwoodf7913302009-11-28 22:27:10 -05002058 private boolean keyguardIsShowingTq() {
Dianne Hackborn9dc06cc2009-11-17 18:19:23 -08002059 return mKeyguardMediator.isShowingAndNotHidden();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002060 }
2061
2062 /** {@inheritDoc} */
2063 public boolean inKeyguardRestrictedKeyInputMode() {
2064 return mKeyguardMediator.isInputRestricted();
2065 }
2066
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002067 void sendCloseSystemWindows() {
2068 sendCloseSystemWindows(mContext, null);
2069 }
2070
2071 void sendCloseSystemWindows(String reason) {
2072 sendCloseSystemWindows(mContext, reason);
2073 }
2074
2075 static void sendCloseSystemWindows(Context context, String reason) {
2076 if (ActivityManagerNative.isSystemReady()) {
Dianne Hackborn94a679d2009-07-28 17:51:07 -07002077 try {
2078 ActivityManagerNative.getDefault().closeSystemDialogs(reason);
2079 } catch (RemoteException e) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002080 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002081 }
2082 }
2083
The Android Open Source Project0727d222009-03-11 12:11:58 -07002084 public int rotationForOrientationLw(int orientation, int lastRotation,
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002085 boolean displayEnabled) {
Mitsuru Oshima831d0d92009-06-16 18:27:18 -07002086
2087 if (mPortraitRotation < 0) {
2088 // Initialize the rotation angles for each orientation once.
2089 Display d = ((WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE))
2090 .getDefaultDisplay();
2091 if (d.getWidth() > d.getHeight()) {
2092 mPortraitRotation = Surface.ROTATION_90;
2093 mLandscapeRotation = Surface.ROTATION_0;
2094 } else {
2095 mPortraitRotation = Surface.ROTATION_0;
2096 mLandscapeRotation = Surface.ROTATION_90;
2097 }
2098 }
2099
The Android Open Source Project0727d222009-03-11 12:11:58 -07002100 synchronized (mLock) {
2101 switch (orientation) {
2102 case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:
2103 //always return landscape if orientation set to landscape
Mitsuru Oshima831d0d92009-06-16 18:27:18 -07002104 return mLandscapeRotation;
The Android Open Source Project0727d222009-03-11 12:11:58 -07002105 case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT:
2106 //always return portrait if orientation set to portrait
Mitsuru Oshima831d0d92009-06-16 18:27:18 -07002107 return mPortraitRotation;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002108 }
The Android Open Source Project0727d222009-03-11 12:11:58 -07002109 // case for nosensor meaning ignore sensor and consider only lid
2110 // or orientation sensor disabled
2111 //or case.unspecified
2112 if (mLidOpen) {
Mike Lockwood1753f7f2009-08-24 14:49:07 -07002113 return mLidOpenRotation;
Dianne Hackborn78968392010-03-04 20:47:56 -08002114 } else if (mUiMode == Configuration.UI_MODE_TYPE_CAR && mCarDockRotation >= 0) {
Mike Lockwood2d1efbd2009-09-16 12:15:53 -04002115 return mCarDockRotation;
Dianne Hackborn78968392010-03-04 20:47:56 -08002116 } else if (mUiMode == Configuration.UI_MODE_TYPE_DESK && mDeskDockRotation >= 0) {
Mike Lockwood2d1efbd2009-09-16 12:15:53 -04002117 return mDeskDockRotation;
The Android Open Source Project0727d222009-03-11 12:11:58 -07002118 } else {
Dianne Hackborn03759ed2009-03-27 16:04:08 -07002119 if (useSensorForOrientationLp(orientation)) {
The Android Open Source Project0727d222009-03-11 12:11:58 -07002120 // If the user has enabled auto rotation by default, do it.
Dianne Hackborn03759ed2009-03-27 16:04:08 -07002121 int curRotation = mOrientationListener.getCurrentRotation();
2122 return curRotation >= 0 ? curRotation : lastRotation;
The Android Open Source Project0727d222009-03-11 12:11:58 -07002123 }
2124 return Surface.ROTATION_0;
2125 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002126 }
2127 }
2128
2129 public boolean detectSafeMode() {
2130 try {
2131 int menuState = mWindowManager.getKeycodeState(KeyEvent.KEYCODE_MENU);
Dianne Hackbornb1a79802009-09-29 15:18:31 -07002132 int sState = mWindowManager.getKeycodeState(KeyEvent.KEYCODE_S);
Dianne Hackborna8730f92009-11-17 15:53:36 -08002133 int dpadState = mWindowManager.getDPadKeycodeState(KeyEvent.KEYCODE_DPAD_CENTER);
2134 int trackballState = mWindowManager.getTrackballScancodeState(RawInputEvent.BTN_MOUSE);
Dianne Hackbornb1a79802009-09-29 15:18:31 -07002135 mSafeMode = menuState > 0 || sState > 0 || dpadState > 0 || trackballState > 0;
2136 performHapticFeedbackLw(null, mSafeMode
2137 ? HapticFeedbackConstants.SAFE_MODE_ENABLED
2138 : HapticFeedbackConstants.SAFE_MODE_DISABLED, true);
Dianne Hackborn73b1ec82009-10-04 13:33:00 -07002139 if (mSafeMode) {
2140 Log.i(TAG, "SAFE MODE ENABLED (menu=" + menuState + " s=" + sState
2141 + " dpad=" + dpadState + " trackball=" + trackballState + ")");
2142 } else {
2143 Log.i(TAG, "SAFE MODE not enabled");
2144 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002145 return mSafeMode;
2146 } catch (RemoteException e) {
2147 // Doom! (it's also local)
2148 throw new RuntimeException("window manager dead");
2149 }
2150 }
2151
Dianne Hackborn181ceb52009-08-27 22:16:40 -07002152 static long[] getLongIntArray(Resources r, int resid) {
2153 int[] ar = r.getIntArray(resid);
2154 if (ar == null) {
2155 return null;
2156 }
2157 long[] out = new long[ar.length];
2158 for (int i=0; i<ar.length; i++) {
2159 out[i] = ar[i];
2160 }
2161 return out;
2162 }
2163
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002164 /** {@inheritDoc} */
2165 public void systemReady() {
Dianne Hackbornb1a79802009-09-29 15:18:31 -07002166 // tell the keyguard
2167 mKeyguardMediator.onSystemReady();
2168 android.os.SystemProperties.set("dev.bootcomplete", "1");
2169 synchronized (mLock) {
2170 updateOrientationListenerLp();
Dianne Hackbornc777e072010-02-12 13:07:59 -08002171 mSystemReady = true;
2172 mHandler.post(new Runnable() {
2173 public void run() {
2174 updateSettings();
2175 }
2176 });
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002177 }
2178 }
2179
Mike Lockwood28569302010-01-28 11:54:40 -05002180 /** {@inheritDoc} */
2181 public void userActivity() {
2182 synchronized (mScreenLockTimeout) {
2183 if (mLockScreenTimerActive) {
2184 // reset the timer
2185 mHandler.removeCallbacks(mScreenLockTimeout);
2186 mHandler.postDelayed(mScreenLockTimeout, mLockScreenTimeout);
2187 }
2188 }
2189 }
2190
2191 Runnable mScreenLockTimeout = new Runnable() {
2192 public void run() {
2193 synchronized (this) {
2194 if (localLOGV) Log.v(TAG, "mScreenLockTimeout activating keyguard");
2195 mKeyguardMediator.doKeyguardTimeout();
2196 mLockScreenTimerActive = false;
2197 }
2198 }
2199 };
2200
2201 private void updateLockScreenTimeout() {
2202 synchronized (mScreenLockTimeout) {
2203 boolean enable = (mAllowLockscreenWhenOn && mScreenOn && mKeyguardMediator.isSecure());
2204 if (mLockScreenTimerActive != enable) {
2205 if (enable) {
2206 if (localLOGV) Log.v(TAG, "setting lockscreen timer");
2207 mHandler.postDelayed(mScreenLockTimeout, mLockScreenTimeout);
2208 } else {
2209 if (localLOGV) Log.v(TAG, "clearing lockscreen timer");
2210 mHandler.removeCallbacks(mScreenLockTimeout);
2211 }
2212 mLockScreenTimerActive = enable;
2213 }
2214 }
2215 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002216
2217 /** {@inheritDoc} */
2218 public void enableScreenAfterBoot() {
2219 readLidState();
Dianne Hackborn32bc91d2009-03-27 16:16:03 -07002220 updateRotation(Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002221 }
Dianne Hackbornf54c8f92009-09-19 14:03:57 -07002222
Dianne Hackborn03759ed2009-03-27 16:04:08 -07002223 void updateRotation(int animFlags) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002224 mPowerManager.setKeyboardVisibility(mLidOpen);
Mike Lockwood1753f7f2009-08-24 14:49:07 -07002225 int rotation = Surface.ROTATION_0;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002226 if (mLidOpen) {
Mike Lockwood1753f7f2009-08-24 14:49:07 -07002227 rotation = mLidOpenRotation;
Dianne Hackborn78968392010-03-04 20:47:56 -08002228 } else if (mUiMode == Configuration.UI_MODE_TYPE_CAR && mCarDockRotation >= 0) {
Mike Lockwood2d1efbd2009-09-16 12:15:53 -04002229 rotation = mCarDockRotation;
Dianne Hackborn78968392010-03-04 20:47:56 -08002230 } else if (mUiMode == Configuration.UI_MODE_TYPE_DESK && mDeskDockRotation >= 0) {
Mike Lockwood2d1efbd2009-09-16 12:15:53 -04002231 rotation = mDeskDockRotation;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002232 }
2233 //if lid is closed orientation will be portrait
2234 try {
2235 //set orientation on WindowManager
Dianne Hackborn32bc91d2009-03-27 16:16:03 -07002236 mWindowManager.setRotation(rotation, true,
2237 mFancyRotationAnimation | animFlags);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002238 } catch (RemoteException e) {
2239 // Ignore
2240 }
2241 }
2242
2243 /**
Dianne Hackborn39c2d712009-09-22 11:41:31 -07002244 * Return an Intent to launch the currently active dock as home. Returns
2245 * null if the standard home should be launched.
2246 * @return
2247 */
2248 Intent createHomeDockIntent() {
Dianne Hackborn39c2d712009-09-22 11:41:31 -07002249 Intent intent;
Dianne Hackborn78968392010-03-04 20:47:56 -08002250 if (mUiMode == Configuration.UI_MODE_TYPE_CAR) {
Dianne Hackborn39c2d712009-09-22 11:41:31 -07002251 intent = mCarDockIntent;
Dianne Hackborn78968392010-03-04 20:47:56 -08002252 } else if (mUiMode == Configuration.UI_MODE_TYPE_DESK) {
Dianne Hackborn39c2d712009-09-22 11:41:31 -07002253 intent = mDeskDockIntent;
2254 } else {
Dianne Hackborn39c2d712009-09-22 11:41:31 -07002255 return null;
2256 }
2257
2258 ActivityInfo ai = intent.resolveActivityInfo(
2259 mContext.getPackageManager(), PackageManager.GET_META_DATA);
2260 if (ai == null) {
2261 return null;
2262 }
2263
2264 if (ai.metaData != null && ai.metaData.getBoolean(Intent.METADATA_DOCK_HOME)) {
2265 intent = new Intent(intent);
2266 intent.setClassName(ai.packageName, ai.name);
2267 return intent;
2268 }
2269
2270 return null;
2271 }
2272
2273 void startDockOrHome() {
2274 Intent dock = createHomeDockIntent();
2275 if (dock != null) {
2276 try {
2277 mContext.startActivity(dock);
2278 return;
2279 } catch (ActivityNotFoundException e) {
2280 }
2281 }
2282 mContext.startActivity(mHomeIntent);
2283 }
2284
2285 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002286 * goes to the home screen
2287 * @return whether it did anything
2288 */
2289 boolean goHome() {
2290 if (false) {
2291 // This code always brings home to the front.
Dianne Hackborn256dd3b2009-05-19 18:51:21 -07002292 try {
2293 ActivityManagerNative.getDefault().stopAppSwitches();
2294 } catch (RemoteException e) {
2295 }
Dianne Hackborn94a679d2009-07-28 17:51:07 -07002296 sendCloseSystemWindows();
Dianne Hackborn39c2d712009-09-22 11:41:31 -07002297 startDockOrHome();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002298 } else {
2299 // This code brings home to the front or, if it is already
2300 // at the front, puts the device to sleep.
2301 try {
Wink Savilled2e6a332010-02-12 12:12:06 -08002302 if (SystemProperties.getInt("persist.sys.uts-test-mode", 0) == 1) {
2303 /// Roll back EndcallBehavior as the cupcake design to pass P1 lab entry.
2304 Log.d(TAG, "UTS-TEST-MODE");
2305 } else {
2306 ActivityManagerNative.getDefault().stopAppSwitches();
2307 sendCloseSystemWindows();
2308 Intent dock = createHomeDockIntent();
2309 if (dock != null) {
2310 int result = ActivityManagerNative.getDefault()
2311 .startActivity(null, dock,
2312 dock.resolveTypeIfNeeded(mContext.getContentResolver()),
2313 null, 0, null, null, 0, true /* onlyIfNeeded*/, false);
2314 if (result == IActivityManager.START_RETURN_INTENT_TO_CALLER) {
2315 return false;
2316 }
Dianne Hackborn39c2d712009-09-22 11:41:31 -07002317 }
2318 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002319 int result = ActivityManagerNative.getDefault()
2320 .startActivity(null, mHomeIntent,
2321 mHomeIntent.resolveTypeIfNeeded(mContext.getContentResolver()),
2322 null, 0, null, null, 0, true /* onlyIfNeeded*/, false);
2323 if (result == IActivityManager.START_RETURN_INTENT_TO_CALLER) {
2324 return false;
2325 }
2326 } catch (RemoteException ex) {
2327 // bummer, the activity manager, which is in this process, is dead
2328 }
2329 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002330 return true;
2331 }
2332
The Android Open Source Project0727d222009-03-11 12:11:58 -07002333 public void setCurrentOrientationLw(int newOrientation) {
2334 synchronized (mLock) {
2335 if (newOrientation != mCurrentAppOrientation) {
2336 mCurrentAppOrientation = newOrientation;
2337 updateOrientationListenerLp();
2338 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002339 }
2340 }
Amith Yamasanic33cb712010-02-10 15:21:49 -08002341
The Android Open Source Project0727d222009-03-11 12:11:58 -07002342 public boolean performHapticFeedbackLw(WindowState win, int effectId, boolean always) {
Jim Miller89b17222009-10-27 14:54:09 -07002343 final boolean hapticsDisabled = Settings.System.getInt(mContext.getContentResolver(),
2344 Settings.System.HAPTIC_FEEDBACK_ENABLED, 0) == 0;
Dianne Hackborn9dc06cc2009-11-17 18:19:23 -08002345 if (!always && (hapticsDisabled || mKeyguardMediator.isShowingAndNotHidden())) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002346 return false;
2347 }
Amith Yamasanic33cb712010-02-10 15:21:49 -08002348 long[] pattern = null;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002349 switch (effectId) {
2350 case HapticFeedbackConstants.LONG_PRESS:
Amith Yamasanic33cb712010-02-10 15:21:49 -08002351 pattern = mLongPressVibePattern;
2352 break;
Dianne Hackbornb1a79802009-09-29 15:18:31 -07002353 case HapticFeedbackConstants.VIRTUAL_KEY:
Amith Yamasanic33cb712010-02-10 15:21:49 -08002354 pattern = mVirtualKeyVibePattern;
2355 break;
2356 case HapticFeedbackConstants.KEYBOARD_TAP:
2357 pattern = mKeyboardTapVibePattern;
2358 break;
Dianne Hackbornb1a79802009-09-29 15:18:31 -07002359 case HapticFeedbackConstants.SAFE_MODE_DISABLED:
Amith Yamasanic33cb712010-02-10 15:21:49 -08002360 pattern = mSafeModeDisabledVibePattern;
2361 break;
Dianne Hackbornb1a79802009-09-29 15:18:31 -07002362 case HapticFeedbackConstants.SAFE_MODE_ENABLED:
Amith Yamasanic33cb712010-02-10 15:21:49 -08002363 pattern = mSafeModeEnabledVibePattern;
2364 break;
Adam Powell9d86faf2010-02-10 11:39:57 -08002365 case HapticFeedbackConstants.SCROLL_BARRIER:
Amith Yamasanic33cb712010-02-10 15:21:49 -08002366 pattern = mScrollBarrierVibePattern;
2367 break;
2368 default:
2369 return false;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002370 }
Amith Yamasanic33cb712010-02-10 15:21:49 -08002371 if (pattern.length == 1) {
2372 // One-shot vibration
2373 mVibrator.vibrate(pattern[0]);
2374 } else {
2375 // Pattern vibration
2376 mVibrator.vibrate(pattern, -1);
2377 }
2378 return true;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002379 }
2380
Dianne Hackborn0041e972009-07-24 17:14:43 -07002381 public void keyFeedbackFromInput(KeyEvent event) {
2382 if (event.getAction() == KeyEvent.ACTION_DOWN
2383 && (event.getFlags()&KeyEvent.FLAG_VIRTUAL_HARD_KEY) != 0) {
Dianne Hackbornb1a79802009-09-29 15:18:31 -07002384 performHapticFeedbackLw(null, HapticFeedbackConstants.VIRTUAL_KEY, false);
Dianne Hackborn0041e972009-07-24 17:14:43 -07002385 }
2386 }
2387
The Android Open Source Project0727d222009-03-11 12:11:58 -07002388 public void screenOnStoppedLw() {
Dianne Hackborn9dc06cc2009-11-17 18:19:23 -08002389 if (!mKeyguardMediator.isShowingAndNotHidden() && mPowerManager.isScreenOn()) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002390 long curTime = SystemClock.uptimeMillis();
2391 mPowerManager.userActivity(curTime, false, LocalPowerManager.OTHER_EVENT);
2392 }
2393 }
Mike Lockwoodfcb39242009-10-21 23:01:07 -04002394
2395 public boolean allowKeyRepeat() {
2396 // disable key repeat when screen is off
2397 return mScreenOn;
2398 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002399}