OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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.chrome.browser.omnibox.geo; |
| 6 |
| 7 import android.content.Context; |
| 8 import android.os.AsyncTask; |
| 9 import android.os.SystemClock; |
| 10 |
| 11 import org.chromium.base.Log; |
| 12 import org.chromium.base.ThreadUtils; |
| 13 import org.chromium.base.VisibleForTesting; |
| 14 import org.chromium.base.annotations.SuppressFBWarnings; |
| 15 |
| 16 import javax.annotation.Nullable; |
| 17 |
| 18 /** |
| 19 * VisibleNetworksTracker keeps track of the visible networks. |
| 20 */ |
| 21 public class VisibleNetworksTracker { |
| 22 private static final String TAG = "VNTracker"; |
| 23 |
| 24 @VisibleForTesting |
| 25 static final int AGE_THRESHOLD = 5 * 60 * 1000; // 5 min |
| 26 |
| 27 @Nullable |
| 28 private static VisibleNetworks sVisibleNetworks; |
| 29 private static long sVisibleNetworksTime = Long.MAX_VALUE; |
| 30 |
| 31 @Nullable |
| 32 private static AsyncTask<Void, Void, VisibleNetworks> sOngoingRefresh; |
| 33 |
| 34 private static VisibleNetworks sVisibleNetworksForTesting; |
| 35 private static boolean sUseVisibleNetworksForTesting; |
| 36 |
| 37 /** |
| 38 * Returns last known visible networks. It returns the cached value if the c
ache is valid or it |
| 39 * computes the simplest possible visibleNetworks fast, and triggers a backg
round asynchronous |
| 40 * refresh. Might return null if visible networks cannot be computed. |
| 41 */ |
| 42 @Nullable |
| 43 public static VisibleNetworks getLastKnownVisibleNetworks(final Context cont
ext) { |
| 44 if (sUseVisibleNetworksForTesting) return sVisibleNetworksForTesting; |
| 45 |
| 46 if (isValidCachedVisibleNetworks()) return getCachedVisibleNetworks(); |
| 47 |
| 48 VisibleNetworks visibleNetworks = null; |
| 49 try { |
| 50 // Include only the connected cell/wifi to minimize latency and comp
ute the simplest |
| 51 // visible networks possible. |
| 52 visibleNetworks = PlatformNetworksManager.computeVisibleNetworks( |
| 53 context, false /* includeAllVisibleNotConnectedNetworks */); |
| 54 } catch (Exception e) { |
| 55 Log.e(TAG, "Failed to get the visible networks. Error: ", e.toString
()); |
| 56 } |
| 57 // Update cache asynchronously. |
| 58 refreshVisibleNetworks(context); |
| 59 |
| 60 return visibleNetworks; |
| 61 } |
| 62 |
| 63 /** |
| 64 * Determines if the visible networks need to be refreshed and asynchronousl
y updates them if |
| 65 * needed. |
| 66 */ |
| 67 @SuppressFBWarnings("LI_LAZY_INIT_UPDATE_STATIC") |
| 68 public static void refreshVisibleNetworks(final Context context) { |
| 69 ThreadUtils.assertOnUiThread(); |
| 70 if (isValidCachedVisibleNetworks() || sOngoingRefresh != null) { |
| 71 return; |
| 72 } |
| 73 sOngoingRefresh = new AsyncTask<Void, Void, VisibleNetworks>() { |
| 74 @Override |
| 75 protected VisibleNetworks doInBackground(Void... params) { |
| 76 VisibleNetworks visibleNetworks = null; |
| 77 try { |
| 78 // Include all visible wifis and cells. |
| 79 visibleNetworks = PlatformNetworksManager.computeVisibleNetw
orks( |
| 80 context, true /* includeAllVisibleNotConnectedNetwor
ks */); |
| 81 } catch (Exception e) { |
| 82 Log.e(TAG, "Failed to get the visible networks. Error: ", e.
toString()); |
| 83 } |
| 84 return visibleNetworks; |
| 85 } |
| 86 |
| 87 @Override |
| 88 protected void onPostExecute(VisibleNetworks visibleNetworks) { |
| 89 sOngoingRefresh = null; |
| 90 setCachedVisibleNetworks(visibleNetworks); |
| 91 } |
| 92 }; |
| 93 sOngoingRefresh.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); |
| 94 } |
| 95 |
| 96 @Nullable |
| 97 @VisibleForTesting |
| 98 static VisibleNetworks getCachedVisibleNetworks() { |
| 99 return sVisibleNetworks; |
| 100 } |
| 101 |
| 102 @VisibleForTesting |
| 103 static long getCachedVisibleNetworksTime() { |
| 104 return sVisibleNetworksTime; |
| 105 } |
| 106 |
| 107 @VisibleForTesting |
| 108 static void clearCache() { |
| 109 setCachedVisibleNetworks(null); |
| 110 sVisibleNetworksTime = Long.MAX_VALUE; |
| 111 } |
| 112 |
| 113 @VisibleForTesting |
| 114 static void setVisibleNetworksForTesting(VisibleNetworks visibleNetworksForT
esting) { |
| 115 sVisibleNetworksForTesting = visibleNetworksForTesting; |
| 116 sUseVisibleNetworksForTesting = true; |
| 117 } |
| 118 |
| 119 private static void setCachedVisibleNetworks(VisibleNetworks visibleNetworks
) { |
| 120 ThreadUtils.assertOnUiThread(); |
| 121 sVisibleNetworks = visibleNetworks; |
| 122 sVisibleNetworksTime = SystemClock.elapsedRealtime(); |
| 123 } |
| 124 |
| 125 private static boolean isValidCachedVisibleNetworks() { |
| 126 return sVisibleNetworks != null && sVisibleNetworksTime != Long.MAX_VALU
E |
| 127 && !sVisibleNetworks.isEmpty() |
| 128 && SystemClock.elapsedRealtime() - sVisibleNetworksTime < AGE_TH
RESHOLD; |
| 129 } |
| 130 } |
OLD | NEW |