diff --git a/APED/app/src/main/java/com/example/aped/ui/plots/PlotFragment.java b/APED/app/src/main/java/com/example/aped/ui/plots/PlotFragment.java index b0b6a09..5288915 100644 --- a/APED/app/src/main/java/com/example/aped/ui/plots/PlotFragment.java +++ b/APED/app/src/main/java/com/example/aped/ui/plots/PlotFragment.java @@ -1,13 +1,15 @@ package com.example.aped.ui.plots; import android.net.sip.SipSession; +import android.content.DialogInterface; import android.os.Bundle; import androidx.fragment.app.Fragment; - import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.Button; +import android.widget.Toast; import com.example.aped.MainActivity; import com.example.aped.R; import com.example.aped.communication.Communicator; @@ -16,9 +18,9 @@ import com.jjoe64.graphview.GraphView; import com.jjoe64.graphview.series.DataPoint; import com.jjoe64.graphview.series.LineGraphSeries; +import com.google.android.material.dialog.MaterialAlertDialogBuilder; import org.json.JSONException; import org.json.JSONObject; - import java.util.ArrayList; import java.util.List; @@ -30,6 +32,8 @@ public class PlotFragment extends Fragment { private List bufferedDevices; /** Line Chart View that is displayed **/ private GraphView graphView; + /** List of all the devices with buffers. **/ + private List bufferedDevicesSelected; /** * paul @@ -51,12 +55,46 @@ public class PlotFragment extends Fragment { getBufferedValues(mainActivity.getDelivery(), mainActivity.getXml()); mainActivity.getConfigurationHandler().setCurrentView(R.id.nav_plots); + + Button buttonChoseDevice = view.findViewById(R.id.button_alert); + buttonChoseDevice.setOnClickListener(new View.OnClickListener() { + + @Override + public void onClick(final View v) { + bufferedDevicesSelected = new ArrayList<>(); + MaterialAlertDialogBuilder alertDialogBuilder = new MaterialAlertDialogBuilder(mainActivity); + alertDialogBuilder.setTitle("Choose your devices"); + alertDialogBuilder.setMultiChoiceItems(bufferedDevices.toArray(new String[bufferedDevices.size()]), null, new DialogInterface.OnMultiChoiceClickListener() { + @Override + public void onClick(final DialogInterface dialog, final int which, final boolean isChecked) { + if (isChecked) { + bufferedDevicesSelected.add(bufferedDevices.get(which)); + } else if (bufferedDevices.contains(bufferedDevices.get(which))) { + bufferedDevicesSelected.remove(bufferedDevices.get(which)); + } + } + }); + alertDialogBuilder.setPositiveButton("display", new DialogInterface.OnClickListener() { + @Override + public void onClick(final DialogInterface dialog, final int which) { + String data = ""; + for (String item:bufferedDevicesSelected) { + data = data + " " + item; + } + Toast.makeText(mainActivity, data, Toast.LENGTH_SHORT).show(); + } + }); + alertDialogBuilder.create(); + alertDialogBuilder.show(); + } + }); + return view; } - private List getBufferedDevices(IXML xml){ + private List getBufferedDevices(final IXML xml) { List returnList = new ArrayList<>(); - for (String deviceName : xml.getDeviceNames()){ + for (String deviceName : xml.getDeviceNames()) { if (xml.getBufferSize(deviceName) > 0) { returnList.add(deviceName); } @@ -64,17 +102,17 @@ public class PlotFragment extends Fragment { return returnList; } - private void getBufferedValues(Communicator communicator, IXML xml){ - for (String deviceName : this.bufferedDevices){ - communicator.readBuffer(deviceName,response -> { + private void getBufferedValues(final Communicator communicator, final IXML xml) { + for (String deviceName : this.bufferedDevices) { + communicator.readBuffer(deviceName, response -> { try { JSONObject buffer = response.getJSONObject("buffer"); - for(String pin : (List) (xml.getPort(deviceName)).get("pins")) { + for (String pin : (List) (xml.getPort(deviceName)).get("pins")) { String valueString = buffer.getString(pin); - valueString = valueString.substring(2,valueString.length()-2); + valueString = valueString.substring(2, valueString.length() - 2); String[] valueStringArray = valueString.split("', '"); int[] values = new int[valueString.length()]; - for (int i = 0; i < valueStringArray.length; i++){ + for (int i = 0; i < valueStringArray.length; i++) { values[i] = Integer.parseInt(valueStringArray[i]); } diff --git a/APED/app/src/main/res/layout/fragment_plot.xml b/APED/app/src/main/res/layout/fragment_plot.xml index 7e7f063..a984a1f 100644 --- a/APED/app/src/main/res/layout/fragment_plot.xml +++ b/APED/app/src/main/res/layout/fragment_plot.xml @@ -9,4 +9,11 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/graph"/> - \ No newline at end of file + android:layout_height="match_parent"/> + +