Java Code Examples for android.os.Parcel#unmarshall()

The following examples show how to use android.os.Parcel#unmarshall() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BundleManager.java    From Lucid-Browser with Apache License 2.0 6 votes vote down vote up
public Bundle restoreFromPreferences() {
    Bundle bundle = null;
    SharedPreferences settings = activity.getSharedPreferences("instance", 0);
    String serialized = settings.getString("parcel", null);

    if (serialized != null) {
        Parcel parcel = Parcel.obtain();
        try {
            byte[] data = Base64.decode(serialized, 0);
            parcel.unmarshall(data, 0, data.length);
            parcel.setDataPosition(0);
            bundle = parcel.readBundle();
        } finally {
            parcel.recycle();
        }
    }
    return bundle;
}
 
Example 2
Source File: BundlePreference.java    From rx-shared-preferences with Apache License 2.0 6 votes vote down vote up
@Override
protected Bundle getValue(String key, Bundle defValue) {
  try {
    String serialized = mSharedPreferences.getString(key, null);
    if (serialized != null) {
      Parcel parcel = Parcel.obtain();
      byte[] data = Base64.decode(serialized, 0);
      parcel.unmarshall(data, 0, data.length);
      parcel.setDataPosition(0);
      Bundle bundle = new Bundle();
      bundle.readFromParcel(parcel);
      return bundle;
    }
  } catch (Exception ignore) {
    if (RxSharedPreferences.DEBUG) {
      ignore.printStackTrace();
    }
  }
  return defValue;
}
 
Example 3
Source File: ElementStack.java    From deltachat-android with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Pops the first different state from the supplied element.
 */
@Nullable EditorElement pop(@NonNull EditorElement element) {
  if (stack.empty()) return null;

  byte[] elementBytes = getBytes(element);
  byte[] stackData    = null;

  while (!stack.empty() && stackData == null) {
    byte[] topData = stack.pop();

    if (!Arrays.equals(topData, elementBytes)) {
      stackData = topData;
    }
  }

  if (stackData == null) return null;

  Parcel parcel = Parcel.obtain();
  try {
    parcel.unmarshall(stackData, 0, stackData.length);
    parcel.setDataPosition(0);
    return parcel.readParcelable(EditorElement.class.getClassLoader());
  } finally {
    parcel.recycle();
  }
}
 
Example 4
Source File: MasterSecret.java    From Silence with GNU General Public License v3.0 6 votes vote down vote up
public MasterSecret parcelClone() {
  Parcel thisParcel = Parcel.obtain();
  Parcel thatParcel = Parcel.obtain();
  byte[] bytes      = null;

  thisParcel.writeValue(this);
  bytes = thisParcel.marshall();

  thatParcel.unmarshall(bytes, 0, bytes.length);
  thatParcel.setDataPosition(0);

  MasterSecret that = (MasterSecret)thatParcel.readValue(MasterSecret.class.getClassLoader());

  thisParcel.recycle();
  thatParcel.recycle();

  return that;
}
 
Example 5
Source File: IntentUtil.java    From OpenYOLO-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Deserializes an intent from the provided bytes array.
 * @throws BadParcelableException if the intent is null, not present, or malformed.
 */
@NonNull
public static Intent fromBytes(@NonNull byte[] intentBytes) throws BadParcelableException {
    require(intentBytes, notNullValue());

    Intent intent;

    try {
        Parcel parcel = Parcel.obtain();
        parcel.unmarshall(intentBytes, 0, intentBytes.length);
        parcel.setDataPosition(0);
        intent = parcel.readParcelable(IntentUtil.class.getClassLoader());
        parcel.recycle();
    } catch (Exception ex) {
        throw new BadParcelableException(ex);
    }

    validate(intent, notNullValue(), BadParcelableException.class);

    return intent;
}
 
Example 6
Source File: LaunchApp.java    From PUMA with Apache License 2.0 6 votes vote down vote up
private AccessibilityNodeInfo loadViewFromFile(String fn, int size) {
	final Parcel p = Parcel.obtain();
	AccessibilityNodeInfo node = null;

	try {
		FileInputStream fis = new FileInputStream(new File(fn));
		byte[] rawData = new byte[size];
		int actualSize = fis.read(rawData);

		if (actualSize == size) {
			p.unmarshall(rawData, 0, size);
			p.setDataPosition(0);
			node = AccessibilityNodeInfo.CREATOR.createFromParcel(p);
		} else {
			Util.err("Read failed: read " + actualSize + " bytes, expected " + size + " bytes");
		}
	} catch (IOException e) {
		e.printStackTrace();
	}

	return node;
}
 
