DozeFactory.java revision ff2c4563cdee60576847e161678549bc501e8d84
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.Application;
20import android.content.Context;
21import android.hardware.SensorManager;
22import android.os.Handler;
23import android.os.PowerManager;
24
25import com.android.internal.hardware.AmbientDisplayConfiguration;
26import com.android.systemui.SystemUIApplication;
27import com.android.systemui.statusbar.phone.DozeParameters;
28
29public class DozeFactory {
30
31    /** Creates a DozeMachine with its parts for {@code dozeService}. */
32    public static DozeMachine assembleMachine(DozeService dozeService) {
33        Context context = dozeService;
34        SensorManager sensorManager = context.getSystemService(SensorManager.class);
35        PowerManager powerManager = context.getSystemService(PowerManager.class);
36
37        DozeHost host = getHost(dozeService);
38        AmbientDisplayConfiguration config = new AmbientDisplayConfiguration(context);
39        DozeParameters params = new DozeParameters(context);
40        Handler handler = new Handler();
41        DozeFactory.WakeLock wakeLock = new DozeFactory.WakeLock(powerManager.newWakeLock(
42                PowerManager.PARTIAL_WAKE_LOCK, "Doze"));
43
44        DozeMachine machine = new DozeMachine(dozeService, params, wakeLock);
45        machine.setParts(new DozeMachine.Part[]{
46                new DozeTriggers(context, machine, host, config, params,
47                        sensorManager, handler, wakeLock),
48                new DozeUi(context, machine, wakeLock, host),
49        });
50
51        return machine;
52    }
53
54    private static DozeHost getHost(DozeService service) {
55        Application appCandidate = service.getApplication();
56        final SystemUIApplication app = (SystemUIApplication) appCandidate;
57        return app.getComponent(DozeHost.class);
58    }
59
60    /** A wrapper around {@link PowerManager.WakeLock} for testability. */
61    public static class WakeLock {
62        private final PowerManager.WakeLock mInner;
63
64        public WakeLock(PowerManager.WakeLock inner) {
65            mInner = inner;
66        }
67
68        /** @see PowerManager.WakeLock#acquire() */
69        public void acquire() {
70            mInner.acquire();
71        }
72
73        /** @see PowerManager.WakeLock#release() */
74        public void release() {
75            mInner.release();
76        }
77
78        /** @see PowerManager.WakeLock#wrap(Runnable) */
79        public Runnable wrap(Runnable runnable) {
80            return mInner.wrap(runnable);
81        }
82    }
83}
84