DozeFactory.java revision a1e6b3157cd5cfc7a3a70f0a9671447d0849c642
1/*
2 * Copyright (C) 2016 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 com.android.systemui.doze;
18
19import android.app.AlarmManager;
20import android.app.Application;
21import android.app.PendingIntent;
22import android.content.Context;
23import android.hardware.SensorManager;
24import android.os.Handler;
25
26import com.android.internal.hardware.AmbientDisplayConfiguration;
27import com.android.systemui.SystemUIApplication;
28import com.android.systemui.plugins.doze.DozeProvider;
29import com.android.systemui.statusbar.phone.DozeParameters;
30import com.android.systemui.util.wakelock.WakeLock;
31
32public class DozeFactory {
33
34    private final DozeProvider mDozePlugin;
35
36    public DozeFactory(DozeProvider plugin) {
37        mDozePlugin = plugin;
38    }
39
40    /** Creates a DozeMachine with its parts for {@code dozeService}. */
41    public DozeMachine assembleMachine(DozeService dozeService) {
42        Context context = dozeService;
43        SensorManager sensorManager = context.getSystemService(SensorManager.class);
44        AlarmManager alarmManager = context.getSystemService(AlarmManager.class);
45
46        DozeHost host = getHost(dozeService);
47        AmbientDisplayConfiguration config = new AmbientDisplayConfiguration(context);
48        DozeParameters params = new DozeParameters(context);
49        Handler handler = new Handler();
50        WakeLock wakeLock = WakeLock.createPartial(context, "Doze");
51
52        DozeMachine machine = new DozeMachine(
53                DozeSuspendScreenStatePreventingAdapter.wrapIfNeeded(
54                        DozeScreenStatePreventingAdapter.wrapIfNeeded(dozeService, params), params),
55                config,
56                wakeLock);
57        machine.setParts(new DozeMachine.Part[]{
58                createDozeTriggers(context, sensorManager, host, config, params, handler, wakeLock,
59                        machine),
60                createDozeUi(context, host, wakeLock, machine, handler, alarmManager),
61        });
62
63        return machine;
64    }
65
66    private DozeTriggers createDozeTriggers(Context context, SensorManager sensorManager,
67            DozeHost host, AmbientDisplayConfiguration config, DozeParameters params,
68            Handler handler, WakeLock wakeLock, DozeMachine machine) {
69        boolean allowPulseTriggers = mDozePlugin == null || mDozePlugin.allowDefaultPulseTriggers();
70        return new DozeTriggers(context, machine, host, config, params,
71                sensorManager, handler, wakeLock, allowPulseTriggers);
72    }
73
74    private DozeMachine.Part createDozeUi(Context context, DozeHost host, WakeLock wakeLock,
75            DozeMachine machine, Handler handler, AlarmManager alarmManager) {
76        if (mDozePlugin != null) {
77            DozeProvider.DozeUi dozeUi = mDozePlugin.provideDozeUi(context,
78                    pluginMachine(context, machine, host),
79                    wakeLock);
80            return (oldState, newState) -> {
81                dozeUi.transitionTo(pluginState(oldState),
82                        pluginState(newState));
83            };
84        } else {
85            return new DozeUi(context, alarmManager, machine, wakeLock, host, handler);
86        }
87    }
88
89    private DozeProvider.DozeMachine pluginMachine(Context context, DozeMachine machine,
90            DozeHost host) {
91        return new DozeProvider.DozeMachine() {
92            @Override
93            public void requestState(DozeProvider.DozeState state) {
94                if (state == DozeProvider.DozeState.WAKE_UP) {
95                    machine.wakeUp();
96                    return;
97                }
98                machine.requestState(implState(state));
99            }
100
101            @Override
102            public void requestSendIntent(PendingIntent intent) {
103                host.startPendingIntentDismissingKeyguard(intent);
104            }
105        };
106    }
107
108    private DozeMachine.State implState(DozeProvider.DozeState s) {
109        switch (s) {
110            case UNINITIALIZED:
111                return DozeMachine.State.UNINITIALIZED;
112            case INITIALIZED:
113                return DozeMachine.State.INITIALIZED;
114            case DOZE:
115                return DozeMachine.State.DOZE;
116            case DOZE_AOD:
117                return DozeMachine.State.DOZE_AOD;
118            case DOZE_REQUEST_PULSE:
119                return DozeMachine.State.DOZE_REQUEST_PULSE;
120            case DOZE_PULSING:
121                return DozeMachine.State.DOZE_PULSING;
122            case DOZE_PULSE_DONE:
123                return DozeMachine.State.DOZE_PULSE_DONE;
124            case FINISH:
125                return DozeMachine.State.FINISH;
126            default:
127                throw new IllegalArgumentException("Unknown state: " + s);
128        }
129    }
130
131    private DozeProvider.DozeState pluginState(DozeMachine.State s) {
132        switch (s) {
133            case UNINITIALIZED:
134                return DozeProvider.DozeState.UNINITIALIZED;
135            case INITIALIZED:
136                return DozeProvider.DozeState.INITIALIZED;
137            case DOZE:
138                return DozeProvider.DozeState.DOZE;
139            case DOZE_AOD:
140                return DozeProvider.DozeState.DOZE_AOD;
141            case DOZE_REQUEST_PULSE:
142                return DozeProvider.DozeState.DOZE_REQUEST_PULSE;
143            case DOZE_PULSING:
144                return DozeProvider.DozeState.DOZE_PULSING;
145            case DOZE_PULSE_DONE:
146                return DozeProvider.DozeState.DOZE_PULSE_DONE;
147            case FINISH:
148                return DozeProvider.DozeState.FINISH;
149            default:
150                throw new IllegalArgumentException("Unknown state: " + s);
151        }
152    }
153
154    public static DozeHost getHost(DozeService service) {
155        Application appCandidate = service.getApplication();
156        final SystemUIApplication app = (SystemUIApplication) appCandidate;
157        return app.getComponent(DozeHost.class);
158    }
159}
160