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

The following examples show how to use android.os.Parcel#unmarshall() . These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: Lucid-Browser   File: BundleManager.java    License: 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
@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
/**
 * 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
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 5
Source Project: braintree_android   File: PayPal.java    License: 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 6
Source Project: PUMA   File: LaunchApp.java    License: 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 Project: OpenYOLO-Android   File: IntentUtil.java    License: 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 8
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 9
/**
 * 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 10
/**
 * 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 11
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 12
Source Project: AndroidBase   File: ParcelFn.java    License: 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 13
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 14
Source Project: Intra   File: AppInfoTest.java    License: 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 15
Source Project: BusWear   File: WearBusTools.java    License: 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 16
Source Project: LLApp   File: CacheUtils.java    License: 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 17
public static Parcel deserialize(byte[] bytes) {
  Parcel parcel = Parcel.obtain();
  parcel.unmarshall(bytes, 0, bytes.length);
  parcel.setDataPosition(0);
  return parcel;
}
 
Example 18
Source Project: Silence   File: ParcelUtil.java    License: 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 19
public static Parcel deserialize(byte[] bytes) {
  Parcel parcel = Parcel.obtain();
  parcel.unmarshall(bytes, 0, bytes.length);
  parcel.setDataPosition(0);
  return parcel;
}
 
Example 20
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;
}