<commit_before>package com.aware.plugin.geo_collector;

import android.content.ContentValues;
import android.content.Intent;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.os.HandlerThread;
import android.util.Log;

import com.aware.Aware;
import com.aware.Aware_Preferences;
import com.aware.plugin.geo_collector.GeoCollector_Provider.GeoCollectorTermData;
import com.aware.utils.Aware_Plugin;

import de.unipassau.mics.contextopheles.base.ContextophelesConstants;
import de.unipassau.mics.contextopheles.utils.CommonSettings;

/**
 * Main Plugin for the GEO Collector
 *
 * @author <NAME>
 * @email: <EMAIL>
 */

public class Plugin extends Aware_Plugin {

    private static final String TAG = ContextophelesConstants.TAG_GEO_COLLECTOR + " Plugin";
    public static final String ACTION_AWARE_GEOCOLLECTOR = "ACTION_AWARE_GEOCOLLECTOR";

    public static final String EXTRA_TERMCONTENT = "termcontent";
    public static String lastTermContent = "";

    public static Uri geonameResolverContentUri;
    private static GeonameResolverObserver geonameResolverObs = null;

    public static Uri termCollectorGeoTermContentUri;
    private static TermCollectorGeoTermObserver termCollectorGeoTermObs = null;

    /**
     * Thread manager
     */
    private static HandlerThread threads = null;

    @Override
    public void onCreate() {
        Log.d(TAG, "Plugin Created");
        super.onCreate();

        // Share the context back to the framework and other applications
        CONTEXT_PRODUCER = new Aware_Plugin.ContextProducer() {
            @Override
            public void onContext() {
                Intent notification = new Intent(ACTION_AWARE_GEOCOLLECTOR);
                notification
                        .putExtra(Plugin.EXTRA_TERMCONTENT, lastTermContent);
                sendBroadcast(notification);
            }
        };

        DATABASE_TABLES = GeoCollector_Provider.DATABASE_TABLES;
        TABLES_FIELDS = GeoCollector_Provider.TABLES_FIELDS;
        CONTEXT_URIS = new Uri[]{GeoCollectorTermData.CONTENT_URI};

        threads = new HandlerThread(TAG);
        threads.start();

        // Set the observers, that run in independent threads, for
        // responsiveness

        geonameResolverContentUri = ContextophelesConstants.GEONAME_RESOLVER_CONTENT_URI;
        geonameResolverObs = new GeonameResolverObserver(new Handler(
                threads.getLooper()));
        getContentResolver().registerContentObserver(
                geonameResolverContentUri, true, geonameResolverObs);
        Log.d(TAG, "geonameResolverObs registered");

        termCollectorGeoTermContentUri = ContextophelesConstants.TERM_COLLECTOR_GEODATA_CONTENT_URI;
        termCollectorGeoTermObs = new TermCollectorGeoTermObserver(new Handler(
                threads.getLooper()));
        getContentResolver().registerContentObserver(
                termCollectorGeoTermContentUri, true, termCollectorGeoTermObs);
        Log.d(TAG, "termCollectorGeoTermObs registered");


        Log.d(TAG, "Plugin Started");
    }

    @Override
    public void onDestroy() {

        Log.d(TAG, "Plugin is destroyed");

        super.onDestroy();

        getContentResolver().unregisterContentObserver(geonameResolverObs);
        getContentResolver().unregisterContentObserver(termCollectorGeoTermObs);

    }


    public class TermCollectorGeoTermObserver extends ContentObserver {
        public TermCollectorGeoTermObserver(Handler handler) {
            super(handler);
        }

        @Override
        public void onChange(boolean selfChange) {
            super.onChange(selfChange);

            Log.wtf(TAG, "@onChange (TermCollectorGeoTermObserver)");


            // set cursor to first item
            Cursor cursor = getContentResolver().query(
                    termCollectorGeoTermContentUri, null, null, null,
                    ContextophelesConstants.TERM_COLLECTOR_FIELD_TIMESTAMP + " DESC LIMIT 1");

            if (cursor != null && cursor.moveToFirst()) {

                saveGeoData(cursor.getLong(cursor.getColumnIndex(ContextophelesConstants.TERM_COLLECTOR_FIELD_TIMESTAMP)),
                        termCollectorGeoTermContentUri.toString(), cursor.getString(cursor
                        .getColumnIndex(ContextophelesConstants.TERM_COLLECTOR_GEODATA_FIELD_TERM_CONTENT)));
            }

            if (cursor != null && !cursor.isClosed()) {
                cursor.close();
            }
        }

    }

