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