1 /*
2  * Copyright (C) 2017 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 
19 import android.view.Display;
20 
21 import androidx.annotation.VisibleForTesting;
22 
23 import com.android.systemui.statusbar.phone.DozeParameters;
24 
25 import java.util.concurrent.Executor;
26 
27 /**
28  * Prevents usage of doze screen states on devices that don't support them.
29  */
30 public class DozeScreenStatePreventingAdapter extends DozeMachine.Service.Delegate {
31 
32     @VisibleForTesting
DozeScreenStatePreventingAdapter(DozeMachine.Service inner, Executor bgExecutor)33     DozeScreenStatePreventingAdapter(DozeMachine.Service inner, Executor bgExecutor) {
34         super(inner, bgExecutor);
35     }
36 
37     @Override
setDozeScreenState(int state)38     public void setDozeScreenState(int state) {
39         if (state == Display.STATE_DOZE) {
40             state = Display.STATE_ON;
41         } else if (state == Display.STATE_DOZE_SUSPEND) {
42             state = Display.STATE_ON_SUSPEND;
43         }
44         super.setDozeScreenState(state);
45     }
46 
47     /**
48      * If the device supports the doze display state, return {@code inner}. Otherwise
49      * return a new instance of {@link DozeScreenStatePreventingAdapter} wrapping {@code inner}.
50      */
wrapIfNeeded(DozeMachine.Service inner, DozeParameters params, Executor bgExecutor)51     public static DozeMachine.Service wrapIfNeeded(DozeMachine.Service inner,
52             DozeParameters params, Executor bgExecutor) {
53         return isNeeded(params) ? new DozeScreenStatePreventingAdapter(inner, bgExecutor) : inner;
54     }
55 
isNeeded(DozeParameters params)56     private static boolean isNeeded(DozeParameters params) {
57         return !params.getDisplayStateSupported();
58     }
59 }
60