<commit_before>package info.nightscout.androidaps.data;

import android.support.v4.util.LongSparseArray;

import com.crashlytics.android.Crashlytics;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.plugins.Overview.Notification;
import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification;
import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification;
import info.nightscout.utils.DecimalFormatter;
import info.nightscout.utils.ToastUtils;

public class Profile {
    private static Logger log = LoggerFactory.getLogger(Profile.class);

    private JSONObject json;
    private String units = null;
    double dia = Constants.defaultDIA;
    TimeZone timeZone = TimeZone.getDefault();
    JSONArray isf;
    private LongSparseArray<Double> isf_v = null; // oldest at index 0
    JSONArray ic;
    private LongSparseArray<Double> ic_v = null; // oldest at index 0
    JSONArray basal;
    private LongSparseArray<Double> basal_v = null; // oldest at index 0
    JSONArray targetLow;
    JSONArray targetHigh;

    public Profile(JSONObject json, String units) {
        this(json);
        if (this.units == null) {
            if (units != null)
                this.units = units;
            else {
                Crashlytics.log("Profile failover failed too");
                this.units = Constants.MGDL;
            }
        }
    }

    public Profile(JSONObject json) {
        this.json = json;
        try {
            if (json.has("units"))
                units = json.getString("units").toLowerCase();
            if (json.has("dia"))
                dia = json.getDouble("dia");
            if (json.has("dia"))
                dia = json.getDouble("dia");
            if (json.has("timezone"))
                timeZone = TimeZone.getTimeZone(json.getString("timezone"));
            isf = json.getJSONArray("sens");
            if (getIsf(0) == null) {
                int defaultISF = units.equals(Constants.MGDL) ? 400 : 20;
                isf = new JSONArray("[{\"time\":\"00:00\",\"value\":\"" + defaultISF + "\",\"timeAsSeconds\":\"0\"}]");
                Notification noisf = new Notification(Notification.ISF_MISSING, MainApp.sResources.getString(R.string.isfmissing), Notification.URGENT);
                MainApp.bus().post(new EventNewNotification(noisf));
            } else {
                MainApp.bus().post(new EventDismissNotification(Notification.ISF_MISSING));
            }
            ic = json.getJSONArray("carbratio");
            if (getIc(0) == null) {
                int defaultIC = 25;
                isf = new JSONArray("[{\"time\":\"00:00\",\"value\":\"" + defaultIC + "\",\"timeAsSeconds\":\"0\"}]");
                Notification noic = new Notification(Notification.IC_MISSING, MainApp.sResources.getString(R.string.icmissing), Notification.URGENT);
                MainApp.bus().post(new EventNewNotification(noic));
            } else {
                MainApp.bus().post(new EventDismissNotification(Notification.IC_MISSING));
            }
            basal = json.getJSONArray("basal");
            if (getBasal(0) == null) {
                double defaultBasal = 0.1d;
                isf = new JSONArray("[{\"time\":\"00:00\",\"value\":\"" + defaultBasal + "\",\"timeAsSeconds\":\"0\"}]");
                Notification nobasal = new Notification(Notification.BASAL_MISSING, MainApp.sResources.getString(R.string.basalmissing), Notification.URGENT);
                MainApp.bus().post(new EventNewNotification(nobasal));
            } else {
                MainApp.bus().post(new EventDismissNotification(Notification.BASAL_MISSING));
            }
            targetLow = json.getJSONArray("target_low");
            if (getTargetLow(0) == null) {
                double defaultLow = units.equals(Constants.MGDL) ? 120 : 6;
                isf = new JSONArray("[{\"time\":\"00:00\",\"value\":\"" + defaultLow + "\",\"timeAsSeconds\":\"0\"}]");
                Notification notarget = new Notification(Notification.TARGET_MISSING, MainApp.sResources.getString(R.string.targetmissing), Notification.URGENT);
                MainApp.bus().post(new EventNewNotification(notarget));
            } else {
                MainApp.bus().post(new EventDismissNotification(Notification.TARGET_MISSING));
            }
            targetHigh = json.getJSONArray("target_high");
            if (getTargetHigh(0) == null) {
                double defaultHigh = units.equals(Constants.MGDL) ? 160 : 8;
                isf = new JSONArray("[{\"time\":\"00:00\",\"value\":\"" + defaultHigh + "\",\"timeAsSeconds\":\"0\"}]");
                Notification notarget = new Notification(Notification.TARGET_MISSING, MainApp.sResources.getString(R.string.targetmissing), Notification.URGENT);
                MainApp.bus().post(new EventNewNotification(notarget));
            } else {
                MainApp.bus().post(new EventDismissNotification(Notification.TARGET_MISSING));
            }
        } catch (JSONException e) {
            e.printStackTrace();<commit_msg>Fix initalizing profile with default values.<commit_after>package info.nightscout.androidaps.data;

import android.support.v4.util.LongSparseArray;

import com.crashlytics.android.Crashlytics;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.plugins.Overview.Notification;
import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification;
import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification;
import info.nightscout.utils.DecimalFormatter;
import info.nightscout.utils.ToastUtils;

public class Profile {
    private static Logger log = LoggerFactory.getLogger(Profile.class);

    private JSONObject json;
    private String units = null;
    double dia = Constants.defaultDIA;
    TimeZone timeZone = TimeZone.getDefault();
    JSONArray isf;
    private LongSparseArray<Double> isf_v = null; // oldest at index 0
    JSONArray ic;
    private LongSparseArray<Double> ic_v = null; // oldest at index 0
    JSONArray basal;
    private LongSparseArray<Double> basal_v = null; // oldest at index 0
    JSONArray targetLow;
    JSONArray targetHigh;

    public Profile(JSONObject json, String units) {
        this(json);
        if (this.units == null) {
            if (units != null)
                this.units = units;
            else {
                Crashlytics.log("Profile failover failed too");
                this.units = Constants.MGDL;
            }
        }
    }

    public Profile(JSONObject json) {
        this.json = json;
        try {
            if (json.has("units"))
                units = json.getString("units").toLowerCase();
            if (json.has("dia"))
                dia = json.getDouble("dia");
            if (json.has("dia"))
                dia = json.getDouble("dia");
            if (json.has("timezone"))
                timeZone = TimeZone.getTimeZone(json.getString("timezone"));
            isf = json.getJSONArray("sens");
            if (getIsf(0) == null) {
                int defaultISF = units.equals(Constants.MGDL) ? 400 : 20;
                isf = new JSONArray("[{\"time\":\"00:00\",\"value\":\"" + defaultISF + "\",\"timeAsSeconds\":\"0\"}]");
                Notification noisf = new Notification(Notification.ISF_MISSING, MainApp.sResources.getString(R.string.isfmissing), Notification.URGENT);
                MainApp.bus().post(new EventNewNotification(noisf));
            } else {
                MainApp.bus().post(new EventDismissNotification(Notification.ISF_MISSING));
            }
            ic = json.getJSONArray("carbratio");
            if (getIc(0) == null) {
                int defaultIC = 25;
                ic = new JSONArray("[{\"time\":\"00:00\",\"value\":\"" + defaultIC + "\",\"timeAsSeconds\":\"0\"}]");
                Notification noic = new Notification(Notification.IC_MISSING, MainApp.sResources.getString(R.string.icmissing), Notification.URGENT);
                MainApp.bus().post(new EventNewNotification(noic));
            } else {
                MainApp.bus().post(new EventDismissNotification(Notification.IC_MISSING));
            }
            basal = json.getJSONArray("basal");
            if (getBasal(0) == null) {
                double defaultBasal = 0.1d;
                basal = new JSONArray("[{\"time\":\"00:00\",\"value\":\"" + defaultBasal + "\",\"timeAsSeconds\":\"0\"}]");
                Notification nobasal = new Notification(Notification.BASAL_MISSING, MainApp.sResources.getString(R.string.basalmissing), Notification.URGENT);
                MainApp.bus().post(new EventNewNotification(nobasal));
            } else {
                MainApp.bus().post(new EventDismissNotification(Notification.BASAL_MISSING));
            }
            targetLow = json.getJSONArray("target_low");
            if (getTargetLow(0) == null) {
                double defaultLow = units.equals(Constants.MGDL) ? 120 : 6;
                targetLow = new JSONArray("[{\"time\":\"00:00\",\"value\":\"" + defaultLow + "\",\"timeAsSeconds\":\"0\"}]");
                Notification notarget = new Notification(Notification.TARGET_MISSING, MainApp.sResources.getString(R.string.targetmissing), Notification.URGENT);
                MainApp.bus().post(new EventNewNotification(notarget));
            } else {
                MainApp.bus().post(new EventDismissNotification(Notification.TARGET_MISSING));
            }
            targetHigh = json.getJSONArray("target_high");
            if (getTargetHigh(0) == null) {
                double defaultHigh = units.equals(Constants.MGDL) ? 160 : 8;
                targetHigh = new JSONArray("[{\"time\":\"00:00\",\"value\":\"" + defaultHigh + "\",\"timeAsSeconds\":\"0\"}]");
                Notification notarget = new Notification(Notification.TARGET_MISSING, MainApp.sResources.getString(R.string.targetmissing), Notification.URGENT);
                MainApp.bus().post(new EventNewNotification(notarget));
            } else {
                MainApp.bus().post(new EventDismissNotification(Notification.TARGET_MISSING));
            }
        } catch (JSONException e) {
            e.printStackTrace();
{'fraction_non_alphanumeric': 0.13493156976210158, 'fraction_numerical': 0.009736382841921559, 'mean_word_length': 2.5247652962123666, 'pattern_counts': {'":': 30, '<': 9, '<?xml version=': 0, '>': 9, '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': '1692337', 'n_tokens_mistral': 3197, 'n_tokens_neox': 2996, 'n_words': 540}