v0.8.0
This commit is contained in:
commit
4cb9717db2
97 files changed
+5946
No files matched your search
@@ -0,0 +1,136 @@
|
||||
package io.simplydvb.tv
|
||||
|
||||
import android.app.Notification
|
||||
import android.app.NotificationChannel
|
||||
import android.app.NotificationManager
|
||||
import android.app.Service
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.IntentFilter
|
||||
import android.content.pm.ServiceInfo
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.os.Handler
|
||||
import android.os.IBinder
|
||||
import android.os.Looper
|
||||
import android.provider.Settings
|
||||
import android.util.Log
|
||||
import io.simplydvb.tv.ui.LiveActivity
|
||||
|
||||
/**
|
||||
* Optional autostart of the own player: when the TV starts and when it wakes up from
|
||||
* standby (Sony TVs rarely boot – the power button only switches the screen).
|
||||
*
|
||||
* Android 10+ lets an app open a screen from the background only with the
|
||||
* "Display over other apps" permission, so that is required.
|
||||
*/
|
||||
object AutoStart {
|
||||
private const val TAG = "simply-tv"
|
||||
private fun prefs(ctx: Context) = ctx.getSharedPreferences("live", Context.MODE_PRIVATE)
|
||||
|
||||
fun enabled(ctx: Context) = prefs(ctx).getBoolean("autostart", false)
|
||||
|
||||
fun setEnabled(ctx: Context, on: Boolean) {
|
||||
prefs(ctx).edit().putBoolean("autostart", on).apply()
|
||||
apply(ctx)
|
||||
}
|
||||
|
||||
fun allowed(ctx: Context) = Settings.canDrawOverlays(ctx)
|
||||
|
||||
/** Opens the system screen for "Display over other apps"; false when the TV has none. */
|
||||
fun requestPermission(ctx: Context): Boolean = try {
|
||||
ctx.startActivity(
|
||||
Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + ctx.packageName))
|
||||
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK),
|
||||
)
|
||||
true
|
||||
} catch (_: Exception) {
|
||||
false
|
||||
}
|
||||
|
||||
/** Starts or stops the watcher service according to the setting. */
|
||||
fun apply(ctx: Context) {
|
||||
val i = Intent(ctx, WakeService::class.java)
|
||||
if (enabled(ctx)) {
|
||||
try {
|
||||
ctx.startForegroundService(i)
|
||||
} catch (e: Exception) {
|
||||
Log.w(TAG, "autostart service: $e")
|
||||
}
|
||||
} else {
|
||||
ctx.stopService(i)
|
||||
}
|
||||
}
|
||||
|
||||
fun launchPlayer(ctx: Context, reason: String) {
|
||||
if (!enabled(ctx) || ServerConfig.all(ctx).none { it.configured }) return
|
||||
if (!allowed(ctx)) {
|
||||
Log.w(TAG, "autostart ($reason): no 'display over other apps' permission")
|
||||
return
|
||||
}
|
||||
Log.i(TAG, "autostart ($reason)")
|
||||
try {
|
||||
ctx.startActivity(
|
||||
Intent(ctx, LiveActivity::class.java)
|
||||
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_REORDER_TO_FRONT),
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
Log.w(TAG, "autostart: $e")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** TV started: start the watcher and open the player. */
|
||||
class BootReceiver : BroadcastReceiver() {
|
||||
override fun onReceive(ctx: Context, intent: Intent) {
|
||||
if (!AutoStart.enabled(ctx)) return
|
||||
AutoStart.apply(ctx)
|
||||
if (intent.action == Intent.ACTION_MY_PACKAGE_REPLACED) return
|
||||
// let the launcher come up first, otherwise it covers the player
|
||||
Handler(Looper.getMainLooper()).postDelayed({ AutoStart.launchPlayer(ctx, "boot") }, 4000)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Keeps a screen-on listener (it can only be registered at run time) so the player also
|
||||
* opens when the TV wakes up from standby.
|
||||
*/
|
||||
class WakeService : Service() {
|
||||
private val main = Handler(Looper.getMainLooper())
|
||||
private val receiver = object : BroadcastReceiver() {
|
||||
override fun onReceive(ctx: Context, intent: Intent) {
|
||||
main.removeCallbacksAndMessages(null)
|
||||
main.postDelayed({ AutoStart.launchPlayer(ctx, "wake") }, 2500)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
val nm = getSystemService(NotificationManager::class.java)
|
||||
nm.createNotificationChannel(NotificationChannel("autostart", getString(R.string.autostart), NotificationManager.IMPORTANCE_MIN))
|
||||
val n = Notification.Builder(this, "autostart")
|
||||
.setSmallIcon(R.drawable.ic_tv)
|
||||
.setContentTitle(getString(R.string.autostart))
|
||||
.build()
|
||||
if (Build.VERSION.SDK_INT >= 34) {
|
||||
startForeground(1, n, ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE)
|
||||
} else {
|
||||
startForeground(1, n)
|
||||
}
|
||||
registerReceiver(receiver, IntentFilter(Intent.ACTION_SCREEN_ON))
|
||||
}
|
||||
|
||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||
if (!AutoStart.enabled(this)) stopSelf()
|
||||
return START_STICKY
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
unregisterReceiver(receiver)
|
||||
main.removeCallbacksAndMessages(null)
|
||||
super.onDestroy()
|
||||
}
|
||||
|
||||
override fun onBind(intent: Intent?): IBinder? = null
|
||||
}
|
||||
Reference in new issue
Block a user