1 /*
2  * Copyright (C) 2020 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.location.injector;
18 
19 import static com.android.server.location.LocationManagerService.D;
20 import static com.android.server.location.LocationManagerService.TAG;
21 
22 import android.util.Log;
23 
24 import java.util.concurrent.CopyOnWriteArrayList;
25 
26 /**
27  * Provides accessors and listeners for screen interactive state (screen on/off).
28  */
29 public abstract class ScreenInteractiveHelper {
30 
31     /**
32      * Listener for screen interactive changes.
33      */
34     public interface ScreenInteractiveChangedListener {
35         /**
36          * Called when the screen interative state changes.
37          */
onScreenInteractiveChanged(boolean isInteractive)38         void onScreenInteractiveChanged(boolean isInteractive);
39     }
40 
41     private final CopyOnWriteArrayList<ScreenInteractiveChangedListener> mListeners;
42 
ScreenInteractiveHelper()43     public ScreenInteractiveHelper() {
44         mListeners = new CopyOnWriteArrayList<>();
45     }
46 
47     /**
48      * Add a listener for changes to screen interactive state. Callbacks occur on an unspecified
49      * thread.
50      */
addListener(ScreenInteractiveChangedListener listener)51     public final void addListener(ScreenInteractiveChangedListener listener) {
52         mListeners.add(listener);
53     }
54 
55     /**
56      * Removes a listener for changes to screen interactive state.
57      */
removeListener(ScreenInteractiveChangedListener listener)58     public final void removeListener(ScreenInteractiveChangedListener listener) {
59         mListeners.remove(listener);
60     }
61 
notifyScreenInteractiveChanged(boolean interactive)62     protected final void notifyScreenInteractiveChanged(boolean interactive) {
63         if (D) {
64             Log.d(TAG, "screen interactive is now " + interactive);
65         }
66 
67         for (ScreenInteractiveChangedListener listener : mListeners) {
68             listener.onScreenInteractiveChanged(interactive);
69         }
70     }
71 
72     /**
73      * Returns true if the screen is currently interactive, and false otherwise.
74      */
isInteractive()75     public abstract boolean isInteractive();
76 }
77