diff --git a/build.gradle b/build.gradle index 15f4c64..db20f39 100644 --- a/build.gradle +++ b/build.gradle @@ -19,6 +19,7 @@ allprojects { repositories { google() mavenCentral() + jcenter() } } diff --git a/githubAppUpdate/build.gradle b/githubAppUpdate/build.gradle index 719c214..50960ff 100644 --- a/githubAppUpdate/build.gradle +++ b/githubAppUpdate/build.gradle @@ -9,7 +9,7 @@ android { defaultConfig { targetSdkVersion 33 - minSdkVersion 21 + minSdkVersion 26 project.archivesBaseName = "githubAppUpdate" } } @@ -18,6 +18,10 @@ dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" implementation 'com.google.code.gson:gson:2.10' implementation "androidx.work:work-runtime-ktx:2.7.1" + implementation 'org.jfrog.artifactory.client:artifactory-java-client-services:2.13.0' +// implementation 'org.apache.httpcomponents:httpclient:4.5.13' +// api 'org.apache.httpcomponents:httpcore:4.4.14' +// api 'org.apache.httpcomponents:httpmime:4.5.6' implementation "com.squareup.retrofit2:retrofit:2.9.0" implementation "androidx.appcompat:appcompat:1.5.1" diff --git a/githubAppUpdate/src/main/java/info/hannes/github/AppUpdateHelper.kt b/githubAppUpdate/src/main/java/info/hannes/github/AppUpdateHelper.kt index 1d44596..d30d3ff 100644 --- a/githubAppUpdate/src/main/java/info/hannes/github/AppUpdateHelper.kt +++ b/githubAppUpdate/src/main/java/info/hannes/github/AppUpdateHelper.kt @@ -21,6 +21,8 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import okhttp3.logging.HttpLoggingInterceptor +import org.jfrog.artifactory.client.Artifactory +import org.jfrog.artifactory.client.ArtifactoryClientBuilder import retrofit2.Response import java.util.concurrent.TimeUnit @@ -48,6 +50,41 @@ object AppUpdateHelper { DownloadWorker.run(activity, currentVersionName, gitRepoUrl, repeatTime, timeUnit) } + fun checkArtifactoryDialog( + 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) + + if (force || prefs.getLong(key, 0) < System.currentTimeMillis() - 1000 * 3600 * 24 / 24 / 60 * 5) { + try { + val versionList = requestArtifactoryVersions(gitRepoUrl) + prefs.edit().putLong(key, System.currentTimeMillis()).apply() + + versionList?.body()?.firstOrNull()?.let { release -> + val assetApk = release.assets.find { it.name.endsWith("release.apk") } + + Log.d("AppUpdateHelper", release.tagName + " > " + currentVersionName + " " + (release.tagName > currentVersionName)) + callback?.invoke(release.tagName) + if (release.tagName > currentVersionName) { + askUser(activity, currentVersionName, release, assetApk) + } else { + callback?.invoke("Nothing to do with ${release.tagName}") + } + } + } catch (e: Exception) { + Log.e("AppUpdateHelper", "git check deliver: ${e.message}") + Toast.makeText(activity, "git check delivers: ${e.message}", Toast.LENGTH_LONG).show() + } + } + } + fun checkWithDialog( activity: AppCompatActivity, gitRepoUrl: String, @@ -111,6 +148,18 @@ object AppUpdateHelper { return client.github.getGithubVersions(gitRepoUrl.user(), gitRepoUrl.repo()).execute() } + private suspend fun requestArtifactoryVersions(artifactoryRepoUrl: String): Response>? { + val artifactory: Artifactory = ArtifactoryClientBuilder.create() + .setUrl(artifactoryRepoUrl) + //.setUsername("username") + //.setPassword("password") + .build() // TODO runtime error with "No static field INSTANCE of type Lorg/apache/http/conn/ssl/AllowAllHostnameVerifier" + val versionList = withContext(Dispatchers.Default) { + artifactory.repositories() + } + return null + } + private suspend fun requestGithubVersions(gitRepoUrl: String): Response> { val versionList = withContext(Dispatchers.Default) { val client = GithubClient(HttpLoggingInterceptor.Level.BODY) diff --git a/sample/build.gradle b/sample/build.gradle index 611cada..75fe27c 100644 --- a/sample/build.gradle +++ b/sample/build.gradle @@ -10,7 +10,7 @@ android { versionCode getGitCommitCount() versionName "1.0" - minSdkVersion 21 + minSdkVersion 26 targetSdkVersion 33 buildConfigField "String", 'GIT_REPOSITORY', "\"" + getGitOriginRemote() + "\"" @@ -18,6 +18,7 @@ android { packagingOptions { pickFirst 'META-INF/atomicfu.kotlin_module' + pickFirst 'META-INF/DEPENDENCIES' } buildFeatures { diff --git a/sample/src/main/java/info/hannes/github/sample/MainActivity.kt b/sample/src/main/java/info/hannes/github/sample/MainActivity.kt index 2b8c883..b64254f 100644 --- a/sample/src/main/java/info/hannes/github/sample/MainActivity.kt +++ b/sample/src/main/java/info/hannes/github/sample/MainActivity.kt @@ -27,7 +27,7 @@ class MainActivity : AppCompatActivity() { BuildConfig.GIT_REPOSITORY ) - binding.button.setOnClickListener { + binding.buttonGithub.setOnClickListener { AppUpdateHelper.checkWithDialog( this, BuildConfig.GIT_REPOSITORY, @@ -35,6 +35,15 @@ class MainActivity : AppCompatActivity() { force = true // just to enable debugging, without you can only debug once a day ) } + + binding.buttonArtifactory.setOnClickListener { + AppUpdateHelper.checkArtifactoryDialog( + this, + "https://artifactory.myserver.info", + { msg -> Log.d("result", msg) }, + force = true // just to enable debugging, without you can only debug once a day + ) + } } } diff --git a/sample/src/main/res/layout/activity_main.xml b/sample/src/main/res/layout/activity_main.xml index 746ee3a..7c09803 100644 --- a/sample/src/main/res/layout/activity_main.xml +++ b/sample/src/main/res/layout/activity_main.xml @@ -6,19 +6,28 @@ android:layout_width="match_parent" android:layout_height="match_parent"> -