blob: d22d2c3fd47f1308cd641fd0bff4537458bee8ac [file] [log] [blame]
Adrian Roosff2c4562016-11-03 12:13:36 -07001/*
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
Adrian Roosfe54aa02016-11-07 14:14:25 -080019import android.app.AlarmManager;
Adrian Roosff2c4562016-11-03 12:13:36 -070020import android.app.Application;
21import android.content.Context;
Adrian Roos2981eb02017-05-26 18:40:09 -070022import android.hardware.Sensor;
Adrian Roosff2c4562016-11-03 12:13:36 -070023import android.hardware.SensorManager;
Issei Suzukica19e6e2019-02-26 12:39:11 +010024import android.hardware.display.AmbientDisplayConfiguration;
Adrian Roosff2c4562016-11-03 12:13:36 -070025import android.os.Handler;
Adrian Roosff2c4562016-11-03 12:13:36 -070026
Lucas Dupin16cfe452018-02-08 13:14:50 -080027import com.android.keyguard.KeyguardUpdateMonitor;
Adrian Roos7a8ae8a2017-08-02 16:26:50 +020028import com.android.systemui.Dependency;
Adrian Roos2981eb02017-05-26 18:40:09 -070029import com.android.systemui.R;
Adrian Roosff2c4562016-11-03 12:13:36 -070030import com.android.systemui.SystemUIApplication;
Dave Mankoffdde5ee62019-05-02 17:36:11 -040031import com.android.systemui.classifier.FalsingManagerFactory;
lpeter8a5f4702019-01-18 16:53:07 +080032import com.android.systemui.dock.DockManager;
Lucas Dupinbd6c92b2019-07-17 09:49:52 -070033import com.android.systemui.statusbar.phone.BiometricUnlockController;
Adrian Roosff2c4562016-11-03 12:13:36 -070034import com.android.systemui.statusbar.phone.DozeParameters;
Adrian Roos7a8ae8a2017-08-02 16:26:50 +020035import com.android.systemui.util.AsyncSensorManager;
Adrian Roos0fb55ae2017-04-14 14:49:11 -070036import com.android.systemui.util.wakelock.DelayedWakeLock;
Adrian Roosc1b50322017-02-27 21:07:58 +010037import com.android.systemui.util.wakelock.WakeLock;
Adrian Roosff2c4562016-11-03 12:13:36 -070038
39public class DozeFactory {
40
Adrian Roos664c9d72017-04-28 15:52:24 -070041 public DozeFactory() {
Adrian Roosf9d13f62016-11-08 15:42:20 -080042 }
43
Adrian Roosff2c4562016-11-03 12:13:36 -070044 /** Creates a DozeMachine with its parts for {@code dozeService}. */
Adrian Roosf9d13f62016-11-08 15:42:20 -080045 public DozeMachine assembleMachine(DozeService dozeService) {
Adrian Roosff2c4562016-11-03 12:13:36 -070046 Context context = dozeService;
Adrian Roos7a8ae8a2017-08-02 16:26:50 +020047 SensorManager sensorManager = Dependency.get(AsyncSensorManager.class);
Adrian Roosfe54aa02016-11-07 14:14:25 -080048 AlarmManager alarmManager = context.getSystemService(AlarmManager.class);
Lucas Dupind7221352019-04-29 19:43:11 -070049 DockManager dockManager = Dependency.get(DockManager.class);
Adrian Roosff2c4562016-11-03 12:13:36 -070050
51 DozeHost host = getHost(dozeService);
52 AmbientDisplayConfiguration config = new AmbientDisplayConfiguration(context);
Lucas Dupin16cfe452018-02-08 13:14:50 -080053 DozeParameters params = DozeParameters.getInstance(context);
Adrian Roosff2c4562016-11-03 12:13:36 -070054 Handler handler = new Handler();
Adrian Roos0fb55ae2017-04-14 14:49:11 -070055 WakeLock wakeLock = new DelayedWakeLock(handler,
56 WakeLock.createPartial(context, "Doze"));
Adrian Roosff2c4562016-11-03 12:13:36 -070057
Adrian Roos3e23eb52017-07-07 15:58:57 +020058 DozeMachine.Service wrappedService = dozeService;
59 wrappedService = new DozeBrightnessHostForwarder(wrappedService, host);
60 wrappedService = DozeScreenStatePreventingAdapter.wrapIfNeeded(wrappedService, params);
61 wrappedService = DozeSuspendScreenStatePreventingAdapter.wrapIfNeeded(wrappedService,
62 params);
63
Adrian Roos2981eb02017-05-26 18:40:09 -070064 DozeMachine machine = new DozeMachine(wrappedService, config, wakeLock);
Adrian Roosff2c4562016-11-03 12:13:36 -070065 machine.setParts(new DozeMachine.Part[]{
Adrian Roos2f5a3852018-04-23 17:48:08 +020066 new DozePauser(handler, machine, alarmManager, params.getPolicy()),
Dave Mankoffdde5ee62019-05-02 17:36:11 -040067 new DozeFalsingManagerAdapter(FalsingManagerFactory.getInstance(context)),
Adrian Roos6023ccb2017-06-28 16:22:02 +020068 createDozeTriggers(context, sensorManager, host, alarmManager, config, params,
lpeter8a5f4702019-01-18 16:53:07 +080069 handler, wakeLock, machine, dockManager),
Lucas Dupin43d0d732017-11-16 11:23:49 -080070 createDozeUi(context, host, wakeLock, machine, handler, alarmManager, params),
Lucas Dupin16cfe452018-02-08 13:14:50 -080071 new DozeScreenState(wrappedService, handler, params, wakeLock),
Adrian Roos2f5a3852018-04-23 17:48:08 +020072 createDozeScreenBrightness(context, wrappedService, sensorManager, host, params,
73 handler),
Lucas Dupinbd6c92b2019-07-17 09:49:52 -070074 new DozeWallpaperState(context, getBiometricUnlockController(dozeService)),
Aran Ink4c0d5602019-06-21 14:27:32 -040075 new DozeDockHandler(context, machine, host, config, handler, dockManager),
76 new DozeAuthRemover(dozeService)
Adrian Roosff2c4562016-11-03 12:13:36 -070077 });
78
79 return machine;
80 }
81
Adrian Roosc7fd6962017-09-06 16:46:46 +020082 private DozeMachine.Part createDozeScreenBrightness(Context context,
Adrian Roosc8e29e72017-08-03 18:25:42 +020083 DozeMachine.Service service, SensorManager sensorManager, DozeHost host,
Adrian Roos2f5a3852018-04-23 17:48:08 +020084 DozeParameters params, Handler handler) {
Adrian Roos2981eb02017-05-26 18:40:09 -070085 Sensor sensor = DozeSensors.findSensorWithType(sensorManager,
86 context.getString(R.string.doze_brightness_sensor_type));
jackqdyulei8443dd02017-08-24 16:14:34 -070087 return new DozeScreenBrightness(context, service, sensorManager, sensor, host, handler,
Adrian Roos2f5a3852018-04-23 17:48:08 +020088 params.getPolicy());
Adrian Roos2981eb02017-05-26 18:40:09 -070089 }
90
Adrian Roosf9d13f62016-11-08 15:42:20 -080091 private DozeTriggers createDozeTriggers(Context context, SensorManager sensorManager,
Adrian Roos6023ccb2017-06-28 16:22:02 +020092 DozeHost host, AlarmManager alarmManager, AmbientDisplayConfiguration config,
lpeter8a5f4702019-01-18 16:53:07 +080093 DozeParameters params, Handler handler, WakeLock wakeLock, DozeMachine machine,
94 DockManager dockManager) {
Adrian Roos664c9d72017-04-28 15:52:24 -070095 boolean allowPulseTriggers = true;
Adrian Roos6023ccb2017-06-28 16:22:02 +020096 return new DozeTriggers(context, machine, host, alarmManager, config, params,
lpeter8a5f4702019-01-18 16:53:07 +080097 sensorManager, handler, wakeLock, allowPulseTriggers, dockManager);
Adrian Roosf9d13f62016-11-08 15:42:20 -080098 }
99
100 private DozeMachine.Part createDozeUi(Context context, DozeHost host, WakeLock wakeLock,
Lucas Dupin43d0d732017-11-16 11:23:49 -0800101 DozeMachine machine, Handler handler, AlarmManager alarmManager,
102 DozeParameters params) {
Lucas Dupin16cfe452018-02-08 13:14:50 -0800103 return new DozeUi(context, alarmManager, machine, wakeLock, host, handler, params,
104 KeyguardUpdateMonitor.getInstance(context));
Adrian Roosf9d13f62016-11-08 15:42:20 -0800105 }
106
Adrian Roos0b2e23a2017-01-03 12:13:49 -0800107 public static DozeHost getHost(DozeService service) {
Adrian Roosff2c4562016-11-03 12:13:36 -0700108 Application appCandidate = service.getApplication();
109 final SystemUIApplication app = (SystemUIApplication) appCandidate;
110 return app.getComponent(DozeHost.class);
111 }
Lucas Dupinbd6c92b2019-07-17 09:49:52 -0700112
113 public static BiometricUnlockController getBiometricUnlockController(DozeService service) {
114 Application appCandidate = service.getApplication();
115 final SystemUIApplication app = (SystemUIApplication) appCandidate;
116 return app.getComponent(BiometricUnlockController.class);
117 }
Adrian Roosff2c4562016-11-03 12:13:36 -0700118}