import android.os.Environment;
import android.os.Handler;
import android.provider.Downloads;
+import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import com.android.providers.downloads.ui.DownloadItem.DownloadSelectListener;
-import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashSet;
private View mEmptyView;
private ViewGroup mSelectionMenuView;
private Button mSelectionDeleteButton;
- private Button mSelectionRestartButton;
private DownloadManager mDownloadManager;
private Cursor mDateSortedCursor;
private int mLocalUriColumnId;
private int mMediaTypeColumnId;
private int mReasonColumndId;
+ private int mMediaProviderUriId;
private boolean mIsSortedBySize = false;
private Set<Long> mSelectedIds = new HashSet<Long>();
mDateSortedCursor.getColumnIndexOrThrow(DownloadManager.COLUMN_MEDIA_TYPE);
mReasonColumndId =
mDateSortedCursor.getColumnIndexOrThrow(DownloadManager.COLUMN_REASON);
+ mMediaProviderUriId =
+ mDateSortedCursor.getColumnIndexOrThrow(
+ DownloadManager.COLUMN_MEDIAPROVIDER_URI);
mDateSortedAdapter = new DateSortedDownloadAdapter(this, mDateSortedCursor, this);
mDateOrderedListView.setAdapter(mDateSortedAdapter);
mSelectionMenuView = (ViewGroup) findViewById(R.id.selection_menu);
mSelectionDeleteButton = (Button) findViewById(R.id.selection_delete);
mSelectionDeleteButton.setOnClickListener(this);
- mSelectionRestartButton = (Button) findViewById(R.id.selection_retry);
- mSelectionRestartButton.setOnClickListener(this);
((Button) findViewById(R.id.deselect_all)).setOnClickListener(this);
}
if (isPausedForWifi(cursor)) {
mQueuedDownloadId = id;
mQueuedDialog = new AlertDialog.Builder(this)
- .setTitle(R.string.dialog_title_not_available)
+ .setTitle(R.string.dialog_title_queued_body)
.setMessage(R.string.dialog_queued_body)
.setPositiveButton(R.string.keep_queued_download, null)
.setNegativeButton(R.string.remove_download, getDeleteClickHandler(id))
new AlertDialog.Builder(this)
.setTitle(R.string.dialog_title_not_available)
.setMessage(dialogBody)
- .setPositiveButton(R.string.remove_download, getDeleteClickHandler(downloadId))
- .setNegativeButton(R.string.retry_download, getRestartClickHandler(downloadId))
+ .setNegativeButton(R.string.delete_download, getDeleteClickHandler(downloadId))
+ .setPositiveButton(R.string.retry_download, getRestartClickHandler(downloadId))
.show();
}
* Set up the contents of the selection menu based on the current selection.
*/
private void updateSelectionMenu() {
- mSelectionRestartButton.setVisibility(View.GONE);
int deleteButtonStringId = R.string.delete_download;
if (mSelectedIds.size() == 1) {
Cursor cursor = mDownloadManager.query(new DownloadManager.Query()
cursor.moveToFirst();
switch (cursor.getInt(mStatusColumnId)) {
case DownloadManager.STATUS_FAILED:
- deleteButtonStringId = R.string.remove_download;
- mSelectionRestartButton.setVisibility(View.VISIBLE);
+ deleteButtonStringId = R.string.delete_download;
break;
case DownloadManager.STATUS_PENDING:
clearSelection();
break;
- case R.id.selection_retry:
- for (Long downloadId : mSelectedIds) {
- mDownloadManager.restartDownload(downloadId);
- }
- clearSelection();
- break;
-
case R.id.deselect_all:
clearSelection();
break;
if (isComplete && localUri != null) {
String path = Uri.parse(localUri).getPath();
if (path.startsWith(Environment.getExternalStorageDirectory().getPath())) {
- String mediaType = mDateSortedCursor.getString(mMediaTypeColumnId);
- deleteDownloadedFile(path, mediaType);
+ String mediaProviderUri = mDateSortedCursor.getString(mMediaProviderUriId);
+ if (TextUtils.isEmpty(mediaProviderUri)) {
+ // downloads database doesn't have the mediaprovider_uri. It means
+ // this download occurred before mediaprovider_uri column existed
+ // in downloads table. Since MediaProvider needs the mediaprovider_uri to
+ // delete this download, just set the 'deleted' flag to 1 on this row
+ // in the database. DownloadService, upon seeing this flag set to 1, will
+ // re-scan the file and get the MediaProviderUri and then delete the file
+ mDownloadManager.markRowDeleted(downloadId);
+ return;
+ } else {
+ getContentResolver().delete(Uri.parse(mediaProviderUri), null, null);
+ }
}
}
}
mDownloadManager.remove(downloadId);
}
- /**
- * Delete the file at the given path. Try sending an intent to delete it, but if that goes
- * unhandled, delete it ourselves.
- * @param path path to the file to delete
- */
- private void deleteDownloadedFile(String path, String mediaType) {
- Intent intent = new Intent(Intent.ACTION_DELETE);
- File file = new File(path);
- intent.setDataAndType(Uri.fromFile(file), mediaType);
- try {
- startActivity(intent);
- } catch (ActivityNotFoundException exc) {
- file.delete();
- }
- }
-
@Override
public boolean isDownloadSelected(long id) {
return mSelectedIds.contains(id);