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.
This commit is contained in:
Hans-Christoph Steiner 2015-08-26 21:28:53 +02:00
parent cba103af77
commit 1f607d3e6d
2 changed files with 25 additions and 12 deletions

View File

@ -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

View File

@ -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