Merge branch 'online_feed_view' into develop

This commit is contained in:
daniel oeh 2013-03-06 21:04:37 +01:00
commit 5d9d38184f
7 changed files with 441 additions and 67 deletions

View File

@ -0,0 +1,46 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:paddingLeft="4dp" >
<TextView
android:id="@+id/txtvItemname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="4dp"
android:ellipsize="end"
android:maxLines="2"
android:textColor="?android:attr/textColorPrimary"
android:textSize="@dimen/text_size_medium" />
<TextView
android:id="@+id/txtvPublished"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/txtvItemname"
android:layout_marginBottom="4dp"
android:textColor="?android:attr/textColorTertiary"
android:textSize="@dimen/text_size_micro" />
<ImageView
android:id="@+id/imgvType"
android:layout_width="@dimen/enc_icons_size"
android:layout_height="@dimen/enc_icons_size"
android:layout_below="@id/txtvPublished"
android:padding="2dp" />
<TextView
android:id="@+id/txtvLenSize"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@id/txtvPublished"
android:maxLines="2"
android:textColor="?android:attr/textColorTertiary"
android:textSize="@dimen/text_size_micro" />
</RelativeLayout>

View File

@ -0,0 +1,236 @@
package de.danoeh.antennapod.activity;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import de.danoeh.antennapod.AppConfig;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.asynctask.DownloadStatus;
import de.danoeh.antennapod.feed.Feed;
import de.danoeh.antennapod.preferences.UserPreferences;
import de.danoeh.antennapod.service.download.Downloader;
import de.danoeh.antennapod.service.download.DownloaderCallback;
import de.danoeh.antennapod.service.download.HttpDownloader;
import de.danoeh.antennapod.syndication.handler.FeedHandler;
import de.danoeh.antennapod.syndication.handler.UnsupportedFeedtypeException;
import de.danoeh.antennapod.util.DownloadError;
import de.danoeh.antennapod.util.FileNameGenerator;
import de.danoeh.antennapod.util.StorageUtils;
import de.danoeh.antennapod.util.URLChecker;
/**
* Downloads a feed from a feed URL and parses it. Subclasses can display the
* feed object that was parsed. This activity MUST be started with a given URL
* or an Exception will be thrown.
*
* If the feed cannot be downloaded or parsed, an error dialog will be displayed
* and the activity will finish as soon as the error dialog is closed.
*/
public abstract class OnlineFeedViewActivity extends SherlockFragmentActivity {
private static final String TAG = "OnlineFeedViewActivity";
private static final String ARG_FEEDURL = "arg.feedurl";
public static final int RESULT_ERROR = 2;
private Feed feed;
private Downloader downloader;
@Override
protected void onCreate(Bundle arg0) {
setTheme(UserPreferences.getTheme());
super.onCreate(arg0);
StorageUtils.checkStorageAvailability(this);
final String feedUrl = getIntent().getStringExtra(ARG_FEEDURL);
if (feedUrl == null) {
throw new IllegalArgumentException(
"Activity must be started with feedurl argument!");
}
if (AppConfig.DEBUG)
Log.d(TAG, "Activity was started with url " + feedUrl);
setLoadingLayout();
startFeedDownload(feedUrl);
}
@Override
protected void onStop() {
super.onStop();
if (downloader != null && downloader.getStatus().isDone() == false) {
downloader.cancel();
}
}
private DownloaderCallback downloaderCallback = new DownloaderCallback() {
@Override
public void onDownloadCompleted(final Downloader downloader) {
runOnUiThread(new Runnable() {
@Override
public void run() {
DownloadStatus status = downloader.getStatus();
if (status != null) {
if (!status.isCancelled()) {
if (status.isSuccessful()) {
parseFeed();
} else {
String errorMsg = DownloadError.getErrorString(
OnlineFeedViewActivity.this,
status.getReason());
if (errorMsg != null
&& status.getReasonDetailed() != null) {
errorMsg += " ("
+ status.getReasonDetailed() + ")";
}
showErrorDialog(errorMsg);
}
}
} else {
Log.wtf(TAG,
"DownloadStatus returned by Downloader was null");
finish();
}
}
});
}
};
private void startFeedDownload(String url) {
if (AppConfig.DEBUG)
Log.d(TAG, "Starting feed download");
url = URLChecker.prepareURL(url);
feed = new Feed(url, new Date());
String fileUrl = new File(getExternalCacheDir(),
FileNameGenerator.generateFileName(feed.getDownload_url()))
.toString();
feed.setFile_url(fileUrl);
DownloadStatus status = new DownloadStatus(feed, "OnlineFeed");
HttpDownloader httpDownloader = new HttpDownloader(downloaderCallback,
status);
httpDownloader.start();
}
/** Displays a progress indicator. */
private void setLoadingLayout() {
LinearLayout ll = new LinearLayout(this);
LinearLayout.LayoutParams llLayoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
ProgressBar pb = new ProgressBar(this);
pb.setIndeterminate(true);
LinearLayout.LayoutParams pbLayoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
pbLayoutParams.gravity = Gravity.CENTER;
ll.addView(pb, pbLayoutParams);
addContentView(ll, llLayoutParams);
}
private void parseFeed() {
if (feed == null || feed.getFile_url() == null) {
throw new IllegalStateException(
"feed must be non-null and downloaded when parseFeed is called");
}
if (AppConfig.DEBUG)
Log.d(TAG, "Parsing feed");
Thread thread = new Thread() {
@Override
public void run() {
String reasonDetailed = new String();
boolean successful = false;
FeedHandler handler = new FeedHandler();
try {
handler.parseFeed(feed);
successful = true;
} catch (SAXException e) {
e.printStackTrace();
reasonDetailed = e.getMessage();
} catch (IOException e) {
e.printStackTrace();
reasonDetailed = e.getMessage();
} catch (ParserConfigurationException e) {
e.printStackTrace();
reasonDetailed = e.getMessage();
} catch (UnsupportedFeedtypeException e) {
e.printStackTrace();
reasonDetailed = e.getMessage();
} finally {
boolean rc = new File(feed.getFile_url()).delete();
if (AppConfig.DEBUG)
Log.d(TAG, "Deleted feed source file. Result: " + rc);
}
if (successful) {
runOnUiThread(new Runnable() {
@Override
public void run() {
showFeedInformation();
}
});
} else {
final String errorMsg = DownloadError.getErrorString(
OnlineFeedViewActivity.this,
DownloadError.ERROR_PARSER_EXCEPTION)
+ " (" + reasonDetailed + ")";
runOnUiThread(new Runnable() {
@Override
public void run() {
showErrorDialog(errorMsg);
}
});
}
}
};
thread.start();
}
/** Called when feed parsed successfully */
protected void showFeedInformation() {
}
private void showErrorDialog(String errorMsg) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.error_label);
if (errorMsg != null) {
builder.setMessage(getString(R.string.error_msg_prefix) + errorMsg);
} else {
builder.setMessage(R.string.error_msg_prefix);
}
builder.setNeutralButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
setResult(RESULT_ERROR);
finish();
}
});
}
}

