Add badge to notify when update is available

This commit is contained in:
PabloG02 2023-01-12 13:42:53 +01:00
parent 411b79ed9e
commit 68b0abfa91
3 changed files with 37 additions and 1 deletions

View File

@ -14,10 +14,14 @@ import android.net.Uri
import android.os.Handler
import android.os.Looper
import android.text.Html
import android.widget.ImageView
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat.startActivity
import com.google.android.material.badge.BadgeDrawable
import com.google.android.material.badge.BadgeDrawable.BOTTOM_END
import com.google.android.material.badge.BadgeUtils
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
@ -121,5 +125,36 @@ class AppUpdater : BroadcastReceiver() {
}
}
}
@JvmStatic
@com.google.android.material.badge.ExperimentalBadgeUtils
fun checkForUpdates2(context : Context, icon: ImageView) {
CoroutineScope(Dispatchers.IO).launch {
val url = URL("$baseUrl/builds")
try {
val response = url.readText()
val jsonBuilds = JSONTokener(response).nextValue() as JSONArray
var ftx1Index = 0
while (ftx1Index < jsonBuilds.length() && !jsonBuilds.getJSONObject(ftx1Index).get("branch").equals(branch)) {
ftx1Index++
}
if (ftx1Index >= jsonBuilds.length())
ftx1Index = 0
val remoteBuildGitHash = jsonBuilds.getJSONObject(ftx1Index).getJSONObject("commit").getString("id")
if(!BuildConfig.GIT_HASH.equals(remoteBuildGitHash)){
val badge = BadgeDrawable.create(context)
badge.badgeGravity = BOTTOM_END
badge.verticalOffset = 25
badge.horizontalOffset = 25
badge.backgroundColor = context.getResources().getColor(R.color.colorPrimary)
BadgeUtils.attachBadgeDrawable(badge, icon)
}
} catch (e : IOException) {
e.printStackTrace()
}
}
}
}
}

View File

@ -154,6 +154,7 @@ class MainActivity : AppCompatActivity() {
if (BuildConfig.FLAVOR.equals("edge")) {
binding.checkUpdatesIcon.visibility = View.GONE
} else {
AppUpdater.checkForUpdates2(context, binding.checkUpdatesIcon)
binding.checkUpdatesIcon.setOnClickListener {
AppUpdater.checkForUpdates(context)
}

View File

@ -4,5 +4,5 @@
android:width="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path android:fillColor="#000" android:pathData="M5,20H19V18H5M19,9H15V3H9V9H5L12,16L19,9Z" />
<path android:fillColor="?attr/colorOnSecondary" android:pathData="M5,20H19V18H5M19,9H15V3H9V9H5L12,16L19,9Z" />
</vector>