1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-09 03:37:03 +01:00

[Huawei] Change to use MediaManager

This commit is contained in:
Martin.JM 2024-05-09 19:57:59 +02:00
parent b909e123a4
commit 25b1f8f3fa
4 changed files with 48 additions and 11 deletions

View File

@ -22,6 +22,7 @@ import android.content.SharedPreferences;
import android.media.AudioManager;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.widget.Toast;
import org.slf4j.Logger;
@ -244,7 +245,9 @@ public class AsynchronousResponse {
GB.toast("Failed to send music status request", Toast.LENGTH_SHORT, GB.ERROR, e);
LOG.error("Failed to send music status request (1)", e);
}
// Send Music Info
// Update and send Music Info
this.support.refreshMediaManager();
this.support.sendSetMusic();
} else if (response.commandId == MusicControl.Control.id) {
if (!(response instanceof MusicControl.Control.Response))
@ -319,6 +322,13 @@ public class AsynchronousResponse {
GB.toast("Failed to send music status request", Toast.LENGTH_SHORT, GB.ERROR, e);
LOG.error("Failed to send music status request (2)", e);
}
// Delay so the media app has a moment to change state
new Handler(Looper.getMainLooper()).postDelayed(() -> {
// Update and send Music Info
this.support.refreshMediaManager();
this.support.sendSetMusic();
}, 100);
}
}
}

View File

@ -16,6 +16,8 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.service.devices.huawei;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.location.Location;
import android.net.Uri;
@ -25,6 +27,7 @@ import java.util.ArrayList;
import java.util.UUID;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.HuaweiConstants;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.CallSpec;
import nodomain.freeyourgadget.gadgetbridge.model.MusicSpec;
import nodomain.freeyourgadget.gadgetbridge.model.MusicStateSpec;
@ -46,6 +49,12 @@ public class HuaweiBRSupport extends AbstractBTBRDeviceSupport {
}
@Override
public void setContext(GBDevice gbDevice, BluetoothAdapter btAdapter, Context context) {
super.setContext(gbDevice, btAdapter, context);
supportProvider.setContext(context);
}
@Override
protected TransactionBuilder initializeDevice(TransactionBuilder builder) {
return supportProvider.initializeDevice(builder);

View File

@ -16,8 +16,10 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.service.devices.huawei;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.content.Context;
import android.location.Location;
import android.net.Uri;
@ -29,6 +31,7 @@ import java.util.ArrayList;
import java.util.UUID;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.HuaweiConstants;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.CallSpec;
import nodomain.freeyourgadget.gadgetbridge.model.MusicSpec;
import nodomain.freeyourgadget.gadgetbridge.model.MusicStateSpec;
@ -53,6 +56,12 @@ public class HuaweiLESupport extends AbstractBTLEDeviceSupport {
supportProvider = new HuaweiSupportProvider(this);
}
@Override
public void setContext(GBDevice gbDevice, BluetoothAdapter btAdapter, Context context) {
super.setContext(gbDevice, btAdapter, context);
supportProvider.setContext(context);
}
@Override
protected TransactionBuilder initializeDevice(TransactionBuilder builder) {
return supportProvider.initializeDevice(builder);

View File

@ -155,6 +155,7 @@ import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.FitnessData;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.requests.SetWorkModeRequest;
import nodomain.freeyourgadget.gadgetbridge.service.serial.GBDeviceProtocol;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
import nodomain.freeyourgadget.gadgetbridge.util.MediaManager;
import nodomain.freeyourgadget.gadgetbridge.util.StringUtils;
public class HuaweiSupportProvider {
@ -174,8 +175,7 @@ public class HuaweiSupportProvider {
protected String androidID;
protected short msgId = 0;
private MusicStateSpec musicStateSpec = null;
private MusicSpec musicSpec = null;
private MediaManager mediaManager = null;
private GpsAndTime.GpsParameters.Response gpsParametersResponse = null;
@ -205,6 +205,10 @@ public class HuaweiSupportProvider {
this.leSupport = support;
}
public void setContext(Context context) {
mediaManager = new MediaManager(context);
}
public boolean isBLE() {
return huaweiType == HuaweiDeviceType.AW || huaweiType == HuaweiDeviceType.BLE || huaweiType == HuaweiDeviceType.SMART;
}
@ -1333,26 +1337,31 @@ public class HuaweiSupportProvider {
}
public void onSetMusicState(MusicStateSpec stateSpec) {
this.musicStateSpec = stateSpec;
if (mediaManager.onSetMusicState(stateSpec))
sendSetMusic();
}
public void onSetMusicInfo(MusicSpec musicSpec) {
this.musicSpec = musicSpec;
if (mediaManager.onSetMusicInfo(musicSpec))
sendSetMusic();
}
public void onSetPhoneVolume() {
// TODO: check when implemented in GB
// We get the audio volume manually, so ignoring the argument
sendSetMusic();
}
public void refreshMediaManager() {
mediaManager.refresh();
}
public void sendSetMusic() {
// This often gets called twice in a row because of onSetMusicState and onSetMusicInfo
// Maybe we can consolidate that into just one request?
SetMusicRequest setMusicRequest = new SetMusicRequest(this, this.musicStateSpec, this.musicSpec);
SetMusicRequest setMusicRequest = new SetMusicRequest(
this,
mediaManager.getBufferMusicStateSpec(),
mediaManager.getBufferMusicSpec()
);
try {
setMusicRequest.doPerform();
} catch (IOException e) {