Example usage for android.app DownloadManager COLUMN_STATUS

List of usage examples for android.app DownloadManager COLUMN_STATUS

Introduction

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

Prototype

String COLUMN_STATUS

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

Click Source Link

Document

Current status of the download, as one of the STATUS_* constants.

Usage

From source file:com.github.notizklotz.derbunddownloader.issuesgrid.DownloadedIssuesActivity.java

@AfterViews
void setupIssuesGrid() {
    gridView.setEmptyView(emptyGridView);
    gridView.setOnItemClickListener(new IssuesGridOnItemClickListener());

    final SimpleCursorAdapter issueListAdapter = new SimpleCursorAdapter(this, R.layout.include_issue, null,
            new String[] { DownloadManager.COLUMN_DESCRIPTION, DownloadManager.COLUMN_STATUS },
            new int[] { R.id.dateTextView, R.id.stateTextView }, 0) {

        @Override/*  w  ww .  j a  v a  2  s. c om*/
        public View getView(final int position, View convertView, ViewGroup parent) {
            View view = super.getView(position, convertView, parent);
            View deleteButton = view.findViewById(R.id.issueDeleteButton);
            deleteButton.setOnClickListener(new IssueDeleteButtonOnClickListener(position));

            // Load the thumbnail image
            ImageView image = (ImageView) view.findViewById(R.id.issueImageView);
            Uri uri = Uri.parse(getCursor().getString(getCursor().getColumnIndex(DownloadManager.COLUMN_URI)));
            Picasso.with(image.getContext()).load(IssueDownloadService_.getThumbnailUriForPDFUri(uri))
                    .placeholder(R.drawable.issue_placeholder).into(image);
            return view;
        }
    };
    issueListAdapter.setViewBinder(new IssuesGridViewBinder(this));
    gridView.setAdapter(issueListAdapter);

    getLoaderManager().initLoader(1, null, new IssuesGridLoaderCallbacks(this, issueListAdapter));
}

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 .j a v  a2s  .  co 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:org.openbmap.activities.DialogPreferenceCatalogs.java

/**
 * Initialises download manager for GINGERBREAD and newer
 *///from  w  w w . j a  v a  2  s.co  m
@SuppressLint("NewApi")
private void initDownloadManager() {
    mDownloadManager = (DownloadManager) getContext().getSystemService(Context.DOWNLOAD_SERVICE);

    mReceiver = new BroadcastReceiver() {
        @SuppressLint("NewApi")
        @Override
        public void onReceive(final Context context, final Intent intent) {
            final String action = intent.getAction();
            if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
                final long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
                final DownloadManager.Query query = new DownloadManager.Query();
                query.setFilterById(downloadId);
                final Cursor c = mDownloadManager.query(query);
                if (c.moveToFirst()) {
                    final int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
                    if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {
                        // we're not checking download id here, that is done in handleDownloads
                        final String uriString = c
                                .getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
                        handleDownloads(uriString);
                    } else {
                        final int reason = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_REASON));
                        Log.e(TAG, "Download failed: " + reason);
                    }
                }
            }
        }
    };

    getContext().registerReceiver(mReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}

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

@Override
protected String doInBackground(final String... strings) {
    try {/*from ww w.  j  a v  a 2 s. com*/
        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:org.openbmap.activities.DialogPreferenceMaps.java

/**
 * Initialises download manager for GINGERBREAD and newer
 *//*from  www . ja va 2  s  . c  o m*/
@SuppressLint("NewApi")
private void initDownloadManager() {

    mDownloadManager = (DownloadManager) getContext().getSystemService(Context.DOWNLOAD_SERVICE);

    mReceiver = new BroadcastReceiver() {
        @SuppressLint("NewApi")
        @Override
        public void onReceive(final Context context, final Intent intent) {
            final String action = intent.getAction();
            if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
                final long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
                final DownloadManager.Query query = new DownloadManager.Query();
                query.setFilterById(downloadId);
                final Cursor c = mDownloadManager.query(query);
                if (c.moveToFirst()) {
                    final int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
                    if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {
                        // we're not checking download id here, that is done in handleDownloads
                        final String uriString = c
                                .getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
                        handleDownloads(uriString);
                    } else {
                        final int reason = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_REASON));
                        Log.e(TAG, "Download failed: " + reason);
                    }
                }
            }
        }
    };

    getContext().registerReceiver(mReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}

From source file:org.amahi.anywhere.util.Downloader.java

