Search in sources :

Example 61 with Request

use of android.app.DownloadManager.Request in project XposedInstaller by rovo89.

the class DownloadsUtil method add.

private static DownloadInfo add(Builder b) {
    Context context = b.mContext;
    removeAllForUrl(context, b.mUrl);
    if (!b.mDialog) {
        synchronized (mCallbacks) {
            mCallbacks.put(b.mUrl, b.mCallback);
        }
    }
    Request request = new Request(Uri.parse(b.mUrl));
    request.setTitle(b.mTitle);
    request.setMimeType(b.mMimeType.toString());
    if (b.mDestination != null) {
        b.mDestination.getParentFile().mkdirs();
        removeAllForLocalFile(context, b.mDestination);
        request.setDestinationUri(Uri.fromFile(b.mDestination));
    }
    request.setNotificationVisibility(Request.VISIBILITY_VISIBLE);
    DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
    long id = dm.enqueue(request);
    if (b.mDialog) {
        showDownloadDialog(b, id);
    }
    return getById(context, id);
}
Also used : Context(android.content.Context) Request(android.app.DownloadManager.Request) DownloadManager(android.app.DownloadManager)

Example 62 with Request

use of android.app.DownloadManager.Request in project robolectric by robolectric.

the class ShadowDownloadManagerTest method query_shouldReturnAll.

@Test
public void query_shouldReturnAll() {
    ShadowDownloadManager manager = new ShadowDownloadManager();
    manager.enqueue(request.setDestinationUri(destination));
    Uri secondUri = Uri.parse("http://example.com/foo2.mp4");
    Uri secondDestination = Uri.parse("file:///storage/foo2.mp4");
    Request secondRequest = new Request(secondUri);
    manager.enqueue(secondRequest.setDestinationUri(secondDestination));
    Cursor cursor = manager.query(new DownloadManager.Query());
    cursor.moveToNext();
    assertThat(cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_URI))).isEqualTo(uri.toString());
    assertThat(cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI))).isEqualTo(destination.toString());
    cursor.moveToNext();
    assertThat(cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_URI))).isEqualTo(secondUri.toString());
    assertThat(cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI))).isEqualTo(secondDestination.toString());
}
Also used : ShadowRequest(org.robolectric.shadows.ShadowDownloadManager.ShadowRequest) Request(android.app.DownloadManager.Request) Cursor(android.database.Cursor) Uri(android.net.Uri) DownloadManager(android.app.DownloadManager) Test(org.junit.Test)

Example 63 with Request

use of android.app.DownloadManager.Request in project Xposed-Tinted-Status-Bar by MohammadAG.

the class DownloadsUtil method add.

public static DownloadInfo add(Context context, String title, String url, DownloadFinishedCallback callback) {
    removeAllForUrl(context, url);
    synchronized (mCallbacks) {
        mCallbacks.put(url, callback);
    }
    DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
    Request request = new Request(Uri.parse(url));
    request.setTitle(title);
    request.setMimeType(MIME_TYPE_APK);
    request.setNotificationVisibility(Request.VISIBILITY_VISIBLE);
    long id = dm.enqueue(request);
    return getById(context, id);
}
Also used : Request(android.app.DownloadManager.Request) DownloadManager(android.app.DownloadManager)

Example 64 with Request

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

the class DownloadManagerStressTest method testMultipleDownloads.

/**
     * Attempts to download several files simultaneously
     */
@LargeTest
public void testMultipleDownloads() throws Exception {
    // need to be sure all current downloads have stopped first
    removeAllCurrentDownloads();
    int NUM_FILES = 10;
    // 10 kb
    int MAX_FILE_SIZE = 10 * 1024;
    Random r = new LoggingRng();
    for (int i = 0; i < NUM_FILES; ++i) {
        int size = r.nextInt(MAX_FILE_SIZE);
        byte[] blobData = generateData(size, DataType.TEXT);
        Uri uri = getServerUri(DEFAULT_FILENAME + i);
        Request request = new Request(uri);
        request.setTitle(String.format("%s--%d", DEFAULT_FILENAME + i, i));
        // Prepare the mock server with a standard response
        enqueueResponse(buildResponse(HTTP_OK, blobData));
        long requestID = mDownloadManager.enqueue(request);
    }
    waitForDownloadsOrTimeout(WAIT_FOR_DOWNLOAD_POLL_TIME, MAX_WAIT_FOR_DOWNLOAD_TIME);
    Cursor cursor = mDownloadManager.query(new Query());
    try {
        assertEquals(NUM_FILES, cursor.getCount());
        if (cursor.moveToFirst()) {
            do {
                int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
                String filename = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_URI));
                String errorString = String.format("File %s failed to download successfully. Status code: %d", filename, status);
                assertEquals(errorString, DownloadManager.STATUS_SUCCESSFUL, status);
            } while (cursor.moveToNext());
        }
        assertEquals(NUM_FILES, mReceiver.numDownloadsCompleted());
    } finally {
        cursor.close();
    }
}
Also used : Random(java.util.Random) Query(android.app.DownloadManager.Query) Request(android.app.DownloadManager.Request) Cursor(android.database.Cursor) Uri(android.net.Uri) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Example 65 with Request

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

the class DownloadManagerFunctionalTest method doErrorTest.

/**
     * Verifies a particular error code was received from a download
     *
     * @param uri The uri to enqueue to the DownloadManager
     * @param error The error code expected
     * @throws Exception if the test fails
     */
public void doErrorTest(Uri uri, int error) throws Exception {
    Request request = new Request(uri);
    request.setTitle(DEFAULT_FILENAME);
    long dlRequest = mDownloadManager.enqueue(request);
    waitForDownloadOrTimeout(dlRequest);
    Cursor cursor = getCursor(dlRequest);
    try {
        verifyInt(cursor, DownloadManager.COLUMN_REASON, error);
    } finally {
        cursor.close();
    }
}
Also used : Request(android.app.DownloadManager.Request) Cursor(android.database.Cursor)

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