diff options
author | daniel oeh <daniel.oeh@gmail.com> | 2012-07-01 14:28:49 +0200 |
---|---|---|
committer | daniel oeh <daniel.oeh@gmail.com> | 2012-07-01 14:28:49 +0200 |
commit | 2cf9f5045b0cc94e3fc541791781b3c0c9bb4e17 (patch) | |
tree | 51aeb7628e66f2ea1b02d257a79657630b66f694 /src/de/podfetcher/fragment | |
parent | d1b1c92efe79e6aa7bb954d8b17ecfed2c7655b8 (diff) | |
download | AntennaPod-2cf9f5045b0cc94e3fc541791781b3c0c9bb4e17.zip |
Moved description webview to fragment
Diffstat (limited to 'src/de/podfetcher/fragment')
-rw-r--r-- | src/de/podfetcher/fragment/ItemDescriptionFragment.java | 144 |
1 files changed, 144 insertions, 0 deletions
diff --git a/src/de/podfetcher/fragment/ItemDescriptionFragment.java b/src/de/podfetcher/fragment/ItemDescriptionFragment.java new file mode 100644 index 000000000..0c4f4f9b0 --- /dev/null +++ b/src/de/podfetcher/fragment/ItemDescriptionFragment.java @@ -0,0 +1,144 @@ +package de.podfetcher.fragment; + +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; + +import org.apache.commons.lang3.StringEscapeUtils; + +import android.app.Activity; +import android.os.AsyncTask; +import android.os.Bundle; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.webkit.WebView; + +import com.actionbarsherlock.app.SherlockFragment; + +import de.podfetcher.feed.Feed; +import de.podfetcher.feed.FeedItem; +import de.podfetcher.feed.FeedManager; + +/** Displays the description of a FeedItem in a Webview. */ +public class ItemDescriptionFragment extends SherlockFragment { + + private static final String TAG = "ItemDescriptionFragment"; + private static final String ARG_FEED_ID = "arg.feedId"; + private static final String ARG_FEEDITEM_ID = "arg.feedItemId"; + + private WebView webvDescription; + private FeedItem item; + + private AsyncTask<Void, Void, Void> webViewLoader; + + public static ItemDescriptionFragment newInstance(FeedItem item) { + ItemDescriptionFragment f = new ItemDescriptionFragment(); + Bundle args = new Bundle(); + args.putLong(ARG_FEED_ID, item.getFeed().getId()); + args.putLong(ARG_FEEDITEM_ID, item.getId()); + f.setArguments(args); + return f; + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + webvDescription = new WebView(getActivity()); + return webvDescription; + } + + + @Override + public void onAttach(Activity activity) { + super.onAttach(activity); + if (webViewLoader == null) { + webViewLoader = createLoader(); + webViewLoader.execute(); + } + } + + @Override + public void onDetach() { + super.onDetach(); + if (webViewLoader != null) { + webViewLoader.cancel(true); + } + } + + @Override + public void onDestroy() { + super.onDestroy(); + if (webViewLoader != null) { + webViewLoader.cancel(true); + } + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + FeedManager manager = FeedManager.getInstance(); + Bundle args = getArguments(); + long feedId = args.getLong(ARG_FEED_ID, -1); + long itemId = args.getLong(ARG_FEEDITEM_ID, -1); + if (feedId != -1 && itemId != -1) { + Feed feed = manager.getFeed(feedId); + item = manager.getFeedItem(itemId, feed); + } else { + Log.e(TAG, TAG + " was called with invalid arguments"); + } + + } + + private AsyncTask<Void, Void, Void> createLoader() { + return new AsyncTask<Void, Void, Void>() { + @Override + protected void onCancelled() { + super.onCancelled(); + getSherlockActivity().setSupportProgressBarIndeterminateVisibility(false); + webViewLoader = null; + } + + String url; + + @Override + protected void onPostExecute(Void result) { + super.onPostExecute(result); + webvDescription.loadData(url, "text/html", "utf-8"); + getSherlockActivity().setSupportProgressBarIndeterminateVisibility(false); + Log.d(TAG, "Webview loaded"); + webViewLoader = null; + } + + @Override + protected void onPreExecute() { + super.onPreExecute(); + getSherlockActivity().setSupportProgressBarIndeterminateVisibility(true); + } + + @Override + protected Void doInBackground(Void... params) { + Log.d(TAG, "Loading Webview"); + url = ""; + try { + if (item.getContentEncoded() == null) { + url = URLEncoder.encode(item.getDescription(), "utf-8") + .replaceAll("\\+", " "); + } else { + url = URLEncoder.encode( + StringEscapeUtils.unescapeHtml4(item + .getContentEncoded()), "utf-8").replaceAll( + "\\+", " "); + } + + } catch (UnsupportedEncodingException e) { + url = "Page could not be loaded"; + e.printStackTrace(); + } + + return null; + } + + }; + } +} |