Search in sources :

Example 46 with ParcelFileDescriptor

use of android.os.ParcelFileDescriptor in project platform_frameworks_base by android.

the class PackageInstallerSession method openReadInternal.

private ParcelFileDescriptor openReadInternal(String name) throws IOException {
    assertPreparedAndNotSealed("openRead");
    try {
        if (!FileUtils.isValidExtFilename(name)) {
            throw new IllegalArgumentException("Invalid name: " + name);
        }
        final File target = new File(resolveStageDir(), name);
        final FileDescriptor targetFd = Libcore.os.open(target.getAbsolutePath(), O_RDONLY, 0);
        return new ParcelFileDescriptor(targetFd);
    } catch (ErrnoException e) {
        throw e.rethrowAsIOException();
    }
}
Also used : ErrnoException(android.system.ErrnoException) ParcelFileDescriptor(android.os.ParcelFileDescriptor) File(java.io.File) ParcelFileDescriptor(android.os.ParcelFileDescriptor) FileDescriptor(java.io.FileDescriptor)

Example 47 with ParcelFileDescriptor

use of android.os.ParcelFileDescriptor in project platform_frameworks_base by android.

the class PackageInstallerSession method openWriteInternal.

private ParcelFileDescriptor openWriteInternal(String name, long offsetBytes, long lengthBytes) throws IOException {
    // Quick sanity check of state, and allocate a pipe for ourselves. We
    // then do heavy disk allocation outside the lock, but this open pipe
    // will block any attempted install transitions.
    final FileBridge bridge;
    synchronized (mLock) {
        assertPreparedAndNotSealed("openWrite");
        bridge = new FileBridge();
        mBridges.add(bridge);
    }
    try {
        // Use installer provided name for now; we always rename later
        if (!FileUtils.isValidExtFilename(name)) {
            throw new IllegalArgumentException("Invalid name: " + name);
        }
        final File target;
        final long identity = Binder.clearCallingIdentity();
        try {
            target = new File(resolveStageDir(), name);
        } finally {
            Binder.restoreCallingIdentity(identity);
        }
        // TODO: this should delegate to DCS so the system process avoids
        // holding open FDs into containers.
        final FileDescriptor targetFd = Libcore.os.open(target.getAbsolutePath(), O_CREAT | O_WRONLY, 0644);
        Os.chmod(target.getAbsolutePath(), 0644);
        // cache space to grow, if needed.
        if (lengthBytes > 0) {
            final StructStat stat = Libcore.os.fstat(targetFd);
            final long deltaBytes = lengthBytes - stat.st_size;
            // Only need to free up space when writing to internal stage
            if (stageDir != null && deltaBytes > 0) {
                mPm.freeStorage(params.volumeUuid, deltaBytes);
            }
            Libcore.os.posix_fallocate(targetFd, 0, lengthBytes);
        }
        if (offsetBytes > 0) {
            Libcore.os.lseek(targetFd, offsetBytes, OsConstants.SEEK_SET);
        }
        bridge.setTargetFile(targetFd);
        bridge.start();
        return new ParcelFileDescriptor(bridge.getClientSocket());
    } catch (ErrnoException e) {
        throw e.rethrowAsIOException();
    }
}
Also used : StructStat(android.system.StructStat) ErrnoException(android.system.ErrnoException) FileBridge(android.os.FileBridge) ParcelFileDescriptor(android.os.ParcelFileDescriptor) File(java.io.File) ParcelFileDescriptor(android.os.ParcelFileDescriptor) FileDescriptor(java.io.FileDescriptor)

Example 48 with ParcelFileDescriptor

use of android.os.ParcelFileDescriptor in project platform_frameworks_base by android.

the class DevicePolicyManagerService method shareBugreportWithDeviceOwnerIfExists.

private void shareBugreportWithDeviceOwnerIfExists(String bugreportUriString, String bugreportHash) {
    ParcelFileDescriptor pfd = null;
    try {
        if (bugreportUriString == null) {
            throw new FileNotFoundException();
        }
        Uri bugreportUri = Uri.parse(bugreportUriString);
        pfd = mContext.getContentResolver().openFileDescriptor(bugreportUri, "r");
        synchronized (this) {
            Intent intent = new Intent(DeviceAdminReceiver.ACTION_BUGREPORT_SHARE);
            intent.setComponent(mOwners.getDeviceOwnerComponent());
            intent.setDataAndType(bugreportUri, RemoteBugreportUtils.BUGREPORT_MIMETYPE);
            intent.putExtra(DeviceAdminReceiver.EXTRA_BUGREPORT_HASH, bugreportHash);
            mContext.grantUriPermission(mOwners.getDeviceOwnerComponent().getPackageName(), bugreportUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
            mContext.sendBroadcastAsUser(intent, UserHandle.of(mOwners.getDeviceOwnerUserId()));
        }
    } catch (FileNotFoundException e) {
        Bundle extras = new Bundle();
        extras.putInt(DeviceAdminReceiver.EXTRA_BUGREPORT_FAILURE_REASON, DeviceAdminReceiver.BUGREPORT_FAILURE_FILE_NO_LONGER_AVAILABLE);
        sendDeviceOwnerCommand(DeviceAdminReceiver.ACTION_BUGREPORT_FAILED, extras);
    } finally {
        try {
            if (pfd != null) {
                pfd.close();
            }
        } catch (IOException ex) {
        // Ignore
        }
        mRemoteBugreportSharingAccepted.set(false);
        setDeviceOwnerRemoteBugreportUriAndHash(null, null);
    }
}
Also used : Bundle(android.os.Bundle) PersistableBundle(android.os.PersistableBundle) ParcelFileDescriptor(android.os.ParcelFileDescriptor) FileNotFoundException(java.io.FileNotFoundException) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) IOException(java.io.IOException) Uri(android.net.Uri)

Example 49 with ParcelFileDescriptor

use of android.os.ParcelFileDescriptor in project platform_frameworks_base by android.

the class WallpaperManagerService method setWallpaper.

@Override
public ParcelFileDescriptor setWallpaper(String name, String callingPackage, Rect cropHint, boolean allowBackup, Bundle extras, int which, IWallpaperManagerCallback completion, int userId) {
    userId = ActivityManager.handleIncomingUser(getCallingPid(), getCallingUid(), userId, false, /* all */
    true, /* full */
    "changing wallpaper", null);
    checkPermission(android.Manifest.permission.SET_WALLPAPER);
    if ((which & (FLAG_LOCK | FLAG_SYSTEM)) == 0) {
        final String msg = "Must specify a valid wallpaper category to set";
        Slog.e(TAG, msg);
        throw new IllegalArgumentException(msg);
    }
    if (!isWallpaperSupported(callingPackage) || !isSetWallpaperAllowed(callingPackage)) {
        return null;
    }
    // "null" means the no-op crop, preserving the full input image
    if (cropHint == null) {
        cropHint = new Rect(0, 0, 0, 0);
    } else {
        if (cropHint.isEmpty() || cropHint.left < 0 || cropHint.top < 0) {
            throw new IllegalArgumentException("Invalid crop rect supplied: " + cropHint);
        }
    }
    synchronized (mLock) {
        if (DEBUG)
            Slog.v(TAG, "setWallpaper which=0x" + Integer.toHexString(which));
        WallpaperData wallpaper;
        /* If we're setting system but not lock, and lock is currently sharing the system
             * wallpaper, we need to migrate that image over to being lock-only before
             * the caller here writes new bitmap data.
             */
        if (which == FLAG_SYSTEM && mLockWallpaperMap.get(userId) == null) {
            if (DEBUG) {
                Slog.i(TAG, "Migrating system->lock to preserve");
            }
            migrateSystemToLockWallpaperLocked(userId);
        }
        wallpaper = getWallpaperSafeLocked(userId, which);
        final long ident = Binder.clearCallingIdentity();
        try {
            ParcelFileDescriptor pfd = updateWallpaperBitmapLocked(name, wallpaper, extras);
            if (pfd != null) {
                wallpaper.imageWallpaperPending = true;
                wallpaper.whichPending = which;
                wallpaper.setComplete = completion;
                wallpaper.cropHint.set(cropHint);
                wallpaper.allowBackup = allowBackup;
            }
            return pfd;
        } finally {
            Binder.restoreCallingIdentity(ident);
        }
    }
}
Also used : Rect(android.graphics.Rect) ParcelFileDescriptor(android.os.ParcelFileDescriptor)

