Give better feedback when a firmware cannot be installed #234

This commit is contained in:
cpfeiffer 2016-03-22 16:11:55 +01:00
parent 767f359319
commit b0ec74696d
3 changed files with 39 additions and 7 deletions

View File

@ -26,12 +26,13 @@ public class MiBandFWHelper {
private final Uri uri; private final Uri uri;
private final ContentResolver cr; private final ContentResolver cr;
private final /**
@NonNull * The backing firmware info instance, which in general supports the provided
AbstractMiFirmwareInfo firmwareInfo; * given firmware. You must call AbstractMiFirmwareInfo#checkValid() before
private final * attempting to flash it.
@NonNull */
byte[] fw; private final @NonNull AbstractMiFirmwareInfo firmwareInfo;
private final @NonNull byte[] fw;
/** /**
* Provides a different notification API which is also used on Mi1A devices. * Provides a different notification API which is also used on Mi1A devices.
@ -139,6 +140,11 @@ public class MiBandFWHelper {
return AbstractMiFirmwareInfo.determineFirmwareInfoFor(wholeFirmwareBytes); return AbstractMiFirmwareInfo.determineFirmwareInfoFor(wholeFirmwareBytes);
} }
/**
* 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.
*/
public AbstractMiFirmwareInfo getFirmwareInfo() { public AbstractMiFirmwareInfo getFirmwareInfo() {
return firmwareInfo; return firmwareInfo;
} }

View File

@ -47,6 +47,14 @@ public class MiBandFWInstallHandler implements InstallHandler {
return; return;
} }
try {
helper.getFirmwareInfo().checkValid();
} catch (IllegalArgumentException ex) {
installActivity.setInfoText(ex.getLocalizedMessage());
installActivity.setInstallEnabled(false);
return;
}
GenericItem fwItem = new GenericItem(mContext.getString(R.string.miband_installhandler_miband_firmware, helper.getHumanFirmwareVersion())); GenericItem fwItem = new GenericItem(mContext.getString(R.string.miband_installhandler_miband_firmware, helper.getHumanFirmwareVersion()));
fwItem.setIcon(R.drawable.ic_device_miband); fwItem.setIcon(R.drawable.ic_device_miband);

View File

@ -23,7 +23,6 @@ public abstract class AbstractMiFirmwareInfo {
throw new IllegalArgumentException("Unsupported data (maybe not even a firmware?)."); throw new IllegalArgumentException("Unsupported data (maybe not even a firmware?).");
} }
if (candidates.length == 1) { if (candidates.length == 1) {
candidates[0].checkValid();
return candidates[0]; return candidates[0];
} }
throw new IllegalArgumentException("don't know for which device the firmware is, matches multiple devices"); throw new IllegalArgumentException("don't know for which device the firmware is, matches multiple devices");
@ -60,10 +59,24 @@ public abstract class AbstractMiFirmwareInfo {
public abstract int getFirmwareVersion(); public abstract int getFirmwareVersion();
/**
* Returns true if the firmware data is recognized as such and can be
* handled by this instance. No further sanity checks are done at this point.
*/
protected abstract boolean isGenerallySupportedFirmware(); protected abstract boolean isGenerallySupportedFirmware();
/**
* This method checks whether the firmware data is recognized as such and can be handled
* by this instance. It will be called by #isGenerallySupportedFirmware() in order to check
* whether this instance can be used at all or shall be thrown away.
*/
protected abstract boolean isHeaderValid(); protected abstract boolean isHeaderValid();
/**
* Checks whether this instance, with the provided firmware data is compatible with the
* given device. Must be called to avoid installing Mi1 firmware on Mi1A, for example.
* @param device
*/
public abstract boolean isGenerallyCompatibleWith(GBDevice device); public abstract boolean isGenerallyCompatibleWith(GBDevice device);
public @NonNull byte[] getFirmwareBytes() { public @NonNull byte[] getFirmwareBytes() {
@ -80,6 +93,11 @@ public abstract class AbstractMiFirmwareInfo {
public abstract boolean isSingleMiBandFirmware(); public abstract boolean isSingleMiBandFirmware();
/**
* Performs a thorough sanity check of the firmware data and throws IllegalArgumentException
* if there's any problem with it.
* @throws IllegalArgumentException
*/
public void checkValid() throws IllegalArgumentException { public void checkValid() throws IllegalArgumentException {
} }