Search in sources :

Example 1 with BatteryRead

use of com.edotasx.amazfit.db.model.BatteryRead in project AmazMod by edotassi.

the class BatteryChartCard method updateChart.

private void updateChart() {
    final List<Entry> yValues = new ArrayList<Entry>();
    final List<Integer> colors = new ArrayList<>();
    final int days = PreferenceManager.getInt(getContext(), Constants.PREFERENCE_BATTERY_CHART_RANGE, 5);
    Calendar calendar = Calendar.getInstance();
    long highX = calendar.getTimeInMillis();
    calendar.add(Calendar.DATE, -1 * days);
    long lowX = calendar.getTimeInMillis();
    List<BatteryRead> batteryReadList = SQLite.select().from(BatteryRead.class).where(BatteryRead_Table.date.greaterThan(lowX)).queryList();
    BatteryRead prevRead = null;
    for (int i = 0; i < batteryReadList.size(); i++) {
        BatteryRead read = batteryReadList.get(i);
        if ((read.getLevel() > 0) && ((prevRead == null) || (read.getLevel() != prevRead.getLevel()))) {
            Entry entry = new Entry(read.getDate(), read.getLevel());
            yValues.add(entry);
            colors.add(Color.parseColor(read.isCharging() ? "#00E676" : "#3F51B5"));
        }
        prevRead = read;
    }
    if (yValues.size() == 0) {
        return;
    }
    LineDataSet lineDataSet = new LineDataSet(yValues, "Battery");
    lineDataSet.setLineWidth(1.5f);
    lineDataSet.setDrawCircleHole(false);
    lineDataSet.setDrawCircles(false);
    lineDataSet.setDrawValues(false);
    Drawable drawable = ContextCompat.getDrawable(getContext(), R.drawable.fade_blue_battery);
    lineDataSet.setDrawFilled(true);
    lineDataSet.setAxisDependency(YAxis.AxisDependency.LEFT);
    lineDataSet.setFillDrawable(drawable);
    lineDataSet.setColors(colors);
    lineDataSet.setMode(LineDataSet.Mode.LINEAR);
    lineDataSet.setCubicIntensity(0.05f);
    Description description = new Description();
    description.setText("");
    chart.setDescription(description);
    XAxis xAxis = chart.getXAxis();
    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
    xAxis.setDrawGridLines(false);
    xAxis.setLabelRotationAngle(-45);
    xAxis.setTextSize(8);
    xAxis.setAxisMinimum(lowX);
    xAxis.setAxisMaximum(highX);
    final Calendar now = Calendar.getInstance();
    final SimpleDateFormat simpleDateFormatHours = new SimpleDateFormat("HH");
    final SimpleDateFormat simpleDateFormatHoursMinutes = new SimpleDateFormat("HH:mm");
    final SimpleDateFormat simpleDateFormatDateMonth = new SimpleDateFormat("dd/MM");
    xAxis.setValueFormatter(new IAxisValueFormatter() {

        @Override
        public String getFormattedValue(float value, AxisBase axis) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis((long) value);
            Date date = calendar.getTime();
            if ((days > 1) || (now.get(Calendar.DATE) != calendar.get(Calendar.DATE))) {
                int minutes = calendar.get(Calendar.MINUTE);
                if (minutes > 30) {
                    calendar.add(Calendar.HOUR, 1);
                }
                return simpleDateFormatHours.format(date) + "\n" + simpleDateFormatDateMonth.format(date);
            } else {
                return simpleDateFormatHoursMinutes.format(calendar.getTime()) + "\n" + simpleDateFormatDateMonth.format(calendar.getTime());
            }
        }
    });
    YAxis leftAxis = chart.getAxisLeft();
    leftAxis.setDrawAxisLine(true);
    leftAxis.setDrawZeroLine(false);
    leftAxis.setDrawGridLines(true);
    leftAxis.setAxisMinimum(0);
    leftAxis.setAxisMaximum(100);
    chart.getAxisRight().setEnabled(false);
    chart.getLegend().setEnabled(false);
    chart.setTouchEnabled(false);
    chart.setXAxisRenderer(new CustomXAxisRenderer(chart.getViewPortHandler(), chart.getXAxis(), chart.getTransformer(YAxis.AxisDependency.LEFT)));
    LineData lineData = new LineData(lineDataSet);
    chart.setData(lineData);
    chart.invalidate();
}
Also used : BatteryRead(com.edotasx.amazfit.db.model.BatteryRead) Description(com.github.mikephil.charting.components.Description) LineDataSet(com.github.mikephil.charting.data.LineDataSet) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) Drawable(android.graphics.drawable.Drawable) IAxisValueFormatter(com.github.mikephil.charting.formatter.IAxisValueFormatter) AxisBase(com.github.mikephil.charting.components.AxisBase) XAxis(com.github.mikephil.charting.components.XAxis) Date(java.util.Date) Entry(com.github.mikephil.charting.data.Entry) LineData(com.github.mikephil.charting.data.LineData) SimpleDateFormat(java.text.SimpleDateFormat) YAxis(com.github.mikephil.charting.components.YAxis)

Example 2 with BatteryRead

use of com.edotasx.amazfit.db.model.BatteryRead in project AmazMod by edotassi.

the class NotificationManager method updateBatteryLevel.

