2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.systemui.doze;
19 import android.app.AlarmManager;
20 import android.app.Application;
21 import android.content.Context;
22 import android.hardware.SensorManager;
23 import android.os.Handler;
25 import com.android.internal.hardware.AmbientDisplayConfiguration;
26 import com.android.systemui.SystemUIApplication;
27 import com.android.systemui.statusbar.phone.DozeParameters;
28 import com.android.systemui.util.wakelock.WakeLock;
30 public class DozeFactory {
32 public DozeFactory() {
35 /** Creates a DozeMachine with its parts for {@code dozeService}. */
36 public DozeMachine assembleMachine(DozeService dozeService) {
37 Context context = dozeService;
38 SensorManager sensorManager = context.getSystemService(SensorManager.class);
39 AlarmManager alarmManager = context.getSystemService(AlarmManager.class);
41 DozeHost host = getHost(dozeService);
42 AmbientDisplayConfiguration config = new AmbientDisplayConfiguration(context);
43 DozeParameters params = new DozeParameters(context);
44 Handler handler = new Handler();
45 WakeLock wakeLock = WakeLock.createPartial(context, "Doze");
47 DozeMachine machine = new DozeMachine(
48 DozeScreenStatePreventingAdapter.wrapIfNeeded(dozeService, params),
51 machine.setParts(new DozeMachine.Part[]{
52 createDozeTriggers(context, sensorManager, host, config, params, handler, wakeLock,
54 createDozeUi(context, host, wakeLock, machine, handler, alarmManager),
60 private DozeTriggers createDozeTriggers(Context context, SensorManager sensorManager,
61 DozeHost host, AmbientDisplayConfiguration config, DozeParameters params,
62 Handler handler, WakeLock wakeLock, DozeMachine machine) {
63 boolean allowPulseTriggers = true;
64 return new DozeTriggers(context, machine, host, config, params,
65 sensorManager, handler, wakeLock, allowPulseTriggers);
68 private DozeMachine.Part createDozeUi(Context context, DozeHost host, WakeLock wakeLock,
69 DozeMachine machine, Handler handler, AlarmManager alarmManager) {
70 return new DozeUi(context, alarmManager, machine, wakeLock, host, handler);
73 public static DozeHost getHost(DozeService service) {
74 Application appCandidate = service.getApplication();
75 final SystemUIApplication app = (SystemUIApplication) appCandidate;
76 return app.getComponent(DozeHost.class);