Example 50 with ParcelFileDescriptor

use of android.os.ParcelFileDescriptor in project platform_frameworks_base by android.

the class DropBoxTest method testDropBoxEntrySerialization.

public void testDropBoxEntrySerialization() throws Exception {
    // Make sure DropBoxManager.Entry can be serialized to a Parcel and back
    // under a variety of conditions.
    Parcel parcel = Parcel.obtain();
    File dir = getEmptyDir("testDropBoxEntrySerialization");
    new DropBoxManager.Entry("empty", 1000000).writeToParcel(parcel, 0);
    new DropBoxManager.Entry("string", 2000000, "String Value").writeToParcel(parcel, 0);
    new DropBoxManager.Entry("bytes", 3000000, "Bytes Value".getBytes(), DropBoxManager.IS_TEXT).writeToParcel(parcel, 0);
    new DropBoxManager.Entry("zerobytes", 4000000, new byte[0], 0).writeToParcel(parcel, 0);
    new DropBoxManager.Entry("emptybytes", 5000000, (byte[]) null, DropBoxManager.IS_EMPTY).writeToParcel(parcel, 0);
    try {
        new DropBoxManager.Entry("badbytes", 99999, "Bad Bytes Value".getBytes(), DropBoxManager.IS_EMPTY).writeToParcel(parcel, 0);
        fail("IllegalArgumentException expected for non-null byte[] and IS_EMPTY flags");
    } catch (IllegalArgumentException e) {
    // expected
    }
    try {
        new DropBoxManager.Entry("badbytes", 99999, (byte[]) null, 0).writeToParcel(parcel, 0);
        fail("IllegalArgumentException expected for null byte[] and non-IS_EMPTY flags");
    } catch (IllegalArgumentException e) {
    // expected
    }
    File f = new File(dir, "file.dat");
    FileOutputStream os = new FileOutputStream(f);
    os.write("File Value".getBytes());
    os.close();
    new DropBoxManager.Entry("file", 6000000, f, DropBoxManager.IS_TEXT).writeToParcel(parcel, Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
    new DropBoxManager.Entry("binfile", 7000000, f, 0).writeToParcel(parcel, Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
    new DropBoxManager.Entry("emptyfile", 8000000, (ParcelFileDescriptor) null, DropBoxManager.IS_EMPTY).writeToParcel(parcel, 0);
    try {
        new DropBoxManager.Entry("badfile", 99999, new File(dir, "nonexist.dat"), 0);
        fail("IOException expected for nonexistent file");
    } catch (IOException e) {
    // expected
    }
    try {
        new DropBoxManager.Entry("badfile", 99999, f, DropBoxManager.IS_EMPTY).writeToParcel(parcel, 0);
        fail("IllegalArgumentException expected for non-null file and IS_EMPTY flags");
    } catch (IllegalArgumentException e) {
    // expected
    }
    try {
        new DropBoxManager.Entry("badfile", 99999, (ParcelFileDescriptor) null, 0);
        fail("IllegalArgumentException expected for null PFD and non-IS_EMPTY flags");
    } catch (IllegalArgumentException e) {
    // expected
    }
    File gz = new File(dir, "file.gz");
    GZIPOutputStream gzout = new GZIPOutputStream(new FileOutputStream(gz));
    gzout.write("Gzip File Value".getBytes());
    gzout.close();
    new DropBoxManager.Entry("gzipfile", 9000000, gz, DropBoxManager.IS_TEXT | DropBoxManager.IS_GZIPPED).writeToParcel(parcel, Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
    new DropBoxManager.Entry("gzipbinfile", 10000000, gz, DropBoxManager.IS_GZIPPED).writeToParcel(parcel, Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
    //
    // Switch from writing to reading
    //
    parcel.setDataPosition(0);
    DropBoxManager.Entry e;
    e = DropBoxManager.Entry.CREATOR.createFromParcel(parcel);
    assertEquals("empty", e.getTag());
    assertEquals(1000000, e.getTimeMillis());
    assertEquals(DropBoxManager.IS_EMPTY, e.getFlags());
    assertEquals(null, e.getText(100));
    assertEquals(null, e.getInputStream());
    e.close();
    e = DropBoxManager.Entry.CREATOR.createFromParcel(parcel);
    assertEquals("string", e.getTag());
    assertEquals(2000000, e.getTimeMillis());
    assertEquals(DropBoxManager.IS_TEXT, e.getFlags());
    assertEquals("String Value", e.getText(100));
    assertEquals("String Value", new BufferedReader(new InputStreamReader(e.getInputStream())).readLine());
    e.close();
    e = DropBoxManager.Entry.CREATOR.createFromParcel(parcel);
    assertEquals("bytes", e.getTag());
    assertEquals(3000000, e.getTimeMillis());
    assertEquals(DropBoxManager.IS_TEXT, e.getFlags());
    assertEquals("Bytes Value", e.getText(100));
    e.close();
    e = DropBoxManager.Entry.CREATOR.createFromParcel(parcel);
    assertEquals("zerobytes", e.getTag());
    assertEquals(4000000, e.getTimeMillis());
    assertEquals(0, e.getFlags());
    assertEquals(null, e.getText(100));
    assertEquals(null, new BufferedReader(new InputStreamReader(e.getInputStream())).readLine());
    e.close();
    e = DropBoxManager.Entry.CREATOR.createFromParcel(parcel);
    assertEquals("emptybytes", e.getTag());
    assertEquals(5000000, e.getTimeMillis());
    assertEquals(DropBoxManager.IS_EMPTY, e.getFlags());
    assertEquals(null, e.getText(100));
    assertEquals(null, e.getInputStream());
    e.close();
    e = DropBoxManager.Entry.CREATOR.createFromParcel(parcel);
    assertEquals("file", e.getTag());
    assertEquals(6000000, e.getTimeMillis());
    assertEquals(DropBoxManager.IS_TEXT, e.getFlags());
    assertEquals("File Value", e.getText(100));
    e.close();
    e = DropBoxManager.Entry.CREATOR.createFromParcel(parcel);
    assertEquals("binfile", e.getTag());
    assertEquals(7000000, e.getTimeMillis());
    assertEquals(0, e.getFlags());
    assertEquals(null, e.getText(100));
    assertEquals("File Value", new BufferedReader(new InputStreamReader(e.getInputStream())).readLine());
    e.close();
    e = DropBoxManager.Entry.CREATOR.createFromParcel(parcel);
    assertEquals("emptyfile", e.getTag());
    assertEquals(8000000, e.getTimeMillis());
    assertEquals(DropBoxManager.IS_EMPTY, e.getFlags());
    assertEquals(null, e.getText(100));
    assertEquals(null, e.getInputStream());
    e.close();
    e = DropBoxManager.Entry.CREATOR.createFromParcel(parcel);
    assertEquals("gzipfile", e.getTag());
    assertEquals(9000000, e.getTimeMillis());
    assertEquals(DropBoxManager.IS_TEXT, e.getFlags());
    assertEquals("Gzip File Value", e.getText(100));
    e.close();
    e = DropBoxManager.Entry.CREATOR.createFromParcel(parcel);
    assertEquals("gzipbinfile", e.getTag());
    assertEquals(10000000, e.getTimeMillis());
    assertEquals(0, e.getFlags());
    assertEquals(null, e.getText(100));
    assertEquals("Gzip File Value", new BufferedReader(new InputStreamReader(e.getInputStream())).readLine());
    e.close();
    assertEquals(0, parcel.dataAvail());
    parcel.recycle();
}
Also used : DropBoxManager(android.os.DropBoxManager) InputStreamReader(java.io.InputStreamReader) GZIPOutputStream(java.util.zip.GZIPOutputStream) Parcel(android.os.Parcel) FileOutputStream(java.io.FileOutputStream) ParcelFileDescriptor(android.os.ParcelFileDescriptor) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) File(java.io.File)

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