package com.facebook.share.internal; import android.content.Intent; import android.graphics.Bitmap; import android.net.Uri; import android.os.Bundle; import android.os.ParcelFileDescriptor; import android.util.Pair; import com.facebook.AccessToken; import com.facebook.CallbackManager; import com.facebook.FacebookCallback; import com.facebook.FacebookException; import com.facebook.FacebookGraphResponseException; import com.facebook.FacebookOperationCanceledException; import com.facebook.FacebookRequestError; import com.facebook.FacebookSdk; import com.facebook.GraphRequest; import com.facebook.GraphResponse; import com.facebook.HttpMethod; import com.facebook.appevents.InternalAppEventsLogger; import com.facebook.devicerequests.internal.DeviceRequestsHelper; import com.facebook.internal.AnalyticsEvents; import com.facebook.internal.AppCall; import com.facebook.internal.CallbackManagerImpl; import com.facebook.internal.NativeAppCallAttachmentStore; import com.facebook.internal.NativeProtocol; import com.facebook.internal.Utility; import com.facebook.share.Sharer; import com.facebook.share.model.CameraEffectTextures; import com.facebook.share.model.ShareCameraEffectContent; import com.facebook.share.model.ShareMedia; import com.facebook.share.model.ShareMediaContent; import com.facebook.share.model.SharePhoto; import com.facebook.share.model.SharePhotoContent; import com.facebook.share.model.ShareStoryContent; import com.facebook.share.model.ShareVideo; import com.facebook.share.model.ShareVideoContent; import com.google.android.gms.drive.DriveFile; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.UUID; import kotlin.collections.CollectionsKt__CollectionsJVMKt; import kotlin.collections.CollectionsKt__IterablesKt; import kotlin.jvm.internal.Intrinsics; import kotlin.text.StringsKt__StringsJVMKt; import kotlin.text.StringsKt__StringsKt; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; /* loaded from: classes2.dex */ public final class ShareInternalUtility { public static final ShareInternalUtility INSTANCE = new ShareInternalUtility(); public static final String MY_STAGING_RESOURCES = "me/staging_resources"; public static final String STAGING_PARAM = "file"; private ShareInternalUtility() { } public static final void invokeCallbackWithException(FacebookCallback facebookCallback, Exception exception) { Intrinsics.checkNotNullParameter(exception, "exception"); if (exception instanceof FacebookException) { invokeOnErrorCallback(facebookCallback, (FacebookException) exception); } else { invokeCallbackWithError(facebookCallback, Intrinsics.stringPlus("Error preparing share content: ", exception.getLocalizedMessage())); } } public static final void invokeCallbackWithError(FacebookCallback facebookCallback, String str) { invokeOnErrorCallback(facebookCallback, str); } public static final void invokeCallbackWithResults(FacebookCallback facebookCallback, String str, GraphResponse graphResponse) { Intrinsics.checkNotNullParameter(graphResponse, "graphResponse"); FacebookRequestError error = graphResponse.getError(); if (error != null) { String errorMessage = error.getErrorMessage(); if (Utility.isNullOrEmpty(errorMessage)) { errorMessage = "Unexpected error sharing."; } invokeOnErrorCallback(facebookCallback, graphResponse, errorMessage); return; } invokeOnSuccessCallback(facebookCallback, str); } public static final String getNativeDialogCompletionGesture(Bundle result) { Intrinsics.checkNotNullParameter(result, "result"); if (result.containsKey(NativeProtocol.RESULT_ARGS_DIALOG_COMPLETION_GESTURE_KEY)) { return result.getString(NativeProtocol.RESULT_ARGS_DIALOG_COMPLETION_GESTURE_KEY); } return result.getString(NativeProtocol.EXTRA_DIALOG_COMPLETION_GESTURE_KEY); } public static final String getShareDialogPostId(Bundle result) { Intrinsics.checkNotNullParameter(result, "result"); if (result.containsKey(ShareConstants.RESULT_POST_ID)) { return result.getString(ShareConstants.RESULT_POST_ID); } if (result.containsKey(ShareConstants.EXTRA_RESULT_POST_ID)) { return result.getString(ShareConstants.EXTRA_RESULT_POST_ID); } return result.getString(ShareConstants.WEB_DIALOG_RESULT_PARAM_POST_ID); } public static final boolean handleActivityResult(int i, int i2, Intent intent, ResultProcessor resultProcessor) { AppCall appCallFromActivityResult = INSTANCE.getAppCallFromActivityResult(i, i2, intent); if (appCallFromActivityResult == null) { return false; } NativeAppCallAttachmentStore nativeAppCallAttachmentStore = NativeAppCallAttachmentStore.INSTANCE; NativeAppCallAttachmentStore.cleanupAttachmentsForCall(appCallFromActivityResult.getCallId()); if (resultProcessor == null) { return true; } FacebookException exceptionFromErrorData = intent != null ? NativeProtocol.getExceptionFromErrorData(NativeProtocol.getErrorDataFromResultIntent(intent)) : null; if (exceptionFromErrorData != null) { if (exceptionFromErrorData instanceof FacebookOperationCanceledException) { resultProcessor.onCancel(appCallFromActivityResult); } else { resultProcessor.onError(appCallFromActivityResult, exceptionFromErrorData); } } else { resultProcessor.onSuccess(appCallFromActivityResult, intent != null ? NativeProtocol.getSuccessResultsFromIntent(intent) : null); } return true; } public static final ResultProcessor getShareResultProcessor(final FacebookCallback facebookCallback) { return new ResultProcessor(facebookCallback) { // from class: com.facebook.share.internal.ShareInternalUtility$getShareResultProcessor$1 final /* synthetic */ FacebookCallback $callback; /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */ { super(facebookCallback); this.$callback = facebookCallback; } @Override // com.facebook.share.internal.ResultProcessor public void onSuccess(AppCall appCall, Bundle bundle) { boolean equals; boolean equals2; Intrinsics.checkNotNullParameter(appCall, "appCall"); if (bundle != null) { String nativeDialogCompletionGesture = ShareInternalUtility.getNativeDialogCompletionGesture(bundle); if (nativeDialogCompletionGesture != null) { equals = StringsKt__StringsJVMKt.equals("post", nativeDialogCompletionGesture, true); if (!equals) { equals2 = StringsKt__StringsJVMKt.equals("cancel", nativeDialogCompletionGesture, true); if (equals2) { ShareInternalUtility.invokeOnCancelCallback(this.$callback); return; } else { ShareInternalUtility.invokeOnErrorCallback(this.$callback, new FacebookException(NativeProtocol.ERROR_UNKNOWN_ERROR)); return; } } } ShareInternalUtility.invokeOnSuccessCallback(this.$callback, ShareInternalUtility.getShareDialogPostId(bundle)); } } @Override // com.facebook.share.internal.ResultProcessor public void onCancel(AppCall appCall) { Intrinsics.checkNotNullParameter(appCall, "appCall"); ShareInternalUtility shareInternalUtility = ShareInternalUtility.INSTANCE; ShareInternalUtility.invokeOnCancelCallback(this.$callback); } @Override // com.facebook.share.internal.ResultProcessor public void onError(AppCall appCall, FacebookException error) { Intrinsics.checkNotNullParameter(appCall, "appCall"); Intrinsics.checkNotNullParameter(error, "error"); ShareInternalUtility shareInternalUtility = ShareInternalUtility.INSTANCE; ShareInternalUtility.invokeOnErrorCallback(this.$callback, error); } }; } private final AppCall getAppCallFromActivityResult(int i, int i2, Intent intent) { UUID callIdFromIntent = NativeProtocol.getCallIdFromIntent(intent); if (callIdFromIntent == null) { return null; } return AppCall.Companion.finishPendingCall(callIdFromIntent, i); } public static final void registerStaticShareCallback(final int i) { CallbackManagerImpl.Companion.registerStaticCallback(i, new CallbackManagerImpl.Callback() { // from class: com.facebook.share.internal.ShareInternalUtility$$ExternalSyntheticLambda0 @Override // com.facebook.internal.CallbackManagerImpl.Callback public final boolean onActivityResult(int i2, Intent intent) { boolean m644registerStaticShareCallback$lambda0; m644registerStaticShareCallback$lambda0 = ShareInternalUtility.m644registerStaticShareCallback$lambda0(i, i2, intent); return m644registerStaticShareCallback$lambda0; } }); } /* JADX INFO: Access modifiers changed from: private */ /* renamed from: registerStaticShareCallback$lambda-0, reason: not valid java name */ public static final boolean m644registerStaticShareCallback$lambda0(int i, int i2, Intent intent) { return handleActivityResult(i, i2, intent, getShareResultProcessor(null)); } public static final void registerSharerCallback(final int i, CallbackManager callbackManager, final FacebookCallback facebookCallback) { if (!(callbackManager instanceof CallbackManagerImpl)) { throw new FacebookException("Unexpected CallbackManager, please use the provided Factory."); } ((CallbackManagerImpl) callbackManager).registerCallback(i, new CallbackManagerImpl.Callback() { // from class: com.facebook.share.internal.ShareInternalUtility$$ExternalSyntheticLambda1 @Override // com.facebook.internal.CallbackManagerImpl.Callback public final boolean onActivityResult(int i2, Intent intent) { boolean m643registerSharerCallback$lambda1; m643registerSharerCallback$lambda1 = ShareInternalUtility.m643registerSharerCallback$lambda1(i, facebookCallback, i2, intent); return m643registerSharerCallback$lambda1; } }); } /* JADX INFO: Access modifiers changed from: private */ /* renamed from: registerSharerCallback$lambda-1, reason: not valid java name */ public static final boolean m643registerSharerCallback$lambda1(int i, FacebookCallback facebookCallback, int i2, Intent intent) { return handleActivityResult(i, i2, intent, getShareResultProcessor(facebookCallback)); } public static final List getPhotoUrls(SharePhotoContent sharePhotoContent, UUID appCallId) { Intrinsics.checkNotNullParameter(appCallId, "appCallId"); List photos = sharePhotoContent == null ? null : sharePhotoContent.getPhotos(); if (photos == null) { return null; } ArrayList arrayList = new ArrayList(); Iterator it = photos.iterator(); while (it.hasNext()) { NativeAppCallAttachmentStore.Attachment attachment = INSTANCE.getAttachment(appCallId, (SharePhoto) it.next()); if (attachment != null) { arrayList.add(attachment); } } ArrayList arrayList2 = new ArrayList(CollectionsKt__IterablesKt.collectionSizeOrDefault(arrayList, 10)); Iterator it2 = arrayList.iterator(); while (it2.hasNext()) { arrayList2.add(((NativeAppCallAttachmentStore.Attachment) it2.next()).getAttachmentUrl()); } NativeAppCallAttachmentStore.addAttachments(arrayList); return arrayList2; } public static final String getVideoUrl(ShareVideoContent shareVideoContent, UUID appCallId) { ShareVideo video; Intrinsics.checkNotNullParameter(appCallId, "appCallId"); Uri localUrl = (shareVideoContent == null || (video = shareVideoContent.getVideo()) == null) ? null : video.getLocalUrl(); if (localUrl == null) { return null; } NativeAppCallAttachmentStore.Attachment createAttachment = NativeAppCallAttachmentStore.createAttachment(appCallId, localUrl); NativeAppCallAttachmentStore.addAttachments(CollectionsKt__CollectionsJVMKt.listOf(createAttachment)); return createAttachment.getAttachmentUrl(); } public static final List getMediaInfos(ShareMediaContent shareMediaContent, UUID appCallId) { Bundle bundle; Intrinsics.checkNotNullParameter(appCallId, "appCallId"); List> media = shareMediaContent == null ? null : shareMediaContent.getMedia(); if (media == null) { return null; } ArrayList arrayList = new ArrayList(); ArrayList arrayList2 = new ArrayList(); for (ShareMedia shareMedia : media) { NativeAppCallAttachmentStore.Attachment attachment = INSTANCE.getAttachment(appCallId, shareMedia); if (attachment == null) { bundle = null; } else { arrayList.add(attachment); bundle = new Bundle(); bundle.putString("type", shareMedia.getMediaType().name()); bundle.putString(ShareConstants.MEDIA_URI, attachment.getAttachmentUrl()); } if (bundle != null) { arrayList2.add(bundle); } } NativeAppCallAttachmentStore.addAttachments(arrayList); return arrayList2; } public static final Bundle getTextureUrlBundle(ShareCameraEffectContent shareCameraEffectContent, UUID appCallId) { Intrinsics.checkNotNullParameter(appCallId, "appCallId"); CameraEffectTextures textures = shareCameraEffectContent == null ? null : shareCameraEffectContent.getTextures(); if (textures == null) { return null; } Bundle bundle = new Bundle(); ArrayList arrayList = new ArrayList(); for (String str : textures.keySet()) { NativeAppCallAttachmentStore.Attachment attachment = INSTANCE.getAttachment(appCallId, textures.getTextureUri(str), textures.getTextureBitmap(str)); if (attachment != null) { arrayList.add(attachment); bundle.putString(str, attachment.getAttachmentUrl()); } } NativeAppCallAttachmentStore.addAttachments(arrayList); return bundle; } public static final JSONArray removeNamespacesFromOGJsonArray(JSONArray jsonArray, boolean z) throws JSONException { Intrinsics.checkNotNullParameter(jsonArray, "jsonArray"); JSONArray jSONArray = new JSONArray(); int length = jsonArray.length(); if (length > 0) { int i = 0; while (true) { int i2 = i + 1; Object obj = jsonArray.get(i); if (obj instanceof JSONArray) { obj = removeNamespacesFromOGJsonArray((JSONArray) obj, z); } else if (obj instanceof JSONObject) { obj = removeNamespacesFromOGJsonObject((JSONObject) obj, z); } jSONArray.put(obj); if (i2 >= length) { break; } i = i2; } } return jSONArray; } public static final JSONObject removeNamespacesFromOGJsonObject(JSONObject jSONObject, boolean z) { if (jSONObject == null) { return null; } try { JSONObject jSONObject2 = new JSONObject(); JSONObject jSONObject3 = new JSONObject(); JSONArray names = jSONObject.names(); if (names == null) { return null; } int length = names.length(); if (length > 0) { int i = 0; while (true) { int i2 = i + 1; String key = names.getString(i); Object obj = jSONObject.get(key); if (obj instanceof JSONObject) { obj = removeNamespacesFromOGJsonObject((JSONObject) obj, true); } else if (obj instanceof JSONArray) { obj = removeNamespacesFromOGJsonArray((JSONArray) obj, true); } Intrinsics.checkNotNullExpressionValue(key, "key"); Pair fieldNameAndNamespaceFromFullName = getFieldNameAndNamespaceFromFullName(key); String str = (String) fieldNameAndNamespaceFromFullName.first; String str2 = (String) fieldNameAndNamespaceFromFullName.second; if (z) { if (str != null && Intrinsics.areEqual(str, DeviceRequestsHelper.SDK_HEADER)) { jSONObject2.put(key, obj); } else { if (str != null && !Intrinsics.areEqual(str, "og")) { jSONObject3.put(str2, obj); } jSONObject2.put(str2, obj); } } else if (str != null && Intrinsics.areEqual(str, "fb")) { jSONObject2.put(key, obj); } else { jSONObject2.put(str2, obj); } if (i2 >= length) { break; } i = i2; } } if (jSONObject3.length() > 0) { jSONObject2.put("data", jSONObject3); } return jSONObject2; } catch (JSONException unused) { throw new FacebookException("Failed to create json object from share content"); } } public static final Pair getFieldNameAndNamespaceFromFullName(String fullName) { String str; int i; Intrinsics.checkNotNullParameter(fullName, "fullName"); int indexOf$default = StringsKt__StringsKt.indexOf$default((CharSequence) fullName, ':', 0, false, 6, (Object) null); if (indexOf$default == -1 || fullName.length() <= (i = indexOf$default + 1)) { str = null; } else { str = fullName.substring(0, indexOf$default); Intrinsics.checkNotNullExpressionValue(str, "(this as java.lang.Strin…ing(startIndex, endIndex)"); fullName = fullName.substring(i); Intrinsics.checkNotNullExpressionValue(fullName, "(this as java.lang.String).substring(startIndex)"); } return new Pair<>(str, fullName); } private final NativeAppCallAttachmentStore.Attachment getAttachment(UUID uuid, ShareMedia shareMedia) { Uri uri; Bitmap bitmap; if (shareMedia instanceof SharePhoto) { SharePhoto sharePhoto = (SharePhoto) shareMedia; bitmap = sharePhoto.getBitmap(); uri = sharePhoto.getImageUrl(); } else if (shareMedia instanceof ShareVideo) { uri = ((ShareVideo) shareMedia).getLocalUrl(); bitmap = null; } else { uri = null; bitmap = null; } return getAttachment(uuid, uri, bitmap); } private final NativeAppCallAttachmentStore.Attachment getAttachment(UUID uuid, Uri uri, Bitmap bitmap) { if (bitmap != null) { return NativeAppCallAttachmentStore.createAttachment(uuid, bitmap); } if (uri != null) { return NativeAppCallAttachmentStore.createAttachment(uuid, uri); } return null; } public static final void invokeOnCancelCallback(FacebookCallback facebookCallback) { INSTANCE.logShareResult(AnalyticsEvents.PARAMETER_SHARE_OUTCOME_CANCELLED, null); if (facebookCallback == null) { return; } facebookCallback.onCancel(); } public static final void invokeOnSuccessCallback(FacebookCallback facebookCallback, String str) { INSTANCE.logShareResult(AnalyticsEvents.PARAMETER_SHARE_OUTCOME_SUCCEEDED, null); if (facebookCallback == null) { return; } facebookCallback.onSuccess(new Sharer.Result(str)); } public static final void invokeOnErrorCallback(FacebookCallback facebookCallback, GraphResponse graphResponse, String str) { INSTANCE.logShareResult("error", str); if (facebookCallback == null) { return; } facebookCallback.onError(new FacebookGraphResponseException(graphResponse, str)); } public static final void invokeOnErrorCallback(FacebookCallback facebookCallback, String str) { INSTANCE.logShareResult("error", str); if (facebookCallback == null) { return; } facebookCallback.onError(new FacebookException(str)); } public static final void invokeOnErrorCallback(FacebookCallback facebookCallback, FacebookException ex) { Intrinsics.checkNotNullParameter(ex, "ex"); INSTANCE.logShareResult("error", ex.getMessage()); if (facebookCallback == null) { return; } facebookCallback.onError(ex); } private final void logShareResult(String str, String str2) { InternalAppEventsLogger internalAppEventsLogger = new InternalAppEventsLogger(FacebookSdk.getApplicationContext()); Bundle bundle = new Bundle(); bundle.putString(AnalyticsEvents.PARAMETER_SHARE_OUTCOME, str); if (str2 != null) { bundle.putString("error_message", str2); } internalAppEventsLogger.logEventImplicitly(AnalyticsEvents.EVENT_SHARE_RESULT, bundle); } public static final GraphRequest newUploadStagingResourceWithImageRequest(AccessToken accessToken, Bitmap bitmap, GraphRequest.Callback callback) { Bundle bundle = new Bundle(1); bundle.putParcelable("file", bitmap); return new GraphRequest(accessToken, MY_STAGING_RESOURCES, bundle, HttpMethod.POST, callback, null, 32, null); } public static final GraphRequest newUploadStagingResourceWithImageRequest(AccessToken accessToken, File file, GraphRequest.Callback callback) throws FileNotFoundException { GraphRequest.ParcelableResourceWithMimeType parcelableResourceWithMimeType = new GraphRequest.ParcelableResourceWithMimeType(ParcelFileDescriptor.open(file, DriveFile.MODE_READ_ONLY), "image/png"); Bundle bundle = new Bundle(1); bundle.putParcelable("file", parcelableResourceWithMimeType); return new GraphRequest(accessToken, MY_STAGING_RESOURCES, bundle, HttpMethod.POST, callback, null, 32, null); } public static final GraphRequest newUploadStagingResourceWithImageRequest(AccessToken accessToken, Uri imageUri, GraphRequest.Callback callback) throws FileNotFoundException { Intrinsics.checkNotNullParameter(imageUri, "imageUri"); String path = imageUri.getPath(); if (Utility.isFileUri(imageUri) && path != null) { return newUploadStagingResourceWithImageRequest(accessToken, new File(path), callback); } if (!Utility.isContentUri(imageUri)) { throw new FacebookException("The image Uri must be either a file:// or content:// Uri"); } GraphRequest.ParcelableResourceWithMimeType parcelableResourceWithMimeType = new GraphRequest.ParcelableResourceWithMimeType(imageUri, "image/png"); Bundle bundle = new Bundle(1); bundle.putParcelable("file", parcelableResourceWithMimeType); return new GraphRequest(accessToken, MY_STAGING_RESOURCES, bundle, HttpMethod.POST, callback, null, 32, null); } public static final Bundle getStickerUrl(ShareStoryContent shareStoryContent, UUID appCallId) { Intrinsics.checkNotNullParameter(appCallId, "appCallId"); if (shareStoryContent == null || shareStoryContent.getStickerAsset() == null) { return null; } new ArrayList().add(shareStoryContent.getStickerAsset()); NativeAppCallAttachmentStore.Attachment attachment = INSTANCE.getAttachment(appCallId, shareStoryContent.getStickerAsset()); if (attachment == null) { return null; } Bundle bundle = new Bundle(); bundle.putString(ShareConstants.MEDIA_URI, attachment.getAttachmentUrl()); String uriExtension = getUriExtension(attachment.getOriginalUri()); if (uriExtension != null) { Utility.putNonEmptyString(bundle, ShareConstants.MEDIA_EXTENSION, uriExtension); } NativeAppCallAttachmentStore nativeAppCallAttachmentStore = NativeAppCallAttachmentStore.INSTANCE; NativeAppCallAttachmentStore.addAttachments(CollectionsKt__CollectionsJVMKt.listOf(attachment)); return bundle; } public static final Bundle getBackgroundAssetMediaInfo(ShareStoryContent shareStoryContent, UUID appCallId) { Intrinsics.checkNotNullParameter(appCallId, "appCallId"); Bundle bundle = null; if (shareStoryContent != null && shareStoryContent.getBackgroundAsset() != null) { ShareMedia backgroundAsset = shareStoryContent.getBackgroundAsset(); NativeAppCallAttachmentStore.Attachment attachment = INSTANCE.getAttachment(appCallId, backgroundAsset); if (attachment == null) { return null; } bundle = new Bundle(); bundle.putString("type", backgroundAsset.getMediaType().name()); bundle.putString(ShareConstants.MEDIA_URI, attachment.getAttachmentUrl()); String uriExtension = getUriExtension(attachment.getOriginalUri()); if (uriExtension != null) { Utility.putNonEmptyString(bundle, ShareConstants.MEDIA_EXTENSION, uriExtension); } NativeAppCallAttachmentStore nativeAppCallAttachmentStore = NativeAppCallAttachmentStore.INSTANCE; NativeAppCallAttachmentStore.addAttachments(CollectionsKt__CollectionsJVMKt.listOf(attachment)); } return bundle; } public static final String getUriExtension(Uri uri) { if (uri == null) { return null; } String uri2 = uri.toString(); Intrinsics.checkNotNullExpressionValue(uri2, "uri.toString()"); int lastIndexOf$default = StringsKt__StringsKt.lastIndexOf$default((CharSequence) uri2, '.', 0, false, 6, (Object) null); if (lastIndexOf$default == -1) { return null; } String substring = uri2.substring(lastIndexOf$default); Intrinsics.checkNotNullExpressionValue(substring, "(this as java.lang.String).substring(startIndex)"); return substring; } }