package com.facebook.share.internal;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v4.content.d;
import com.facebook.AccessToken;
import com.facebook.AccessTokenTracker;
import com.facebook.FacebookException;
import com.facebook.FacebookRequestError;
import com.facebook.FacebookSdk;
import com.facebook.GraphRequest;
import com.facebook.GraphRequestBatch;
import com.facebook.GraphRequestBatch$Callback;
import com.facebook.GraphResponse;
import com.facebook.HttpMethod;
import com.facebook.LoggingBehavior;
import com.facebook.appevents.AppEventsLogger;
import com.facebook.internal.BundleJSONConverter;
import com.facebook.internal.CallbackManagerImpl;
import com.facebook.internal.CallbackManagerImpl.RequestCodeOffset;
import com.facebook.internal.FileLruCache;
import com.facebook.internal.FileLruCache.Limits;
import com.facebook.internal.FragmentWrapper;
import com.facebook.internal.Logger;
import com.facebook.internal.Utility;
import com.facebook.internal.WorkQueue;
import com.facebook.share.internal.LikeContent.Builder;
import com.facebook.share.widget.LikeView.ObjectType;
import com.tencent.matrix.trace.core.AppMethodBeat;
import com.tencent.sqlitelint.config.SharePluginInfo;
import com.tencent.ytcommon.BuildConfig;
import java.io.Closeable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Locale;
import java.util.concurrent.ConcurrentHashMap;
import org.json.JSONException;
import org.json.JSONObject;

@Deprecated
public class LikeActionController {
    @Deprecated
    public static final String ACTION_LIKE_ACTION_CONTROLLER_DID_ERROR = "com.facebook.sdk.LikeActionController.DID_ERROR";
    @Deprecated
    public static final String ACTION_LIKE_ACTION_CONTROLLER_DID_RESET = "com.facebook.sdk.LikeActionController.DID_RESET";
    @Deprecated
    public static final String ACTION_LIKE_ACTION_CONTROLLER_UPDATED = "com.facebook.sdk.LikeActionController.UPDATED";
    @Deprecated
    public static final String ACTION_OBJECT_ID_KEY = "com.facebook.sdk.LikeActionController.OBJECT_ID";
    private static final int ERROR_CODE_OBJECT_ALREADY_LIKED = 3501;
    @Deprecated
    public static final String ERROR_INVALID_OBJECT_ID = "Invalid Object Id";
    @Deprecated
    public static final String ERROR_PUBLISH_ERROR = "Unable to publish the like/unlike action";
    private static final String JSON_BOOL_IS_OBJECT_LIKED_KEY = "is_object_liked";
    private static final String JSON_BUNDLE_FACEBOOK_DIALOG_ANALYTICS_BUNDLE = "facebook_dialog_analytics_bundle";
    private static final String JSON_INT_OBJECT_TYPE_KEY = "object_type";
    private static final String JSON_INT_VERSION_KEY = "com.facebook.share.internal.LikeActionController.version";
    private static final String JSON_STRING_LIKE_COUNT_WITHOUT_LIKE_KEY = "like_count_string_without_like";
    private static final String JSON_STRING_LIKE_COUNT_WITH_LIKE_KEY = "like_count_string_with_like";
    private static final String JSON_STRING_OBJECT_ID_KEY = "object_id";
    private static final String JSON_STRING_SOCIAL_SENTENCE_WITHOUT_LIKE_KEY = "social_sentence_without_like";
    private static final String JSON_STRING_SOCIAL_SENTENCE_WITH_LIKE_KEY = "social_sentence_with_like";
    private static final String JSON_STRING_UNLIKE_TOKEN_KEY = "unlike_token";
    private static final String LIKE_ACTION_CONTROLLER_STORE = "com.facebook.LikeActionController.CONTROLLER_STORE_KEY";
    private static final String LIKE_ACTION_CONTROLLER_STORE_OBJECT_SUFFIX_KEY = "OBJECT_SUFFIX";
    private static final String LIKE_ACTION_CONTROLLER_STORE_PENDING_OBJECT_ID_KEY = "PENDING_CONTROLLER_KEY";
    private static final int LIKE_ACTION_CONTROLLER_VERSION = 3;
    private static final String LIKE_DIALOG_RESPONSE_LIKE_COUNT_STRING_KEY = "like_count_string";
    private static final String LIKE_DIALOG_RESPONSE_OBJECT_IS_LIKED_KEY = "object_is_liked";
    private static final String LIKE_DIALOG_RESPONSE_SOCIAL_SENTENCE_KEY = "social_sentence";
    private static final String LIKE_DIALOG_RESPONSE_UNLIKE_TOKEN_KEY = "unlike_token";
    private static final int MAX_CACHE_SIZE = 128;
    private static final int MAX_OBJECT_SUFFIX = 1000;
    private static final String TAG = LikeActionController.class.getSimpleName();
    private static AccessTokenTracker accessTokenTracker;
    private static final ConcurrentHashMap<String, LikeActionController> cache = new ConcurrentHashMap();
    private static FileLruCache controllerDiskCache;
    private static WorkQueue diskIOWorkQueue = new WorkQueue(1);
    private static Handler handler;
    private static boolean isInitialized;
    private static WorkQueue mruCacheWorkQueue = new WorkQueue(1);
    private static String objectIdForPendingController;
    private static volatile int objectSuffix;
    private AppEventsLogger appEventsLogger;
    private Bundle facebookDialogAnalyticsBundle;
    private boolean isObjectLiked;
    private boolean isObjectLikedOnServer;
    private boolean isPendingLikeOrUnlike;
    private String likeCountStringWithLike;
    private String likeCountStringWithoutLike;
    private String objectId;
    private boolean objectIsPage;
    private ObjectType objectType;
    private String socialSentenceWithLike;
    private String socialSentenceWithoutLike;
    private String unlikeToken;
    private String verifiedObjectId;

