package com.learnium.RNDeviceInfo;

import android.content.SharedPreferences;
import android.content.Context;
import android.os.RemoteException;

import com.android.installreferrer.api.InstallReferrerClient;
import com.android.installreferrer.api.InstallReferrerStateListener;
import com.android.installreferrer.api.ReferrerDetails;

import static com.android.installreferrer.api.InstallReferrerClient.InstallReferrerResponse;

public class RNInstallReferrerClient implements InstallReferrerStateListener {

  private SharedPreferences sharedPreferences;
  private InstallReferrerClient referrerClient;

  RNInstallReferrerClient(Context context) {
    sharedPreferences = context.getSharedPreferences("react-native-device-info", Context.MODE_PRIVATE);
    referrerClient = InstallReferrerClient.newBuilder(context).build();
    referrerClient.startConnection(this);
  }

  private String getInstallReferrer() {
    try {
      return referrerClient
        .getInstallReferrer()
        .getInstallReferrer();
    } catch (RemoteException e) {
      e.printStackTrace();
      return null;
    }
  }

  referrerClient.startConnection(new InstallReferrerStateListener() {
    @Override
    public void onInstallReferrerSetupFinished(int responseCode) {
      switch (responseCode) {
        case InstallReferrerResponse.OK:
          // Connection established
          SharedPreferences.Editor editor = sharedPreferences.edit();
          editor.putString("installReferrer", getInstallReferrer());
          editor.apply();
          break;
        case InstallReferrerResponse.FEATURE_NOT_SUPPORTED:
          // API not available on the current Play Store app
          break;
        case InstallReferrerResponse.SERVICE_UNAVAILABLE:
          // Connection could not be established
          break;
      }
    }

    @Override
    public void onInstallReferrerServiceDisconnected() {
      // Try to restart the connection on the next request to
      // Google Play by calling the startConnection() method.
    }
  });
}
@@ -137,16 +137,41 @@
 ception;
+%0Aimport android.util.Log;
 %0A%0Aimport
@@ -356,178 +356,44 @@
 s;%0A%0A
-import static com.android.installreferrer.api.InstallReferrerClient.InstallReferrerResponse;%0A%0Apublic class RNInstallReferrerClient implements InstallReferrerStateListener
+public class RNInstallReferrerClient
  %7B%0A%0A
@@ -471,17 +471,18 @@
 rClient 
-r
+mR
 eferrerC
@@ -638,25 +638,26 @@
 IVATE);%0A    
-r
+mR
 eferrerClien
@@ -715,17 +715,18 @@
 ();%0A    
-r
+mR
 eferrerC
@@ -751,12 +751,36 @@
 ion(
-this
+installReferrerStateListener
 );%0A 
@@ -846,17 +846,18 @@
  return 
-r
+mR
 eferrerC
@@ -862,32 +862,38 @@
 rClient%0A        
+      
 .getInstallRefer
@@ -898,16 +898,22 @@
 errer()%0A
+      
         
@@ -1032,39 +1032,80 @@
 %0A%0A  
-referrerClient.startConnection(
+private InstallReferrerStateListener installReferrerStateListener =%0A    
 new 
@@ -1137,24 +1137,26 @@
 ner() %7B%0A    
+  
 @Override%0A  
@@ -1144,36 +1144,32 @@
 %0A      @Override
-%0A   
  public void onI
@@ -1212,24 +1212,26 @@
 onseCode) %7B%0A
+  
       switch
@@ -1248,37 +1248,61 @@
 Code) %7B%0A        
+  
 case 
+InstallReferrerClient.
 InstallReferrerR
@@ -1313,16 +1313,18 @@
 nse.OK:%0A
+  
         
@@ -1343,32 +1343,370 @@
 ion established%0A
+            try %7B%0A              if (BuildConfig.DEBUG) Log.d(%22InstallReferrerState%22, %22OK%22);%0A              ReferrerDetails response = mReferrerClient.getInstallReferrer();%0A              response.getInstallReferrer();%0A              response.getReferrerClickTimestampSeconds();%0A              response.getInstallBeginTimestampSeconds();%0A%0A    
           Shared
@@ -1755,24 +1755,28 @@
 ces.edit();%0A
+    
           ed
@@ -1838,24 +1838,28 @@
 ;%0A          
+ 
+   
 editor.apply
@@ -1858,24 +1858,165 @@
 or.apply();%0A
+%0A              mReferrerClient.endConnection();%0A            %7D catch (RemoteException e) %7B%0A              e.printStackTrace();%0A            %7D%0A  
           br
@@ -2020,37 +2020,61 @@
  break;%0A        
+ 
+ 
 case 
+InstallReferrerClient.
 InstallReferrerR
@@ -2104,16 +2104,109 @@
 PORTED:%0A
+            if (BuildConfig.DEBUG) Log.d(%22InstallReferrerState%22, %22FEATURE_NOT_SUPPORTED%22);%0A  
         
@@ -2260,39 +2260,43 @@
 e app%0A          
+  
 break;%0A
+  
         case Ins
@@ -2284,32 +2284,54 @@
 %0A          case 
+InstallReferrerClient.
 InstallReferrerR
@@ -2359,16 +2359,107 @@
 ILABLE:%0A
+            if (BuildConfig.DEBUG) Log.d(%22InstallReferrerState%22, %22SERVICE_UNAVAILABLE%22);%0A  
         
@@ -2505,31 +2505,35 @@
 d%0A          
+  
 break;%0A
+  
       %7D%0A    
@@ -2536,15 +2536,19 @@
     
+  
 %7D%0A%0A    
+ 
+ 
 @Ove
@@ -2552,20 +2552,16 @@
 Override
-%0A   
  public 
@@ -2612,16 +2612,18 @@
 %7B%0A      
+  
 // Try t
@@ -2670,16 +2670,18 @@
 uest to%0A
+  
       //
@@ -2734,21 +2734,94 @@
 method.%0A
+        mReferrerClient.startConnection(installReferrerStateListener);%0A  
     %7D%0A  
 %7D);%0A%7D
@@ -2819,9 +2819,10 @@
 %7D%0A  
+  
 %7D
-)
 ;%0A%7D

Enhance InstallReferrer response and loggers