mirror of
https://github.com/yggdrasil-network/yggdrasil-android.git
synced 2025-04-28 14:15:08 +03:00
* Fixes #30, added quick settings icon. * Added saving of enabled state, added some fixes and refactorings. * Fixed a bug with turning on/off the VPN. * Fixed possibility to add duplicate servers in DNS settings.
This commit is contained in:
parent
ee81f4e902
commit
aa94ccad26
12 changed files with 441 additions and 60 deletions
|
@ -1,15 +1,28 @@
|
|||
package eu.neilalexander.yggdrasil
|
||||
|
||||
import android.app.Application
|
||||
import android.app.*
|
||||
import android.content.ComponentName
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.os.Build
|
||||
import android.service.quicksettings.TileService
|
||||
import androidx.annotation.RequiresApi
|
||||
import androidx.core.app.NotificationCompat
|
||||
|
||||
class GlobalApplication: Application() {
|
||||
const val PREF_KEY_ENABLED = "enabled"
|
||||
|
||||
class GlobalApplication: Application(), YggStateReceiver.StateReceiver {
|
||||
private lateinit var config: ConfigurationProxy
|
||||
private var currentState: State = State.Disabled
|
||||
var updaterConnections: Int = 0
|
||||
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
config = ConfigurationProxy(applicationContext)
|
||||
val callback = NetworkStateCallback(this)
|
||||
callback.register()
|
||||
val receiver = YggStateReceiver(this)
|
||||
receiver.register(this)
|
||||
}
|
||||
|
||||
fun subscribe() {
|
||||
|
@ -25,4 +38,63 @@ class GlobalApplication: Application() {
|
|||
fun needUiUpdates(): Boolean {
|
||||
return updaterConnections > 0
|
||||
}
|
||||
|
||||
fun getCurrentState(): State {
|
||||
return currentState
|
||||
}
|
||||
|
||||
@RequiresApi(Build.VERSION_CODES.N)
|
||||
override fun onStateChange(state: State) {
|
||||
if (state != currentState) {
|
||||
val componentName = ComponentName(this, YggTileService::class.java)
|
||||
TileService.requestListeningState(this, componentName)
|
||||
|
||||
if (state != State.Disabled) {
|
||||
val notification = createServiceNotification(this, state)
|
||||
val notificationManager: NotificationManager =
|
||||
this.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
||||
notificationManager.notify(SERVICE_NOTIFICATION_ID, notification)
|
||||
}
|
||||
|
||||
currentState = state
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun createServiceNotification(context: Context, state: State): Notification {
|
||||
// Create the NotificationChannel, but only on API 26+ because
|
||||
// the NotificationChannel class is new and not in the support library
|
||||
val channelId = "Foreground Service"
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
val name = context.getString(R.string.channel_name)
|
||||
val descriptionText = context.getString(R.string.channel_description)
|
||||
val importance = NotificationManager.IMPORTANCE_MIN
|
||||
val channel = NotificationChannel(channelId, name, importance).apply {
|
||||
description = descriptionText
|
||||
}
|
||||
// Register the channel with the system
|
||||
val notificationManager: NotificationManager =
|
||||
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
||||
notificationManager.createNotificationChannel(channel)
|
||||
}
|
||||
|
||||
val intent = Intent(context, MainActivity::class.java).apply {
|
||||
this.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
|
||||
}
|
||||
val pendingIntent: PendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
|
||||
|
||||
val text = when (state) {
|
||||
State.Disabled -> context.getText(R.string.tile_disabled)
|
||||
State.Enabled -> context.getText(R.string.tile_enabled)
|
||||
State.Connected -> context.getText(R.string.tile_connected)
|
||||
else -> context.getText(R.string.tile_disabled)
|
||||
}
|
||||
|
||||
return NotificationCompat.Builder(context, channelId)
|
||||
.setContentTitle(context.getText(R.string.app_name))
|
||||
.setContentText(text)
|
||||
.setSmallIcon(R.drawable.ic_tile_icon)
|
||||
.setContentIntent(pendingIntent)
|
||||
.setPriority(NotificationCompat.PRIORITY_MIN)
|
||||
.build()
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue