<commit_before>package org.fraunhofer.cese.funf_sensor;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.inject.Inject;

import org.fraunhofer.cese.funf_sensor.Probe.AccelerometerProbe;
import org.fraunhofer.cese.funf_sensor.Probe.AudioProbe;
import org.fraunhofer.cese.funf_sensor.Probe.BluetoothProbe;
import org.fraunhofer.cese.funf_sensor.Probe.CallStateProbe;
import org.fraunhofer.cese.funf_sensor.Probe.ForegroundProbe;
import org.fraunhofer.cese.funf_sensor.Probe.MyRunningApplicationsProbe;
import org.fraunhofer.cese.funf_sensor.Probe.PowerProbe;
import org.fraunhofer.cese.funf_sensor.Probe.SMSProbe;
import org.fraunhofer.cese.funf_sensor.Probe.StateProbe;
import org.fraunhofer.cese.funf_sensor.appengine.GoogleAppEnginePipeline;
import org.fraunhofer.cese.funf_sensor.cache.Cache;
import org.fraunhofer.cese.funf_sensor.cache.RemoteUploadResult;
import org.fraunhofer.cese.funf_sensor.cache.UploadStatusListener;

import java.text.DateFormat;
import java.util.Date;

import edu.mit.media.funf.FunfManager;
import edu.mit.media.funf.probe.builtin.ScreenProbe;
import edu.mit.media.funf.probe.builtin.SimpleLocationProbe;
import edu.mit.media.funf.probe.builtin.WifiProbe;
import roboguice.activity.RoboActivity;

public class MainActivity extends RoboActivity {
    private static final String TAG = "Fraunhofer." + MainActivity.class.getSimpleName();

    public static final String PIPELINE_NAME = "appengine";
    private FunfManager funfManager;

    @Inject
    private GoogleAppEnginePipeline pipeline;

    //probes
    private AccelerometerProbe accelerometerProbe;
    private ForegroundProbe foregroundProbe;
    private MyRunningApplicationsProbe myRunningApplicationsProbe;
    private ScreenProbe screenProbe;
    private SimpleLocationProbe locationProbe;
    private WifiProbe wifiProbe;

    private SMSProbe sMSProbe;
    private PowerProbe powerProbe;
    private StateProbe stateProbe;
    private CallStateProbe callStateProbe;
    private AudioProbe audioProbe;
    private BluetoothProbe bluetoothProbe;

    private Switch collectDataSwitch;
    private Button instantSendButton;
    private TextView dataCountView;
    private TextView uploadResultView;

    private UploadStatusListener uploadStatusListener;
    private AsyncTask<Void, Long, Void> cacheCountUpdater;


    private ServiceConnection funfManagerConn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            funfManager = ((FunfManager.LocalBinder) service).getManager();
            Gson gson = funfManager.getGson();

            accelerometerProbe = gson.fromJson(new JsonObject(), AccelerometerProbe.class);
            foregroundProbe = gson.fromJson(new JsonObject(), ForegroundProbe.class);
            locationProbe = gson.fromJson(getString(R.string.probe_location), SimpleLocationProbe.class);
            wifiProbe = gson.fromJson(new JsonObject(), WifiProbe.class);
            myRunningApplicationsProbe = gson.fromJson(new JsonObject(), MyRunningApplicationsProbe.class);
            screenProbe = gson.fromJson(new JsonObject(), ScreenProbe.class);
            sMSProbe = gson.fromJson(new JsonObject(), SMSProbe.class);
            powerProbe = gson.fromJson(new JsonObject(), PowerProbe.class);
            stateProbe = gson.fromJson(new JsonObject(), StateProbe.class);
            callStateProbe = gson.fromJson(new JsonObject(), CallStateProbe.class);
            audioProbe = gson.fromJson(new JsonObject(), AudioProbe.class);
            bluetoothProbe = gson.fromJson(new JsonObject(), BluetoothProbe.class);

            // Initialize the pipeline
            funfManager.registerPipeline(PIPELINE_NAME, pipeline);
            pipeline = (GoogleAppEnginePipeline) funfManager.getRegisteredPipeline(PIPELINE_NAME);

