Skip to content

Commit 0ca1fe4

Browse files
committed
misc changes
1 parent 09947ed commit 0ca1fe4

File tree

6 files changed

+61
-26
lines changed

6 files changed

+61
-26
lines changed

app/src/main/java/org/akanework/gramophone/logic/utils/MediaRoutes.kt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,8 @@ object MediaRoutes {
262262
// TODO: It seems like speaker is the default fallback for anything. So filtering
263263
// for speaker will not work well.
264264
// https://cs.android.com/android/platform/superproject/+/android10-release:frameworks/base/services/core/java/com/android/server/audio/AudioDeviceInventory.java;l=863;drc=f7345252b8b33fe7cf69622f55e4226b6ef0100d
265-
return audioManager.firstOutputDeviceByType(AudioDeviceInfo.TYPE_BUILTIN_SPEAKER)
265+
return audioManager.firstOutputDeviceByType(AudioDeviceInfo.TYPE_BUILTIN_SPEAKER,
266+
AudioDeviceInfo.TYPE_BUILTIN_EARPIECE)
266267
} catch (t: Resources.NotFoundException) {
267268
Log.w(TAG, "Failed to check if $this is speaker", t)
268269
}
@@ -307,8 +308,10 @@ object MediaRoutes {
307308
try {
308309
if (TextUtils.equals(
309310
Resources.getSystem().getText(
310-
Resources.getSystem()
311-
.getIdentifier("default_audio_route_name_hdmi", "string", "android")
311+
Resources.getSystem().getIdentifier(
312+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
313+
"default_audio_route_name_hdmi" else
314+
"default_media_route_name_hdmi", "string", "android")
312315
), name
313316
)
314317
)
@@ -359,7 +362,8 @@ object MediaRoutes {
359362
// TODO: It seems like speaker is the default fallback for anything. So filtering
360363
// for speaker will not work well.
361364
// https://cs.android.com/android/platform/superproject/+/android10-release:frameworks/base/services/core/java/com/android/server/audio/AudioDeviceInventory.java;l=863;drc=f7345252b8b33fe7cf69622f55e4226b6ef0100d
362-
return audioManager.firstOutputDeviceByType(AudioDeviceInfo.TYPE_BUILTIN_SPEAKER)
365+
return audioManager.firstOutputDeviceByType(AudioDeviceInfo.TYPE_BUILTIN_SPEAKER,
366+
AudioDeviceInfo.TYPE_BUILTIN_EARPIECE)
363367
} catch (t: Resources.NotFoundException) {
364368
Log.w(TAG, "Failed to check if $this is speaker", t)
365369
}

app/src/main/java/org/akanework/gramophone/logic/utils/PostAmpAudioSink.kt

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,14 @@ class PostAmpAudioSink(
111111
private var rgVolume = 1f
112112

113113
init {
114-
var forVolumeChanged = false
115-
try {
116-
AudioSystemHiddenApi.addVolumeCallback(context, this)
117-
} catch (e: Exception) {
118-
Log.e(TAG, "failed to register volume cb", e)
119-
forVolumeChanged = true
114+
var forVolumeChanged = Build.VERSION.SDK_INT < Build.VERSION_CODES.Q
115+
if (!forVolumeChanged) {
116+
try {
117+
AudioSystemHiddenApi.addVolumeCallback(context, this)
118+
} catch (e: Exception) {
119+
Log.e(TAG, "failed to register volume cb", e)
120+
forVolumeChanged = true
121+
}
120122
}
121123
ContextCompat.registerReceiver(
122124
context,
@@ -542,10 +544,12 @@ class PostAmpAudioSink(
542544
volumeEffect?.releaseSafe()
543545
volumeEffect = null
544546
context.unregisterReceiver(receiver)
545-
try {
546-
AudioSystemHiddenApi.removeVolumeCallback(context, this)
547-
} catch (e: Exception) {
548-
Log.w(TAG, "failed to remove volume cb", e)
547+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
548+
try {
549+
AudioSystemHiddenApi.removeVolumeCallback(context, this)
550+
} catch (e: Exception) {
551+
Log.w(TAG, "failed to remove volume cb", e)
552+
}
549553
}
550554
super.release()
551555
}

app/src/main/java/org/akanework/gramophone/ui/MainActivity.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import android.net.Uri
3131
import android.os.Bundle
3232
import android.os.Handler
3333
import android.os.Looper
34-
import android.os.PersistableBundle
3534
import android.provider.MediaStore
3635
import android.provider.Settings
3736
import android.view.Choreographer
@@ -83,7 +82,6 @@ import org.akanework.gramophone.ui.components.PlayerBottomSheet
8382
import org.akanework.gramophone.ui.fragments.BaseFragment
8483
import org.akanework.gramophone.ui.fragments.SearchFragment
8584
import org.akanework.gramophone.ui.fragments.ViewPagerFragment
86-
import org.json.JSONObject
8785
import uk.akane.libphonograph.manipulator.ItemManipulator
8886
import java.io.File
8987

@@ -228,7 +226,7 @@ class MainActivity : BaseActivity() {
228226
.setItems((playlists.map {
229227
it.title ?: it.path?.absolutePath ?: it.id.toString()
230228
} + getString(R.string.create_playlist)).toTypedArray())
231-
{ d, item ->
229+
{ _, item ->
232230
if (playlists.size == item) {
233231
PlaylistAdapter.playlistNameDialog(this, R.string.create_playlist, "") { name ->
234232
CoroutineScope(Dispatchers.Default).launch {
@@ -340,8 +338,8 @@ class MainActivity : BaseActivity() {
340338
}
341339
}
342340

343-
override fun onSaveInstanceState(outState: Bundle, outPersistentState: PersistableBundle) {
344-
super.onSaveInstanceState(outState, outPersistentState)
341+
override fun onSaveInstanceState(outState: Bundle) {
342+
super.onSaveInstanceState(outState)
345343
if (pendingRequest != null) {
346344
outState.putBundle("AddToPlaylistPendingRequest", pendingRequest)
347345
}

app/src/main/java/org/akanework/gramophone/ui/fragments/PlaylistEditFragment.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ class PlaylistEditFragment : BaseFragment(false) {
3131
): View? {
3232
val theItem = MutableSharedFlow<Playlist?>(replay = 1)
3333

34+
// TODO(ASAP): show warning and offer to convert (non-destructively) playlist if it's
35+
// MediaStore DB only or unsupported format
3436
val rootView = inflater.inflate(R.layout.fragment_general_sub, container, false)
3537
val topAppBar = rootView.findViewById<MaterialToolbar>(R.id.topAppBar)
3638
val collapsingToolbarLayout =

app/src/main/java/uk/akane/libphonograph/reader/Reader.kt

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package uk.akane.libphonograph.reader
22

3+
import android.content.ContentResolver
34
import android.content.ContentUris
45
import android.content.Context
56
import android.net.Uri
@@ -169,13 +170,36 @@ internal object Reader {
169170
val hasVolume = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
170171
MediaStore.getExternalVolumeNames(context).contains(MediaStore.VOLUME_EXTERNAL_PRIMARY)
171172
} else true
172-
val cursor = if (hasVolume) context.contentResolver.query(
173-
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
174-
projection,
175-
selection,
176-
null,
177-
MediaStore.Audio.Media.TITLE + " COLLATE UNICODE ASC",
178-
) else null
173+
val sortOrder = MediaStore.Audio.Media.TITLE + " COLLATE UNICODE ASC"
174+
val cursor = if (hasVolume) {
175+
// TODO: convert coroutine cancellation to cancellationSignal
176+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
177+
val queryArgs = Bundle()
178+
queryArgs.putString(ContentResolver.QUERY_ARG_SQL_SELECTION, selection)
179+
queryArgs.putString(ContentResolver.QUERY_ARG_SQL_SORT_ORDER, sortOrder)
180+
/*if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
181+
queryArgs.putInt(MediaStore.QUERY_ARG_MATCH_PENDING, MediaStore.MATCH_INCLUDE)
182+
} TODO separately find pending files (but only pending by fuse) and if they
183+
were last modified over Y seconds ago (assumes every write updates mtime), scan
184+
them. in the end we dont do much harm with scanning too early so whatever.*/
185+
// TODO: maybe we can use QUERY_ARG_INCLUDE_RECENTLY_UNMOUNTED_VOLUMES?
186+
context.contentResolver.query(
187+
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
188+
projection,
189+
queryArgs,
190+
null
191+
)
192+
} else {
193+
context.contentResolver.query(
194+
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
195+
projection,
196+
selection,
197+
null,
198+
sortOrder,
199+
null
200+
)
201+
}
202+
} else null
179203
val defaultZone by lazy {
180204
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
181205
ZoneId.systemDefault()

hificore/src/main/java/org/nift4/gramophone/hificore/AudioSystemHiddenApi.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import android.media.AudioManager
66
import android.os.Build
77
import android.os.IBinder
88
import android.os.Parcel
9+
import androidx.annotation.RequiresApi
910
import androidx.core.content.getSystemService
1011
import androidx.media3.common.util.Log
1112
import com.google.common.util.concurrent.MoreExecutors
@@ -272,6 +273,7 @@ object AudioSystemHiddenApi {
272273
}
273274

274275
@Throws(IllegalStateException::class, IllegalArgumentException::class)
276+
@RequiresApi(Build.VERSION_CODES.Q)
275277
fun addVolumeCallback(context: Context, cb: VolumeChangeListener) {
276278
if (adapterCache.containsKey(cb))
277279
throw IllegalArgumentException("already registered $cb")
@@ -310,6 +312,7 @@ object AudioSystemHiddenApi {
310312
}
311313

312314
@Throws(IllegalStateException::class, IllegalArgumentException::class)
315+
@RequiresApi(Build.VERSION_CODES.Q)
313316
fun removeVolumeCallback(context: Context, cb: VolumeChangeListener) {
314317
val adapter =
315318
adapterCache.remove(cb) ?: throw IllegalArgumentException("never registered $cb")

0 commit comments

Comments
 (0)