<commit_before>package info.nightscout.android.upload.nightscout;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.preference.PreferenceManager;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONArray;
import org.json.JSONObject;

import java.math.BigDecimal;
import java.net.URL;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import info.nightscout.android.R;
import info.nightscout.android.medtronic.MainActivity;
import info.nightscout.android.model.medtronicNg.PumpStatusEvent;
import info.nightscout.android.upload.nightscout.serializer.EntriesSerializer;
import io.realm.Realm;
import io.realm.RealmResults;

public class NightscoutUploadIntentService extends IntentService {

    private static final String TAG = NightscoutUploadIntentService.class.getSimpleName();
    private static final SimpleDateFormat ISO8601_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.getDefault());
    private static final int SOCKET_TIMEOUT = 60 * 1000;
    private static final int CONNECTION_TIMEOUT = 30 * 1000;
    Context mContext;
    private Realm mRealm;

    public NightscoutUploadIntentService() {
        super(NightscoutUploadIntentService.class.getName());
    }

    protected void sendStatus(String message) {
        Intent localIntent =
                new Intent(Constants.ACTION_STATUS_MESSAGE)
                        .putExtra(Constants.EXTENDED_DATA, message);
        LocalBroadcastManager.getInstance(this).sendBroadcast(localIntent);
    }

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

        Log.i(TAG, "onCreate called");
        mContext = this.getBaseContext();
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Log.d(TAG, "onHandleIntent called");
        mRealm = Realm.getDefaultInstance();

        RealmResults<PumpStatusEvent> records = mRealm
                .where(PumpStatusEvent.class)
                .equalTo("uploaded", false)
                .notEqualTo("sgv", 0)
                .findAll();

        if (records.size() > 0) {
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);

            Boolean enableRESTUpload = prefs.getBoolean("EnableRESTUpload", false);
            try {
                if (enableRESTUpload) {
                    long start = System.currentTimeMillis();
                    Log.i(TAG, String.format("Starting upload of %s record using a REST API", records.size()));
                    doRESTUpload(prefs, records);
                    Log.i(TAG, String.format("Finished upload of %s record using a REST API in %s ms", records.size(), System.currentTimeMillis() - start));
                }
            } catch (Exception e) {
                Log.e(TAG, "ERROR uploading data!!!!!", e);
            }
        } else {
            Log.i(TAG, "No records has to be uploaded");
        }

        NightscoutUploadReceiver.completeWakefulIntent(intent);
    }

    private void doRESTUpload(SharedPreferences prefs, RealmResults<PumpStatusEvent> records) {
        String apiScheme = "https://";
        String apiUrl = "";
        String apiSecret = prefs.getString(mContext.getString(R.string.preference_api_secret), "YOURAPISECRET");

        // TODO - this code needs to go to the Settings Activity.
        // Add the extra match for "KEY@" to support the previous single field
        Pattern p = Pattern.compile("(.*\\/\\/)?(.*@)?([^\\/]*)(.*)");
        Matcher m = p.matcher(prefs.getString(mContext.getString(R.string.preference_nightscout_url), ""));

        if (m.find()) {
            apiUrl = m.group(3);

            // Only override apiSecret from URL (the "old" way), if the API secret preference is empty
            if (apiSecret.equals("YOURAPISECRET") || apiSecret.equals("")) {
                apiSecret = (m.group(2) == null) ? "" : m.group(2).replace("@", "");
            }

            // Override the URI scheme if it's been provided in the preference)
            if (m.group(1) != null && !m.group(1).equals("")) {
                apiScheme = m.group(1);<commit_msg>fixing<commit_after>package info.nightscout.android.upload.nightscout;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.preference.PreferenceManager;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONArray;
import org.json.JSONObject;

import java.math.BigDecimal;
import java.net.URL;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import info.nightscout.android.R;
import info.nightscout.android.medtronic.MainActivity;
import info.nightscout.android.model.medtronicNg.PumpStatusEvent;
import info.nightscout.android.upload.nightscout.serializer.EntriesSerializer;
import io.realm.Realm;
import io.realm.RealmResults;

public class NightscoutUploadIntentService extends IntentService {

    private static final String TAG = NightscoutUploadIntentService.class.getSimpleName();
    private static final SimpleDateFormat ISO8601_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.getDefault());
    private static final int SOCKET_TIMEOUT = 60 * 1000;
    private static final int CONNECTION_TIMEOUT = 30 * 1000;
    private Context mContext;
    private Realm mRealm;

    public NightscoutUploadIntentService() {
        super(NightscoutUploadIntentService.class.getName());
    }

    protected void sendStatus(String message) {
        Intent localIntent =
                new Intent(Constants.ACTION_STATUS_MESSAGE)
                        .putExtra(Constants.EXTENDED_DATA, message);
        LocalBroadcastManager.getInstance(this).sendBroadcast(localIntent);
    }

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

        Log.i(TAG, "onCreate called");
        mContext = this.getBaseContext();

    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Log.d(TAG, "onHandleIntent called");
        mRealm = Realm.getDefaultInstance();

        RealmResults<PumpStatusEvent> records = mRealm
                .where(PumpStatusEvent.class)
                .equalTo("uploaded", false)
                .notEqualTo("sgv", 0)
                .findAll();

        if (records.size() > 0) {
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);

            Boolean enableRESTUpload = prefs.getBoolean("EnableRESTUpload", false);
            try {
                if (enableRESTUpload) {
                    long start = System.currentTimeMillis();
                    Log.i(TAG, String.format("Starting upload of %s record using a REST API", records.size()));
                    doRESTUpload(prefs, records);
                    Log.i(TAG, String.format("Finished upload of %s record using a REST API in %s ms", records.size(), System.currentTimeMillis() - start));
                }
            } catch (Exception e) {
                Log.e(TAG, "ERROR uploading data!!!!!", e);
            }
        } else {
            Log.i(TAG, "No records has to be uploaded");
        }
        mRealm.close();

        NightscoutUploadReceiver.completeWakefulIntent(intent);
    }

    private void doRESTUpload(SharedPreferences prefs, RealmResults<PumpStatusEvent> records) {
        String apiScheme = "https://";
        String apiUrl = "";
        String apiSecret = prefs.getString(mContext.getString(R.string.preference_api_secret), "YOURAPISECRET");

        // TODO - this code needs to go to the Settings Activity.
        // Add the extra match for "KEY@" to support the previous single field
        Pattern p = Pattern.compile("(.*\\/\\/)?(.*@)?([^\\/]*)(.*)");
        Matcher m = p.matcher(prefs.getString(mContext.getString(R.string.preference_nightscout_url), ""));

        if (m.find()) {
            apiUrl = m.group(3);

            // Only override apiSecret from URL (the "old" way), if the API secret preference is empty
            if (apiSecret.equals("YOURAPISECRET") || apiSecret.equals("")) {
                apiSecret = (m.group(2) == null) ? "" : m.group(2).replace("@", "");
            }

            // Override the URI scheme if it's been provided in the preference)
            if (m.group(1) != null && !m.group(1).equals("")) {
                apiScheme = m.group(1);
{'fraction_non_alphanumeric': 0.11276948590381426, 'fraction_numerical': 0.005182421227197346, 'mean_word_length': 2.9888383629599007, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, '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': '1895979', 'n_tokens_mistral': 2784, 'n_tokens_neox': 2664, 'n_words': 591}