Skip to content

Commit

Permalink
Add option to update via wi-fi only
Browse files Browse the repository at this point in the history
  • Loading branch information
ccomeaux committed Mar 22, 2014
1 parent 92a3533 commit ab9d320
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 11 deletions.
Binary file added src/res/drawable-hdpi/ic_action_network_cell.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions src/res/values/strings.xml
Expand Up @@ -6,8 +6,12 @@
<string name="byline_suffix_hotness_boardgame">on the Board Game Hotness</string>
<!-- Settings -->
<string name="settings_title">Settings</string>
<string name="settings_network">Network</string>
<string name="settings_wifi">Update via Wi-Fi only</string>
<string name="settings_wifi_descr">User-initiaied updates will happen over any network connection.</string>
<string name="settings_about">About</string>
<string name="settings_version">Version</string>
<string name="settings_licenses">Thanks to</string>
<string name="settings_key_wifi_only">WIFI_ONLY</string>

</resources>
10 changes: 9 additions & 1 deletion src/res/xml/preference_headers.xml
Expand Up @@ -2,7 +2,15 @@
<preference-headers xmlns:android="http://schemas.android.com/apk/res/android" >

<header
android:fragment="com.eaux.app.muzei.bgg.SettingsActivity$PrefFragment"
android:fragment="com.eaux.app.muzei.bgg.SettingsActivity$SettingsFragment"
android:icon="@drawable/ic_action_network_cell"
android:title="@string/settings_network" >
<extra
android:name="fragment"
android:value="com.eaux.app.muzei.bgg.settings.NETWORK" />
</header>
<header
android:fragment="com.eaux.app.muzei.bgg.SettingsActivity$SettingsFragment"
android:icon="@drawable/ic_action_about"
android:title="@string/settings_about" >
<extra
Expand Down
11 changes: 11 additions & 0 deletions src/res/xml/preference_network.xml
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:muzei="http://schemas.android.com/apk/res/com.eaux.app.muzei.bgg" >

<CheckBoxPreference
android:defaultValue="false"
android:key="@string/settings_key_wifi_only"
android:summary="@string/settings_wifi_descr"
android:title="@string/settings_wifi" />

</PreferenceScreen>
35 changes: 27 additions & 8 deletions src/src/com/eaux/app/muzei/bgg/BggMuzeiArtSource.java
Expand Up @@ -5,6 +5,10 @@
import org.simpleframework.xml.core.Persister;

import retrofit.RestAdapter;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.Log;

Expand All @@ -19,6 +23,8 @@ public class BggMuzeiArtSource extends RemoteMuzeiArtSource {
private static final String SOURCE_NAME = "BggMuzeiArtSource";

private static final int ROTATE_TIME_MILLIS = 3 * 60 * 60 * 1000; // rotate every 3 hours
private static final int INITIAL_RETRY_TIME_MILLIS = 10 * 1000; // start retry every 10 seconds
private static final String PREF_NO_WIFI_RETRY_ATTEMPT = "no_wifi_retry_attempt";

public BggMuzeiArtSource() {
super(SOURCE_NAME);
Expand All @@ -31,7 +37,19 @@ public void onCreate() {
}

@Override
protected void onTryUpdate(int arg0) throws RetryException {
protected void onTryUpdate(int reason) throws RetryException {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if (reason == UPDATE_REASON_SCHEDULED || reason == UPDATE_REASON_OTHER) {
if (prefs.getBoolean(getString(R.string.settings_key_wifi_only), false) && !isWifiConnected()) {
Log.w(TAG, "Not connected to Wi-Fi.");
int retryAttempt = prefs.getInt(PREF_NO_WIFI_RETRY_ATTEMPT, 0);
scheduleUpdate(System.currentTimeMillis() + (INITIAL_RETRY_TIME_MILLIS << retryAttempt));
prefs.edit().putInt(PREF_NO_WIFI_RETRY_ATTEMPT, retryAttempt + 1).apply();
return;
}
}
prefs.edit().remove(PREF_NO_WIFI_RETRY_ATTEMPT).apply();

String currentToken = (getCurrentArtwork() != null) ? getCurrentArtwork().getToken() : null;

RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint("http://boardgamegeek.com/")
Expand All @@ -40,16 +58,11 @@ protected void onTryUpdate(int arg0) throws RetryException {
BggService service = restAdapter.create(BggService.class);
HotGamesResponse response = service.getHotGames();

if (response == null || response.hotGames == null) {
if (response == null || response.hotGames == null || response.hotGames.size() == 0) {
Log.w(TAG, "Invalid response");
throw new RetryException();
}

if (response.hotGames.size() == 0) {
Log.w(TAG, "No games returned from API.");
scheduleUpdate(System.currentTimeMillis() + ROTATE_TIME_MILLIS);
return;
}

Random random = new Random();
HotGame hg;
String token;
Expand All @@ -66,4 +79,10 @@ protected void onTryUpdate(int arg0) throws RetryException {

scheduleUpdate(System.currentTimeMillis() + ROTATE_TIME_MILLIS);
}

private boolean isWifiConnected() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
return ni != null && ni.isConnected();
}
}
6 changes: 4 additions & 2 deletions src/src/com/eaux/app/muzei/bgg/SettingsActivity.java
Expand Up @@ -9,11 +9,13 @@
import android.preference.PreferenceFragment;

public class SettingsActivity extends PreferenceActivity {
private final static String ACTION_NETWORK = "com.eaux.app.muzei.bgg.settings.NETWORK";
private final static String ACTION_ABOUT = "com.eaux.app.muzei.bgg.settings.ABOUT";
private static final HashMap<String, Integer> mFragmentMap = buildFragmentMap();

private static HashMap<String, Integer> buildFragmentMap() {
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put(ACTION_NETWORK, R.xml.preference_network);
map.put(ACTION_ABOUT, R.xml.preference_about);
return map;
}
Expand All @@ -33,7 +35,7 @@ public void onBuildHeaders(List<Header> target) {
loadHeadersFromResource(R.xml.preference_headers, target);
}

public static class PrefFragment extends PreferenceFragment {
public static class SettingsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -49,6 +51,6 @@ public void onCreate(Bundle savedInstanceState) {

@Override
protected boolean isValidFragment(String fragmentName) {
return "com.eaux.app.muzei.bgg.SettingsActivity$PrefFragment".equals(fragmentName);
return "com.eaux.app.muzei.bgg.SettingsActivity$SettingsFragment".equals(fragmentName);
}
}

0 comments on commit ab9d320

Please sign in to comment.