mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge.git
synced 2025-01-10 17:11:56 +01:00
Adjust service for CalendarReceiver for multidevice (#2765)
Co-authored-by: vanous <vanous@noreply.codeberg.org> Co-committed-by: vanous <vanous@noreply.codeberg.org>
This commit is contained in:
parent
2a0b7b64fb
commit
f589487588
@ -90,6 +90,10 @@ public class CalendarReceiver extends BroadcastReceiver {
|
|||||||
onReceive(GBApplication.getContext(), new Intent());
|
onReceive(GBApplication.getContext(), new Intent());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public GBDevice getGBDevice(){
|
||||||
|
return mGBDevice;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onReceive(Context context, Intent intent) {
|
public void onReceive(Context context, Intent intent) {
|
||||||
LOG.info("got calendar changed broadcast");
|
LOG.info("got calendar changed broadcast");
|
||||||
|
@ -316,7 +316,7 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
|
|||||||
private AutoConnectIntervalReceiver mAutoConnectInvervalReceiver= null;
|
private AutoConnectIntervalReceiver mAutoConnectInvervalReceiver= null;
|
||||||
|
|
||||||
private AlarmReceiver mAlarmReceiver = null;
|
private AlarmReceiver mAlarmReceiver = null;
|
||||||
private CalendarReceiver mCalendarReceiver = null;
|
private List<CalendarReceiver> mCalendarReceiver = new ArrayList<>();
|
||||||
private CMWeatherReceiver mCMWeatherReceiver = null;
|
private CMWeatherReceiver mCMWeatherReceiver = null;
|
||||||
private LineageOsWeatherReceiver mLineageOsWeatherReceiver = null;
|
private LineageOsWeatherReceiver mLineageOsWeatherReceiver = null;
|
||||||
private TinyWeatherForecastGermanyReceiver mTinyWeatherForecastGermanyReceiver = null;
|
private TinyWeatherForecastGermanyReceiver mTinyWeatherForecastGermanyReceiver = null;
|
||||||
@ -371,6 +371,7 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
|
|||||||
private void updateReceiversState(){
|
private void updateReceiversState(){
|
||||||
boolean enableReceivers = false;
|
boolean enableReceivers = false;
|
||||||
boolean anyDeviceInitialized = false;
|
boolean anyDeviceInitialized = false;
|
||||||
|
List <GBDevice> devicesWithCalendar = new ArrayList<>();
|
||||||
|
|
||||||
FeatureSet features = new FeatureSet();
|
FeatureSet features = new FeatureSet();
|
||||||
|
|
||||||
@ -386,10 +387,12 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
|
|||||||
DeviceCoordinator coordinator = struct.getCoordinator();
|
DeviceCoordinator coordinator = struct.getCoordinator();
|
||||||
if(coordinator != null){
|
if(coordinator != null){
|
||||||
features.logicalOr(coordinator);
|
features.logicalOr(coordinator);
|
||||||
|
if (coordinator.supportsCalendarEvents()){
|
||||||
|
devicesWithCalendar.add(struct.getDevice());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
setReceiversEnableState(enableReceivers, anyDeviceInitialized, features, devicesWithCalendar);
|
||||||
setReceiversEnableState(enableReceivers, anyDeviceInitialized, features);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -967,8 +970,17 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean deviceHasCalendarReceiverRegistered(GBDevice device){
|
||||||
|
for (CalendarReceiver receiver: mCalendarReceiver){
|
||||||
|
if(receiver.getGBDevice().equals(device)){
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
private void setReceiversEnableState(boolean enable, boolean initialized, FeatureSet features) {
|
|
||||||
|
private void setReceiversEnableState(boolean enable, boolean initialized, FeatureSet features, List <GBDevice> devicesWithCalendar) {
|
||||||
LOG.info("Setting broadcast receivers to: " + enable);
|
LOG.info("Setting broadcast receivers to: " + enable);
|
||||||
|
|
||||||
if(enable && features == null){
|
if(enable && features == null){
|
||||||
@ -976,14 +988,17 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (enable && initialized && features.supportsCalendarEvents()) {
|
if (enable && initialized && features.supportsCalendarEvents()) {
|
||||||
if (mCalendarReceiver == null) {
|
for (GBDevice deviceWithCalendar : devicesWithCalendar) {
|
||||||
if (!(GBApplication.isRunningMarshmallowOrLater() && ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CALENDAR) == PackageManager.PERMISSION_DENIED)) {
|
if (!deviceHasCalendarReceiverRegistered(deviceWithCalendar)) {
|
||||||
IntentFilter calendarIntentFilter = new IntentFilter();
|
if (!(GBApplication.isRunningMarshmallowOrLater() && ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CALENDAR) == PackageManager.PERMISSION_DENIED)) {
|
||||||
calendarIntentFilter.addAction("android.intent.action.PROVIDER_CHANGED");
|
IntentFilter calendarIntentFilter = new IntentFilter();
|
||||||
calendarIntentFilter.addDataScheme("content");
|
calendarIntentFilter.addAction("android.intent.action.PROVIDER_CHANGED");
|
||||||
calendarIntentFilter.addDataAuthority("com.android.calendar", null);
|
calendarIntentFilter.addDataScheme("content");
|
||||||
mCalendarReceiver = new CalendarReceiver(null);
|
calendarIntentFilter.addDataAuthority("com.android.calendar", null);
|
||||||
registerReceiver(mCalendarReceiver, calendarIntentFilter);
|
CalendarReceiver receiver = new CalendarReceiver(deviceWithCalendar);
|
||||||
|
registerReceiver(receiver, calendarIntentFilter);
|
||||||
|
mCalendarReceiver.add(receiver);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (mAlarmReceiver == null) {
|
if (mAlarmReceiver == null) {
|
||||||
@ -991,10 +1006,10 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
|
|||||||
registerReceiver(mAlarmReceiver, new IntentFilter("DAILY_ALARM"));
|
registerReceiver(mAlarmReceiver, new IntentFilter("DAILY_ALARM"));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (mCalendarReceiver != null) {
|
for (CalendarReceiver registeredReceiver: mCalendarReceiver){
|
||||||
unregisterReceiver(mCalendarReceiver);
|
unregisterReceiver(registeredReceiver);
|
||||||
mCalendarReceiver = null;
|
|
||||||
}
|
}
|
||||||
|
mCalendarReceiver.clear();
|
||||||
if (mAlarmReceiver != null) {
|
if (mAlarmReceiver != null) {
|
||||||
unregisterReceiver(mAlarmReceiver);
|
unregisterReceiver(mAlarmReceiver);
|
||||||
mAlarmReceiver = null;
|
mAlarmReceiver = null;
|
||||||
@ -1152,7 +1167,7 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
|
|||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
|
|
||||||
LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver);
|
LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver);
|
||||||
setReceiversEnableState(false, false, null); // disable BroadcastReceivers
|
setReceiversEnableState(false, false, null, null); // disable BroadcastReceivers
|
||||||
|
|
||||||
unregisterReceiver(mBlueToothConnectReceiver);
|
unregisterReceiver(mBlueToothConnectReceiver);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user