<commit_before>package io.branch.rnbranch;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.BroadcastReceiver;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.os.Handler;

import com.facebook.react.bridge.*;
import com.facebook.react.bridge.Promise;
import com.facebook.react.modules.core.*;
import com.facebook.react.bridge.ReadableMap;

import io.branch.referral.*;
import io.branch.referral.Branch.BranchLinkCreateListener;
import io.branch.referral.BuildConfig;
import io.branch.referral.util.*;
import io.branch.referral.Branch;
import io.branch.indexing.*;

import org.json.*;

import java.lang.ref.WeakReference;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class RNBranchModule extends ReactContextBaseJavaModule {
    public static final String REACT_CLASS = "RNBranch";
    public static final String REACT_MODULE_NAME = "RNBranch";
    public static final String NATIVE_INIT_SESSION_FINISHED_EVENT = "io.branch.rnbranch.RNBranchModule.onInitSessionFinished";
    public static final String NATIVE_INIT_SESSION_FINISHED_EVENT_BRANCH_UNIVERSAL_OBJECT = "branch_universal_object";
    public static final String NATIVE_INIT_SESSION_FINISHED_EVENT_LINK_PROPERTIES = "link_properties";
    public static final String NATIVE_INIT_SESSION_FINISHED_EVENT_PARAMS = "params";
    public static final String NATIVE_INIT_SESSION_FINISHED_EVENT_ERROR = "error";
    public static final String NATIVE_INIT_SESSION_FINISHED_EVENT_URI = "uri";
    private static final String RN_INIT_SESSION_SUCCESS_EVENT = "RNBranch.initSessionSuccess";
    private static final String RN_INIT_SESSION_ERROR_EVENT = "RNBranch.initSessionError";
    private static final String INIT_SESSION_SUCCESS = "INIT_SESSION_SUCCESS";
    private static final String INIT_SESSION_ERROR = "INIT_SESSION_ERROR";
    private static final String ADD_TO_CART_EVENT = "ADD_TO_CART_EVENT";
    private static final String ADD_TO_WISHLIST_EVENT = "ADD_TO_WISHLIST_EVENT";
    private static final String PURCHASED_EVENT = "PURCHASED_EVENT";
    private static final String PURCHASE_INITIATED_EVENT = "PURCHASE_INITIATED_EVENT";
    private static final String REGISTER_VIEW_EVENT = "REGISTER_VIEW_EVENT";
    private static final String SHARE_COMPLETED_EVENT = "SHARE_COMPLETED_EVENT";
    private static final String SHARE_INITIATED_EVENT = "SHARE_INITIATED_EVENT";

    private static final String STANDARD_EVENT_ADD_TO_CART = "STANDARD_EVENT_ADD_TO_CART";
    private static final String STANDARD_EVENT_ADD_TO_WISHLIST = "STANDARD_EVENT_ADD_TO_WISHLIST";
    private static final String STANDARD_EVENT_VIEW_CART = "STANDARD_EVENT_VIEW_CART";
    private static final String STANDARD_EVENT_INITIATE_PURCHASE = "STANDARD_EVENT_INITIATE_PURCHASE";
    private static final String STANDARD_EVENT_ADD_PAYMENT_INFO = "STANDARD_EVENT_ADD_PAYMENT_INFO";
    private static final String STANDARD_EVENT_PURCHASE = "STANDARD_EVENT_PURCHASE";
    private static final String STANDARD_EVENT_SPEND_CREDITS = "STANDARD_EVENT_SPEND_CREDITS";

    private static final String STANDARD_EVENT_SEARCH = "STANDARD_EVENT_SEARCH";
    private static final String STANDARD_EVENT_VIEW_ITEM = "STANDARD_EVENT_VIEW_ITEM";
    private static final String STANDARD_EVENT_VIEW_ITEMS = "STANDARD_EVENT_VIEW_ITEMS";
    private static final String STANDARD_EVENT_RATE = "STANDARD_EVENT_RATE";
    private static final String STANDARD_EVENT_SHARE = "STANDARD_EVENT_SHARE";

    private static final String STANDARD_EVENT_COMPLETE_REGISTRATION = "STANDARD_EVENT_COMPLETE_REGISTRATION";
    private static final String STANDARD_EVENT_COMPLETE_TUTORIAL = "STANDARD_EVENT_COMPLETE_TUTORIAL";
    private static final String STANDARD_EVENT_ACHIEVE_LEVEL = "STANDARD_EVENT_ACHIEVE_LEVEL";
    private static final String STANDARD_EVENT_UNLOCK_ACHIEVEMENT = "STANDARD_EVENT_UNLOCK_ACHIEVEMENT";

    private static final String IDENT_FIELD_NAME = "ident";
    public static final String UNIVERSAL_OBJECT_NOT_FOUND_ERROR_CODE = "RNBranch::Error::BUONotFound";
    private static final long AGING_HASH_TTL = 3600000;

    private static JSONObject initSessionResult = null;
    private BroadcastReceiver mInitSessionEventReceiver = null;
    private static WeakReference<Branch.BranchUniversalReferralInitListener> initListener = null;

    private static Activity mActivity = null;
    private static boolean mUseDebug = false;
    private static boolean mInitialized = false;
    private static JSONObject mRequestMetadata = new JSONObject();

    private AgingHash<String, BranchUniversalObject> mUniversalObjectMap = new AgingHash<>(AGING_HASH_TTL);

    public static void initSession(final Uri uri, Activity reactActivity, Branch.BranchUniversalReferralInitListener anInitListener) {
        initListener = new WeakReference<>(anInitListener);
        initSession(uri, reactActivity);
    }

    public static void initSession(final Uri uri, Activity reactActivity) {
        Branch branch = setupBranch(reactActivity.getApplicationContext());

        mActivity = reactActivity;
        branch.initSession(new Branch.BranchReferralInitListener(){

            private Activity mmActivity = null;

            @Override
            public void onInitFinished(JSONObject referringParams, BranchError error) {

                Log.d(REACT_CLASS, "onInitFinished");
                JSONObject result = new JSONObject();
                Uri referringUri = null;
                try{
                    boolean clickedBranchLink = false;
                    // getXXX throws. It's OK for these to be missing.
                    try {
                        clickedBranchLink = referringParams.getBoolean("+clicked_branch_link");
                    }
                    catch (JSONException e) {

                    }

                    String referringLink = null;
                    if (clickedBranchLink) {
                        try {
                            referringLink = referringParams.getString("~referring_link");
                        }
                        catch (JSONException e) {

                        }
                    }
                    else {
                        try {
                            referringLink = referringParams.getString("+non_branch_link");
                        }
                        catch (JSONException e) {

                        }
                    }

                    if (referringLink != null) referringUri = Uri.parse(referringLink);

                    result.put(NATIVE_INIT_SESSION_FINISHED_EVENT_PARAMS, referringParams);
                    result.put(NATIVE_INIT_SESSION_FINISHED_EVENT_ERROR, error != null ? error.getMessage() : JSONObject.NULL);
                    result.put(NATIVE_INIT_SESSION_FINISHED_EVENT_URI, referringLink != null ? referringLink : JSONObject.NULL);
                } catch(JSONException ex) {
                    try {
                        result.put("error", "Failed to convert result to JSONObject: " + ex.getMessage());
                    } catch(JSONException k) {}
                }
                initSessionResult = result;

                BranchUniversalObject branchUniversalObject =  BranchUniversalObject.getReferredBranchUniversalObject();
                LinkProperties linkProperties = LinkProperties.getReferredLinkProperties();

                if (initListener != null) {
                    Branch.BranchUniversalReferralInitListener listener = initListener.get();
                    if (listener != null) listener.onInitFinished(branchUniversalObject, linkProperties, error);
                }
                generateLocalBroadcast(referringParams, referringUri, branchUniversalObject, linkProperties, error);
            }

            private Branch.BranchReferralInitListener init(Activity activity) {
                mmActivity = activity;
                return this;
            }

            private void generateLocalBroadcast(JSONObject referringParams,
                                                Uri uri,
                                                BranchUniversalObject branchUniversalObject,
                                                LinkProperties linkProperties,
                                                BranchError error) {
                Intent broadcastIntent = new Intent(NATIVE_INIT_SESSION_FINISHED_EVENT);

                if (referringParams != null) {
                    broadcastIntent.putExtra(NATIVE_INIT_SESSION_FINISHED_EVENT_PARAMS, referringParams.toString());
                }

                if (branchUniversalObject != null) {<commit_msg>[Android] remove WeakReference from BranchUniversalReferralInitListener<commit_after>package io.branch.rnbranch;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.BroadcastReceiver;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.os.Handler;

import com.facebook.react.bridge.*;
import com.facebook.react.bridge.Promise;
import com.facebook.react.modules.core.*;
import com.facebook.react.bridge.ReadableMap;

import io.branch.referral.*;
import io.branch.referral.Branch.BranchLinkCreateListener;
import io.branch.referral.BuildConfig;
import io.branch.referral.util.*;
import io.branch.referral.Branch;
import io.branch.indexing.*;

import org.json.*;

import java.lang.ref.WeakReference;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class RNBranchModule extends ReactContextBaseJavaModule {
    public static final String REACT_CLASS = "RNBranch";
    public static final String REACT_MODULE_NAME = "RNBranch";
    public static final String NATIVE_INIT_SESSION_FINISHED_EVENT = "io.branch.rnbranch.RNBranchModule.onInitSessionFinished";
    public static final String NATIVE_INIT_SESSION_FINISHED_EVENT_BRANCH_UNIVERSAL_OBJECT = "branch_universal_object";
    public static final String NATIVE_INIT_SESSION_FINISHED_EVENT_LINK_PROPERTIES = "link_properties";
    public static final String NATIVE_INIT_SESSION_FINISHED_EVENT_PARAMS = "params";
    public static final String NATIVE_INIT_SESSION_FINISHED_EVENT_ERROR = "error";
    public static final String NATIVE_INIT_SESSION_FINISHED_EVENT_URI = "uri";
    private static final String RN_INIT_SESSION_SUCCESS_EVENT = "RNBranch.initSessionSuccess";
    private static final String RN_INIT_SESSION_ERROR_EVENT = "RNBranch.initSessionError";
    private static final String INIT_SESSION_SUCCESS = "INIT_SESSION_SUCCESS";
    private static final String INIT_SESSION_ERROR = "INIT_SESSION_ERROR";
    private static final String ADD_TO_CART_EVENT = "ADD_TO_CART_EVENT";
    private static final String ADD_TO_WISHLIST_EVENT = "ADD_TO_WISHLIST_EVENT";
    private static final String PURCHASED_EVENT = "PURCHASED_EVENT";
    private static final String PURCHASE_INITIATED_EVENT = "PURCHASE_INITIATED_EVENT";
    private static final String REGISTER_VIEW_EVENT = "REGISTER_VIEW_EVENT";
    private static final String SHARE_COMPLETED_EVENT = "SHARE_COMPLETED_EVENT";
    private static final String SHARE_INITIATED_EVENT = "SHARE_INITIATED_EVENT";

    private static final String STANDARD_EVENT_ADD_TO_CART = "STANDARD_EVENT_ADD_TO_CART";
    private static final String STANDARD_EVENT_ADD_TO_WISHLIST = "STANDARD_EVENT_ADD_TO_WISHLIST";
    private static final String STANDARD_EVENT_VIEW_CART = "STANDARD_EVENT_VIEW_CART";
    private static final String STANDARD_EVENT_INITIATE_PURCHASE = "STANDARD_EVENT_INITIATE_PURCHASE";
    private static final String STANDARD_EVENT_ADD_PAYMENT_INFO = "STANDARD_EVENT_ADD_PAYMENT_INFO";
    private static final String STANDARD_EVENT_PURCHASE = "STANDARD_EVENT_PURCHASE";
    private static final String STANDARD_EVENT_SPEND_CREDITS = "STANDARD_EVENT_SPEND_CREDITS";

    private static final String STANDARD_EVENT_SEARCH = "STANDARD_EVENT_SEARCH";
    private static final String STANDARD_EVENT_VIEW_ITEM = "STANDARD_EVENT_VIEW_ITEM";
    private static final String STANDARD_EVENT_VIEW_ITEMS = "STANDARD_EVENT_VIEW_ITEMS";
    private static final String STANDARD_EVENT_RATE = "STANDARD_EVENT_RATE";
    private static final String STANDARD_EVENT_SHARE = "STANDARD_EVENT_SHARE";

    private static final String STANDARD_EVENT_COMPLETE_REGISTRATION = "STANDARD_EVENT_COMPLETE_REGISTRATION";
    private static final String STANDARD_EVENT_COMPLETE_TUTORIAL = "STANDARD_EVENT_COMPLETE_TUTORIAL";
    private static final String STANDARD_EVENT_ACHIEVE_LEVEL = "STANDARD_EVENT_ACHIEVE_LEVEL";
    private static final String STANDARD_EVENT_UNLOCK_ACHIEVEMENT = "STANDARD_EVENT_UNLOCK_ACHIEVEMENT";

    private static final String IDENT_FIELD_NAME = "ident";
    public static final String UNIVERSAL_OBJECT_NOT_FOUND_ERROR_CODE = "RNBranch::Error::BUONotFound";
    private static final long AGING_HASH_TTL = 3600000;

    private static JSONObject initSessionResult = null;
    private BroadcastReceiver mInitSessionEventReceiver = null;
    private static Branch.BranchUniversalReferralInitListener initListener = null;

    private static Activity mActivity = null;
    private static boolean mUseDebug = false;
    private static boolean mInitialized = false;
    private static JSONObject mRequestMetadata = new JSONObject();

    private AgingHash<String, BranchUniversalObject> mUniversalObjectMap = new AgingHash<>(AGING_HASH_TTL);

    public static void initSession(final Uri uri, Activity reactActivity, Branch.BranchUniversalReferralInitListener anInitListener) {
        initListener = anInitListener;
        initSession(uri, reactActivity);
    }

    public static void initSession(final Uri uri, Activity reactActivity) {
        Branch branch = setupBranch(reactActivity.getApplicationContext());

        mActivity = reactActivity;
        branch.initSession(new Branch.BranchReferralInitListener(){

            private Activity mmActivity = null;

            @Override
            public void onInitFinished(JSONObject referringParams, BranchError error) {

                Log.d(REACT_CLASS, "onInitFinished");
                JSONObject result = new JSONObject();
                Uri referringUri = null;
                try{
                    boolean clickedBranchLink = false;
                    // getXXX throws. It's OK for these to be missing.
                    try {
                        clickedBranchLink = referringParams.getBoolean("+clicked_branch_link");
                    }
                    catch (JSONException e) {

                    }

                    String referringLink = null;
                    if (clickedBranchLink) {
                        try {
                            referringLink = referringParams.getString("~referring_link");
                        }
                        catch (JSONException e) {

                        }
                    }
                    else {
                        try {
                            referringLink = referringParams.getString("+non_branch_link");
                        }
                        catch (JSONException e) {

                        }
                    }

                    if (referringLink != null) referringUri = Uri.parse(referringLink);

                    result.put(NATIVE_INIT_SESSION_FINISHED_EVENT_PARAMS, referringParams);
                    result.put(NATIVE_INIT_SESSION_FINISHED_EVENT_ERROR, error != null ? error.getMessage() : JSONObject.NULL);
                    result.put(NATIVE_INIT_SESSION_FINISHED_EVENT_URI, referringLink != null ? referringLink : JSONObject.NULL);
                } catch(JSONException ex) {
                    try {
                        result.put("error", "Failed to convert result to JSONObject: " + ex.getMessage());
                    } catch(JSONException k) {}
                }
                initSessionResult = result;

                BranchUniversalObject branchUniversalObject =  BranchUniversalObject.getReferredBranchUniversalObject();
                LinkProperties linkProperties = LinkProperties.getReferredLinkProperties();

                if (initListener != null) {
                    initListener.onInitFinished(branchUniversalObject, linkProperties, error);
                }
                generateLocalBroadcast(referringParams, referringUri, branchUniversalObject, linkProperties, error);
            }

            private Branch.BranchReferralInitListener init(Activity activity) {
                mmActivity = activity;
                return this;
            }

            private void generateLocalBroadcast(JSONObject referringParams,
                                                Uri uri,
                                                BranchUniversalObject branchUniversalObject,
                                                LinkProperties linkProperties,
                                                BranchError error) {
                Intent broadcastIntent = new Intent(NATIVE_INIT_SESSION_FINISHED_EVENT);

                if (referringParams != null) {
                    broadcastIntent.putExtra(NATIVE_INIT_SESSION_FINISHED_EVENT_PARAMS, referringParams.toString());
                }

                if (branchUniversalObject != null) {
{'fraction_non_alphanumeric': 0.062144967052942514, 'fraction_numerical': 0.0009088843444671666, 'mean_word_length': 2.9349575324094768, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2494477', 'n_tokens_mistral': 4677, 'n_tokens_neox': 4465, 'n_words': 997}