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"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/txtvTitle"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:textStyle="bold"/>
<RelativeLayout
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
android:id="@+id/txtvDownloaded"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentLeft="true"/>
android:layout_height="wrap_content"
android:layout_alignParentLeft="true" />
<TextView
android:id="@+id/txtvPercent"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"/>
android:layout_height="wrap_content"
android:layout_alignParentRight="true" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>

View File

@ -75,5 +75,7 @@
<string name="language_label">Language</string>
<string name="move_up_label">Move up</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>

View File

@ -1,6 +1,5 @@
package de.podfetcher.activity;
import de.podfetcher.R;
import de.podfetcher.storage.DownloadRequester;
import de.podfetcher.adapter.DownloadlistAdapter;
@ -9,62 +8,135 @@ import de.podfetcher.asynctask.DownloadStatus;
import de.podfetcher.feed.FeedMedia;
import de.podfetcher.feed.FeedFile;
import com.actionbarsherlock.app.SherlockListActivity;
import com.actionbarsherlock.view.ActionMode;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import android.content.Intent;
import android.os.Bundle;
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 {
private static final String TAG = "DownloadActivity";
private static final int MENU_SHOW_LOG = 0;
private DownloadlistAdapter dla;
private DownloadRequester requester;
/** Shows all running downloads in a list */
public class DownloadActivity extends SherlockListActivity implements
ActionMode.Callback {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
private static final String TAG = "DownloadActivity";
private static final int MENU_SHOW_LOG = 0;
private static final int MENU_CANCEL_ALL_DOWNLOADS = 1;
private DownloadlistAdapter dla;
private DownloadRequester requester;
requester = DownloadRequester.getInstance();
observer.execute(requester.getDownloads().toArray(
new FeedFile[requester.getDownloads().size()]));
private ActionMode mActionMode;
private DownloadStatus selectedDownload;
}
@Override
protected void onStop() {
super.onStop();
Log.d(TAG, "Stopping Activity");
observer.cancel(true);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
private final DownloadObserver observer = new DownloadObserver(this) {
@Override
protected void onProgressUpdate(DownloadStatus... values) {
if (dla != null) {
dla.notifyDataSetChanged();
} else {
dla = new DownloadlistAdapter(getContext(), 0, getStatusList());
setListAdapter(dla);
dla.notifyDataSetChanged();
}
}
};
@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);
requester = DownloadRequester.getInstance();
observer.execute(requester.getDownloads().toArray(
new FeedFile[requester.getDownloads().size()]));
}
@Override
protected void onStop() {
super.onStop();
Log.d(TAG, "Stopping Activity");
observer.cancel(true);
}
private final DownloadObserver observer = new DownloadObserver(this) {
@Override
protected void onProgressUpdate(DownloadStatus... values) {
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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_SHOW_LOG:
startActivity(new Intent(this, DownloadLogActivity.class));
break;
case MENU_CANCEL_ALL_DOWNLOADS:
requester.cancelAllDownloads(this);
break;
}
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;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.LayoutInflater;
import android.content.Context;
import android.widget.ArrayAdapter;
import android.widget.ProgressBar;
import android.widget.TextView;
import de.podfetcher.R;
import de.podfetcher.util.Converter;
import de.podfetcher.asynctask.DownloadObserver;
import de.podfetcher.asynctask.DownloadStatus;
import de.podfetcher.feed.Feed;
import de.podfetcher.feed.FeedFile;
import de.podfetcher.feed.FeedImage;
import de.podfetcher.feed.FeedMedia;
import de.podfetcher.util.Converter;
public class DownloadlistAdapter extends ArrayAdapter<DownloadStatus> {
public DownloadlistAdapter(Context context,
int textViewResourceId, DownloadStatus[] objects) {
super(context, textViewResourceId, objects);
}
public DownloadlistAdapter(Context context, int textViewResourceId,
DownloadStatus[] 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
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.downloaded = (TextView) convertView.findViewById(R.id.txtvDownloaded);
holder.percent = (TextView) convertView.findViewById(R.id.txtvPercent);
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
String titleText = null;
if (feedFile.getClass() == FeedMedia.class) {
titleText = ((FeedMedia) feedFile).getItem().getTitle();
} else if (feedFile.getClass() == Feed.class) {
titleText = ((Feed) feedFile).getTitle();
} else if (feedFile.getClass() == FeedImage.class) {
titleText = "[Image] " + ((FeedImage) feedFile).getTitle();
}
holder.title.setText(titleText);
holder.downloaded.setText(Converter.byteToString(status.getSoFar()) + " / "
+ Converter.byteToString(status.getSize()));
holder.percent.setText(status.getProgressPercent() + "%");
String titleText = null;
if (feedFile.getClass() == FeedMedia.class) {
titleText = ((FeedMedia) feedFile).getItem().getTitle();
} else if (feedFile.getClass() == Feed.class) {
titleText = ((Feed) feedFile).getTitle();
} else if (feedFile.getClass() == FeedImage.class) {
titleText = "[Image] " + ((FeedImage) feedFile).getTitle();
}
holder.title.setText(titleText);
holder.message.setText(status.getStatusMsg());
holder.downloaded.setText(Converter.byteToString(status.getSoFar())
+ " / " + Converter.byteToString(status.getSize()));
holder.percent.setText(status.getProgressPercent() + "%");
holder.progbar.setProgress(status.getProgressPercent());
return convertView;
}
return convertView;
}
static class Holder {
TextView title;
TextView downloaded;
TextView percent;
}
static class Holder {
TextView title;
TextView message;
TextView downloaded;
TextView percent;
ProgressBar progbar;
}
}

View File

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

View File

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

View File

@ -123,6 +123,20 @@ public class DownloadRequester {
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 */
public FeedFile getFeedFile(long id) {