Skip to content

Commit

Permalink
QuestionFragment: Avoid non-responsive UI before the last question.
Browse files Browse the repository at this point in the history
Don't get the Offer Discussion setting from the account.
Contrary to the AccountManager documentation, any use of it from the
AccountManager causes a slowdown and a StrictModeViolation.
  • Loading branch information
murraycu committed Dec 4, 2014
1 parent 7705fcc commit f0db9ff
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 10 deletions.
16 changes: 14 additions & 2 deletions app/src/main/java/com/murrayc/galaxyzoo/app/LoginUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.JsonReader;

Expand Down Expand Up @@ -330,7 +329,7 @@ private static void copyPrefToAccount(final AccountManager mgr, final Account ac
static void copyPrefsToAccount(final Context context, final AccountManager accountManager, final Account account) {
//Copy the preferences into the account.
//See also SettingsFragment.onSharedPreferenceChanged()
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
final SharedPreferences prefs = Utils.getPreferences(context);
final Map<String, ?> keys = prefs.getAll();
for(final Map.Entry<String, ?> entry : keys.entrySet()) {
final Object value = entry.getValue();
Expand All @@ -346,6 +345,19 @@ static void copyPrefsToAccount(final Context context, final AccountManager accou
}
}

/**
* Get the "use-wifi only" setting from the account.
*
* Don't call this from the main thread - use an AsyncTask, for instance.
* Or use Utils.getUseWifiOnlyFromSharedPrefs().
*
* @param context
* @return
*/
public static boolean getUseWifiOnly(final Context context) {
return getBooleanPref(context, R.string.pref_key_wifi_only);
}

public static class LoginDetails {
public String name = null;
public String authApiKey = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ private void showNextQuestion(final String questionId, final String answerId) {
//Skip the "Discuss" question, depending on the setting:
if (!TextUtils.isEmpty(nextQuestionId)
&& nextQuestionId.equals(Config.QUESTION_ID_DISCUSS)
&& !Utils.getShowDiscussQuestion(getActivity())) {
&& !Utils.getShowDiscussQuestionFromSharedPrefs(getActivity())) {
//Add a "No" for the Discuss question without even showing the question:
storeAnswer(nextQuestionId, Config.ANSWER_ID_DISCUSS_NO);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ private void updateFromCursor() {

if (mCursor.getCount() <= 0) { //In case the query returned no rows.
Log.error("SubjectExtrasFragment.updateFromCursor(): The ContentProvider query returned no rows.");
UiUtils.warnAboutMissingNetwork(activity, Utils.getUseWifiOnly(activity));
UiUtils.warnAboutMissingNetwork(activity, Utils.getUseWifiOnlyFromSharedPrefs(activity));

return;
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/murrayc/galaxyzoo/app/UiUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public static Bundle getTransitionOptionsBundle(final Activity activity, final V
* @return
*/
static boolean warnAboutMissingNetwork(final Activity activity) {
return warnAboutMissingNetwork(activity, Utils.getUseWifiOnly(activity));
return warnAboutMissingNetwork(activity, Utils.getUseWifiOnlyFromSharedPrefs(activity));
}

/**
Expand Down
36 changes: 32 additions & 4 deletions app/src/main/java/com/murrayc/galaxyzoo/app/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
Expand All @@ -40,14 +41,41 @@ public class Utils {

public static final String STRING_ENCODING = "UTF-8";

public static boolean getUseWifiOnly(final Context context) {
return LoginUtils.getBooleanPref(context, R.string.pref_key_wifi_only);
/**
* Ideally you would use LoginUtils.getUseWifiOnly() instead of the copy that is in
* the SharedPreferences, but this is useful when you don't want to call from the main thread.
*
* @param context
* @return
*/
public static boolean getUseWifiOnlyFromSharedPrefs(final Context context) {
//We use the SharedPreferences rather than the copy of our prefs in the Account,
//to avoid using the Account in the main thread.
final SharedPreferences prefs = getPreferences(context);
final String key = context.getString(R.string.pref_key_wifi_only);
return prefs.getBoolean(key, false /* default */);
}

public static boolean getShowDiscussQuestion(final Context context) {
return LoginUtils.getBooleanPref(context, R.string.pref_key_show_discuss_question);
/**
* Ideally you would use LoginUtils.getShowDiscussQuestion() instead of the copy that is in
* the SharedPreferences, but this is useful when you don't want to call from the main thread.
*
* @param context
* @return
*/
public static boolean getShowDiscussQuestionFromSharedPrefs(final Context context) {
//We use the SharedPreferences rather than the copy of our prefs in the Account,
//to avoid using the Account in the main thread.
final SharedPreferences prefs = getPreferences(context);
final String key = context.getString(R.string.pref_key_show_discuss_question);
return prefs.getBoolean(key, true /* default */);
}

static SharedPreferences getPreferences(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context);
}


public static File getExternalCacheDir(final Context context) {
try {
return context.getExternalCacheDir();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.RequestFuture;
import com.murrayc.galaxyzoo.app.Log;
import com.murrayc.galaxyzoo.app.LoginUtils;
import com.murrayc.galaxyzoo.app.Utils;

import java.io.FileOutputStream;
Expand All @@ -45,6 +46,8 @@
import java.util.concurrent.TimeoutException;

/**
* Don't use any of these methods from the main thread.
*
* Created by murrayc on 8/25/14.
*/
public class HttpUtils {
Expand All @@ -60,9 +63,15 @@ public static void throwIfNoNetwork(final Context context) {
}
}

/**
* Don't call this from the main thread because it uses the Account.
*
* @param context
* @return
*/
public static boolean getNetworkIsConnected(final Context context) {
final Utils.NetworkConnected networkConnected =
Utils.getNetworkIsConnected(context, Utils.getUseWifiOnly(context));
Utils.getNetworkIsConnected(context, LoginUtils.getUseWifiOnly(context));
return networkConnected.connected;
}

Expand Down

0 comments on commit f0db9ff

Please sign in to comment.