* don't load all Feeds and FeedItems to memory when refreshing the flattr status

with items received from the flattr api
* added method for resetting flattr status of all feeds and items

This adresses https://github.com/danieloeh/AntennaPod/pull/331#issuecomment-32112929
items 1 and 3.

followup on #331
This commit is contained in:
Simon Rutishauser 2014-01-15 20:24:06 +01:00
parent fd3cdfa184
commit 01cfe2a94b
3 changed files with 96 additions and 70 deletions

View File

@ -21,8 +21,10 @@ import de.danoeh.antennapod.util.flattr.SimpleFlattrThing;
import org.shredzone.flattr4j.model.Flattr; import org.shredzone.flattr4j.model.Flattr;
import java.io.File; import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.util.*; import java.util.*;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
@ -875,17 +877,20 @@ public class DBWriter {
return false; return false;
} }
private static String normalizeURI(String uri) { /**
String normalizedURI = null; * format an url for querying the database
if (uri != null) { * (postfix a / and apply percent-encoding)
try { */
normalizedURI = (new URI(uri)).normalize().toString(); private static String formatURIForQuery(String uri) {
if (!normalizedURI.endsWith("/")) try
normalizedURI = normalizedURI + "/"; {
} catch (URISyntaxException e) { return URLEncoder.encode(uri.endsWith("/") ? uri.substring(0, uri.length()-1) : uri, "UTF-8");
} }
catch (UnsupportedEncodingException e)
{
Log.e(TAG, e.getMessage());
return "";
} }
return normalizedURI;
} }
@ -911,72 +916,44 @@ public class DBWriter {
return null; return null;
} }
/**
* Reset flattr status to unflattrd for all items
*/
public static Future<?> clearAllFlattrStatus(final Context context)
{
Log.d(TAG, "clearAllFlattrStatus()");
return dbExec.submit(new Runnable() {
@Override
public void run() {
PodDBAdapter adapter = new PodDBAdapter(context);
adapter.open();
adapter.clearAllFlattrStatus();
adapter.close();
}
});
}
/** /**
* Set flattr status of the feeds/feeditems in flattrList to flattred at the given timestamp, * Set flattr status of the feeds/feeditems in flattrList to flattred at the given timestamp,
* where the information has been retrieved from the flattr API * where the information has been retrieved from the flattr API
*/ */
public static void setFlattredStatus(final Context context, List<Flattr> flattrList) { public static Future<?> setFlattredStatus(final Context context, final List<Flattr> flattrList) {
class FlattrLinkTime { Log.d(TAG, "setFlattredStatus to status retrieved from flattr api running with " + flattrList.size() + " items");
public String paymentLink; // clear flattr status in db
public long time; clearAllFlattrStatus(context);
FlattrLinkTime(String paymentLink, long time) { // submit list with flattred things having normalized URLs to db
this.paymentLink = paymentLink; return dbExec.submit(new Runnable() {
this.time = time; @Override
} public void run() {
} PodDBAdapter adapter = new PodDBAdapter(context);
adapter.open();
// build list with flattred things having normalized URLs for (Flattr flattr : flattrList) {
ArrayList<FlattrLinkTime> flattrLinkTime = new ArrayList<FlattrLinkTime>(flattrList.size()); adapter.setItemFlattrStatus(formatURIForQuery(flattr.getThing().getUrl()), new FlattrStatus(flattr.getCreated().getTime()));
for (Flattr flattr : flattrList) {
flattrLinkTime.add(new FlattrLinkTime(normalizeURI(flattr.getThing().getUrl()), flattr.getCreated().getTime()));
if (AppConfig.DEBUG)
Log.d(TAG, "FlattredUrl: " + flattr.getThing().getUrl());
}
String paymentLink;
List<Feed> feeds = DBReader.getFeedList(context);
for (final Feed feed : feeds) {
// check if the feed has been flattred
paymentLink = feed.getPaymentLink();
if (paymentLink != null) {
String feedThingUrl = normalizeURI(Uri.parse(paymentLink).getQueryParameter("url"));
feed.getFlattrStatus().setUnflattred(); // reset our offline status tracking
if (AppConfig.DEBUG)
Log.d(TAG, "Feed: Trying to match " + feedThingUrl);
for (final FlattrLinkTime flattr : flattrLinkTime) {
if (flattr.paymentLink.equals(feedThingUrl)) {
feed.setFlattrStatus(new FlattrStatus(flattr.time));
setFeedFlattrStatus(context, feed, false);
break;
}
}
}
// check if any of the feeditems have been flattred
for (final FeedItem item : DBReader.getFeedItemList(context, feed)) {
paymentLink = item.getPaymentLink();
if (paymentLink != null) {
String feedItemThingUrl = normalizeURI(Uri.parse(paymentLink).getQueryParameter("url"));
item.getFlattrStatus().setUnflattred(); // reset our offline status tracking
if (AppConfig.DEBUG)
Log.d(TAG, "FeedItem: Trying to match " + feedItemThingUrl);
for (final FlattrLinkTime flattr : flattrLinkTime) {
if (flattr.paymentLink.equals(feedItemThingUrl)) {
item.setFlattrStatus(new FlattrStatus(flattr.time));
setFeedItemFlattrStatus(context, item, false);
break;
}
}
}
} }
adapter.close();
} }
});
} }
} }