    class GetPageIdRequestWrapper extends AbstractRequestWrapper {
        boolean objectIsPage;
        String verifiedObjectId;

        GetPageIdRequestWrapper(String str, ObjectType objectType) {
            super(LikeActionController.this, str, objectType);
            AppMethodBeat.i(97007);
            Bundle bundle = new Bundle();
            bundle.putString("fields", SharePluginInfo.ISSUE_KEY_ID);
            bundle.putString("ids", str);
            setRequest(new GraphRequest(AccessToken.getCurrentAccessToken(), BuildConfig.FLAVOR, bundle, HttpMethod.GET));
            AppMethodBeat.o(97007);
        }

        /* Access modifiers changed, original: protected */
        public void processSuccess(GraphResponse graphResponse) {
            AppMethodBeat.i(97008);
            JSONObject tryGetJSONObjectFromResponse = Utility.tryGetJSONObjectFromResponse(graphResponse.getJSONObject(), this.objectId);
            if (tryGetJSONObjectFromResponse != null) {
                this.verifiedObjectId = tryGetJSONObjectFromResponse.optString(SharePluginInfo.ISSUE_KEY_ID);
                this.objectIsPage = !Utility.isNullOrEmpty(this.verifiedObjectId);
            }
            AppMethodBeat.o(97008);
        }

        /* Access modifiers changed, original: protected */
        public void processError(FacebookRequestError facebookRequestError) {
            AppMethodBeat.i(97009);
            Logger.log(LoggingBehavior.REQUESTS, LikeActionController.TAG, "Error getting the FB id for object '%s' with type '%s' : %s", new Object[]{this.objectId, this.objectType, facebookRequestError});
            AppMethodBeat.o(97009);
        }
    }

    static class MRUCacheWorkItem implements Runnable {
        private static ArrayList<String> mruCachedItems = new ArrayList();
        private String cacheItem;
        private boolean shouldTrim;

        static {
            AppMethodBeat.i(97014);
            AppMethodBeat.o(97014);
        }

        MRUCacheWorkItem(String str, boolean z) {
            this.cacheItem = str;
            this.shouldTrim = z;
        }

        public void run() {
            AppMethodBeat.i(97013);
            if (this.cacheItem != null) {
                mruCachedItems.remove(this.cacheItem);
                mruCachedItems.add(0, this.cacheItem);
            }
            if (this.shouldTrim && mruCachedItems.size() >= LikeActionController.MAX_CACHE_SIZE) {
                while (64 < mruCachedItems.size()) {
                    ArrayList arrayList = mruCachedItems;
                    LikeActionController.cache.remove((String) arrayList.remove(arrayList.size() - 1));
                }
            }
            AppMethodBeat.o(97013);
        }
    }

    static class SerializeToDiskWorkItem implements Runnable {
        private String cacheKey;
        private String controllerJson;

        SerializeToDiskWorkItem(String str, String str2) {
            this.cacheKey = str;
            this.controllerJson = str2;
        }

        public void run() {
            AppMethodBeat.i(97020);
            LikeActionController.access$2600(this.cacheKey, this.controllerJson);
            AppMethodBeat.o(97020);
        }
    }

    static /* synthetic */ void access$000(LikeActionController likeActionController, int i, int i2, Intent intent) {
        AppMethodBeat.i(97059);
        likeActionController.onActivityResult(i, i2, intent);
        AppMethodBeat.o(97059);
    }

    static /* synthetic */ AppEventsLogger access$1200(LikeActionController likeActionController) {
        AppMethodBeat.i(97062);
        AppEventsLogger appEventsLogger = likeActionController.getAppEventsLogger();
        AppMethodBeat.o(97062);
        return appEventsLogger;
    }

    static /* synthetic */ void access$1300(LikeActionController likeActionController, boolean z, String str, String str2, String str3, String str4, String str5) {
        AppMethodBeat.i(97063);
        likeActionController.updateState(z, str, str2, str3, str4, str5);
        AppMethodBeat.o(97063);
    }

    static /* synthetic */ void access$1400(LikeActionController likeActionController, String str, Bundle bundle) {
        AppMethodBeat.i(97064);
        likeActionController.logAppEventForError(str, bundle);
        AppMethodBeat.o(97064);
    }

    static /* synthetic */ void access$1500(LikeActionController likeActionController, String str, Bundle bundle) {
        AppMethodBeat.i(97065);
        broadcastAction(likeActionController, str, bundle);
        AppMethodBeat.o(97065);
    }

    static /* synthetic */ void access$1900(LikeActionController likeActionController, boolean z) {
        AppMethodBeat.i(97066);
        likeActionController.publishDidError(z);
        AppMethodBeat.o(97066);
    }

