• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.car.systeminterface;
18 
19 import android.content.Context;
20 import android.os.PowerManager;
21 import android.os.PowerManager.WakeLock;
22 
23 import com.android.car.CarLog;
24 
25 /**
26  * Interface that abstracts wake lock operations
27  */
28 public interface WakeLockInterface {
releaseAllWakeLocks()29     void releaseAllWakeLocks();
switchToPartialWakeLock()30     void switchToPartialWakeLock();
switchToFullWakeLock()31     void switchToFullWakeLock();
32 
33     class DefaultImpl implements WakeLockInterface {
34         private final WakeLock mPartialWakeLock;
35         private final WakeLock mFullWakeLock;
36 
DefaultImpl(Context context)37         DefaultImpl(Context context) {
38             PowerManager powerManager =
39                     (PowerManager) context.getSystemService(Context.POWER_SERVICE);
40             mFullWakeLock = powerManager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK,
41                 CarLog.TAG_POWER);
42             mPartialWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
43                 CarLog.TAG_POWER);
44         }
45 
46         @Override
switchToPartialWakeLock()47         public void switchToPartialWakeLock() {
48             if (!mPartialWakeLock.isHeld()) {
49                 mPartialWakeLock.acquire();
50             }
51             if (mFullWakeLock.isHeld()) {
52                 mFullWakeLock.release();
53             }
54         }
55 
56         @Override
switchToFullWakeLock()57         public void switchToFullWakeLock() {
58             if (!mFullWakeLock.isHeld()) {
59                 mFullWakeLock.acquire();
60             }
61             if (mPartialWakeLock.isHeld()) {
62                 mPartialWakeLock.release();
63             }
64         }
65 
66         @Override
releaseAllWakeLocks()67         public void releaseAllWakeLocks() {
68             if (mPartialWakeLock.isHeld()) {
69                 mPartialWakeLock.release();
70             }
71             if (mFullWakeLock.isHeld()) {
72                 mFullWakeLock.release();
73             }
74         }
75     }
76 }
77