导航菜单

页面标题

页面副标题

xDrip+ v04633772025.07.16 - BasalProfileEditor.java 源代码

正在查看: xDrip+ v04633772025.07.16 应用的 BasalProfileEditor.java JAVA 源代码文件

本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。


package com.eveningoutpost.dexdrip.profileeditor;

import android.R;
import android.annotation.SuppressLint;
import android.graphics.Rect;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.text.style.StyleSpan;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.eveningoutpost.dexdrip.models.BgReading;
import com.eveningoutpost.dexdrip.models.JoH;
import com.eveningoutpost.dexdrip.models.UserError;
import com.eveningoutpost.dexdrip.ui.charts.BasalChart;
import com.eveningoutpost.dexdrip.ui.dialog.GenericConfirmDialog;
import com.eveningoutpost.dexdrip.ui.helpers.ColorUtil;
import com.eveningoutpost.dexdrip.ui.helpers.UiHelper;
import com.eveningoutpost.dexdrip.utilitymodels.ColorCache;
import com.eveningoutpost.dexdrip.utilitymodels.PersistentStore;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import lecho.lib.hellocharts.animation.ChartAnimationListener;
import lecho.lib.hellocharts.gesture.ContainerScrollType;
import lecho.lib.hellocharts.gesture.ZoomType;
import lecho.lib.hellocharts.listener.ColumnChartOnValueSelectListener;
import lecho.lib.hellocharts.model.Column;
import lecho.lib.hellocharts.model.SubcolumnValue;
import lecho.lib.hellocharts.model.Viewport;
import lecho.lib.hellocharts.view.ColumnChartView;

public class BasalProfileEditor extends AppCompatActivity implements AdapterView.OnItemSelectedListener, ChartAnimationListener {
    private final int SELECTED_COLUMN_COLOR;
    private final int UNSELECTED_COLUMN_COLOR;
    Spinner basalSelectSpinner;
    TextView basalStepLabel;
    Spinner basalStepSpinner;
    private ColumnChartView chart;
    private Rect goodMargin;
    Button minusButton;
    MenuItem minusMenuItem;
    Button plusButton;
    MenuItem plusMenuItem;
    Button setButton;
    EditText setValue;
    private long lastSelection = -1;
    private int selectedColumnA = -1;
    private int selectedColumnB = -1;
    private int flipper = 0;
    private volatile boolean controlsLocked = false;

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {
    }