    static /* synthetic */ void access$200(LikeActionController likeActionController) {
        AppMethodBeat.i(97060);
        likeActionController.refreshStatusAsync();
        AppMethodBeat.o(97060);
    }

    static /* synthetic */ void access$2100(LikeActionController likeActionController, Bundle bundle) {
        AppMethodBeat.i(97067);
        likeActionController.publishAgainIfNeeded(bundle);
        AppMethodBeat.o(97067);
    }

    static /* synthetic */ void access$2400(LikeActionController likeActionController, String str, FacebookRequestError facebookRequestError) {
        AppMethodBeat.i(97068);
        likeActionController.logAppEventForError(str, facebookRequestError);
        AppMethodBeat.o(97068);
    }

    static /* synthetic */ void access$2600(String str, String str2) {
        AppMethodBeat.i(97069);
        serializeToDiskSynchronously(str, str2);
        AppMethodBeat.o(97069);
    }

    static /* synthetic */ void access$2700(String str, ObjectType objectType, CreationCallback creationCallback) {
        AppMethodBeat.i(97070);
        createControllerForObjectIdAndType(str, objectType, creationCallback);
        AppMethodBeat.o(97070);
    }

    static /* synthetic */ void access$600(LikeActionController likeActionController, String str) {
        AppMethodBeat.i(97061);
        broadcastAction(likeActionController, str);
        AppMethodBeat.o(97061);
    }

    static {
        AppMethodBeat.i(97071);
        AppMethodBeat.o(97071);
    }

    @Deprecated
    public static boolean handleOnActivityResult(final int i, final int i2, final Intent intent) {
        AppMethodBeat.i(97021);
        if (Utility.isNullOrEmpty(objectIdForPendingController)) {
            objectIdForPendingController = FacebookSdk.getApplicationContext().getSharedPreferences(LIKE_ACTION_CONTROLLER_STORE, 0).getString(LIKE_ACTION_CONTROLLER_STORE_PENDING_OBJECT_ID_KEY, null);
        }
        if (Utility.isNullOrEmpty(objectIdForPendingController)) {
            AppMethodBeat.o(97021);
            return false;
        }
        getControllerForObjectId(objectIdForPendingController, ObjectType.UNKNOWN, new CreationCallback() {
            public final void onComplete(LikeActionController likeActionController, FacebookException facebookException) {
                AppMethodBeat.i(96980);
                if (facebookException == null) {
                    LikeActionController.access$000(likeActionController, i, i2, intent);
                    AppMethodBeat.o(96980);
                    return;
                }
                Utility.logd(LikeActionController.TAG, facebookException);
                AppMethodBeat.o(96980);
            }
        });
        AppMethodBeat.o(97021);
        return true;
    }

    @Deprecated
    public static void getControllerForObjectId(String str, ObjectType objectType, CreationCallback creationCallback) {
        AppMethodBeat.i(97022);
        if (!isInitialized) {
            performFirstInitialize();
        }
        LikeActionController controllerFromInMemoryCache = getControllerFromInMemoryCache(str);
        if (controllerFromInMemoryCache != null) {
            verifyControllerAndInvokeCallback(controllerFromInMemoryCache, objectType, creationCallback);
            AppMethodBeat.o(97022);
            return;
        }
        diskIOWorkQueue.addActiveWorkItem(new CreateLikeActionControllerWorkItem(str, objectType, creationCallback));
        AppMethodBeat.o(97022);
    }

    private static void verifyControllerAndInvokeCallback(LikeActionController likeActionController, ObjectType objectType, CreationCallback creationCallback) {
        FacebookException facebookException;
        AppMethodBeat.i(97023);
        ObjectType mostSpecificObjectType = ShareInternalUtility.getMostSpecificObjectType(objectType, likeActionController.objectType);
        if (mostSpecificObjectType == null) {
            facebookException = new FacebookException("Object with id:\"%s\" is already marked as type:\"%s\". Cannot change the type to:\"%s\"", new Object[]{likeActionController.objectId, likeActionController.objectType.toString(), objectType.toString()});
            likeActionController = null;
        } else {
            likeActionController.objectType = mostSpecificObjectType;
            facebookException = null;
        }
        invokeCallbackWithController(creationCallback, likeActionController, facebookException);
        AppMethodBeat.o(97023);
    }

    private static void createControllerForObjectIdAndType(String str, ObjectType objectType, CreationCallback creationCallback) {
        AppMethodBeat.i(97024);
        LikeActionController controllerFromInMemoryCache = getControllerFromInMemoryCache(str);
        if (controllerFromInMemoryCache != null) {
            verifyControllerAndInvokeCallback(controllerFromInMemoryCache, objectType, creationCallback);
            AppMethodBeat.o(97024);
            return;
        }
        controllerFromInMemoryCache = deserializeFromDiskSynchronously(str);
        if (controllerFromInMemoryCache == null) {
            controllerFromInMemoryCache = new LikeActionController(str, objectType);
            serializeToDiskAsync(controllerFromInMemoryCache);
        }
        putControllerInMemoryCache(str, controllerFromInMemoryCache);
        handler.post(new 2(controllerFromInMemoryCache));
        invokeCallbackWithController(creationCallback, controllerFromInMemoryCache, null);
        AppMethodBeat.o(97024);
    }

