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 android.os.PowerManager.locationPowerSaveModeToString; 20 21 import static com.android.server.location.LocationManagerService.D; 22 import static com.android.server.location.LocationManagerService.TAG; 23 import static com.android.server.location.eventlog.LocationEventLog.EVENT_LOG; 24 25 import android.os.PowerManager.LocationPowerSaveMode; 26 import android.util.Log; 27 28 import java.util.concurrent.CopyOnWriteArrayList; 29 30 /** 31 * Provides accessors and listeners for location power save mode. 32 */ 33 public abstract class LocationPowerSaveModeHelper { 34 35 /** 36 * Listener for location power save mode changes. 37 */ 38 public interface LocationPowerSaveModeChangedListener { 39 /** 40 * Called when the location power save mode changes. 41 */ onLocationPowerSaveModeChanged(@ocationPowerSaveMode int locationPowerSaveMode)42 void onLocationPowerSaveModeChanged(@LocationPowerSaveMode int locationPowerSaveMode); 43 } 44 45 private final CopyOnWriteArrayList<LocationPowerSaveModeChangedListener> mListeners; 46 LocationPowerSaveModeHelper()47 public LocationPowerSaveModeHelper() { 48 mListeners = new CopyOnWriteArrayList<>(); 49 } 50 51 /** 52 * Add a listener for changes to location power save mode. Callbacks occur on an unspecified 53 * thread. 54 */ addListener(LocationPowerSaveModeChangedListener listener)55 public final void addListener(LocationPowerSaveModeChangedListener listener) { 56 mListeners.add(listener); 57 } 58 59 /** 60 * Removes a listener for changes to location power save mode. 61 */ removeListener(LocationPowerSaveModeChangedListener listener)62 public final void removeListener(LocationPowerSaveModeChangedListener listener) { 63 mListeners.remove(listener); 64 } 65 notifyLocationPowerSaveModeChanged( @ocationPowerSaveMode int locationPowerSaveMode)66 protected final void notifyLocationPowerSaveModeChanged( 67 @LocationPowerSaveMode int locationPowerSaveMode) { 68 if (D) { 69 Log.d(TAG, "location power save mode is now " + locationPowerSaveModeToString( 70 locationPowerSaveMode)); 71 } 72 EVENT_LOG.logLocationPowerSaveMode(locationPowerSaveMode); 73 74 for (LocationPowerSaveModeChangedListener listener : mListeners) { 75 listener.onLocationPowerSaveModeChanged(locationPowerSaveMode); 76 } 77 } 78 79 /** 80 * Returns the current location power save mode. 81 */ 82 @LocationPowerSaveMode getLocationPowerSaveMode()83 public abstract int getLocationPowerSaveMode(); 84 } 85