2021-01-10 23:37:09 +01:00
|
|
|
/* Copyright (C) 2016-2021 Andreas Shimokawa, Carsten Pfeiffer, Petr Vaněk
|
2017-03-10 14:53:19 +01:00
|
|
|
|
|
|
|
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 <http://www.gnu.org/licenses/>. */
|
2016-03-21 23:41:37 +01:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.util;
|
|
|
|
|
2016-12-11 02:10:07 +01:00
|
|
|
import java.util.Collection;
|
|
|
|
|
2016-03-21 23:41:37 +01:00
|
|
|
public class ArrayUtils {
|
|
|
|
/**
|
2016-12-11 23:29:22 +01:00
|
|
|
* Checks the two given arrays for equality, but comparing the second array with a specified
|
|
|
|
* subset of the first array.
|
2016-03-27 17:44:20 +02:00
|
|
|
*
|
2016-12-11 23:29:22 +01:00
|
|
|
* @param first the array in which to look for the second array
|
|
|
|
* @param second the data to look for inside the first array
|
|
|
|
* @param startIndex the start index (inclusive) inside the first array from which to start the comparison
|
|
|
|
* @return whether the second byte array is equal to the specified subset of the first byte array
|
|
|
|
* @throws IllegalArgumentException when one of the arrays is null or start index/length are wrong
|
2016-03-21 23:41:37 +01:00
|
|
|
*/
|
2016-12-11 23:29:22 +01:00
|
|
|
public static boolean equals(byte[] first, byte[] second, int startIndex) {
|
2016-03-21 23:41:37 +01:00
|
|
|
if (first == null) {
|
|
|
|
throw new IllegalArgumentException("first must not be null");
|
|
|
|
}
|
|
|
|
if (second == null) {
|
|
|
|
throw new IllegalArgumentException("second must not be null");
|
|
|
|
}
|
2016-12-11 23:29:22 +01:00
|
|
|
if (startIndex < 0) {
|
|
|
|
throw new IllegalArgumentException("startIndex must be >= 0");
|
2016-03-21 23:41:37 +01:00
|
|
|
}
|
2016-12-11 23:29:22 +01:00
|
|
|
|
|
|
|
if (second.length + startIndex > first.length) {
|
2016-03-21 23:41:37 +01:00
|
|
|
return false;
|
|
|
|
}
|
2016-12-11 23:29:22 +01:00
|
|
|
for (int i = 0; i < second.length; i++) {
|
|
|
|
if (first[startIndex + i] != second[i]) {
|
2016-03-21 23:41:37 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2016-12-11 02:10:07 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a collection of Integer values to an int[] array.
|
|
|
|
* @param values
|
|
|
|
* @return null if the given collection is null, otherwise an array of the same size as the collection
|
|
|
|
* @throws NullPointerException when an element of the collection is null
|
|
|
|
*/
|
|
|
|
public static int[] toIntArray(Collection<Integer> values) {
|
|
|
|
if (values == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
int i = 0;
|
|
|
|
int[] result = new int[values.size()];
|
|
|
|
for (Integer value : values) {
|
|
|
|
result[i++] = value;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2017-03-07 23:20:59 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the given byte array starts with the given values
|
|
|
|
* @param array the array to check
|
|
|
|
* @param values the values which the other array is checked to start with
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public static boolean startsWith(byte[] array, byte[] values) {
|
|
|
|
return equals(array, values, 0);
|
|
|
|
}
|
2021-09-21 16:37:19 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts an array to string representation
|
|
|
|
*
|
|
|
|
* @param array the array to convert
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public static String arrayToString(byte[] array) {
|
|
|
|
StringBuilder stringBuilder = new StringBuilder();
|
|
|
|
for (byte i : array) {
|
|
|
|
stringBuilder.append(String.format("0x%02X ", i));
|
|
|
|
}
|
|
|
|
return stringBuilder.toString();
|
|
|
|
}
|
Garmin Vivomove HR support
- communication protocols
- device support implementation
- download FIT file storage
Features:
- basic connectivity: time sync, battery status, HW/FW version info
- real-time activity tracking
- fitness data sync
- find the device, find the phone
- factory reset
Features implemented but not working:
- notifications: fully implemented, seem to communicate correctly, but not shown on watch
Features implemented partially (not expected to work now):
- weather information (and in future possibly weather alerts)
- music info
- firmware update: only the initial file upload implemented, not used
Things to improve/change:
- Device name hardcoded in `VivomoveHrCoordinator.getSupportedType`, service UUIDs not available
- Download FIT file storage: Should be store (and offer the user to export?) the FIT data forever?
- Obviously, various code improvements, cleanup, etc.
2023-06-15 17:47:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds a value in a byte array
|
|
|
|
* @param value the value to find in the array
|
|
|
|
* @param array the array to search
|
|
|
|
* @return index of the first occurrence of the value in the array, -1 if the array does not contain the value
|
|
|
|
*/
|
|
|
|
public static int indexOf(byte value, byte[] array) {
|
|
|
|
return indexOf(value, array, 0, array.length);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds a value in a byte array
|
|
|
|
* @param value the value to find in the array
|
|
|
|
* @param array the array to search
|
|
|
|
* @param offset initial offset in the array to be searched (0 = start at the beginning)
|
|
|
|
* @param size number of bytes to search beginning at the given offset
|
|
|
|
* @return index of the first occurrence of the value in the array (from the beginning of the array, i.e. not from
|
|
|
|
* the offset position), -1 if the array does not contain the value
|
|
|
|
*/
|
|
|
|
public static int indexOf(byte value, byte[] array, int offset, int size) {
|
|
|
|
for (int i = offset; i < offset + size; ++i) {
|
|
|
|
if (array[i] == value) return i;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
2016-03-21 23:41:37 +01:00
|
|
|
}
|