From 275025d8c4ee5b00496e856ce1445633d4c6724d Mon Sep 17 00:00:00 2001 From: Lucas Rocha Date: Fri, 23 May 2014 14:27:53 +0100 Subject: [PATCH] Bug 1010261 - Change SuggestedSites to account for enabled/disabled state (r=mfinkle) --- mobile/android/base/db/BrowserContract.java | 2 ++ mobile/android/base/db/SuggestedSites.java | 21 +++++++++++++++++-- .../base/preferences/GeckoPreferences.java | 9 ++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/mobile/android/base/db/BrowserContract.java b/mobile/android/base/db/BrowserContract.java index 44a1f349ee9..7712f1663b5 100644 --- a/mobile/android/base/db/BrowserContract.java +++ b/mobile/android/base/db/BrowserContract.java @@ -438,6 +438,8 @@ public class BrowserContract { public static final class SuggestedSites implements CommonColumns, URLColumns { private SuggestedSites() {} + public static final Uri CONTENT_URI = Uri.withAppendedPath(AUTHORITY_URI, "suggestedsites"); + public static final String IMAGE_URL = "image_url"; public static final String BG_COLOR = "bg_color"; } diff --git a/mobile/android/base/db/SuggestedSites.java b/mobile/android/base/db/SuggestedSites.java index 0386f38f036..01b26955239 100644 --- a/mobile/android/base/db/SuggestedSites.java +++ b/mobile/android/base/db/SuggestedSites.java @@ -6,6 +6,7 @@ package org.mozilla.gecko.db; import android.content.Context; +import android.content.SharedPreferences; import android.database.Cursor; import android.database.MatrixCursor; import android.database.MatrixCursor.RowBuilder; @@ -22,9 +23,11 @@ import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import org.mozilla.gecko.GeckoSharedPrefs; import org.mozilla.gecko.R; import org.mozilla.gecko.db.BrowserContract; import org.mozilla.gecko.mozglue.RobocopTarget; +import org.mozilla.gecko.preferences.GeckoPreferences; import org.mozilla.gecko.util.RawResource; /** @@ -150,6 +153,11 @@ public class SuggestedSites { return sites; } + private boolean isEnabled() { + final SharedPreferences prefs = GeckoSharedPrefs.forApp(context); + return prefs.getBoolean(GeckoPreferences.PREFS_SUGGESTED_SITES, true); + } + /** * Returns a {@code Cursor} with the list of suggested websites. * @@ -166,14 +174,20 @@ public class SuggestedSites { * @param excludeUrls list of URLs to be excluded from the list. */ public Cursor get(int limit, List excludeUrls) { + final MatrixCursor cursor = new MatrixCursor(COLUMNS); + + // Return an empty cursor if suggested sites have been + // disabled by the user. + if (!isEnabled()) { + return cursor; + } + List sites = cachedSites.get(); if (sites == null) { Log.d(LOGTAG, "No cached sites, refreshing."); sites = refresh(); } - final MatrixCursor cursor = new MatrixCursor(COLUMNS); - // Return empty cursor if there was an error when // loading the suggested sites or the list is empty. if (sites == null || sites.isEmpty()) { @@ -199,6 +213,9 @@ public class SuggestedSites { row.add(site.bgColor); } + cursor.setNotificationUri(context.getContentResolver(), + BrowserContract.SuggestedSites.CONTENT_URI); + return cursor; } } \ No newline at end of file diff --git a/mobile/android/base/preferences/GeckoPreferences.java b/mobile/android/base/preferences/GeckoPreferences.java index f75f1bd4f7f..a6e3c3b9cdb 100644 --- a/mobile/android/base/preferences/GeckoPreferences.java +++ b/mobile/android/base/preferences/GeckoPreferences.java @@ -29,6 +29,7 @@ import org.mozilla.gecko.TelemetryContract; import org.mozilla.gecko.background.announcements.AnnouncementsConstants; import org.mozilla.gecko.background.common.GlobalConstants; import org.mozilla.gecko.background.healthreport.HealthReportConstants; +import org.mozilla.gecko.db.BrowserContract.SuggestedSites; import org.mozilla.gecko.home.HomePanelPicker; import org.mozilla.gecko.util.GeckoEventListener; import org.mozilla.gecko.util.ThreadUtils; @@ -40,6 +41,7 @@ import android.app.Dialog; import android.app.Fragment; import android.app.FragmentManager; import android.app.NotificationManager; +import android.content.ContentResolver; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; @@ -112,6 +114,7 @@ OnSharedPreferenceChangeListener private static final String PREFS_BROWSER_LOCALE = "locale"; public static final String PREFS_RESTORE_SESSION = NON_PREF_PREFIX + "restoreSession3"; + public static final String PREFS_SUGGESTED_SITES = NON_PREF_PREFIX + "home_suggested_sites"; // These values are chosen to be distinct from other Activity constants. private static final int REQUEST_CODE_PREF_SCREEN = 5; @@ -901,6 +904,12 @@ OnSharedPreferenceChangeListener if (PREFS_BROWSER_LOCALE.equals(key)) { onLocaleSelected(BrowserLocaleManager.getLanguageTag(lastLocale), sharedPreferences.getString(key, null)); + } else if (PREFS_SUGGESTED_SITES.equals(key)) { + final ContentResolver cr = getApplicationContext().getContentResolver(); + + // This will force all active suggested sites cursors + // to request a refresh (e.g. cursor loaders). + cr.notifyChange(SuggestedSites.CONTENT_URI, null); } }