Example usage for android.os Parcel obtain

List of usage examples for android.os Parcel obtain

Introduction

In this page you can find the example usage for android.os Parcel obtain.

Prototype

public static Parcel obtain() 

Source Link

Document

Retrieve a new Parcel object from the pool.

Usage

From source file:com.nestlabs.sdk.NestConfigAndroidTest.java

@Test
public void testNestConfigToParcel() {
    String testClientId = "test-id";
    String testClientSecret = "test-secret";
    String testRedirectURL = "test-redirect-url";

    NestConfig config = new NestConfig.Builder().clientID(testClientId).clientSecret(testClientSecret)
            .redirectURL(testRedirectURL).build();

    Parcel parcel = Parcel.obtain();
    config.writeToParcel(parcel, 0);/*from ww  w.  j a  v  a 2s. com*/

    parcel.setDataPosition(0);

    NestConfig configFromParcel = NestConfig.CREATOR.createFromParcel(parcel);
    assertEquals(config, configFromParcel);
}

From source file:com.scvngr.levelup.core.model.CauseAffiliationTest.java

@SmallTest
public void testParcel_nullId() {
    final CauseAffiliation causeAffiliation = CauseAffiliationFixture.getFullModel(null);

    final Parcel parcel = Parcel.obtain();
    causeAffiliation.writeToParcel(parcel, 0);
    parcel.setDataPosition(0);/*w w w.j  a v  a 2 s.  com*/

    final CauseAffiliation causeAffiliation2 = CauseAffiliation.CREATOR.createFromParcel(parcel);
    parcel.recycle();
    assertEquals(causeAffiliation, causeAffiliation2);
}

From source file:Main.java

public static byte[] getBytes(@NonNull Parcelable parcelable) {
    if (parcelable == null) {
        return new byte[0];
    }//from   ww  w .  ja  v a2s  .c om
    Parcel parcel = Parcel.obtain();
    parcelable.writeToParcel(parcel, 0);
    byte[] bytes = parcel.marshall();
    parcel.recycle();
    return bytes;
}

From source file:io.github.data4all.model.DeviceOrientationTest.java

/**
 * Create a new Parcel to save/parcelable the testDeviceOrientation,
 * afterwards a new deviceorientation is created from the parcel and we
 * check if it contains all attributes./*from  ww  w.  j a  v  a 2s .c  o m*/
 */
@Test
public void test_parcelable_node() {
    Parcel newParcel = Parcel.obtain();
    DeviceOrientation testDeviceOrientation = new DeviceOrientation(10, 20, 30, 1234567);

    testDeviceOrientation.writeToParcel(newParcel, 0);
    newParcel.setDataPosition(0);
    DeviceOrientation deParcelDeviceOrientation = DeviceOrientation.CREATOR.createFromParcel(newParcel);

    assertEquals(testDeviceOrientation.getAzimuth(), deParcelDeviceOrientation.getAzimuth(), 0);
    assertEquals(testDeviceOrientation.getPitch(), deParcelDeviceOrientation.getPitch(), 0);
    assertEquals(testDeviceOrientation.getRoll(), deParcelDeviceOrientation.getRoll(), 0);
    assertEquals(testDeviceOrientation.getTimestamp(), deParcelDeviceOrientation.getTimestamp(), 0);

}

From source file:jp.alessandro.android.iab.PurchaseParcelableTest.java

private void writeToParcel(Purchase purchase) {
    // Obtain a Parcel object and write the parcelable object to it
    Parcel parcel = Parcel.obtain();
    purchase.writeToParcel(parcel, purchase.describeContents());

    // After you're done with writing, you need to reset the parcel for reading
    parcel.setDataPosition(0);//  w  w w.  ja va2  s .  c om

    Purchase fromParcel = Purchase.CREATOR.createFromParcel(parcel);

    assertThat(purchase.getOriginalJson()).isEqualTo(fromParcel.getOriginalJson());
    assertThat(purchase.getOrderId()).isEqualTo(fromParcel.getOrderId());
    assertThat(purchase.getPackageName()).isEqualTo(fromParcel.getPackageName());
    assertThat(purchase.getSku()).isEqualTo(fromParcel.getSku());
    assertThat(purchase.getPurchaseTime()).isEqualTo(fromParcel.getPurchaseTime());
    assertThat(purchase.getPurchaseState()).isEqualTo(fromParcel.getPurchaseState());
    assertThat(purchase.getDeveloperPayload()).isEqualTo(fromParcel.getDeveloperPayload());
    assertThat(purchase.getToken()).isEqualTo(fromParcel.getToken());
    assertThat(purchase.isAutoRenewing()).isEqualTo(fromParcel.isAutoRenewing());
    assertThat(purchase.getSignature()).isEqualTo(fromParcel.getSignature());
}

