| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.device.geolocation; | 5 package org.chromium.device.geolocation; |
| 6 | 6 |
| 7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.location.Criteria; | 8 import android.location.Criteria; |
| 9 import android.location.Location; | 9 import android.location.Location; |
| 10 import android.location.LocationListener; | 10 import android.location.LocationListener; |
| 11 import android.location.LocationManager; | 11 import android.location.LocationManager; |
| 12 import android.os.Bundle; | 12 import android.os.Bundle; |
| 13 | 13 |
| 14 import org.chromium.base.ContextUtils; |
| 14 import org.chromium.base.Log; | 15 import org.chromium.base.Log; |
| 15 import org.chromium.base.ThreadUtils; | 16 import org.chromium.base.ThreadUtils; |
| 16 import org.chromium.base.VisibleForTesting; | 17 import org.chromium.base.VisibleForTesting; |
| 17 | 18 |
| 18 import java.util.List; | 19 import java.util.List; |
| 19 | 20 |
| 20 /** | 21 /** |
| 21 * This is a LocationProvider using Android APIs [1]. It is a separate class for
clarity | 22 * This is a LocationProvider using Android APIs [1]. It is a separate class for
clarity |
| 22 * so that it can manage all processing completely on the UI thread. The contain
er class | 23 * so that it can manage all processing completely on the UI thread. The contain
er class |
| 23 * ensures that the start/stop calls into this class are done on the UI thread. | 24 * ensures that the start/stop calls into this class are done on the UI thread. |
| 24 * | 25 * |
| 25 * [1] https://developer.android.com/reference/android/location/package-summary.
html | 26 * [1] https://developer.android.com/reference/android/location/package-summary.
html |
| 26 */ | 27 */ |
| 27 public class LocationProviderAndroid | 28 public class LocationProviderAndroid |
| 28 implements LocationListener, LocationProviderFactory.LocationProvider { | 29 implements LocationListener, LocationProviderFactory.LocationProvider { |
| 29 private static final String TAG = "cr_LocationProvider"; | 30 private static final String TAG = "cr_LocationProvider"; |
| 30 | 31 |
| 31 private Context mContext; | |
| 32 private LocationManager mLocationManager; | 32 private LocationManager mLocationManager; |
| 33 private boolean mIsRunning; | 33 private boolean mIsRunning; |
| 34 | 34 |
| 35 LocationProviderAndroid(Context context) { | 35 LocationProviderAndroid() {} |
| 36 mContext = context; | |
| 37 } | |
| 38 | 36 |
| 39 @Override | 37 @Override |
| 40 public void start(boolean enableHighAccuracy) { | 38 public void start(boolean enableHighAccuracy) { |
| 41 ThreadUtils.assertOnUiThread(); | 39 ThreadUtils.assertOnUiThread(); |
| 42 unregisterFromLocationUpdates(); | 40 unregisterFromLocationUpdates(); |
| 43 registerForLocationUpdates(enableHighAccuracy); | 41 registerForLocationUpdates(enableHighAccuracy); |
| 44 } | 42 } |
| 45 | 43 |
| 46 @Override | 44 @Override |
| 47 public void stop() { | 45 public void stop() { |
| (...skipping 26 matching lines...) Expand all Loading... |
| 74 @Override | 72 @Override |
| 75 public void onProviderDisabled(String provider) {} | 73 public void onProviderDisabled(String provider) {} |
| 76 | 74 |
| 77 @VisibleForTesting | 75 @VisibleForTesting |
| 78 public void setLocationManagerForTesting(LocationManager manager) { | 76 public void setLocationManagerForTesting(LocationManager manager) { |
| 79 mLocationManager = manager; | 77 mLocationManager = manager; |
| 80 } | 78 } |
| 81 | 79 |
| 82 private void createLocationManagerIfNeeded() { | 80 private void createLocationManagerIfNeeded() { |
| 83 if (mLocationManager != null) return; | 81 if (mLocationManager != null) return; |
| 84 mLocationManager = (LocationManager) mContext.getSystemService(Context.L
OCATION_SERVICE); | 82 mLocationManager = (LocationManager) ContextUtils.getApplicationContext(
).getSystemService( |
| 83 Context.LOCATION_SERVICE); |
| 85 if (mLocationManager == null) { | 84 if (mLocationManager == null) { |
| 86 Log.e(TAG, "Could not get location manager."); | 85 Log.e(TAG, "Could not get location manager."); |
| 87 } | 86 } |
| 88 } | 87 } |
| 89 | 88 |
| 90 /** | 89 /** |
| 91 * Registers this object with the location service. | 90 * Registers this object with the location service. |
| 92 */ | 91 */ |
| 93 private void registerForLocationUpdates(boolean enableHighAccuracy) { | 92 private void registerForLocationUpdates(boolean enableHighAccuracy) { |
| 94 createLocationManagerIfNeeded(); | 93 createLocationManagerIfNeeded(); |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 /* | 153 /* |
| 155 * Checks if the passive location provider is the only provider available | 154 * Checks if the passive location provider is the only provider available |
| 156 * in the system. | 155 * in the system. |
| 157 */ | 156 */ |
| 158 private boolean isOnlyPassiveLocationProviderEnabled() { | 157 private boolean isOnlyPassiveLocationProviderEnabled() { |
| 159 final List<String> providers = mLocationManager.getProviders(true); | 158 final List<String> providers = mLocationManager.getProviders(true); |
| 160 return providers != null && providers.size() == 1 | 159 return providers != null && providers.size() == 1 |
| 161 && providers.get(0).equals(LocationManager.PASSIVE_PROVIDER); | 160 && providers.get(0).equals(LocationManager.PASSIVE_PROVIDER); |
| 162 } | 161 } |
| 163 } | 162 } |
| OLD | NEW |