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();
}
use of android.os.ParcelFileDescriptor in project android_frameworks_base by ParanoidAndroid.
the class Am method runProfile.
private void runProfile() throws Exception {
String profileFile = null;
boolean start = false;
boolean wall = false;
int userId = UserHandle.USER_CURRENT;
int profileType = 0;
String process = null;
String cmd = nextArgRequired();
if ("start".equals(cmd)) {
start = true;
String opt;
while ((opt = nextOption()) != null) {
if (opt.equals("--user")) {
userId = parseUserArg(nextArgRequired());
} else if (opt.equals("--wall")) {
wall = true;
} else {
System.err.println("Error: Unknown option: " + opt);
return;
}
}
process = nextArgRequired();
} else if ("stop".equals(cmd)) {
String opt;
while ((opt = nextOption()) != null) {
if (opt.equals("--user")) {
userId = parseUserArg(nextArgRequired());
} else {
System.err.println("Error: Unknown option: " + opt);
return;
}
}
process = nextArg();
} else {
// Compatibility with old syntax: process is specified first.
process = cmd;
cmd = nextArgRequired();
if ("start".equals(cmd)) {
start = true;
} else if (!"stop".equals(cmd)) {
throw new IllegalArgumentException("Profile command " + process + " not valid");
}
}
if (userId == UserHandle.USER_ALL) {
System.err.println("Error: Can't profile with user 'all'");
return;
}
ParcelFileDescriptor fd = null;
if (start) {
profileFile = nextArgRequired();
try {
fd = ParcelFileDescriptor.open(new File(profileFile), ParcelFileDescriptor.MODE_CREATE | ParcelFileDescriptor.MODE_TRUNCATE | ParcelFileDescriptor.MODE_READ_WRITE);
} catch (FileNotFoundException e) {
System.err.println("Error: Unable to open file: " + profileFile);
return;
}
}
try {
if (wall) {
// XXX doesn't work -- this needs to be set before booting.
String props = SystemProperties.get("dalvik.vm.extra-opts");
if (props == null || !props.contains("-Xprofile:wallclock")) {
props = props + " -Xprofile:wallclock";
//SystemProperties.set("dalvik.vm.extra-opts", props);
}
} else if (start) {
//removeWallOption();
}
if (!mAm.profileControl(process, userId, start, profileFile, fd, profileType)) {
wall = false;
throw new AndroidException("PROFILE FAILED on process " + process);
}
} finally {
if (!wall) {
//removeWallOption();
}
}
}
use of android.os.ParcelFileDescriptor in project android_frameworks_base by ParanoidAndroid.
the class WallpaperManager method setBitmap.
/**
* Change the current system wallpaper to a bitmap. The given bitmap is
* converted to a PNG and stored as the wallpaper. On success, the intent
* {@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast.
*
* <p>This method requires the caller to hold the permission
* {@link android.Manifest.permission#SET_WALLPAPER}.
*
* @param bitmap The bitmap to save.
*
* @throws IOException If an error occurs reverting to the default
* wallpaper.
*/
public void setBitmap(Bitmap bitmap) throws IOException {
if (sGlobals.mService == null) {
Log.w(TAG, "WallpaperService not running");
return;
}
try {
ParcelFileDescriptor fd = sGlobals.mService.setWallpaper(null);
if (fd == null) {
return;
}
FileOutputStream fos = null;
try {
fos = new ParcelFileDescriptor.AutoCloseOutputStream(fd);
bitmap.compress(Bitmap.CompressFormat.PNG, 90, fos);
} finally {
if (fos != null) {
fos.close();
}
}
} catch (RemoteException e) {
// Ignore
}
}
use of android.os.ParcelFileDescriptor in project android_frameworks_base by ParanoidAndroid.
the class BackupHelperDispatcher method doOneBackup.
private void doOneBackup(ParcelFileDescriptor oldState, BackupDataOutput data, ParcelFileDescriptor newState, Header header, BackupHelper helper) throws IOException {
int err;
FileDescriptor newStateFD = newState.getFileDescriptor();
// allocate space for the header in the file
int pos = allocateHeader_native(header, newStateFD);
if (pos < 0) {
throw new IOException("allocateHeader_native failed (error " + pos + ")");
}
data.setKeyPrefix(header.keyPrefix);
// do the backup
helper.performBackup(oldState, data, newState);
// fill in the header (seeking back to pos). The file pointer will be returned to
// where it was at the end of performBackup. Header.chunkSize will not be filled in.
err = writeHeader_native(header, newStateFD, pos);
if (err != 0) {
throw new IOException("writeHeader_native failed (error " + err + ")");
}
}
use of android.os.ParcelFileDescriptor in project android_frameworks_base by ParanoidAndroid.
the class BackupHelperDispatcher method performBackup.
public void performBackup(ParcelFileDescriptor oldState, BackupDataOutput data, ParcelFileDescriptor newState) throws IOException {
// First, do the helpers that we've already done, since they're already in the state
// file.
int err;
Header header = new Header();
TreeMap<String, BackupHelper> helpers = (TreeMap<String, BackupHelper>) mHelpers.clone();
FileDescriptor oldStateFD = null;
FileDescriptor newStateFD = newState.getFileDescriptor();
if (oldState != null) {
oldStateFD = oldState.getFileDescriptor();
while ((err = readHeader_native(header, oldStateFD)) >= 0) {
if (err == 0) {
BackupHelper helper = helpers.get(header.keyPrefix);
Log.d(TAG, "handling existing helper '" + header.keyPrefix + "' " + helper);
if (helper != null) {
doOneBackup(oldState, data, newState, header, helper);
helpers.remove(header.keyPrefix);
} else {
skipChunk_native(oldStateFD, header.chunkSize);
}
}
}
}
// Then go through and do the rest that we haven't done.
for (Map.Entry<String, BackupHelper> entry : helpers.entrySet()) {
header.keyPrefix = entry.getKey();
Log.d(TAG, "handling new helper '" + header.keyPrefix + "'");
BackupHelper helper = entry.getValue();
doOneBackup(oldState, data, newState, header, helper);
}
}
Aggregations