Chromium Code Reviews| 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.components.location; | |
| 6 | |
| 7 import android.Manifest; | |
| 8 import android.content.Context; | |
| 9 import android.content.Intent; | |
| 10 import android.content.pm.PackageManager; | |
| 11 import android.os.Build; | |
| 12 import android.os.Process; | |
| 13 import android.provider.Settings; | |
| 14 import android.text.TextUtils; | |
| 15 | |
| 16 import org.chromium.base.ThreadUtils; | |
| 17 import org.chromium.base.VisibleForTesting; | |
| 18 import org.chromium.base.annotations.SuppressFBWarnings; | |
| 19 | |
| 20 /** | |
| 21 * Provides methods for querying Chrome's ability to use Android's location serv ices. | |
| 22 * | |
| 23 * This class should be used only on the UI thread. | |
| 24 */ | |
| 25 public class LocationUtils { | |
| 26 // Used to construct sInstance if that's null. | |
| 27 private static Factory sFactory; | |
| 28 | |
| 29 private static LocationUtils sInstance; | |
| 30 | |
| 31 protected LocationUtils() {} | |
| 32 | |
| 33 /** | |
| 34 * Returns the singleton instance of LocationSettings, creating it if needed . | |
| 35 */ | |
| 36 @SuppressFBWarnings("LI_LAZY_INIT_STATIC") | |
| 37 public static LocationUtils getInstance() { | |
| 38 ThreadUtils.assertOnUiThread(); | |
| 39 if (sInstance == null) { | |
| 40 if (sFactory == null) { | |
| 41 sInstance = new LocationUtils(); | |
| 42 } else { | |
| 43 sInstance = sFactory.create(); | |
| 44 } | |
| 45 } | |
| 46 return sInstance; | |
| 47 } | |
| 48 | |
| 49 private boolean hasPermission(Context context, String name) { | |
| 50 return context.checkPermission(name, Process.myPid(), Process.myUid()) | |
| 51 == PackageManager.PERMISSION_GRANTED; | |
| 52 } | |
| 53 | |
| 54 /** | |
| 55 * Returns true if Chromium has permission to access location. | |
| 56 * | |
| 57 * Check both chromeHasLocationPermission() and isSystemLocationSettingEnabl ed() to determine if | |
|
Ted C
2016/06/13 19:56:21
looks like this comment predates the rename to chr
Jeffrey Yasskin
2016/06/16 16:02:33
Done.
| |
| 58 * Chromium's location requests will return results. | |
| 59 */ | |
| 60 public boolean chromiumHasLocationPermission(Context context) { | |
|
Ted C
2016/06/13 19:56:21
personally, I would call this hasLocationAndroidPe
Finnur
2016/06/16 13:44:44
FWIW: I'm also uncomfortable with the chromium pre
Jeffrey Yasskin
2016/06/16 16:02:33
I was trying to distinguish this from some website
| |
| 61 return hasPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION ) | |
| 62 || hasPermission(context, Manifest.permission.ACCESS_FINE_LOCATI ON); | |
| 63 } | |
| 64 | |
| 65 /** | |
| 66 * Returns whether location services are enabled system-wide, i.e. whether a ny application is | |
| 67 * able to access location. | |
| 68 */ | |
| 69 public boolean isSystemLocationSettingEnabled(Context context) { | |
| 70 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { | |
| 71 return Settings.Secure.getInt(context.getContentResolver(), | |
| 72 Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATI ON_MODE_OFF) | |
| 73 != Settings.Secure.LOCATION_MODE_OFF; | |
| 74 } else { | |
| 75 return !TextUtils.isEmpty(Settings.Secure.getString( | |
| 76 context.getContentResolver(), Settings.Secure.LOCATION_PROVI DERS_ALLOWED)); | |
| 77 } | |
| 78 } | |
| 79 | |
| 80 /** | |
| 81 * Returns an intent to launch Android Location Settings. | |
| 82 */ | |
| 83 public Intent getSystemLocationSettingsIntent() { | |
| 84 Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); | |
| 85 i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | |
| 86 return i; | |
| 87 } | |
| 88 | |
| 89 /** Instantiate this to explain how to create a LocationUtils instance in | |
|
Ted C
2016/06/13 19:56:21
move this down a line to avoid the off by one inde
Jeffrey Yasskin
2016/06/16 16:02:33
Done.
| |
| 90 * LocationUtils.getInstance(). | |
| 91 */ | |
| 92 public interface Factory { public LocationUtils create(); } | |
| 93 | |
| 94 /** | |
| 95 * Call this to use a different subclass of LocationUtils throughout the pro gram. | |
| 96 * This can be used by embedders in addition to tests. | |
| 97 */ | |
| 98 @VisibleForTesting | |
| 99 public static void setFactory(Factory factory) { | |
| 100 sFactory = factory; | |
| 101 sInstance = null; | |
| 102 } | |
| 103 } | |
| OLD | NEW |