blob: 06997019b8211167df078f79621f31a8b8d2f126 [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
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800263 static final int ENDCALL_HOME = 0x1;
264 static final int ENDCALL_SLEEPS = 0x2;
265 static final int DEFAULT_ENDCALL_BEHAVIOR = ENDCALL_SLEEPS;
266 int mEndcallBehavior;
Dianne Hackborn03759ed2009-03-27 16:04:08 -0700267
Mitsuru Oshima831d0d92009-06-16 18:27:18 -0700268 int mLandscapeRotation = -1;
269 int mPortraitRotation = -1;
270
Dianne Hackborn03759ed2009-03-27 16:04:08 -0700271 // Nothing to see here, move along...
272 int mFancyRotationAnimation;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800273
274 ShortcutManager mShortcutManager;
275 PowerManager.WakeLock mBroadcastWakeLock;
276
277 class SettingsObserver extends ContentObserver {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800278 SettingsObserver(Handler handler) {
279 super(handler);
280 }
281
282 void observe() {
283 ContentResolver resolver = mContext.getContentResolver();
284 resolver.registerContentObserver(Settings.System.getUriFor(
285 Settings.System.END_BUTTON_BEHAVIOR), false, this);
286 resolver.registerContentObserver(Settings.System.getUriFor(
287 Settings.System.ACCELEROMETER_ROTATION), false, this);
Mike Lockwood28569302010-01-28 11:54:40 -0500288 resolver.registerContentObserver(Settings.System.getUriFor(
289 Settings.System.SCREEN_OFF_TIMEOUT), false, this);
Dianne Hackbornc777e072010-02-12 13:07:59 -0800290 resolver.registerContentObserver(Settings.System.getUriFor(
291 Settings.System.POINTER_LOCATION), false, this);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800292 resolver.registerContentObserver(Settings.Secure.getUriFor(
293 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
Dianne Hackborn03759ed2009-03-27 16:04:08 -0700294 resolver.registerContentObserver(Settings.System.getUriFor(
295 "fancy_rotation_anim"), false, this);
Dianne Hackbornc777e072010-02-12 13:07:59 -0800296 updateSettings();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800297 }
298
299 @Override public void onChange(boolean selfChange) {
Dianne Hackbornc777e072010-02-12 13:07:59 -0800300 updateSettings();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800301 try {
Dianne Hackborn32bc91d2009-03-27 16:16:03 -0700302 mWindowManager.setRotation(USE_LAST_ROTATION, false,
303 mFancyRotationAnimation);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800304 } catch (RemoteException e) {
305 // Ignore
306 }
307 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800308 }
309
310 class MyOrientationListener extends WindowOrientationListener {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800311 MyOrientationListener(Context context) {
312 super(context);
313 }
314
315 @Override
The Android Open Source Projectd06b0972009-03-13 13:04:23 -0700316 public void onOrientationChanged(int rotation) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800317 // Send updates based on orientation value
Dianne Hackborn05726582009-09-22 17:28:24 -0700318 if (localLOGV) Log.v(TAG, "onOrientationChanged, rotation changed to " +rotation);
Dianne Hackborn03759ed2009-03-27 16:04:08 -0700319 try {
Dianne Hackborn32bc91d2009-03-27 16:16:03 -0700320 mWindowManager.setRotation(rotation, false,
321 mFancyRotationAnimation);
Dianne Hackborn03759ed2009-03-27 16:04:08 -0700322 } catch (RemoteException e) {
323 // Ignore
Wink Saville37c124c2009-04-02 01:37:02 -0700324
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800325 }
326 }
327 }
328 MyOrientationListener mOrientationListener;
329
Dianne Hackborn03759ed2009-03-27 16:04:08 -0700330 boolean useSensorForOrientationLp(int appOrientation) {
Joe Onorato1b58b3e2009-09-22 16:44:16 -0700331 // The app says use the sensor.
Dianne Hackborn03759ed2009-03-27 16:04:08 -0700332 if (appOrientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800333 return true;
334 }
Joe Onorato1b58b3e2009-09-22 16:44:16 -0700335 // The user preference says we can rotate, and the app is willing to rotate.
336 if (mAccelerometerDefault != 0 &&
337 (appOrientation == ActivityInfo.SCREEN_ORIENTATION_USER
338 || appOrientation == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800339 return true;
340 }
Joe Onorato1b58b3e2009-09-22 16:44:16 -0700341 // We're in a dock that has a rotation affinity, an the app is willing to rotate.
Dianne Hackborn78968392010-03-04 20:47:56 -0800342 if ((mCarDockEnablesAccelerometer && mUiMode == Configuration.UI_MODE_TYPE_CAR)
343 || (mDeskDockEnablesAccelerometer && mUiMode == Configuration.UI_MODE_TYPE_DESK)) {
Joe Onorato1b58b3e2009-09-22 16:44:16 -0700344 // Note we override the nosensor flag here.
345 if (appOrientation == ActivityInfo.SCREEN_ORIENTATION_USER
346 || appOrientation == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
347 || appOrientation == ActivityInfo.SCREEN_ORIENTATION_NOSENSOR) {
348 return true;
349 }
350 }
351 // Else, don't use the sensor.
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800352 return false;
353 }
354
Suchi Amalapurapua9e1b162009-03-24 22:24:47 -0700355 /*
356 * We always let the sensor be switched on by default except when
357 * the user has explicitly disabled sensor based rotation or when the
358 * screen is switched off.
359 */
The Android Open Source Project0727d222009-03-11 12:11:58 -0700360 boolean needSensorRunningLp() {
361 if (mCurrentAppOrientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800362 // If the application has explicitly requested to follow the
363 // orientation, then we need to turn the sensor or.
364 return true;
365 }
Dianne Hackborn78968392010-03-04 20:47:56 -0800366 if ((mCarDockEnablesAccelerometer && mUiMode == Configuration.UI_MODE_TYPE_CAR) ||
367 (mDeskDockEnablesAccelerometer && mUiMode == Configuration.UI_MODE_TYPE_DESK)) {
Mike Lockwoode9867d22009-09-20 01:59:02 -0400368 // enable accelerometer if we are docked in a dock that enables accelerometer
369 // orientation management,
370 return true;
371 }
Suchi Amalapurapua9e1b162009-03-24 22:24:47 -0700372 if (mAccelerometerDefault == 0) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800373 // If the setting for using the sensor by default is enabled, then
374 // we will always leave it on. Note that the user could go to
375 // a window that forces an orientation that does not use the
376 // sensor and in theory we could turn it off... however, when next
377 // turning it on we won't have a good value for the current
378 // orientation for a little bit, which can cause orientation
379 // changes to lag, so we'd like to keep it always on. (It will
380 // still be turned off when the screen is off.)
Suchi Amalapurapua9e1b162009-03-24 22:24:47 -0700381 return false;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800382 }
Suchi Amalapurapua9e1b162009-03-24 22:24:47 -0700383 return true;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800384 }
385
386 /*
387 * Various use cases for invoking this function
388 * screen turning off, should always disable listeners if already enabled
389 * screen turned on and current app has sensor based orientation, enable listeners
390 * if not already enabled
391 * screen turned on and current app does not have sensor orientation, disable listeners if
392 * already enabled
393 * screen turning on and current app has sensor based orientation, enable listeners if needed
394 * screen turning on and current app has nosensor based orientation, do nothing
395 */
The Android Open Source Project0727d222009-03-11 12:11:58 -0700396 void updateOrientationListenerLp() {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800397 if (!mOrientationListener.canDetectOrientation()) {
398 // If sensor is turned off or nonexistent for some reason
399 return;
400 }
401 //Could have been invoked due to screen turning on or off or
402 //change of the currently visible window's orientation
Dianne Hackborn05726582009-09-22 17:28:24 -0700403 if (localLOGV) Log.v(TAG, "Screen status="+mScreenOn+
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800404 ", current orientation="+mCurrentAppOrientation+
405 ", SensorEnabled="+mOrientationSensorEnabled);
406 boolean disable = true;
The Android Open Source Project0727d222009-03-11 12:11:58 -0700407 if (mScreenOn) {
408 if (needSensorRunningLp()) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800409 disable = false;
410 //enable listener if not already enabled
The Android Open Source Project0727d222009-03-11 12:11:58 -0700411 if (!mOrientationSensorEnabled) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800412 mOrientationListener.enable();
Dianne Hackborn05726582009-09-22 17:28:24 -0700413 if(localLOGV) Log.v(TAG, "Enabling listeners");
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800414 mOrientationSensorEnabled = true;
415 }
416 }
417 }
418 //check if sensors need to be disabled
The Android Open Source Project0727d222009-03-11 12:11:58 -0700419 if (disable && mOrientationSensorEnabled) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800420 mOrientationListener.disable();
Dianne Hackborn05726582009-09-22 17:28:24 -0700421 if(localLOGV) Log.v(TAG, "Disabling listeners");
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800422 mOrientationSensorEnabled = false;
423 }
424 }
425
Dianne Hackborn0041e972009-07-24 17:14:43 -0700426 Runnable mPowerLongPress = new Runnable() {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800427 public void run() {
428 mShouldTurnOffOnKeyUp = false;
The Android Open Source Project0727d222009-03-11 12:11:58 -0700429 performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800430 sendCloseSystemWindows(SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS);
431 showGlobalActionsDialog();
432 }
433 };
434
435 void showGlobalActionsDialog() {
436 if (mGlobalActions == null) {
437 mGlobalActions = new GlobalActions(mContext);
438 }
Dianne Hackborn9dc06cc2009-11-17 18:19:23 -0800439 final boolean keyguardShowing = mKeyguardMediator.isShowingAndNotHidden();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800440 mGlobalActions.showDialog(keyguardShowing, isDeviceProvisioned());
441 if (keyguardShowing) {
442 // since it took two seconds of long press to bring this up,
443 // poke the wake lock so they have some time to see the dialog.
444 mKeyguardMediator.pokeWakelock();
445 }
446 }
447
448 boolean isDeviceProvisioned() {
449 return Settings.Secure.getInt(
450 mContext.getContentResolver(), Settings.Secure.DEVICE_PROVISIONED, 0) != 0;
451 }
452
453 /**
454 * When a home-key longpress expires, close other system windows and launch the recent apps
455 */
456 Runnable mHomeLongPress = new Runnable() {
457 public void run() {
458 /*
459 * Eat the longpress so it won't dismiss the recent apps dialog when
460 * the user lets go of the home key
461 */
462 mHomePressed = false;
The Android Open Source Project0727d222009-03-11 12:11:58 -0700463 performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800464 sendCloseSystemWindows(SYSTEM_DIALOG_REASON_RECENT_APPS);
465 showRecentAppsDialog();
466 }
467 };
468
469 /**
470 * Create (if necessary) and launch the recent apps dialog
471 */
472 void showRecentAppsDialog() {
473 if (mRecentAppsDialog == null) {
474 mRecentAppsDialog = new RecentApplicationsDialog(mContext);
475 }
476 mRecentAppsDialog.show();
477 }
478
479 /** {@inheritDoc} */
480 public void init(Context context, IWindowManager windowManager,
481 LocalPowerManager powerManager) {
482 mContext = context;
483 mWindowManager = windowManager;
484 mPowerManager = powerManager;
485 mKeyguardMediator = new KeyguardViewMediator(context, this, powerManager);
486 mHandler = new Handler();
487 mOrientationListener = new MyOrientationListener(mContext);
488 SettingsObserver settingsObserver = new SettingsObserver(mHandler);
489 settingsObserver.observe();
490 mShortcutManager = new ShortcutManager(context, mHandler);
491 mShortcutManager.observe();
492 mHomeIntent = new Intent(Intent.ACTION_MAIN, null);
493 mHomeIntent.addCategory(Intent.CATEGORY_HOME);
494 mHomeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
495 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Dianne Hackborn39c2d712009-09-22 11:41:31 -0700496 mCarDockIntent = new Intent(Intent.ACTION_MAIN, null);
497 mCarDockIntent.addCategory(Intent.CATEGORY_CAR_DOCK);
498 mCarDockIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
499 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
500 mDeskDockIntent = new Intent(Intent.ACTION_MAIN, null);
501 mDeskDockIntent.addCategory(Intent.CATEGORY_DESK_DOCK);
502 mDeskDockIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
503 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800504 PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
505 mBroadcastWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
506 "PhoneWindowManager.mBroadcastWakeLock");
507 mEnableShiftMenuBugReports = "1".equals(SystemProperties.get("ro.debuggable"));
Dianne Hackborn2862fff2009-09-15 22:56:29 -0700508 mLidOpenRotation = readRotation(
Mike Lockwood2d1efbd2009-09-16 12:15:53 -0400509 com.android.internal.R.integer.config_lidOpenRotation);
510 mCarDockRotation = readRotation(
511 com.android.internal.R.integer.config_carDockRotation);
512 mDeskDockRotation = readRotation(
513 com.android.internal.R.integer.config_deskDockRotation);
Mike Lockwoode9867d22009-09-20 01:59:02 -0400514 mCarDockEnablesAccelerometer = mContext.getResources().getBoolean(
515 com.android.internal.R.bool.config_carDockEnablesAccelerometer);
516 mDeskDockEnablesAccelerometer = mContext.getResources().getBoolean(
517 com.android.internal.R.bool.config_deskDockEnablesAccelerometer);
Dianne Hackborn2862fff2009-09-15 22:56:29 -0700518 mLidKeyboardAccessibility = mContext.getResources().getInteger(
519 com.android.internal.R.integer.config_lidKeyboardAccessibility);
520 mLidNavigationAccessibility = mContext.getResources().getInteger(
521 com.android.internal.R.integer.config_lidNavigationAccessibility);
Mike Lockwood1753f7f2009-08-24 14:49:07 -0700522 // register for dock events
Dianne Hackborn78968392010-03-04 20:47:56 -0800523 IntentFilter filter = new IntentFilter();
524 filter.addAction(UiModeManager.ACTION_ENTER_CAR_MODE);
525 filter.addAction(UiModeManager.ACTION_EXIT_CAR_MODE);
526 filter.addAction(UiModeManager.ACTION_ENTER_DESK_MODE);
527 filter.addAction(UiModeManager.ACTION_EXIT_DESK_MODE);
528 context.registerReceiver(mDockReceiver, filter);
Dianne Hackbornb1a79802009-09-29 15:18:31 -0700529 mVibrator = new Vibrator();
530 mLongPressVibePattern = getLongIntArray(mContext.getResources(),
531 com.android.internal.R.array.config_longPressVibePattern);
532 mVirtualKeyVibePattern = getLongIntArray(mContext.getResources(),
533 com.android.internal.R.array.config_virtualKeyVibePattern);
Amith Yamasanic33cb712010-02-10 15:21:49 -0800534 mKeyboardTapVibePattern = getLongIntArray(mContext.getResources(),
535 com.android.internal.R.array.config_keyboardTapVibePattern);
Dianne Hackbornb1a79802009-09-29 15:18:31 -0700536 mSafeModeDisabledVibePattern = getLongIntArray(mContext.getResources(),
537 com.android.internal.R.array.config_safeModeDisabledVibePattern);
538 mSafeModeEnabledVibePattern = getLongIntArray(mContext.getResources(),
539 com.android.internal.R.array.config_safeModeEnabledVibePattern);
Adam Powell9d86faf2010-02-10 11:39:57 -0800540 mScrollBarrierVibePattern = getLongIntArray(mContext.getResources(),
541 com.android.internal.R.array.config_scrollBarrierVibePattern);
Mike Lockwood1753f7f2009-08-24 14:49:07 -0700542 }
543
Dianne Hackbornc777e072010-02-12 13:07:59 -0800544 public void updateSettings() {
545 ContentResolver resolver = mContext.getContentResolver();
546 boolean updateRotation = false;
547 View addView = null;
548 View removeView = null;
549 synchronized (mLock) {
550 mEndcallBehavior = Settings.System.getInt(resolver,
551 Settings.System.END_BUTTON_BEHAVIOR, DEFAULT_ENDCALL_BEHAVIOR);
552 mFancyRotationAnimation = Settings.System.getInt(resolver,
553 "fancy_rotation_anim", 0) != 0 ? 0x80 : 0;
554 int accelerometerDefault = Settings.System.getInt(resolver,
555 Settings.System.ACCELEROMETER_ROTATION, DEFAULT_ACCELEROMETER_ROTATION);
556 if (mAccelerometerDefault != accelerometerDefault) {
557 mAccelerometerDefault = accelerometerDefault;
558 updateOrientationListenerLp();
559 }
560 if (mSystemReady) {
561 int pointerLocation = Settings.System.getInt(resolver,
562 Settings.System.POINTER_LOCATION, 0);
563 if (mPointerLocationMode != pointerLocation) {
564 mPointerLocationMode = pointerLocation;
565 if (pointerLocation != 0) {
566 if (mPointerLocationView == null) {
567 mPointerLocationView = new PointerLocationView(mContext);
568 mPointerLocationView.setPrintCoords(false);
569 addView = mPointerLocationView;
570 }
571 } else {
572 removeView = mPointerLocationView;
573 mPointerLocationView = null;
574 }
575 }
576 }
577 // use screen off timeout setting as the timeout for the lockscreen
578 mLockScreenTimeout = Settings.System.getInt(resolver,
579 Settings.System.SCREEN_OFF_TIMEOUT, 0);
580 String imId = Settings.Secure.getString(resolver,
581 Settings.Secure.DEFAULT_INPUT_METHOD);
582 boolean hasSoftInput = imId != null && imId.length() > 0;
583 if (mHasSoftInput != hasSoftInput) {
584 mHasSoftInput = hasSoftInput;
585 updateRotation = true;
586 }
587 }
588 if (updateRotation) {
589 updateRotation(0);
590 }
591 if (addView != null) {
592 WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
593 WindowManager.LayoutParams.MATCH_PARENT,
594 WindowManager.LayoutParams.MATCH_PARENT);
595 lp.type = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
596 lp.flags =
597 WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE|
598 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|
599 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
600 lp.format = PixelFormat.TRANSLUCENT;
601 lp.setTitle("PointerLocation");
602 WindowManagerImpl wm = (WindowManagerImpl)
603 mContext.getSystemService(Context.WINDOW_SERVICE);
604 wm.addView(addView, lp);
605 }
606 if (removeView != null) {
607 WindowManagerImpl wm = (WindowManagerImpl)
608 mContext.getSystemService(Context.WINDOW_SERVICE);
609 wm.removeView(removeView);
610 }
611 }
612
Mike Lockwood2d1efbd2009-09-16 12:15:53 -0400613 private int readRotation(int resID) {
Mike Lockwood1753f7f2009-08-24 14:49:07 -0700614 try {
615 int rotation = mContext.getResources().getInteger(resID);
616 switch (rotation) {
617 case 0:
618 return Surface.ROTATION_0;
619 case 90:
620 return Surface.ROTATION_90;
621 case 180:
622 return Surface.ROTATION_180;
623 case 270:
624 return Surface.ROTATION_270;
Mike Lockwood1753f7f2009-08-24 14:49:07 -0700625 }
626 } catch (Resources.NotFoundException e) {
Mike Lockwood2d1efbd2009-09-16 12:15:53 -0400627 // fall through
Mike Lockwood1753f7f2009-08-24 14:49:07 -0700628 }
Mike Lockwood2d1efbd2009-09-16 12:15:53 -0400629 return -1;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800630 }
631
632 /** {@inheritDoc} */
633 public int checkAddPermission(WindowManager.LayoutParams attrs) {
634 int type = attrs.type;
Mitsuru Oshima831d0d92009-06-16 18:27:18 -0700635
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800636 if (type < WindowManager.LayoutParams.FIRST_SYSTEM_WINDOW
637 || type > WindowManager.LayoutParams.LAST_SYSTEM_WINDOW) {
638 return WindowManagerImpl.ADD_OKAY;
639 }
640 String permission = null;
641 switch (type) {
642 case TYPE_TOAST:
643 // XXX right now the app process has complete control over
644 // this... should introduce a token to let the system
645 // monitor/control what they are doing.
646 break;
647 case TYPE_INPUT_METHOD:
Dianne Hackbornb7cd8b92009-08-08 20:53:03 -0700648 case TYPE_WALLPAPER:
649 // The window manager will check these.
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800650 break;
651 case TYPE_PHONE:
652 case TYPE_PRIORITY_PHONE:
653 case TYPE_SYSTEM_ALERT:
654 case TYPE_SYSTEM_ERROR:
655 case TYPE_SYSTEM_OVERLAY:
656 permission = android.Manifest.permission.SYSTEM_ALERT_WINDOW;
657 break;
658 default:
659 permission = android.Manifest.permission.INTERNAL_SYSTEM_WINDOW;
660 }
661 if (permission != null) {
662 if (mContext.checkCallingOrSelfPermission(permission)
663 != PackageManager.PERMISSION_GRANTED) {
664 return WindowManagerImpl.ADD_PERMISSION_DENIED;
665 }
666 }
667 return WindowManagerImpl.ADD_OKAY;
668 }
669
670 public void adjustWindowParamsLw(WindowManager.LayoutParams attrs) {
671 switch (attrs.type) {
672 case TYPE_SYSTEM_OVERLAY:
673 case TYPE_TOAST:
674 // These types of windows can't receive input events.
675 attrs.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
676 | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
677 break;
678 }
679 }
680
681 void readLidState() {
682 try {
Mike Lockwood1753f7f2009-08-24 14:49:07 -0700683 int sw = mWindowManager.getSwitchState(RawInputEvent.SW_LID);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800684 if (sw >= 0) {
685 mLidOpen = sw == 0;
686 }
687 } catch (RemoteException e) {
688 // Ignore
689 }
690 }
691
Dianne Hackborn2862fff2009-09-15 22:56:29 -0700692 private int determineHiddenState(boolean lidOpen,
693 int mode, int hiddenValue, int visibleValue) {
694 switch (mode) {
695 case 1:
696 return lidOpen ? visibleValue : hiddenValue;
697 case 2:
698 return lidOpen ? hiddenValue : visibleValue;
699 }
700 return visibleValue;
701 }
702
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800703 /** {@inheritDoc} */
704 public void adjustConfigurationLw(Configuration config) {
705 readLidState();
706 final boolean lidOpen = !KEYBOARD_ALWAYS_HIDDEN && mLidOpen;
707 mPowerManager.setKeyboardVisibility(lidOpen);
Dianne Hackborn2862fff2009-09-15 22:56:29 -0700708 config.hardKeyboardHidden = determineHiddenState(lidOpen,
709 mLidKeyboardAccessibility, Configuration.HARDKEYBOARDHIDDEN_YES,
710 Configuration.HARDKEYBOARDHIDDEN_NO);
711 config.navigationHidden = determineHiddenState(lidOpen,
712 mLidNavigationAccessibility, Configuration.NAVIGATIONHIDDEN_YES,
713 Configuration.NAVIGATIONHIDDEN_NO);
714 config.keyboardHidden = (config.hardKeyboardHidden
715 == Configuration.HARDKEYBOARDHIDDEN_NO || mHasSoftInput)
716 ? Configuration.KEYBOARDHIDDEN_NO
717 : Configuration.KEYBOARDHIDDEN_YES;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800718 }
719
720 public boolean isCheekPressedAgainstScreen(MotionEvent ev) {
721 if(ev.getSize() > SLIDE_TOUCH_EVENT_SIZE_LIMIT) {
722 return true;
723 }
724 int size = ev.getHistorySize();
725 for(int i = 0; i < size; i++) {
726 if(ev.getHistoricalSize(i) > SLIDE_TOUCH_EVENT_SIZE_LIMIT) {
727 return true;
728 }
729 }
730 return false;
731 }
732
Dianne Hackbornc777e072010-02-12 13:07:59 -0800733 public void dispatchedPointerEventLw(MotionEvent ev, int targetX, int targetY) {
734 if (mPointerLocationView == null) {
735 return;
736 }
737 synchronized (mLock) {
738 if (mPointerLocationView == null) {
739 return;
740 }
741 ev.offsetLocation(targetX, targetY);
742 mPointerLocationView.addTouchEvent(ev);
743 ev.offsetLocation(-targetX, -targetY);
744 }
745 }
746
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800747 /** {@inheritDoc} */
748 public int windowTypeToLayerLw(int type) {
749 if (type >= FIRST_APPLICATION_WINDOW && type <= LAST_APPLICATION_WINDOW) {
750 return APPLICATION_LAYER;
751 }
752 switch (type) {
753 case TYPE_STATUS_BAR:
754 return STATUS_BAR_LAYER;
755 case TYPE_STATUS_BAR_PANEL:
756 return STATUS_BAR_PANEL_LAYER;
Dianne Hackborn6a294ce2009-12-03 11:37:44 -0800757 case TYPE_SYSTEM_DIALOG:
758 return SYSTEM_DIALOG_LAYER;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800759 case TYPE_SEARCH_BAR:
760 return SEARCH_BAR_LAYER;
761 case TYPE_PHONE:
762 return PHONE_LAYER;
763 case TYPE_KEYGUARD:
764 return KEYGUARD_LAYER;
765 case TYPE_KEYGUARD_DIALOG:
766 return KEYGUARD_DIALOG_LAYER;
767 case TYPE_SYSTEM_ALERT:
768 return SYSTEM_ALERT_LAYER;
769 case TYPE_SYSTEM_ERROR:
770 return SYSTEM_ERROR_LAYER;
771 case TYPE_INPUT_METHOD:
772 return INPUT_METHOD_LAYER;
773 case TYPE_INPUT_METHOD_DIALOG:
774 return INPUT_METHOD_DIALOG_LAYER;
775 case TYPE_SYSTEM_OVERLAY:
776 return SYSTEM_OVERLAY_LAYER;
777 case TYPE_PRIORITY_PHONE:
778 return PRIORITY_PHONE_LAYER;
779 case TYPE_TOAST:
780 return TOAST_LAYER;
Dianne Hackbornb7cd8b92009-08-08 20:53:03 -0700781 case TYPE_WALLPAPER:
782 return WALLPAPER_LAYER;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800783 }
784 Log.e(TAG, "Unknown window type: " + type);
785 return APPLICATION_LAYER;
786 }
787
788 /** {@inheritDoc} */
789 public int subWindowTypeToLayerLw(int type) {
790 switch (type) {
791 case TYPE_APPLICATION_PANEL:
792 case TYPE_APPLICATION_ATTACHED_DIALOG:
793 return APPLICATION_PANEL_SUBLAYER;
794 case TYPE_APPLICATION_MEDIA:
795 return APPLICATION_MEDIA_SUBLAYER;
Dianne Hackborn5cb8d792009-05-21 17:34:15 -0700796 case TYPE_APPLICATION_MEDIA_OVERLAY:
797 return APPLICATION_MEDIA_OVERLAY_SUBLAYER;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800798 case TYPE_APPLICATION_SUB_PANEL:
799 return APPLICATION_SUB_PANEL_SUBLAYER;
800 }
801 Log.e(TAG, "Unknown sub-window type: " + type);
802 return 0;
803 }
804
Dianne Hackbornabe0da02009-09-18 01:55:39 -0700805 public int getMaxWallpaperLayer() {
806 return STATUS_BAR_LAYER;
807 }
808
Dianne Hackborn01ad2f42009-09-24 19:24:56 -0700809 public boolean doesForceHide(WindowState win, WindowManager.LayoutParams attrs) {
810 return attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD;
811 }
812
813 public boolean canBeForceHidden(WindowState win, WindowManager.LayoutParams attrs) {
814 return attrs.type != WindowManager.LayoutParams.TYPE_STATUS_BAR
815 && attrs.type != WindowManager.LayoutParams.TYPE_WALLPAPER;
816 }
817
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800818 /** {@inheritDoc} */
819 public View addStartingWindow(IBinder appToken, String packageName,
820 int theme, CharSequence nonLocalizedLabel,
821 int labelRes, int icon) {
822 if (!SHOW_STARTING_ANIMATIONS) {
823 return null;
824 }
825 if (packageName == null) {
826 return null;
827 }
828
829 Context context = mContext;
830 boolean setTheme = false;
831 //Log.i(TAG, "addStartingWindow " + packageName + ": nonLocalizedLabel="
832 // + nonLocalizedLabel + " theme=" + Integer.toHexString(theme));
833 if (theme != 0 || labelRes != 0) {
834 try {
835 context = context.createPackageContext(packageName, 0);
836 if (theme != 0) {
837 context.setTheme(theme);
838 setTheme = true;
839 }
840 } catch (PackageManager.NameNotFoundException e) {
841 // Ignore
842 }
843 }
844 if (!setTheme) {
845 context.setTheme(com.android.internal.R.style.Theme);
846 }
847
848 Window win = PolicyManager.makeNewWindow(context);
849 if (win.getWindowStyle().getBoolean(
850 com.android.internal.R.styleable.Window_windowDisablePreview, false)) {
851 return null;
852 }
853
854 Resources r = context.getResources();
855 win.setTitle(r.getText(labelRes, nonLocalizedLabel));
856
857 win.setType(
858 WindowManager.LayoutParams.TYPE_APPLICATION_STARTING);
859 // Force the window flags: this is a fake window, so it is not really
860 // touchable or focusable by the user. We also add in the ALT_FOCUSABLE_IM
861 // flag because we do know that the next window will take input
862 // focus, so we want to get the IME window up on top of us right away.
863 win.setFlags(
864 WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE|
865 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|
866 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
867 WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE|
868 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|
869 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
870
Romain Guycc6828c2010-01-08 15:06:37 -0800871 win.setLayout(WindowManager.LayoutParams.MATCH_PARENT,
872 WindowManager.LayoutParams.MATCH_PARENT);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800873
874 final WindowManager.LayoutParams params = win.getAttributes();
875 params.token = appToken;
876 params.packageName = packageName;
877 params.windowAnimations = win.getWindowStyle().getResourceId(
878 com.android.internal.R.styleable.Window_windowAnimationStyle, 0);
879 params.setTitle("Starting " + packageName);
880
881 try {
882 WindowManagerImpl wm = (WindowManagerImpl)
883 context.getSystemService(Context.WINDOW_SERVICE);
884 View view = win.getDecorView();
885
886 if (win.isFloating()) {
887 // Whoops, there is no way to display an animation/preview
888 // of such a thing! After all that work... let's skip it.
889 // (Note that we must do this here because it is in
890 // getDecorView() where the theme is evaluated... maybe
891 // we should peek the floating attribute from the theme
892 // earlier.)
893 return null;
894 }
895
896 if (localLOGV) Log.v(
897 TAG, "Adding starting window for " + packageName
898 + " / " + appToken + ": "
899 + (view.getParent() != null ? view : null));
900
901 wm.addView(view, params);
902
903 // Only return the view if it was successfully added to the
904 // window manager... which we can tell by it having a parent.
905 return view.getParent() != null ? view : null;
906 } catch (WindowManagerImpl.BadTokenException e) {
907 // ignore
908 Log.w(TAG, appToken + " already running, starting window not displayed");
909 }
910
911 return null;
912 }
913
914 /** {@inheritDoc} */
915 public void removeStartingWindow(IBinder appToken, View window) {
916 // RuntimeException e = new RuntimeException();
917 // Log.i(TAG, "remove " + appToken + " " + window, e);
918
919 if (localLOGV) Log.v(
920 TAG, "Removing starting window for " + appToken + ": " + window);
921
922 if (window != null) {
923 WindowManagerImpl wm = (WindowManagerImpl) mContext.getSystemService(Context.WINDOW_SERVICE);
924 wm.removeView(window);
925 }
926 }
927
928 /**
929 * Preflight adding a window to the system.
930 *
931 * Currently enforces that three window types are singletons:
932 * <ul>
933 * <li>STATUS_BAR_TYPE</li>
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800934 * <li>KEYGUARD_TYPE</li>
935 * </ul>
936 *
937 * @param win The window to be added
938 * @param attrs Information about the window to be added
939 *
940 * @return If ok, WindowManagerImpl.ADD_OKAY. If too many singletons, WindowManagerImpl.ADD_MULTIPLE_SINGLETON
941 */
942 public int prepareAddWindowLw(WindowState win, WindowManager.LayoutParams attrs) {
943 switch (attrs.type) {
944 case TYPE_STATUS_BAR:
945 if (mStatusBar != null) {
946 return WindowManagerImpl.ADD_MULTIPLE_SINGLETON;
947 }
948 mStatusBar = win;
949 break;
Dianne Hackborn08743722009-12-21 12:16:51 -0800950 case TYPE_STATUS_BAR_PANEL:
951 mStatusBarPanels.add(win);
952 break;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800953 case TYPE_KEYGUARD:
954 if (mKeyguard != null) {
955 return WindowManagerImpl.ADD_MULTIPLE_SINGLETON;
956 }
957 mKeyguard = win;
958 break;
959 }
960 return WindowManagerImpl.ADD_OKAY;
961 }
962
963 /** {@inheritDoc} */
964 public void removeWindowLw(WindowState win) {
965 if (mStatusBar == win) {
966 mStatusBar = null;
967 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800968 else if (mKeyguard == win) {
969 mKeyguard = null;
Dianne Hackborn08743722009-12-21 12:16:51 -0800970 } else {
971 mStatusBarPanels.remove(win);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800972 }
973 }
974
975 static final boolean PRINT_ANIM = false;
976
977 /** {@inheritDoc} */
978 public int selectAnimationLw(WindowState win, int transit) {
979 if (PRINT_ANIM) Log.i(TAG, "selectAnimation in " + win
980 + ": transit=" + transit);
981 if (transit == TRANSIT_PREVIEW_DONE) {
982 if (win.hasAppShownWindows()) {
983 if (PRINT_ANIM) Log.i(TAG, "**** STARTING EXIT");
984 return com.android.internal.R.anim.app_starting_exit;
985 }
986 }
987
988 return 0;
989 }
990
Dianne Hackborn01ad2f42009-09-24 19:24:56 -0700991 public Animation createForceHideEnterAnimation() {
992 return AnimationUtils.loadAnimation(mContext,
993 com.android.internal.R.anim.lock_screen_behind_enter);
994 }
995
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800996 static ITelephony getPhoneInterface() {
997 return ITelephony.Stub.asInterface(ServiceManager.checkService(Context.TELEPHONY_SERVICE));
998 }
999
1000 static IAudioService getAudioInterface() {
1001 return IAudioService.Stub.asInterface(ServiceManager.checkService(Context.AUDIO_SERVICE));
1002 }
1003
1004 boolean keyguardOn() {
1005 return keyguardIsShowingTq() || inKeyguardRestrictedKeyInputMode();
1006 }
1007
1008 private static final int[] WINDOW_TYPES_WHERE_HOME_DOESNT_WORK = {
1009 WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
1010 WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
1011 };
1012
1013 /** {@inheritDoc} */
1014 public boolean interceptKeyTi(WindowState win, int code, int metaKeys, boolean down,
Dianne Hackborn0041e972009-07-24 17:14:43 -07001015 int repeatCount, int flags) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001016 boolean keyguardOn = keyguardOn();
1017
1018 if (false) {
1019 Log.d(TAG, "interceptKeyTi code=" + code + " down=" + down + " repeatCount="
1020 + repeatCount + " keyguardOn=" + keyguardOn + " mHomePressed=" + mHomePressed);
1021 }
1022
1023 // Clear a pending HOME longpress if the user releases Home
1024 // TODO: This could probably be inside the next bit of logic, but that code
1025 // turned out to be a bit fragile so I'm doing it here explicitly, for now.
1026 if ((code == KeyEvent.KEYCODE_HOME) && !down) {
1027 mHandler.removeCallbacks(mHomeLongPress);
1028 }
1029
1030 // If the HOME button is currently being held, then we do special
1031 // chording with it.
1032 if (mHomePressed) {
1033
1034 // If we have released the home key, and didn't do anything else
1035 // while it was pressed, then it is time to go home!
1036 if (code == KeyEvent.KEYCODE_HOME) {
1037 if (!down) {
1038 mHomePressed = false;
1039
Dianne Hackborn0041e972009-07-24 17:14:43 -07001040 if ((flags&KeyEvent.FLAG_CANCELED) == 0) {
1041 // If an incoming call is ringing, HOME is totally disabled.
1042 // (The user is already on the InCallScreen at this point,
1043 // and his ONLY options are to answer or reject the call.)
1044 boolean incomingRinging = false;
1045 try {
1046 ITelephony phoneServ = getPhoneInterface();
1047 if (phoneServ != null) {
1048 incomingRinging = phoneServ.isRinging();
1049 } else {
1050 Log.w(TAG, "Unable to find ITelephony interface");
1051 }
1052 } catch (RemoteException ex) {
1053 Log.w(TAG, "RemoteException from getPhoneInterface()", ex);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001054 }
Dianne Hackborn0041e972009-07-24 17:14:43 -07001055
1056 if (incomingRinging) {
1057 Log.i(TAG, "Ignoring HOME; there's a ringing incoming call.");
1058 } else {
1059 launchHomeFromHotKey();
1060 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001061 } else {
Dianne Hackborn0041e972009-07-24 17:14:43 -07001062 Log.i(TAG, "Ignoring HOME; event canceled.");
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001063 }
1064 }
1065 }
1066
1067 return true;
1068 }
Dianne Hackborn0041e972009-07-24 17:14:43 -07001069
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001070 // First we always handle the home key here, so applications
1071 // can never break it, although if keyguard is on, we do let
1072 // it handle it, because that gives us the correct 5 second
1073 // timeout.
1074 if (code == KeyEvent.KEYCODE_HOME) {
1075
1076 // If a system window has focus, then it doesn't make sense
1077 // right now to interact with applications.
1078 WindowManager.LayoutParams attrs = win != null ? win.getAttrs() : null;
1079 if (attrs != null) {
1080 final int type = attrs.type;
1081 if (type == WindowManager.LayoutParams.TYPE_KEYGUARD
1082 || type == WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG) {
1083 // the "app" is keyguard, so give it the key
1084 return false;
1085 }
1086 final int typeCount = WINDOW_TYPES_WHERE_HOME_DOESNT_WORK.length;
1087 for (int i=0; i<typeCount; i++) {
1088 if (type == WINDOW_TYPES_WHERE_HOME_DOESNT_WORK[i]) {
1089 // don't do anything, but also don't pass it to the app
1090 return true;
1091 }
1092 }
1093 }
1094
1095 if (down && repeatCount == 0) {
1096 if (!keyguardOn) {
1097 mHandler.postDelayed(mHomeLongPress, ViewConfiguration.getGlobalActionKeyTimeout());
1098 }
1099 mHomePressed = true;
1100 }
1101 return true;
1102 } else if (code == KeyEvent.KEYCODE_MENU) {
1103 // Hijack modified menu keys for debugging features
1104 final int chordBug = KeyEvent.META_SHIFT_ON;
1105
1106 if (down && repeatCount == 0) {
1107 if (mEnableShiftMenuBugReports && (metaKeys & chordBug) == chordBug) {
1108 Intent intent = new Intent(Intent.ACTION_BUG_REPORT);
1109 mContext.sendOrderedBroadcast(intent, null);
1110 return true;
1111 } else if (SHOW_PROCESSES_ON_ALT_MENU &&
1112 (metaKeys & KeyEvent.META_ALT_ON) == KeyEvent.META_ALT_ON) {
1113 Intent service = new Intent();
1114 service.setClassName(mContext, "com.android.server.LoadAverageService");
1115 ContentResolver res = mContext.getContentResolver();
1116 boolean shown = Settings.System.getInt(
1117 res, Settings.System.SHOW_PROCESSES, 0) != 0;
1118 if (!shown) {
1119 mContext.startService(service);
1120 } else {
1121 mContext.stopService(service);
1122 }
1123 Settings.System.putInt(
1124 res, Settings.System.SHOW_PROCESSES, shown ? 0 : 1);
1125 return true;
1126 }
1127 }
1128 } else if (code == KeyEvent.KEYCODE_NOTIFICATION) {
1129 if (down) {
1130 // this key doesn't exist on current hardware, but if a device
1131 // didn't have a touchscreen, it would want one of these to open
1132 // the status bar.
1133 IStatusBar sbs = IStatusBar.Stub.asInterface(ServiceManager.getService("statusbar"));
1134 if (sbs != null) {
1135 try {
1136 sbs.toggle();
1137 } catch (RemoteException e) {
1138 // we're screwed anyway, since it's in this process
1139 throw new RuntimeException(e);
1140 }
1141 }
1142 }
1143 return true;
1144 } else if (code == KeyEvent.KEYCODE_SEARCH) {
1145 if (down) {
1146 if (repeatCount == 0) {
1147 mSearchKeyPressed = true;
1148 }
1149 } else {
1150 mSearchKeyPressed = false;
1151
1152 if (mConsumeSearchKeyUp) {
1153 // Consume the up-event
1154 mConsumeSearchKeyUp = false;
1155 return true;
1156 }
1157 }
1158 }
1159
1160 // Shortcuts are invoked through Search+key, so intercept those here
1161 if (mSearchKeyPressed) {
1162 if (down && repeatCount == 0 && !keyguardOn) {
1163 Intent shortcutIntent = mShortcutManager.getIntent(code, metaKeys);
1164 if (shortcutIntent != null) {
1165 shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1166 mContext.startActivity(shortcutIntent);
1167
1168 /*
1169 * We launched an app, so the up-event of the search key
1170 * should be consumed
1171 */
1172 mConsumeSearchKeyUp = true;
1173 return true;
1174 }
1175 }
1176 }
1177
1178 return false;
1179 }
1180
1181 /**
1182 * A home key -> launch home action was detected. Take the appropriate action
1183 * given the situation with the keyguard.
1184 */
1185 void launchHomeFromHotKey() {
Dianne Hackborn9dc06cc2009-11-17 18:19:23 -08001186 if (mKeyguardMediator.isShowingAndNotHidden()) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001187 // don't launch home if keyguard showing
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001188 } else if (!mHideLockScreen && mKeyguardMediator.isInputRestricted()) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001189 // when in keyguard restricted mode, must first verify unlock
1190 // before launching home
1191 mKeyguardMediator.verifyUnlock(new OnKeyguardExitResult() {
1192 public void onKeyguardExitResult(boolean success) {
1193 if (success) {
Dianne Hackborn256dd3b2009-05-19 18:51:21 -07001194 try {
1195 ActivityManagerNative.getDefault().stopAppSwitches();
1196 } catch (RemoteException e) {
1197 }
Joe Onoratof275f0c2009-11-24 16:11:13 -05001198 sendCloseSystemWindows(SYSTEM_DIALOG_REASON_HOME_KEY);
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001199 startDockOrHome();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001200 }
1201 }
1202 });
1203 } else {
1204 // no keyguard stuff to worry about, just launch home!
Dianne Hackborn256dd3b2009-05-19 18:51:21 -07001205 try {
1206 ActivityManagerNative.getDefault().stopAppSwitches();
1207 } catch (RemoteException e) {
1208 }
Joe Onoratof275f0c2009-11-24 16:11:13 -05001209 sendCloseSystemWindows(SYSTEM_DIALOG_REASON_HOME_KEY);
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001210 startDockOrHome();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001211 }
1212 }
1213
1214 public void getContentInsetHintLw(WindowManager.LayoutParams attrs, Rect contentInset) {
1215 final int fl = attrs.flags;
1216
1217 if ((fl &
1218 (FLAG_LAYOUT_IN_SCREEN | FLAG_FULLSCREEN | FLAG_LAYOUT_INSET_DECOR))
1219 == (FLAG_LAYOUT_IN_SCREEN | FLAG_LAYOUT_INSET_DECOR)) {
1220 contentInset.set(mCurLeft, mCurTop, mW - mCurRight, mH - mCurBottom);
1221 } else {
1222 contentInset.setEmpty();
1223 }
1224 }
1225
1226 /** {@inheritDoc} */
1227 public void beginLayoutLw(int displayWidth, int displayHeight) {
1228 mW = displayWidth;
1229 mH = displayHeight;
1230 mDockLeft = mContentLeft = mCurLeft = 0;
1231 mDockTop = mContentTop = mCurTop = 0;
1232 mDockRight = mContentRight = mCurRight = displayWidth;
1233 mDockBottom = mContentBottom = mCurBottom = displayHeight;
1234 mDockLayer = 0x10000000;
1235
1236 // decide where the status bar goes ahead of time
1237 if (mStatusBar != null) {
1238 final Rect pf = mTmpParentFrame;
1239 final Rect df = mTmpDisplayFrame;
1240 final Rect vf = mTmpVisibleFrame;
1241 pf.left = df.left = vf.left = 0;
1242 pf.top = df.top = vf.top = 0;
1243 pf.right = df.right = vf.right = displayWidth;
1244 pf.bottom = df.bottom = vf.bottom = displayHeight;
1245
1246 mStatusBar.computeFrameLw(pf, df, vf, vf);
1247 if (mStatusBar.isVisibleLw()) {
1248 // If the status bar is hidden, we don't want to cause
1249 // windows behind it to scroll.
1250 mDockTop = mContentTop = mCurTop = mStatusBar.getFrameLw().bottom;
The Android Open Source Project11267662009-03-18 17:39:47 -07001251 if (DEBUG_LAYOUT) Log.v(TAG, "Status bar: mDockBottom="
1252 + mDockBottom + " mContentBottom="
1253 + mContentBottom + " mCurBottom=" + mCurBottom);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001254 }
1255 }
1256 }
1257
1258 void setAttachedWindowFrames(WindowState win, int fl, int sim,
1259 WindowState attached, boolean insetDecors, Rect pf, Rect df, Rect cf, Rect vf) {
1260 if (win.getSurfaceLayer() > mDockLayer && attached.getSurfaceLayer() < mDockLayer) {
1261 // Here's a special case: if this attached window is a panel that is
1262 // above the dock window, and the window it is attached to is below
1263 // the dock window, then the frames we computed for the window it is
1264 // attached to can not be used because the dock is effectively part
1265 // of the underlying window and the attached window is floating on top
1266 // of the whole thing. So, we ignore the attached window and explicitly
1267 // compute the frames that would be appropriate without the dock.
1268 df.left = cf.left = vf.left = mDockLeft;
1269 df.top = cf.top = vf.top = mDockTop;
1270 df.right = cf.right = vf.right = mDockRight;
1271 df.bottom = cf.bottom = vf.bottom = mDockBottom;
1272 } else {
1273 // The effective display frame of the attached window depends on
1274 // whether it is taking care of insetting its content. If not,
1275 // we need to use the parent's content frame so that the entire
1276 // window is positioned within that content. Otherwise we can use
1277 // the display frame and let the attached window take care of
1278 // positioning its content appropriately.
1279 if ((sim & SOFT_INPUT_MASK_ADJUST) != SOFT_INPUT_ADJUST_RESIZE) {
1280 cf.set(attached.getDisplayFrameLw());
1281 } else {
1282 // If the window is resizing, then we want to base the content
1283 // frame on our attached content frame to resize... however,
1284 // things can be tricky if the attached window is NOT in resize
1285 // mode, in which case its content frame will be larger.
1286 // Ungh. So to deal with that, make sure the content frame
1287 // we end up using is not covering the IM dock.
1288 cf.set(attached.getContentFrameLw());
1289 if (attached.getSurfaceLayer() < mDockLayer) {
1290 if (cf.left < mContentLeft) cf.left = mContentLeft;
1291 if (cf.top < mContentTop) cf.top = mContentTop;
1292 if (cf.right > mContentRight) cf.right = mContentRight;
1293 if (cf.bottom > mContentBottom) cf.bottom = mContentBottom;
1294 }
1295 }
1296 df.set(insetDecors ? attached.getDisplayFrameLw() : cf);
1297 vf.set(attached.getVisibleFrameLw());
1298 }
1299 // The LAYOUT_IN_SCREEN flag is used to determine whether the attached
1300 // window should be positioned relative to its parent or the entire
1301 // screen.
1302 pf.set((fl & FLAG_LAYOUT_IN_SCREEN) == 0
1303 ? attached.getFrameLw() : df);
1304 }
1305
1306 /** {@inheritDoc} */
Dianne Hackborn0ecadf72009-03-31 18:00:37 -07001307 public void layoutWindowLw(WindowState win, WindowManager.LayoutParams attrs,
1308 WindowState attached) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001309 // we've already done the status bar
1310 if (win == mStatusBar) {
1311 return;
1312 }
1313
1314 if (false) {
1315 if ("com.google.android.youtube".equals(attrs.packageName)
1316 && attrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
1317 Log.i(TAG, "GOTCHA!");
1318 }
1319 }
1320
1321 final int fl = attrs.flags;
1322 final int sim = attrs.softInputMode;
1323
1324 final Rect pf = mTmpParentFrame;
1325 final Rect df = mTmpDisplayFrame;
1326 final Rect cf = mTmpContentFrame;
1327 final Rect vf = mTmpVisibleFrame;
1328
1329 if (attrs.type == TYPE_INPUT_METHOD) {
1330 pf.left = df.left = cf.left = vf.left = mDockLeft;
1331 pf.top = df.top = cf.top = vf.top = mDockTop;
1332 pf.right = df.right = cf.right = vf.right = mDockRight;
1333 pf.bottom = df.bottom = cf.bottom = vf.bottom = mDockBottom;
1334 // IM dock windows always go to the bottom of the screen.
1335 attrs.gravity = Gravity.BOTTOM;
1336 mDockLayer = win.getSurfaceLayer();
1337 } else {
1338 if ((fl &
1339 (FLAG_LAYOUT_IN_SCREEN | FLAG_FULLSCREEN | FLAG_LAYOUT_INSET_DECOR))
1340 == (FLAG_LAYOUT_IN_SCREEN | FLAG_LAYOUT_INSET_DECOR)) {
1341 // This is the case for a normal activity window: we want it
1342 // to cover all of the screen space, and it can take care of
1343 // moving its contents to account for screen decorations that
1344 // intrude into that space.
1345 if (attached != null) {
1346 // If this window is attached to another, our display
1347 // frame is the same as the one we are attached to.
1348 setAttachedWindowFrames(win, fl, sim, attached, true, pf, df, cf, vf);
1349 } else {
1350 pf.left = df.left = 0;
1351 pf.top = df.top = 0;
1352 pf.right = df.right = mW;
1353 pf.bottom = df.bottom = mH;
1354 if ((sim & SOFT_INPUT_MASK_ADJUST) != SOFT_INPUT_ADJUST_RESIZE) {
1355 cf.left = mDockLeft;
1356 cf.top = mDockTop;
1357 cf.right = mDockRight;
1358 cf.bottom = mDockBottom;
1359 } else {
1360 cf.left = mContentLeft;
1361 cf.top = mContentTop;
1362 cf.right = mContentRight;
1363 cf.bottom = mContentBottom;
1364 }
1365 vf.left = mCurLeft;
1366 vf.top = mCurTop;
1367 vf.right = mCurRight;
1368 vf.bottom = mCurBottom;
1369 }
1370 } else if ((fl & FLAG_LAYOUT_IN_SCREEN) != 0) {
1371 // A window that has requested to fill the entire screen just
1372 // gets everything, period.
1373 pf.left = df.left = cf.left = 0;
1374 pf.top = df.top = cf.top = 0;
1375 pf.right = df.right = cf.right = mW;
1376 pf.bottom = df.bottom = cf.bottom = mH;
1377 vf.left = mCurLeft;
1378 vf.top = mCurTop;
1379 vf.right = mCurRight;
1380 vf.bottom = mCurBottom;
1381 } else if (attached != null) {
1382 // A child window should be placed inside of the same visible
1383 // frame that its parent had.
1384 setAttachedWindowFrames(win, fl, sim, attached, false, pf, df, cf, vf);
1385 } else {
1386 // Otherwise, a normal window must be placed inside the content
1387 // of all screen decorations.
1388 pf.left = mContentLeft;
1389 pf.top = mContentTop;
1390 pf.right = mContentRight;
1391 pf.bottom = mContentBottom;
1392 if ((sim & SOFT_INPUT_MASK_ADJUST) != SOFT_INPUT_ADJUST_RESIZE) {
1393 df.left = cf.left = mDockLeft;
1394 df.top = cf.top = mDockTop;
1395 df.right = cf.right = mDockRight;
1396 df.bottom = cf.bottom = mDockBottom;
1397 } else {
1398 df.left = cf.left = mContentLeft;
1399 df.top = cf.top = mContentTop;
1400 df.right = cf.right = mContentRight;
1401 df.bottom = cf.bottom = mContentBottom;
1402 }
1403 vf.left = mCurLeft;
1404 vf.top = mCurTop;
1405 vf.right = mCurRight;
1406 vf.bottom = mCurBottom;
1407 }
1408 }
1409
1410 if ((fl & FLAG_LAYOUT_NO_LIMITS) != 0) {
1411 df.left = df.top = cf.left = cf.top = vf.left = vf.top = -10000;
1412 df.right = df.bottom = cf.right = cf.bottom = vf.right = vf.bottom = 10000;
1413 }
1414
The Android Open Source Project11267662009-03-18 17:39:47 -07001415 if (DEBUG_LAYOUT) Log.v(TAG, "Compute frame " + attrs.getTitle()
1416 + ": sim=#" + Integer.toHexString(sim)
1417 + " pf=" + pf.toShortString() + " df=" + df.toShortString()
1418 + " cf=" + cf.toShortString() + " vf=" + vf.toShortString());
1419
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001420 if (false) {
1421 if ("com.google.android.youtube".equals(attrs.packageName)
1422 && attrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
1423 if (true || localLOGV) Log.v(TAG, "Computing frame of " + win +
The Android Open Source Project11267662009-03-18 17:39:47 -07001424 ": sim=#" + Integer.toHexString(sim)
1425 + " pf=" + pf.toShortString() + " df=" + df.toShortString()
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001426 + " cf=" + cf.toShortString() + " vf=" + vf.toShortString());
1427 }
1428 }
1429
1430 win.computeFrameLw(pf, df, cf, vf);
1431
1432 // Dock windows carve out the bottom of the screen, so normal windows
1433 // can't appear underneath them.
1434 if (attrs.type == TYPE_INPUT_METHOD && !win.getGivenInsetsPendingLw()) {
1435 int top = win.getContentFrameLw().top;
1436 top += win.getGivenContentInsetsLw().top;
1437 if (mContentBottom > top) {
1438 mContentBottom = top;
1439 }
1440 top = win.getVisibleFrameLw().top;
1441 top += win.getGivenVisibleInsetsLw().top;
1442 if (mCurBottom > top) {
1443 mCurBottom = top;
1444 }
The Android Open Source Project11267662009-03-18 17:39:47 -07001445 if (DEBUG_LAYOUT) Log.v(TAG, "Input method: mDockBottom="
1446 + mDockBottom + " mContentBottom="
1447 + mContentBottom + " mCurBottom=" + mCurBottom);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001448 }
1449 }
1450
1451 /** {@inheritDoc} */
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001452 public int finishLayoutLw() {
Dianne Hackborn5f3063e2010-03-10 16:01:58 -08001453 return 0;
1454 }
1455
1456 /** {@inheritDoc} */
1457 public void beginAnimationLw(int displayWidth, int displayHeight) {
1458 mTopFullscreenOpaqueWindowState = null;
1459 mForceStatusBar = false;
1460
1461 mHideLockScreen = false;
1462 mAllowLockscreenWhenOn = false;
1463 mDismissKeyguard = false;
1464 }
1465
1466 /** {@inheritDoc} */
1467 public void animatingWindowLw(WindowState win,
1468 WindowManager.LayoutParams attrs) {
1469 if (mTopFullscreenOpaqueWindowState == null &&
1470 win.isVisibleOrBehindKeyguardLw()) {
1471 if ((attrs.flags & FLAG_FORCE_NOT_FULLSCREEN) != 0) {
1472 mForceStatusBar = true;
1473 }
1474 if (attrs.type >= FIRST_APPLICATION_WINDOW
1475 && attrs.type <= LAST_APPLICATION_WINDOW
1476 && win.fillsScreenLw(mW, mH, false, false)) {
1477 if (DEBUG_LAYOUT) Log.v(TAG, "Fullscreen window: " + win);
1478 mTopFullscreenOpaqueWindowState = win;
1479 if ((attrs.flags & FLAG_SHOW_WHEN_LOCKED) != 0) {
1480 if (localLOGV) Log.v(TAG, "Setting mHideLockScreen to true by win " + win);
1481 mHideLockScreen = true;
1482 }
1483 if ((attrs.flags & FLAG_DISMISS_KEYGUARD) != 0) {
1484 if (localLOGV) Log.v(TAG, "Setting mDismissKeyguard to true by win " + win);
1485 mDismissKeyguard = true;
1486 }
1487 if ((attrs.flags & FLAG_ALLOW_LOCK_WHILE_SCREEN_ON) != 0) {
1488 mAllowLockscreenWhenOn = true;
1489 }
1490 }
1491 }
1492 }
1493
1494 /** {@inheritDoc} */
1495 public int finishAnimationLw() {
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001496 int changes = 0;
Dianne Hackborn5f3063e2010-03-10 16:01:58 -08001497
The Android Open Source Projectc84bf282009-03-09 11:52:14 -07001498 boolean hiding = false;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001499 if (mStatusBar != null) {
Dianne Hackbornd70bc2f2009-10-06 23:25:51 -07001500 if (localLOGV) Log.i(TAG, "force=" + mForceStatusBar
1501 + " top=" + mTopFullscreenOpaqueWindowState);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001502 if (mForceStatusBar) {
Dianne Hackborn0ecadf72009-03-31 18:00:37 -07001503 if (DEBUG_LAYOUT) Log.v(TAG, "Showing status bar");
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001504 if (mStatusBar.showLw(true)) changes |= FINISH_LAYOUT_REDO_LAYOUT;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001505 } else if (mTopFullscreenOpaqueWindowState != null) {
1506 //Log.i(TAG, "frame: " + mTopFullscreenOpaqueWindowState.getFrameLw()
1507 // + " shown frame: " + mTopFullscreenOpaqueWindowState.getShownFrameLw());
1508 //Log.i(TAG, "attr: " + mTopFullscreenOpaqueWindowState.getAttrs());
1509 WindowManager.LayoutParams lp =
1510 mTopFullscreenOpaqueWindowState.getAttrs();
1511 boolean hideStatusBar =
1512 (lp.flags & WindowManager.LayoutParams.FLAG_FULLSCREEN) != 0;
1513 if (hideStatusBar) {
Dianne Hackborn0ecadf72009-03-31 18:00:37 -07001514 if (DEBUG_LAYOUT) Log.v(TAG, "Hiding status bar");
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001515 if (mStatusBar.hideLw(true)) changes |= FINISH_LAYOUT_REDO_LAYOUT;
The Android Open Source Projectc84bf282009-03-09 11:52:14 -07001516 hiding = true;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001517 } else {
Dianne Hackborn0ecadf72009-03-31 18:00:37 -07001518 if (DEBUG_LAYOUT) Log.v(TAG, "Showing status bar");
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001519 if (mStatusBar.showLw(true)) changes |= FINISH_LAYOUT_REDO_LAYOUT;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001520 }
1521 }
1522 }
Dianne Hackborn5f3063e2010-03-10 16:01:58 -08001523
1524 if (changes != 0 && hiding) {
1525 IStatusBar sbs = IStatusBar.Stub.asInterface(ServiceManager.getService("statusbar"));
1526 if (sbs != null) {
1527 try {
1528 // Make sure the window shade is hidden.
1529 sbs.deactivate();
1530 } catch (RemoteException e) {
1531 }
1532 }
1533 }
1534
Suchi Amalapurapu9cdc9032009-05-14 18:01:07 -07001535 // Hide the key guard if a visible window explicitly specifies that it wants to be displayed
1536 // when the screen is locked
1537 if (mKeyguard != null) {
Dianne Hackborn05726582009-09-22 17:28:24 -07001538 if (localLOGV) Log.v(TAG, "finishLayoutLw::mHideKeyguard="+mHideLockScreen);
1539 if (mDismissKeyguard && !mKeyguardMediator.isSecure()) {
Dianne Hackborn08743722009-12-21 12:16:51 -08001540 if (mKeyguard.hideLw(true)) {
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001541 changes |= FINISH_LAYOUT_REDO_LAYOUT
1542 | FINISH_LAYOUT_REDO_CONFIG
1543 | FINISH_LAYOUT_REDO_WALLPAPER;
1544 }
Dianne Hackborn05726582009-09-22 17:28:24 -07001545 if (mKeyguardMediator.isShowing()) {
Dianne Hackbornb446e972009-09-20 15:23:25 -07001546 mHandler.post(new Runnable() {
1547 public void run() {
Dianne Hackborn05726582009-09-22 17:28:24 -07001548 mKeyguardMediator.keyguardDone(false, false);
Dianne Hackbornb446e972009-09-20 15:23:25 -07001549 }
1550 });
1551 }
Dianne Hackborn05726582009-09-22 17:28:24 -07001552 } else if (mHideLockScreen) {
Dianne Hackborn08743722009-12-21 12:16:51 -08001553 if (mKeyguard.hideLw(true)) {
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001554 changes |= FINISH_LAYOUT_REDO_LAYOUT
1555 | FINISH_LAYOUT_REDO_CONFIG
1556 | FINISH_LAYOUT_REDO_WALLPAPER;
1557 }
Mike Lockwoodf3bfed52010-01-21 16:38:44 -05001558 mKeyguardMediator.setHidden(true);
Suchi Amalapurapu9cdc9032009-05-14 18:01:07 -07001559 } else {
Dianne Hackborn08743722009-12-21 12:16:51 -08001560 if (mKeyguard.showLw(true)) {
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001561 changes |= FINISH_LAYOUT_REDO_LAYOUT
1562 | FINISH_LAYOUT_REDO_CONFIG
1563 | FINISH_LAYOUT_REDO_WALLPAPER;
1564 }
Mike Lockwoodf3bfed52010-01-21 16:38:44 -05001565 mKeyguardMediator.setHidden(false);
Suchi Amalapurapu9cdc9032009-05-14 18:01:07 -07001566 }
1567 }
The Android Open Source Projectc84bf282009-03-09 11:52:14 -07001568
Mike Lockwood28569302010-01-28 11:54:40 -05001569 // update since mAllowLockscreenWhenOn might have changed
1570 updateLockScreenTimeout();
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001571 return changes;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001572 }
1573
Dianne Hackborn08743722009-12-21 12:16:51 -08001574 public boolean allowAppAnimationsLw() {
1575 if (mKeyguard != null && mKeyguard.isVisibleLw()) {
1576 // If keyguard is currently visible, no reason to animate
1577 // behind it.
1578 return false;
1579 }
1580 if (mStatusBar != null && mStatusBar.isVisibleLw()) {
1581 Rect rect = new Rect(mStatusBar.getShownFrameLw());
1582 for (int i=mStatusBarPanels.size()-1; i>=0; i--) {
1583 WindowState w = mStatusBarPanels.get(i);
1584 if (w.isVisibleLw()) {
1585 rect.union(w.getShownFrameLw());
1586 }
1587 }
1588 final int insetw = mW/10;
1589 final int inseth = mH/10;
1590 if (rect.contains(insetw, inseth, mW-insetw, mH-inseth)) {
1591 // All of the status bar windows put together cover the
1592 // screen, so the app can't be seen. (Note this test doesn't
1593 // work if the rects of these windows are at off offsets or
1594 // sizes, causing gaps in the rect union we have computed.)
1595 return false;
1596 }
1597 }
1598 return true;
1599 }
1600
Dianne Hackborn0ecadf72009-03-31 18:00:37 -07001601 /** {@inheritDoc} */
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001602 public boolean preprocessInputEventTq(RawInputEvent event) {
1603 switch (event.type) {
1604 case RawInputEvent.EV_SW:
Mike Lockwood1753f7f2009-08-24 14:49:07 -07001605 if (event.keycode == RawInputEvent.SW_LID) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001606 // lid changed state
1607 mLidOpen = event.value == 0;
Dianne Hackbornb446e972009-09-20 15:23:25 -07001608 boolean awakeNow = mKeyguardMediator.doLidChangeTq(mLidOpen);
Dianne Hackborn32bc91d2009-03-27 16:16:03 -07001609 updateRotation(Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
Dianne Hackbornb446e972009-09-20 15:23:25 -07001610 if (awakeNow) {
1611 // If the lid opening and we don't have to keep the
1612 // keyguard up, then we can turn on the screen
1613 // immediately.
1614 mKeyguardMediator.pokeWakelock();
1615 } else if (keyguardIsShowingTq()) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001616 if (mLidOpen) {
Dianne Hackbornb446e972009-09-20 15:23:25 -07001617 // If we are opening the lid and not hiding the
1618 // keyguard, then we need to have it turn on the
1619 // screen once it is shown.
1620 mKeyguardMediator.onWakeKeyWhenKeyguardShowingTq(
1621 KeyEvent.KEYCODE_POWER);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001622 }
1623 } else {
1624 // Light up the keyboard if we are sliding up.
1625 if (mLidOpen) {
1626 mPowerManager.userActivity(SystemClock.uptimeMillis(), false,
1627 LocalPowerManager.BUTTON_EVENT);
1628 } else {
1629 mPowerManager.userActivity(SystemClock.uptimeMillis(), false,
1630 LocalPowerManager.OTHER_EVENT);
1631 }
1632 }
1633 }
1634 }
1635 return false;
1636 }
1637
1638
1639 /** {@inheritDoc} */
1640 public boolean isAppSwitchKeyTqTiLwLi(int keycode) {
1641 return keycode == KeyEvent.KEYCODE_HOME
1642 || keycode == KeyEvent.KEYCODE_ENDCALL;
1643 }
1644
1645 /** {@inheritDoc} */
1646 public boolean isMovementKeyTi(int keycode) {
1647 switch (keycode) {
1648 case KeyEvent.KEYCODE_DPAD_UP:
1649 case KeyEvent.KEYCODE_DPAD_DOWN:
1650 case KeyEvent.KEYCODE_DPAD_LEFT:
1651 case KeyEvent.KEYCODE_DPAD_RIGHT:
1652 return true;
1653 }
1654 return false;
1655 }
1656
1657
1658 /**
1659 * @return Whether a telephone call is in progress right now.
1660 */
1661 boolean isInCall() {
1662 final ITelephony phone = getPhoneInterface();
1663 if (phone == null) {
1664 Log.w(TAG, "couldn't get ITelephony reference");
1665 return false;
1666 }
1667 try {
1668 return phone.isOffhook();
1669 } catch (RemoteException e) {
1670 Log.w(TAG, "ITelephony.isOffhhook threw RemoteException " + e);
1671 return false;
1672 }
1673 }
1674
1675 /**
1676 * @return Whether music is being played right now.
1677 */
1678 boolean isMusicActive() {
Eric Laurent413cb9d2009-07-17 11:52:43 -07001679 final AudioManager am = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
1680 if (am == null) {
1681 Log.w(TAG, "isMusicActive: couldn't get AudioManager reference");
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001682 return false;
1683 }
Eric Laurent413cb9d2009-07-17 11:52:43 -07001684 return am.isMusicActive();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001685 }
1686
1687 /**
1688 * Tell the audio service to adjust the volume appropriate to the event.
1689 * @param keycode
1690 */
Mike Lockwooda53e3812009-09-25 10:51:39 -04001691 void handleVolumeKey(int stream, int keycode) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001692 final IAudioService audio = getAudioInterface();
1693 if (audio == null) {
Mike Lockwooda53e3812009-09-25 10:51:39 -04001694 Log.w(TAG, "handleVolumeKey: couldn't get IAudioService reference");
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001695 return;
1696 }
1697 try {
1698 // since audio is playing, we shouldn't have to hold a wake lock
1699 // during the call, but we do it as a precaution for the rare possibility
1700 // that the music stops right before we call this
1701 mBroadcastWakeLock.acquire();
Mike Lockwooda53e3812009-09-25 10:51:39 -04001702 audio.adjustStreamVolume(stream,
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001703 keycode == KeyEvent.KEYCODE_VOLUME_UP
1704 ? AudioManager.ADJUST_RAISE
1705 : AudioManager.ADJUST_LOWER,
1706 0);
1707 } catch (RemoteException e) {
1708 Log.w(TAG, "IAudioService.adjustStreamVolume() threw RemoteException " + e);
1709 } finally {
1710 mBroadcastWakeLock.release();
1711 }
1712 }
1713
1714 static boolean isMediaKey(int code) {
1715 if (code == KeyEvent.KEYCODE_HEADSETHOOK ||
Andy Stadler8b89d692009-04-10 16:24:49 -07001716 code == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE ||
1717 code == KeyEvent.KEYCODE_MEDIA_STOP ||
1718 code == KeyEvent.KEYCODE_MEDIA_NEXT ||
1719 code == KeyEvent.KEYCODE_MEDIA_PREVIOUS ||
Johan Redestig2e2a3d02010-01-05 08:45:33 +01001720 code == KeyEvent.KEYCODE_MEDIA_REWIND ||
Andy Stadler8b89d692009-04-10 16:24:49 -07001721 code == KeyEvent.KEYCODE_MEDIA_FAST_FORWARD) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001722 return true;
1723 }
1724 return false;
1725 }
1726
1727 /** {@inheritDoc} */
1728 public int interceptKeyTq(RawInputEvent event, boolean screenIsOn) {
1729 int result = ACTION_PASS_TO_USER;
1730 final boolean isWakeKey = isWakeKeyTq(event);
Mike Lockwoodc72b15f2009-11-28 20:58:22 -05001731 // If screen is off then we treat the case where the keyguard is open but hidden
1732 // the same as if it were open and in front.
1733 // This will prevent any keys other than the power button from waking the screen
1734 // when the keyguard is hidden by another activity.
1735 final boolean keyguardActive = (screenIsOn ?
1736 mKeyguardMediator.isShowingAndNotHidden() :
1737 mKeyguardMediator.isShowing());
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001738
1739 if (false) {
1740 Log.d(TAG, "interceptKeyTq event=" + event + " keycode=" + event.keycode
Mike Lockwoodc72b15f2009-11-28 20:58:22 -05001741 + " screenIsOn=" + screenIsOn + " keyguardActive=" + keyguardActive);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001742 }
1743
Mike Lockwoodc72b15f2009-11-28 20:58:22 -05001744 if (keyguardActive) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001745 if (screenIsOn) {
1746 // when the screen is on, always give the event to the keyguard
1747 result |= ACTION_PASS_TO_USER;
1748 } else {
1749 // otherwise, don't pass it to the user
1750 result &= ~ACTION_PASS_TO_USER;
1751
1752 final boolean isKeyDown =
1753 (event.type == RawInputEvent.EV_KEY) && (event.value != 0);
1754 if (isWakeKey && isKeyDown) {
1755
1756 // tell the mediator about a wake key, it may decide to
1757 // turn on the screen depending on whether the key is
1758 // appropriate.
1759 if (!mKeyguardMediator.onWakeKeyWhenKeyguardShowingTq(event.keycode)
1760 && (event.keycode == KeyEvent.KEYCODE_VOLUME_DOWN
1761 || event.keycode == KeyEvent.KEYCODE_VOLUME_UP)) {
Mike Lockwood99ff0a82009-12-02 01:37:38 -05001762 // when keyguard is showing and screen off, we need
1763 // to handle the volume key for calls and music here
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001764 if (isInCall()) {
Mike Lockwood99ff0a82009-12-02 01:37:38 -05001765 handleVolumeKey(AudioManager.STREAM_VOICE_CALL, event.keycode);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001766 } else if (isMusicActive()) {
Mike Lockwooda53e3812009-09-25 10:51:39 -04001767 handleVolumeKey(AudioManager.STREAM_MUSIC, event.keycode);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001768 }
1769 }
1770 }
1771 }
1772 } else if (!screenIsOn) {
Mike Lockwooda53e3812009-09-25 10:51:39 -04001773 // If we are in-call with screen off and keyguard is not showing,
1774 // then handle the volume key ourselves.
1775 // This is necessary because the phone app will disable the keyguard
1776 // when the proximity sensor is in use.
1777 if (isInCall() && event.type == RawInputEvent.EV_KEY &&
1778 (event.keycode == KeyEvent.KEYCODE_VOLUME_DOWN
1779 || event.keycode == KeyEvent.KEYCODE_VOLUME_UP)) {
1780 result &= ~ACTION_PASS_TO_USER;
1781 handleVolumeKey(AudioManager.STREAM_VOICE_CALL, event.keycode);
1782 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001783 if (isWakeKey) {
1784 // a wake key has a sole purpose of waking the device; don't pass
1785 // it to the user
1786 result |= ACTION_POKE_USER_ACTIVITY;
1787 result &= ~ACTION_PASS_TO_USER;
1788 }
1789 }
1790
1791 int type = event.type;
1792 int code = event.keycode;
1793 boolean down = event.value != 0;
1794
1795 if (type == RawInputEvent.EV_KEY) {
Dianne Hackborn0041e972009-07-24 17:14:43 -07001796 if (code == KeyEvent.KEYCODE_ENDCALL
1797 || code == KeyEvent.KEYCODE_POWER) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001798 if (down) {
Mike Lockwood2eb47962009-10-23 11:29:52 -04001799 boolean handled = false;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001800 // key repeats are generated by the window manager, and we don't see them
1801 // here, so unless the driver is doing something it shouldn't be, we know
1802 // this is the real press event.
Mike Lockwood75d3caa2009-10-22 13:14:31 -04001803 ITelephony phoneServ = getPhoneInterface();
1804 if (phoneServ != null) {
Dianne Hackborn0041e972009-07-24 17:14:43 -07001805 try {
Mike Lockwood2eb47962009-10-23 11:29:52 -04001806 if (code == KeyEvent.KEYCODE_ENDCALL) {
1807 handled = phoneServ.endCall();
1808 } else if (code == KeyEvent.KEYCODE_POWER && phoneServ.isRinging()) {
1809 // Pressing power during incoming call should silence the ringer
1810 phoneServ.silenceRinger();
1811 handled = true;
Dianne Hackborn0041e972009-07-24 17:14:43 -07001812 }
1813 } catch (RemoteException ex) {
Mike Lockwood75d3caa2009-10-22 13:14:31 -04001814 Log.w(TAG, "ITelephony threw RemoteException" + ex);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001815 }
Mike Lockwood75d3caa2009-10-22 13:14:31 -04001816 } else {
1817 Log.w(TAG, "!!! Unable to find ITelephony interface !!!");
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001818 }
Mike Lockwood75d3caa2009-10-22 13:14:31 -04001819 // power button should turn off screen in addition to hanging up the phone
Mike Lockwood2eb47962009-10-23 11:29:52 -04001820 if ((handled && code != KeyEvent.KEYCODE_POWER) || !screenIsOn) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001821 mShouldTurnOffOnKeyUp = false;
1822 } else {
1823 // only try to turn off the screen if we didn't already hang up
1824 mShouldTurnOffOnKeyUp = true;
Dianne Hackborn0041e972009-07-24 17:14:43 -07001825 mHandler.postDelayed(mPowerLongPress,
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001826 ViewConfiguration.getGlobalActionKeyTimeout());
1827 result &= ~ACTION_PASS_TO_USER;
1828 }
1829 } else {
Dianne Hackborn0041e972009-07-24 17:14:43 -07001830 mHandler.removeCallbacks(mPowerLongPress);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001831 if (mShouldTurnOffOnKeyUp) {
1832 mShouldTurnOffOnKeyUp = false;
Mike Lockwoodb4e2d1c2009-11-23 15:54:10 -05001833 boolean gohome, sleeps;
1834 if (code == KeyEvent.KEYCODE_ENDCALL) {
1835 gohome = (mEndcallBehavior & ENDCALL_HOME) != 0;
1836 sleeps = (mEndcallBehavior & ENDCALL_SLEEPS) != 0;
1837 } else {
1838 gohome = false;
1839 sleeps = true;
1840 }
Mike Lockwoodc72b15f2009-11-28 20:58:22 -05001841 if (keyguardActive
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001842 || (sleeps && !gohome)
1843 || (gohome && !goHome() && sleeps)) {
1844 // they must already be on the keyguad or home screen,
1845 // go to sleep instead
1846 Log.d(TAG, "I'm tired mEndcallBehavior=0x"
1847 + Integer.toHexString(mEndcallBehavior));
1848 result &= ~ACTION_POKE_USER_ACTIVITY;
1849 result |= ACTION_GO_TO_SLEEP;
1850 }
1851 result &= ~ACTION_PASS_TO_USER;
1852 }
1853 }
1854 } else if (isMediaKey(code)) {
1855 // This key needs to be handled even if the screen is off.
1856 // If others need to be handled while it's off, this is a reasonable
1857 // pattern to follow.
1858 if ((result & ACTION_PASS_TO_USER) == 0) {
1859 // Only do this if we would otherwise not pass it to the user. In that
1860 // case, the PhoneWindow class will do the same thing, except it will
1861 // only do it if the showing app doesn't process the key on its own.
1862 KeyEvent keyEvent = new KeyEvent(event.when, event.when,
1863 down ? KeyEvent.ACTION_DOWN : KeyEvent.ACTION_UP,
1864 code, 0);
1865 mBroadcastWakeLock.acquire();
1866 mHandler.post(new PassHeadsetKey(keyEvent));
1867 }
1868 } else if (code == KeyEvent.KEYCODE_CALL) {
1869 // If an incoming call is ringing, answer it!
1870 // (We handle this key here, rather than in the InCallScreen, to make
1871 // sure we'll respond to the key even if the InCallScreen hasn't come to
1872 // the foreground yet.)
1873
1874 // We answer the call on the DOWN event, to agree with
1875 // the "fallback" behavior in the InCallScreen.
1876 if (down) {
1877 try {
1878 ITelephony phoneServ = getPhoneInterface();
1879 if (phoneServ != null) {
1880 if (phoneServ.isRinging()) {
1881 Log.i(TAG, "interceptKeyTq:"
1882 + " CALL key-down while ringing: Answer the call!");
1883 phoneServ.answerRingingCall();
1884
1885 // And *don't* pass this key thru to the current activity
1886 // (which is presumably the InCallScreen.)
1887 result &= ~ACTION_PASS_TO_USER;
1888 }
1889 } else {
1890 Log.w(TAG, "CALL button: Unable to find ITelephony interface");
1891 }
1892 } catch (RemoteException ex) {
1893 Log.w(TAG, "CALL button: RemoteException from getPhoneInterface()", ex);
1894 }
1895 }
1896 } else if ((code == KeyEvent.KEYCODE_VOLUME_UP)
1897 || (code == KeyEvent.KEYCODE_VOLUME_DOWN)) {
1898 // If an incoming call is ringing, either VOLUME key means
1899 // "silence ringer". We handle these keys here, rather than
1900 // in the InCallScreen, to make sure we'll respond to them
1901 // even if the InCallScreen hasn't come to the foreground yet.
1902
1903 // Look for the DOWN event here, to agree with the "fallback"
1904 // behavior in the InCallScreen.
1905 if (down) {
1906 try {
1907 ITelephony phoneServ = getPhoneInterface();
1908 if (phoneServ != null) {
1909 if (phoneServ.isRinging()) {
1910 Log.i(TAG, "interceptKeyTq:"
1911 + " VOLUME key-down while ringing: Silence ringer!");
1912 // Silence the ringer. (It's safe to call this
1913 // even if the ringer has already been silenced.)
1914 phoneServ.silenceRinger();
1915
1916 // And *don't* pass this key thru to the current activity
1917 // (which is probably the InCallScreen.)
1918 result &= ~ACTION_PASS_TO_USER;
1919 }
1920 } else {
1921 Log.w(TAG, "VOLUME button: Unable to find ITelephony interface");
1922 }
1923 } catch (RemoteException ex) {
1924 Log.w(TAG, "VOLUME button: RemoteException from getPhoneInterface()", ex);
1925 }
1926 }
1927 }
1928 }
1929
1930 return result;
1931 }
1932
1933 class PassHeadsetKey implements Runnable {
1934 KeyEvent mKeyEvent;
1935
1936 PassHeadsetKey(KeyEvent keyEvent) {
1937 mKeyEvent = keyEvent;
1938 }
1939
1940 public void run() {
1941 if (ActivityManagerNative.isSystemReady()) {
1942 Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
1943 intent.putExtra(Intent.EXTRA_KEY_EVENT, mKeyEvent);
1944 mContext.sendOrderedBroadcast(intent, null, mBroadcastDone,
1945 mHandler, Activity.RESULT_OK, null, null);
1946 }
1947 }
1948 }
1949
1950 BroadcastReceiver mBroadcastDone = new BroadcastReceiver() {
1951 public void onReceive(Context context, Intent intent) {
1952 mBroadcastWakeLock.release();
1953 }
1954 };
Mike Lockwood1753f7f2009-08-24 14:49:07 -07001955
1956 BroadcastReceiver mDockReceiver = new BroadcastReceiver() {
1957 public void onReceive(Context context, Intent intent) {
Dianne Hackborn78968392010-03-04 20:47:56 -08001958 try {
1959 IUiModeManager uiModeService = IUiModeManager.Stub.asInterface(
1960 ServiceManager.getService(Context.UI_MODE_SERVICE));
1961 mUiMode = uiModeService.getCurrentModeType();
Dianne Hackborn78968392010-03-04 20:47:56 -08001962 } catch (RemoteException e) {
Dianne Hackborn80fa1662009-10-07 14:02:10 -07001963 }
Mike Lockwood1753f7f2009-08-24 14:49:07 -07001964 updateRotation(Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
Mike Lockwoode9867d22009-09-20 01:59:02 -04001965 updateOrientationListenerLp();
Mike Lockwood1753f7f2009-08-24 14:49:07 -07001966 }
1967 };
1968
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001969 /** {@inheritDoc} */
1970 public boolean isWakeRelMovementTq(int device, int classes,
1971 RawInputEvent event) {
1972 // if it's tagged with one of the wake bits, it wakes up the device
1973 return ((event.flags & (FLAG_WAKE | FLAG_WAKE_DROPPED)) != 0);
1974 }
1975
1976 /** {@inheritDoc} */
1977 public boolean isWakeAbsMovementTq(int device, int classes,
1978 RawInputEvent event) {
1979 // if it's tagged with one of the wake bits, it wakes up the device
1980 return ((event.flags & (FLAG_WAKE | FLAG_WAKE_DROPPED)) != 0);
1981 }
1982
1983 /**
1984 * Given the current state of the world, should this key wake up the device?
1985 */
1986 protected boolean isWakeKeyTq(RawInputEvent event) {
1987 // There are not key maps for trackball devices, but we'd still
1988 // like to have pressing it wake the device up, so force it here.
1989 int keycode = event.keycode;
1990 int flags = event.flags;
1991 if (keycode == RawInputEvent.BTN_MOUSE) {
1992 flags |= WindowManagerPolicy.FLAG_WAKE;
1993 }
1994 return (flags
1995 & (WindowManagerPolicy.FLAG_WAKE | WindowManagerPolicy.FLAG_WAKE_DROPPED)) != 0;
1996 }
1997
1998 /** {@inheritDoc} */
1999 public void screenTurnedOff(int why) {
Dianne Hackborn74489012009-03-24 20:50:09 -07002000 EventLog.writeEvent(70000, 0);
2001 mKeyguardMediator.onScreenTurnedOff(why);
The Android Open Source Project0727d222009-03-11 12:11:58 -07002002 synchronized (mLock) {
The Android Open Source Project0727d222009-03-11 12:11:58 -07002003 mScreenOn = false;
2004 updateOrientationListenerLp();
Mike Lockwood28569302010-01-28 11:54:40 -05002005 updateLockScreenTimeout();
The Android Open Source Project0727d222009-03-11 12:11:58 -07002006 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002007 }
2008
2009 /** {@inheritDoc} */
2010 public void screenTurnedOn() {
Dianne Hackborn74489012009-03-24 20:50:09 -07002011 EventLog.writeEvent(70000, 1);
2012 mKeyguardMediator.onScreenTurnedOn();
The Android Open Source Project0727d222009-03-11 12:11:58 -07002013 synchronized (mLock) {
The Android Open Source Project0727d222009-03-11 12:11:58 -07002014 mScreenOn = true;
2015 updateOrientationListenerLp();
Mike Lockwood28569302010-01-28 11:54:40 -05002016 updateLockScreenTimeout();
The Android Open Source Project0727d222009-03-11 12:11:58 -07002017 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002018 }
2019
2020 /** {@inheritDoc} */
Dianne Hackborn08743722009-12-21 12:16:51 -08002021 public boolean isScreenOn() {
2022 return mScreenOn;
2023 }
2024
2025 /** {@inheritDoc} */
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002026 public void enableKeyguard(boolean enabled) {
2027 mKeyguardMediator.setKeyguardEnabled(enabled);
2028 }
2029
2030 /** {@inheritDoc} */
2031 public void exitKeyguardSecurely(OnKeyguardExitResult callback) {
2032 mKeyguardMediator.verifyUnlock(callback);
2033 }
2034
Mike Lockwoodf7913302009-11-28 22:27:10 -05002035 private boolean keyguardIsShowingTq() {
Dianne Hackborn9dc06cc2009-11-17 18:19:23 -08002036 return mKeyguardMediator.isShowingAndNotHidden();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002037 }
2038
2039 /** {@inheritDoc} */
2040 public boolean inKeyguardRestrictedKeyInputMode() {
2041 return mKeyguardMediator.isInputRestricted();
2042 }
2043
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002044 void sendCloseSystemWindows() {
2045 sendCloseSystemWindows(mContext, null);
2046 }
2047
2048 void sendCloseSystemWindows(String reason) {
2049 sendCloseSystemWindows(mContext, reason);
2050 }
2051
2052 static void sendCloseSystemWindows(Context context, String reason) {
2053 if (ActivityManagerNative.isSystemReady()) {
Dianne Hackborn94a679d2009-07-28 17:51:07 -07002054 try {
2055 ActivityManagerNative.getDefault().closeSystemDialogs(reason);
2056 } catch (RemoteException e) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002057 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002058 }
2059 }
2060
The Android Open Source Project0727d222009-03-11 12:11:58 -07002061 public int rotationForOrientationLw(int orientation, int lastRotation,
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002062 boolean displayEnabled) {
Mitsuru Oshima831d0d92009-06-16 18:27:18 -07002063
2064 if (mPortraitRotation < 0) {
2065 // Initialize the rotation angles for each orientation once.
2066 Display d = ((WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE))
2067 .getDefaultDisplay();
2068 if (d.getWidth() > d.getHeight()) {
2069 mPortraitRotation = Surface.ROTATION_90;
2070 mLandscapeRotation = Surface.ROTATION_0;
2071 } else {
2072 mPortraitRotation = Surface.ROTATION_0;
2073 mLandscapeRotation = Surface.ROTATION_90;
2074 }
2075 }
2076
The Android Open Source Project0727d222009-03-11 12:11:58 -07002077 synchronized (mLock) {
2078 switch (orientation) {
2079 case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:
2080 //always return landscape if orientation set to landscape
Mitsuru Oshima831d0d92009-06-16 18:27:18 -07002081 return mLandscapeRotation;
The Android Open Source Project0727d222009-03-11 12:11:58 -07002082 case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT:
2083 //always return portrait if orientation set to portrait
Mitsuru Oshima831d0d92009-06-16 18:27:18 -07002084 return mPortraitRotation;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002085 }
The Android Open Source Project0727d222009-03-11 12:11:58 -07002086 // case for nosensor meaning ignore sensor and consider only lid
2087 // or orientation sensor disabled
2088 //or case.unspecified
2089 if (mLidOpen) {
Mike Lockwood1753f7f2009-08-24 14:49:07 -07002090 return mLidOpenRotation;
Dianne Hackborn78968392010-03-04 20:47:56 -08002091 } else if (mUiMode == Configuration.UI_MODE_TYPE_CAR && mCarDockRotation >= 0) {
Mike Lockwood2d1efbd2009-09-16 12:15:53 -04002092 return mCarDockRotation;
Dianne Hackborn78968392010-03-04 20:47:56 -08002093 } else if (mUiMode == Configuration.UI_MODE_TYPE_DESK && mDeskDockRotation >= 0) {
Mike Lockwood2d1efbd2009-09-16 12:15:53 -04002094 return mDeskDockRotation;
The Android Open Source Project0727d222009-03-11 12:11:58 -07002095 } else {
Dianne Hackborn03759ed2009-03-27 16:04:08 -07002096 if (useSensorForOrientationLp(orientation)) {
The Android Open Source Project0727d222009-03-11 12:11:58 -07002097 // If the user has enabled auto rotation by default, do it.
Dianne Hackborn03759ed2009-03-27 16:04:08 -07002098 int curRotation = mOrientationListener.getCurrentRotation();
2099 return curRotation >= 0 ? curRotation : lastRotation;
The Android Open Source Project0727d222009-03-11 12:11:58 -07002100 }
2101 return Surface.ROTATION_0;
2102 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002103 }
2104 }
2105
2106 public boolean detectSafeMode() {
2107 try {
2108 int menuState = mWindowManager.getKeycodeState(KeyEvent.KEYCODE_MENU);
Dianne Hackbornb1a79802009-09-29 15:18:31 -07002109 int sState = mWindowManager.getKeycodeState(KeyEvent.KEYCODE_S);
Dianne Hackborna8730f92009-11-17 15:53:36 -08002110 int dpadState = mWindowManager.getDPadKeycodeState(KeyEvent.KEYCODE_DPAD_CENTER);
2111 int trackballState = mWindowManager.getTrackballScancodeState(RawInputEvent.BTN_MOUSE);
Dianne Hackbornb1a79802009-09-29 15:18:31 -07002112 mSafeMode = menuState > 0 || sState > 0 || dpadState > 0 || trackballState > 0;
2113 performHapticFeedbackLw(null, mSafeMode
2114 ? HapticFeedbackConstants.SAFE_MODE_ENABLED
2115 : HapticFeedbackConstants.SAFE_MODE_DISABLED, true);
Dianne Hackborn73b1ec82009-10-04 13:33:00 -07002116 if (mSafeMode) {
2117 Log.i(TAG, "SAFE MODE ENABLED (menu=" + menuState + " s=" + sState
2118 + " dpad=" + dpadState + " trackball=" + trackballState + ")");
2119 } else {
2120 Log.i(TAG, "SAFE MODE not enabled");
2121 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002122 return mSafeMode;
2123 } catch (RemoteException e) {
2124 // Doom! (it's also local)
2125 throw new RuntimeException("window manager dead");
2126 }
2127 }
2128
Dianne Hackborn181ceb52009-08-27 22:16:40 -07002129 static long[] getLongIntArray(Resources r, int resid) {
2130 int[] ar = r.getIntArray(resid);
2131 if (ar == null) {
2132 return null;
2133 }
2134 long[] out = new long[ar.length];
2135 for (int i=0; i<ar.length; i++) {
2136 out[i] = ar[i];
2137 }
2138 return out;
2139 }
2140
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002141 /** {@inheritDoc} */
2142 public void systemReady() {
Dianne Hackbornb1a79802009-09-29 15:18:31 -07002143 // tell the keyguard
2144 mKeyguardMediator.onSystemReady();
2145 android.os.SystemProperties.set("dev.bootcomplete", "1");
2146 synchronized (mLock) {
2147 updateOrientationListenerLp();
Dianne Hackbornc777e072010-02-12 13:07:59 -08002148 mSystemReady = true;
2149 mHandler.post(new Runnable() {
2150 public void run() {
2151 updateSettings();
2152 }
2153 });
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002154 }
2155 }
2156
Mike Lockwood28569302010-01-28 11:54:40 -05002157 /** {@inheritDoc} */
2158 public void userActivity() {
2159 synchronized (mScreenLockTimeout) {
2160 if (mLockScreenTimerActive) {
2161 // reset the timer
2162 mHandler.removeCallbacks(mScreenLockTimeout);
2163 mHandler.postDelayed(mScreenLockTimeout, mLockScreenTimeout);
2164 }
2165 }
2166 }
2167
2168 Runnable mScreenLockTimeout = new Runnable() {
2169 public void run() {
2170 synchronized (this) {
2171 if (localLOGV) Log.v(TAG, "mScreenLockTimeout activating keyguard");
2172 mKeyguardMediator.doKeyguardTimeout();
2173 mLockScreenTimerActive = false;
2174 }
2175 }
2176 };
2177
2178 private void updateLockScreenTimeout() {
2179 synchronized (mScreenLockTimeout) {
2180 boolean enable = (mAllowLockscreenWhenOn && mScreenOn && mKeyguardMediator.isSecure());
2181 if (mLockScreenTimerActive != enable) {
2182 if (enable) {
2183 if (localLOGV) Log.v(TAG, "setting lockscreen timer");
2184 mHandler.postDelayed(mScreenLockTimeout, mLockScreenTimeout);
2185 } else {
2186 if (localLOGV) Log.v(TAG, "clearing lockscreen timer");
2187 mHandler.removeCallbacks(mScreenLockTimeout);
2188 }
2189 mLockScreenTimerActive = enable;
2190 }
2191 }
2192 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002193
2194 /** {@inheritDoc} */
2195 public void enableScreenAfterBoot() {
2196 readLidState();
Dianne Hackborn32bc91d2009-03-27 16:16:03 -07002197 updateRotation(Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002198 }
Dianne Hackbornf54c8f92009-09-19 14:03:57 -07002199
Dianne Hackborn03759ed2009-03-27 16:04:08 -07002200 void updateRotation(int animFlags) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002201 mPowerManager.setKeyboardVisibility(mLidOpen);
Mike Lockwood1753f7f2009-08-24 14:49:07 -07002202 int rotation = Surface.ROTATION_0;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002203 if (mLidOpen) {
Mike Lockwood1753f7f2009-08-24 14:49:07 -07002204 rotation = mLidOpenRotation;
Dianne Hackborn78968392010-03-04 20:47:56 -08002205 } else if (mUiMode == Configuration.UI_MODE_TYPE_CAR && mCarDockRotation >= 0) {
Mike Lockwood2d1efbd2009-09-16 12:15:53 -04002206 rotation = mCarDockRotation;
Dianne Hackborn78968392010-03-04 20:47:56 -08002207 } else if (mUiMode == Configuration.UI_MODE_TYPE_DESK && mDeskDockRotation >= 0) {
Mike Lockwood2d1efbd2009-09-16 12:15:53 -04002208 rotation = mDeskDockRotation;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002209 }
2210 //if lid is closed orientation will be portrait
2211 try {
2212 //set orientation on WindowManager
Dianne Hackborn32bc91d2009-03-27 16:16:03 -07002213 mWindowManager.setRotation(rotation, true,
2214 mFancyRotationAnimation | animFlags);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002215 } catch (RemoteException e) {
2216 // Ignore
2217 }
2218 }
2219
2220 /**
Dianne Hackborn39c2d712009-09-22 11:41:31 -07002221 * Return an Intent to launch the currently active dock as home. Returns
2222 * null if the standard home should be launched.
2223 * @return
2224 */
2225 Intent createHomeDockIntent() {
Dianne Hackborn39c2d712009-09-22 11:41:31 -07002226 Intent intent;
Dianne Hackborn78968392010-03-04 20:47:56 -08002227 if (mUiMode == Configuration.UI_MODE_TYPE_CAR) {
Dianne Hackborn39c2d712009-09-22 11:41:31 -07002228 intent = mCarDockIntent;
Dianne Hackborn78968392010-03-04 20:47:56 -08002229 } else if (mUiMode == Configuration.UI_MODE_TYPE_DESK) {
Dianne Hackborn39c2d712009-09-22 11:41:31 -07002230 intent = mDeskDockIntent;
2231 } else {
Dianne Hackborn39c2d712009-09-22 11:41:31 -07002232 return null;
2233 }
2234
2235 ActivityInfo ai = intent.resolveActivityInfo(
2236 mContext.getPackageManager(), PackageManager.GET_META_DATA);
2237 if (ai == null) {
2238 return null;
2239 }
2240
2241 if (ai.metaData != null && ai.metaData.getBoolean(Intent.METADATA_DOCK_HOME)) {
2242 intent = new Intent(intent);
2243 intent.setClassName(ai.packageName, ai.name);
2244 return intent;
2245 }
2246
2247 return null;
2248 }
2249
2250 void startDockOrHome() {
2251 Intent dock = createHomeDockIntent();
2252 if (dock != null) {
2253 try {
2254 mContext.startActivity(dock);
2255 return;
2256 } catch (ActivityNotFoundException e) {
2257 }
2258 }
2259 mContext.startActivity(mHomeIntent);
2260 }
2261
2262 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002263 * goes to the home screen
2264 * @return whether it did anything
2265 */
2266 boolean goHome() {
2267 if (false) {
2268 // This code always brings home to the front.
Dianne Hackborn256dd3b2009-05-19 18:51:21 -07002269 try {
2270 ActivityManagerNative.getDefault().stopAppSwitches();
2271 } catch (RemoteException e) {
2272 }
Dianne Hackborn94a679d2009-07-28 17:51:07 -07002273 sendCloseSystemWindows();
Dianne Hackborn39c2d712009-09-22 11:41:31 -07002274 startDockOrHome();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002275 } else {
2276 // This code brings home to the front or, if it is already
2277 // at the front, puts the device to sleep.
2278 try {
Wink Savilled2e6a332010-02-12 12:12:06 -08002279 if (SystemProperties.getInt("persist.sys.uts-test-mode", 0) == 1) {
2280 /// Roll back EndcallBehavior as the cupcake design to pass P1 lab entry.
2281 Log.d(TAG, "UTS-TEST-MODE");
2282 } else {
2283 ActivityManagerNative.getDefault().stopAppSwitches();
2284 sendCloseSystemWindows();
2285 Intent dock = createHomeDockIntent();
2286 if (dock != null) {
2287 int result = ActivityManagerNative.getDefault()
2288 .startActivity(null, dock,
2289 dock.resolveTypeIfNeeded(mContext.getContentResolver()),
2290 null, 0, null, null, 0, true /* onlyIfNeeded*/, false);
2291 if (result == IActivityManager.START_RETURN_INTENT_TO_CALLER) {
2292 return false;
2293 }
Dianne Hackborn39c2d712009-09-22 11:41:31 -07002294 }
2295 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002296 int result = ActivityManagerNative.getDefault()
2297 .startActivity(null, mHomeIntent,
2298 mHomeIntent.resolveTypeIfNeeded(mContext.getContentResolver()),
2299 null, 0, null, null, 0, true /* onlyIfNeeded*/, false);
2300 if (result == IActivityManager.START_RETURN_INTENT_TO_CALLER) {
2301 return false;
2302 }
2303 } catch (RemoteException ex) {
2304 // bummer, the activity manager, which is in this process, is dead
2305 }
2306 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002307 return true;
2308 }
2309
The Android Open Source Project0727d222009-03-11 12:11:58 -07002310 public void setCurrentOrientationLw(int newOrientation) {
2311 synchronized (mLock) {
2312 if (newOrientation != mCurrentAppOrientation) {
2313 mCurrentAppOrientation = newOrientation;
2314 updateOrientationListenerLp();
2315 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002316 }
2317 }
Amith Yamasanic33cb712010-02-10 15:21:49 -08002318
The Android Open Source Project0727d222009-03-11 12:11:58 -07002319 public boolean performHapticFeedbackLw(WindowState win, int effectId, boolean always) {
Jim Miller89b17222009-10-27 14:54:09 -07002320 final boolean hapticsDisabled = Settings.System.getInt(mContext.getContentResolver(),
2321 Settings.System.HAPTIC_FEEDBACK_ENABLED, 0) == 0;
Dianne Hackborn9dc06cc2009-11-17 18:19:23 -08002322 if (!always && (hapticsDisabled || mKeyguardMediator.isShowingAndNotHidden())) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002323 return false;
2324 }
Amith Yamasanic33cb712010-02-10 15:21:49 -08002325 long[] pattern = null;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002326 switch (effectId) {
2327 case HapticFeedbackConstants.LONG_PRESS:
Amith Yamasanic33cb712010-02-10 15:21:49 -08002328 pattern = mLongPressVibePattern;
2329 break;
Dianne Hackbornb1a79802009-09-29 15:18:31 -07002330 case HapticFeedbackConstants.VIRTUAL_KEY:
Amith Yamasanic33cb712010-02-10 15:21:49 -08002331 pattern = mVirtualKeyVibePattern;
2332 break;
2333 case HapticFeedbackConstants.KEYBOARD_TAP:
2334 pattern = mKeyboardTapVibePattern;
2335 break;
Dianne Hackbornb1a79802009-09-29 15:18:31 -07002336 case HapticFeedbackConstants.SAFE_MODE_DISABLED:
Amith Yamasanic33cb712010-02-10 15:21:49 -08002337 pattern = mSafeModeDisabledVibePattern;
2338 break;
Dianne Hackbornb1a79802009-09-29 15:18:31 -07002339 case HapticFeedbackConstants.SAFE_MODE_ENABLED:
Amith Yamasanic33cb712010-02-10 15:21:49 -08002340 pattern = mSafeModeEnabledVibePattern;
2341 break;
Adam Powell9d86faf2010-02-10 11:39:57 -08002342 case HapticFeedbackConstants.SCROLL_BARRIER:
Amith Yamasanic33cb712010-02-10 15:21:49 -08002343 pattern = mScrollBarrierVibePattern;
2344 break;
2345 default:
2346 return false;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002347 }
Amith Yamasanic33cb712010-02-10 15:21:49 -08002348 if (pattern.length == 1) {
2349 // One-shot vibration
2350 mVibrator.vibrate(pattern[0]);
2351 } else {
2352 // Pattern vibration
2353 mVibrator.vibrate(pattern, -1);
2354 }
2355 return true;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002356 }
2357
Dianne Hackborn0041e972009-07-24 17:14:43 -07002358 public void keyFeedbackFromInput(KeyEvent event) {
2359 if (event.getAction() == KeyEvent.ACTION_DOWN
2360 && (event.getFlags()&KeyEvent.FLAG_VIRTUAL_HARD_KEY) != 0) {
Dianne Hackbornb1a79802009-09-29 15:18:31 -07002361 performHapticFeedbackLw(null, HapticFeedbackConstants.VIRTUAL_KEY, false);
Dianne Hackborn0041e972009-07-24 17:14:43 -07002362 }
2363 }
2364
The Android Open Source Project0727d222009-03-11 12:11:58 -07002365 public void screenOnStoppedLw() {
Dianne Hackborn9dc06cc2009-11-17 18:19:23 -08002366 if (!mKeyguardMediator.isShowingAndNotHidden() && mPowerManager.isScreenOn()) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002367 long curTime = SystemClock.uptimeMillis();
2368 mPowerManager.userActivity(curTime, false, LocalPowerManager.OTHER_EVENT);
2369 }
2370 }
Mike Lockwoodfcb39242009-10-21 23:01:07 -04002371
2372 public boolean allowKeyRepeat() {
2373 // disable key repeat when screen is off
2374 return mScreenOn;
2375 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002376}
Wink Saville37c124c2009-04-02 01:37:02 -07002377