Sandman.java revision 11159e9a785a143c9f3765bdf5a5ccfd77842d7a
1/* 2 * Copyright (C) 2012 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 android.service.dreams; 18 19import android.content.ComponentName; 20import android.content.Context; 21import android.content.Intent; 22import android.os.PowerManager; 23import android.os.RemoteException; 24import android.os.ServiceManager; 25import android.os.SystemClock; 26import android.os.UserHandle; 27import android.provider.Settings; 28import android.util.Slog; 29 30/** 31 * Internal helper for launching dreams to ensure consistency between the 32 * <code>UiModeManagerService</code> system service and the <code>Somnambulator</code> activity. 33 * 34 * @hide 35 */ 36public final class Sandman { 37 private static final String TAG = "Sandman"; 38 39 private static final int DEFAULT_SCREENSAVER_ENABLED = 1; 40 private static final int DEFAULT_SCREENSAVER_ACTIVATED_ON_DOCK = 1; 41 42 // The component name of a special dock app that merely launches a dream. 43 // We don't want to launch this app when docked because it causes an unnecessary 44 // activity transition. We just want to start the dream. 45 private static final ComponentName SOMNAMBULATOR_COMPONENT = 46 new ComponentName("com.android.systemui", "com.android.systemui.Somnambulator"); 47 48 49 // The sandman is eternal. No one instantiates him. 50 private Sandman() { 51 } 52 53 /** 54 * Returns true if the specified dock app intent should be started. 55 * False if we should dream instead, if appropriate. 56 */ 57 public static boolean shouldStartDockApp(Context context, Intent intent) { 58 ComponentName name = intent.resolveActivity(context.getPackageManager()); 59 return name != null && !name.equals(SOMNAMBULATOR_COMPONENT); 60 } 61 62 /** 63 * Starts a dream manually. 64 */ 65 public static void startDreamByUserRequest(Context context) { 66 startDream(context, false); 67 } 68 69 /** 70 * Starts a dream when docked if the system has been configured to do so, 71 * otherwise does nothing. 72 */ 73 public static void startDreamWhenDockedIfAppropriate(Context context) { 74 if (!isScreenSaverEnabled(context) 75 || !isScreenSaverActivatedOnDock(context)) { 76 Slog.i(TAG, "Dreams currently disabled for docks."); 77 return; 78 } 79 80 startDream(context, true); 81 } 82 83 private static void startDream(Context context, boolean docked) { 84 try { 85 IDreamManager dreamManagerService = IDreamManager.Stub.asInterface( 86 ServiceManager.getService(DreamService.DREAM_SERVICE)); 87 if (dreamManagerService != null && !dreamManagerService.isDreaming()) { 88 if (docked) { 89 Slog.i(TAG, "Activating dream while docked."); 90 91 // Wake up. 92 // The power manager will wake up the system automatically when it starts 93 // receiving power from a dock but there is a race between that happening 94 // and the UI mode manager starting a dream. We want the system to already 95 // be awake by the time this happens. Otherwise the dream may not start. 96 PowerManager powerManager = 97 (PowerManager)context.getSystemService(Context.POWER_SERVICE); 98 powerManager.wakeUp(SystemClock.uptimeMillis()); 99 } else { 100 Slog.i(TAG, "Activating dream by user request."); 101 } 102 103 // Dream. 104 dreamManagerService.dream(); 105 } 106 } catch (RemoteException ex) { 107 Slog.e(TAG, "Could not start dream when docked.", ex); 108 } 109 } 110 111 private static boolean isScreenSaverEnabled(Context context) { 112 return Settings.Secure.getIntForUser(context.getContentResolver(), 113 Settings.Secure.SCREENSAVER_ENABLED, DEFAULT_SCREENSAVER_ENABLED, 114 UserHandle.USER_CURRENT) != 0; 115 } 116 117 private static boolean isScreenSaverActivatedOnDock(Context context) { 118 return Settings.Secure.getIntForUser(context.getContentResolver(), 119 Settings.Secure.SCREENSAVER_ACTIVATE_ON_DOCK, 120 DEFAULT_SCREENSAVER_ACTIVATED_ON_DOCK, UserHandle.USER_CURRENT) != 0; 121 } 122} 123