Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import android.content.Intent
import android.content.pm.ApplicationInfo
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.util.Log
import android.widget.Toast
Expand All @@ -24,13 +23,15 @@ import retrofit2.Response
import java.lang.RuntimeException
import java.net.HttpURLConnection
import java.util.concurrent.TimeUnit
import androidx.core.net.toUri
import androidx.core.content.edit


object AppUpdateHelper {

private fun Activity.getVersionName(): String = try {
this.getPackageInfo().versionName ?: ""
} catch (e: PackageManager.NameNotFoundException) {
} catch (_: PackageManager.NameNotFoundException) {
""
}

Expand Down Expand Up @@ -64,7 +65,7 @@ object AppUpdateHelper {
if (force || prefs.getLong(key, 0) < System.currentTimeMillis() - 1000 * 3600 * 24 / 24 / 60 * 5) {
try {
val versionList = requestGithubVersions(gitRepoUrl, token)
prefs.edit().putLong(key, System.currentTimeMillis()).apply()
prefs.edit { putLong(key, System.currentTimeMillis()) }

versionList.body()?.firstOrNull()?.let { release ->
val assetApk = release.assets.find { it.name.endsWith("release.apk") }
Expand Down Expand Up @@ -146,15 +147,15 @@ object AppUpdateHelper {
dialog.dismiss()
}
.setPositiveButton(activity.getString(R.string.showRelease)) { dialog, _ ->
val uriUrl = Uri.parse(release.htmlUrl)
val uriUrl = release.htmlUrl.toUri()
Log.d("open", uriUrl.toString())
activity.startActivity(Intent(Intent.ACTION_VIEW, uriUrl))
dialog.dismiss()
}

assetApk?.let {
dialog.setNeutralButton(activity.getString(R.string.directDownload)) { dialog, _ ->
val uriUrl = Uri.parse(it.browserDownloadUrl)
val uriUrl = it.browserDownloadUrl.toUri()
Log.d("open", uriUrl.toString())
activity.startActivity(Intent(Intent.ACTION_VIEW, uriUrl))
dialog.dismiss()
Expand Down
24 changes: 10 additions & 14 deletions githubAppUpdate/src/main/java/info/hannes/github/Notify.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,19 @@ import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.net.Uri
import android.os.Build
import androidx.core.app.NotificationCompat
import androidx.core.net.toUri
import info.hannes.github.model.Asset
import info.hannes.github.model.GithubVersion

internal object Notify {

private var MessageID = 120
private const val channelId = "channelAppUpdate"
private const val channelFireBaseMsg = "Channel appUpdate"
private const val CHANNEL_ID = "channelAppUpdate"
private const val CHANNEL_FIREBASE_MSG = "Channel appUpdate"

private val pendingIntentFlags = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
} else {
PendingIntent.FLAG_UPDATE_CURRENT
}
private const val PENDING_INTENT_FLAGS = PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT

fun notification(context: Context, messageString: String, notificationTitle: String, assetApk: Asset?, release: GithubVersion) {

Expand All @@ -31,7 +27,7 @@ internal object Notify {
val notificationManager = context.getSystemService(ns) as NotificationManager

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notificationChannel = NotificationChannel(channelId, channelFireBaseMsg, NotificationManager.IMPORTANCE_LOW)
val notificationChannel = NotificationChannel(CHANNEL_ID, CHANNEL_FIREBASE_MSG, NotificationManager.IMPORTANCE_LOW)
notificationChannel.enableLights(true)
notificationChannel.lightColor = Color.RED
notificationChannel.enableVibration(true)
Expand All @@ -46,11 +42,11 @@ internal object Notify {
//the message that will be displayed as the ticker
val ticker = "$contentTitle $messageString"

val showUrl = Uri.parse(release.htmlUrl)
val showPendingIntent = PendingIntent.getActivity(context, 0, Intent(Intent.ACTION_VIEW, showUrl), pendingIntentFlags)
val showUrl = release.htmlUrl.toUri()
val showPendingIntent = PendingIntent.getActivity(context, 0, Intent(Intent.ACTION_VIEW, showUrl), PENDING_INTENT_FLAGS)

//build the notification
val notificationCompat = NotificationCompat.Builder(context, channelId)
val notificationCompat = NotificationCompat.Builder(context, CHANNEL_ID)
.setAutoCancel(true)
.setContentTitle(contentTitle)
.setContentIntent(showPendingIntent)
Expand All @@ -62,8 +58,8 @@ internal object Notify {
.addAction(R.drawable.githib_logo, context.getString(R.string.showRelease), showPendingIntent)

assetApk?.let {
val uriUrl = Uri.parse(it.browserDownloadUrl)
val directPendingIntent = PendingIntent.getActivity(context, 0, Intent(Intent.ACTION_VIEW, uriUrl), pendingIntentFlags)
val uriUrl = it.browserDownloadUrl.toUri()
val directPendingIntent = PendingIntent.getActivity(context, 0, Intent(Intent.ACTION_VIEW, uriUrl), PENDING_INTENT_FLAGS)
notificationCompat.addAction(R.drawable.githib_logo, context.getString(R.string.directDownload), directPendingIntent)
}

Expand Down
Loading