Search in sources :

Example 11 with Parcel

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

the class SyncStorageEngine method writeStatisticsLocked.

/**
     * Write all sync statistics to the sync status file.
     */
private void writeStatisticsLocked() {
    if (DEBUG_FILE)
        Log.v(TAG, "Writing new " + mStatisticsFile.getBaseFile());
    // The file is being written, so we don't need to have a scheduled
    // write until the next change.
    removeMessages(MSG_WRITE_STATISTICS);
    FileOutputStream fos = null;
    try {
        fos = mStatisticsFile.startWrite();
        Parcel out = Parcel.obtain();
        final int N = mDayStats.length;
        for (int i = 0; i < N; i++) {
            DayStats ds = mDayStats[i];
            if (ds == null) {
                break;
            }
            out.writeInt(STATISTICS_FILE_ITEM);
            out.writeInt(ds.day);
            out.writeInt(ds.successCount);
            out.writeLong(ds.successTime);
            out.writeInt(ds.failureCount);
            out.writeLong(ds.failureTime);
        }
        out.writeInt(STATISTICS_FILE_END);
        fos.write(out.marshall());
        out.recycle();
        mStatisticsFile.finishWrite(fos);
    } catch (java.io.IOException e1) {
        Log.w(TAG, "Error writing stats", e1);
        if (fos != null) {
            mStatisticsFile.failWrite(fos);
        }
    }
}
Also used : Parcel(android.os.Parcel) FileOutputStream(java.io.FileOutputStream)

Example 12 with Parcel

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

the class WindowManagerService method viewServerWindowCommand.

/**
     * Sends a command to a target window. The result of the command, if any, will be
     * written in the output stream of the specified socket.
     *
     * The parameters must follow this syntax:
     * windowHashcode extra
     *
     * Where XX is the length in characeters of the windowTitle.
     *
     * The first parameter is the target window. The window with the specified hashcode
     * will be the target. If no target can be found, nothing happens. The extra parameters
     * will be delivered to the target window and as parameters to the command itself.
     *
     * @param client The remote client to sent the result, if any, to.
     * @param command The command to execute.
     * @param parameters The command parameters.
     *
     * @return True if the command was successfully delivered, false otherwise. This does
     *         not indicate whether the command itself was successful.
     */
boolean viewServerWindowCommand(Socket client, String command, String parameters) {
    if (isSystemSecure()) {
        return false;
    }
    boolean success = true;
    Parcel data = null;
    Parcel reply = null;
    BufferedWriter out = null;
    // Any uncaught exception will crash the system process
    try {
        // Find the hashcode of the window
        int index = parameters.indexOf(' ');
        if (index == -1) {
            index = parameters.length();
        }
        final String code = parameters.substring(0, index);
        int hashCode = (int) Long.parseLong(code, 16);
        // Extract the command's parameter after the window description
        if (index < parameters.length()) {
            parameters = parameters.substring(index + 1);
        } else {
            parameters = "";
        }
        final WindowState window = findWindow(hashCode);
        if (window == null) {
            return false;
        }
        data = Parcel.obtain();
        data.writeInterfaceToken("android.view.IWindow");
        data.writeString(command);
        data.writeString(parameters);
        data.writeInt(1);
        ParcelFileDescriptor.fromSocket(client).writeToParcel(data, 0);
        reply = Parcel.obtain();
        final IBinder binder = window.mClient.asBinder();
        // TODO: GET THE TRANSACTION CODE IN A SAFER MANNER
        binder.transact(IBinder.FIRST_CALL_TRANSACTION, data, reply, 0);
        reply.readException();
        if (!client.isOutputShutdown()) {
            out = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
            out.write("DONE\n");
            out.flush();
        }
    } catch (Exception e) {
        Slog.w(TAG, "Could not send command " + command + " with parameters " + parameters, e);
        success = false;
    } finally {
        if (data != null) {
            data.recycle();
        }
        if (reply != null) {
            reply.recycle();
        }
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
            }
        }
    }
    return success;
}
Also used : IBinder(android.os.IBinder) Parcel(android.os.Parcel) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) Point(android.graphics.Point) RemoteException(android.os.RemoteException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) NoSuchElementException(java.util.NoSuchElementException) BufferedWriter(java.io.BufferedWriter)

Example 13 with Parcel

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

the class DropBoxTest method testDropBoxEntrySerializationDoesntLeakFileDescriptors.

public void testDropBoxEntrySerializationDoesntLeakFileDescriptors() throws Exception {
    File dir = getEmptyDir("testDropBoxEntrySerialization");
    File f = new File(dir, "file.dat");
    FileOutputStream os = new FileOutputStream(f);
    os.write("File Value".getBytes());
    os.close();
    int before = countOpenFiles();
    assertTrue(before > 0);
    for (int i = 0; i < 1000; i++) {
        Parcel parcel = Parcel.obtain();
        new DropBoxManager.Entry("file", 1000000, f, 0).writeToParcel(parcel, Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
        parcel.setDataPosition(0);
        DropBoxManager.Entry e = DropBoxManager.Entry.CREATOR.createFromParcel(parcel);
        assertEquals("file", e.getTag());
        e.close();
        parcel.recycle();
    }
    int after = countOpenFiles();
    assertTrue(after > 0);
    assertTrue(after < before + 20);
}
Also used : DropBoxManager(android.os.DropBoxManager) Parcel(android.os.Parcel) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 14 with Parcel

use of android.os.Parcel 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 15 with Parcel

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

the class ActivityManagerProxy method getConfiguration.

public Configuration getConfiguration() throws RemoteException {
    Parcel data = Parcel.obtain();
    Parcel reply = Parcel.obtain();
    data.writeInterfaceToken(IActivityManager.descriptor);
    mRemote.transact(GET_CONFIGURATION_TRANSACTION, data, reply, 0);
    reply.readException();
    Configuration res = Configuration.CREATOR.createFromParcel(reply);
    reply.recycle();
    data.recycle();
    return res;
}
Also used : Configuration(android.content.res.Configuration) Parcel(android.os.Parcel)

Aggregations

Parcel (android.os.Parcel)3542 Point (android.graphics.Point)263 Test (org.junit.Test)215 SmallTest (android.test.suitebuilder.annotation.SmallTest)163 RemoteException (android.os.RemoteException)139 IBinder (android.os.IBinder)120 IOException (java.io.IOException)105 FileOutputStream (java.io.FileOutputStream)72 File (java.io.File)69 Bundle (android.os.Bundle)56 Intent (android.content.Intent)51 SmallTest (android.support.test.filters.SmallTest)51 ArrayList (java.util.ArrayList)49 ParcelFileDescriptor (android.os.ParcelFileDescriptor)35 FileInputStream (java.io.FileInputStream)34 ComponentName (android.content.ComponentName)33 KeyphraseRecognitionEvent (android.hardware.soundtrigger.SoundTrigger.KeyphraseRecognitionEvent)28 Keyphrase (android.hardware.soundtrigger.SoundTrigger.Keyphrase)24 KeyphraseSoundModel (android.hardware.soundtrigger.SoundTrigger.KeyphraseSoundModel)20 Uri (android.net.Uri)20