    private static synchronized void performFirstInitialize() {
        synchronized (LikeActionController.class) {
            AppMethodBeat.i(97025);
            if (isInitialized) {
                AppMethodBeat.o(97025);
            } else {
                handler = new Handler(Looper.getMainLooper());
                objectSuffix = FacebookSdk.getApplicationContext().getSharedPreferences(LIKE_ACTION_CONTROLLER_STORE, 0).getInt(LIKE_ACTION_CONTROLLER_STORE_OBJECT_SUFFIX_KEY, 1);
                controllerDiskCache = new FileLruCache(TAG, new Limits());
                registerAccessTokenTracker();
                CallbackManagerImpl.registerStaticCallback(RequestCodeOffset.Like.toRequestCode(), new 3());
                isInitialized = true;
                AppMethodBeat.o(97025);
            }
        }
    }

    private static void invokeCallbackWithController(CreationCallback creationCallback, LikeActionController likeActionController, FacebookException facebookException) {
        AppMethodBeat.i(97026);
        if (creationCallback == null) {
            AppMethodBeat.o(97026);
            return;
        }
        handler.post(new 4(creationCallback, likeActionController, facebookException));
        AppMethodBeat.o(97026);
    }

    private static void registerAccessTokenTracker() {
        AppMethodBeat.i(97027);
        accessTokenTracker = new 5();
        AppMethodBeat.o(97027);
    }

    private static void putControllerInMemoryCache(String str, LikeActionController likeActionController) {
        AppMethodBeat.i(97028);
        String cacheKeyForObjectId = getCacheKeyForObjectId(str);
        mruCacheWorkQueue.addActiveWorkItem(new MRUCacheWorkItem(cacheKeyForObjectId, true));
        cache.put(cacheKeyForObjectId, likeActionController);
        AppMethodBeat.o(97028);
    }

    private static LikeActionController getControllerFromInMemoryCache(String str) {
        AppMethodBeat.i(97029);
        String cacheKeyForObjectId = getCacheKeyForObjectId(str);
        LikeActionController likeActionController = (LikeActionController) cache.get(cacheKeyForObjectId);
        if (likeActionController != null) {
            mruCacheWorkQueue.addActiveWorkItem(new MRUCacheWorkItem(cacheKeyForObjectId, false));
        }
        AppMethodBeat.o(97029);
        return likeActionController;
    }

    private static void serializeToDiskAsync(LikeActionController likeActionController) {
        AppMethodBeat.i(97030);
        String serializeToJson = serializeToJson(likeActionController);
        String cacheKeyForObjectId = getCacheKeyForObjectId(likeActionController.objectId);
        if (!(Utility.isNullOrEmpty(serializeToJson) || Utility.isNullOrEmpty(cacheKeyForObjectId))) {
            diskIOWorkQueue.addActiveWorkItem(new SerializeToDiskWorkItem(cacheKeyForObjectId, serializeToJson));
        }
        AppMethodBeat.o(97030);
    }

    private static void serializeToDiskSynchronously(String str, String str2) {
        AppMethodBeat.i(97031);
        Closeable closeable = null;
        try {
            closeable = controllerDiskCache.openPutStream(str);
            closeable.write(str2.getBytes());
            if (closeable != null) {
                Utility.closeQuietly(closeable);
                AppMethodBeat.o(97031);
                return;
            }
        } catch (IOException e) {
            if (closeable != null) {
                Utility.closeQuietly(closeable);
                AppMethodBeat.o(97031);
                return;
            }
        } catch (Throwable th) {
            if (closeable != null) {
                Utility.closeQuietly(closeable);
            }
            AppMethodBeat.o(97031);
        }
        AppMethodBeat.o(97031);
    }

    /* JADX WARNING: Removed duplicated region for block: B:19:0x0037  */
    private static com.facebook.share.internal.LikeActionController deserializeFromDiskSynchronously(java.lang.String r5) {
        /*
        r0 = 0;
        r4 = 97032; // 0x17b08 float:1.35971E-40 double:4.794E-319;
        com.tencent.matrix.trace.core.AppMethodBeat.i(r4);
        r1 = getCacheKeyForObjectId(r5);	 Catch:{ IOException -> 0x002a, all -> 0x0032 }
        r2 = controllerDiskCache;	 Catch:{ IOException -> 0x002a, all -> 0x0032 }
        r1 = r2.get(r1);	 Catch:{ IOException -> 0x002a, all -> 0x0032 }
        if (r1 == 0) goto L_0x0021;
    L_0x0013:
        r2 = com.facebook.internal.Utility.readStreamToString(r1);	 Catch:{ IOException -> 0x0042, all -> 0x003e }
        r3 = com.facebook.internal.Utility.isNullOrEmpty(r2);	 Catch:{ IOException -> 0x0042, all -> 0x003e }
        if (r3 != 0) goto L_0x0021;
    L_0x001d:
        r0 = deserializeFromJson(r2);	 Catch:{ IOException -> 0x0042, all -> 0x003e }
    L_0x0021:
        if (r1 == 0) goto L_0x0026;
    L_0x0023:
        com.facebook.internal.Utility.closeQuietly(r1);
    L_0x0026:
        com.tencent.matrix.trace.core.AppMethodBeat.o(r4);
        return r0;
    L_0x002a:
        r1 = move-exception;
        r1 = r0;
    L_0x002c:
        if (r1 == 0) goto L_0x0026;
    L_0x002e:
        com.facebook.internal.Utility.closeQuietly(r1);
        goto L_0x0026;
    L_0x0032:
        r1 = move-exception;
        r2 = r1;
        r3 = r0;
    L_0x0035:
        if (r3 == 0) goto L_0x003a;
    L_0x0037:
        com.facebook.internal.Utility.closeQuietly(r3);
    L_0x003a:
        com.tencent.matrix.trace.core.AppMethodBeat.o(r4);
        throw r2;
    L_0x003e:
        r0 = move-exception;
        r2 = r0;
        r3 = r1;
        goto L_0x0035;
    L_0x0042:
        r2 = move-exception;
        goto L_0x002c;
        */
        throw new UnsupportedOperationException("Method not decompiled: com.facebook.share.internal.LikeActionController.deserializeFromDiskSynchronously(java.lang.String):com.facebook.share.internal.LikeActionController");
    }

