OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.base; | |
6 | |
7 import android.Manifest; | |
8 import android.content.Context; | |
9 import android.content.Intent; | |
10 import android.content.pm.PackageManager; | |
11 import android.location.LocationManager; | |
12 import android.os.Process; | |
13 import android.provider.Settings; | |
14 | |
15 import org.chromium.base.annotations.SuppressFBWarnings; | |
16 | |
17 /** | |
18 * Provides methods for querying Chrome's ability to use Android's location serv ices. | |
19 * | |
20 * This class should be used only on the UI thread. | |
21 */ | |
22 public class LocationUtils { | |
23 private static LocationUtils sInstance; | |
24 | |
25 protected LocationUtils() {} | |
26 | |
27 /** | |
28 * Returns the singleton instance of LocationSettings, creating it if needed . | |
29 */ | |
30 @SuppressFBWarnings("LI_LAZY_INIT_STATIC") | |
31 public static LocationUtils getInstance() { | |
32 ThreadUtils.assertOnUiThread(); | |
33 if (sInstance == null) { | |
34 sInstance = new LocationUtils(); | |
35 } | |
36 return sInstance; | |
37 } | |
38 | |
39 private boolean hasPermission(Context context, String name) { | |
40 return context.checkPermission(name, Process.myPid(), Process.myUid()) | |
41 == PackageManager.PERMISSION_GRANTED; | |
42 } | |
43 | |
44 /** | |
45 * Returns true if Chromium has permission to access location. | |
46 * | |
47 * Check both chromeHasLocationPermission() and isSystemLocationSettingEnabl ed() to determine if | |
48 * Chromium's location requests will return results. | |
49 */ | |
50 public boolean chromiumHasLocationPermission(Context context) { | |
51 return hasPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION ) | |
52 || hasPermission(context, Manifest.permission.ACCESS_FINE_LOCATI ON); | |
53 } | |
54 | |
55 /** | |
56 * Returns whether location services are enabled system-wide, i.e. whether a ny application is | |
57 * able to access location. | |
58 */ | |
59 public boolean isSystemLocationSettingEnabled(Context context) { | |
60 LocationManager locationManager = | |
61 (LocationManager) context.getSystemService(Context.LOCATION_SERV ICE); | |
62 return (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVID ER) | |
Jeffrey Yasskin
2016/06/06 15:53:48
https://developer.android.com/reference/android/lo
Torne
2016/06/07 11:30:43
Should probably do what the documentation says.
Jeffrey Yasskin
2016/06/09 22:08:11
Done.
| |
63 || locationManager.isProviderEnabled(LocationManager.GPS_PROVIDE R)); | |
64 } | |
65 | |
66 /** | |
67 * Returns an intent to launch Android Location Settings. | |
68 */ | |
69 public Intent getSystemLocationSettingsIntent() { | |
70 Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); | |
71 i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | |
72 return i; | |
73 } | |
74 | |
75 @VisibleForTesting | |
76 public static void setInstanceForTesting(LocationUtils instance) { | |
77 sInstance = instance; | |
78 } | |
79 } | |
OLD | NEW |