feeditem description should now be loaded correctly

This commit is contained in:
daniel oeh
2012-11-24 23:49:04 +01:00
parent 82426bb8e4
commit 7f3387239d
2 changed files with 48 additions and 27 deletions

View File

@ -1347,8 +1347,13 @@ public class FeedManager {
cursor.close(); cursor.close();
} }
/**
* Loads description and contentEncoded values from the database and caches it in the feeditem. The task
* callback will contain a String-array with the description at index 0 and
* the value of contentEncoded at index 1.
*/
public void loadExtraInformationOfItem(final Context context, public void loadExtraInformationOfItem(final Context context,
final FeedItem item, FeedManager.TaskCallback callback) { final FeedItem item, FeedManager.TaskCallback<String[]> callback) {
if (AppConfig.DEBUG) { if (AppConfig.DEBUG) {
Log.d(TAG, Log.d(TAG,
"Loading extra information of item with id " + item.getId()); "Loading extra information of item with id " + item.getId());
@ -1356,7 +1361,7 @@ public class FeedManager {
Log.d(TAG, "Title: " + item.getTitle()); Log.d(TAG, "Title: " + item.getTitle());
} }
} }
dbExec.execute(new FeedManager.Task(new Handler(), callback) { dbExec.execute(new FeedManager.Task<String[]>(new Handler(), callback) {
@Override @Override
public void execute() { public void execute() {
@ -1364,10 +1369,13 @@ public class FeedManager {
adapter.open(); adapter.open();
Cursor extraCursor = adapter.getExtraInformationOfItem(item); Cursor extraCursor = adapter.getExtraInformationOfItem(item);
if (extraCursor.moveToFirst()) { if (extraCursor.moveToFirst()) {
item.setCachedDescription(extraCursor String description = extraCursor
.getString(PodDBAdapter.IDX_FI_EXTRA_DESCRIPTION)); .getString(PodDBAdapter.IDX_FI_EXTRA_DESCRIPTION);
item.setCachedContentEncoded(extraCursor String contentEncoded = extraCursor
.getString(PodDBAdapter.IDX_FI_EXTRA_CONTENT_ENCODED)); .getString(PodDBAdapter.IDX_FI_EXTRA_CONTENT_ENCODED);
item.setCachedDescription(description);
item.setCachedContentEncoded(contentEncoded);
setResult(new String[] {description, contentEncoded});
} }
adapter.close(); adapter.close();
} }
@ -1377,7 +1385,8 @@ public class FeedManager {
public void searchFeedItemDescription(final Context context, public void searchFeedItemDescription(final Context context,
final Feed feed, final String query, final Feed feed, final String query,
FeedManager.QueryTaskCallback callback) { FeedManager.QueryTaskCallback callback) {
dbExec.execute(new FeedManager.QueryTask(context, new Handler(), callback) { dbExec.execute(new FeedManager.QueryTask(context, new Handler(),
callback) {
@Override @Override
public void execute(PodDBAdapter adapter) { public void execute(PodDBAdapter adapter) {
@ -1391,7 +1400,8 @@ public class FeedManager {
public void searchFeedItemContentEncoded(final Context context, public void searchFeedItemContentEncoded(final Context context,
final Feed feed, final String query, final Feed feed, final String query,
FeedManager.QueryTaskCallback callback) { FeedManager.QueryTaskCallback callback) {
dbExec.execute(new FeedManager.QueryTask(context, new Handler(), callback) { dbExec.execute(new FeedManager.QueryTask(context, new Handler(),
callback) {
@Override @Override
public void execute(PodDBAdapter adapter) { public void execute(PodDBAdapter adapter) {
@ -1423,20 +1433,22 @@ public class FeedManager {
} }
/** Is called by a FeedManagerTask after completion. */ /** Is called by a FeedManagerTask after completion. */
public interface TaskCallback { public interface TaskCallback<V> {
void onCompletion(Cursor result); void onCompletion(V result);
} }
/** Is called by a FeedManager.QueryTask after completion. */ /** Is called by a FeedManager.QueryTask after completion. */
public interface QueryTaskCallback { public interface QueryTaskCallback {
void handleResult(Cursor result); void handleResult(Cursor result);
void onCompletion(); void onCompletion();
} }
/** A runnable that can post a callback to a handler after completion. */ /** A runnable that can post a callback to a handler after completion. */
abstract class Task implements Runnable { abstract class Task<V> implements Runnable {
private Handler handler; private Handler handler;
private TaskCallback callback; private TaskCallback<V> callback;
private V result;
/** /**
* Standard contructor. No callbacks are going to be posted to a * Standard contructor. No callbacks are going to be posted to a
@ -1450,7 +1462,7 @@ public class FeedManager {
* The Task will post a Runnable to 'handler' that will execute the * The Task will post a Runnable to 'handler' that will execute the
* 'callback' after completion. * 'callback' after completion.
*/ */
public Task(Handler handler, TaskCallback callback) { public Task(Handler handler, TaskCallback<V> callback) {
super(); super();
this.handler = handler; this.handler = handler;
this.callback = callback; this.callback = callback;
@ -1463,7 +1475,7 @@ public class FeedManager {
handler.post(new Runnable() { handler.post(new Runnable() {
@Override @Override
public void run() { public void run() {
callback.onCompletion(null); callback.onCompletion(result);
} }
}); });
} }
@ -1471,6 +1483,10 @@ public class FeedManager {
/** This method will be executed in the same thread as the run() method. */ /** This method will be executed in the same thread as the run() method. */
public abstract void execute(); public abstract void execute();
public void setResult(V result) {
this.result = result;
}
} }
/** /**
@ -1485,7 +1501,8 @@ public class FeedManager {
private Context context; private Context context;
private Handler handler; private Handler handler;
public QueryTask(Context context, Handler handler, QueryTaskCallback callback) { public QueryTask(Context context, Handler handler,
QueryTaskCallback callback) {
this.callback = callback; this.callback = callback;
this.context = context; this.context = context;
this.handler = handler; this.handler = handler;
@ -1508,7 +1525,7 @@ public class FeedManager {
public void run() { public void run() {
callback.onCompletion(); callback.onCompletion();
} }
}); });
} }
} }

View File

@ -33,7 +33,7 @@ public class ItemDescriptionFragment extends SherlockFragment {
private FeedItem item; private FeedItem item;
private AsyncTask<Void, Void, Void> webViewLoader; private AsyncTask<Void, Void, Void> webViewLoader;
private String descriptionRef; private String descriptionRef;
private String contentEncodedRef; private String contentEncodedRef;
@ -116,18 +116,25 @@ public class ItemDescriptionFragment extends SherlockFragment {
&& item.getContentEncoded() == null) { && item.getContentEncoded() == null) {
Log.i(TAG, "Loading data"); Log.i(TAG, "Loading data");
FeedManager.getInstance().loadExtraInformationOfItem( FeedManager.getInstance().loadExtraInformationOfItem(
getActivity(), item, new FeedManager.TaskCallback() { getActivity(), item,
new FeedManager.TaskCallback<String[]>() {
@Override @Override
public void onCompletion(Cursor result) { public void onCompletion(String[] result) {
if (item.getDescription() == null if (result == null || result.length != 2) {
&& item.getContentEncoded() == null) {
Log.e(TAG, "No description found"); Log.e(TAG, "No description found");
} else {
descriptionRef = result[0];
contentEncodedRef = result[1];
} }
startLoader(); startLoader();
} }
}); });
} else { } else {
Log.i(TAG, "Using cached data"); contentEncodedRef = item.getContentEncoded();
descriptionRef = item.getDescription();
if (AppConfig.DEBUG)
Log.d(TAG, "Using cached data");
startLoader(); startLoader();
} }
} else { } else {
@ -142,8 +149,6 @@ public class ItemDescriptionFragment extends SherlockFragment {
@SuppressLint("NewApi") @SuppressLint("NewApi")
private void startLoader() { private void startLoader() {
contentEncodedRef = item.getContentEncoded();
descriptionRef = item.getDescription();
webViewLoader = createLoader(); webViewLoader = createLoader();
if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.GINGERBREAD_MR1) { if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.GINGERBREAD_MR1) {
webViewLoader.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); webViewLoader.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
@ -195,8 +200,7 @@ public class ItemDescriptionFragment extends SherlockFragment {
if (AppConfig.DEBUG) if (AppConfig.DEBUG)
Log.d(TAG, "Loading Webview"); Log.d(TAG, "Loading Webview");
data = ""; data = "";
if (contentEncodedRef == null if (contentEncodedRef == null && descriptionRef != null) {
&& descriptionRef != null) {
data = descriptionRef; data = descriptionRef;
} else { } else {
data = StringEscapeUtils.unescapeHtml4(contentEncodedRef); data = StringEscapeUtils.unescapeHtml4(contentEncodedRef);