Skip to content

Commit 6490abc

Browse files
authored
Merge pull request #120 from hannesa2/AutomatedCurrentVersionName
Automated versionName
2 parents 1ba27a0 + d6f7e60 commit 6490abc

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ This library provides an easy way to update app with provided apk in release.
2020

2121
2. Display the change log dialog by putting the following code in your activity's `onCreate()` method:
2222

23-
```java
23+
```kotlin
2424
AppUpdateHelper.checkForNewVersion(
25-
MainActivity@ this,
25+
this,
2626
BuildConfig.GIT_REPOSITORY,
27-
BuildConfig.VERSION_NAME
27+
{ msg -> Log.d("result", msg) }
2828
)
2929
```
3030

githubAppUpdate/src/main/java/info/hannes/github/AppUpdateHelper.kt

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,48 @@
11
package info.hannes.github
22

3+
import android.app.Activity
34
import android.app.AlertDialog
5+
import android.content.Context
46
import android.content.Intent
7+
import android.content.pm.PackageInfo
8+
import android.content.pm.PackageManager
59
import android.net.Uri
10+
import android.os.Build
611
import android.util.Log
712
import android.widget.Toast
813
import androidx.appcompat.app.AppCompatActivity
914
import androidx.lifecycle.coroutineScope
1015
import androidx.preference.PreferenceManager
16+
import info.hannes.github.AppUpdateHelper.getVersionName
1117
import kotlinx.coroutines.Dispatchers
1218
import kotlinx.coroutines.launch
1319
import kotlinx.coroutines.withContext
1420
import okhttp3.logging.HttpLoggingInterceptor
1521

1622
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
1844
) = activity.lifecycle.coroutineScope.launch(Dispatchers.Main) {
45+
val currentVersionName = activity.getVersionName()
1946
val key = "LAST_VERSION_CHECK"
2047
val prefs = PreferenceManager.getDefaultSharedPreferences(activity)
2148
val gitRepoUrlHttps = gitRepoUrl.replace("git@", "https//")

sample/src/main/java/info/hannes/github/sample/MainActivity.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ class MainActivity : AppCompatActivity() {
1919
}
2020

2121
AppUpdateHelper.checkForNewVersion(
22-
this,
23-
BuildConfig.GIT_REPOSITORY,
24-
BuildConfig.VERSION_NAME,
25-
{ msg -> Log.d("result", msg) }
22+
this,
23+
BuildConfig.GIT_REPOSITORY,
24+
{ msg -> Log.d("result", msg) },
25+
force = true // just to enable debugging, without you can only debug once a day
2626
)
2727
}
2828

0 commit comments

Comments
 (0)