Skip to content

Commit 3da6c21

Browse files
Jeff BrownAndroid (Google) Code Review
authored andcommitted
Merge "Do not launch Somnambulator as a dock app." into jb-mr1-dev
2 parents 3003bad + 11159e9 commit 3da6c21

File tree

3 files changed

+152
-89
lines changed

3 files changed

+152
-89
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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+
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+
}
Lines changed: 14 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*);
1+
/*
22
* Copyright (C) 2012 The Android Open Source Project
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -18,38 +18,23 @@
1818

1919
import android.app.Activity;
2020
import android.content.Intent;
21-
import android.os.RemoteException;
22-
import android.os.ServiceManager;
23-
import android.os.UserHandle;
24-
import android.provider.Settings;
25-
import android.service.dreams.DreamService;
26-
import android.service.dreams.IDreamManager;
27-
import android.util.Slog;
28-
21+
import android.service.dreams.Sandman;
22+
23+
/**
24+
* A simple activity that launches a dream.
25+
* <p>
26+
* Note: This Activity is special. If this class is moved to another package or
27+
* renamed, be sure to update the component name in {@link Sandman}.
28+
* </p>
29+
*/
2930
public class Somnambulator extends Activity {
30-
public static final String TAG = "Somnambulator";
31-
32-
public static final int DEFAULT_SCREENSAVER_ENABLED = 1;
33-
public static final int DEFAULT_SCREENSAVER_ACTIVATED_ON_DOCK = 1;
34-
3531
public Somnambulator() {
3632
}
3733

38-
private boolean isScreenSaverEnabled() {
39-
return Settings.Secure.getIntForUser(getContentResolver(),
40-
Settings.Secure.SCREENSAVER_ENABLED, DEFAULT_SCREENSAVER_ENABLED,
41-
UserHandle.USER_CURRENT) != 0;
42-
}
43-
44-
private boolean isScreenSaverActivatedOnDock() {
45-
return Settings.Secure.getIntForUser(getContentResolver(),
46-
Settings.Secure.SCREENSAVER_ACTIVATE_ON_DOCK,
47-
DEFAULT_SCREENSAVER_ACTIVATED_ON_DOCK, UserHandle.USER_CURRENT) != 0;
48-
}
49-
5034
@Override
5135
public void onStart() {
5236
super.onStart();
37+
5338
final Intent launchIntent = getIntent();
5439
final String action = launchIntent.getAction();
5540
if (Intent.ACTION_CREATE_SHORTCUT.equals(action)) {
@@ -64,23 +49,12 @@ public void onStart() {
6449
setResult(RESULT_OK, resultIntent);
6550
} else {
6651
boolean docked = launchIntent.hasCategory(Intent.CATEGORY_DESK_DOCK);
67-
68-
if (docked && !(isScreenSaverEnabled() && isScreenSaverActivatedOnDock())) {
69-
Slog.i(TAG, "Dreams currently disabled for docks.");
52+
if (docked) {
53+
Sandman.startDreamWhenDockedIfAppropriate(this);
7054
} else {
71-
IDreamManager somnambulist = IDreamManager.Stub.asInterface(
72-
ServiceManager.checkService(DreamService.DREAM_SERVICE));
73-
if (somnambulist != null) {
74-
try {
75-
Slog.v(TAG, "Dreaming on " + (docked ? "dock insertion" : "user request"));
76-
somnambulist.dream();
77-
} catch (RemoteException e) {
78-
// fine, stay asleep then
79-
}
80-
}
55+
Sandman.startDreamByUserRequest(this);
8156
}
8257
}
8358
finish();
8459
}
85-
8660
}

services/java/com/android/server/UiModeManagerService.java

Lines changed: 16 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,9 @@
3737
import android.os.PowerManager;
3838
import android.os.RemoteException;
3939
import android.os.ServiceManager;
40-
import android.os.SystemClock;
4140
import android.os.UserHandle;
4241
import android.provider.Settings;
43-
import android.service.dreams.DreamService;
44-
import android.service.dreams.IDreamManager;
42+
import android.service.dreams.Sandman;
4543
import android.util.Slog;
4644

4745
import java.io.FileDescriptor;
@@ -59,9 +57,6 @@ final class UiModeManagerService extends IUiModeManager.Stub {
5957
private static final boolean ENABLE_LAUNCH_CAR_DOCK_APP = true;
6058
private static final boolean ENABLE_LAUNCH_DESK_DOCK_APP = true;
6159

62-
private static final int DEFAULT_SCREENSAVER_ENABLED = 1;
63-
private static final int DEFAULT_SCREENSAVER_ACTIVATED_ON_DOCK = 1;
64-
6560
private final Context mContext;
6661
private final TwilightService mTwilightService;
6762
private final Handler mHandler = new Handler();
@@ -496,60 +491,32 @@ private void sendConfigurationAndStartDreamOrDockAppLocked(String category) {
496491
// activity manager take care of both the start and config
497492
// change.
498493
Intent homeIntent = buildHomeIntent(category);
499-
try {
500-
int result = ActivityManagerNative.getDefault().startActivityWithConfig(
501-
null, homeIntent, null, null, null, 0, 0,
502-
mConfiguration, null, UserHandle.USER_CURRENT);
503-
if (result >= ActivityManager.START_SUCCESS) {
504-
dockAppStarted = true;
505-
} else if (result != ActivityManager.START_INTENT_NOT_RESOLVED) {
506-
Slog.e(TAG, "Could not start dock app: " + homeIntent
507-
+ ", startActivityWithConfig result " + result);
494+
if (Sandman.shouldStartDockApp(mContext, homeIntent)) {
495+
try {
496+
int result = ActivityManagerNative.getDefault().startActivityWithConfig(
497+
null, homeIntent, null, null, null, 0, 0,
498+
mConfiguration, null, UserHandle.USER_CURRENT);
499+
if (result >= ActivityManager.START_SUCCESS) {
500+
dockAppStarted = true;
501+
} else if (result != ActivityManager.START_INTENT_NOT_RESOLVED) {
502+
Slog.e(TAG, "Could not start dock app: " + homeIntent
503+
+ ", startActivityWithConfig result " + result);
504+
}
505+
} catch (RemoteException ex) {
506+
Slog.e(TAG, "Could not start dock app: " + homeIntent, ex);
508507
}
509-
} catch (RemoteException ex) {
510-
Slog.e(TAG, "Could not start dock app: " + homeIntent, ex);
511508
}
512509
}
513510

514511
// Send the new configuration.
515512
sendConfigurationLocked();
516513

517514
// If we did not start a dock app, then start dreaming if supported.
518-
if (category != null && !dockAppStarted
519-
&& isScreenSaverEnabledLocked() && isScreenSaverActivatedOnDockLocked()) {
520-
Slog.i(TAG, "Activating dream while docked.");
521-
try {
522-
IDreamManager dreamManagerService = IDreamManager.Stub.asInterface(
523-
ServiceManager.getService(DreamService.DREAM_SERVICE));
524-
if (dreamManagerService != null && !dreamManagerService.isDreaming()) {
525-
// Wake up.
526-
// The power manager will wake up the system when it starts receiving power
527-
// but there is a race between that happening and the UI mode manager
528-
// starting a dream. We want the system to already be awake
529-
// by the time this happens. Otherwise the dream may not start.
530-
mPowerManager.wakeUp(SystemClock.uptimeMillis());
531-
532-
// Dream.
533-
dreamManagerService.dream();
534-
}
535-
} catch (RemoteException ex) {
536-
Slog.e(TAG, "Could not start dream when docked.", ex);
537-
}
515+
if (category != null && !dockAppStarted) {
516+
Sandman.startDreamWhenDockedIfAppropriate(mContext);
538517
}
539518
}
540519

541-
private boolean isScreenSaverEnabledLocked() {
542-
return Settings.Secure.getIntForUser(mContext.getContentResolver(),
543-
Settings.Secure.SCREENSAVER_ENABLED, DEFAULT_SCREENSAVER_ENABLED,
544-
UserHandle.USER_CURRENT) != 0;
545-
}
546-
547-
private boolean isScreenSaverActivatedOnDockLocked() {
548-
return Settings.Secure.getIntForUser(mContext.getContentResolver(),
549-
Settings.Secure.SCREENSAVER_ACTIVATE_ON_DOCK,
550-
DEFAULT_SCREENSAVER_ACTIVATED_ON_DOCK, UserHandle.USER_CURRENT) != 0;
551-
}
552-
553520
private void adjustStatusBarCarModeLocked() {
554521
if (mStatusBarManager == null) {
555522
mStatusBarManager = (StatusBarManager)

0 commit comments

Comments
 (0)