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