    private static LikeActionController deserializeFromJson(String str) {
        AppMethodBeat.i(97033);
        LikeActionController likeActionController;
        try {
            JSONObject jSONObject = new JSONObject(str);
            if (jSONObject.optInt(JSON_INT_VERSION_KEY, -1) != 3) {
                AppMethodBeat.o(97033);
                return null;
            }
            likeActionController = new LikeActionController(jSONObject.getString(JSON_STRING_OBJECT_ID_KEY), ObjectType.fromInt(jSONObject.optInt(JSON_INT_OBJECT_TYPE_KEY, ObjectType.UNKNOWN.getValue())));
            likeActionController.likeCountStringWithLike = jSONObject.optString(JSON_STRING_LIKE_COUNT_WITH_LIKE_KEY, null);
            likeActionController.likeCountStringWithoutLike = jSONObject.optString(JSON_STRING_LIKE_COUNT_WITHOUT_LIKE_KEY, null);
            likeActionController.socialSentenceWithLike = jSONObject.optString(JSON_STRING_SOCIAL_SENTENCE_WITH_LIKE_KEY, null);
            likeActionController.socialSentenceWithoutLike = jSONObject.optString(JSON_STRING_SOCIAL_SENTENCE_WITHOUT_LIKE_KEY, null);
            likeActionController.isObjectLiked = jSONObject.optBoolean(JSON_BOOL_IS_OBJECT_LIKED_KEY);
            likeActionController.unlikeToken = jSONObject.optString("unlike_token", null);
            jSONObject = jSONObject.optJSONObject(JSON_BUNDLE_FACEBOOK_DIALOG_ANALYTICS_BUNDLE);
            if (jSONObject != null) {
                likeActionController.facebookDialogAnalyticsBundle = BundleJSONConverter.convertToBundle(jSONObject);
            }
            AppMethodBeat.o(97033);
            return likeActionController;
        } catch (JSONException e) {
            likeActionController = null;
        }
    }

    private static String serializeToJson(LikeActionController likeActionController) {
        AppMethodBeat.i(97034);
        JSONObject jSONObject = new JSONObject();
        try {
            jSONObject.put(JSON_INT_VERSION_KEY, 3);
            jSONObject.put(JSON_STRING_OBJECT_ID_KEY, likeActionController.objectId);
            jSONObject.put(JSON_INT_OBJECT_TYPE_KEY, likeActionController.objectType.getValue());
            jSONObject.put(JSON_STRING_LIKE_COUNT_WITH_LIKE_KEY, likeActionController.likeCountStringWithLike);
            jSONObject.put(JSON_STRING_LIKE_COUNT_WITHOUT_LIKE_KEY, likeActionController.likeCountStringWithoutLike);
            jSONObject.put(JSON_STRING_SOCIAL_SENTENCE_WITH_LIKE_KEY, likeActionController.socialSentenceWithLike);
            jSONObject.put(JSON_STRING_SOCIAL_SENTENCE_WITHOUT_LIKE_KEY, likeActionController.socialSentenceWithoutLike);
            jSONObject.put(JSON_BOOL_IS_OBJECT_LIKED_KEY, likeActionController.isObjectLiked);
            jSONObject.put("unlike_token", likeActionController.unlikeToken);
            if (likeActionController.facebookDialogAnalyticsBundle != null) {
                JSONObject convertToJSON = BundleJSONConverter.convertToJSON(likeActionController.facebookDialogAnalyticsBundle);
                if (convertToJSON != null) {
                    jSONObject.put(JSON_BUNDLE_FACEBOOK_DIALOG_ANALYTICS_BUNDLE, convertToJSON);
                }
            }
            String jSONObject2 = jSONObject.toString();
            AppMethodBeat.o(97034);
            return jSONObject2;
        } catch (JSONException e) {
            AppMethodBeat.o(97034);
            return null;
        }
    }

    private static String getCacheKeyForObjectId(String str) {
        AppMethodBeat.i(97035);
        String str2 = null;
        AccessToken currentAccessToken = AccessToken.getCurrentAccessToken();
        if (AccessToken.isCurrentAccessTokenActive()) {
            str2 = currentAccessToken.getToken();
        }
        if (str2 != null) {
            str2 = Utility.md5hash(str2);
        }
        str2 = String.format(Locale.ROOT, "%s|%s|com.fb.sdk.like|%d", new Object[]{str, Utility.coerceValueIfNullOrEmpty(str2, BuildConfig.FLAVOR), Integer.valueOf(objectSuffix)});
        AppMethodBeat.o(97035);
        return str2;
    }

