blob: 99a8f484a1b96a23a2b8247c97fc2e1635a0cea7 [file] [log] [blame]
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.internal.policy.impl;
18
19import android.app.Activity;
20import android.app.ActivityManagerNative;
21import android.app.IActivityManager;
22import android.app.IStatusBar;
Dianne Hackborn39c2d712009-09-22 11:41:31 -070023import android.content.ActivityNotFoundException;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080024import android.content.BroadcastReceiver;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080025import android.content.ContentResolver;
26import android.content.Context;
27import android.content.Intent;
Mike Lockwood1753f7f2009-08-24 14:49:07 -070028import android.content.IntentFilter;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080029import android.content.pm.ActivityInfo;
30import android.content.pm.PackageManager;
31import android.content.res.Configuration;
32import android.content.res.Resources;
33import android.database.ContentObserver;
Dianne Hackbornc777e072010-02-12 13:07:59 -080034import android.graphics.PixelFormat;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080035import android.graphics.Rect;
Dianne Hackbornf54c8f92009-09-19 14:03:57 -070036import android.os.BatteryManager;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080037import android.os.Handler;
38import android.os.IBinder;
39import android.os.LocalPowerManager;
40import android.os.PowerManager;
41import android.os.RemoteException;
42import android.os.ServiceManager;
43import android.os.SystemClock;
44import android.os.SystemProperties;
45import android.os.Vibrator;
46import android.provider.Settings;
47
Dianne Hackbornc777e072010-02-12 13:07:59 -080048import com.android.common.ui.PointerLocationView;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080049import com.android.internal.policy.PolicyManager;
50import com.android.internal.telephony.ITelephony;
51import android.util.Config;
52import android.util.EventLog;
53import android.util.Log;
Mitsuru Oshima831d0d92009-06-16 18:27:18 -070054import android.view.Display;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080055import android.view.Gravity;
56import android.view.HapticFeedbackConstants;
57import android.view.IWindowManager;
58import android.view.KeyEvent;
59import android.view.MotionEvent;
60import android.view.WindowOrientationListener;
61import android.view.RawInputEvent;
62import android.view.Surface;
63import android.view.View;
64import android.view.ViewConfiguration;
65import android.view.Window;
66import android.view.WindowManager;
67import static android.view.WindowManager.LayoutParams.FIRST_APPLICATION_WINDOW;
68import static android.view.WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN;
69import static android.view.WindowManager.LayoutParams.FLAG_FULLSCREEN;
70import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
71import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR;
72import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
Suchi Amalapurapu9cdc9032009-05-14 18:01:07 -070073import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
Dianne Hackborn39c2d712009-09-22 11:41:31 -070074import static android.view.WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD;
Mike Lockwood28569302010-01-28 11:54:40 -050075import static android.view.WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080076import static android.view.WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST;
77import static android.view.WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;
78import static android.view.WindowManager.LayoutParams.LAST_APPLICATION_WINDOW;
79import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA;
Dianne Hackborn5cb8d792009-05-21 17:34:15 -070080import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA_OVERLAY;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080081import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_PANEL;
82import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL;
83import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
84import static android.view.WindowManager.LayoutParams.TYPE_KEYGUARD;
85import static android.view.WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG;
86import static android.view.WindowManager.LayoutParams.TYPE_PHONE;
87import static android.view.WindowManager.LayoutParams.TYPE_PRIORITY_PHONE;
88import static android.view.WindowManager.LayoutParams.TYPE_SEARCH_BAR;
89import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR;
90import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR_PANEL;
Dianne Hackborn6a294ce2009-12-03 11:37:44 -080091import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080092import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
93import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
94import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
95import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
96import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
97import static android.view.WindowManager.LayoutParams.TYPE_TOAST;
Dianne Hackbornb7cd8b92009-08-08 20:53:03 -070098import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080099import android.view.WindowManagerImpl;
100import android.view.WindowManagerPolicy;
Dianne Hackborn01ad2f42009-09-24 19:24:56 -0700101import android.view.animation.Animation;
102import android.view.animation.AnimationUtils;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800103import android.media.IAudioService;
104import android.media.AudioManager;
105
Dianne Hackborn08743722009-12-21 12:16:51 -0800106import java.util.ArrayList;
107
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800108/**
The Android Open Source Project0727d222009-03-11 12:11:58 -0700109 * WindowManagerPolicy implementation for the Android phone UI. This
110 * introduces a new method suffix, Lp, for an internal lock of the
111 * PhoneWindowManager. This is used to protect some internal state, and
112 * can be acquired with either thw Lw and Li lock held, so has the restrictions
113 * of both of those when held.
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800114 */
115public class PhoneWindowManager implements WindowManagerPolicy {
116 static final String TAG = "WindowManager";
117 static final boolean DEBUG = false;
118 static final boolean localLOGV = DEBUG ? Config.LOGD : Config.LOGV;
The Android Open Source Project11267662009-03-18 17:39:47 -0700119 static final boolean DEBUG_LAYOUT = false;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800120 static final boolean SHOW_STARTING_ANIMATIONS = true;
121 static final boolean SHOW_PROCESSES_ON_ALT_MENU = false;
122
Dianne Hackbornb7cd8b92009-08-08 20:53:03 -0700123 // wallpaper is at the bottom, though the window manager may move it.
Dianne Hackborn3c3bd442009-08-19 17:09:46 -0700124 static final int WALLPAPER_LAYER = 2;
Dianne Hackbornb7cd8b92009-08-08 20:53:03 -0700125 static final int APPLICATION_LAYER = 2;
126 static final int PHONE_LAYER = 3;
127 static final int SEARCH_BAR_LAYER = 4;
128 static final int STATUS_BAR_PANEL_LAYER = 5;
Dianne Hackborn6a294ce2009-12-03 11:37:44 -0800129 static final int SYSTEM_DIALOG_LAYER = 6;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800130 // toasts and the plugged-in battery thing
Dianne Hackborn6a294ce2009-12-03 11:37:44 -0800131 static final int TOAST_LAYER = 7;
132 static final int STATUS_BAR_LAYER = 8;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800133 // SIM errors and unlock. Not sure if this really should be in a high layer.
Dianne Hackborn6a294ce2009-12-03 11:37:44 -0800134 static final int PRIORITY_PHONE_LAYER = 9;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800135 // like the ANR / app crashed dialogs
Dianne Hackborn6a294ce2009-12-03 11:37:44 -0800136 static final int SYSTEM_ALERT_LAYER = 10;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800137 // system-level error dialogs
Dianne Hackborn6a294ce2009-12-03 11:37:44 -0800138 static final int SYSTEM_ERROR_LAYER = 11;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800139 // on-screen keyboards and other such input method user interfaces go here.
Dianne Hackborn6a294ce2009-12-03 11:37:44 -0800140 static final int INPUT_METHOD_LAYER = 12;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800141 // on-screen keyboards and other such input method user interfaces go here.
Dianne Hackborn6a294ce2009-12-03 11:37:44 -0800142 static final int INPUT_METHOD_DIALOG_LAYER = 13;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800143 // the keyguard; nothing on top of these can take focus, since they are
144 // responsible for power management when displayed.
Dianne Hackborn6a294ce2009-12-03 11:37:44 -0800145 static final int KEYGUARD_LAYER = 14;
146 static final int KEYGUARD_DIALOG_LAYER = 15;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800147 // things in here CAN NOT take focus, but are shown on top of everything else.
Dianne Hackborn6a294ce2009-12-03 11:37:44 -0800148 static final int SYSTEM_OVERLAY_LAYER = 16;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800149
Dianne Hackborn5cb8d792009-05-21 17:34:15 -0700150 static final int APPLICATION_MEDIA_SUBLAYER = -2;
151 static final int APPLICATION_MEDIA_OVERLAY_SUBLAYER = -1;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800152 static final int APPLICATION_PANEL_SUBLAYER = 1;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800153 static final int APPLICATION_SUB_PANEL_SUBLAYER = 2;
154
155 static final float SLIDE_TOUCH_EVENT_SIZE_LIMIT = 0.6f;
156
157 // Debugging: set this to have the system act like there is no hard keyboard.
158 static final boolean KEYBOARD_ALWAYS_HIDDEN = false;
159
160 static public final String SYSTEM_DIALOG_REASON_KEY = "reason";
161 static public final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions";
162 static public final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";
Joe Onoratof275f0c2009-11-24 16:11:13 -0500163 static public final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800164
The Android Open Source Project0727d222009-03-11 12:11:58 -0700165 final Object mLock = new Object();
166
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800167 Context mContext;
168 IWindowManager mWindowManager;
169 LocalPowerManager mPowerManager;
170 Vibrator mVibrator; // Vibrator for giving feedback of orientation changes
171
Dianne Hackborn181ceb52009-08-27 22:16:40 -0700172 // Vibrator pattern for haptic feedback of a long press.
173 long[] mLongPressVibePattern;
174
175 // Vibrator pattern for haptic feedback of virtual key press.
176 long[] mVirtualKeyVibePattern;
177
Dianne Hackbornb1a79802009-09-29 15:18:31 -0700178 // Vibrator pattern for haptic feedback during boot when safe mode is disabled.
179 long[] mSafeModeDisabledVibePattern;
180
181 // Vibrator pattern for haptic feedback during boot when safe mode is enabled.
182 long[] mSafeModeEnabledVibePattern;
183
Adam Powell9d86faf2010-02-10 11:39:57 -0800184 // Vibrator pattern for haptic feedback when the user hits a touch scroll barrier.
185 long[] mScrollBarrierVibePattern;
186
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800187 /** If true, hitting shift & menu will broadcast Intent.ACTION_BUG_REPORT */
188 boolean mEnableShiftMenuBugReports = false;
189
190 boolean mSafeMode;
191 WindowState mStatusBar = null;
Dianne Hackborn08743722009-12-21 12:16:51 -0800192 final ArrayList<WindowState> mStatusBarPanels = new ArrayList<WindowState>();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800193 WindowState mKeyguard = null;
194 KeyguardViewMediator mKeyguardMediator;
195 GlobalActions mGlobalActions;
196 boolean mShouldTurnOffOnKeyUp;
197 RecentApplicationsDialog mRecentAppsDialog;
198 Handler mHandler;
199
Dianne Hackborn05726582009-09-22 17:28:24 -0700200 final IntentFilter mBatteryStatusFilter = new IntentFilter();
201
Dianne Hackbornc777e072010-02-12 13:07:59 -0800202 boolean mSystemReady;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800203 boolean mLidOpen;
Dianne Hackborn05726582009-09-22 17:28:24 -0700204 int mPlugged;
Dianne Hackborn80fa1662009-10-07 14:02:10 -0700205 boolean mRegisteredBatteryReceiver;
Mike Lockwood2d1efbd2009-09-16 12:15:53 -0400206 int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
Mike Lockwood1753f7f2009-08-24 14:49:07 -0700207 int mLidOpenRotation;
Mike Lockwood2d1efbd2009-09-16 12:15:53 -0400208 int mCarDockRotation;
209 int mDeskDockRotation;
Dianne Hackborn05726582009-09-22 17:28:24 -0700210 int mCarDockKeepsScreenOn;
211 int mDeskDockKeepsScreenOn;
Mike Lockwoode9867d22009-09-20 01:59:02 -0400212 boolean mCarDockEnablesAccelerometer;
213 boolean mDeskDockEnablesAccelerometer;
Dianne Hackborn2862fff2009-09-15 22:56:29 -0700214 int mLidKeyboardAccessibility;
215 int mLidNavigationAccessibility;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800216 boolean mScreenOn = false;
217 boolean mOrientationSensorEnabled = false;
218 int mCurrentAppOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
219 static final int DEFAULT_ACCELEROMETER_ROTATION = 0;
220 int mAccelerometerDefault = DEFAULT_ACCELEROMETER_ROTATION;
221 boolean mHasSoftInput = false;
222
Dianne Hackbornc777e072010-02-12 13:07:59 -0800223 int mPointerLocationMode = 0;
224 PointerLocationView mPointerLocationView = null;
225
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800226 // The current size of the screen.
227 int mW, mH;
228 // During layout, the current screen borders with all outer decoration
229 // (status bar, input method dock) accounted for.
230 int mCurLeft, mCurTop, mCurRight, mCurBottom;
231 // During layout, the frame in which content should be displayed
232 // to the user, accounting for all screen decoration except for any
233 // space they deem as available for other content. This is usually
234 // the same as mCur*, but may be larger if the screen decor has supplied
235 // content insets.
236 int mContentLeft, mContentTop, mContentRight, mContentBottom;
237 // During layout, the current screen borders along with input method
238 // windows are placed.
239 int mDockLeft, mDockTop, mDockRight, mDockBottom;
240 // During layout, the layer at which the doc window is placed.
241 int mDockLayer;
242
243 static final Rect mTmpParentFrame = new Rect();
244 static final Rect mTmpDisplayFrame = new Rect();
245 static final Rect mTmpContentFrame = new Rect();
246 static final Rect mTmpVisibleFrame = new Rect();
247
248 WindowState mTopFullscreenOpaqueWindowState;
249 boolean mForceStatusBar;
Dianne Hackborn39c2d712009-09-22 11:41:31 -0700250 boolean mHideLockScreen;
251 boolean mDismissKeyguard;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800252 boolean mHomePressed;
253 Intent mHomeIntent;
Dianne Hackborn39c2d712009-09-22 11:41:31 -0700254 Intent mCarDockIntent;
255 Intent mDeskDockIntent;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800256 boolean mSearchKeyPressed;
257 boolean mConsumeSearchKeyUp;
258
Mike Lockwood28569302010-01-28 11:54:40 -0500259 // support for activating the lock screen while the screen is on
260 boolean mAllowLockscreenWhenOn;
261 int mLockScreenTimeout;
262 boolean mLockScreenTimerActive;
263
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800264 static final int ENDCALL_HOME = 0x1;
265 static final int ENDCALL_SLEEPS = 0x2;
266 static final int DEFAULT_ENDCALL_BEHAVIOR = ENDCALL_SLEEPS;
267 int mEndcallBehavior;
Dianne Hackborn03759ed2009-03-27 16:04:08 -0700268
Mitsuru Oshima831d0d92009-06-16 18:27:18 -0700269 int mLandscapeRotation = -1;
270 int mPortraitRotation = -1;
271
Dianne Hackborn03759ed2009-03-27 16:04:08 -0700272 // Nothing to see here, move along...
273 int mFancyRotationAnimation;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800274
275 ShortcutManager mShortcutManager;
276 PowerManager.WakeLock mBroadcastWakeLock;
Dianne Hackbornf54c8f92009-09-19 14:03:57 -0700277 PowerManager.WakeLock mDockWakeLock;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800278
279 class SettingsObserver extends ContentObserver {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800280 SettingsObserver(Handler handler) {
281 super(handler);
282 }
283
284 void observe() {
285 ContentResolver resolver = mContext.getContentResolver();
286 resolver.registerContentObserver(Settings.System.getUriFor(
287 Settings.System.END_BUTTON_BEHAVIOR), false, this);
288 resolver.registerContentObserver(Settings.System.getUriFor(
289 Settings.System.ACCELEROMETER_ROTATION), false, this);
Mike Lockwood28569302010-01-28 11:54:40 -0500290 resolver.registerContentObserver(Settings.System.getUriFor(
291 Settings.System.SCREEN_OFF_TIMEOUT), false, this);
Dianne Hackbornc777e072010-02-12 13:07:59 -0800292 resolver.registerContentObserver(Settings.System.getUriFor(
293 Settings.System.POINTER_LOCATION), false, this);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800294 resolver.registerContentObserver(Settings.Secure.getUriFor(
295 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
Dianne Hackborn03759ed2009-03-27 16:04:08 -0700296 resolver.registerContentObserver(Settings.System.getUriFor(
297 "fancy_rotation_anim"), false, this);
Dianne Hackbornc777e072010-02-12 13:07:59 -0800298 updateSettings();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800299 }
300
301 @Override public void onChange(boolean selfChange) {
Dianne Hackbornc777e072010-02-12 13:07:59 -0800302 updateSettings();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800303 try {
Dianne Hackborn32bc91d2009-03-27 16:16:03 -0700304 mWindowManager.setRotation(USE_LAST_ROTATION, false,
305 mFancyRotationAnimation);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800306 } catch (RemoteException e) {
307 // Ignore
308 }
309 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800310 }
311
312 class MyOrientationListener extends WindowOrientationListener {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800313 MyOrientationListener(Context context) {
314 super(context);
315 }
316
317 @Override
The Android Open Source Projectd06b0972009-03-13 13:04:23 -0700318 public void onOrientationChanged(int rotation) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800319 // Send updates based on orientation value
Dianne Hackborn05726582009-09-22 17:28:24 -0700320 if (localLOGV) Log.v(TAG, "onOrientationChanged, rotation changed to " +rotation);
Dianne Hackborn03759ed2009-03-27 16:04:08 -0700321 try {
Dianne Hackborn32bc91d2009-03-27 16:16:03 -0700322 mWindowManager.setRotation(rotation, false,
323 mFancyRotationAnimation);
Dianne Hackborn03759ed2009-03-27 16:04:08 -0700324 } catch (RemoteException e) {
325 // Ignore
Wink Saville37c124c2009-04-02 01:37:02 -0700326
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800327 }
328 }
329 }
330 MyOrientationListener mOrientationListener;
331
Dianne Hackborn03759ed2009-03-27 16:04:08 -0700332 boolean useSensorForOrientationLp(int appOrientation) {
Joe Onorato1b58b3e2009-09-22 16:44:16 -0700333 // The app says use the sensor.
Dianne Hackborn03759ed2009-03-27 16:04:08 -0700334 if (appOrientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800335 return true;
336 }
Joe Onorato1b58b3e2009-09-22 16:44:16 -0700337 // The user preference says we can rotate, and the app is willing to rotate.
338 if (mAccelerometerDefault != 0 &&
339 (appOrientation == ActivityInfo.SCREEN_ORIENTATION_USER
340 || appOrientation == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800341 return true;
342 }
Joe Onorato1b58b3e2009-09-22 16:44:16 -0700343 // We're in a dock that has a rotation affinity, an the app is willing to rotate.
344 if ((mCarDockEnablesAccelerometer && mDockState == Intent.EXTRA_DOCK_STATE_CAR)
345 || (mDeskDockEnablesAccelerometer && mDockState == Intent.EXTRA_DOCK_STATE_DESK)) {
346 // Note we override the nosensor flag here.
347 if (appOrientation == ActivityInfo.SCREEN_ORIENTATION_USER
348 || appOrientation == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
349 || appOrientation == ActivityInfo.SCREEN_ORIENTATION_NOSENSOR) {
350 return true;
351 }
352 }
353 // Else, don't use the sensor.
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800354 return false;
355 }
356
Suchi Amalapurapua9e1b162009-03-24 22:24:47 -0700357 /*
358 * We always let the sensor be switched on by default except when
359 * the user has explicitly disabled sensor based rotation or when the
360 * screen is switched off.
361 */
The Android Open Source Project0727d222009-03-11 12:11:58 -0700362 boolean needSensorRunningLp() {
363 if (mCurrentAppOrientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800364 // If the application has explicitly requested to follow the
365 // orientation, then we need to turn the sensor or.
366 return true;
367 }
Mike Lockwoode9867d22009-09-20 01:59:02 -0400368 if ((mCarDockEnablesAccelerometer && mDockState == Intent.EXTRA_DOCK_STATE_CAR) ||
369 (mDeskDockEnablesAccelerometer && mDockState == Intent.EXTRA_DOCK_STATE_DESK)) {
370 // enable accelerometer if we are docked in a dock that enables accelerometer
371 // orientation management,
372 return true;
373 }
Suchi Amalapurapua9e1b162009-03-24 22:24:47 -0700374 if (mAccelerometerDefault == 0) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800375 // If the setting for using the sensor by default is enabled, then
376 // we will always leave it on. Note that the user could go to
377 // a window that forces an orientation that does not use the
378 // sensor and in theory we could turn it off... however, when next
379 // turning it on we won't have a good value for the current
380 // orientation for a little bit, which can cause orientation
381 // changes to lag, so we'd like to keep it always on. (It will
382 // still be turned off when the screen is off.)
Suchi Amalapurapua9e1b162009-03-24 22:24:47 -0700383 return false;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800384 }
Suchi Amalapurapua9e1b162009-03-24 22:24:47 -0700385 return true;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800386 }
387
388 /*
389 * Various use cases for invoking this function
390 * screen turning off, should always disable listeners if already enabled
391 * screen turned on and current app has sensor based orientation, enable listeners
392 * if not already enabled
393 * screen turned on and current app does not have sensor orientation, disable listeners if
394 * already enabled
395 * screen turning on and current app has sensor based orientation, enable listeners if needed
396 * screen turning on and current app has nosensor based orientation, do nothing
397 */
The Android Open Source Project0727d222009-03-11 12:11:58 -0700398 void updateOrientationListenerLp() {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800399 if (!mOrientationListener.canDetectOrientation()) {
400 // If sensor is turned off or nonexistent for some reason
401 return;
402 }
403 //Could have been invoked due to screen turning on or off or
404 //change of the currently visible window's orientation
Dianne Hackborn05726582009-09-22 17:28:24 -0700405 if (localLOGV) Log.v(TAG, "Screen status="+mScreenOn+
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800406 ", current orientation="+mCurrentAppOrientation+
407 ", SensorEnabled="+mOrientationSensorEnabled);
408 boolean disable = true;
The Android Open Source Project0727d222009-03-11 12:11:58 -0700409 if (mScreenOn) {
410 if (needSensorRunningLp()) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800411 disable = false;
412 //enable listener if not already enabled
The Android Open Source Project0727d222009-03-11 12:11:58 -0700413 if (!mOrientationSensorEnabled) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800414 mOrientationListener.enable();
Dianne Hackborn05726582009-09-22 17:28:24 -0700415 if(localLOGV) Log.v(TAG, "Enabling listeners");
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800416 mOrientationSensorEnabled = true;
417 }
418 }
419 }
420 //check if sensors need to be disabled
The Android Open Source Project0727d222009-03-11 12:11:58 -0700421 if (disable && mOrientationSensorEnabled) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800422 mOrientationListener.disable();
Dianne Hackborn05726582009-09-22 17:28:24 -0700423 if(localLOGV) Log.v(TAG, "Disabling listeners");
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800424 mOrientationSensorEnabled = false;
425 }
426 }
427
Dianne Hackborn0041e972009-07-24 17:14:43 -0700428 Runnable mPowerLongPress = new Runnable() {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800429 public void run() {
430 mShouldTurnOffOnKeyUp = false;
The Android Open Source Project0727d222009-03-11 12:11:58 -0700431 performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800432 sendCloseSystemWindows(SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS);
433 showGlobalActionsDialog();
434 }
435 };
436
437 void showGlobalActionsDialog() {
438 if (mGlobalActions == null) {
439 mGlobalActions = new GlobalActions(mContext);
440 }
Dianne Hackborn9dc06cc2009-11-17 18:19:23 -0800441 final boolean keyguardShowing = mKeyguardMediator.isShowingAndNotHidden();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800442 mGlobalActions.showDialog(keyguardShowing, isDeviceProvisioned());
443 if (keyguardShowing) {
444 // since it took two seconds of long press to bring this up,
445 // poke the wake lock so they have some time to see the dialog.
446 mKeyguardMediator.pokeWakelock();
447 }
448 }
449
450 boolean isDeviceProvisioned() {
451 return Settings.Secure.getInt(
452 mContext.getContentResolver(), Settings.Secure.DEVICE_PROVISIONED, 0) != 0;
453 }
454
455 /**
456 * When a home-key longpress expires, close other system windows and launch the recent apps
457 */
458 Runnable mHomeLongPress = new Runnable() {
459 public void run() {
460 /*
461 * Eat the longpress so it won't dismiss the recent apps dialog when
462 * the user lets go of the home key
463 */
464 mHomePressed = false;
The Android Open Source Project0727d222009-03-11 12:11:58 -0700465 performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800466 sendCloseSystemWindows(SYSTEM_DIALOG_REASON_RECENT_APPS);
467 showRecentAppsDialog();
468 }
469 };
470
471 /**
472 * Create (if necessary) and launch the recent apps dialog
473 */
474 void showRecentAppsDialog() {
475 if (mRecentAppsDialog == null) {
476 mRecentAppsDialog = new RecentApplicationsDialog(mContext);
477 }
478 mRecentAppsDialog.show();
479 }
480
481 /** {@inheritDoc} */
482 public void init(Context context, IWindowManager windowManager,
483 LocalPowerManager powerManager) {
484 mContext = context;
485 mWindowManager = windowManager;
486 mPowerManager = powerManager;
487 mKeyguardMediator = new KeyguardViewMediator(context, this, powerManager);
488 mHandler = new Handler();
489 mOrientationListener = new MyOrientationListener(mContext);
490 SettingsObserver settingsObserver = new SettingsObserver(mHandler);
491 settingsObserver.observe();
492 mShortcutManager = new ShortcutManager(context, mHandler);
493 mShortcutManager.observe();
494 mHomeIntent = new Intent(Intent.ACTION_MAIN, null);
495 mHomeIntent.addCategory(Intent.CATEGORY_HOME);
496 mHomeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
497 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Dianne Hackborn39c2d712009-09-22 11:41:31 -0700498 mCarDockIntent = new Intent(Intent.ACTION_MAIN, null);
499 mCarDockIntent.addCategory(Intent.CATEGORY_CAR_DOCK);
500 mCarDockIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
501 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
502 mDeskDockIntent = new Intent(Intent.ACTION_MAIN, null);
503 mDeskDockIntent.addCategory(Intent.CATEGORY_DESK_DOCK);
504 mDeskDockIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
505 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800506 PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
507 mBroadcastWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
508 "PhoneWindowManager.mBroadcastWakeLock");
Mike Lockwoode224cef2010-01-13 21:11:08 -0800509 mDockWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE,
Dianne Hackbornf54c8f92009-09-19 14:03:57 -0700510 "PhoneWindowManager.mDockWakeLock");
511 mDockWakeLock.setReferenceCounted(false);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800512 mEnableShiftMenuBugReports = "1".equals(SystemProperties.get("ro.debuggable"));
Dianne Hackborn2862fff2009-09-15 22:56:29 -0700513 mLidOpenRotation = readRotation(
Mike Lockwood2d1efbd2009-09-16 12:15:53 -0400514 com.android.internal.R.integer.config_lidOpenRotation);
515 mCarDockRotation = readRotation(
516 com.android.internal.R.integer.config_carDockRotation);
517 mDeskDockRotation = readRotation(
518 com.android.internal.R.integer.config_deskDockRotation);
Dianne Hackborn05726582009-09-22 17:28:24 -0700519 mCarDockKeepsScreenOn = mContext.getResources().getInteger(
520 com.android.internal.R.integer.config_carDockKeepsScreenOn);
521 mDeskDockKeepsScreenOn = mContext.getResources().getInteger(
522 com.android.internal.R.integer.config_deskDockKeepsScreenOn);
Mike Lockwoode9867d22009-09-20 01:59:02 -0400523 mCarDockEnablesAccelerometer = mContext.getResources().getBoolean(
524 com.android.internal.R.bool.config_carDockEnablesAccelerometer);
525 mDeskDockEnablesAccelerometer = mContext.getResources().getBoolean(
526 com.android.internal.R.bool.config_deskDockEnablesAccelerometer);
Dianne Hackborn2862fff2009-09-15 22:56:29 -0700527 mLidKeyboardAccessibility = mContext.getResources().getInteger(
528 com.android.internal.R.integer.config_lidKeyboardAccessibility);
529 mLidNavigationAccessibility = mContext.getResources().getInteger(
530 com.android.internal.R.integer.config_lidNavigationAccessibility);
Dianne Hackbornf54c8f92009-09-19 14:03:57 -0700531 // register for battery events
Dianne Hackborn05726582009-09-22 17:28:24 -0700532 mBatteryStatusFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
533 mPlugged = 0;
Dianne Hackborn80fa1662009-10-07 14:02:10 -0700534 updatePlugged(context.registerReceiver(null, mBatteryStatusFilter));
Mike Lockwood1753f7f2009-08-24 14:49:07 -0700535 // register for dock events
536 context.registerReceiver(mDockReceiver, new IntentFilter(Intent.ACTION_DOCK_EVENT));
Dianne Hackbornb1a79802009-09-29 15:18:31 -0700537 mVibrator = new Vibrator();
538 mLongPressVibePattern = getLongIntArray(mContext.getResources(),
539 com.android.internal.R.array.config_longPressVibePattern);
540 mVirtualKeyVibePattern = getLongIntArray(mContext.getResources(),
541 com.android.internal.R.array.config_virtualKeyVibePattern);
542 mSafeModeDisabledVibePattern = getLongIntArray(mContext.getResources(),
543 com.android.internal.R.array.config_safeModeDisabledVibePattern);
544 mSafeModeEnabledVibePattern = getLongIntArray(mContext.getResources(),
545 com.android.internal.R.array.config_safeModeEnabledVibePattern);
Adam Powell9d86faf2010-02-10 11:39:57 -0800546 mScrollBarrierVibePattern = getLongIntArray(mContext.getResources(),
547 com.android.internal.R.array.config_scrollBarrierVibePattern);
Mike Lockwood1753f7f2009-08-24 14:49:07 -0700548 }
549
Dianne Hackbornc777e072010-02-12 13:07:59 -0800550 public void updateSettings() {
551 ContentResolver resolver = mContext.getContentResolver();
552 boolean updateRotation = false;
553 View addView = null;
554 View removeView = null;
555 synchronized (mLock) {
556 mEndcallBehavior = Settings.System.getInt(resolver,
557 Settings.System.END_BUTTON_BEHAVIOR, DEFAULT_ENDCALL_BEHAVIOR);
558 mFancyRotationAnimation = Settings.System.getInt(resolver,
559 "fancy_rotation_anim", 0) != 0 ? 0x80 : 0;
560 int accelerometerDefault = Settings.System.getInt(resolver,
561 Settings.System.ACCELEROMETER_ROTATION, DEFAULT_ACCELEROMETER_ROTATION);
562 if (mAccelerometerDefault != accelerometerDefault) {
563 mAccelerometerDefault = accelerometerDefault;
564 updateOrientationListenerLp();
565 }
566 if (mSystemReady) {
567 int pointerLocation = Settings.System.getInt(resolver,
568 Settings.System.POINTER_LOCATION, 0);
569 if (mPointerLocationMode != pointerLocation) {
570 mPointerLocationMode = pointerLocation;
571 if (pointerLocation != 0) {
572 if (mPointerLocationView == null) {
573 mPointerLocationView = new PointerLocationView(mContext);
574 mPointerLocationView.setPrintCoords(false);
575 addView = mPointerLocationView;
576 }
577 } else {
578 removeView = mPointerLocationView;
579 mPointerLocationView = null;
580 }
581 }
582 }
583 // use screen off timeout setting as the timeout for the lockscreen
584 mLockScreenTimeout = Settings.System.getInt(resolver,
585 Settings.System.SCREEN_OFF_TIMEOUT, 0);
586 String imId = Settings.Secure.getString(resolver,
587 Settings.Secure.DEFAULT_INPUT_METHOD);
588 boolean hasSoftInput = imId != null && imId.length() > 0;
589 if (mHasSoftInput != hasSoftInput) {
590 mHasSoftInput = hasSoftInput;
591 updateRotation = true;
592 }
593 }
594 if (updateRotation) {
595 updateRotation(0);
596 }
597 if (addView != null) {
598 WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
599 WindowManager.LayoutParams.MATCH_PARENT,
600 WindowManager.LayoutParams.MATCH_PARENT);
601 lp.type = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
602 lp.flags =
603 WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE|
604 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|
605 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
606 lp.format = PixelFormat.TRANSLUCENT;
607 lp.setTitle("PointerLocation");
608 WindowManagerImpl wm = (WindowManagerImpl)
609 mContext.getSystemService(Context.WINDOW_SERVICE);
610 wm.addView(addView, lp);
611 }
612 if (removeView != null) {
613 WindowManagerImpl wm = (WindowManagerImpl)
614 mContext.getSystemService(Context.WINDOW_SERVICE);
615 wm.removeView(removeView);
616 }
617 }
618
Dianne Hackborn80fa1662009-10-07 14:02:10 -0700619 void updatePlugged(Intent powerIntent) {
Dianne Hackborn05726582009-09-22 17:28:24 -0700620 if (localLOGV) Log.v(TAG, "New battery status: " + powerIntent.getExtras());
621 if (powerIntent != null) {
622 mPlugged = powerIntent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
623 if (localLOGV) Log.v(TAG, "PLUGGED: " + mPlugged);
624 }
625 }
626
Mike Lockwood2d1efbd2009-09-16 12:15:53 -0400627 private int readRotation(int resID) {
Mike Lockwood1753f7f2009-08-24 14:49:07 -0700628 try {
629 int rotation = mContext.getResources().getInteger(resID);
630 switch (rotation) {
631 case 0:
632 return Surface.ROTATION_0;
633 case 90:
634 return Surface.ROTATION_90;
635 case 180:
636 return Surface.ROTATION_180;
637 case 270:
638 return Surface.ROTATION_270;
Mike Lockwood1753f7f2009-08-24 14:49:07 -0700639 }
640 } catch (Resources.NotFoundException e) {
Mike Lockwood2d1efbd2009-09-16 12:15:53 -0400641 // fall through
Mike Lockwood1753f7f2009-08-24 14:49:07 -0700642 }
Mike Lockwood2d1efbd2009-09-16 12:15:53 -0400643 return -1;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800644 }
645
646 /** {@inheritDoc} */
647 public int checkAddPermission(WindowManager.LayoutParams attrs) {
648 int type = attrs.type;
Mitsuru Oshima831d0d92009-06-16 18:27:18 -0700649
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800650 if (type < WindowManager.LayoutParams.FIRST_SYSTEM_WINDOW
651 || type > WindowManager.LayoutParams.LAST_SYSTEM_WINDOW) {
652 return WindowManagerImpl.ADD_OKAY;
653 }
654 String permission = null;
655 switch (type) {
656 case TYPE_TOAST:
657 // XXX right now the app process has complete control over
658 // this... should introduce a token to let the system
659 // monitor/control what they are doing.
660 break;
661 case TYPE_INPUT_METHOD:
Dianne Hackbornb7cd8b92009-08-08 20:53:03 -0700662 case TYPE_WALLPAPER:
663 // The window manager will check these.
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800664 break;
665 case TYPE_PHONE:
666 case TYPE_PRIORITY_PHONE:
667 case TYPE_SYSTEM_ALERT:
668 case TYPE_SYSTEM_ERROR:
669 case TYPE_SYSTEM_OVERLAY:
670 permission = android.Manifest.permission.SYSTEM_ALERT_WINDOW;
671 break;
672 default:
673 permission = android.Manifest.permission.INTERNAL_SYSTEM_WINDOW;
674 }
675 if (permission != null) {
676 if (mContext.checkCallingOrSelfPermission(permission)
677 != PackageManager.PERMISSION_GRANTED) {
678 return WindowManagerImpl.ADD_PERMISSION_DENIED;
679 }
680 }
681 return WindowManagerImpl.ADD_OKAY;
682 }
683
684 public void adjustWindowParamsLw(WindowManager.LayoutParams attrs) {
685 switch (attrs.type) {
686 case TYPE_SYSTEM_OVERLAY:
687 case TYPE_TOAST:
688 // These types of windows can't receive input events.
689 attrs.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
690 | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
691 break;
692 }
693 }
694
695 void readLidState() {
696 try {
Mike Lockwood1753f7f2009-08-24 14:49:07 -0700697 int sw = mWindowManager.getSwitchState(RawInputEvent.SW_LID);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800698 if (sw >= 0) {
699 mLidOpen = sw == 0;
700 }
701 } catch (RemoteException e) {
702 // Ignore
703 }
704 }
705
Dianne Hackborn2862fff2009-09-15 22:56:29 -0700706 private int determineHiddenState(boolean lidOpen,
707 int mode, int hiddenValue, int visibleValue) {
708 switch (mode) {
709 case 1:
710 return lidOpen ? visibleValue : hiddenValue;
711 case 2:
712 return lidOpen ? hiddenValue : visibleValue;
713 }
714 return visibleValue;
715 }
716
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800717 /** {@inheritDoc} */
718 public void adjustConfigurationLw(Configuration config) {
719 readLidState();
720 final boolean lidOpen = !KEYBOARD_ALWAYS_HIDDEN && mLidOpen;
721 mPowerManager.setKeyboardVisibility(lidOpen);
Dianne Hackborn2862fff2009-09-15 22:56:29 -0700722 config.hardKeyboardHidden = determineHiddenState(lidOpen,
723 mLidKeyboardAccessibility, Configuration.HARDKEYBOARDHIDDEN_YES,
724 Configuration.HARDKEYBOARDHIDDEN_NO);
725 config.navigationHidden = determineHiddenState(lidOpen,
726 mLidNavigationAccessibility, Configuration.NAVIGATIONHIDDEN_YES,
727 Configuration.NAVIGATIONHIDDEN_NO);
728 config.keyboardHidden = (config.hardKeyboardHidden
729 == Configuration.HARDKEYBOARDHIDDEN_NO || mHasSoftInput)
730 ? Configuration.KEYBOARDHIDDEN_NO
731 : Configuration.KEYBOARDHIDDEN_YES;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800732 }
733
734 public boolean isCheekPressedAgainstScreen(MotionEvent ev) {
735 if(ev.getSize() > SLIDE_TOUCH_EVENT_SIZE_LIMIT) {
736 return true;
737 }
738 int size = ev.getHistorySize();
739 for(int i = 0; i < size; i++) {
740 if(ev.getHistoricalSize(i) > SLIDE_TOUCH_EVENT_SIZE_LIMIT) {
741 return true;
742 }
743 }
744 return false;
745 }
746
Dianne Hackbornc777e072010-02-12 13:07:59 -0800747 public void dispatchedPointerEventLw(MotionEvent ev, int targetX, int targetY) {
748 if (mPointerLocationView == null) {
749 return;
750 }
751 synchronized (mLock) {
752 if (mPointerLocationView == null) {
753 return;
754 }
755 ev.offsetLocation(targetX, targetY);
756 mPointerLocationView.addTouchEvent(ev);
757 ev.offsetLocation(-targetX, -targetY);
758 }
759 }
760
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800761 /** {@inheritDoc} */
762 public int windowTypeToLayerLw(int type) {
763 if (type >= FIRST_APPLICATION_WINDOW && type <= LAST_APPLICATION_WINDOW) {
764 return APPLICATION_LAYER;
765 }
766 switch (type) {
767 case TYPE_STATUS_BAR:
768 return STATUS_BAR_LAYER;
769 case TYPE_STATUS_BAR_PANEL:
770 return STATUS_BAR_PANEL_LAYER;
Dianne Hackborn6a294ce2009-12-03 11:37:44 -0800771 case TYPE_SYSTEM_DIALOG:
772 return SYSTEM_DIALOG_LAYER;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800773 case TYPE_SEARCH_BAR:
774 return SEARCH_BAR_LAYER;
775 case TYPE_PHONE:
776 return PHONE_LAYER;
777 case TYPE_KEYGUARD:
778 return KEYGUARD_LAYER;
779 case TYPE_KEYGUARD_DIALOG:
780 return KEYGUARD_DIALOG_LAYER;
781 case TYPE_SYSTEM_ALERT:
782 return SYSTEM_ALERT_LAYER;
783 case TYPE_SYSTEM_ERROR:
784 return SYSTEM_ERROR_LAYER;
785 case TYPE_INPUT_METHOD:
786 return INPUT_METHOD_LAYER;
787 case TYPE_INPUT_METHOD_DIALOG:
788 return INPUT_METHOD_DIALOG_LAYER;
789 case TYPE_SYSTEM_OVERLAY:
790 return SYSTEM_OVERLAY_LAYER;
791 case TYPE_PRIORITY_PHONE:
792 return PRIORITY_PHONE_LAYER;
793 case TYPE_TOAST:
794 return TOAST_LAYER;
Dianne Hackbornb7cd8b92009-08-08 20:53:03 -0700795 case TYPE_WALLPAPER:
796 return WALLPAPER_LAYER;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800797 }
798 Log.e(TAG, "Unknown window type: " + type);
799 return APPLICATION_LAYER;
800 }
801
802 /** {@inheritDoc} */
803 public int subWindowTypeToLayerLw(int type) {
804 switch (type) {
805 case TYPE_APPLICATION_PANEL:
806 case TYPE_APPLICATION_ATTACHED_DIALOG:
807 return APPLICATION_PANEL_SUBLAYER;
808 case TYPE_APPLICATION_MEDIA:
809 return APPLICATION_MEDIA_SUBLAYER;
Dianne Hackborn5cb8d792009-05-21 17:34:15 -0700810 case TYPE_APPLICATION_MEDIA_OVERLAY:
811 return APPLICATION_MEDIA_OVERLAY_SUBLAYER;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800812 case TYPE_APPLICATION_SUB_PANEL:
813 return APPLICATION_SUB_PANEL_SUBLAYER;
814 }
815 Log.e(TAG, "Unknown sub-window type: " + type);
816 return 0;
817 }
818
Dianne Hackbornabe0da02009-09-18 01:55:39 -0700819 public int getMaxWallpaperLayer() {
820 return STATUS_BAR_LAYER;
821 }
822
Dianne Hackborn01ad2f42009-09-24 19:24:56 -0700823 public boolean doesForceHide(WindowState win, WindowManager.LayoutParams attrs) {
824 return attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD;
825 }
826
827 public boolean canBeForceHidden(WindowState win, WindowManager.LayoutParams attrs) {
828 return attrs.type != WindowManager.LayoutParams.TYPE_STATUS_BAR
829 && attrs.type != WindowManager.LayoutParams.TYPE_WALLPAPER;
830 }
831
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800832 /** {@inheritDoc} */
833 public View addStartingWindow(IBinder appToken, String packageName,
834 int theme, CharSequence nonLocalizedLabel,
835 int labelRes, int icon) {
836 if (!SHOW_STARTING_ANIMATIONS) {
837 return null;
838 }
839 if (packageName == null) {
840 return null;
841 }
842
843 Context context = mContext;
844 boolean setTheme = false;
845 //Log.i(TAG, "addStartingWindow " + packageName + ": nonLocalizedLabel="
846 // + nonLocalizedLabel + " theme=" + Integer.toHexString(theme));
847 if (theme != 0 || labelRes != 0) {
848 try {
849 context = context.createPackageContext(packageName, 0);
850 if (theme != 0) {
851 context.setTheme(theme);
852 setTheme = true;
853 }
854 } catch (PackageManager.NameNotFoundException e) {
855 // Ignore
856 }
857 }
858 if (!setTheme) {
859 context.setTheme(com.android.internal.R.style.Theme);
860 }
861
862 Window win = PolicyManager.makeNewWindow(context);
863 if (win.getWindowStyle().getBoolean(
864 com.android.internal.R.styleable.Window_windowDisablePreview, false)) {
865 return null;
866 }
867
868 Resources r = context.getResources();
869 win.setTitle(r.getText(labelRes, nonLocalizedLabel));
870
871 win.setType(
872 WindowManager.LayoutParams.TYPE_APPLICATION_STARTING);
873 // Force the window flags: this is a fake window, so it is not really
874 // touchable or focusable by the user. We also add in the ALT_FOCUSABLE_IM
875 // flag because we do know that the next window will take input
876 // focus, so we want to get the IME window up on top of us right away.
877 win.setFlags(
878 WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE|
879 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|
880 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
881 WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE|
882 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|
883 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
884
Romain Guycc6828c2010-01-08 15:06:37 -0800885 win.setLayout(WindowManager.LayoutParams.MATCH_PARENT,
886 WindowManager.LayoutParams.MATCH_PARENT);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800887
888 final WindowManager.LayoutParams params = win.getAttributes();
889 params.token = appToken;
890 params.packageName = packageName;
891 params.windowAnimations = win.getWindowStyle().getResourceId(
892 com.android.internal.R.styleable.Window_windowAnimationStyle, 0);
893 params.setTitle("Starting " + packageName);
894
895 try {
896 WindowManagerImpl wm = (WindowManagerImpl)
897 context.getSystemService(Context.WINDOW_SERVICE);
898 View view = win.getDecorView();
899
900 if (win.isFloating()) {
901 // Whoops, there is no way to display an animation/preview
902 // of such a thing! After all that work... let's skip it.
903 // (Note that we must do this here because it is in
904 // getDecorView() where the theme is evaluated... maybe
905 // we should peek the floating attribute from the theme
906 // earlier.)
907 return null;
908 }
909
910 if (localLOGV) Log.v(
911 TAG, "Adding starting window for " + packageName
912 + " / " + appToken + ": "
913 + (view.getParent() != null ? view : null));
914
915 wm.addView(view, params);
916
917 // Only return the view if it was successfully added to the
918 // window manager... which we can tell by it having a parent.
919 return view.getParent() != null ? view : null;
920 } catch (WindowManagerImpl.BadTokenException e) {
921 // ignore
922 Log.w(TAG, appToken + " already running, starting window not displayed");
923 }
924
925 return null;
926 }
927
928 /** {@inheritDoc} */
929 public void removeStartingWindow(IBinder appToken, View window) {
930 // RuntimeException e = new RuntimeException();
931 // Log.i(TAG, "remove " + appToken + " " + window, e);
932
933 if (localLOGV) Log.v(
934 TAG, "Removing starting window for " + appToken + ": " + window);
935
936 if (window != null) {
937 WindowManagerImpl wm = (WindowManagerImpl) mContext.getSystemService(Context.WINDOW_SERVICE);
938 wm.removeView(window);
939 }
940 }
941
942 /**
943 * Preflight adding a window to the system.
944 *
945 * Currently enforces that three window types are singletons:
946 * <ul>
947 * <li>STATUS_BAR_TYPE</li>
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800948 * <li>KEYGUARD_TYPE</li>
949 * </ul>
950 *
951 * @param win The window to be added
952 * @param attrs Information about the window to be added
953 *
954 * @return If ok, WindowManagerImpl.ADD_OKAY. If too many singletons, WindowManagerImpl.ADD_MULTIPLE_SINGLETON
955 */
956 public int prepareAddWindowLw(WindowState win, WindowManager.LayoutParams attrs) {
957 switch (attrs.type) {
958 case TYPE_STATUS_BAR:
959 if (mStatusBar != null) {
960 return WindowManagerImpl.ADD_MULTIPLE_SINGLETON;
961 }
962 mStatusBar = win;
963 break;
Dianne Hackborn08743722009-12-21 12:16:51 -0800964 case TYPE_STATUS_BAR_PANEL:
965 mStatusBarPanels.add(win);
966 break;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800967 case TYPE_KEYGUARD:
968 if (mKeyguard != null) {
969 return WindowManagerImpl.ADD_MULTIPLE_SINGLETON;
970 }
971 mKeyguard = win;
972 break;
973 }
974 return WindowManagerImpl.ADD_OKAY;
975 }
976
977 /** {@inheritDoc} */
978 public void removeWindowLw(WindowState win) {
979 if (mStatusBar == win) {
980 mStatusBar = null;
981 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800982 else if (mKeyguard == win) {
983 mKeyguard = null;
Dianne Hackborn08743722009-12-21 12:16:51 -0800984 } else {
985 mStatusBarPanels.remove(win);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800986 }
987 }
988
989 static final boolean PRINT_ANIM = false;
990
991 /** {@inheritDoc} */
992 public int selectAnimationLw(WindowState win, int transit) {
993 if (PRINT_ANIM) Log.i(TAG, "selectAnimation in " + win
994 + ": transit=" + transit);
995 if (transit == TRANSIT_PREVIEW_DONE) {
996 if (win.hasAppShownWindows()) {
997 if (PRINT_ANIM) Log.i(TAG, "**** STARTING EXIT");
998 return com.android.internal.R.anim.app_starting_exit;
999 }
1000 }
1001
1002 return 0;
1003 }
1004
Dianne Hackborn01ad2f42009-09-24 19:24:56 -07001005 public Animation createForceHideEnterAnimation() {
1006 return AnimationUtils.loadAnimation(mContext,
1007 com.android.internal.R.anim.lock_screen_behind_enter);
1008 }
1009
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001010 static ITelephony getPhoneInterface() {
1011 return ITelephony.Stub.asInterface(ServiceManager.checkService(Context.TELEPHONY_SERVICE));
1012 }
1013
1014 static IAudioService getAudioInterface() {
1015 return IAudioService.Stub.asInterface(ServiceManager.checkService(Context.AUDIO_SERVICE));
1016 }
1017
1018 boolean keyguardOn() {
1019 return keyguardIsShowingTq() || inKeyguardRestrictedKeyInputMode();
1020 }
1021
1022 private static final int[] WINDOW_TYPES_WHERE_HOME_DOESNT_WORK = {
1023 WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
1024 WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
1025 };
1026
1027 /** {@inheritDoc} */
1028 public boolean interceptKeyTi(WindowState win, int code, int metaKeys, boolean down,
Dianne Hackborn0041e972009-07-24 17:14:43 -07001029 int repeatCount, int flags) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001030 boolean keyguardOn = keyguardOn();
1031
1032 if (false) {
1033 Log.d(TAG, "interceptKeyTi code=" + code + " down=" + down + " repeatCount="
1034 + repeatCount + " keyguardOn=" + keyguardOn + " mHomePressed=" + mHomePressed);
1035 }
1036
1037 // Clear a pending HOME longpress if the user releases Home
1038 // TODO: This could probably be inside the next bit of logic, but that code
1039 // turned out to be a bit fragile so I'm doing it here explicitly, for now.
1040 if ((code == KeyEvent.KEYCODE_HOME) && !down) {
1041 mHandler.removeCallbacks(mHomeLongPress);
1042 }
1043
1044 // If the HOME button is currently being held, then we do special
1045 // chording with it.
1046 if (mHomePressed) {
1047
1048 // If we have released the home key, and didn't do anything else
1049 // while it was pressed, then it is time to go home!
1050 if (code == KeyEvent.KEYCODE_HOME) {
1051 if (!down) {
1052 mHomePressed = false;
1053
Dianne Hackborn0041e972009-07-24 17:14:43 -07001054 if ((flags&KeyEvent.FLAG_CANCELED) == 0) {
1055 // If an incoming call is ringing, HOME is totally disabled.
1056 // (The user is already on the InCallScreen at this point,
1057 // and his ONLY options are to answer or reject the call.)
1058 boolean incomingRinging = false;
1059 try {
1060 ITelephony phoneServ = getPhoneInterface();
1061 if (phoneServ != null) {
1062 incomingRinging = phoneServ.isRinging();
1063 } else {
1064 Log.w(TAG, "Unable to find ITelephony interface");
1065 }
1066 } catch (RemoteException ex) {
1067 Log.w(TAG, "RemoteException from getPhoneInterface()", ex);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001068 }
Dianne Hackborn0041e972009-07-24 17:14:43 -07001069
1070 if (incomingRinging) {
1071 Log.i(TAG, "Ignoring HOME; there's a ringing incoming call.");
1072 } else {
1073 launchHomeFromHotKey();
1074 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001075 } else {
Dianne Hackborn0041e972009-07-24 17:14:43 -07001076 Log.i(TAG, "Ignoring HOME; event canceled.");
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001077 }
1078 }
1079 }
1080
1081 return true;
1082 }
Dianne Hackborn0041e972009-07-24 17:14:43 -07001083
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001084 // First we always handle the home key here, so applications
1085 // can never break it, although if keyguard is on, we do let
1086 // it handle it, because that gives us the correct 5 second
1087 // timeout.
1088 if (code == KeyEvent.KEYCODE_HOME) {
1089
1090 // If a system window has focus, then it doesn't make sense
1091 // right now to interact with applications.
1092 WindowManager.LayoutParams attrs = win != null ? win.getAttrs() : null;
1093 if (attrs != null) {
1094 final int type = attrs.type;
1095 if (type == WindowManager.LayoutParams.TYPE_KEYGUARD
1096 || type == WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG) {
1097 // the "app" is keyguard, so give it the key
1098 return false;
1099 }
1100 final int typeCount = WINDOW_TYPES_WHERE_HOME_DOESNT_WORK.length;
1101 for (int i=0; i<typeCount; i++) {
1102 if (type == WINDOW_TYPES_WHERE_HOME_DOESNT_WORK[i]) {
1103 // don't do anything, but also don't pass it to the app
1104 return true;
1105 }
1106 }
1107 }
1108
1109 if (down && repeatCount == 0) {
1110 if (!keyguardOn) {
1111 mHandler.postDelayed(mHomeLongPress, ViewConfiguration.getGlobalActionKeyTimeout());
1112 }
1113 mHomePressed = true;
1114 }
1115 return true;
1116 } else if (code == KeyEvent.KEYCODE_MENU) {
1117 // Hijack modified menu keys for debugging features
1118 final int chordBug = KeyEvent.META_SHIFT_ON;
1119
1120 if (down && repeatCount == 0) {
1121 if (mEnableShiftMenuBugReports && (metaKeys & chordBug) == chordBug) {
1122 Intent intent = new Intent(Intent.ACTION_BUG_REPORT);
1123 mContext.sendOrderedBroadcast(intent, null);
1124 return true;
1125 } else if (SHOW_PROCESSES_ON_ALT_MENU &&
1126 (metaKeys & KeyEvent.META_ALT_ON) == KeyEvent.META_ALT_ON) {
1127 Intent service = new Intent();
1128 service.setClassName(mContext, "com.android.server.LoadAverageService");
1129 ContentResolver res = mContext.getContentResolver();
1130 boolean shown = Settings.System.getInt(
1131 res, Settings.System.SHOW_PROCESSES, 0) != 0;
1132 if (!shown) {
1133 mContext.startService(service);
1134 } else {
1135 mContext.stopService(service);
1136 }
1137 Settings.System.putInt(
1138 res, Settings.System.SHOW_PROCESSES, shown ? 0 : 1);
1139 return true;
1140 }
1141 }
1142 } else if (code == KeyEvent.KEYCODE_NOTIFICATION) {
1143 if (down) {
1144 // this key doesn't exist on current hardware, but if a device
1145 // didn't have a touchscreen, it would want one of these to open
1146 // the status bar.
1147 IStatusBar sbs = IStatusBar.Stub.asInterface(ServiceManager.getService("statusbar"));
1148 if (sbs != null) {
1149 try {
1150 sbs.toggle();
1151 } catch (RemoteException e) {
1152 // we're screwed anyway, since it's in this process
1153 throw new RuntimeException(e);
1154 }
1155 }
1156 }
1157 return true;
1158 } else if (code == KeyEvent.KEYCODE_SEARCH) {
1159 if (down) {
1160 if (repeatCount == 0) {
1161 mSearchKeyPressed = true;
1162 }
1163 } else {
1164 mSearchKeyPressed = false;
1165
1166 if (mConsumeSearchKeyUp) {
1167 // Consume the up-event
1168 mConsumeSearchKeyUp = false;
1169 return true;
1170 }
1171 }
1172 }
1173
1174 // Shortcuts are invoked through Search+key, so intercept those here
1175 if (mSearchKeyPressed) {
1176 if (down && repeatCount == 0 && !keyguardOn) {
1177 Intent shortcutIntent = mShortcutManager.getIntent(code, metaKeys);
1178 if (shortcutIntent != null) {
1179 shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1180 mContext.startActivity(shortcutIntent);
1181
1182 /*
1183 * We launched an app, so the up-event of the search key
1184 * should be consumed
1185 */
1186 mConsumeSearchKeyUp = true;
1187 return true;
1188 }
1189 }
1190 }
1191
1192 return false;
1193 }
1194
1195 /**
1196 * A home key -> launch home action was detected. Take the appropriate action
1197 * given the situation with the keyguard.
1198 */
1199 void launchHomeFromHotKey() {
Dianne Hackborn9dc06cc2009-11-17 18:19:23 -08001200 if (mKeyguardMediator.isShowingAndNotHidden()) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001201 // don't launch home if keyguard showing
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001202 } else if (!mHideLockScreen && mKeyguardMediator.isInputRestricted()) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001203 // when in keyguard restricted mode, must first verify unlock
1204 // before launching home
1205 mKeyguardMediator.verifyUnlock(new OnKeyguardExitResult() {
1206 public void onKeyguardExitResult(boolean success) {
1207 if (success) {
Dianne Hackborn256dd3b2009-05-19 18:51:21 -07001208 try {
1209 ActivityManagerNative.getDefault().stopAppSwitches();
1210 } catch (RemoteException e) {
1211 }
Joe Onoratof275f0c2009-11-24 16:11:13 -05001212 sendCloseSystemWindows(SYSTEM_DIALOG_REASON_HOME_KEY);
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001213 startDockOrHome();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001214 }
1215 }
1216 });
1217 } else {
1218 // no keyguard stuff to worry about, just launch home!
Dianne Hackborn256dd3b2009-05-19 18:51:21 -07001219 try {
1220 ActivityManagerNative.getDefault().stopAppSwitches();
1221 } catch (RemoteException e) {
1222 }
Joe Onoratof275f0c2009-11-24 16:11:13 -05001223 sendCloseSystemWindows(SYSTEM_DIALOG_REASON_HOME_KEY);
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001224 startDockOrHome();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001225 }
1226 }
1227
1228 public void getContentInsetHintLw(WindowManager.LayoutParams attrs, Rect contentInset) {
1229 final int fl = attrs.flags;
1230
1231 if ((fl &
1232 (FLAG_LAYOUT_IN_SCREEN | FLAG_FULLSCREEN | FLAG_LAYOUT_INSET_DECOR))
1233 == (FLAG_LAYOUT_IN_SCREEN | FLAG_LAYOUT_INSET_DECOR)) {
1234 contentInset.set(mCurLeft, mCurTop, mW - mCurRight, mH - mCurBottom);
1235 } else {
1236 contentInset.setEmpty();
1237 }
1238 }
1239
1240 /** {@inheritDoc} */
1241 public void beginLayoutLw(int displayWidth, int displayHeight) {
1242 mW = displayWidth;
1243 mH = displayHeight;
1244 mDockLeft = mContentLeft = mCurLeft = 0;
1245 mDockTop = mContentTop = mCurTop = 0;
1246 mDockRight = mContentRight = mCurRight = displayWidth;
1247 mDockBottom = mContentBottom = mCurBottom = displayHeight;
1248 mDockLayer = 0x10000000;
1249
Dianne Hackborn0ecadf72009-03-31 18:00:37 -07001250 mTopFullscreenOpaqueWindowState = null;
1251 mForceStatusBar = false;
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001252 mHideLockScreen = false;
Mike Lockwood28569302010-01-28 11:54:40 -05001253 mAllowLockscreenWhenOn = false;
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001254 mDismissKeyguard = false;
Dianne Hackborn0ecadf72009-03-31 18:00:37 -07001255
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001256 // decide where the status bar goes ahead of time
1257 if (mStatusBar != null) {
1258 final Rect pf = mTmpParentFrame;
1259 final Rect df = mTmpDisplayFrame;
1260 final Rect vf = mTmpVisibleFrame;
1261 pf.left = df.left = vf.left = 0;
1262 pf.top = df.top = vf.top = 0;
1263 pf.right = df.right = vf.right = displayWidth;
1264 pf.bottom = df.bottom = vf.bottom = displayHeight;
1265
1266 mStatusBar.computeFrameLw(pf, df, vf, vf);
1267 if (mStatusBar.isVisibleLw()) {
1268 // If the status bar is hidden, we don't want to cause
1269 // windows behind it to scroll.
1270 mDockTop = mContentTop = mCurTop = mStatusBar.getFrameLw().bottom;
The Android Open Source Project11267662009-03-18 17:39:47 -07001271 if (DEBUG_LAYOUT) Log.v(TAG, "Status bar: mDockBottom="
1272 + mDockBottom + " mContentBottom="
1273 + mContentBottom + " mCurBottom=" + mCurBottom);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001274 }
1275 }
1276 }
1277
1278 void setAttachedWindowFrames(WindowState win, int fl, int sim,
1279 WindowState attached, boolean insetDecors, Rect pf, Rect df, Rect cf, Rect vf) {
1280 if (win.getSurfaceLayer() > mDockLayer && attached.getSurfaceLayer() < mDockLayer) {
1281 // Here's a special case: if this attached window is a panel that is
1282 // above the dock window, and the window it is attached to is below
1283 // the dock window, then the frames we computed for the window it is
1284 // attached to can not be used because the dock is effectively part
1285 // of the underlying window and the attached window is floating on top
1286 // of the whole thing. So, we ignore the attached window and explicitly
1287 // compute the frames that would be appropriate without the dock.
1288 df.left = cf.left = vf.left = mDockLeft;
1289 df.top = cf.top = vf.top = mDockTop;
1290 df.right = cf.right = vf.right = mDockRight;
1291 df.bottom = cf.bottom = vf.bottom = mDockBottom;
1292 } else {
1293 // The effective display frame of the attached window depends on
1294 // whether it is taking care of insetting its content. If not,
1295 // we need to use the parent's content frame so that the entire
1296 // window is positioned within that content. Otherwise we can use
1297 // the display frame and let the attached window take care of
1298 // positioning its content appropriately.
1299 if ((sim & SOFT_INPUT_MASK_ADJUST) != SOFT_INPUT_ADJUST_RESIZE) {
1300 cf.set(attached.getDisplayFrameLw());
1301 } else {
1302 // If the window is resizing, then we want to base the content
1303 // frame on our attached content frame to resize... however,
1304 // things can be tricky if the attached window is NOT in resize
1305 // mode, in which case its content frame will be larger.
1306 // Ungh. So to deal with that, make sure the content frame
1307 // we end up using is not covering the IM dock.
1308 cf.set(attached.getContentFrameLw());
1309 if (attached.getSurfaceLayer() < mDockLayer) {
1310 if (cf.left < mContentLeft) cf.left = mContentLeft;
1311 if (cf.top < mContentTop) cf.top = mContentTop;
1312 if (cf.right > mContentRight) cf.right = mContentRight;
1313 if (cf.bottom > mContentBottom) cf.bottom = mContentBottom;
1314 }
1315 }
1316 df.set(insetDecors ? attached.getDisplayFrameLw() : cf);
1317 vf.set(attached.getVisibleFrameLw());
1318 }
1319 // The LAYOUT_IN_SCREEN flag is used to determine whether the attached
1320 // window should be positioned relative to its parent or the entire
1321 // screen.
1322 pf.set((fl & FLAG_LAYOUT_IN_SCREEN) == 0
1323 ? attached.getFrameLw() : df);
1324 }
1325
1326 /** {@inheritDoc} */
Dianne Hackborn0ecadf72009-03-31 18:00:37 -07001327 public void layoutWindowLw(WindowState win, WindowManager.LayoutParams attrs,
1328 WindowState attached) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001329 // we've already done the status bar
1330 if (win == mStatusBar) {
1331 return;
1332 }
1333
1334 if (false) {
1335 if ("com.google.android.youtube".equals(attrs.packageName)
1336 && attrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
1337 Log.i(TAG, "GOTCHA!");
1338 }
1339 }
1340
1341 final int fl = attrs.flags;
1342 final int sim = attrs.softInputMode;
1343
1344 final Rect pf = mTmpParentFrame;
1345 final Rect df = mTmpDisplayFrame;
1346 final Rect cf = mTmpContentFrame;
1347 final Rect vf = mTmpVisibleFrame;
1348
1349 if (attrs.type == TYPE_INPUT_METHOD) {
1350 pf.left = df.left = cf.left = vf.left = mDockLeft;
1351 pf.top = df.top = cf.top = vf.top = mDockTop;
1352 pf.right = df.right = cf.right = vf.right = mDockRight;
1353 pf.bottom = df.bottom = cf.bottom = vf.bottom = mDockBottom;
1354 // IM dock windows always go to the bottom of the screen.
1355 attrs.gravity = Gravity.BOTTOM;
1356 mDockLayer = win.getSurfaceLayer();
1357 } else {
1358 if ((fl &
1359 (FLAG_LAYOUT_IN_SCREEN | FLAG_FULLSCREEN | FLAG_LAYOUT_INSET_DECOR))
1360 == (FLAG_LAYOUT_IN_SCREEN | FLAG_LAYOUT_INSET_DECOR)) {
1361 // This is the case for a normal activity window: we want it
1362 // to cover all of the screen space, and it can take care of
1363 // moving its contents to account for screen decorations that
1364 // intrude into that space.
1365 if (attached != null) {
1366 // If this window is attached to another, our display
1367 // frame is the same as the one we are attached to.
1368 setAttachedWindowFrames(win, fl, sim, attached, true, pf, df, cf, vf);
1369 } else {
1370 pf.left = df.left = 0;
1371 pf.top = df.top = 0;
1372 pf.right = df.right = mW;
1373 pf.bottom = df.bottom = mH;
1374 if ((sim & SOFT_INPUT_MASK_ADJUST) != SOFT_INPUT_ADJUST_RESIZE) {
1375 cf.left = mDockLeft;
1376 cf.top = mDockTop;
1377 cf.right = mDockRight;
1378 cf.bottom = mDockBottom;
1379 } else {
1380 cf.left = mContentLeft;
1381 cf.top = mContentTop;
1382 cf.right = mContentRight;
1383 cf.bottom = mContentBottom;
1384 }
1385 vf.left = mCurLeft;
1386 vf.top = mCurTop;
1387 vf.right = mCurRight;
1388 vf.bottom = mCurBottom;
1389 }
1390 } else if ((fl & FLAG_LAYOUT_IN_SCREEN) != 0) {
1391 // A window that has requested to fill the entire screen just
1392 // gets everything, period.
1393 pf.left = df.left = cf.left = 0;
1394 pf.top = df.top = cf.top = 0;
1395 pf.right = df.right = cf.right = mW;
1396 pf.bottom = df.bottom = cf.bottom = mH;
1397 vf.left = mCurLeft;
1398 vf.top = mCurTop;
1399 vf.right = mCurRight;
1400 vf.bottom = mCurBottom;
1401 } else if (attached != null) {
1402 // A child window should be placed inside of the same visible
1403 // frame that its parent had.
1404 setAttachedWindowFrames(win, fl, sim, attached, false, pf, df, cf, vf);
1405 } else {
1406 // Otherwise, a normal window must be placed inside the content
1407 // of all screen decorations.
1408 pf.left = mContentLeft;
1409 pf.top = mContentTop;
1410 pf.right = mContentRight;
1411 pf.bottom = mContentBottom;
1412 if ((sim & SOFT_INPUT_MASK_ADJUST) != SOFT_INPUT_ADJUST_RESIZE) {
1413 df.left = cf.left = mDockLeft;
1414 df.top = cf.top = mDockTop;
1415 df.right = cf.right = mDockRight;
1416 df.bottom = cf.bottom = mDockBottom;
1417 } else {
1418 df.left = cf.left = mContentLeft;
1419 df.top = cf.top = mContentTop;
1420 df.right = cf.right = mContentRight;
1421 df.bottom = cf.bottom = mContentBottom;
1422 }
1423 vf.left = mCurLeft;
1424 vf.top = mCurTop;
1425 vf.right = mCurRight;
1426 vf.bottom = mCurBottom;
1427 }
1428 }
1429
1430 if ((fl & FLAG_LAYOUT_NO_LIMITS) != 0) {
1431 df.left = df.top = cf.left = cf.top = vf.left = vf.top = -10000;
1432 df.right = df.bottom = cf.right = cf.bottom = vf.right = vf.bottom = 10000;
1433 }
1434
The Android Open Source Project11267662009-03-18 17:39:47 -07001435 if (DEBUG_LAYOUT) Log.v(TAG, "Compute frame " + attrs.getTitle()
1436 + ": sim=#" + Integer.toHexString(sim)
1437 + " pf=" + pf.toShortString() + " df=" + df.toShortString()
1438 + " cf=" + cf.toShortString() + " vf=" + vf.toShortString());
1439
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001440 if (false) {
1441 if ("com.google.android.youtube".equals(attrs.packageName)
1442 && attrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
1443 if (true || localLOGV) Log.v(TAG, "Computing frame of " + win +
The Android Open Source Project11267662009-03-18 17:39:47 -07001444 ": sim=#" + Integer.toHexString(sim)
1445 + " pf=" + pf.toShortString() + " df=" + df.toShortString()
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001446 + " cf=" + cf.toShortString() + " vf=" + vf.toShortString());
1447 }
1448 }
1449
1450 win.computeFrameLw(pf, df, cf, vf);
1451
Dianne Hackborne7c8c7c2009-10-07 21:27:43 -07001452 if (mTopFullscreenOpaqueWindowState == null &&
1453 win.isVisibleOrBehindKeyguardLw()) {
Dianne Hackborn0ecadf72009-03-31 18:00:37 -07001454 if ((attrs.flags & FLAG_FORCE_NOT_FULLSCREEN) != 0) {
1455 mForceStatusBar = true;
Dianne Hackbornd70bc2f2009-10-06 23:25:51 -07001456 }
Dianne Hackborne7c8c7c2009-10-07 21:27:43 -07001457 if (attrs.type >= FIRST_APPLICATION_WINDOW
Dianne Hackborn0ecadf72009-03-31 18:00:37 -07001458 && attrs.type <= LAST_APPLICATION_WINDOW
1459 && win.fillsScreenLw(mW, mH, false, false)) {
1460 if (DEBUG_LAYOUT) Log.v(TAG, "Fullscreen window: " + win);
1461 mTopFullscreenOpaqueWindowState = win;
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001462 if ((attrs.flags & FLAG_SHOW_WHEN_LOCKED) != 0) {
Dianne Hackborn05726582009-09-22 17:28:24 -07001463 if (localLOGV) Log.v(TAG, "Setting mHideLockScreen to true by win " + win);
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001464 mHideLockScreen = true;
1465 }
Dianne Hackborn0ecadf72009-03-31 18:00:37 -07001466 }
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001467 if ((attrs.flags & FLAG_DISMISS_KEYGUARD) != 0) {
Dianne Hackborn05726582009-09-22 17:28:24 -07001468 if (localLOGV) Log.v(TAG, "Setting mDismissKeyguard to true by win " + win);
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001469 mDismissKeyguard = true;
Suchi Amalapurapu9cdc9032009-05-14 18:01:07 -07001470 }
Mike Lockwood28569302010-01-28 11:54:40 -05001471 if ((attrs.flags & FLAG_ALLOW_LOCK_WHILE_SCREEN_ON) != 0) {
1472 mAllowLockscreenWhenOn = true;
1473 }
Dianne Hackborn0ecadf72009-03-31 18:00:37 -07001474 }
1475
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001476 // Dock windows carve out the bottom of the screen, so normal windows
1477 // can't appear underneath them.
1478 if (attrs.type == TYPE_INPUT_METHOD && !win.getGivenInsetsPendingLw()) {
1479 int top = win.getContentFrameLw().top;
1480 top += win.getGivenContentInsetsLw().top;
1481 if (mContentBottom > top) {
1482 mContentBottom = top;
1483 }
1484 top = win.getVisibleFrameLw().top;
1485 top += win.getGivenVisibleInsetsLw().top;
1486 if (mCurBottom > top) {
1487 mCurBottom = top;
1488 }
The Android Open Source Project11267662009-03-18 17:39:47 -07001489 if (DEBUG_LAYOUT) Log.v(TAG, "Input method: mDockBottom="
1490 + mDockBottom + " mContentBottom="
1491 + mContentBottom + " mCurBottom=" + mCurBottom);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001492 }
1493 }
1494
1495 /** {@inheritDoc} */
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001496 public int finishLayoutLw() {
1497 int changes = 0;
The Android Open Source Projectc84bf282009-03-09 11:52:14 -07001498 boolean hiding = false;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001499 if (mStatusBar != null) {
Dianne Hackbornd70bc2f2009-10-06 23:25:51 -07001500 if (localLOGV) Log.i(TAG, "force=" + mForceStatusBar
1501 + " top=" + mTopFullscreenOpaqueWindowState);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001502 if (mForceStatusBar) {
Dianne Hackborn0ecadf72009-03-31 18:00:37 -07001503 if (DEBUG_LAYOUT) Log.v(TAG, "Showing status bar");
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001504 if (mStatusBar.showLw(true)) changes |= FINISH_LAYOUT_REDO_LAYOUT;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001505 } else if (mTopFullscreenOpaqueWindowState != null) {
1506 //Log.i(TAG, "frame: " + mTopFullscreenOpaqueWindowState.getFrameLw()
1507 // + " shown frame: " + mTopFullscreenOpaqueWindowState.getShownFrameLw());
1508 //Log.i(TAG, "attr: " + mTopFullscreenOpaqueWindowState.getAttrs());
1509 WindowManager.LayoutParams lp =
1510 mTopFullscreenOpaqueWindowState.getAttrs();
1511 boolean hideStatusBar =
1512 (lp.flags & WindowManager.LayoutParams.FLAG_FULLSCREEN) != 0;
1513 if (hideStatusBar) {
Dianne Hackborn0ecadf72009-03-31 18:00:37 -07001514 if (DEBUG_LAYOUT) Log.v(TAG, "Hiding status bar");
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001515 if (mStatusBar.hideLw(true)) changes |= FINISH_LAYOUT_REDO_LAYOUT;
The Android Open Source Projectc84bf282009-03-09 11:52:14 -07001516 hiding = true;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001517 } else {
Dianne Hackborn0ecadf72009-03-31 18:00:37 -07001518 if (DEBUG_LAYOUT) Log.v(TAG, "Showing status bar");
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001519 if (mStatusBar.showLw(true)) changes |= FINISH_LAYOUT_REDO_LAYOUT;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001520 }
1521 }
1522 }
Suchi Amalapurapu9cdc9032009-05-14 18:01:07 -07001523 // Hide the key guard if a visible window explicitly specifies that it wants to be displayed
1524 // when the screen is locked
1525 if (mKeyguard != null) {
Dianne Hackborn05726582009-09-22 17:28:24 -07001526 if (localLOGV) Log.v(TAG, "finishLayoutLw::mHideKeyguard="+mHideLockScreen);
1527 if (mDismissKeyguard && !mKeyguardMediator.isSecure()) {
Dianne Hackborn08743722009-12-21 12:16:51 -08001528 if (mKeyguard.hideLw(true)) {
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001529 changes |= FINISH_LAYOUT_REDO_LAYOUT
1530 | FINISH_LAYOUT_REDO_CONFIG
1531 | FINISH_LAYOUT_REDO_WALLPAPER;
1532 }
Dianne Hackborn05726582009-09-22 17:28:24 -07001533 if (mKeyguardMediator.isShowing()) {
Dianne Hackbornb446e972009-09-20 15:23:25 -07001534 mHandler.post(new Runnable() {
1535 public void run() {
Dianne Hackborn05726582009-09-22 17:28:24 -07001536 mKeyguardMediator.keyguardDone(false, false);
Dianne Hackbornb446e972009-09-20 15:23:25 -07001537 }
1538 });
1539 }
Dianne Hackborn05726582009-09-22 17:28:24 -07001540 } else if (mHideLockScreen) {
Dianne Hackborn08743722009-12-21 12:16:51 -08001541 if (mKeyguard.hideLw(true)) {
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001542 changes |= FINISH_LAYOUT_REDO_LAYOUT
1543 | FINISH_LAYOUT_REDO_CONFIG
1544 | FINISH_LAYOUT_REDO_WALLPAPER;
1545 }
Mike Lockwoodf3bfed52010-01-21 16:38:44 -05001546 mKeyguardMediator.setHidden(true);
Suchi Amalapurapu9cdc9032009-05-14 18:01:07 -07001547 } else {
Dianne Hackborn08743722009-12-21 12:16:51 -08001548 if (mKeyguard.showLw(true)) {
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001549 changes |= FINISH_LAYOUT_REDO_LAYOUT
1550 | FINISH_LAYOUT_REDO_CONFIG
1551 | FINISH_LAYOUT_REDO_WALLPAPER;
1552 }
Mike Lockwoodf3bfed52010-01-21 16:38:44 -05001553 mKeyguardMediator.setHidden(false);
Suchi Amalapurapu9cdc9032009-05-14 18:01:07 -07001554 }
1555 }
The Android Open Source Projectc84bf282009-03-09 11:52:14 -07001556
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001557 if (changes != 0 && hiding) {
The Android Open Source Projectc84bf282009-03-09 11:52:14 -07001558 IStatusBar sbs = IStatusBar.Stub.asInterface(ServiceManager.getService("statusbar"));
1559 if (sbs != null) {
1560 try {
1561 // Make sure the window shade is hidden.
1562 sbs.deactivate();
1563 } catch (RemoteException e) {
1564 }
1565 }
1566 }
Mike Lockwood28569302010-01-28 11:54:40 -05001567
1568 // update since mAllowLockscreenWhenOn might have changed
1569 updateLockScreenTimeout();
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001570 return changes;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001571 }
1572
1573 /** {@inheritDoc} */
Dianne Hackborn0ecadf72009-03-31 18:00:37 -07001574 public void beginAnimationLw(int displayWidth, int displayHeight) {
1575 }
1576
1577 /** {@inheritDoc} */
1578 public void animatingWindowLw(WindowState win,
1579 WindowManager.LayoutParams attrs) {
1580 }
1581
1582 /** {@inheritDoc} */
1583 public boolean finishAnimationLw() {
1584 return false;
1585 }
1586
Dianne Hackborn08743722009-12-21 12:16:51 -08001587 public boolean allowAppAnimationsLw() {
1588 if (mKeyguard != null && mKeyguard.isVisibleLw()) {
1589 // If keyguard is currently visible, no reason to animate
1590 // behind it.
1591 return false;
1592 }
1593 if (mStatusBar != null && mStatusBar.isVisibleLw()) {
1594 Rect rect = new Rect(mStatusBar.getShownFrameLw());
1595 for (int i=mStatusBarPanels.size()-1; i>=0; i--) {
1596 WindowState w = mStatusBarPanels.get(i);
1597 if (w.isVisibleLw()) {
1598 rect.union(w.getShownFrameLw());
1599 }
1600 }
1601 final int insetw = mW/10;
1602 final int inseth = mH/10;
1603 if (rect.contains(insetw, inseth, mW-insetw, mH-inseth)) {
1604 // All of the status bar windows put together cover the
1605 // screen, so the app can't be seen. (Note this test doesn't
1606 // work if the rects of these windows are at off offsets or
1607 // sizes, causing gaps in the rect union we have computed.)
1608 return false;
1609 }
1610 }
1611 return true;
1612 }
1613
Dianne Hackborn0ecadf72009-03-31 18:00:37 -07001614 /** {@inheritDoc} */
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001615 public boolean preprocessInputEventTq(RawInputEvent event) {
1616 switch (event.type) {
1617 case RawInputEvent.EV_SW:
Mike Lockwood1753f7f2009-08-24 14:49:07 -07001618 if (event.keycode == RawInputEvent.SW_LID) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001619 // lid changed state
1620 mLidOpen = event.value == 0;
Dianne Hackbornb446e972009-09-20 15:23:25 -07001621 boolean awakeNow = mKeyguardMediator.doLidChangeTq(mLidOpen);
Dianne Hackborn32bc91d2009-03-27 16:16:03 -07001622 updateRotation(Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
Dianne Hackbornb446e972009-09-20 15:23:25 -07001623 if (awakeNow) {
1624 // If the lid opening and we don't have to keep the
1625 // keyguard up, then we can turn on the screen
1626 // immediately.
1627 mKeyguardMediator.pokeWakelock();
1628 } else if (keyguardIsShowingTq()) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001629 if (mLidOpen) {
Dianne Hackbornb446e972009-09-20 15:23:25 -07001630 // If we are opening the lid and not hiding the
1631 // keyguard, then we need to have it turn on the
1632 // screen once it is shown.
1633 mKeyguardMediator.onWakeKeyWhenKeyguardShowingTq(
1634 KeyEvent.KEYCODE_POWER);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001635 }
1636 } else {
1637 // Light up the keyboard if we are sliding up.
1638 if (mLidOpen) {
1639 mPowerManager.userActivity(SystemClock.uptimeMillis(), false,
1640 LocalPowerManager.BUTTON_EVENT);
1641 } else {
1642 mPowerManager.userActivity(SystemClock.uptimeMillis(), false,
1643 LocalPowerManager.OTHER_EVENT);
1644 }
1645 }
1646 }
1647 }
1648 return false;
1649 }
1650
1651
1652 /** {@inheritDoc} */
1653 public boolean isAppSwitchKeyTqTiLwLi(int keycode) {
1654 return keycode == KeyEvent.KEYCODE_HOME
1655 || keycode == KeyEvent.KEYCODE_ENDCALL;
1656 }
1657
1658 /** {@inheritDoc} */
1659 public boolean isMovementKeyTi(int keycode) {
1660 switch (keycode) {
1661 case KeyEvent.KEYCODE_DPAD_UP:
1662 case KeyEvent.KEYCODE_DPAD_DOWN:
1663 case KeyEvent.KEYCODE_DPAD_LEFT:
1664 case KeyEvent.KEYCODE_DPAD_RIGHT:
1665 return true;
1666 }
1667 return false;
1668 }
1669
1670
1671 /**
1672 * @return Whether a telephone call is in progress right now.
1673 */
1674 boolean isInCall() {
1675 final ITelephony phone = getPhoneInterface();
1676 if (phone == null) {
1677 Log.w(TAG, "couldn't get ITelephony reference");
1678 return false;
1679 }
1680 try {
1681 return phone.isOffhook();
1682 } catch (RemoteException e) {
1683 Log.w(TAG, "ITelephony.isOffhhook threw RemoteException " + e);
1684 return false;
1685 }
1686 }
1687
1688 /**
1689 * @return Whether music is being played right now.
1690 */
1691 boolean isMusicActive() {
Eric Laurent413cb9d2009-07-17 11:52:43 -07001692 final AudioManager am = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
1693 if (am == null) {
1694 Log.w(TAG, "isMusicActive: couldn't get AudioManager reference");
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001695 return false;
1696 }
Eric Laurent413cb9d2009-07-17 11:52:43 -07001697 return am.isMusicActive();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001698 }
1699
1700 /**
1701 * Tell the audio service to adjust the volume appropriate to the event.
1702 * @param keycode
1703 */
Mike Lockwooda53e3812009-09-25 10:51:39 -04001704 void handleVolumeKey(int stream, int keycode) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001705 final IAudioService audio = getAudioInterface();
1706 if (audio == null) {
Mike Lockwooda53e3812009-09-25 10:51:39 -04001707 Log.w(TAG, "handleVolumeKey: couldn't get IAudioService reference");
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001708 return;
1709 }
1710 try {
1711 // since audio is playing, we shouldn't have to hold a wake lock
1712 // during the call, but we do it as a precaution for the rare possibility
1713 // that the music stops right before we call this
1714 mBroadcastWakeLock.acquire();
Mike Lockwooda53e3812009-09-25 10:51:39 -04001715 audio.adjustStreamVolume(stream,
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001716 keycode == KeyEvent.KEYCODE_VOLUME_UP
1717 ? AudioManager.ADJUST_RAISE
1718 : AudioManager.ADJUST_LOWER,
1719 0);
1720 } catch (RemoteException e) {
1721 Log.w(TAG, "IAudioService.adjustStreamVolume() threw RemoteException " + e);
1722 } finally {
1723 mBroadcastWakeLock.release();
1724 }
1725 }
1726
1727 static boolean isMediaKey(int code) {
1728 if (code == KeyEvent.KEYCODE_HEADSETHOOK ||
Andy Stadler8b89d692009-04-10 16:24:49 -07001729 code == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE ||
1730 code == KeyEvent.KEYCODE_MEDIA_STOP ||
1731 code == KeyEvent.KEYCODE_MEDIA_NEXT ||
1732 code == KeyEvent.KEYCODE_MEDIA_PREVIOUS ||
Johan Redestig2e2a3d02010-01-05 08:45:33 +01001733 code == KeyEvent.KEYCODE_MEDIA_REWIND ||
Andy Stadler8b89d692009-04-10 16:24:49 -07001734 code == KeyEvent.KEYCODE_MEDIA_FAST_FORWARD) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001735 return true;
1736 }
1737 return false;
1738 }
1739
1740 /** {@inheritDoc} */
1741 public int interceptKeyTq(RawInputEvent event, boolean screenIsOn) {
1742 int result = ACTION_PASS_TO_USER;
1743 final boolean isWakeKey = isWakeKeyTq(event);
Mike Lockwoodc72b15f2009-11-28 20:58:22 -05001744 // If screen is off then we treat the case where the keyguard is open but hidden
1745 // the same as if it were open and in front.
1746 // This will prevent any keys other than the power button from waking the screen
1747 // when the keyguard is hidden by another activity.
1748 final boolean keyguardActive = (screenIsOn ?
1749 mKeyguardMediator.isShowingAndNotHidden() :
1750 mKeyguardMediator.isShowing());
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001751
1752 if (false) {
1753 Log.d(TAG, "interceptKeyTq event=" + event + " keycode=" + event.keycode
Mike Lockwoodc72b15f2009-11-28 20:58:22 -05001754 + " screenIsOn=" + screenIsOn + " keyguardActive=" + keyguardActive);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001755 }
1756
Mike Lockwoodc72b15f2009-11-28 20:58:22 -05001757 if (keyguardActive) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001758 if (screenIsOn) {
1759 // when the screen is on, always give the event to the keyguard
1760 result |= ACTION_PASS_TO_USER;
1761 } else {
1762 // otherwise, don't pass it to the user
1763 result &= ~ACTION_PASS_TO_USER;
1764
1765 final boolean isKeyDown =
1766 (event.type == RawInputEvent.EV_KEY) && (event.value != 0);
1767 if (isWakeKey && isKeyDown) {
1768
1769 // tell the mediator about a wake key, it may decide to
1770 // turn on the screen depending on whether the key is
1771 // appropriate.
1772 if (!mKeyguardMediator.onWakeKeyWhenKeyguardShowingTq(event.keycode)
1773 && (event.keycode == KeyEvent.KEYCODE_VOLUME_DOWN
1774 || event.keycode == KeyEvent.KEYCODE_VOLUME_UP)) {
Mike Lockwood99ff0a82009-12-02 01:37:38 -05001775 // when keyguard is showing and screen off, we need
1776 // to handle the volume key for calls and music here
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001777 if (isInCall()) {
Mike Lockwood99ff0a82009-12-02 01:37:38 -05001778 handleVolumeKey(AudioManager.STREAM_VOICE_CALL, event.keycode);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001779 } else if (isMusicActive()) {
Mike Lockwooda53e3812009-09-25 10:51:39 -04001780 handleVolumeKey(AudioManager.STREAM_MUSIC, event.keycode);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001781 }
1782 }
1783 }
1784 }
1785 } else if (!screenIsOn) {
Mike Lockwooda53e3812009-09-25 10:51:39 -04001786 // If we are in-call with screen off and keyguard is not showing,
1787 // then handle the volume key ourselves.
1788 // This is necessary because the phone app will disable the keyguard
1789 // when the proximity sensor is in use.
1790 if (isInCall() && event.type == RawInputEvent.EV_KEY &&
1791 (event.keycode == KeyEvent.KEYCODE_VOLUME_DOWN
1792 || event.keycode == KeyEvent.KEYCODE_VOLUME_UP)) {
1793 result &= ~ACTION_PASS_TO_USER;
1794 handleVolumeKey(AudioManager.STREAM_VOICE_CALL, event.keycode);
1795 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001796 if (isWakeKey) {
1797 // a wake key has a sole purpose of waking the device; don't pass
1798 // it to the user
1799 result |= ACTION_POKE_USER_ACTIVITY;
1800 result &= ~ACTION_PASS_TO_USER;
1801 }
1802 }
1803
1804 int type = event.type;
1805 int code = event.keycode;
1806 boolean down = event.value != 0;
1807
1808 if (type == RawInputEvent.EV_KEY) {
Dianne Hackborn0041e972009-07-24 17:14:43 -07001809 if (code == KeyEvent.KEYCODE_ENDCALL
1810 || code == KeyEvent.KEYCODE_POWER) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001811 if (down) {
Mike Lockwood2eb47962009-10-23 11:29:52 -04001812 boolean handled = false;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001813 // key repeats are generated by the window manager, and we don't see them
1814 // here, so unless the driver is doing something it shouldn't be, we know
1815 // this is the real press event.
Mike Lockwood75d3caa2009-10-22 13:14:31 -04001816 ITelephony phoneServ = getPhoneInterface();
1817 if (phoneServ != null) {
Dianne Hackborn0041e972009-07-24 17:14:43 -07001818 try {
Mike Lockwood2eb47962009-10-23 11:29:52 -04001819 if (code == KeyEvent.KEYCODE_ENDCALL) {
1820 handled = phoneServ.endCall();
1821 } else if (code == KeyEvent.KEYCODE_POWER && phoneServ.isRinging()) {
1822 // Pressing power during incoming call should silence the ringer
1823 phoneServ.silenceRinger();
1824 handled = true;
Dianne Hackborn0041e972009-07-24 17:14:43 -07001825 }
1826 } catch (RemoteException ex) {
Mike Lockwood75d3caa2009-10-22 13:14:31 -04001827 Log.w(TAG, "ITelephony threw RemoteException" + ex);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001828 }
Mike Lockwood75d3caa2009-10-22 13:14:31 -04001829 } else {
1830 Log.w(TAG, "!!! Unable to find ITelephony interface !!!");
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001831 }
Mike Lockwood75d3caa2009-10-22 13:14:31 -04001832 // power button should turn off screen in addition to hanging up the phone
Mike Lockwood2eb47962009-10-23 11:29:52 -04001833 if ((handled && code != KeyEvent.KEYCODE_POWER) || !screenIsOn) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001834 mShouldTurnOffOnKeyUp = false;
1835 } else {
1836 // only try to turn off the screen if we didn't already hang up
1837 mShouldTurnOffOnKeyUp = true;
Dianne Hackborn0041e972009-07-24 17:14:43 -07001838 mHandler.postDelayed(mPowerLongPress,
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001839 ViewConfiguration.getGlobalActionKeyTimeout());
1840 result &= ~ACTION_PASS_TO_USER;
1841 }
1842 } else {
Dianne Hackborn0041e972009-07-24 17:14:43 -07001843 mHandler.removeCallbacks(mPowerLongPress);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001844 if (mShouldTurnOffOnKeyUp) {
1845 mShouldTurnOffOnKeyUp = false;
Mike Lockwoodb4e2d1c2009-11-23 15:54:10 -05001846 boolean gohome, sleeps;
1847 if (code == KeyEvent.KEYCODE_ENDCALL) {
1848 gohome = (mEndcallBehavior & ENDCALL_HOME) != 0;
1849 sleeps = (mEndcallBehavior & ENDCALL_SLEEPS) != 0;
1850 } else {
1851 gohome = false;
1852 sleeps = true;
1853 }
Mike Lockwoodc72b15f2009-11-28 20:58:22 -05001854 if (keyguardActive
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001855 || (sleeps && !gohome)
1856 || (gohome && !goHome() && sleeps)) {
1857 // they must already be on the keyguad or home screen,
1858 // go to sleep instead
1859 Log.d(TAG, "I'm tired mEndcallBehavior=0x"
1860 + Integer.toHexString(mEndcallBehavior));
1861 result &= ~ACTION_POKE_USER_ACTIVITY;
1862 result |= ACTION_GO_TO_SLEEP;
1863 }
1864 result &= ~ACTION_PASS_TO_USER;
1865 }
1866 }
1867 } else if (isMediaKey(code)) {
1868 // This key needs to be handled even if the screen is off.
1869 // If others need to be handled while it's off, this is a reasonable
1870 // pattern to follow.
1871 if ((result & ACTION_PASS_TO_USER) == 0) {
1872 // Only do this if we would otherwise not pass it to the user. In that
1873 // case, the PhoneWindow class will do the same thing, except it will
1874 // only do it if the showing app doesn't process the key on its own.
1875 KeyEvent keyEvent = new KeyEvent(event.when, event.when,
1876 down ? KeyEvent.ACTION_DOWN : KeyEvent.ACTION_UP,
1877 code, 0);
1878 mBroadcastWakeLock.acquire();
1879 mHandler.post(new PassHeadsetKey(keyEvent));
1880 }
1881 } else if (code == KeyEvent.KEYCODE_CALL) {
1882 // If an incoming call is ringing, answer it!
1883 // (We handle this key here, rather than in the InCallScreen, to make
1884 // sure we'll respond to the key even if the InCallScreen hasn't come to
1885 // the foreground yet.)
1886
1887 // We answer the call on the DOWN event, to agree with
1888 // the "fallback" behavior in the InCallScreen.
1889 if (down) {
1890 try {
1891 ITelephony phoneServ = getPhoneInterface();
1892 if (phoneServ != null) {
1893 if (phoneServ.isRinging()) {
1894 Log.i(TAG, "interceptKeyTq:"
1895 + " CALL key-down while ringing: Answer the call!");
1896 phoneServ.answerRingingCall();
1897
1898 // And *don't* pass this key thru to the current activity
1899 // (which is presumably the InCallScreen.)
1900 result &= ~ACTION_PASS_TO_USER;
1901 }
1902 } else {
1903 Log.w(TAG, "CALL button: Unable to find ITelephony interface");
1904 }
1905 } catch (RemoteException ex) {
1906 Log.w(TAG, "CALL button: RemoteException from getPhoneInterface()", ex);
1907 }
1908 }
1909 } else if ((code == KeyEvent.KEYCODE_VOLUME_UP)
1910 || (code == KeyEvent.KEYCODE_VOLUME_DOWN)) {
1911 // If an incoming call is ringing, either VOLUME key means
1912 // "silence ringer". We handle these keys here, rather than
1913 // in the InCallScreen, to make sure we'll respond to them
1914 // even if the InCallScreen hasn't come to the foreground yet.
1915
1916 // Look for the DOWN event here, to agree with the "fallback"
1917 // behavior in the InCallScreen.
1918 if (down) {
1919 try {
1920 ITelephony phoneServ = getPhoneInterface();
1921 if (phoneServ != null) {
1922 if (phoneServ.isRinging()) {
1923 Log.i(TAG, "interceptKeyTq:"
1924 + " VOLUME key-down while ringing: Silence ringer!");
1925 // Silence the ringer. (It's safe to call this
1926 // even if the ringer has already been silenced.)
1927 phoneServ.silenceRinger();
1928
1929 // And *don't* pass this key thru to the current activity
1930 // (which is probably the InCallScreen.)
1931 result &= ~ACTION_PASS_TO_USER;
1932 }
1933 } else {
1934 Log.w(TAG, "VOLUME button: Unable to find ITelephony interface");
1935 }
1936 } catch (RemoteException ex) {
1937 Log.w(TAG, "VOLUME button: RemoteException from getPhoneInterface()", ex);
1938 }
1939 }
1940 }
1941 }
1942
1943 return result;
1944 }
1945
1946 class PassHeadsetKey implements Runnable {
1947 KeyEvent mKeyEvent;
1948
1949 PassHeadsetKey(KeyEvent keyEvent) {
1950 mKeyEvent = keyEvent;
1951 }
1952
1953 public void run() {
1954 if (ActivityManagerNative.isSystemReady()) {
1955 Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
1956 intent.putExtra(Intent.EXTRA_KEY_EVENT, mKeyEvent);
1957 mContext.sendOrderedBroadcast(intent, null, mBroadcastDone,
1958 mHandler, Activity.RESULT_OK, null, null);
1959 }
1960 }
1961 }
1962
1963 BroadcastReceiver mBroadcastDone = new BroadcastReceiver() {
1964 public void onReceive(Context context, Intent intent) {
1965 mBroadcastWakeLock.release();
1966 }
1967 };
Mike Lockwood1753f7f2009-08-24 14:49:07 -07001968
Dianne Hackborn80fa1662009-10-07 14:02:10 -07001969 BroadcastReceiver mBatteryReceiver = new BroadcastReceiver() {
Dianne Hackbornf54c8f92009-09-19 14:03:57 -07001970 public void onReceive(Context context, Intent intent) {
Dianne Hackborn80fa1662009-10-07 14:02:10 -07001971 updatePlugged(intent);
1972 updateDockKeepingScreenOn();
Dianne Hackbornf54c8f92009-09-19 14:03:57 -07001973 }
1974 };
1975
Mike Lockwood1753f7f2009-08-24 14:49:07 -07001976 BroadcastReceiver mDockReceiver = new BroadcastReceiver() {
1977 public void onReceive(Context context, Intent intent) {
Mike Lockwood2d1efbd2009-09-16 12:15:53 -04001978 mDockState = intent.getIntExtra(Intent.EXTRA_DOCK_STATE,
Mike Lockwood1753f7f2009-08-24 14:49:07 -07001979 Intent.EXTRA_DOCK_STATE_UNDOCKED);
Dianne Hackborn80fa1662009-10-07 14:02:10 -07001980 boolean watchBattery = mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED;
1981 if (watchBattery != mRegisteredBatteryReceiver) {
1982 mRegisteredBatteryReceiver = watchBattery;
1983 if (watchBattery) {
1984 updatePlugged(mContext.registerReceiver(mBatteryReceiver,
1985 mBatteryStatusFilter));
1986 } else {
1987 mContext.unregisterReceiver(mBatteryReceiver);
1988 }
1989 }
Mike Lockwood1753f7f2009-08-24 14:49:07 -07001990 updateRotation(Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
Dianne Hackborn80fa1662009-10-07 14:02:10 -07001991 updateDockKeepingScreenOn();
Mike Lockwoode9867d22009-09-20 01:59:02 -04001992 updateOrientationListenerLp();
Mike Lockwood1753f7f2009-08-24 14:49:07 -07001993 }
1994 };
1995
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001996 /** {@inheritDoc} */
1997 public boolean isWakeRelMovementTq(int device, int classes,
1998 RawInputEvent event) {
1999 // if it's tagged with one of the wake bits, it wakes up the device
2000 return ((event.flags & (FLAG_WAKE | FLAG_WAKE_DROPPED)) != 0);
2001 }
2002
2003 /** {@inheritDoc} */
2004 public boolean isWakeAbsMovementTq(int device, int classes,
2005 RawInputEvent event) {
2006 // if it's tagged with one of the wake bits, it wakes up the device
2007 return ((event.flags & (FLAG_WAKE | FLAG_WAKE_DROPPED)) != 0);
2008 }
2009
2010 /**
2011 * Given the current state of the world, should this key wake up the device?
2012 */
2013 protected boolean isWakeKeyTq(RawInputEvent event) {
2014 // There are not key maps for trackball devices, but we'd still
2015 // like to have pressing it wake the device up, so force it here.
2016 int keycode = event.keycode;
2017 int flags = event.flags;
2018 if (keycode == RawInputEvent.BTN_MOUSE) {
2019 flags |= WindowManagerPolicy.FLAG_WAKE;
2020 }
2021 return (flags
2022 & (WindowManagerPolicy.FLAG_WAKE | WindowManagerPolicy.FLAG_WAKE_DROPPED)) != 0;
2023 }
2024
2025 /** {@inheritDoc} */
2026 public void screenTurnedOff(int why) {
Dianne Hackborn74489012009-03-24 20:50:09 -07002027 EventLog.writeEvent(70000, 0);
2028 mKeyguardMediator.onScreenTurnedOff(why);
The Android Open Source Project0727d222009-03-11 12:11:58 -07002029 synchronized (mLock) {
The Android Open Source Project0727d222009-03-11 12:11:58 -07002030 mScreenOn = false;
2031 updateOrientationListenerLp();
Mike Lockwood28569302010-01-28 11:54:40 -05002032 updateLockScreenTimeout();
The Android Open Source Project0727d222009-03-11 12:11:58 -07002033 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002034 }
2035
2036 /** {@inheritDoc} */
2037 public void screenTurnedOn() {
Dianne Hackborn74489012009-03-24 20:50:09 -07002038 EventLog.writeEvent(70000, 1);
2039 mKeyguardMediator.onScreenTurnedOn();
The Android Open Source Project0727d222009-03-11 12:11:58 -07002040 synchronized (mLock) {
The Android Open Source Project0727d222009-03-11 12:11:58 -07002041 mScreenOn = true;
2042 updateOrientationListenerLp();
Mike Lockwood28569302010-01-28 11:54:40 -05002043 updateLockScreenTimeout();
The Android Open Source Project0727d222009-03-11 12:11:58 -07002044 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002045 }
2046
2047 /** {@inheritDoc} */
Dianne Hackborn08743722009-12-21 12:16:51 -08002048 public boolean isScreenOn() {
2049 return mScreenOn;
2050 }
2051
2052 /** {@inheritDoc} */
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002053 public void enableKeyguard(boolean enabled) {
2054 mKeyguardMediator.setKeyguardEnabled(enabled);
2055 }
2056
2057 /** {@inheritDoc} */
2058 public void exitKeyguardSecurely(OnKeyguardExitResult callback) {
2059 mKeyguardMediator.verifyUnlock(callback);
2060 }
2061
Mike Lockwoodf7913302009-11-28 22:27:10 -05002062 private boolean keyguardIsShowingTq() {
Dianne Hackborn9dc06cc2009-11-17 18:19:23 -08002063 return mKeyguardMediator.isShowingAndNotHidden();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002064 }
2065
2066 /** {@inheritDoc} */
2067 public boolean inKeyguardRestrictedKeyInputMode() {
2068 return mKeyguardMediator.isInputRestricted();
2069 }
2070
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002071 void sendCloseSystemWindows() {
2072 sendCloseSystemWindows(mContext, null);
2073 }
2074
2075 void sendCloseSystemWindows(String reason) {
2076 sendCloseSystemWindows(mContext, reason);
2077 }
2078
2079 static void sendCloseSystemWindows(Context context, String reason) {
2080 if (ActivityManagerNative.isSystemReady()) {
Dianne Hackborn94a679d2009-07-28 17:51:07 -07002081 try {
2082 ActivityManagerNative.getDefault().closeSystemDialogs(reason);
2083 } catch (RemoteException e) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002084 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002085 }
2086 }
2087
The Android Open Source Project0727d222009-03-11 12:11:58 -07002088 public int rotationForOrientationLw(int orientation, int lastRotation,
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002089 boolean displayEnabled) {
Mitsuru Oshima831d0d92009-06-16 18:27:18 -07002090
2091 if (mPortraitRotation < 0) {
2092 // Initialize the rotation angles for each orientation once.
2093 Display d = ((WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE))
2094 .getDefaultDisplay();
2095 if (d.getWidth() > d.getHeight()) {
2096 mPortraitRotation = Surface.ROTATION_90;
2097 mLandscapeRotation = Surface.ROTATION_0;
2098 } else {
2099 mPortraitRotation = Surface.ROTATION_0;
2100 mLandscapeRotation = Surface.ROTATION_90;
2101 }
2102 }
2103
The Android Open Source Project0727d222009-03-11 12:11:58 -07002104 synchronized (mLock) {
2105 switch (orientation) {
2106 case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:
2107 //always return landscape if orientation set to landscape
Mitsuru Oshima831d0d92009-06-16 18:27:18 -07002108 return mLandscapeRotation;
The Android Open Source Project0727d222009-03-11 12:11:58 -07002109 case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT:
2110 //always return portrait if orientation set to portrait
Mitsuru Oshima831d0d92009-06-16 18:27:18 -07002111 return mPortraitRotation;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002112 }
The Android Open Source Project0727d222009-03-11 12:11:58 -07002113 // case for nosensor meaning ignore sensor and consider only lid
2114 // or orientation sensor disabled
2115 //or case.unspecified
2116 if (mLidOpen) {
Mike Lockwood1753f7f2009-08-24 14:49:07 -07002117 return mLidOpenRotation;
Mike Lockwood2d1efbd2009-09-16 12:15:53 -04002118 } else if (mDockState == Intent.EXTRA_DOCK_STATE_CAR && mCarDockRotation >= 0) {
2119 return mCarDockRotation;
2120 } else if (mDockState == Intent.EXTRA_DOCK_STATE_DESK && mDeskDockRotation >= 0) {
2121 return mDeskDockRotation;
The Android Open Source Project0727d222009-03-11 12:11:58 -07002122 } else {
Dianne Hackborn03759ed2009-03-27 16:04:08 -07002123 if (useSensorForOrientationLp(orientation)) {
The Android Open Source Project0727d222009-03-11 12:11:58 -07002124 // If the user has enabled auto rotation by default, do it.
Dianne Hackborn03759ed2009-03-27 16:04:08 -07002125 int curRotation = mOrientationListener.getCurrentRotation();
2126 return curRotation >= 0 ? curRotation : lastRotation;
The Android Open Source Project0727d222009-03-11 12:11:58 -07002127 }
2128 return Surface.ROTATION_0;
2129 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002130 }
2131 }
2132
2133 public boolean detectSafeMode() {
2134 try {
2135 int menuState = mWindowManager.getKeycodeState(KeyEvent.KEYCODE_MENU);
Dianne Hackbornb1a79802009-09-29 15:18:31 -07002136 int sState = mWindowManager.getKeycodeState(KeyEvent.KEYCODE_S);
Dianne Hackborna8730f92009-11-17 15:53:36 -08002137 int dpadState = mWindowManager.getDPadKeycodeState(KeyEvent.KEYCODE_DPAD_CENTER);
2138 int trackballState = mWindowManager.getTrackballScancodeState(RawInputEvent.BTN_MOUSE);
Dianne Hackbornb1a79802009-09-29 15:18:31 -07002139 mSafeMode = menuState > 0 || sState > 0 || dpadState > 0 || trackballState > 0;
2140 performHapticFeedbackLw(null, mSafeMode
2141 ? HapticFeedbackConstants.SAFE_MODE_ENABLED
2142 : HapticFeedbackConstants.SAFE_MODE_DISABLED, true);
Dianne Hackborn73b1ec82009-10-04 13:33:00 -07002143 if (mSafeMode) {
2144 Log.i(TAG, "SAFE MODE ENABLED (menu=" + menuState + " s=" + sState
2145 + " dpad=" + dpadState + " trackball=" + trackballState + ")");
2146 } else {
2147 Log.i(TAG, "SAFE MODE not enabled");
2148 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002149 return mSafeMode;
2150 } catch (RemoteException e) {
2151 // Doom! (it's also local)
2152 throw new RuntimeException("window manager dead");
2153 }
2154 }
2155
Dianne Hackborn181ceb52009-08-27 22:16:40 -07002156 static long[] getLongIntArray(Resources r, int resid) {
2157 int[] ar = r.getIntArray(resid);
2158 if (ar == null) {
2159 return null;
2160 }
2161 long[] out = new long[ar.length];
2162 for (int i=0; i<ar.length; i++) {
2163 out[i] = ar[i];
2164 }
2165 return out;
2166 }
2167
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002168 /** {@inheritDoc} */
2169 public void systemReady() {
Dianne Hackbornb1a79802009-09-29 15:18:31 -07002170 // tell the keyguard
2171 mKeyguardMediator.onSystemReady();
2172 android.os.SystemProperties.set("dev.bootcomplete", "1");
2173 synchronized (mLock) {
2174 updateOrientationListenerLp();
Dianne Hackbornc777e072010-02-12 13:07:59 -08002175 mSystemReady = true;
2176 mHandler.post(new Runnable() {
2177 public void run() {
2178 updateSettings();
2179 }
2180 });
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002181 }
2182 }
2183
Mike Lockwood28569302010-01-28 11:54:40 -05002184 /** {@inheritDoc} */
2185 public void userActivity() {
2186 synchronized (mScreenLockTimeout) {
2187 if (mLockScreenTimerActive) {
2188 // reset the timer
2189 mHandler.removeCallbacks(mScreenLockTimeout);
2190 mHandler.postDelayed(mScreenLockTimeout, mLockScreenTimeout);
2191 }
2192 }
2193 }
2194
2195 Runnable mScreenLockTimeout = new Runnable() {
2196 public void run() {
2197 synchronized (this) {
2198 if (localLOGV) Log.v(TAG, "mScreenLockTimeout activating keyguard");
2199 mKeyguardMediator.doKeyguardTimeout();
2200 mLockScreenTimerActive = false;
2201 }
2202 }
2203 };
2204
2205 private void updateLockScreenTimeout() {
2206 synchronized (mScreenLockTimeout) {
2207 boolean enable = (mAllowLockscreenWhenOn && mScreenOn && mKeyguardMediator.isSecure());
2208 if (mLockScreenTimerActive != enable) {
2209 if (enable) {
2210 if (localLOGV) Log.v(TAG, "setting lockscreen timer");
2211 mHandler.postDelayed(mScreenLockTimeout, mLockScreenTimeout);
2212 } else {
2213 if (localLOGV) Log.v(TAG, "clearing lockscreen timer");
2214 mHandler.removeCallbacks(mScreenLockTimeout);
2215 }
2216 mLockScreenTimerActive = enable;
2217 }
2218 }
2219 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002220
2221 /** {@inheritDoc} */
2222 public void enableScreenAfterBoot() {
2223 readLidState();
Dianne Hackborn32bc91d2009-03-27 16:16:03 -07002224 updateRotation(Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002225 }
2226
Dianne Hackborn80fa1662009-10-07 14:02:10 -07002227 void updateDockKeepingScreenOn() {
Dianne Hackborn05726582009-09-22 17:28:24 -07002228 if (mPlugged != 0) {
2229 if (localLOGV) Log.v(TAG, "Update: mDockState=" + mDockState
2230 + " mPlugged=" + mPlugged
2231 + " mCarDockKeepsScreenOn" + mCarDockKeepsScreenOn
2232 + " mDeskDockKeepsScreenOn" + mDeskDockKeepsScreenOn);
Dianne Hackbornf54c8f92009-09-19 14:03:57 -07002233 if (mDockState == Intent.EXTRA_DOCK_STATE_CAR
Dianne Hackborn05726582009-09-22 17:28:24 -07002234 && (mPlugged&mCarDockKeepsScreenOn) != 0) {
Dianne Hackbornf54c8f92009-09-19 14:03:57 -07002235 if (!mDockWakeLock.isHeld()) {
2236 mDockWakeLock.acquire();
2237 }
2238 return;
2239 } else if (mDockState == Intent.EXTRA_DOCK_STATE_DESK
Dianne Hackborn05726582009-09-22 17:28:24 -07002240 && (mPlugged&mDeskDockKeepsScreenOn) != 0) {
Dianne Hackbornf54c8f92009-09-19 14:03:57 -07002241 if (!mDockWakeLock.isHeld()) {
2242 mDockWakeLock.acquire();
2243 }
2244 return;
2245 }
2246 }
2247
2248 if (mDockWakeLock.isHeld()) {
2249 mDockWakeLock.release();
2250 }
2251 }
2252
Dianne Hackborn03759ed2009-03-27 16:04:08 -07002253 void updateRotation(int animFlags) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002254 mPowerManager.setKeyboardVisibility(mLidOpen);
Mike Lockwood1753f7f2009-08-24 14:49:07 -07002255 int rotation = Surface.ROTATION_0;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002256 if (mLidOpen) {
Mike Lockwood1753f7f2009-08-24 14:49:07 -07002257 rotation = mLidOpenRotation;
Mike Lockwood2d1efbd2009-09-16 12:15:53 -04002258 } else if (mDockState == Intent.EXTRA_DOCK_STATE_CAR && mCarDockRotation >= 0) {
2259 rotation = mCarDockRotation;
2260 } else if (mDockState == Intent.EXTRA_DOCK_STATE_DESK && mDeskDockRotation >= 0) {
2261 rotation = mDeskDockRotation;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002262 }
2263 //if lid is closed orientation will be portrait
2264 try {
2265 //set orientation on WindowManager
Dianne Hackborn32bc91d2009-03-27 16:16:03 -07002266 mWindowManager.setRotation(rotation, true,
2267 mFancyRotationAnimation | animFlags);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002268 } catch (RemoteException e) {
2269 // Ignore
2270 }
2271 }
2272
2273 /**
Dianne Hackborn39c2d712009-09-22 11:41:31 -07002274 * Return an Intent to launch the currently active dock as home. Returns
2275 * null if the standard home should be launched.
2276 * @return
2277 */
2278 Intent createHomeDockIntent() {
2279 if (mDockState == Intent.EXTRA_DOCK_STATE_UNDOCKED) {
2280 return null;
2281 }
2282
2283 Intent intent;
2284 if (mDockState == Intent.EXTRA_DOCK_STATE_CAR) {
2285 intent = mCarDockIntent;
2286 } else if (mDockState == Intent.EXTRA_DOCK_STATE_DESK) {
2287 intent = mDeskDockIntent;
2288 } else {
2289 Log.w(TAG, "Unknown dock state: " + mDockState);
2290 return null;
2291 }
2292
2293 ActivityInfo ai = intent.resolveActivityInfo(
2294 mContext.getPackageManager(), PackageManager.GET_META_DATA);
2295 if (ai == null) {
2296 return null;
2297 }
2298
2299 if (ai.metaData != null && ai.metaData.getBoolean(Intent.METADATA_DOCK_HOME)) {
2300 intent = new Intent(intent);
2301 intent.setClassName(ai.packageName, ai.name);
2302 return intent;
2303 }
2304
2305 return null;
2306 }
2307
2308 void startDockOrHome() {
2309 Intent dock = createHomeDockIntent();
2310 if (dock != null) {
2311 try {
2312 mContext.startActivity(dock);
2313 return;
2314 } catch (ActivityNotFoundException e) {
2315 }
2316 }
2317 mContext.startActivity(mHomeIntent);
2318 }
2319
2320 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002321 * goes to the home screen
2322 * @return whether it did anything
2323 */
2324 boolean goHome() {
2325 if (false) {
2326 // This code always brings home to the front.
Dianne Hackborn256dd3b2009-05-19 18:51:21 -07002327 try {
2328 ActivityManagerNative.getDefault().stopAppSwitches();
2329 } catch (RemoteException e) {
2330 }
Dianne Hackborn94a679d2009-07-28 17:51:07 -07002331 sendCloseSystemWindows();
Dianne Hackborn39c2d712009-09-22 11:41:31 -07002332 startDockOrHome();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002333 } else {
2334 // This code brings home to the front or, if it is already
2335 // at the front, puts the device to sleep.
2336 try {
Dianne Hackborn256dd3b2009-05-19 18:51:21 -07002337 ActivityManagerNative.getDefault().stopAppSwitches();
Dianne Hackborn94a679d2009-07-28 17:51:07 -07002338 sendCloseSystemWindows();
Dianne Hackborn39c2d712009-09-22 11:41:31 -07002339 Intent dock = createHomeDockIntent();
2340 if (dock != null) {
2341 int result = ActivityManagerNative.getDefault()
2342 .startActivity(null, dock,
2343 dock.resolveTypeIfNeeded(mContext.getContentResolver()),
2344 null, 0, null, null, 0, true /* onlyIfNeeded*/, false);
2345 if (result == IActivityManager.START_RETURN_INTENT_TO_CALLER) {
2346 return false;
2347 }
2348 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002349 int result = ActivityManagerNative.getDefault()
2350 .startActivity(null, mHomeIntent,
2351 mHomeIntent.resolveTypeIfNeeded(mContext.getContentResolver()),
2352 null, 0, null, null, 0, true /* onlyIfNeeded*/, false);
2353 if (result == IActivityManager.START_RETURN_INTENT_TO_CALLER) {
2354 return false;
2355 }
2356 } catch (RemoteException ex) {
2357 // bummer, the activity manager, which is in this process, is dead
2358 }
2359 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002360 return true;
2361 }
2362
The Android Open Source Project0727d222009-03-11 12:11:58 -07002363 public void setCurrentOrientationLw(int newOrientation) {
2364 synchronized (mLock) {
2365 if (newOrientation != mCurrentAppOrientation) {
2366 mCurrentAppOrientation = newOrientation;
2367 updateOrientationListenerLp();
2368 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002369 }
2370 }
2371
The Android Open Source Project0727d222009-03-11 12:11:58 -07002372 public boolean performHapticFeedbackLw(WindowState win, int effectId, boolean always) {
Jim Miller89b17222009-10-27 14:54:09 -07002373 final boolean hapticsDisabled = Settings.System.getInt(mContext.getContentResolver(),
2374 Settings.System.HAPTIC_FEEDBACK_ENABLED, 0) == 0;
Dianne Hackborn9dc06cc2009-11-17 18:19:23 -08002375 if (!always && (hapticsDisabled || mKeyguardMediator.isShowingAndNotHidden())) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002376 return false;
2377 }
2378 switch (effectId) {
2379 case HapticFeedbackConstants.LONG_PRESS:
Dianne Hackborn181ceb52009-08-27 22:16:40 -07002380 mVibrator.vibrate(mLongPressVibePattern, -1);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002381 return true;
Dianne Hackbornb1a79802009-09-29 15:18:31 -07002382 case HapticFeedbackConstants.VIRTUAL_KEY:
2383 mVibrator.vibrate(mVirtualKeyVibePattern, -1);
2384 return true;
2385 case HapticFeedbackConstants.SAFE_MODE_DISABLED:
2386 mVibrator.vibrate(mSafeModeDisabledVibePattern, -1);
2387 return true;
2388 case HapticFeedbackConstants.SAFE_MODE_ENABLED:
2389 mVibrator.vibrate(mSafeModeEnabledVibePattern, -1);
2390 return true;
Adam Powell9d86faf2010-02-10 11:39:57 -08002391 case HapticFeedbackConstants.SCROLL_BARRIER:
2392 mVibrator.vibrate(mScrollBarrierVibePattern, -1);
2393 return true;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002394 }
2395 return false;
2396 }
2397
Dianne Hackborn0041e972009-07-24 17:14:43 -07002398 public void keyFeedbackFromInput(KeyEvent event) {
2399 if (event.getAction() == KeyEvent.ACTION_DOWN
2400 && (event.getFlags()&KeyEvent.FLAG_VIRTUAL_HARD_KEY) != 0) {
Dianne Hackbornb1a79802009-09-29 15:18:31 -07002401 performHapticFeedbackLw(null, HapticFeedbackConstants.VIRTUAL_KEY, false);
Dianne Hackborn0041e972009-07-24 17:14:43 -07002402 }
2403 }
2404
The Android Open Source Project0727d222009-03-11 12:11:58 -07002405 public void screenOnStoppedLw() {
Dianne Hackborn9dc06cc2009-11-17 18:19:23 -08002406 if (!mKeyguardMediator.isShowingAndNotHidden() && mPowerManager.isScreenOn()) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002407 long curTime = SystemClock.uptimeMillis();
2408 mPowerManager.userActivity(curTime, false, LocalPowerManager.OTHER_EVENT);
2409 }
2410 }
Mike Lockwoodfcb39242009-10-21 23:01:07 -04002411
2412 public boolean allowKeyRepeat() {
2413 // disable key repeat when screen is off
2414 return mScreenOn;
2415 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002416}
Wink Saville37c124c2009-04-02 01:37:02 -07002417