1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-12-01 14:32:54 +01:00

Zepp OS: Cache apps for app management

This commit is contained in:
José Rebelo 2023-06-15 21:59:16 +01:00
parent 853470a2bd
commit 9b7d5eee42
11 changed files with 101 additions and 19 deletions

View File

@ -22,7 +22,7 @@ import android.net.Uri;
import java.io.IOException; import java.io.IOException;
import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.devices.miband.AbstractMiBandFWHelper; import nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiFWHelper;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType; import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.AbstractHuami2021FWInstallHandler; import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.AbstractHuami2021FWInstallHandler;
@ -38,7 +38,7 @@ class AmazfitBand7FWInstallHandler extends AbstractHuami2021FWInstallHandler {
} }
@Override @Override
protected AbstractMiBandFWHelper createHelper(final Uri uri, final Context context) throws IOException { protected HuamiFWHelper createHelper(final Uri uri, final Context context) throws IOException {
return new AmazfitBand7FWHelper(uri, context); return new AmazfitBand7FWHelper(uri, context);
} }

View File

@ -22,7 +22,7 @@ import android.net.Uri;
import java.io.IOException; import java.io.IOException;
import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.devices.miband.AbstractMiBandFWHelper; import nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiFWHelper;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType; import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.AbstractHuami2021FWInstallHandler; import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.AbstractHuami2021FWInstallHandler;
@ -38,7 +38,7 @@ class AmazfitGTR3FWInstallHandler extends AbstractHuami2021FWInstallHandler {
} }
@Override @Override
protected AbstractMiBandFWHelper createHelper(Uri uri, Context context) throws IOException { protected HuamiFWHelper createHelper(Uri uri, Context context) throws IOException {
return new AmazfitGTR3FWHelper(uri, context); return new AmazfitGTR3FWHelper(uri, context);
} }

View File

@ -22,7 +22,7 @@ import android.net.Uri;
import java.io.IOException; import java.io.IOException;
import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.devices.miband.AbstractMiBandFWHelper; import nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiFWHelper;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType; import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.AbstractHuami2021FWInstallHandler; import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.AbstractHuami2021FWInstallHandler;
@ -38,7 +38,7 @@ class AmazfitGTR3ProFWInstallHandler extends AbstractHuami2021FWInstallHandler {
} }
@Override @Override
protected AbstractMiBandFWHelper createHelper(Uri uri, Context context) throws IOException { protected HuamiFWHelper createHelper(Uri uri, Context context) throws IOException {
return new AmazfitGTR3ProFWHelper(uri, context); return new AmazfitGTR3ProFWHelper(uri, context);
} }

View File

@ -22,7 +22,7 @@ import android.net.Uri;
import java.io.IOException; import java.io.IOException;
import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.devices.miband.AbstractMiBandFWHelper; import nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiFWHelper;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType; import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.AbstractHuami2021FWInstallHandler; import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.AbstractHuami2021FWInstallHandler;
@ -38,7 +38,7 @@ class AmazfitGTR4FWInstallHandler extends AbstractHuami2021FWInstallHandler {
} }
@Override @Override
protected AbstractMiBandFWHelper createHelper(final Uri uri, final Context context) throws IOException { protected HuamiFWHelper createHelper(final Uri uri, final Context context) throws IOException {
return new AmazfitGTR4FWHelper(uri, context); return new AmazfitGTR4FWHelper(uri, context);
} }

View File

@ -22,7 +22,7 @@ import android.net.Uri;
import java.io.IOException; import java.io.IOException;
import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.devices.miband.AbstractMiBandFWHelper; import nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiFWHelper;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType; import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.AbstractHuami2021FWInstallHandler; import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.AbstractHuami2021FWInstallHandler;
@ -38,7 +38,7 @@ class AmazfitGTS3FWInstallHandler extends AbstractHuami2021FWInstallHandler {
} }
@Override @Override
protected AbstractMiBandFWHelper createHelper(Uri uri, Context context) throws IOException { protected HuamiFWHelper createHelper(Uri uri, Context context) throws IOException {
return new AmazfitGTS3FWHelper(uri, context); return new AmazfitGTS3FWHelper(uri, context);
} }

