<commit_before>package info.nightscout.androidaps.plugins.PumpInsight.connector;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;

import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.plugins.PumpInsight.events.EventInsightPumpUpdateGui;
import info.nightscout.androidaps.plugins.PumpInsight.utils.Helpers;
import sugar.free.sightparser.handling.ServiceConnectionCallback;
import sugar.free.sightparser.handling.SightServiceConnector;
import sugar.free.sightparser.handling.StatusCallback;
import sugar.free.sightparser.pipeline.Status;

/**
 * Created by jamorham on 23/01/2018.
 *
 * Connects to SightRemote app service using SightParser library
 *
 * SightRemote and SightParser created by <NAME>
 *
 * Original proof of concept SightProxy by jamorham
 *
 */

public class Connector {

    private static final String TAG = "InsightConnector";
    private static final String COMPANION_APP_PACKAGE = "sugar.free.sightremote";
    private static final String HISTORY_IDENTIFIER = "sugar.free.sightremote.history";
    private static final String HISTORY_RECEIVER = "sugar.free.sightremote.HISTORY";
    private static volatile Connector instance;
    private volatile SightServiceConnector serviceConnector;
    private volatile Status lastStatus = null;
    private volatile long lastStatusTime = -1;
    private boolean companionAppInstalled = false;
    private int serviceReconnects = 0;

    private StatusCallback statusCallback = new StatusCallback() {
        @Override
        public void onStatusChange(Status status) {

            synchronized (this) {
                log("Status change: " + status);
                lastStatus = status;
                lastStatusTime = Helpers.tsl();
                switch (status) {
                    // TODO automated reactions to change in status
                }
                MainApp.bus().post(new EventInsightPumpUpdateGui());
            }
        }
    };

    private ServiceConnectionCallback connectionCallback = new ServiceConnectionCallback() {
        @Override
        public void onServiceConnected() {
            log("On service connected");
            serviceConnector.connect();
            statusCallback.onStatusChange(safeGetStatus());
        }

        @Override
        public void onServiceDisconnected() {
            log("Disconnected from service");
            if (Helpers.ratelimit("insight-automatic-reconnect", 30)) {
                log("Scheduling automatic service reconnection");
                Helpers.runOnUiThreadDelayed(new Runnable() {
                    @Override
                    public void run() {
                        init();
                    }
                }, 20000);
            }
        }
    };

    private BroadcastReceiver historyReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            log("Receiving history broadcast!");

            // TODO check action
            final Bundle bundle = intent.getBundleExtra(HISTORY_IDENTIFIER);
            if (bundle != null) {
                //HistoryArray history = (HistoryArray) bundle.getSerializable("history");
            } else {
                log("History bundle was null!");
            }
        }
    };


    private Connector() {
        registerHistoryReceiver();
    }

    public static Connector get() {
        if (instance == null) {
            init_instance();
        }
        return instance;
    }

    private synchronized static void init_instance() {
        if (instance == null) {
            instance = new Connector();
        }
    }

    private static boolean isCompanionAppInstalled() {
        return Helpers.checkPackageExists(MainApp.instance(), TAG, COMPANION_APP_PACKAGE);
    }

    public static void connectToPump() {
        log("Attempting to connect to pump");
        get().getServiceConnector().connect();
    }

    private static void log(String msg) {
        android.util.Log.e("INSIGHTPUMP", msg);
    }


    public synchronized void init() {
        log("Connector::init()");
        if (serviceConnector == null) {
            companionAppInstalled = isCompanionAppInstalled();
            if (companionAppInstalled) {
                serviceConnector = new SightServiceConnector(MainApp.instance());
                serviceConnector.addStatusCallback(statusCallback);
                serviceConnector.setConnectionCallback(connectionCallback);
                serviceConnector.connectToService();
                log("Trying to connect");
            } else {
                log("Not trying init due to missing companion app");
            }
        } else {
            if (!serviceConnector.isConnectedToService()) {
                if (serviceReconnects > 0) {
                    serviceConnector = null;
                    init();
                } else {
                    log("Trying to reconnect to service (" + serviceReconnects + ")");
                    serviceConnector.connectToService();
                    serviceReconnects++;
                }
            } else {
                serviceReconnects = 0; // everything ok
            }
        }
    }<commit_msg>Attempt to eliminate duplicate connectors<commit_after>package info.nightscout.androidaps.plugins.PumpInsight.connector;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;

import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.plugins.PumpInsight.events.EventInsightPumpUpdateGui;
import info.nightscout.androidaps.plugins.PumpInsight.utils.Helpers;
import sugar.free.sightparser.handling.ServiceConnectionCallback;
import sugar.free.sightparser.handling.SightServiceConnector;
import sugar.free.sightparser.handling.StatusCallback;
import sugar.free.sightparser.pipeline.Status;

/**
 * Created by jamorham on 23/01/2018.
 *
 * Connects to SightRemote app service using SightParser library
 *
 * SightRemote and SightParser created by <NAME>
 *
 * Original proof of concept SightProxy by jamorham
 *
 */

public class Connector {

    private static final String TAG = "InsightConnector";
    private static final String COMPANION_APP_PACKAGE = "sugar.free.sightremote";
    private static final String HISTORY_IDENTIFIER = "sugar.free.sightremote.history";
    private static final String HISTORY_RECEIVER = "sugar.free.sightremote.HISTORY";
    private static volatile Connector instance;
    private volatile SightServiceConnector serviceConnector;
    private volatile Status lastStatus = null;
    private volatile long lastStatusTime = -1;
    private boolean companionAppInstalled = false;
    private int serviceReconnects = 0;

    private StatusCallback statusCallback = new StatusCallback() {
        @Override
        public synchronized void onStatusChange(Status status) {

                log("Status change: " + status);
                lastStatus = status;
                lastStatusTime = Helpers.tsl();
                switch (status) {
                    // TODO automated reactions to change in status
                }
                MainApp.bus().post(new EventInsightPumpUpdateGui());
            }

    };

    private ServiceConnectionCallback connectionCallback = new ServiceConnectionCallback() {
        @Override
        public synchronized void onServiceConnected() {
            log("On service connected");
            try {
                serviceConnector.connect();
            } catch (NullPointerException e) {
                log("ERROR: null pointer when trying to connect to pump");
            }
            statusCallback.onStatusChange(safeGetStatus());
        }

        @Override
        public synchronized void onServiceDisconnected() {
            log("Disconnected from service");
            if (Helpers.ratelimit("insight-automatic-reconnect", 30)) {
                log("Scheduling automatic service reconnection");
                Helpers.runOnUiThreadDelayed(new Runnable() {
                    @Override
                    public void run() {
                        init();
                    }
                }, 20000);
            }
        }
    };

    private BroadcastReceiver historyReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            log("Receiving history broadcast!");

            // TODO check action
            final Bundle bundle = intent.getBundleExtra(HISTORY_IDENTIFIER);
            if (bundle != null) {
                //HistoryArray history = (HistoryArray) bundle.getSerializable("history");
            } else {
                log("History bundle was null!");
            }
        }
    };


    private Connector() {
        registerHistoryReceiver();
    }

    public static Connector get() {
        if (instance == null) {
            init_instance();
        }
        return instance;
    }

    private synchronized static void init_instance() {
        if (instance == null) {
            instance = new Connector();
        }
    }

    private static boolean isCompanionAppInstalled() {
        return Helpers.checkPackageExists(MainApp.instance(), TAG, COMPANION_APP_PACKAGE);
    }

    public static void connectToPump() {
        log("Attempting to connect to pump");
        get().getServiceConnector().connect();
    }

    static void log(String msg) {
        android.util.Log.e("INSIGHTPUMP", msg);
    }


    public synchronized void init() {
        log("Connector::init()");
        if (serviceConnector == null) {
            companionAppInstalled = isCompanionAppInstalled();
            if (companionAppInstalled) {
                serviceConnector = new SightServiceConnector(MainApp.instance());
                serviceConnector.removeStatusCallback(statusCallback);
                serviceConnector.addStatusCallback(statusCallback);
                serviceConnector.setConnectionCallback(connectionCallback);
                serviceConnector.connectToService();
                log("Trying to connect");
            } else {
                log("Not trying init due to missing companion app");
            }
        } else {
            if (!serviceConnector.isConnectedToService()) {
                if (serviceReconnects > 0) {
                    serviceConnector = null;
                    init();
                } else {
                    log("Trying to reconnect to service (" + serviceReconnects + ")");
                    serviceConnector.connectToService();
                    serviceReconnects++;
                }
            } else {
                serviceReconnects = 0; // everything ok
            }
        }
    }
{'fraction_non_alphanumeric': 0.08595253367543297, 'fraction_numerical': 0.0034820855859983508, 'mean_word_length': 2.179143606175357, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, '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': '3320773', 'n_tokens_mistral': 2728, 'n_tokens_neox': 2595, 'n_words': 634}