From 9524197e08236cf44463d439b681faf90d5eed03 Mon Sep 17 00:00:00 2001 From: Victor Ng Date: Wed, 13 Apr 2016 11:02:27 -0400 Subject: [PATCH 1/2] Don't allow null to come back from string prefs --- .../mozilla/mozstumbler/service/Prefs.java | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/libraries/stumbler/src/main/java/org/mozilla/mozstumbler/service/Prefs.java b/libraries/stumbler/src/main/java/org/mozilla/mozstumbler/service/Prefs.java index f87cc6042..363b8a136 100644 --- a/libraries/stumbler/src/main/java/org/mozilla/mozstumbler/service/Prefs.java +++ b/libraries/stumbler/src/main/java/org/mozilla/mozstumbler/service/Prefs.java @@ -109,7 +109,7 @@ protected static void apply(SharedPreferences.Editor editor) { /// public synchronized String getUserAgent() { String s = getStringPref(USER_AGENT_PREF); - return (s == null) ? AppGlobals.appName + "/" + AppGlobals.appVersionName : s; + return (TextUtils.isEmpty(s)) ? AppGlobals.appName + "/" + AppGlobals.appVersionName : s; } /// @@ -129,7 +129,7 @@ public synchronized void setFirefoxScanEnabled(boolean on) { public synchronized String getMozApiKey() { String s = getStringPref(MOZ_API_KEY); - return (s == null) ? "no-mozilla-api-key" : s; + return (TextUtils.isEmpty(s)) ? "no-mozilla-api-key" : s; } public synchronized void setMozApiKey(String s) { @@ -150,10 +150,10 @@ public synchronized void setLastAttemptedUploadTime(long time) { public synchronized String getNickname() { String nickname = getStringPref(NICKNAME_PREF); - if (nickname != null) { - nickname = nickname.trim(); + if (!TextUtils.isEmpty(nickname)) { + return nickname.trim(); } - return TextUtils.isEmpty(nickname) ? null : nickname; + return TextUtils.isEmpty(nickname) ? "" : nickname; } public synchronized void setNickname(String nick) { @@ -168,7 +168,7 @@ public synchronized String getLeaderboardUID() { if (!TextUtils.isEmpty(uid)) { return uid; } - return null; + return ""; } public synchronized void setLeaderboardUID(String uid) { @@ -179,10 +179,10 @@ public synchronized void setLeaderboardUID(String uid) { public synchronized String getEmail() { String email = getStringPref(EMAIL_PREF); - if (email != null) { + if (!TextUtils.isEmpty(email)) { email = email.trim(); } - return TextUtils.isEmpty(email) ? null : email; + return TextUtils.isEmpty(email) ? "" : email; } public synchronized void setEmail(String email) { @@ -214,7 +214,7 @@ public synchronized void setSaveStumbleLogs(boolean state) { /// protected String getStringPref(String key) { - return getPrefs().getString(key, null); + return getPrefs().getString(key, ""); } protected boolean getBoolPrefWithDefault(String key, boolean def) { @@ -353,9 +353,9 @@ public void setBearerToken(String bearerToken) { public String getBearerToken() { String bearerToken = getStringPref(FXA_LOGIN_PREF); - if (bearerToken != null) { + if (!TextUtils.isEmpty(bearerToken)) { bearerToken = bearerToken.trim(); } - return TextUtils.isEmpty(bearerToken) ? null : bearerToken; + return TextUtils.isEmpty(bearerToken) ? "" : bearerToken; } } From 84875220c2ad28af07c16472954558559689e236 Mon Sep 17 00:00:00 2001 From: Victor Ng Date: Wed, 13 Apr 2016 11:03:12 -0400 Subject: [PATCH 2/2] Update all callers of string prefs to use TextUtils.isEmpty instead of a NULL comparison when checking for empty strings --- .../client/leaderboard/LBStumblerBundleReceiver.java | 5 +++-- .../mozstumbler/client/leaderboard/LBUploadTask.java | 2 +- .../client/subactivities/LeaderboardActivity.java | 12 +++--------- .../client/subactivities/PreferencesScreen.java | 6 +++++- .../service/uploadthread/AsyncUploadParam.java | 6 ++++-- 5 files changed, 16 insertions(+), 15 deletions(-) diff --git a/android/src/main/java/org/mozilla/mozstumbler/client/leaderboard/LBStumblerBundleReceiver.java b/android/src/main/java/org/mozilla/mozstumbler/client/leaderboard/LBStumblerBundleReceiver.java index ee0b86640..40616b26d 100644 --- a/android/src/main/java/org/mozilla/mozstumbler/client/leaderboard/LBStumblerBundleReceiver.java +++ b/android/src/main/java/org/mozilla/mozstumbler/client/leaderboard/LBStumblerBundleReceiver.java @@ -8,6 +8,7 @@ import android.content.Intent; import android.content.IntentFilter; import android.support.v4.content.LocalBroadcastManager; +import android.text.TextUtils; import org.mozilla.mozstumbler.service.Prefs; import org.mozilla.mozstumbler.service.stumblerthread.Reporter; @@ -23,7 +24,7 @@ class UploadingOccurringReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // Guard against LeaderBoard intents when we're not logged into FxA - if (Prefs.getInstance(context).getBearerToken() == null) { + if (TextUtils.isEmpty(Prefs.getInstance(context).getBearerToken())) { return; } @@ -56,7 +57,7 @@ public LBStumblerBundleReceiver(Context c) { @Override public void onReceive(Context context, Intent intent) { // Guard against LeaderBoard intents when we're not logged into FxA - if (Prefs.getInstance(context).getBearerToken() != null) { + if (!TextUtils.isEmpty(Prefs.getInstance(context).getBearerToken())) { final StumblerBundle bundle = intent.getParcelableExtra(Reporter.NEW_BUNDLE_ARG_BUNDLE); if (bundle.hasRadioData()) { mStorage.insert(bundle.getGpsPosition()); diff --git a/android/src/main/java/org/mozilla/mozstumbler/client/leaderboard/LBUploadTask.java b/android/src/main/java/org/mozilla/mozstumbler/client/leaderboard/LBUploadTask.java index 9b134cc57..d1873a897 100644 --- a/android/src/main/java/org/mozilla/mozstumbler/client/leaderboard/LBUploadTask.java +++ b/android/src/main/java/org/mozilla/mozstumbler/client/leaderboard/LBUploadTask.java @@ -78,7 +78,7 @@ protected HashMap getHeaders(AsyncUploadParam param) { String bearerToken = Prefs.getInstanceWithoutContext().getBearerToken(); headers.put("Content-Encoding", "gzip"); - if (bearerToken != null) { + if (!TextUtils.isEmpty(bearerToken)) { headers.put(FxAService.BEARER_HEADER, "Bearer " + bearerToken); } return headers; diff --git a/android/src/main/java/org/mozilla/mozstumbler/client/subactivities/LeaderboardActivity.java b/android/src/main/java/org/mozilla/mozstumbler/client/subactivities/LeaderboardActivity.java index fc2160f89..1a308b41c 100644 --- a/android/src/main/java/org/mozilla/mozstumbler/client/subactivities/LeaderboardActivity.java +++ b/android/src/main/java/org/mozilla/mozstumbler/client/subactivities/LeaderboardActivity.java @@ -49,7 +49,6 @@ protected void onStart() { final Activity activity = this; mWebView.setWebChromeClient(new WebChromeClient() { public void onProgressChanged(WebView view, int progress) { - //activity.setProgress(progress * 100); setSupportProgress(progress * 100); if (progress > 45 && !mHasError) { mWebView.setVisibility(View.VISIBLE); @@ -77,24 +76,19 @@ public void onPageFinished(WebView webview, String url) { }); setProgress(0); - ClientPrefs prefs = ClientPrefs.getInstance(getApplicationContext()); + ClientPrefs prefs = ClientPrefs.getInstance(this); URI tmpURI = null; String url = null; try { - tmpURI = new URI(getPrefs().getLbBaseURI() + "/?profile=" + getPrefs().getLeaderboardUID()); + tmpURI = new URI(prefs.getLbBaseURI() + "/?profile=" + prefs.getLeaderboardUID()); url = tmpURI.normalize().toString(); } catch (URISyntaxException e) { Log.e(LOG_TAG, "Error normalizing URL", e); - url = getPrefs().getLbBaseURI(); + url = prefs.getLbBaseURI(); } mWebView.loadUrl(url); } - - private ClientPrefs getPrefs() { - return ClientPrefs.getInstance(this); - } - } diff --git a/android/src/main/java/org/mozilla/mozstumbler/client/subactivities/PreferencesScreen.java b/android/src/main/java/org/mozilla/mozstumbler/client/subactivities/PreferencesScreen.java index b131aa8d3..0ed438eb1 100644 --- a/android/src/main/java/org/mozilla/mozstumbler/client/subactivities/PreferencesScreen.java +++ b/android/src/main/java/org/mozilla/mozstumbler/client/subactivities/PreferencesScreen.java @@ -49,6 +49,9 @@ import org.mozilla.mozstumbler.svclocator.services.log.ILogger; import org.mozilla.mozstumbler.svclocator.services.log.LoggerUtil; +import java.util.Random; +import java.util.concurrent.ThreadLocalRandom; + public class PreferencesScreen extends PreferenceActivity implements IFxACallbacks{ private static final String LOG_TAG = LoggerUtil.makeLogTag(PreferencesScreen.class); @@ -531,7 +534,8 @@ private void setFxANickname(String displayName) { } private String defaultDisplayName() { - return "user_" + Integer.toString(Math.abs(getPrefs().getEmail().hashCode())); + Random rand = new Random(System.currentTimeMillis() % 1000); + return "user_" + rand.nextInt(Integer.MAX_VALUE-1) + 1; } @Override diff --git a/libraries/stumbler/src/main/java/org/mozilla/mozstumbler/service/uploadthread/AsyncUploadParam.java b/libraries/stumbler/src/main/java/org/mozilla/mozstumbler/service/uploadthread/AsyncUploadParam.java index 7198f49f1..2743a129e 100644 --- a/libraries/stumbler/src/main/java/org/mozilla/mozstumbler/service/uploadthread/AsyncUploadParam.java +++ b/libraries/stumbler/src/main/java/org/mozilla/mozstumbler/service/uploadthread/AsyncUploadParam.java @@ -4,6 +4,8 @@ package org.mozilla.mozstumbler.service.uploadthread; +import android.text.TextUtils; + public class AsyncUploadParam { final boolean useWifiOnly; @@ -16,11 +18,11 @@ public AsyncUploadParam(boolean wifiOnly, String nick, String email) { - if (email == null) { + if (TextUtils.isEmpty(email)) { email = ""; } - if (nick == null) { + if (TextUtils.isEmpty(nick)) { nick = ""; }