Skip to content

Commit

Permalink
Merge pull request #120 from hannesa2/AutomatedCurrentVersionName
Browse files Browse the repository at this point in the history
Automated versionName
  • Loading branch information
hannesa2 committed Nov 20, 2022
2 parents 1ba27a0 + d6f7e60 commit 6490abc
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ This library provides an easy way to update app with provided apk in release.

2. Display the change log dialog by putting the following code in your activity's `onCreate()` method:
```java
```kotlin
AppUpdateHelper.checkForNewVersion(
MainActivity@ this,
this,
BuildConfig.GIT_REPOSITORY,
BuildConfig.VERSION_NAME
{ msg -> Log.d("result", msg) }
)
```
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,48 @@
package info.hannes.github

import android.app.Activity
import android.app.AlertDialog
import android.content.Context
import android.content.Intent
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.coroutineScope
import androidx.preference.PreferenceManager
import info.hannes.github.AppUpdateHelper.getVersionName
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import okhttp3.logging.HttpLoggingInterceptor

object AppUpdateHelper {
fun checkForNewVersion(activity: AppCompatActivity, gitRepoUrl: String, currentVersionName: String, callback: ((String) -> Unit)? = null, force: Boolean = false

private fun Activity.getVersionName(): String = try {
this.getPackageInfo().versionName ?: ""
} catch (e: PackageManager.NameNotFoundException) {
""
}

@Suppress("DEPRECATION")
fun Context.getPackageInfo(): PackageInfo {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
packageManager.getPackageInfo(packageName, PackageManager.PackageInfoFlags.of(0))
} else {
packageManager.getPackageInfo(packageName, 0)
}
}

fun checkForNewVersion(
activity: AppCompatActivity,
gitRepoUrl: String,
callback: ((String) -> Unit)? = null,
force: Boolean = false
) = activity.lifecycle.coroutineScope.launch(Dispatchers.Main) {
val currentVersionName = activity.getVersionName()
val key = "LAST_VERSION_CHECK"
val prefs = PreferenceManager.getDefaultSharedPreferences(activity)
val gitRepoUrlHttps = gitRepoUrl.replace("git@", "https//")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ class MainActivity : AppCompatActivity() {
}

AppUpdateHelper.checkForNewVersion(
this,
BuildConfig.GIT_REPOSITORY,
BuildConfig.VERSION_NAME,
{ msg -> Log.d("result", msg) }
this,
BuildConfig.GIT_REPOSITORY,
{ msg -> Log.d("result", msg) },
force = true // just to enable debugging, without you can only debug once a day
)
}

Expand Down

0 comments on commit 6490abc

Please sign in to comment.