fix installing pbz/pbw files from content provides (eg. download manager)

This still does not fix pbw isntallation problems with FW 3.x since the content does not get copied to the pbw cache yet when content providers are involved
master
Andreas Shimokawa 2015-12-06 16:48:43 +01:00
parent 05a8436f7c
commit 9ebb320e10
1 changed files with 39 additions and 31 deletions

View File

@ -18,12 +18,10 @@ import java.io.InputStream;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.ByteOrder; import java.nio.ByteOrder;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream; import java.util.zip.ZipInputStream;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceApp; import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceApp;
@ -32,6 +30,7 @@ import nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.PebbleProtoco
public class PBWReader { public class PBWReader {
private static final Logger LOG = LoggerFactory.getLogger(PBWReader.class); private static final Logger LOG = LoggerFactory.getLogger(PBWReader.class);
private static final HashMap<String, Byte> appFileTypesMap; private static final HashMap<String, Byte> appFileTypesMap;
private static final HashMap<String, Byte> fwFileTypesMap;
static { static {
appFileTypesMap = new HashMap<>(); appFileTypesMap = new HashMap<>();
@ -40,8 +39,6 @@ public class PBWReader {
appFileTypesMap.put("worker", PebbleProtocol.PUTBYTES_TYPE_WORKER); appFileTypesMap.put("worker", PebbleProtocol.PUTBYTES_TYPE_WORKER);
} }
private static final HashMap<String, Byte> fwFileTypesMap;
static { static {
fwFileTypesMap = new HashMap<>(); fwFileTypesMap = new HashMap<>();
fwFileTypesMap.put("firmware", PebbleProtocol.PUTBYTES_TYPE_FIRMWARE); fwFileTypesMap.put("firmware", PebbleProtocol.PUTBYTES_TYPE_FIRMWARE);
@ -65,11 +62,17 @@ public class PBWReader {
this.uri = uri; this.uri = uri;
cr = context.getContentResolver(); cr = context.getContentResolver();
InputStream fin;
try {
fin = new BufferedInputStream(cr.openInputStream(uri));
} catch (FileNotFoundException e) {
e.printStackTrace();
return;
}
if (uri.toString().endsWith(".pbl") && platform.equals("aplite")) { if (uri.toString().endsWith(".pbl") && platform.equals("aplite")) {
InputStream fin;
STM32CRC stm32crc = new STM32CRC(); STM32CRC stm32crc = new STM32CRC();
try { try {
fin = new BufferedInputStream(cr.openInputStream(uri));
byte[] buf = new byte[2000]; byte[] buf = new byte[2000];
while (fin.available() > 0) { while (fin.available() > 0) {
int count = fin.read(buf); int count = fin.read(buf);
@ -95,43 +98,48 @@ public class PBWReader {
} }
String platformDir = ""; String platformDir = "";
if (!uri.toString().endsWith(".pbz")) { if (!uri.toString().endsWith(".pbz")) {
platformDir = platform + "/"; platformDir = platform + "/";
}
ZipFile zipFile; if (platform.equals("aplite")) {
try { boolean hasApliteDir = false;
zipFile = new ZipFile(uri.getPath()); InputStream afin;
} catch (IOException e) {
e.printStackTrace();
return;
}
try {
afin = new BufferedInputStream(cr.openInputStream(uri));
} catch (FileNotFoundException e) {
e.printStackTrace();
return;
}
if (platform.equals("aplite")) { ZipInputStream zis = new ZipInputStream(afin);
boolean hasApliteDir = false; ZipEntry ze;
Enumeration zentries = zipFile.entries(); try {
while (zentries.hasMoreElements()) { while ((ze = zis.getNextEntry()) != null) {
ZipEntry entry = (ZipEntry) zentries.nextElement(); if (ze.getName().startsWith("aplite/")) {
if (entry.getName().startsWith("aplite/")) { hasApliteDir = true;
hasApliteDir = true; break;
break; }
}
zis.close();
} catch (IOException e) {
e.printStackTrace();
}
if (!hasApliteDir) {
platformDir = "";
} }
} }
if (!hasApliteDir) {
platformDir = "";
}
} }
ZipInputStream zis = new ZipInputStream(fin);
ZipEntry ze;
pebbleInstallables = new ArrayList<>(); pebbleInstallables = new ArrayList<>();
byte[] buffer = new byte[1024]; byte[] buffer = new byte[1024];
int count; int count;
try { try {
Enumeration zentries = zipFile.entries(); while ((ze = zis.getNextEntry()) != null) {
while (zentries.hasMoreElements()) {
ZipEntry ze = (ZipEntry) zentries.nextElement();
InputStream zis = zipFile.getInputStream(ze);
String fileName = ze.getName(); String fileName = ze.getName();
if (fileName.equals(platformDir + "manifest.json")) { if (fileName.equals(platformDir + "manifest.json")) {
long bytes = ze.getSize(); long bytes = ze.getSize();
@ -226,8 +234,8 @@ public class PBWReader {
LOG.info("got flags from pebble-app.bin: " + mFlags); LOG.info("got flags from pebble-app.bin: " + mFlags);
// more follows but, not interesting for us // more follows but, not interesting for us
} }
zis.close();
} }
zis.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }