Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ android {
}
packaging {
resources {
pickFirsts += setOf("META-INF/atomicfu.kotlin_module")
pickFirsts += setOf("META-INF/atomicfu.kotlin_module", "META-INF/DEPENDENCIES")
}
}
compileOptions {
Expand Down
19 changes: 19 additions & 0 deletions app/src/androidTest/java/info/hannes/github/sample/SmokeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,23 @@ class SmokeTest {
.check(matches(isDisplayed()))
.perform(click())
}

@Test
fun updateArtifactory() {
onView(isRoot())
.perform(captureToBitmap { bitmap: Bitmap -> bitmap.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-1") })
onView(withId(R.id.buttonArtifactory)).perform(click())
onView(isRoot())
.perform(captureToBitmap { bitmap: Bitmap -> bitmap.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-2") })
onView(withText(info.hannes.github.R.string.new_version))
.inRoot(isDialog())
.check(matches(isDisplayed()))
onView(isRoot())
.perform(captureToBitmap { bitmap: Bitmap -> bitmap.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-3") })

onView(withText("SHOW"))
.inRoot(isDialog())
.check(matches(isDisplayed()))
.perform(click())
}
}
9 changes: 9 additions & 0 deletions app/src/main/java/info/hannes/github/sample/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,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
)
}
}

}
13 changes: 11 additions & 2 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
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
Expand All @@ -18,7 +19,15 @@
android:layout_height="wrap_content"
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
7 changes: 7 additions & 0 deletions githubAppUpdate/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ android {
publishing {
singleVariant("release") {}
}
testOptions {
unitTests.isReturnDefaultValues = true
}
}

base {
Expand All @@ -33,6 +36,7 @@ dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:2.3.20")
implementation("com.google.code.gson:gson:2.13.2")
implementation("androidx.work:work-runtime-ktx:2.11.2")
implementation("org.jfrog.artifactory.client:artifactory-java-client-services:2.19.1")

implementation("com.squareup.retrofit2:retrofit:3.0.0")
implementation("androidx.appcompat:appcompat:1.7.1")
Expand All @@ -41,6 +45,9 @@ dependencies {
api("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.10.2")
api("androidx.lifecycle:lifecycle-runtime-ktx:2.10.0")
implementation("com.squareup.okhttp3:logging-interceptor:5.3.2")

testImplementation("junit:junit:4.13.2")
testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.4")
}

afterEvaluate {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,20 @@ 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.lang.RuntimeException
import java.net.HttpURLConnection
import java.util.concurrent.TimeUnit
import androidx.core.net.toUri
import androidx.core.content.edit

import org.jfrog.artifactory.client.model.impl.RepositoryTypeImpl.LOCAL
import org.jfrog.artifactory.client.model.impl.RepositoryTypeImpl.REMOTE
import org.jfrog.artifactory.client.model.impl.RepositoryTypeImpl.VIRTUAL
import org.jfrog.artifactory.client.model.impl.RepositoryTypeImpl.FEDERATED


object AppUpdateHelper {

Expand All @@ -44,11 +51,46 @@ object AppUpdateHelper {
}

// silently in background
fun checkForNewVersion(activity: AppCompatActivity, gitRepoUrl: String, repeatTime : Long = 6, timeUnit: TimeUnit = TimeUnit.HOURS, token: String? = null) {
fun checkForNewVersion(activity: AppCompatActivity, gitRepoUrl: String, repeatTime: Long = 6, timeUnit: TimeUnit = TimeUnit.HOURS, token: String? = null) {
val currentVersionName = activity.getVersionName()
DownloadWorker.run(activity, currentVersionName, gitRepoUrl, repeatTime, timeUnit, token)
}

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 @@ -91,7 +133,7 @@ object AppUpdateHelper {
currentVersionName: String,
gitRepoUrl: String,
token: String? = null
){
) {
try {
val versionList = requestVersionsSync(gitRepoUrl, token)

Expand All @@ -117,6 +159,41 @@ object AppUpdateHelper {
return client.github.getGithubVersions(gitRepoUrl.user(), gitRepoUrl.repo()).execute()
}

suspend fun requestArtifactoryVersions(artifactoryRepoUrl: String): Response<MutableList<GithubVersion>>? {
return try {
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"
withContext(Dispatchers.Default) {
val repositories = artifactory.repositories()

// val allBuilds = artifactory.builds().allBuilds
// Log.d("allBuilds", allBuilds.toString())

val localRepoList = repositories.list(LOCAL)
val remoteRepoList = repositories.list(REMOTE)
val virtualRepoList = repositories.list(VIRTUAL)
val federatedRepoList = repositories.list(FEDERATED)

Log.d("data", localRepoList.toString())
// val list = artifactory.storage().storageInfo.repositoriesSummaryList
// Log.d("list", list.toString())
val dataA = artifactory.repository("AppStore")
.download("/AppStore/AutonomousCar/app/1.2/app-1.2.pom")
.doDownload()
Log.d("dataA", dataA.toString())
Log.d("data", repositories.toString())
Log.d("data", repositories.replicationApi)
}
null
} catch (e: Exception) {
Log.e("AppUpdateHelper", "Artifactory check failed: ${e.message}")
null
}
}

private suspend fun requestGithubVersions(gitRepoUrl: String, token: String? = null): Response<MutableList<GithubVersion>> {
val versionList = withContext(Dispatchers.Default) {
val client = GithubClient(HttpLoggingInterceptor.Level.BODY, token)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package info.hannes.github

import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.delay
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Test

@OptIn(ExperimentalCoroutinesApi::class)
class AppUpdateHelperTest {

@org.junit.Before
fun setUp() {
}

@org.junit.After
fun tearDown() {
}

@Test
fun getPackageInfo() {
}

@Test
fun requestArtifactoryVersions() = runTest {
val data = AppUpdateHelper.requestArtifactoryVersions("https://artifactory.mxtracks.info")
assertNull(data)
}

@Test
fun dataShouldBeHelloWorld() = runTest {
val data = fetchData()
assertEquals("Hello world", data)
}

suspend fun fetchData(): String {
delay(1000L)
return "Hello world"
}

}
Loading