Skip to content

Commit

Permalink
fix launch on boot on Oreo+
Browse files Browse the repository at this point in the history
  • Loading branch information
tsynik committed Apr 26, 2024
1 parent a848365 commit a071da6
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 88 deletions.
58 changes: 37 additions & 21 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,29 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.QUICKBOOT_POWERON" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_ALL_DOWNLOADS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission
android:name="android.permission.ACCESS_WIFI_STATE"
tools:ignore="LeanbackUsesWifi" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.QUICKBOOT_POWERON" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission
android:name="android.permission.WRITE_SETTINGS"
android:name="android.permission.WRITE_SECURE_SETTINGS"
tools:ignore="ProtectedPermissions" />
<uses-permission
android:name="android.permission.WRITE_SECURE_SETTINGS"
android:name="android.permission.WRITE_SETTINGS"
tools:ignore="ProtectedPermissions" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

<uses-sdk tools:overrideLibrary="androidx.tvprovider" />

Expand All @@ -46,9 +49,9 @@
android:theme="@style/Theme.TorrServe.DayNight">
<activity
android:name="ru.yourok.torrserve.ui.activities.main.MainActivity"
android:launchMode="singleInstance"
android:configChanges="uiMode"
android:exported="true">
android:exported="true"
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.VIEW" />

Expand All @@ -72,9 +75,9 @@

<activity
android:name="ru.yourok.torrserve.ui.activities.play.PlayActivity"
android:theme="@style/PlayDialog.DayNight"
android:configChanges="uiMode"
android:exported="true">
android:exported="true"
android:theme="@style/PlayDialog.DayNight">
<intent-filter>
<action android:name="android.intent.action.VIEW" />

Expand Down Expand Up @@ -206,7 +209,8 @@
</intent-filter>
</activity>

<activity android:name="ru.yourok.torrserve.ui.activities.main.SettingsActivity"
<activity
android:name="ru.yourok.torrserve.ui.activities.main.SettingsActivity"
android:configChanges="uiMode"
android:exported="true">
<intent-filter>
Expand All @@ -225,8 +229,10 @@
android:resource="@xml/provider_paths" />
</provider>

<receiver android:name=".server.local.services.BootCompletedReceiver"
android:exported="true">
<receiver
android:name=".server.local.services.BCReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
Expand All @@ -249,12 +255,22 @@

</receiver>

<service android:name=".server.local.TorrService" />
<service android:name=".server.local.services.Notification" />
<service
android:name=".server.local.TorrService"
android:exported="false"
android:foregroundServiceType="mediaPlayback" />
<service
android:name=".server.local.services.NotificationTS"
android:exported="false"
android:foregroundServiceType="specialUse">
<property
android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE"
android:value="torrserver" />
</service>
<service
android:name=".server.local.services.GlobalTorrService"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"
android:exported="true">
android:exported="true"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>
Expand Down
62 changes: 45 additions & 17 deletions app/src/main/java/ru/yourok/torrserve/server/local/TorrService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@ package ru.yourok.torrserve.server.local

import android.app.Service
import android.content.Intent
import android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK
import android.os.Build
import android.os.IBinder
import android.util.Log
import androidx.core.app.NotificationCompat
import androidx.core.app.ServiceCompat
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch
import ru.yourok.torrserve.BuildConfig
import ru.yourok.torrserve.R
import ru.yourok.torrserve.ad.ADManager
import ru.yourok.torrserve.app.App
import ru.yourok.torrserve.atv.Utils
import ru.yourok.torrserve.server.api.Api
import ru.yourok.torrserve.server.local.services.NotificationHelper
import ru.yourok.torrserve.server.local.services.NotificationTS
import ru.yourok.torrserve.settings.Settings
import ru.yourok.torrserve.settings.Settings.isAccessibilityOn
import ru.yourok.torrserve.utils.Accessibility
Expand Down Expand Up @@ -46,14 +52,15 @@ class TorrService : Service() {

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
intent?.let {
val needToMoveToForeground = intent.getBooleanExtra(NEED_FOREGROUND_KEY, false)
if (it.action != null) {
when (it.action) {
ActionStart -> {
startServer()
ACTION_START -> {
startServer(needToMoveToForeground)
return START_STICKY
}

ActionStop -> {
ACTION_STOP -> {
stopServer(intent.hasExtra("forceclose"))
return START_NOT_STICKY
}
Expand All @@ -63,17 +70,28 @@ class TorrService : Service() {
return START_NOT_STICKY
}

private fun startServer() {
private fun startServer(nfg: Boolean = false) {
serviceScope.launch {
if (BuildConfig.DEBUG) Log.d("TorrService", "startServer(nfg:$nfg)")

if (isLocal() && isAccessibilityOn() && !Accessibility.isEnabledService(App.context)) {
if (BuildConfig.DEBUG) Log.d("TorrService", "Try to enable AssessibilityService")
Accessibility.enableService(App.context, true)
}

if (serverFile.exists() && isLocal() && Api.echo().isEmpty()) {
if (BuildConfig.DEBUG) Log.d("TorrService", "startServer()")
if (nfg) { // fix start local server on boot
val builder = NotificationCompat.Builder(this@TorrService, NotificationTS().channelId)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(getString(R.string.app_name))
.setContentText("Start Foreground")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
ServiceCompat.startForeground(this@TorrService, NotificationTS().notificationId, builder.build(), FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK)
}
serverFile.run()
notification.doBindService(this@TorrService)
}

Utils.updateAtvCards()
}
}
Expand All @@ -97,26 +115,36 @@ class TorrService : Service() {
}

companion object {
const val ActionStart = "ru.yourok.torrserve.server.action_start"
const val ActionStop = "ru.yourok.torrserve.server.action_stop"
const val ACTION_START = "ru.yourok.torrserve.server.action_start"
const val ACTION_STOP = "ru.yourok.torrserve.server.action_stop"
const val NEED_FOREGROUND_KEY = "need_foreground"


fun start() {
val context = App.context
val intent = Intent(context, TorrService::class.java)
intent.action = ACTION_START
intent.putExtra(NEED_FOREGROUND_KEY, false)
try {
val intent = Intent(App.context, TorrService::class.java)
intent.action = ActionStart
App.context.startService(intent)
} catch (e: Exception) {
e.printStackTrace()
context.startService(intent)
} catch (ex: IllegalStateException) {
if (isLocal()) { // avoid ANR on remote
intent.putExtra(NEED_FOREGROUND_KEY, true)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(intent)
}
}
}
}

fun stop() {
val context = App.context
val intent = Intent(context, TorrService::class.java)
intent.action = ACTION_STOP
try {
val intent = Intent(App.context, TorrService::class.java)
intent.action = ActionStop
App.context.startService(intent)
} catch (e: Exception) {
e.printStackTrace()
context.startService(intent)
} catch (ex: Exception) {
ex.printStackTrace()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import ru.yourok.torrserve.atv.Utils
import ru.yourok.torrserve.server.local.TorrService
import ru.yourok.torrserve.settings.Settings

class BootCompletedReceiver : BroadcastReceiver() {
class BCReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
if (Intent.ACTION_BOOT_COMPLETED == intent!!.action) {
if (Settings.isBootStart()) {
Expand Down
Loading

0 comments on commit a071da6

Please sign in to comment.