Compare commits

..

4 Commits

Author SHA1 Message Date
birdbird
b0e850d17e Merge branch 'playlistSorting' into 'develop'
Don't sort playlists even when Sort by Disc is activated

Closes #1229

See merge request ultrasonic/ultrasonic!1007
2023-05-16 10:42:25 +00:00
tzugen
a97c6e15e9
Don't sort playlists even when Sort by Disc is activated 2023-05-16 09:59:20 +02:00
birdbird
d084a35316 Merge branch 'blue' into 'develop'
Fix missing bluetooth permissions

Closes #791

See merge request ultrasonic/ultrasonic!1006
2023-05-16 07:37:37 +00:00
birdbird
e8bfa5dc04 Fix missing bluetooth permissions 2023-05-16 07:37:36 +00:00
5 changed files with 144 additions and 119 deletions

View File

@ -3,6 +3,7 @@
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:installLocation="auto"> android:installLocation="auto">
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" /> <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

View File

@ -1,100 +0,0 @@
/*
This file is part of Subsonic.
Subsonic is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Subsonic is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Subsonic. If not, see <http://www.gnu.org/licenses/>.
Copyright 2010 (C) Sindre Mehus
*/
package org.moire.ultrasonic.receiver;
import android.annotation.SuppressLint;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothProfile;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import org.moire.ultrasonic.util.Constants;
import org.moire.ultrasonic.util.Settings;
import timber.log.Timber;
/**
* Resume or pause playback on Bluetooth A2DP connect/disconnect.
*
* @author Sindre Mehus
*/
@SuppressLint("MissingPermission")
public class BluetoothIntentReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
int state = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, -1);
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String action = intent.getAction();
String name = device != null ? device.getName() : "Unknown";
String address = device != null ? device.getAddress() : "Unknown";
Timber.d("A2DP State: %d; Action: %s; Device: %s; Address: %s", state, action, name, address);
boolean actionBluetoothDeviceConnected = false;
boolean actionBluetoothDeviceDisconnected = false;
boolean actionA2dpConnected = false;
boolean actionA2dpDisconnected = false;
if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action))
{
actionBluetoothDeviceConnected = true;
}
else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action) || BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED.equals(action))
{
actionBluetoothDeviceDisconnected = true;
}
if (state == android.bluetooth.BluetoothA2dp.STATE_CONNECTED) actionA2dpConnected = true;
else if (state == android.bluetooth.BluetoothA2dp.STATE_DISCONNECTED) actionA2dpDisconnected = true;
boolean resume = false;
boolean pause = false;
switch (Settings.getResumeOnBluetoothDevice())
{
case Constants.PREFERENCE_VALUE_ALL: resume = actionA2dpConnected || actionBluetoothDeviceConnected;
break;
case Constants.PREFERENCE_VALUE_A2DP: resume = actionA2dpConnected;
break;
}
switch (Settings.getPauseOnBluetoothDevice())
{
case Constants.PREFERENCE_VALUE_ALL: pause = actionA2dpDisconnected || actionBluetoothDeviceDisconnected;
break;
case Constants.PREFERENCE_VALUE_A2DP: pause = actionA2dpDisconnected;
break;
}
if (resume)
{
Timber.i("Connected to Bluetooth device %s address %s, resuming playback.", name, address);
context.sendBroadcast(new Intent(Constants.CMD_RESUME_OR_PLAY).setPackage(context.getPackageName()));
}
if (pause)
{
Timber.i("Disconnected from Bluetooth device %s address %s, requesting pause.", name, address);
context.sendBroadcast(new Intent(Constants.CMD_PAUSE).setPackage(context.getPackageName()));
}
}
}

View File

