From 1f607d3e6dee9ffa82624d10d7d3413a7ead4c6d Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Wed, 26 Aug 2015 21:28:53 +0200 Subject: [PATCH] improve net check for index updates; update index on app start This remove the magic number "1" from the network state check, and makes explicit that it is checking the active network connection. This is then used to check whether it is appropriate to update the index when FDroid first starts. --- F-Droid/src/org/fdroid/fdroid/FDroid.java | 5 ++- .../src/org/fdroid/fdroid/UpdateService.java | 32 ++++++++++++------- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/F-Droid/src/org/fdroid/fdroid/FDroid.java b/F-Droid/src/org/fdroid/fdroid/FDroid.java index 80346c758..b78c537c0 100644 --- a/F-Droid/src/org/fdroid/fdroid/FDroid.java +++ b/F-Droid/src/org/fdroid/fdroid/FDroid.java @@ -34,7 +34,6 @@ import android.support.v4.view.ViewPager; import android.support.v7.app.ActionBarActivity; import android.support.v7.app.AlertDialog; import android.text.TextUtils; -import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; @@ -100,6 +99,10 @@ public class FDroid extends ActionBarActivity { getContentResolver().registerContentObserver(uri, true, new AppObserver()); InstallIntoSystemDialogActivity.firstTime(this); + + if (UpdateService.isNetworkAvailableForUpdate(this)) { + UpdateService.updateNow(this); + } } @Override diff --git a/F-Droid/src/org/fdroid/fdroid/UpdateService.java b/F-Droid/src/org/fdroid/fdroid/UpdateService.java index b07573151..f40571198 100644 --- a/F-Droid/src/org/fdroid/fdroid/UpdateService.java +++ b/F-Droid/src/org/fdroid/fdroid/UpdateService.java @@ -305,18 +305,28 @@ public class UpdateService extends IntentService implements ProgressListener { return false; } - // If we are to update the repos only on wifi, make sure that - // connection is active - if (prefs.getBoolean(Preferences.PREF_UPD_WIFI_ONLY, false)) { - ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); - NetworkInfo.State wifi = conMan.getNetworkInfo(1).getState(); - if (wifi != NetworkInfo.State.CONNECTED && - wifi != NetworkInfo.State.CONNECTING) { - Log.i(TAG, "Skipping update - wifi not available"); - return false; - } + return isNetworkAvailableForUpdate(this); + } + + /** + * If we are to update the repos only on wifi, make sure that connection is active + */ + public static boolean isNetworkAvailableForUpdate(Context context) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); + + // this could be cellular or wifi + NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); + if (activeNetwork == null) + return false; + + if (activeNetwork.getType() != ConnectivityManager.TYPE_WIFI + && prefs.getBoolean(Preferences.PREF_UPD_WIFI_ONLY, false)) { + Log.i(TAG, "Skipping update - wifi not available"); + return false; + } else { + return activeNetwork.isConnectedOrConnecting(); } - return true; } @Override