@ -61,11 +61,9 @@ class App : Application(), libtailscale.AppContext {
companion object {
companion object {
const val STATUS _CHANNEL _ID = " tailscale-status "
const val STATUS _CHANNEL _ID = " tailscale-status "
const val STATUS _NOTIFICATION _ID = 1
const val STATUS _NOTIFICATION _ID = 1
const val NOTIFY _CHANNEL _ID = " tailscale-notify "
const val NOTIFY _NOTIFICATION _ID = 2
private const val PEER _TAG = " peer "
private const val PEER _TAG = " peer "
private const val FILE _CHANNEL _ID = " tailscale-files "
private const val FILE _CHANNEL _ID = " tailscale-files "
private const val FILE _NOTIFICATION _ID = 3
private const val FILE _NOTIFICATION _ID = 2
private const val TAG = " App "
private const val TAG = " App "
private val networkConnectivityRequest =
private val networkConnectivityRequest =
NetworkRequest . Builder ( )
NetworkRequest . Builder ( )
@ -114,15 +112,13 @@ class App : Application(), libtailscale.AppContext {
Notifier . start ( applicationScope )
Notifier . start ( applicationScope )
connectivityManager = this . getSystemService ( Context . CONNECTIVITY _SERVICE ) as ConnectivityManager
connectivityManager = this . getSystemService ( Context . CONNECTIVITY _SERVICE ) as ConnectivityManager
setAndRegisterNetworkCallbacks ( )
setAndRegisterNetworkCallbacks ( )
createNotificationChannel (
NOTIFY _CHANNEL _ID , " Notifications " , NotificationManagerCompat . IMPORTANCE _DEFAULT )
createNotificationChannel (
createNotificationChannel (
STATUS _CHANNEL _ID , " VPN Status " , NotificationManagerCompat . IMPORTANCE _LOW )
STATUS _CHANNEL _ID , " VPN Status " , NotificationManagerCompat . IMPORTANCE _LOW )
createNotificationChannel (
createNotificationChannel (
FILE _CHANNEL _ID , " File transfers " , NotificationManagerCompat . IMPORTANCE _DEFAULT )
FILE _CHANNEL _ID , " File transfers " , NotificationManagerCompat . IMPORTANCE _DEFAULT )
appInstance = this
appInstance = this
applicationScope . launch {
applicationScope . launch {
Notifier . tileActive. collect { isTileReadyToBeActive -> setTileActive ( isTileReadyToBeActive ) }
Notifier . connStatus. collect { connStatus -> updateConnStatus ( connStatus ) }
}
}
}
}
@ -212,15 +208,33 @@ class App : Application(), libtailscale.AppContext {
EncryptedSharedPreferences . PrefValueEncryptionScheme . AES256 _GCM )
EncryptedSharedPreferences . PrefValueEncryptionScheme . AES256 _GCM )
}
}
fun setTileActive ( ready : Boolean ) {
fun updateConnStatus ( ready : Boolean ) {
if ( Build . VERSION . SDK _INT < Build . VERSION_CODES . N ) {
if ( Build . VERSION . SDK _INT < Build . VERSION_CODES . N ) {
return
return
}
}
QuickToggleService . setReady ( this , ready )
QuickToggleService . setReady ( this , ready )
Log . d ( " App " , " Set Tile Ready: $ready " )
Log . d ( " App " , " Set Tile Ready: $ready " )
val action = if ( ready ) IPNReceiver . INTENT _DISCONNECT _VPN else IPNReceiver . INTENT _CONNECT _VPN
val intent = Intent ( this , IPNReceiver :: class . java ) . apply {
this . action = action
}
val pendingIntent : PendingIntent = PendingIntent . getBroadcast (
this ,
0 ,
intent ,
PendingIntent . FLAG _UPDATE _CURRENT or PendingIntent . FLAG _IMMUTABLE
)
if ( ready ) {
if ( ready ) {
startVPN ( )
startVPN ( )
}
}
val notificationMessage = if ( ready ) getString ( R . string . connected ) else getString ( R . string . not _connected )
notify (
" Tailscale " ,
notificationMessage ,
STATUS _CHANNEL _ID ,
pendingIntent ,
STATUS _NOTIFICATION _ID
)
}
}
fun getHostname ( ) : String {
fun getHostname ( ) : String {
@ -323,12 +337,22 @@ class App : Application(), libtailscale.AppContext {
}
}
val pending : PendingIntent =
val pending : PendingIntent =
PendingIntent . getActivity ( this , 0 , viewIntent , PendingIntent . FLAG _UPDATE _CURRENT )
PendingIntent . getActivity ( this , 0 , viewIntent , PendingIntent . FLAG _UPDATE _CURRENT )
notify ( getString ( R . string . file _notification ) , msg , FILE _CHANNEL _ID , pending , FILE _NOTIFICATION _ID )
}
fun createNotificationChannel ( id : String ? , name : String ? , importance : Int ) {
val channel = NotificationChannel ( id , name , importance )
val nm : NotificationManagerCompat = NotificationManagerCompat . from ( this )
nm . createNotificationChannel ( channel )
}
fun notify ( title : String ? , message : String ? , channel : String , intent : PendingIntent ? , notificationID : Int ) {
val builder : NotificationCompat . Builder =
val builder : NotificationCompat . Builder =
NotificationCompat . Builder ( this , FILE _CHANNEL _ID )
NotificationCompat . Builder ( this , channel )
. setSmallIcon ( R . drawable . ic _notification )
. setSmallIcon ( R . drawable . ic _notification )
. setContentTitle ( " File received " )
. setContentTitle ( title )
. setContentText ( msg )
. setContentText ( m es sa ge )
. setContentIntent ( pending )
. setContentIntent ( intent )
. setAutoCancel ( true )
. setAutoCancel ( true )
. setOnlyAlertOnce ( true )
. setOnlyAlertOnce ( true )
. setPriority ( NotificationCompat . PRIORITY _DEFAULT )
. setPriority ( NotificationCompat . PRIORITY _DEFAULT )
@ -344,13 +368,7 @@ class App : Application(), libtailscale.AppContext {
// for ActivityCompat#requestPermissions for more details.
// for ActivityCompat#requestPermissions for more details.
return
return
}
}
nm . notify ( FILE _NOTIFICATION _ID , builder . build ( ) )
nm . notify ( notificationID , builder . build ( ) )
}
fun createNotificationChannel ( id : String ? , name : String ? , importance : Int ) {
val channel = NotificationChannel ( id , name , importance )
val nm : NotificationManagerCompat = NotificationManagerCompat . from ( this )
nm . createNotificationChannel ( channel )
}
}
override fun getInterfacesAsString ( ) : String {
override fun getInterfacesAsString ( ) : String {