Skip to content

Commit

Permalink
Release-4.2.2 fixed calling completion handler prior sending data to …
Browse files Browse the repository at this point in the history
…server. (#19)

Bug Fix: Fixes a race condition where the collection handlers say they have completed prior to transmissions of the collection data to servers.

Any users of 4.2.X should upgrade to this to resolve the possible race condition.
  • Loading branch information
Jyothi-HV-r authored Feb 25, 2022
1 parent 0f5384c commit 8d978b9
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 146 deletions.
18 changes: 1 addition & 17 deletions CheckoutExample/.idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion CheckoutExample/.idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion CheckoutExample/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 3 additions & 13 deletions CheckoutExample/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 0 additions & 12 deletions CheckoutExample/.idea/runConfigurations.xml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,65 +10,32 @@
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.kount.api.analytics.AnalyticsCollector;

import java.util.Locale;
import java.util.UUID;

public class MainActivity extends AppCompatActivity implements ActivityCompat.OnRequestPermissionsResultCallback {

final int PERMISSIONS_REQUEST_LOCATION = 0;
static final int MERCHANT_ID = 999999; // Insert your valid merchant ID
static final int ENVIRONMENT = AnalyticsCollector.ENVIRONMENT_TEST;//For production need to add AnalyticsCollector.ENVIRONMENT_PRODUCTION
static String deviceSessionID = "";
private String KEY_UUID = "UUID";

TextView location;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("Sample");
initView();
location = (TextView) findViewById(R.id.location);

//required section
AnalyticsCollector.setMerchantId(MERCHANT_ID);
//end required section

// This turns the alpha collections on(true)/off(false). It defaults to true
AnalyticsCollector.collectAnalytics(true);

// For production you need to add AnalyticsCollector.ENVIRONMENT_PRODUCTION
AnalyticsCollector.setEnvironment(ENVIRONMENT);

//Optional SessionID section
/** If you want to pass in a self generated sessionID(or one given to you by your servers)
* you can set it using colde like this. Otherwise the AnalyticsCollector will generate one
* for you.Make sure you set session id only one time in a user session.
* To do so set your sessionID below.*/
if (savedInstanceState == null) {
deviceSessionID = UUID.randomUUID().toString();
AnalyticsCollector.setSessionId(deviceSessionID);
} else {
deviceSessionID = savedInstanceState.getString(KEY_UUID);
AnalyticsCollector.setSessionId(deviceSessionID);
}
// END OPTIONAL SESSION_ID SECTION

//Request location permission for Android 6.0 & above
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
requestLocationPermissions(this);
else
AnalyticsCollector.collectDeviceDataForSession(this);


}

private void initView() {
final TextView merchant = (TextView) findViewById(R.id.merchant);
final TextView environment = (TextView) findViewById(R.id.environment);
final Button checkoutButton = (Button) findViewById(R.id.checkoutButton);
Expand All @@ -92,42 +59,60 @@ public void onClick(View v) {
startActivity(intent);
}
});
}

void requestLocationPermissions(Activity activity) {
final TextView location = (TextView) findViewById(R.id.location);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// Check for location permissions so the Data Collector can gather the device location
requestLocationPermission(this);
} else {
location.setText("Allowed");
//Calling this will start standard DeviceData Collection
AnalyticsCollector.collectDeviceDataForSession(this);
}
super.onCreate(savedInstanceState);

if (ContextCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)) {
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, AnalyticsCollector.REQUEST_PERMISSION_LOCATION);
}

private void requestLocationPermission(Activity activity) {
if (ContextCompat.checkSelfPermission(
activity,
Manifest.permission.ACCESS_FINE_LOCATION
) != PackageManager.PERMISSION_GRANTED
) {
if (ActivityCompat.shouldShowRequestPermissionRationale(
activity,
Manifest.permission.ACCESS_FINE_LOCATION
)
) {
ActivityCompat.requestPermissions(
activity,
new String[]{(Manifest.permission.ACCESS_FINE_LOCATION)},
AnalyticsCollector.REQUEST_PERMISSION_LOCATION
);
} else {
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, AnalyticsCollector.REQUEST_PERMISSION_LOCATION);
ActivityCompat.requestPermissions(
activity,
new String[]{(Manifest.permission.ACCESS_FINE_LOCATION)},
AnalyticsCollector.REQUEST_PERMISSION_LOCATION
);
}
} else {
//This block executes when permission is already granted.
AnalyticsCollector.collectDeviceDataForSession(activity);
location.setText("Allowed");
AnalyticsCollector.collectDeviceDataForSession(activity);
}
}

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
final TextView location = (TextView) findViewById(R.id.location);
if (requestCode == AnalyticsCollector.REQUEST_PERMISSION_LOCATION) {
//this block executes when a user grant/deny the permission
AnalyticsCollector.collectDeviceDataForSession(this);
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
location.setText("Allowed");
} else {
location.setText("Denied");
}
}
}

