Example usage for android.app DownloadManager STATUS_FAILED

List of usage examples for android.app DownloadManager STATUS_FAILED

Introduction

In this page you can find the example usage for android.app DownloadManager STATUS_FAILED.

Prototype

int STATUS_FAILED

To view the source code for android.app DownloadManager STATUS_FAILED.

Click Source Link

Document

Value of #COLUMN_STATUS when the download has failed (and will not be retried).

Usage

From source file:Main.java

public static void CheckDwnloadStatus(DownloadManager downloadManager, Activity activity, long id) {

    // TODO Auto-generated method stub
    DownloadManager.Query query = new DownloadManager.Query();
    query.setFilterById(id);//  w ww.j  av a  2s  .c  om
    Cursor cursor = downloadManager.query(query);
    if (cursor.moveToFirst()) {
        int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
        int status = cursor.getInt(columnIndex);
        int columnReason = cursor.getColumnIndex(DownloadManager.COLUMN_REASON);
        int reason = cursor.getInt(columnReason);

        switch (status) {
        case DownloadManager.STATUS_FAILED:
            String failedReason = "";
            switch (reason) {
            case DownloadManager.ERROR_CANNOT_RESUME:
                failedReason = "ERROR_CANNOT_RESUME";
                break;
            case DownloadManager.ERROR_DEVICE_NOT_FOUND:
                failedReason = "ERROR_DEVICE_NOT_FOUND";
                break;
            case DownloadManager.ERROR_FILE_ALREADY_EXISTS:
                failedReason = "ERROR_FILE_ALREADY_EXISTS";
                break;
            case DownloadManager.ERROR_FILE_ERROR:
                failedReason = "ERROR_FILE_ERROR";
                break;
            case DownloadManager.ERROR_HTTP_DATA_ERROR:
                failedReason = "ERROR_HTTP_DATA_ERROR";
                break;
            case DownloadManager.ERROR_INSUFFICIENT_SPACE:
                failedReason = "ERROR_INSUFFICIENT_SPACE";
                break;
            case DownloadManager.ERROR_TOO_MANY_REDIRECTS:
                failedReason = "ERROR_TOO_MANY_REDIRECTS";
                break;
            case DownloadManager.ERROR_UNHANDLED_HTTP_CODE:
                failedReason = "ERROR_UNHANDLED_HTTP_CODE";
                break;
            case DownloadManager.ERROR_UNKNOWN:
                failedReason = "ERROR_UNKNOWN";
                break;
            default:
                failedReason = "unknown reason";
                break;
            }

            Toast.makeText(activity, "FAILED: " + failedReason, Toast.LENGTH_LONG).show();
            break;
        case DownloadManager.STATUS_PAUSED:
            String pausedReason = "";

            switch (reason) {
            case DownloadManager.PAUSED_QUEUED_FOR_WIFI:
                pausedReason = "PAUSED_QUEUED_FOR_WIFI";
                break;
            case DownloadManager.PAUSED_UNKNOWN:
                pausedReason = "PAUSED_UNKNOWN";
                break;
            case DownloadManager.PAUSED_WAITING_FOR_NETWORK:
                pausedReason = "PAUSED_WAITING_FOR_NETWORK";
                break;
            case DownloadManager.PAUSED_WAITING_TO_RETRY:
                pausedReason = "PAUSED_WAITING_TO_RETRY";
                break;
            }

            Toast.makeText(activity, "PAUSED: " + pausedReason, Toast.LENGTH_LONG).show();
            break;
        case DownloadManager.STATUS_PENDING:
            Toast.makeText(activity, "PENDING", Toast.LENGTH_LONG).show();
            break;
        case DownloadManager.STATUS_RUNNING:
            Toast.makeText(activity, "RUNNING", Toast.LENGTH_LONG).show();
            break;
        case DownloadManager.STATUS_SUCCESSFUL:

            Toast.makeText(activity, "SUCCESSFUL", Toast.LENGTH_LONG).show();
            break;
        }
    }
}

From source file:Main.java

public static String reasonString(Cursor c) {
    String msg = "???";

    //get reason index
    Integer statusint = statusInt(c);
    Integer reasonint = reasonInt(c);
    //interpret reason index depending on status
    if (statusint == DownloadManager.STATUS_PAUSED)
        msg = REASONLISTPAUSED[reasonint];
    else if (statusint == DownloadManager.STATUS_FAILED)
        msg = REASONLISTFAILED[reasonint - 1000];

    return (msg);
}

