From 0364f7cbc1f9bb3ea741619893ce4fd9d404d03a Mon Sep 17 00:00:00 2001 From: Sultan Date: Fri, 18 Oct 2024 17:19:28 +0200 Subject: [PATCH 1/7] feat: Add android in app update --- android/app/build.gradle | 4 ++ .../main/java/net/artsy/app/MainActivity.kt | 43 +++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/android/app/build.gradle b/android/app/build.gradle index 8d0c32b3673..cec7ce0076f 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -155,6 +155,10 @@ dependencies { implementation 'com.facebook.fresco:webpsupport:2.6.0' implementation 'com.android.support:support-core-utils:24.2.1' + // for Android In App Update + implementation 'com.google.android.play:app-update:2.1.0' + implementation 'com.google.android.play:app-update-ktx:2.1.0' + if (hermesEnabled.toBoolean()) { implementation("com.facebook.react:hermes-android") diff --git a/android/app/src/main/java/net/artsy/app/MainActivity.kt b/android/app/src/main/java/net/artsy/app/MainActivity.kt index 9aaa8827517..ab71c60473d 100644 --- a/android/app/src/main/java/net/artsy/app/MainActivity.kt +++ b/android/app/src/main/java/net/artsy/app/MainActivity.kt @@ -9,6 +9,7 @@ import android.view.View import android.view.WindowInsets import android.view.WindowManager import android.content.Intent +import android.content.IntentSender import android.net.Uri import androidx.annotation.Nullable import com.facebook.react.ReactActivity @@ -18,9 +19,20 @@ import com.dieam.reactnativepushnotification.modules.RNPushNotification import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.fabricEnabled import com.facebook.react.defaults.DefaultReactActivityDelegate +import com.google.android.play.core.appupdate.AppUpdateManagerFactory; +import androidx.activity.result.contract.ActivityResultContracts +import com.google.android.gms.tasks.Task +import com.google.android.play.core.appupdate.AppUpdateInfo +import com.google.android.play.core.appupdate.AppUpdateManager +import com.google.android.play.core.appupdate.AppUpdateOptions +import com.google.android.play.core.install.model.AppUpdateType +import com.google.android.play.core.install.model.UpdateAvailability + import android.util.Log class MainActivity : ReactActivity() { + private val DAYS_FOR_FLEXIBLE_UPDATE = 7 + private lateinit var appUpdateManager: AppUpdateManager /** * Returns the name of the main component registered from JavaScript. This is @@ -90,6 +102,37 @@ class MainActivity : ReactActivity() { return null } }) + appUpdateManager = AppUpdateManagerFactory.create(this) + + checkForAppUpdate() + } + + private fun checkForAppUpdate() { + val appUpdateInfoTask: Task = appUpdateManager.appUpdateInfo + + appUpdateInfoTask.addOnSuccessListener { appUpdateInfo -> + if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE && + (appUpdateInfo.clientVersionStalenessDays() ?: -1) >= DAYS_FOR_FLEXIBLE_UPDATE && + appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE)) { + // Start a flexible update + try { + appUpdateManager.startUpdateFlowForResult( + appUpdateInfo, + registerForActivityResult(ActivityResultContracts.StartIntentSenderForResult()) { result -> + // handle callback + if (result.resultCode != RESULT_OK) { + Log.d("ARTSY", "Update flow failed! Result code: ${result.resultCode}") + // If the update is canceled or fails, + // you can request to start the update again. + } + }, + AppUpdateOptions.newBuilder(AppUpdateType.FLEXIBLE).build() + ) + } catch (e: IntentSender.SendIntentException) { + e.printStackTrace() + } + } + } } // Basic overriding this class required for braze integration: From abbe3db90ee885ec5fd7070a3eb29ec30e8bbb16 Mon Sep 17 00:00:00 2001 From: Sultan Date: Mon, 21 Oct 2024 09:38:47 +0200 Subject: [PATCH 2/7] change to build a new android build --- src/app/AppRegistry.tsx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/app/AppRegistry.tsx b/src/app/AppRegistry.tsx index 203afc1d453..e8ecf5aa097 100644 --- a/src/app/AppRegistry.tsx +++ b/src/app/AppRegistry.tsx @@ -328,6 +328,8 @@ const artQuizScreenOptions = { export type AppModule = keyof typeof modules +// comment + export const modules = defineModules({ Activity: reactModule(ActivityScreen, { fullBleed: true, From 99510e87ec3d90d72866411f1546c7ffab0898b1 Mon Sep 17 00:00:00 2001 From: Sultan Date: Mon, 21 Oct 2024 16:01:41 +0200 Subject: [PATCH 3/7] add logs to test --- .../src/main/java/net/artsy/app/MainActivity.kt | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/android/app/src/main/java/net/artsy/app/MainActivity.kt b/android/app/src/main/java/net/artsy/app/MainActivity.kt index ab71c60473d..daff21092b1 100644 --- a/android/app/src/main/java/net/artsy/app/MainActivity.kt +++ b/android/app/src/main/java/net/artsy/app/MainActivity.kt @@ -21,7 +21,6 @@ import com.facebook.react.defaults.DefaultReactActivityDelegate import com.google.android.play.core.appupdate.AppUpdateManagerFactory; import androidx.activity.result.contract.ActivityResultContracts -import com.google.android.gms.tasks.Task import com.google.android.play.core.appupdate.AppUpdateInfo import com.google.android.play.core.appupdate.AppUpdateManager import com.google.android.play.core.appupdate.AppUpdateOptions @@ -32,6 +31,7 @@ import android.util.Log class MainActivity : ReactActivity() { private val DAYS_FOR_FLEXIBLE_UPDATE = 7 + private val TAG = "ArtsyApp" private lateinit var appUpdateManager: AppUpdateManager /** @@ -108,20 +108,29 @@ class MainActivity : ReactActivity() { } private fun checkForAppUpdate() { - val appUpdateInfoTask: Task = appUpdateManager.appUpdateInfo + Log.d(TAG, "checkForAppUpdate: started checking for update!") + val appUpdateInfoTask = appUpdateManager.appUpdateInfo + + appUpdateInfoTask.addOnFailureListener { appUpdateInfo -> + Log.d(TAG, "checkForAppUpdate: task failed: ${appUpdateInfo.toString()}") + } appUpdateInfoTask.addOnSuccessListener { appUpdateInfo -> + Log.d(TAG, "checkForAppUpdate: adding listener, appUpdateInfo: ${appUpdateInfo.toString()}") if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE && (appUpdateInfo.clientVersionStalenessDays() ?: -1) >= DAYS_FOR_FLEXIBLE_UPDATE && appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE)) { + Log.d(TAG, "appUpdateInfoTask.addOnSuccessListener: passed appUpdateInfo if statements") // Start a flexible update try { + Log.d(TAG, "appUpdateInfoTask.addOnSuccessListener: trying to start an update flow") appUpdateManager.startUpdateFlowForResult( appUpdateInfo, registerForActivityResult(ActivityResultContracts.StartIntentSenderForResult()) { result -> + Log.d(TAG, "startUpdateFlowForResult: getting result: ${result.toString()}") // handle callback if (result.resultCode != RESULT_OK) { - Log.d("ARTSY", "Update flow failed! Result code: ${result.resultCode}") + Log.d(TAG, "Update flow failed! Result code: ${result.resultCode}") // If the update is canceled or fails, // you can request to start the update again. } @@ -129,6 +138,7 @@ class MainActivity : ReactActivity() { AppUpdateOptions.newBuilder(AppUpdateType.FLEXIBLE).build() ) } catch (e: IntentSender.SendIntentException) { + Log.d(TAG, "startUpdateFlowForResult: errored out with ${e.toString()}") e.printStackTrace() } } From a4f05761ad2b732c9d2a1314b44ce1bf3ac175c1 Mon Sep 17 00:00:00 2001 From: Sultan Date: Mon, 21 Oct 2024 16:40:43 +0200 Subject: [PATCH 4/7] add log to build a new version --- android/app/src/main/java/net/artsy/app/MainActivity.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/android/app/src/main/java/net/artsy/app/MainActivity.kt b/android/app/src/main/java/net/artsy/app/MainActivity.kt index daff21092b1..675e54e47c3 100644 --- a/android/app/src/main/java/net/artsy/app/MainActivity.kt +++ b/android/app/src/main/java/net/artsy/app/MainActivity.kt @@ -108,6 +108,7 @@ class MainActivity : ReactActivity() { } private fun checkForAppUpdate() { + Log.d(TAG, "here we go!") Log.d(TAG, "checkForAppUpdate: started checking for update!") val appUpdateInfoTask = appUpdateManager.appUpdateInfo From a52a6205f539f02ac34ac9d103b2c70ad21b11bf Mon Sep 17 00:00:00 2001 From: Sultan Date: Mon, 21 Oct 2024 17:53:08 +0200 Subject: [PATCH 5/7] add checks for conditions --- android/app/src/main/java/net/artsy/app/MainActivity.kt | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/android/app/src/main/java/net/artsy/app/MainActivity.kt b/android/app/src/main/java/net/artsy/app/MainActivity.kt index 675e54e47c3..e995cdffe94 100644 --- a/android/app/src/main/java/net/artsy/app/MainActivity.kt +++ b/android/app/src/main/java/net/artsy/app/MainActivity.kt @@ -118,6 +118,12 @@ class MainActivity : ReactActivity() { appUpdateInfoTask.addOnSuccessListener { appUpdateInfo -> Log.d(TAG, "checkForAppUpdate: adding listener, appUpdateInfo: ${appUpdateInfo.toString()}") + + Log.d(TAG, "checkForAppUpdate: conditions: \n" + + "appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE: ${appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE}\n" + + "(appUpdateInfo.clientVersionStalenessDays() ?: -1) >= DAYS_FOR_FLEXIBLE_UPDATE: ${(appUpdateInfo.clientVersionStalenessDays() ?: -1) >= DAYS_FOR_FLEXIBLE_UPDATE}" + + "appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE): ${appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE)}" + ) if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE && (appUpdateInfo.clientVersionStalenessDays() ?: -1) >= DAYS_FOR_FLEXIBLE_UPDATE && appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE)) { From dab22504d8fcc829e424b5020297fc8f8833ec3e Mon Sep 17 00:00:00 2001 From: Sultan Date: Mon, 21 Oct 2024 21:27:56 +0200 Subject: [PATCH 6/7] remove staleness check --- android/app/src/main/java/net/artsy/app/MainActivity.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/android/app/src/main/java/net/artsy/app/MainActivity.kt b/android/app/src/main/java/net/artsy/app/MainActivity.kt index e995cdffe94..d9460874219 100644 --- a/android/app/src/main/java/net/artsy/app/MainActivity.kt +++ b/android/app/src/main/java/net/artsy/app/MainActivity.kt @@ -125,7 +125,6 @@ class MainActivity : ReactActivity() { "appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE): ${appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE)}" ) if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE && - (appUpdateInfo.clientVersionStalenessDays() ?: -1) >= DAYS_FOR_FLEXIBLE_UPDATE && appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE)) { Log.d(TAG, "appUpdateInfoTask.addOnSuccessListener: passed appUpdateInfo if statements") // Start a flexible update From a18fc14ffe9c64c8f0c44450c6e488108021ffbf Mon Sep 17 00:00:00 2001 From: Sultan Date: Mon, 21 Oct 2024 22:07:39 +0200 Subject: [PATCH 7/7] add new line --- android/app/src/main/java/net/artsy/app/MainActivity.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/android/app/src/main/java/net/artsy/app/MainActivity.kt b/android/app/src/main/java/net/artsy/app/MainActivity.kt index d9460874219..1f99eb6ebd7 100644 --- a/android/app/src/main/java/net/artsy/app/MainActivity.kt +++ b/android/app/src/main/java/net/artsy/app/MainActivity.kt @@ -121,7 +121,7 @@ class MainActivity : ReactActivity() { Log.d(TAG, "checkForAppUpdate: conditions: \n" + "appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE: ${appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE}\n" + - "(appUpdateInfo.clientVersionStalenessDays() ?: -1) >= DAYS_FOR_FLEXIBLE_UPDATE: ${(appUpdateInfo.clientVersionStalenessDays() ?: -1) >= DAYS_FOR_FLEXIBLE_UPDATE}" + + "(appUpdateInfo.clientVersionStalenessDays() ?: -1) >= DAYS_FOR_FLEXIBLE_UPDATE: ${(appUpdateInfo.clientVersionStalenessDays() ?: -1) >= DAYS_FOR_FLEXIBLE_UPDATE}\n" + "appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE): ${appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE)}" ) if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE &&