Add ability to clear the download log, as requested in AntennaPod/AntennaPod#506

This commit is contained in:
Martin Fietz 2015-02-28 19:23:39 +01:00
parent 30a202665a
commit 8f9c9c8d3b
3 changed files with 73 additions and 3 deletions

View File

@ -1,25 +1,35 @@
package de.danoeh.antennapod.fragment; package de.danoeh.antennapod.fragment;
import android.content.Context; import android.content.Context;
import android.content.res.TypedArray;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.ListFragment; import android.support.v4.app.ListFragment;
import android.support.v4.view.MenuItemCompat;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.widget.ListView; import android.widget.ListView;
import java.util.List;
import de.danoeh.antennapod.R; import de.danoeh.antennapod.R;
import de.danoeh.antennapod.adapter.DownloadLogAdapter; import de.danoeh.antennapod.adapter.DownloadLogAdapter;
import de.danoeh.antennapod.core.feed.EventDistributor; import de.danoeh.antennapod.core.feed.EventDistributor;
import de.danoeh.antennapod.core.service.download.DownloadStatus; import de.danoeh.antennapod.core.service.download.DownloadStatus;
import de.danoeh.antennapod.core.storage.DBReader; import de.danoeh.antennapod.core.storage.DBReader;
import de.danoeh.antennapod.core.storage.DBWriter;
import java.util.List; import de.danoeh.antennapod.menuhandler.MenuItemUtils;
import de.danoeh.antennapod.menuhandler.NavDrawerActivity;
/** /**
* Shows the download log * Shows the download log
*/ */
public class DownloadLogFragment extends ListFragment { public class DownloadLogFragment extends ListFragment {
private static final String TAG = "DownloadLogFragment";
private List<DownloadStatus> downloadLog; private List<DownloadStatus> downloadLog;
private DownloadLogAdapter adapter; private DownloadLogAdapter adapter;
@ -29,6 +39,7 @@ public class DownloadLogFragment extends ListFragment {
@Override @Override
public void onStart() { public void onStart() {
super.onStart(); super.onStart();
setHasOptionsMenu(true);
EventDistributor.getInstance().register(contentUpdate); EventDistributor.getInstance().register(contentUpdate);
startItemLoader(); startItemLoader();
} }
@ -63,7 +74,7 @@ public class DownloadLogFragment extends ListFragment {
} }
setListShown(true); setListShown(true);
adapter.notifyDataSetChanged(); adapter.notifyDataSetChanged();
getActivity().supportInvalidateOptionsMenu();
} }
private DownloadLogAdapter.ItemAccess itemAccess = new DownloadLogAdapter.ItemAccess() { private DownloadLogAdapter.ItemAccess itemAccess = new DownloadLogAdapter.ItemAccess() {
@ -105,6 +116,41 @@ public class DownloadLogFragment extends ListFragment {
} }
} }
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
if (itemsLoaded && !MenuItemUtils.isActivityDrawerOpen((NavDrawerActivity) getActivity())) {
MenuItem clearHistory = menu.add(Menu.NONE, R.id.clear_history_item, Menu.CATEGORY_CONTAINER, R.string.clear_history_label);
MenuItemCompat.setShowAsAction(clearHistory, MenuItemCompat.SHOW_AS_ACTION_IF_ROOM);
TypedArray drawables = getActivity().obtainStyledAttributes(new int[]{R.attr.content_discard});
clearHistory.setIcon(drawables.getDrawable(0));
drawables.recycle();
}
}
@Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
if (itemsLoaded && !MenuItemUtils.isActivityDrawerOpen((NavDrawerActivity) getActivity())) {
menu.findItem(R.id.clear_history_item).setVisible(downloadLog != null && !downloadLog.isEmpty());
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (!super.onOptionsItemSelected(item)) {
switch (item.getItemId()) {
case R.id.clear_history_item:
DBWriter.clearDownloadLog(getActivity());
return true;
default:
return false;
}
} else {
return true;
}
}
private class ItemLoader extends AsyncTask<Void, Void, List<DownloadStatus>> { private class ItemLoader extends AsyncTask<Void, Void, List<DownloadStatus>> {
@Override @Override

View File

@ -238,6 +238,26 @@ public class DBWriter {
}); });
} }
/**
* Deletes the entire download log.
*
* @param context A context that is used for opening a database connection.
*/
public static Future<?> clearDownloadLog(final Context context) {
return dbExec.submit(new Runnable() {
@Override
public void run() {
PodDBAdapter adapter = new PodDBAdapter(context);
adapter.open();
adapter.clearDownloadLog();
adapter.close();
EventDistributor.getInstance()
.sendDownloadLogUpdateBroadcast();
}
});
}
/** /**
* Adds a FeedMedia object to the playback history. A FeedMedia object is in the playback history if * Adds a FeedMedia object to the playback history. A FeedMedia object is in the playback history if
* its playback completion date is set to a non-null value. This method will set the playback completion date to the * its playback completion date is set to a non-null value. This method will set the playback completion date to the

View File

@ -889,6 +889,10 @@ public class PodDBAdapter {
db.update(TABLE_NAME_FEED_MEDIA, values, null, null); db.update(TABLE_NAME_FEED_MEDIA, values, null, null);
} }
public void clearDownloadLog() {
db.delete(TABLE_NAME_DOWNLOAD_LOG, null, null);
}
/** /**
* Get all Feeds from the Feed Table. * Get all Feeds from the Feed Table.
* *