Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui.doze; |
| 18 | |
Adrian Roos | fe54aa0 | 2016-11-07 14:14:25 -0800 | [diff] [blame] | 19 | import android.app.AlarmManager; |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 20 | import android.app.Application; |
| 21 | import android.content.Context; |
Adrian Roos | 2981eb0 | 2017-05-26 18:40:09 -0700 | [diff] [blame] | 22 | import android.hardware.Sensor; |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 23 | import android.hardware.SensorManager; |
Issei Suzuki | ca19e6e | 2019-02-26 12:39:11 +0100 | [diff] [blame] | 24 | import android.hardware.display.AmbientDisplayConfiguration; |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 25 | import android.os.Handler; |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 26 | |
Lucas Dupin | 16cfe45 | 2018-02-08 13:14:50 -0800 | [diff] [blame] | 27 | import com.android.keyguard.KeyguardUpdateMonitor; |
Adrian Roos | 7a8ae8a | 2017-08-02 16:26:50 +0200 | [diff] [blame] | 28 | import com.android.systemui.Dependency; |
Adrian Roos | 2981eb0 | 2017-05-26 18:40:09 -0700 | [diff] [blame] | 29 | import com.android.systemui.R; |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 30 | import com.android.systemui.SystemUIApplication; |
Dave Mankoff | dde5ee6 | 2019-05-02 17:36:11 -0400 | [diff] [blame] | 31 | import com.android.systemui.classifier.FalsingManagerFactory; |
lpeter | 8a5f470 | 2019-01-18 16:53:07 +0800 | [diff] [blame] | 32 | import com.android.systemui.dock.DockManager; |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 33 | import com.android.systemui.statusbar.phone.DozeParameters; |
Adrian Roos | 7a8ae8a | 2017-08-02 16:26:50 +0200 | [diff] [blame] | 34 | import com.android.systemui.util.AsyncSensorManager; |
Adrian Roos | 0fb55ae | 2017-04-14 14:49:11 -0700 | [diff] [blame] | 35 | import com.android.systemui.util.wakelock.DelayedWakeLock; |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 36 | import com.android.systemui.util.wakelock.WakeLock; |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 37 | |
| 38 | public class DozeFactory { |
| 39 | |
Adrian Roos | 664c9d7 | 2017-04-28 15:52:24 -0700 | [diff] [blame] | 40 | public DozeFactory() { |
Adrian Roos | f9d13f6 | 2016-11-08 15:42:20 -0800 | [diff] [blame] | 41 | } |
| 42 | |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 43 | /** Creates a DozeMachine with its parts for {@code dozeService}. */ |
Adrian Roos | f9d13f6 | 2016-11-08 15:42:20 -0800 | [diff] [blame] | 44 | public DozeMachine assembleMachine(DozeService dozeService) { |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 45 | Context context = dozeService; |
Adrian Roos | 7a8ae8a | 2017-08-02 16:26:50 +0200 | [diff] [blame] | 46 | SensorManager sensorManager = Dependency.get(AsyncSensorManager.class); |
Adrian Roos | fe54aa0 | 2016-11-07 14:14:25 -0800 | [diff] [blame] | 47 | AlarmManager alarmManager = context.getSystemService(AlarmManager.class); |
Lucas Dupin | d722135 | 2019-04-29 19:43:11 -0700 | [diff] [blame] | 48 | DockManager dockManager = Dependency.get(DockManager.class); |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 49 | |
| 50 | DozeHost host = getHost(dozeService); |
| 51 | AmbientDisplayConfiguration config = new AmbientDisplayConfiguration(context); |
Lucas Dupin | 16cfe45 | 2018-02-08 13:14:50 -0800 | [diff] [blame] | 52 | DozeParameters params = DozeParameters.getInstance(context); |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 53 | Handler handler = new Handler(); |
Adrian Roos | 0fb55ae | 2017-04-14 14:49:11 -0700 | [diff] [blame] | 54 | WakeLock wakeLock = new DelayedWakeLock(handler, |
| 55 | WakeLock.createPartial(context, "Doze")); |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 56 | |
Adrian Roos | 3e23eb5 | 2017-07-07 15:58:57 +0200 | [diff] [blame] | 57 | 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 Roos | 2981eb0 | 2017-05-26 18:40:09 -0700 | [diff] [blame] | 63 | DozeMachine machine = new DozeMachine(wrappedService, config, wakeLock); |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 64 | machine.setParts(new DozeMachine.Part[]{ |
Adrian Roos | 2f5a385 | 2018-04-23 17:48:08 +0200 | [diff] [blame] | 65 | new DozePauser(handler, machine, alarmManager, params.getPolicy()), |
Dave Mankoff | dde5ee6 | 2019-05-02 17:36:11 -0400 | [diff] [blame] | 66 | new DozeFalsingManagerAdapter(FalsingManagerFactory.getInstance(context)), |
Adrian Roos | 6023ccb | 2017-06-28 16:22:02 +0200 | [diff] [blame] | 67 | createDozeTriggers(context, sensorManager, host, alarmManager, config, params, |
lpeter | 8a5f470 | 2019-01-18 16:53:07 +0800 | [diff] [blame] | 68 | handler, wakeLock, machine, dockManager), |
Lucas Dupin | 43d0d73 | 2017-11-16 11:23:49 -0800 | [diff] [blame] | 69 | createDozeUi(context, host, wakeLock, machine, handler, alarmManager, params), |
Lucas Dupin | 16cfe45 | 2018-02-08 13:14:50 -0800 | [diff] [blame] | 70 | new DozeScreenState(wrappedService, handler, params, wakeLock), |
Adrian Roos | 2f5a385 | 2018-04-23 17:48:08 +0200 | [diff] [blame] | 71 | createDozeScreenBrightness(context, wrappedService, sensorManager, host, params, |
| 72 | handler), |
Lucas Dupin | 5f00fa5 | 2019-03-27 22:46:53 -0700 | [diff] [blame] | 73 | new DozeWallpaperState(context), |
Aran Ink | 4c0d560 | 2019-06-21 14:27:32 -0400 | [diff] [blame] | 74 | new DozeDockHandler(context, machine, host, config, handler, dockManager), |
| 75 | new DozeAuthRemover(dozeService) |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 76 | }); |
| 77 | |
| 78 | return machine; |
| 79 | } |
| 80 | |
Adrian Roos | c7fd696 | 2017-09-06 16:46:46 +0200 | [diff] [blame] | 81 | private DozeMachine.Part createDozeScreenBrightness(Context context, |
Adrian Roos | c8e29e7 | 2017-08-03 18:25:42 +0200 | [diff] [blame] | 82 | DozeMachine.Service service, SensorManager sensorManager, DozeHost host, |
Adrian Roos | 2f5a385 | 2018-04-23 17:48:08 +0200 | [diff] [blame] | 83 | DozeParameters params, Handler handler) { |
Adrian Roos | 2981eb0 | 2017-05-26 18:40:09 -0700 | [diff] [blame] | 84 | Sensor sensor = DozeSensors.findSensorWithType(sensorManager, |
| 85 | context.getString(R.string.doze_brightness_sensor_type)); |
jackqdyulei | 8443dd0 | 2017-08-24 16:14:34 -0700 | [diff] [blame] | 86 | return new DozeScreenBrightness(context, service, sensorManager, sensor, host, handler, |
Adrian Roos | 2f5a385 | 2018-04-23 17:48:08 +0200 | [diff] [blame] | 87 | params.getPolicy()); |
Adrian Roos | 2981eb0 | 2017-05-26 18:40:09 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Adrian Roos | f9d13f6 | 2016-11-08 15:42:20 -0800 | [diff] [blame] | 90 | private DozeTriggers createDozeTriggers(Context context, SensorManager sensorManager, |
Adrian Roos | 6023ccb | 2017-06-28 16:22:02 +0200 | [diff] [blame] | 91 | DozeHost host, AlarmManager alarmManager, AmbientDisplayConfiguration config, |
lpeter | 8a5f470 | 2019-01-18 16:53:07 +0800 | [diff] [blame] | 92 | DozeParameters params, Handler handler, WakeLock wakeLock, DozeMachine machine, |
| 93 | DockManager dockManager) { |
Adrian Roos | 664c9d7 | 2017-04-28 15:52:24 -0700 | [diff] [blame] | 94 | boolean allowPulseTriggers = true; |
Adrian Roos | 6023ccb | 2017-06-28 16:22:02 +0200 | [diff] [blame] | 95 | return new DozeTriggers(context, machine, host, alarmManager, config, params, |
lpeter | 8a5f470 | 2019-01-18 16:53:07 +0800 | [diff] [blame] | 96 | sensorManager, handler, wakeLock, allowPulseTriggers, dockManager); |
Adrian Roos | f9d13f6 | 2016-11-08 15:42:20 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | private DozeMachine.Part createDozeUi(Context context, DozeHost host, WakeLock wakeLock, |
Lucas Dupin | 43d0d73 | 2017-11-16 11:23:49 -0800 | [diff] [blame] | 100 | DozeMachine machine, Handler handler, AlarmManager alarmManager, |
| 101 | DozeParameters params) { |
Lucas Dupin | 16cfe45 | 2018-02-08 13:14:50 -0800 | [diff] [blame] | 102 | return new DozeUi(context, alarmManager, machine, wakeLock, host, handler, params, |
| 103 | KeyguardUpdateMonitor.getInstance(context)); |
Adrian Roos | f9d13f6 | 2016-11-08 15:42:20 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Adrian Roos | 0b2e23a | 2017-01-03 12:13:49 -0800 | [diff] [blame] | 106 | public static DozeHost getHost(DozeService service) { |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 107 | Application appCandidate = service.getApplication(); |
| 108 | final SystemUIApplication app = (SystemUIApplication) appCandidate; |
| 109 | return app.getComponent(DozeHost.class); |
| 110 | } |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 111 | } |