make "Only on WiFi" also apply to updates, graphics, and icons

The "Only on WiFi" pref originally only controlled index updates, but now
it makes sense to include all of the various files that are downloaded.

#1381
This commit is contained in:
Hans-Christoph Steiner 2018-04-06 15:50:57 +02:00
parent 7ba1966538
commit 5bd276c804
6 changed files with 125 additions and 43 deletions

View File

@ -271,6 +271,9 @@
android:name=".DeleteCacheService"
android:exported="false"/>
<service android:name=".net.WifiStateChangeService"/>
<service
android:name=".net.ConnectivityMonitorService"
android:exported="false"/>
<service android:name=".localrepo.SwapService"/>
<service
android:name=".installer.InstallManagerService"

View File

@ -64,6 +64,7 @@ import org.fdroid.fdroid.data.Repo;
import org.fdroid.fdroid.data.RepoProvider;
import org.fdroid.fdroid.installer.ApkFileProvider;
import org.fdroid.fdroid.installer.InstallHistoryService;
import org.fdroid.fdroid.net.ConnectivityMonitorService;
import org.fdroid.fdroid.net.ImageLoaderForUIL;
import org.fdroid.fdroid.net.WifiStateChangeService;
import org.fdroid.fdroid.views.hiding.HidingManager;
@ -110,6 +111,8 @@ public class FDroidApp extends Application {
public static volatile String bssid;
public static volatile Repo repo = new Repo();
public static volatile int networkState = ConnectivityMonitorService.FLAG_NET_UNAVAILABLE;
private static volatile String lastWorkingMirror = null;
private static volatile int numTries = Integer.MAX_VALUE;
private static volatile int timeout = 10000;
@ -374,11 +377,10 @@ public class FDroidApp extends Application {
}
});
final Context context = this;
Preferences.get().registerUnstableUpdatesChangeListener(new Preferences.ChangeListener() {
@Override
public void onPreferenceChange() {
AppProvider.Helper.calcSuggestedApks(context);
AppProvider.Helper.calcSuggestedApks(FDroidApp.this);
}
});
@ -434,6 +436,8 @@ public class FDroidApp extends Application {
.build();
ImageLoader.getInstance().init(config);
ConnectivityMonitorService.registerAndStart(this);
FDroidApp.initWifiSettings();
WifiStateChangeService.start(this, null);
// if the HTTPS pref changes, then update all affected things

View File

@ -6,6 +6,7 @@ import android.os.Build;
import android.preference.PreferenceManager;
import android.util.Log;
import info.guardianproject.netcipher.NetCipher;
import org.fdroid.fdroid.net.ConnectivityMonitorService;
import java.net.InetSocketAddress;
import java.net.Proxy;
@ -273,8 +274,14 @@ public final class Preferences implements SharedPreferences.OnSharedPreferenceCh
return preferences.getBoolean(PREF_AUTO_DOWNLOAD_INSTALL_UPDATES, false);
}
public boolean isUpdateOnlyOnUnmeteredNetworks() {
return preferences.getBoolean(PREF_UPD_WIFI_ONLY, false);
/**
* Do the network conditions and user preferences allow for things to be
* downloaded in the background.
*/
public boolean isBackgroundDownloadAllowed() {
return FDroidApp.networkState == ConnectivityMonitorService.FLAG_NET_NO_LIMIT ||
(FDroidApp.networkState == ConnectivityMonitorService.FLAG_NET_METERED
&& !preferences.getBoolean(PREF_UPD_WIFI_ONLY, false));
}
/**

View File

@ -27,8 +27,6 @@ import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
@ -49,6 +47,7 @@ import org.fdroid.fdroid.data.Repo;
import org.fdroid.fdroid.data.RepoProvider;
import org.fdroid.fdroid.data.Schema;
import org.fdroid.fdroid.installer.InstallManagerService;
import org.fdroid.fdroid.net.ConnectivityMonitorService;
import org.fdroid.fdroid.views.main.MainActivity;
import java.util.ArrayList;
@ -79,10 +78,6 @@ public class UpdateService extends IntentService {
private static final int NOTIFY_ID_UPDATING = 0;
private static final int FLAG_NET_UNAVAILABLE = 0;
private static final int FLAG_NET_METERED = 1;
private static final int FLAG_NET_NO_LIMIT = 2;
private static Handler toastHandler;
private NotificationManager notificationManager;
@ -312,33 +307,6 @@ public class UpdateService extends IntentService {
return true;
}
/**
* 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.
*/
private static int getNetworkState(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
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;
}
}
/**
* 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<CharSequence> 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);
}
}

View File

@ -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());
}
}
}

View File

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