blob: fc3d1a52342f55a542b60e234210a7b0c44a5cde [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),
Aran Ink4c0d5602019-06-21 14:27:32 -040074 new DozeDockHandler(context, machine, host, config, handler, dockManager),
75 new DozeAuthRemover(dozeService)
Adrian Roosff2c4562016-11-03 12:13:36 -070076 });
77
78 return machine;
79 }
80
Adrian Roosc7fd6962017-09-06 16:46:46 +020081 private DozeMachine.Part createDozeScreenBrightness(Context context,
Adrian Roosc8e29e72017-08-03 18:25:42 +020082 DozeMachine.Service service, SensorManager sensorManager, DozeHost host,
Adrian Roos2f5a3852018-04-23 17:48:08 +020083 DozeParameters params, Handler handler) {
Adrian Roos2981eb02017-05-26 18:40:09 -070084 Sensor sensor = DozeSensors.findSensorWithType(sensorManager,
85 context.getString(R.string.doze_brightness_sensor_type));
jackqdyulei8443dd02017-08-24 16:14:34 -070086 return new DozeScreenBrightness(context, service, sensorManager, sensor, host, handler,
Adrian Roos2f5a3852018-04-23 17:48:08 +020087 params.getPolicy());
Adrian Roos2981eb02017-05-26 18:40:09 -070088 }
89
Adrian Roosf9d13f62016-11-08 15:42:20 -080090 private DozeTriggers createDozeTriggers(Context context, SensorManager sensorManager,
Adrian Roos6023ccb2017-06-28 16:22:02 +020091 DozeHost host, AlarmManager alarmManager, AmbientDisplayConfiguration config,
lpeter8a5f4702019-01-18 16:53:07 +080092 DozeParameters params, Handler handler, WakeLock wakeLock, DozeMachine machine,
93 DockManager dockManager) {
Adrian Roos664c9d72017-04-28 15:52:24 -070094 boolean allowPulseTriggers = true;
Adrian Roos6023ccb2017-06-28 16:22:02 +020095 return new DozeTriggers(context, machine, host, alarmManager, config, params,
lpeter8a5f4702019-01-18 16:53:07 +080096 sensorManager, handler, wakeLock, allowPulseTriggers, dockManager);
Adrian Roosf9d13f62016-11-08 15:42:20 -080097 }
98
99 private DozeMachine.Part createDozeUi(Context context, DozeHost host, WakeLock wakeLock,
Lucas Dupin43d0d732017-11-16 11:23:49 -0800100 DozeMachine machine, Handler handler, AlarmManager alarmManager,
101 DozeParameters params) {
Lucas Dupin16cfe452018-02-08 13:14:50 -0800102 return new DozeUi(context, alarmManager, machine, wakeLock, host, handler, params,
103 KeyguardUpdateMonitor.getInstance(context));
Adrian Roosf9d13f62016-11-08 15:42:20 -0800104 }
105
Adrian Roos0b2e23a2017-01-03 12:13:49 -0800106 public static DozeHost getHost(DozeService service) {
Adrian Roosff2c4562016-11-03 12:13:36 -0700107 Application appCandidate = service.getApplication();
108 final SystemUIApplication app = (SystemUIApplication) appCandidate;
109 return app.getComponent(DozeHost.class);
110 }
Adrian Roosff2c4562016-11-03 12:13:36 -0700111}