<commit_before>package edu.strathmore.serc.sercopenenergymonitorv3;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ExecutionException;

public class MainActivity extends AppCompatActivity {

    //For the call to be made to the CMS API
    private static String rootLinkAddress = "https://serc.strathmore.edu";
    private static String apiKey = "36ec19e2a135f22b50883d555eea2114";

    // For the SwipeRefreshLayout used both in the onCreate and refresh method
    private SwipeRefreshLayout swipeRefreshLayout;

    // For the adapter used in the ListView of the Main Activity/Screen.
    // Needs to be global as it is used both in the onCreate and refresh method
    private RecordingStationAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // Log start of onCreate method
        Log.i("SERC Log", "OnCreate");

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        // Create an ArrayList of RecordingStation Objects with the variable name recordingStations
        //Full Array list from the CMS platform
        Log.i("SERC Log:", "Calling CMSApi");
        ArrayList<RecordingStation> recordingStations = getRecordingStationsList();

        // Sublist of recording stations as chosen in settings
        ArrayList<RecordingStation> recordingStationsForAdapter = getRecordingStationInSettings(recordingStations);


        // Setting the RecordingStationAdapter<RecordingStation> to the ListView to display
        adapter = new RecordingStationAdapter(this, recordingStationsForAdapter);
        ListView listView = (ListView) findViewById(R.id.polling_results_list_view);
        listView.setAdapter(adapter);

        /*
         * OnItemClickLister for each item in the ListView. When an item in the listview is clicked,
         * this sends an intent to open GraphActivity (while passing some information about the
         * object to GraphActivity within the intent)
         */
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // Intent to open GraphActivity
                Intent graphIntent = new Intent(MainActivity.this, GraphActivity.class);

                // Getting the station ID, name and tag of the Clicked item to be sent with the intent
                graphIntent.putExtra("Station_ID", adapter.getItem(position).getStationID());
                graphIntent.putExtra("Station_name", adapter.getItem(position).getStationName());
                graphIntent.putExtra("Station_tag", adapter.getItem(position).getStationTag());

                // Start GraphActivity
                startActivity(graphIntent);

            }
        });


        /**
         * This is the listener for when a user long clicks/presses on an item in the listview.
         * This opens a Dialog showing all the information stored for the that RecordingStation object
         */
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                AlertDialog.Builder  alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
                alertDialogBuilder.setTitle("Station Details");
                alertDialogBuilder.setIcon(R.mipmap.ic_launcher_serc);
                alertDialogBuilder.setPositiveButton("Ok", null);

                Date currentTime = new Date(adapter.getItem(position).getStationTime()*1000);
                SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy h:mm a");
                String stationTime = sdf.format(currentTime);
                CharSequence[] stationDetails = {
                        "Station ID: " + String.valueOf(adapter.getItem(position).getStationID()),
                        "Station Name: " + adapter.getItem(position).getStationName(),
                        "Station Tag: " + adapter.getItem(position).getStationTag(),
                        "Current Reading: " + String.valueOf(adapter.getItem(position).getStationValueReading()),
                        "Current Time: " + stationTime};

                alertDialogBuilder.setItems(stationDetails, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (which == 0){
                            Toast.makeText(getBaseContext(), "The ID sent from the EmonCMS platform for this particular station", Toast.LENGTH_SHORT);
                        }

                    }
                });

                AlertDialog alertDialog = alertDialogBuilder.create();
                alertDialog.show();

                /* This returns a boolean to indicate whether you have consumed the event and it
                 * should not be carried further. That is, return true to indicate that you have
                 * handled the event and it should stop here; return false if you have not handled
                 * it and/or the event should continue to any other on-click listeners.<commit_msg>Added Recording Station Details on long click<commit_after>package edu.strathmore.serc.sercopenenergymonitorv3;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ExecutionException;

public class MainActivity extends AppCompatActivity {

    //For the call to be made to the CMS API
    private static String rootLinkAddress = "https://serc.strathmore.edu";
    private static String apiKey = "36ec19e2a135f22b50883d555eea2114";

    // For the SwipeRefreshLayout used both in the onCreate and refresh method
    private SwipeRefreshLayout swipeRefreshLayout;

    // For the adapter used in the ListView of the Main Activity/Screen.
    // Needs to be global as it is used both in the onCreate and refresh method
    private RecordingStationAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // Log start of onCreate method
        Log.i("SERC Log", "OnCreate");

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        // Create an ArrayList of RecordingStation Objects with the variable name recordingStations
        //Full Array list from the CMS platform
        Log.i("SERC Log:", "Calling CMSApi");
        ArrayList<RecordingStation> recordingStations = getRecordingStationsList();

        // Sublist of recording stations as chosen in settings
        ArrayList<RecordingStation> recordingStationsForAdapter = getRecordingStationInSettings(recordingStations);


        // Setting the RecordingStationAdapter<RecordingStation> to the ListView to display
        adapter = new RecordingStationAdapter(this, recordingStationsForAdapter);
        ListView listView = (ListView) findViewById(R.id.polling_results_list_view);
        listView.setAdapter(adapter);

        /*
         * OnItemClickLister for each item in the ListView. When an item in the listview is clicked,
         * this sends an intent to open GraphActivity (while passing some information about the
         * object to GraphActivity within the intent)
         */
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // Intent to open GraphActivity
                Intent graphIntent = new Intent(MainActivity.this, GraphActivity.class);

                // Getting the station ID, name and tag of the Clicked item to be sent with the intent
                graphIntent.putExtra("Station_ID", adapter.getItem(position).getStationID());
                graphIntent.putExtra("Station_name", adapter.getItem(position).getStationName());
                graphIntent.putExtra("Station_tag", adapter.getItem(position).getStationTag());

                // Start GraphActivity
                startActivity(graphIntent);

            }
        });


        /**
         * This is the listener for when a user long clicks/presses on an item in the listview.
         * This opens a Dialog showing all the information stored for the that RecordingStation object
         */
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                AlertDialog.Builder  alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
                alertDialogBuilder.setTitle("Station Details");
                alertDialogBuilder.setIcon(R.mipmap.ic_launcher_serc);
                alertDialogBuilder.setPositiveButton("Ok", null);

                Date currentTime = new Date(adapter.getItem(position).getStationTime()*1000);
                SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy h:mm a");
                String stationTime = sdf.format(currentTime);
                CharSequence[] stationDetails = {
                        "Station ID: " + String.valueOf(adapter.getItem(position).getStationID()),
                        "Station Name: " + adapter.getItem(position).getStationName(),
                        "Station Tag: " + adapter.getItem(position).getStationTag(),
                        "Current Reading: " + String.valueOf(adapter.getItem(position).getStationValueReading()),
                        "Current Time: " + stationTime};

                alertDialogBuilder.setItems(stationDetails, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        

                    }
                });

                AlertDialog alertDialog = alertDialogBuilder.create();
                alertDialog.show();

                /* This returns a boolean to indicate whether you have consumed the event and it
                 * should not be carried further. That is, return true to indicate that you have
                 * handled the event and it should stop here; return false if you have not handled
                 * it and/or the event should continue to any other on-click listeners.
{'fraction_non_alphanumeric': 0.07699885076342144, 'fraction_numerical': 0.005171564603513381, 'mean_word_length': 2.7279681762545898, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1568688', 'n_tokens_mistral': 3042, 'n_tokens_neox': 2878, 'n_words': 879}