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.preferences; | 5 package org.chromium.chrome.browser.preferences; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
8 import android.content.Intent; | 8 import android.content.Intent; |
9 import android.location.LocationManager; | 9 import android.location.LocationManager; |
10 import android.provider.Settings; | 10 import android.provider.Settings; |
11 | 11 |
12 import org.chromium.base.ApplicationStatus; | 12 import org.chromium.base.ApplicationStatus; |
13 import org.chromium.base.CalledByNative; | 13 import org.chromium.base.CalledByNative; |
14 import org.chromium.base.ThreadUtils; | 14 import org.chromium.base.ThreadUtils; |
15 import org.chromium.base.VisibleForTesting; | 15 import org.chromium.base.VisibleForTesting; |
16 import org.chromium.base.annotations.SuppressFBWarnings; | 16 import org.chromium.base.annotations.SuppressFBWarnings; |
17 import org.chromium.chrome.browser.ChromiumApplication; | 17 import org.chromium.chrome.browser.ChromiumApplication; |
18 | 18 |
19 import java.util.concurrent.Callable; | |
20 import java.util.concurrent.ExecutionException; | |
21 | |
22 /** | 19 /** |
23 * Provides methods for querying Android system-wide location settings as well a
s Chrome's internal | 20 * Provides methods for querying Android system-wide location settings as well a
s Chrome's internal |
24 * location setting. | 21 * location setting. |
| 22 * |
| 23 * This class should be used only on the UI thread. |
25 */ | 24 */ |
26 public class LocationSettings { | 25 public class LocationSettings { |
27 | 26 |
28 private static LocationSettings sInstance; | 27 private static LocationSettings sInstance; |
29 | 28 |
30 protected final Context mContext; | 29 protected final Context mContext; |
31 | 30 |
32 /** | 31 /** |
33 * Don't use this; use getInstance() instead. This should be used only by th
e Application inside | 32 * Don't use this; use getInstance() instead. This should be used only by th
e Application inside |
34 * of createLocationSettings(). | 33 * of createLocationSettings(). |
35 */ | 34 */ |
36 public LocationSettings(Context context) { | 35 public LocationSettings(Context context) { |
37 mContext = context; | 36 mContext = context; |
38 } | 37 } |
39 | 38 |
40 /** | 39 /** |
41 * Returns the singleton instance of LocationSettings, creating it if needed
. | 40 * Returns the singleton instance of LocationSettings, creating it if needed
. |
42 */ | 41 */ |
43 @SuppressFBWarnings("LI_LAZY_INIT_STATIC") | 42 @SuppressFBWarnings("LI_LAZY_INIT_STATIC") |
44 public static LocationSettings getInstance() { | 43 public static LocationSettings getInstance() { |
| 44 ThreadUtils.assertOnUiThread(); |
45 if (sInstance == null) { | 45 if (sInstance == null) { |
46 ChromiumApplication application = | 46 ChromiumApplication application = |
47 (ChromiumApplication) ApplicationStatus.getApplicationContex
t(); | 47 (ChromiumApplication) ApplicationStatus.getApplicationContex
t(); |
48 sInstance = application.createLocationSettings(); | 48 sInstance = application.createLocationSettings(); |
49 } | 49 } |
50 return sInstance; | 50 return sInstance; |
51 } | 51 } |
52 | 52 |
53 /** | 53 /** |
54 * Returns true if location is enabled system-wide and the Chrome location s
etting is enabled. | 54 * Returns true if location is enabled system-wide and the Chrome location s
etting is enabled. |
55 */ | 55 */ |
56 @CalledByNative | 56 @CalledByNative |
57 public static boolean areAllLocationSettingsEnabled() { | 57 public static boolean areAllLocationSettingsEnabled() { |
58 try { | 58 LocationSettings settings = LocationSettings.getInstance(); |
59 return ThreadUtils.runOnUiThreadBlocking(new Callable<Boolean>(){ | 59 return settings.isChromeLocationSettingEnabled() |
60 @Override | 60 && settings.isSystemLocationSettingEnabled(); |
61 public Boolean call() throws Exception { | |
62 LocationSettings settings = LocationSettings.getInstance(); | |
63 return settings.isChromeLocationSettingEnabled() | |
64 && settings.isSystemLocationSettingEnabled(); | |
65 } | |
66 }); | |
67 } catch (ExecutionException e) { | |
68 e.printStackTrace(); | |
69 return false; | |
70 } | |
71 } | 61 } |
72 | 62 |
73 /** | 63 /** |
74 * Returns whether Chrome's user-configurable location setting is enabled. | 64 * Returns whether Chrome's user-configurable location setting is enabled. |
75 */ | 65 */ |
76 public boolean isChromeLocationSettingEnabled() { | 66 public boolean isChromeLocationSettingEnabled() { |
77 return PrefServiceBridge.getInstance().isAllowLocationEnabled(); | 67 return PrefServiceBridge.getInstance().isAllowLocationEnabled(); |
78 } | 68 } |
79 | 69 |
80 /** | 70 /** |
(...skipping 14 matching lines...) Expand all Loading... |
95 Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); | 85 Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); |
96 i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | 86 i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
97 return i; | 87 return i; |
98 } | 88 } |
99 | 89 |
100 @VisibleForTesting | 90 @VisibleForTesting |
101 public static void setInstanceForTesting(LocationSettings instance) { | 91 public static void setInstanceForTesting(LocationSettings instance) { |
102 sInstance = instance; | 92 sInstance = instance; |
103 } | 93 } |
104 } | 94 } |
OLD | NEW |