Example 7
Source File: AndroidSharedKeyRecord.java    From commcare-android with Apache License 2.0 6 votes vote down vote up
public Bundle getIncomingCallout(Intent incoming) {
    byte[] incomingCallout = incoming.getByteArrayExtra(EXTRA_KEY_CALLOUT);
    byte[] incomingKey = incoming.getByteArrayExtra(EXTRA_KEY_CALLOUT_SYMETRIC_KEY);
    Parcel p = Parcel.obtain();
    byte[] decoded;
    try {
        byte[] aesKey = CryptUtil.decrypt(incomingKey, CryptUtil.getPrivateKeyCipher(this.privateKey));
        decoded = CryptUtil.decrypt(incomingCallout, CryptUtil.getAesKeyCipher(aesKey));
    } catch (GeneralSecurityException gse) {
        Logger.exception("Error getting incoming callout", gse);
        return null;
    }

    p.unmarshall(decoded, 0, decoded.length);
    p.setDataPosition(0);

    Bundle result = p.readBundle();
    p.recycle();
    return result;
}
 
Example 8
Source File: PayPal.java    From braintree_android with MIT License 6 votes vote down vote up
@Nullable
private static Request getPersistedRequest(Context context) {
    SharedPreferences prefs = BraintreeSharedPreferences.getSharedPreferences(context);

    try {
        byte[] requestBytes = Base64.decode(prefs.getString(REQUEST_KEY, ""), 0);
        Parcel parcel = Parcel.obtain();
        parcel.unmarshall(requestBytes, 0, requestBytes.length);
        parcel.setDataPosition(0);

        String type = prefs.getString(REQUEST_TYPE_KEY, "");
        if (BillingAgreementRequest.class.getSimpleName().equals(type)) {
            return BillingAgreementRequest.CREATOR.createFromParcel(parcel);
        } else if (CheckoutRequest.class.getSimpleName().equals(type)) {
            return CheckoutRequest.CREATOR.createFromParcel(parcel);
        }
    } catch (Exception ignored) {
    } finally {
        prefs.edit()
                .remove(REQUEST_KEY)
                .remove(REQUEST_TYPE_KEY)
                .apply();
    }

    return null;
}
 
Example 9
Source File: CacheUtils.java    From LLApp with Apache License 2.0 5 votes vote down vote up
private static <T> T bytes2Parcelable(byte[] bytes, Parcelable.Creator<T> creator) {
    if (bytes == null) return null;
    Parcel parcel = Parcel.obtain();
    parcel.unmarshall(bytes, 0, bytes.length);
    parcel.setDataPosition(0);
    T result = creator.createFromParcel(parcel);
    parcel.recycle();
    return result;
}
 
Example 10
Source File: WearBusTools.java    From BusWear with Apache License 2.0 5 votes vote down vote up
/**
 * Converts the byte[] to a Parcel
 *
 * @param bytes
 * @return
 */
public static Parcel byteToParcel(@NonNull byte[] bytes) {
    Parcel parcel = Parcel.obtain();
    parcel.unmarshall(bytes, 0, bytes.length);
    parcel.setDataPosition(0);
    return parcel;
}
 
Example 11
Source File: AppInfoTest.java    From Intra with Apache License 2.0 5 votes vote down vote up
private void verifyParcelSerializationRoundtrip(AppInfo info) {
  Parcel parcel1 = Parcel.obtain();
  info.writeToParcel(parcel1, 0);
  byte[] data = parcel1.marshall();
  parcel1.recycle();
  Parcel parcel2 = Parcel.obtain();
  parcel2.unmarshall(data, 0, data.length);
  parcel2.setDataPosition(0);
  AppInfo copy = AppInfo.CREATOR.createFromParcel(parcel2);
  parcel2.recycle();
  assertEquals(0, info.compareTo(copy));
}
 
Example 12
Source File: SharedPreferencesMessageStore.java    From mobile-messaging-sdk-android with Apache License 2.0 5 votes vote down vote up
private Bundle deserialize(String serialized) {
    if (serialized == null) {
        return null;
    }

    Parcel parcel = Parcel.obtain();
    try {
        byte[] data = Base64.decode(serialized, 0);
        parcel.unmarshall(data, 0, data.length);
        parcel.setDataPosition(0);
        return parcel.readBundle();
    } finally {
        parcel.recycle();
    }
}
 
Example 13
Source File: ParcelFn.java    From AndroidBase with Apache License 2.0 5 votes vote down vote up
public static <T> T unmarshall(byte[] array) {
    Parcel parcel = Parcel.obtain();
    parcel.unmarshall(array, 0, array.length);
    parcel.setDataPosition(0);
    Object value = parcel.readValue(CLASS_LOADER);
    parcel.recycle();
    return (T)value;
}
 
