导航菜单

页面标题

页面副标题

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

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

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


package com.eveningoutpost.dexdrip;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.PowerManager;
import android.text.format.DateFormat;
import com.eveningoutpost.dexdrip.importedlibraries.usbserial.driver.UsbId;
import com.eveningoutpost.dexdrip.models.BgReading;
import com.eveningoutpost.dexdrip.models.GlucoseData;
import com.eveningoutpost.dexdrip.models.JoH;
import com.eveningoutpost.dexdrip.models.Libre2RawValue;
import com.eveningoutpost.dexdrip.models.Libre2Sensor;
import com.eveningoutpost.dexdrip.models.Sensor;
import com.eveningoutpost.dexdrip.models.UserError;
import com.eveningoutpost.dexdrip.utilitymodels.Pref;
import com.eveningoutpost.dexdrip.utilitymodels.StatusItem;
import com.eveningoutpost.dexdrip.utils.DexCollectionType;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class LibreReceiver extends BroadcastReceiver {
    private static final String TAG = "LibreReceiver";
    private static volatile String bluetoothAddress = "";
    private static volatile String connectionState = "";
    private static volatile long last_reading = 0;
    private static volatile String libre_calc_doku = "wait for next reading...";
    private static final Object lock = new Object();

    @Override
    public void onReceive(final Context context, final Intent intent) {
        if (DexCollectionType.getDexCollectionType() != DexCollectionType.LibreReceiver) {
            return;
        }
        new Thread() {
            @Override
            public void run() {
                char c;
                PowerManager.WakeLock wakeLock = JoH.getWakeLock("libre-receiver", UsbId.SILABS_CP2102);
                synchronized (LibreReceiver.lock) {
                    try {
                        UserError.Log.d(LibreReceiver.TAG, "libre onReceiver: " + intent.getAction());
                        JoH.benchmark(null);
                        String action = intent.getAction();
                        if (action == null) {
                            return;
                        }
                        switch (action.hashCode()) {
                            case -970136353:
                                if (action.equals("com.librelink.app.ThirdPartyIntegration.CONNECTION_STATE")) {
                                    c = 2;
                                    break;
                                }
                                c = 65535;
                                break;
                            case 295532777:
                                if (action.equals("com.librelink.app.ThirdPartyIntegration.SENSOR_ACTIVATE")) {
                                    c = 0;
                                    break;
                                }
                                c = 65535;
                                break;
                            case 604323852:
                                if (action.equals("com.librelink.app.ThirdPartyIntegration.GLUCOSE_READING")) {
                                    c = 3;
                                    break;
                                }
                                c = 65535;
                                break;
                            case 1099357555:
                                if (action.equals("com.librelink.app.ThirdPartyIntegration.SENSOR_SCAN")) {
                                    c = 1;
                                    break;
                                }
                                c = 65535;
                                break;
                            default:
                                c = 65535;
                                break;
                        }
                        if (c == 0) {
                            UserError.Log.v(LibreReceiver.TAG, "Receiving LibreData activation");
                            try {
                                LibreReceiver.saveSensorStartTime(intent.getBundleExtra("sensor"), intent.getBundleExtra("bleManager").getString("sensorSerial"));
                            } catch (NullPointerException e) {
                                UserError.Log.e(LibreReceiver.TAG, "Null pointer in LIBRE2_ACTIVATION: " + e);
                            }
                            Sensor.createDefaultIfMissing();
                        }
                        if (c == 1) {
                            UserError.Log.v(LibreReceiver.TAG, "Receiving LibreData scan");
                            Sensor.createDefaultIfMissing();
                            try {
                                long currentDeduplicationPeriod = DexCollectionType.getCurrentDeduplicationPeriod();
                                Bundle bundle = intent.getBundleExtra("sas").getBundle("realTimeGlucoseReadings");
                                Iterator<String> it = bundle.keySet().iterator();
                                while (it.hasNext()) {
                                    Bundle bundle2 = bundle.getBundle(it.next());
                                    BgReading.bgReadingInsertFromInt((int) Math.round(bundle2.getDouble("glucoseValue")), bundle2.getLong("timestamp"), currentDeduplicationPeriod, false, "Libre2");
                                }
                            } catch (Exception e2) {
                                UserError.Log.e(LibreReceiver.TAG, "Got exception processing realtime: " + e2);
                            }
                            try {
                                Bundle bundle3 = intent.getBundleExtra("sas").getBundle("historicGlucoseReadings");
                                ArrayList arrayList = new ArrayList(bundle3.size());
                                Iterator<String> it2 = bundle3.keySet().iterator();
                                while (it2.hasNext()) {
                                    Bundle bundle4 = bundle3.getBundle(it2.next());
                                    GlucoseData glucoseData = new GlucoseData((int) Math.round(bundle4.getDouble("glucoseValue")), bundle4.getLong("timestamp"));
                                    glucoseData.glucoseLevel = glucoseData.glucoseLevelRaw;
                                    arrayList.add(glucoseData);
                                }
                                LibreAlarmReceiver.insertFromHistory(arrayList, false);
                            } catch (Exception e3) {
                                UserError.Log.e(LibreReceiver.TAG, "Got exception processing history: " + e3);
                            }
                            Home.staticRefreshBGChartsOnIdle();
                        }
                        if (c == 2) {
                            JoH.dumpBundle(intent.getExtras(), LibreReceiver.TAG);
                            try {
                                String unused = LibreReceiver.bluetoothAddress = intent.getBundleExtra("bleManager").getString("sensorAddress");
                            } catch (Exception e4) {
                                UserError.Log.e(LibreReceiver.TAG, "Exception parsing libre2connection sensorAddress: " + e4);
                            }
                            try {
                                String unused2 = LibreReceiver.connectionState = intent.getStringExtra("connectionState");
                            } catch (Exception e5) {
                                UserError.Log.e(LibreReceiver.TAG, "Exception parsing libre2connection connectionState: " + e5);
                            }
                        } else if (c != 3) {
                            UserError.Log.e(LibreReceiver.TAG, "Unknown action! " + action);
                            JoH.dumpBundle(intent.getExtras(), LibreReceiver.TAG);
                        } else {
                            Libre2RawValue processIntent = LibreReceiver.processIntent(intent);
                            if (processIntent == null) {
                                return;
                            }
                            UserError.Log.v(LibreReceiver.TAG, "got bg reading: from sensor:" + processIntent.serial + " rawValue:" + processIntent.glucose + " at:" + processIntent.timestamp);
                            if (!BgReading.last_within_millis(DexCollectionType.getCurrentDeduplicationPeriod())) {
                                long stringToInt = Pref.getStringToInt("libre_filter_length", 25);
                                List<Libre2RawValue> weightedAverageInterval = Libre2RawValue.weightedAverageInterval(stringToInt == 25 ? 20L : stringToInt);
                                weightedAverageInterval.add(processIntent);
                                LibreReceiver.processValues(processIntent, weightedAverageInterval, stringToInt, context);
                            }
                            processIntent.save();
                            LibreReceiver.clearNFCsensorAge();
                        }
                    } finally {
                        JoH.benchmark(LibreReceiver.TAG);
                        JoH.releaseWakeLock(wakeLock);
                    }
                }
            }
        }.start();
    }

    public static void clearNFCsensorAge() {
        if (Pref.getInt("nfc_sensor_age", 0) != 0) {
            Pref.setInt("nfc_sensor_age", 0);
        }
    }

    public static Libre2RawValue processIntent(Intent intent) {
        Bundle bundleExtra = intent.getBundleExtra("sas");
        if (bundleExtra != null) {
            try {
                saveSensorStartTime(bundleExtra.getBundle("currentSensor"), intent.getBundleExtra("bleManager").getString("sensorSerial"));
            } catch (NullPointerException e) {
                UserError.Log.e(TAG, "Null pointer exception in processIntent: " + e);
            }
        }
        if (!intent.hasExtra("glucose") || !intent.hasExtra("timestamp") || !intent.hasExtra("bleManager")) {
            UserError.Log.e(TAG, "Received faulty intent from LibreLink.");
            return null;
        }
        double doubleExtra = intent.getDoubleExtra("glucose", BgReading.BESTOFFSET);
        long longExtra = intent.getLongExtra("timestamp", 0L);
        last_reading = longExtra;
        String string = intent.getBundleExtra("bleManager").getString("sensorSerial");
        if (string == null) {
            UserError.Log.e(TAG, "Received faulty intent from LibreLink.");
            return null;
        }
        Libre2RawValue libre2RawValue = new Libre2RawValue();
        libre2RawValue.timestamp = longExtra;
        libre2RawValue.glucose = doubleExtra;
        libre2RawValue.serial = string;
        return libre2RawValue;
    }

    public static void processValues(Libre2RawValue libre2RawValue, List<Libre2RawValue> list, long j, Context context) {
        if (Sensor.currentSensor() == null) {
            Sensor.create(libre2RawValue.timestamp, libre2RawValue.serial);
        }
        BgReading.bgReadingInsertLibre2(calculateWeightedAverage(list, libre2RawValue.timestamp, TimeUnit.MINUTES.toMillis(j)), libre2RawValue.timestamp, libre2RawValue.glucose);
    }

    public static void saveSensorStartTime(Bundle bundle, String str) {
        if (bundle == null || !bundle.containsKey("sensorStartTime")) {
            return;
        }
        long j = bundle.getLong("sensorStartTime");
        Sensor currentSensor = Sensor.currentSensor();
        if (currentSensor != null && !currentSensor.uuid.equals(str)) {
            Sensor.stopSensor();
            currentSensor = null;
        }
        if (currentSensor == null) {
            Sensor.create(j, str);
        }
    }

    private static double calculateWeightedAverage(List<Libre2RawValue> list, long j, long j2) {
        DecimalFormat decimalFormat = new DecimalFormat("#,###,###,##0.00");
        libre_calc_doku = "";
        double d = BgReading.BESTOFFSET;
        double d2 = 0.0d;
        for (Libre2RawValue libre2RawValue : list) {
            double d3 = 1.0d - ((j - libre2RawValue.timestamp) / j2);
            d += libre2RawValue.glucose * d3;
            d2 += d3;
            libre_calc_doku += ((Object) DateFormat.format("kk:mm:ss :", libre2RawValue.timestamp)) + " w:" + decimalFormat.format(d3) + " raw: " + libre2RawValue.glucose + "\n";
        }
        return Math.round(d / d2);
    }

    public static List<StatusItem> megaStatus() {
        ArrayList arrayList = new ArrayList();
        Sensor currentSensor = Sensor.currentSensor();
        if (currentSensor != null) {
            arrayList.add(new StatusItem("Libre2 Sensor", currentSensor.uuid + "\nStart: " + ((Object) DateFormat.format("dd.MM.yyyy kk:mm", currentSensor.started_at))));
        }
        if (last_reading > 0) {
            try {
                arrayList.add(new StatusItem(xdrip.gs(2131755738), DateFormat.format("dd.MM.yyyy kk:mm:ss", last_reading).toString()));
            } catch (Exception e) {
                UserError.Log.e(TAG, "Error readlast: " + e);
            }
        }
        if (!JoH.emptyString(connectionState)) {
            arrayList.add(new StatusItem("Bluetooth Link", connectionState));
        }
        if (Home.get_engineering_mode()) {
            arrayList.add(new StatusItem("Last Calc.", libre_calc_doku));
            if (!JoH.emptyString(bluetoothAddress)) {
                arrayList.add(new StatusItem("Bluetooth Mac", bluetoothAddress));
            }
        }
        if (Pref.getBooleanDefaultFalse("Libre2_showSensors")) {
            arrayList.add(new StatusItem("Sensors", Libre2Sensor.Libre2Sensors()));
        }
        return arrayList;
    }
}