Skip to content

Commit b6d01de

Browse files
add last app version (#702)
* add last app version * remove unused changes * fix: encryption logic and add last app version * minor changes * remove testing crash * cleanup * spotless apply
1 parent 5feaa98 commit b6d01de

File tree

4 files changed

+71
-65
lines changed

4 files changed

+71
-65
lines changed

app/src/main/java/com/yogeshpaliyal/keypass/ui/CrashActivity.kt

Lines changed: 58 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ import android.net.Uri
66
import android.os.Build
77
import android.os.Bundle
88
import androidx.appcompat.app.AppCompatActivity
9+
import com.yogeshpaliyal.common.data.UserSettings
10+
import com.yogeshpaliyal.common.utils.getUserSettings
911
import com.yogeshpaliyal.keypass.BuildConfig
1012
import com.yogeshpaliyal.keypass.databinding.ActivityCrashBinding
1113
import dagger.hilt.android.AndroidEntryPoint
14+
import kotlinx.coroutines.runBlocking
1215
import java.lang.StringBuilder
1316

1417
@AndroidEntryPoint
@@ -31,35 +34,70 @@ class CrashActivity : AppCompatActivity() {
3134
binding = ActivityCrashBinding.inflate(layoutInflater)
3235
setContentView(binding.root)
3336

34-
binding.txtCrash.text = intent.extras?.getString(ARG_DATA)
37+
binding.txtCrash.text = getCrashWithMetaData(intent.extras?.getString(ARG_DATA))
3538

3639
binding.btnSendFeedback.setOnClickListener {
37-
val deviceInfo = StringBuilder()
38-
deviceInfo.append(binding.txtCrash.text.toString())
39-
try {
40-
deviceInfo.append("\n")
41-
deviceInfo.append("App Version: " + BuildConfig.VERSION_NAME)
42-
deviceInfo.append("\n")
43-
deviceInfo.append("Brand Name: " + Build.BRAND)
44-
deviceInfo.append("\n")
45-
deviceInfo.append("Manufacturer Name: " + Build.MANUFACTURER)
46-
deviceInfo.append("\n")
47-
deviceInfo.append("Device Name: " + Build.MODEL)
48-
deviceInfo.append("\n")
49-
deviceInfo.append("Device API Version: " + Build.VERSION.SDK_INT)
50-
deviceInfo.append("\n")
51-
} catch (e: Exception) {
52-
e.printStackTrace()
53-
}
54-
5540
val intent = Intent(Intent.ACTION_SENDTO)
5641
intent.data = Uri.parse("mailto:")
5742

5843
intent.putExtra(Intent.EXTRA_EMAIL, arrayOf("yogeshpaliyal.foss+keypass@gmail.com"))
5944
intent.putExtra(Intent.EXTRA_SUBJECT, "Crash Report in KeyPass")
60-
intent.putExtra(Intent.EXTRA_TEXT, deviceInfo.toString())
45+
intent.putExtra(Intent.EXTRA_TEXT, binding.txtCrash.text.toString())
6146

6247
startActivity(Intent.createChooser(intent, ""))
6348
}
6449
}
50+
51+
private fun getCrashWithMetaData(crashData: String?): String {
52+
var userSettings: UserSettings? = null
53+
var currentAppVersion = "Not able to fetch"
54+
var lastAppVersion = "Not able to fetch"
55+
runBlocking {
56+
try {
57+
userSettings = getUserSettings()
58+
currentAppVersion = userSettings?.currentAppVersion.toString()
59+
lastAppVersion = userSettings?.lastAppVersion.toString()
60+
} catch (e: Exception) {
61+
currentAppVersion = e.message ?: "Not able to fetch"
62+
lastAppVersion = e.message ?: "Not able to fetch"
63+
e.printStackTrace()
64+
}
65+
}
66+
val installerPackageName = getInstallerPackageName(this, BuildConfig.APPLICATION_ID)
67+
val deviceInfo = StringBuilder()
68+
deviceInfo.append(crashData)
69+
try {
70+
deviceInfo.append("\n")
71+
deviceInfo.append("App Version from Build: " + BuildConfig.VERSION_NAME)
72+
deviceInfo.append("\n")
73+
deviceInfo.append("Current App Version: $currentAppVersion")
74+
deviceInfo.append("\n")
75+
deviceInfo.append("Previous App Version: $lastAppVersion")
76+
deviceInfo.append("\n")
77+
deviceInfo.append("Installed from: $installerPackageName")
78+
deviceInfo.append("\n")
79+
deviceInfo.append("Brand Name: " + Build.BRAND)
80+
deviceInfo.append("\n")
81+
deviceInfo.append("Manufacturer Name: " + Build.MANUFACTURER)
82+
deviceInfo.append("\n")
83+
deviceInfo.append("Device Name: " + Build.MODEL)
84+
deviceInfo.append("\n")
85+
deviceInfo.append("Device API Version: " + Build.VERSION.SDK_INT)
86+
deviceInfo.append("\n")
87+
} catch (e: Exception) {
88+
e.printStackTrace()
89+
}
90+
return deviceInfo.toString()
91+
}
92+
93+
fun getInstallerPackageName(context: Context, packageName: String): String? {
94+
kotlin.runCatching {
95+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
96+
return context.packageManager.getInstallSourceInfo(packageName).installingPackageName
97+
}
98+
@Suppress("DEPRECATION")
99+
return context.packageManager.getInstallerPackageName(packageName)
100+
}
101+
return null
102+
}
65103
}

