Example usage for android.app DownloadManager COLUMN_ID

List of usage examples for android.app DownloadManager COLUMN_ID

Introduction

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

Prototype

String COLUMN_ID

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

Click Source Link

Document

An identifier for a particular download, unique across the system.

Usage

From source file:Main.java

/**
 * Find a download with the specified name.  Returns -1 if none was
 * found.//from  w  ww . j a va 2s .  co  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:com.appjma.appdeployer.adapter.DownloadLoader.java

@Override
public Map<String, DownloadLoader.DownloadItem> loadInBackground() {
    Map<String, DownloadLoader.DownloadItem> map = Maps.newHashMap();
    Cursor cursor = mDownloadManager.query(new DownloadManager.Query());
    int columnId = cursor.getColumnIndex(DownloadManager.COLUMN_ID);
    int columnStatus = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
    try {//from  ww w  .  ja va  2 s  .  c  o  m
        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
            String id = cursor.getString(columnId);
            int status = cursor.getInt(columnStatus);
            map.put(id, new DownloadItem(id, status));
        }
    } finally {
        cursor.close();
    }
    return map;
}

From source file:com.commonsware.android.arXiv.DownloadsActivity.java

private void deleteFiles() {
    DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
    Cursor c = dm.query(new DownloadManager.Query());
    if (c != null) {
        long[] ids = new long[c.getCount()];
        int position = 0;
        c.moveToFirst();//from  w w w.  j  a va  2  s .c o  m
        while (!c.isAfterLast()) {
            ids[position] = c.getLong(c.getColumnIndex(DownloadManager.COLUMN_ID));
            c.moveToNext();
            position++;
        }
        dm.remove(ids);
    }
    Toast.makeText(DownloadsActivity.this, R.string.deleted_history, Toast.LENGTH_SHORT).show();
    finish();
}

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

private void queryStatus(View v) {
    Cursor c = mgr.query(new DownloadManager.Query().setFilterById(lastDownload));

    if (c == null) {
        Toast.makeText(getActivity(), R.string.download_not_found, Toast.LENGTH_LONG).show();
    } else {// ww w  .j  a v  a2s  . c  o m
        c.moveToFirst();

        Log.d(getClass().getName(), "COLUMN_ID: " + c.getLong(c.getColumnIndex(DownloadManager.COLUMN_ID)));
        Log.d(getClass().getName(), "COLUMN_BYTES_DOWNLOADED_SO_FAR: "
                + c.getLong(c.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)));
        Log.d(getClass().getName(), "COLUMN_LAST_MODIFIED_TIMESTAMP: "
                + c.getLong(c.getColumnIndex(DownloadManager.COLUMN_LAST_MODIFIED_TIMESTAMP)));
        Log.d(getClass().getName(),
                "COLUMN_LOCAL_URI: " + c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI)));
        Log.d(getClass().getName(),
                "COLUMN_STATUS: " + c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS)));
        Log.d(getClass().getName(),
                "COLUMN_REASON: " + c.getInt(c.getColumnIndex(DownloadManager.COLUMN_REASON)));

        Toast.makeText(getActivity(), statusMessage(c), Toast.LENGTH_LONG).show();

        c.close();
    }
}

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 w w.  ja va2  s.co m
    }
    cur.close();

    return downloadId;
}

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

/**
 * Cancel ongoing and enqueued video downloads.
 *///w  ww  .j a  va  2  s  .c  om
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:com.hughes.android.dictionary.DictionaryManagerActivity.java

private synchronized void downloadDictionary(final String downloadUrl, long bytes, Button downloadButton) {
    String destFile;/*from ww w  .j a v  a 2  s.  c o  m*/
    try {
        destFile = new File(new URL(downloadUrl).getPath()).getName();
    } catch (MalformedURLException e) {
        throw new RuntimeException("Invalid download URL!", e);
    }
    DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
    final DownloadManager.Query query = new DownloadManager.Query();
    query.setFilterByStatus(
            DownloadManager.STATUS_PAUSED | DownloadManager.STATUS_PENDING | DownloadManager.STATUS_RUNNING);
    final Cursor cursor = downloadManager.query(query);

    // Due to a bug, cursor is null instead of empty when
    // the download manager is disabled.
    if (cursor == null) {
        new AlertDialog.Builder(DictionaryManagerActivity.this).setTitle(getString(R.string.error))
                .setMessage(getString(R.string.downloadFailed, R.string.downloadManagerQueryFailed))
                .setNeutralButton("Close", null).show();
        return;
    }

    while (cursor.moveToNext()) {
        if (downloadUrl.equals(cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_URI))))
            break;
        if (destFile.equals(cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_TITLE))))
            break;
    }
    if (!cursor.isAfterLast()) {
        downloadManager.remove(cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_ID)));
        downloadButton.setText(getString(R.string.downloadButton, bytes / 1024.0 / 1024.0));
        cursor.close();
        return;
    }
    cursor.close();
    Request request = new Request(Uri.parse(downloadUrl));

    Log.d(LOG, "Downloading to: " + destFile);
    request.setTitle(destFile);

    File destFilePath = new File(application.getDictDir(), destFile);
    destFilePath.delete();
    try {
        request.setDestinationUri(Uri.fromFile(destFilePath));
    } catch (Exception e) {
    }

    try {
        downloadManager.enqueue(request);
    } catch (SecurityException e) {
        request = new Request(Uri.parse(downloadUrl));
        request.setTitle(destFile);
        downloadManager.enqueue(request);
    }
    Log.w(LOG, "Download started: " + destFile);
    downloadButton.setText("X");
}