1 /*
2  * Copyright (C) 2022 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.server.usb;
18 
19 import android.os.PowerManagerInternal;
20 import android.util.Log;
21 
22 import com.android.server.LocalServices;
23 
24 import java.time.Instant;
25 
26 /**
27  * Sends power boost events to the power manager.
28  */
29 public class PowerBoostSetter {
30     private static final String TAG = "PowerBoostSetter";
31     // Set power boost timeout to 15 seconds
32     private static final int POWER_BOOST_TIMEOUT_MS = 15 * 1000;
33 
34     PowerManagerInternal mPowerManagerInternal = null;
35     Instant mPreviousTimeout = null;
36 
PowerBoostSetter()37     PowerBoostSetter() {
38         mPowerManagerInternal = LocalServices.getService(PowerManagerInternal.class);
39     }
40 
41     /**
42      * Boosts the CPU clock frequency as if the screen is touched
43      */
boostPower()44     public void boostPower() {
45         if (mPowerManagerInternal == null) {
46             mPowerManagerInternal = LocalServices.getService(PowerManagerInternal.class);
47         }
48 
49         if (mPowerManagerInternal == null) {
50             Log.w(TAG, "PowerManagerInternal null");
51         } else if ((mPreviousTimeout == null) || Instant.now().isAfter(
52                 mPreviousTimeout.plusMillis(POWER_BOOST_TIMEOUT_MS / 2))) {
53             // Only boost if the previous timeout is at least halfway done
54             mPreviousTimeout = Instant.now();
55             mPowerManagerInternal.setPowerBoost(PowerManagerInternal.BOOST_INTERACTION,
56                     POWER_BOOST_TIMEOUT_MS);
57         }
58     }
59 }
60