List of usage examples for android.app DownloadManager query
public Cursor query(Query query)
From source file:org.wso2.iot.agent.api.ApplicationManager.java
/** * Initiate downloading via DownloadManager API. * * @param url - File URL.//from ww w. j ava 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(); }