    private static void broadcastAction(LikeActionController likeActionController, String str) {
        AppMethodBeat.i(97036);
        broadcastAction(likeActionController, str, null);
        AppMethodBeat.o(97036);
    }

    private static void broadcastAction(LikeActionController likeActionController, String str, Bundle bundle) {
        AppMethodBeat.i(97037);
        Intent intent = new Intent(str);
        if (likeActionController != null) {
            if (bundle == null) {
                bundle = new Bundle();
            }
            bundle.putString(ACTION_OBJECT_ID_KEY, likeActionController.getObjectId());
        }
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        d.S(FacebookSdk.getApplicationContext()).c(intent);
        AppMethodBeat.o(97037);
    }

    private LikeActionController(String str, ObjectType objectType) {
        this.objectId = str;
        this.objectType = objectType;
    }

    @Deprecated
    public String getObjectId() {
        return this.objectId;
    }

    @Deprecated
    public String getLikeCountString() {
        return this.isObjectLiked ? this.likeCountStringWithLike : this.likeCountStringWithoutLike;
    }

    @Deprecated
    public String getSocialSentence() {
        return this.isObjectLiked ? this.socialSentenceWithLike : this.socialSentenceWithoutLike;
    }

    @Deprecated
    public boolean isObjectLiked() {
        return this.isObjectLiked;
    }

    @Deprecated
    public boolean shouldEnableView() {
        return false;
    }

    @Deprecated
    public void toggleLike(Activity activity, FragmentWrapper fragmentWrapper, Bundle bundle) {
        boolean z = true;
        AppMethodBeat.i(97038);
        boolean z2 = !this.isObjectLiked;
        if (canUseOGPublish()) {
            updateLikeState(z2);
            if (this.isPendingLikeOrUnlike) {
                getAppEventsLogger().logSdkEvent("fb_like_control_did_undo_quickly", null, bundle);
                AppMethodBeat.o(97038);
                return;
            }
            if (!publishLikeOrUnlikeAsync(z2, bundle)) {
                if (z2) {
                    z = false;
                }
                updateLikeState(z);
            }
            AppMethodBeat.o(97038);
        }
        presentLikeDialog(activity, fragmentWrapper, bundle);
        AppMethodBeat.o(97038);
    }

    private AppEventsLogger getAppEventsLogger() {
        AppMethodBeat.i(97039);
        if (this.appEventsLogger == null) {
            this.appEventsLogger = AppEventsLogger.newLogger(FacebookSdk.getApplicationContext());
        }
        AppEventsLogger appEventsLogger = this.appEventsLogger;
        AppMethodBeat.o(97039);
        return appEventsLogger;
    }

    private boolean publishLikeOrUnlikeAsync(boolean z, Bundle bundle) {
        boolean z2 = true;
        AppMethodBeat.i(97040);
        if (canUseOGPublish()) {
            if (z) {
                publishLikeAsync(bundle);
            } else if (!Utility.isNullOrEmpty(this.unlikeToken)) {
                publishUnlikeAsync(bundle);
            }
            AppMethodBeat.o(97040);
            return z2;
        }
        z2 = false;
        AppMethodBeat.o(97040);
        return z2;
    }

    private void publishDidError(boolean z) {
        AppMethodBeat.i(97041);
        updateLikeState(z);
        Bundle bundle = new Bundle();
        bundle.putString("com.facebook.platform.status.ERROR_DESCRIPTION", ERROR_PUBLISH_ERROR);
        broadcastAction(this, ACTION_LIKE_ACTION_CONTROLLER_DID_ERROR, bundle);
        AppMethodBeat.o(97041);
    }

    private void updateLikeState(boolean z) {
        AppMethodBeat.i(97042);
        updateState(z, this.likeCountStringWithLike, this.likeCountStringWithoutLike, this.socialSentenceWithLike, this.socialSentenceWithoutLike, this.unlikeToken);
        AppMethodBeat.o(97042);
    }

    private void updateState(boolean z, String str, String str2, String str3, String str4, String str5) {
        AppMethodBeat.i(97043);
        String coerceValueIfNullOrEmpty = Utility.coerceValueIfNullOrEmpty(str, null);
        String coerceValueIfNullOrEmpty2 = Utility.coerceValueIfNullOrEmpty(str2, null);
        String coerceValueIfNullOrEmpty3 = Utility.coerceValueIfNullOrEmpty(str3, null);
        String coerceValueIfNullOrEmpty4 = Utility.coerceValueIfNullOrEmpty(str4, null);
        String coerceValueIfNullOrEmpty5 = Utility.coerceValueIfNullOrEmpty(str5, null);
        Object obj = (z == this.isObjectLiked && Utility.areObjectsEqual(coerceValueIfNullOrEmpty, this.likeCountStringWithLike) && Utility.areObjectsEqual(coerceValueIfNullOrEmpty2, this.likeCountStringWithoutLike) && Utility.areObjectsEqual(coerceValueIfNullOrEmpty3, this.socialSentenceWithLike) && Utility.areObjectsEqual(coerceValueIfNullOrEmpty4, this.socialSentenceWithoutLike) && Utility.areObjectsEqual(coerceValueIfNullOrEmpty5, this.unlikeToken)) ? null : 1;
        if (obj == null) {
            AppMethodBeat.o(97043);
            return;
        }
        this.isObjectLiked = z;
        this.likeCountStringWithLike = coerceValueIfNullOrEmpty;
        this.likeCountStringWithoutLike = coerceValueIfNullOrEmpty2;
        this.socialSentenceWithLike = coerceValueIfNullOrEmpty3;
        this.socialSentenceWithoutLike = coerceValueIfNullOrEmpty4;
        this.unlikeToken = coerceValueIfNullOrEmpty5;
        serializeToDiskAsync(this);
        broadcastAction(this, ACTION_LIKE_ACTION_CONTROLLER_UPDATED);
        AppMethodBeat.o(97043);
    }

