huawei: Initial HuaweiInstallHandler

* able to check if there is watchface bin insize zip
* calc sha256
* show watchface preview
* no real install for now
This commit is contained in:
Vitaliy Tomin 2024-03-17 22:49:49 +08:00
parent c1e0b1fcd5
commit 054e8e18ee
2 changed files with 133 additions and 1 deletions

View File

@ -0,0 +1,129 @@
package nodomain.freeyourgadget.gadgetbridge.devices.huawei;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import com.google.gson.Gson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.activities.InstallActivity;
import nodomain.freeyourgadget.gadgetbridge.devices.InstallHandler;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
import nodomain.freeyourgadget.gadgetbridge.model.GenericItem;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
import nodomain.freeyourgadget.gadgetbridge.util.GBZipFile;
import nodomain.freeyourgadget.gadgetbridge.util.UriHelper;
import nodomain.freeyourgadget.gadgetbridge.util.ZipFileException;
public class HuaweiInstallHandler implements InstallHandler {
private static final Logger LOG = LoggerFactory.getLogger(HuaweiInstallHandler.class);
private final Context context;
Bitmap previewbMap;
byte[] watchfaceBin;
byte[] watchfaceSHA256;
public HuaweiInstallHandler(Uri uri, Context context) {
this.context = context;
UriHelper uriHelper;
try {
uriHelper = UriHelper.get(uri, this.context);
GBZipFile watchfacePackage = new GBZipFile(uriHelper.openInputStream());
String watchfaceDescription = new String(watchfacePackage.getFileFromZip("description.xml"));
watchfaceBin = watchfacePackage.getFileFromZip("com.huawei.watchface");
final byte[] preview = watchfacePackage.getFileFromZip("preview/cover.jpg");
previewbMap = BitmapFactory.decodeByteArray(preview, 0, preview.length);
} catch (ZipFileException e) {
LOG.error("Unable to read watchface file.", e);
return;
} catch (FileNotFoundException e) {
LOG.error("The watchface file was not found.", e);
return;
} catch (IOException e) {
LOG.error("General IO error occurred.", e);
return;
} catch (Exception e) {
LOG.error("Unknown error occurred.", e);
return;
}
try {
MessageDigest m = MessageDigest.getInstance("SHA256");
m.update(watchfaceBin, 0, watchfaceBin.length);
watchfaceSHA256 = m.digest();
} catch (NoSuchAlgorithmException e) {
LOG.error("Digest alghoritm not found.", e);
return;
}
LOG.info("watchface loaded, SHA256: "+ GB.hexdump(watchfaceSHA256));
}
@Override
public void validateInstallation(InstallActivity installActivity, GBDevice device) {
installActivity.setInstallEnabled(true);
GenericItem installItem = new GenericItem();
if (previewbMap != null) {
installItem.setPreview(previewbMap);
}
if (device.isBusy()) {
LOG.error("Firmware cannot be installed (device busy)");
installActivity.setInfoText("Firmware cannot be installed (device busy)");
installActivity.setInfoText(device.getBusyTask());
installActivity.setInstallEnabled(false);
return;
}
if (device.getType() != DeviceType.HUAWEIBAND7 || !device.isConnected()) { //FIXME: Add all tested huawei devices?
LOG.error("Firmware cannot be installed (not connected or wrong device)");
installActivity.setInfoText("Firmware cannot be installed (not connected or wrong device)");
installActivity.setInstallEnabled(false);
return;
}
if (!isValid()) {
LOG.error("Firmware cannot be installed (not valid)");
installActivity.setInfoText("Firmware cannot be installed (not valid)");
installActivity.setInstallEnabled(false);
}
installItem.setName("Huawei watchface preview");
//installItem.setDetails(getVersion());
installActivity.setInfoText(context.getString(R.string.firmware_install_warning, "(unknown)"));
installActivity.setInstallItem(installItem);
LOG.debug("Initialized HuaweiInstallHandler");
}
@Override
public boolean isValid() {
return true; //FIXME implement real check
}
@Override
public void onStartInstall(GBDevice device) {
}
}

View File

@ -37,6 +37,7 @@ import nodomain.freeyourgadget.gadgetbridge.GBException;
import nodomain.freeyourgadget.gadgetbridge.devices.AbstractBLEDeviceCoordinator;
import nodomain.freeyourgadget.gadgetbridge.devices.InstallHandler;
import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.HuaweiInstallHandler;
import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample;
import nodomain.freeyourgadget.gadgetbridge.entities.BaseActivitySummaryDao;
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
@ -210,7 +211,9 @@ public abstract class HuaweiLECoordinator extends AbstractBLEDeviceCoordinator i
@Override
public InstallHandler findInstallHandler(Uri uri, Context context) {
return null;
HuaweiInstallHandler handler = new HuaweiInstallHandler(uri, context);
return handler.isValid() ? handler : null;
}
@Override