Search in sources :

Example 16 with ParcelFileDescriptor

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

the class WallpaperManagerService method updateWallpaperBitmapLocked.

ParcelFileDescriptor updateWallpaperBitmapLocked(String name, WallpaperData wallpaper) {
    if (name == null)
        name = "";
    try {
        File dir = getWallpaperDir(wallpaper.userId);
        if (!dir.exists()) {
            dir.mkdir();
            FileUtils.setPermissions(dir.getPath(), FileUtils.S_IRWXU | FileUtils.S_IRWXG | FileUtils.S_IXOTH, -1, -1);
        }
        File file = new File(dir, WALLPAPER);
        ParcelFileDescriptor fd = ParcelFileDescriptor.open(file, MODE_CREATE | MODE_READ_WRITE);
        if (!SELinux.restorecon(file)) {
            return null;
        }
        wallpaper.name = name;
        return fd;
    } catch (FileNotFoundException e) {
        Slog.w(TAG, "Error setting wallpaper", e);
    }
    return null;
}
Also used : ParcelFileDescriptor(android.os.ParcelFileDescriptor) FileNotFoundException(java.io.FileNotFoundException) File(java.io.File) JournaledFile(com.android.internal.util.JournaledFile)

Example 17 with ParcelFileDescriptor

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

the class ActivityManagerService method openContentUri.

public ParcelFileDescriptor openContentUri(Uri uri) throws RemoteException {
    enforceNotIsolatedCaller("openContentUri");
    final int userId = UserHandle.getCallingUserId();
    String name = uri.getAuthority();
    ContentProviderHolder cph = getContentProviderExternalUnchecked(name, null, userId);
    ParcelFileDescriptor pfd = null;
    if (cph != null) {
        // We record the binder invoker's uid in thread-local storage before
        // going to the content provider to open the file.  Later, in the code
        // that handles all permissions checks, we look for this uid and use
        // that rather than the Activity Manager's own uid.  The effect is that
        // we do the check against the caller's permissions even though it looks
        // to the content provider like the Activity Manager itself is making
        // the request.
        sCallerIdentity.set(new Identity(Binder.getCallingPid(), Binder.getCallingUid()));
        try {
            pfd = cph.provider.openFile(null, uri, "r");
        } catch (FileNotFoundException e) {
        // do nothing; pfd will be returned null
        } finally {
            // Ensure that whatever happens, we clean up the identity state
            sCallerIdentity.remove();
        }
        // We've got the fd now, so we're done with the provider.
        removeContentProviderExternalUnchecked(name, null, userId);
    } else {
        Slog.d(TAG, "Failed to get provider for authority '" + name + "'");
    }
    return pfd;
}
Also used : ParcelFileDescriptor(android.os.ParcelFileDescriptor) FileNotFoundException(java.io.FileNotFoundException)

Example 18 with ParcelFileDescriptor

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

the class Vpn method establish.

/**
     * Establish a VPN network and return the file descriptor of the VPN
     * interface. This methods returns {@code null} if the application is
     * revoked or not prepared.
     *
     * @param config The parameters to configure the network.
     * @return The file descriptor of the VPN interface.
     */
