Example usage for android.app DownloadManager STATUS_SUCCESSFUL

List of usage examples for android.app DownloadManager STATUS_SUCCESSFUL

Introduction

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

Prototype

int STATUS_SUCCESSFUL

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

Click Source Link

Document

Value of #COLUMN_STATUS when the download has successfully completed.

Usage

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

private void notifyReceivers(String lang) {
    //notify language activity
    Intent resultIntent = new Intent(ACTION_INSTALL_COMPLETED);
    resultIntent.putExtra(EXTRA_OCR_LANGUAGE, lang);
    resultIntent.putExtra(EXTRA_STATUS, DownloadManager.STATUS_SUCCESSFUL);
    sendBroadcast(resultIntent);/* w w  w. ja va  2s.  c  o m*/
}

From source file:org.openbmap.activities.DialogPreferenceCatalogs.java

/**
 * Initialises download manager for GINGERBREAD and newer
 *///from   www .j  a v a  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:com.giovanniterlingen.windesheim.controllers.DownloadController.java

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

/**
 * Initialises download manager for GINGERBREAD and newer
 *///from  ww  w .  j ava  2  s .c om
@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.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 ww  .  ja va2s  .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.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 ww  . jav a2  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//from www. j a v  a 2s  . 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: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 ww  w  .  j a  v  a 2s.  co 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.example.linhdq.test.main_menu.language.OCRLanguageActivity.java

private void updateLanguage(final OcrLanguage language, int status) {
    if (status == DownloadManager.STATUS_SUCCESSFUL) {
        final InstallStatus installStatus = OcrLanguageDataStore.isLanguageInstalled(language.getValue(),
                OCRLanguageActivity.this);
        language.setInstallStatus(installStatus);
        if (installStatus.isInstalled()) {
            language.setDownloading(false);
            mAdapter.notifyDataSetChanged();
        }/*from  w w  w.j  a  v a2  s  .  c  o  m*/
    } else {
        language.setDownloading(false);
        mAdapter.notifyDataSetChanged();
        runOnUiThread(new Runnable() {

            @Override
            public void run() {
                String msg = getString(R.string.download_failed);
                msg = String.format(msg, language.getDisplayText());
                Toast.makeText(OCRLanguageActivity.this, msg, Toast.LENGTH_LONG).show();
            }
        });
    }
}

From source file:com.renard.ocr.main_menu.language.OCRLanguageActivity.java

private void updateLanguage(final OcrLanguage language, int status) {
    if (status == DownloadManager.STATUS_SUCCESSFUL) {
        final OcrLanguage.InstallStatus installStatus = OcrLanguageDataStore
                .isLanguageInstalled(language.getValue(), OCRLanguageActivity.this);
        language.setInstallStatus(installStatus);
        if (installStatus.isInstalled()) {
            language.setDownloading(false);
            mAdapter.notifyDataSetChanged();
        }/*ww  w . ja  v a2s .c om*/
    } else {
        language.setDownloading(false);
        mAdapter.notifyDataSetChanged();
        runOnUiThread(new Runnable() {

            @Override
            public void run() {
                String msg = getString(R.string.download_failed);
                msg = String.format(msg, language.getDisplayText());
                Toast.makeText(OCRLanguageActivity.this, msg, Toast.LENGTH_LONG).show();
            }
        });
    }
}