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

use parcelable for passing alarms around

This commit is contained in:
Daniele Gobbetti 2015-06-25 14:34:21 +02:00 committed by Daniele Gobbetti
parent 764dd70e45
commit dc3ed1659c
9 changed files with 142 additions and 91 deletions

View File

@ -21,6 +21,7 @@ import android.widget.Toast;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.UUID;
import nodomain.freeyourgadget.gadgetbridge.GBDevice.State;
@ -267,7 +268,8 @@ public class BluetoothCommunicationService extends Service {
mStarted = true;
break;
case ACTION_SET_ALARMS:
mDeviceSupport.onSetAlarms();
ArrayList<GBAlarm> alarms = intent.getParcelableArrayListExtra("alarms");
mDeviceSupport.onSetAlarms(alarms);
break;
}

View File

@ -2,6 +2,7 @@ package nodomain.freeyourgadget.gadgetbridge;
import android.net.Uri;
import java.util.ArrayList;
import java.util.UUID;
public interface EventHandler {
@ -13,7 +14,7 @@ public interface EventHandler {
void onSetTime(long ts);
void onSetAlarms();
void onSetAlarms(ArrayList<GBAlarm> alarms);
void onSetCallState(String number, String name, GBCommand command);

View File

@ -1,13 +1,16 @@
package nodomain.freeyourgadget.gadgetbridge;
import android.content.SharedPreferences;
import android.os.Parcel;
import android.os.Parcelable;
import android.preference.PreferenceManager;
import java.util.Calendar;
import static nodomain.freeyourgadget.gadgetbridge.miband.MiBandConst.PREF_MIBAND_ALARM_PREFIX;
public class GBAlarm {
public class GBAlarm implements Parcelable {
private int index;
private boolean enabled;
private boolean smartWakeup;
@ -50,6 +53,41 @@ public class GBAlarm {
store();
}
private static GBAlarm readFromParcel(Parcel pc) {
int index = pc.readInt();
boolean enabled = Boolean.parseBoolean(pc.readString());
boolean smartWakeup = Boolean.parseBoolean(pc.readString());
int repetition = pc.readInt();
int hour = pc.readInt();
int minute = pc.readInt();
return new GBAlarm(index, enabled, smartWakeup, (byte)repetition, hour, minute);
}
@Override
public boolean equals(Object o) {
if (o instanceof GBAlarm) {
GBAlarm comp = (GBAlarm)o;
return comp.getIndex() == getIndex();
} else {
return false;
}
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.index);
dest.writeString(String.valueOf(this.enabled));
dest.writeString(String.valueOf(this.smartWakeup));
dest.writeInt(this.repetition);
dest.writeInt(this.hour);
dest.writeInt(this.minute);
}
public int getIndex() {
return this.index;
}
@ -135,4 +173,15 @@ public class GBAlarm {
String pref = PREF_MIBAND_ALARM_PREFIX +(this.index+1);
sharedPrefs.edit().putString(pref, this.toPreferences()).apply();
}
public static final Creator CREATOR = new Creator() {
public GBAlarm createFromParcel(Parcel in) {
return readFromParcel(in);
}
public GBAlarm[] newArray(int size) {
return new GBAlarm[size];
}
};
}

View File

@ -7,6 +7,7 @@ import android.net.Uri;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.UUID;
/**
@ -228,10 +229,10 @@ public class ServiceDeviceSupport implements DeviceSupport {
}
@Override
public void onSetAlarms() {
public void onSetAlarms(ArrayList<GBAlarm> alarms) {
if (checkBusy("set alarms")) {
return;
}
delegate.onSetAlarms();
delegate.onSetAlarms(alarms);
}
}

View File

@ -3,6 +3,7 @@ package nodomain.freeyourgadget.gadgetbridge.activities;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Parcelable;
import android.preference.PreferenceManager;
import android.text.format.DateFormat;
import android.view.View;
@ -42,14 +43,8 @@ public class AlarmDetails extends Activity {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm_details);
int index = getIntent().getExtras().getInt("alarm_index");
if (index <0 || index > 2) {
finish();
}else {
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
String pref = PREF_MIBAND_ALARM_PREFIX +(index+1);
alarm = new GBAlarm(sharedPrefs.getString(pref, ""));
//TODO: this is horrible and error-prone
Parcelable p = getIntent().getExtras().getParcelable("alarm");
alarm = (GBAlarm) p;
timePicker = (TimePicker) findViewById(R.id.alarm_time_picker);
ctvSmartWakeup = (CheckedTextView) findViewById(R.id.alarm_ctv_smart_wakeup);
@ -126,8 +121,6 @@ public class AlarmDetails extends Activity {
}
}
@Override
protected void onDestroy() {

View File

@ -26,7 +26,7 @@ public class ConfigureAlarms extends Activity {
ListView alarmListView;
private GBAlarmListAdapter mGBAlarmListAdapter;
final List<GBAlarm> alarmList = new ArrayList<>();
final ArrayList<GBAlarm> alarmList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -49,7 +49,9 @@ public class ConfigureAlarms extends Activity {
protected void onDestroy() {
super.onDestroy();
Intent startIntent = new Intent(ConfigureAlarms.this, BluetoothCommunicationService.class);
startIntent.putParcelableArrayListExtra("alarms", alarmList);
startIntent.setAction(BluetoothCommunicationService.ACTION_SET_ALARMS);
startService(startIntent);
}
}

View File

@ -74,7 +74,7 @@ public class GBAlarmListAdapter extends ArrayAdapter<GBAlarm> {
public void onClick(View v) {
Intent startIntent;
startIntent = new Intent(mContext, AlarmDetails.class);
startIntent.putExtra("alarm_index", alarm.getIndex());
startIntent.putExtra("alarm", alarm);
mContext.startActivity(startIntent);
}
});

View File

@ -12,6 +12,7 @@ import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.GregorianCalendar;
@ -328,14 +329,13 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
}
@Override
public void onSetAlarms() {
public void onSetAlarms(ArrayList<GBAlarm> alarms) {
try {
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getContext());
BluetoothGattCharacteristic characteristic = getCharacteristic(MiBandService.UUID_CHARACTERISTIC_CONTROL_POINT);
TransactionBuilder builder = performInitialized("Set alarm");
queueAlarm(new GBAlarm(sharedPrefs.getString(PREF_MIBAND_ALARM1, GBAlarm.DEFAULT_ALARM1)), builder, characteristic);
queueAlarm(new GBAlarm(sharedPrefs.getString(PREF_MIBAND_ALARM2, GBAlarm.DEFAULT_ALARM2)), builder, characteristic);
queueAlarm(new GBAlarm(sharedPrefs.getString(PREF_MIBAND_ALARM3, GBAlarm.DEFAULT_ALARM3)), builder, characteristic);
for (GBAlarm alarm : alarms) {
queueAlarm(alarm, builder, characteristic);
}
builder.queue(getQueue());
} catch (IOException ex) {
LOG.error("Unable to set alarms on MI device", ex);

View File

@ -2,7 +2,10 @@ package nodomain.freeyourgadget.gadgetbridge.pebble;
import android.net.Uri;
import java.util.ArrayList;
import nodomain.freeyourgadget.gadgetbridge.AbstractBTDeviceSupport;
import nodomain.freeyourgadget.gadgetbridge.GBAlarm;
import nodomain.freeyourgadget.gadgetbridge.GBDeviceIoThread;
import nodomain.freeyourgadget.gadgetbridge.protocol.GBDeviceProtocol;
@ -40,7 +43,7 @@ public class PebbleSupport extends AbstractBTDeviceSupport {
}
@Override
public void onSetAlarms() {
public void onSetAlarms(ArrayList<GBAlarm> alarms) {
//nothing to do ATM
}
}