android: make intent optional in onStartCommand (#229)

Updates tailscale/corp#18202

Kotlin requires a nullable optional here.  The rest of this is ktfmt

Signed-off-by: Jonathan Nobels <jonathan@tailscale.com>
pull/232/head
Jonathan Nobels 3 months ago committed by GitHub
parent 72753bb82a
commit 7b7f7254ba
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -10,27 +10,28 @@ import android.os.Build
import android.system.OsConstants import android.system.OsConstants
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat import androidx.core.app.NotificationManagerCompat
import libtailscale.Libtailscale
import java.util.UUID import java.util.UUID
import libtailscale.Libtailscale
open class IPNService : VpnService(), libtailscale.IPNService { open class IPNService : VpnService(), libtailscale.IPNService {
private val randomID: String = UUID.randomUUID().toString() private val randomID: String = UUID.randomUUID().toString()
override fun id(): String { override fun id(): String {
return randomID return randomID
} }
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
if (intent != null && ACTION_STOP_VPN == intent.getAction()) { if (intent != null && ACTION_STOP_VPN == intent.action) {
(getApplicationContext() as App).autoConnect = false (applicationContext as App).autoConnect = false
close() close()
return START_NOT_STICKY return START_NOT_STICKY
} }
val app = getApplicationContext() as App val app = applicationContext as App
if (intent != null && "android.net.VpnService" == intent.getAction()) { if (intent != null && "android.net.VpnService" == intent.action) {
// Start VPN and connect to it due to Always-on VPN // Start VPN and connect to it due to Always-on VPN
val i = Intent(IPNReceiver.INTENT_CONNECT_VPN) val i = Intent(IPNReceiver.INTENT_CONNECT_VPN)
i.setPackage(getPackageName()) i.setPackage(packageName)
i.setClass(getApplicationContext(), IPNReceiver::class.java) i.setClass(applicationContext, IPNReceiver::class.java)
sendBroadcast(i) sendBroadcast(i)
Libtailscale.requestVPN(this) Libtailscale.requestVPN(this)
app.setWantRunning(true) app.setWantRunning(true)
@ -38,12 +39,11 @@ open class IPNService : VpnService(), libtailscale.IPNService {
} }
Libtailscale.requestVPN(this) Libtailscale.requestVPN(this)
if (app.vpnReady && app.autoConnect) { if (app.vpnReady && app.autoConnect) {
app.setWantRunning(true); app.setWantRunning(true)
} }
return START_STICKY return START_STICKY
} }
private fun close() { private fun close() {
stopForeground(true) stopForeground(true)
Libtailscale.serviceDisconnect(this) Libtailscale.serviceDisconnect(this)
@ -60,24 +60,29 @@ open class IPNService : VpnService(), libtailscale.IPNService {
} }
private fun configIntent(): PendingIntent { private fun configIntent(): PendingIntent {
return PendingIntent.getActivity(this, 0, Intent(this, IPNActivity::class.java), return PendingIntent.getActivity(
this,
0,
Intent(this, IPNActivity::class.java),
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE) PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
} }
private fun disallowApp(b: Builder, name: String) { private fun disallowApp(b: Builder, name: String) {
try { try {
b.addDisallowedApplication(name) b.addDisallowedApplication(name)
} catch (e: PackageManager.NameNotFoundException) { } catch (e: PackageManager.NameNotFoundException) {}
}
} }
override fun newBuilder(): VPNServiceBuilder { override fun newBuilder(): VPNServiceBuilder {
val b: Builder = Builder() val b: Builder =
Builder()
.setConfigureIntent(configIntent()) .setConfigureIntent(configIntent())
.allowFamily(OsConstants.AF_INET) .allowFamily(OsConstants.AF_INET)
.allowFamily(OsConstants.AF_INET6) .allowFamily(OsConstants.AF_INET6)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) b.setMetered(false) // Inherit the metered status from the underlying networks. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) b.setUnderlyingNetworks(null) // Use all available networks. b.setMetered(false) // Inherit the metered status from the underlying networks.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
b.setUnderlyingNetworks(null) // Use all available networks.
// RCS/Jibe https://github.com/tailscale/tailscale/issues/2322 // RCS/Jibe https://github.com/tailscale/tailscale/issues/2322
disallowApp(b, "com.google.android.apps.messaging") disallowApp(b, "com.google.android.apps.messaging")
@ -101,7 +106,8 @@ open class IPNService : VpnService(), libtailscale.IPNService {
} }
fun notify(title: String?, message: String?) { fun notify(title: String?, message: String?) {
val builder: NotificationCompat.Builder = NotificationCompat.Builder(this, App.NOTIFY_CHANNEL_ID) val builder: NotificationCompat.Builder =
NotificationCompat.Builder(this, App.NOTIFY_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification) .setSmallIcon(R.drawable.ic_notification)
.setContentTitle(title) .setContentTitle(title)
.setContentText(message) .setContentText(message)
@ -114,7 +120,8 @@ open class IPNService : VpnService(), libtailscale.IPNService {
} }
fun updateStatusNotification(title: String?, message: String?) { fun updateStatusNotification(title: String?, message: String?) {
val builder: NotificationCompat.Builder = NotificationCompat.Builder(this, App.STATUS_CHANNEL_ID) val builder: NotificationCompat.Builder =
NotificationCompat.Builder(this, App.STATUS_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification) .setSmallIcon(R.drawable.ic_notification)
.setContentTitle(title) .setContentTitle(title)
.setContentText(message) .setContentText(message)

Loading…
Cancel
Save