Skip to content

Commit

Permalink
Artifactory
Browse files Browse the repository at this point in the history
  • Loading branch information
hannesa2 committed Dec 17, 2022
1 parent d0c16c6 commit ae4ea38
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 7 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ allprojects {
repositories {
google()
mavenCentral()
jcenter()
}
}

Expand Down
6 changes: 5 additions & 1 deletion githubAppUpdate/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ android {

defaultConfig {
targetSdkVersion 33
minSdkVersion 21
minSdkVersion 26
project.archivesBaseName = "githubAppUpdate"
}
}
Expand All @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -111,6 +148,18 @@ object AppUpdateHelper {
return client.github.getGithubVersions(gitRepoUrl.user(), gitRepoUrl.repo()).execute()
}

private suspend fun requestArtifactoryVersions(artifactoryRepoUrl: String): Response<MutableList<GithubVersion>>? {
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<MutableList<GithubVersion>> {
val versionList = withContext(Dispatchers.Default) {
val client = GithubClient(HttpLoggingInterceptor.Level.BODY)
Expand Down
3 changes: 2 additions & 1 deletion sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ android {
versionCode getGitCommitCount()
versionName "1.0"

minSdkVersion 21
minSdkVersion 26
targetSdkVersion 33

buildConfigField "String", 'GIT_REPOSITORY', "\"" + getGitOriginRemote() + "\""
}

packagingOptions {
pickFirst 'META-INF/atomicfu.kotlin_module'
pickFirst 'META-INF/DEPENDENCIES'
}

buildFeatures {
Expand Down
11 changes: 10 additions & 1 deletion sample/src/main/java/info/hannes/github/sample/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,23 @@ class MainActivity : AppCompatActivity() {
BuildConfig.GIT_REPOSITORY
)

binding.button.setOnClickListener {
binding.buttonGithub.setOnClickListener {
AppUpdateHelper.checkWithDialog(
this,
BuildConfig.GIT_REPOSITORY,
{ msg -> Log.d("result", msg) },
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
)
}
}

}
17 changes: 13 additions & 4 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,28 @@
android:layout_width="match_parent"
android:layout_height="match_parent">

<FrameLayout
<LinearLayout
android:id="@+id/fragment_main"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="match_parent">

<Button
android:id="@+id/button"
android:id="@+id/buttonGithub"
android:layout_gravity="center|center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="check"
android:text="check Github"
tools:ignore="HardcodedText" />
</FrameLayout>

<Button
android:id="@+id/buttonArtifactory"
android:layout_gravity="center|center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="check Artifactory"
tools:ignore="HardcodedText" />
</LinearLayout>

<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
Expand Down

0 comments on commit ae4ea38

Please sign in to comment.