private void updateBatteryLevel() {
    if (PreferenceManager.getBoolean(context, Constants.PREFERENCE_DISABLE_BATTERY_CHART, false)) {
        return;
    }
    int intervalWithNotifications = 1000 * 60 * PreferenceManager.getInt(context, Constants.PREFERENCE_BATTERY_SYNC_WITH_NOTIFICATIONS, 5);
    if ((lastNotificationTime == -1) || (System.currentTimeMillis() - lastNotificationTime) > intervalWithNotifications) {
        Log.d("BatteryAlarm", "notification trigger");
        if (transporter == null) {
            transporter = Transporter.get(context, "com.huami.watch.companion");
            dataListener = new Transporter.DataListener() {

                @Override
                public void onDataReceived(TransportDataItem transportDataItem) {
                    String action = transportDataItem.getAction();
                    Log.d("BatteryAlarm", "action: " + action);
                    if ("com.huami.watch.companion.transport.SyncBattery".equals(action)) {
                        BatteryInfo batteryInfo = BatteryInfoHelper.getBatteryInfo(transportDataItem.getData());
                        Log.d("BatteryAlarm", "batteryLvl: " + batteryInfo.getBatteryLevel());
                        FlowManager.init(context);
                        BatteryRead batteryRead = new BatteryRead();
                        batteryRead.setDate(System.currentTimeMillis());
                        batteryRead.setLevel(batteryInfo.getBatteryLevel());
                        batteryRead.setCharging(batteryInfo.isBatteryCharging());
                        FlowManager.getModelAdapter(BatteryRead.class).insert(batteryRead);
                        transporter.removeDataListener(dataListener);
                    }
                }
            };
            transporter.addDataListener(dataListener);
            transporter.connectTransportService();
        }
        SyncUtil.syncRequestBattery(transporter, true);
    }
}
Also used : BatteryRead(com.edotasx.amazfit.db.model.BatteryRead) BatteryInfo(com.huami.watch.companion.battery.bean.BatteryInfo) TransportDataItem(com.huami.watch.transport.TransportDataItem) Transporter(com.huami.watch.transport.Transporter)

Example 3 with BatteryRead

use of com.edotasx.amazfit.db.model.BatteryRead in project AmazMod by edotassi.

the class BatteryStatsReceiver method onReceive.

@Override
public void onReceive(final Context context, Intent intent) {
    Log.d("BatteryAlarm", "onReceive");
    if (transporter == null) {
        transporter = Transporter.get(context, "com.huami.watch.companion");
        dataListener = new Transporter.DataListener() {

            @Override
            public void onDataReceived(TransportDataItem transportDataItem) {
                String action = transportDataItem.getAction();
                Log.d("BatteryAlarm", "action: " + action);
                if ("com.huami.watch.companion.transport.SyncBattery".equals(action)) {
                    BatteryInfo batteryInfo = BatteryInfoHelper.getBatteryInfo(transportDataItem.getData());
                    Log.d("BatteryAlarm", "batteryLvl: " + batteryInfo.getBatteryLevel());
                    FlowManager.init(context);
                    BatteryRead batteryRead = new BatteryRead();
                    batteryRead.setDate(System.currentTimeMillis());
                    batteryRead.setLevel(batteryInfo.getBatteryLevel());
                    batteryRead.setCharging(batteryInfo.isBatteryCharging());
                    FlowManager.getModelAdapter(BatteryRead.class).insert(batteryRead);
                    transporter.removeDataListener(dataListener);
                }
            }
        };
        transporter.addDataListener(dataListener);
    }
    SyncUtil.syncRequestBattery(transporter, true);
}
Also used : BatteryRead(com.edotasx.amazfit.db.model.BatteryRead) BatteryInfo(com.huami.watch.companion.battery.bean.BatteryInfo) TransportDataItem(com.huami.watch.transport.TransportDataItem) Transporter(com.huami.watch.transport.Transporter)

Example 4 with BatteryRead

use of com.edotasx.amazfit.db.model.BatteryRead in project AmazMod by edotassi.

the class BatteryCard method updateBatteryViews.

@DexWrap
public void updateBatteryViews(BatteryInfo batteryInfo) {
    updateBatteryViews(batteryInfo);
    BatteryRead batteryRead = new BatteryRead();
    batteryRead.setDate(System.currentTimeMillis());
    batteryRead.setLevel(batteryInfo.getBatteryLevel());
    batteryRead.setCharging(batteryInfo.isBatteryCharging());
    FlowManager.getModelAdapter(BatteryRead.class).insert(batteryRead);
    Log.d("BatteryChart", "level: " + batteryInfo.getBatteryLevel());
    RxBus.get().post(new BatteryHistoryUpdatedEvent());
}
Also used : BatteryRead(com.edotasx.amazfit.db.model.BatteryRead) BatteryHistoryUpdatedEvent(com.edotasx.amazfit.events.BatteryHistoryUpdatedEvent) DexWrap(lanchon.dexpatcher.annotation.DexWrap)

Aggregations

BatteryRead (com.edotasx.amazfit.db.model.BatteryRead)4 BatteryInfo (com.huami.watch.companion.battery.bean.BatteryInfo)2 TransportDataItem (com.huami.watch.transport.TransportDataItem)2 Transporter (com.huami.watch.transport.Transporter)2 Drawable (android.graphics.drawable.Drawable)1 BatteryHistoryUpdatedEvent (com.edotasx.amazfit.events.BatteryHistoryUpdatedEvent)1 AxisBase (com.github.mikephil.charting.components.AxisBase)1 Description (com.github.mikephil.charting.components.Description)1 XAxis (com.github.mikephil.charting.components.XAxis)1 YAxis (com.github.mikephil.charting.components.YAxis)1 Entry (com.github.mikephil.charting.data.Entry)1 LineData (com.github.mikephil.charting.data.LineData)1 LineDataSet (com.github.mikephil.charting.data.LineDataSet)1 IAxisValueFormatter (com.github.mikephil.charting.formatter.IAxisValueFormatter)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 DexWrap (lanchon.dexpatcher.annotation.DexWrap)1