From source file:com.bmd.android.collection.example.EnhancedArrayMapTest.java

public void testParcelable() {

    final Bundle bundle = new Bundle();
    bundle.putParcelable("array", mArray);

    final Parcel parcel = Parcel.obtain();
    bundle.writeToParcel(parcel, 0);// w w w .j a v a2  s  .  c  o  m

    parcel.setDataPosition(0);

    final Bundle out = parcel.readBundle();
    out.setClassLoader(AndroidCollections.class.getClassLoader());

    assertThat(out.getParcelable("array")).isEqualTo(mArray);
}

From source file:net.geniecode.ttr.ScheduleReceiver.java

@SuppressWarnings("deprecation")
@SuppressLint({ "Recycle", "NewApi", "InlinedApi" })
@Override/* w ww. j  av  a  2 s .c  om*/
public void onReceive(Context context, Intent intent) {
    if (!Schedules.SCHEDULE_ACTION.equals(intent.getAction())) {
        // Unknown intent, bail.
        return;
    }

    Schedule schedule = null;
    // Grab the schedule from the intent. Since the remote AlarmManagerService
    // fills in the Intent to add some extra data, it must unparcel the
    // Schedule object. It throws a ClassNotFoundException when unparcelling.
    // To avoid this, do the marshalling ourselves.
    final byte[] data = intent.getByteArrayExtra(Schedules.SCHEDULE_RAW_DATA);
    if (data != null) {
        Parcel in = Parcel.obtain();
        in.unmarshall(data, 0, data.length);
        in.setDataPosition(0);
        schedule = Schedule.CREATOR.createFromParcel(in);
    }

    if (schedule == null) {
        // Make sure we set the next schedule if needed.
        Schedules.setNextSchedule(context);
        return;
    }

    // Disable this schedule if it does not repeat.
    if (!schedule.daysOfWeek.isRepeatSet()) {
        Schedules.enableSchedule(context, schedule.id, false);
    } else {
        // Enable the next schedule if there is one. The above call to
        // enableSchedule will call setNextSchedule so avoid calling it twice.
        Schedules.setNextSchedule(context);
    }

    long now = System.currentTimeMillis();

    if (now > schedule.time + STALE_WINDOW) {
        return;
    }

    // Get telephony service
    mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

    // Execute only for devices with versions of Android less than 4.2
    if (android.os.Build.VERSION.SDK_INT < 17) {
        // Get flight mode state
        boolean isEnabled = Settings.System.getInt(context.getContentResolver(),
                Settings.System.AIRPLANE_MODE_ON, 0) == 1;

        // Get Wi-Fi service
        mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

        if ((schedule.aponoff) && (!isEnabled) && (schedule.mode.equals("1"))
                && (mTelephonyManager.getCallState() == TelephonyManager.CALL_STATE_IDLE)) {
            // Enable flight mode
            Settings.System.putInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON,
                    isEnabled ? 0 : 1);

            // Get Wi-Fi state and disable that one too, just in case
            // (On some devices it doesn't get disabled when the flight mode is
            // turned on, so we do it here)
            boolean isWifiEnabled = mWifiManager.isWifiEnabled();

            SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, 0);

            if (isWifiEnabled) {
                SharedPreferences.Editor editor = settings.edit();
                editor.putBoolean(WIFI_STATE, isWifiEnabled);
                editor.commit();
                mWifiManager.setWifiEnabled(false);
            } else {
                SharedPreferences.Editor editor = settings.edit();
                editor.putBoolean(WIFI_STATE, isWifiEnabled);
                editor.commit();
            }

            // Post an intent to reload
            Intent relintent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
            relintent.putExtra("state", !isEnabled);
            context.sendBroadcast(relintent);
        } else if ((!schedule.aponoff) && (isEnabled) && (schedule.mode.equals("1"))) {
            // Disable flight mode
            Settings.System.putInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON,
                    isEnabled ? 0 : 1);

            // Restore previously remembered Wi-Fi state
            SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, 0);
            Boolean WiFiState = settings.getBoolean(WIFI_STATE, true);

            if (WiFiState) {
                mWifiManager.setWifiEnabled(true);
            }

            // Post an intent to reload
            Intent relintent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
            relintent.putExtra("state", !isEnabled);
            context.sendBroadcast(relintent);
        }
        // Check whether there are ongoing phone calls, and if so
        // show notification instead of just enabling the flight mode
        else if ((schedule.aponoff) && (!isEnabled) && (schedule.mode.equals("1"))
                && (mTelephonyManager.getCallState() != TelephonyManager.CALL_STATE_IDLE)) {
            setNotification(context);
        }
        // Execute for devices with Android 4.2   or higher
    } else {
        // Get flight mode state
        String result = Settings.Global.getString(context.getContentResolver(),
                Settings.Global.AIRPLANE_MODE_ON);

        if ((schedule.aponoff) && (result.equals("0")) && (schedule.mode.equals("1"))
                && (mTelephonyManager.getCallState() == TelephonyManager.CALL_STATE_IDLE)) {
            // Keep the device awake while enabling flight mode
            Intent service = new Intent(context, ScheduleIntentService.class);
            startWakefulService(context, service);
        } else if ((!schedule.aponoff) && (result.equals("1")) && (schedule.mode.equals("1"))) {
            // Keep the device awake while enabling flight mode
            Intent service = new Intent(context, ScheduleIntentService.class);
            startWakefulService(context, service);
        } else if ((schedule.aponoff) && (result.equals("0")) && (schedule.mode.equals("1"))
                && (mTelephonyManager.getCallState() != TelephonyManager.CALL_STATE_IDLE)) {
            setNotification(context);
        }
    }

    // Get audio service
    mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);

    // Get current ringer mode and set silent or normal mode accordingly
    switch (mAudioManager.getRingerMode()) {
    case AudioManager.RINGER_MODE_SILENT:
        if ((!schedule.silentonoff) && (schedule.mode.equals("2"))) {
            mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
        }
        break;
    case AudioManager.RINGER_MODE_NORMAL:
        if ((schedule.silentonoff) && (schedule.mode.equals("2"))) {
            mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
        }
        break;
    case AudioManager.RINGER_MODE_VIBRATE:
        // If the phone is set to vibrate let's make it completely silent
        if ((schedule.silentonoff) && (schedule.mode.equals("2"))) {
            mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
        }
        // or restore the regular sounds on it if that's scheduled
        else if ((!schedule.silentonoff) && (schedule.mode.equals("2"))) {
            mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
        }
        break;
    }
}

