blob: 90cb05a8e6ee1852bd43db41adf15b3b636d4ed9 [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;
lpeter8a5f4702019-01-18 16:53:07 +080031import com.android.systemui.dock.DockManager;
Lucas Dupin7a0e5bc2019-08-09 10:35:34 -070032import com.android.systemui.keyguard.WakefulnessLifecycle;
Dave Mankoff781ef7e2019-06-28 16:33:25 -040033import com.android.systemui.plugins.FalsingManager;
Lucas Dupinbd6c92b2019-07-17 09:49:52 -070034import com.android.systemui.statusbar.phone.BiometricUnlockController;
Adrian Roosff2c4562016-11-03 12:13:36 -070035import com.android.systemui.statusbar.phone.DozeParameters;
Adrian Roos7a8ae8a2017-08-02 16:26:50 +020036import com.android.systemui.util.AsyncSensorManager;
Adrian Roos0fb55ae2017-04-14 14:49:11 -070037import com.android.systemui.util.wakelock.DelayedWakeLock;
Adrian Roosc1b50322017-02-27 21:07:58 +010038import com.android.systemui.util.wakelock.WakeLock;
Adrian Roosff2c4562016-11-03 12:13:36 -070039
40public class DozeFactory {
41
Adrian Roos664c9d72017-04-28 15:52:24 -070042 public DozeFactory() {
Adrian Roosf9d13f62016-11-08 15:42:20 -080043 }
44
Adrian Roosff2c4562016-11-03 12:13:36 -070045 /** Creates a DozeMachine with its parts for {@code dozeService}. */
Dave Mankoff781ef7e2019-06-28 16:33:25 -040046 public DozeMachine assembleMachine(DozeService dozeService, FalsingManager falsingManager) {
Adrian Roosff2c4562016-11-03 12:13:36 -070047 Context context = dozeService;
Adrian Roos7a8ae8a2017-08-02 16:26:50 +020048 SensorManager sensorManager = Dependency.get(AsyncSensorManager.class);
Adrian Roosfe54aa02016-11-07 14:14:25 -080049 AlarmManager alarmManager = context.getSystemService(AlarmManager.class);
Lucas Dupind7221352019-04-29 19:43:11 -070050 DockManager dockManager = Dependency.get(DockManager.class);
Lucas Dupin7a0e5bc2019-08-09 10:35:34 -070051 WakefulnessLifecycle wakefulnessLifecycle = Dependency.get(WakefulnessLifecycle.class);
Adrian Roosff2c4562016-11-03 12:13:36 -070052
53 DozeHost host = getHost(dozeService);
54 AmbientDisplayConfiguration config = new AmbientDisplayConfiguration(context);
Lucas Dupin16cfe452018-02-08 13:14:50 -080055 DozeParameters params = DozeParameters.getInstance(context);
Adrian Roosff2c4562016-11-03 12:13:36 -070056 Handler handler = new Handler();
Adrian Roos0fb55ae2017-04-14 14:49:11 -070057 WakeLock wakeLock = new DelayedWakeLock(handler,
58 WakeLock.createPartial(context, "Doze"));
Adrian Roosff2c4562016-11-03 12:13:36 -070059
Adrian Roos3e23eb52017-07-07 15:58:57 +020060 DozeMachine.Service wrappedService = dozeService;
61 wrappedService = new DozeBrightnessHostForwarder(wrappedService, host);
62 wrappedService = DozeScreenStatePreventingAdapter.wrapIfNeeded(wrappedService, params);
63 wrappedService = DozeSuspendScreenStatePreventingAdapter.wrapIfNeeded(wrappedService,
64 params);
65
Lucas Dupin7a0e5bc2019-08-09 10:35:34 -070066 DozeMachine machine = new DozeMachine(wrappedService, config, wakeLock,
67 wakefulnessLifecycle);
Adrian Roosff2c4562016-11-03 12:13:36 -070068 machine.setParts(new DozeMachine.Part[]{
Adrian Roos2f5a3852018-04-23 17:48:08 +020069 new DozePauser(handler, machine, alarmManager, params.getPolicy()),
Dave Mankoff781ef7e2019-06-28 16:33:25 -040070 new DozeFalsingManagerAdapter(falsingManager),
Adrian Roos6023ccb2017-06-28 16:22:02 +020071 createDozeTriggers(context, sensorManager, host, alarmManager, config, params,
lpeter8a5f4702019-01-18 16:53:07 +080072 handler, wakeLock, machine, dockManager),
Lucas Dupin43d0d732017-11-16 11:23:49 -080073 createDozeUi(context, host, wakeLock, machine, handler, alarmManager, params),
Lucas Dupin16cfe452018-02-08 13:14:50 -080074 new DozeScreenState(wrappedService, handler, params, wakeLock),
Adrian Roos2f5a3852018-04-23 17:48:08 +020075 createDozeScreenBrightness(context, wrappedService, sensorManager, host, params,
76 handler),
Lucas Dupinbd6c92b2019-07-17 09:49:52 -070077 new DozeWallpaperState(context, getBiometricUnlockController(dozeService)),
Aran Ink4c0d5602019-06-21 14:27:32 -040078 new DozeDockHandler(context, machine, host, config, handler, dockManager),
79 new DozeAuthRemover(dozeService)
Adrian Roosff2c4562016-11-03 12:13:36 -070080 });
81
82 return machine;
83 }
84
Adrian Roosc7fd6962017-09-06 16:46:46 +020085 private DozeMachine.Part createDozeScreenBrightness(Context context,
Adrian Roosc8e29e72017-08-03 18:25:42 +020086 DozeMachine.Service service, SensorManager sensorManager, DozeHost host,
Adrian Roos2f5a3852018-04-23 17:48:08 +020087 DozeParameters params, Handler handler) {
Adrian Roos2981eb02017-05-26 18:40:09 -070088 Sensor sensor = DozeSensors.findSensorWithType(sensorManager,
89 context.getString(R.string.doze_brightness_sensor_type));
jackqdyulei8443dd02017-08-24 16:14:34 -070090 return new DozeScreenBrightness(context, service, sensorManager, sensor, host, handler,
Adrian Roos2f5a3852018-04-23 17:48:08 +020091 params.getPolicy());
Adrian Roos2981eb02017-05-26 18:40:09 -070092 }
93
Adrian Roosf9d13f62016-11-08 15:42:20 -080094 private DozeTriggers createDozeTriggers(Context context, SensorManager sensorManager,
Adrian Roos6023ccb2017-06-28 16:22:02 +020095 DozeHost host, AlarmManager alarmManager, AmbientDisplayConfiguration config,
lpeter8a5f4702019-01-18 16:53:07 +080096 DozeParameters params, Handler handler, WakeLock wakeLock, DozeMachine machine,
97 DockManager dockManager) {
Adrian Roos664c9d72017-04-28 15:52:24 -070098 boolean allowPulseTriggers = true;
Adrian Roos6023ccb2017-06-28 16:22:02 +020099 return new DozeTriggers(context, machine, host, alarmManager, config, params,
lpeter8a5f4702019-01-18 16:53:07 +0800100 sensorManager, handler, wakeLock, allowPulseTriggers, dockManager);
Adrian Roosf9d13f62016-11-08 15:42:20 -0800101 }
102
103 private DozeMachine.Part createDozeUi(Context context, DozeHost host, WakeLock wakeLock,
Lucas Dupin43d0d732017-11-16 11:23:49 -0800104 DozeMachine machine, Handler handler, AlarmManager alarmManager,
105 DozeParameters params) {
Lucas Dupin16cfe452018-02-08 13:14:50 -0800106 return new DozeUi(context, alarmManager, machine, wakeLock, host, handler, params,
107 KeyguardUpdateMonitor.getInstance(context));
Adrian Roosf9d13f62016-11-08 15:42:20 -0800108 }
109
Adrian Roos0b2e23a2017-01-03 12:13:49 -0800110 public static DozeHost getHost(DozeService service) {
Adrian Roosff2c4562016-11-03 12:13:36 -0700111 Application appCandidate = service.getApplication();
112 final SystemUIApplication app = (SystemUIApplication) appCandidate;
113 return app.getComponent(DozeHost.class);
114 }
Lucas Dupinbd6c92b2019-07-17 09:49:52 -0700115
116 public static BiometricUnlockController getBiometricUnlockController(DozeService service) {
117 Application appCandidate = service.getApplication();
118 final SystemUIApplication app = (SystemUIApplication) appCandidate;
119 return app.getComponent(BiometricUnlockController.class);
120 }
Adrian Roosff2c4562016-11-03 12:13:36 -0700121}