mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2024-06-16 10:00:08 +02:00
Compare commits
14 Commits
01b457895a
...
164c5e52a4
Author | SHA1 | Date | |
---|---|---|---|
|
164c5e52a4 | ||
|
4c14dd5f72 | ||
|
e75f80c3f9 | ||
|
15803eedea | ||
|
a0782d318b | ||
|
ffcb67636e | ||
|
db37222171 | ||
|
96f87cf913 | ||
|
c7841b4947 | ||
|
56d087da2f | ||
|
e7bd1620fe | ||
|
81ef7698f6 | ||
|
ee7b76517a | ||
|
ce18a5a6f8 |
|
@ -43,7 +43,7 @@ public class GBDaoGenerator {
|
|||
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
final Schema schema = new Schema(44, MAIN_PACKAGE + ".entities");
|
||||
final Schema schema = new Schema(45, MAIN_PACKAGE + ".entities");
|
||||
|
||||
Entity userAttributes = addUserAttributes(schema);
|
||||
Entity user = addUserInfo(schema, userAttributes);
|
||||
|
@ -591,6 +591,8 @@ public class GBDaoGenerator {
|
|||
indexUnique.makeUnique();
|
||||
worldClock.addIndex(indexUnique);
|
||||
worldClock.addStringProperty("label").notNull();
|
||||
worldClock.addBooleanProperty("enabled");
|
||||
worldClock.addStringProperty("code");
|
||||
worldClock.addStringProperty("timeZoneId").notNull();
|
||||
worldClock.addToOne(user, userId);
|
||||
worldClock.addToOne(device, deviceId);
|
||||
|
|
|
@ -53,6 +53,7 @@ import nodomain.freeyourgadget.gadgetbridge.entities.User;
|
|||
import nodomain.freeyourgadget.gadgetbridge.entities.WorldClock;
|
||||
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.StringUtils;
|
||||
|
||||
|
||||
public class ConfigureWorldClocks extends AbstractGBActivity {
|
||||
|
@ -165,9 +166,11 @@ public class ConfigureWorldClocks extends AbstractGBActivity {
|
|||
private WorldClock createDefaultWorldClock(@NonNull Device device, @NonNull User user) {
|
||||
final WorldClock worldClock = new WorldClock();
|
||||
final String timezone = TimeZone.getDefault().getID();
|
||||
worldClock.setEnabled(true);
|
||||
worldClock.setTimeZoneId(timezone);
|
||||
final String[] timezoneParts = timezone.split("/");
|
||||
worldClock.setLabel(timezoneParts[timezoneParts.length - 1]);
|
||||
worldClock.setCode(StringUtils.truncate(timezoneParts[timezoneParts.length - 1], 3).toUpperCase(Locale.getDefault()));
|
||||
|
||||
worldClock.setDeviceId(device.getId());
|
||||
worldClock.setUserId(user.getId());
|
||||
|
|
|
@ -25,6 +25,7 @@ import android.text.TextWatcher;
|
|||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.CheckBox;
|
||||
import android.widget.EditText;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
@ -43,6 +44,7 @@ import nodomain.freeyourgadget.gadgetbridge.entities.WorldClock;
|
|||
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.StringUtils;
|
||||
|
||||
public class WorldClockDetails extends AbstractGBActivity {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(WorldClockDetails.class);
|
||||
|
@ -54,6 +56,9 @@ public class WorldClockDetails extends AbstractGBActivity {
|
|||
|
||||
TextView worldClockTimezone;
|
||||
EditText worldClockLabel;
|
||||
EditText worldClockCode;
|
||||
View worldClockEnabledCard;
|
||||
CheckBox worldClockEnabled;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
|
@ -68,8 +73,11 @@ public class WorldClockDetails extends AbstractGBActivity {
|
|||
return;
|
||||
}
|
||||
|
||||
worldClockEnabledCard = findViewById(R.id.card_enabled);
|
||||
worldClockEnabled = findViewById(R.id.world_clock_enabled);
|
||||
worldClockTimezone = findViewById(R.id.world_clock_timezone);
|
||||
worldClockLabel = findViewById(R.id.world_clock_label);
|
||||
worldClockCode = findViewById(R.id.world_clock_code);
|
||||
|
||||
device = getIntent().getParcelableExtra(GBDevice.EXTRA_DEVICE);
|
||||
final DeviceCoordinator coordinator = DeviceHelper.getInstance().getCoordinator(device);
|
||||
|
@ -91,6 +99,14 @@ public class WorldClockDetails extends AbstractGBActivity {
|
|||
}
|
||||
});
|
||||
|
||||
if (coordinator.supportsDisabledWorldClocks()) {
|
||||
worldClockEnabled.setOnCheckedChangeListener((buttonView, isChecked) -> {
|
||||
worldClock.setEnabled(isChecked);
|
||||
});
|
||||
} else {
|
||||
worldClockEnabledCard.setVisibility(View.GONE);
|
||||
}
|
||||
|
||||
worldClockLabel.setFilters(new InputFilter[]{new InputFilter.LengthFilter(coordinator.getWorldClocksLabelLength())});
|
||||
worldClockLabel.addTextChangedListener(new TextWatcher() {
|
||||
@Override
|
||||
|
@ -107,6 +123,22 @@ public class WorldClockDetails extends AbstractGBActivity {
|
|||
}
|
||||
});
|
||||
|
||||
worldClockCode.setFilters(new InputFilter[]{new InputFilter.LengthFilter(3)});
|
||||
worldClockCode.addTextChangedListener(new TextWatcher() {
|
||||
@Override
|
||||
public void beforeTextChanged(final CharSequence s, int start, int count, int after) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTextChanged(final CharSequence s, int start, int before, int count) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterTextChanged(final Editable s) {
|
||||
worldClock.setCode(s.toString());
|
||||
}
|
||||
});
|
||||
|
||||
final FloatingActionButton fab = findViewById(R.id.fab_save);
|
||||
fab.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
|
@ -150,25 +182,37 @@ public class WorldClockDetails extends AbstractGBActivity {
|
|||
}
|
||||
|
||||
public void updateUiFromWorldClock() {
|
||||
final DeviceCoordinator coordinator = DeviceHelper.getInstance().getCoordinator(device);
|
||||
final int maxLabelLength = coordinator.getWorldClocksLabelLength();
|
||||
|
||||
worldClockEnabled.setChecked(worldClock.getEnabled() == null || worldClock.getEnabled());
|
||||
|
||||
final String oldTimezone = worldClockTimezone.getText().toString();
|
||||
|
||||
worldClockTimezone.setText(worldClock.getTimeZoneId());
|
||||
|
||||
// Check if the label was still the default (the timezone city name)
|
||||
// If so, and if the user changed the timezone, update the label to match the new city name
|
||||
if (!oldTimezone.equals(worldClock.getTimeZoneId())) {
|
||||
if (!StringUtils.isNullOrEmpty(oldTimezone) && !oldTimezone.equals(worldClock.getTimeZoneId())) {
|
||||
final String[] oldTimezoneParts = oldTimezone.split("/");
|
||||
final String[] newTimezoneParts = worldClock.getTimeZoneId().split("/");
|
||||
final String newLabel = newTimezoneParts[newTimezoneParts.length - 1];
|
||||
final String oldLabel = oldTimezoneParts[oldTimezoneParts.length - 1];
|
||||
final String newLabel = StringUtils.truncate(newTimezoneParts[newTimezoneParts.length - 1], maxLabelLength);
|
||||
final String oldLabel = StringUtils.truncate(oldTimezoneParts[oldTimezoneParts.length - 1], maxLabelLength);
|
||||
final String userLabel = worldClockLabel.getText().toString();
|
||||
|
||||
if (userLabel.equals(oldLabel)) {
|
||||
if (StringUtils.isNullOrEmpty(userLabel) || userLabel.equals(oldLabel)) {
|
||||
// The label was still the original, so let's override it with the new city
|
||||
worldClock.setLabel(newLabel);
|
||||
}
|
||||
final String newCode = StringUtils.truncate(newLabel, 3).toUpperCase();
|
||||
final String oldCode = StringUtils.truncate(oldLabel, 3).toUpperCase();
|
||||
final String userCode = worldClockCode.getText().toString();
|
||||
if (StringUtils.isNullOrEmpty(userCode) || userCode.equals(oldCode)) {
|
||||
// The code was still the original, so let's override it with the new one
|
||||
worldClock.setCode(newCode);
|
||||
}
|
||||
}
|
||||
|
||||
worldClockLabel.setText(worldClock.getLabel());
|
||||
worldClockCode.setText(worldClock.getCode());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,44 @@
|
|||
/* Copyright (C) 2022 José Rebelo
|
||||
|
||||
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/>. */
|
||||
package nodomain.freeyourgadget.gadgetbridge.database.schema;
|
||||
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
|
||||
import nodomain.freeyourgadget.gadgetbridge.database.DBHelper;
|
||||
import nodomain.freeyourgadget.gadgetbridge.database.DBUpdateScript;
|
||||
import nodomain.freeyourgadget.gadgetbridge.entities.WorldClockDao;
|
||||
|
||||
public class GadgetbridgeUpdate_45 implements DBUpdateScript {
|
||||
@Override
|
||||
public void upgradeSchema(final SQLiteDatabase db) {
|
||||
if (!DBHelper.existsColumn(WorldClockDao.TABLENAME, WorldClockDao.Properties.Code.columnName, db)) {
|
||||
final String statement = "ALTER TABLE " + WorldClockDao.TABLENAME + " ADD COLUMN "
|
||||
+ WorldClockDao.Properties.Code.columnName + " TEXT";
|
||||
db.execSQL(statement);
|
||||
}
|
||||
|
||||
if (!DBHelper.existsColumn(WorldClockDao.TABLENAME, WorldClockDao.Properties.Enabled.columnName, db)) {
|
||||
final String statement = "ALTER TABLE " + WorldClockDao.TABLENAME + " ADD COLUMN "
|
||||
+ WorldClockDao.Properties.Enabled.columnName + " BOOLEAN DEFAULT TRUE";
|
||||
db.execSQL(statement);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void downgradeSchema(final SQLiteDatabase db) {
|
||||
}
|
||||
}
|
|
@ -264,6 +264,11 @@ public abstract class AbstractDeviceCoordinator implements DeviceCoordinator {
|
|||
return 10;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsDisabledWorldClocks() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsRgbLedColor() {
|
||||
return false;
|
||||
|
|
|
@ -397,6 +397,12 @@ public interface DeviceCoordinator {
|
|||
*/
|
||||
int getWorldClocksLabelLength();
|
||||
|
||||
/**
|
||||
* Indicates whether the device supports disabled world clocks that can be enabled through
|
||||
* a menu on the device.
|
||||
*/
|
||||
boolean supportsDisabledWorldClocks();
|
||||
|
||||
/**
|
||||
* Indicates whether the device has an led which supports custom colors
|
||||
*/
|
||||
|
|
|
@ -85,8 +85,17 @@ public abstract class Huami2021Coordinator extends HuamiCoordinator {
|
|||
|
||||
@Override
|
||||
public int getWorldClocksSlotCount() {
|
||||
// TODO: It's supported, but not implemented - even in the official app
|
||||
return 0;
|
||||
return 20; // as enforced by Zepp
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getWorldClocksLabelLength() {
|
||||
return 30; // at least
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsDisabledWorldClocks() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -162,7 +171,9 @@ public abstract class Huami2021Coordinator extends HuamiCoordinator {
|
|||
settings.add(R.xml.devicesettings_header_time);
|
||||
//settings.add(R.xml.devicesettings_timeformat);
|
||||
settings.add(R.xml.devicesettings_dateformat_2);
|
||||
// TODO settings.add(R.xml.devicesettings_world_clocks);
|
||||
if (getWorldClocksSlotCount() > 0) {
|
||||
settings.add(R.xml.devicesettings_world_clocks);
|
||||
}
|
||||
|
||||
//
|
||||
// Display
|
||||
|
|
|
@ -87,11 +87,6 @@ public class AmazfitNeoCoordinator extends HuamiCoordinator {
|
|||
return 20; // max in Zepp app
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getWorldClocksLabelLength() {
|
||||
return 3; // neo has 3 letter city codes
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getReminderSlotCount(final GBDevice device) {
|
||||
return 0; // Neo does not support reminders
|
||||
|
|
|
@ -20,17 +20,16 @@ import static java.nio.charset.StandardCharsets.UTF_8;
|
|||
|
||||
import android.content.Context;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import nodomain.freeyourgadget.gadgetbridge.R;
|
||||
import nodomain.freeyourgadget.gadgetbridge.activities.InstallActivity;
|
||||
|
@ -39,69 +38,40 @@ import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
|||
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
|
||||
import nodomain.freeyourgadget.gadgetbridge.model.GenericItem;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.UriHelper;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.ZipFile;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.ZipFileException;
|
||||
|
||||
public class PineTimeInstallHandler implements InstallHandler {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(PineTimeInstallHandler.class);
|
||||
private static final Pattern binNameVersionPattern = Pattern.compile(".*-((?:\\d+\\.){2}\\d+).bin$");
|
||||
|
||||
private final Context context;
|
||||
private boolean valid = false;
|
||||
private String version = "(Unknown version)";
|
||||
|
||||
private InfiniTimeDFUPackage dfuPackageManifest;
|
||||
|
||||
public PineTimeInstallHandler(Uri uri, Context context) {
|
||||
this.context = context;
|
||||
|
||||
UriHelper uriHelper;
|
||||
InputStream inputStream;
|
||||
ZipInputStream zipInputStream;
|
||||
|
||||
InfiniTimeDFUPackage metadata = null;
|
||||
try {
|
||||
uriHelper = UriHelper.get(uri, this.context);
|
||||
inputStream = new BufferedInputStream(uriHelper.openInputStream());
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||
zipInputStream = new ZipInputStream(inputStream, UTF_8);
|
||||
} else {
|
||||
zipInputStream = new ZipInputStream(inputStream);
|
||||
|
||||
ZipFile dfuPackage = new ZipFile(uriHelper.openInputStream());
|
||||
String manifest = new String(dfuPackage.getFileFromZip("manifest.json"));
|
||||
|
||||
if (!manifest.trim().isEmpty()) {
|
||||
dfuPackageManifest = new Gson().fromJson(manifest.trim(), InfiniTimeDFUPackage.class);
|
||||
}
|
||||
|
||||
ZipEntry entry;
|
||||
while ((entry = zipInputStream.getNextEntry()) != null) {
|
||||
if (entry.isDirectory()) {
|
||||
continue;
|
||||
}
|
||||
if (entry.getName().equals("manifest.json")) {
|
||||
LOG.debug("Found manifest.json in DFU zip");
|
||||
StringBuilder json = new StringBuilder();
|
||||
|
||||
final byte[] buffer = new byte[1024];
|
||||
|
||||
int read;
|
||||
while ((read = zipInputStream.read(buffer, 0, buffer.length)) != -1) {
|
||||
json.append(new String(buffer, 0, read));
|
||||
}
|
||||
|
||||
Gson gson = new Gson();
|
||||
metadata = gson.fromJson(json.toString().trim(), InfiniTimeDFUPackage.class);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
zipInputStream.close();
|
||||
inputStream.close();
|
||||
} catch (ZipFileException e) {
|
||||
LOG.error("Unable to read manifest file.", e);
|
||||
} catch (FileNotFoundException e) {
|
||||
LOG.error("The DFU file was not found.", e);
|
||||
} catch (IOException e) {
|
||||
LOG.error("General IO error occurred.", e);
|
||||
} catch (Exception e) {
|
||||
valid = false;
|
||||
return;
|
||||
}
|
||||
|
||||
if (metadata != null &&
|
||||
metadata.manifest != null &&
|
||||
metadata.manifest.application != null &&
|
||||
metadata.manifest.application.bin_file != null) {
|
||||
valid = true;
|
||||
version = metadata.manifest.application.bin_file;
|
||||
} else {
|
||||
valid = false;
|
||||
LOG.error("Somehow metadata was found, but some data was missing");
|
||||
LOG.error("Unknown error occurred.", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -124,7 +94,7 @@ public class PineTimeInstallHandler implements InstallHandler {
|
|||
return;
|
||||
}
|
||||
|
||||
if (!valid) {
|
||||
if (!isValid()) {
|
||||
LOG.error("Firmware cannot be installed (not valid)");
|
||||
installActivity.setInfoText("Firmware cannot be installed (not valid)");
|
||||
installActivity.setInstallEnabled(false);
|
||||
|
@ -133,20 +103,32 @@ public class PineTimeInstallHandler implements InstallHandler {
|
|||
GenericItem installItem = new GenericItem();
|
||||
installItem.setIcon(R.drawable.ic_firmware);
|
||||
installItem.setName("PineTime firmware");
|
||||
installItem.setDetails(version);
|
||||
installItem.setDetails(getVersion());
|
||||
|
||||
installActivity.setInfoText(context.getString(R.string.firmware_install_warning, "(unknown)"));
|
||||
installActivity.setInstallItem(installItem);
|
||||
LOG.debug("Initialized PineTimeInstallHandler");
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onStartInstall(GBDevice device) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid() {
|
||||
return valid;
|
||||
return dfuPackageManifest != null &&
|
||||
dfuPackageManifest.manifest != null &&
|
||||
dfuPackageManifest.manifest.application != null &&
|
||||
dfuPackageManifest.manifest.application.bin_file != null;
|
||||
}
|
||||
|
||||
// TODO: obtain version information from manifest file instead
|
||||
private String getVersion() {
|
||||
String binFileName = dfuPackageManifest.manifest.application.bin_file;
|
||||
Matcher regexMatcher = binNameVersionPattern.matcher(binFileName);
|
||||
|
||||
if (regexMatcher.matches())
|
||||
return regexMatcher.group(1);
|
||||
return "(Unknown version)";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,7 +24,9 @@ public interface WorldClock extends Serializable {
|
|||
*/
|
||||
String EXTRA_WORLD_CLOCK = "world_clock";
|
||||
|
||||
Boolean getEnabled();
|
||||
String getWorldClockId();
|
||||
String getLabel();
|
||||
String getCode();
|
||||
String getTimeZoneId();
|
||||
}
|
||||
|
|
|
@ -21,28 +21,20 @@ import org.json.JSONObject;
|
|||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipInputStream;
|
||||
|
||||
import nodomain.freeyourgadget.gadgetbridge.service.btle.BLETypeConversions;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.ArrayUtils;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.ZipFile;
|
||||
|
||||
|
||||
public abstract class Huami2021FirmwareInfo extends AbstractHuamiFirmwareInfo {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(Huami2021FirmwareInfo.class);
|
||||
|
||||
public static final byte[] ZIP_HEADER = new byte[]{
|
||||
0x50, 0x4B, 0x03, 0x04
|
||||
};
|
||||
|
||||
public Huami2021FirmwareInfo(final byte[] bytes) {
|
||||
super(bytes);
|
||||
}
|
||||
|
@ -60,43 +52,49 @@ public abstract class Huami2021FirmwareInfo extends AbstractHuamiFirmwareInfo {
|
|||
@Override
|
||||
protected HuamiFirmwareType determineFirmwareType(final byte[] bytes) {
|
||||
if (ArrayUtils.equals(bytes, UIHHContainer.UIHH_HEADER, 0)) {
|
||||
final UIHHContainer uihh = UIHHContainer.fromRawBytes(bytes);
|
||||
if (uihh == null) {
|
||||
LOG.warn("Invalid UIHH file");
|
||||
return HuamiFirmwareType.INVALID;
|
||||
}
|
||||
|
||||
UIHHContainer.FileEntry uihhFirmwareZipFile = null;
|
||||
boolean hasChangelog = false;
|
||||
for (final UIHHContainer.FileEntry file : uihh.getFiles()) {
|
||||
switch(file.getType()) {
|
||||
case FIRMWARE_ZIP:
|
||||
uihhFirmwareZipFile = file;
|
||||
continue;
|
||||
case FIRMWARE_CHANGELOG:
|
||||
hasChangelog = true;
|
||||
continue;
|
||||
default:
|
||||
LOG.warn("Unexpected file for {}", file.getType());
|
||||
}
|
||||
}
|
||||
|
||||
if (uihhFirmwareZipFile != null && hasChangelog) {
|
||||
final byte[] firmwareBin = getFileFromZip(uihhFirmwareZipFile.getContent(), "META/firmware.bin");
|
||||
|
||||
if (isCompatibleFirmwareBin(firmwareBin)) {
|
||||
return HuamiFirmwareType.FIRMWARE_UIHH_2021_ZIP_WITH_CHANGELOG;
|
||||
}
|
||||
}
|
||||
return handleUihhPackage(bytes);
|
||||
} else if (ZipFile.isZipFile(bytes)) {
|
||||
return handleZipPackage(bytes);
|
||||
} else {
|
||||
return HuamiFirmwareType.INVALID;
|
||||
}
|
||||
}
|
||||
|
||||
private HuamiFirmwareType handleUihhPackage(byte[] bytes) {
|
||||
final UIHHContainer uihh = UIHHContainer.fromRawBytes(bytes);
|
||||
if (uihh == null) {
|
||||
LOG.warn("Invalid UIHH file");
|
||||
return HuamiFirmwareType.INVALID;
|
||||
}
|
||||
|
||||
if (!ArrayUtils.equals(bytes, ZIP_HEADER, 0)) {
|
||||
return HuamiFirmwareType.INVALID;
|
||||
UIHHContainer.FileEntry uihhFirmwareZipFile = null;
|
||||
boolean hasChangelog = false;
|
||||
for (final UIHHContainer.FileEntry file : uihh.getFiles()) {
|
||||
switch(file.getType()) {
|
||||
case FIRMWARE_ZIP:
|
||||
uihhFirmwareZipFile = file;
|
||||
continue;
|
||||
case FIRMWARE_CHANGELOG:
|
||||
hasChangelog = true;
|
||||
continue;
|
||||
default:
|
||||
LOG.warn("Unexpected file for {}", file.getType());
|
||||
}
|
||||
}
|
||||
|
||||
final byte[] firmwareBin = getFileFromZip(bytes, "META/firmware.bin");
|
||||
if (uihhFirmwareZipFile != null && hasChangelog) {
|
||||
byte[] firmwareBin = ZipFile.tryReadFileQuick(uihhFirmwareZipFile.getContent(), "META/firmware.bin");
|
||||
|
||||
if (isCompatibleFirmwareBin(firmwareBin)) {
|
||||
return HuamiFirmwareType.FIRMWARE_UIHH_2021_ZIP_WITH_CHANGELOG;
|
||||
}
|
||||
}
|
||||
|
||||
return HuamiFirmwareType.INVALID;
|
||||
}
|
||||
|
||||
private HuamiFirmwareType handleZipPackage(byte[] bytes) {
|
||||
final byte[] firmwareBin = ZipFile.tryReadFileQuick(bytes, "META/firmware.bin");
|
||||
if (isCompatibleFirmwareBin(firmwareBin)) {
|
||||
return HuamiFirmwareType.FIRMWARE;
|
||||
}
|
||||
|
@ -195,7 +193,7 @@ public abstract class Huami2021FirmwareInfo extends AbstractHuamiFirmwareInfo {
|
|||
}
|
||||
|
||||
public String getFirmwareVersion(final byte[] fwbytes) {
|
||||
final byte[] firmwareBin = getFileFromZip(fwbytes, "META/firmware.bin");
|
||||
final byte[] firmwareBin = ZipFile.tryReadFileQuick(fwbytes, "META/firmware.bin");
|
||||
|
||||
if (firmwareBin == null) {
|
||||
LOG.warn("Failed to read firmware.bin");
|
||||
|
@ -228,7 +226,7 @@ public abstract class Huami2021FirmwareInfo extends AbstractHuamiFirmwareInfo {
|
|||
}
|
||||
|
||||
public String getAppName() {
|
||||
final byte[] appJsonBin = getFileFromZip(getBytes(), "app.json");
|
||||
final byte[] appJsonBin = ZipFile.tryReadFileQuick(getBytes(), "app.json");
|
||||
if (appJsonBin == null) {
|
||||
LOG.warn("Failed to get app.json from zip");
|
||||
return null;
|
||||
|
@ -252,7 +250,7 @@ public abstract class Huami2021FirmwareInfo extends AbstractHuamiFirmwareInfo {
|
|||
}
|
||||
|
||||
public String getAppType() {
|
||||
final byte[] appJsonBin = getFileFromZip(getBytes(), "app.json");
|
||||
final byte[] appJsonBin = ZipFile.tryReadFileQuick(getBytes(), "app.json");
|
||||
if (appJsonBin == null) {
|
||||
LOG.warn("Failed to get app.json from zip");
|
||||
return null;
|
||||
|
@ -270,35 +268,4 @@ public abstract class Huami2021FirmwareInfo extends AbstractHuamiFirmwareInfo {
|
|||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static byte[] getFileFromZip(final byte[] zipBytes, final String path) {
|
||||
try (ZipInputStream zipInputStream = new ZipInputStream(new ByteArrayInputStream(zipBytes))) {
|
||||
ZipEntry zipEntry;
|
||||
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
|
||||
if (zipEntry.getName().equals(path)) {
|
||||
return readAllBytes(zipInputStream);
|
||||
}
|
||||
}
|
||||
} catch (final IOException e) {
|
||||
LOG.error(String.format("Failed to read %s from zip", path), e);
|
||||
return null;
|
||||
}
|
||||
|
||||
LOG.debug("{} not found in zip", path);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static byte[] readAllBytes(final InputStream is) throws IOException {
|
||||
final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
|
||||
|
||||
int n;
|
||||
byte[] buf = new byte[16384];
|
||||
|
||||
while ((n = is.read(buf, 0, buf.length)) != -1) {
|
||||
buffer.write(buf, 0, n);
|
||||
}
|
||||
|
||||
return buffer.toByteArray();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -785,9 +785,8 @@ public abstract class Huami2021Support extends HuamiSupport {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected void sendWorldClocks(final TransactionBuilder builder,
|
||||
final List<? extends WorldClock> clocks) {
|
||||
// TODO not yet implemented
|
||||
protected boolean isWorldClocksEncrypted() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -1101,7 +1101,11 @@ public abstract class HuamiSupport extends AbstractBTLEDeviceSupport implements
|
|||
return;
|
||||
}
|
||||
|
||||
writeToChunked2021(builder, (short) 0x0008, baos.toByteArray(), false);
|
||||
writeToChunked2021(builder, (short) 0x0008, baos.toByteArray(), isWorldClocksEncrypted());
|
||||
}
|
||||
|
||||
protected boolean isWorldClocksEncrypted() {
|
||||
return false;
|
||||
}
|
||||
|
||||
private byte[] encodeWorldClock(final WorldClock clock) {
|
||||
|
@ -1113,8 +1117,12 @@ public abstract class HuamiSupport extends AbstractBTLEDeviceSupport implements
|
|||
final TimeZone timezone = TimeZone.getTimeZone(clock.getTimeZoneId());
|
||||
final ZoneId zoneId = ZoneId.of(clock.getTimeZoneId());
|
||||
|
||||
// Usually the 3-letter city code (eg. LIS for Lisbon), but doesn't seem to be used in the UI (used in Amazfit Neo)
|
||||
baos.write(StringUtils.truncate(clock.getLabel(), 3).toUpperCase().getBytes(StandardCharsets.UTF_8));
|
||||
// Usually the 3-letter city code (eg. LIS for Lisbon)
|
||||
if (clock.getCode() != null) {
|
||||
baos.write(StringUtils.truncate(clock.getCode(), 3).toUpperCase().getBytes(StandardCharsets.UTF_8));
|
||||
} else {
|
||||
baos.write(StringUtils.truncate(clock.getLabel(), 3).toUpperCase().getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
baos.write(0x00);
|
||||
|
||||
// Some other string? Seems to be empty
|
||||
|
@ -1164,6 +1172,10 @@ public abstract class HuamiSupport extends AbstractBTLEDeviceSupport implements
|
|||
baos.write((byte) ((nextTransitionTs >> (i * 8)) & 0xff));
|
||||
}
|
||||
|
||||
if (coordinator.supportsDisabledWorldClocks()) {
|
||||
baos.write((byte) (clock.getEnabled() ? 0x01 : 0x00));
|
||||
}
|
||||
|
||||
return baos.toByteArray();
|
||||
} catch (final IOException e) {
|
||||
throw new RuntimeException("This should never happen", e);
|
||||
|
|
|
@ -0,0 +1,116 @@
|
|||
package nodomain.freeyourgadget.gadgetbridge.util;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipException;
|
||||
import java.util.zip.ZipInputStream;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* Utility class for recognition and reading of ZIP archives.
|
||||
*/
|
||||
public class ZipFile {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(ZipFile.class);
|
||||
public static final byte[] ZIP_HEADER = new byte[]{
|
||||
0x50, 0x4B, 0x03, 0x04
|
||||
};
|
||||
|
||||
private final byte[] zipBytes;
|
||||
|
||||
/**
|
||||
* Open ZIP file from byte array already in memory.
|
||||
* @param zipBytes data to handle as a ZIP file.
|
||||
*/
|
||||
public ZipFile(byte[] zipBytes) {
|
||||
this.zipBytes = zipBytes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Open ZIP file from InputStream.<br>
|
||||
* This will read the entire file into memory at once.
|
||||
* @param inputStream data to handle as a ZIP file.
|
||||
*/
|
||||
public ZipFile(InputStream inputStream) throws IOException {
|
||||
this.zipBytes = readAllBytes(inputStream);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if data resembles a ZIP file.<br>
|
||||
* The check is not infallible: it may report self-extracting or other exotic ZIP archives as not a ZIP file, and it may report a corrupted ZIP file as a ZIP file.
|
||||
* @param data The data to check.
|
||||
* @return Whether data resembles a ZIP file.
|
||||
*/
|
||||
public static boolean isZipFile(byte[] data) {
|
||||
return ArrayUtils.equals(data, ZIP_HEADER, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the contents of file at path into a byte array.
|
||||
* @param path Path of the file in the ZIP file.
|
||||
* @return byte array contatining the contents of the requested file.
|
||||
* @throws ZipFileException If the specified path does not exist or references a directory, or if some other I/O error occurs. In other words, if return value would otherwise be null.
|
||||
*/
|
||||
public byte[] getFileFromZip(final String path) throws ZipFileException {
|
||||
try (InputStream is = new ByteArrayInputStream(zipBytes); ZipInputStream zipInputStream = new ZipInputStream(is)) {
|
||||
ZipEntry zipEntry;
|
||||
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
|
||||
if (!zipEntry.getName().equals(path)) continue; // TODO: is this always a path? The documentation is very vague.
|
||||
|
||||
if (zipEntry.isDirectory()) {
|
||||
throw new ZipFileException(String.format("Path in ZIP file is a directory: %s", path));
|
||||
}
|
||||
|
||||
return readAllBytes(zipInputStream);
|
||||
}
|
||||
|
||||
throw new ZipFileException(String.format("Path in ZIP file was not found: %s", path));
|
||||
|
||||
} catch (ZipException e) {
|
||||
throw new ZipFileException("The ZIP file might be corrupted");
|
||||
} catch (IOException e) {
|
||||
throw new ZipFileException("General IO error");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to obtain file from ZIP file without much hassle, but is not safe.<br>
|
||||
* Please only use this in place of old code where correctness of the result is checked only later on.<br>
|
||||
* Use getFileFromZip of ZipFile instance instead.
|
||||
* @param zipBytes
|
||||
* @param path Path of the file in the ZIP file.
|
||||
* @return Contents of requested file or null.
|
||||
*/
|
||||
@Deprecated
|
||||
@Nullable
|
||||
public static byte[] tryReadFileQuick(final byte[] zipBytes, final String path) {
|
||||
try {
|
||||
return new ZipFile(zipBytes).getFileFromZip(path);
|
||||
} catch (ZipFileException e) {
|
||||
LOG.error("Quick ZIP reading failed.", e);
|
||||
} catch (Exception e) {
|
||||
LOG.error("Unable to close ZipFile.", e);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static byte[] readAllBytes(final InputStream is) throws IOException {
|
||||
final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
|
||||
|
||||
int n;
|
||||
byte[] buf = new byte[16384];
|
||||
|
||||
while ((n = is.read(buf, 0, buf.length)) != -1) {
|
||||
buffer.write(buf, 0, n);
|
||||
}
|
||||
|
||||
return buffer.toByteArray();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package nodomain.freeyourgadget.gadgetbridge.util;
|
||||
|
||||
public class ZipFileException extends Exception {
|
||||
public ZipFileException(String message) {
|
||||
super(String.format("Error while reading ZIP file: %s", message));
|
||||
}
|
||||
}
|
|
@ -6,7 +6,33 @@
|
|||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical"
|
||||
tools:context="nodomain.freeyourgadget.gadgetbridge.activities.ReminderDetails">
|
||||
tools:context="nodomain.freeyourgadget.gadgetbridge.activities.WorldClockDetails">
|
||||
|
||||
<androidx.cardview.widget.CardView
|
||||
android:id="@+id/card_enabled"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_margin="8dp"
|
||||
android:foreground="?android:attr/selectableItemBackground"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
card_view:cardCornerRadius="4dp"
|
||||
card_view:cardElevation="4dp"
|
||||
card_view:contentPadding="4dp">
|
||||
|
||||
<androidx.constraintlayout.widget.ConstraintLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_margin="8dp">
|
||||
|
||||
<CheckBox
|
||||
android:id="@+id/world_clock_enabled"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:text="@string/function_enabled" />
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
</androidx.cardview.widget.CardView>
|
||||
|
||||
<androidx.cardview.widget.CardView
|
||||
android:id="@+id/card_timezone"
|
||||
|
@ -16,7 +42,7 @@
|
|||
android:foreground="?android:attr/selectableItemBackground"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@id/card_enabled"
|
||||
card_view:cardCornerRadius="4dp"
|
||||
card_view:cardElevation="4dp"
|
||||
card_view:contentPadding="4dp">
|
||||
|
@ -40,7 +66,7 @@
|
|||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="2dp"
|
||||
android:text="?"
|
||||
android:text=""
|
||||
android:textAppearance="?android:attr/textAppearanceLarge"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@id/label_timezone" />
|
||||
|
@ -85,6 +111,44 @@
|
|||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
</androidx.cardview.widget.CardView>
|
||||
|
||||
<androidx.cardview.widget.CardView
|
||||
android:id="@+id/card_code"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_margin="8dp"
|
||||
android:foreground="?android:attr/selectableItemBackground"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@id/card_label"
|
||||
card_view:cardCornerRadius="4dp"
|
||||
card_view:cardElevation="4dp"
|
||||
card_view:contentPadding="4dp">
|
||||
|
||||
<androidx.constraintlayout.widget.ConstraintLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_margin="8dp">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/label_code"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/world_clock_code"
|
||||
android:textAppearance="?android:attr/textAppearance"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
<EditText
|
||||
android:id="@+id/world_clock_code"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="2dp"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@id/label_code" />
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
</androidx.cardview.widget.CardView>
|
||||
|
||||
<com.google.android.material.floatingactionbutton.FloatingActionButton
|
||||
android:id="@+id/fab_save"
|
||||
android:layout_width="wrap_content"
|
||||
|
|
|
@ -651,8 +651,10 @@
|
|||
<string name="world_clock_delete_confirm_description">Are you sure you want to delete the world clock?</string>
|
||||
<string name="world_clock_no_free_slots_title">No free slots</string>
|
||||
<string name="world_clock_no_free_slots_description">The device has no free slots for world clocks (total slots: %1$s)</string>
|
||||
<string name="function_enabled">Enabled</string>
|
||||
<string name="world_clock_timezone">Time Zone</string>
|
||||
<string name="world_clock_label">Label</string>
|
||||
<string name="world_clock_code">Code</string>
|
||||
<string name="title_activity_alarm_details">Alarm details</string>
|
||||
<string name="title_activity_reminder_details">Reminder details</string>
|
||||
<string name="title_activity_world_clock_details">World Clock details</string>
|
||||
|
|
|
@ -0,0 +1,167 @@
|
|||
package nodomain.freeyourgadget.gadgetbridge.test;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.ZipFile;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.ZipFileException;
|
||||
|
||||
public class ZipFileTest extends TestBase {
|
||||
private static final String TEST_FILE_NAME = "manifest.json";
|
||||
private static final String TEST_NESTED_FILE_NAME = "directory/manifest.json";
|
||||
private static final String TEST_FILE_CONTENTS_1 = "{ \"mykey\": \"myvalue\", \"myarr\": [0, 1, 2, 3] }";
|
||||
private static final String TEST_FILE_CONTENTS_2 = "{\n" +
|
||||
" \"manifest\": {\n" +
|
||||
" \"application\": {\n" +
|
||||
" \"bin_file\": \"pinetime-mcuboot-app-image-1.10.0.bin\",\n" +
|
||||
" \"dat_file\": \"pinetime-mcuboot-app-image-1.10.0.dat\",\n" +
|
||||
" \"init_packet_data\": {\n" +
|
||||
" \"application_version\": 4294967295,\n" +
|
||||
" \"device_revision\": 65535,\n" +
|
||||
" \"device_type\": 82,\n" +
|
||||
" \"firmware_crc16\": 21770,\n" +
|
||||
" \"softdevice_req\": [\n" +
|
||||
" 65534\n" +
|
||||
" ]\n" +
|
||||
" }\n" +
|
||||
" },\n" +
|
||||
" \"dfu_version\": 0.5\n" +
|
||||
" }\n" +
|
||||
"}";
|
||||
|
||||
@Test
|
||||
public void testZipSize1() throws IOException, ZipFileException {
|
||||
final String contents = TEST_FILE_CONTENTS_1;
|
||||
|
||||
byte[] zipArchive = createZipArchive(TEST_FILE_NAME, contents);
|
||||
|
||||
ZipFile zipFile = new ZipFile(zipArchive);
|
||||
String readContents = new String(zipFile.getFileFromZip(TEST_FILE_NAME));
|
||||
|
||||
Assert.assertEquals(contents, readContents);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testZipSize2() throws IOException, ZipFileException {
|
||||
final String contents = TEST_FILE_CONTENTS_2;
|
||||
|
||||
byte[] zipArchive = createZipArchive(TEST_FILE_NAME, contents);
|
||||
|
||||
ZipFile zipFile = new ZipFile(zipArchive);
|
||||
String readContents = new String(zipFile.getFileFromZip(TEST_FILE_NAME));
|
||||
|
||||
Assert.assertEquals(contents, readContents);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testZipSize3() throws IOException, ZipFileException, JSONException {
|
||||
String contents = makeLargeJsonObject(new JSONObject(TEST_FILE_CONTENTS_2), 4).toString(4);
|
||||
|
||||
byte[] zipArchive = createZipArchive(TEST_FILE_NAME, contents);
|
||||
|
||||
ZipFile zipFile = new ZipFile(zipArchive);
|
||||
String readContents = new String(zipFile.getFileFromZip(TEST_FILE_NAME));
|
||||
|
||||
Assert.assertEquals(contents, readContents);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testZipSize4() throws IOException, ZipFileException, JSONException {
|
||||
String contents = makeLargeJsonObject(new JSONObject(TEST_FILE_CONTENTS_2), 32).toString(4);
|
||||
|
||||
byte[] zipArchive = createZipArchive(TEST_FILE_NAME, contents);
|
||||
|
||||
ZipFile zipFile = new ZipFile(zipArchive);
|
||||
String readContents = new String(zipFile.getFileFromZip(TEST_FILE_NAME));
|
||||
|
||||
Assert.assertEquals(contents, readContents);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testZipFileInDir() throws IOException, ZipFileException {
|
||||
String contents = TEST_FILE_CONTENTS_1;
|
||||
|
||||
byte[] zipArchive = createZipArchive(TEST_NESTED_FILE_NAME, contents);
|
||||
|
||||
ZipFile zipFile = new ZipFile(zipArchive);
|
||||
String readContents = new String(zipFile.getFileFromZip(TEST_NESTED_FILE_NAME));
|
||||
|
||||
Assert.assertEquals(contents, readContents);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testZipFilesUnorderedAccess() throws IOException, ZipFileException {
|
||||
String contents1 = TEST_FILE_CONTENTS_1;
|
||||
String contents2 = TEST_FILE_CONTENTS_2;
|
||||
String contents3 = "zbuMyWvIxeKgcWnsSYOd8CTLgjc9x7ti21OlLlGduMJVXlKc835WEUKJ3xR6GDA5d0tHSXnYxZkDlznFQVyueHhwYywsMO9PlkJqjOCA2Mn8uTuTliIKUNPBraFipOodb6rW31HdKLOd7gmniLF5mvdRPHOUKIXSMciqogOsZnvGXylMx6TegesGBWAeHFhTSQ5xXOrOEUsDHK78M3A0yFXzLE0XgwI90Tl87OHyWfE0y0yINv5PjxgGCLUB7mHYFpgPW1C5yyIkb2JA6CePE3hHv369khwmLumW7P9ErZhzdGgeskz6Os0p5HMrTFuySc0PWxsIfru1HldIH9TZTSMCbd91G5jCCikyx2zrzDKaasuQZyBGZcMjr1zcCLpPQiKT7ELSoUBCKhiFODxbFA06MC5bLXh2WvyP8W2kVxT2T4AnDX6pwf1BKs4nbHpAjvMmHrzlhQp7Q6VWBEiniY5M9QW4ExRcMGIBYXvY7vu5p";
|
||||
|
||||
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
final ZipOutputStream zipWriteStream = new ZipOutputStream(baos);
|
||||
|
||||
writeFileToZip(contents1, "file1", zipWriteStream);
|
||||
writeFileToZip(contents2, "file2", zipWriteStream);
|
||||
writeFileToZip(contents3, "file3", zipWriteStream);
|
||||
zipWriteStream.close();
|
||||
|
||||
ZipFile zipFile = new ZipFile(baos.toByteArray());
|
||||
String readContents2 = new String(zipFile.getFileFromZip("file2"));
|
||||
String readContents1 = new String(zipFile.getFileFromZip("file1"));
|
||||
String readContents3 = new String(zipFile.getFileFromZip("file3"));
|
||||
|
||||
Assert.assertEquals(contents1, readContents1);
|
||||
Assert.assertEquals(contents2, readContents2);
|
||||
Assert.assertEquals(contents3, readContents3);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a ZIP archive with a single text file.
|
||||
* The archive will not be saved to a file, it is kept in memory.
|
||||
*
|
||||
* @return the ZIP archive
|
||||
*/
|
||||
private byte[] createZipArchive(String path, String fileContents) throws IOException {
|
||||
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
final ZipOutputStream zipFile = new ZipOutputStream(baos);
|
||||
|
||||
writeFileToZip(fileContents, path, zipFile);
|
||||
zipFile.close();
|
||||
|
||||
return baos.toByteArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a larger JSON object for testing purposes, based on a preexisting JSON object.
|
||||
*/
|
||||
private JSONObject makeLargeJsonObject(JSONObject base, int repetitions) throws JSONException {
|
||||
JSONObject manifestObj = base.getJSONObject("manifest");
|
||||
JSONArray array = new JSONArray();
|
||||
|
||||
for (int i = 0; i < repetitions; i++) {
|
||||
array.put(manifestObj);
|
||||
}
|
||||
|
||||
return base.put("array", array);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write given data to file at given path into an already opened ZIP archive.
|
||||
* Allows to create an archive with multiple files.
|
||||
*/
|
||||
private void writeFileToZip(String fileContents, String path, ZipOutputStream zipFile) throws IOException {
|
||||
byte[] data = fileContents.getBytes(StandardCharsets.UTF_8);
|
||||
|
||||
ZipEntry zipEntry = new ZipEntry(path);
|
||||
zipFile.putNextEntry(zipEntry);
|
||||
zipFile.write(data, 0, fileContents.length());
|
||||
zipFile.closeEntry();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user