| 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 hasAndroidLocationPermission() and isSystemLocationSettingEnab
led() to determine | |
| 58 * if Chromium's location requests will return results. | |
| 59 */ | |
| 60 public boolean hasAndroidLocationPermission(Context context) { | |
| 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 /** | |
| 90 * Instantiate this to explain how to create a LocationUtils instance in | |
| 91 * LocationUtils.getInstance(). | |
| 92 */ | |
| 93 public interface Factory { public LocationUtils create(); } | |
| 94 | |
| 95 /** | |
| 96 * Call this to use a different subclass of LocationUtils throughout the pro
gram. | |
| 97 * This can be used by embedders in addition to tests. | |
| 98 */ | |
| 99 @VisibleForTesting | |
| 100 public static void setFactory(Factory factory) { | |
| 101 sFactory = factory; | |
| 102 sInstance = null; | |
| 103 } | |
| 104 } | |
| OLD | NEW |