@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(KEY_UUID, deviceSessionID);
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}

}
3 changes: 3 additions & 0 deletions CheckoutExample/kotlinexample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@ dependencies {
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation 'com.google.android.instantapps:instantapps:1.1.0'

implementation 'com.google.android.instantapps:instantapps:1.1.0'
implementation 'com.google.code.gson:gson:2.8.6'
implementation group: 'com.android.volley', name: 'volley', version: '1.2.1'
implementation "com.google.android.material:material:1.4.0"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,55 +11,27 @@ import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.kount.api.analytics.AnalyticsCollector
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*

class MainActivity : AppCompatActivity(), ActivityCompat.OnRequestPermissionsResultCallback {
private val ENVIRONMENT: Int = AnalyticsCollector.ENVIRONMENT_TEST//For production need to add AnalyticsCollector.ENVIRONMENT_PRODUCTION
var deviceSessionID: String? = ""
private val KEY_UUID = "UUID"
private val PERMISSIONS_REQUEST_LOCATION = 0
private val ENVIRONMENT: Int =
AnalyticsCollector.ENVIRONMENT_TEST//For production need to add AnalyticsCollector.ENVIRONMENT_PRODUCTION


companion object Cons {
val MERCHANT_ID = 999999 // Insert your valid merchant ID
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "Sample"
initView()

//required section
AnalyticsCollector.setMerchantId(MERCHANT_ID)
//end required section

// This turns the alpha collections on(true)/off(false). It defaults to true
AnalyticsCollector.setMerchantId(MERCHANT_ID)
//This turns the alpha collections on(true)/off(false). It defaults to true
AnalyticsCollector.collectAnalytics(true)

// For production you need to add AnalyticsCollector.ENVIRONMENT_PRODUCTION
AnalyticsCollector.setEnvironment(ENVIRONMENT)

//Optional SessionID section
/** If you want to pass in a self generated sessionID(or one given to you by your servers)
* you can set it using colde like this. Otherwise the AnalyticsCollector will generate one
* for you.Make sure you set session id only one time in a user session.
* To do so set your sessionID below. */
if (savedInstanceState == null) {
deviceSessionID = UUID.randomUUID().toString()
AnalyticsCollector.setSessionId(deviceSessionID!!)
} else {
deviceSessionID = savedInstanceState.getString(KEY_UUID)
AnalyticsCollector.setSessionId(deviceSessionID!!)
}
// END OPTIONAL SESSION_ID SECTION

//Request location permission for Android 6.0 & above
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
requestLocationPermissions(this)
else
AnalyticsCollector.collectDeviceDataForSession(this)
}

private fun initView() {
merchant.text = "$MERCHANT_ID"
when (ENVIRONMENT) {
AnalyticsCollector.ENVIRONMENT_TEST -> environment.text = "Test"
Expand All @@ -71,38 +43,60 @@ class MainActivity : AppCompatActivity(), ActivityCompat.OnRequestPermissionsRes
checkoutButton.setOnClickListener {
startActivity(Intent(this@MainActivity, CollectionActivity::class.java))
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
// Check for location permissions so the Data Collector can gather the device location
requestLocationPermission(this) else {
location.text = "Allowed"

//Calling this will start standard DeviceData Collection
AnalyticsCollector.collectDeviceDataForSession(this)
}
super.onCreate(savedInstanceState)

}

fun requestLocationPermissions(activity: Activity?) {
if (ContextCompat.checkSelfPermission(activity!!, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)) {
ActivityCompat.requestPermissions(activity, arrayOf(
Manifest.permission.ACCESS_FINE_LOCATION), AnalyticsCollector.REQUEST_PERMISSION_LOCATION)
private fun requestLocationPermission(activity: Activity) {
if (ContextCompat.checkSelfPermission(
activity,
Manifest.permission.ACCESS_FINE_LOCATION
) != PackageManager.PERMISSION_GRANTED
) {
if (ActivityCompat.shouldShowRequestPermissionRationale(
activity,
Manifest.permission.ACCESS_FINE_LOCATION
)
) {
ActivityCompat.requestPermissions(
activity,
arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
AnalyticsCollector.REQUEST_PERMISSION_LOCATION
)
} else {
ActivityCompat.requestPermissions(activity, arrayOf(
Manifest.permission.ACCESS_FINE_LOCATION), AnalyticsCollector.REQUEST_PERMISSION_LOCATION)
ActivityCompat.requestPermissions(
activity,
arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
AnalyticsCollector.REQUEST_PERMISSION_LOCATION
)
}
} else {
//This block executes when permission is already granted.
AnalyticsCollector.collectDeviceDataForSession(activity)
location.text = "Allowed"
AnalyticsCollector.collectDeviceDataForSession(activity)
}
}

override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
if (requestCode == AnalyticsCollector.REQUEST_PERMISSION_LOCATION) {
//this block executes when a user grant/deny the permission
AnalyticsCollector.collectDeviceDataForSession(this)
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
location.text = "Allowed"
} else {
location.text = "Denied"
}
}
}

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putString(KEY_UUID, deviceSessionID)
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
}
}
Binary file not shown.

0 comments on commit 8d978b9

Please sign in to comment.