blob: 8694d2ad607cae570d900d7f9e177512b11232df [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;
Adrian Roosff2c4562016-11-03 12:13:36 -070033import com.android.systemui.statusbar.phone.DozeParameters;
Adrian Roos7a8ae8a2017-08-02 16:26:50 +020034import com.android.systemui.util.AsyncSensorManager;
Adrian Roos0fb55ae2017-04-14 14:49:11 -070035import com.android.systemui.util.wakelock.DelayedWakeLock;
Adrian Roosc1b50322017-02-27 21:07:58 +010036import com.android.systemui.util.wakelock.WakeLock;
Adrian Roosff2c4562016-11-03 12:13:36 -070037
38public class DozeFactory {
39
Adrian Roos664c9d72017-04-28 15:52:24 -070040 public DozeFactory() {
Adrian Roosf9d13f62016-11-08 15:42:20 -080041 }
42
Adrian Roosff2c4562016-11-03 12:13:36 -070043 /** Creates a DozeMachine with its parts for {@code dozeService}. */
Adrian Roosf9d13f62016-11-08 15:42:20 -080044 public DozeMachine assembleMachine(DozeService dozeService) {
Adrian Roosff2c4562016-11-03 12:13:36 -070045 Context context = dozeService;
Adrian Roos7a8ae8a2017-08-02 16:26:50 +020046 SensorManager sensorManager = Dependency.get(AsyncSensorManager.class);
Adrian Roosfe54aa02016-11-07 14:14:25 -080047 AlarmManager alarmManager = context.getSystemService(AlarmManager.class);
Lucas Dupind7221352019-04-29 19:43:11 -070048 DockManager dockManager = Dependency.get(DockManager.class);
Adrian Roosff2c4562016-11-03 12:13:36 -070049
50 DozeHost host = getHost(dozeService);
51 AmbientDisplayConfiguration config = new AmbientDisplayConfiguration(context);
Lucas Dupin16cfe452018-02-08 13:14:50 -080052 DozeParameters params = DozeParameters.getInstance(context);
Adrian Roosff2c4562016-11-03 12:13:36 -070053 Handler handler = new Handler();
Adrian Roos0fb55ae2017-04-14 14:49:11 -070054 WakeLock wakeLock = new DelayedWakeLock(handler,
55 WakeLock.createPartial(context, "Doze"));
Adrian Roosff2c4562016-11-03 12:13:36 -070056
Adrian Roos3e23eb52017-07-07 15:58:57 +020057 DozeMachine.Service wrappedService = dozeService;
58 wrappedService = new DozeBrightnessHostForwarder(wrappedService, host);
59 wrappedService = DozeScreenStatePreventingAdapter.wrapIfNeeded(wrappedService, params);
60 wrappedService = DozeSuspendScreenStatePreventingAdapter.wrapIfNeeded(wrappedService,
61 params);
62
Adrian Roos2981eb02017-05-26 18:40:09 -070063 DozeMachine machine = new DozeMachine(wrappedService, config, wakeLock);
Adrian Roosff2c4562016-11-03 12:13:36 -070064 machine.setParts(new DozeMachine.Part[]{
Adrian Roos2f5a3852018-04-23 17:48:08 +020065 new DozePauser(handler, machine, alarmManager, params.getPolicy()),
Dave Mankoffdde5ee62019-05-02 17:36:11 -040066 new DozeFalsingManagerAdapter(FalsingManagerFactory.getInstance(context)),
Adrian Roos6023ccb2017-06-28 16:22:02 +020067 createDozeTriggers(context, sensorManager, host, alarmManager, config, params,
lpeter8a5f4702019-01-18 16:53:07 +080068 handler, wakeLock, machine, dockManager),
Lucas Dupin43d0d732017-11-16 11:23:49 -080069 createDozeUi(context, host, wakeLock, machine, handler, alarmManager, params),
Lucas Dupin16cfe452018-02-08 13:14:50 -080070 new DozeScreenState(wrappedService, handler, params, wakeLock),
Adrian Roos2f5a3852018-04-23 17:48:08 +020071 createDozeScreenBrightness(context, wrappedService, sensorManager, host, params,
72 handler),
Lucas Dupin5f00fa52019-03-27 22:46:53 -070073 new DozeWallpaperState(context),
lpeter8a5f4702019-01-18 16:53:07 +080074 new DozeDockHandler(context, machine, host, config, handler, dockManager)
Adrian Roosff2c4562016-11-03 12:13:36 -070075 });
76
77 return machine;
78 }
79
Adrian Roosc7fd6962017-09-06 16:46:46 +020080 private DozeMachine.Part createDozeScreenBrightness(Context context,
Adrian Roosc8e29e72017-08-03 18:25:42 +020081 DozeMachine.Service service, SensorManager sensorManager, DozeHost host,
Adrian Roos2f5a3852018-04-23 17:48:08 +020082 DozeParameters params, Handler handler) {
Adrian Roos2981eb02017-05-26 18:40:09 -070083 Sensor sensor = DozeSensors.findSensorWithType(sensorManager,
84 context.getString(R.string.doze_brightness_sensor_type));
jackqdyulei8443dd02017-08-24 16:14:34 -070085 return new DozeScreenBrightness(context, service, sensorManager, sensor, host, handler,
Adrian Roos2f5a3852018-04-23 17:48:08 +020086 params.getPolicy());
Adrian Roos2981eb02017-05-26 18:40:09 -070087 }
88
Adrian Roosf9d13f62016-11-08 15:42:20 -080089 private DozeTriggers createDozeTriggers(Context context, SensorManager sensorManager,
Adrian Roos6023ccb2017-06-28 16:22:02 +020090 DozeHost host, AlarmManager alarmManager, AmbientDisplayConfiguration config,
lpeter8a5f4702019-01-18 16:53:07 +080091 DozeParameters params, Handler handler, WakeLock wakeLock, DozeMachine machine,
92 DockManager dockManager) {
Adrian Roos664c9d72017-04-28 15:52:24 -070093 boolean allowPulseTriggers = true;
Adrian Roos6023ccb2017-06-28 16:22:02 +020094 return new DozeTriggers(context, machine, host, alarmManager, config, params,
lpeter8a5f4702019-01-18 16:53:07 +080095 sensorManager, handler, wakeLock, allowPulseTriggers, dockManager);
Adrian Roosf9d13f62016-11-08 15:42:20 -080096 }
97
98 private DozeMachine.Part createDozeUi(Context context, DozeHost host, WakeLock wakeLock,
Lucas Dupin43d0d732017-11-16 11:23:49 -080099 DozeMachine machine, Handler handler, AlarmManager alarmManager,
100 DozeParameters params) {
Lucas Dupin16cfe452018-02-08 13:14:50 -0800101 return new DozeUi(context, alarmManager, machine, wakeLock, host, handler, params,
102 KeyguardUpdateMonitor.getInstance(context));
Adrian Roosf9d13f62016-11-08 15:42:20 -0800103 }
104
Adrian Roos0b2e23a2017-01-03 12:13:49 -0800105 public static DozeHost getHost(DozeService service) {
Adrian Roosff2c4562016-11-03 12:13:36 -0700106 Application appCandidate = service.getApplication();
107 final SystemUIApplication app = (SystemUIApplication) appCandidate;
108 return app.getComponent(DozeHost.class);
109 }
Adrian Roosff2c4562016-11-03 12:13:36 -0700110}