    public class GeonameResolverObserver extends ContentObserver {
        public GeonameResolverObserver(Handler handler) {
            super(handler);
        }

        @Override
        public void onChange(boolean selfChange) {
            super.onChange(selfChange);

            Log.wtf(TAG, "@onChange (GeonameResolverObserver)");

            // run only, if use of location is allowed
            if  (CommonSettings.getQueryUseOfLocation(getContentResolver())){
            // set cursor to first item
            Cursor cursor = getContentResolver().query(
                    geonameResolverContentUri, null, null, null,
                    ContextophelesConstants.GEONAME_RESOLVER_FIELD_TIMESTAMP + " DESC LIMIT 1");
            if (cursor != null && cursor.moveToFirst()) {

                saveGeoData(cursor.getLong(cursor.getColumnIndex(ContextophelesConstants.GEONAME_RESOLVER_FIELD_TIMESTAMP)),
                        geonameResolverContentUri.toString(), cursor.getString(cursor
                        .getColumnIndex(ContextophelesConstants.GEONAME_RESOLVER_FIELD_NAME)));
            }

            if (cursor != null && !cursor.isClosed()) {
                cursor.close();
            }
            }
        }

    }

    private void saveGeoData(long timestamp, String source, String content) {
        ContentValues rowData = new ContentValues();
        rowData.put(GeoCollectorTermData.DEVICE_ID, Aware.getSetting(
                getContentResolver(), Aware_Preferences.DEVICE_ID));
        rowData.put(GeoCollectorTermData.TIMESTAMP, timestamp);
        rowData.put(GeoCollectorTermData.TERM_SOURCE, source);
        rowData.put(GeoCollectorTermData.TERM_CONTENT, content);

        Log.d(TAG, "Saving " + rowData.toString());
        getContentResolver().insert(GeoCollectorTermData.CONTENT_URI, rowData);
    }

}
<commit_msg>More Logging<commit_after>package com.aware.plugin.geo_collector;

import android.content.ContentValues;
import android.content.Intent;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.os.HandlerThread;
import android.util.Log;

import com.aware.Aware;
import com.aware.Aware_Preferences;
import com.aware.plugin.geo_collector.GeoCollector_Provider.GeoCollectorTermData;
import com.aware.utils.Aware_Plugin;

import de.unipassau.mics.contextopheles.base.ContextophelesConstants;
import de.unipassau.mics.contextopheles.utils.CommonSettings;

/**
 * Main Plugin for the GEO Collector
 *
 * @author <NAME>
 * @email: <EMAIL>
 */

public class Plugin extends Aware_Plugin {

    private static final String TAG = ContextophelesConstants.TAG_GEO_COLLECTOR + " Plugin";
    public static final String ACTION_AWARE_GEOCOLLECTOR = "ACTION_AWARE_GEOCOLLECTOR";

    public static final String EXTRA_TERMCONTENT = "termcontent";
    public static String lastTermContent = "";

    public static Uri geonameResolverContentUri;
    private static GeonameResolverObserver geonameResolverObs = null;

    public static Uri termCollectorGeoTermContentUri;
    private static TermCollectorGeoTermObserver termCollectorGeoTermObs = null;

    /**
     * Thread manager
     */
    private static HandlerThread threads = null;

    @Override
    public void onCreate() {
        Log.d(TAG, "Plugin Created");
        super.onCreate();

        // Share the context back to the framework and other applications
        CONTEXT_PRODUCER = new Aware_Plugin.ContextProducer() {
            @Override
            public void onContext() {
                Intent notification = new Intent(ACTION_AWARE_GEOCOLLECTOR);
                notification
                        .putExtra(Plugin.EXTRA_TERMCONTENT, lastTermContent);
                sendBroadcast(notification);
            }
        };

        DATABASE_TABLES = GeoCollector_Provider.DATABASE_TABLES;
        TABLES_FIELDS = GeoCollector_Provider.TABLES_FIELDS;
        CONTEXT_URIS = new Uri[]{GeoCollectorTermData.CONTENT_URI};

        threads = new HandlerThread(TAG);
        threads.start();

        // Set the observers, that run in independent threads, for
        // responsiveness

        geonameResolverContentUri = ContextophelesConstants.GEONAME_RESOLVER_CONTENT_URI;
        geonameResolverObs = new GeonameResolverObserver(new Handler(
                threads.getLooper()));
        getContentResolver().registerContentObserver(
                geonameResolverContentUri, true, geonameResolverObs);
        Log.d(TAG, "geonameResolverObs registered");

        termCollectorGeoTermContentUri = ContextophelesConstants.TERM_COLLECTOR_GEODATA_CONTENT_URI;
        termCollectorGeoTermObs = new TermCollectorGeoTermObserver(new Handler(
                threads.getLooper()));
        getContentResolver().registerContentObserver(
                termCollectorGeoTermContentUri, true, termCollectorGeoTermObs);
        Log.d(TAG, "termCollectorGeoTermObs registered");


        Log.d(TAG, "Plugin Started");
    }

