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:org.wso2.iot.agent.api.ApplicationManager.java

/**
 * Initiate downloading via DownloadManager API.
 *
 * @param url     - File URL./*from   www .  ja  v  a 2 s .  co  m*/
 * @param appName - Name of the application to be downloaded.
 */
private void downloadViaDownloadManager(String url, String appName) {
    final DownloadManager downloadManager = (DownloadManager) context
            .getSystemService(Context.DOWNLOAD_SERVICE);
    Uri downloadUri = Uri.parse(url);
    DownloadManager.Request request = new DownloadManager.Request(downloadUri);

    // Restrict the types of networks over which this download may
    // proceed.
    request.setAllowedNetworkTypes(
            DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
    // Set whether this download may proceed over a roaming connection.
    request.setAllowedOverRoaming(true);
    // Set the title of this download, to be displayed in notifications
    // (if enabled).
    request.setTitle(resources.getString(R.string.downloader_message_title));
    request.setVisibleInDownloadsUi(false);
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
    // Set the local destination for the downloaded file to a path
    // within the application's external files directory
    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, appName);
    // Enqueue a new download and same the referenceId
    downloadReference = downloadManager.enqueue(request);
    new Thread(new Runnable() {
        @Override
        public void run() {
            boolean downloading = true;
            int progress = 0;
            while (downloading) {
                downloadOngoing = true;
                DownloadManager.Query query = new DownloadManager.Query();
                query.setFilterById(downloadReference);
                Cursor cursor = downloadManager.query(query);
                cursor.moveToFirst();
                int bytesDownloaded = cursor
                        .getInt(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
                int bytesTotal = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
                if (cursor.getInt(cursor
                        .getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL) {
                    downloading = false;
                }
                if (cursor.getInt(cursor
                        .getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_FAILED) {
                    downloading = false;
                    Preference.putString(context, context.getResources().getString(R.string.app_install_status),
                            context.getResources().getString(R.string.app_status_value_download_failed));
                    Preference.putString(context,
                            context.getResources().getString(R.string.app_install_failed_message),
                            "App download failed due to a connection issue.");
                }
                int downloadProgress = 0;
                if (bytesTotal > 0) {
                    downloadProgress = (int) ((bytesDownloaded * 100l) / bytesTotal);
                }
                if (downloadProgress != DOWNLOAD_PERCENTAGE_TOTAL) {
                    progress += DOWNLOADER_INCREMENT;
                } else {
                    progress = DOWNLOAD_PERCENTAGE_TOTAL;
                    Preference.putString(context, context.getResources().getString(R.string.app_install_status),
                            context.getResources().getString(R.string.app_status_value_download_completed));
                }

                Preference.putString(context, resources.getString(R.string.app_download_progress),
                        String.valueOf(progress));
                cursor.close();
            }
            downloadOngoing = false;
        }
    }).start();
}

From source file:com.mobicage.rogerthat.plugins.messaging.BrandingMgr.java

@SuppressLint("InlinedApi")
private File getDownloadedFile(final Long downloadId) throws DownloadNotCompletedException {
    final DownloadManager dwnlMgr = getDownloadManager();
    final Cursor cursor = dwnlMgr.query(new Query().setFilterById(downloadId));
    try {/*from   ww  w.  ja  va 2s.c  o  m*/
        if (!cursor.moveToFirst()) {
            L.w("Download with id " + downloadId + " not found!");
            return null;
        }

        final int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
        switch (status) {
        case DownloadManager.STATUS_SUCCESSFUL:
            final String filePath = cursor
                    .getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME));
            return new File(filePath);
        case DownloadManager.STATUS_FAILED:
            return null;
        default: // Not completed
            L.w("Unexpected DownloadManager.STATUS: " + status);
            throw new BrandingMgr.DownloadNotCompletedException();
        }
    } finally {
        cursor.close();
    }
}