导航菜单

页面标题

页面副标题

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

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

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


package com.eveningoutpost.dexdrip.wearintegration;

import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.util.Base64;
import android.util.Log;
import com.eveningoutpost.dexdrip.BestGlucose;
import com.eveningoutpost.dexdrip.g5model.Extensions;
import com.eveningoutpost.dexdrip.g5model.Transmitter;
import com.eveningoutpost.dexdrip.models.ActiveBgAlert;
import com.eveningoutpost.dexdrip.models.ActiveBluetoothDevice;
import com.eveningoutpost.dexdrip.models.BgReading;
import com.eveningoutpost.dexdrip.models.HeartRate;
import com.eveningoutpost.dexdrip.models.JoH;
import com.eveningoutpost.dexdrip.models.StepCounter;
import com.eveningoutpost.dexdrip.models.TransmitterData;
import com.eveningoutpost.dexdrip.models.Treatments;
import com.eveningoutpost.dexdrip.models.UserError;
import com.eveningoutpost.dexdrip.utilitymodels.AlertPlayer;
import com.eveningoutpost.dexdrip.utilitymodels.BgGraphBuilder;
import com.eveningoutpost.dexdrip.utilitymodels.BgSparklineBuilder;
import com.eveningoutpost.dexdrip.utilitymodels.Pref;
import com.eveningoutpost.dexdrip.utils.PowerStateReceiver;
import com.eveningoutpost.dexdrip.xdrip;
import com.huami.watch.transport.DataBundle;
import com.huami.watch.transport.DataTransportResult;
import com.huami.watch.transport.TransportDataItem;
import com.kieronquinn.library.amazfitcommunication.Transporter;
import com.kieronquinn.library.amazfitcommunication.TransporterClassic;
import java.io.ByteArrayOutputStream;
import java.util.Set;
import org.json.JSONException;
import org.json.JSONObject;

