1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-06-15 09:30:30 +02:00
Gadgetbridge/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/pebble/PBWReader.java
2015-04-06 20:58:35 +02:00

154 lines
4.6 KiB
Java

package nodomain.freeyourgadget.gadgetbridge.pebble;
import android.content.ContentResolver;
import android.content.Context;
import android.net.Uri;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import nodomain.freeyourgadget.gadgetbridge.GBDeviceApp;
public class PBWReader {
private GBDeviceApp app;
private final Uri uri;
private final ContentResolver cr;
public PBWReader(Uri uri, Context context) {
this.uri = uri;
cr = context.getContentResolver();
InputStream fin = null;
try {
fin = new BufferedInputStream(cr.openInputStream(uri));
} catch (FileNotFoundException e) {
e.printStackTrace();
return;
}
ZipInputStream zis = new ZipInputStream(fin);
ZipEntry ze = null;
try {
while ((ze = zis.getNextEntry()) != null) {
if (ze.getName().equals("appinfo.json")) {
long bytes = ze.getSize();
if (bytes > 8192) // that should be too much
break;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int count;
while ((count = zis.read(buffer)) != -1) {
baos.write(buffer, 0, count);
}
String jsonString = baos.toString();
try {
JSONObject json = new JSONObject(jsonString);
String appName = json.getString("shortName");
String appCreator = json.getString("companyName");
String appVersion = json.getString("versionLabel");
if (appName != null && appCreator != null && appVersion != null) {
// FIXME: dont assume WATCHFACE
app = new GBDeviceApp(-1, -1, appName, appCreator, appVersion, GBDeviceApp.Type.WATCHFACE);
}
} catch (JSONException e) {
e.printStackTrace();
break;
}
}
}
zis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public GBDeviceApp getGBDeviceApp() {
return app;
}
public ZipInputStream getInputStreamFile(String filename) {
InputStream fin = null;
try {
fin = new BufferedInputStream(cr.openInputStream(uri));
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
ZipInputStream zis = new ZipInputStream(fin);
ZipEntry ze = null;
try {
while ((ze = zis.getNextEntry()) != null) {
if (ze.getName().equals(filename)) {
return zis;
}
}
zis.close();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public int getFileSize(String filename) {
InputStream fin = null;
try {
fin = new BufferedInputStream(cr.openInputStream(uri));
} catch (FileNotFoundException e) {
e.printStackTrace();
return -1;
}
ZipInputStream zis = new ZipInputStream(fin);
ZipEntry ze = null;
try {
while ((ze = zis.getNextEntry()) != null) {
if (ze.getName().equals(filename)) {
return (int) ze.getSize();
}
}
zis.close();
} catch (IOException e) {
e.printStackTrace();
}
return -1;
}
public ZipInputStream getInputStreamAppBinary() {
return getInputStreamFile("pebble-app.bin");
}
public int getAppBinarySize() {
return getFileSize("pebble-app.bin");
}
public ZipInputStream getInputStreamAppWorker() {
return getInputStreamFile("pebble-worker.bin");
}
public int getAppWorkerSize() {
return getFileSize("pebble-worker.bin");
}
public ZipInputStream getInputStreamAppResources() {
return getInputStreamFile("app_resources.pbpack");
}
public int getAppResourcesSize() {
return getFileSize("pebble-resources.pbpack");
}
}