    @Override
    public void onDestroy() {

        Log.d(TAG, "Plugin is destroyed");

        super.onDestroy();

        getContentResolver().unregisterContentObserver(geonameResolverObs);
        getContentResolver().unregisterContentObserver(termCollectorGeoTermObs);

    }


    public class TermCollectorGeoTermObserver extends ContentObserver {
        public TermCollectorGeoTermObserver(Handler handler) {
            super(handler);
        }

        @Override
        public void onChange(boolean selfChange) {
            super.onChange(selfChange);

            Log.wtf(TAG, "@onChange (TermCollectorGeoTermObserver)");


            // set cursor to first item
            Cursor cursor = getContentResolver().query(
                    termCollectorGeoTermContentUri, null, null, null,
                    ContextophelesConstants.TERM_COLLECTOR_FIELD_TIMESTAMP + " DESC LIMIT 1");

            if (cursor != null && cursor.moveToFirst()) {

                saveGeoData(cursor.getLong(cursor.getColumnIndex(ContextophelesConstants.TERM_COLLECTOR_FIELD_TIMESTAMP)),
                        termCollectorGeoTermContentUri.toString(), cursor.getString(cursor
                        .getColumnIndex(ContextophelesConstants.TERM_COLLECTOR_GEODATA_FIELD_TERM_CONTENT)));
            }

            if (cursor != null && !cursor.isClosed()) {
                cursor.close();
            }
        }

    }

    public class GeonameResolverObserver extends ContentObserver {
        public GeonameResolverObserver(Handler handler) {
            super(handler);
        }

        @Override
        public void onChange(boolean selfChange) {
            super.onChange(selfChange);

            Log.wtf(TAG, "@onChange (GeonameResolverObserver)");

            // run only, if use of location is allowed
            if  (CommonSettings.getQueryUseOfLocation(getContentResolver())){
            // set cursor to first item
            Cursor cursor = getContentResolver().query(
                    geonameResolverContentUri, null, null, null,
                    ContextophelesConstants.GEONAME_RESOLVER_FIELD_TIMESTAMP + " DESC LIMIT 1");
            if (cursor != null && cursor.moveToFirst()) {

                saveGeoData(cursor.getLong(cursor.getColumnIndex(ContextophelesConstants.GEONAME_RESOLVER_FIELD_TIMESTAMP)),
                        geonameResolverContentUri.toString(), cursor.getString(cursor
                        .getColumnIndex(ContextophelesConstants.GEONAME_RESOLVER_FIELD_NAME)));
            }

            if (cursor != null && !cursor.isClosed()) {
                cursor.close();
            }
            } else {
                Log.d(TAG, "GeonameResolver ignores input, as useOfLocation is disabled.");
            }
        }

    }

    private void saveGeoData(long timestamp, String source, String content) {
        ContentValues rowData = new ContentValues();
        rowData.put(GeoCollectorTermData.DEVICE_ID, Aware.getSetting(
                getContentResolver(), Aware_Preferences.DEVICE_ID));
        rowData.put(GeoCollectorTermData.TIMESTAMP, timestamp);
        rowData.put(GeoCollectorTermData.TERM_SOURCE, source);
        rowData.put(GeoCollectorTermData.TERM_CONTENT, content);

        Log.d(TAG, "Saving " + rowData.toString());
        getContentResolver().insert(GeoCollectorTermData.CONTENT_URI, rowData);
    }

}

{'fraction_non_alphanumeric': 0.08231754358895232, 'fraction_numerical': 0.00030859435272334514, 'mean_word_length': 2.782608695652174, 'pattern_counts': {'":': 0, '<': 7, '<?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': '4940323', 'n_tokens_mistral': 3615, 'n_tokens_neox': 3410, 'n_words': 650}