<commit_before>package edu.umt.csci427.canary;
import android.app.Fragment;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.media.AudioManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import java.util.List;

public class MainActivity extends ActionBarActivity implements
        MonitorFragment.OnMonitorFragmentInteractionListener,
        AddMonitorFragment.AddMonitorListener {

    AlertService mService;
    boolean mBound = false;
    //Will create the monitors
    private OpenICEAbstractFactory factory = null;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (savedInstanceState == null) {
            Intent intent = new Intent(this, OpenICEService.class);
            startService(intent);

            Intent startServiceIntent = new Intent(this, AlertService.class);
            startService(startServiceIntent);

            ViewManager.attachMainActivity(this);
        }

    }

    @Override
    protected void onStart() {
        super.onStart();
        // Bind to LocalService
        Intent intent = new Intent(this, AlertService.class);
        getApplicationContext().bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
    }

    /** Defines callbacks for service binding, passed to bindService() */
    private ServiceConnection mConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName className,
                                       IBinder service) {
            // We've bound to LocalService, cast the IBinder and get LocalService instance
            AlertService.LocalBinder binder = (AlertService.LocalBinder) service;
            mService = binder.getService();
            mBound = true;
            Log.v("ZZZ", "Service bound");
        }

        @Override
        public void onServiceDisconnected(ComponentName arg0) {
            mBound = false;
        }
    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        Intent i = new Intent(this, LineService.class);
        AudioManager am= (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE);

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
            }
        else if (id == R.id.add_monitor)
        {
            DialogFragment addMonitorFrag = new AddMonitorFragment();
            addMonitorFrag.show(getSupportFragmentManager(), "monitors");

            return true;
        }
        else if (id == R.id.action_line_start) {
            //check for a connected headset/adapter
            Boolean tStat = am.isWiredHeadsetOn();
            if (tStat == true){
                startService(i);
            }
            else{
                Toast.makeText(getBaseContext(), "Error: Connect Adapter!", Toast.LENGTH_LONG).show();
            }
            return true;
        }
        else if (id == R.id.action_line_stop) {
            stopService(i);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onFragmentInteraction()
    {

    }

    @Override
    public void launchThresholdOnClick()
    {
        onFragmentInteraction();
    }

    @Override
    public AlertService getAlertService() {
        return mService;
    }


    //@Override
    public void onFragmentInteraction(Uri uri) {}


    @Override
    public void onMonitorListClick(DialogFragment dialog, int which) {

        //Retrieve selection array
        String[] monitorSelection = getResources().getStringArray(R.array.monitor_list);
        //Create factory
        factory = OpenICEAbstractFactory.GetSimulatedFactory(monitorSelection[which]);

        if(factory != null){
            //Create monitor object
            Monitor myMonitor = factory.PackageOpenICESimulatedData(monitorSelection[which]);

            //Add to View Manager.
            ViewManager.addMonitorToScreen(MonitorFragment.newInstance(myMonitor));

            //Prompt user to set thresholds
            ThresholdFragment thresholdsFrag = myMonitor.getThresholdFragment();
            thresholdsFrag.show(getFragmentManager(), "ThresholdsFragment");

        }
        else{
            ///show toast saying they cant add more.
            Toast.makeText(this, monitorSelection[which] + " Factory not found.",
                    Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onDestroy(){
        stopService(new Intent(this, OpenICEService.class));
        super.onDestroy();
    }

    /*****************************************
     * When a fragment is attached to the activity
     * add its tag to the list this allows us to
     * remove it later if necessary
     ****************************************/
    @Override
    public void onAttachFragment(Fragment fragment){
        //Instantiate the list if it needs to be.
     /*   if(attachedFragmentsList == null){
            attachedFragmentsList = new ArrayList<>();
        }
            //Check to make sure the tag is not null or whitespace.
        if(fragment != null && !fragment.getTag().isEmpty() && fragment.getTag() != null){
            //Add this tag to the attachments list so we can find it later and remove it
            //in the on pause, and potentially re add it onResume.
            attachedFragmentsList.add(fragment.getTag());
        }*/
        super.onAttachFragment(fragment);
    }

    /********************************************
    Removes all of the fragments from the activity
    when the onPause is called in the life cycle
    Allows the screen to rotate without errors.<commit_msg>Override the onBackPressed() Method in the main activity<commit_after>package edu.umt.csci427.canary;
import android.app.Fragment;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.media.AudioManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import java.util.List;

public class MainActivity extends ActionBarActivity implements
        MonitorFragment.OnMonitorFragmentInteractionListener,
        AddMonitorFragment.AddMonitorListener {

    AlertService mService;
    boolean mBound = false;
    //Will create the monitors
    private OpenICEAbstractFactory factory = null;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (savedInstanceState == null) {
            Intent intent = new Intent(this, OpenICEService.class);
            startService(intent);

            Intent startServiceIntent = new Intent(this, AlertService.class);
            startService(startServiceIntent);

            ViewManager.attachMainActivity(this);
        }

    }

    @Override
    protected void onStart() {
        super.onStart();
        // Bind to LocalService
        Intent intent = new Intent(this, AlertService.class);
        getApplicationContext().bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
    }

    /** Defines callbacks for service binding, passed to bindService() */
    private ServiceConnection mConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName className,
                                       IBinder service) {
            // We've bound to LocalService, cast the IBinder and get LocalService instance
            AlertService.LocalBinder binder = (AlertService.LocalBinder) service;
            mService = binder.getService();
            mBound = true;
            Log.v("ZZZ", "Service bound");
        }

        @Override
        public void onServiceDisconnected(ComponentName arg0) {
            mBound = false;
        }
    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        Intent i = new Intent(this, LineService.class);
        AudioManager am= (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE);

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
            }
        else if (id == R.id.add_monitor)
        {
            DialogFragment addMonitorFrag = new AddMonitorFragment();
            addMonitorFrag.show(getSupportFragmentManager(), "monitors");

            return true;
        }
        else if (id == R.id.action_line_start) {
            //check for a connected headset/adapter
            Boolean tStat = am.isWiredHeadsetOn();
            if (tStat == true){
                startService(i);
            }
            else{
                Toast.makeText(getBaseContext(), "Error: Connect Adapter!", Toast.LENGTH_LONG).show();
            }
            return true;
        }
        else if (id == R.id.action_line_stop) {
            stopService(i);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onFragmentInteraction()
    {

    }

    @Override
    public void launchThresholdOnClick()
    {
        onFragmentInteraction();
    }

    @Override
    public AlertService getAlertService() {
        return mService;
    }




    @Override
    public void onMonitorListClick(DialogFragment dialog, int which) {

        //Retrieve selection array
        String[] monitorSelection = getResources().getStringArray(R.array.monitor_list);
        //Create factory
        factory = OpenICEAbstractFactory.GetSimulatedFactory(monitorSelection[which]);

        if(factory != null){
            //Create monitor object
            Monitor myMonitor = factory.PackageOpenICESimulatedData(monitorSelection[which]);

            //Add to View Manager.
            ViewManager.addMonitorToScreen(MonitorFragment.newInstance(myMonitor));

            //Prompt user to set thresholds
            ThresholdFragment thresholdsFrag = myMonitor.getThresholdFragment();
            thresholdsFrag.show(getFragmentManager(), "ThresholdsFragment");

        }
        else{
            ///show toast saying they cant add more.
            Toast.makeText(this, monitorSelection[which] + " Factory not found.",
                    Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * Overridden methods necessary.
     */
    @Override
    public void onDestroy(){
        //Stop the OpenICE service.
        stopService(new Intent(this, OpenICEService.class));
        super.onDestroy();
    }

    //Overriding this method keeps the back button from being pressed and executing.
    @Override
    public void onBackPressed(){}

    /*****************************************
     * When a fragment is attached to the activity
     * add its tag to the list this allows us to
     * remove it later if necessary
     ****************************************/
    @Override
    public void onAttachFragment(Fragment fragment){
        //Instantiate the list if it needs to be.
     /*   if(attachedFragmentsList == null){
            attachedFragmentsList = new ArrayList<>();
        }
            //Check to make sure the tag is not null or whitespace.
        if(fragment != null && !fragment.getTag().isEmpty() && fragment.getTag() != null){
            //Add this tag to the attachments list so we can find it later and remove it
            //in the on pause, and potentially re add it onResume.
            attachedFragmentsList.add(fragment.getTag());
        }*/
        super.onAttachFragment(fragment);
    }

    /********************************************
    Removes all of the fragments from the activity
    when the onPause is called in the life cycle
    Allows the screen to rotate without errors.
{'fraction_non_alphanumeric': 0.10160708921598077, 'fraction_numerical': 0.0009011715229798738, 'mean_word_length': 2.430448222565688, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5393320', 'n_tokens_mistral': 3391, 'n_tokens_neox': 3258, 'n_words': 928}