Improved downloadlist activity

This commit is contained in:
daniel oeh 2012-07-05 12:43:57 +02:00
parent 66ebc4975b
commit 3c4df7a89d
7 changed files with 300 additions and 175 deletions

View File

@ -1,27 +1,44 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical"> android:orientation="vertical" >
<TextView <TextView
android:id="@+id/txtvTitle" android:id="@+id/txtvTitle"
android:layout_height="wrap_content"
android:layout_width="match_parent" android:layout_width="match_parent"
android:textStyle="bold"/>
<RelativeLayout
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_width="match_parent"> android:textStyle="bold" />
<TextView
android:id="@+id/txtvMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp" />
<ProgressBar
android:id="@+id/progProgress"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="16dp"
android:layout_margin="4dp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp" >
<TextView <TextView
android:id="@+id/txtvDownloaded" android:id="@+id/txtvDownloaded"
android:layout_height="wrap_content"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_alignParentLeft="true"/> android:layout_height="wrap_content"
android:layout_alignParentLeft="true" />
<TextView <TextView
android:id="@+id/txtvPercent" android:id="@+id/txtvPercent"
android:layout_height="wrap_content"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_alignParentRight="true"/> android:layout_height="wrap_content"
android:layout_alignParentRight="true" />
</RelativeLayout> </RelativeLayout>
</LinearLayout>
</LinearLayout>

View File

@ -75,5 +75,7 @@
<string name="language_label">Language</string> <string name="language_label">Language</string>
<string name="move_up_label">Move up</string> <string name="move_up_label">Move up</string>
<string name="move_down_label">Move down</string> <string name="move_down_label">Move down</string>
<string name="cancel_all_downloads_label">Cancel all downloads</string>
<string name="download_cancelled_msg">Download cancelled</string>
</resources> </resources>

View File

