Example usage for android.app DownloadManager.Request DownloadManager.Request

List of usage examples for android.app DownloadManager.Request DownloadManager.Request

Introduction

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

Prototype

Request(String uriString) 

Source Link

Usage

From source file:fr.kwiatkowski.apktrack.service.WebService.java

/**
 * Downloads the APK for a given app if a download URL is available.
 * APK downloads take place through the Download Service. Files are stored on a dedicated
 * folder on the external storage of the device.
 * @param app The app whose APK is to be downloaded.
 *///  w ww  .j  a  v a 2 s.  c  o  m
private void _download_apk(InstalledApp app, String request_source) {
    if (app.get_download_url() == null) {
        return;
    }

    Uri uri = Uri
            .parse(String.format(app.get_download_url(), app.get_display_name(), app.get_latest_version()));

    DownloadManager dm = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
    DownloadManager.Request request = new DownloadManager.Request(uri);

    // Indicate whether the download may take place over mobile data.
    // Download over data is okay on user click, but respect the preference for background checks.
    if (ScheduledCheckService.SERVICE_SOURCE.equals(request_source) && PreferenceManager
            .getDefaultSharedPreferences(this).getBoolean(SettingsFragment.KEY_PREF_WIFI_ONLY, true)) {
        request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
    } else {
        request.setAllowedNetworkTypes(
                DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);
    }

    // Don't download APKs when roaming.
    request.setAllowedOverRoaming(false).setTitle(getString(getApplicationInfo().labelRes))
            .setDescription(app.get_display_name() + " " + app.get_latest_version() + ".apk")
            .setVisibleInDownloadsUi(false).setDestinationInExternalFilesDir(this,
                    Environment.DIRECTORY_DOWNLOADS,
                    app.get_package_name() + "-" + app.get_latest_version() + ".apk");
    long id = dm.enqueue(request);
    app.set_download_id(id);
    app.save();
    EventBusHelper.post_sticky(ModelModifiedMessage.event_type.APP_UPDATED, app.get_package_name());
}