Moved description webview to fragment

This commit is contained in:
daniel oeh 2012-07-01 14:28:49 +02:00
parent d1b1c92efe
commit 2cf9f5045b
3 changed files with 165 additions and 82 deletions

View File

@ -31,10 +31,11 @@
android:textStyle="bold"
android:gravity="center_horizontal"
android:text="@string/description_label"/>
<WebView
android:id="@+id/webvDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@+id/description_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</FrameLayout>
</LinearLayout>

View File

@ -1,44 +1,32 @@
package de.podfetcher.activity;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.DateFormat;
import org.apache.commons.lang3.StringEscapeUtils;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.Window;
import de.podfetcher.R;
import de.podfetcher.asynctask.DownloadObserver;
import de.podfetcher.asynctask.DownloadStatus;
import de.podfetcher.feed.Feed;
import de.podfetcher.feed.FeedItem;
import de.podfetcher.feed.FeedManager;
import de.podfetcher.feed.FeedMedia;
import de.podfetcher.fragment.ItemlistFragment;
import de.podfetcher.fragment.FeedlistFragment;
import de.podfetcher.service.PlaybackService;
import de.podfetcher.storage.DownloadRequester;
import de.podfetcher.fragment.ItemDescriptionFragment;
import de.podfetcher.fragment.ItemlistFragment;
import de.podfetcher.util.FeedItemMenuHandler;
/** Displays a single FeedItem and provides various actions */
public class ItemviewActivity extends SherlockActivity {
public class ItemviewActivity extends SherlockFragmentActivity {
private static final String TAG = "ItemviewActivity";
private FeedManager manager;
@ -47,7 +35,6 @@ public class ItemviewActivity extends SherlockActivity {
// Widgets
private TextView txtvTitle;
private TextView txtvPublished;
private WebView webvDescription;
@Override
public void onCreate(Bundle savedInstanceState) {
@ -82,33 +69,25 @@ public class ItemviewActivity extends SherlockActivity {
private void populateUI() {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setContentView(R.layout.feeditemview);
getSupportActionBar().setLogo(new BitmapDrawable(item.getFeed().getImage().getImageBitmap()));
getSupportActionBar().setLogo(
new BitmapDrawable(item.getFeed().getImage().getImageBitmap()));
txtvTitle = (TextView) findViewById(R.id.txtvItemname);
txtvPublished = (TextView) findViewById(R.id.txtvPublished);
webvDescription = (WebView) findViewById(R.id.webvDescription);
setTitle(item.getFeed().getTitle());
txtvPublished.setText(DateUtils.formatSameDayTime(item.getPubDate()
.getTime(), System.currentTimeMillis(), DateFormat.MEDIUM,
DateFormat.SHORT));
txtvTitle.setText(item.getTitle());
webViewLoader.execute();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
ItemDescriptionFragment fragment = ItemDescriptionFragment.newInstance(item);
fragmentTransaction.add(R.id.description_fragment, fragment);
fragmentTransaction.commit();
}
/*
* TODO implement final DownloadObserver downloadObserver = new
* DownloadObserver(this) {
*
* @Override protected void onProgressUpdate( DownloadStatus... values) {
*
* }
*
* @Override protected void onPostExecute(Boolean result) { boolean r =
* getStatusList()[0].isSuccessful(); if (r) { //setDownloadedState(); }
* else { //setNotDownloadedState(); } } };
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return FeedItemMenuHandler.onCreateMenu(new MenuInflater(this), menu);
@ -117,7 +96,7 @@ public class ItemviewActivity extends SherlockActivity {
@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
if (!FeedItemMenuHandler.onMenuItemClicked(this, menuItem, item)) {
switch(menuItem.getItemId()) {
switch (menuItem.getItemId()) {
case android.R.id.home:
finish();
break;
@ -132,45 +111,4 @@ public class ItemviewActivity extends SherlockActivity {
return FeedItemMenuHandler.onPrepareMenu(menu, item);
}
private AsyncTask<Void, Void, Void> webViewLoader = new AsyncTask<Void, Void, Void>() {
String url;
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
webvDescription.loadData(url, "text/html", "utf-8");
setSupportProgressBarIndeterminateVisibility(false);
Log.d(TAG, "Webview loaded");
}
@Override
protected void onPreExecute() {
super.onPreExecute();
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;
}
};
}

View File

@ -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;
}
};
}
}