    private void presentLikeDialog(Activity activity, FragmentWrapper fragmentWrapper, Bundle bundle) {
        String str;
        AppMethodBeat.i(97044);
        if (LikeDialog.canShowNativeDialog()) {
            str = "fb_like_control_did_present_dialog";
        } else if (LikeDialog.canShowWebFallback()) {
            str = "fb_like_control_did_present_fallback_dialog";
        } else {
            logAppEventForError("present_dialog", bundle);
            Utility.logd(TAG, "Cannot show the Like Dialog on this device.");
            broadcastAction(null, ACTION_LIKE_ACTION_CONTROLLER_UPDATED);
            str = null;
        }
        if (str != null) {
            if (this.objectType != null) {
                str = this.objectType.toString();
            } else {
                str = ObjectType.UNKNOWN.toString();
            }
            LikeContent build = new Builder().setObjectId(this.objectId).setObjectType(str).build();
            if (fragmentWrapper != null) {
                new LikeDialog(fragmentWrapper).show(build);
            } else {
                new LikeDialog(activity).show(build);
            }
            saveState(bundle);
            getAppEventsLogger().logSdkEvent("fb_like_control_did_present_dialog", null, bundle);
        }
        AppMethodBeat.o(97044);
    }

    private void onActivityResult(int i, int i2, Intent intent) {
        AppMethodBeat.i(97045);
        ShareInternalUtility.handleActivityResult(i, i2, intent, getResultProcessor(this.facebookDialogAnalyticsBundle));
        clearState();
        AppMethodBeat.o(97045);
    }

    private ResultProcessor getResultProcessor(Bundle bundle) {
        AppMethodBeat.i(97046);
        6 6 = new 6(this, null, bundle);
        AppMethodBeat.o(97046);
        return 6;
    }

    private void saveState(Bundle bundle) {
        AppMethodBeat.i(97047);
        storeObjectIdForPendingController(this.objectId);
        this.facebookDialogAnalyticsBundle = bundle;
        serializeToDiskAsync(this);
        AppMethodBeat.o(97047);
    }

    private void clearState() {
        AppMethodBeat.i(97048);
        this.facebookDialogAnalyticsBundle = null;
        storeObjectIdForPendingController(null);
        AppMethodBeat.o(97048);
    }

    private static void storeObjectIdForPendingController(String str) {
        AppMethodBeat.i(97049);
        objectIdForPendingController = str;
        FacebookSdk.getApplicationContext().getSharedPreferences(LIKE_ACTION_CONTROLLER_STORE, 0).edit().putString(LIKE_ACTION_CONTROLLER_STORE_PENDING_OBJECT_ID_KEY, objectIdForPendingController).apply();
        AppMethodBeat.o(97049);
    }

    private boolean canUseOGPublish() {
        AppMethodBeat.i(97050);
        AccessToken currentAccessToken = AccessToken.getCurrentAccessToken();
        if (this.objectIsPage || this.verifiedObjectId == null || !AccessToken.isCurrentAccessTokenActive() || currentAccessToken.getPermissions() == null || !currentAccessToken.getPermissions().contains("publish_actions")) {
            AppMethodBeat.o(97050);
            return false;
        }
        AppMethodBeat.o(97050);
        return true;
    }

    private void publishLikeAsync(final Bundle bundle) {
        AppMethodBeat.i(97051);
        this.isPendingLikeOrUnlike = true;
        fetchVerifiedObjectId(new RequestCompletionCallback() {
            public void onComplete() {
                AppMethodBeat.i(96992);
                if (Utility.isNullOrEmpty(LikeActionController.this.verifiedObjectId)) {
                    Bundle bundle = new Bundle();
                    bundle.putString("com.facebook.platform.status.ERROR_DESCRIPTION", LikeActionController.ERROR_INVALID_OBJECT_ID);
                    LikeActionController.access$1500(LikeActionController.this, LikeActionController.ACTION_LIKE_ACTION_CONTROLLER_DID_ERROR, bundle);
                    AppMethodBeat.o(96992);
                    return;
                }
                GraphRequestBatch graphRequestBatch = new GraphRequestBatch();
                PublishLikeRequestWrapper publishLikeRequestWrapper = new PublishLikeRequestWrapper(LikeActionController.this, LikeActionController.this.verifiedObjectId, LikeActionController.this.objectType);
                publishLikeRequestWrapper.addToBatch(graphRequestBatch);
                graphRequestBatch.addCallback(new 1(this, publishLikeRequestWrapper));
                graphRequestBatch.executeAsync();
                AppMethodBeat.o(96992);
            }
        });
        AppMethodBeat.o(97051);
    }