@ -0,0 +1,127 @@
/*
* BluetoothIntentReceiver.kt
* Copyright (C) 2009-2022 Ultrasonic developers
*
* Distributed under terms of the GNU GPLv3 license.
*/
package org.moire.ultrasonic.receiver
import android.Manifest
import android.bluetooth.BluetoothA2dp
import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothDevice.ACTION_ACL_CONNECTED
import android.bluetooth.BluetoothDevice.ACTION_ACL_DISCONNECTED
import android.bluetooth.BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED
import android.bluetooth.BluetoothProfile
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import androidx.core.app.ActivityCompat
import org.moire.ultrasonic.app.UApp
import org.moire.ultrasonic.util.Constants
import org.moire.ultrasonic.util.Constants.PREFERENCE_VALUE_A2DP
import org.moire.ultrasonic.util.Constants.PREFERENCE_VALUE_ALL
import org.moire.ultrasonic.util.Constants.PREFERENCE_VALUE_DISABLED
import org.moire.ultrasonic.util.Settings
import timber.log.Timber
/**
* Resume or pause playback on Bluetooth A2DP connect/disconnect.
*/
class BluetoothIntentReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val state = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, -1)
val device = intent.getBluetoothDevice()
val action = intent.action
// Whether to log the name of the bluetooth device
val name = device.getNameSafely()
Timber.d("Bluetooth device: $name; State: $state; Action: $action")
// In these flags we store what kind of device (any or a2dp) has (dis)connected
var connectionStatus = PREFERENCE_VALUE_DISABLED
var disconnectionStatus = PREFERENCE_VALUE_DISABLED
// First check for general devices
when (action) {
ACTION_ACL_CONNECTED -> {
connectionStatus = PREFERENCE_VALUE_ALL
}
ACTION_ACL_DISCONNECTED,
ACTION_ACL_DISCONNECT_REQUESTED -> {
disconnectionStatus = PREFERENCE_VALUE_ALL
}
}
// Then check for A2DP devices
when (state) {
BluetoothA2dp.STATE_CONNECTED -> {
connectionStatus = PREFERENCE_VALUE_A2DP
}
BluetoothA2dp.STATE_DISCONNECTED -> {
disconnectionStatus = PREFERENCE_VALUE_A2DP
}
}
// Flags to store which action should be performed
var shouldResume = false
var shouldPause = false
// Now check the settings and set the appropriate flags
when (Settings.resumeOnBluetoothDevice) {
PREFERENCE_VALUE_ALL -> {
shouldResume = (connectionStatus != PREFERENCE_VALUE_DISABLED)
}
PREFERENCE_VALUE_A2DP -> {
shouldResume = (connectionStatus == PREFERENCE_VALUE_A2DP)
}
}
when (Settings.pauseOnBluetoothDevice) {
PREFERENCE_VALUE_ALL -> {
shouldPause = (disconnectionStatus != PREFERENCE_VALUE_DISABLED)
}
PREFERENCE_VALUE_A2DP -> {
shouldPause = (disconnectionStatus == PREFERENCE_VALUE_A2DP)
}
}
if (shouldResume) {
Timber.i("Connected to Bluetooth device $name; Resuming playback.")
context.sendBroadcast(
Intent(Constants.CMD_RESUME_OR_PLAY)
.setPackage(context.packageName)
)
}
if (shouldPause) {
Timber.i("Disconnected from Bluetooth device $name; Requesting pause.")
context.sendBroadcast(
Intent(Constants.CMD_PAUSE)
.setPackage(context.packageName)
)
}
}
}
private fun BluetoothDevice?.getNameSafely(): String? {
val logBluetoothName = Build.VERSION.SDK_INT >= Build.VERSION_CODES.S &&
(
ActivityCompat.checkSelfPermission(
UApp.applicationContext(), Manifest.permission.BLUETOOTH_CONNECT
) != PackageManager.PERMISSION_GRANTED
)
return if (logBluetoothName) this?.name else "Unknown"
}
private fun Intent.getBluetoothDevice(): BluetoothDevice? {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
getParcelableExtra(BluetoothDevice.EXTRA_DEVICE, BluetoothDevice::class.java)
} else {
getParcelableExtra(BluetoothDevice.EXTRA_DEVICE)
}
}

View File