@ -1,6 +1,5 @@
package de.podfetcher.activity; package de.podfetcher.activity;
import de.podfetcher.R; import de.podfetcher.R;
import de.podfetcher.storage.DownloadRequester; import de.podfetcher.storage.DownloadRequester;
import de.podfetcher.adapter.DownloadlistAdapter; import de.podfetcher.adapter.DownloadlistAdapter;
@ -9,62 +8,135 @@ import de.podfetcher.asynctask.DownloadStatus;
import de.podfetcher.feed.FeedMedia; import de.podfetcher.feed.FeedMedia;
import de.podfetcher.feed.FeedFile; import de.podfetcher.feed.FeedFile;
import com.actionbarsherlock.app.SherlockListActivity; import com.actionbarsherlock.app.SherlockListActivity;
import com.actionbarsherlock.view.ActionMode;
import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem; import com.actionbarsherlock.view.MenuItem;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log; import android.util.Log;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
public class DownloadActivity extends SherlockListActivity { /** Shows all running downloads in a list */
private static final String TAG = "DownloadActivity"; public class DownloadActivity extends SherlockListActivity implements
private static final int MENU_SHOW_LOG = 0; ActionMode.Callback {
private DownloadlistAdapter dla;
private DownloadRequester requester;
@Override private static final String TAG = "DownloadActivity";
protected void onCreate(Bundle savedInstanceState) { private static final int MENU_SHOW_LOG = 0;
super.onCreate(savedInstanceState); private static final int MENU_CANCEL_ALL_DOWNLOADS = 1;
private DownloadlistAdapter dla;
private DownloadRequester requester;
requester = DownloadRequester.getInstance(); private ActionMode mActionMode;
observer.execute(requester.getDownloads().toArray( private DownloadStatus selectedDownload;
new FeedFile[requester.getDownloads().size()]));
} @Override
protected void onCreate(Bundle savedInstanceState) {
@Override super.onCreate(savedInstanceState);
protected void onStop() {
super.onStop();
Log.d(TAG, "Stopping Activity");
observer.cancel(true);
}
private final DownloadObserver observer = new DownloadObserver(this) { requester = DownloadRequester.getInstance();
@Override observer.execute(requester.getDownloads().toArray(
protected void onProgressUpdate(DownloadStatus... values) { new FeedFile[requester.getDownloads().size()]));
if (dla != null) {
dla.notifyDataSetChanged(); }
} else {
dla = new DownloadlistAdapter(getContext(), 0, getStatusList()); @Override
setListAdapter(dla); protected void onStop() {
dla.notifyDataSetChanged(); super.onStop();
} Log.d(TAG, "Stopping Activity");
} observer.cancel(true);
}; }
@Override private final DownloadObserver observer = new DownloadObserver(this) {
public boolean onCreateOptionsMenu(Menu menu) { @Override
menu.add(Menu.NONE, MENU_SHOW_LOG, Menu.NONE, R.string.show_download_log) protected void onProgressUpdate(DownloadStatus... values) {
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
dla = new DownloadlistAdapter(getContext(), 0, getStatusList());
setListAdapter(dla);
dla.notifyDataSetChanged();
}
};
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View view,
int position, long id) {
DownloadStatus selection = dla.getItem(position);
if (selection != null && mActionMode != null) {
mActionMode.finish();
}
selectedDownload = selection;
mActionMode = startActionMode(DownloadActivity.this);
return true;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(Menu.NONE, MENU_SHOW_LOG, Menu.NONE,
R.string.show_download_log).setShowAsAction(
MenuItem.SHOW_AS_ACTION_IF_ROOM);
menu.add(Menu.NONE, MENU_CANCEL_ALL_DOWNLOADS, Menu.NONE,
R.string.cancel_all_downloads_label).setShowAsAction(
MenuItem.SHOW_AS_ACTION_IF_ROOM);
return true; return true;
} }
@Override @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) { switch (item.getItemId()) {
case MENU_SHOW_LOG: case MENU_SHOW_LOG:
startActivity(new Intent(this, DownloadLogActivity.class)); startActivity(new Intent(this, DownloadLogActivity.class));
break;
case MENU_CANCEL_ALL_DOWNLOADS:
requester.cancelAllDownloads(this);
break;
} }
return true; return true;
} }
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
if (!selectedDownload.isDone()) {
menu.add(Menu.NONE, R.id.cancel_download_item, Menu.NONE,
R.string.cancel_download_label).setIcon(
R.drawable.navigation_cancel);
}
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
boolean handled = false;
switch (item.getItemId()) {
case R.id.cancel_download_item:
requester.cancelDownload(this, selectedDownload.getFeedFile()
.getDownloadId());
handled = true;
break;
}
mActionMode.finish();
return handled;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
mActionMode = null;
selectedDownload = null;
}
} }

View File

