blob: e61dd6caa6e631797afb5e741479521555864142 [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;
Dianne Hackborn78968392010-03-04 20:47:56 -080022import android.app.IUiModeManager;
23import android.app.UiModeManager;
Dianne Hackborn39c2d712009-09-22 11:41:31 -070024import android.content.ActivityNotFoundException;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080025import android.content.BroadcastReceiver;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080026import android.content.ContentResolver;
27import android.content.Context;
28import android.content.Intent;
Mike Lockwood1753f7f2009-08-24 14:49:07 -070029import android.content.IntentFilter;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080030import android.content.pm.ActivityInfo;
31import android.content.pm.PackageManager;
32import android.content.res.Configuration;
33import android.content.res.Resources;
34import android.database.ContentObserver;
Dianne Hackbornc777e072010-02-12 13:07:59 -080035import android.graphics.PixelFormat;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080036import android.graphics.Rect;
37import android.os.Handler;
38import android.os.IBinder;
39import android.os.LocalPowerManager;
Jeff Browna41ca772010-08-11 14:46:32 -070040import android.os.Looper;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080041import 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
Jim Millere6ad1a82010-08-20 19:25:39 -070049import com.android.internal.R;
Joe Onoratod208e702010-10-08 16:22:43 -040050import com.android.internal.app.ShutdownThread;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080051import com.android.internal.policy.PolicyManager;
Joe Onorato0cbda992010-05-02 16:28:15 -070052import com.android.internal.statusbar.IStatusBarService;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080053import com.android.internal.telephony.ITelephony;
Jeff Browna41ca772010-08-11 14:46:32 -070054import com.android.internal.view.BaseInputHandler;
Dianne Hackborn6019c9d2010-03-01 21:43:11 -080055import com.android.internal.widget.PointerLocationView;
56
Jeff Brown4d396052010-10-29 21:50:21 -070057import android.telephony.TelephonyManager;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080058import android.util.Config;
59import android.util.EventLog;
60import android.util.Log;
Jeff Browna41ca772010-08-11 14:46:32 -070061import android.util.Slog;
Mitsuru Oshima831d0d92009-06-16 18:27:18 -070062import android.view.Display;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080063import android.view.Gravity;
64import android.view.HapticFeedbackConstants;
65import android.view.IWindowManager;
Jeff Browna41ca772010-08-11 14:46:32 -070066import android.view.InputChannel;
Jeff Brown4d396052010-10-29 21:50:21 -070067import android.view.InputDevice;
Jeff Browna41ca772010-08-11 14:46:32 -070068import android.view.InputQueue;
69import android.view.InputHandler;
Jeff Brown6b53e8d2010-11-10 16:03:06 -080070import android.view.KeyCharacterMap;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080071import android.view.KeyEvent;
72import android.view.MotionEvent;
73import android.view.WindowOrientationListener;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080074import android.view.Surface;
75import android.view.View;
76import android.view.ViewConfiguration;
77import android.view.Window;
78import android.view.WindowManager;
79import static android.view.WindowManager.LayoutParams.FIRST_APPLICATION_WINDOW;
80import static android.view.WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN;
81import static android.view.WindowManager.LayoutParams.FLAG_FULLSCREEN;
82import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
83import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR;
84import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
Suchi Amalapurapu9cdc9032009-05-14 18:01:07 -070085import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
Dianne Hackborn39c2d712009-09-22 11:41:31 -070086import static android.view.WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD;
Mike Lockwood28569302010-01-28 11:54:40 -050087import static android.view.WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080088import static android.view.WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST;
89import static android.view.WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;
Dianne Hackborndea3ef72010-10-28 14:24:22 -070090import static android.view.WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080091import static android.view.WindowManager.LayoutParams.LAST_APPLICATION_WINDOW;
92import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA;
Dianne Hackborn5cb8d792009-05-21 17:34:15 -070093import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA_OVERLAY;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080094import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_PANEL;
95import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL;
96import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
Christopher Tatea53146c2010-09-07 11:57:52 -070097import static android.view.WindowManager.LayoutParams.TYPE_DRAG;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080098import static android.view.WindowManager.LayoutParams.TYPE_KEYGUARD;
99import static android.view.WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG;
100import static android.view.WindowManager.LayoutParams.TYPE_PHONE;
101import static android.view.WindowManager.LayoutParams.TYPE_PRIORITY_PHONE;
102import static android.view.WindowManager.LayoutParams.TYPE_SEARCH_BAR;
Jeff Brown3b2b3542010-10-15 00:54:27 -0700103import static android.view.WindowManager.LayoutParams.TYPE_SECURE_SYSTEM_OVERLAY;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800104import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR;
105import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR_PANEL;
Joe Onorato29fc2c92010-11-24 10:26:50 -0800106import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR_SUB_PANEL;
Dianne Hackborn6a294ce2009-12-03 11:37:44 -0800107import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800108import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
109import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
110import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
111import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
112import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
113import static android.view.WindowManager.LayoutParams.TYPE_TOAST;
Dianne Hackbornb7cd8b92009-08-08 20:53:03 -0700114import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800115import android.view.WindowManagerImpl;
116import android.view.WindowManagerPolicy;
Dianne Hackborn01ad2f42009-09-24 19:24:56 -0700117import android.view.animation.Animation;
118import android.view.animation.AnimationUtils;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800119import android.media.IAudioService;
120import android.media.AudioManager;
121
Dianne Hackborn08743722009-12-21 12:16:51 -0800122import java.util.ArrayList;
123
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800124/**
The Android Open Source Project0727d222009-03-11 12:11:58 -0700125 * WindowManagerPolicy implementation for the Android phone UI. This
126 * introduces a new method suffix, Lp, for an internal lock of the
127 * PhoneWindowManager. This is used to protect some internal state, and
128 * can be acquired with either thw Lw and Li lock held, so has the restrictions
129 * of both of those when held.
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800130 */
131public class PhoneWindowManager implements WindowManagerPolicy {
132 static final String TAG = "WindowManager";
133 static final boolean DEBUG = false;
134 static final boolean localLOGV = DEBUG ? Config.LOGD : Config.LOGV;
The Android Open Source Project11267662009-03-18 17:39:47 -0700135 static final boolean DEBUG_LAYOUT = false;
Jeff Brown49ed71d2010-12-06 17:13:33 -0800136 static final boolean DEBUG_FALLBACK = false;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800137 static final boolean SHOW_STARTING_ANIMATIONS = true;
138 static final boolean SHOW_PROCESSES_ON_ALT_MENU = false;
Joe Onoratod208e702010-10-08 16:22:43 -0400139
140 static final int LONG_PRESS_POWER_NOTHING = 0;
141 static final int LONG_PRESS_POWER_GLOBAL_ACTIONS = 1;
142 static final int LONG_PRESS_POWER_SHUT_OFF = 2;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800143
Joe Onorato46b0d682010-11-22 17:37:27 -0800144 static final int LONG_PRESS_HOME_NOTHING = 0;
145 static final int LONG_PRESS_HOME_RECENT_DIALOG = 1;
146 static final int LONG_PRESS_HOME_RECENT_ACTIVITY = 2;
147
Dianne Hackbornb7cd8b92009-08-08 20:53:03 -0700148 // wallpaper is at the bottom, though the window manager may move it.
Dianne Hackborn3c3bd442009-08-19 17:09:46 -0700149 static final int WALLPAPER_LAYER = 2;
Dianne Hackbornb7cd8b92009-08-08 20:53:03 -0700150 static final int APPLICATION_LAYER = 2;
151 static final int PHONE_LAYER = 3;
152 static final int SEARCH_BAR_LAYER = 4;
Joe Onorato29fc2c92010-11-24 10:26:50 -0800153 static final int STATUS_BAR_SUB_PANEL_LAYER = 5;
Dianne Hackborn6a294ce2009-12-03 11:37:44 -0800154 static final int SYSTEM_DIALOG_LAYER = 6;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800155 // toasts and the plugged-in battery thing
Dianne Hackborn6a294ce2009-12-03 11:37:44 -0800156 static final int TOAST_LAYER = 7;
157 static final int STATUS_BAR_LAYER = 8;
Joe Onorato29fc2c92010-11-24 10:26:50 -0800158 static final int STATUS_BAR_PANEL_LAYER = 9;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800159 // SIM errors and unlock. Not sure if this really should be in a high layer.
Joe Onorato29fc2c92010-11-24 10:26:50 -0800160 static final int PRIORITY_PHONE_LAYER = 10;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800161 // like the ANR / app crashed dialogs
Joe Onorato29fc2c92010-11-24 10:26:50 -0800162 static final int SYSTEM_ALERT_LAYER = 11;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800163 // system-level error dialogs
Joe Onorato29fc2c92010-11-24 10:26:50 -0800164 static final int SYSTEM_ERROR_LAYER = 12;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800165 // on-screen keyboards and other such input method user interfaces go here.
Joe Onorato29fc2c92010-11-24 10:26:50 -0800166 static final int INPUT_METHOD_LAYER = 13;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800167 // on-screen keyboards and other such input method user interfaces go here.
Joe Onorato29fc2c92010-11-24 10:26:50 -0800168 static final int INPUT_METHOD_DIALOG_LAYER = 14;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800169 // the keyguard; nothing on top of these can take focus, since they are
170 // responsible for power management when displayed.
Joe Onorato29fc2c92010-11-24 10:26:50 -0800171 static final int KEYGUARD_LAYER = 15;
172 static final int KEYGUARD_DIALOG_LAYER = 16;
Christopher Tatea53146c2010-09-07 11:57:52 -0700173 // the drag layer: input for drag-and-drop is associated with this window,
174 // which sits above all other focusable windows
Joe Onorato29fc2c92010-11-24 10:26:50 -0800175 static final int DRAG_LAYER = 17;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800176 // things in here CAN NOT take focus, but are shown on top of everything else.
Joe Onorato29fc2c92010-11-24 10:26:50 -0800177 static final int SYSTEM_OVERLAY_LAYER = 18;
178 static final int SECURE_SYSTEM_OVERLAY_LAYER = 19;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800179
Dianne Hackborn5cb8d792009-05-21 17:34:15 -0700180 static final int APPLICATION_MEDIA_SUBLAYER = -2;
181 static final int APPLICATION_MEDIA_OVERLAY_SUBLAYER = -1;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800182 static final int APPLICATION_PANEL_SUBLAYER = 1;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800183 static final int APPLICATION_SUB_PANEL_SUBLAYER = 2;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800184
185 // Debugging: set this to have the system act like there is no hard keyboard.
186 static final boolean KEYBOARD_ALWAYS_HIDDEN = false;
187
188 static public final String SYSTEM_DIALOG_REASON_KEY = "reason";
189 static public final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions";
190 static public final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";
Joe Onoratof275f0c2009-11-24 16:11:13 -0500191 static public final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800192
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700193 // Useful scan codes.
194 private static final int SW_LID = 0x00;
195 private static final int BTN_MOUSE = 0x110;
196
The Android Open Source Project0727d222009-03-11 12:11:58 -0700197 final Object mLock = new Object();
198
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800199 Context mContext;
200 IWindowManager mWindowManager;
201 LocalPowerManager mPowerManager;
Joe Onorato93056472010-09-10 10:30:46 -0400202 IStatusBarService mStatusBarService;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800203 Vibrator mVibrator; // Vibrator for giving feedback of orientation changes
204
Dianne Hackborn181ceb52009-08-27 22:16:40 -0700205 // Vibrator pattern for haptic feedback of a long press.
206 long[] mLongPressVibePattern;
207
208 // Vibrator pattern for haptic feedback of virtual key press.
209 long[] mVirtualKeyVibePattern;
210
Amith Yamasanic33cb712010-02-10 15:21:49 -0800211 // Vibrator pattern for a short vibration.
212 long[] mKeyboardTapVibePattern;
213
Dianne Hackbornb1a79802009-09-29 15:18:31 -0700214 // Vibrator pattern for haptic feedback during boot when safe mode is disabled.
215 long[] mSafeModeDisabledVibePattern;
216
217 // Vibrator pattern for haptic feedback during boot when safe mode is enabled.
218 long[] mSafeModeEnabledVibePattern;
Romain Guy8154cd32010-03-29 14:41:15 -0700219
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800220 /** If true, hitting shift & menu will broadcast Intent.ACTION_BUG_REPORT */
221 boolean mEnableShiftMenuBugReports = false;
222
223 boolean mSafeMode;
224 WindowState mStatusBar = null;
Daniel Sandler40427442010-07-16 11:44:52 -0400225 boolean mStatusBarCanHide;
Dianne Hackborn08743722009-12-21 12:16:51 -0800226 final ArrayList<WindowState> mStatusBarPanels = new ArrayList<WindowState>();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800227 WindowState mKeyguard = null;
228 KeyguardViewMediator mKeyguardMediator;
229 GlobalActions mGlobalActions;
Jeff Brown4d396052010-10-29 21:50:21 -0700230 volatile boolean mPowerKeyHandled;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800231 RecentApplicationsDialog mRecentAppsDialog;
232 Handler mHandler;
Dianne Hackborn05726582009-09-22 17:28:24 -0700233
Dianne Hackbornc777e072010-02-12 13:07:59 -0800234 boolean mSystemReady;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800235 boolean mLidOpen;
Dianne Hackborn78968392010-03-04 20:47:56 -0800236 int mUiMode = Configuration.UI_MODE_TYPE_NORMAL;
Dianne Hackborn867ab6472010-04-29 13:28:56 -0700237 int mDockMode = Intent.EXTRA_DOCK_STATE_UNDOCKED;
Mike Lockwood1753f7f2009-08-24 14:49:07 -0700238 int mLidOpenRotation;
Mike Lockwood2d1efbd2009-09-16 12:15:53 -0400239 int mCarDockRotation;
240 int mDeskDockRotation;
Daniel Sandlerb73617d2010-08-17 00:41:00 -0400241
242 int mUserRotationMode = WindowManagerPolicy.USER_ROTATION_FREE;
243 int mUserRotation = Surface.ROTATION_0;
244
Dianne Hackborna1111872010-11-23 20:55:11 -0800245 boolean mAllowAllRotations;
Mike Lockwoode9867d22009-09-20 01:59:02 -0400246 boolean mCarDockEnablesAccelerometer;
247 boolean mDeskDockEnablesAccelerometer;
Dianne Hackborn2862fff2009-09-15 22:56:29 -0700248 int mLidKeyboardAccessibility;
249 int mLidNavigationAccessibility;
Joe Onoratod208e702010-10-08 16:22:43 -0400250 int mLongPressOnPowerBehavior = -1;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800251 boolean mScreenOn = false;
252 boolean mOrientationSensorEnabled = false;
253 int mCurrentAppOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
254 static final int DEFAULT_ACCELEROMETER_ROTATION = 0;
255 int mAccelerometerDefault = DEFAULT_ACCELEROMETER_ROTATION;
256 boolean mHasSoftInput = false;
257
Dianne Hackbornc777e072010-02-12 13:07:59 -0800258 int mPointerLocationMode = 0;
259 PointerLocationView mPointerLocationView = null;
Jeff Browna41ca772010-08-11 14:46:32 -0700260 InputChannel mPointerLocationInputChannel;
261
262 private final InputHandler mPointerLocationInputHandler = new BaseInputHandler() {
263 @Override
Jeff Brown3915bb82010-11-05 15:02:16 -0700264 public void handleMotion(MotionEvent event, InputQueue.FinishedCallback finishedCallback) {
265 boolean handled = false;
266 try {
267 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
268 synchronized (mLock) {
269 if (mPointerLocationView != null) {
270 mPointerLocationView.addTouchEvent(event);
271 handled = true;
272 }
273 }
Jeff Brown93ed4e32010-09-23 13:51:48 -0700274 }
Jeff Brown3915bb82010-11-05 15:02:16 -0700275 } finally {
276 finishedCallback.finished(handled);
Jeff Browna41ca772010-08-11 14:46:32 -0700277 }
278 }
279 };
Dianne Hackbornc777e072010-02-12 13:07:59 -0800280
Joe Onorato29fc2c92010-11-24 10:26:50 -0800281 // The current size of the screen; really; (ir)regardless of whether the status
282 // bar can be hidden or not
283 int mUnrestrictedScreenLeft, mUnrestrictedScreenTop;
284 int mUnrestrictedScreenWidth, mUnrestrictedScreenHeight;
Dianne Hackborn82de1ae2010-10-28 11:28:39 -0700285 // The current size of the screen; these may be different than (0,0)-(dw,dh)
286 // if the status bar can't be hidden; in that case it effectively carves out
287 // that area of the display from all other windows.
Joe Onorato29fc2c92010-11-24 10:26:50 -0800288 int mRestrictedScreenLeft, mRestrictedScreenTop;
289 int mRestrictedScreenWidth, mRestrictedScreenHeight;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800290 // During layout, the current screen borders with all outer decoration
291 // (status bar, input method dock) accounted for.
292 int mCurLeft, mCurTop, mCurRight, mCurBottom;
293 // During layout, the frame in which content should be displayed
294 // to the user, accounting for all screen decoration except for any
295 // space they deem as available for other content. This is usually
296 // the same as mCur*, but may be larger if the screen decor has supplied
297 // content insets.
298 int mContentLeft, mContentTop, mContentRight, mContentBottom;
Dianne Hackborn8e11ef02010-11-18 19:47:42 -0800299 // During layout, the current screen borders along which input method
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800300 // windows are placed.
301 int mDockLeft, mDockTop, mDockRight, mDockBottom;
302 // During layout, the layer at which the doc window is placed.
303 int mDockLayer;
304
305 static final Rect mTmpParentFrame = new Rect();
306 static final Rect mTmpDisplayFrame = new Rect();
307 static final Rect mTmpContentFrame = new Rect();
308 static final Rect mTmpVisibleFrame = new Rect();
309
310 WindowState mTopFullscreenOpaqueWindowState;
Joe Onorato93056472010-09-10 10:30:46 -0400311 boolean mTopIsFullscreen;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800312 boolean mForceStatusBar;
Dianne Hackborn39c2d712009-09-22 11:41:31 -0700313 boolean mHideLockScreen;
314 boolean mDismissKeyguard;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800315 boolean mHomePressed;
316 Intent mHomeIntent;
Dianne Hackborn39c2d712009-09-22 11:41:31 -0700317 Intent mCarDockIntent;
318 Intent mDeskDockIntent;
Jeff Brown49ed71d2010-12-06 17:13:33 -0800319 int mShortcutKeyPressed = -1;
320 boolean mConsumeShortcutKeyUp;
Daniel Sandlere02d8082010-10-08 15:13:22 -0400321 boolean mShowMenuKey = false; // track FLAG_NEEDS_MENU_KEY on frontmost window
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800322
Mike Lockwood28569302010-01-28 11:54:40 -0500323 // support for activating the lock screen while the screen is on
324 boolean mAllowLockscreenWhenOn;
325 int mLockScreenTimeout;
326 boolean mLockScreenTimerActive;
327
David Brownbaf8d092010-03-08 21:52:59 -0800328 // Behavior of ENDCALL Button. (See Settings.System.END_BUTTON_BEHAVIOR.)
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800329 int mEndcallBehavior;
David Brownbaf8d092010-03-08 21:52:59 -0800330
331 // Behavior of POWER button while in-call and screen on.
332 // (See Settings.Secure.INCALL_POWER_BUTTON_BEHAVIOR.)
333 int mIncallPowerBehavior;
334
Steve Howarda7bfe6a2010-09-23 16:47:49 -0700335 int mLandscapeRotation = -1; // default landscape rotation
336 int mSeascapeRotation = -1; // "other" landscape rotation, 180 degrees from mLandscapeRotation
Dianne Hackborne5439f22010-10-02 16:53:50 -0700337 int mPortraitRotation = -1; // default portrait rotation
338 int mUpsideDownRotation = -1; // "other" portrait rotation
Mitsuru Oshima831d0d92009-06-16 18:27:18 -0700339
Dianne Hackborn03759ed2009-03-27 16:04:08 -0700340 // Nothing to see here, move along...
341 int mFancyRotationAnimation;
Jim Milleree969aa2010-08-26 20:17:43 -0700342
Joe Onorato46b0d682010-11-22 17:37:27 -0800343 // What we do when the user long presses on home
344 private int mLongPressOnHomeBehavior = -1;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800345
346 ShortcutManager mShortcutManager;
347 PowerManager.WakeLock mBroadcastWakeLock;
348
Jeff Brown49ed71d2010-12-06 17:13:33 -0800349 final KeyCharacterMap.FallbackAction mFallbackAction = new KeyCharacterMap.FallbackAction();
350
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800351 class SettingsObserver extends ContentObserver {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800352 SettingsObserver(Handler handler) {
353 super(handler);
354 }
David Brownbaf8d092010-03-08 21:52:59 -0800355
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800356 void observe() {
357 ContentResolver resolver = mContext.getContentResolver();
358 resolver.registerContentObserver(Settings.System.getUriFor(
359 Settings.System.END_BUTTON_BEHAVIOR), false, this);
David Brownbaf8d092010-03-08 21:52:59 -0800360 resolver.registerContentObserver(Settings.Secure.getUriFor(
361 Settings.Secure.INCALL_POWER_BUTTON_BEHAVIOR), false, this);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800362 resolver.registerContentObserver(Settings.System.getUriFor(
363 Settings.System.ACCELEROMETER_ROTATION), false, this);
Mike Lockwood28569302010-01-28 11:54:40 -0500364 resolver.registerContentObserver(Settings.System.getUriFor(
Daniel Sandlerb73617d2010-08-17 00:41:00 -0400365 Settings.System.USER_ROTATION), false, this);
366 resolver.registerContentObserver(Settings.System.getUriFor(
Mike Lockwood28569302010-01-28 11:54:40 -0500367 Settings.System.SCREEN_OFF_TIMEOUT), false, this);
Dianne Hackbornc777e072010-02-12 13:07:59 -0800368 resolver.registerContentObserver(Settings.System.getUriFor(
369 Settings.System.POINTER_LOCATION), false, this);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800370 resolver.registerContentObserver(Settings.Secure.getUriFor(
371 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
Dianne Hackborn03759ed2009-03-27 16:04:08 -0700372 resolver.registerContentObserver(Settings.System.getUriFor(
373 "fancy_rotation_anim"), false, this);
Dianne Hackbornc777e072010-02-12 13:07:59 -0800374 updateSettings();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800375 }
376
377 @Override public void onChange(boolean selfChange) {
Dianne Hackbornc777e072010-02-12 13:07:59 -0800378 updateSettings();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800379 try {
Dianne Hackborn32bc91d2009-03-27 16:16:03 -0700380 mWindowManager.setRotation(USE_LAST_ROTATION, false,
381 mFancyRotationAnimation);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800382 } catch (RemoteException e) {
383 // Ignore
384 }
385 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800386 }
387
388 class MyOrientationListener extends WindowOrientationListener {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800389 MyOrientationListener(Context context) {
390 super(context);
391 }
392
393 @Override
The Android Open Source Projectd06b0972009-03-13 13:04:23 -0700394 public void onOrientationChanged(int rotation) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800395 // Send updates based on orientation value
Dianne Hackborn05726582009-09-22 17:28:24 -0700396 if (localLOGV) Log.v(TAG, "onOrientationChanged, rotation changed to " +rotation);
Dianne Hackborn03759ed2009-03-27 16:04:08 -0700397 try {
Dianne Hackborn32bc91d2009-03-27 16:16:03 -0700398 mWindowManager.setRotation(rotation, false,
399 mFancyRotationAnimation);
Dianne Hackborn03759ed2009-03-27 16:04:08 -0700400 } catch (RemoteException e) {
401 // Ignore
Wink Saville37c124c2009-04-02 01:37:02 -0700402
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800403 }
404 }
405 }
406 MyOrientationListener mOrientationListener;
407
Dianne Hackborn03759ed2009-03-27 16:04:08 -0700408 boolean useSensorForOrientationLp(int appOrientation) {
Joe Onorato1b58b3e2009-09-22 16:44:16 -0700409 // The app says use the sensor.
Dianne Hackborne5439f22010-10-02 16:53:50 -0700410 if (appOrientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR
411 || appOrientation == ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR
412 || appOrientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE
413 || appOrientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800414 return true;
415 }
Joe Onorato1b58b3e2009-09-22 16:44:16 -0700416 // The user preference says we can rotate, and the app is willing to rotate.
417 if (mAccelerometerDefault != 0 &&
418 (appOrientation == ActivityInfo.SCREEN_ORIENTATION_USER
Dianne Hackborne5439f22010-10-02 16:53:50 -0700419 || appOrientation == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800420 return true;
421 }
Dianne Hackborne5439f22010-10-02 16:53:50 -0700422 // We're in a dock that has a rotation affinity, and the app is willing to rotate.
Dianne Hackborn867ab6472010-04-29 13:28:56 -0700423 if ((mCarDockEnablesAccelerometer && mDockMode == Intent.EXTRA_DOCK_STATE_CAR)
424 || (mDeskDockEnablesAccelerometer && mDockMode == Intent.EXTRA_DOCK_STATE_DESK)) {
Joe Onorato1b58b3e2009-09-22 16:44:16 -0700425 // Note we override the nosensor flag here.
426 if (appOrientation == ActivityInfo.SCREEN_ORIENTATION_USER
427 || appOrientation == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
Dianne Hackborne5439f22010-10-02 16:53:50 -0700428 || appOrientation == ActivityInfo.SCREEN_ORIENTATION_NOSENSOR) {
Joe Onorato1b58b3e2009-09-22 16:44:16 -0700429 return true;
430 }
431 }
432 // Else, don't use the sensor.
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800433 return false;
434 }
435
Suchi Amalapurapua9e1b162009-03-24 22:24:47 -0700436 /*
437 * We always let the sensor be switched on by default except when
438 * the user has explicitly disabled sensor based rotation or when the
439 * screen is switched off.
440 */
The Android Open Source Project0727d222009-03-11 12:11:58 -0700441 boolean needSensorRunningLp() {
Dianne Hackborne5439f22010-10-02 16:53:50 -0700442 if (mCurrentAppOrientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR
443 || mCurrentAppOrientation == ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR
444 || mCurrentAppOrientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT
445 || mCurrentAppOrientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800446 // If the application has explicitly requested to follow the
447 // orientation, then we need to turn the sensor or.
448 return true;
449 }
Dianne Hackborn867ab6472010-04-29 13:28:56 -0700450 if ((mCarDockEnablesAccelerometer && mDockMode == Intent.EXTRA_DOCK_STATE_CAR) ||
451 (mDeskDockEnablesAccelerometer && mDockMode == Intent.EXTRA_DOCK_STATE_DESK)) {
Mike Lockwoode9867d22009-09-20 01:59:02 -0400452 // enable accelerometer if we are docked in a dock that enables accelerometer
453 // orientation management,
454 return true;
455 }
Suchi Amalapurapua9e1b162009-03-24 22:24:47 -0700456 if (mAccelerometerDefault == 0) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800457 // If the setting for using the sensor by default is enabled, then
458 // we will always leave it on. Note that the user could go to
459 // a window that forces an orientation that does not use the
460 // sensor and in theory we could turn it off... however, when next
461 // turning it on we won't have a good value for the current
462 // orientation for a little bit, which can cause orientation
463 // changes to lag, so we'd like to keep it always on. (It will
464 // still be turned off when the screen is off.)
Suchi Amalapurapua9e1b162009-03-24 22:24:47 -0700465 return false;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800466 }
Suchi Amalapurapua9e1b162009-03-24 22:24:47 -0700467 return true;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800468 }
469
470 /*
471 * Various use cases for invoking this function
472 * screen turning off, should always disable listeners if already enabled
473 * screen turned on and current app has sensor based orientation, enable listeners
474 * if not already enabled
475 * screen turned on and current app does not have sensor orientation, disable listeners if
476 * already enabled
477 * screen turning on and current app has sensor based orientation, enable listeners if needed
478 * screen turning on and current app has nosensor based orientation, do nothing
479 */
The Android Open Source Project0727d222009-03-11 12:11:58 -0700480 void updateOrientationListenerLp() {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800481 if (!mOrientationListener.canDetectOrientation()) {
482 // If sensor is turned off or nonexistent for some reason
483 return;
484 }
485 //Could have been invoked due to screen turning on or off or
486 //change of the currently visible window's orientation
Dianne Hackborn05726582009-09-22 17:28:24 -0700487 if (localLOGV) Log.v(TAG, "Screen status="+mScreenOn+
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800488 ", current orientation="+mCurrentAppOrientation+
489 ", SensorEnabled="+mOrientationSensorEnabled);
490 boolean disable = true;
The Android Open Source Project0727d222009-03-11 12:11:58 -0700491 if (mScreenOn) {
492 if (needSensorRunningLp()) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800493 disable = false;
494 //enable listener if not already enabled
The Android Open Source Project0727d222009-03-11 12:11:58 -0700495 if (!mOrientationSensorEnabled) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800496 mOrientationListener.enable();
Dianne Hackborn05726582009-09-22 17:28:24 -0700497 if(localLOGV) Log.v(TAG, "Enabling listeners");
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800498 mOrientationSensorEnabled = true;
499 }
500 }
501 }
502 //check if sensors need to be disabled
The Android Open Source Project0727d222009-03-11 12:11:58 -0700503 if (disable && mOrientationSensorEnabled) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800504 mOrientationListener.disable();
Dianne Hackborn05726582009-09-22 17:28:24 -0700505 if(localLOGV) Log.v(TAG, "Disabling listeners");
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800506 mOrientationSensorEnabled = false;
507 }
508 }
509
Jeff Brown4d396052010-10-29 21:50:21 -0700510 private void interceptPowerKeyDown(boolean handled) {
511 mPowerKeyHandled = handled;
512 if (!handled) {
513 mHandler.postDelayed(mPowerLongPress, ViewConfiguration.getGlobalActionKeyTimeout());
514 }
515 }
516
517 private boolean interceptPowerKeyUp(boolean canceled) {
518 if (!mPowerKeyHandled) {
519 mHandler.removeCallbacks(mPowerLongPress);
520 return !canceled;
521 } else {
522 mPowerKeyHandled = true;
523 return false;
524 }
525 }
526
527 private final Runnable mPowerLongPress = new Runnable() {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800528 public void run() {
Jeff Brown4d396052010-10-29 21:50:21 -0700529 if (!mPowerKeyHandled) {
530 // The context isn't read
531 if (mLongPressOnPowerBehavior < 0) {
532 mLongPressOnPowerBehavior = mContext.getResources().getInteger(
533 com.android.internal.R.integer.config_longPressOnPowerBehavior);
534 }
535 switch (mLongPressOnPowerBehavior) {
536 case LONG_PRESS_POWER_NOTHING:
537 break;
538 case LONG_PRESS_POWER_GLOBAL_ACTIONS:
539 mPowerKeyHandled = true;
540 performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false);
541 sendCloseSystemWindows(SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS);
542 showGlobalActionsDialog();
543 break;
544 case LONG_PRESS_POWER_SHUT_OFF:
545 mPowerKeyHandled = true;
546 performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false);
547 sendCloseSystemWindows(SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS);
548 ShutdownThread.shutdown(mContext, true);
549 break;
550 }
Joe Onoratod208e702010-10-08 16:22:43 -0400551 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800552 }
553 };
554
555 void showGlobalActionsDialog() {
556 if (mGlobalActions == null) {
557 mGlobalActions = new GlobalActions(mContext);
558 }
Dianne Hackborn9dc06cc2009-11-17 18:19:23 -0800559 final boolean keyguardShowing = mKeyguardMediator.isShowingAndNotHidden();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800560 mGlobalActions.showDialog(keyguardShowing, isDeviceProvisioned());
561 if (keyguardShowing) {
562 // since it took two seconds of long press to bring this up,
563 // poke the wake lock so they have some time to see the dialog.
564 mKeyguardMediator.pokeWakelock();
565 }
566 }
567
568 boolean isDeviceProvisioned() {
569 return Settings.Secure.getInt(
570 mContext.getContentResolver(), Settings.Secure.DEVICE_PROVISIONED, 0) != 0;
571 }
572
573 /**
574 * When a home-key longpress expires, close other system windows and launch the recent apps
575 */
576 Runnable mHomeLongPress = new Runnable() {
577 public void run() {
578 /*
579 * Eat the longpress so it won't dismiss the recent apps dialog when
580 * the user lets go of the home key
581 */
582 mHomePressed = false;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800583 showRecentAppsDialog();
584 }
585 };
586
587 /**
588 * Create (if necessary) and launch the recent apps dialog
589 */
590 void showRecentAppsDialog() {
Jim Milleree969aa2010-08-26 20:17:43 -0700591 // We can't initialize this in init() since the configuration hasn't been loaded yet.
Joe Onorato46b0d682010-11-22 17:37:27 -0800592 if (mLongPressOnHomeBehavior < 0) {
593 mLongPressOnHomeBehavior
Joe Onorato50262e52010-11-26 14:04:54 -0800594 = mContext.getResources().getInteger(R.integer.config_longPressOnHomeBehavior);
Joe Onorato46b0d682010-11-22 17:37:27 -0800595 if (mLongPressOnHomeBehavior < LONG_PRESS_HOME_NOTHING ||
596 mLongPressOnHomeBehavior > LONG_PRESS_HOME_RECENT_ACTIVITY) {
597 mLongPressOnHomeBehavior = LONG_PRESS_HOME_NOTHING;
598 }
599 }
600
601 if (mLongPressOnHomeBehavior != LONG_PRESS_HOME_NOTHING) {
602 performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false);
603 sendCloseSystemWindows(SYSTEM_DIALOG_REASON_RECENT_APPS);
Jim Milleree969aa2010-08-26 20:17:43 -0700604 }
605
Joe Onorato46b0d682010-11-22 17:37:27 -0800606 if (mLongPressOnHomeBehavior == LONG_PRESS_HOME_RECENT_DIALOG) {
607 // Fallback to dialog if we fail to launch the above.
608 if (mRecentAppsDialog == null) {
609 mRecentAppsDialog = new RecentApplicationsDialog(mContext);
610 }
611 mRecentAppsDialog.show();
612 } else if (mLongPressOnHomeBehavior == LONG_PRESS_HOME_RECENT_ACTIVITY) {
Jim Millere6ad1a82010-08-20 19:25:39 -0700613 try {
Jim Milleree969aa2010-08-26 20:17:43 -0700614 Intent intent = new Intent();
615 intent.setClassName("com.android.systemui",
Joe Onorato46b0d682010-11-22 17:37:27 -0800616 "com.android.systemui.recent.RecentApplicationsActivity");
Jim Milleree969aa2010-08-26 20:17:43 -0700617 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
618 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
619 mContext.startActivity(intent);
Jim Millere6ad1a82010-08-20 19:25:39 -0700620 return;
621 } catch (ActivityNotFoundException e) {
622 Log.e(TAG, "Failed to launch RecentAppsIntent", e);
623 }
624 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800625 }
626
627 /** {@inheritDoc} */
628 public void init(Context context, IWindowManager windowManager,
629 LocalPowerManager powerManager) {
630 mContext = context;
631 mWindowManager = windowManager;
632 mPowerManager = powerManager;
633 mKeyguardMediator = new KeyguardViewMediator(context, this, powerManager);
634 mHandler = new Handler();
635 mOrientationListener = new MyOrientationListener(mContext);
636 SettingsObserver settingsObserver = new SettingsObserver(mHandler);
637 settingsObserver.observe();
638 mShortcutManager = new ShortcutManager(context, mHandler);
639 mShortcutManager.observe();
640 mHomeIntent = new Intent(Intent.ACTION_MAIN, null);
641 mHomeIntent.addCategory(Intent.CATEGORY_HOME);
642 mHomeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
643 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Dianne Hackborn39c2d712009-09-22 11:41:31 -0700644 mCarDockIntent = new Intent(Intent.ACTION_MAIN, null);
645 mCarDockIntent.addCategory(Intent.CATEGORY_CAR_DOCK);
646 mCarDockIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
647 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
648 mDeskDockIntent = new Intent(Intent.ACTION_MAIN, null);
649 mDeskDockIntent.addCategory(Intent.CATEGORY_DESK_DOCK);
650 mDeskDockIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
651 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Jim Millere6ad1a82010-08-20 19:25:39 -0700652
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800653 PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
654 mBroadcastWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
655 "PhoneWindowManager.mBroadcastWakeLock");
656 mEnableShiftMenuBugReports = "1".equals(SystemProperties.get("ro.debuggable"));
Dianne Hackborn2862fff2009-09-15 22:56:29 -0700657 mLidOpenRotation = readRotation(
Mike Lockwood2d1efbd2009-09-16 12:15:53 -0400658 com.android.internal.R.integer.config_lidOpenRotation);
659 mCarDockRotation = readRotation(
660 com.android.internal.R.integer.config_carDockRotation);
661 mDeskDockRotation = readRotation(
662 com.android.internal.R.integer.config_deskDockRotation);
Dianne Hackborna1111872010-11-23 20:55:11 -0800663 mAllowAllRotations = mContext.getResources().getBoolean(
664 com.android.internal.R.bool.config_allowAllRotations);
Mike Lockwoode9867d22009-09-20 01:59:02 -0400665 mCarDockEnablesAccelerometer = mContext.getResources().getBoolean(
666 com.android.internal.R.bool.config_carDockEnablesAccelerometer);
667 mDeskDockEnablesAccelerometer = mContext.getResources().getBoolean(
668 com.android.internal.R.bool.config_deskDockEnablesAccelerometer);
Dianne Hackborn2862fff2009-09-15 22:56:29 -0700669 mLidKeyboardAccessibility = mContext.getResources().getInteger(
670 com.android.internal.R.integer.config_lidKeyboardAccessibility);
671 mLidNavigationAccessibility = mContext.getResources().getInteger(
672 com.android.internal.R.integer.config_lidNavigationAccessibility);
Mike Lockwood1753f7f2009-08-24 14:49:07 -0700673 // register for dock events
Dianne Hackborn78968392010-03-04 20:47:56 -0800674 IntentFilter filter = new IntentFilter();
675 filter.addAction(UiModeManager.ACTION_ENTER_CAR_MODE);
676 filter.addAction(UiModeManager.ACTION_EXIT_CAR_MODE);
677 filter.addAction(UiModeManager.ACTION_ENTER_DESK_MODE);
678 filter.addAction(UiModeManager.ACTION_EXIT_DESK_MODE);
Dianne Hackborn867ab6472010-04-29 13:28:56 -0700679 filter.addAction(Intent.ACTION_DOCK_EVENT);
680 Intent intent = context.registerReceiver(mDockReceiver, filter);
681 if (intent != null) {
682 // Retrieve current sticky dock event broadcast.
683 mDockMode = intent.getIntExtra(Intent.EXTRA_DOCK_STATE,
684 Intent.EXTRA_DOCK_STATE_UNDOCKED);
685 }
Dianne Hackbornb1a79802009-09-29 15:18:31 -0700686 mVibrator = new Vibrator();
687 mLongPressVibePattern = getLongIntArray(mContext.getResources(),
688 com.android.internal.R.array.config_longPressVibePattern);
689 mVirtualKeyVibePattern = getLongIntArray(mContext.getResources(),
690 com.android.internal.R.array.config_virtualKeyVibePattern);
Amith Yamasanic33cb712010-02-10 15:21:49 -0800691 mKeyboardTapVibePattern = getLongIntArray(mContext.getResources(),
692 com.android.internal.R.array.config_keyboardTapVibePattern);
Dianne Hackbornb1a79802009-09-29 15:18:31 -0700693 mSafeModeDisabledVibePattern = getLongIntArray(mContext.getResources(),
694 com.android.internal.R.array.config_safeModeDisabledVibePattern);
695 mSafeModeEnabledVibePattern = getLongIntArray(mContext.getResources(),
696 com.android.internal.R.array.config_safeModeEnabledVibePattern);
Daniel Sandler40427442010-07-16 11:44:52 -0400697
698 // Note: the Configuration is not stable here, so we cannot load mStatusBarCanHide from
699 // config_statusBarCanHide because the latter depends on the screen size
Mike Lockwood1753f7f2009-08-24 14:49:07 -0700700 }
701
Dianne Hackbornc777e072010-02-12 13:07:59 -0800702 public void updateSettings() {
703 ContentResolver resolver = mContext.getContentResolver();
704 boolean updateRotation = false;
705 View addView = null;
706 View removeView = null;
707 synchronized (mLock) {
708 mEndcallBehavior = Settings.System.getInt(resolver,
David Brownbaf8d092010-03-08 21:52:59 -0800709 Settings.System.END_BUTTON_BEHAVIOR,
710 Settings.System.END_BUTTON_BEHAVIOR_DEFAULT);
711 mIncallPowerBehavior = Settings.Secure.getInt(resolver,
712 Settings.Secure.INCALL_POWER_BUTTON_BEHAVIOR,
713 Settings.Secure.INCALL_POWER_BUTTON_BEHAVIOR_DEFAULT);
Dianne Hackbornc777e072010-02-12 13:07:59 -0800714 mFancyRotationAnimation = Settings.System.getInt(resolver,
715 "fancy_rotation_anim", 0) != 0 ? 0x80 : 0;
716 int accelerometerDefault = Settings.System.getInt(resolver,
717 Settings.System.ACCELEROMETER_ROTATION, DEFAULT_ACCELEROMETER_ROTATION);
Daniel Sandlerb73617d2010-08-17 00:41:00 -0400718
719 // set up rotation lock state
Daniel Sandler1b815d62010-11-05 10:24:27 -0400720 mUserRotationMode = (accelerometerDefault == 0)
Daniel Sandlerb73617d2010-08-17 00:41:00 -0400721 ? WindowManagerPolicy.USER_ROTATION_LOCKED
722 : WindowManagerPolicy.USER_ROTATION_FREE;
723 mUserRotation = Settings.System.getInt(resolver,
724 Settings.System.USER_ROTATION,
725 Surface.ROTATION_0);
726
Dianne Hackbornc777e072010-02-12 13:07:59 -0800727 if (mAccelerometerDefault != accelerometerDefault) {
728 mAccelerometerDefault = accelerometerDefault;
729 updateOrientationListenerLp();
730 }
Daniel Sandlerb73617d2010-08-17 00:41:00 -0400731
Dianne Hackbornc777e072010-02-12 13:07:59 -0800732 if (mSystemReady) {
733 int pointerLocation = Settings.System.getInt(resolver,
734 Settings.System.POINTER_LOCATION, 0);
735 if (mPointerLocationMode != pointerLocation) {
736 mPointerLocationMode = pointerLocation;
737 if (pointerLocation != 0) {
738 if (mPointerLocationView == null) {
739 mPointerLocationView = new PointerLocationView(mContext);
740 mPointerLocationView.setPrintCoords(false);
741 addView = mPointerLocationView;
742 }
743 } else {
744 removeView = mPointerLocationView;
745 mPointerLocationView = null;
746 }
747 }
748 }
749 // use screen off timeout setting as the timeout for the lockscreen
750 mLockScreenTimeout = Settings.System.getInt(resolver,
751 Settings.System.SCREEN_OFF_TIMEOUT, 0);
752 String imId = Settings.Secure.getString(resolver,
753 Settings.Secure.DEFAULT_INPUT_METHOD);
754 boolean hasSoftInput = imId != null && imId.length() > 0;
755 if (mHasSoftInput != hasSoftInput) {
756 mHasSoftInput = hasSoftInput;
757 updateRotation = true;
758 }
759 }
760 if (updateRotation) {
761 updateRotation(0);
762 }
763 if (addView != null) {
764 WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
765 WindowManager.LayoutParams.MATCH_PARENT,
766 WindowManager.LayoutParams.MATCH_PARENT);
Jeff Brown3b2b3542010-10-15 00:54:27 -0700767 lp.type = WindowManager.LayoutParams.TYPE_SECURE_SYSTEM_OVERLAY;
Dianne Hackbornc777e072010-02-12 13:07:59 -0800768 lp.flags =
769 WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE|
770 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|
771 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
772 lp.format = PixelFormat.TRANSLUCENT;
773 lp.setTitle("PointerLocation");
774 WindowManagerImpl wm = (WindowManagerImpl)
775 mContext.getSystemService(Context.WINDOW_SERVICE);
776 wm.addView(addView, lp);
Jeff Browna41ca772010-08-11 14:46:32 -0700777
778 if (mPointerLocationInputChannel == null) {
779 try {
780 mPointerLocationInputChannel =
781 mWindowManager.monitorInput("PointerLocationView");
782 InputQueue.registerInputChannel(mPointerLocationInputChannel,
783 mPointerLocationInputHandler, mHandler.getLooper().getQueue());
784 } catch (RemoteException ex) {
785 Slog.e(TAG, "Could not set up input monitoring channel for PointerLocation.",
786 ex);
787 }
788 }
Dianne Hackbornc777e072010-02-12 13:07:59 -0800789 }
790 if (removeView != null) {
Jeff Browna41ca772010-08-11 14:46:32 -0700791 if (mPointerLocationInputChannel != null) {
792 InputQueue.unregisterInputChannel(mPointerLocationInputChannel);
793 mPointerLocationInputChannel.dispose();
794 mPointerLocationInputChannel = null;
795 }
796
Dianne Hackbornc777e072010-02-12 13:07:59 -0800797 WindowManagerImpl wm = (WindowManagerImpl)
798 mContext.getSystemService(Context.WINDOW_SERVICE);
799 wm.removeView(removeView);
800 }
801 }
802
Mike Lockwood2d1efbd2009-09-16 12:15:53 -0400803 private int readRotation(int resID) {
Mike Lockwood1753f7f2009-08-24 14:49:07 -0700804 try {
805 int rotation = mContext.getResources().getInteger(resID);
806 switch (rotation) {
807 case 0:
808 return Surface.ROTATION_0;
809 case 90:
810 return Surface.ROTATION_90;
811 case 180:
812 return Surface.ROTATION_180;
813 case 270:
814 return Surface.ROTATION_270;
Mike Lockwood1753f7f2009-08-24 14:49:07 -0700815 }
816 } catch (Resources.NotFoundException e) {
Mike Lockwood2d1efbd2009-09-16 12:15:53 -0400817 // fall through
Mike Lockwood1753f7f2009-08-24 14:49:07 -0700818 }
Mike Lockwood2d1efbd2009-09-16 12:15:53 -0400819 return -1;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800820 }
821
822 /** {@inheritDoc} */
823 public int checkAddPermission(WindowManager.LayoutParams attrs) {
824 int type = attrs.type;
Mitsuru Oshima831d0d92009-06-16 18:27:18 -0700825
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800826 if (type < WindowManager.LayoutParams.FIRST_SYSTEM_WINDOW
827 || type > WindowManager.LayoutParams.LAST_SYSTEM_WINDOW) {
828 return WindowManagerImpl.ADD_OKAY;
829 }
830 String permission = null;
831 switch (type) {
832 case TYPE_TOAST:
833 // XXX right now the app process has complete control over
834 // this... should introduce a token to let the system
835 // monitor/control what they are doing.
836 break;
837 case TYPE_INPUT_METHOD:
Dianne Hackbornb7cd8b92009-08-08 20:53:03 -0700838 case TYPE_WALLPAPER:
839 // The window manager will check these.
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800840 break;
841 case TYPE_PHONE:
842 case TYPE_PRIORITY_PHONE:
843 case TYPE_SYSTEM_ALERT:
844 case TYPE_SYSTEM_ERROR:
845 case TYPE_SYSTEM_OVERLAY:
846 permission = android.Manifest.permission.SYSTEM_ALERT_WINDOW;
847 break;
848 default:
849 permission = android.Manifest.permission.INTERNAL_SYSTEM_WINDOW;
850 }
851 if (permission != null) {
852 if (mContext.checkCallingOrSelfPermission(permission)
853 != PackageManager.PERMISSION_GRANTED) {
854 return WindowManagerImpl.ADD_PERMISSION_DENIED;
855 }
856 }
857 return WindowManagerImpl.ADD_OKAY;
858 }
859
860 public void adjustWindowParamsLw(WindowManager.LayoutParams attrs) {
861 switch (attrs.type) {
862 case TYPE_SYSTEM_OVERLAY:
Jeff Brown3b2b3542010-10-15 00:54:27 -0700863 case TYPE_SECURE_SYSTEM_OVERLAY:
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800864 case TYPE_TOAST:
865 // These types of windows can't receive input events.
866 attrs.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
867 | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
868 break;
869 }
870 }
871
872 void readLidState() {
873 try {
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700874 int sw = mWindowManager.getSwitchState(SW_LID);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800875 if (sw >= 0) {
876 mLidOpen = sw == 0;
877 }
878 } catch (RemoteException e) {
879 // Ignore
880 }
881 }
882
Dianne Hackborn2862fff2009-09-15 22:56:29 -0700883 private int determineHiddenState(boolean lidOpen,
884 int mode, int hiddenValue, int visibleValue) {
885 switch (mode) {
886 case 1:
887 return lidOpen ? visibleValue : hiddenValue;
888 case 2:
889 return lidOpen ? hiddenValue : visibleValue;
890 }
891 return visibleValue;
892 }
893
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800894 /** {@inheritDoc} */
895 public void adjustConfigurationLw(Configuration config) {
896 readLidState();
897 final boolean lidOpen = !KEYBOARD_ALWAYS_HIDDEN && mLidOpen;
898 mPowerManager.setKeyboardVisibility(lidOpen);
Dianne Hackborn2862fff2009-09-15 22:56:29 -0700899 config.hardKeyboardHidden = determineHiddenState(lidOpen,
900 mLidKeyboardAccessibility, Configuration.HARDKEYBOARDHIDDEN_YES,
901 Configuration.HARDKEYBOARDHIDDEN_NO);
902 config.navigationHidden = determineHiddenState(lidOpen,
903 mLidNavigationAccessibility, Configuration.NAVIGATIONHIDDEN_YES,
904 Configuration.NAVIGATIONHIDDEN_NO);
905 config.keyboardHidden = (config.hardKeyboardHidden
906 == Configuration.HARDKEYBOARDHIDDEN_NO || mHasSoftInput)
907 ? Configuration.KEYBOARDHIDDEN_NO
908 : Configuration.KEYBOARDHIDDEN_YES;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800909 }
910
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800911 /** {@inheritDoc} */
912 public int windowTypeToLayerLw(int type) {
913 if (type >= FIRST_APPLICATION_WINDOW && type <= LAST_APPLICATION_WINDOW) {
914 return APPLICATION_LAYER;
915 }
916 switch (type) {
917 case TYPE_STATUS_BAR:
918 return STATUS_BAR_LAYER;
919 case TYPE_STATUS_BAR_PANEL:
920 return STATUS_BAR_PANEL_LAYER;
Joe Onorato29fc2c92010-11-24 10:26:50 -0800921 case TYPE_STATUS_BAR_SUB_PANEL:
922 return STATUS_BAR_SUB_PANEL_LAYER;
Dianne Hackborn6a294ce2009-12-03 11:37:44 -0800923 case TYPE_SYSTEM_DIALOG:
924 return SYSTEM_DIALOG_LAYER;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800925 case TYPE_SEARCH_BAR:
926 return SEARCH_BAR_LAYER;
927 case TYPE_PHONE:
928 return PHONE_LAYER;
929 case TYPE_KEYGUARD:
930 return KEYGUARD_LAYER;
931 case TYPE_KEYGUARD_DIALOG:
932 return KEYGUARD_DIALOG_LAYER;
933 case TYPE_SYSTEM_ALERT:
934 return SYSTEM_ALERT_LAYER;
935 case TYPE_SYSTEM_ERROR:
936 return SYSTEM_ERROR_LAYER;
937 case TYPE_INPUT_METHOD:
938 return INPUT_METHOD_LAYER;
939 case TYPE_INPUT_METHOD_DIALOG:
940 return INPUT_METHOD_DIALOG_LAYER;
941 case TYPE_SYSTEM_OVERLAY:
942 return SYSTEM_OVERLAY_LAYER;
Jeff Brown3b2b3542010-10-15 00:54:27 -0700943 case TYPE_SECURE_SYSTEM_OVERLAY:
944 return SECURE_SYSTEM_OVERLAY_LAYER;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800945 case TYPE_PRIORITY_PHONE:
946 return PRIORITY_PHONE_LAYER;
947 case TYPE_TOAST:
948 return TOAST_LAYER;
Dianne Hackbornb7cd8b92009-08-08 20:53:03 -0700949 case TYPE_WALLPAPER:
950 return WALLPAPER_LAYER;
Christopher Tatea53146c2010-09-07 11:57:52 -0700951 case TYPE_DRAG:
952 return DRAG_LAYER;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800953 }
954 Log.e(TAG, "Unknown window type: " + type);
955 return APPLICATION_LAYER;
956 }
957
958 /** {@inheritDoc} */
959 public int subWindowTypeToLayerLw(int type) {
960 switch (type) {
961 case TYPE_APPLICATION_PANEL:
962 case TYPE_APPLICATION_ATTACHED_DIALOG:
963 return APPLICATION_PANEL_SUBLAYER;
964 case TYPE_APPLICATION_MEDIA:
965 return APPLICATION_MEDIA_SUBLAYER;
Dianne Hackborn5cb8d792009-05-21 17:34:15 -0700966 case TYPE_APPLICATION_MEDIA_OVERLAY:
967 return APPLICATION_MEDIA_OVERLAY_SUBLAYER;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800968 case TYPE_APPLICATION_SUB_PANEL:
969 return APPLICATION_SUB_PANEL_SUBLAYER;
970 }
971 Log.e(TAG, "Unknown sub-window type: " + type);
972 return 0;
973 }
974
Dianne Hackbornabe0da02009-09-18 01:55:39 -0700975 public int getMaxWallpaperLayer() {
976 return STATUS_BAR_LAYER;
977 }
978
Dianne Hackborn01ad2f42009-09-24 19:24:56 -0700979 public boolean doesForceHide(WindowState win, WindowManager.LayoutParams attrs) {
980 return attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD;
981 }
982
983 public boolean canBeForceHidden(WindowState win, WindowManager.LayoutParams attrs) {
984 return attrs.type != WindowManager.LayoutParams.TYPE_STATUS_BAR
985 && attrs.type != WindowManager.LayoutParams.TYPE_WALLPAPER;
986 }
987
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800988 /** {@inheritDoc} */
989 public View addStartingWindow(IBinder appToken, String packageName,
990 int theme, CharSequence nonLocalizedLabel,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -0800991 int labelRes, int icon, int windowFlags) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800992 if (!SHOW_STARTING_ANIMATIONS) {
993 return null;
994 }
995 if (packageName == null) {
996 return null;
997 }
998
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800999 try {
Dianne Hackborn867ab6472010-04-29 13:28:56 -07001000 Context context = mContext;
1001 boolean setTheme = false;
1002 //Log.i(TAG, "addStartingWindow " + packageName + ": nonLocalizedLabel="
1003 // + nonLocalizedLabel + " theme=" + Integer.toHexString(theme));
1004 if (theme != 0 || labelRes != 0) {
1005 try {
1006 context = context.createPackageContext(packageName, 0);
1007 if (theme != 0) {
1008 context.setTheme(theme);
1009 setTheme = true;
1010 }
1011 } catch (PackageManager.NameNotFoundException e) {
1012 // Ignore
1013 }
1014 }
1015 if (!setTheme) {
1016 context.setTheme(com.android.internal.R.style.Theme);
1017 }
1018
1019 Window win = PolicyManager.makeNewWindow(context);
1020 if (win.getWindowStyle().getBoolean(
1021 com.android.internal.R.styleable.Window_windowDisablePreview, false)) {
1022 return null;
1023 }
1024
1025 Resources r = context.getResources();
1026 win.setTitle(r.getText(labelRes, nonLocalizedLabel));
1027
1028 win.setType(
1029 WindowManager.LayoutParams.TYPE_APPLICATION_STARTING);
1030 // Force the window flags: this is a fake window, so it is not really
1031 // touchable or focusable by the user. We also add in the ALT_FOCUSABLE_IM
1032 // flag because we do know that the next window will take input
1033 // focus, so we want to get the IME window up on top of us right away.
1034 win.setFlags(
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001035 windowFlags|
Dianne Hackborn867ab6472010-04-29 13:28:56 -07001036 WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE|
1037 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|
1038 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001039 windowFlags|
Dianne Hackborn867ab6472010-04-29 13:28:56 -07001040 WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE|
1041 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|
1042 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
1043
1044 win.setLayout(WindowManager.LayoutParams.MATCH_PARENT,
1045 WindowManager.LayoutParams.MATCH_PARENT);
1046
1047 final WindowManager.LayoutParams params = win.getAttributes();
1048 params.token = appToken;
1049 params.packageName = packageName;
1050 params.windowAnimations = win.getWindowStyle().getResourceId(
1051 com.android.internal.R.styleable.Window_windowAnimationStyle, 0);
1052 params.setTitle("Starting " + packageName);
1053
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001054 WindowManagerImpl wm = (WindowManagerImpl)
1055 context.getSystemService(Context.WINDOW_SERVICE);
1056 View view = win.getDecorView();
1057
1058 if (win.isFloating()) {
1059 // Whoops, there is no way to display an animation/preview
1060 // of such a thing! After all that work... let's skip it.
1061 // (Note that we must do this here because it is in
1062 // getDecorView() where the theme is evaluated... maybe
1063 // we should peek the floating attribute from the theme
1064 // earlier.)
1065 return null;
1066 }
1067
1068 if (localLOGV) Log.v(
1069 TAG, "Adding starting window for " + packageName
1070 + " / " + appToken + ": "
1071 + (view.getParent() != null ? view : null));
1072
1073 wm.addView(view, params);
1074
1075 // Only return the view if it was successfully added to the
1076 // window manager... which we can tell by it having a parent.
1077 return view.getParent() != null ? view : null;
1078 } catch (WindowManagerImpl.BadTokenException e) {
1079 // ignore
1080 Log.w(TAG, appToken + " already running, starting window not displayed");
Dianne Hackborn867ab6472010-04-29 13:28:56 -07001081 } catch (RuntimeException e) {
1082 // don't crash if something else bad happens, for example a
1083 // failure loading resources because we are loading from an app
1084 // on external storage that has been unmounted.
1085 Log.w(TAG, appToken + " failed creating starting window", e);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001086 }
1087
1088 return null;
1089 }
1090
1091 /** {@inheritDoc} */
1092 public void removeStartingWindow(IBinder appToken, View window) {
1093 // RuntimeException e = new RuntimeException();
1094 // Log.i(TAG, "remove " + appToken + " " + window, e);
1095
1096 if (localLOGV) Log.v(
1097 TAG, "Removing starting window for " + appToken + ": " + window);
1098
1099 if (window != null) {
1100 WindowManagerImpl wm = (WindowManagerImpl) mContext.getSystemService(Context.WINDOW_SERVICE);
1101 wm.removeView(window);
1102 }
1103 }
1104
1105 /**
1106 * Preflight adding a window to the system.
1107 *
1108 * Currently enforces that three window types are singletons:
1109 * <ul>
1110 * <li>STATUS_BAR_TYPE</li>
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001111 * <li>KEYGUARD_TYPE</li>
1112 * </ul>
1113 *
1114 * @param win The window to be added
1115 * @param attrs Information about the window to be added
1116 *
1117 * @return If ok, WindowManagerImpl.ADD_OKAY. If too many singletons, WindowManagerImpl.ADD_MULTIPLE_SINGLETON
1118 */
1119 public int prepareAddWindowLw(WindowState win, WindowManager.LayoutParams attrs) {
1120 switch (attrs.type) {
1121 case TYPE_STATUS_BAR:
Joe Onorato8bc6c512010-06-04 16:21:12 -04001122 mContext.enforceCallingOrSelfPermission(
1123 android.Manifest.permission.STATUS_BAR_SERVICE,
1124 "PhoneWindowManager");
1125 // TODO: Need to handle the race condition of the status bar proc
1126 // dying and coming back before the removeWindowLw cleanup has happened.
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001127 if (mStatusBar != null) {
1128 return WindowManagerImpl.ADD_MULTIPLE_SINGLETON;
1129 }
1130 mStatusBar = win;
Daniel Sandler40427442010-07-16 11:44:52 -04001131
1132 // The Configuration will be stable by now, so we can load this
1133 mStatusBarCanHide = mContext.getResources().getBoolean(
1134 com.android.internal.R.bool.config_statusBarCanHide);
1135
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001136 break;
Dianne Hackborn08743722009-12-21 12:16:51 -08001137 case TYPE_STATUS_BAR_PANEL:
Joe Onorato8bc6c512010-06-04 16:21:12 -04001138 mContext.enforceCallingOrSelfPermission(
1139 android.Manifest.permission.STATUS_BAR_SERVICE,
1140 "PhoneWindowManager");
Dianne Hackborn08743722009-12-21 12:16:51 -08001141 mStatusBarPanels.add(win);
1142 break;
Joe Onorato29fc2c92010-11-24 10:26:50 -08001143 case TYPE_STATUS_BAR_SUB_PANEL:
1144 mContext.enforceCallingOrSelfPermission(
1145 android.Manifest.permission.STATUS_BAR_SERVICE,
1146 "PhoneWindowManager");
1147 mStatusBarPanels.add(win);
1148 break;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001149 case TYPE_KEYGUARD:
1150 if (mKeyguard != null) {
1151 return WindowManagerImpl.ADD_MULTIPLE_SINGLETON;
1152 }
1153 mKeyguard = win;
1154 break;
1155 }
1156 return WindowManagerImpl.ADD_OKAY;
1157 }
1158
1159 /** {@inheritDoc} */
1160 public void removeWindowLw(WindowState win) {
1161 if (mStatusBar == win) {
1162 mStatusBar = null;
1163 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001164 else if (mKeyguard == win) {
1165 mKeyguard = null;
Dianne Hackborn08743722009-12-21 12:16:51 -08001166 } else {
1167 mStatusBarPanels.remove(win);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001168 }
1169 }
1170
1171 static final boolean PRINT_ANIM = false;
1172
1173 /** {@inheritDoc} */
1174 public int selectAnimationLw(WindowState win, int transit) {
1175 if (PRINT_ANIM) Log.i(TAG, "selectAnimation in " + win
1176 + ": transit=" + transit);
1177 if (transit == TRANSIT_PREVIEW_DONE) {
1178 if (win.hasAppShownWindows()) {
1179 if (PRINT_ANIM) Log.i(TAG, "**** STARTING EXIT");
1180 return com.android.internal.R.anim.app_starting_exit;
1181 }
1182 }
1183
1184 return 0;
1185 }
1186
Dianne Hackborn01ad2f42009-09-24 19:24:56 -07001187 public Animation createForceHideEnterAnimation() {
1188 return AnimationUtils.loadAnimation(mContext,
1189 com.android.internal.R.anim.lock_screen_behind_enter);
1190 }
1191
Jeff Brown4d396052010-10-29 21:50:21 -07001192 static ITelephony getTelephonyService() {
1193 ITelephony telephonyService = ITelephony.Stub.asInterface(
1194 ServiceManager.checkService(Context.TELEPHONY_SERVICE));
1195 if (telephonyService == null) {
1196 Log.w(TAG, "Unable to find ITelephony interface.");
1197 }
1198 return telephonyService;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001199 }
1200
Jeff Brown4d396052010-10-29 21:50:21 -07001201 static IAudioService getAudioService() {
1202 IAudioService audioService = IAudioService.Stub.asInterface(
1203 ServiceManager.checkService(Context.AUDIO_SERVICE));
1204 if (audioService == null) {
1205 Log.w(TAG, "Unable to find IAudioService interface.");
1206 }
1207 return audioService;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001208 }
1209
1210 boolean keyguardOn() {
1211 return keyguardIsShowingTq() || inKeyguardRestrictedKeyInputMode();
1212 }
1213
1214 private static final int[] WINDOW_TYPES_WHERE_HOME_DOESNT_WORK = {
1215 WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
1216 WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
1217 };
1218
1219 /** {@inheritDoc} */
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001220 @Override
Jeff Brown1f245102010-11-18 20:53:46 -08001221 public boolean interceptKeyBeforeDispatching(WindowState win, KeyEvent event, int policyFlags) {
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001222 final boolean keyguardOn = keyguardOn();
Jeff Brown1f245102010-11-18 20:53:46 -08001223 final int keyCode = event.getKeyCode();
1224 final int repeatCount = event.getRepeatCount();
1225 final int metaState = event.getMetaState();
1226 final boolean down = event.getAction() == KeyEvent.ACTION_DOWN;
1227 final boolean canceled = event.isCanceled();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001228
1229 if (false) {
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001230 Log.d(TAG, "interceptKeyTi keyCode=" + keyCode + " down=" + down + " repeatCount="
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001231 + repeatCount + " keyguardOn=" + keyguardOn + " mHomePressed=" + mHomePressed);
1232 }
1233
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001234 // First we always handle the home key here, so applications
1235 // can never break it, although if keyguard is on, we do let
1236 // it handle it, because that gives us the correct 5 second
1237 // timeout.
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001238 if (keyCode == KeyEvent.KEYCODE_HOME) {
Jeff Brown49ed71d2010-12-06 17:13:33 -08001239 // Clear a pending HOME longpress if the user releases Home
1240 if (!down) {
1241 mHandler.removeCallbacks(mHomeLongPress);
1242 }
1243
1244 // If we have released the home key, and didn't do anything else
1245 // while it was pressed, then it is time to go home!
1246 if (mHomePressed && !down) {
1247 mHomePressed = false;
1248 if (!canceled) {
1249 // If an incoming call is ringing, HOME is totally disabled.
1250 // (The user is already on the InCallScreen at this point,
1251 // and his ONLY options are to answer or reject the call.)
1252 boolean incomingRinging = false;
1253 try {
1254 ITelephony telephonyService = getTelephonyService();
1255 if (telephonyService != null) {
1256 incomingRinging = telephonyService.isRinging();
1257 }
1258 } catch (RemoteException ex) {
1259 Log.w(TAG, "RemoteException from getPhoneInterface()", ex);
1260 }
1261
1262 if (incomingRinging) {
1263 Log.i(TAG, "Ignoring HOME; there's a ringing incoming call.");
1264 } else {
1265 launchHomeFromHotKey();
1266 }
1267 } else {
1268 Log.i(TAG, "Ignoring HOME; event canceled.");
1269 }
1270 return true;
1271 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001272
1273 // If a system window has focus, then it doesn't make sense
1274 // right now to interact with applications.
1275 WindowManager.LayoutParams attrs = win != null ? win.getAttrs() : null;
1276 if (attrs != null) {
1277 final int type = attrs.type;
1278 if (type == WindowManager.LayoutParams.TYPE_KEYGUARD
1279 || type == WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG) {
1280 // the "app" is keyguard, so give it the key
1281 return false;
1282 }
1283 final int typeCount = WINDOW_TYPES_WHERE_HOME_DOESNT_WORK.length;
1284 for (int i=0; i<typeCount; i++) {
1285 if (type == WINDOW_TYPES_WHERE_HOME_DOESNT_WORK[i]) {
1286 // don't do anything, but also don't pass it to the app
1287 return true;
1288 }
1289 }
1290 }
1291
1292 if (down && repeatCount == 0) {
1293 if (!keyguardOn) {
1294 mHandler.postDelayed(mHomeLongPress, ViewConfiguration.getGlobalActionKeyTimeout());
1295 }
1296 mHomePressed = true;
1297 }
1298 return true;
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001299 } else if (keyCode == KeyEvent.KEYCODE_MENU) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001300 // Hijack modified menu keys for debugging features
1301 final int chordBug = KeyEvent.META_SHIFT_ON;
1302
1303 if (down && repeatCount == 0) {
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001304 if (mEnableShiftMenuBugReports && (metaState & chordBug) == chordBug) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001305 Intent intent = new Intent(Intent.ACTION_BUG_REPORT);
1306 mContext.sendOrderedBroadcast(intent, null);
1307 return true;
1308 } else if (SHOW_PROCESSES_ON_ALT_MENU &&
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001309 (metaState & KeyEvent.META_ALT_ON) == KeyEvent.META_ALT_ON) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001310 Intent service = new Intent();
1311 service.setClassName(mContext, "com.android.server.LoadAverageService");
1312 ContentResolver res = mContext.getContentResolver();
1313 boolean shown = Settings.System.getInt(
1314 res, Settings.System.SHOW_PROCESSES, 0) != 0;
1315 if (!shown) {
1316 mContext.startService(service);
1317 } else {
1318 mContext.stopService(service);
1319 }
1320 Settings.System.putInt(
1321 res, Settings.System.SHOW_PROCESSES, shown ? 0 : 1);
1322 return true;
1323 }
1324 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001325 } else if (keyCode == KeyEvent.KEYCODE_SEARCH) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001326 if (down) {
1327 if (repeatCount == 0) {
Jeff Brown49ed71d2010-12-06 17:13:33 -08001328 mShortcutKeyPressed = keyCode;
1329 mConsumeShortcutKeyUp = false;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001330 }
Jeff Brown49ed71d2010-12-06 17:13:33 -08001331 } else if (keyCode == mShortcutKeyPressed) {
1332 mShortcutKeyPressed = -1;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001333
Jeff Brown49ed71d2010-12-06 17:13:33 -08001334 if (mConsumeShortcutKeyUp) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001335 // Consume the up-event
Jeff Brown49ed71d2010-12-06 17:13:33 -08001336 mConsumeShortcutKeyUp = false;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001337 return true;
1338 }
1339 }
Jeff Brown49ed71d2010-12-06 17:13:33 -08001340 } else if (keyCode == KeyEvent.KEYCODE_APP_SWITCH) {
1341 if (!down) {
1342 showRecentAppsDialog();
1343 }
1344 return true;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001345 }
1346
Jeff Brown49ed71d2010-12-06 17:13:33 -08001347 // Shortcuts are invoked through Search+key or Meta+key, so intercept those here
1348 if ((mShortcutKeyPressed != -1 && !mConsumeShortcutKeyUp)
1349 || (metaState & KeyEvent.META_META_ON) != 0) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001350 if (down && repeatCount == 0 && !keyguardOn) {
Jeff Brown1f245102010-11-18 20:53:46 -08001351 Intent shortcutIntent = mShortcutManager.getIntent(event);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001352 if (shortcutIntent != null) {
1353 shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1354 mContext.startActivity(shortcutIntent);
1355
1356 /*
1357 * We launched an app, so the up-event of the search key
1358 * should be consumed
1359 */
Jeff Brown49ed71d2010-12-06 17:13:33 -08001360 if (mShortcutKeyPressed != -1) {
1361 mConsumeShortcutKeyUp = true;
1362 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001363 return true;
1364 }
1365 }
1366 }
1367
1368 return false;
1369 }
1370
Jeff Brown3915bb82010-11-05 15:02:16 -07001371 /** {@inheritDoc} */
1372 @Override
Jeff Brown49ed71d2010-12-06 17:13:33 -08001373 public KeyEvent dispatchUnhandledKey(WindowState win, KeyEvent event, int policyFlags) {
1374 if (DEBUG_FALLBACK) {
Jeff Brown1f245102010-11-18 20:53:46 -08001375 Slog.d(TAG, "Unhandled key: win=" + win + ", action=" + event.getAction()
1376 + ", flags=" + event.getFlags()
1377 + ", keyCode=" + event.getKeyCode()
1378 + ", scanCode=" + event.getScanCode()
1379 + ", metaState=" + event.getMetaState()
1380 + ", repeatCount=" + event.getRepeatCount()
1381 + ", policyFlags=" + policyFlags);
Jeff Brown3915bb82010-11-05 15:02:16 -07001382 }
Jeff Brown49ed71d2010-12-06 17:13:33 -08001383
1384 if ((event.getFlags() & KeyEvent.FLAG_FALLBACK) == 0) {
1385 final KeyCharacterMap kcm = event.getKeyCharacterMap();
1386 boolean fallback = kcm.getFallbackAction(event.getKeyCode(), event.getMetaState(),
1387 mFallbackAction);
1388
1389 if (fallback) {
1390 if (DEBUG_FALLBACK) {
1391 Slog.d(TAG, "Fallback: keyCode=" + mFallbackAction.keyCode
1392 + " metaState=" + Integer.toHexString(mFallbackAction.metaState));
1393 }
1394
1395 int flags = event.getFlags() | KeyEvent.FLAG_FALLBACK;
1396 KeyEvent fallbackEvent = KeyEvent.obtain(
1397 event.getDownTime(), event.getEventTime(),
1398 event.getAction(), mFallbackAction.keyCode,
1399 event.getRepeatCount(), mFallbackAction.metaState,
1400 event.getDeviceId(), event.getScanCode(),
1401 flags, event.getSource(), null);
1402 int actions = interceptKeyBeforeQueueing(fallbackEvent, policyFlags, true);
1403 if ((actions & ACTION_PASS_TO_USER) != 0) {
1404 if (!interceptKeyBeforeDispatching(win, fallbackEvent, policyFlags)) {
1405 if (DEBUG_FALLBACK) {
1406 Slog.d(TAG, "Performing fallback.");
1407 }
1408 return fallbackEvent;
1409 }
1410 }
1411 fallbackEvent.recycle();
1412 }
1413 }
1414
1415 if (DEBUG_FALLBACK) {
1416 Slog.d(TAG, "No fallback.");
1417 }
1418 return null;
Jeff Brown3915bb82010-11-05 15:02:16 -07001419 }
1420
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001421 /**
1422 * A home key -> launch home action was detected. Take the appropriate action
1423 * given the situation with the keyguard.
1424 */
1425 void launchHomeFromHotKey() {
Dianne Hackborn9dc06cc2009-11-17 18:19:23 -08001426 if (mKeyguardMediator.isShowingAndNotHidden()) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001427 // don't launch home if keyguard showing
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001428 } else if (!mHideLockScreen && mKeyguardMediator.isInputRestricted()) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001429 // when in keyguard restricted mode, must first verify unlock
1430 // before launching home
1431 mKeyguardMediator.verifyUnlock(new OnKeyguardExitResult() {
1432 public void onKeyguardExitResult(boolean success) {
1433 if (success) {
Dianne Hackborn256dd3b2009-05-19 18:51:21 -07001434 try {
1435 ActivityManagerNative.getDefault().stopAppSwitches();
1436 } catch (RemoteException e) {
1437 }
Joe Onoratof275f0c2009-11-24 16:11:13 -05001438 sendCloseSystemWindows(SYSTEM_DIALOG_REASON_HOME_KEY);
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001439 startDockOrHome();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001440 }
1441 }
1442 });
1443 } else {
1444 // no keyguard stuff to worry about, just launch home!
Dianne Hackborn256dd3b2009-05-19 18:51:21 -07001445 try {
1446 ActivityManagerNative.getDefault().stopAppSwitches();
1447 } catch (RemoteException e) {
1448 }
Joe Onoratof275f0c2009-11-24 16:11:13 -05001449 sendCloseSystemWindows(SYSTEM_DIALOG_REASON_HOME_KEY);
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001450 startDockOrHome();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001451 }
1452 }
1453
1454 public void getContentInsetHintLw(WindowManager.LayoutParams attrs, Rect contentInset) {
1455 final int fl = attrs.flags;
1456
Dianne Hackborn82de1ae2010-10-28 11:28:39 -07001457 if ((fl & (FLAG_LAYOUT_IN_SCREEN | FLAG_FULLSCREEN | FLAG_LAYOUT_INSET_DECOR))
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001458 == (FLAG_LAYOUT_IN_SCREEN | FLAG_LAYOUT_INSET_DECOR)) {
Dianne Hackborn82de1ae2010-10-28 11:28:39 -07001459 contentInset.set(mCurLeft, mCurTop,
Joe Onorato29fc2c92010-11-24 10:26:50 -08001460 (mRestrictedScreenLeft+mRestrictedScreenWidth) - mCurRight,
1461 (mRestrictedScreenTop+mRestrictedScreenHeight) - mCurBottom);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001462 } else {
1463 contentInset.setEmpty();
1464 }
1465 }
1466
1467 /** {@inheritDoc} */
1468 public void beginLayoutLw(int displayWidth, int displayHeight) {
Joe Onorato29fc2c92010-11-24 10:26:50 -08001469 mUnrestrictedScreenLeft = mUnrestrictedScreenTop = 0;
1470 mUnrestrictedScreenWidth = displayWidth;
1471 mUnrestrictedScreenHeight = displayHeight;
1472 mRestrictedScreenLeft = mRestrictedScreenTop = 0;
1473 mRestrictedScreenWidth = displayWidth;
1474 mRestrictedScreenHeight = displayHeight;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001475 mDockLeft = mContentLeft = mCurLeft = 0;
1476 mDockTop = mContentTop = mCurTop = 0;
1477 mDockRight = mContentRight = mCurRight = displayWidth;
1478 mDockBottom = mContentBottom = mCurBottom = displayHeight;
1479 mDockLayer = 0x10000000;
1480
1481 // decide where the status bar goes ahead of time
1482 if (mStatusBar != null) {
1483 final Rect pf = mTmpParentFrame;
1484 final Rect df = mTmpDisplayFrame;
1485 final Rect vf = mTmpVisibleFrame;
1486 pf.left = df.left = vf.left = 0;
1487 pf.top = df.top = vf.top = 0;
1488 pf.right = df.right = vf.right = displayWidth;
1489 pf.bottom = df.bottom = vf.bottom = displayHeight;
1490
1491 mStatusBar.computeFrameLw(pf, df, vf, vf);
1492 if (mStatusBar.isVisibleLw()) {
1493 // If the status bar is hidden, we don't want to cause
1494 // windows behind it to scroll.
Joe Onorato34bcebc2010-07-07 18:05:01 -04001495 final Rect r = mStatusBar.getFrameLw();
Dianne Hackborn82de1ae2010-10-28 11:28:39 -07001496 if (mStatusBarCanHide) {
1497 // Status bar may go away, so the screen area it occupies
1498 // is available to apps but just covering them when the
1499 // status bar is visible.
1500 if (mDockTop == r.top) mDockTop = r.bottom;
1501 else if (mDockBottom == r.bottom) mDockBottom = r.top;
1502 mContentTop = mCurTop = mDockTop;
1503 mContentBottom = mCurBottom = mDockBottom;
1504 if (DEBUG_LAYOUT) Log.v(TAG, "Status bar: mDockTop=" + mDockTop
1505 + " mContentTop=" + mContentTop
1506 + " mCurTop=" + mCurTop
1507 + " mDockBottom=" + mDockBottom
1508 + " mContentBottom=" + mContentBottom
1509 + " mCurBottom=" + mCurBottom);
1510 } else {
1511 // Status bar can't go away; the part of the screen it
1512 // covers does not exist for anything behind it.
Joe Onorato29fc2c92010-11-24 10:26:50 -08001513 if (mRestrictedScreenTop == r.top) {
1514 mRestrictedScreenTop = r.bottom;
1515 mRestrictedScreenHeight -= (r.bottom-r.top);
1516 } else if ((mRestrictedScreenHeight-mRestrictedScreenTop) == r.bottom) {
1517 mRestrictedScreenHeight -= (r.bottom-r.top);
Dianne Hackborn82de1ae2010-10-28 11:28:39 -07001518 }
Joe Onorato29fc2c92010-11-24 10:26:50 -08001519 mContentTop = mCurTop = mDockTop = mRestrictedScreenTop;
1520 mContentBottom = mCurBottom = mDockBottom
1521 = mRestrictedScreenTop + mRestrictedScreenHeight;
1522 if (DEBUG_LAYOUT) Log.v(TAG, "Status bar: mRestrictedScreenTop="
1523 + mRestrictedScreenTop
1524 + " mRestrictedScreenHeight=" + mRestrictedScreenHeight);
Dianne Hackborn82de1ae2010-10-28 11:28:39 -07001525 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001526 }
1527 }
1528 }
1529
Dianne Hackborndea3ef72010-10-28 14:24:22 -07001530 void setAttachedWindowFrames(WindowState win, int fl, int adjust,
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001531 WindowState attached, boolean insetDecors, Rect pf, Rect df, Rect cf, Rect vf) {
1532 if (win.getSurfaceLayer() > mDockLayer && attached.getSurfaceLayer() < mDockLayer) {
1533 // Here's a special case: if this attached window is a panel that is
1534 // above the dock window, and the window it is attached to is below
1535 // the dock window, then the frames we computed for the window it is
1536 // attached to can not be used because the dock is effectively part
1537 // of the underlying window and the attached window is floating on top
1538 // of the whole thing. So, we ignore the attached window and explicitly
1539 // compute the frames that would be appropriate without the dock.
1540 df.left = cf.left = vf.left = mDockLeft;
1541 df.top = cf.top = vf.top = mDockTop;
1542 df.right = cf.right = vf.right = mDockRight;
1543 df.bottom = cf.bottom = vf.bottom = mDockBottom;
1544 } else {
1545 // The effective display frame of the attached window depends on
1546 // whether it is taking care of insetting its content. If not,
1547 // we need to use the parent's content frame so that the entire
1548 // window is positioned within that content. Otherwise we can use
1549 // the display frame and let the attached window take care of
1550 // positioning its content appropriately.
Dianne Hackborndea3ef72010-10-28 14:24:22 -07001551 if (adjust != SOFT_INPUT_ADJUST_RESIZE) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001552 cf.set(attached.getDisplayFrameLw());
1553 } else {
1554 // If the window is resizing, then we want to base the content
1555 // frame on our attached content frame to resize... however,
1556 // things can be tricky if the attached window is NOT in resize
1557 // mode, in which case its content frame will be larger.
1558 // Ungh. So to deal with that, make sure the content frame
1559 // we end up using is not covering the IM dock.
1560 cf.set(attached.getContentFrameLw());
1561 if (attached.getSurfaceLayer() < mDockLayer) {
1562 if (cf.left < mContentLeft) cf.left = mContentLeft;
1563 if (cf.top < mContentTop) cf.top = mContentTop;
1564 if (cf.right > mContentRight) cf.right = mContentRight;
1565 if (cf.bottom > mContentBottom) cf.bottom = mContentBottom;
1566 }
1567 }
1568 df.set(insetDecors ? attached.getDisplayFrameLw() : cf);
1569 vf.set(attached.getVisibleFrameLw());
1570 }
1571 // The LAYOUT_IN_SCREEN flag is used to determine whether the attached
1572 // window should be positioned relative to its parent or the entire
1573 // screen.
1574 pf.set((fl & FLAG_LAYOUT_IN_SCREEN) == 0
1575 ? attached.getFrameLw() : df);
1576 }
1577
1578 /** {@inheritDoc} */
Dianne Hackborn0ecadf72009-03-31 18:00:37 -07001579 public void layoutWindowLw(WindowState win, WindowManager.LayoutParams attrs,
1580 WindowState attached) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001581 // we've already done the status bar
1582 if (win == mStatusBar) {
1583 return;
1584 }
1585
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001586 final int fl = attrs.flags;
1587 final int sim = attrs.softInputMode;
1588
1589 final Rect pf = mTmpParentFrame;
1590 final Rect df = mTmpDisplayFrame;
1591 final Rect cf = mTmpContentFrame;
1592 final Rect vf = mTmpVisibleFrame;
1593
1594 if (attrs.type == TYPE_INPUT_METHOD) {
1595 pf.left = df.left = cf.left = vf.left = mDockLeft;
1596 pf.top = df.top = cf.top = vf.top = mDockTop;
1597 pf.right = df.right = cf.right = vf.right = mDockRight;
1598 pf.bottom = df.bottom = cf.bottom = vf.bottom = mDockBottom;
1599 // IM dock windows always go to the bottom of the screen.
1600 attrs.gravity = Gravity.BOTTOM;
1601 mDockLayer = win.getSurfaceLayer();
1602 } else {
Dianne Hackborndea3ef72010-10-28 14:24:22 -07001603 final int adjust = sim & SOFT_INPUT_MASK_ADJUST;
1604
Dianne Hackborn82de1ae2010-10-28 11:28:39 -07001605 if ((fl & (FLAG_LAYOUT_IN_SCREEN | FLAG_FULLSCREEN | FLAG_LAYOUT_INSET_DECOR))
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001606 == (FLAG_LAYOUT_IN_SCREEN | FLAG_LAYOUT_INSET_DECOR)) {
1607 // This is the case for a normal activity window: we want it
1608 // to cover all of the screen space, and it can take care of
1609 // moving its contents to account for screen decorations that
1610 // intrude into that space.
1611 if (attached != null) {
1612 // If this window is attached to another, our display
1613 // frame is the same as the one we are attached to.
1614 setAttachedWindowFrames(win, fl, sim, attached, true, pf, df, cf, vf);
1615 } else {
Joe Onorato29fc2c92010-11-24 10:26:50 -08001616 if (attrs.type == TYPE_STATUS_BAR_PANEL) {
1617 // Status bar panels are the only windows who can go on top of
1618 // the status bar. They are protected by the STATUS_BAR_SERVICE
1619 // permission, so they have the same privileges as the status
1620 // bar itself.
1621 pf.left = df.left = mUnrestrictedScreenLeft;
1622 pf.top = df.top = mUnrestrictedScreenTop;
1623 pf.right = df.right = mUnrestrictedScreenLeft+mUnrestrictedScreenWidth;
1624 pf.bottom = df.bottom = mUnrestrictedScreenTop+mUnrestrictedScreenHeight;
1625 } else {
1626 pf.left = df.left = mRestrictedScreenLeft;
1627 pf.top = df.top = mRestrictedScreenTop;
1628 pf.right = df.right = mRestrictedScreenLeft+mRestrictedScreenWidth;
1629 pf.bottom = df.bottom = mRestrictedScreenTop+mRestrictedScreenHeight;
1630 }
Dianne Hackborndea3ef72010-10-28 14:24:22 -07001631 if (adjust != SOFT_INPUT_ADJUST_RESIZE) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001632 cf.left = mDockLeft;
1633 cf.top = mDockTop;
1634 cf.right = mDockRight;
1635 cf.bottom = mDockBottom;
1636 } else {
1637 cf.left = mContentLeft;
1638 cf.top = mContentTop;
1639 cf.right = mContentRight;
1640 cf.bottom = mContentBottom;
1641 }
Dianne Hackborndea3ef72010-10-28 14:24:22 -07001642 if (adjust != SOFT_INPUT_ADJUST_NOTHING) {
1643 vf.left = mCurLeft;
1644 vf.top = mCurTop;
1645 vf.right = mCurRight;
1646 vf.bottom = mCurBottom;
1647 } else {
1648 vf.set(cf);
1649 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001650 }
Dianne Hackborn82de1ae2010-10-28 11:28:39 -07001651 } else if ((fl & FLAG_LAYOUT_IN_SCREEN) != 0) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001652 // A window that has requested to fill the entire screen just
1653 // gets everything, period.
Joe Onorato29fc2c92010-11-24 10:26:50 -08001654 if (attrs.type == TYPE_STATUS_BAR_PANEL) {
1655 pf.left = df.left = cf.left = mUnrestrictedScreenLeft;
1656 pf.top = df.top = cf.top = mUnrestrictedScreenTop;
1657 pf.right = df.right = cf.right
1658 = mUnrestrictedScreenLeft+mUnrestrictedScreenWidth;
1659 pf.bottom = df.bottom = cf.bottom
1660 = mUnrestrictedScreenTop+mUnrestrictedScreenHeight;
1661 } else {
1662 pf.left = df.left = cf.left = mRestrictedScreenLeft;
1663 pf.top = df.top = cf.top = mRestrictedScreenTop;
1664 pf.right = df.right = cf.right = mRestrictedScreenLeft+mRestrictedScreenWidth;
1665 pf.bottom = df.bottom = cf.bottom
1666 = mRestrictedScreenTop+mRestrictedScreenHeight;
1667 }
Dianne Hackborndea3ef72010-10-28 14:24:22 -07001668 if (adjust != SOFT_INPUT_ADJUST_NOTHING) {
1669 vf.left = mCurLeft;
1670 vf.top = mCurTop;
1671 vf.right = mCurRight;
1672 vf.bottom = mCurBottom;
1673 } else {
1674 vf.set(cf);
1675 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001676 } else if (attached != null) {
1677 // A child window should be placed inside of the same visible
1678 // frame that its parent had.
Dianne Hackborndea3ef72010-10-28 14:24:22 -07001679 setAttachedWindowFrames(win, fl, adjust, attached, false, pf, df, cf, vf);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001680 } else {
1681 // Otherwise, a normal window must be placed inside the content
1682 // of all screen decorations.
1683 pf.left = mContentLeft;
1684 pf.top = mContentTop;
1685 pf.right = mContentRight;
1686 pf.bottom = mContentBottom;
Dianne Hackborndea3ef72010-10-28 14:24:22 -07001687 if (adjust != SOFT_INPUT_ADJUST_RESIZE) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001688 df.left = cf.left = mDockLeft;
1689 df.top = cf.top = mDockTop;
1690 df.right = cf.right = mDockRight;
1691 df.bottom = cf.bottom = mDockBottom;
1692 } else {
1693 df.left = cf.left = mContentLeft;
1694 df.top = cf.top = mContentTop;
1695 df.right = cf.right = mContentRight;
1696 df.bottom = cf.bottom = mContentBottom;
1697 }
Dianne Hackborndea3ef72010-10-28 14:24:22 -07001698 if (adjust != SOFT_INPUT_ADJUST_NOTHING) {
1699 vf.left = mCurLeft;
1700 vf.top = mCurTop;
1701 vf.right = mCurRight;
1702 vf.bottom = mCurBottom;
1703 } else {
1704 vf.set(cf);
1705 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001706 }
1707 }
1708
1709 if ((fl & FLAG_LAYOUT_NO_LIMITS) != 0) {
1710 df.left = df.top = cf.left = cf.top = vf.left = vf.top = -10000;
1711 df.right = df.bottom = cf.right = cf.bottom = vf.right = vf.bottom = 10000;
1712 }
1713
The Android Open Source Project11267662009-03-18 17:39:47 -07001714 if (DEBUG_LAYOUT) Log.v(TAG, "Compute frame " + attrs.getTitle()
1715 + ": sim=#" + Integer.toHexString(sim)
1716 + " pf=" + pf.toShortString() + " df=" + df.toShortString()
1717 + " cf=" + cf.toShortString() + " vf=" + vf.toShortString());
1718
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001719 win.computeFrameLw(pf, df, cf, vf);
1720
1721 // Dock windows carve out the bottom of the screen, so normal windows
1722 // can't appear underneath them.
1723 if (attrs.type == TYPE_INPUT_METHOD && !win.getGivenInsetsPendingLw()) {
1724 int top = win.getContentFrameLw().top;
1725 top += win.getGivenContentInsetsLw().top;
1726 if (mContentBottom > top) {
1727 mContentBottom = top;
1728 }
1729 top = win.getVisibleFrameLw().top;
1730 top += win.getGivenVisibleInsetsLw().top;
1731 if (mCurBottom > top) {
1732 mCurBottom = top;
1733 }
The Android Open Source Project11267662009-03-18 17:39:47 -07001734 if (DEBUG_LAYOUT) Log.v(TAG, "Input method: mDockBottom="
1735 + mDockBottom + " mContentBottom="
1736 + mContentBottom + " mCurBottom=" + mCurBottom);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001737 }
1738 }
1739
1740 /** {@inheritDoc} */
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001741 public int finishLayoutLw() {
Dianne Hackborn5f3063e2010-03-10 16:01:58 -08001742 return 0;
1743 }
1744
1745 /** {@inheritDoc} */
1746 public void beginAnimationLw(int displayWidth, int displayHeight) {
1747 mTopFullscreenOpaqueWindowState = null;
1748 mForceStatusBar = false;
1749
1750 mHideLockScreen = false;
1751 mAllowLockscreenWhenOn = false;
1752 mDismissKeyguard = false;
1753 }
1754
1755 /** {@inheritDoc} */
1756 public void animatingWindowLw(WindowState win,
1757 WindowManager.LayoutParams attrs) {
1758 if (mTopFullscreenOpaqueWindowState == null &&
1759 win.isVisibleOrBehindKeyguardLw()) {
1760 if ((attrs.flags & FLAG_FORCE_NOT_FULLSCREEN) != 0) {
1761 mForceStatusBar = true;
1762 }
1763 if (attrs.type >= FIRST_APPLICATION_WINDOW
1764 && attrs.type <= LAST_APPLICATION_WINDOW
Dianne Hackborn6493e642010-10-01 13:06:30 -07001765 && attrs.x == 0 && attrs.y == 0
1766 && attrs.width == WindowManager.LayoutParams.MATCH_PARENT
1767 && attrs.height == WindowManager.LayoutParams.MATCH_PARENT) {
Dianne Hackborn5f3063e2010-03-10 16:01:58 -08001768 if (DEBUG_LAYOUT) Log.v(TAG, "Fullscreen window: " + win);
1769 mTopFullscreenOpaqueWindowState = win;
1770 if ((attrs.flags & FLAG_SHOW_WHEN_LOCKED) != 0) {
1771 if (localLOGV) Log.v(TAG, "Setting mHideLockScreen to true by win " + win);
1772 mHideLockScreen = true;
1773 }
1774 if ((attrs.flags & FLAG_DISMISS_KEYGUARD) != 0) {
1775 if (localLOGV) Log.v(TAG, "Setting mDismissKeyguard to true by win " + win);
1776 mDismissKeyguard = true;
1777 }
1778 if ((attrs.flags & FLAG_ALLOW_LOCK_WHILE_SCREEN_ON) != 0) {
1779 mAllowLockscreenWhenOn = true;
1780 }
1781 }
1782 }
1783 }
1784
1785 /** {@inheritDoc} */
1786 public int finishAnimationLw() {
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001787 int changes = 0;
Joe Onorato93056472010-09-10 10:30:46 -04001788 boolean topIsFullscreen = false;
Daniel Sandlere02d8082010-10-08 15:13:22 -04001789
1790 final WindowManager.LayoutParams lp = (mTopFullscreenOpaqueWindowState != null)
1791 ? mTopFullscreenOpaqueWindowState.getAttrs()
1792 : null;
1793
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001794 if (mStatusBar != null) {
Dianne Hackbornd70bc2f2009-10-06 23:25:51 -07001795 if (localLOGV) Log.i(TAG, "force=" + mForceStatusBar
1796 + " top=" + mTopFullscreenOpaqueWindowState);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001797 if (mForceStatusBar) {
Dianne Hackborn0ecadf72009-03-31 18:00:37 -07001798 if (DEBUG_LAYOUT) Log.v(TAG, "Showing status bar");
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001799 if (mStatusBar.showLw(true)) changes |= FINISH_LAYOUT_REDO_LAYOUT;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001800 } else if (mTopFullscreenOpaqueWindowState != null) {
Joe Onorato93056472010-09-10 10:30:46 -04001801 if (localLOGV) {
1802 Log.d(TAG, "frame: " + mTopFullscreenOpaqueWindowState.getFrameLw()
1803 + " shown frame: " + mTopFullscreenOpaqueWindowState.getShownFrameLw());
1804 Log.d(TAG, "attr: " + mTopFullscreenOpaqueWindowState.getAttrs()
1805 + " lp.flags=0x" + Integer.toHexString(lp.flags));
1806 }
1807 topIsFullscreen = (lp.flags & WindowManager.LayoutParams.FLAG_FULLSCREEN) != 0;
1808 // The subtle difference between the window for mTopFullscreenOpaqueWindowState
1809 // and mTopIsFullscreen is that that mTopIsFullscreen is set only if the window
1810 // has the FLAG_FULLSCREEN set. Not sure if there is another way that to be the
1811 // case though.
1812 if (topIsFullscreen) {
Daniel Sandler40427442010-07-16 11:44:52 -04001813 if (mStatusBarCanHide) {
1814 if (DEBUG_LAYOUT) Log.v(TAG, "Hiding status bar");
1815 if (mStatusBar.hideLw(true)) changes |= FINISH_LAYOUT_REDO_LAYOUT;
Daniel Sandler40427442010-07-16 11:44:52 -04001816 } else if (localLOGV) {
1817 Log.v(TAG, "Preventing status bar from hiding by policy");
1818 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001819 } else {
Dianne Hackborn0ecadf72009-03-31 18:00:37 -07001820 if (DEBUG_LAYOUT) Log.v(TAG, "Showing status bar");
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001821 if (mStatusBar.showLw(true)) changes |= FINISH_LAYOUT_REDO_LAYOUT;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001822 }
1823 }
1824 }
Daniel Sandlere02d8082010-10-08 15:13:22 -04001825
1826 boolean topNeedsMenu = mShowMenuKey;
1827 if (lp != null) {
1828 topNeedsMenu = (lp.flags & WindowManager.LayoutParams.FLAG_NEEDS_MENU_KEY) != 0;
1829 }
1830
1831 if (DEBUG_LAYOUT) Log.v(TAG, "Top window "
1832 + (topNeedsMenu ? "needs" : "does not need")
1833 + " the MENU key");
1834
1835 final boolean changedFullscreen = (mTopIsFullscreen != topIsFullscreen);
1836 final boolean changedMenu = (topNeedsMenu != mShowMenuKey);
1837
1838 if (changedFullscreen || changedMenu) {
Joe Onorato93056472010-09-10 10:30:46 -04001839 final boolean topIsFullscreenF = topIsFullscreen;
Daniel Sandlere02d8082010-10-08 15:13:22 -04001840 final boolean topNeedsMenuF = topNeedsMenu;
1841
Joe Onorato93056472010-09-10 10:30:46 -04001842 mTopIsFullscreen = topIsFullscreen;
Daniel Sandlere02d8082010-10-08 15:13:22 -04001843 mShowMenuKey = topNeedsMenu;
1844
Joe Onorato93056472010-09-10 10:30:46 -04001845 mHandler.post(new Runnable() {
1846 public void run() {
1847 if (mStatusBarService == null) {
1848 // This is the one that can not go away, but it doesn't come up
1849 // before the window manager does, so don't fail if it doesn't
1850 // exist. This works as long as no fullscreen windows come up
1851 // before the status bar service does.
1852 mStatusBarService = IStatusBarService.Stub.asInterface(
1853 ServiceManager.getService("statusbar"));
1854 }
1855 final IStatusBarService sbs = mStatusBarService;
1856 if (mStatusBarService != null) {
1857 try {
Daniel Sandlere02d8082010-10-08 15:13:22 -04001858 if (changedMenu) {
1859 sbs.setMenuKeyVisible(topNeedsMenuF);
1860 }
Daniel Sandler59485d742010-12-02 00:11:33 -05001861 if (changedFullscreen) {
1862 sbs.setActiveWindowIsFullscreen(topIsFullscreenF);
1863 }
Joe Onorato93056472010-09-10 10:30:46 -04001864 } catch (RemoteException e) {
1865 // This should be impossible because we're in the same process.
1866 mStatusBarService = null;
1867 }
1868 }
1869 }
1870 });
Dianne Hackborn5f3063e2010-03-10 16:01:58 -08001871 }
1872
Suchi Amalapurapu9cdc9032009-05-14 18:01:07 -07001873 // Hide the key guard if a visible window explicitly specifies that it wants to be displayed
1874 // when the screen is locked
1875 if (mKeyguard != null) {
Joe Onorato93056472010-09-10 10:30:46 -04001876 if (localLOGV) Log.v(TAG, "finishAnimationLw::mHideKeyguard="+mHideLockScreen);
Dianne Hackborn05726582009-09-22 17:28:24 -07001877 if (mDismissKeyguard && !mKeyguardMediator.isSecure()) {
Dianne Hackborn08743722009-12-21 12:16:51 -08001878 if (mKeyguard.hideLw(true)) {
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001879 changes |= FINISH_LAYOUT_REDO_LAYOUT
1880 | FINISH_LAYOUT_REDO_CONFIG
1881 | FINISH_LAYOUT_REDO_WALLPAPER;
1882 }
Dianne Hackborn05726582009-09-22 17:28:24 -07001883 if (mKeyguardMediator.isShowing()) {
Dianne Hackbornb446e972009-09-20 15:23:25 -07001884 mHandler.post(new Runnable() {
1885 public void run() {
Dianne Hackborn05726582009-09-22 17:28:24 -07001886 mKeyguardMediator.keyguardDone(false, false);
Dianne Hackbornb446e972009-09-20 15:23:25 -07001887 }
1888 });
1889 }
Dianne Hackborn05726582009-09-22 17:28:24 -07001890 } else if (mHideLockScreen) {
Dianne Hackborn08743722009-12-21 12:16:51 -08001891 if (mKeyguard.hideLw(true)) {
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001892 changes |= FINISH_LAYOUT_REDO_LAYOUT
1893 | FINISH_LAYOUT_REDO_CONFIG
1894 | FINISH_LAYOUT_REDO_WALLPAPER;
1895 }
Mike Lockwoodf3bfed52010-01-21 16:38:44 -05001896 mKeyguardMediator.setHidden(true);
Suchi Amalapurapu9cdc9032009-05-14 18:01:07 -07001897 } else {
Dianne Hackborn08743722009-12-21 12:16:51 -08001898 if (mKeyguard.showLw(true)) {
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001899 changes |= FINISH_LAYOUT_REDO_LAYOUT
1900 | FINISH_LAYOUT_REDO_CONFIG
1901 | FINISH_LAYOUT_REDO_WALLPAPER;
1902 }
Mike Lockwoodf3bfed52010-01-21 16:38:44 -05001903 mKeyguardMediator.setHidden(false);
Suchi Amalapurapu9cdc9032009-05-14 18:01:07 -07001904 }
1905 }
The Android Open Source Projectc84bf282009-03-09 11:52:14 -07001906
Mike Lockwood28569302010-01-28 11:54:40 -05001907 // update since mAllowLockscreenWhenOn might have changed
1908 updateLockScreenTimeout();
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001909 return changes;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001910 }
1911
Dianne Hackborn08743722009-12-21 12:16:51 -08001912 public boolean allowAppAnimationsLw() {
1913 if (mKeyguard != null && mKeyguard.isVisibleLw()) {
1914 // If keyguard is currently visible, no reason to animate
1915 // behind it.
1916 return false;
1917 }
1918 if (mStatusBar != null && mStatusBar.isVisibleLw()) {
1919 Rect rect = new Rect(mStatusBar.getShownFrameLw());
1920 for (int i=mStatusBarPanels.size()-1; i>=0; i--) {
1921 WindowState w = mStatusBarPanels.get(i);
1922 if (w.isVisibleLw()) {
1923 rect.union(w.getShownFrameLw());
1924 }
1925 }
Joe Onorato29fc2c92010-11-24 10:26:50 -08001926 final int insetw = mRestrictedScreenWidth/10;
1927 final int inseth = mRestrictedScreenHeight/10;
1928 if (rect.contains(insetw, inseth, mRestrictedScreenWidth-insetw,
1929 mRestrictedScreenHeight-inseth)) {
Dianne Hackborn08743722009-12-21 12:16:51 -08001930 // All of the status bar windows put together cover the
1931 // screen, so the app can't be seen. (Note this test doesn't
1932 // work if the rects of these windows are at off offsets or
1933 // sizes, causing gaps in the rect union we have computed.)
1934 return false;
1935 }
1936 }
1937 return true;
1938 }
1939
Dianne Hackborn0ecadf72009-03-31 18:00:37 -07001940 /** {@inheritDoc} */
Jeff Brown46b9ac02010-04-22 18:58:52 -07001941 public void notifyLidSwitchChanged(long whenNanos, boolean lidOpen) {
1942 // lid changed state
1943 mLidOpen = lidOpen;
1944 boolean awakeNow = mKeyguardMediator.doLidChangeTq(mLidOpen);
1945 updateRotation(Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
1946 if (awakeNow) {
1947 // If the lid opening and we don't have to keep the
1948 // keyguard up, then we can turn on the screen
1949 // immediately.
1950 mKeyguardMediator.pokeWakelock();
1951 } else if (keyguardIsShowingTq()) {
1952 if (mLidOpen) {
1953 // If we are opening the lid and not hiding the
1954 // keyguard, then we need to have it turn on the
1955 // screen once it is shown.
1956 mKeyguardMediator.onWakeKeyWhenKeyguardShowingTq(
1957 KeyEvent.KEYCODE_POWER);
1958 }
1959 } else {
1960 // Light up the keyboard if we are sliding up.
1961 if (mLidOpen) {
1962 mPowerManager.userActivity(SystemClock.uptimeMillis(), false,
1963 LocalPowerManager.BUTTON_EVENT);
1964 } else {
1965 mPowerManager.userActivity(SystemClock.uptimeMillis(), false,
1966 LocalPowerManager.OTHER_EVENT);
1967 }
1968 }
1969 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001970
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001971 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001972 * @return Whether music is being played right now.
1973 */
1974 boolean isMusicActive() {
Eric Laurent413cb9d2009-07-17 11:52:43 -07001975 final AudioManager am = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
1976 if (am == null) {
1977 Log.w(TAG, "isMusicActive: couldn't get AudioManager reference");
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001978 return false;
1979 }
Eric Laurent413cb9d2009-07-17 11:52:43 -07001980 return am.isMusicActive();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001981 }
1982
1983 /**
1984 * Tell the audio service to adjust the volume appropriate to the event.
1985 * @param keycode
1986 */
Mike Lockwooda53e3812009-09-25 10:51:39 -04001987 void handleVolumeKey(int stream, int keycode) {
Jeff Brown4d396052010-10-29 21:50:21 -07001988 IAudioService audioService = getAudioService();
1989 if (audioService == null) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001990 return;
1991 }
1992 try {
1993 // since audio is playing, we shouldn't have to hold a wake lock
1994 // during the call, but we do it as a precaution for the rare possibility
1995 // that the music stops right before we call this
Jeff Brownb0418da2010-11-01 15:24:01 -07001996 // TODO: Actually handle MUTE.
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001997 mBroadcastWakeLock.acquire();
Jeff Brown4d396052010-10-29 21:50:21 -07001998 audioService.adjustStreamVolume(stream,
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001999 keycode == KeyEvent.KEYCODE_VOLUME_UP
2000 ? AudioManager.ADJUST_RAISE
2001 : AudioManager.ADJUST_LOWER,
2002 0);
2003 } catch (RemoteException e) {
2004 Log.w(TAG, "IAudioService.adjustStreamVolume() threw RemoteException " + e);
2005 } finally {
2006 mBroadcastWakeLock.release();
2007 }
2008 }
Jeff Brown4d396052010-10-29 21:50:21 -07002009
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002010 /** {@inheritDoc} */
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002011 @Override
Jeff Brown1f245102010-11-18 20:53:46 -08002012 public int interceptKeyBeforeQueueing(KeyEvent event, int policyFlags, boolean isScreenOn) {
2013 final boolean down = event.getAction() == KeyEvent.ACTION_DOWN;
2014 final boolean canceled = event.isCanceled();
2015 final int keyCode = event.getKeyCode();
Jeff Browne20c9e02010-10-11 14:20:19 -07002016
Jeff Brown3122e442010-10-11 23:32:49 -07002017 final boolean isInjected = (policyFlags & WindowManagerPolicy.FLAG_INJECTED) != 0;
Jeff Brown3122e442010-10-11 23:32:49 -07002018
Mike Lockwoodc72b15f2009-11-28 20:58:22 -05002019 // If screen is off then we treat the case where the keyguard is open but hidden
2020 // the same as if it were open and in front.
2021 // This will prevent any keys other than the power button from waking the screen
2022 // when the keyguard is hidden by another activity.
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002023 final boolean keyguardActive = (isScreenOn ?
Mike Lockwoodc72b15f2009-11-28 20:58:22 -05002024 mKeyguardMediator.isShowingAndNotHidden() :
2025 mKeyguardMediator.isShowing());
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002026
2027 if (false) {
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002028 Log.d(TAG, "interceptKeyTq keycode=" + keyCode
2029 + " screenIsOn=" + isScreenOn + " keyguardActive=" + keyguardActive);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002030 }
2031
Jeff Brown4d396052010-10-29 21:50:21 -07002032 if (down && (policyFlags & WindowManagerPolicy.FLAG_VIRTUAL) != 0) {
2033 performHapticFeedbackLw(null, HapticFeedbackConstants.VIRTUAL_KEY, false);
2034 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002035
Jeff Brown4d396052010-10-29 21:50:21 -07002036 // Basic policy based on screen state and keyguard.
2037 // FIXME: This policy isn't quite correct. We shouldn't care whether the screen
2038 // is on or off, really. We should care about whether the device is in an
2039 // interactive state or is in suspend pretending to be "off".
2040 // The primary screen might be turned off due to proximity sensor or
2041 // because we are presenting media on an auxiliary screen or remotely controlling
2042 // the device some other way (which is why we have an exemption here for injected
2043 // events).
2044 int result;
2045 if (isScreenOn || isInjected) {
2046 // When the screen is on or if the key is injected pass the key to the application.
2047 result = ACTION_PASS_TO_USER;
2048 } else {
2049 // When the screen is off and the key is not injected, determine whether
2050 // to wake the device but don't pass the key to the application.
2051 result = 0;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002052
Jeff Brown4d396052010-10-29 21:50:21 -07002053 final boolean isWakeKey = (policyFlags
2054 & (WindowManagerPolicy.FLAG_WAKE | WindowManagerPolicy.FLAG_WAKE_DROPPED)) != 0;
2055 if (down && isWakeKey) {
2056 if (keyguardActive) {
2057 // If the keyguard is showing, let it decide what to do with the wake key.
2058 mKeyguardMediator.onWakeKeyWhenKeyguardShowingTq(keyCode);
2059 } else {
2060 // Otherwise, wake the device ourselves.
2061 result |= ACTION_POKE_USER_ACTIVITY;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002062 }
2063 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002064 }
2065
Jeff Brown4d396052010-10-29 21:50:21 -07002066 // Handle special keys.
2067 switch (keyCode) {
2068 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jeff Brownb0418da2010-11-01 15:24:01 -07002069 case KeyEvent.KEYCODE_VOLUME_UP:
2070 case KeyEvent.KEYCODE_VOLUME_MUTE: {
Jeff Brown4d396052010-10-29 21:50:21 -07002071 if (down) {
2072 ITelephony telephonyService = getTelephonyService();
2073 if (telephonyService != null) {
2074 try {
2075 if (telephonyService.isRinging()) {
2076 // If an incoming call is ringing, either VOLUME key means
2077 // "silence ringer". We handle these keys here, rather than
2078 // in the InCallScreen, to make sure we'll respond to them
2079 // even if the InCallScreen hasn't come to the foreground yet.
2080 // Look for the DOWN event here, to agree with the "fallback"
2081 // behavior in the InCallScreen.
2082 Log.i(TAG, "interceptKeyBeforeQueueing:"
2083 + " VOLUME key-down while ringing: Silence ringer!");
2084
2085 // Silence the ringer. (It's safe to call this
2086 // even if the ringer has already been silenced.)
2087 telephonyService.silenceRinger();
2088
2089 // And *don't* pass this key thru to the current activity
2090 // (which is probably the InCallScreen.)
2091 result &= ~ACTION_PASS_TO_USER;
2092 break;
2093 }
2094 if (telephonyService.isOffhook()
2095 && (result & ACTION_PASS_TO_USER) == 0) {
2096 // If we are in call but we decided not to pass the key to
2097 // the application, handle the volume change here.
2098 handleVolumeKey(AudioManager.STREAM_VOICE_CALL, keyCode);
2099 break;
2100 }
2101 } catch (RemoteException ex) {
2102 Log.w(TAG, "ITelephony threw RemoteException", ex);
2103 }
2104 }
2105
2106 if (isMusicActive() && (result & ACTION_PASS_TO_USER) == 0) {
2107 // If music is playing but we decided not to pass the key to the
2108 // application, handle the volume change here.
2109 handleVolumeKey(AudioManager.STREAM_MUSIC, keyCode);
2110 break;
2111 }
2112 }
2113 break;
2114 }
2115
2116 case KeyEvent.KEYCODE_ENDCALL: {
2117 result &= ~ACTION_PASS_TO_USER;
2118 if (down) {
2119 ITelephony telephonyService = getTelephonyService();
2120 boolean hungUp = false;
2121 if (telephonyService != null) {
2122 try {
2123 hungUp = telephonyService.endCall();
2124 } catch (RemoteException ex) {
2125 Log.w(TAG, "ITelephony threw RemoteException", ex);
2126 }
2127 }
2128 interceptPowerKeyDown(!isScreenOn || hungUp);
2129 } else {
2130 if (interceptPowerKeyUp(canceled)) {
2131 if ((mEndcallBehavior
2132 & Settings.System.END_BUTTON_BEHAVIOR_HOME) != 0) {
2133 if (goHome()) {
2134 break;
Dianne Hackborn0041e972009-07-24 17:14:43 -07002135 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002136 }
Jeff Brown4d396052010-10-29 21:50:21 -07002137 if ((mEndcallBehavior
2138 & Settings.System.END_BUTTON_BEHAVIOR_SLEEP) != 0) {
2139 result = (result & ~ACTION_POKE_USER_ACTIVITY) | ACTION_GO_TO_SLEEP;
2140 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002141 }
Jeff Brown4d396052010-10-29 21:50:21 -07002142 }
2143 break;
2144 }
2145
2146 case KeyEvent.KEYCODE_POWER: {
2147 result &= ~ACTION_PASS_TO_USER;
2148 if (down) {
2149 ITelephony telephonyService = getTelephonyService();
2150 boolean hungUp = false;
2151 if (telephonyService != null) {
2152 try {
2153 if (telephonyService.isRinging()) {
2154 // Pressing Power while there's a ringing incoming
2155 // call should silence the ringer.
2156 telephonyService.silenceRinger();
2157 } else if ((mIncallPowerBehavior
2158 & Settings.Secure.INCALL_POWER_BUTTON_BEHAVIOR_HANGUP) != 0
2159 && telephonyService.isOffhook()) {
2160 // Otherwise, if "Power button ends call" is enabled,
2161 // the Power button will hang up any current active call.
2162 hungUp = telephonyService.endCall();
2163 }
2164 } catch (RemoteException ex) {
2165 Log.w(TAG, "ITelephony threw RemoteException", ex);
2166 }
2167 }
2168 interceptPowerKeyDown(!isScreenOn || hungUp);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002169 } else {
Jeff Brown4d396052010-10-29 21:50:21 -07002170 if (interceptPowerKeyUp(canceled)) {
2171 result = (result & ~ACTION_POKE_USER_ACTIVITY) | ACTION_GO_TO_SLEEP;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002172 }
Jeff Brown4d396052010-10-29 21:50:21 -07002173 }
2174 break;
2175 }
2176
2177 case KeyEvent.KEYCODE_MEDIA_PLAY:
2178 case KeyEvent.KEYCODE_MEDIA_PAUSE:
2179 case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
2180 if (down) {
2181 ITelephony telephonyService = getTelephonyService();
2182 if (telephonyService != null) {
2183 try {
2184 if (!telephonyService.isIdle()) {
2185 // Suppress PLAY/PAUSE toggle when phone is ringing or in-call
2186 // to avoid music playback.
2187 break;
2188 }
2189 } catch (RemoteException ex) {
2190 Log.w(TAG, "ITelephony threw RemoteException", ex);
Jeff Brown3122e442010-10-11 23:32:49 -07002191 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002192 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002193 }
Jeff Brown4d396052010-10-29 21:50:21 -07002194 case KeyEvent.KEYCODE_HEADSETHOOK:
2195 case KeyEvent.KEYCODE_MUTE:
2196 case KeyEvent.KEYCODE_MEDIA_STOP:
2197 case KeyEvent.KEYCODE_MEDIA_NEXT:
2198 case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
2199 case KeyEvent.KEYCODE_MEDIA_REWIND:
2200 case KeyEvent.KEYCODE_MEDIA_RECORD:
2201 case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD: {
2202 if ((result & ACTION_PASS_TO_USER) == 0) {
2203 // Only do this if we would otherwise not pass it to the user. In that
2204 // case, the PhoneWindow class will do the same thing, except it will
2205 // only do it if the showing app doesn't process the key on its own.
Jeff Brown4d396052010-10-29 21:50:21 -07002206 mBroadcastWakeLock.acquire();
Jeff Brown1f245102010-11-18 20:53:46 -08002207 mHandler.post(new PassHeadsetKey(new KeyEvent(event)));
Jeff Brown4d396052010-10-29 21:50:21 -07002208 }
2209 break;
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002210 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002211
Jeff Brown4d396052010-10-29 21:50:21 -07002212 case KeyEvent.KEYCODE_CALL: {
2213 if (down) {
2214 ITelephony telephonyService = getTelephonyService();
2215 if (telephonyService != null) {
2216 try {
2217 if (telephonyService.isRinging()) {
2218 Log.i(TAG, "interceptKeyBeforeQueueing:"
2219 + " CALL key-down while ringing: Answer the call!");
2220 telephonyService.answerRingingCall();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002221
Jeff Brown4d396052010-10-29 21:50:21 -07002222 // And *don't* pass this key thru to the current activity
2223 // (which is presumably the InCallScreen.)
2224 result &= ~ACTION_PASS_TO_USER;
2225 }
2226 } catch (RemoteException ex) {
2227 Log.w(TAG, "ITelephony threw RemoteException", ex);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002228 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002229 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002230 }
Jeff Brown4d396052010-10-29 21:50:21 -07002231 break;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002232 }
2233 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002234 return result;
2235 }
2236
2237 class PassHeadsetKey implements Runnable {
2238 KeyEvent mKeyEvent;
2239
2240 PassHeadsetKey(KeyEvent keyEvent) {
2241 mKeyEvent = keyEvent;
2242 }
2243
2244 public void run() {
2245 if (ActivityManagerNative.isSystemReady()) {
2246 Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
2247 intent.putExtra(Intent.EXTRA_KEY_EVENT, mKeyEvent);
2248 mContext.sendOrderedBroadcast(intent, null, mBroadcastDone,
2249 mHandler, Activity.RESULT_OK, null, null);
2250 }
2251 }
2252 }
2253
2254 BroadcastReceiver mBroadcastDone = new BroadcastReceiver() {
2255 public void onReceive(Context context, Intent intent) {
2256 mBroadcastWakeLock.release();
2257 }
2258 };
Mike Lockwood1753f7f2009-08-24 14:49:07 -07002259
2260 BroadcastReceiver mDockReceiver = new BroadcastReceiver() {
2261 public void onReceive(Context context, Intent intent) {
Dianne Hackborn867ab6472010-04-29 13:28:56 -07002262 if (Intent.ACTION_DOCK_EVENT.equals(intent.getAction())) {
2263 mDockMode = intent.getIntExtra(Intent.EXTRA_DOCK_STATE,
2264 Intent.EXTRA_DOCK_STATE_UNDOCKED);
2265 } else {
2266 try {
2267 IUiModeManager uiModeService = IUiModeManager.Stub.asInterface(
2268 ServiceManager.getService(Context.UI_MODE_SERVICE));
2269 mUiMode = uiModeService.getCurrentModeType();
2270 } catch (RemoteException e) {
2271 }
Dianne Hackborn80fa1662009-10-07 14:02:10 -07002272 }
Mike Lockwood1753f7f2009-08-24 14:49:07 -07002273 updateRotation(Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
Mike Lockwoode9867d22009-09-20 01:59:02 -04002274 updateOrientationListenerLp();
Mike Lockwood1753f7f2009-08-24 14:49:07 -07002275 }
2276 };
2277
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002278 /** {@inheritDoc} */
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002279 public void screenTurnedOff(int why) {
Dianne Hackborn74489012009-03-24 20:50:09 -07002280 EventLog.writeEvent(70000, 0);
2281 mKeyguardMediator.onScreenTurnedOff(why);
The Android Open Source Project0727d222009-03-11 12:11:58 -07002282 synchronized (mLock) {
The Android Open Source Project0727d222009-03-11 12:11:58 -07002283 mScreenOn = false;
2284 updateOrientationListenerLp();
Mike Lockwood28569302010-01-28 11:54:40 -05002285 updateLockScreenTimeout();
The Android Open Source Project0727d222009-03-11 12:11:58 -07002286 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002287 }
2288
2289 /** {@inheritDoc} */
2290 public void screenTurnedOn() {
Dianne Hackborn74489012009-03-24 20:50:09 -07002291 EventLog.writeEvent(70000, 1);
2292 mKeyguardMediator.onScreenTurnedOn();
The Android Open Source Project0727d222009-03-11 12:11:58 -07002293 synchronized (mLock) {
The Android Open Source Project0727d222009-03-11 12:11:58 -07002294 mScreenOn = true;
2295 updateOrientationListenerLp();
Mike Lockwood28569302010-01-28 11:54:40 -05002296 updateLockScreenTimeout();
The Android Open Source Project0727d222009-03-11 12:11:58 -07002297 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002298 }
2299
2300 /** {@inheritDoc} */
Dianne Hackborn08743722009-12-21 12:16:51 -08002301 public boolean isScreenOn() {
2302 return mScreenOn;
2303 }
2304
2305 /** {@inheritDoc} */
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002306 public void enableKeyguard(boolean enabled) {
2307 mKeyguardMediator.setKeyguardEnabled(enabled);
2308 }
2309
2310 /** {@inheritDoc} */
2311 public void exitKeyguardSecurely(OnKeyguardExitResult callback) {
2312 mKeyguardMediator.verifyUnlock(callback);
2313 }
2314
Mike Lockwoodf7913302009-11-28 22:27:10 -05002315 private boolean keyguardIsShowingTq() {
Dianne Hackborn9dc06cc2009-11-17 18:19:23 -08002316 return mKeyguardMediator.isShowingAndNotHidden();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002317 }
2318
2319 /** {@inheritDoc} */
2320 public boolean inKeyguardRestrictedKeyInputMode() {
2321 return mKeyguardMediator.isInputRestricted();
2322 }
2323
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002324 void sendCloseSystemWindows() {
2325 sendCloseSystemWindows(mContext, null);
2326 }
2327
2328 void sendCloseSystemWindows(String reason) {
2329 sendCloseSystemWindows(mContext, reason);
2330 }
2331
2332 static void sendCloseSystemWindows(Context context, String reason) {
2333 if (ActivityManagerNative.isSystemReady()) {
Dianne Hackborn94a679d2009-07-28 17:51:07 -07002334 try {
2335 ActivityManagerNative.getDefault().closeSystemDialogs(reason);
2336 } catch (RemoteException e) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002337 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002338 }
2339 }
2340
The Android Open Source Project0727d222009-03-11 12:11:58 -07002341 public int rotationForOrientationLw(int orientation, int lastRotation,
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002342 boolean displayEnabled) {
Mitsuru Oshima831d0d92009-06-16 18:27:18 -07002343
2344 if (mPortraitRotation < 0) {
2345 // Initialize the rotation angles for each orientation once.
2346 Display d = ((WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE))
2347 .getDefaultDisplay();
2348 if (d.getWidth() > d.getHeight()) {
2349 mPortraitRotation = Surface.ROTATION_90;
2350 mLandscapeRotation = Surface.ROTATION_0;
Dianne Hackborne5439f22010-10-02 16:53:50 -07002351 mUpsideDownRotation = Surface.ROTATION_270;
Steve Howarda7bfe6a2010-09-23 16:47:49 -07002352 mSeascapeRotation = Surface.ROTATION_180;
Mitsuru Oshima831d0d92009-06-16 18:27:18 -07002353 } else {
2354 mPortraitRotation = Surface.ROTATION_0;
2355 mLandscapeRotation = Surface.ROTATION_90;
Dianne Hackborne5439f22010-10-02 16:53:50 -07002356 mUpsideDownRotation = Surface.ROTATION_180;
Steve Howarda7bfe6a2010-09-23 16:47:49 -07002357 mSeascapeRotation = Surface.ROTATION_270;
Mitsuru Oshima831d0d92009-06-16 18:27:18 -07002358 }
2359 }
2360
The Android Open Source Project0727d222009-03-11 12:11:58 -07002361 synchronized (mLock) {
Dianne Hackborne5439f22010-10-02 16:53:50 -07002362 switch (orientation) {
2363 case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT:
2364 //always return portrait if orientation set to portrait
2365 return mPortraitRotation;
2366 case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:
2367 //always return landscape if orientation set to landscape
2368 return mLandscapeRotation;
2369 case ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT:
2370 //always return portrait if orientation set to portrait
2371 return mUpsideDownRotation;
2372 case ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE:
2373 //always return seascape if orientation set to reverse landscape
2374 return mSeascapeRotation;
2375 case ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE:
2376 //return either landscape rotation based on the sensor
2377 mOrientationListener.setAllow180Rotation(false);
2378 return getCurrentLandscapeRotation(lastRotation);
2379 case ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT:
2380 mOrientationListener.setAllow180Rotation(true);
2381 return getCurrentPortraitRotation(lastRotation);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002382 }
Dianne Hackborne5439f22010-10-02 16:53:50 -07002383
Dianne Hackborna1111872010-11-23 20:55:11 -08002384 mOrientationListener.setAllow180Rotation(mAllowAllRotations ||
Mathias Agopianf9ce8792010-11-13 22:01:50 -08002385 orientation == ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
Dianne Hackborne5439f22010-10-02 16:53:50 -07002386
The Android Open Source Project0727d222009-03-11 12:11:58 -07002387 // case for nosensor meaning ignore sensor and consider only lid
2388 // or orientation sensor disabled
2389 //or case.unspecified
2390 if (mLidOpen) {
Mike Lockwood1753f7f2009-08-24 14:49:07 -07002391 return mLidOpenRotation;
Dianne Hackborn867ab6472010-04-29 13:28:56 -07002392 } else if (mDockMode == Intent.EXTRA_DOCK_STATE_CAR && mCarDockRotation >= 0) {
Mike Lockwood2d1efbd2009-09-16 12:15:53 -04002393 return mCarDockRotation;
Dianne Hackborn867ab6472010-04-29 13:28:56 -07002394 } else if (mDockMode == Intent.EXTRA_DOCK_STATE_DESK && mDeskDockRotation >= 0) {
Mike Lockwood2d1efbd2009-09-16 12:15:53 -04002395 return mDeskDockRotation;
Daniel Sandlerb73617d2010-08-17 00:41:00 -04002396 } else if (mUserRotationMode == WindowManagerPolicy.USER_ROTATION_LOCKED) {
2397 return mUserRotation;
The Android Open Source Project0727d222009-03-11 12:11:58 -07002398 } else {
Dianne Hackborn03759ed2009-03-27 16:04:08 -07002399 if (useSensorForOrientationLp(orientation)) {
Steve Howard5eb49582010-08-16 11:41:58 -07002400 return mOrientationListener.getCurrentRotation(lastRotation);
The Android Open Source Project0727d222009-03-11 12:11:58 -07002401 }
2402 return Surface.ROTATION_0;
2403 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002404 }
2405 }
2406
Steve Howarda7bfe6a2010-09-23 16:47:49 -07002407 private int getCurrentLandscapeRotation(int lastRotation) {
Dianne Hackborne5439f22010-10-02 16:53:50 -07002408 int sensorRotation = mOrientationListener.getCurrentRotation(lastRotation);
2409 if (isLandscapeOrSeascape(sensorRotation)) {
2410 return sensorRotation;
Steve Howarda7bfe6a2010-09-23 16:47:49 -07002411 }
2412 // try to preserve the old rotation if it was landscape
2413 if (isLandscapeOrSeascape(lastRotation)) {
2414 return lastRotation;
2415 }
Dianne Hackborne5439f22010-10-02 16:53:50 -07002416 // default to one of the primary landscape rotation
Steve Howarda7bfe6a2010-09-23 16:47:49 -07002417 return mLandscapeRotation;
2418 }
2419
2420 private boolean isLandscapeOrSeascape(int sensorRotation) {
2421 return sensorRotation == mLandscapeRotation || sensorRotation == mSeascapeRotation;
2422 }
2423
Dianne Hackborne5439f22010-10-02 16:53:50 -07002424 private int getCurrentPortraitRotation(int lastRotation) {
2425 int sensorRotation = mOrientationListener.getCurrentRotation(lastRotation);
2426 if (isAnyPortrait(sensorRotation)) {
2427 return sensorRotation;
2428 }
2429 // try to preserve the old rotation if it was portrait
2430 if (isAnyPortrait(lastRotation)) {
2431 return lastRotation;
2432 }
2433 // default to one of the primary portrait rotations
2434 return mPortraitRotation;
2435 }
2436
2437 private boolean isAnyPortrait(int sensorRotation) {
2438 return sensorRotation == mPortraitRotation || sensorRotation == mUpsideDownRotation;
2439 }
2440
Daniel Sandlerb73617d2010-08-17 00:41:00 -04002441
2442 // User rotation: to be used when all else fails in assigning an orientation to the device
2443 public void setUserRotationMode(int mode, int rot) {
2444 ContentResolver res = mContext.getContentResolver();
2445 mUserRotationMode = mode;
2446 if (mode == WindowManagerPolicy.USER_ROTATION_LOCKED) {
2447 mUserRotation = rot;
2448 Settings.System.putInt(res,
2449 Settings.System.ACCELEROMETER_ROTATION,
2450 0);
2451 Settings.System.putInt(res,
2452 Settings.System.USER_ROTATION,
2453 rot);
2454 } else {
2455 Settings.System.putInt(res,
2456 Settings.System.ACCELEROMETER_ROTATION,
2457 1);
2458 }
2459 }
2460
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002461 public boolean detectSafeMode() {
2462 try {
2463 int menuState = mWindowManager.getKeycodeState(KeyEvent.KEYCODE_MENU);
Dianne Hackbornb1a79802009-09-29 15:18:31 -07002464 int sState = mWindowManager.getKeycodeState(KeyEvent.KEYCODE_S);
Dianne Hackborna8730f92009-11-17 15:53:36 -08002465 int dpadState = mWindowManager.getDPadKeycodeState(KeyEvent.KEYCODE_DPAD_CENTER);
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002466 int trackballState = mWindowManager.getTrackballScancodeState(BTN_MOUSE);
Dianne Hackbornb1a79802009-09-29 15:18:31 -07002467 mSafeMode = menuState > 0 || sState > 0 || dpadState > 0 || trackballState > 0;
2468 performHapticFeedbackLw(null, mSafeMode
2469 ? HapticFeedbackConstants.SAFE_MODE_ENABLED
2470 : HapticFeedbackConstants.SAFE_MODE_DISABLED, true);
Dianne Hackborn73b1ec82009-10-04 13:33:00 -07002471 if (mSafeMode) {
2472 Log.i(TAG, "SAFE MODE ENABLED (menu=" + menuState + " s=" + sState
2473 + " dpad=" + dpadState + " trackball=" + trackballState + ")");
2474 } else {
2475 Log.i(TAG, "SAFE MODE not enabled");
2476 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002477 return mSafeMode;
2478 } catch (RemoteException e) {
2479 // Doom! (it's also local)
2480 throw new RuntimeException("window manager dead");
2481 }
2482 }
2483
Dianne Hackborn181ceb52009-08-27 22:16:40 -07002484 static long[] getLongIntArray(Resources r, int resid) {
2485 int[] ar = r.getIntArray(resid);
2486 if (ar == null) {
2487 return null;
2488 }
2489 long[] out = new long[ar.length];
2490 for (int i=0; i<ar.length; i++) {
2491 out[i] = ar[i];
2492 }
2493 return out;
2494 }
2495
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002496 /** {@inheritDoc} */
2497 public void systemReady() {
Dianne Hackbornb1a79802009-09-29 15:18:31 -07002498 // tell the keyguard
2499 mKeyguardMediator.onSystemReady();
2500 android.os.SystemProperties.set("dev.bootcomplete", "1");
2501 synchronized (mLock) {
2502 updateOrientationListenerLp();
Dianne Hackbornc777e072010-02-12 13:07:59 -08002503 mSystemReady = true;
2504 mHandler.post(new Runnable() {
2505 public void run() {
2506 updateSettings();
2507 }
2508 });
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002509 }
2510 }
2511
Mike Lockwood28569302010-01-28 11:54:40 -05002512 /** {@inheritDoc} */
2513 public void userActivity() {
2514 synchronized (mScreenLockTimeout) {
2515 if (mLockScreenTimerActive) {
2516 // reset the timer
2517 mHandler.removeCallbacks(mScreenLockTimeout);
2518 mHandler.postDelayed(mScreenLockTimeout, mLockScreenTimeout);
2519 }
2520 }
2521 }
2522
2523 Runnable mScreenLockTimeout = new Runnable() {
2524 public void run() {
2525 synchronized (this) {
2526 if (localLOGV) Log.v(TAG, "mScreenLockTimeout activating keyguard");
2527 mKeyguardMediator.doKeyguardTimeout();
2528 mLockScreenTimerActive = false;
2529 }
2530 }
2531 };
2532
2533 private void updateLockScreenTimeout() {
2534 synchronized (mScreenLockTimeout) {
2535 boolean enable = (mAllowLockscreenWhenOn && mScreenOn && mKeyguardMediator.isSecure());
2536 if (mLockScreenTimerActive != enable) {
2537 if (enable) {
2538 if (localLOGV) Log.v(TAG, "setting lockscreen timer");
2539 mHandler.postDelayed(mScreenLockTimeout, mLockScreenTimeout);
2540 } else {
2541 if (localLOGV) Log.v(TAG, "clearing lockscreen timer");
2542 mHandler.removeCallbacks(mScreenLockTimeout);
2543 }
2544 mLockScreenTimerActive = enable;
2545 }
2546 }
2547 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002548
2549 /** {@inheritDoc} */
2550 public void enableScreenAfterBoot() {
2551 readLidState();
Dianne Hackborn32bc91d2009-03-27 16:16:03 -07002552 updateRotation(Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002553 }
Dianne Hackbornf54c8f92009-09-19 14:03:57 -07002554
Dianne Hackborn03759ed2009-03-27 16:04:08 -07002555 void updateRotation(int animFlags) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002556 mPowerManager.setKeyboardVisibility(mLidOpen);
Mike Lockwood1753f7f2009-08-24 14:49:07 -07002557 int rotation = Surface.ROTATION_0;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002558 if (mLidOpen) {
Mike Lockwood1753f7f2009-08-24 14:49:07 -07002559 rotation = mLidOpenRotation;
Dianne Hackborn867ab6472010-04-29 13:28:56 -07002560 } else if (mDockMode == Intent.EXTRA_DOCK_STATE_CAR && mCarDockRotation >= 0) {
Mike Lockwood2d1efbd2009-09-16 12:15:53 -04002561 rotation = mCarDockRotation;
Dianne Hackborn867ab6472010-04-29 13:28:56 -07002562 } else if (mDockMode == Intent.EXTRA_DOCK_STATE_DESK && mDeskDockRotation >= 0) {
Mike Lockwood2d1efbd2009-09-16 12:15:53 -04002563 rotation = mDeskDockRotation;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002564 }
2565 //if lid is closed orientation will be portrait
2566 try {
2567 //set orientation on WindowManager
Dianne Hackborn32bc91d2009-03-27 16:16:03 -07002568 mWindowManager.setRotation(rotation, true,
2569 mFancyRotationAnimation | animFlags);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002570 } catch (RemoteException e) {
2571 // Ignore
2572 }
2573 }
2574
2575 /**
Dianne Hackborn39c2d712009-09-22 11:41:31 -07002576 * Return an Intent to launch the currently active dock as home. Returns
2577 * null if the standard home should be launched.
2578 * @return
2579 */
2580 Intent createHomeDockIntent() {
Dianne Hackborn39c2d712009-09-22 11:41:31 -07002581 Intent intent;
Dianne Hackborn867ab6472010-04-29 13:28:56 -07002582
2583 // What home does is based on the mode, not the dock state. That
2584 // is, when in car mode you should be taken to car home regardless
2585 // of whether we are actually in a car dock.
Dianne Hackborn78968392010-03-04 20:47:56 -08002586 if (mUiMode == Configuration.UI_MODE_TYPE_CAR) {
Dianne Hackborn39c2d712009-09-22 11:41:31 -07002587 intent = mCarDockIntent;
Dianne Hackborn78968392010-03-04 20:47:56 -08002588 } else if (mUiMode == Configuration.UI_MODE_TYPE_DESK) {
Dianne Hackborn39c2d712009-09-22 11:41:31 -07002589 intent = mDeskDockIntent;
2590 } else {
Dianne Hackborn39c2d712009-09-22 11:41:31 -07002591 return null;
2592 }
2593
2594 ActivityInfo ai = intent.resolveActivityInfo(
2595 mContext.getPackageManager(), PackageManager.GET_META_DATA);
2596 if (ai == null) {
2597 return null;
2598 }
2599
2600 if (ai.metaData != null && ai.metaData.getBoolean(Intent.METADATA_DOCK_HOME)) {
2601 intent = new Intent(intent);
2602 intent.setClassName(ai.packageName, ai.name);
2603 return intent;
2604 }
2605
2606 return null;
2607 }
2608
2609 void startDockOrHome() {
2610 Intent dock = createHomeDockIntent();
2611 if (dock != null) {
2612 try {
2613 mContext.startActivity(dock);
2614 return;
2615 } catch (ActivityNotFoundException e) {
2616 }
2617 }
2618 mContext.startActivity(mHomeIntent);
2619 }
2620
2621 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002622 * goes to the home screen
2623 * @return whether it did anything
2624 */
2625 boolean goHome() {
2626 if (false) {
2627 // This code always brings home to the front.
Dianne Hackborn256dd3b2009-05-19 18:51:21 -07002628 try {
2629 ActivityManagerNative.getDefault().stopAppSwitches();
2630 } catch (RemoteException e) {
2631 }
Dianne Hackborn94a679d2009-07-28 17:51:07 -07002632 sendCloseSystemWindows();
Dianne Hackborn39c2d712009-09-22 11:41:31 -07002633 startDockOrHome();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002634 } else {
2635 // This code brings home to the front or, if it is already
2636 // at the front, puts the device to sleep.
2637 try {
Wink Savilled2e6a332010-02-12 12:12:06 -08002638 if (SystemProperties.getInt("persist.sys.uts-test-mode", 0) == 1) {
2639 /// Roll back EndcallBehavior as the cupcake design to pass P1 lab entry.
2640 Log.d(TAG, "UTS-TEST-MODE");
2641 } else {
2642 ActivityManagerNative.getDefault().stopAppSwitches();
2643 sendCloseSystemWindows();
2644 Intent dock = createHomeDockIntent();
2645 if (dock != null) {
2646 int result = ActivityManagerNative.getDefault()
2647 .startActivity(null, dock,
2648 dock.resolveTypeIfNeeded(mContext.getContentResolver()),
2649 null, 0, null, null, 0, true /* onlyIfNeeded*/, false);
2650 if (result == IActivityManager.START_RETURN_INTENT_TO_CALLER) {
2651 return false;
2652 }
Dianne Hackborn39c2d712009-09-22 11:41:31 -07002653 }
2654 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002655 int result = ActivityManagerNative.getDefault()
2656 .startActivity(null, mHomeIntent,
2657 mHomeIntent.resolveTypeIfNeeded(mContext.getContentResolver()),
2658 null, 0, null, null, 0, true /* onlyIfNeeded*/, false);
2659 if (result == IActivityManager.START_RETURN_INTENT_TO_CALLER) {
2660 return false;
2661 }
2662 } catch (RemoteException ex) {
2663 // bummer, the activity manager, which is in this process, is dead
2664 }
2665 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002666 return true;
2667 }
2668
The Android Open Source Project0727d222009-03-11 12:11:58 -07002669 public void setCurrentOrientationLw(int newOrientation) {
2670 synchronized (mLock) {
2671 if (newOrientation != mCurrentAppOrientation) {
2672 mCurrentAppOrientation = newOrientation;
2673 updateOrientationListenerLp();
2674 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002675 }
2676 }
Amith Yamasanic33cb712010-02-10 15:21:49 -08002677
The Android Open Source Project0727d222009-03-11 12:11:58 -07002678 public boolean performHapticFeedbackLw(WindowState win, int effectId, boolean always) {
Jim Miller89b17222009-10-27 14:54:09 -07002679 final boolean hapticsDisabled = Settings.System.getInt(mContext.getContentResolver(),
2680 Settings.System.HAPTIC_FEEDBACK_ENABLED, 0) == 0;
Dianne Hackborn9dc06cc2009-11-17 18:19:23 -08002681 if (!always && (hapticsDisabled || mKeyguardMediator.isShowingAndNotHidden())) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002682 return false;
2683 }
Amith Yamasanic33cb712010-02-10 15:21:49 -08002684 long[] pattern = null;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002685 switch (effectId) {
2686 case HapticFeedbackConstants.LONG_PRESS:
Amith Yamasanic33cb712010-02-10 15:21:49 -08002687 pattern = mLongPressVibePattern;
2688 break;
Dianne Hackbornb1a79802009-09-29 15:18:31 -07002689 case HapticFeedbackConstants.VIRTUAL_KEY:
Amith Yamasanic33cb712010-02-10 15:21:49 -08002690 pattern = mVirtualKeyVibePattern;
2691 break;
2692 case HapticFeedbackConstants.KEYBOARD_TAP:
2693 pattern = mKeyboardTapVibePattern;
2694 break;
Dianne Hackbornb1a79802009-09-29 15:18:31 -07002695 case HapticFeedbackConstants.SAFE_MODE_DISABLED:
Amith Yamasanic33cb712010-02-10 15:21:49 -08002696 pattern = mSafeModeDisabledVibePattern;
2697 break;
Dianne Hackbornb1a79802009-09-29 15:18:31 -07002698 case HapticFeedbackConstants.SAFE_MODE_ENABLED:
Amith Yamasanic33cb712010-02-10 15:21:49 -08002699 pattern = mSafeModeEnabledVibePattern;
2700 break;
Amith Yamasanic33cb712010-02-10 15:21:49 -08002701 default:
2702 return false;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002703 }
Amith Yamasanic33cb712010-02-10 15:21:49 -08002704 if (pattern.length == 1) {
2705 // One-shot vibration
2706 mVibrator.vibrate(pattern[0]);
2707 } else {
2708 // Pattern vibration
2709 mVibrator.vibrate(pattern, -1);
2710 }
2711 return true;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002712 }
2713
The Android Open Source Project0727d222009-03-11 12:11:58 -07002714 public void screenOnStoppedLw() {
Dianne Hackborn9dc06cc2009-11-17 18:19:23 -08002715 if (!mKeyguardMediator.isShowingAndNotHidden() && mPowerManager.isScreenOn()) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002716 long curTime = SystemClock.uptimeMillis();
2717 mPowerManager.userActivity(curTime, false, LocalPowerManager.OTHER_EVENT);
2718 }
2719 }
Mike Lockwoodfcb39242009-10-21 23:01:07 -04002720
2721 public boolean allowKeyRepeat() {
2722 // disable key repeat when screen is off
2723 return mScreenOn;
2724 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002725}