public synchronized ParcelFileDescriptor establish(VpnConfig config) {
    // Check if the caller is already prepared.
    PackageManager pm = mContext.getPackageManager();
    ApplicationInfo app = null;
    try {
        app = pm.getApplicationInfo(mPackage, 0);
    } catch (Exception e) {
        return null;
    }
    if (Binder.getCallingUid() != app.uid) {
        return null;
    }
    // Check if the service is properly declared.
    Intent intent = new Intent(VpnConfig.SERVICE_INTERFACE);
    intent.setClassName(mPackage, config.user);
    ResolveInfo info = pm.resolveService(intent, 0);
    if (info == null) {
        throw new SecurityException("Cannot find " + config.user);
    }
    if (!BIND_VPN_SERVICE.equals(info.serviceInfo.permission)) {
        throw new SecurityException(config.user + " does not require " + BIND_VPN_SERVICE);
    }
    // Load the label.
    String label = app.loadLabel(pm).toString();
    // Load the icon and convert it into a bitmap.
    Drawable icon = app.loadIcon(pm);
    Bitmap bitmap = null;
    if (icon.getIntrinsicWidth() > 0 && icon.getIntrinsicHeight() > 0) {
        int width = mContext.getResources().getDimensionPixelSize(android.R.dimen.notification_large_icon_width);
        int height = mContext.getResources().getDimensionPixelSize(android.R.dimen.notification_large_icon_height);
        icon.setBounds(0, 0, width, height);
        bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(bitmap);
        icon.draw(c);
        c.setBitmap(null);
    }
    // Configure the interface. Abort if any of these steps fails.
    ParcelFileDescriptor tun = ParcelFileDescriptor.adoptFd(jniCreate(config.mtu));
    try {
        updateState(DetailedState.CONNECTING, "establish");
        String interfaze = jniGetName(tun.getFd());
        if (jniSetAddresses(interfaze, config.addresses) < 1) {
            throw new IllegalArgumentException("At least one address must be specified");
        }
        if (config.routes != null) {
            jniSetRoutes(interfaze, config.routes);
        }
        Connection connection = new Connection();
        if (!mContext.bindService(intent, connection, Context.BIND_AUTO_CREATE)) {
            throw new IllegalStateException("Cannot bind " + config.user);
        }
        if (mConnection != null) {
            mContext.unbindService(mConnection);
        }
        if (mInterface != null && !mInterface.equals(interfaze)) {
            jniReset(mInterface);
        }
        mConnection = connection;
        mInterface = interfaze;
    } catch (RuntimeException e) {
        updateState(DetailedState.FAILED, "establish");
        IoUtils.closeQuietly(tun);
        throw e;
    }
    Log.i(TAG, "Established by " + config.user + " on " + mInterface);
    // Fill more values.
    config.user = mPackage;
    config.interfaze = mInterface;
    // Override DNS servers and show the notification.
    final long token = Binder.clearCallingIdentity();
    try {
        mCallback.override(config.dnsServers, config.searchDomains);
        showNotification(config, label, bitmap);
    } finally {
        Binder.restoreCallingIdentity(token);
    }
    // TODO: ensure that contract class eventually marks as connected
    updateState(DetailedState.AUTHENTICATING, "establish");
    return tun;
}
Also used : Canvas(android.graphics.Canvas) ApplicationInfo(android.content.pm.ApplicationInfo) Drawable(android.graphics.drawable.Drawable) ServiceConnection(android.content.ServiceConnection) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) RemoteException(android.os.RemoteException) ResolveInfo(android.content.pm.ResolveInfo) Bitmap(android.graphics.Bitmap) PackageManager(android.content.pm.PackageManager) ParcelFileDescriptor(android.os.ParcelFileDescriptor)

Example 19 with ParcelFileDescriptor

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

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)

Example 20 with ParcelFileDescriptor

use of android.os.ParcelFileDescriptor in project SeriesGuide by UweTrottmann.

the class JsonExportTask method exportData.

private int exportData(File exportPath, @BackupType int type) {
    // check if there is any data to export
    Cursor data = getDataCursor(type);
    if (data == null) {
        // query failed
        return ERROR;
    }
    if (data.getCount() == 0) {
        // There is no data? Done.
        data.close();
        return SUCCESS;
    }
    publishProgress(data.getCount(), 0);
    // try to export all data
    try {
        if (!isUseDefaultFolders) {
            // ensure the user has selected a backup file
            Uri backupFileUri = getDataBackupFile(type);
            if (backupFileUri == null) {
                return ERROR_FILE_ACCESS;
            }
            ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(backupFileUri, "w");
            if (pfd == null) {
                return ERROR_FILE_ACCESS;
            }
            FileOutputStream out = new FileOutputStream(pfd.getFileDescriptor());
            if (type == BACKUP_SHOWS) {
                writeJsonStreamShows(out, data);
            } else if (type == BACKUP_LISTS) {
                writeJsonStreamLists(out, data);
            } else if (type == BACKUP_MOVIES) {
                writeJsonStreamMovies(out, data);
            }
            // let the document provider know we're done.
            pfd.close();
        } else {
            File backupFile;
            if (type == BACKUP_SHOWS) {
                backupFile = new File(exportPath, EXPORT_JSON_FILE_SHOWS);
            } else if (type == BACKUP_LISTS) {
                backupFile = new File(exportPath, EXPORT_JSON_FILE_LISTS);
            } else if (type == BACKUP_MOVIES) {
                backupFile = new File(exportPath, EXPORT_JSON_FILE_MOVIES);
            } else {
                return ERROR;
            }
            OutputStream out = new FileOutputStream(backupFile);
            if (type == BACKUP_SHOWS) {
                writeJsonStreamShows(out, data);
            } else if (type == BACKUP_LISTS) {
                writeJsonStreamLists(out, data);
            } else {
                writeJsonStreamMovies(out, data);
            }
        }
    } catch (FileNotFoundException e) {
        Timber.e(e, "Backup file not found.");
        removeBackupFileUri(type);
        return ERROR_FILE_ACCESS;
    } catch (IOException | SecurityException e) {
        Timber.e(e, "Could not access backup file.");
        removeBackupFileUri(type);
        return ERROR_FILE_ACCESS;
    } catch (JsonParseException e) {
        Timber.e(e, "JSON export failed.");
        return ERROR;
    } finally {
        data.close();
    }
    return SUCCESS;
}
Also used : FileOutputStream(java.io.FileOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ParcelFileDescriptor(android.os.ParcelFileDescriptor) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Cursor(android.database.Cursor) JsonParseException(com.google.gson.JsonParseException) Uri(android.net.Uri) 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