            Log.i(TAG, "Enabling pipeline: " + PIPELINE_NAME);
            funfManager.enablePipeline(PIPELINE_NAME);
            pipeline.setEnabled(true);
            registerListeners();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {


            if (funfManager != null && pipeline.isEnabled()) {
                Log.d(TAG, "Service disconnected. Disabling pipeline: " + PIPELINE_NAME);
                pipeline.setEnabled(false);
                funfManager.disablePipeline(PIPELINE_NAME);
                unregisterListeners();
            }
            funfManager = null;
        }
    };

    private void registerListeners() {
        accelerometerProbe.registerPassiveListener(pipeline);
        foregroundProbe.registerPassiveListener(pipeline);
        locationProbe.registerPassiveListener(pipeline);
        wifiProbe.registerPassiveListener(pipeline);
        myRunningApplicationsProbe.registerPassiveListener(pipeline);
        screenProbe.registerPassiveListener(pipeline);
        sMSProbe.registerPassiveListener(pipeline);
        powerProbe.registerPassiveListener(pipeline);
        stateProbe.registerPassiveListener(pipeline);
        callStateProbe.registerPassiveListener(pipeline);
        audioProbe.registerPassiveListener(pipeline);
        bluetoothProbe.registerListener(pipeline);
    }

    private void unregisterListeners() {

        accelerometerProbe.unregisterPassiveListener(pipeline);
        foregroundProbe.unregisterPassiveListener(pipeline);
        locationProbe.unregisterPassiveListener(pipeline);
        wifiProbe.unregisterPassiveListener(pipeline);
        myRunningApplicationsProbe.unregisterPassiveListener(pipeline);
        screenProbe.unregisterPassiveListener(pipeline);
        sMSProbe.unregisterPassiveListener(pipeline);
        powerProbe.unregisterPassiveListener(pipeline);
        stateProbe.unregisterPassiveListener(pipeline);
        callStateProbe.unregisterPassiveListener(pipeline);
        audioProbe.unregisterPassiveListener(pipeline);
        bluetoothProbe.unregisterListener(pipeline);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
        dataCountView = (TextView) findViewById(R.id.dataCountText);
        uploadResultView = (TextView) findViewById(R.id.uploadResult);
        collectDataSwitch = (Switch) findViewById(R.id.switch1);
        collectDataSwitch.setChecked(true);
        collectDataSwitch.setOnCheckedChangeListener(
                new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if (isChecked) {
                            Log.i(TAG, "Enabling pipeline: " + PIPELINE_NAME);
                            funfManager.enablePipeline(PIPELINE_NAME);
                            pipeline.setEnabled(true);
                            registerListeners();
                        } else {
                            Log.d(TAG, "Disabling pipeline: " + PIPELINE_NAME);
                            unregisterListeners();
                            pipeline.setEnabled(false);
                            funfManager.disablePipeline(PIPELINE_NAME);
                        }
                    }
                }
        );


        instantSendButton = (Button) findViewById(R.id.SendButton);
        instantSendButton.setOnClickListener(
                new View.OnClickListener() {
                    private final DateFormat df = DateFormat.getDateTimeInstance();

                    @Override
                    public void onClick(View view) {
                        String text = "Upload requested on " + df.format(new Date()) + "\n";
                        if (!pipeline.isEnabled()) {
                            text += "Error: Data system is disabled.";
                            uploadResultView.setText(text);
                            return;
                        }

                        int status = pipeline.requestUpload();
                        if (status == Cache.UPLOAD_READY)
                            text += "Upload started...";
                        else if (status == Cache.UPLOAD_ALREADY_IN_PROGRESS)
                            text += "Upload in progress...";
                        else {
                            String errorText = "";
                            if ((status & Cache.INTERNAL_ERROR) == Cache.INTERNAL_ERROR)
                                errorText += "\n- An internal error occurred and data could not be uploaded.";
                            if ((status & Cache.UPLOAD_INTERVAL_NOT_MET) == Cache.UPLOAD_INTERVAL_NOT_MET)
                                errorText += "\n- An upload was just requested; please wait a few seconds.";
                            if ((status & Cache.NO_INTERNET_CONNECTION) == Cache.NO_INTERNET_CONNECTION)
                                errorText += "\n- No internet connection detected.";
                            if ((status & Cache.DATABASE_LIMIT_NOT_MET) == Cache.DATABASE_LIMIT_NOT_MET)
                                errorText += "\n- No entries to upload";

                            if (!errorText.isEmpty()) {
                                text += "Error:" + errorText;<commit_msg>Upload now button is always enabled. Clsoes #49<commit_after>package org.fraunhofer.cese.funf_sensor;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.inject.Inject;

import org.fraunhofer.cese.funf_sensor.Probe.AccelerometerProbe;
import org.fraunhofer.cese.funf_sensor.Probe.AudioProbe;
import org.fraunhofer.cese.funf_sensor.Probe.BluetoothProbe;
import org.fraunhofer.cese.funf_sensor.Probe.CallStateProbe;
import org.fraunhofer.cese.funf_sensor.Probe.ForegroundProbe;
import org.fraunhofer.cese.funf_sensor.Probe.MyRunningApplicationsProbe;
import org.fraunhofer.cese.funf_sensor.Probe.PowerProbe;
import org.fraunhofer.cese.funf_sensor.Probe.SMSProbe;
import org.fraunhofer.cese.funf_sensor.Probe.StateProbe;
import org.fraunhofer.cese.funf_sensor.appengine.GoogleAppEnginePipeline;
import org.fraunhofer.cese.funf_sensor.cache.Cache;
import org.fraunhofer.cese.funf_sensor.cache.RemoteUploadResult;
import org.fraunhofer.cese.funf_sensor.cache.UploadStatusListener;

import java.text.DateFormat;
import java.util.Date;

import edu.mit.media.funf.FunfManager;
import edu.mit.media.funf.probe.builtin.ScreenProbe;
import edu.mit.media.funf.probe.builtin.SimpleLocationProbe;
import edu.mit.media.funf.probe.builtin.WifiProbe;
import roboguice.activity.RoboActivity;

public class MainActivity extends RoboActivity {
    private static final String TAG = "Fraunhofer." + MainActivity.class.getSimpleName();

    public static final String PIPELINE_NAME = "appengine";
    private FunfManager funfManager;

    @Inject
    private GoogleAppEnginePipeline pipeline;

    //probes
    private AccelerometerProbe accelerometerProbe;
    private ForegroundProbe foregroundProbe;
    private MyRunningApplicationsProbe myRunningApplicationsProbe;
    private ScreenProbe screenProbe;
    private SimpleLocationProbe locationProbe;
    private WifiProbe wifiProbe;

    private SMSProbe sMSProbe;
    private PowerProbe powerProbe;
    private StateProbe stateProbe;
    private CallStateProbe callStateProbe;
    private AudioProbe audioProbe;
    private BluetoothProbe bluetoothProbe;

    private Switch collectDataSwitch;
    private Button instantSendButton;
    private TextView dataCountView;
    private TextView uploadResultView;

    private UploadStatusListener uploadStatusListener;
    private AsyncTask<Void, Long, Void> cacheCountUpdater;


    private ServiceConnection funfManagerConn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            funfManager = ((FunfManager.LocalBinder) service).getManager();
            Gson gson = funfManager.getGson();

            accelerometerProbe = gson.fromJson(new JsonObject(), AccelerometerProbe.class);
            foregroundProbe = gson.fromJson(new JsonObject(), ForegroundProbe.class);
            locationProbe = gson.fromJson(getString(R.string.probe_location), SimpleLocationProbe.class);
            wifiProbe = gson.fromJson(new JsonObject(), WifiProbe.class);
            myRunningApplicationsProbe = gson.fromJson(new JsonObject(), MyRunningApplicationsProbe.class);
            screenProbe = gson.fromJson(new JsonObject(), ScreenProbe.class);
            sMSProbe = gson.fromJson(new JsonObject(), SMSProbe.class);
            powerProbe = gson.fromJson(new JsonObject(), PowerProbe.class);
            stateProbe = gson.fromJson(new JsonObject(), StateProbe.class);
            callStateProbe = gson.fromJson(new JsonObject(), CallStateProbe.class);
            audioProbe = gson.fromJson(new JsonObject(), AudioProbe.class);
            bluetoothProbe = gson.fromJson(new JsonObject(), BluetoothProbe.class);

            // Initialize the pipeline
            funfManager.registerPipeline(PIPELINE_NAME, pipeline);
            pipeline = (GoogleAppEnginePipeline) funfManager.getRegisteredPipeline(PIPELINE_NAME);

            Log.i(TAG, "Enabling pipeline: " + PIPELINE_NAME);
            funfManager.enablePipeline(PIPELINE_NAME);
            pipeline.setEnabled(true);
            registerListeners();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {


            if (funfManager != null && pipeline.isEnabled()) {
                Log.d(TAG, "Service disconnected. Disabling pipeline: " + PIPELINE_NAME);
                pipeline.setEnabled(false);
                funfManager.disablePipeline(PIPELINE_NAME);
                unregisterListeners();
            }
            funfManager = null;
        }
    };

    private void registerListeners() {
        accelerometerProbe.registerPassiveListener(pipeline);
        foregroundProbe.registerPassiveListener(pipeline);
        locationProbe.registerPassiveListener(pipeline);
        wifiProbe.registerPassiveListener(pipeline);
        myRunningApplicationsProbe.registerPassiveListener(pipeline);
        screenProbe.registerPassiveListener(pipeline);
        sMSProbe.registerPassiveListener(pipeline);
        powerProbe.registerPassiveListener(pipeline);
        stateProbe.registerPassiveListener(pipeline);
        callStateProbe.registerPassiveListener(pipeline);
        audioProbe.registerPassiveListener(pipeline);
        bluetoothProbe.registerListener(pipeline);
    }

    private void unregisterListeners() {

        accelerometerProbe.unregisterPassiveListener(pipeline);
        foregroundProbe.unregisterPassiveListener(pipeline);
        locationProbe.unregisterPassiveListener(pipeline);
        wifiProbe.unregisterPassiveListener(pipeline);
        myRunningApplicationsProbe.unregisterPassiveListener(pipeline);
        screenProbe.unregisterPassiveListener(pipeline);
        sMSProbe.unregisterPassiveListener(pipeline);
        powerProbe.unregisterPassiveListener(pipeline);
        stateProbe.unregisterPassiveListener(pipeline);
        callStateProbe.unregisterPassiveListener(pipeline);
        audioProbe.unregisterPassiveListener(pipeline);
        bluetoothProbe.unregisterListener(pipeline);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
        dataCountView = (TextView) findViewById(R.id.dataCountText);
        uploadResultView = (TextView) findViewById(R.id.uploadResult);
        collectDataSwitch = (Switch) findViewById(R.id.switch1);
        collectDataSwitch.setChecked(true);
        collectDataSwitch.setOnCheckedChangeListener(
                new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if (isChecked) {
                            Log.i(TAG, "Enabling pipeline: " + PIPELINE_NAME);
                            funfManager.enablePipeline(PIPELINE_NAME);
                            pipeline.setEnabled(true);
                            registerListeners();
                        } else {
                            Log.d(TAG, "Disabling pipeline: " + PIPELINE_NAME);
                            unregisterListeners();
                            pipeline.setEnabled(false);
                            funfManager.disablePipeline(PIPELINE_NAME);
                        }
                    }
                }
        );

        instantSendButton = (Button) findViewById(R.id.SendButton);
        instantSendButton.setOnClickListener(
                new View.OnClickListener() {
                    private final DateFormat df = DateFormat.getDateTimeInstance();

                    @Override
                    public void onClick(View view) {
                        String text = "Upload requested on " + df.format(new Date()) + "\n";

                        int status = pipeline.requestUpload();
                        if (status == Cache.UPLOAD_READY)
                            text += "Upload started...";
                        else if (status == Cache.UPLOAD_ALREADY_IN_PROGRESS)
                            text += "Upload in progress...";
                        else {
                            String errorText = "";
                            if ((status & Cache.INTERNAL_ERROR) == Cache.INTERNAL_ERROR)
                                errorText += "\n- An internal error occurred and data could not be uploaded.";
                            if ((status & Cache.UPLOAD_INTERVAL_NOT_MET) == Cache.UPLOAD_INTERVAL_NOT_MET)
                                errorText += "\n- An upload was just requested; please wait a few seconds.";
                            if ((status & Cache.NO_INTERNET_CONNECTION) == Cache.NO_INTERNET_CONNECTION)
                                errorText += "\n- No internet connection detected.";
                            if ((status & Cache.DATABASE_LIMIT_NOT_MET) == Cache.DATABASE_LIMIT_NOT_MET)
                                errorText += "\n- No entries to upload";

                            if (!errorText.isEmpty()) {
                                text += "Error:" + errorText;
{'fraction_non_alphanumeric': 0.08309380715965509, 'fraction_numerical': 0.00020904102430101908, 'mean_word_length': 2.6119290298225746, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '2370316', 'n_tokens_mistral': 5130, 'n_tokens_neox': 5077, 'n_words': 878}