diff --git a/app/src/main/java/com/tonyodev/fetchdemo/App.java b/app/src/main/java/com/tonyodev/fetchdemo/App.java index 27f9db66..d5a80ad1 100644 --- a/app/src/main/java/com/tonyodev/fetchdemo/App.java +++ b/app/src/main/java/com/tonyodev/fetchdemo/App.java @@ -17,6 +17,13 @@ public class App extends Application { @Override public void onCreate() { super.onCreate(); + + //Set settings for Fetch + new Fetch.Settings(this) + .setAllowedNetwork(Fetch.NETWORK_ALL) + .enableLogging(true) + .apply(); + fetch = Fetch.getInstance(this); fetch.addFetchListener(new FetchListener() { diff --git a/fetch/src/main/java/com/tonyodev/fetch/Fetch.java b/fetch/src/main/java/com/tonyodev/fetch/Fetch.java index 9030166a..18dd375f 100644 --- a/fetch/src/main/java/com/tonyodev/fetch/Fetch.java +++ b/fetch/src/main/java/com/tonyodev/fetch/Fetch.java @@ -490,18 +490,7 @@ public void resume(long id) { public void setAllowedNetwork(int networkType) { Utils.throwIfNotUsable(this); - - int type = NETWORK_ALL; - - if (networkType == NETWORK_WIFI) { - type = NETWORK_WIFI; - } - - Bundle extras = new Bundle(); - extras.putInt(FetchService.ACTION_TYPE, FetchService.ACTION_NETWORK); - extras.putInt(FetchService.EXTRA_NETWORK_ID, type); - - FetchService.sendToService(context,extras); + new Settings(context).setAllowedNetwork(networkType).apply(); } /** @@ -978,14 +967,86 @@ private boolean isLoggingEnabled() { * is ON by default. * * @param enabled enable or disable console logging + * + * @throws NotUsableException if the release method has been called on Fetch. */ public void enableLogging(boolean enabled) { - Bundle extras = new Bundle(); - extras.putInt(FetchService.ACTION_TYPE, FetchService.ACTION_LOGGING); - extras.putBoolean(FetchService.EXTRA_LOGGING_ID, enabled); + Utils.throwIfNotUsable(this); + new Settings(context).enableLogging(enabled).apply(); + } - FetchService.sendToService(context,extras); + /** + * The Settings class is used to apply + * settings to Fetch and the FetchService. + * */ + public static class Settings { + + private final Context context; + private final List settings = new ArrayList<>(); + public Settings(@NonNull Context context) { + + if(context == null) { + throw new NullPointerException("Context cannot be null"); + } + this.context = context; + } + + /** + * Enables or Disables console logging + * for Fetch and the FetchService. Logging + * is ON by default. + * + * @param enabled enable or disable console logging + * + * @return the settings instance + */ + public Settings enableLogging(boolean enabled) { + + Bundle extras = new Bundle(); + extras.putInt(FetchService.ACTION_TYPE,FetchService.ACTION_LOGGING); + extras.putBoolean(FetchService.EXTRA_LOGGING_ID,enabled); + settings.add(extras); + + return this; + } + + /** + * Sets the allowed network connection type the FetchService can use to download requests. + * + *

This method only accepts two values: Fetch.NETWORK_WIFI or Fetch.NETWORK_ALL. The default is + * Fetch.NETWORK_ALL. + * + * @param networkType allowed network type + * + * @return the settings instance + * */ + public Settings setAllowedNetwork(int networkType) { + + int type = NETWORK_ALL; + + if (networkType == NETWORK_WIFI) { + type = NETWORK_WIFI; + } + + Bundle extras = new Bundle(); + extras.putInt(FetchService.ACTION_TYPE,FetchService.ACTION_NETWORK); + extras.putInt(FetchService.EXTRA_NETWORK_ID,type); + settings.add(extras); + + return this; + } + + /** + * Apply the new settings to Fetch and the FetchService + * */ + public void apply(){ + + for (Bundle setting : settings) { + FetchService.sendToService(context,setting); + } + } } + } \ No newline at end of file