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.provider; 18 19 import android.annotation.Nullable; 20 import android.content.Context; 21 import android.location.LocationManager; 22 import android.location.LocationResult; 23 import android.location.provider.ProviderRequest; 24 import android.os.Binder; 25 26 import com.android.internal.util.Preconditions; 27 import com.android.server.location.injector.Injector; 28 29 import java.util.Collection; 30 31 /** 32 * A location provider manager specifically for the passive provider. 33 */ 34 public class PassiveLocationProviderManager extends LocationProviderManager { 35 PassiveLocationProviderManager(Context context, Injector injector)36 public PassiveLocationProviderManager(Context context, Injector injector) { 37 super(context, injector, LocationManager.PASSIVE_PROVIDER, null); 38 } 39 40 @Override setRealProvider(AbstractLocationProvider provider)41 public void setRealProvider(AbstractLocationProvider provider) { 42 Preconditions.checkArgument(provider instanceof PassiveLocationProvider); 43 super.setRealProvider(provider); 44 } 45 46 @Override setMockProvider(@ullable MockLocationProvider provider)47 public void setMockProvider(@Nullable MockLocationProvider provider) { 48 if (provider != null) { 49 throw new IllegalArgumentException("Cannot mock the passive provider"); 50 } 51 } 52 53 /** 54 * Reports a new location to passive location provider clients. 55 */ updateLocation(LocationResult locationResult)56 public void updateLocation(LocationResult locationResult) { 57 synchronized (mMultiplexerLock) { 58 PassiveLocationProvider passive = (PassiveLocationProvider) mProvider.getProvider(); 59 Preconditions.checkState(passive != null); 60 61 final long identity = Binder.clearCallingIdentity(); 62 try { 63 passive.updateLocation(locationResult); 64 } finally { 65 Binder.restoreCallingIdentity(identity); 66 } 67 } 68 } 69 70 @Override mergeRegistrations(Collection<Registration> registrations)71 protected ProviderRequest mergeRegistrations(Collection<Registration> registrations) { 72 return new ProviderRequest.Builder().setIntervalMillis(0).build(); 73 } 74 75 @Override calculateRequestDelayMillis(long newIntervalMs, Collection<Registration> registrations)76 protected long calculateRequestDelayMillis(long newIntervalMs, 77 Collection<Registration> registrations) { 78 return 0; 79 } 80 81 @Override getServiceState()82 protected String getServiceState() { 83 return mProvider.getCurrentRequest().isActive() ? "registered" : "unregistered"; 84 } 85 } 86