From 5028458fe8c441de7b6ac6872d6df76d941c8a8a Mon Sep 17 00:00:00 2001 From: cpfeiffer Date: Sun, 16 Sep 2018 13:37:08 +0200 Subject: [PATCH] Fix some static analysis warnings --- .../charts/AbstractWeekChartFragment.java | 10 +-- .../charts/WeekSleepChartFragment.java | 16 ++--- .../charts/WeekStepsChartFragment.java | 14 ++--- .../service/devices/xwatch/XWatchSupport.java | 38 ++++++------ .../devices/zetime/ZeTimeDeviceSupport.java | 61 ++++++++----------- 5 files changed, 63 insertions(+), 76 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/AbstractWeekChartFragment.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/AbstractWeekChartFragment.java index c6b07dee4..5a5ab43f6 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/AbstractWeekChartFragment.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/AbstractWeekChartFragment.java @@ -108,7 +108,7 @@ public abstract class AbstractWeekChartFragment extends AbstractChartFragment { List entries = new ArrayList<>(); ArrayList labels = new ArrayList(); - int balance = 0; + long balance = 0; for (int counter = 0; counter < TOTAL_DAYS; counter++) { ActivityAmounts amounts = getActivityAmountsForDay(db, day, device); @@ -171,7 +171,7 @@ public abstract class AbstractWeekChartFragment extends AbstractChartFragment { set.setValueFormatter(getPieValueFormatter()); } - return new DayData(data, formatPieValue((int) totalValue)); + return new DayData(data, formatPieValue((long) totalValue)); } @Override @@ -321,7 +321,7 @@ public abstract class AbstractWeekChartFragment extends AbstractChartFragment { abstract float[] getTotalsForActivityAmounts(ActivityAmounts activityAmounts); - abstract String formatPieValue(int value); + abstract String formatPieValue(long value); abstract String[] getPieLabels(); @@ -335,9 +335,9 @@ public abstract class AbstractWeekChartFragment extends AbstractChartFragment { abstract String getPieDescription(int targetValue); - protected abstract int calculateBalance(ActivityAmounts amounts); + protected abstract long calculateBalance(ActivityAmounts amounts); - protected abstract String getBalanceMessage(int balance, int targetValue); + protected abstract String getBalanceMessage(long balance, int targetValue); private class WeekChartsData> extends DefaultChartsData { private final String balanceMessage; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/WeekSleepChartFragment.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/WeekSleepChartFragment.java index 1d82cd5c8..fffa5fbf8 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/WeekSleepChartFragment.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/WeekSleepChartFragment.java @@ -60,7 +60,7 @@ public class WeekSleepChartFragment extends AbstractWeekChartFragment { @Override - protected int calculateBalance(ActivityAmounts activityAmounts) { + protected long calculateBalance(ActivityAmounts activityAmounts) { long balance = 0; for (ActivityAmount amount : activityAmounts.getAmounts()) { @@ -72,13 +72,13 @@ public class WeekSleepChartFragment extends AbstractWeekChartFragment { } @Override - protected String getBalanceMessage(int balance, int targetValue) { + protected String getBalanceMessage(long balance, int targetValue) { if (balance > 0) { final long totalBalance = balance - (targetValue * TOTAL_DAYS); if (totalBalance > 0) - return getString(R.string.overslept, getHM((int) totalBalance)); + return getString(R.string.overslept, getHM(totalBalance)); else - return getString(R.string.lack_of_sleep, getHM((int) Math.abs(totalBalance))); + return getString(R.string.lack_of_sleep, getHM(Math.abs(totalBalance))); } else return getString(R.string.no_data); } @@ -100,8 +100,8 @@ public class WeekSleepChartFragment extends AbstractWeekChartFragment { } @Override - protected String formatPieValue(int value) { - return DateTimeUtils.formatDurationHoursMinutes((long) value, TimeUnit.MINUTES); + protected String formatPieValue(long value) { + return DateTimeUtils.formatDurationHoursMinutes(value, TimeUnit.MINUTES); } @Override @@ -114,7 +114,7 @@ public class WeekSleepChartFragment extends AbstractWeekChartFragment { return new IValueFormatter() { @Override public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) { - return formatPieValue((int) value); + return formatPieValue((long) value); } }; } @@ -164,7 +164,7 @@ public class WeekSleepChartFragment extends AbstractWeekChartFragment { chart.getLegend().setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER); } - private String getHM(int value) { + private String getHM(long value) { return DateTimeUtils.formatDurationHoursMinutes(value, TimeUnit.MINUTES); } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/WeekStepsChartFragment.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/WeekStepsChartFragment.java index df298ca24..2291e3665 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/WeekStepsChartFragment.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/WeekStepsChartFragment.java @@ -50,7 +50,7 @@ public class WeekStepsChartFragment extends AbstractWeekChartFragment { @Override float[] getTotalsForActivityAmounts(ActivityAmounts activityAmounts) { - int totalSteps = 0; + long totalSteps = 0; for (ActivityAmount amount : activityAmounts.getAmounts()) { totalSteps += amount.getTotalSteps(); } @@ -58,8 +58,8 @@ public class WeekStepsChartFragment extends AbstractWeekChartFragment { } @Override - protected int calculateBalance(ActivityAmounts activityAmounts) { - int balance = 0; + protected long calculateBalance(ActivityAmounts activityAmounts) { + long balance = 0; for (ActivityAmount amount : activityAmounts.getAmounts()) { balance += amount.getTotalSteps(); } @@ -67,7 +67,7 @@ public class WeekStepsChartFragment extends AbstractWeekChartFragment { } @Override - protected String formatPieValue(int value) { + protected String formatPieValue(long value) { return String.valueOf(value); } @@ -103,13 +103,13 @@ public class WeekStepsChartFragment extends AbstractWeekChartFragment { } @Override - protected String getBalanceMessage(int balance, int targetValue) { + protected String getBalanceMessage(long balance, int targetValue) { if (balance > 0) { final long totalBalance = balance - (targetValue * TOTAL_DAYS); if (totalBalance > 0) - return getString(R.string.overstep, (int) Math.abs(totalBalance)); + return getString(R.string.overstep, Math.abs(totalBalance)); else - return getString(R.string.lack_of_step, (int) Math.abs(totalBalance)); + return getString(R.string.lack_of_step, Math.abs(totalBalance)); } else return getString(R.string.no_data); } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/xwatch/XWatchSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/xwatch/XWatchSupport.java index 4c5ee35f0..d0144995d 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/xwatch/XWatchSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/xwatch/XWatchSupport.java @@ -1,19 +1,19 @@ -/* Copyright (C) 2018 Andreas Shimokawa, ladbsoft - - This file is part of Gadgetbridge. - - Gadgetbridge is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published - by the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - Gadgetbridge is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see . */ +/* Copyright (C) 2018 Andreas Shimokawa, ladbsoft + + This file is part of Gadgetbridge. + + Gadgetbridge is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published + by the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Gadgetbridge is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . */ package nodomain.freeyourgadget.gadgetbridge.service.devices.xwatch; import android.bluetooth.BluetoothGatt; @@ -548,9 +548,9 @@ public class XWatchSupport extends AbstractBTLEDeviceSupport { int yearInt, monthInt, dayInt, hoursMinutesInt = 0; int hours, minutes = 0; - yearInt = Integer.valueOf(String.format("%02x", year, 16)); - monthInt = Integer.valueOf(String.format("%02x", month, 16)); - dayInt = Integer.valueOf(String.format("%02x", day, 16)); + yearInt = Integer.valueOf(String.format("%02x", year), 16); + monthInt = Integer.valueOf(String.format("%02x", month), 16); + dayInt = Integer.valueOf(String.format("%02x", day), 16); hoursMinutesInt = Integer.valueOf(String.format("%02x", hoursminutes), 16); minutes = hoursMinutesInt % 4; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/zetime/ZeTimeDeviceSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/zetime/ZeTimeDeviceSupport.java index bbfee0214..23310194c 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/zetime/ZeTimeDeviceSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/zetime/ZeTimeDeviceSupport.java @@ -200,14 +200,12 @@ public class ZeTimeDeviceSupport extends AbstractBTLEDeviceSupport { music[5] = musicState; System.arraycopy(songtitle.getBytes(StandardCharsets.UTF_8), 0, music, 6, songtitle.getBytes(StandardCharsets.UTF_8).length); music[music.length - 1] = ZeTimeConstants.CMD_END; - if (music != null) { - try { - TransactionBuilder builder = performInitialized("setMusicStateInfo"); - replyMsgToWatch(builder, music); - builder.queue(getQueue()); - } catch (IOException e) { - GB.toast(getContext(), "Error setting music state and info: " + e.getLocalizedMessage(), Toast.LENGTH_LONG, GB.ERROR); - } + try { + TransactionBuilder builder = performInitialized("setMusicStateInfo"); + replyMsgToWatch(builder, music); + builder.queue(getQueue()); + } catch (IOException e) { + GB.toast(getContext(), "Error setting music state and info: " + e.getLocalizedMessage(), Toast.LENGTH_LONG, GB.ERROR); } } } @@ -345,14 +343,12 @@ public class ZeTimeDeviceSupport extends AbstractBTLEDeviceSupport { } System.arraycopy(songtitle.getBytes(StandardCharsets.UTF_8), 0, music, 6, songtitle.getBytes(StandardCharsets.UTF_8).length); music[music.length - 1] = ZeTimeConstants.CMD_END; - if (music != null) { - try { - TransactionBuilder builder = performInitialized("setMusicStateInfo"); - replyMsgToWatch(builder, music); - builder.queue(getQueue()); - } catch (IOException e) { - GB.toast(getContext(), "Error setting music state and info: " + e.getLocalizedMessage(), Toast.LENGTH_LONG, GB.ERROR); - } + try { + TransactionBuilder builder = performInitialized("setMusicStateInfo"); + replyMsgToWatch(builder, music); + builder.queue(getQueue()); + } catch (IOException e) { + GB.toast(getContext(), "Error setting music state and info: " + e.getLocalizedMessage(), Toast.LENGTH_LONG, GB.ERROR); } } } @@ -379,15 +375,12 @@ public class ZeTimeDeviceSupport extends AbstractBTLEDeviceSupport { CalendarEvent[14] = (byte) calendarEventSpec.title.getBytes(StandardCharsets.UTF_8).length; System.arraycopy(calendarEventSpec.title.getBytes(StandardCharsets.UTF_8), 0, CalendarEvent, 15, calendarEventSpec.title.getBytes(StandardCharsets.UTF_8).length); CalendarEvent[CalendarEvent.length-1] = ZeTimeConstants.CMD_END; - if(CalendarEvent != null) - { - try { - TransactionBuilder builder = performInitialized("sendCalendarEvenr"); - sendMsgToWatch(builder, CalendarEvent); - builder.queue(getQueue()); - } catch (IOException e) { - GB.toast(getContext(), "Error sending calendar event: " + e.getLocalizedMessage(), Toast.LENGTH_LONG, GB.ERROR); - } + try { + TransactionBuilder builder = performInitialized("sendCalendarEvenr"); + sendMsgToWatch(builder, CalendarEvent); + builder.queue(getQueue()); + } catch (IOException e) { + GB.toast(getContext(), "Error sending calendar event: " + e.getLocalizedMessage(), Toast.LENGTH_LONG, GB.ERROR); } } @@ -449,15 +442,12 @@ public class ZeTimeDeviceSupport extends AbstractBTLEDeviceSupport { } System.arraycopy(weatherSpec.location.getBytes(StandardCharsets.UTF_8), 0, weather, 25, weatherSpec.location.getBytes(StandardCharsets.UTF_8).length); weather[weather.length-1] = ZeTimeConstants.CMD_END; - if(weather != null) - { - try { - TransactionBuilder builder = performInitialized("sendWeahter"); - sendMsgToWatch(builder, weather); - builder.queue(getQueue()); - } catch (IOException e) { - GB.toast(getContext(), "Error sending weather: " + e.getLocalizedMessage(), Toast.LENGTH_LONG, GB.ERROR); - } + try { + TransactionBuilder builder = performInitialized("sendWeahter"); + sendMsgToWatch(builder, weather); + builder.queue(getQueue()); + } catch (IOException e) { + GB.toast(getContext(), "Error sending weather: " + e.getLocalizedMessage(), Toast.LENGTH_LONG, GB.ERROR); } } @@ -620,8 +610,6 @@ public class ZeTimeDeviceSupport extends AbstractBTLEDeviceSupport { notification[5] = ZeTimeConstants.NOTIFICATION_SOCIAL; break; } - if(notification != null) - { try { TransactionBuilder builder = performInitialized("sendNotification"); sendMsgToWatch(builder, notification); @@ -629,7 +617,6 @@ public class ZeTimeDeviceSupport extends AbstractBTLEDeviceSupport { } catch (IOException e) { GB.toast(getContext(), "Error sending notification: " + e.getLocalizedMessage(), Toast.LENGTH_LONG, GB.ERROR); } - } } @Override