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 17package com.android.systemui.doze; 18 19import android.app.AlarmManager; 20import android.app.Application; 21import android.content.Context; 22import android.hardware.SensorManager; 23import android.os.Handler; 24 25import com.android.internal.hardware.AmbientDisplayConfiguration; 26import com.android.systemui.SystemUIApplication; 27import com.android.systemui.statusbar.phone.DozeParameters; 28import com.android.systemui.util.wakelock.WakeLock; 29 30public class DozeFactory { 31 32 public DozeFactory() { 33 } 34 35 /** Creates a DozeMachine with its parts for {@code dozeService}. */ 36 public DozeMachine assembleMachine(DozeService dozeService) { 37 Context context = dozeService; 38 SensorManager sensorManager = context.getSystemService(SensorManager.class); 39 AlarmManager alarmManager = context.getSystemService(AlarmManager.class); 40 41 DozeHost host = getHost(dozeService); 42 AmbientDisplayConfiguration config = new AmbientDisplayConfiguration(context); 43 DozeParameters params = new DozeParameters(context); 44 Handler handler = new Handler(); 45 WakeLock wakeLock = WakeLock.createPartial(context, "Doze"); 46 47 DozeMachine machine = new DozeMachine( 48 DozeScreenStatePreventingAdapter.wrapIfNeeded(dozeService, params), 49 config, 50 wakeLock); 51 machine.setParts(new DozeMachine.Part[]{ 52 createDozeTriggers(context, sensorManager, host, config, params, handler, wakeLock, 53 machine), 54 createDozeUi(context, host, wakeLock, machine, handler, alarmManager), 55 }); 56 57 return machine; 58 } 59 60 private DozeTriggers createDozeTriggers(Context context, SensorManager sensorManager, 61 DozeHost host, AmbientDisplayConfiguration config, DozeParameters params, 62 Handler handler, WakeLock wakeLock, DozeMachine machine) { 63 boolean allowPulseTriggers = true; 64 return new DozeTriggers(context, machine, host, config, params, 65 sensorManager, handler, wakeLock, allowPulseTriggers); 66 } 67 68 private DozeMachine.Part createDozeUi(Context context, DozeHost host, WakeLock wakeLock, 69 DozeMachine machine, Handler handler, AlarmManager alarmManager) { 70 return new DozeUi(context, alarmManager, machine, wakeLock, host, handler); 71 } 72 73 public static DozeHost getHost(DozeService service) { 74 Application appCandidate = service.getApplication(); 75 final SystemUIApplication app = (SystemUIApplication) appCandidate; 76 return app.getComponent(DozeHost.class); 77 } 78} 79