View File

@ -572,6 +572,53 @@ public class PodDBAdapter {
db.update(TABLE_NAME_FEED_ITEMS, values, KEY_ID + "=?", new String[]{String.valueOf(feedItem.getId())}); db.update(TABLE_NAME_FEED_ITEMS, values, KEY_ID + "=?", new String[]{String.valueOf(feedItem.getId())});
} }
/**
* Update the flattr status of a feed or feed item specified by its payment link
* and the new flattr status to use
*/
public void setItemFlattrStatus(String url, FlattrStatus status)
{
//Log.d(TAG, "setItemFlattrStatus(" + url + ") = " + status.toString());
ContentValues values = new ContentValues();
values.put(KEY_FLATTR_STATUS, status.toLong());
// regexps in sqlite would be neat!
String[] query_urls = new String[]{
"*" + url + "&*",
"*" + url + "%2F&*",
"*" + url + "",
"*" + url + "%2F"
};
if (db.update(TABLE_NAME_FEEDS, values,
KEY_PAYMENT_LINK + " GLOB ?"
+ " OR " + KEY_PAYMENT_LINK + " GLOB ?"
+ " OR " + KEY_PAYMENT_LINK + " GLOB ?"
+ " OR " + KEY_PAYMENT_LINK + " GLOB ?", query_urls) > 0)
{
Log.i(TAG, "setItemFlattrStatus found match for " + url + " = " + status.toLong() + " in Feeds table");
return;
}
if (db.update(TABLE_NAME_FEED_ITEMS, values,
KEY_PAYMENT_LINK + " GLOB ?"
+ " OR " + KEY_PAYMENT_LINK + " GLOB ?"
+ " OR " + KEY_PAYMENT_LINK + " GLOB ?"
+ " OR " + KEY_PAYMENT_LINK + " GLOB ?", query_urls) > 0)
{
Log.i(TAG, "setItemFlattrStatus found match for " + url + " = " + status.toLong() + " in FeedsItems table");
}
}
/**
* Reset flattr status to unflattrd for all items
*/
public void clearAllFlattrStatus()
{
ContentValues values = new ContentValues();
values.put(KEY_FLATTR_STATUS, 0);
db.update(TABLE_NAME_FEEDS, values, null, null);
db.update(TABLE_NAME_FEED_ITEMS, values, null, null);
}
/** /**
* Inserts or updates a feeditem entry * Inserts or updates a feeditem entry

View File

@ -30,6 +30,7 @@ import de.danoeh.antennapod.PodcastApp;
import de.danoeh.antennapod.R; import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.FlattrAuthActivity; import de.danoeh.antennapod.activity.FlattrAuthActivity;
import de.danoeh.antennapod.asynctask.FlattrTokenFetcher; import de.danoeh.antennapod.asynctask.FlattrTokenFetcher;
import de.danoeh.antennapod.storage.DBWriter;
/** Utility methods for doing something with flattr. */ /** Utility methods for doing something with flattr. */
@ -190,7 +191,8 @@ public class FlattrUtils {
deleteToken(); deleteToken();
FlattrServiceCreator.deleteFlattrService(); FlattrServiceCreator.deleteFlattrService();
showRevokeDialog(context); showRevokeDialog(context);
} DBWriter.clearAllFlattrStatus(context);
}
// ------------------------------------------------ DIALOGS // ------------------------------------------------ DIALOGS