private void finishDownloading() {
    DownloadManager.Query downloadQuery = new DownloadManager.Query().setFilterById(downloadId);

    Cursor downloadInformation = getDownloadManager(context).query(downloadQuery);

    downloadInformation.moveToFirst();/*from  w  w  w  . j  a  v a 2  s. c  o  m*/

    int downloadStatus = downloadInformation
            .getInt(downloadInformation.getColumnIndex(DownloadManager.COLUMN_STATUS));

    if (downloadStatus == DownloadManager.STATUS_SUCCESSFUL) {
        String downloadUri = downloadInformation
                .getString(downloadInformation.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));

        if (downloadUri.substring(0, 7).matches("file://")) {
            downloadUri = downloadUri.substring(7);
        }

        try {
            URI uri = new URI(downloadUri);
            downloadUri = uri.getPath();
        } catch (URISyntaxException e) {
            Log.e("Downloader", "Invalid Uri: " + downloadUri);
        }

        File file = new File(downloadUri);
        Uri contentUri = FileProvider.getUriForFile(context, "org.amahi.anywhere.fileprovider", file);
        BusProvider.getBus().post(new FileDownloadedEvent(contentUri));
    } else {
        BusProvider.getBus().post(new FileDownloadFailedEvent());
    }

    downloadInformation.close();

}

From source file:de.escoand.readdaily.DownloadHandler.java

@Override
public void onReceive(final Context context, final Intent intent) {
    final DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
    final Database db = Database.getInstance(context);
    final Cursor downloads = db.getDownloads();

    LogHandler.log(Log.WARN, "receive starting");

    while (downloads.moveToNext()) {
        final long id = downloads.getLong(downloads.getColumnIndex(Database.COLUMN_ID));
        final String name = downloads.getString(downloads.getColumnIndex(Database.COLUMN_SUBSCRIPTION));
        final String mime = downloads.getString(downloads.getColumnIndex(Database.COLUMN_TYPE));
        final Cursor download = manager.query(new DownloadManager.Query().setFilterById(id));

        // download exists
        if (!download.moveToFirst())
            continue;

        // download finished
        if (download.getInt(
                download.getColumnIndex(DownloadManager.COLUMN_STATUS)) != DownloadManager.STATUS_SUCCESSFUL)
            continue;

        // import file in background
        new Thread(new Runnable() {
            @Override//  ww w . ja  v a2s  . co m
            public void run() {
                try {
                    LogHandler.log(Log.WARN, "import starting of " + name);

                    final FileInputStream stream = new ParcelFileDescriptor.AutoCloseInputStream(
                            manager.openDownloadedFile(id));
                    final String mimeServer = manager.getMimeTypeForDownloadedFile(id);

                    LogHandler.log(Log.INFO, "id: " + String.valueOf(id));
                    LogHandler.log(Log.INFO, "manager: " + manager.toString());
                    LogHandler.log(Log.INFO, "stream: " + stream.toString());
                    LogHandler.log(Log.INFO, "mime: " + mime);
                    LogHandler.log(Log.INFO, "mimeServer: " + mimeServer);

                    switch (mime != null ? mime : (mimeServer != null ? mimeServer : "")) {

                    // register feedback
                    case "application/json":
                        final byte[] buf = new byte[256];
                        final int len = stream.read(buf);
                        LogHandler.log(Log.WARN, "register feedback: " + new String(buf, 0, len));
                        break;

                    // csv data
                    case "text/plain":
                        db.importCSV(name, stream);
                        break;

                    // xml data
                    case "application/xml":
                    case "text/xml":
                        db.importXML(name, stream);
                        break;

                    // zipped data
                    case "application/zip":
                        db.importZIP(name, stream);
                        break;

                    // do nothing
                    default:
                        LogHandler.log(new IntentFilter.MalformedMimeTypeException());
                        break;
                    }

                    stream.close();
                    LogHandler.log(Log.WARN, "import finished (" + name + ")");
                }

                // file error
                catch (FileNotFoundException e) {
                    LogHandler.logAndShow(e, context, R.string.message_download_open);
                }

                // stream error
                catch (IOException e) {
                    LogHandler.logAndShow(e, context, R.string.message_download_read);
                }

                // xml error
                catch (XmlPullParserException e) {
                    LogHandler.logAndShow(e, context, R.string.message_download_xml);
                }

                // clean
                finally {
                    manager.remove(id);
                    db.removeDownload(id);
                    LogHandler.log(Log.WARN, "clean finished");
                }
            }
        }).start();
    }

    downloads.close();
    LogHandler.log(Log.WARN, "receiving done");
}

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

private static long[] getDownloadProgress(long id) {
    DownloadManager.Query q = new DownloadManager.Query();
    q.setFilterById(id);/* www  .  j  a v  a  2 s .c om*/

    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:com.zsxj.pda.ui.client.LoginActivity.java

private boolean downloading() {
    DownloadManager.Query query = new DownloadManager.Query();
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    long downloadId = prefs.getLong(PrefKeys.DOWNLOAD_ID, 0);
    query.setFilterById(downloadId);/*from   w  w  w  .  j  av  a 2s .  c  om*/
    DownloadManager dm = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
    Cursor c = dm.query(query);
    if (c.moveToNext()) {
        int statusIdx = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
        int status = c.getInt(statusIdx);
        if (DownloadManager.STATUS_RUNNING == status) {
            return true;
        }
    }
    return false;
}

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 {/*from  w w  w  .  ja v a  2  s  .c om*/
            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);
}