Search in sources :

Example 56 with Request

use of android.app.DownloadManager.Request in project android_frameworks_base by AOSPA.

the class DownloadManagerTestApp method runDownloadMultipleSwitching.

/**
     * Tests that downloads resume when switching back and forth from having connectivity to
     * having no connectivity using both WiFi and airplane mode.
     *
     * Note: Device has no mobile access when running this test.
     *
     * @throws Exception if unsuccessful
     */
public void runDownloadMultipleSwitching() throws Exception {
    String filename = DOWNLOAD_FILENAME;
    long filesize = DOWNLOAD_FILESIZE;
    doCommonDownloadSetup();
    String localDownloadDirectory = Environment.getExternalStorageDirectory().getPath();
    File downloadedFile = new File(localDownloadDirectory, filename);
    long dlRequest = -1;
    try {
        downloadedFile.delete();
        // Make sure there are no pending downloads currently going on
        removeAllCurrentDownloads();
        Uri remoteUri = getExternalFileUri(filename);
        Request request = new Request(remoteUri);
        // Local destination of downloaded file
        Uri localUri = Uri.fromFile(downloadedFile);
        Log.i(LOG_TAG, "setting localUri to: " + localUri.getPath());
        request.setDestinationUri(localUri);
        request.setAllowedNetworkTypes(Request.NETWORK_MOBILE | Request.NETWORK_WIFI);
        dlRequest = mDownloadManager.enqueue(request);
        waitForDownloadToStart(dlRequest);
        // make sure we're starting to download some data...
        waitToReceiveData(dlRequest, EXPECTED_PROGRESS);
        // download disable
        setWiFiStateOn(false);
        // download disable
        Log.i(LOG_TAG, "Turning on airplane mode...");
        setAirplaneModeOn(true);
        // wait 5 secs
        Thread.sleep(5 * 1000);
        // download disable
        setWiFiStateOn(true);
        // wait 5 secs
        Thread.sleep(5 * 1000);
        waitToReceiveData(dlRequest, EXPECTED_PROGRESS);
        // download enable
        Log.i(LOG_TAG, "Turning off airplane mode...");
        setAirplaneModeOn(false);
        // wait 5 seconds
        Thread.sleep(5 * 1000);
        waitToReceiveData(dlRequest, EXPECTED_PROGRESS);
        // download disable
        Log.i(LOG_TAG, "Turning off WiFi...");
        setWiFiStateOn(false);
        // wait 5 secs
        Thread.sleep(5 * 1000);
        // finally, turn WiFi back on and finish up the download
        Log.i(LOG_TAG, "Turning on WiFi...");
        setWiFiStateOn(true);
        Log.i(LOG_TAG, "Waiting up to 10 minutes for download to complete...");
        assertTrue("download not finished", waitForDownload(dlRequest, 10 * 60 * 1000));
        ParcelFileDescriptor pfd = mDownloadManager.openDownloadedFile(dlRequest);
        verifyFileSize(pfd, filesize);
    } finally {
        Log.i(LOG_TAG, "Cleaning up files...");
        if (dlRequest != -1) {
            mDownloadManager.remove(dlRequest);
        }
        downloadedFile.delete();
    }
}
Also used : Request(android.app.DownloadManager.Request) ParcelFileDescriptor(android.os.ParcelFileDescriptor) File(java.io.File) Uri(android.net.Uri)

Example 57 with Request

use of android.app.DownloadManager.Request in project SmartMesh_Android by SmartMeshFoundation.

the class AdvancedWebView method handleDownload.

/**
 * Handles a download by loading the file from `fromUrl` and saving it to `toFilename` on the external storage
 * <p/>
 * This requires the two permissions `android.permission.INTERNET` and `android.permission.WRITE_EXTERNAL_STORAGE`
 * <p/>
 * Only supported on API level 9 (Android 2.3) and above
 *
 * @param context    a valid `Context` reference
 * @param fromUrl    the URL of the file to download, e.g. the one from `AdvancedWebView.onDownloadRequested(...)`
 * @param toFilename the name of the destination file where the download should be saved, e.g. `myImage.jpg`
 * @return whether the download has been successfully handled or not
 */
@SuppressLint("NewApi")
public static boolean handleDownload(final Context context, final String fromUrl, final String toFilename) {
    if (Build.VERSION.SDK_INT < 9) {
        throw new RuntimeException("Method requires API level 9 or above");
    }
    final Request request = new Request(Uri.parse(fromUrl));
    if (Build.VERSION.SDK_INT >= 11) {
        request.allowScanningByMediaScanner();
        request.setNotificationVisibility(Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    }
    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, toFilename);
    final DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
    try {
        try {
            dm.enqueue(request);
        } catch (SecurityException e) {
            if (Build.VERSION.SDK_INT >= 11) {
                request.setNotificationVisibility(Request.VISIBILITY_VISIBLE);
            }
            dm.enqueue(request);
        }
        return true;
    }// if the download manager app has been disabled on the device
     catch (IllegalArgumentException e) {
        // show the settings screen where the user can enable the download manager app again
        openAppSettings(context, AdvancedWebView.PACKAGE_NAME_DOWNLOAD_MANAGER);
        return false;
    }
}
Also used : ClientCertRequest(android.webkit.ClientCertRequest) Request(android.app.DownloadManager.Request) WebResourceRequest(android.webkit.WebResourceRequest) PermissionRequest(android.webkit.PermissionRequest) DownloadManager(android.app.DownloadManager) SuppressLint(android.annotation.SuppressLint)