From source file:edu.umich.flowfence.common.ParceledPayload.java

public static boolean canParcelObject(Object obj) {
    Parcel p = Parcel.obtain();
    try {/*w w  w  .  j  ava 2  s. c o m*/
        p.writeValue(obj);
        return true;
    } catch (Exception e) {
        return false;
    } finally {
        p.recycle();
    }
}

From source file:com.scvngr.levelup.core.model.UserTest.java

@SmallTest
public void testParcel_valid() throws JSONException {
    final JSONObject object = UserFixture.getMinimalJsonObject();

    object.remove(UserJsonFactory.JsonKeys.CUSTOM_ATTRIBUTES);
    final User user = new UserJsonFactory().from(object);
    final Parcel parcel = Parcel.obtain();
    try {//from   ww w.jav  a 2  s  .  c o m
        user.writeToParcel(parcel, 0);
        parcel.setDataPosition(0);

        final User parceled = User.CREATOR.createFromParcel(parcel);
        assertEquals(user, parceled);
    } finally {
        parcel.recycle();
    }
}

From source file:com.oasisfeng.android.service.notification.StatusBarNotificationCompat.java

private static UserHandle toUserHandle(final int user) {
    final Parcel parcel = Parcel.obtain();
    try {/*ww w .j  a v  a  2s .c om*/
        parcel.writeInt(user);
        parcel.setDataPosition(0);
        return UserHandle.readFromParcel(parcel);
    } finally {
        parcel.recycle();
    }
}