From 1c0b8fd2db0386fda1cc1344854632eac2c3f697 Mon Sep 17 00:00:00 2001 From: Shabinder Singh Date: Sat, 29 May 2021 15:56:26 +0530 Subject: [PATCH] Dialog Fixes and Update Notification Fixes --- .../main/java/com/shabinder/spotiflyer/App.kt | 4 ++ .../com/shabinder/spotiflyer/MainActivity.kt | 23 +++++++--- .../spotiflyer/ui/AnalyticsDialog.kt | 19 ++++++-- .../spotiflyer/ui/PermissionDialog.kt | 4 +- .../spotiflyer/utils/SignatureVerification.kt | 43 +++++++++++++++++++ 5 files changed, 81 insertions(+), 12 deletions(-) create mode 100644 android/src/main/java/com/shabinder/spotiflyer/utils/SignatureVerification.kt diff --git a/android/src/main/java/com/shabinder/spotiflyer/App.kt b/android/src/main/java/com/shabinder/spotiflyer/App.kt index c66d93c6..21218117 100644 --- a/android/src/main/java/com/shabinder/spotiflyer/App.kt +++ b/android/src/main/java/com/shabinder/spotiflyer/App.kt @@ -35,6 +35,10 @@ import org.matomo.sdk.TrackerBuilder class App: Application(), KoinComponent { + companion object { + const val SIGNATURE_HEX = "53304f6d75736a2f30484230334c454b714753525763724259444d3d0a" + } + val tracker: Tracker by lazy { TrackerBuilder.createDefault( "https://kind-grasshopper-73.telebit.io/matomo/matomo.php", 1) diff --git a/android/src/main/java/com/shabinder/spotiflyer/MainActivity.kt b/android/src/main/java/com/shabinder/spotiflyer/MainActivity.kt index 0cafb6ec..33ff834b 100644 --- a/android/src/main/java/com/shabinder/spotiflyer/MainActivity.kt +++ b/android/src/main/java/com/shabinder/spotiflyer/MainActivity.kt @@ -124,9 +124,13 @@ class MainActivity : ComponentActivity() { var askForAnalyticsPermission by remember { mutableStateOf(false) } AnalyticsDialog( askForAnalyticsPermission, - dir::enableAnalytics, + enableAnalytics = { + dir.enableAnalytics() + dir.firstLaunchDone() + }, dismissDialog = { askForAnalyticsPermission = false + dir.firstLaunchDone() } ) @@ -136,7 +140,6 @@ class MainActivity : ComponentActivity() { delay(2500) // Ask For Analytics Permission on first Dialog askForAnalyticsPermission = true - dir.firstLaunchDone() } } } @@ -147,12 +150,20 @@ class MainActivity : ComponentActivity() { } private fun initialise() { - checkIfLatestVersion() - handleIntentFromExternalActivity() - if(dir.isAnalyticsEnabled){ - // Download/App Install Event + val isGithubRelease = checkAppSignature(this).also { + Log.i("SpotiFlyer Github Rel.:",it.toString()) + } + /* + * Only Send an `Update Notification` on Github Release Builds + * and Track Downloads for all other releases like F-Droid, + * for `Github Downloads` we will track Downloads using : https://tooomm.github.io/github-release-stats/?username=Shabinder&repository=SpotiFlyer + * */ + if(isGithubRelease) { checkIfLatestVersion() } + if(dir.isAnalyticsEnabled && !isGithubRelease) { + // Download/App Install Event for F-Droid builds TrackHelper.track().download().with(tracker) } + handleIntentFromExternalActivity() } @Composable diff --git a/android/src/main/java/com/shabinder/spotiflyer/ui/AnalyticsDialog.kt b/android/src/main/java/com/shabinder/spotiflyer/ui/AnalyticsDialog.kt index 9d47b52b..f047c62b 100644 --- a/android/src/main/java/com/shabinder/spotiflyer/ui/AnalyticsDialog.kt +++ b/android/src/main/java/com/shabinder/spotiflyer/ui/AnalyticsDialog.kt @@ -3,6 +3,9 @@ package com.shabinder.spotiflyer.ui import androidx.compose.animation.AnimatedVisibility import androidx.compose.animation.ExperimentalAnimationApi import androidx.compose.foundation.background +import androidx.compose.foundation.clickable +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxWidth @@ -13,6 +16,7 @@ import androidx.compose.material.Icon import androidx.compose.material.Text import androidx.compose.material.TextButton import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.rounded.Close import androidx.compose.material.icons.rounded.Insights import androidx.compose.runtime.Composable import androidx.compose.ui.Alignment @@ -38,10 +42,17 @@ fun AnalyticsDialog( AlertDialog( onDismissRequest = dismissDialog, title = { - Row(verticalAlignment = Alignment.CenterVertically) { - Icon(Icons.Rounded.Insights,"Analytics", Modifier.size(52.dp)) - Spacer(Modifier.padding(horizontal = 4.dp)) + Row(verticalAlignment = Alignment.CenterVertically,horizontalArrangement = Arrangement.SpaceBetween) { + Icon(Icons.Rounded.Insights,"Analytics", Modifier.size(32.dp)) + Spacer(Modifier.padding(horizontal = 8.dp)) Text("Grant Analytics",style = SpotiFlyerTypography.h5,textAlign = TextAlign.Center) + Spacer(Modifier.padding(horizontal = 8.dp)) + Column { + Icon(Icons.Rounded.Close,"Decline Analytics", Modifier.size(24.dp).clickable { + dismissDialog() + }) + Spacer(Modifier.padding(vertical = 8.dp)) + } } }, backgroundColor = Color.DarkGray, @@ -61,7 +72,7 @@ fun AnalyticsDialog( text = { Text("Your Data is Anonymized and will never be shared with any 3rd party service",style = SpotiFlyerTypography.body2,textAlign = TextAlign.Center) }, - properties = DialogProperties(dismissOnBackPress = true,dismissOnClickOutside = false) + properties = DialogProperties(dismissOnBackPress = false,dismissOnClickOutside = false) ) } } \ No newline at end of file diff --git a/android/src/main/java/com/shabinder/spotiflyer/ui/PermissionDialog.kt b/android/src/main/java/com/shabinder/spotiflyer/ui/PermissionDialog.kt index 468e0328..62b8c263 100644 --- a/android/src/main/java/com/shabinder/spotiflyer/ui/PermissionDialog.kt +++ b/android/src/main/java/com/shabinder/spotiflyer/ui/PermissionDialog.kt @@ -104,7 +104,7 @@ fun PermissionDialog( ) } } - Row( + /*Row( modifier = Modifier.fillMaxWidth().padding(top = 6.dp), verticalAlignment = Alignment.CenterVertically ) { @@ -120,7 +120,7 @@ fun PermissionDialog( style = SpotiFlyerTypography.subtitle2, ) } - } + }*/ } } ) diff --git a/android/src/main/java/com/shabinder/spotiflyer/utils/SignatureVerification.kt b/android/src/main/java/com/shabinder/spotiflyer/utils/SignatureVerification.kt new file mode 100644 index 00000000..176ae381 --- /dev/null +++ b/android/src/main/java/com/shabinder/spotiflyer/utils/SignatureVerification.kt @@ -0,0 +1,43 @@ +package com.shabinder.spotiflyer.utils + +import android.annotation.SuppressLint +import android.content.Context +import android.content.pm.PackageInfo +import android.content.pm.PackageManager +import android.util.Base64 +import android.util.Log +import com.shabinder.spotiflyer.App +import java.security.MessageDigest + +@Suppress("DEPRECATION") +@SuppressLint("PackageManagerGetSignatures") +fun checkAppSignature(context: Context): Boolean { + try { + val packageInfo: PackageInfo = + context.packageManager.getPackageInfo(context.packageName, PackageManager.GET_SIGNATURES) + for (signature in packageInfo.signatures) { + val md: MessageDigest = MessageDigest.getInstance("SHA") + md.update(signature.toByteArray()) + val currentSignature: String = Base64.encodeToString(md.digest(), Base64.DEFAULT) + // Log.d("REMOVE_ME", "Include this string as a value for SIGNATURE:$currentSignature") + // Log.d("REMOVE_ME HEX", "Include this string as a value for SIGNATURE Hex:${currentSignature.toByteArray().toHEX()}") + + //compare signatures + if (App.SIGNATURE_HEX == currentSignature.toByteArray().toHEX()) { + return true + } + } + } catch (e: Exception) { + e.printStackTrace() + //assumes an issue in checking signature., but we let the caller decide on what to do. + } + return false +} + +fun ByteArray.toHEX(): String { + val builder = StringBuilder() + for (aByte in this) { + builder.append(String.format("%02x", aByte)) + } + return builder.toString() +} \ No newline at end of file