Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
android:maxSdkVersion="28" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="32" />
<uses-permission android:name="android.permission.READ_MEDIA_AUDIO" />

<uses-feature
android:name="android.hardware.usb.host"
Expand All @@ -47,6 +48,8 @@
<application
android:name=".LastWaveApplication"
android:allowBackup="true"
android:fullBackupContent="@xml/backup_rules"
android:dataExtractionRules="@xml/data_extraction_rules"
android:appCategory="audio"
android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/com/lastwave/app/LastWaveApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ class LastWaveApplication : Application(), ImageLoaderFactory {
// asynchronously on startup so offline playback works immediately.
applicationScope.launch(Dispatchers.IO) {
delay(OPTIONAL_STARTUP_DELAY_MS)
runCatching { trackDownloadManager.get().restoreDownloadsAfterReinstall() }
runCatching { trackDownloadManager.get().syncDownloadsFromStorage() }
.onFailure { com.lastwave.app.diagnostics.AppLog.e("LastWaveStartup", "Download sync startup failed", it) }
}
Expand Down
58 changes: 52 additions & 6 deletions app/src/main/java/com/lastwave/app/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.lastwave.app.util.AppLocaleManager
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.drop
import kotlinx.coroutines.flow.map
Expand Down Expand Up @@ -50,7 +51,20 @@ class MainActivity : androidx.fragment.app.FragmentActivity() {
@Inject
lateinit var appLocaleManager: AppLocaleManager

private val notificationPermission = registerForActivityResult(ActivityResultContracts.RequestPermission()) { }
@Inject
lateinit var trackDownloadManager: dagger.Lazy<com.lastwave.app.data.download.TrackDownloadManager>

private val audioPermissionLauncher = registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted ->
if (isGranted) {
lifecycleScope.launch(Dispatchers.IO) {
runCatching { trackDownloadManager.get().restoreDownloadsAfterReinstall() }
}
}
}

private val notificationPermission = registerForActivityResult(ActivityResultContracts.RequestPermission()) {
checkAndRequestAudioPermission()
}

override fun attachBaseContext(newBase: Context) {
// FragmentActivity gets no AppCompat locale backport, so pin the
Expand Down Expand Up @@ -124,11 +138,19 @@ class MainActivity : androidx.fragment.app.FragmentActivity() {
),
)
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU &&
checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED
) {
runCatching { notificationPermission.launch(Manifest.permission.POST_NOTIFICATIONS) }
.onFailure { android.util.Log.w(STARTUP_TAG, "Notification permission request skipped", it) }
if (savedInstanceState == null) {
val needsNotificationPermission = Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU &&
checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED

if (needsNotificationPermission) {
runCatching { notificationPermission.launch(Manifest.permission.POST_NOTIFICATIONS) }
.onFailure {
android.util.Log.w(STARTUP_TAG, "Notification permission request skipped", it)
checkAndRequestAudioPermission()
}
} else {
checkAndRequestAudioPermission()
}
}

setContent {
Expand Down Expand Up @@ -276,7 +298,31 @@ class MainActivity : androidx.fragment.app.FragmentActivity() {
handleNavigationIntent(intent)
}

private fun checkAndRequestAudioPermission() {
if (hasPromptedAudioThisSession) return
hasPromptedAudioThisSession = true

val downloadManager = trackDownloadManager.get()
if (downloadManager.isDownloadsScanned()) return

val requiredPermission = downloadManager.getRequiredAudioPermission()
if (checkSelfPermission(requiredPermission) == PackageManager.PERMISSION_GRANTED) {
lifecycleScope.launch(Dispatchers.IO) {
runCatching { downloadManager.restoreDownloadsAfterReinstall() }
}
return
}

runCatching {
audioPermissionLauncher.launch(requiredPermission)
}.onFailure {
android.util.Log.w(STARTUP_TAG, "Audio permission request skipped", it)
}
}

private companion object {
const val STARTUP_TAG = "LastWaveStartup"
@Volatile
var hasPromptedAudioThisSession = false
}
}
Loading
Loading