blob: eeb340b67e687ca85827382668531bee47726efa [file] [log] [blame]
Jeff Brown11159e92012-10-11 15:58:37 -07001/*
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
Jeff Brown11159e92012-10-11 15:58:37 -070039 // The component name of a special dock app that merely launches a dream.
40 // We don't want to launch this app when docked because it causes an unnecessary
41 // activity transition. We just want to start the dream.
42 private static final ComponentName SOMNAMBULATOR_COMPONENT =
43 new ComponentName("com.android.systemui", "com.android.systemui.Somnambulator");
44
45
46 // The sandman is eternal. No one instantiates him.
47 private Sandman() {
48 }
49
50 /**
51 * Returns true if the specified dock app intent should be started.
52 * False if we should dream instead, if appropriate.
53 */
54 public static boolean shouldStartDockApp(Context context, Intent intent) {
55 ComponentName name = intent.resolveActivity(context.getPackageManager());
56 return name != null && !name.equals(SOMNAMBULATOR_COMPONENT);
57 }
58
59 /**
60 * Starts a dream manually.
61 */
62 public static void startDreamByUserRequest(Context context) {
63 startDream(context, false);
64 }
65
66 /**
67 * Starts a dream when docked if the system has been configured to do so,
68 * otherwise does nothing.
69 */
70 public static void startDreamWhenDockedIfAppropriate(Context context) {
71 if (!isScreenSaverEnabled(context)
72 || !isScreenSaverActivatedOnDock(context)) {
73 Slog.i(TAG, "Dreams currently disabled for docks.");
74 return;
75 }
76
77 startDream(context, true);
78 }
79
80 private static void startDream(Context context, boolean docked) {
81 try {
82 IDreamManager dreamManagerService = IDreamManager.Stub.asInterface(
83 ServiceManager.getService(DreamService.DREAM_SERVICE));
84 if (dreamManagerService != null && !dreamManagerService.isDreaming()) {
85 if (docked) {
86 Slog.i(TAG, "Activating dream while docked.");
87
88 // Wake up.
89 // The power manager will wake up the system automatically when it starts
90 // receiving power from a dock but there is a race between that happening
91 // and the UI mode manager starting a dream. We want the system to already
92 // be awake by the time this happens. Otherwise the dream may not start.
93 PowerManager powerManager =
94 (PowerManager)context.getSystemService(Context.POWER_SERVICE);
Dianne Hackborn280a64e2015-07-13 14:48:08 -070095 powerManager.wakeUp(SystemClock.uptimeMillis(),
96 "android.service.dreams:DREAM");
Jeff Brown11159e92012-10-11 15:58:37 -070097 } else {
98 Slog.i(TAG, "Activating dream by user request.");
99 }
100
101 // Dream.
102 dreamManagerService.dream();
103 }
104 } catch (RemoteException ex) {
105 Slog.e(TAG, "Could not start dream when docked.", ex);
106 }
107 }
108
109 private static boolean isScreenSaverEnabled(Context context) {
John Spurlocked108f32012-10-18 16:49:24 -0400110 int def = context.getResources().getBoolean(
111 com.android.internal.R.bool.config_dreamsEnabledByDefault) ? 1 : 0;
Jeff Brown11159e92012-10-11 15:58:37 -0700112 return Settings.Secure.getIntForUser(context.getContentResolver(),
John Spurlocked108f32012-10-18 16:49:24 -0400113 Settings.Secure.SCREENSAVER_ENABLED, def,
Jeff Brown11159e92012-10-11 15:58:37 -0700114 UserHandle.USER_CURRENT) != 0;
115 }
116
117 private static boolean isScreenSaverActivatedOnDock(Context context) {
John Spurlocked108f32012-10-18 16:49:24 -0400118 int def = context.getResources().getBoolean(
119 com.android.internal.R.bool.config_dreamsActivatedOnDockByDefault) ? 1 : 0;
Jeff Brown11159e92012-10-11 15:58:37 -0700120 return Settings.Secure.getIntForUser(context.getContentResolver(),
John Spurlocked108f32012-10-18 16:49:24 -0400121 Settings.Secure.SCREENSAVER_ACTIVATE_ON_DOCK, def,
122 UserHandle.USER_CURRENT) != 0;
Jeff Brown11159e92012-10-11 15:58:37 -0700123 }
124}