summaryrefslogtreecommitdiffstats
path: root/packages/SystemUI/src/com/android/systemui/DreamsDockLauncher.java
blob: 73249b44b28e26d7e9228d201af7c37198c29a83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package com.android.systemui;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Slog;

public class DreamsDockLauncher extends Activity {
    private static final String TAG = "DreamsDockLauncher";

    // Launch the screen saver if started as an activity.
    @Override
    protected void onCreate (Bundle icicle) {
        super.onCreate(icicle);
        launchDream(this);
        finish();
    }

    private static void launchDream(Context context) {
        try {
            String component = Settings.Secure.getString(
                    context.getContentResolver(), Settings.Secure.SCREENSAVER_COMPONENT);
            if (component == null) {
                component = context.getResources().getString(
                    com.android.internal.R.string.config_defaultDreamComponent);
            }
            if (component != null) {
                // dismiss the notification shade, recents, etc.
                context.sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));

                ComponentName cn = ComponentName.unflattenFromString(component);
                Intent zzz = new Intent(Intent.ACTION_MAIN)
                    .setComponent(cn)
                    .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                            | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
                            | Intent.FLAG_ACTIVITY_NO_USER_ACTION
                            | Intent.FLAG_FROM_BACKGROUND
                            | Intent.FLAG_ACTIVITY_NO_HISTORY
                        );
                Slog.v(TAG, "Starting screen saver on dock event: " + component);
                context.startActivity(zzz);
            } else {
                Slog.e(TAG, "Couldn't start screen saver: none selected");
            }
        } catch (android.content.ActivityNotFoundException exc) {
            // no screensaver? give up
            Slog.e(TAG, "Couldn't start screen saver: none installed");
        }
    }

    // Trap low-level dock events and launch the screensaver.
    public static class DockEventReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            final boolean activateOnDock = 0 != Settings.Secure.getInt(
                context.getContentResolver(),
                Settings.Secure.SCREENSAVER_ACTIVATE_ON_DOCK, 0);

            if (!activateOnDock) return;

            if (Intent.ACTION_DOCK_EVENT.equals(intent.getAction())) {
                Bundle extras = intent.getExtras();
                int state = extras
                        .getInt(Intent.EXTRA_DOCK_STATE, Intent.EXTRA_DOCK_STATE_UNDOCKED);
                if (state == Intent.EXTRA_DOCK_STATE_DESK
                        || state == Intent.EXTRA_DOCK_STATE_LE_DESK
                        || state == Intent.EXTRA_DOCK_STATE_HE_DESK) {
                    launchDream(context);
                }
            }
        }
    }
}