2012-02-26 21:25:13 +01:00
|
|
|
/*
|
|
|
|
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
|
|
|
|
*/
|
2015-07-26 18:15:07 +02:00
|
|
|
package org.moire.ultrasonic.receiver;
|
2012-02-26 21:25:13 +01:00
|
|
|
|
2021-12-20 20:20:08 +01:00
|
|
|
import android.annotation.SuppressLint;
|
2014-02-10 07:42:52 +01:00
|
|
|
import android.bluetooth.BluetoothDevice;
|
2020-09-28 17:12:36 +02:00
|
|
|
import android.bluetooth.BluetoothProfile;
|
2012-02-26 21:25:13 +01:00
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
2013-12-04 07:36:02 +01:00
|
|
|
|
2020-06-22 18:35:58 +02:00
|
|
|
import org.moire.ultrasonic.util.Constants;
|
2021-09-24 18:20:53 +02:00
|
|
|
import org.moire.ultrasonic.util.Settings;
|
2021-12-20 20:20:08 +01:00
|
|
|
|
|
|
|
import timber.log.Timber;
|
2012-02-26 21:25:13 +01:00
|
|
|
|
|
|
|
/**
|
2021-05-04 16:29:27 +02:00
|
|
|
* Resume or pause playback on Bluetooth A2DP connect/disconnect.
|
2013-12-04 07:36:02 +01:00
|
|
|
*
|
2012-02-26 21:25:13 +01:00
|
|
|
* @author Sindre Mehus
|
|
|
|
*/
|
2021-12-20 20:20:08 +01:00
|
|
|
@SuppressLint("MissingPermission")
|
2013-12-04 07:36:02 +01:00
|
|
|
public class BluetoothIntentReceiver extends BroadcastReceiver
|
|
|
|
{
|
2013-05-31 10:02:25 +02:00
|
|
|
@Override
|
2013-12-04 07:36:02 +01:00
|
|
|
public void onReceive(Context context, Intent intent)
|
|
|
|
{
|
2020-09-28 17:12:36 +02:00
|
|
|
int state = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, -1);
|
2014-02-10 07:42:52 +01:00
|
|
|
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
|
2013-05-31 10:02:25 +02:00
|
|
|
String action = intent.getAction();
|
2020-09-25 19:03:53 +02:00
|
|
|
String name = device != null ? device.getName() : "Unknown";
|
|
|
|
String address = device != null ? device.getAddress() : "Unknown";
|
2013-05-31 10:02:25 +02:00
|
|
|
|
2020-09-30 14:47:59 +02:00
|
|
|
Timber.d("A2DP State: %d; Action: %s; Device: %s; Address: %s", state, action, name, address);
|
2013-05-31 10:02:25 +02:00
|
|
|
|
2020-09-28 17:12:36 +02:00
|
|
|
boolean actionBluetoothDeviceConnected = false;
|
|
|
|
boolean actionBluetoothDeviceDisconnected = false;
|
|
|
|
boolean actionA2dpConnected = false;
|
|
|
|
boolean actionA2dpDisconnected = false;
|
2013-06-07 04:27:45 +02:00
|
|
|
|
2014-02-10 07:42:52 +01:00
|
|
|
if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action))
|
2013-12-04 07:36:02 +01:00
|
|
|
{
|
2020-09-28 17:12:36 +02:00
|
|
|
actionBluetoothDeviceConnected = true;
|
2014-02-10 07:42:52 +01:00
|
|
|
}
|
|
|
|
else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action) || BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED.equals(action))
|
|
|
|
{
|
2020-09-28 17:12:36 +02:00
|
|
|
actionBluetoothDeviceDisconnected = true;
|
2013-12-04 07:36:02 +01:00
|
|
|
}
|
2013-06-07 04:27:45 +02:00
|
|
|
|
2020-09-28 17:12:36 +02:00
|
|
|
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;
|
|
|
|
|
2021-09-24 18:20:53 +02:00
|
|
|
switch (Settings.getResumeOnBluetoothDevice())
|
2020-09-28 17:12:36 +02:00
|
|
|
{
|
|
|
|
case Constants.PREFERENCE_VALUE_ALL: resume = actionA2dpConnected || actionBluetoothDeviceConnected;
|
|
|
|
break;
|
|
|
|
case Constants.PREFERENCE_VALUE_A2DP: resume = actionA2dpConnected;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-09-24 18:20:53 +02:00
|
|
|
switch (Settings.getPauseOnBluetoothDevice())
|
2020-09-28 17:12:36 +02:00
|
|
|
{
|
|
|
|
case Constants.PREFERENCE_VALUE_ALL: pause = actionA2dpDisconnected || actionBluetoothDeviceDisconnected;
|
|
|
|
break;
|
|
|
|
case Constants.PREFERENCE_VALUE_A2DP: pause = actionA2dpDisconnected;
|
|
|
|
break;
|
|
|
|
}
|
2013-05-31 10:02:25 +02:00
|
|
|
|
2020-09-28 17:12:36 +02:00
|
|
|
if (resume)
|
|
|
|
{
|
2020-09-30 14:47:59 +02:00
|
|
|
Timber.i("Connected to Bluetooth device %s address %s, resuming playback.", name, address);
|
2020-09-29 11:09:24 +02:00
|
|
|
context.sendBroadcast(new Intent(Constants.CMD_RESUME_OR_PLAY).setPackage(context.getPackageName()));
|
2020-09-28 17:12:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pause)
|
2013-12-04 07:36:02 +01:00
|
|
|
{
|
2020-09-30 14:47:59 +02:00
|
|
|
Timber.i("Disconnected from Bluetooth device %s address %s, requesting pause.", name, address);
|
2020-09-29 11:09:24 +02:00
|
|
|
context.sendBroadcast(new Intent(Constants.CMD_PAUSE).setPackage(context.getPackageName()));
|
2013-05-31 10:02:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|