Example 58 with Request

use of android.app.DownloadManager.Request in project Awful.apk by Awful.

the class ThreadDisplayFragment method enqueueDownload.

public void enqueueDownload(Uri link) {
    if (AwfulUtils.isMarshmallow()) {
        int permissionCheck = ContextCompat.checkSelfPermission(this.getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
            downloadLink = link;
            requestPermissions(new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, Constants.AWFUL_PERMISSION_WRITE_EXTERNAL_STORAGE);
            return;
        }
    }
    Request request = new Request(link);
    request.setNotificationVisibility(Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    if (link.getLastPathSegment().equals("attachment.php") && link.getHost().equals("forums.somethingawful.com")) {
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "attachment.png");
        request.addRequestHeader("Cookie", CookieManager.getInstance().getCookie(Constants.COOKIE_DOMAIN));
    } else {
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, link.getLastPathSegment());
    }
    request.allowScanningByMediaScanner();
    DownloadManager dlManager = (DownloadManager) getAwfulActivity().getSystemService(AwfulActivity.DOWNLOAD_SERVICE);
    dlManager.enqueue(request);
}
Also used : IgnoreRequest(com.ferg.awfulapp.task.IgnoreRequest) AwfulRequest(com.ferg.awfulapp.task.AwfulRequest) MarkLastReadRequest(com.ferg.awfulapp.task.MarkLastReadRequest) BookmarkRequest(com.ferg.awfulapp.task.BookmarkRequest) CloseOpenRequest(com.ferg.awfulapp.task.CloseOpenRequest) SinglePostRequest(com.ferg.awfulapp.task.SinglePostRequest) ReportRequest(com.ferg.awfulapp.task.ReportRequest) Request(android.app.DownloadManager.Request) VoteRequest(com.ferg.awfulapp.task.VoteRequest) ProfileRequest(com.ferg.awfulapp.task.ProfileRequest) PostRequest(com.ferg.awfulapp.task.PostRequest) ImageSizeRequest(com.ferg.awfulapp.task.ImageSizeRequest) DownloadManager(android.app.DownloadManager)

Example 59 with Request

use of android.app.DownloadManager.Request in project Fairphone by Kwamecorp.

the class FairphoneUpdater method createDownloadRequest.

private Request createDownloadRequest(String url, String fileName, String downloadTitle) {
    Request request = new Request(Uri.parse(url));
    Environment.getExternalStoragePublicDirectory(Environment.getExternalStorageDirectory() + VersionParserHelper.UPDATER_FOLDER).mkdirs();
    request.setDestinationInExternalPublicDir(VersionParserHelper.UPDATER_FOLDER, fileName);
    request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
    request.setAllowedOverRoaming(false);
    request.setTitle(downloadTitle);
    return request;
}
Also used : Request(android.app.DownloadManager.Request)

Example 60 with Request

use of android.app.DownloadManager.Request in project Fairphone by Kwamecorp.

the class UpdaterService method createDownloadRequest.

private Request createDownloadRequest(String url, String fileName) {
    Request request = new Request(Uri.parse(url));
    Environment.getExternalStoragePublicDirectory(Environment.getExternalStorageDirectory() + VersionParserHelper.UPDATER_FOLDER).mkdirs();
    request.setDestinationInExternalPublicDir(VersionParserHelper.UPDATER_FOLDER, fileName);
    request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
    request.setAllowedOverRoaming(false);
    Resources resources = getApplicationContext().getResources();
    request.setTitle(resources.getString(R.string.downloadUpdateTitle));
    return request;
}
Also used : Request(android.app.DownloadManager.Request) Resources(android.content.res.Resources)

Aggregations

Request (android.app.DownloadManager.Request)122 Uri (android.net.Uri)86 LargeTest (android.test.suitebuilder.annotation.LargeTest)41 File (java.io.File)38 Cursor (android.database.Cursor)32 ParcelFileDescriptor (android.os.ParcelFileDescriptor)24 DownloadManager (android.app.DownloadManager)17 Suppress (android.test.suitebuilder.annotation.Suppress)10 Query (android.app.DownloadManager.Query)6 DataOutputStream (java.io.DataOutputStream)6 FileOutputStream (java.io.FileOutputStream)6 HashSet (java.util.HashSet)6 Random (java.util.Random)6 Resources (android.content.res.Resources)5 UiObject (android.support.test.uiautomator.UiObject)5 MockResponse (com.google.mockwebserver.MockResponse)4 TimeoutException (java.util.concurrent.TimeoutException)4 AlertDialog (android.app.AlertDialog)3 Context (android.content.Context)3 DialogInterface (android.content.DialogInterface)3