Skip to content

Commit

Permalink
refactor: migrate to SdkCheck helper class
Browse files Browse the repository at this point in the history
  • Loading branch information
timschneeb committed Mar 12, 2023
1 parent 73eccc8 commit 062cce2
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import me.timschneeberger.rootlessjamesdsp.utils.Cache
import me.timschneeberger.rootlessjamesdsp.utils.Notifications
import me.timschneeberger.rootlessjamesdsp.utils.extensions.ContextExtensions.registerLocalReceiver
import me.timschneeberger.rootlessjamesdsp.utils.Preferences
import me.timschneeberger.rootlessjamesdsp.utils.sdkAbove
import org.koin.android.ext.android.inject
import org.koin.android.ext.koin.androidContext
import org.koin.android.ext.koin.androidLogger
Expand All @@ -36,8 +37,7 @@ import java.util.*

class MainApplication : Application(), SharedPreferences.OnSharedPreferenceChangeListener {
init {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
{
sdkAbove(Build.VERSION_CODES.P) {
HiddenApiBypass.addHiddenApiExemptions("L")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import me.timschneeberger.rootlessjamesdsp.BuildConfig
import me.timschneeberger.rootlessjamesdsp.service.RootAudioProcessorService
import me.timschneeberger.rootlessjamesdsp.service.RootlessAudioProcessorService
import me.timschneeberger.rootlessjamesdsp.utils.SystemServices
import me.timschneeberger.rootlessjamesdsp.utils.sdkAbove
import timber.log.Timber

/**
Expand All @@ -30,32 +31,31 @@ class EngineLauncherActivity : BaseActivity() {
return
}

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q)
return

// If projection token available, start immediately
if(app.mediaProjectionStartIntent != null) {
Timber.d("Reusing old projection token to start service")
RootlessAudioProcessorService.start(this, app.mediaProjectionStartIntent)
return
}
sdkAbove(Build.VERSION_CODES.Q) {
// If projection token available, start immediately
if(app.mediaProjectionStartIntent != null) {
Timber.d("Reusing old projection token to start service")
RootlessAudioProcessorService.start(this, app.mediaProjectionStartIntent)
return
}

setFinishOnTouchOutside(false)
setFinishOnTouchOutside(false)

capturePermissionLauncher = registerForActivityResult(
ActivityResultContracts.StartActivityForResult()
) { result ->
if (result.resultCode == RESULT_OK) {
app.mediaProjectionStartIntent = result.data
Timber.d("Using new projection token to start service")
capturePermissionLauncher = registerForActivityResult(
ActivityResultContracts.StartActivityForResult()
) { result ->
if (result.resultCode == RESULT_OK) {
app.mediaProjectionStartIntent = result.data
Timber.d("Using new projection token to start service")

RootlessAudioProcessorService.start(this, result.data)
RootlessAudioProcessorService.start(this, result.data)
}
finish()
}
finish()
}

SystemServices.get<MediaProjectionManager>(this)
.createScreenCaptureIntent()
.let(capturePermissionLauncher::launch)
SystemServices.get<MediaProjectionManager>(this)
.createScreenCaptureIntent()
.let(capturePermissionLauncher::launch)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import me.timschneeberger.rootlessjamesdsp.service.RootAudioProcessorService
import me.timschneeberger.rootlessjamesdsp.service.RootlessAudioProcessorService
import me.timschneeberger.rootlessjamesdsp.utils.Constants
import me.timschneeberger.rootlessjamesdsp.utils.Result
import me.timschneeberger.rootlessjamesdsp.utils.SdkCheck
import me.timschneeberger.rootlessjamesdsp.utils.StorageUtils
import me.timschneeberger.rootlessjamesdsp.utils.SystemServices
import me.timschneeberger.rootlessjamesdsp.utils.extensions.AssetManagerExtensions.installPrivateAssets
Expand All @@ -57,6 +58,7 @@ import me.timschneeberger.rootlessjamesdsp.utils.extensions.ContextExtensions.to
import me.timschneeberger.rootlessjamesdsp.utils.extensions.ContextExtensions.unregisterLocalReceiver
import me.timschneeberger.rootlessjamesdsp.utils.extensions.PermissionExtensions.hasDumpPermission
import me.timschneeberger.rootlessjamesdsp.utils.extensions.PermissionExtensions.hasRecordPermission
import me.timschneeberger.rootlessjamesdsp.utils.sdkAbove
import me.timschneeberger.rootlessjamesdsp.view.FloatingToggleButton
import org.koin.core.component.inject
import timber.log.Timber
Expand Down Expand Up @@ -166,8 +168,7 @@ class MainActivity : BaseActivity() {
showLibraryLoadError()

// Rootless: Check permissions and launch onboarding if required
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q &&
BuildConfig.ROOTLESS && (!hasDumpPermission() || !hasRecordPermission())) {
if(SdkCheck.isQ && BuildConfig.ROOTLESS && (!hasDumpPermission() || !hasRecordPermission())) {
Timber.i("Launching onboarding (first boot: $firstBoot)")

startActivity(Intent(this, OnboardingActivity::class.java).apply {
Expand Down Expand Up @@ -256,17 +257,18 @@ class MainActivity : BaseActivity() {
binding.powerToggle.toggleOnClick = false
binding.powerToggle.setOnToggleClickListener(object : FloatingToggleButton.OnToggleClickListener{
override fun onClick() {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && BuildConfig.ROOTLESS) {
if(SdkCheck.isQ && BuildConfig.ROOTLESS) {
if (binding.powerToggle.isToggled) {
// Currently on, let's turn it off
RootlessAudioProcessorService.stop(this@MainActivity)
binding.powerToggle.isToggled = false
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {

sdkAbove(Build.VERSION_CODES.R) {
binding.powerToggle.performHapticFeedback(HapticFeedbackConstants.REJECT)
}
} else {
// Currently off, let's turn it on
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
sdkAbove(Build.VERSION_CODES.R) {
binding.powerToggle.performHapticFeedback(HapticFeedbackConstants.CONFIRM)
}
requestCapturePermission()
Expand All @@ -279,7 +281,7 @@ class MainActivity : BaseActivity() {
}
})

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && BuildConfig.ROOTLESS) {
if (SdkCheck.isQ && BuildConfig.ROOTLESS) {
capturePermissionLauncher = registerForActivityResult(
ActivityResultContracts.StartActivityForResult()
) { result ->
Expand All @@ -294,7 +296,7 @@ class MainActivity : BaseActivity() {
}

// Rootless: request capture permission instantly, if redirected from onboarding
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && BuildConfig.ROOTLESS) {
if (SdkCheck.isQ && BuildConfig.ROOTLESS) {
if (intent.getBooleanExtra(EXTRA_FORCE_SHOW_CAPTURE_PROMPT, false)) {
requestCapturePermission()
}
Expand All @@ -312,7 +314,7 @@ class MainActivity : BaseActivity() {
}

// Root: request notification permission on Android 13 because the onboarding is not used for root
if (!BuildConfig.ROOTLESS && Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
if (!BuildConfig.ROOTLESS && SdkCheck.isTiramisu) {
runtimePermissionLauncher = registerForActivityResult(
ActivityResultContracts.RequestMultiplePermissions()
) { isGranted ->
Expand Down Expand Up @@ -495,12 +497,14 @@ class MainActivity : BaseActivity() {
}

private fun bindProcessorService() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && BuildConfig.ROOTLESS) {
Intent(this, RootlessAudioProcessorService::class.java).also { intent ->
val ret = bindService(intent, processorServiceConnection, 0)
// Service not active
if(!ret)
requestCapturePermission()
if (BuildConfig.ROOTLESS) {
sdkAbove(Build.VERSION_CODES.Q) {
Intent(this, RootlessAudioProcessorService::class.java).also { intent ->
val ret = bindService(intent, processorServiceConnection, 0)
// Service not active
if (!ret)
requestCapturePermission()
}
}
}
else if (!BuildConfig.ROOTLESS) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import me.timschneeberger.rootlessjamesdsp.model.room.AppBlocklistViewModel
import me.timschneeberger.rootlessjamesdsp.model.room.AppBlocklistViewModelFactory
import me.timschneeberger.rootlessjamesdsp.model.room.BlockedApp
import me.timschneeberger.rootlessjamesdsp.service.RootlessAudioProcessorService
import me.timschneeberger.rootlessjamesdsp.utils.SdkCheck
import me.timschneeberger.rootlessjamesdsp.utils.SystemServices
import me.timschneeberger.rootlessjamesdsp.utils.extensions.CompatExtensions.getParcelableAs
import me.timschneeberger.rootlessjamesdsp.utils.extensions.ContextExtensions.getAppIcon
Expand Down Expand Up @@ -80,7 +81,7 @@ class AppCompatibilityFragment : Fragment() {
Timber.d("Requesting retry")

projectIntent?.let {
if (BuildConfig.ROOTLESS && Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
if (BuildConfig.ROOTLESS && SdkCheck.isQ)
RootlessAudioProcessorService.start(requireContext(), it)
}

Expand All @@ -100,7 +101,7 @@ class AppCompatibilityFragment : Fragment() {

Timer("Reboot", false).schedule(100L) {
projectIntent?.let {
if (BuildConfig.ROOTLESS && Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
if (BuildConfig.ROOTLESS && SdkCheck.isQ)
RootlessAudioProcessorService.start(requireContext(), it)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import me.timschneeberger.rootlessjamesdsp.utils.extensions.PermissionExtensions
import me.timschneeberger.rootlessjamesdsp.utils.extensions.PermissionExtensions.hasNotificationPermission
import me.timschneeberger.rootlessjamesdsp.utils.extensions.PermissionExtensions.hasRecordPermission
import me.timschneeberger.rootlessjamesdsp.utils.Preferences
import me.timschneeberger.rootlessjamesdsp.utils.SdkCheck
import me.timschneeberger.rootlessjamesdsp.utils.sdkAbove
import org.koin.android.ext.android.inject
import rikka.shizuku.Shizuku
Expand Down Expand Up @@ -348,7 +349,7 @@ class OnboardingFragment : Fragment() {
}
else if(number == PAGE_RUNTIME_PERMISSIONS) {
val pageBinding = binding.onboardingPage5
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
if(!SdkCheck.isTiramisu) {
pageBinding.findViewById<View>(R.id.onboarding_notification_permission).visibility = View.GONE
}
}
Expand Down Expand Up @@ -510,7 +511,7 @@ class OnboardingFragment : Fragment() {
private fun updateSetupMethods() {
val page = binding.methodSelect

if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.Q) {
if(!SdkCheck.isQ) {
page.methodsShizukuCard.isEnabled = false
page.methodsShizukuCard.isClickable = false
page.methodsShizukuCard.isFocusable = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import me.timschneeberger.rootlessjamesdsp.utils.extensions.ContextExtensions.re
import me.timschneeberger.rootlessjamesdsp.utils.extensions.ContextExtensions.unregisterLocalReceiver
import me.timschneeberger.rootlessjamesdsp.utils.extensions.PermissionExtensions.hasProjectMediaAppOp
import me.timschneeberger.rootlessjamesdsp.utils.Preferences
import me.timschneeberger.rootlessjamesdsp.utils.sdkAbove
import org.koin.android.ext.android.inject
import org.koin.core.component.KoinComponent

Expand Down Expand Up @@ -104,13 +105,12 @@ class QuickTileService : TileService(),
return
}

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q)
return

// Rootless
if(!toggled)
sdkAbove(Build.VERSION_CODES.Q) {
// Rootless
if (!toggled)
RootlessAudioProcessorService.stop(this)
else
launchService()
else
launchService()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import me.timschneeberger.rootlessjamesdsp.utils.extensions.ContextExtensions.se
import me.timschneeberger.rootlessjamesdsp.utils.Preferences
import me.timschneeberger.rootlessjamesdsp.utils.ServiceNotificationHelper
import me.timschneeberger.rootlessjamesdsp.utils.SystemServices
import me.timschneeberger.rootlessjamesdsp.utils.sdkAbove
import org.koin.android.ext.android.inject
import org.koin.core.component.KoinComponent
import timber.log.Timber
Expand Down Expand Up @@ -92,13 +93,14 @@ class RootAudioProcessorService : BaseAudioProcessorService(), KoinComponent,
ServiceNotificationHelper.createServiceNotificationLegacy(this)
else
ServiceNotificationHelper.createServiceNotification(this, arrayOf())
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {

sdkAbove(Build.VERSION_CODES.Q) {
startForeground(
Notifications.ID_SERVICE_STATUS,
notification,
ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK
)
} else {
}.below {
startForeground(
Notifications.ID_SERVICE_STATUS,
notification
Expand Down Expand Up @@ -238,10 +240,11 @@ class RootAudioProcessorService : BaseAudioProcessorService(), KoinComponent,
return
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
sdkAbove(Build.VERSION_CODES.O) {
context.startForegroundService(intent)
else
}.below {
context.startService(intent)
}
}

fun startServiceEnhanced(context: Context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import me.timschneeberger.rootlessjamesdsp.utils.Preferences
import me.timschneeberger.rootlessjamesdsp.utils.ServiceNotificationHelper
import me.timschneeberger.rootlessjamesdsp.utils.SystemServices
import me.timschneeberger.rootlessjamesdsp.utils.extensions.CompatExtensions.getParcelableAs
import me.timschneeberger.rootlessjamesdsp.utils.sdkAbove
import org.koin.android.ext.android.inject
import timber.log.Timber
import java.io.IOException
Expand Down Expand Up @@ -551,7 +552,8 @@ class RootlessAudioProcessorService : BaseAudioProcessorService() {
.setUsage(AudioAttributes.USAGE_UNKNOWN)
.setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN)
.setFlags(0)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {

sdkAbove(Build.VERSION_CODES.Q) {
attributesBuilder.setAllowedCapturePolicy(AudioAttributes.ALLOW_CAPTURE_BY_NONE)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ val isDynamicColorAvailable by lazy {
fun View.setBackgroundFromAttribute(@AttrRes attrRes: Int) {
val a = TypedValue()
context.theme.resolveAttribute(attrRes, a, true)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && a.isColorType) {
if (SdkCheck.isQ && a.isColorType) {
setBackgroundColor(a.data)
} else {
background = ResourcesCompat.getDrawable(context.resources, a.resourceId, context.theme)
Expand Down

0 comments on commit 062cce2

Please sign in to comment.