From source file:com.cypher.cota.helpers.DownloadHelper.java

private static void checkDownloadFinished(long downloadId, boolean installIfFinished) {
    long id = Long.parseLong(PreferenceUtils.getPreference(sContext, PreferenceUtils.DOWNLOAD_ROM_ID, "-1"));
    if (id == -1L || (downloadId != 0 && downloadId != id)) {
        return;//from  www. j  a  v  a  2s  .  c  o  m
    }
    String md5 = PreferenceUtils.getPreference(sContext, PreferenceUtils.DOWNLOAD_ROM_MD5, null);
    DownloadManager.Query query = new DownloadManager.Query();
    query.setFilterById(id);
    Cursor cursor = sDownloadManager.query(query);
    if (cursor.moveToFirst()) {
        int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
        int status = cursor.getInt(columnIndex);
        switch (status) {
        case DownloadManager.STATUS_FAILED:
            removeDownload(id, true);
            int reasonText = getDownloadError(cursor);
            sCallback.onDownloadError(sContext.getResources().getString(reasonText));
            break;
        case DownloadManager.STATUS_SUCCESSFUL:
            if (installIfFinished) {
                String uriString = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
                sCallback.onDownloadFinished(Uri.parse(uriString), md5);
            }
            downloadSuccesful();
            break;
        default:
            cancelDownload(id);
            break;
        }
    } else {
        removeDownload(id, true);
    }
    cursor.close();
}

From source file:com.commonsware.android.downmgr.DownloadFragment.java

private String statusMessage(Cursor c) {
    String msg = "???";

    switch (c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS))) {
    case DownloadManager.STATUS_FAILED:
        msg = getActivity().getString(R.string.download_failed);
        break;//from   w w  w . ja v a  2  s  .c  om

    case DownloadManager.STATUS_PAUSED:
        msg = getActivity().getString(R.string.download_paused);
        break;

    case DownloadManager.STATUS_PENDING:
        msg = getActivity().getString(R.string.download_pending);
        break;

    case DownloadManager.STATUS_RUNNING:
        msg = getActivity().getString(R.string.download_in_progress);
        break;

    case DownloadManager.STATUS_SUCCESSFUL:
        msg = getActivity().getString(R.string.download_complete);
        break;

    default:
        msg = getActivity().getString(R.string.download_is_nowhere_in_sight);
        break;
    }

    return (msg);
}

From source file:com.giovanniterlingen.windesheim.controllers.DownloadController.java

@Override
protected String doInBackground(final String... strings) {
    try {//from  w  w  w . ja  v  a2  s .  c o  m
        activeDownloads.put(contentId, new Download());
        int lastSlash = url.lastIndexOf('/');
        String fileName = url.substring(lastSlash + 1);

        File directory = Environment.getExternalStoragePublicDirectory(
                ApplicationLoader.applicationContext.getResources().getString(R.string.app_name));
        if (!directory.exists()) {
            directory.mkdirs();
        }

        final String encodedUrl = new URI("https", "elo.windesheim.nl", url, null).toString();

        downloadManager = (DownloadManager) activity.getSystemService(Context.DOWNLOAD_SERVICE);
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(encodedUrl));
        request.addRequestHeader("Cookie", new CookieController().getNatSchoolCookie()).setTitle(fileName)
                .setDescription(activity.getResources().getString(R.string.downloading))
                .setDestinationInExternalPublicDir(File.separator
                        + ApplicationLoader.applicationContext.getResources().getString(R.string.app_name),
                        fileName);

        currentDownloadId = downloadManager.enqueue(request);
        while (true) {
            if (isCancelled()) {
                return "cancelled";
            }
            DownloadManager.Query query = new DownloadManager.Query();
            query.setFilterById(currentDownloadId);
            Cursor cursor = downloadManager.query(query);
            if (cursor.getCount() == 0) {
                return "cancelled";
            }
            cursor.moveToFirst();
            int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
            if (status == DownloadManager.STATUS_SUCCESSFUL || status == DownloadManager.STATUS_FAILED) {
                break;
            }
            if (status == DownloadManager.STATUS_PAUSED || status == DownloadManager.STATUS_PENDING) {
                // paused, reset download state to pending
                activeDownloads.put(contentId, new Download());
                NotificationCenter.getInstance().postNotificationName(NotificationCenter.downloadPending,
                        studyRouteId, adapterPosition, contentId);
                Thread.sleep(100);
                continue;
            }
            long downloaded = cursor
                    .getLong(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
            long total = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
            cursor.close();
            if (total > 0 && downloaded > 0) {
                int progress = (int) (downloaded * 100 / total);
                String s = Formatter.formatFileSize(activity, downloaded) + "/"
                        + Formatter.formatFileSize(activity, total);
                activeDownloads.get(contentId).setProgress(progress);
                activeDownloads.get(contentId).setProgressString(s);
                publishProgress(progress, s);
            }
            Thread.sleep(100);
        }
        return new File(directory, fileName).getAbsolutePath();
    } catch (SecurityException e) {
        return "permission";
    } catch (Exception e) {
        return null;
    }
}

From source file:de.elanev.studip.android.app.frontend.courses.CourseDocumentsFragment.java

private void queryStatus(int queryStatus, int queryReason) {

    switch (queryStatus) {
    case DownloadManager.STATUS_FAILED:
        showToastMessage(getDownloadFailedReason(queryReason));
        break;//from www.  java 2s.  c  o m

    case DownloadManager.STATUS_SUCCESSFUL:
        showToastMessage(R.string.download_completed);
        try {
            // Show the download activity
            startActivity(new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS));
        } catch (ActivityNotFoundException e) {
            // No download manager installed or active, let the user handle the downloads.
        }

        break;

    default:
        showToastMessage(R.string.unknown_error);
        break;
    }

}

From source file:com.cypher.cota.helpers.DownloadHelper.java

private static long[] getDownloadProgress(long id) {
    DownloadManager.Query q = new DownloadManager.Query();
    q.setFilterById(id);/*from   ww  w  .  ja va 2 s.c o m*/

    Cursor cursor = sDownloadManager.query(q);
    int status;

    if (cursor == null || !cursor.moveToFirst()) {
        status = DownloadManager.STATUS_FAILED;
    } else {
        status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
    }

    long error = -1;
    long totalBytes = -1;
    long downloadedBytes = -1;

    switch (status) {
    case DownloadManager.STATUS_PAUSED:
    case DownloadManager.STATUS_RUNNING:
        downloadedBytes = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
        totalBytes = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
        break;
    case DownloadManager.STATUS_FAILED:
        sDownloadingRom = false;
        error = getDownloadError(cursor);
        break;
    }

    if (cursor != null) {
        cursor.close();
    }

    return new long[] { status, totalBytes, downloadedBytes, error };
}

From source file:scal.io.liger.LigerDownloadManager.java

public boolean checkQueue() {

    String fileName = ZipHelper.getExpansionZipFilename(context, mainOrPatch, version);
    String filePath = ZipHelper.getExpansionZipDirectory(context, mainOrPatch, version);

    File checkFile = new File(filePath, fileName + ".tmp");
    boolean foundInQueue = false;

    // need to check if a download has already been queued for this file
    //HashMap<Long, QueueItem> queueMap = QueueManager.loadQueue(context);

    //for (Long queueId : queueMap.keySet()) {

    //Timber.d("QUEUE ITEM IS " + queueMap.get(queueId).getQueueFile() + " LOOKING FOR " + checkFile.getName());

    //if (checkFile.getName().equals(queueMap.get(queueId).getQueueFile())) {

    Long queueId = QueueManager.checkQueue(context, checkFile);

    if (queueId == null) {

        // not found
        foundInQueue = false;/*from   w w w. ja va2  s  .  c  o  m*/

    } else if (queueId.equals(QueueManager.DUPLICATE_QUERY)) {

        // not exactly in queue, but someone is already looking for this item, so avoid collision
        foundInQueue = true;

    } else if (queueId < 0) {
        // use negative numbers to flag non-manager downloads

        if (checkFileProgress()) {

            Timber.d("QUEUE ITEM FOUND FOR " + checkFile.getName() + " AND DOWNLOAD PROGRESS OBSERVED, LEAVING "
                    + queueId.toString() + " IN QUEUE ");
            foundInQueue = true;

        } else {

            Timber.d("QUEUE ITEM FOUND FOR " + checkFile.getName()
                    + " BUT NO DOWNLOAD PROGRESS OBSERVED, REMOVING " + queueId.toString() + " FROM QUEUE ");
            QueueManager.removeFromQueue(context, Long.valueOf(queueId));

        }

    } else {
        // use download manager ids to flag manager downloads

        // need to init download manager to check queue
        initDownloadManager();

        DownloadManager.Query query = new DownloadManager.Query();
        query.setFilterById(queueId.longValue());
        Cursor c = dManager.query(query);
        try {
            if (c.moveToFirst()) {

                int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
                if (DownloadManager.STATUS_FAILED == c.getInt(columnIndex)) {

                    Timber.d("QUEUE ITEM FOUND FOR " + checkFile.getName()
                            + " BUT DOWNLOAD STATUS IS FAILED, REMOVING " + queueId.toString()
                            + " FROM QUEUE ");
                    QueueManager.removeFromQueue(context, Long.valueOf(queueId));

                } else if (DownloadManager.STATUS_PAUSED == c.getInt(columnIndex)) {

                    Timber.d("QUEUE ITEM FOUND FOR " + checkFile.getName()
                            + " AND DOWNLOAD STATUS IS PAUSED, LEAVING " + queueId.toString() + " IN QUEUE ");
                    foundInQueue = true;

                } else if (DownloadManager.STATUS_PENDING == c.getInt(columnIndex)) {

                    Timber.d("QUEUE ITEM FOUND FOR " + checkFile.getName()
                            + " AND DOWNLOAD STATUS IS PENDING, LEAVING " + queueId.toString() + " IN QUEUE ");
                    foundInQueue = true;

                } else if (DownloadManager.STATUS_RUNNING == c.getInt(columnIndex)) {

                    Timber.d("QUEUE ITEM FOUND FOR " + checkFile.getName()
                            + " AND DOWNLOAD STATUS IS RUNNING, LEAVING " + queueId.toString() + " IN QUEUE ");
                    foundInQueue = true;

                } else if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {

                    Timber.d("QUEUE ITEM FOUND FOR " + checkFile.getName()
                            + " BUT DOWNLOAD STATUS IS SUCCESSFUL, REMOVING " + queueId.toString()
                            + " FROM QUEUE ");
                    QueueManager.removeFromQueue(context, Long.valueOf(queueId));

                } else {

                    Timber.d("QUEUE ITEM FOUND FOR " + checkFile.getName()
                            + " BUT DOWNLOAD STATUS IS UNKNOWN, REMOVING " + queueId.toString()
                            + " FROM QUEUE ");
                    QueueManager.removeFromQueue(context, Long.valueOf(queueId));

                }
            } else {

                Timber.d("QUEUE ITEM FOUND FOR " + checkFile.getName()
                        + " BUT NOTHING FOUND IN DOWNLOAD MANAGER, REMOVING " + queueId.toString()
                        + " FROM QUEUE ");
                QueueManager.removeFromQueue(context, Long.valueOf(queueId));

            }
        } finally {
            if (c != null) {
                c.close();
            }
        }

        // cleanup
        c.close();
    }
    //}

    // skipping timeout check for now, timeout duration undecided

    /*
    if (foundInQueue) {
        Date currentTime = new Date();
        long queuedTime = queueMap.get(queueId).getQueueTime();
        if ((currentTime.getTime() - queueMap.get(queueId).getQueueTime()) > QueueManager.queueTimeout) {
            
            Timber.d("TIMEOUT EXCEEDED, REMOVING " + queueId.toString() + " FROM DOWNLOAD MANAGER.");
            int numberRemoved = manager.remove(queueId);
            
            if (numberRemoved == 1) {
                Timber.d("REMOVED FROM DOWNLOAD MANAGER, RE-QUEUEING: " + queueId.toString() + " -> " + uriFile.toString());
                QueueManager.removeFromQueue(context, Long.valueOf(queueId));
                foundInQueue = false;
            } else {
                Timber.d("FAILED TO REMOVE FROM DOWNLOAD MANAGER, NOT QUEUEING: " + queueId.toString() + " -> " + uriFile.toString());
            }
        }
    }
    */
    //}

    return foundInQueue;
}

From source file:com.concentricsky.android.khanacademy.util.OfflineVideoManager.java

