import android.content.Context
import android.os.Bundle
import android.os.RemoteException
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.*
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.*
import com.android.installreferrer.api.InstallReferrerClient
import com.android.installreferrer.api.InstallReferrerStateListener
import com.android.installreferrer.api.ReferrerDetails
import com.example.newcanaryproject.ui.theme.*
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
NewCanaryProjectTheme {
// in the below line, we are specifying background color for our application
Surface(
// in the below line, we are specifying modifier and color for our app
modifier = Modifier.fillMaxSize(), color = MaterialTheme.colors.background
) {
// in the below line, we are specifying theme as scaffold.
Scaffold(
// in scaffold we are specifying top bar.
topBar = {
// inside top bar we are specifying background color.
TopAppBar(backgroundColor = greenColor,
// along with that we are specifying title for our top bar.
title = {
// in the top bar we are specifying tile as a text
Text(
// in the below line, we are specifying text to display in top app bar.
text = "Google Play Install Referrer",
// in the below line, we are specifying modifier to fill max width.
modifier = Modifier.fillMaxWidth(),
// in the below line, we are specifying text alignment.
textAlign = TextAlign.Center,
// in the below line, we are specifying color for our text.
color = Color.White
)
})
}) {
// in the below line, we are calling pop window dialog method to display ui.
GooglePlayInstallReferrer(LocalContext.current)
}
}
}
}
}
}
@Composable
fun GooglePlayInstallReferrer(ctx: Context) {
// in the below line, we are creating a variable for referrer
val referrer = remember {
mutableStateOf("")
}
// in the below line, we are building our install referrer client and building it.
var referrerClient: InstallReferrerClient = InstallReferrerClient.newBuilder(ctx).build();
// in the below line, we are starting its connection.
referrerClient.startConnection(object : InstallReferrerStateListener {
override fun onInstallReferrerSetupFinished(responseCode: Int) {
// this method is called when install referrer setup is finished.
when (responseCode) {
InstallReferrerClient.InstallReferrerResponse.OK -> {
// this case is called when the status is OK and
var response: ReferrerDetails? = null
try {
// in the below line, we are getting referrer details
// by calling get install referrer.
response = referrerClient.installReferrer
// in the below line, we are getting referrer url.
val referrerUrl = response.installReferrer
// in the below line, we are getting referrer click time.
val referrerClickTime = response.referrerClickTimestampSeconds
// in the below line, we are getting app install time
val appInstallTime = response.installBeginTimestampSeconds
// in the below line, we are getting our time when
// user has used our apps instant experience.
val instantExperienceLaunched = response.googlePlayInstantParam
// in the below line, we are getting our
// apps install referrer.
var refrer = response.installReferrer
// in the below line, we are setting all detail to our text view.
referrer.value =
"Referrer is : \n$referrerUrl\nReferrer Click Time is : $referrerClickTime\nApp Install Time : $appInstallTime"
} catch (e: RemoteException) {
// handling error case.
e.printStackTrace()
}
}
InstallReferrerClient.InstallReferrerResponse.FEATURE_NOT_SUPPORTED ->
// API not available on the current Play Store app.
Toast.makeText(
ctx,
"Feature not supported..",
Toast.LENGTH_SHORT
).show()
InstallReferrerClient.InstallReferrerResponse.SERVICE_UNAVAILABLE ->
// Connection couldn't be established.
Toast.makeText(
ctx,
"Fail to establish connection",
Toast.LENGTH_SHORT
).show()
}
}
override fun onInstallReferrerServiceDisconnected() {
// Try to restart the connection on the next request to
// Google Play by calling the startConnection() method.
Toast.makeText(ctx, "Service disconnected..", Toast.LENGTH_SHORT)
.show()
}
})
// in the below line, we are creating a column.
Column(
// in this column we are specifying
// modifier to add padding and fill
// max size
modifier = Modifier
.fillMaxSize()
.padding(horizontal = 20.dp),
// in the below line, line we are adding horizontal alignment
// and vertical arrangement
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
// in the below line, line we are creating a text view.
Text(
text = "Google Play Install Referrer",
color = greenColor,
fontSize = 20.sp,
fontWeight = FontWeight.Bold
)
// in the below line, we are adding a spacer.
Spacer(modifier = Modifier.height(20.dp))
// in the below line, we are creating a simple text
Text(
text = referrer.value,
color = Color.Black,
fontSize = 18.sp,
textAlign = TextAlign.Center
)
}
}