    private void publishUnlikeAsync(Bundle bundle) {
        AppMethodBeat.i(97052);
        this.isPendingLikeOrUnlike = true;
        GraphRequestBatch graphRequestBatch = new GraphRequestBatch();
        PublishUnlikeRequestWrapper publishUnlikeRequestWrapper = new PublishUnlikeRequestWrapper(this, this.unlikeToken);
        publishUnlikeRequestWrapper.addToBatch(graphRequestBatch);
        graphRequestBatch.addCallback(new 8(this, publishUnlikeRequestWrapper, bundle));
        graphRequestBatch.executeAsync();
        AppMethodBeat.o(97052);
    }

    private void refreshStatusAsync() {
        AppMethodBeat.i(97053);
        if (AccessToken.isCurrentAccessTokenActive()) {
            fetchVerifiedObjectId(new 9(this));
            AppMethodBeat.o(97053);
            return;
        }
        refreshStatusViaService();
        AppMethodBeat.o(97053);
    }

    private void refreshStatusViaService() {
        AppMethodBeat.i(97054);
        LikeStatusClient likeStatusClient = new LikeStatusClient(FacebookSdk.getApplicationContext(), FacebookSdk.getApplicationId(), this.objectId);
        if (likeStatusClient.start()) {
            likeStatusClient.setCompletedListener(new 10(this));
            AppMethodBeat.o(97054);
            return;
        }
        AppMethodBeat.o(97054);
    }

    private void publishAgainIfNeeded(Bundle bundle) {
        AppMethodBeat.i(97055);
        if (!(this.isObjectLiked == this.isObjectLikedOnServer || publishLikeOrUnlikeAsync(this.isObjectLiked, bundle))) {
            publishDidError(!this.isObjectLiked);
        }
        AppMethodBeat.o(97055);
    }

    private void fetchVerifiedObjectId(final RequestCompletionCallback requestCompletionCallback) {
        AppMethodBeat.i(97056);
        if (Utility.isNullOrEmpty(this.verifiedObjectId)) {
            final GetOGObjectIdRequestWrapper getOGObjectIdRequestWrapper = new GetOGObjectIdRequestWrapper(this, this.objectId, this.objectType);
            final GetPageIdRequestWrapper getPageIdRequestWrapper = new GetPageIdRequestWrapper(this.objectId, this.objectType);
            GraphRequestBatch graphRequestBatch = new GraphRequestBatch();
            getOGObjectIdRequestWrapper.addToBatch(graphRequestBatch);
            getPageIdRequestWrapper.addToBatch(graphRequestBatch);
            graphRequestBatch.addCallback(new GraphRequestBatch$Callback() {
                public void onBatchCompleted(GraphRequestBatch graphRequestBatch) {
                    AppMethodBeat.i(96982);
                    LikeActionController.this.verifiedObjectId = getOGObjectIdRequestWrapper.verifiedObjectId;
                    if (Utility.isNullOrEmpty(LikeActionController.this.verifiedObjectId)) {
                        LikeActionController.this.verifiedObjectId = getPageIdRequestWrapper.verifiedObjectId;
                        LikeActionController.this.objectIsPage = getPageIdRequestWrapper.objectIsPage;
                    }
                    if (Utility.isNullOrEmpty(LikeActionController.this.verifiedObjectId)) {
                        FacebookRequestError error;
                        Logger.log(LoggingBehavior.DEVELOPER_ERRORS, LikeActionController.TAG, "Unable to verify the FB id for '%s'. Verify that it is a valid FB object or page", new Object[]{LikeActionController.this.objectId});
                        LikeActionController likeActionController = LikeActionController.this;
                        String str = "get_verified_id";
                        if (getPageIdRequestWrapper.getError() != null) {
                            error = getPageIdRequestWrapper.getError();
                        } else {
                            error = getOGObjectIdRequestWrapper.getError();
                        }
                        LikeActionController.access$2400(likeActionController, str, error);
                    }
                    if (requestCompletionCallback != null) {
                        requestCompletionCallback.onComplete();
                    }
                    AppMethodBeat.o(96982);
                }
            });
            graphRequestBatch.executeAsync();
            AppMethodBeat.o(97056);
            return;
        }
        if (requestCompletionCallback != null) {
            requestCompletionCallback.onComplete();
        }
        AppMethodBeat.o(97056);
    }

    private void logAppEventForError(String str, Bundle bundle) {
        AppMethodBeat.i(97057);
        Bundle bundle2 = new Bundle(bundle);
        bundle2.putString(JSON_STRING_OBJECT_ID_KEY, this.objectId);
        bundle2.putString(JSON_INT_OBJECT_TYPE_KEY, this.objectType.toString());
        bundle2.putString("current_action", str);
        getAppEventsLogger().logSdkEvent("fb_like_control_error", null, bundle2);
        AppMethodBeat.o(97057);
    }

    private void logAppEventForError(String str, FacebookRequestError facebookRequestError) {
        AppMethodBeat.i(97058);
        Bundle bundle = new Bundle();
        if (facebookRequestError != null) {
            JSONObject requestResult = facebookRequestError.getRequestResult();
            if (requestResult != null) {
                bundle.putString("error", requestResult.toString());
            }
        }
        logAppEventForError(str, bundle);
        AppMethodBeat.o(97058);
    }
}
