2015-08-03 23:09:49 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.devices.miband;
|
2015-07-23 17:14:51 +02:00
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.net.Uri;
|
2016-03-20 01:05:23 +01:00
|
|
|
import android.support.annotation.NonNull;
|
2015-07-23 17:14:51 +02:00
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import java.io.BufferedInputStream;
|
2015-08-04 23:02:36 +02:00
|
|
|
import java.io.IOException;
|
2015-07-23 17:14:51 +02:00
|
|
|
import java.io.InputStream;
|
|
|
|
|
2016-03-11 01:31:16 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
2015-12-07 01:11:07 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
2016-03-20 01:05:23 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.miband.AbstractMiFirmwareInfo;
|
2015-08-04 23:02:36 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.FileUtils;
|
|
|
|
|
2016-03-11 01:31:16 +01:00
|
|
|
/**
|
2016-03-20 01:05:23 +01:00
|
|
|
* Also see Mi1SFirmwareInfo.
|
2016-03-11 01:31:16 +01:00
|
|
|
*/
|
2015-07-23 17:14:51 +02:00
|
|
|
public class MiBandFWHelper {
|
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(MiBandFWHelper.class);
|
|
|
|
|
2016-03-22 16:11:55 +01:00
|
|
|
/**
|
|
|
|
* The backing firmware info instance, which in general supports the provided
|
|
|
|
* given firmware. You must call AbstractMiFirmwareInfo#checkValid() before
|
|
|
|
* attempting to flash it.
|
|
|
|
*/
|
2016-03-22 21:55:15 +01:00
|
|
|
@NonNull
|
|
|
|
private final AbstractMiFirmwareInfo firmwareInfo;
|
|
|
|
@NonNull
|
|
|
|
private final byte[] fw;
|
2015-07-23 17:14:51 +02:00
|
|
|
|
2015-10-22 00:53:27 +02:00
|
|
|
/**
|
|
|
|
* Provides a different notification API which is also used on Mi1A devices.
|
|
|
|
*/
|
|
|
|
public static final int FW_16779790 = 16779790;
|
|
|
|
|
2015-07-23 17:14:51 +02:00
|
|
|
private final int[] whitelistedFirmwareVersion = {
|
2015-07-28 17:30:20 +02:00
|
|
|
16779534, // 1.0.9.14 tested by developer
|
2015-07-28 22:03:53 +02:00
|
|
|
16779547, //1.0.9.27 tested by developer
|
2015-08-30 00:21:51 +02:00
|
|
|
16779568, //1.0.9.48 tested by developer
|
|
|
|
16779585, //1.0.9.65 tested by developer
|
2015-09-17 16:03:15 +02:00
|
|
|
16779779, //1.0.10.3 reported on the wiki
|
2015-12-07 01:22:27 +01:00
|
|
|
16779782, //1.0.10.6 reported on the wiki
|
2015-09-17 16:03:15 +02:00
|
|
|
16779787, //1.0.10.11 tested by developer
|
2015-10-22 00:53:27 +02:00
|
|
|
//FW_16779790, //1.0.10.14 reported on the wiki (vibration does not work currently)
|
2016-08-04 00:05:01 +02:00
|
|
|
68094986, // 4.15.12.10 tested by developer
|
2016-07-08 22:17:19 +02:00
|
|
|
68158215, // 4.16.3.7 tested by developer
|
2016-10-05 23:17:02 +02:00
|
|
|
68158486, // 4.16.4.22 tested by developer and user
|
2015-12-07 01:22:27 +01:00
|
|
|
84870926, // 5.15.7.14 tested by developer
|
2015-07-23 17:14:51 +02:00
|
|
|
};
|
|
|
|
|
2015-08-04 23:02:36 +02:00
|
|
|
public MiBandFWHelper(Uri uri, Context context) throws IOException {
|
2015-10-06 16:56:01 +02:00
|
|
|
String pebblePattern = ".*\\.(pbw|pbz|pbl)";
|
2015-08-31 17:40:46 +02:00
|
|
|
if (uri.getPath().matches(pebblePattern)) {
|
|
|
|
throw new IOException("Firmware has a filename that looks like a Pebble app/firmware.");
|
|
|
|
}
|
|
|
|
|
2016-03-23 22:06:48 +01:00
|
|
|
try (InputStream in = new BufferedInputStream(context.getContentResolver().openInputStream(uri))) {
|
2015-08-04 23:02:36 +02:00
|
|
|
this.fw = FileUtils.readAll(in, 1024 * 1024); // 1 MB
|
2016-03-20 01:05:23 +01:00
|
|
|
this.firmwareInfo = determineFirmwareInfoFor(fw);
|
2015-12-07 01:11:07 +01:00
|
|
|
} catch (IOException ex) {
|
|
|
|
throw ex; // pass through
|
2016-03-20 01:05:23 +01:00
|
|
|
} catch (IllegalArgumentException ex) {
|
|
|
|
throw new IOException("This doesn't seem to be a Mi Band firmware: " + ex.getLocalizedMessage(), ex);
|
2015-07-23 17:14:51 +02:00
|
|
|
} catch (Exception e) {
|
2015-08-04 23:02:36 +02:00
|
|
|
throw new IOException("Error reading firmware file: " + uri.toString(), e);
|
2015-07-23 17:14:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-20 01:05:23 +01:00
|
|
|
public int getFirmwareVersion() {
|
|
|
|
// FIXME: UnsupportedOperationException!
|
|
|
|
return firmwareInfo.getFirst().getFirmwareVersion();
|
2015-12-07 01:11:07 +01:00
|
|
|
}
|
|
|
|
|
2016-03-20 01:05:23 +01:00
|
|
|
public int getFirmware2Version() {
|
|
|
|
return firmwareInfo.getFirst().getFirmwareVersion();
|
2015-07-23 17:14:51 +02:00
|
|
|
}
|
|
|
|
|
2016-03-11 01:31:16 +01:00
|
|
|
public static String formatFirmwareVersion(int version) {
|
|
|
|
if (version == -1)
|
|
|
|
return GBApplication.getContext().getString(R.string._unknown_);
|
|
|
|
|
|
|
|
return String.format("%d.%d.%d.%d",
|
|
|
|
version >> 24 & 255,
|
|
|
|
version >> 16 & 255,
|
|
|
|
version >> 8 & 255,
|
|
|
|
version & 255);
|
|
|
|
}
|
|
|
|
|
2015-07-23 17:14:51 +02:00
|
|
|
public String getHumanFirmwareVersion() {
|
2016-03-20 01:05:23 +01:00
|
|
|
return format(getFirmwareVersion());
|
2015-07-23 17:14:51 +02:00
|
|
|
}
|
|
|
|
|
2016-03-11 01:31:16 +01:00
|
|
|
public String getHumanFirmwareVersion2() {
|
2016-03-20 01:05:23 +01:00
|
|
|
return format(firmwareInfo.getSecond().getFirmwareVersion());
|
2016-03-11 01:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public String format(int version) {
|
|
|
|
return formatFirmwareVersion(version);
|
|
|
|
}
|
|
|
|
|
2016-03-22 21:55:15 +01:00
|
|
|
@NonNull
|
2015-07-23 17:14:51 +02:00
|
|
|
public byte[] getFw() {
|
|
|
|
return fw;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isFirmwareWhitelisted() {
|
|
|
|
for (int wlf : whitelistedFirmwareVersion) {
|
|
|
|
if (wlf == getFirmwareVersion()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2015-12-07 01:11:07 +01:00
|
|
|
|
|
|
|
public boolean isFirmwareGenerallyCompatibleWith(GBDevice device) {
|
2016-03-20 01:05:23 +01:00
|
|
|
return firmwareInfo.isGenerallyCompatibleWith(device);
|
2015-12-07 01:11:07 +01:00
|
|
|
}
|
2016-03-11 01:31:16 +01:00
|
|
|
|
|
|
|
public boolean isSingleFirmware() {
|
2016-03-20 01:05:23 +01:00
|
|
|
return firmwareInfo.isSingleMiBandFirmware();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param wholeFirmwareBytes
|
|
|
|
* @return
|
|
|
|
* @throws IllegalArgumentException when the data is not recognized as firmware data
|
|
|
|
*/
|
2016-03-20 15:00:05 +01:00
|
|
|
public static
|
|
|
|
@NonNull
|
|
|
|
AbstractMiFirmwareInfo determineFirmwareInfoFor(byte[] wholeFirmwareBytes) {
|
2016-03-20 01:05:23 +01:00
|
|
|
return AbstractMiFirmwareInfo.determineFirmwareInfoFor(wholeFirmwareBytes);
|
|
|
|
}
|
|
|
|
|
2016-03-22 16:11:55 +01:00
|
|
|
/**
|
|
|
|
* The backing firmware info instance, which in general supports the provided
|
|
|
|
* given firmware. You MUST call AbstractMiFirmwareInfo#checkValid() AND
|
|
|
|
* isGenerallyCompatibleWithDevice() before attempting to flash it.
|
|
|
|
*/
|
2016-03-22 21:55:15 +01:00
|
|
|
@NonNull
|
2016-03-20 01:05:23 +01:00
|
|
|
public AbstractMiFirmwareInfo getFirmwareInfo() {
|
|
|
|
return firmwareInfo;
|
2016-03-11 01:31:16 +01:00
|
|
|
}
|
2015-07-28 17:30:20 +02:00
|
|
|
}
|