Removed pebble symbol from Unciv icon when showing persistent notification (#2003)

Optimized Notification service texts
https://github.com/yairm210/Unciv/issues/1680
This commit is contained in:
wrov 2020-02-23 19:45:25 +01:00 committed by GitHub
parent 6009c1b5d3
commit 31d9bb0c5d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 45 additions and 18 deletions

View File

@ -1747,8 +1747,9 @@ An error has occured = Ein Fehler ist aufgetreten
Multiplayer turn notifier service terminated = Multiplayer Zug Benachrichtigungsdienst wurde beendet Multiplayer turn notifier service terminated = Multiplayer Zug Benachrichtigungsdienst wurde beendet
Your friends are waiting on your turn. = Deine Freunde warten auf deinen Zug. Your friends are waiting on your turn. = Deine Freunde warten auf deinen Zug.
Unciv - It's your turn! = Unciv - Du bist am Zug! Unciv - It's your turn! = Unciv - Du bist am Zug!
Unciv will inform you when it's your turn. = Unciv wird dich benachrichtigen wenn du am Zug bist. Unciv will inform you when it's your turn in multiplayer. = Unciv wird dich benachrichtigen, wenn du im Multiplayer am Zug bist.
Last checked: [lastTimeChecked]. Checks ca. every [checkPeriod] minute(s) when Internet available. = Zuletzt geprüft: [lastTimeChecked]. Prüft etwa alle [checkPeriod] Minute(n) wenn Internet vorhanden. Last online turn check: [lastTimeChecked] = Letzter online Zug Check: [lastTimeChecked]
Checks ca. every [checkPeriod] minute(s) when Internet available. = Prüft etwa alle [checkPeriod] Minute(n) wenn Internet vorhanden.
Configurable in Unciv options menu. = Konfigurierbar im Unciv Optionsmenü. Configurable in Unciv options menu. = Konfigurierbar im Unciv Optionsmenü.
Unciv multiplayer turn notifier running = Unciv Multiplayer Zug Benachrichtiger läuft. Unciv multiplayer turn notifier running = Unciv Multiplayer Zug Benachrichtiger läuft.
Multiplayer options = Multiplayer Einstellungen Multiplayer options = Multiplayer Einstellungen

View File

@ -1746,8 +1746,9 @@ An error has occured =
Multiplayer turn notifier service terminated = Multiplayer turn notifier service terminated =
Your friends are waiting on your turn. = Your friends are waiting on your turn. =
Unciv - It's your turn! = Unciv - It's your turn! =
Unciv will inform you when it's your turn. = Unciv will inform you when it's your turn in multiplayer. =
Last checked: [lastTimeChecked]. Checks ca. every [checkPeriod] minute(s) when Internet available. = Last online turn check: [lastTimeChecked] =
Checks ca. every [checkPeriod] minute(s) when Internet available. =
Configurable in Unciv options menu. = Configurable in Unciv options menu. =
Unciv multiplayer turn notifier running = Unciv multiplayer turn notifier running =
Multiplayer options = Multiplayer options =

View File

@ -12,7 +12,7 @@ import java.io.File
class AndroidLauncher : AndroidApplication() { class AndroidLauncher : AndroidApplication() {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
createNotificationChannels() MultiplayerTurnCheckWorker.createNotificationChannels(applicationContext)
// Only allow mods on KK+, to avoid READ_EXTERNAL_STORAGE permission earlier versions need // Only allow mods on KK+, to avoid READ_EXTERNAL_STORAGE permission earlier versions need
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
@ -26,14 +26,6 @@ class AndroidLauncher : AndroidApplication() {
initialize(game, config) initialize(game, config)
} }
/**
* Necessary for Multiplayer Turner Checker, starting with Android Oreo
*/
private fun createNotificationChannels() {
MultiplayerTurnCheckWorker.createNotificationChannelInfo(context)
MultiplayerTurnCheckWorker.createNotificationChannelService(context)
}
/** /**
* Copies mods from external data directory (where users can access) to the private one (where * Copies mods from external data directory (where users can access) to the private one (where
* libGDX reads from). Note: deletes all files currently in the private mod directory and * libGDX reads from). Note: deletes all files currently in the private mod directory and

View File

@ -10,6 +10,7 @@ import android.os.Build
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationCompat.DEFAULT_VIBRATE import androidx.core.app.NotificationCompat.DEFAULT_VIBRATE
import androidx.core.app.NotificationManagerCompat import androidx.core.app.NotificationManagerCompat
import androidx.core.content.ContextCompat.getSystemService
import androidx.work.* import androidx.work.*
import com.badlogic.gdx.backends.android.AndroidApplication import com.badlogic.gdx.backends.android.AndroidApplication
import com.unciv.logic.GameInfo import com.unciv.logic.GameInfo
@ -27,13 +28,18 @@ class MultiplayerTurnCheckWorker(appContext: Context, workerParams: WorkerParame
const val WORK_TAG = "UNCIV_MULTIPLAYER_TURN_CHECKER_WORKER" const val WORK_TAG = "UNCIV_MULTIPLAYER_TURN_CHECKER_WORKER"
const val NOTIFICATION_ID_SERVICE = 1 const val NOTIFICATION_ID_SERVICE = 1
const val NOTIFICATION_ID_INFO = 2 const val NOTIFICATION_ID_INFO = 2
// Notification Channels can't be modified after creation.
// Therefore Unciv needs to create new ones and delete previously used ones.
// Add old channel names here when replacing them with new ones below.
private val HISTORIC_NOTIFICATION_CHANNELS = arrayOf("UNCIV_NOTIFICATION_CHANNEL_SERVICE")
private const val NOTIFICATION_CHANNEL_ID_INFO = "UNCIV_NOTIFICATION_CHANNEL_INFO" private const val NOTIFICATION_CHANNEL_ID_INFO = "UNCIV_NOTIFICATION_CHANNEL_INFO"
private const val NOTIFICATION_CHANNEL_ID_SERVICE = "UNCIV_NOTIFICATION_CHANNEL_SERVICE" private const val NOTIFICATION_CHANNEL_ID_SERVICE = "UNCIV_NOTIFICATION_CHANNEL_SERVICE_02"
// These fields need to be volatile because they are set by main thread but later accessed by worker thread. // These fields need to be volatile because they are set by main thread but later accessed by worker thread.
// Classes used here need to be primitive or internally synchronized to avoid visibility issues. // Classes used here need to be primitive or internally synchronized to avoid visibility issues.
// Long and double must not be used here because they have visibility issues: https://stackoverflow.com/a/9278798
@Volatile private var failCount = 0 @Volatile private var failCount = 0
@Volatile private var gameId = "" @Volatile private var gameId = ""
@Volatile private var userId = "" @Volatile private var userId = ""
@ -90,6 +96,8 @@ class MultiplayerTurnCheckWorker(appContext: Context, workerParams: WorkerParame
val descriptionText = "Shown constantly to inform you about background checking." val descriptionText = "Shown constantly to inform you about background checking."
val importance = NotificationManager.IMPORTANCE_MIN val importance = NotificationManager.IMPORTANCE_MIN
val mChannel = NotificationChannel(NOTIFICATION_CHANNEL_ID_SERVICE, name, importance) val mChannel = NotificationChannel(NOTIFICATION_CHANNEL_ID_SERVICE, name, importance)
mChannel.setShowBadge(false)
mChannel.setLockscreenVisibility(NotificationCompat.VISIBILITY_PUBLIC)
mChannel.description = descriptionText mChannel.description = descriptionText
val notificationManager = appContext.getSystemService(AndroidApplication.NOTIFICATION_SERVICE) as NotificationManager val notificationManager = appContext.getSystemService(AndroidApplication.NOTIFICATION_SERVICE) as NotificationManager
@ -110,16 +118,17 @@ class MultiplayerTurnCheckWorker(appContext: Context, workerParams: WorkerParame
val notification: NotificationCompat.Builder = NotificationCompat.Builder(appContext, NOTIFICATION_CHANNEL_ID_SERVICE) val notification: NotificationCompat.Builder = NotificationCompat.Builder(appContext, NOTIFICATION_CHANNEL_ID_SERVICE)
.setPriority(NotificationManagerCompat.IMPORTANCE_MIN) // it's only a status .setPriority(NotificationManagerCompat.IMPORTANCE_MIN) // it's only a status
.setContentTitle("Unciv multiplayer turn notifier running".tr()) .setContentTitle(("Last online turn check: [$lastTimeChecked]").tr())
.setStyle(NotificationCompat.BigTextStyle() .setStyle(NotificationCompat.BigTextStyle()
.bigText("Unciv will inform you when it's your turn.".tr() + "\n" + .bigText("Unciv will inform you when it's your turn in multiplayer.".tr() + " " +
"Last checked: [$lastTimeChecked]. Checks ca. every [$checkPeriod] minute(s) when Internet available.".tr() "Checks ca. every [$checkPeriod] minute(s) when Internet available.".tr()
+ " " + "Configurable in Unciv options menu.".tr())) + " " + "Configurable in Unciv options menu.".tr()))
.setSmallIcon(R.drawable.uncivicon2) .setSmallIcon(R.drawable.uncivicon2)
.setContentIntent(pendingIntent) .setContentIntent(pendingIntent)
.setCategory(NotificationCompat.CATEGORY_SERVICE) .setCategory(NotificationCompat.CATEGORY_SERVICE)
.setOnlyAlertOnce(true) .setOnlyAlertOnce(true)
.setOngoing(true) .setOngoing(true)
.setShowWhen(false)
with(NotificationManagerCompat.from(appContext)) { with(NotificationManagerCompat.from(appContext)) {
notify(NOTIFICATION_ID_INFO, notification.build()) notify(NOTIFICATION_ID_INFO, notification.build())
@ -168,6 +177,30 @@ class MultiplayerTurnCheckWorker(appContext: Context, workerParams: WorkerParame
enqueue(applicationContext, 1) enqueue(applicationContext, 1)
} }
} }
/**
* Necessary for Multiplayer Turner Checker, starting with Android Oreo
*/
fun createNotificationChannels(appContext: Context) {
createNotificationChannelInfo(appContext)
createNotificationChannelService(appContext)
destroyOldChannels(appContext)
}
/**
* Notification Channels can't be modified after creation.
* Therefore Unciv needs to create new ones and delete legacy ones.
*/
private fun destroyOldChannels(appContext: Context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notificationManager = appContext.getSystemService(AndroidApplication.NOTIFICATION_SERVICE) as NotificationManager
HISTORIC_NOTIFICATION_CHANNELS.forEach {
if (null != notificationManager.getNotificationChannel(it)) {
notificationManager.deleteNotificationChannel(it)
}
}
}
}
} }
override fun doWork(): Result { override fun doWork(): Result {