@ -7,6 +7,7 @@ import android.os.Bundle
import android.view.View
import android.view.View
import androidx.core.content.FileProvider
import androidx.core.content.FileProvider
import com.fox2code.foxcompat.app.FoxActivity
import com.fox2code.foxcompat.app.FoxActivity
import com.fox2code.mmm.androidacy.AndroidacyRepoData
import com.fox2code.mmm.utils.io.net.Http
import com.fox2code.mmm.utils.io.net.Http
import com.google.android.material.bottomnavigation.BottomNavigationItemView
import com.google.android.material.bottomnavigation.BottomNavigationItemView
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.google.android.material.bottomnavigation.BottomNavigationView
@ -177,11 +178,10 @@ class UpdateActivity : FoxActivity() {
progressIndicator . isIndeterminate = true
progressIndicator . isIndeterminate = true
}
}
// check for update
// check for update
val shouldUpdate = AppUpdateManager . appUpdateManager . peekShouldUpdate( )
val shouldUpdate = AppUpdateManager . appUpdateManager . checkUpdate( true )
// if shouldUpdate is true, then we have an update
// if shouldUpdate is true, then we have an update
if ( shouldUpdate ) {
if ( shouldUpdate ) {
runOnUiThread {
runOnUiThread {
// set status text to update available
// set status text to update available
statusTextView . setText ( R . string . update _available )
statusTextView . setText ( R . string . update _available )
// set button text to download
// set button text to download
@ -190,15 +190,25 @@ class UpdateActivity : FoxActivity() {
button . tooltipText = getString ( R . string . download _update )
button . tooltipText = getString ( R . string . download _update )
}
}
button . isEnabled = true
button . isEnabled = true
// set changelog text. changelog could be markdown, so we need to convert it
val changelogTextView = findViewById < MaterialTextView > ( R . id . update _changelog )
val markwon = Markwon . create ( this @UpdateActivity )
markwon . setMarkdown ( changelogTextView ,
AppUpdateManager . appUpdateManager . changes . toString ( )
)
}
}
// return
// return
} else {
} else {
runOnUiThread {
runOnUiThread {
// set status text to no update available
// set status text to no update available
statusTextView . setText ( R . string . no _update _available )
statusTextView . setText ( R . string . no _update _available )
// set changelog text. changelog could be markdown, so we need to convert it
val changelogTextView = findViewById < MaterialTextView > ( R . id . update _changelog )
val markwon = Markwon . create ( this @UpdateActivity )
markwon . setMarkdown ( changelogTextView ,
AppUpdateManager . appUpdateManager . changes . toString ( )
)
}
}
// set progress bar to error
// return
}
}
runOnUiThread {
runOnUiThread {
progressIndicator . isIndeterminate = false
progressIndicator . isIndeterminate = false
@ -213,9 +223,20 @@ class UpdateActivity : FoxActivity() {
runOnUiThread { progressIndicator . isIndeterminate = true }
runOnUiThread { progressIndicator . isIndeterminate = true }
// get status text view
// get status text view
val statusTextView = findViewById < MaterialTextView > ( R . id . update _progress _text )
val statusTextView = findViewById < MaterialTextView > ( R . id . update _progress _text )
var lastestJSON : ByteArray ? = ByteArray ( 0 )
val lastestJSON : ByteArray ?
// get changelog from
// make a request to https://production-api.androidacy.com/ammm/updates/check with appVersionCode and token/device_id/client_id
// and the changelog is the json string in changelog
var token = AndroidacyRepoData . token
if ( ! AndroidacyRepoData . getInstance ( ) . isValidToken ( token ) ) {
Timber . w ( " Invalid token, not checking for updates " )
token = AndroidacyRepoData . getInstance ( ) . requestNewToken ( )
}
val deviceId = AndroidacyRepoData . generateDeviceId ( )
val clientId = BuildConfig . ANDROIDACY _CLIENT _ID
val url = " https://production-api.androidacy.com/ammm/updates/check?appVersionCode= ${BuildConfig.VERSION_CODE} &token= $token &device_id= $deviceId &client_id= $clientId "
try {
try {
lastestJSON = Http . doHttpGet ( AppUpdateManager . RELEASES _API _URL , false )
lastestJSON = Http . doHttpGet ( url , false )
} catch ( e : Exception ) {
} catch ( e : Exception ) {
// when logging, REMOVE the json from the log
// when logging, REMOVE the json from the log
Timber . e ( e , " Error downloading update info " )
Timber . e ( e , " Error downloading update info " )
@ -224,46 +245,34 @@ class UpdateActivity : FoxActivity() {
progressIndicator . setProgressCompat ( 100 , false )
progressIndicator . setProgressCompat ( 100 , false )
statusTextView . setText ( R . string . error _download _update )
statusTextView . setText ( R . string . error _download _update )
}
}
return
}
}
// convert to JSON
// convert to JSON
val latestJSON = JSONObject ( String ( lastestJSON !! ) )
val latestJSON = JSONObject ( String ( lastestJSON ) )
val changelog = latestJSON . getString ( " body " )
val changelog = latestJSON . getString ( " changelog " )
// set changelog text. changelog could be markdown, so we need to convert it to HTML
runOnUiThread {
val changelogTextView = findViewById < MaterialTextView > ( R . id . update _changelog )
// set changelog text. changelog could be markdown, so we need to convert it
val markwon = Markwon . builder ( this ) . build ( )
val changelogTextView = findViewById < MaterialTextView > ( R . id . update _changelog )
runOnUiThread { markwon . setMarkdown ( changelogTextView , changelog ) }
val markwon = Markwon . create ( this @UpdateActivity )
// we already know that there is an update, so we can get the latest version of our architecture. We're going to have to iterate through the assets to find the one we want
markwon . setMarkdown ( changelogTextView , changelog )
val assets = latestJSON . getJSONArray ( " assets " )
// get the asset we want
var asset : JSONObject ? = null
// iterate through assets until we find the one that contains Build.SUPPORTED_ABIS[0]
while ( Objects . isNull ( asset ) ) {
for ( i in 0 until assets . length ( ) ) {
val asset1 = assets . getJSONObject ( i )
if ( asset1 . getString ( " name " ) . contains ( Build . SUPPORTED _ABIS [ 0 ] ) ) {
asset = asset1
break
}
}
}
// if asset is null, then we are in a bad state
if ( Objects . isNull ( asset ) ) {
// set status text to error
runOnUiThread {
statusTextView . setText ( R . string . error _no _asset )
// set progress bar to error
progressIndicator . isIndeterminate = false
progressIndicator . setProgressCompat ( 100 , false )
}
// return
return
}
}
// get the download url
// get the download url
val downloadUrl = asset ?. getString ( " browser_download_url " )
var downloadUrl = url . replace ( " check " , " download " )
// get the download size
// append arch to download url. coerce anything like arm64-* or aarch64-* to arm64 and anything like arm-* or armeabi-* to arm
val downloadSize = asset ?. getLong ( " size " )
downloadUrl += if ( Build . SUPPORTED _ABIS [ 0 ] . contains ( " arm64 " ) || Build . SUPPORTED _ABIS [ 0 ] . contains ( " aarch64 " ) ) {
" &arch=arm64 "
} else if ( Build . SUPPORTED _ABIS [ 0 ] . contains ( " arm " ) || Build . SUPPORTED _ABIS [ 0 ] . contains ( " armeabi " ) ) {
" &arch=arm "
} else if ( Build . SUPPORTED _ABIS [ 0 ] . contains ( " x86_64 " ) ) {
" &arch=x86_64 "
} else if ( Build . SUPPORTED _ABIS [ 0 ] . contains ( " x86 " ) ) {
" &arch=x86 "
} else {
// assume universal and hope for the best, because we don't know what to do
Timber . w ( " Unknown arch ${Build.SUPPORTED_ABIS[0]} when downloading update, assuming universal " )
" &arch=universal "
}
runOnUiThread {
runOnUiThread {
// set status text to downloading update
// set status text to downloading update
statusTextView . text = getString ( R . string . downloading _update , 0 )
statusTextView . text = getString ( R . string . downloading _update , 0 )
// set progress bar to 0
// set progress bar to 0
@ -273,9 +282,8 @@ class UpdateActivity : FoxActivity() {
// download the update
// download the update
var update = ByteArray ( 0 )
var update = ByteArray ( 0 )
try {
try {
update = Http . doHttpGet ( downloadUrl !! ) { downloaded : Int , total : Int , _ : Boolean ->
update = Http . doHttpGet ( downloadUrl ) { downloaded : Int , total : Int , _ : Boolean ->
runOnUiThread {
runOnUiThread {
// update progress bar
// update progress bar
progressIndicator . setProgressCompat (
progressIndicator . setProgressCompat (
( downloaded . toFloat ( ) / total . toFloat ( ) * 100 ) . toInt ( ) ,
( downloaded . toFloat ( ) / total . toFloat ( ) * 100 ) . toInt ( ) ,
@ -308,19 +316,6 @@ class UpdateActivity : FoxActivity() {
// return
// return
return
return
}
}
// if update is not the same size as the download size, then we are in a bad state
if ( update . size . toLong ( ) != downloadSize ) {
runOnUiThread {
// set status text to error
statusTextView . setText ( R . string . error _download _update )
// set progress bar to error
progressIndicator . isIndeterminate = false
progressIndicator . setProgressCompat ( 100 , false )
}
// return
return
}
// set status text to installing update
// set status text to installing update
runOnUiThread {
runOnUiThread {
statusTextView . setText ( R . string . installing _update )
statusTextView . setText ( R . string . installing _update )