Example usage for android.app DownloadManager STATUS_PENDING

List of usage examples for android.app DownloadManager STATUS_PENDING

Introduction

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

Prototype

int STATUS_PENDING

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

Click Source Link

Document

Value of #COLUMN_STATUS when the download is waiting to start.

Usage

From source file:Main.java

/**
 * Find a download with the specified name.  Returns -1 if none was
 * found./*  ww w  .  j  a  va2 s .  c o  m*/
 */
static long findPath(DownloadManager dm, String path) {
    DownloadManager.Query query = new DownloadManager.Query();
    query.setFilterByStatus(
            DownloadManager.STATUS_PAUSED | DownloadManager.STATUS_PENDING | DownloadManager.STATUS_RUNNING);
    Cursor c = dm.query(query);

    if (!c.moveToFirst())
        return -1;

    final int columnID = c.getColumnIndexOrThrow(DownloadManager.COLUMN_ID);
    final int columnLocalURI = c.getColumnIndexOrThrow(DownloadManager.COLUMN_LOCAL_URI);

    do {
        final String uri = c.getString(columnLocalURI);
        if (uri != null && uri.endsWith(path))
            return c.getLong(columnID);
    } while (c.moveToNext());

    return -1;
}

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 w  w.java  2 s .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:com.appjma.appdeployer.adapter.AppVersionsAdapter.java

@Override
public void bindView(View view, Context context, Cursor cursor) {
    ViewHolder holder = (ViewHolder) view.getTag();
    String version = cursor.getString(PROJECTION_VERSION);
    long updatedAt = cursor.getLong(PROJECTION_UPDATED_AT);
    long id = cursor.getLong(PROJECTION_APP_VERSION_ID);
    String downloadManagerId = cursor.getString(PROJECTION_DOWNLOAD_MANAGER_ID);

    int status = -1;
    if (downloadManagerId != null) {
        DownloadItem downloadItem = mMap.get(downloadManagerId);
        if (downloadItem != null) {
            status = downloadItem.mStatus;
        }//from www. j a v  a2  s.c om
    }
    if (status == DownloadManager.STATUS_PENDING || status == DownloadManager.STATUS_RUNNING) {
        holder.mButton.setBackgroundResource(R.drawable.ic_list_item_downloading);
    } else if (status == DownloadManager.STATUS_SUCCESSFUL) {
        holder.mButton.setBackgroundResource(R.drawable.ic_list_item_downloaded);
    } else {
        holder.mButton.setBackgroundResource(R.drawable.ic_list_item_download);
    }
    holder.mPosition = cursor.getPosition();
    holder.mText1.setText(String.format(mVersionFormat, version));
    CharSequence updatedAtText = DateUtils.getRelativeTimeSpanString(updatedAt, mNow,
            DateUtils.MINUTE_IN_MILLIS);
    holder.mText2.setText(updatedAtText);
    holder.mId = id;
}

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;// w w w .j  a  va  2s  .c o m

    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 {/*  w w  w.  j a va2  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:com.example.linhdq.test.main_menu.language.OCRLanguageActivity.java

private void updateLanguageListWithDownloadManagerStatus(OCRLanguageAdapter adapter) {
    if (adapter != null) {
        // find languages that are currently being downloaded
        Query query = new Query();
        query.setFilterByStatus(DownloadManager.STATUS_RUNNING | DownloadManager.STATUS_PENDING
                | DownloadManager.STATUS_PAUSED);
        final DownloadManager dm = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
        Cursor c = dm.query(query);
        if (c == null) {
            return;
        }/*from  w  ww . jav a 2  s.c  o  m*/
        int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_TITLE);
        while (c.moveToNext()) {
            final String title = c.getString(columnIndex);
            adapter.setDownloading(title, true);
        }
        adapter.notifyDataSetChanged();
        c.close();
    }
}

From source file:com.renard.ocr.help.OCRLanguageActivity.java

private void updateLanguageListWithDownloadManagerStatus(OCRLanguageAdapter adapter) {
    if (adapter != null) {
        // find languages that are currently beeing downloaded
        Query query = new Query();
        query.setFilterByStatus(DownloadManager.STATUS_RUNNING | DownloadManager.STATUS_PENDING
                | DownloadManager.STATUS_PAUSED);
        final DownloadManager dm = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
        Cursor c = dm.query(query);
        int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_TITLE);
        while (c.moveToNext()) {
            final String title = c.getString(columnIndex);
            adapter.setDownloading(title, true);
        }/*w  w w .  j av  a 2  s .c  o  m*/
        adapter.notifyDataSetChanged();
        c.close();
    }
}

From source file:com.bluros.updater.UpdatesSettings.java

@Override
protected void onStart() {
    super.onStart();

    // Determine if there are any in-progress downloads
    mDownloadId = mPrefs.getLong(Constants.DOWNLOAD_ID, -1);
    if (mDownloadId >= 0) {
        Cursor c = mDownloadManager.query(new DownloadManager.Query().setFilterById(mDownloadId));
        if (c == null || !c.moveToFirst()) {
            Toast.makeText(this, R.string.download_not_found, Toast.LENGTH_LONG).show();
        } else {/*w  w  w .  j a v a  2s  .  c  o  m*/
            int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
            Uri uri = Uri.parse(c.getString(c.getColumnIndex(DownloadManager.COLUMN_URI)));
            if (status == DownloadManager.STATUS_PENDING || status == DownloadManager.STATUS_RUNNING
                    || status == DownloadManager.STATUS_PAUSED) {
                mDownloadFileName = uri.getLastPathSegment();
            }
        }
        if (c != null) {
            c.close();
        }
    }
    if (mDownloadId < 0 || mDownloadFileName == null) {
        resetDownloadState();
    }

    requestUpdateLayout();

    IntentFilter filter = new IntentFilter(UpdateCheckService.ACTION_CHECK_FINISHED);
    filter.addAction(DownloadReceiver.ACTION_DOWNLOAD_STARTED);
    registerReceiver(mReceiver, filter);

    checkForDownloadCompleted(getIntent());
    setIntent(null);
}

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. j  ava 2 s.co  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:org.apache.cordova.backgroundDownload.BackgroundDownload.java

private long findActiveDownload(String uri) {
    DownloadManager mgr = (DownloadManager) cordova.getActivity().getSystemService(Context.DOWNLOAD_SERVICE);

    long downloadId = DOWNLOAD_ID_UNDEFINED;

    DownloadManager.Query query = new DownloadManager.Query();
    query.setFilterByStatus(DownloadManager.STATUS_PAUSED | DownloadManager.STATUS_PENDING
            | DownloadManager.STATUS_RUNNING | DownloadManager.STATUS_SUCCESSFUL);
    Cursor cur = mgr.query(query);
    int idxId = cur.getColumnIndex(DownloadManager.COLUMN_ID);
    int idxUri = cur.getColumnIndex(DownloadManager.COLUMN_URI);
    for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
        if (uri.equals(cur.getString(idxUri))) {
            downloadId = cur.getLong(idxId);
            break;
        }/*from w ww  . j a  v a 2s . c o  m*/
    }
    cur.close();

    return downloadId;
}