package com.huawei.hms.common.internal.safeparcel; import android.content.Intent; import android.os.Bundle; import android.os.Parcel; import android.os.Parcelable; import com.huawei.hms.common.internal.Preconditions; import com.huawei.hms.common.util.Base64Utils; import java.util.ArrayList; import java.util.Iterator; import org.simpleframework.xml.core.FieldScanner; /* loaded from: classes2.dex */ public final class SafeParcelableSerializer { public static S deserializeFromBytes(byte[] bArr, Parcelable.Creator creator) { Preconditions.checkNotNull(creator); Parcel obtain = Parcel.obtain(); obtain.unmarshall(bArr, 0, bArr.length); obtain.setDataPosition(0); S createFromParcel = creator.createFromParcel(obtain); obtain.recycle(); return createFromParcel; } public static S deserializeFromIntentExtra(Intent intent, String str, Parcelable.Creator creator) { byte[] byteArrayExtra; if (intent == null || (byteArrayExtra = intent.getByteArrayExtra(str)) == null) { return null; } return (S) deserializeFromBytes(byteArrayExtra, creator); } public static S deserializeFromString(String str, Parcelable.Creator creator) { return (S) deserializeFromBytes(Base64Utils.decodeUrlSafe(str), creator); } public static ArrayList deserializeIterableFromBundle(Bundle bundle, String str, Parcelable.Creator creator) { ArrayList arrayList; if (bundle == null || (arrayList = (ArrayList) bundle.getSerializable(str)) == null) { return null; } FieldScanner fieldScanner = (ArrayList) new ArrayList(arrayList.size()); Iterator it = arrayList.iterator(); while (it.hasNext()) { fieldScanner.add(deserializeFromBytes((byte[]) it.next(), creator)); } return fieldScanner; } public static ArrayList deserializeIterableFromIntentExtra(Intent intent, String str, Parcelable.Creator creator) { ArrayList arrayList; if (intent == null || (arrayList = (ArrayList) intent.getSerializableExtra(str)) == null) { return null; } FieldScanner fieldScanner = (ArrayList) new ArrayList(arrayList.size()); Iterator it = arrayList.iterator(); while (it.hasNext()) { fieldScanner.add(deserializeFromBytes((byte[]) it.next(), creator)); } return fieldScanner; } public static void serializeIterableToBundle(Iterable iterable, Bundle bundle, String str) { if (iterable == null || bundle == null) { return; } ArrayList arrayList = new ArrayList(); Iterator it = iterable.iterator(); while (it.hasNext()) { arrayList.add(serializeToBytes(it.next())); } bundle.putSerializable(str, arrayList); } public static void serializeIterableToIntentExtra(Iterable iterable, Intent intent, String str) { if (iterable == null || intent == null) { return; } ArrayList arrayList = new ArrayList(); Iterator it = iterable.iterator(); while (it.hasNext()) { arrayList.add(serializeToBytes(it.next())); } intent.putExtra(str, arrayList); } public static byte[] serializeToBytes(S s) { Parcel obtain = Parcel.obtain(); if (s != null) { s.writeToParcel(obtain, 0); } byte[] marshall = obtain.marshall(); obtain.recycle(); return marshall; } public static void serializeToIntentExtra(S s, Intent intent, String str) { if (s == null || intent == null) { return; } intent.putExtra(str, serializeToBytes(s)); } public static String serializeToString(S s) { return Base64Utils.encodeUrlSafe(serializeToBytes(s)); } }