public static void removeFavorite(Context context, String name) { if (!hasFavorite(context, name)) { return; } List<Favorite> favorites = getFavorites(context); boolean hasDeleted = false; int highestIndex = -1; for (Favorite f : favorites) { if (!hasDeleted) { if (f.getName().equals(name)) { highestIndex = f.getIndex(); Log.v(Tag, "Removing favorite: " + f.getName() + " " + f.getIndex()); hasDeleted = true; final String fav = PREFS_FAV_PREFIX + f.getIndex(); removeSetting(context, fav); removeSetting(context, fav + ".name"); } } else { highestIndex = f.getIndex(); Log.v(Tag, "Subtracting: " + f.getName() + " " + f.getIndex()); // Subtract one from index f.setIndex(f.getIndex() - 1); f.persist(context); } } // Remove the last one if (highestIndex != -1) { final String fav = PREFS_FAV_PREFIX + highestIndex; removeSetting(context, fav); removeSetting(context, fav + ".name"); } }
public static List<Favorite> getFavorites(Context ctx) { List<Favorite> res = new ArrayList<Favorite>(6); int index = 0; while (true) { final String fav = PREFS_FAV_PREFIX + index++; if (contains(ctx, fav)) { Favorite f = new Favorite(); f.setActive(isSet(ctx, fav, false)); f.setIndex(index - 1); f.setName(getSetting(ctx, fav + ".name")); if (f.getName() != null) { res.add(f); } f.setOtherFavorites(isSet(ctx, fav + ".otherfavs", false)); int targetIndex = 0; while (contains(ctx, fav + ".targets." + targetIndex)) { String targetName = getSetting(ctx, fav + ".targets." + targetIndex++); if (targetName != null && !targetName.equals("")) { f.addTarget(targetName); } } } else { break; } } return res; }