@ -1,67 +1,75 @@
package de.podfetcher.adapter; package de.podfetcher.adapter;
import android.widget.ArrayAdapter; import android.content.Context;
import android.widget.TextView; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.LayoutInflater; import android.widget.ArrayAdapter;
import android.content.Context; import android.widget.ProgressBar;
import android.widget.TextView;
import de.podfetcher.R; import de.podfetcher.R;
import de.podfetcher.util.Converter;
import de.podfetcher.asynctask.DownloadObserver;
import de.podfetcher.asynctask.DownloadStatus; import de.podfetcher.asynctask.DownloadStatus;
import de.podfetcher.feed.Feed; import de.podfetcher.feed.Feed;
import de.podfetcher.feed.FeedFile; import de.podfetcher.feed.FeedFile;
import de.podfetcher.feed.FeedImage; import de.podfetcher.feed.FeedImage;
import de.podfetcher.feed.FeedMedia; import de.podfetcher.feed.FeedMedia;
import de.podfetcher.util.Converter;
public class DownloadlistAdapter extends ArrayAdapter<DownloadStatus> { public class DownloadlistAdapter extends ArrayAdapter<DownloadStatus> {
public DownloadlistAdapter(Context context, public DownloadlistAdapter(Context context, int textViewResourceId,
int textViewResourceId, DownloadStatus[] objects) { DownloadStatus[] objects) {
super(context, textViewResourceId, objects); super(context, textViewResourceId, objects);
} }
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder;
DownloadStatus status = getItem(position);
FeedFile feedFile = status.getFeedFile();
// Inflate layout
if (convertView == null) {
holder = new Holder();
LayoutInflater inflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.downloadlist_item, null);
holder.title = (TextView) convertView.findViewById(R.id.txtvTitle);
holder.message = (TextView) convertView
.findViewById(R.id.txtvMessage);
holder.downloaded = (TextView) convertView
.findViewById(R.id.txtvDownloaded);
holder.percent = (TextView) convertView
.findViewById(R.id.txtvPercent);
holder.progbar = (ProgressBar) convertView
.findViewById(R.id.progProgress);
@Override convertView.setTag(holder);
public View getView(int position, View convertView, ViewGroup parent) { } else {
Holder holder; holder = (Holder) convertView.getTag();
DownloadStatus status = getItem(position); }
FeedFile feedFile = status.getFeedFile();
// Inflate layout
if (convertView == null) {
holder = new Holder();
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.downloadlist_item, null);
holder.title = (TextView) convertView.findViewById(R.id.txtvTitle);
holder.downloaded = (TextView) convertView.findViewById(R.id.txtvDownloaded);
holder.percent = (TextView) convertView.findViewById(R.id.txtvPercent);
convertView.setTag(holder); String titleText = null;
} else { if (feedFile.getClass() == FeedMedia.class) {
holder = (Holder) convertView.getTag(); titleText = ((FeedMedia) feedFile).getItem().getTitle();
} } else if (feedFile.getClass() == Feed.class) {
titleText = ((Feed) feedFile).getTitle();
String titleText = null; } else if (feedFile.getClass() == FeedImage.class) {
if (feedFile.getClass() == FeedMedia.class) { titleText = "[Image] " + ((FeedImage) feedFile).getTitle();
titleText = ((FeedMedia) feedFile).getItem().getTitle(); }
} else if (feedFile.getClass() == Feed.class) { holder.title.setText(titleText);
titleText = ((Feed) feedFile).getTitle(); holder.message.setText(status.getStatusMsg());
} else if (feedFile.getClass() == FeedImage.class) { holder.downloaded.setText(Converter.byteToString(status.getSoFar())
titleText = "[Image] " + ((FeedImage) feedFile).getTitle(); + " / " + Converter.byteToString(status.getSize()));
} holder.percent.setText(status.getProgressPercent() + "%");
holder.title.setText(titleText); holder.progbar.setProgress(status.getProgressPercent());
holder.downloaded.setText(Converter.byteToString(status.getSoFar()) + " / "
+ Converter.byteToString(status.getSize()));
holder.percent.setText(status.getProgressPercent() + "%");
return convertView; return convertView;
} }
static class Holder { static class Holder {
TextView title; TextView title;
TextView downloaded; TextView message;
TextView percent; TextView downloaded;
} TextView percent;
ProgressBar progbar;
}
} }

View File

