|
1 | 1 | package info.hannes.github
|
2 | 2 |
|
| 3 | +import android.app.Activity |
3 | 4 | import android.app.AlertDialog
|
| 5 | +import android.content.Context |
4 | 6 | import android.content.Intent
|
| 7 | +import android.content.pm.PackageInfo |
| 8 | +import android.content.pm.PackageManager |
5 | 9 | import android.net.Uri
|
| 10 | +import android.os.Build |
6 | 11 | import android.util.Log
|
7 | 12 | import android.widget.Toast
|
8 | 13 | import androidx.appcompat.app.AppCompatActivity
|
9 | 14 | import androidx.lifecycle.coroutineScope
|
10 | 15 | import androidx.preference.PreferenceManager
|
| 16 | +import info.hannes.github.AppUpdateHelper.getVersionName |
11 | 17 | import kotlinx.coroutines.Dispatchers
|
12 | 18 | import kotlinx.coroutines.launch
|
13 | 19 | import kotlinx.coroutines.withContext
|
14 | 20 | import okhttp3.logging.HttpLoggingInterceptor
|
15 | 21 |
|
16 | 22 | object AppUpdateHelper {
|
17 |
| - fun checkForNewVersion(activity: AppCompatActivity, gitRepoUrl: String, currentVersionName: String, callback: ((String) -> Unit)? = null, force: Boolean = false |
| 23 | + |
| 24 | + private fun Activity.getVersionName(): String = try { |
| 25 | + this.getPackageInfo().versionName ?: "" |
| 26 | + } catch (e: PackageManager.NameNotFoundException) { |
| 27 | + "" |
| 28 | + } |
| 29 | + |
| 30 | + @Suppress("DEPRECATION") |
| 31 | + fun Context.getPackageInfo(): PackageInfo { |
| 32 | + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { |
| 33 | + packageManager.getPackageInfo(packageName, PackageManager.PackageInfoFlags.of(0)) |
| 34 | + } else { |
| 35 | + packageManager.getPackageInfo(packageName, 0) |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + fun checkForNewVersion( |
| 40 | + activity: AppCompatActivity, |
| 41 | + gitRepoUrl: String, |
| 42 | + callback: ((String) -> Unit)? = null, |
| 43 | + force: Boolean = false |
18 | 44 | ) = activity.lifecycle.coroutineScope.launch(Dispatchers.Main) {
|
| 45 | + val currentVersionName = activity.getVersionName() |
19 | 46 | val key = "LAST_VERSION_CHECK"
|
20 | 47 | val prefs = PreferenceManager.getDefaultSharedPreferences(activity)
|
21 | 48 | val gitRepoUrlHttps = gitRepoUrl.replace("git@", "https//")
|
|
0 commit comments