    public BasalProfileEditor() {
        ColorCache.X x = ColorCache.X.color_basal_tbr;
        this.SELECTED_COLUMN_COLOR = ColorUtil.adjustHue(ColorCache.getCol(x), -180.0f);
        this.UNSELECTED_COLUMN_COLOR = ColorCache.getCol(x);
    }

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(2131492897);
        JoH.fixActionBar(this);
        this.chart = findViewById(2131296377);
        this.basalSelectSpinner = (Spinner) findViewById(2131296383);
        this.basalStepSpinner = (Spinner) findViewById(2131296389);
        this.basalStepLabel = (TextView) findViewById(2131296388);
        this.setValue = (EditText) findViewById(2131296386);
        this.setButton = (Button) findViewById(2131296385);
        this.plusButton = (Button) findViewById(2131296382);
        this.minusButton = (Button) findViewById(2131296380);
        this.chart.setDataAnimationListener(this);
        this.chart.setOnValueTouchListener(new ValueTouchListener());
        setChartFromSpinnerSelection();
        autoSetViewPort(true);
        refreshScreenElements();
        refreshZoomFeatures();
        populateBasalNameSpinner();
        populateBasalStepSpinner();
    }

    private void refreshZoomFeatures() {
        this.chart.setZoomType(ZoomType.HORIZONTAL);
        this.chart.setContainerScrollEnabled(true, ContainerScrollType.HORIZONTAL);
        this.chart.setMaxZoom(4.0d);
        this.chart.setZoomLevel(getNewMaxViewport().centerX(), getNewMaxViewport().centerY(), 1.0d);
        this.chart.setTapZoomEnabled(false);
    }

    private static void setLastBasalProfileName(String str) {
        PersistentStore.setString("LAST_BASAL_PROFILE_NAME", str);
    }

    public static String getLastBasalProfileName() {
        return PersistentStore.getString("LAST_BASAL_PROFILE_NAME", "1");
    }

    private void setChartFromSpinnerSelection() {
        this.chart.getChartComputator().resetContentRect();
        if (this.chart.getChartComputator().getContentRectMinusAxesMargins().right != 0) {
            this.goodMargin = new Rect();
            Rect contentRectMinusAxesMargins = this.chart.getChartComputator().getContentRectMinusAxesMargins();
            this.goodMargin.bottom = contentRectMinusAxesMargins.bottom;
        }
        this.chart.setColumnChartData(BasalChart.columnData(getLastBasalProfileName()));
        if (this.goodMargin != null) {
            this.chart.getChartComputator().getContentRectMinusAllMargins().bottom = this.goodMargin.bottom;
            this.chart.getChartComputator().getContentRectMinusAxesMargins().bottom = this.goodMargin.bottom;
            BasalChart.refreshAxis(this.chart);
            autoSetViewPort(true);
            refreshZoomFeatures();
        }
        this.chart.getAxesRenderer().onChartDataChanged();
    }

    private void fixElipsus(ViewGroup viewGroup) {
        if (viewGroup == null) {
            viewGroup = (ViewGroup) getWindow().getDecorView().findViewById(R.id.content).getRootView();
        }
        int childCount = viewGroup.getChildCount();
        Log.d("BasalProfileEditor", "CHILDREN: " + childCount);
        for (int i = 0; i < childCount; i++) {
            View childAt = viewGroup.getChildAt(i);
            if (childAt instanceof TextView) {
                TextView textView = (TextView) childAt;
                String charSequence = textView.getText().toString();
                Log.d("BasalProfileEditor", charSequence);
                if (charSequence.contains("Daily")) {
                    Log.d("BasalProfileEditor", "CHILDREN hit");
                    Log.d("BasalProfileEditor", "" + textView.getMaxEms());
                    Log.d("BasalProfileEditor", "" + textView.getMaxWidth());
                    Log.d("BasalProfileEditor", "" + textView.getWidth());
                    textView.setEllipsize(null);
                    textView.setMinWidth(UiHelper.convertDpToPixel(300.0f));
                    return;
                }
            } else if (childAt instanceof ViewGroup) {
                fixElipsus((ViewGroup) childAt);
            }
        }
    }

    private int findSpinnerPositionForName(String str) {
        int count = this.basalSelectSpinner.getAdapter().getCount();
        for (int i = 0; i < count; i++) {
            if (this.basalSelectSpinner.getItemAtPosition(i).toString().equals(str)) {
                return i;
            }
        }
        return 0;
    }

    private void populateBasalNameSpinner() {
        ArrayList arrayList = new ArrayList();
        arrayList.add("1");
        arrayList.add("2");
        arrayList.add("3");
        arrayList.add("4");
        arrayList.add("5");
        this.basalSelectSpinner.setAdapter((SpinnerAdapter) new ArrayAdapter(this, R.layout.simple_spinner_dropdown_item, arrayList));
        this.basalSelectSpinner.setSelection(findSpinnerPositionForName(getLastBasalProfileName()));
        this.basalSelectSpinner.setOnItemSelectedListener(this);
    }

    private void populateBasalStepSpinner() {
        ArrayList arrayList = new ArrayList();
        arrayList.add("0.5");
        arrayList.add("0.2");
        arrayList.add("0.1");
        arrayList.add("0.05");
        arrayList.add("0.02");
        arrayList.add("0.01");
        this.basalStepSpinner.setAdapter((SpinnerAdapter) new ArrayAdapter(this, R.layout.simple_spinner_dropdown_item, arrayList));
        this.basalStepSpinner.setOnItemSelectedListener(this);
        int i = (int) PersistentStore.getLong("PREF_STORED_BASAL_STEP");
        if (i == 0) {
            this.basalStepSpinner.setSelection(3);
        } else {
            this.basalStepSpinner.setSelection(i - 1);
        }
        buttonsToMatchStep();
    }

    @SuppressLint({"SetTextI18n"})
    private void buttonsToMatchStep() {
        this.plusButton.setText("+" + ((String) this.basalStepSpinner.getSelectedItem()));
        this.minusButton.setText("-" + ((String) this.basalStepSpinner.getSelectedItem()));
    }

    private float getStepValue() {
        return Float.parseFloat(this.basalStepSpinner.getSelectedItem().toString());
    }

    public void basalEditPlus(MenuItem menuItem) {
        adjustSelectedColumns(getStepValue(), false);
    }

    public void basalEditLoad(MenuItem menuItem) {
        setChartFromSpinnerSelection();
        refreshTotals();
    }

    public void basalEditSave(MenuItem menuItem) {
        GenericConfirmDialog.show(this, "Confirm Overwrite", "Are you sure you want to save this to profile " + this.basalSelectSpinner.getSelectedItem().toString() + " ?", new Runnable() {
            @Override
            public void run() {
                BasalProfile.save(BasalProfileEditor.getLastBasalProfileName(), BasalProfileEditor.this.getListOfValues());
            }
        });
    }

    public void basalEditMinus(MenuItem menuItem) {
        adjustSelectedColumns(-getStepValue(), false);
    }

    public void basalButtonPlus(View view) {
        adjustSelectedColumns(getStepValue(), false);
    }

    public void basalButtonMinus(View view) {
        adjustSelectedColumns(-getStepValue(), false);
    }

    public void onBasalSet(View view) {
        try {
            adjustSelectedColumns((float) JoH.tolerantParseDouble(this.setValue.getText().toString()), true);
        } catch (NullPointerException | NumberFormatException unused) {
            JoH.static_toast_short("Number out of range");
        }
    }

    public void refreshTotals() {
        String format;
        float totalBasal = BasalChart.getTotalBasal(this.chart.getColumnChartData());
        float totalImmutableBasal = BasalChart.getTotalImmutableBasal(this.chart.getColumnChartData());
        float f = totalBasal - totalImmutableBasal;
        if (totalBasal == totalImmutableBasal) {
            format = String.format("Daily basal: ^%s^ U", JoH.qs0(totalBasal, 2));
        } else {
            StringBuilder sb = new StringBuilder();
            sb.append(f > 0.0f ? "+" : "");
            sb.append(JoH.qs0(f, 2));
            format = String.format("Daily basal: ^%s^ U, was: %s, change: ^%s^", JoH.qs0(totalBasal, 2), JoH.qs0(totalImmutableBasal, 2), sb.toString());
        }
        ArrayList arrayList = new ArrayList();
        int i = -1;
        while (true) {
            i = format.indexOf(94, i + 1);
            if (i <= -1) {
                break;
            } else {
                arrayList.add(Integer.valueOf(i - arrayList.size()));
            }
        }
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(format.replaceAll("\\^", ""));
        for (int i2 = 0; i2 < arrayList.size() - 1; i2 += 2) {
            spannableStringBuilder.setSpan(new StyleSpan(1), ((Integer) arrayList.get(i2)).intValue(), ((Integer) arrayList.get(i2 + 1)).intValue(), 18);
        }
        try {
            getSupportActionBar().setSubtitle(getString(2131755203) + "    (loaded profile: " + getLastBasalProfileName() + ")");
            getSupportActionBar().setTitle(spannableStringBuilder);
            fixElipsus(null);
        } catch (Exception e) {
            Log.e("BasalProfileEditor", "Got exception: " + e);
        }
    }

    private void refreshScreenElements() {
        JoH.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                BasalProfileEditor.this.autoSetViewPort(false);
                BasalProfileEditor.this.refreshTotals();
            }
        });
    }

    private Viewport getNewMaxViewport() {
        Viewport viewport = new Viewport(this.chart.getMaximumViewport());
        float round = (Math.round(((r1 * 2.0f) + 0.5f) + 0.2f) / 2.0f) + 0.1f;
        UserError.Log.d("BasalProfileEditor", "BASAL max: " + BasalChart.getMaxYvalue(this.chart.getChartData()) + " " + round);
        viewport.top = (double) round;
        return viewport;
    }

    public Viewport autoSetViewPort(boolean z) {
        Viewport newMaxViewport = getNewMaxViewport();
        Log.d("BasalProfileEditor", "Setting viewport: " + newMaxViewport.top + " " + newMaxViewport.right);
        this.chart.setViewportCalculationEnabled(false);
        this.chart.setMaximumViewport(newMaxViewport);
        if (z) {
            this.chart.setCurrentViewport(newMaxViewport);
        } else {
            Viewport currentViewport = this.chart.getCurrentViewport();
            currentViewport.top = newMaxViewport.top;
            this.chart.setCurrentViewport(currentViewport);
        }
        BasalChart.refreshAxis(this.chart);
        return newMaxViewport;
    }

    private void adjustSelectedColumns(float f, boolean z) {
        if (this.controlsLocked) {
            UserError.Log.d("BasalProfileEditor", "controls locked");
            return;
        }
        ArrayList arrayList = new ArrayList(48);
        Iterator<Integer> it = getSelectColumnList().iterator();
        while (it.hasNext()) {
            SubcolumnValue subcolumnValue = (SubcolumnValue) ((Column) this.chart.getChartData().getColumns().get(it.next().intValue())).getValues().get(0);
            if (z) {
                subcolumnValue.setTarget(Math.max(0.0f, f));
            } else {
                subcolumnValue.setTarget(Math.max(BgReading.BESTOFFSET, subcolumnValue.getValue() + f));
                UserError.Log.d("BasalProfileEditor", "SET TARGET: " + subcolumnValue.getValue());
            }
            arrayList.add(subcolumnValue);
        }
        this.chart.startDataAnimation();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(2131558402, menu);
        this.plusMenuItem = menu.findItem(2131296381);
        this.minusMenuItem = menu.findItem(2131296379);
        setPlusMinusVisible(false);
        return true;
    }

    public void selectedColumn(int i) {
        int i2 = this.selectedColumnA;
        if (i == i2 && i2 == this.selectedColumnB) {
            this.selectedColumnA = -1;
            this.selectedColumnB = -1;
            setPlusMinusVisible(false);
            showSelectedColumns();
            return;
        }
        setPlusMinusVisible(true);
        if (this.selectedColumnA == -1) {
            this.selectedColumnA = i;
        } else if (this.selectedColumnB == -1) {
            this.selectedColumnB = i;
        } else {
            int i3 = this.flipper;
            if (i3 % 2 == 0) {
                this.selectedColumnA = i;
            } else {
                this.selectedColumnB = i;
            }
            this.flipper = i3 + 1;
        }
        showSelectedColumns();
    }

    private void unselectAllColumns() {
        Iterator it = this.chart.getChartData().getColumns().iterator();
        while (it.hasNext()) {
            ((SubcolumnValue) ((Column) it.next()).getValues().get(0)).setColor(this.UNSELECTED_COLUMN_COLOR);
        }
    }

    private void showSelectedColumns() {
        unselectAllColumns();
        Iterator<Integer> it = getSelectColumnList().iterator();
        while (it.hasNext()) {
            selectColumn(it.next().intValue());
        }
    }

    private List<Integer> getSelectColumnList() {
        int i;
        ArrayList arrayList = new ArrayList();
        int i2 = this.selectedColumnA;
        if (i2 != -1 && (i = this.selectedColumnB) != -1) {
            int max = Math.max(this.selectedColumnA, this.selectedColumnB);
            for (int min = Math.min(i2, i); min <= max; min++) {
                arrayList.add(Integer.valueOf(min));
            }
        } else if (i2 != -1) {
            arrayList.add(Integer.valueOf(i2));
        } else {
            int i3 = this.selectedColumnB;
            if (i3 != -1) {
                arrayList.add(Integer.valueOf(i3));
            }
        }
        return arrayList;
    }

    private void selectColumn(int i) {
        if (i < 0) {
            return;
        }
        ((SubcolumnValue) ((Column) this.chart.getChartData().getColumns().get(i)).getValues().get(0)).setColor(this.SELECTED_COLUMN_COLOR);
    }

    private void setPlusMinusVisible(boolean z) {
        this.plusMenuItem.setVisible(z);
        this.minusMenuItem.setVisible(z);
        this.setValue.setVisibility(z ? 0 : 4);
        this.setButton.setVisibility(z ? 0 : 4);
        this.plusButton.setVisibility(z ? 0 : 4);
        this.minusButton.setVisibility(z ? 0 : 4);
        this.basalStepSpinner.setVisibility(z ? 0 : 4);
        this.basalStepLabel.setVisibility(z ? 0 : 4);
    }

    public List<Double> getListOfValues() {
        int size = this.chart.getChartData().getColumns().size();
        ArrayList arrayList = new ArrayList(size);
        for (int i = 0; i < size; i++) {
            arrayList.add(Double.valueOf(JoH.roundDouble(((SubcolumnValue) ((Column) this.chart.getChartData().getColumns().get(i)).getValues().get(0)).getValue(), 2)));
        }
        return arrayList;
    }

    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long j) {
        if (adapterView == this.basalStepSpinner) {
            UserError.Log.d("BasalProfileEditor", "Step Spinner selected it: " + i);
            this.basalStepSpinner.setSelection(i);
            PersistentStore.setLong("PREF_STORED_BASAL_STEP", (long) (i + 1));
            buttonsToMatchStep();
            return;
        }
        if (adapterView == this.basalSelectSpinner) {
            UserError.Log.d("BasalProfileEditor", "Name Spinner selected it: " + i);
            setLastBasalProfileName(adapterView.getSelectedItem().toString());
            return;
        }
        UserError.Log.wtf("BasalProfileEditor", "Unknown spinner selected");
    }

    public void onAnimationStarted() {
        UserError.Log.d("BasalProfileEditor", "Animation started");
        this.controlsLocked = true;
    }

    public void onAnimationFinished() {
        UserError.Log.d("BasalProfileEditor", "Animation finished");
        Iterator<Integer> it = getSelectColumnList().iterator();
        while (it.hasNext()) {
            BasalChart.setLabelForSubcolumn((SubcolumnValue) ((Column) this.chart.getChartData().getColumns().get(it.next().intValue())).getValues().get(0));
        }
        refreshScreenElements();
        this.controlsLocked = false;
    }

    private class ValueTouchListener implements ColumnChartOnValueSelectListener {
        public void onValueDeselected() {
        }

        private ValueTouchListener() {
        }

        public void onValueSelected(int i, int i2, SubcolumnValue subcolumnValue) {
            BasalProfileEditor.this.selectedColumn(i);
        }
    }
}