public class Amazfitservice extends Service {
    private static String action;
    private static String alert_to_send;
    private static int default_snooze;
    private ActiveBluetoothDevice activeBluetoothDevice;
    DataBundle dataBundle = new DataBundle();
    private BluetoothAdapter mBluetoothAdapter;
    private BluetoothManager mBluetoothManager;
    private SharedPreferences prefs;
    private Transporter transporter;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        this.prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        TransporterClassic transporterClassic = (TransporterClassic) Transporter.get(getApplicationContext(), "com.eveningoutpost.dexdrip.wearintegration");
        this.transporter = transporterClassic;
        transporterClassic.connectTransportService();
        this.transporter.addChannelListener(new Transporter.ChannelListener() {
            @Override
            public void onChannelChanged(boolean z) {
                if (z) {
                    UserError.Log.e("Amazfitservice", "channel changed - trying automatic resend ");
                }
                Amazfitservice.start("xDrip_synced_SGV_data");
            }
        });
        this.transporter.addDataListener(new Transporter.DataListener() {
            @Override
            public void onDataReceived(TransportDataItem transportDataItem) {
                if (transportDataItem.getAction().equals("SGVDataConfirmation")) {
                    transportDataItem.getData();
                }
                if (transportDataItem.getAction().equals("CancelConfirmation")) {
                    transportDataItem.getData();
                }
                if (transportDataItem.getAction().equals("Amazfit_Remote_Snooze")) {
                    DataBundle data = transportDataItem.getData();
                    UserError.Log.e("Amazfitservice", "Remote SNOOZE recieved for " + data.getInt("snoozetime") + " mins");
                    if (ActiveBgAlert.currentlyAlerting() && data.getInt("snoozetime") > 0) {
                        UserError.Log.e("Amazfitservice", "snoozing all alarms");
                        AlertPlayer.getPlayer().Snooze(xdrip.getAppContext(), data.getInt("snoozetime"), true);
                        data.putString("reply_message", "Snooze accepted by Phone");
                    } else if (ActiveBgAlert.currentlyAlerting()) {
                        AlertPlayer.defaultSnooze();
                        data.putString("reply_message", "Snooze accepted by Phone");
                    } else {
                        UserError.Log.e("Amazfitservice", "No Alarms found to snooze");
                        data.putString("reply_message", "No alert found");
                    }
                }
                if (transportDataItem.getAction().equals("Amazfit_Healthdata")) {
                    DataBundle data2 = transportDataItem.getData();
                    StepCounter.createEfficientRecord(JoH.tsl(), data2.getInt("steps"));
                    HeartRate.create(JoH.tsl(), data2.getInt("heart_rate"), data2.getInt("heart_acuracy"));
                }
                if (transportDataItem.getAction().equals("Amazfit_Treatmentsdata")) {
                    DataBundle data3 = transportDataItem.getData();
                    Treatments.create(data3.getDouble("carbs"), data3.getDouble("insulin"), data3.getLong("timestamp"));
                }
            }
        });
    }

    @Override
    public void onDestroy() {
        Transporter transporter = this.transporter;
        if (transporter != null) {
            transporter.disconnectTransportService();
        }
        UserError.Log.e("Amazfitservice", "killing service ");
        super.onDestroy();
    }

    @Override
    public int onStartCommand(Intent intent, int i, int i2) {
        Transporter.DataSendResultCallback dataSendResultCallback = new Transporter.DataSendResultCallback() {
            @Override
            public void onResultBack(DataTransportResult dataTransportResult) {
                UserError.Log.e("Amazfitservice", dataTransportResult.toString());
            }
        };
        if (!this.transporter.isTransportServiceConnected()) {
            UserError.Log.e("Amazfitservice", "Service not connected - trying to reconnect ");
            this.transporter.connectTransportService();
        }
        if (!this.transporter.isTransportServiceConnected()) {
            UserError.Log.e("Amazfitservice", "Service is not connectable ");
            return 1;
        }
        DataBundle dataBundle = new DataBundle();
        dataBundle.putString("Data", getDatatosend());
        this.transporter.send(getAction(), dataBundle, dataSendResultCallback);
        return 1;
    }

    public static String getAction() {
        return action;
    }

    public String getDatatosend() {
        String str = new String();
        if (action.equals("xDrip_synced_SGV_data")) {
            str = getSGVJSON();
        }
        if (action.equals("xDrip_Alarm")) {
            str = getAlarmJSON();
        }
        if (action.equals("xDrip_Otheralert")) {
            str = getOtheralertJSON();
        }
        return action.equals("xDrip_AlarmCancel") ? getAlarmCancelJSON() : str;
    }

    private String gettransmitterbattery() {
        TransmitterData last = TransmitterData.last();
        if (last == null || last.sensor_battery_level == 0) {
            return "not available";
        }
        if (System.currentTimeMillis() - last.timestamp > 86400000) {
            return "no data in 24 hours";
        }
        String str = "" + last.sensor_battery_level;
        int i = last.sensor_battery_level;
        if (i <= 207) {
            return str + " - very low";
        }
        if (i <= 210) {
            return (str + " - low") + "\n(experimental interpretation)";
        }
        return str + " - ok";
    }

    public String getCurrentDevice() {
        this.activeBluetoothDevice = ActiveBluetoothDevice.first();
        this.mBluetoothManager = (BluetoothManager) getSystemService("bluetooth");
        ActiveBluetoothDevice activeBluetoothDevice = this.activeBluetoothDevice;
        String str = activeBluetoothDevice != null ? activeBluetoothDevice.name : "None Set";
        if (this.prefs.getString("dex_collection_method", "BluetoothWixel").compareTo("DexcomG5") != 0) {
            return str;
        }
        Transmitter transmitter = new Transmitter(this.prefs.getString("dex_txid", "ABCDEF"));
        BluetoothAdapter adapter = this.mBluetoothManager.getAdapter();
        this.mBluetoothAdapter = adapter;
        if (adapter == null) {
            return "No Bluetooth";
        }
        Set<BluetoothDevice> bondedDevices = adapter.getBondedDevices();
        if (bondedDevices == null || bondedDevices.size() <= 0) {
            return str;
        }
        for (BluetoothDevice bluetoothDevice : bondedDevices) {
            if (bluetoothDevice.getName() != null && Extensions.lastTwoCharactersOfString(transmitter.transmitterId).equals(Extensions.lastTwoCharactersOfString(bluetoothDevice.getName()))) {
                str = transmitter.transmitterId;
            }
        }
        return str;
    }

    private String getCollectionMethod() {
        return this.prefs.getString("dex_collection_method", "BluetoothWixel").replace("Dexbridge", "xBridge");
    }

    public String getSGVJSON() {
        String str;
        int i = Pref.getInt("nfc_sensor_age", 0);
        String str2 = Pref.getBooleanDefaultFalse("nfc_age_problem") ? " ⚠⚠⚠" : "";
        double d = JoH.tolerantParseDouble(this.prefs.getString("nfc_expiry_days", "14.5")) - (i / 1440.0d);
        BestGlucose.DisplayGlucose displayGlucose = BestGlucose.getDisplayGlucose();
        try {
            JSONObject jSONObject = new JSONObject();
            jSONObject.put("Collection_info", getCollectionMethod());
            jSONObject.put("hardware_source_info", getCurrentDevice());
            jSONObject.put("sensor.latest_battery_level", gettransmitterbattery());
            StringBuilder sb = new StringBuilder();
            if (d >= BgReading.BESTOFFSET) {
                str = JoH.qs(d, 1) + "d";
            } else {
                str = "EXPIRED! ";
            }
            sb.append(str);
            sb.append(str2);
            jSONObject.put("sensor_expires", sb.toString());
            jSONObject.put("date", displayGlucose.timestamp);
            jSONObject.put("sgv", String.valueOf(displayGlucose.unitized) + String.valueOf(displayGlucose.delta_arrow));
            jSONObject.put("delta", String.valueOf(displayGlucose.spannableString(displayGlucose.unitized_delta)));
            jSONObject.put("ishigh", displayGlucose.isHigh());
            jSONObject.put("islow", displayGlucose.isLow());
            jSONObject.put("isstale", displayGlucose.isStale());
            jSONObject.put("plugin_name", displayGlucose.plugin_name);
            jSONObject.put("phone_battery", String.valueOf(PowerStateReceiver.getBatteryLevel(getApplicationContext())));
            if (Pref.getBoolean("pref_amazfit_widget_graph", false)) {
                jSONObject.put("SGVGraph", BitmaptoString(createWearBitmap(Pref.getStringToInt("amazfit_widget_graph_hours", 4))));
            } else {
                jSONObject.put("SGVGraph", "false");
            }
            if (Pref.getBoolean("pref_amazfit_watchface_graph", false)) {
                jSONObject.put("WFGraph", BitmaptoString(createWFBitmap(Pref.getStringToInt("amazfit_watchface_graph_hours", 4))));
            } else {
                jSONObject.put("WFGraph", "false");
            }
            return jSONObject.toString();
        } catch (NullPointerException | JSONException e) {
            Log.w("AmazfitService", e.toString());
            return "";
        }
    }

    public String getAlarmJSON() {
        BestGlucose.DisplayGlucose displayGlucose = BestGlucose.getDisplayGlucose();
        try {
            JSONObject jSONObject = new JSONObject();
            jSONObject.put("alarmtext", alert_to_send);
            jSONObject.put("date", System.currentTimeMillis());
            jSONObject.put("sgv", String.valueOf(displayGlucose.unitized) + String.valueOf(displayGlucose.delta_arrow));
            jSONObject.put("default_snooze", default_snooze);
            return jSONObject.toString();
        } catch (JSONException e) {
            Log.w("AmazfitService", e.toString());
            return "";
        }
    }

    public String getOtheralertJSON() {
        BestGlucose.DisplayGlucose displayGlucose = BestGlucose.getDisplayGlucose();
        try {
            JSONObject jSONObject = new JSONObject();
            jSONObject.put("alarmtext", alert_to_send);
            jSONObject.put("date", System.currentTimeMillis());
            jSONObject.put("sgv", String.valueOf(displayGlucose.unitized) + String.valueOf(displayGlucose.delta_arrow));
            return jSONObject.toString();
        } catch (JSONException e) {
            Log.w("AmazfitService", e.toString());
            return "";
        }
    }

    public String getAlarmCancelJSON() {
        try {
            JSONObject jSONObject = new JSONObject();
            jSONObject.put("reply_message", "Watch acknowledged CANCEL");
            jSONObject.put("date", System.currentTimeMillis());
            return jSONObject.toString();
        } catch (JSONException e) {
            Log.w("AmazfitService", e.toString());
            return "";
        }
    }

    public static void start(String str, String str2, int i) {
        action = str;
        alert_to_send = str2;
        default_snooze = i;
        JoH.startService(Amazfitservice.class);
    }

    public static void start(String str) {
        action = str;
        JoH.startService(Amazfitservice.class);
    }

    private Bitmap createWearBitmapTinyDots(long j, long j2) {
        return new BgSparklineBuilder(xdrip.getAppContext()).setBgGraphBuilder(new BgGraphBuilder(xdrip.getAppContext())).setStart(j).setEnd(j2).showAxes().setWidthPx(290).setHeightPx(160).setTinyDots().build();
    }

    private Bitmap createWearBitmapSmallDots(long j, long j2) {
        return new BgSparklineBuilder(xdrip.getAppContext()).setBgGraphBuilder(new BgGraphBuilder(xdrip.getAppContext())).setStart(j).setEnd(j2).showAxes().setWidthPx(290).setHeightPx(160).setSmallDots().build();
    }

    private Bitmap createWFBitmapTinyDots(long j, long j2) {
        return new BgSparklineBuilder(xdrip.getAppContext()).setBgGraphBuilder(new BgGraphBuilder(xdrip.getAppContext())).setStart(j).setEnd(j2).setWidthPx(300).setHeightPx(130).setTinyDots().build();
    }

    private Bitmap createWFBitmapSmallDots(long j, long j2) {
        return new BgSparklineBuilder(xdrip.getAppContext()).setBgGraphBuilder(new BgGraphBuilder(xdrip.getAppContext())).setStart(j).setEnd(j2).setWidthPx(300).setHeightPx(130).setSmallDots().build();
    }

    private Bitmap createWearBitmap(long j) {
        if (Pref.getBooleanDefaultFalse("pref_amazfit_widget_graph_dots")) {
            return createWearBitmapTinyDots(System.currentTimeMillis() - (j * 3600000), System.currentTimeMillis());
        }
        return createWearBitmapSmallDots(System.currentTimeMillis() - (j * 3600000), System.currentTimeMillis());
    }

    private Bitmap createWFBitmap(long j) {
        if (Pref.getBooleanDefaultFalse("pref_amazfit_watchface_graph_dots")) {
            return createWFBitmapTinyDots(System.currentTimeMillis() - (j * 3600000), System.currentTimeMillis());
        }
        return createWFBitmapSmallDots(System.currentTimeMillis() - (j * 3600000), System.currentTimeMillis());
    }

    private String BitmaptoString(Bitmap bitmap) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
        return Base64.encodeToString(byteArrayOutputStream.toByteArray(), 0);
    }
}