avoid waiting on the GUI thread

This commit is contained in:
daniel oeh 2014-01-12 02:28:47 +01:00
parent 0921e49d7c
commit fd3cdfa184
4 changed files with 62 additions and 63 deletions

View File

@ -233,7 +233,7 @@ public class FlattrClickWorker extends AsyncTask<Void, String, Void> {
flattr_failed.add(thing.getTitle());
}
Log.d(TAG, "flattrQueue processing - going to write thing back to db with flattr_status " + Long.toString(thing.getFlattrStatus().toLong()));
DBWriter.setFlattredStatus(context, thing);
DBWriter.setFlattredStatus(context, thing, false);
}
}

View File

@ -1,36 +1,33 @@
package de.danoeh.antennapod.storage;
import java.io.File;
import java.util.Date;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadFactory;
import org.shredzone.flattr4j.model.Flattr;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.util.Log;
import android.net.Uri;
import de.danoeh.antennapod.AppConfig;
import de.danoeh.antennapod.asynctask.FlattrClickWorker;
import de.danoeh.antennapod.feed.*;
import de.danoeh.antennapod.preferences.GpodnetPreferences;
import de.danoeh.antennapod.preferences.PlaybackPreferences;
import de.danoeh.antennapod.service.GpodnetSyncService;
import de.danoeh.antennapod.service.PlaybackService;
import de.danoeh.antennapod.service.download.DownloadStatus;
import de.danoeh.antennapod.util.QueueAccess;
import de.danoeh.antennapod.util.flattr.*;
import de.danoeh.antennapod.util.flattr.FlattrStatus;
import de.danoeh.antennapod.util.flattr.FlattrThing;
import de.danoeh.antennapod.util.flattr.SimpleFlattrThing;
import org.shredzone.flattr4j.model.Flattr;
import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadFactory;
/**
* Provides methods for writing data to AntennaPod's database.
@ -215,9 +212,12 @@ public class DBWriter {
/**
* Saves the FlattrStatus of a Feed object in the database.
*
* @param startFlattrClickWorker true if FlattrClickWorker should be started after the FlattrStatus has been saved
*/
public static Future<?> setFeedFlattrStatus(final Context context,
final Feed feed) {
private static Future<?> setFeedFlattrStatus(final Context context,
final Feed feed,
final boolean startFlattrClickWorker) {
return dbExec.submit(new Runnable() {
@Override
@ -226,6 +226,9 @@ public class DBWriter {
adapter.open();
adapter.setFeedFlattrStatus(feed);
adapter.close();
if (startFlattrClickWorker) {
new FlattrClickWorker(context).executeAsync();
}
}
});
}
@ -471,7 +474,7 @@ public class DBWriter {
});
}
/**
* Moves the specified item to the top of the queue.
*
@ -497,7 +500,7 @@ public class DBWriter {
}
});
}
/**
* Moves the specified item to the bottom of the queue.
*
@ -516,7 +519,7 @@ public class DBWriter {
for (long id : queueIdList) {
if (id == itemId) {
moveQueueItemHelper(context, currentLocation, queueIdList.size() - 1,
broadcastUpdate);
broadcastUpdate);
return;
}
currentLocation++;
@ -525,7 +528,7 @@ public class DBWriter {
}
});
}
/**
* Changes the position of a FeedItem in the queue.
*
@ -549,7 +552,7 @@ public class DBWriter {
/**
* Changes the position of a FeedItem in the queue.
*
* <p/>
* This function must be run using the ExecutorService (dbExec).
*
* @param context A context that is used for opening a database connection.
@ -560,7 +563,7 @@ public class DBWriter {
* @throws IndexOutOfBoundsException if (to < 0 || to >= queue.size()) || (from < 0 || from >= queue.size())
*/
private static void moveQueueItemHelper(final Context context, final int from,
final int to, final boolean broadcastUpdate) {
final int to, final boolean broadcastUpdate) {
final PodDBAdapter adapter = new PodDBAdapter(context);
adapter.open();
final List<FeedItem> queue = DBReader
@ -783,9 +786,12 @@ public class DBWriter {
/**
* Saves the FlattrStatus of a FeedItem object in the database.
*
* @param startFlattrClickWorker true if FlattrClickWorker should be started after the FlattrStatus has been saved
*/
public static Future<?> setFeedItemFlattrStatus(final Context context,
final FeedItem item) {
final FeedItem item,
final boolean startFlattrClickWorker) {
return dbExec.submit(new Runnable() {
@Override
@ -794,6 +800,9 @@ public class DBWriter {
adapter.open();
adapter.setFeedItemFlattrStatus(item);
adapter.close();
if (startFlattrClickWorker) {
new FlattrClickWorker(context).executeAsync();
}
}
});
}
@ -871,27 +880,33 @@ public class DBWriter {
if (uri != null) {
try {
normalizedURI = (new URI(uri)).normalize().toString();
if (! normalizedURI.endsWith("/"))
if (!normalizedURI.endsWith("/"))
normalizedURI = normalizedURI + "/";
}
catch (URISyntaxException e) {
} catch (URISyntaxException e) {
}
}
return normalizedURI;
}
// Set flattr status of the passed thing (either a FeedItem or a Feed)
public static Future<?> setFlattredStatus(Context context, FlattrThing thing) {
/**
* Set flattr status of the passed thing (either a FeedItem or a Feed)
*
* @param context
* @param thing
* @param startFlattrClickWorker true if FlattrClickWorker should be started after the FlattrStatus has been saved
* @return
*/
public static Future<?> setFlattredStatus(Context context, FlattrThing thing, boolean startFlattrClickWorker) {
// must propagate this to back db
if (thing instanceof FeedItem)
return setFeedItemFlattrStatus(context, (FeedItem)thing);
return setFeedItemFlattrStatus(context, (FeedItem) thing, startFlattrClickWorker);
else if (thing instanceof Feed)
return setFeedFlattrStatus(context, (Feed)thing);
else if (thing instanceof SimpleFlattrThing)
{} // SimpleFlattrThings are generated on the fly and do not have DB backing
return setFeedFlattrStatus(context, (Feed) thing, startFlattrClickWorker);
else if (thing instanceof SimpleFlattrThing) {
} // SimpleFlattrThings are generated on the fly and do not have DB backing
else
Log.e(TAG, "flattrQueue processing - thing is neither FeedItem nor Feed nor SimpleFlattrThing");
Log.e(TAG, "flattrQueue processing - thing is neither FeedItem nor Feed nor SimpleFlattrThing");
return null;
}
@ -913,7 +928,7 @@ public class DBWriter {
// build list with flattred things having normalized URLs
ArrayList<FlattrLinkTime> flattrLinkTime = new ArrayList<FlattrLinkTime>(flattrList.size());
for (Flattr flattr: flattrList) {
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());
@ -922,7 +937,7 @@ public class DBWriter {
String paymentLink;
List<Feed> feeds = DBReader.getFeedList(context);
for (final Feed feed: feeds) {
for (final Feed feed : feeds) {
// check if the feed has been flattred
paymentLink = feed.getPaymentLink();
if (paymentLink != null) {
@ -932,17 +947,17 @@ public class DBWriter {
if (AppConfig.DEBUG)
Log.d(TAG, "Feed: Trying to match " + feedThingUrl);
for (final FlattrLinkTime flattr: flattrLinkTime) {
for (final FlattrLinkTime flattr : flattrLinkTime) {
if (flattr.paymentLink.equals(feedThingUrl)) {
feed.setFlattrStatus(new FlattrStatus(flattr.time));
setFeedFlattrStatus(context, feed);
setFeedFlattrStatus(context, feed, false);
break;
}
}
}
// check if any of the feeditems have been flattred
for (final FeedItem item: DBReader.getFeedItemList(context, feed)) {
for (final FeedItem item : DBReader.getFeedItemList(context, feed)) {
paymentLink = item.getPaymentLink();
if (paymentLink != null) {
@ -952,10 +967,10 @@ public class DBWriter {
if (AppConfig.DEBUG)
Log.d(TAG, "FeedItem: Trying to match " + feedItemThingUrl);
for (final FlattrLinkTime flattr: flattrLinkTime) {
for (final FlattrLinkTime flattr : flattrLinkTime) {
if (flattr.paymentLink.equals(feedItemThingUrl)) {
item.setFlattrStatus(new FlattrStatus(flattr.time));
setFeedItemFlattrStatus(context, item);
setFeedItemFlattrStatus(context, item, false);
break;
}
}

View File

@ -166,15 +166,7 @@ public class FeedItemMenuHandler {
break;
case R.id.support_item:
selectedItem.getFlattrStatus().setFlattrQueue();
Future<?> future = DBWriter.setFlattredStatus(context, selectedItem);
try {
synchronized (future) {
future.wait(10);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
new FlattrClickWorker(context).executeAsync();
DBWriter.setFlattredStatus(context, selectedItem, true);
break;
case R.id.share_link_item:
ShareUtils.shareFeedItemLink(context, selectedItem);

View File

@ -85,15 +85,7 @@ public class FeedMenuHandler {
break;
case R.id.support_item:
selectedFeed.getFlattrStatus().setFlattrQueue();
Future<?> future = DBWriter.setFlattredStatus(context, selectedFeed);
try {
synchronized (future) {
future.wait(10);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
new FlattrClickWorker(context).executeAsync();
DBWriter.setFlattredStatus(context, selectedFeed, true);
break;
case R.id.share_link_item:
ShareUtils.shareFeedlink(context, selectedFeed);