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
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ android {
versionCode getGitCommitCount()
versionName "1.0"

minSdkVersion 21
minSdkVersion 26
compileSdk 35
targetSdkVersion 35

Expand All @@ -20,7 +20,7 @@ android {
}
packagingOptions {
resources {
pickFirsts += ["META-INF/atomicfu.kotlin_module"]
pickFirsts += ["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
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ allprojects {
repositories {
google()
mavenCentral()
jcenter()
}
}
14 changes: 13 additions & 1 deletion githubAppUpdate/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ android {
defaultConfig {
compileSdk 35
targetSdkVersion 35
minSdkVersion 21
minSdkVersion 26
project.archivesBaseName = "githubAppUpdate"
}
compileOptions {
Expand All @@ -22,12 +22,21 @@ android {
publishing {
singleVariant("release") {}
}
packagingOptions {
resources {
pickFirsts += ["META-INF/atomicfu.kotlin_module", "META-INF/DEPENDENCIES"]
}
}
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation "com.google.code.gson:gson:2.13.2"
implementation "androidx.work:work-runtime-ktx:2.10.5"
implementation 'org.jfrog.artifactory.client:artifactory-java-client-services:2.19.1'
// 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:3.0.0"
implementation "androidx.appcompat:appcompat:1.7.1"
Expand All @@ -38,6 +47,9 @@ dependencies {
api "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.10.2"
api "androidx.lifecycle:lifecycle-runtime-ktx:2.9.4"
implementation "com.squareup.okhttp3:logging-interceptor:5.2.0"

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

project.afterEvaluate {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,18 @@ 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 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 @@ -43,11 +50,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 @@ -90,7 +132,7 @@ object AppUpdateHelper {
currentVersionName: String,
gitRepoUrl: String,
token: String? = null
){
) {
try {
val versionList = requestVersionsSync(gitRepoUrl, token)

Expand All @@ -116,6 +158,37 @@ object AppUpdateHelper {
return client.github.getGithubVersions(gitRepoUrl.user(), gitRepoUrl.repo()).execute()
}

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) {
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)
}
Log.d("data", versionList.toString())
return 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,41 @@
package info.hannes.github

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

@OptIn(ExperimentalCoroutinesApi::class)
class AppUpdateHelperTest {

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

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

@org.junit.Test
fun getPackageInfo() {
}

@org.junit.Test
fun requestArtifactoryVersions() = runTest {
val data = AppUpdateHelper.requestArtifactoryVersions("https://artifactory.mxtracks.info")
assertEquals("Hello world", data)
}

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

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

}
Loading