Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(FF): Add android in app update #10976

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
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: 4 additions & 0 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
59 changes: 59 additions & 0 deletions android/app/src/main/java/net/artsy/app/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.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 val TAG = "ArtsyApp"
private lateinit var appUpdateManager: AppUpdateManager

/**
* Returns the name of the main component registered from JavaScript. This is
Expand Down Expand Up @@ -90,6 +102,53 @@ class MainActivity : ReactActivity() {
return null
}
})
appUpdateManager = AppUpdateManagerFactory.create(this)

checkForAppUpdate()
}

private fun checkForAppUpdate() {
Log.d(TAG, "here we go!")
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()}")

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}\n" +
"appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE): ${appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE)}"
)
if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE &&
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(TAG, "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) {
Log.d(TAG, "startUpdateFlowForResult: errored out with ${e.toString()}")
e.printStackTrace()
}
}
}
}

// Basic overriding this class required for braze integration:
Expand Down
2 changes: 2 additions & 0 deletions src/app/AppRegistry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,8 @@ const artQuizScreenOptions = {

export type AppModule = keyof typeof modules

// comment

export const modules = defineModules({
Activity: reactModule(ActivityScreen, {
fullBleed: true,
Expand Down