Moved description webview to fragment
This commit is contained in:
parent
d1b1c92efe
commit
2cf9f5045b
@ -32,9 +32,10 @@
|
|||||||
android:gravity="center_horizontal"
|
android:gravity="center_horizontal"
|
||||||
android:text="@string/description_label"/>
|
android:text="@string/description_label"/>
|
||||||
|
|
||||||
<WebView
|
<FrameLayout
|
||||||
android:id="@+id/webvDescription"
|
android:id="@+id/description_fragment"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content" />
|
android:layout_height="wrap_content" >
|
||||||
|
</FrameLayout>
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
@ -1,44 +1,32 @@
|
|||||||
package de.podfetcher.activity;
|
package de.podfetcher.activity;
|
||||||
|
|
||||||
import java.io.UnsupportedEncodingException;
|
|
||||||
import java.net.URLEncoder;
|
|
||||||
import java.text.DateFormat;
|
import java.text.DateFormat;
|
||||||
|
|
||||||
import org.apache.commons.lang3.StringEscapeUtils;
|
|
||||||
|
|
||||||
import android.content.Intent;
|
|
||||||
import android.graphics.drawable.BitmapDrawable;
|
import android.graphics.drawable.BitmapDrawable;
|
||||||
import android.os.AsyncTask;
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.support.v4.app.FragmentManager;
|
||||||
|
import android.support.v4.app.FragmentTransaction;
|
||||||
import android.text.format.DateUtils;
|
import android.text.format.DateUtils;
|
||||||
import android.util.Log;
|
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 android.widget.TextView;
|
||||||
|
|
||||||
import com.actionbarsherlock.app.SherlockActivity;
|
import com.actionbarsherlock.app.SherlockFragmentActivity;
|
||||||
import com.actionbarsherlock.view.Menu;
|
import com.actionbarsherlock.view.Menu;
|
||||||
import com.actionbarsherlock.view.MenuInflater;
|
import com.actionbarsherlock.view.MenuInflater;
|
||||||
import com.actionbarsherlock.view.MenuItem;
|
import com.actionbarsherlock.view.MenuItem;
|
||||||
import com.actionbarsherlock.view.Window;
|
import com.actionbarsherlock.view.Window;
|
||||||
|
|
||||||
import de.podfetcher.R;
|
import de.podfetcher.R;
|
||||||
import de.podfetcher.asynctask.DownloadObserver;
|
|
||||||
import de.podfetcher.asynctask.DownloadStatus;
|
|
||||||
import de.podfetcher.feed.Feed;
|
import de.podfetcher.feed.Feed;
|
||||||
import de.podfetcher.feed.FeedItem;
|
import de.podfetcher.feed.FeedItem;
|
||||||
import de.podfetcher.feed.FeedManager;
|
import de.podfetcher.feed.FeedManager;
|
||||||
import de.podfetcher.feed.FeedMedia;
|
|
||||||
import de.podfetcher.fragment.ItemlistFragment;
|
|
||||||
import de.podfetcher.fragment.FeedlistFragment;
|
import de.podfetcher.fragment.FeedlistFragment;
|
||||||
import de.podfetcher.service.PlaybackService;
|
import de.podfetcher.fragment.ItemDescriptionFragment;
|
||||||
import de.podfetcher.storage.DownloadRequester;
|
import de.podfetcher.fragment.ItemlistFragment;
|
||||||
import de.podfetcher.util.FeedItemMenuHandler;
|
import de.podfetcher.util.FeedItemMenuHandler;
|
||||||
|
|
||||||
/** Displays a single FeedItem and provides various actions */
|
/** 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 static final String TAG = "ItemviewActivity";
|
||||||
|
|
||||||
private FeedManager manager;
|
private FeedManager manager;
|
||||||
@ -47,7 +35,6 @@ public class ItemviewActivity extends SherlockActivity {
|
|||||||
// Widgets
|
// Widgets
|
||||||
private TextView txtvTitle;
|
private TextView txtvTitle;
|
||||||
private TextView txtvPublished;
|
private TextView txtvPublished;
|
||||||
private WebView webvDescription;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
@ -82,10 +69,10 @@ public class ItemviewActivity extends SherlockActivity {
|
|||||||
private void populateUI() {
|
private void populateUI() {
|
||||||
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
||||||
setContentView(R.layout.feeditemview);
|
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);
|
txtvTitle = (TextView) findViewById(R.id.txtvItemname);
|
||||||
txtvPublished = (TextView) findViewById(R.id.txtvPublished);
|
txtvPublished = (TextView) findViewById(R.id.txtvPublished);
|
||||||
webvDescription = (WebView) findViewById(R.id.webvDescription);
|
|
||||||
setTitle(item.getFeed().getTitle());
|
setTitle(item.getFeed().getTitle());
|
||||||
|
|
||||||
txtvPublished.setText(DateUtils.formatSameDayTime(item.getPubDate()
|
txtvPublished.setText(DateUtils.formatSameDayTime(item.getPubDate()
|
||||||
@ -93,22 +80,14 @@ public class ItemviewActivity extends SherlockActivity {
|
|||||||
DateFormat.SHORT));
|
DateFormat.SHORT));
|
||||||
txtvTitle.setText(item.getTitle());
|
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
|
@Override
|
||||||
public boolean onCreateOptionsMenu(Menu menu) {
|
public boolean onCreateOptionsMenu(Menu menu) {
|
||||||
return FeedItemMenuHandler.onCreateMenu(new MenuInflater(this), menu);
|
return FeedItemMenuHandler.onCreateMenu(new MenuInflater(this), menu);
|
||||||
@ -132,45 +111,4 @@ public class ItemviewActivity extends SherlockActivity {
|
|||||||
return FeedItemMenuHandler.onPrepareMenu(menu, item);
|
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
144
src/de/podfetcher/fragment/ItemDescriptionFragment.java
Normal file
144
src/de/podfetcher/fragment/ItemDescriptionFragment.java
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user