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 java.util.concurrent.CopyOnWriteArrayList;
20 
21 /**
22  * Provides helpers for emergency sessions.
23  */
24 public abstract class EmergencyHelper {
25 
26     private final CopyOnWriteArrayList<EmergencyStateChangedListener> mListeners;
27 
EmergencyHelper()28     protected EmergencyHelper() {
29         mListeners = new CopyOnWriteArrayList<>();
30     }
31 
32     /**
33      * Listener for emergency state changes.
34      */
35     public interface EmergencyStateChangedListener {
36         /**
37          * Called when state changes.
38          */
onStateChanged()39         void onStateChanged();
40     }
41 
42     /**
43      * Returns true if the device is in an emergency session, or if an emergency session ended
44      * within the given extension time.
45      */
isInEmergency(long extensionTimeMs)46     public abstract boolean isInEmergency(long extensionTimeMs);
47 
48     /**
49      * Add a listener for changes to the emergency location state.
50      */
addOnEmergencyStateChangedListener(EmergencyStateChangedListener listener)51     public void addOnEmergencyStateChangedListener(EmergencyStateChangedListener listener) {
52         mListeners.add(listener);
53     }
54 
55     /**
56      * Remove a listener for changes to the emergency location state.
57      */
removeOnEmergencyStateChangedListener(EmergencyStateChangedListener listener)58     public void removeOnEmergencyStateChangedListener(EmergencyStateChangedListener listener) {
59         mListeners.remove(listener);
60     }
61 
62     /**
63      * Notify listeners for emergency state of state change
64      */
dispatchEmergencyStateChanged()65     protected final void dispatchEmergencyStateChanged() {
66         for (EmergencyStateChangedListener listener : mListeners) {
67             listener.onStateChanged();
68         }
69     }
70 }
71