下面列出了怎么用android.os.BadParcelableException的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* 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;
}
@Override
public void onReceive(Context c, Intent intent) {
try {
final Ticket t = intent.getParcelableExtra(EXTRA_TICKET);
if (t == null) {
DebugLog.w("Scheduled ticket is null");
} else {
if (INTENT_TICKET_ALARM_EXPIRING.equals(intent.getAction())) {
NotificationUtil.notifyTicket(c, t, Preferences.getBoolean(c, Preferences.KEEP_NOTIFICATIONS,
true));
DebugLog.i("Receiving alarm for expiring ticket " + t);
}
if (INTENT_TICKET_ALARM_EXPIRED.equals(intent.getAction())) {
NotificationUtil.notifyTicket(c, t, false);
DebugLog.i("Receiving alarm for expired ticket " + t);
}
}
} catch (BadParcelableException e) {
DebugLog.i("ticket was bought in the old version and alarm received in the new version, ignoring");
}
}
/**
* CertPath containing the public key used to encrypt {@code encryptedRecoveryKeyBlob}.
*/
public @NonNull CertPath getTrustedHardwareCertPath() {
try {
return mCertPath.getCertPath();
} catch (CertificateException e) {
// Rethrow an unchecked exception as it should not happen. If such an issue exists,
// an exception should have been thrown during service initialization.
throw new BadParcelableException(e);
}
}
Parcel readParcel(int validateToken, int level) {
if (DEBUG_PARCEL) Log.d(TAG, "readParcel: at " + mCurParcel.dataPosition()
+ ", avail=" + mCurParcel.dataAvail() + ", windows=" + mNumReadWindows
+ ", views=" + mNumReadViews + ", level=" + level);
int token = mCurParcel.readInt();
if (token != 0) {
if (token != validateToken) {
throw new BadParcelableException("Got token " + Integer.toHexString(token)
+ ", expected token " + Integer.toHexString(validateToken));
}
return mCurParcel;
}
// We have run out of partial data, need to read another batch.
mTransferToken = mCurParcel.readStrongBinder();
if (mTransferToken == null) {
throw new IllegalStateException(
"Reached end of partial data without transfer token");
}
if (DEBUG_PARCEL) Log.d(TAG, "Ran out of partial data at "
+ mCurParcel.dataPosition() + ", token " + mTransferToken);
fetchData();
if (DEBUG_PARCEL) Log.d(TAG, "Creating PooledStringReader @ "
+ mCurParcel.dataPosition());
mStringReader = new PooledStringReader(mCurParcel);
if (DEBUG_PARCEL) Log.d(TAG, "PooledStringReader size = "
+ mStringReader.getStringCount());
if (DEBUG_PARCEL) Log.d(TAG, "readParcel: at " + mCurParcel.dataPosition()
+ ", avail=" + mCurParcel.dataAvail() + ", windows=" + mNumReadWindows
+ ", views=" + mNumReadViews);
mCurParcel.readInt();
return mCurParcel;
}
static List<String> getExtras(Bundle data) {
List<String> result = new ArrayList<>();
if (data == null)
return result;
try {
Set<String> keys = data.keySet();
for (String key : keys) {
Object v = data.get(key);
Object value = v;
if (v != null && v.getClass().isArray()) {
int length = Array.getLength(v);
if (length <= 10) {
String[] elements = new String[length];
for (int i = 0; i < length; i++) {
Object element = Array.get(v, i);
if (element instanceof Long)
elements[i] = element.toString() + " (0x" + Long.toHexString((Long) element) + ")";
else
elements[i] = (element == null ? null : element.toString());
}
value = TextUtils.join(",", elements);
} else
value = "[" + length + "]";
} else if (v instanceof Long)
value = v.toString() + " (0x" + Long.toHexString((Long) v) + ")";
result.add(key + "=" + value + (value == null ? "" : " (" + v.getClass().getSimpleName() + ")"));
}
} catch (BadParcelableException ex) {
// android.os.BadParcelableException: ClassNotFoundException when unmarshalling: ...
Log.e(ex);
}
return result;
}
@Test
public void fromBytes_withNullIntent_throwsBadParcelableException() {
final byte[] intentBytes = toBytesUnchecked(null);
assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {
@Override
public void call() throws Throwable {
IntentUtil.fromBytes(intentBytes);
}
}).isInstanceOf(BadParcelableException.class);
}
@Test
public void fromBytes_withIntentNotPresent_throwsBadParcelableException() {
final byte[] intentBytes = new byte[10];
assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {
@Override
public void call() throws Throwable {
IntentUtil.fromBytes(intentBytes);
}
}).isInstanceOf(BadParcelableException.class);
}
public static <D extends Durable> void writeToParcel(Parcel parcel, D d) {
try {
parcel.writeByteArray(writeToArray(d));
} catch (IOException e) {
throw new BadParcelableException(e);
}
}
public static <D extends Durable> D readFromParcel(Parcel parcel, D d) {
try {
return readFromArray(parcel.createByteArray(), d);
} catch (IOException e) {
throw new BadParcelableException(e);
}
}
public static <D extends Durable> void writeToParcel(Parcel parcel, D d) {
try {
parcel.writeByteArray(writeToArray(d));
} catch (IOException e) {
throw new BadParcelableException(e);
}
}
public static <D extends Durable> D readFromParcel(Parcel parcel, D d) {
try {
return readFromArray(parcel.createByteArray(), d);
} catch (IOException e) {
throw new BadParcelableException(e);
}
}
public static <D extends Durable> void writeToParcel(Parcel parcel, D d) {
try {
parcel.writeByteArray(writeToArray(d));
} catch (IOException e) {
throw new BadParcelableException(e);
}
}
public static <D extends Durable> D readFromParcel(Parcel parcel, D d) {
try {
return readFromArray(parcel.createByteArray(), d);
} catch (IOException e) {
throw new BadParcelableException(e);
}
}
/**
* Sanitizes an intent. In case the intent cannot be unparcelled, all extras will be removed to
* make it safe to use.
* @return A safe to use version of this intent.
*/
public static Intent sanitizeIntent(final Intent incomingIntent) {
if (incomingIntent == null) return null;
try {
incomingIntent.getBooleanExtra("TriggerUnparcel", false);
return incomingIntent;
} catch (BadParcelableException e) {
Log.e(TAG, "Invalid incoming intent.", e);
return incomingIntent.replaceExtras((Bundle) null);
}
}
private RuntimeException asParcelableException(final Throwable e) {
if (e instanceof SecurityException
|| e instanceof BadParcelableException
|| e instanceof IllegalArgumentException
|| e instanceof NullPointerException
|| e instanceof IllegalStateException
|| e instanceof NetworkOnMainThreadException
|| e instanceof UnsupportedOperationException)
return (RuntimeException) e;
return new IllegalStateException(e);
}
/**
* Sanitizes an intent. In case the intent cannot be unparcelled, all extras will be removed to
* make it safe to use.
* @return A safe to use version of this intent.
*/
public static Intent sanitizeIntent(final Intent incomingIntent) {
if (incomingIntent == null) return null;
try {
incomingIntent.getBooleanExtra("TriggerUnparcel", false);
return incomingIntent;
} catch (BadParcelableException e) {
Log.e(TAG, "Invalid incoming intent.", e);
return incomingIntent.replaceExtras((Bundle) null);
}
}
private static boolean intentInvalid(Intent intent) {
if (intent == null) {
return true;
}
try {
// force unparcelling to check if we are missing classes to
// correctly process callout response
intent.hasExtra(INTENT_RESULT_VALUE);
} catch (BadParcelableException e) {
Log.w(TAG, "unable to unparcel intent: " + e.getMessage());
return true;
}
return false;
}
@SuppressLint("NewApi")
@Override
public void onCreate(final Bundle savedInstanceState) {
if (BuildConfig.DEBUG && Build.VERSION.SDK_INT >= 23 && PermissionChecker
.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) !=
PermissionChecker.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
}
try {
super.onCreate(savedInstanceState);
} catch (final BadParcelableException bpe) {
if (BuildConfig.DEBUG) Logger.log(bpe);
}
init();
}