View File

@ -22,7 +22,7 @@ import android.net.Uri;
import java.io.IOException; import java.io.IOException;
import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.devices.miband.AbstractMiBandFWHelper; import nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiFWHelper;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType; import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.AbstractHuami2021FWInstallHandler; import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.AbstractHuami2021FWInstallHandler;
@ -38,7 +38,7 @@ class AmazfitGTS4FWInstallHandler extends AbstractHuami2021FWInstallHandler {
} }
@Override @Override
protected AbstractMiBandFWHelper createHelper(final Uri uri, final Context context) throws IOException { protected HuamiFWHelper createHelper(final Uri uri, final Context context) throws IOException {
return new AmazfitGTS4FWHelper(uri, context); return new AmazfitGTS4FWHelper(uri, context);
} }

View File

@ -22,7 +22,7 @@ import android.net.Uri;
import java.io.IOException; import java.io.IOException;
import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.devices.miband.AbstractMiBandFWHelper; import nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiFWHelper;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType; import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.AbstractHuami2021FWInstallHandler; import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.AbstractHuami2021FWInstallHandler;
@ -38,7 +38,7 @@ class AmazfitGTS4MiniFWInstallHandler extends AbstractHuami2021FWInstallHandler
} }
@Override @Override
protected AbstractMiBandFWHelper createHelper(final Uri uri, final Context context) throws IOException { protected HuamiFWHelper createHelper(final Uri uri, final Context context) throws IOException {
return new AmazfitGTS4MiniFWHelper(uri, context); return new AmazfitGTS4MiniFWHelper(uri, context);
} }

View File

@ -22,7 +22,7 @@ import android.net.Uri;
import java.io.IOException; import java.io.IOException;
import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.devices.miband.AbstractMiBandFWHelper; import nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiFWHelper;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType; import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.AbstractHuami2021FWInstallHandler; import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.AbstractHuami2021FWInstallHandler;
@ -38,7 +38,7 @@ class AmazfitTRex2FWInstallHandler extends AbstractHuami2021FWInstallHandler {
} }
@Override @Override
protected AbstractMiBandFWHelper createHelper(final Uri uri, final Context context) throws IOException { protected HuamiFWHelper createHelper(final Uri uri, final Context context) throws IOException {
return new AmazfitTRex2FWHelper(uri, context); return new AmazfitTRex2FWHelper(uri, context);
} }

View File

@ -22,7 +22,7 @@ import android.net.Uri;
import java.io.IOException; import java.io.IOException;
import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.devices.miband.AbstractMiBandFWHelper; import nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiFWHelper;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType; import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.AbstractHuami2021FWInstallHandler; import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.AbstractHuami2021FWInstallHandler;
@ -38,7 +38,7 @@ class MiBand7FWInstallHandler extends AbstractHuami2021FWInstallHandler {
} }
@Override @Override
protected AbstractMiBandFWHelper createHelper(Uri uri, Context context) throws IOException { protected HuamiFWHelper createHelper(Uri uri, Context context) throws IOException {
return new MiBand7FWHelper(uri, context); return new MiBand7FWHelper(uri, context);
} }

View File

