1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-08-27 17:50:57 +02:00

Prevent some bluetooth events from starting Gadgetbridge

This commit is contained in:
José Rebelo 2024-06-15 23:26:58 +01:00
parent 69fae56a8c
commit 7cafbc2002
12 changed files with 69 additions and 67 deletions

View File

@ -72,7 +72,6 @@ import nodomain.freeyourgadget.gadgetbridge.entities.DaoMaster;
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
import nodomain.freeyourgadget.gadgetbridge.entities.Device;
import nodomain.freeyourgadget.gadgetbridge.externalevents.BluetoothStateChangeReceiver;
import nodomain.freeyourgadget.gadgetbridge.externalevents.TimeChangeReceiver;
import nodomain.freeyourgadget.gadgetbridge.externalevents.opentracks.OpenTracksContentObserver;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceService;
@ -128,8 +127,7 @@ public class GBApplication extends Application {
private static final int CURRENT_PREFS_VERSION = 30;
private static final LimitedQueue<Integer, String> mIDSenderLookup = new LimitedQueue<>(16);
private static Prefs prefs;
private static GBPrefs gbPrefs;
private static GBPrefs prefs;
private static LockHandler lockHandler;
/**
* Note: is null on Lollipop
@ -144,7 +142,7 @@ public class GBApplication extends Application {
private static GBApplication app;
private static Logging logging = new Logging() {
private static final Logging logging = new Logging() {
@Override
protected String createLogDirectory() throws IOException {
if (GBEnvironment.env().isLocalTest()) {
@ -212,8 +210,7 @@ public class GBApplication extends Application {
AndroidThreeTen.init(this);
sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
prefs = new Prefs(sharedPrefs);
gbPrefs = new GBPrefs(prefs);
prefs = new GBPrefs(sharedPrefs);
if (!GBEnvironment.isEnvironmentSetup()) {
GBEnvironment.setupEnvironment(GBEnvironment.createDeviceEnvironment());
@ -1588,14 +1585,10 @@ public class GBApplication extends Application {
return typedValue.data;
}
public static Prefs getPrefs() {
public static GBPrefs getPrefs() {
return prefs;
}
public static GBPrefs getGBPrefs() {
return gbPrefs;
}
public DeviceManager getDeviceManager() {
return deviceManager;
}

View File

@ -127,7 +127,7 @@ public class ExternalPebbleJSActivity extends AbstractGBActivity {
showConfig = extras.getBoolean(SHOW_CONFIG, false);
}
if (GBApplication.getGBPrefs().isBackgroundJsEnabled()) {
if (GBApplication.getPrefs().isBackgroundJsEnabled()) {
if (showConfig) {
Objects.requireNonNull(currentDevice, "Must provide a device when invoking this activity");
Objects.requireNonNull(currentUUID, "Must provide a uuid when invoking this activity");
@ -144,7 +144,7 @@ public class ExternalPebbleJSActivity extends AbstractGBActivity {
}
private void startBackgroundWebViewAndFinish() {
if (GBApplication.getGBPrefs().isBackgroundJsEnabled()) {
if (GBApplication.getPrefs().isBackgroundJsEnabled()) {
WebViewSingleton.ensureCreated(this);
} else {
LOG.warn("BGJs disabled, not starting webview");

View File

@ -82,7 +82,7 @@ public class AlarmReceiver extends BroadcastReceiver {
GBApplication.deviceService().onDeleteCalendarEvent(CalendarEventSpec.TYPE_SUNRISE, id_tomorrow);
GBApplication.deviceService().onDeleteCalendarEvent(CalendarEventSpec.TYPE_SUNSET, id_tomorrow);
GBPrefs gbPrefs = GBApplication.getGBPrefs();
GBPrefs gbPrefs = GBApplication.getPrefs();
float[] longlat = gbPrefs.getLongLat(context);
float longitude = longlat[0];
float latitude = longlat[1];

View File

@ -31,7 +31,7 @@ public class AutoStartReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (GBApplication.getGBPrefs().getAutoStart() &&
if (GBApplication.getPrefs().getAutoStart() &&
(Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction()) ||
Intent.ACTION_MY_PACKAGE_REPLACED.equals(intent.getAction())
)) {

View File

@ -28,22 +28,28 @@ import org.slf4j.LoggerFactory;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceManager;
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
import nodomain.freeyourgadget.gadgetbridge.service.DeviceCommunicationService;
import nodomain.freeyourgadget.gadgetbridge.util.GBPrefs;
public class BluetoothStateChangeReceiver extends BroadcastReceiver {
private static final Logger LOG = LoggerFactory.getLogger(BluetoothStateChangeReceiver.class);
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
final String action = intent.getAction();
if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
if (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1) == BluetoothAdapter.STATE_ON) {
Intent refreshIntent = new Intent(DeviceManager.ACTION_REFRESH_DEVICELIST);
final Intent refreshIntent = new Intent(DeviceManager.ACTION_REFRESH_DEVICELIST);
LocalBroadcastManager.getInstance(context).sendBroadcast(refreshIntent);
Prefs prefs = GBApplication.getPrefs();
final GBPrefs prefs = GBApplication.getPrefs();
if (!DeviceCommunicationService.isRunning(context) && !prefs.getAutoStart()) {
// Prevent starting the service if it isn't yet running
LOG.debug("DeviceCommunicationService not running, ignoring bluetooth on");
return;
}
if (!prefs.getBoolean("general_autoconnectonbluetooth", false)) {
return;
}
@ -51,6 +57,11 @@ public class BluetoothStateChangeReceiver extends BroadcastReceiver {
LOG.info("Bluetooth turned on (ACTION_STATE_CHANGED) => connecting...");
GBApplication.deviceService().connect();
} else if (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1) == BluetoothAdapter.STATE_OFF) {
if (!DeviceCommunicationService.isRunning(context)) {
// Prevent starting the service if it isn't yet running
LOG.debug("DeviceCommunicationService not running, ignoring bluetooth off");
return;
}
LOG.info("Bluetooth turned off => disconnecting...");
GBApplication.deviceService().disconnect();
}

View File

@ -645,19 +645,6 @@ public class NotificationListener extends NotificationListenerService {
}
}
private boolean isServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
if (manager == null) {
return false;
}
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (DeviceCommunicationService.class.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
private boolean handleMediaSessionNotification(final StatusBarNotification sbn) {
final MediaSession.Token token = sbn.getNotification().extras.getParcelable(Notification.EXTRA_MEDIA_SESSION);
return token != null && handleMediaSessionNotification(token);
@ -816,7 +803,7 @@ public class NotificationListener extends NotificationListenerService {
* broadcast receivers because it seems to invalidate the permissions that are
* necessary for NotificationListenerService
*/
if (!isServiceRunning()) {
if (!DeviceCommunicationService.isRunning(this)) {
LOG.trace("Service is not running, ignoring notification");
return true;
}

View File

@ -26,6 +26,7 @@ import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.*;
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.app.Service;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
@ -366,6 +367,19 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
DEVICE_SUPPORT_FACTORY = factory;
}
public static boolean isRunning(final Context context) {
final ActivityManager manager = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
if (manager == null) {
return false;
}
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (DeviceCommunicationService.class.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
public DeviceCommunicationService() {
}
@ -515,7 +529,7 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
if (hasPrefs()) {
getPrefs().getPreferences().registerOnSharedPreferenceChangeListener(this);
allowBluetoothIntentApi = getPrefs().getBoolean(GBPrefs.PREF_ALLOW_INTENT_API, false);
reconnectViaScan = getGBPrefs().getAutoReconnectByScan();
reconnectViaScan = getPrefs().getAutoReconnectByScan();
}
startForeground();
@ -536,7 +550,7 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
if(device.getState() != GBDevice.State.NOT_CONNECTED){
continue;
}
boolean shouldAutoConnect = getGBPrefs().getAutoReconnect(device);
boolean shouldAutoConnect = getPrefs().getAutoReconnect(device);
if(!shouldAutoConnect){
continue;
}
@ -614,7 +628,7 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
boolean autoReconnect = GBPrefs.AUTO_RECONNECT_DEFAULT;
if (prefs != null && prefs.getPreferences() != null) {
autoReconnect = getGBPrefs().getAutoReconnect(gbDevice);
autoReconnect = getPrefs().getAutoReconnect(gbDevice);
if (!fromExtra && !autoReconnect) {
continue;
}
@ -1535,7 +1549,7 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (GBPrefs.DEVICE_AUTO_RECONNECT.equals(key)) {
for(DeviceStruct deviceStruct : deviceStructs){
boolean autoReconnect = getGBPrefs().getAutoReconnect(deviceStruct.getDevice());
boolean autoReconnect = getPrefs().getAutoReconnect(deviceStruct.getDevice());
deviceStruct.getDeviceSupport().setAutoReconnect(autoReconnect);
}
}
@ -1552,14 +1566,10 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
return getPrefs().getPreferences() != null;
}
public Prefs getPrefs() {
public GBPrefs getPrefs() {
return GBApplication.getPrefs();
}
public GBPrefs getGBPrefs() {
return GBApplication.getGBPrefs();
}
public GBDevice[] getGBDevices() {
GBDevice[] devices = new GBDevice[deviceStructs.size()];
for(int i = 0; i < devices.length; i++){

View File

@ -3305,7 +3305,7 @@ public abstract class HuamiSupport extends AbstractBTLEDeviceSupport implements
LOG.error("Error sending wind/humidity", ex);
}
float[] longlat = GBApplication.getGBPrefs().getLongLat(getContext());
float[] longlat = GBApplication.getPrefs().getLongLat(getContext());
float longitude = longlat[0];
float latitude = longlat[1];
if (longitude != 0 && latitude != 0) {

View File

@ -209,7 +209,7 @@ class PebbleIoThread extends GBDeviceIoThread {
mOutStream = mBtSocket.getOutputStream();
}
}
if (GBApplication.getGBPrefs().isBackgroundJsEnabled()) {
if (GBApplication.getPrefs().isBackgroundJsEnabled()) {
Intent startIntent = new Intent(getContext(), ExternalPebbleJSActivity.class);
startIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startIntent.putExtra(ExternalPebbleJSActivity.START_BG_WEBVIEW, true);
@ -242,7 +242,7 @@ class PebbleIoThread extends GBDeviceIoThread {
public void run() {
mIsConnected = connect();
if (!mIsConnected) {
if (GBApplication.getGBPrefs().getAutoReconnect(getDevice()) && !mQuit) {
if (GBApplication.getPrefs().getAutoReconnect(getDevice()) && !mQuit) {
gbDevice.setState(GBDevice.State.WAITING_FOR_RECONNECT);
gbDevice.sendDeviceUpdateIntent(getContext());
}
@ -406,13 +406,13 @@ class PebbleIoThread extends GBDeviceIoThread {
enablePebbleKitSupport(false);
if (mQuit || !GBApplication.getGBPrefs().getAutoReconnect(getDevice())) {
if (mQuit || !GBApplication.getPrefs().getAutoReconnect(getDevice())) {
gbDevice.setState(GBDevice.State.NOT_CONNECTED);
} else {
gbDevice.setState(GBDevice.State.WAITING_FOR_RECONNECT);
}
if (GBApplication.getGBPrefs().isBackgroundJsEnabled()) {
if (GBApplication.getPrefs().isBackgroundJsEnabled()) {
WebViewSingleton.getInstance().disposeWebView();
}
@ -540,7 +540,7 @@ class PebbleIoThread extends GBDeviceIoThread {
break;
case START:
LOG.info("got GBDeviceEventAppManagement START event for uuid: " + appMgmt.uuid);
if (GBApplication.getGBPrefs().isBackgroundJsEnabled()) {
if (GBApplication.getPrefs().isBackgroundJsEnabled()) {
if (mPebbleProtocol.hasAppMessageHandler(appMgmt.uuid)) {
WebViewSingleton.getInstance().stopJavascriptInterface();
} else {
@ -563,7 +563,7 @@ class PebbleIoThread extends GBDeviceIoThread {
setInstallSlot(appInfoEvent.freeSlot);
return false;
} else if (deviceEvent instanceof GBDeviceEventAppMessage) {
if (GBApplication.getGBPrefs().isBackgroundJsEnabled()) {
if (GBApplication.getPrefs().isBackgroundJsEnabled()) {
sendAppMessageJS((GBDeviceEventAppMessage) deviceEvent);
}
if (mEnablePebblekit) {

View File

@ -423,7 +423,7 @@ public class PebbleProtocol extends GBDeviceProtocol {
super(device);
mAppMessageHandlers.put(UUID_MORPHEUZ, new AppMessageHandlerMorpheuz(UUID_MORPHEUZ, PebbleProtocol.this));
mAppMessageHandlers.put(UUID_MISFIT, new AppMessageHandlerMisfit(UUID_MISFIT, PebbleProtocol.this));
if (!GBApplication.getGBPrefs().isBackgroundJsEnabled()) {
if (!GBApplication.getPrefs().isBackgroundJsEnabled()) {
mAppMessageHandlers.put(UUID_PEBBLE_TIMESTYLE, new AppMessageHandlerTimeStylePebble(UUID_PEBBLE_TIMESTYLE, PebbleProtocol.this));
mAppMessageHandlers.put(UUID_PEBSTYLE, new AppMessageHandlerPebStyle(UUID_PEBSTYLE, PebbleProtocol.this));
mAppMessageHandlers.put(UUID_MARIOTIME, new AppMessageHandlerMarioTime(UUID_MARIOTIME, PebbleProtocol.this));

View File

@ -79,7 +79,7 @@ public class GBWebClient extends WebViewClient {
private WebResourceResponse mimicReply(Uri requestedUri) {
if (requestedUri.getHost() != null && (StringUtils.indexOfAny(requestedUri.getHost(), AllowedDomains) != -1)) {
if (GBApplication.getGBPrefs().isBackgroundJsEnabled() && WebViewSingleton.getInstance().internetHelperBound) {
if (GBApplication.getPrefs().isBackgroundJsEnabled() && WebViewSingleton.getInstance().internetHelperBound) {
LOG.debug("WEBVIEW forwarding request to the internet helper");
Bundle bundle = new Bundle();
bundle.putString("URL", requestedUri.toString());

View File

@ -37,7 +37,7 @@ import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
public class GBPrefs {
public class GBPrefs extends Prefs {
// Since this class must not log to slf4j, we use plain android.util.Log
private static final String TAG = "GBPrefs";
@ -72,10 +72,13 @@ public class GBPrefs {
public static final String LAST_DEVICE_ADDRESSES = "last_device_addresses";
public static final String RECONNECT_ONLY_TO_CONNECTED = "general_reconnectonlytoconnected";
private final Prefs mPrefs;
@Deprecated
public GBPrefs(Prefs prefs) {
mPrefs = prefs;
this(prefs.getPreferences());
}
public GBPrefs(final SharedPreferences sharedPrefs) {
super(sharedPrefs);
}
public boolean getAutoReconnect(GBDevice device) {
@ -84,23 +87,23 @@ public class GBPrefs {
}
public boolean getAutoReconnectByScan() {
return mPrefs.getBoolean(RECONNECT_SCAN_KEY, RECONNECT_SCAN_DEFAULT);
return getBoolean(RECONNECT_SCAN_KEY, RECONNECT_SCAN_DEFAULT);
}
public boolean getAutoStart() {
return mPrefs.getBoolean(AUTO_START, AUTO_START_DEFAULT);
return getBoolean(AUTO_START, AUTO_START_DEFAULT);
}
public boolean isBackgroundJsEnabled() {
return mPrefs.getBoolean(BG_JS_ENABLED, BG_JS_ENABLED_DEFAULT);
return getBoolean(BG_JS_ENABLED, BG_JS_ENABLED_DEFAULT);
}
public String getUserName() {
return mPrefs.getString(USER_NAME, USER_NAME_DEFAULT);
return getString(USER_NAME, USER_NAME_DEFAULT);
}
public Date getUserBirthday() {
String date = mPrefs.getString(USER_BIRTHDAY, null);
String date = getString(USER_BIRTHDAY, null);
if (date == null) {
return null;
}
@ -117,7 +120,7 @@ public class GBPrefs {
}
public String getTimeFormat() {
String timeFormat = mPrefs.getString(DeviceSettingsPreferenceConst.PREF_TIMEFORMAT, DeviceSettingsPreferenceConst.PREF_TIMEFORMAT_AUTO);
String timeFormat = getString(DeviceSettingsPreferenceConst.PREF_TIMEFORMAT, DeviceSettingsPreferenceConst.PREF_TIMEFORMAT_AUTO);
if (DeviceSettingsPreferenceConst.PREF_TIMEFORMAT_AUTO.equals(timeFormat)) {
if (DateFormat.is24HourFormat(GBApplication.getContext())) {
timeFormat = DeviceSettingsPreferenceConst.PREF_TIMEFORMAT_24H;
@ -130,14 +133,12 @@ public class GBPrefs {
}
public float[] getLongLat(Context context) {
Prefs prefs = GBApplication.getPrefs();
float latitude = prefs.getFloat("location_latitude", 0);
float longitude = prefs.getFloat("location_longitude", 0);
float latitude = getFloat("location_latitude", 0);
float longitude = getFloat("location_longitude", 0);
Log.i(TAG, "got longitude/latitude from preferences: " + latitude + "/" + longitude);
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
prefs.getBoolean("use_updated_location_if_available", false)) {
getBoolean("use_updated_location_if_available", false)) {
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, false);