1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-28 12:56:49 +01:00

Sleep Widget reacts to clicks now (on Android 8 at least), and...

sends the alarm directly to the wearable.
This commit is contained in:
cpfeiffer 2019-01-10 23:50:02 +01:00
parent b4bbf6186c
commit 51632d16a2
2 changed files with 40 additions and 16 deletions

View File

@ -27,11 +27,17 @@ import android.os.Build;
import android.widget.RemoteViews;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import nodomain.freeyourgadget.gadgetbridge.activities.ConfigureAlarms;
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceManager;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.ActivityUser;
import nodomain.freeyourgadget.gadgetbridge.model.Alarm;
import nodomain.freeyourgadget.gadgetbridge.util.AlarmUtils;
import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
/**
@ -54,7 +60,8 @@ public class SleepAlarmWidget extends AppWidgetProvider {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.sleep_alarm_widget);
// Add our own click intent
Intent intent = new Intent(ACTION);
Intent intent = new Intent(context, SleepAlarmWidget.class);
intent.setAction(ACTION);
PendingIntent clickPI = PendingIntent.getBroadcast(
context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.sleepalarmwidget_text, clickPI);
@ -89,24 +96,40 @@ public class SleepAlarmWidget extends AppWidgetProvider {
// current timestamp
GregorianCalendar calendar = new GregorianCalendar();
// add preferred sleep duration
calendar.add(Calendar.HOUR_OF_DAY, userSleepDuration);
// overwrite the first alarm and activate it
/*
GBAlarm alarm = GBAlarm.createSingleShot(0,0, true, calendar); // FIXME!!!!
alarm.store();
*/
if (GBApplication.isRunningLollipopOrLater()) {
setAlarmViaAlarmManager(context, calendar.getTimeInMillis());
if (userSleepDuration > 0) {
calendar.add(Calendar.HOUR_OF_DAY, userSleepDuration);
} else { // probably testing
calendar.add(Calendar.MINUTE, 1);
}
int hours = calendar.get(Calendar.HOUR_OF_DAY);
int minutes = calendar.get(Calendar.MINUTE);
// overwrite the first alarm and activate it, without
GB.toast(context,
String.format(context.getString(R.string.appwidget_alarms_set), hours, minutes),
Toast.LENGTH_SHORT, GB.INFO);
Context appContext = context.getApplicationContext();
if (appContext instanceof GBApplication) {
GBApplication gbApp = (GBApplication) appContext;
GBDevice selectedDevice = gbApp.getDeviceManager().getSelectedDevice();
if (selectedDevice == null || !selectedDevice.isInitialized()) {
GB.toast(context,
context.getString(R.string.appwidget_not_connected),
Toast.LENGTH_LONG, GB.WARN);
}
}
Alarm alarm = AlarmUtils.createSingleShot(0,true, calendar);
ArrayList<Alarm> alarms = new ArrayList<>(1);
alarms.add(alarm);
GBApplication.deviceService().onSetAlarms(alarms);
// if (GBApplication.isRunningLollipopOrLater()) {
// setAlarmViaAlarmManager(context, calendar.getTimeInMillis());
// }
// int hours = calendar.get(Calendar.HOUR_OF_DAY);
// int minutes = calendar.get(Calendar.MINUTE);
//
// GB.toast(context,
// String.format(context.getString(R.string.appwidget_alarms_set), hours, minutes),
// Toast.LENGTH_SHORT, GB.INFO);
}
}

View File

@ -671,4 +671,5 @@
<string name="pref_invalid_frequency_title">Invalid frequency</string>
<string name="pref_invalid_frequency_message">Please enter a frequency between 87.5 and 108.0</string>
<string name="language_and_region_prefs">Language and region settings</string>
<string name="appwidget_not_connected">Not connected, alarm not set.</string>
</resources>