From 67d3e8a52d4bbd3d91ee0a9f10c1bd1b852340bd Mon Sep 17 00:00:00 2001 From: Mygod Date: Fri, 28 Dec 2018 14:52:46 +0800 Subject: [PATCH] Show invisible foreground notification on television to avoid being killed --- mobile/src/main/java/be/mygod/vpnhotspot/App.kt | 2 ++ .../main/java/be/mygod/vpnhotspot/LocalOnlyHotspotService.kt | 4 ++++ mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt | 5 +++++ 3 files changed, 11 insertions(+) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/App.kt b/mobile/src/main/java/be/mygod/vpnhotspot/App.kt index 8126d3e1..990d6a35 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/App.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/App.kt @@ -2,6 +2,7 @@ package be.mygod.vpnhotspot import android.annotation.SuppressLint import android.app.Application +import android.app.UiModeManager import android.content.SharedPreferences import android.content.res.Configuration import android.net.ConnectivityManager @@ -49,6 +50,7 @@ class App : Application() { val handler = Handler() val pref: SharedPreferences by lazy { PreferenceManager.getDefaultSharedPreferences(deviceStorage) } val connectivity by lazy { getSystemService()!! } + val uiMode by lazy { getSystemService()!! } val wifi by lazy { getSystemService()!! } val operatingChannel: Int get() { diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/LocalOnlyHotspotService.kt b/mobile/src/main/java/be/mygod/vpnhotspot/LocalOnlyHotspotService.kt index 6617040f..36ed6fd5 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/LocalOnlyHotspotService.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/LocalOnlyHotspotService.kt @@ -2,7 +2,9 @@ package be.mygod.vpnhotspot import android.content.Intent import android.content.IntentFilter +import android.content.res.Configuration import android.net.wifi.WifiManager +import android.os.Build import androidx.annotation.RequiresApi import be.mygod.vpnhotspot.App.Companion.app import be.mygod.vpnhotspot.net.TetheringManager @@ -65,6 +67,8 @@ class LocalOnlyHotspotService : IpNeighbourMonitoringService() { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { binder.iface = "" + // show invisible foreground notification on television to avoid being killed + if (app.uiMode.currentModeType == Configuration.UI_MODE_TYPE_TELEVISION) updateNotification() // throws IllegalStateException if the caller attempts to start the LocalOnlyHotspot while they // have an outstanding request. // https://android.googlesource.com/platform/frameworks/opt/net/wifi/+/53e0284/service/java/com/android/server/wifi/WifiServiceImpl.java#1192 diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt b/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt index 781cc918..88bbd05d 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt @@ -3,6 +3,7 @@ package be.mygod.vpnhotspot import android.app.Service import android.content.Intent import android.content.SharedPreferences +import android.content.res.Configuration import android.net.NetworkInfo import android.net.wifi.WpsInfo import android.net.wifi.p2p.WifiP2pDevice @@ -216,6 +217,10 @@ class RepeaterService : Service(), WifiP2pManager.ChannelListener, SharedPrefere override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { if (status != Status.IDLE) return START_NOT_STICKY status = Status.STARTING + // show invisible foreground notification on television to avoid being killed + if (Build.VERSION.SDK_INT >= 26 && app.uiMode.currentModeType == Configuration.UI_MODE_TYPE_TELEVISION) { + showNotification() + } unregisterReceiver() registerReceiver(receiver, intentFilter(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION, WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION))