Bug 1010261 - Change SuggestedSites to account for enabled/disabled state (r=mfinkle)

This commit is contained in:
Lucas Rocha 2014-05-23 14:27:53 +01:00
parent 58541bc625
commit 275025d8c4
3 changed files with 30 additions and 2 deletions

View File

@ -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";
}

View File

@ -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<String> 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<Site> 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;
}
}

View File

@ -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);
}
}