app/src/main/java/com/yogeshpaliyal/keypass/ui/nav/DashboardComposeActivity.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ import androidx.compose.runtime.getValue
1919
import androidx.compose.ui.Modifier
2020
import androidx.compose.ui.platform.LocalContext
2121
import com.yogeshpaliyal.common.data.UserSettings
22+
import com.yogeshpaliyal.common.utils.getUserSettings
2223
import com.yogeshpaliyal.common.utils.getUserSettingsFlow
2324
import com.yogeshpaliyal.common.utils.migrateOldDataToNewerDataStore
25+
import com.yogeshpaliyal.common.utils.setUserSettings
2426
import com.yogeshpaliyal.keypass.BuildConfig
2527
import com.yogeshpaliyal.keypass.ui.addTOTP.TOTPScreen
2628
import com.yogeshpaliyal.keypass.ui.auth.AuthScreen
@@ -66,6 +68,7 @@ class DashboardComposeActivity : AppCompatActivity() {
6668
WindowManager.LayoutParams.FLAG_SECURE
6769
)
6870
}
71+
6972
setContent {
7073
val localUserSettings by getUserSettingsFlow().collectAsState(initial = UserSettings())
7174

@@ -79,6 +82,12 @@ class DashboardComposeActivity : AppCompatActivity() {
7982

8083
LaunchedEffect(key1 = Unit, block = {
8184
migrateOldDataToNewerDataStore()
85+
val userSettings = getUserSettings()
86+
val buildConfigVersion = BuildConfig.VERSION_CODE
87+
val currentAppVersion = userSettings.currentAppVersion
88+
if (buildConfigVersion != currentAppVersion) {
89+
applicationContext.setUserSettings(userSettings.copy(lastAppVersion = currentAppVersion, currentAppVersion = buildConfigVersion))
90+
}
8291
})
8392
}
8493
}

common/src/main/java/com/yogeshpaliyal/common/data/UserSettings.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ data class UserSettings(
1616
val backupDirectory: String? = null,
1717
val backupTime: Long? = null,
1818
val autoBackupEnable: Boolean = false,
19-
val overrideAutoBackup: Boolean = false
19+
val overrideAutoBackup: Boolean = false,
20+
val lastAppVersion: Int? = null,
21+
val currentAppVersion: Int? = null
2022
) {
2123
fun isKeyPresent() = backupKey != null
2224
}

common/src/main/java/com/yogeshpaliyal/common/utils/CryptoManager.kt

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package com.yogeshpaliyal.common.utils
22

33
import android.security.keystore.KeyGenParameterSpec
44
import android.security.keystore.KeyProperties
5-
import java.io.ByteArrayInputStream
65
import java.io.ByteArrayOutputStream
76
import java.io.InputStream
87
import java.io.OutputStream
@@ -64,50 +63,8 @@ class CryptoManager {
6463
}
6564
}
6665

67-
private fun decryptOld(inputStream: InputStream): ByteArray? {
66+
fun decrypt(inputStream: InputStream): ByteArray {
6867
return inputStream.use {
69-
val ivSize = inputStream.read()
70-
val iv = ByteArray(ivSize)
71-
inputStream.read(iv)
72-
val cipher = Cipher.getInstance(TRANSFORMATION)
73-
cipher.init(Cipher.DECRYPT_MODE, getKey(), IvParameterSpec(iv))
74-
val decryptedSize = inputStream.read()
75-
val decryptedData = ByteArray(decryptedSize)
76-
inputStream.read(decryptedData)
77-
78-
// check if more data is there
79-
try {
80-
val input = inputStream.read()
81-
if (input != -1) {
82-
return null
83-
}
84-
} catch (e: Exception) {
85-
e.printStackTrace()
86-
}
87-
88-
return cipher.doFinal(decryptedData)
89-
}
90-
}
91-
92-
private fun copyInputStream(inputS: InputStream): InputStream {
93-
val byteArrayOutputStream = ByteArrayOutputStream()
94-
inputS.copyTo(byteArrayOutputStream, 1024)
95-
val data = byteArrayOutputStream.toByteArray()
96-
return ByteArrayInputStream(data)
97-
}
98-
99-
fun decrypt(inputS: InputStream): ByteArray {
100-
return inputS.use {
101-
// TODO remove after migrations
102-
val data = inputS.readBytes()
103-
val inputStreamForOld = copyInputStream(ByteArrayInputStream(data))
104-
val oldDecrypt = decryptOld(inputStreamForOld)
105-
if (oldDecrypt != null) {
106-
return oldDecrypt
107-
}
108-
109-
val inputStream = copyInputStream(ByteArrayInputStream(data))
110-
11168
val ivSize = inputStream.read()
11269
val iv = ByteArray(ivSize)
11370
inputStream.read(iv)

0 commit comments

Comments
 (0)