@ -36,11 +36,13 @@ import static nodomain.freeyourgadget.gadgetbridge.service.devices.huami.HuamiFi
public abstract class AbstractMiBandFWInstallHandler implements InstallHandler { public abstract class AbstractMiBandFWInstallHandler implements InstallHandler {
private static final Logger LOG = LoggerFactory.getLogger(AbstractMiBandFWInstallHandler.class); private static final Logger LOG = LoggerFactory.getLogger(AbstractMiBandFWInstallHandler.class);
protected final Uri mUri;
protected final Context mContext; protected final Context mContext;
protected AbstractMiBandFWHelper helper; protected AbstractMiBandFWHelper helper;
private String errorMessage; private String errorMessage;
public AbstractMiBandFWInstallHandler(Uri uri, Context context) { public AbstractMiBandFWInstallHandler(Uri uri, Context context) {
mUri = uri;
mContext = context; mContext = context;
try { try {
@ -51,6 +53,10 @@ public abstract class AbstractMiBandFWInstallHandler implements InstallHandler {
} }
} }
public Uri getUri() {
return mUri;
}
public Context getContext() { public Context getContext() {
return mContext; return mContext;
} }

View File

@ -17,21 +17,97 @@
package nodomain.freeyourgadget.gadgetbridge.service.devices.huami; package nodomain.freeyourgadget.gadgetbridge.service.devices.huami;
import android.content.Context; import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri; import android.net.Uri;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator;
import nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiFWHelper;
import nodomain.freeyourgadget.gadgetbridge.devices.miband.AbstractMiBandFWHelper;
import nodomain.freeyourgadget.gadgetbridge.devices.miband.AbstractMiBandFWInstallHandler; import nodomain.freeyourgadget.gadgetbridge.devices.miband.AbstractMiBandFWInstallHandler;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceApp;
import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
import nodomain.freeyourgadget.gadgetbridge.util.FileUtils;
public abstract class AbstractHuami2021FWInstallHandler extends AbstractMiBandFWInstallHandler { public abstract class AbstractHuami2021FWInstallHandler extends AbstractMiBandFWInstallHandler {
private static final Logger LOG = LoggerFactory.getLogger(AbstractHuami2021FWInstallHandler.class);
public AbstractHuami2021FWInstallHandler(final Uri uri, final Context context) { public AbstractHuami2021FWInstallHandler(final Uri uri, final Context context) {
super(uri, context); super(uri, context);
} }
@Override @Override
public void onStartInstall(GBDevice device) { public void onStartInstall(final GBDevice device) {
final AbstractHuamiFirmwareInfo firmwareInfo = getHelper().getFirmwareInfo();
if (firmwareInfo instanceof Huami2021FirmwareInfo) {
saveToCache((Huami2021FirmwareInfo) firmwareInfo, device);
} else {
LOG.warn("firmwareInfo is {} - this should never happen", firmwareInfo.getClass());
}
// Unset the firmware bytes // Unset the firmware bytes
// Huami2021 firmwares are large (> 130MB). With the current architecture, the update operation // Huami2021 firmwares are large (> 130MB). With the current architecture, the update operation
// will re-read them to memory, and we run out-of-memory. // will re-read them to memory, and we run out-of-memory.
helper.unsetFwBytes(); helper.unsetFwBytes();
} }
protected abstract HuamiFWHelper createHelper(Uri uri, Context context) throws IOException;
public HuamiFWHelper getHelper() {
return (HuamiFWHelper) helper;
}
private void saveToCache(final Huami2021FirmwareInfo firmwareInfo, final GBDevice device) {
final DeviceCoordinator coordinator = DeviceHelper.getInstance().getCoordinator(device);
final File appCacheDir;
try {
appCacheDir = coordinator.getAppCacheDir();
} catch (final IOException e) {
LOG.error("Failed to get app cache dir", e);
return ;
}
final GBDeviceApp app = firmwareInfo.getAppInfo();
// write app zip
final File appOutputFile = new File(appCacheDir, app.getUUID().toString() + coordinator.getAppFileExtension());
try {
appCacheDir.mkdirs();
FileUtils.copyURItoFile(getContext(), getUri(), appOutputFile);
} catch (final IOException e) {
LOG.error("Failed to save app to cache", e);
return;
}
// write app metadata
final File metadataOutputFile = new File(appCacheDir, app.getUUID().toString() + ".json");
try (Writer writer = new BufferedWriter(new FileWriter(metadataOutputFile))) {
final JSONObject appJSON = app.getJSON();
writer.write(appJSON.toString());
} catch (final IOException e) {
LOG.error("Failed to write app metadata", e);
return;
}
if (app.getPreviewImage() != null) {
final File previewOutputFile = new File(appCacheDir, app.getUUID().toString() + "_preview.png");
try (FileOutputStream fos = new FileOutputStream(previewOutputFile)) {
app.getPreviewImage().compress(Bitmap.CompressFormat.PNG, 9, fos);
} catch (final IOException e) {
LOG.error("Failed to write app preview", e);
}
}
}
} }