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