1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-28 04:46:51 +01:00

Bind sunrise/sunset alarm receiver to specific device support

Only register it for Pebble devices which enable the relative setting.

Also move the preference to device specific.
This commit is contained in:
Daniele Gobbetti 2024-08-22 14:35:20 +02:00 committed by José Rebelo
parent 795ffde0e1
commit 54cf421295
5 changed files with 32 additions and 27 deletions

View File

@ -22,7 +22,6 @@ import android.app.PendingIntent;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Build;
import net.e175.klaus.solarpositioning.DeltaT; import net.e175.klaus.solarpositioning.DeltaT;
import net.e175.klaus.solarpositioning.SPA; import net.e175.klaus.solarpositioning.SPA;
@ -61,10 +60,6 @@ public class AlarmReceiver extends BroadcastReceiver {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
if (!GBApplication.getPrefs().getBoolean("send_sunrise_sunset", false)) {
LOG.info("won't send sunrise and sunset events (disabled in preferences)");
return;
}
LOG.info("will resend sunrise and sunset events"); LOG.info("will resend sunrise and sunset events");
final GregorianCalendar dateTimeTomorrow = new GregorianCalendar(TimeZone.getTimeZone("UTC")); final GregorianCalendar dateTimeTomorrow = new GregorianCalendar(TimeZone.getTimeZone("UTC"));

View File

@ -22,8 +22,6 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. */ along with this program. If not, see <https://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.service; package nodomain.freeyourgadget.gadgetbridge.service;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.*;
import android.Manifest; import android.Manifest;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.app.ActivityManager; import android.app.ActivityManager;
@ -113,6 +111,8 @@ import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
import nodomain.freeyourgadget.gadgetbridge.util.language.LanguageUtils; import nodomain.freeyourgadget.gadgetbridge.util.language.LanguageUtils;
import nodomain.freeyourgadget.gadgetbridge.util.language.Transliterator; import nodomain.freeyourgadget.gadgetbridge.util.language.Transliterator;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.*;
public class DeviceCommunicationService extends Service implements SharedPreferences.OnSharedPreferenceChangeListener { public class DeviceCommunicationService extends Service implements SharedPreferences.OnSharedPreferenceChangeListener {
public static class DeviceStruct{ public static class DeviceStruct{
private GBDevice device; private GBDevice device;
@ -1310,19 +1310,11 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
} }
} }
} }
if (mAlarmReceiver == null) {
mAlarmReceiver = new AlarmReceiver();
ContextCompat.registerReceiver(this, mAlarmReceiver, new IntentFilter("DAILY_ALARM"), ContextCompat.RECEIVER_EXPORTED);
}
} else { } else {
for (CalendarReceiver registeredReceiver: mCalendarReceiver){ for (CalendarReceiver registeredReceiver: mCalendarReceiver){
unregisterReceiver(registeredReceiver); unregisterReceiver(registeredReceiver);
} }
mCalendarReceiver.clear(); mCalendarReceiver.clear();
if (mAlarmReceiver != null) {
unregisterReceiver(mAlarmReceiver);
mAlarmReceiver = null;
}
} }
if (enable) { if (enable) {

View File

@ -18,9 +18,12 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. */ along with this program. If not, see <https://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.service.devices.pebble; package nodomain.freeyourgadget.gadgetbridge.service.devices.pebble;
import android.content.IntentFilter;
import android.net.Uri; import android.net.Uri;
import android.util.Pair; import android.util.Pair;
import androidx.core.content.ContextCompat;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
@ -34,6 +37,7 @@ import java.util.UUID;
import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.activities.SettingsActivity; import nodomain.freeyourgadget.gadgetbridge.activities.SettingsActivity;
import nodomain.freeyourgadget.gadgetbridge.externalevents.AlarmReceiver;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.CalendarEventSpec; import nodomain.freeyourgadget.gadgetbridge.model.CalendarEventSpec;
import nodomain.freeyourgadget.gadgetbridge.model.CallSpec; import nodomain.freeyourgadget.gadgetbridge.model.CallSpec;
@ -47,10 +51,36 @@ import nodomain.freeyourgadget.gadgetbridge.service.serial.GBDeviceProtocol;
public class PebbleSupport extends AbstractSerialDeviceSupport { public class PebbleSupport extends AbstractSerialDeviceSupport {
private static final Logger LOG = LoggerFactory.getLogger(PebbleSupport.class); private static final Logger LOG = LoggerFactory.getLogger(PebbleSupport.class);
private AlarmReceiver mAlarmReceiver = null;
@Override
public void dispose() {
super.dispose();
unregisterSunriseSunsetAlarmReceiver();
}
private void registerSunriseSunsetAlarmReceiver() {
if (!getDevicePrefs().getBoolean("send_sunrise_sunset", false)) {
LOG.info("won't register sunrise and sunset receiver (disabled in preferences)");
return;
}
unregisterSunriseSunsetAlarmReceiver();
LOG.info("registering sunrise and sunset receiver");
this.mAlarmReceiver = new AlarmReceiver();
ContextCompat.registerReceiver(GBApplication.getContext(), mAlarmReceiver, new IntentFilter("DAILY_ALARM"), ContextCompat.RECEIVER_EXPORTED);
}
private void unregisterSunriseSunsetAlarmReceiver() {
if (mAlarmReceiver != null) {
GBApplication.getContext().unregisterReceiver(mAlarmReceiver);
mAlarmReceiver = null;
}
}
@Override @Override
public boolean connect() { public boolean connect() {
getDeviceIOThread().start(); getDeviceIOThread().start();
registerSunriseSunsetAlarmReceiver();
return true; return true;
} }

View File

@ -30,7 +30,6 @@
android:title="@string/pref_title_enable_outgoing_call" android:title="@string/pref_title_enable_outgoing_call"
app:iconSpaceReserved="false" /> app:iconSpaceReserved="false" />
</PreferenceCategory> </PreferenceCategory>
<!--
<PreferenceCategory <PreferenceCategory
android:title="@string/pref_header_pebble_timeline" android:title="@string/pref_header_pebble_timeline"
app:iconSpaceReserved="false"> app:iconSpaceReserved="false">
@ -41,7 +40,6 @@
android:title="@string/pref_title_sunrise_sunset" android:title="@string/pref_title_sunrise_sunset"
app:iconSpaceReserved="false" /> app:iconSpaceReserved="false" />
</PreferenceCategory> </PreferenceCategory>
-->
<PreferenceCategory <PreferenceCategory
android:title="@string/pref_header_activitytrackers" android:title="@string/pref_header_activitytrackers"
app:iconSpaceReserved="false"> app:iconSpaceReserved="false">

View File

@ -13,16 +13,6 @@
app:iconSpaceReserved="false" app:iconSpaceReserved="false"
app:useSimpleSummaryProvider="true" /> app:useSimpleSummaryProvider="true" />
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory
android:title="@string/pref_header_pebble_timeline"
app:iconSpaceReserved="false">
<SwitchPreferenceCompat
android:key="send_sunrise_sunset"
android:layout="@layout/preference_checkbox"
android:summary="@string/pref_summary_sunrise_sunset"
android:title="@string/pref_title_sunrise_sunset"
app:iconSpaceReserved="false" />
</PreferenceCategory>
<PreferenceCategory <PreferenceCategory
android:title="@string/pref_header_development" android:title="@string/pref_header_development"
app:iconSpaceReserved="false"> app:iconSpaceReserved="false">