/**
 * Cancel ongoing and enqueued video downloads.
 *///from   www .  j  av a 2 s .  c  o  m
public void cancelAllVideoDownloads() {
    final DownloadManager dlm = getDownloadManager();
    final DownloadManager.Query q = new DownloadManager.Query();
    q.setFilterByStatus(DownloadManager.STATUS_FAILED | DownloadManager.STATUS_PAUSED
            | DownloadManager.STATUS_PENDING | DownloadManager.STATUS_RUNNING);

    // Cancel all tasks - we don't want any more downloads enqueued, and we are
    // beginning a cancel task so we don't need any previous one.
    queueExecutor.shutdownNow();
    queueExecutor = Executors.newSingleThreadExecutor();

    new AsyncTask<Void, Void, Integer>() {
        @Override
        protected void onPreExecute() {
            doToast("Stopping downloads...");
        }

        @Override
        protected Integer doInBackground(Void... arg) {
            int result = 0;
            if (isCancelled())
                return result;

            Cursor c = dlm.query(q);
            Long[] removed = new Long[c.getCount()];
            int i = 0;
            while (c.moveToNext()) {
                if (isCancelled())
                    break;

                long id = c.getLong(c.getColumnIndex(DownloadManager.COLUMN_ID));
                removed[i++] = id;
                dlm.remove(id);
                result++;
            }
            c.close();

            UpdateBuilder<Video, String> u = videoDao.updateBuilder();
            try {
                u.where().in("dlm_id", (Object[]) removed);
                u.updateColumnValue("download_status", Video.DL_STATUS_NOT_STARTED);
                u.update();
            } catch (SQLException e) {
                e.printStackTrace();
            }

            return result;
        }

        @Override
        protected void onPostExecute(Integer result) {
            if (result > 0) {
                doToast(result + " downloads cancelled.");
            } else {
                doToast("No downloads in queue.");
            }
            doOfflineVideoSetChanged();
        }

        @Override
        protected void onCancelled(Integer result) {
            if (result > 0) {
                doToast(result + " downloads cancelled.");
            }
            doOfflineVideoSetChanged();
        }
    }.executeOnExecutor(queueExecutor);
}

From source file:org.chromium.chrome.browser.download.DownloadManagerService.java

@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    if (!DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action))
        return;/*from   www  . j a v  a2 s . c  o m*/
    final DownloadManager manager = (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE);

    long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
    if (downloadId == -1)
        return;
    boolean isPendingOMADownload = mOMADownloadHandler.isPendingOMADownload(downloadId);
    boolean isInOMASharedPrefs = isDownloadIdInOMASharedPrefs(downloadId);
    if (isPendingOMADownload || isInOMASharedPrefs) {
        clearPendingOMADownload(downloadId, null);
        mPendingAutoOpenDownloads.remove(downloadId);
    } else if (mPendingAutoOpenDownloads.get(downloadId) != null) {
        Cursor c = manager.query(new DownloadManager.Query().setFilterById(downloadId));
        int statusIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
        while (c.moveToNext()) {
            int status = c.getInt(statusIndex);
            DownloadInfo info = mPendingAutoOpenDownloads.get(downloadId);
            switch (status) {
            case DownloadManager.STATUS_SUCCESSFUL:
                try {
                    mPendingAutoOpenDownloads.remove(downloadId);
                    if (OMADownloadHandler.OMA_DOWNLOAD_DESCRIPTOR_MIME.equalsIgnoreCase(info.getMimeType())) {
                        mOMADownloadHandler.handleOMADownload(info, downloadId);
                        manager.remove(downloadId);
                        break;
                    }
                    Uri uri = manager.getUriForDownloadedFile(downloadId);
                    Intent launchIntent = new Intent(Intent.ACTION_VIEW);

                    launchIntent.setDataAndType(uri, manager.getMimeTypeForDownloadedFile(downloadId));
                    launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                    mContext.startActivity(launchIntent);
                } catch (ActivityNotFoundException e) {
                    Log.w(TAG, "Activity not found.");
                }
                break;
            case DownloadManager.STATUS_FAILED:
                mPendingAutoOpenDownloads.remove(downloadId);
                break;
            default:
                break;
            }
        }
    }

    if (mPendingAutoOpenDownloads.size() == 0) {
        mContext.unregisterReceiver(this);
    }
}