package com.eulerian.android.sdk;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.support.annotation.NonNull;

/**
 * Created by Francois Rouault on 09/03/2015.
 */
class PersistentIdentity {

    private static final String INSTALL_REFERRER_HAS_ALREADY_BEEN_SENT_ONCE = "referrer_sent";
    private static PersistentIdentity mInstance;
    private final SharedPreferences mSharedPreferences;

    static final String KEY_REFERRER = "referrer";
    static final String KEY_ADVERTISING_ID = "id";
    static final String KEY_ADVERTISING_IS_LAT = "isLAT";

    public static PersistentIdentity getInstance() {
        if (mInstance == null) {
            mInstance = new PersistentIdentity();
        }
        return mInstance;
    }

    private PersistentIdentity() {
        mSharedPreferences = EAnalytics.getContext().getSharedPreferences("eanalytics-prefs", Context.MODE_PRIVATE);
    }

    public String getAdvertisingId() {
        return mSharedPreferences.getString(KEY_ADVERTISING_ID, null);
    }

    public boolean getAdvertisingIsLat() {
        return mSharedPreferences.getBoolean(KEY_ADVERTISING_IS_LAT, false);
    }

    public synchronized void saveAdvertisingId(String id, boolean isLAT) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putString(KEY_ADVERTISING_ID, id);
        editor.putBoolean(KEY_ADVERTISING_IS_LAT, isLAT);
        writeEdits(editor);
    }

    private void writeEdits(final SharedPreferences.Editor editor) {
        if (Build.VERSION.SDK_INT >= 9) {
            editor.apply();
        } else {
            editor.commit();
        }
    }

    public void saveInstallReferrer(@NonNull String referrer) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putString(PersistentIdentity.KEY_REFERRER, referrer);
        writeEdits(editor);
    }

    public String getInstallReferrer() {
        return mSharedPreferences.getString(KEY_REFERRER, null);
    }

    boolean shouldFetchInstallReferrer() {
        boolean isInstallReferrerSent = mSharedPreferences.getBoolean(INSTALL_REFERRER_HAS_ALREADY_BEEN_SENT_ONCE, false);
        return !isInstallReferrerSent;
    }

    public boolean shouldSendInstallReferrer() {
        return mSharedPreferences.getBoolean(INSTALL_REFERRER_HAS_ALREADY_BEEN_SENT_ONCE, false)
                && getInstallReferrer() != null;
    }

    public void setInstallReferrerSent() {
        mSharedPreferences.edit()
                .putBoolean(INSTALL_REFERRER_HAS_ALREADY_BEEN_SENT_ONCE, true)
                .putString(KEY_REFERRER, null)
                .apply();
    }
}

@@ -345,16 +345,23 @@
 rer_sent
+_v1_8_0
 %22;%0A    p
@@ -2304,38 +2304,63 @@
 rer() %7B%0A        
-return
+boolean isInstallReferrerSent =
  mSharedPreferen
@@ -2425,16 +2425,17 @@
 , false)
+;
 %0A       
@@ -2439,47 +2439,142 @@
     
-        && getInstallReferrer() != null
+boolean isInstallReferrerAvailable = getInstallReferrer() != null;%0A        return isInstallReferrerAvailable && !isInstallReferrerSent
 ;%0A  
@@ -2738,55 +2738,8 @@
 ue)%0A
-                .putString(KEY_REFERRER, null)%0A
     

Fix send install referrer