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
|
|
|
|
*/
|
2013-04-06 21:47:24 +02:00
|
|
|
package com.thejoshwa.ultrasonic.androidapp.receiver;
|
2012-02-26 21:25:13 +01:00
|
|
|
|
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.util.Log;
|
2013-12-04 07:36:02 +01:00
|
|
|
|
2013-04-06 21:47:24 +02:00
|
|
|
import com.thejoshwa.ultrasonic.androidapp.service.DownloadServiceImpl;
|
|
|
|
import com.thejoshwa.ultrasonic.androidapp.util.Util;
|
2012-02-26 21:25:13 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Request media button focus when connected to Bluetooth A2DP.
|
2013-12-04 07:36:02 +01:00
|
|
|
*
|
2012-02-26 21:25:13 +01:00
|
|
|
* @author Sindre Mehus
|
|
|
|
*/
|
2013-12-04 07:36:02 +01:00
|
|
|
public class BluetoothIntentReceiver extends BroadcastReceiver
|
|
|
|
{
|
2012-02-26 21:25:13 +01:00
|
|
|
|
2013-05-31 10:02:25 +02:00
|
|
|
private static final String TAG = BluetoothIntentReceiver.class.getSimpleName();
|
|
|
|
|
|
|
|
@Override
|
2013-12-04 07:36:02 +01:00
|
|
|
public void onReceive(Context context, Intent intent)
|
|
|
|
{
|
2013-05-31 10:02:25 +02:00
|
|
|
int state = intent.getIntExtra("android.bluetooth.a2dp.extra.SINK_STATE", -1);
|
|
|
|
String action = intent.getAction();
|
|
|
|
|
|
|
|
Log.d(TAG, "Bluetooth Sink State: " + state);
|
|
|
|
Log.d(TAG, "Bluetooth Action: " + action);
|
|
|
|
|
2013-12-04 07:36:02 +01:00
|
|
|
boolean actionConnected = false;
|
|
|
|
boolean actionDisconnected = false;
|
2013-06-07 04:27:45 +02:00
|
|
|
|
2013-12-04 07:36:02 +01:00
|
|
|
if (action != null)
|
|
|
|
{
|
|
|
|
if (action.equals(android.bluetooth.BluetoothDevice.ACTION_ACL_CONNECTED))
|
|
|
|
{
|
|
|
|
actionConnected = true;
|
2013-06-07 04:27:45 +02:00
|
|
|
|
2013-12-04 07:36:02 +01:00
|
|
|
}
|
|
|
|
else if (action.equals(android.bluetooth.BluetoothDevice.ACTION_ACL_DISCONNECTED) || action.equals(android.bluetooth.BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED))
|
|
|
|
{
|
|
|
|
actionDisconnected = true;
|
|
|
|
}
|
|
|
|
}
|
2013-06-07 04:27:45 +02:00
|
|
|
|
|
|
|
boolean connected = state == android.bluetooth.BluetoothA2dp.STATE_CONNECTED || actionConnected;
|
|
|
|
boolean disconnected = state == android.bluetooth.BluetoothA2dp.STATE_DISCONNECTED || actionDisconnected;
|
2013-05-31 10:02:25 +02:00
|
|
|
|
2013-12-04 07:36:02 +01:00
|
|
|
if (connected)
|
|
|
|
{
|
2013-05-31 10:02:25 +02:00
|
|
|
Log.i(TAG, "Connected to Bluetooth A2DP, requesting media button focus.");
|
|
|
|
Util.registerMediaButtonEventReceiver(context);
|
2013-12-04 07:36:02 +01:00
|
|
|
}
|
|
|
|
else if (disconnected)
|
|
|
|
{
|
2013-05-31 10:02:25 +02:00
|
|
|
Log.i(TAG, "Disconnected from Bluetooth A2DP, requesting pause.");
|
|
|
|
context.sendBroadcast(new Intent(DownloadServiceImpl.CMD_PAUSE));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|