1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-06-23 05:20:35 +02:00

Fix some findbugs findings

This commit is contained in:
cpfeiffer 2017-04-09 01:09:23 +02:00
parent a77ff03ca5
commit 1a88858c6f
13 changed files with 81 additions and 72 deletions

View File

@ -199,8 +199,7 @@ public class AppManagerActivity extends AbstractGBFragmentActivity {
static synchronized ArrayList<UUID> getUuidsFromFile(String filename) { static synchronized ArrayList<UUID> getUuidsFromFile(String filename) {
ArrayList<UUID> uuids = new ArrayList<>(); ArrayList<UUID> uuids = new ArrayList<>();
try { try (FileReader fileReader = new FileReader(FileUtils.getExternalFilesDir() + "/" + filename)) {
FileReader fileReader = new FileReader(FileUtils.getExternalFilesDir() + "/" + filename);
BufferedReader in = new BufferedReader(fileReader); BufferedReader in = new BufferedReader(fileReader);
String line; String line;
while ((line = in.readLine()) != null) { while ((line = in.readLine()) != null) {

View File

@ -133,57 +133,57 @@ public final class HPlusConstants {
public static final Map<Character, Byte> transliterateMap = new HashMap<Character, Byte>(){ public static final Map<Character, Byte> transliterateMap = new HashMap<Character, Byte>(){
{ {
//These are missing //These are missing
put('ó', new Byte((byte) 111)); put('ó', (byte) 111);
put('Ó', new Byte((byte) 79)); put('Ó', (byte) 79);
put('í', new Byte((byte) 105)); put('í', (byte) 105);
put('Í', new Byte((byte) 73)); put('Í', (byte) 73);
put('ú', new Byte((byte) 117)); put('ú', (byte) 117);
put('Ú', new Byte((byte) 85)); put('Ú', (byte) 85);
//These mostly belong to the extended ASCII table //These mostly belong to the extended ASCII table
put('Ç', new Byte((byte) 128)); put('Ç', (byte) 128);
put('ü', new Byte((byte) 129)); put('ü', (byte) 129);
put('é', new Byte((byte) 130)); put('é', (byte) 130);
put('â', new Byte((byte) 131)); put('â', (byte) 131);
put('ä', new Byte((byte) 132)); put('ä', (byte) 132);
put('à', new Byte((byte) 133)); put('à', (byte) 133);
put('ã', new Byte((byte) 134)); put('ã', (byte) 134);
put('ç', new Byte((byte) 135)); put('ç', (byte) 135);
put('ê', new Byte((byte) 136)); put('ê', (byte) 136);
put('ë', new Byte((byte) 137)); put('ë', (byte) 137);
put('è', new Byte((byte) 138)); put('Ï', (byte) 139);
put('Ï', new Byte((byte) 139)); put('è', (byte) 138);
put('Î', new Byte((byte) 140)); put('Î', (byte) 140);
put('Ì', new Byte((byte) 141)); put('Ì', (byte) 141);
put('Ã', new Byte((byte) 142)); put('Ã', (byte) 142);
put('Ä', new Byte((byte) 143)); put('Ä', (byte) 143);
put('É', new Byte((byte) 144)); put('É', (byte) 144);
put('æ', new Byte((byte) 145)); put('æ', (byte) 145);
put('Æ', new Byte((byte) 146)); put('Æ', (byte) 146);
put('ô', new Byte((byte) 147)); put('ô', (byte) 147);
put('ö', new Byte((byte) 148)); put('ö', (byte) 148);
put('ò', new Byte((byte) 149)); put('ò', (byte) 149);
put('û', new Byte((byte) 150)); put('û', (byte) 150);
put('ù', new Byte((byte) 151)); put('ù', (byte) 151);
put('ÿ', new Byte((byte) 152)); put('ÿ', (byte) 152);
put('Ö', new Byte((byte) 153)); put('Ö', (byte) 153);
put('Ü', new Byte((byte) 154)); put('Ü', (byte) 154);
put('¢', new Byte((byte) 155)); put('¢', (byte) 155);
put('£', new Byte((byte) 156)); put('£', (byte) 156);
put('¥', new Byte((byte) 157)); put('¥', (byte) 157);
put('ƒ', new Byte((byte) 159)); put('ƒ', (byte) 159);
put('á', new Byte((byte) 160)); put('á', (byte) 160);
put('ñ', new Byte((byte) 164)); put('ñ', (byte) 164);
put('Ñ', new Byte((byte) 165)); put('Ñ', (byte) 165);
put('ª', new Byte((byte) 166)); put('ª', (byte) 166);
put('º', new Byte((byte) 167)); put('º', (byte) 167);
put('¿', new Byte((byte) 168)); put('¿', (byte) 168);
put('¬', new Byte((byte) 170)); put('¬', (byte) 170);
put('½', new Byte((byte) 171)); put('½', (byte) 171);
put('¼', new Byte((byte) 172)); put('¼', (byte) 172);
put('¡', new Byte((byte) 173)); put('¡', (byte) 173);
put('«', new Byte((byte) 174)); put('«', (byte) 174);
put('»', new Byte((byte) 175)); put('»', (byte) 175);
} }
}; };
} }

View File

@ -62,6 +62,16 @@ public class MusicSpec {
this.duration == musicSpec.duration && this.duration == musicSpec.duration &&
this.trackCount == musicSpec.trackCount && this.trackCount == musicSpec.trackCount &&
this.trackNr == musicSpec.trackNr; this.trackNr == musicSpec.trackNr;
}
@Override
public int hashCode() {
int result = artist != null ? artist.hashCode() : 0;
result = 31 * result + (album != null ? album.hashCode() : 0);
result = 31 * result + (track != null ? track.hashCode() : 0);
result = 31 * result + duration;
result = 31 * result + trackCount;
result = 31 * result + trackNr;
return result;
} }
} }

View File

@ -39,8 +39,8 @@ public enum NotificationType {
GENERIC_ALARM_CLOCK(PebbleIconID.ALARM_CLOCK, PebbleColor.Red); GENERIC_ALARM_CLOCK(PebbleIconID.ALARM_CLOCK, PebbleColor.Red);
// Note: if you add any more constants, update all clients as well // Note: if you add any more constants, update all clients as well
public int icon; public final int icon;
public byte color; public final byte color;
NotificationType(int icon, byte color) { NotificationType(int icon, byte color) {
this.icon = icon; this.icon = icon;

View File

@ -30,7 +30,7 @@ public class ValueDecoder {
int percent = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0); int percent = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0);
if (percent > 100 || percent < 0) { if (percent > 100 || percent < 0) {
LOG.warn("Unexpected percent value: " + percent + ": " + GattCharacteristic.toString(characteristic)); LOG.warn("Unexpected percent value: " + percent + ": " + GattCharacteristic.toString(characteristic));
percent = Math.max(100, Math.min(0, percent)); percent = Math.min(100, Math.max(0, percent));
} }
return percent; return percent;
} }

View File

@ -197,8 +197,6 @@ public class LiveviewIoThread extends GBDeviceIoThread {
break; break;
case HEADER_LEN: case HEADER_LEN:
int headerSize = 0xff & incoming[0]; int headerSize = 0xff & incoming[0];
if (headerSize < 0)
throw new IOException();
state = ReaderState.HEADER; state = ReaderState.HEADER;
incoming = new byte[headerSize]; incoming = new byte[headerSize];
break; break;

View File

@ -810,7 +810,7 @@ public class MiBand2Support extends AbstractBTLEDeviceSupport {
} }
private void handleButtonPressed(byte[] value) { private void handleButtonPressed(byte[] value) {
LOG.info("Button pressed: " + value); LOG.info("Button pressed");
logMessageContent(value); logMessageContent(value);
} }

View File

@ -216,7 +216,7 @@ public class FetchActivityOperation extends AbstractMiBand2Operation {
int len = value.length; int len = value.length;
if (len % 4 != 1) { if (len % 4 != 1) {
throw new AssertionError("Unexpected activity array size: " + value); throw new AssertionError("Unexpected activity array size: " + len);
} }
for (int i = 1; i < len; i+=4) { for (int i = 1; i < len; i+=4) {

View File

@ -69,8 +69,8 @@ class AppMessageHandlerZalewszczak extends AppMessageHandler {
} }
ArrayList<Pair<Integer, Object>> pairs = new ArrayList<>(2); ArrayList<Pair<Integer, Object>> pairs = new ArrayList<>(2);
pairs.add(new Pair<>(KEY_TEMP, (Object) (Math.round(weatherSpec.currentTemp - 273) + "C"))); pairs.add(new Pair<Integer, Object>(KEY_TEMP, weatherSpec.currentTemp - 273 + "C"));
pairs.add(new Pair<>(KEY_ICON, (Object) (getIconForConditionCode(weatherSpec.currentConditionCode)))); pairs.add(new Pair<Integer, Object>(KEY_ICON, getIconForConditionCode(weatherSpec.currentConditionCode)));
byte[] weatherMessage = mPebbleProtocol.encodeApplicationMessagePush(PebbleProtocol.ENDPOINT_APPLICATIONMESSAGE, mUUID, pairs); byte[] weatherMessage = mPebbleProtocol.encodeApplicationMessagePush(PebbleProtocol.ENDPOINT_APPLICATIONMESSAGE, mUUID, pairs);
ByteBuffer buf = ByteBuffer.allocate(weatherMessage.length); ByteBuffer buf = ByteBuffer.allocate(weatherMessage.length);

View File

@ -31,15 +31,9 @@ import nodomain.freeyourgadget.gadgetbridge.util.GB;
class DatalogSessionAnalytics extends DatalogSession { class DatalogSessionAnalytics extends DatalogSession {
private static final Logger LOG = LoggerFactory.getLogger(DatalogSessionAnalytics.class); private static final Logger LOG = LoggerFactory.getLogger(DatalogSessionAnalytics.class);
private GBDeviceEventBatteryInfo mGBDeviceEventBatteryInfo = new GBDeviceEventBatteryInfo(); private GBDeviceEventBatteryInfo mGBDeviceEventBatteryInfo = new GBDeviceEventBatteryInfo();
private GBDevice mGBDevice;
DatalogSessionAnalytics(byte id, UUID uuid, int timestamp, int tag, byte itemType, short itemSize, GBDevice device) { DatalogSessionAnalytics(byte id, UUID uuid, int timestamp, int tag, byte itemType, short itemSize, GBDevice device) {
super(id, uuid, timestamp, tag, itemType, itemSize); super(id, uuid, timestamp, tag, itemType, itemSize);
if (mGBDevice == null || !device.equals(mGBDevice)) { //prevent showing information of other pebble watches when switching devices
mGBDevice = device;
mGBDeviceEventBatteryInfo.state = BatteryState.UNKNOWN;
}
// The default notification should not be too bad (one per hour) but we can override this if needed // The default notification should not be too bad (one per hour) but we can override this if needed
//mGBDevice.setBatteryThresholdPercent((short) 5); //mGBDevice.setBatteryThresholdPercent((short) 5);

View File

@ -71,16 +71,17 @@ public class CheckSums {
} }
} }
public static byte[] readAll(InputStream in, long maxLen) throws IOException { // copy&paste of FileUtils.readAll() to have it free from Android dependencies
private static byte[] readAll(InputStream in, long maxLen) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream(Math.max(8192, in.available())); ByteArrayOutputStream out = new ByteArrayOutputStream(Math.max(8192, in.available()));
byte[] buf = new byte[8192]; byte[] buf = new byte[8192];
int read = 0; int read;
long totalRead = 0; long totalRead = 0;
while ((read = in.read(buf)) > 0) { while ((read = in.read(buf)) > 0) {
out.write(buf, 0, read); out.write(buf, 0, read);
totalRead += read; totalRead += read;
if (totalRead > maxLen) { if (totalRead > maxLen) {
throw new IOException("Too much data to read into memory. Got already " + totalRead + buf); throw new IOException("Too much data to read into memory. Got already " + totalRead);
} }
} }
return out.toByteArray(); return out.toByteArray();

View File

@ -54,7 +54,9 @@ public class FileUtils {
if (!sourceFile.exists()) { if (!sourceFile.exists()) {
throw new IOException("Does not exist: " + sourceFile.getAbsolutePath()); throw new IOException("Does not exist: " + sourceFile.getAbsolutePath());
} }
copyFile(new FileInputStream(sourceFile), new FileOutputStream(destFile)); try (FileInputStream in = new FileInputStream(sourceFile); FileOutputStream out = new FileOutputStream(destFile)) {
copyFile(in, out);
}
} }
private static void copyFile(FileInputStream sourceStream, FileOutputStream destStream) throws IOException { private static void copyFile(FileInputStream sourceStream, FileOutputStream destStream) throws IOException {
@ -229,13 +231,13 @@ public class FileUtils {
public static byte[] readAll(InputStream in, long maxLen) throws IOException { public static byte[] readAll(InputStream in, long maxLen) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream(Math.max(8192, in.available())); ByteArrayOutputStream out = new ByteArrayOutputStream(Math.max(8192, in.available()));
byte[] buf = new byte[8192]; byte[] buf = new byte[8192];
int read = 0; int read;
long totalRead = 0; long totalRead = 0;
while ((read = in.read(buf)) > 0) { while ((read = in.read(buf)) > 0) {
out.write(buf, 0, read); out.write(buf, 0, read);
totalRead += read; totalRead += read;
if (totalRead > maxLen) { if (totalRead > maxLen) {
throw new IOException("Too much data to read into memory. Got already " + totalRead + buf); throw new IOException("Too much data to read into memory. Got already " + totalRead);
} }
} }
return out.toByteArray(); return out.toByteArray();

View File

@ -61,4 +61,9 @@ public class Version implements Comparable<Version> {
return false; return false;
return this.compareTo((Version) that) == 0; return this.compareTo((Version) that) == 0;
} }
@Override
public int hashCode() {
return version.hashCode();
}
} }