@ -29,7 +29,8 @@ import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController import androidx.navigation.fragment.findNavController
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import java.util.Locale import java.util.Locale
import org.koin.java.KoinJavaComponent.inject import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
import org.moire.ultrasonic.NavigationGraphDirections import org.moire.ultrasonic.NavigationGraphDirections
import org.moire.ultrasonic.R import org.moire.ultrasonic.R
import org.moire.ultrasonic.api.subsonic.ApiNotSupportedException import org.moire.ultrasonic.api.subsonic.ApiNotSupportedException
@ -55,15 +56,13 @@ import org.moire.ultrasonic.util.Util.toast
* *
* TODO: This file has been converted from Java, but not modernized yet. * TODO: This file has been converted from Java, but not modernized yet.
*/ */
class PlaylistsFragment : Fragment() { class PlaylistsFragment : Fragment(), KoinComponent {
private var refreshPlaylistsListView: SwipeRefreshLayout? = null private var refreshPlaylistsListView: SwipeRefreshLayout? = null
private var playlistsListView: ListView? = null private var playlistsListView: ListView? = null
private var emptyTextView: View? = null private var emptyTextView: View? = null
private var playlistAdapter: ArrayAdapter<Playlist>? = null private var playlistAdapter: ArrayAdapter<Playlist>? = null
private val downloadHandler = inject<DownloadHandler>( private val downloadHandler by inject<DownloadHandler>()
DownloadHandler::class.java
)
private var cancellationToken: CancellationToken? = null private var cancellationToken: CancellationToken? = null
@ -148,7 +147,7 @@ class PlaylistsFragment : Fragment() {
val playlist = playlistsListView!!.getItemAtPosition(info.position) as Playlist val playlist = playlistsListView!!.getItemAtPosition(info.position) as Playlist
when (menuItem.itemId) { when (menuItem.itemId) {
R.id.playlist_menu_pin -> { R.id.playlist_menu_pin -> {
downloadHandler.value.justDownload( downloadHandler.justDownload(
DownloadAction.PIN, DownloadAction.PIN,
fragment = this, fragment = this,
id = playlist.id, id = playlist.id,
@ -158,7 +157,7 @@ class PlaylistsFragment : Fragment() {
) )
} }
R.id.playlist_menu_unpin -> { R.id.playlist_menu_unpin -> {
downloadHandler.value.justDownload( downloadHandler.justDownload(
DownloadAction.UNPIN, DownloadAction.UNPIN,
fragment = this, fragment = this,
id = playlist.id, id = playlist.id,
@ -168,7 +167,7 @@ class PlaylistsFragment : Fragment() {
) )
} }
R.id.playlist_menu_download -> { R.id.playlist_menu_download -> {
downloadHandler.value.justDownload( downloadHandler.justDownload(
DownloadAction.DOWNLOAD, DownloadAction.DOWNLOAD,
fragment = this, fragment = this,
id = playlist.id, id = playlist.id,

View File

@ -40,7 +40,7 @@ class TrackCollectionModel(application: Application) : GenericListModel(applicat
val service = MusicServiceFactory.getMusicService() val service = MusicServiceFactory.getMusicService()
val musicDirectory = service.getMusicDirectory(id, name, refresh) val musicDirectory = service.getMusicDirectory(id, name, refresh)
currentListIsSortable = true
updateList(musicDirectory) updateList(musicDirectory)
} }
} }
@ -51,7 +51,7 @@ class TrackCollectionModel(application: Application) : GenericListModel(applicat
val service = MusicServiceFactory.getMusicService() val service = MusicServiceFactory.getMusicService()
val musicDirectory: MusicDirectory = service.getAlbumAsDir(id, name, refresh) val musicDirectory: MusicDirectory = service.getAlbumAsDir(id, name, refresh)
currentListIsSortable = true
updateList(musicDirectory) updateList(musicDirectory)
} }
} }
@ -60,6 +60,7 @@ class TrackCollectionModel(application: Application) : GenericListModel(applicat
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
val service = MusicServiceFactory.getMusicService() val service = MusicServiceFactory.getMusicService()
val musicDirectory = service.getSongsByGenre(genre, count, offset) val musicDirectory = service.getSongsByGenre(genre, count, offset)
currentListIsSortable = false
updateList(musicDirectory, append) updateList(musicDirectory, append)
} }
} }
@ -76,7 +77,7 @@ class TrackCollectionModel(application: Application) : GenericListModel(applicat
} else { } else {
Util.getSongsFromSearchResult(service.getStarred()) Util.getSongsFromSearchResult(service.getStarred())
} }
currentListIsSortable = false
updateList(musicDirectory) updateList(musicDirectory)
} }
} }
@ -87,8 +88,8 @@ class TrackCollectionModel(application: Application) : GenericListModel(applicat
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
val service = MusicServiceFactory.getMusicService() val service = MusicServiceFactory.getMusicService()
val videos = service.getVideos(refresh) val videos = service.getVideos(refresh)
if (videos != null) { if (videos != null) {
currentListIsSortable = false
updateList(videos) updateList(videos)
} }
} }
@ -99,19 +100,16 @@ class TrackCollectionModel(application: Application) : GenericListModel(applicat
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
val service = MusicServiceFactory.getMusicService() val service = MusicServiceFactory.getMusicService()
val musicDirectory = service.getRandomSongs(size) val musicDirectory = service.getRandomSongs(size)
currentListIsSortable = false currentListIsSortable = false
updateList(musicDirectory, append) updateList(musicDirectory, append)
} }
} }
suspend fun getPlaylist(playlistId: String, playlistName: String) { suspend fun getPlaylist(playlistId: String, playlistName: String) {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
val service = MusicServiceFactory.getMusicService() val service = MusicServiceFactory.getMusicService()
val musicDirectory = service.getPlaylist(playlistId, playlistName) val musicDirectory = service.getPlaylist(playlistId, playlistName)
currentListIsSortable = false
updateList(musicDirectory) updateList(musicDirectory)
} }
} }
@ -121,8 +119,8 @@ class TrackCollectionModel(application: Application) : GenericListModel(applicat
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
val service = MusicServiceFactory.getMusicService() val service = MusicServiceFactory.getMusicService()
val musicDirectory = service.getPodcastEpisodes(podcastChannelId) val musicDirectory = service.getPodcastEpisodes(podcastChannelId)
if (musicDirectory != null) { if (musicDirectory != null) {
currentListIsSortable = false
updateList(musicDirectory) updateList(musicDirectory)
} }
} }
@ -144,7 +142,7 @@ class TrackCollectionModel(application: Application) : GenericListModel(applicat
break break
} }
} }
currentListIsSortable = false
updateList(musicDirectory) updateList(musicDirectory)
} }
} }
@ -153,7 +151,7 @@ class TrackCollectionModel(application: Application) : GenericListModel(applicat
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
val service = MusicServiceFactory.getMusicService() val service = MusicServiceFactory.getMusicService()
val musicDirectory = Util.getSongsFromBookmarks(service.getBookmarks()) val musicDirectory = Util.getSongsFromBookmarks(service.getBookmarks())
currentListIsSortable = false
updateList(musicDirectory) updateList(musicDirectory)
} }
} }