Skip to content

Commit

Permalink
Added new Settings class
Browse files Browse the repository at this point in the history
Settings class allows you to change certain behavors of the fetch service.
  • Loading branch information
tonyofrancis committed Mar 31, 2017
1 parent 48a6144 commit b40f581
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 16 deletions.
7 changes: 7 additions & 0 deletions app/src/main/java/com/tonyodev/fetchdemo/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
93 changes: 77 additions & 16 deletions fetch/src/main/java/com/tonyodev/fetch/Fetch.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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<Bundle> 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.
*
* <p>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);
}
}
}

}

0 comments on commit b40f581

Please sign in to comment.