Search in sources :

Example 91 with ParcelFileDescriptor

use of android.os.ParcelFileDescriptor in project android_frameworks_base by ParanoidAndroid.

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_5MB_FILENAME;
    long filesize = DOWNLOAD_5MB_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...
        waitForFileToGrow(downloadedFile);
        // download disable
        setWiFiStateOn(false);
        // download disable
        Log.i(LOG_TAG, "Turning on airplane mode...");
        setAirplaneModeOn(true);
        // wait 30 secs
        Thread.sleep(30 * 1000);
        // download disable
        setWiFiStateOn(true);
        // wait 30 secs
        Thread.sleep(30 * 1000);
        // download enable
        Log.i(LOG_TAG, "Turning off airplane mode...");
        setAirplaneModeOn(false);
        // wait 5 seconds
        Thread.sleep(5 * 1000);
        // download disable
        Log.i(LOG_TAG, "Turning off WiFi...");
        setWiFiStateOn(false);
        // wait 30 secs
        Thread.sleep(30 * 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 3 minutes for download to complete...");
        waitForDownloadsOrTimeout(dlRequest, 3 * 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 92 with ParcelFileDescriptor

use of android.os.ParcelFileDescriptor in project android_frameworks_base by ParanoidAndroid.

the class DownloadManagerTestApp method verifyFileDownloadSucceeded.

/**
     * Waits for a previously-initiated download and verifies it has completed successfully.
     *
     * @throws Exception if unsuccessful
     */
public void verifyFileDownloadSucceeded() throws Exception {
    String filename = DOWNLOAD_5MB_FILENAME;
    long filesize = DOWNLOAD_5MB_FILESIZE;
    long dlRequest = -1;
    boolean rebootMarkerValid = false;
    DataInputStream dataInputFile = null;
    setWiFiStateOn(true);
    setAirplaneModeOn(false);
    try {
        FileInputStream inFile = mContext.openFileInput(DOWNLOAD_STARTED_FLAG);
        dataInputFile = new DataInputStream(inFile);
        dlRequest = dataInputFile.readLong();
    } catch (Exception e) {
        // The file was't valid so we just leave the flag false
        Log.i(LOG_TAG, "Unable to determine initial download id.");
        throw e;
    } finally {
        if (dataInputFile != null) {
            dataInputFile.close();
        }
        mContext.deleteFile(DOWNLOAD_STARTED_FLAG);
    }
    assertTrue(dlRequest != -1);
    Cursor cursor = getCursor(dlRequest);
    ParcelFileDescriptor pfd = null;
    try {
        assertTrue("Unable to query last initiated download!", cursor.moveToFirst());
        int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
        int status = cursor.getInt(columnIndex);
        int currentWaitTime = 0;
        // Wait until the download finishes; don't wait for a notification b/c
        // the download may well have been completed before the last reboot.
        waitForDownloadOrTimeout_skipNotification(dlRequest);
        Log.i(LOG_TAG, "Verifying download information...");
        // Verify specific info about the file (size, name, etc)...
        pfd = mDownloadManager.openDownloadedFile(dlRequest);
        verifyFileSize(pfd, filesize);
    } catch (Exception e) {
        Log.i(LOG_TAG, "error: " + e.toString());
        throw e;
    } finally {
        // Clean up...
        cursor.close();
        mDownloadManager.remove(dlRequest);
        if (pfd != null) {
            pfd.close();
        }
    }
}
Also used : ParcelFileDescriptor(android.os.ParcelFileDescriptor) DataInputStream(java.io.DataInputStream) Cursor(android.database.Cursor) FileInputStream(java.io.FileInputStream)

Example 93 with ParcelFileDescriptor

use of android.os.ParcelFileDescriptor in project android_frameworks_base by ParanoidAndroid.

the class DownloadManagerTestApp method runDownloadMultipleWiFiEnableDisable.

/**
     * Tests that downloads resume when switching on/off WiFi at various intervals.
     *
     * Note: Device has no mobile access when running this test.
     *
     * @throws Exception if unsuccessful
     */
public void runDownloadMultipleWiFiEnableDisable() throws Exception {
    String filename = DOWNLOAD_5MB_FILENAME;
    long filesize = DOWNLOAD_5MB_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_WIFI);
        dlRequest = mDownloadManager.enqueue(request);
        waitForDownloadToStart(dlRequest);
        // are we making any progress?
        waitForFileToGrow(downloadedFile);
        // download disable
        Log.i(LOG_TAG, "Turning off WiFi...");
        setWiFiStateOn(false);
        // wait 40 seconds
        Thread.sleep(40 * 1000);
        // enable download...
        Log.i(LOG_TAG, "Turning on WiFi again...");
        setWiFiStateOn(true);
        waitForFileToGrow(downloadedFile);
        // download disable
        Log.i(LOG_TAG, "Turning off WiFi...");
        setWiFiStateOn(false);
        // wait 20 seconds
        Thread.sleep(20 * 1000);
        // enable download...
        Log.i(LOG_TAG, "Turning on WiFi again...");
        setWiFiStateOn(true);
        Log.i(LOG_TAG, "Waiting up to 3 minutes for download to complete...");
        waitForDownloadsOrTimeout(dlRequest, 3 * 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 94 with ParcelFileDescriptor

use of android.os.ParcelFileDescriptor in project android_frameworks_base by ParanoidAndroid.

the class SoundPool method load.

/**
     * Load the sound from the specified path.
     *
     * @param path the path to the audio file
     * @param priority the priority of the sound. Currently has no effect. Use
     *                 a value of 1 for future compatibility.
     * @return a sound ID. This value can be used to play or unload the sound.
     */
public int load(String path, int priority) {
    // pass network streams to player
    if (path.startsWith("http:"))
        return _load(path, priority);
    // try local path
    int id = 0;
    try {
        File f = new File(path);
        ParcelFileDescriptor fd = ParcelFileDescriptor.open(f, ParcelFileDescriptor.MODE_READ_ONLY);
        if (fd != null) {
            id = _load(fd.getFileDescriptor(), 0, f.length(), priority);
            fd.close();
        }
    } catch (java.io.IOException e) {
        Log.e(TAG, "error loading " + path);
    }
    return id;
}
Also used : ParcelFileDescriptor(android.os.ParcelFileDescriptor) IOException(java.io.IOException) File(java.io.File)

Example 95 with ParcelFileDescriptor

use of android.os.ParcelFileDescriptor in project cw-omnibus by commonsguy.

the class DemoDocumentsProvider method openDocument.

@Override
public ParcelFileDescriptor openDocument(String documentId, String mode, CancellationSignal signal) throws FileNotFoundException {
    ParcelFileDescriptor[] pipe = null;
    try {
        pipe = ParcelFileDescriptor.createPipe();
        AssetManager assets = getContext().getResources().getAssets();
        new TransferThread(assets.open(documentId), new ParcelFileDescriptor.AutoCloseOutputStream(pipe[1])).start();
    } catch (IOException e) {
        Log.e(getClass().getSimpleName(), "Exception opening pipe", e);
        throw new FileNotFoundException("Could not open pipe for: " + documentId);
    }
    return (pipe[0]);
}
Also used : AssetManager(android.content.res.AssetManager) ParcelFileDescriptor(android.os.ParcelFileDescriptor) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException)

Aggregations

ParcelFileDescriptor (android.os.ParcelFileDescriptor)526 IOException (java.io.IOException)199 FileNotFoundException (java.io.FileNotFoundException)136 RemoteException (android.os.RemoteException)127 File (java.io.File)127 FileDescriptor (java.io.FileDescriptor)58 FileOutputStream (java.io.FileOutputStream)58 AssetFileDescriptor (android.content.res.AssetFileDescriptor)44 FileInputStream (java.io.FileInputStream)36 Parcel (android.os.Parcel)35 Uri (android.net.Uri)33 Intent (android.content.Intent)30 Bundle (android.os.Bundle)29 Cursor (android.database.Cursor)27 StorageManager (android.os.storage.StorageManager)25 Request (android.app.DownloadManager.Request)24 Bitmap (android.graphics.Bitmap)22 InputStream (java.io.InputStream)18 ProfilerInfo (android.app.ProfilerInfo)17 Binder (android.os.Binder)17