View File

@ -1,39 +0,0 @@
package de.danoeh.antennapod.adapter;
import android.widget.BaseAdapter;
import de.danoeh.antennapod.feed.FeedItem;
public abstract class AbstractFeedItemlistAdapter extends BaseAdapter {
ItemAccess itemAccess;
public AbstractFeedItemlistAdapter(ItemAccess itemAccess) {
super();
if (itemAccess == null) {
throw new IllegalArgumentException("itemAccess must not be null");
}
this.itemAccess = itemAccess;
}
@Override
public int getCount() {
return itemAccess.getCount();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public FeedItem getItem(int position) {
return itemAccess.getItem(position);
}
public static interface ItemAccess {
int getCount();
FeedItem getItem(int position);
}
}

View File

@ -0,0 +1,136 @@
package de.danoeh.antennapod.adapter;
import java.text.DateFormat;
import android.content.Context;
import android.content.res.TypedArray;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Adapter;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.adapter.InternalFeedItemlistAdapter.Holder;
import de.danoeh.antennapod.feed.FeedItem;
import de.danoeh.antennapod.feed.FeedManager;
import de.danoeh.antennapod.feed.MediaType;
import de.danoeh.antennapod.storage.DownloadRequester;
import de.danoeh.antennapod.util.Converter;
import de.danoeh.antennapod.util.ThemeUtils;
public class DefaultFeedItemlistAdapter extends BaseAdapter {
ItemAccess itemAccess;
private Context context;
public DefaultFeedItemlistAdapter(Context context, ItemAccess itemAccess) {
super();
this.context = context;
if (itemAccess == null) {
throw new IllegalArgumentException("itemAccess must not be null");
}
this.itemAccess = itemAccess;
}
@Override
public int getCount() {
return itemAccess.getCount();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public FeedItem getItem(int position) {
return itemAccess.getItem(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder;
final FeedItem item = getItem(position);
if (convertView == null) {
holder = new Holder();
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.default_feeditemlist_item, null);
holder.title = (TextView) convertView
.findViewById(R.id.txtvItemname);
holder.lenSize = (TextView) convertView
.findViewById(R.id.txtvLenSize);
holder.published = (TextView) convertView
.findViewById(R.id.txtvPublished);
holder.type = (ImageView) convertView.findViewById(R.id.imgvType);
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
if (!(getItemViewType(position) == Adapter.IGNORE_ITEM_VIEW_TYPE)) {
convertView.setVisibility(View.VISIBLE);
holder.title.setText(item.getTitle());
holder.published.setText(convertView.getResources().getString(
R.string.published_prefix)
+ DateUtils.formatSameDayTime(item.getPubDate().getTime(),
System.currentTimeMillis(), DateFormat.MEDIUM,
DateFormat.SHORT));
if (item.getMedia() == null) {
holder.type.setVisibility(View.GONE);
holder.lenSize.setVisibility(View.GONE);
} else {
holder.lenSize.setVisibility(View.VISIBLE);
holder.lenSize.setText(convertView.getResources().getString(
R.string.size_prefix)
+ Converter.byteToString(item.getMedia().getSize()));
TypedArray typeDrawables = context
.obtainStyledAttributes(new int[] { R.attr.type_audio,
R.attr.type_video });
MediaType mediaType = item.getMedia().getMediaType();
if (mediaType == MediaType.AUDIO) {
holder.type.setImageDrawable(typeDrawables.getDrawable(0));
holder.type.setVisibility(View.VISIBLE);
} else if (mediaType == MediaType.VIDEO) {
holder.type.setImageDrawable(typeDrawables.getDrawable(1));
holder.type.setVisibility(View.VISIBLE);
} else {
holder.type.setImageBitmap(null);
holder.type.setVisibility(View.GONE);
}
}
} else {
convertView.setVisibility(View.GONE);
}
return convertView;
}
protected static class Holder {
TextView title;
TextView published;
TextView lenSize;
ImageView type;
}
public static interface ItemAccess {
int getCount();
FeedItem getItem(int position);
}
protected Context getContext() {
return context;
}
}

View File

@ -21,21 +21,19 @@ import de.danoeh.antennapod.storage.DownloadRequester;
import de.danoeh.antennapod.util.Converter;
import de.danoeh.antennapod.util.ThemeUtils;
public class FeedItemlistAdapter extends AbstractFeedItemlistAdapter {
private Context context;
/** List adapter for items of feeds that the user has already subscribed to. */
public class InternalFeedItemlistAdapter extends DefaultFeedItemlistAdapter {
private ActionButtonCallback callback;
private boolean showFeedtitle;
private int selectedItemIndex;
public static final int SELECTION_NONE = -1;
public FeedItemlistAdapter(Context context,
AbstractFeedItemlistAdapter.ItemAccess itemAccess,
public InternalFeedItemlistAdapter(Context context,
DefaultFeedItemlistAdapter.ItemAccess itemAccess,
ActionButtonCallback callback, boolean showFeedtitle) {
super(itemAccess);
this.context = context;
super(context, itemAccess);
this.callback = callback;
this.showFeedtitle = showFeedtitle;
this.selectedItemIndex = SELECTION_NONE;
@ -48,7 +46,7 @@ public class FeedItemlistAdapter extends AbstractFeedItemlistAdapter {
if (convertView == null) {
holder = new Holder();
LayoutInflater inflater = (LayoutInflater) context
LayoutInflater inflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.feeditemlist_item, null);
holder.title = (TextView) convertView
@ -163,7 +161,7 @@ public class FeedItemlistAdapter extends AbstractFeedItemlistAdapter {
holder.downloading.setVisibility(View.GONE);
}
TypedArray typeDrawables = context.obtainStyledAttributes(
TypedArray typeDrawables = getContext().obtainStyledAttributes(
new int[] { R.attr.type_audio, R.attr.type_video });
MediaType mediaType = item.getMedia().getMediaType();
if (mediaType == MediaType.AUDIO) {
@ -194,14 +192,10 @@ public class FeedItemlistAdapter extends AbstractFeedItemlistAdapter {
}
static class Holder {
TextView title;
static class Holder extends DefaultFeedItemlistAdapter.Holder {
TextView feedtitle;
TextView published;
TextView lenSize;
ImageView inPlaylist;
ImageView downloaded;
ImageView type;
ImageView downloading;
ImageButton butAction;
View statusUnread;

View File

@ -17,9 +17,9 @@ import com.actionbarsherlock.app.SherlockListFragment;
import de.danoeh.antennapod.AppConfig;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.ItemviewActivity;
import de.danoeh.antennapod.adapter.AbstractFeedItemlistAdapter;
import de.danoeh.antennapod.adapter.DefaultFeedItemlistAdapter;
import de.danoeh.antennapod.adapter.ActionButtonCallback;
import de.danoeh.antennapod.adapter.FeedItemlistAdapter;
import de.danoeh.antennapod.adapter.InternalFeedItemlistAdapter;
import de.danoeh.antennapod.dialog.DownloadRequestErrorDialogCreator;
import de.danoeh.antennapod.feed.EventDistributor;
import de.danoeh.antennapod.feed.Feed;
@ -42,11 +42,11 @@ public class ItemlistFragment extends SherlockListFragment {
public static final String EXTRA_SELECTED_FEEDITEM = "extra.de.danoeh.antennapod.activity.selected_feeditem";
public static final String ARGUMENT_FEED_ID = "argument.de.danoeh.antennapod.feed_id";
protected AbstractFeedItemlistAdapter fila;
protected InternalFeedItemlistAdapter fila;
protected FeedManager manager = FeedManager.getInstance();
protected DownloadRequester requester = DownloadRequester.getInstance();
private AbstractFeedItemlistAdapter.ItemAccess itemAccess;
private DefaultFeedItemlistAdapter.ItemAccess itemAccess;
private Feed feed;
@ -56,7 +56,7 @@ public class ItemlistFragment extends SherlockListFragment {
/** Argument for FeeditemlistAdapter */
protected boolean showFeedtitle;
public ItemlistFragment(AbstractFeedItemlistAdapter.ItemAccess itemAccess,
public ItemlistFragment(DefaultFeedItemlistAdapter.ItemAccess itemAccess,
boolean showFeedtitle) {
super();
this.itemAccess = itemAccess;
@ -96,7 +96,7 @@ public class ItemlistFragment extends SherlockListFragment {
long feedId = getArguments().getLong(ARGUMENT_FEED_ID);
final Feed feed = FeedManager.getInstance().getFeed(feedId);
this.feed = feed;
itemAccess = new AbstractFeedItemlistAdapter.ItemAccess() {
itemAccess = new DefaultFeedItemlistAdapter.ItemAccess() {
@Override
public FeedItem getItem(int position) {
@ -111,8 +111,8 @@ public class ItemlistFragment extends SherlockListFragment {
}
}
protected AbstractFeedItemlistAdapter createListAdapter() {
return new FeedItemlistAdapter(getActivity(), itemAccess,
protected InternalFeedItemlistAdapter createListAdapter() {
return new InternalFeedItemlistAdapter(getActivity(), itemAccess,
adapterCallback, showFeedtitle);
}
@ -253,7 +253,7 @@ public class ItemlistFragment extends SherlockListFragment {
return handled;
}
public AbstractFeedItemlistAdapter getListAdapter() {
public InternalFeedItemlistAdapter getListAdapter() {
return fila;
}

View File

@ -3,7 +3,8 @@ package de.danoeh.antennapod.fragment;
import android.os.Bundle;
import android.util.Log;
import de.danoeh.antennapod.AppConfig;
import de.danoeh.antennapod.adapter.AbstractFeedItemlistAdapter;
import de.danoeh.antennapod.adapter.DefaultFeedItemlistAdapter;
import de.danoeh.antennapod.adapter.InternalFeedItemlistAdapter;
import de.danoeh.antennapod.feed.EventDistributor;
import de.danoeh.antennapod.feed.FeedItem;
import de.danoeh.antennapod.feed.FeedManager;
@ -12,7 +13,7 @@ public class PlaybackHistoryFragment extends ItemlistFragment {
private static final String TAG = "PlaybackHistoryFragment";
public PlaybackHistoryFragment() {
super(new AbstractFeedItemlistAdapter.ItemAccess() {
super(new DefaultFeedItemlistAdapter.ItemAccess() {
@Override
public FeedItem getItem(int position) {
@ -40,7 +41,7 @@ public class PlaybackHistoryFragment extends ItemlistFragment {
}
private EventDistributor.EventListener historyUpdate = new EventDistributor.EventListener() {
@Override
public void update(EventDistributor eventDistributor, Integer arg) {
if ((EventDistributor.PLAYBACK_HISTORY_UPDATE & arg) != 0) {
@ -48,7 +49,7 @@ public class PlaybackHistoryFragment extends ItemlistFragment {
Log.d(TAG, "Received content update");
fila.notifyDataSetChanged();
}
}
};