Example 14
Source File: VJobSchedulerService.java    From container with GNU General Public License v3.0 5 votes vote down vote up
private void readJobs() {
    File jobFile = VEnvironment.getJobConfigFile();
    if (!jobFile.exists()) {
        return;
    }
    Parcel p = Parcel.obtain();
    try {
        FileInputStream fis = new FileInputStream(jobFile);
        byte[] bytes = new byte[(int) jobFile.length()];
        int len = fis.read(bytes);
        fis.close();
        if (len != bytes.length) {
            throw new IOException("Unable to read job config.");
        }
        p.unmarshall(bytes, 0, bytes.length);
        p.setDataPosition(0);
        int version = p.readInt();
        if (version != JOB_FILE_VERSION) {
            throw new IOException("Bad version of job file: " + version);
        }
        if (!mJobStore.isEmpty()) {
            mJobStore.clear();
        }
        int count = p.readInt();
        for (int i = 0; i < count; i++) {
            JobId jobId = new JobId(p);
            JobConfig config = new JobConfig(p);
            mJobStore.put(jobId, config);
            mGlobalJobId = Math.max(mGlobalJobId, config.virtualJobId);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        p.recycle();
    }

}
 
Example 15
Source File: SyncStorageEngine.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * Read all sync statistics back in to the initial engine state.
 */
private void readStatisticsLocked() {
    try {
        byte[] data = mStatisticsFile.readFully();
        Parcel in = Parcel.obtain();
        in.unmarshall(data, 0, data.length);
        in.setDataPosition(0);
        int token;
        int index = 0;
        while ((token=in.readInt()) != STATISTICS_FILE_END) {
            if (token == STATISTICS_FILE_ITEM
                    || token == STATISTICS_FILE_ITEM_OLD) {
                int day = in.readInt();
                if (token == STATISTICS_FILE_ITEM_OLD) {
                    day = day - 2009 + 14245;  // Magic!
                }
                DayStats ds = new DayStats(day);
                ds.successCount = in.readInt();
                ds.successTime = in.readLong();
                ds.failureCount = in.readInt();
                ds.failureTime = in.readLong();
                if (index < mDayStats.length) {
                    mDayStats[index] = ds;
                    index++;
                }
            } else {
                // Ooops.
                Slog.w(TAG, "Unknown stats token: " + token);
                break;
            }
        }
    } catch (java.io.IOException e) {
        Slog.i(TAG, "No initial statistics");
    }
}
 
Example 16
Source File: ByteStringToParcelableConverter.java    From android-test with Apache License 2.0 5 votes vote down vote up
/**
 * Converts a {@link ByteString} into a {@link Parcelable}.
 *
 * @param byteString the {@link ByteString} encoding of the {@link Parcelable}
 * @return an instance of {@link Parcelable}
 */
@Override
public Parcelable convert(@NonNull ByteString byteString) {
  Parcel parcel = Parcel.obtain();
  byte[] bytes = byteString.toByteArray();
  Parcelable fromParcel = null;
  try {
    parcel.unmarshall(bytes, 0, bytes.length);
    // Move the current read/write position to the beginning
    parcel.setDataPosition(0);
    Field creatorField = parcelableClass.getField("CREATOR");
    Parcelable.Creator<?> creator = (Creator) creatorField.get(null);
    fromParcel = parcelableClass.cast(creator.createFromParcel(parcel));
  } catch (NoSuchFieldException nsfe) {
    throw new RemoteProtocolException(
        String.format(
            Locale.ROOT,
            "Cannot find CREATOR field for Parcelable %s",
            parcelableClass.getName()),
        nsfe);
  } catch (IllegalAccessException iae) {
    throw new RemoteProtocolException(
        String.format(
            Locale.ROOT,
            "Cannot create instance of %s. CREATOR field is inaccessible",
            parcelableClass.getName()),
        iae);
  } finally {
    if (parcel != null) {
      parcel.recycle();
    }
  }
  return fromParcel;
}
 
Example 17
Source File: ParcelUtil.java    From deltachat-android with GNU General Public License v3.0 4 votes vote down vote up
public static Parcel deserialize(byte[] bytes) {
  Parcel parcel = Parcel.obtain();
  parcel.unmarshall(bytes, 0, bytes.length);
  parcel.setDataPosition(0);
  return parcel;
}
 
Example 18
Source File: AndroidLocationPlayServiceManager.java    From CodenameOne with GNU General Public License v2.0 4 votes vote down vote up
public static Parcel unmarshall(byte[] bytes) {
    Parcel parcel = Parcel.obtain();
    parcel.unmarshall(bytes, 0, bytes.length);
    parcel.setDataPosition(0); // This is extremely important!
    return parcel;
}
 
Example 19
Source File: ParcelUtil.java    From Silence with GNU General Public License v3.0 4 votes vote down vote up
public static Parcel deserialize(byte[] bytes) {
  Parcel parcel = Parcel.obtain();
  parcel.unmarshall(bytes, 0, bytes.length);
  parcel.setDataPosition(0);
  return parcel;
}
 
Example 20
Source File: ParcelUtil.java    From mollyim-android with GNU General Public License v3.0 4 votes vote down vote up
public static Parcel deserialize(byte[] bytes) {
  Parcel parcel = Parcel.obtain();
  parcel.unmarshall(bytes, 0, bytes.length);
  parcel.setDataPosition(0);
  return parcel;
}