diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index a8e9d7557..91ea6c205 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -271,6 +271,9 @@ android:name=".DeleteCacheService" android:exported="false"/> + = 16 && cm.isActiveNetworkMetered()) { - return FLAG_NET_METERED; - } else { - return FLAG_NET_NO_LIMIT; - } - default: - return FLAG_NET_METERED; - } - } - /** * In order to send a {@link Toast} from a {@link IntentService}, we have to do these tricks. */ @@ -371,8 +339,8 @@ public class UpdateService extends IntentService { try { // See if it's time to actually do anything yet... - int netState = getNetworkState(this); - if (netState == FLAG_NET_UNAVAILABLE) { + int netState = ConnectivityMonitorService.getNetworkState(this); + if (netState == ConnectivityMonitorService.FLAG_NET_UNAVAILABLE) { Utils.debugLog(TAG, "No internet, cannot update"); if (manualUpdate) { sendNoInternetToast(); @@ -380,10 +348,10 @@ public class UpdateService extends IntentService { return; } + final Preferences fdroidPrefs = Preferences.get(); if (manualUpdate || forcedUpdate) { Utils.debugLog(TAG, "manually requested or forced update"); - } else if (!verifyIsTimeForScheduledRun() - || (netState == FLAG_NET_METERED && Preferences.get().isUpdateOnlyOnUnmeteredNetworks())) { + } else if (!verifyIsTimeForScheduledRun() || !fdroidPrefs.isBackgroundDownloadAllowed()) { Utils.debugLog(TAG, "don't run update"); return; } @@ -403,7 +371,6 @@ public class UpdateService extends IntentService { ArrayList repoErrors = new ArrayList<>(); boolean changes = false; boolean singleRepoUpdate = !TextUtils.isEmpty(address); - final Preferences fdroidPrefs = Preferences.get(); for (final Repo repo : repos) { if (!repo.inuse) { continue; @@ -442,7 +409,7 @@ public class UpdateService extends IntentService { } // now that downloading the index is done, start downloading updates - if (changes && fdroidPrefs.isAutoDownloadEnabled()) { + if (changes && fdroidPrefs.isAutoDownloadEnabled() && fdroidPrefs.isBackgroundDownloadAllowed()) { autoDownloadUpdates(this); } } diff --git a/app/src/main/java/org/fdroid/fdroid/net/ConnectivityMonitorService.java b/app/src/main/java/org/fdroid/fdroid/net/ConnectivityMonitorService.java new file mode 100644 index 000000000..13df8ee6f --- /dev/null +++ b/app/src/main/java/org/fdroid/fdroid/net/ConnectivityMonitorService.java @@ -0,0 +1,86 @@ +package org.fdroid.fdroid.net; + +import android.app.IntentService; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; +import android.net.ConnectivityManager; +import android.net.NetworkInfo; +import android.os.Build; +import com.nostra13.universalimageloader.core.ImageLoader; +import org.fdroid.fdroid.FDroidApp; +import org.fdroid.fdroid.Preferences; + +/** + * An {@link IntentService} subclass for tracking whether there is metered or + * unmetered internet available, based on + * {@link android.net.ConnectivityManager#CONNECTIVITY_ACTION} + */ +public class ConnectivityMonitorService extends IntentService { + public static final String TAG = "ConnectivityMonitorServ"; + + public static final int FLAG_NET_UNAVAILABLE = 0; + public static final int FLAG_NET_METERED = 1; + public static final int FLAG_NET_NO_LIMIT = 2; + + private static final String ACTION_START = "org.fdroid.fdroid.net.action.CONNECTIVITY_MONITOR"; + + private static final BroadcastReceiver CONNECTIVITY_RECEIVER = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + start(context); + } + }; + + public ConnectivityMonitorService() { + super("ConnectivityMonitorService"); + } + + public static void registerAndStart(Context context) { + context.registerReceiver(CONNECTIVITY_RECEIVER, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); + } + + public static void start(Context context) { + Intent intent = new Intent(context, ConnectivityMonitorService.class); + intent.setAction(ACTION_START); + context.startService(intent); + } + + /** + * Gets the state of internet availability, whether there is no connection at all, + * whether the connection has no usage limit (like most WiFi), or whether this is + * a metered connection like most cellular plans or hotspot WiFi connections. + */ + public static int getNetworkState(Context context) { + ConnectivityManager cm = (ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE); + if (cm == null) { + return FLAG_NET_UNAVAILABLE; + } + NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); + if (activeNetwork == null || !activeNetwork.isConnected()) { + return FLAG_NET_UNAVAILABLE; + } + + int networkType = activeNetwork.getType(); + switch (networkType) { + case ConnectivityManager.TYPE_ETHERNET: + case ConnectivityManager.TYPE_WIFI: + if (Build.VERSION.SDK_INT >= 16 && cm.isActiveNetworkMetered()) { + return FLAG_NET_METERED; + } else { + return FLAG_NET_NO_LIMIT; + } + default: + return FLAG_NET_METERED; + } + } + + @Override + protected void onHandleIntent(Intent intent) { + if (intent != null && ACTION_START.equals(intent.getAction())) { + FDroidApp.networkState = getNetworkState(this); + ImageLoader.getInstance().denyNetworkDownloads(!Preferences.get().isBackgroundDownloadAllowed()); + } + } +} diff --git a/app/src/main/java/org/fdroid/fdroid/views/ScreenShotsActivity.java b/app/src/main/java/org/fdroid/fdroid/views/ScreenShotsActivity.java index bc88526df..4e930ce06 100644 --- a/app/src/main/java/org/fdroid/fdroid/views/ScreenShotsActivity.java +++ b/app/src/main/java/org/fdroid/fdroid/views/ScreenShotsActivity.java @@ -18,6 +18,7 @@ import android.widget.ImageView; import com.nostra13.universalimageloader.core.DisplayImageOptions; import com.nostra13.universalimageloader.core.ImageLoader; import org.fdroid.fdroid.FDroidApp; +import org.fdroid.fdroid.Preferences; import org.fdroid.fdroid.R; import org.fdroid.fdroid.Utils; import org.fdroid.fdroid.data.App; @@ -36,6 +37,8 @@ public class ScreenShotsActivity extends AppCompatActivity { private static final String EXTRA_PACKAGE_NAME = "EXTRA_PACKAGE_NAME"; private static final String EXTRA_START_POSITION = "EXTRA_START_POSITION"; + private static final ImageLoader IMAGE_LOADER = ImageLoader.getInstance(); + public static Intent getStartIntent(Context context, String packageName, int startPosition) { Intent intent = new Intent(context, ScreenShotsActivity.class); intent.putExtra(EXTRA_PACKAGE_NAME, packageName); @@ -68,6 +71,18 @@ public class ScreenShotsActivity extends AppCompatActivity { } } + @Override + protected void onResume() { + super.onResume(); + IMAGE_LOADER.denyNetworkDownloads(false); + } + + @Override + protected void onPause() { + super.onPause(); + IMAGE_LOADER.denyNetworkDownloads(!Preferences.get().isBackgroundDownloadAllowed()); + } + private static class ScreenShotPagerAdapter extends FragmentStatePagerAdapter { private final String[] screenshots;