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.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. j  a  v a 2  s .  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();
    }
}

From source file:com.ywesee.amiko.MainActivity.java

/**
 * Downloads and updates the SQLite database and the error report file
 *//*from   www .  ja v  a2s  .c o m*/
public void downloadUpdates() {
    // Signal that update is in progress
    mUpdateInProgress = true;
    mDownloadedFileCount = 0;

    // First check network connection
    ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();

    // Fetch data...
    if (networkInfo != null && networkInfo.isConnected()) {
        // File URIs
        Uri databaseUri = Uri.parse("http://pillbox.oddb.org/" + Constants.appZippedDatabase());
        Uri reportUri = Uri.parse("http://pillbox.oddb.org/" + Constants.appReportFile());
        Uri interactionsUri = Uri.parse("http://pillbox.oddb.org/" + Constants.appZippedInteractionsFile());
        // NOTE: the default download destination is a shared volume where the system might delete your file if
        // it needs to reclaim space for system use
        DownloadManager.Request requestDatabase = new DownloadManager.Request(databaseUri);
        DownloadManager.Request requestReport = new DownloadManager.Request(reportUri);
        DownloadManager.Request requestInteractions = new DownloadManager.Request(interactionsUri);
        // Allow download only over WIFI and Mobile network
        requestDatabase.setAllowedNetworkTypes(Request.NETWORK_WIFI | Request.NETWORK_MOBILE);
        requestReport.setAllowedNetworkTypes(Request.NETWORK_WIFI | Request.NETWORK_MOBILE);
        requestInteractions.setAllowedNetworkTypes(Request.NETWORK_WIFI | Request.NETWORK_MOBILE);
        // Download visible and shows in notifications while in progress and after completion
        requestDatabase.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        requestReport.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        requestInteractions
                .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        // Set the title of this download, to be displayed in notifications (if enabled).
        requestDatabase.setTitle("AmiKo SQLite database update");
        requestReport.setTitle("AmiKo report update");
        requestInteractions.setTitle("AmiKo drug interactions update");
        // Set a description of this download, to be displayed in notifications (if enabled)
        requestDatabase.setDescription("Updating the AmiKo database.");
        requestReport.setDescription("Updating the AmiKo error report.");
        requestInteractions.setDescription("Updating the AmiKo drug interactions.");
        // Set local destination to standard directory (place where files downloaded by the user are placed)
        /*
        String main_expansion_file_path = Utilities.expansionFileDir(getPackageName());
        requestDatabase.setDestinationInExternalPublicDir(main_expansion_file_path, Constants.appZippedDatabase());
        */
        requestDatabase.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,
                Constants.appZippedDatabase());
        requestReport.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,
                Constants.appReportFile());
        requestInteractions.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,
                Constants.appZippedInteractionsFile());
        // Check if file exist on non persistent store. If yes, delete it.
        if (Utilities.isExternalStorageReadable() && Utilities.isExternalStorageWritable()) {
            Utilities.deleteFile(Constants.appZippedDatabase(),
                    Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS));
            Utilities.deleteFile(Constants.appReportFile(),
                    Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS));
            Utilities.deleteFile(Constants.appZippedInteractionsFile(),
                    Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS));
        }
        // The downloadId is unique across the system. It is used to make future calls related to this download.
        mDatabaseId = mDownloadManager.enqueue(requestDatabase);
        mReportId = mDownloadManager.enqueue(requestReport);
        mInteractionsId = mDownloadManager.enqueue(requestInteractions);

        mProgressBar = new ProgressDialog(MainActivity.this);
        mProgressBar.setMessage("Downloading SQLite database...");
        mProgressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        mProgressBar.setProgress(0);
        mProgressBar.setMax(100);
        mProgressBar.setCancelable(false);
        mProgressBar.show();

        new Thread(new Runnable() {
            @Override
            public void run() {
                boolean downloading = true;
                while (downloading) {
                    DownloadManager.Query q = new DownloadManager.Query();
                    q.setFilterById(mDatabaseId);
                    Cursor cursor = mDownloadManager.query(q);
                    cursor.moveToFirst();
                    int bytes_downloaded = cursor
                            .getInt(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
                    int bytes_total = cursor
                            .getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
                    if (cursor.getInt(cursor.getColumnIndex(
                            DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL) {
                        downloading = false;
                        if (mProgressBar.isShowing())
                            mProgressBar.dismiss();
                    }
                    final int dl_progress = (int) ((bytes_downloaded * 100l) / bytes_total);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mProgressBar.setProgress((int) dl_progress);
                        }
                    });
                    cursor.close();
                }
            }
        }).start();
    } else {
        // Display error report
    }
}