OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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.chrome.browser.omnibox.geo; | 5 package org.chromium.chrome.browser.omnibox.geo; |
6 | 6 |
7 import android.Manifest; | 7 import android.Manifest; |
8 import android.content.Context; | 8 import android.content.Context; |
9 import android.content.pm.PackageManager; | 9 import android.content.pm.PackageManager; |
10 import android.location.Location; | 10 import android.location.Location; |
(...skipping 13 matching lines...) Expand all Loading... |
24 * getLastKnownLocation() returns the current best estimate of the location. If
possible, call | 24 * getLastKnownLocation() returns the current best estimate of the location. If
possible, call |
25 * refreshLastKnownLocation() several seconds before a location is needed to max
imize the chances | 25 * refreshLastKnownLocation() several seconds before a location is needed to max
imize the chances |
26 * that the location is known. | 26 * that the location is known. |
27 */ | 27 */ |
28 class GeolocationTracker { | 28 class GeolocationTracker { |
29 | 29 |
30 private static SelfCancelingListener sListener; | 30 private static SelfCancelingListener sListener; |
31 private static Location sNetworkLocationForTesting; | 31 private static Location sNetworkLocationForTesting; |
32 private static Location sGpsLocationForTesting; | 32 private static Location sGpsLocationForTesting; |
33 private static boolean sUseLocationForTesting; | 33 private static boolean sUseLocationForTesting; |
| 34 private static long sLocationAgeForTesting; |
| 35 private static boolean sUseLocationAgeForTesting; |
34 | 36 |
35 private static class SelfCancelingListener implements LocationListener { | 37 private static class SelfCancelingListener implements LocationListener { |
36 | 38 |
37 // Length of time before the location request should be canceled. This t
imeout ensures the | 39 // Length of time before the location request should be canceled. This t
imeout ensures the |
38 // device doesn't get stuck in an infinite loop trying and failing to ge
t a location, which | 40 // device doesn't get stuck in an infinite loop trying and failing to ge
t a location, which |
39 // would cause battery drain. See: http://crbug.com/309917 | 41 // would cause battery drain. See: http://crbug.com/309917 |
40 private static final int REQUEST_TIMEOUT_MS = 60 * 1000; // 60 sec. | 42 private static final int REQUEST_TIMEOUT_MS = 60 * 1000; // 60 sec. |
41 | 43 |
42 private final LocationManager mLocationManager; | 44 private final LocationManager mLocationManager; |
43 private final Handler mHandler; | 45 private final Handler mHandler; |
(...skipping 27 matching lines...) Expand all Loading... |
71 @Override | 73 @Override |
72 public void onStatusChanged(String provider, int status, Bundle extras)
{ } | 74 public void onStatusChanged(String provider, int status, Bundle extras)
{ } |
73 } | 75 } |
74 | 76 |
75 /** | 77 /** |
76 * Returns the age of location is milliseconds. | 78 * Returns the age of location is milliseconds. |
77 * Note: the age will be invalid if the system clock has been changed since
the location was | 79 * Note: the age will be invalid if the system clock has been changed since
the location was |
78 * created. If the apparent age is negative, Long.MAX_VALUE will be returned
. | 80 * created. If the apparent age is negative, Long.MAX_VALUE will be returned
. |
79 */ | 81 */ |
80 static long getLocationAge(Location location) { | 82 static long getLocationAge(Location location) { |
| 83 if (sUseLocationAgeForTesting) return sLocationAgeForTesting; |
81 long age = System.currentTimeMillis() - location.getTime(); | 84 long age = System.currentTimeMillis() - location.getTime(); |
82 return age >= 0 ? age : Long.MAX_VALUE; | 85 return age >= 0 ? age : Long.MAX_VALUE; |
83 } | 86 } |
84 | 87 |
85 /** | 88 /** |
86 * Returns the last known location or null if none is available. | 89 * Returns the last known location or null if none is available. |
87 * | 90 * |
88 * @param includeGpsFallback Whether the gps provider should also be used as
a fallback. | 91 * @param includeGpsFallback Whether the gps provider should also be used as
a fallback. |
89 * Otherwise only the network provider will be used. | 92 * Otherwise only the network provider will be used. |
90 */ | 93 */ |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 } | 148 } |
146 | 149 |
147 @VisibleForTesting | 150 @VisibleForTesting |
148 static void setLocationForTesting( | 151 static void setLocationForTesting( |
149 Location networkLocationForTesting, Location gpsLocationForTesting)
{ | 152 Location networkLocationForTesting, Location gpsLocationForTesting)
{ |
150 sNetworkLocationForTesting = networkLocationForTesting; | 153 sNetworkLocationForTesting = networkLocationForTesting; |
151 sGpsLocationForTesting = gpsLocationForTesting; | 154 sGpsLocationForTesting = gpsLocationForTesting; |
152 sUseLocationForTesting = true; | 155 sUseLocationForTesting = true; |
153 } | 156 } |
154 | 157 |
| 158 @VisibleForTesting |
| 159 static void setLocationAgeForTesting(Long locationAgeForTesting) { |
| 160 if (locationAgeForTesting == null) { |
| 161 sUseLocationAgeForTesting = false; |
| 162 return; |
| 163 } |
| 164 sLocationAgeForTesting = locationAgeForTesting; |
| 165 sUseLocationAgeForTesting = true; |
| 166 } |
| 167 |
155 private static boolean hasPermission(Context context, String permission) { | 168 private static boolean hasPermission(Context context, String permission) { |
156 return ApiCompatibilityUtils.checkPermission( | 169 return ApiCompatibilityUtils.checkPermission( |
157 context, permission, Process.myPid(), Process.myUid()) | 170 context, permission, Process.myPid(), Process.myUid()) |
158 == PackageManager.PERMISSION_GRANTED; | 171 == PackageManager.PERMISSION_GRANTED; |
159 } | 172 } |
160 | 173 |
161 private static Location chooseLocation( | 174 private static Location chooseLocation( |
162 Location networkLocation, Location gpsLocation, boolean includeGpsFa
llback) { | 175 Location networkLocation, Location gpsLocation, boolean includeGpsFa
llback) { |
163 if (!includeGpsFallback || gpsLocation == null) { | 176 if (!includeGpsFallback || gpsLocation == null) { |
164 return networkLocation; | 177 return networkLocation; |
165 } | 178 } |
166 | 179 |
167 if (networkLocation == null) { | 180 if (networkLocation == null) { |
168 return gpsLocation; | 181 return gpsLocation; |
169 } | 182 } |
170 | 183 |
171 // Both are not null, take the younger one. | 184 // Both are not null, take the younger one. |
172 return networkLocation.getTime() > gpsLocation.getTime() ? networkLocati
on : gpsLocation; | 185 return networkLocation.getTime() > gpsLocation.getTime() ? networkLocati
on : gpsLocation; |
173 } | 186 } |
174 } | 187 } |
OLD | NEW |