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.incallui.answerproximitysensor;
18 
19 import android.content.Context;
20 import android.hardware.display.DisplayManager;
21 import android.hardware.display.DisplayManager.DisplayListener;
22 import android.os.PowerManager;
23 import android.support.annotation.Nullable;
24 import android.view.Display;
25 import com.android.dialer.common.LogUtil;
26 
27 /** The normal PROXIMITY_SCREEN_OFF_WAKE_LOCK provided by the OS. */
28 public class SystemProximityWakeLock implements AnswerProximityWakeLock, DisplayListener {
29 
30   private static final String TAG = "SystemProximityWakeLock";
31 
32   private final Context context;
33   private final PowerManager.WakeLock wakeLock;
34 
35   @Nullable private ScreenOnListener listener;
36 
SystemProximityWakeLock(Context context)37   public SystemProximityWakeLock(Context context) {
38     this.context = context;
39     wakeLock =
40         context
41             .getSystemService(PowerManager.class)
42             .newWakeLock(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, TAG);
43   }
44 
45   @Override
acquire()46   public void acquire() {
47     wakeLock.acquire();
48     context.getSystemService(DisplayManager.class).registerDisplayListener(this, null);
49   }
50 
51   @Override
release()52   public void release() {
53     wakeLock.release();
54     context.getSystemService(DisplayManager.class).unregisterDisplayListener(this);
55   }
56 
57   @Override
isHeld()58   public boolean isHeld() {
59     return wakeLock.isHeld();
60   }
61 
62   @Override
setScreenOnListener(ScreenOnListener listener)63   public void setScreenOnListener(ScreenOnListener listener) {
64     this.listener = listener;
65   }
66 
67   @Override
onDisplayAdded(int displayId)68   public void onDisplayAdded(int displayId) {}
69 
70   @Override
onDisplayRemoved(int displayId)71   public void onDisplayRemoved(int displayId) {}
72 
73   @Override
onDisplayChanged(int displayId)74   public void onDisplayChanged(int displayId) {
75     if (displayId == Display.DEFAULT_DISPLAY) {
76       if (isDefaultDisplayOn(context)) {
77         LogUtil.i("SystemProximityWakeLock.onDisplayChanged", "display turned on");
78         if (listener != null) {
79           listener.onScreenOn();
80         }
81       }
82     }
83   }
84 
isDefaultDisplayOn(Context context)85   private static boolean isDefaultDisplayOn(Context context) {
86     Display display =
87         context.getSystemService(DisplayManager.class).getDisplay(Display.DEFAULT_DISPLAY);
88     return display.getState() != Display.STATE_OFF;
89   }
90 }
91