@ -11,7 +11,8 @@ import java.util.concurrent.Callable;
import android.os.AsyncTask; import android.os.AsyncTask;
/** Observes the status of a specific Download */ /** Observes the status of a specific Download */
public class DownloadObserver extends AsyncTask<FeedFile, DownloadStatus, Boolean> { public class DownloadObserver extends
AsyncTask<FeedFile, DownloadStatus, Boolean> {
private static final String TAG = "DownloadObserver"; private static final String TAG = "DownloadObserver";
/** Types of downloads to observe. */ /** Types of downloads to observe. */
@ -25,10 +26,9 @@ public class DownloadObserver extends AsyncTask<FeedFile, DownloadStatus, Boolea
private final long DEFAULT_WAITING_INTERVALL = 1000L; private final long DEFAULT_WAITING_INTERVALL = 1000L;
private DownloadRequester requester; private DownloadRequester requester;
private Context context; private Context context;
private DownloadStatus[] statusList; private DownloadStatus[] statusList;
public DownloadObserver(Context context) { public DownloadObserver(Context context) {
super(); super();
@ -36,58 +36,63 @@ public class DownloadObserver extends AsyncTask<FeedFile, DownloadStatus, Boolea
requester = DownloadRequester.getInstance(); requester = DownloadRequester.getInstance();
} }
@Override @Override
protected void onCancelled(Boolean result) { protected void onCancelled(Boolean result) {
Log.d(TAG, "Task was cancelled."); Log.d(TAG, "Task was cancelled.");
} }
protected Boolean doInBackground(FeedFile... files) { protected Boolean doInBackground(FeedFile... files) {
Log.d(TAG, "Background Task started."); Log.d(TAG, "Background Task started.");
statusList = new DownloadStatus[files.length]; statusList = new DownloadStatus[files.length];
for (int i = 0; i < files.length; i++) { for (int i = 0; i < files.length; i++) {
FeedFile feedfile = files[i]; FeedFile feedfile = files[i];
statusList[i] = new DownloadStatus(feedfile); statusList[i] = new DownloadStatus(feedfile);
if (feedfile.getFile_url() == null) { if (feedfile.getFile_url() == null) {
statusList[i].reason = NO_DOWNLOAD_FOUND; statusList[i].reason = NO_DOWNLOAD_FOUND;
statusList[i].successful = false; statusList[i].successful = false;
statusList[i].done = true; statusList[i].done = true;
} }
if (feedfile.isDownloaded()) { if (feedfile.isDownloaded()) {
statusList[i].reason = ALREADY_DOWNLOADED; statusList[i].reason = ALREADY_DOWNLOADED;
statusList[i].successful = false; statusList[i].successful = false;
statusList[i].done = true; statusList[i].done = true;
} }
} }
while (downloadsLeft() && !isCancelled()) {
while(downloadsLeft() && !isCancelled()) { for (DownloadStatus status : statusList) {
for (DownloadStatus status : statusList) { if (status.done == false) {
if (status.done == false) { Cursor cursor = getDownloadCursor(status.feedfile);
Cursor cursor = getDownloadCursor(status.feedfile); int statusId = getDownloadStatus(cursor,
int statusId = getDownloadStatus(cursor, DownloadManager.COLUMN_STATUS); DownloadManager.COLUMN_STATUS);
getDownloadProgress(cursor, status); getDownloadProgress(cursor, status);
switch(statusId) { switch (statusId) {
case DownloadManager.STATUS_SUCCESSFUL: case DownloadManager.STATUS_SUCCESSFUL:
status.statusMsg = R.string.download_successful; status.statusMsg = R.string.download_successful;
status.successful = true; status.successful = true;
status.done = true; status.done = true;
case DownloadManager.STATUS_RUNNING: case DownloadManager.STATUS_RUNNING:
status.statusMsg = R.string.download_running; status.statusMsg = R.string.download_running;
break; break;
case DownloadManager.STATUS_FAILED: case DownloadManager.STATUS_FAILED:
status.statusMsg = R.string.download_failed; status.statusMsg = R.string.download_failed;
requester.notifyDownloadService(context); requester.notifyDownloadService(context);
status.successful = false; status.successful = false;
status.done = true; status.done = true;
status.reason = getDownloadStatus(cursor, DownloadManager.COLUMN_REASON); status.reason = getDownloadStatus(cursor,
case DownloadManager.STATUS_PENDING: DownloadManager.COLUMN_REASON);
status.statusMsg = R.string.download_pending; case DownloadManager.STATUS_PENDING:
break; status.statusMsg = R.string.download_pending;
} break;
} default:
} status.done = true;
status.successful = false;
status.statusMsg = R.string.download_cancelled_msg;
}
}
}
publishProgress(statusList); publishProgress(statusList);
@ -97,36 +102,39 @@ public class DownloadObserver extends AsyncTask<FeedFile, DownloadStatus, Boolea
Log.w(TAG, "Thread was interrupted while waiting."); Log.w(TAG, "Thread was interrupted while waiting.");
} }
} }
Log.d(TAG, "Background Task finished."); Log.d(TAG, "Background Task finished.");
return Boolean.valueOf(true); return Boolean.valueOf(true);
} }
public Cursor getDownloadCursor(FeedFile feedfile) { public Cursor getDownloadCursor(FeedFile feedfile) {
DownloadManager.Query query = buildQuery(feedfile.getDownloadId()); DownloadManager.Query query = buildQuery(feedfile.getDownloadId());
DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); DownloadManager manager = (DownloadManager) context
.getSystemService(Context.DOWNLOAD_SERVICE);
Cursor result = manager.query(query); Cursor result = manager.query(query);
return result; return result;
} }
public int getDownloadStatus(Cursor c, String column) { public int getDownloadStatus(Cursor c, String column) {
if(c.moveToFirst()) { if (c.moveToFirst()) {
int status = c.getInt(c.getColumnIndex(column)); int status = c.getInt(c.getColumnIndex(column));
return status; return status;
} else { } else {
return -1; return -1;
} }
} }
private void getDownloadProgress(Cursor c, DownloadStatus status) { private void getDownloadProgress(Cursor c, DownloadStatus status) {
if (c.moveToFirst()) { if (c.moveToFirst()) {
status.size = c.getLong(c.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES)); status.size = c.getLong(c
status.soFar = c.getLong(c.getColumnIndex( .getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)); status.soFar = c
status.progressPercent = (int) (( .getLong(c
(double) status.soFar / (double) status.size) * 100); .getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
Log.d(TAG, "Setting progress to " + status.progressPercent); status.progressPercent = (int) (((double) status.soFar / (double) status.size) * 100);
} Log.d(TAG, "Setting progress to " + status.progressPercent);
} }
}
private DownloadManager.Query buildQuery(long id) { private DownloadManager.Query buildQuery(long id) {
DownloadManager.Query query = new DownloadManager.Query(); DownloadManager.Query query = new DownloadManager.Query();
@ -138,19 +146,18 @@ public class DownloadObserver extends AsyncTask<FeedFile, DownloadStatus, Boolea
return context; return context;
} }
public DownloadStatus[] getStatusList() { public DownloadStatus[] getStatusList() {
return statusList; return statusList;
} }
private boolean downloadsLeft() { private boolean downloadsLeft() {
boolean result = false; boolean result = false;
for (int i = 0; i < statusList.length; i++) { for (int i = 0; i < statusList.length; i++) {
if (statusList[i].done == false) { if (statusList[i].done == false) {
return true; return true;
} }
} }
return result; return result;
} }
} }

View File

@ -84,6 +84,11 @@ public class DownloadStatus {
public void setId(long id) { public void setId(long id) {
this.id = id; this.id = id;
} }
public boolean isDone() {
return done;
}

View File

@ -123,6 +123,20 @@ public class DownloadRequester {
notifyDownloadService(context); notifyDownloadService(context);
} }
} }
/** Cancels all running downloads */
public void cancelAllDownloads(Context context) {
Log.d(TAG, "Cancelling all running downloads");
DownloadManager dm = (DownloadManager) context
.getSystemService(Context.DOWNLOAD_SERVICE);
for (FeedFile f : downloads) {
dm.remove(f.getDownloadId());
f.setFile_url(null);
f.setDownloadId(0);
}
downloads.clear();
notifyDownloadService(context);
}
/** Get a feedfile by its download id */ /** Get a feedfile by its download id */
public FeedFile getFeedFile(long id) { public FeedFile getFeedFile(long id) {