Improve dialogs. Add JSON serialization for actions and events.
This commit is contained in:
parent
0364d3c039
commit
04574d759f
|
@ -1,5 +1,9 @@
|
||||||
package info.nightscout.androidaps.plugins.general.automation;
|
package info.nightscout.androidaps.plugins.general.automation;
|
||||||
|
|
||||||
|
import org.json.JSONArray;
|
||||||
|
import org.json.JSONException;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
@ -27,4 +31,41 @@ public class AutomationEvent {
|
||||||
public String getTitle() {
|
public String getTitle() {
|
||||||
return title;
|
return title;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String toJSON() {
|
||||||
|
JSONObject o = new JSONObject();
|
||||||
|
try {
|
||||||
|
// title
|
||||||
|
o.put("title", title);
|
||||||
|
// trigger
|
||||||
|
o.put("trigger", trigger.toJSON());
|
||||||
|
// actions
|
||||||
|
JSONArray array = new JSONArray();
|
||||||
|
for (Action a : actions) {
|
||||||
|
array.put(a.toJSON());
|
||||||
|
}
|
||||||
|
o.put("actions", array);
|
||||||
|
} catch (JSONException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return o.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public AutomationEvent fromJSON(String data) {
|
||||||
|
try {
|
||||||
|
JSONObject d = new JSONObject(data);
|
||||||
|
// title
|
||||||
|
title = d.getString("title");
|
||||||
|
// trigger
|
||||||
|
trigger = Trigger.instantiate(d.getString("trigger"));
|
||||||
|
// actions
|
||||||
|
JSONArray array = d.getJSONArray("actions");
|
||||||
|
for (int i = 0; i < array.length(); i++) {
|
||||||
|
actions.add(Action.instantiate(array.getJSONObject(i)));
|
||||||
|
}
|
||||||
|
} catch (JSONException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,34 @@
|
||||||
package info.nightscout.androidaps.plugins.general.automation.actions;
|
package info.nightscout.androidaps.plugins.general.automation.actions;
|
||||||
|
|
||||||
|
import org.json.JSONException;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
import info.nightscout.androidaps.queue.Callback;
|
import info.nightscout.androidaps.queue.Callback;
|
||||||
|
|
||||||
public abstract class Action {
|
public abstract class Action {
|
||||||
|
|
||||||
abstract int friendlyName();
|
abstract int friendlyName();
|
||||||
|
|
||||||
abstract void doAction(Callback callback);
|
abstract void doAction(Callback callback);
|
||||||
|
|
||||||
|
public String toJSON() {
|
||||||
|
JSONObject o = new JSONObject();
|
||||||
|
try {
|
||||||
|
o.put("type", this.getClass().getName());
|
||||||
|
} catch (JSONException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return o.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Action instantiate(JSONObject object) {
|
||||||
|
try {
|
||||||
|
String type = object.getString("type");
|
||||||
|
Class clazz = Class.forName(type);
|
||||||
|
return (Action) clazz.newInstance();
|
||||||
|
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | JSONException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,7 +15,6 @@ import butterknife.OnClick;
|
||||||
import butterknife.Unbinder;
|
import butterknife.Unbinder;
|
||||||
import info.nightscout.androidaps.R;
|
import info.nightscout.androidaps.R;
|
||||||
import info.nightscout.androidaps.plugins.general.automation.AutomationEvent;
|
import info.nightscout.androidaps.plugins.general.automation.AutomationEvent;
|
||||||
import info.nightscout.androidaps.plugins.general.automation.AutomationFragment;
|
|
||||||
import info.nightscout.androidaps.plugins.general.automation.AutomationPlugin;
|
import info.nightscout.androidaps.plugins.general.automation.AutomationPlugin;
|
||||||
import info.nightscout.androidaps.plugins.general.automation.triggers.TriggerConnector;
|
import info.nightscout.androidaps.plugins.general.automation.triggers.TriggerConnector;
|
||||||
|
|
||||||
|
@ -30,7 +29,6 @@ public class EditEventDialog extends DialogFragment {
|
||||||
LinearLayout mLayoutTrigger;
|
LinearLayout mLayoutTrigger;
|
||||||
|
|
||||||
private Unbinder mUnbinder;
|
private Unbinder mUnbinder;
|
||||||
private AutomationFragment.TriggerListAdapter mTriggerListAdapter;
|
|
||||||
|
|
||||||
public static EditEventDialog newInstance(AutomationEvent event) {
|
public static EditEventDialog newInstance(AutomationEvent event) {
|
||||||
mEvent = event;
|
mEvent = event;
|
||||||
|
@ -47,12 +45,16 @@ public class EditEventDialog extends DialogFragment {
|
||||||
View view = inflater.inflate(R.layout.automation_dialog_event, container, false);
|
View view = inflater.inflate(R.layout.automation_dialog_event, container, false);
|
||||||
mUnbinder = ButterKnife.bind(this, view);
|
mUnbinder = ButterKnife.bind(this, view);
|
||||||
|
|
||||||
// initialization
|
// load data from bundle
|
||||||
TriggerConnector rootTrigger = new TriggerConnector(TriggerConnector.Type.OR);
|
if (savedInstanceState != null) {
|
||||||
mEvent.setTrigger(rootTrigger);
|
String eventData = savedInstanceState.getString("event");
|
||||||
|
if (eventData != null) mEvent.fromJSON(eventData);
|
||||||
|
} else {
|
||||||
|
mEvent.setTrigger(new TriggerConnector(TriggerConnector.Type.OR));
|
||||||
|
}
|
||||||
|
|
||||||
// display root trigger
|
// display root trigger
|
||||||
mLayoutTrigger.addView(rootTrigger.createView(getContext(), getFragmentManager()));
|
mLayoutTrigger.addView(mEvent.getTrigger().createView(getContext(), getFragmentManager()));
|
||||||
|
|
||||||
return view;
|
return view;
|
||||||
}
|
}
|
||||||
|
@ -82,5 +84,9 @@ public class EditEventDialog extends DialogFragment {
|
||||||
dismiss();
|
dismiss();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onSaveInstanceState(Bundle bundle) {
|
||||||
|
bundle.putString("event", mEvent.toJSON());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -87,9 +87,9 @@ public abstract class Trigger {
|
||||||
|
|
||||||
public abstract boolean shouldRun();
|
public abstract boolean shouldRun();
|
||||||
|
|
||||||
abstract String toJSON();
|
public abstract String toJSON();
|
||||||
|
|
||||||
abstract Trigger fromJSON(String data);
|
/*package*/ abstract Trigger fromJSON(String data);
|
||||||
|
|
||||||
public abstract int friendlyName();
|
public abstract int friendlyName();
|
||||||
|
|
||||||
|
@ -100,7 +100,16 @@ public abstract class Trigger {
|
||||||
|
|
||||||
public abstract Trigger duplicate();
|
public abstract Trigger duplicate();
|
||||||
|
|
||||||
static Trigger instantiate(JSONObject object) {
|
public static Trigger instantiate(String json) {
|
||||||
|
try {
|
||||||
|
return instantiate(new JSONObject(json));
|
||||||
|
} catch (JSONException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Trigger instantiate(JSONObject object) {
|
||||||
try {
|
try {
|
||||||
String type = object.getString("type");
|
String type = object.getString("type");
|
||||||
JSONObject data = object.getJSONObject("data");
|
JSONObject data = object.getJSONObject("data");
|
||||||
|
|
|
@ -71,7 +71,7 @@ public class TriggerBg extends Trigger {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
synchronized String toJSON() {
|
public synchronized String toJSON() {
|
||||||
JSONObject o = new JSONObject();
|
JSONObject o = new JSONObject();
|
||||||
try {
|
try {
|
||||||
o.put("type", TriggerBg.class.getName());
|
o.put("type", TriggerBg.class.getName());
|
||||||
|
|
|
@ -120,7 +120,7 @@ public class TriggerConnector extends Trigger {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
synchronized String toJSON() {
|
public synchronized String toJSON() {
|
||||||
JSONObject o = new JSONObject();
|
JSONObject o = new JSONObject();
|
||||||
try {
|
try {
|
||||||
o.put("type", TriggerConnector.class.getName());
|
o.put("type", TriggerConnector.class.getName());
|
||||||
|
|
|
@ -162,7 +162,7 @@ public class TriggerTime extends Trigger {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
String toJSON() {
|
public String toJSON() {
|
||||||
JSONObject object = new JSONObject();
|
JSONObject object = new JSONObject();
|
||||||
JSONObject data = new JSONObject();
|
JSONObject data = new JSONObject();
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -22,12 +22,37 @@
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:hint="Event Name" />
|
android:hint="Event Name" />
|
||||||
|
|
||||||
|
<View
|
||||||
|
android:layout_width="fill_parent"
|
||||||
|
android:layout_height="2dip"
|
||||||
|
android:layout_marginBottom="5dp"
|
||||||
|
android:layout_marginTop="5dp"
|
||||||
|
android:background="@color/listdelimiter" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="Triggers:"/>
|
||||||
|
|
||||||
<LinearLayout
|
<LinearLayout
|
||||||
android:id="@+id/layoutTrigger"
|
android:id="@+id/layoutTrigger"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
|
android:layout_marginVertical="10dp"
|
||||||
android:orientation="vertical" />
|
android:orientation="vertical" />
|
||||||
|
|
||||||
|
<View
|
||||||
|
android:layout_width="fill_parent"
|
||||||
|
android:layout_height="2dip"
|
||||||
|
android:layout_marginBottom="5dp"
|
||||||
|
android:layout_marginTop="5dp"
|
||||||
|
android:background="@color/listdelimiter" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="Actions:"/>
|
||||||
|
|
||||||
<include layout="@layout/mdtp_done_button" />
|
<include layout="@layout/mdtp_done_button" />
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
|
@ -16,10 +16,16 @@
|
||||||
android:orientation="vertical"
|
android:orientation="vertical"
|
||||||
android:padding="10dp">
|
android:padding="10dp">
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="Please choose a trigger type:"/>
|
||||||
|
|
||||||
<RadioGroup
|
<RadioGroup
|
||||||
android:id="@+id/radioGroup"
|
android:id="@+id/radioGroup"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent" />
|
android:layout_height="match_parent"
|
||||||
|
android:layout_marginVertical="5dp"/>
|
||||||
|
|
||||||
<include layout="@layout/mdtp_done_button" />
|
<include layout="@layout/mdtp_done_button" />
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue