Keeping connectivity manager around rather than getting it every time
This commit is contained in:
parent
efd5780c31
commit
460da386ec
@ -42,6 +42,7 @@ abstract class BaseSuggestionsTask {
|
|||||||
@NonNull private final Application mApplication;
|
@NonNull private final Application mApplication;
|
||||||
@NonNull private final OkHttpClient mHttpClient = new OkHttpClient();
|
@NonNull private final OkHttpClient mHttpClient = new OkHttpClient();
|
||||||
@NonNull private final CacheControl mCacheControl;
|
@NonNull private final CacheControl mCacheControl;
|
||||||
|
@NonNull private final ConnectivityManager mConnectivityManager;
|
||||||
@NonNull private String mQuery;
|
@NonNull private String mQuery;
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
@ -59,6 +60,7 @@ abstract class BaseSuggestionsTask {
|
|||||||
mResultCallback = callback;
|
mResultCallback = callback;
|
||||||
mApplication = application;
|
mApplication = application;
|
||||||
mCacheControl = new CacheControl.Builder().maxStale(1, TimeUnit.DAYS).build();
|
mCacheControl = new CacheControl.Builder().maxStale(1, TimeUnit.DAYS).build();
|
||||||
|
mConnectivityManager = getConnectivityManager(mApplication);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
@ -116,7 +118,7 @@ abstract class BaseSuggestionsTask {
|
|||||||
if (System.currentTimeMillis() - INTERVAL_DAY < cacheFile.lastModified()) {
|
if (System.currentTimeMillis() - INTERVAL_DAY < cacheFile.lastModified()) {
|
||||||
return cacheFile;
|
return cacheFile;
|
||||||
}
|
}
|
||||||
if (!isNetworkConnected(app)) {
|
if (!isNetworkConnected()) {
|
||||||
return cacheFile;
|
return cacheFile;
|
||||||
}
|
}
|
||||||
InputStream in = null;
|
InputStream in = null;
|
||||||
@ -161,20 +163,16 @@ abstract class BaseSuggestionsTask {
|
|||||||
return cacheFile;
|
return cacheFile;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean isNetworkConnected(@NonNull Context context) {
|
private boolean isNetworkConnected() {
|
||||||
NetworkInfo networkInfo = getActiveNetworkInfo(context);
|
NetworkInfo networkInfo = mConnectivityManager.getActiveNetworkInfo();
|
||||||
return networkInfo != null && networkInfo.isConnected();
|
return networkInfo != null && networkInfo.isConnected();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@NonNull
|
||||||
private static NetworkInfo getActiveNetworkInfo(@NonNull Context context) {
|
private static ConnectivityManager getConnectivityManager(@NonNull Context context) {
|
||||||
ConnectivityManager connectivity = (ConnectivityManager) context
|
return (ConnectivityManager) context
|
||||||
.getApplicationContext()
|
.getApplicationContext()
|
||||||
.getSystemService(Context.CONNECTIVITY_SERVICE);
|
.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||||
if (connectivity == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return connectivity.getActiveNetworkInfo();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user