Pebble: store app details in pbw-cache and display them in app manager on firmware 3.x

Improves #93
This commit is contained in:
Andreas Shimokawa 2015-12-07 18:15:26 +01:00
parent 44667a60d1
commit 1c3e0b628b
5 changed files with 102 additions and 11 deletions

View File

@ -1,6 +1,7 @@
###Changelog
####Next Version
* Pebble: store app details in pbw-cache and display them in app manager on firmware 3.x
* Pebble: Increase maximum notification body length from 255 to 512 bytes on firmware 3.x
####Version 0.6.8

View File

@ -16,6 +16,7 @@ import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -80,19 +81,30 @@ public class AppManagerActivity extends Activity {
private List<GBDeviceApp> getCachedApps() {
List<GBDeviceApp> cachedAppList = new ArrayList<>();
File cachePath;
try {
File cachePath = new File(FileUtils.getExternalFilesDir().getPath() + "/pbw-cache");
File files[] = cachePath.listFiles();
if (files != null) {
for (File file : files) {
if (file.getName().endsWith(".pbw")) {
UUID uuid = UUID.fromString(file.getName().substring(0, file.getName().length() - 4));
cachedAppList.add(new GBDeviceApp(uuid, uuid.toString(), "N/A", "", GBDeviceApp.Type.UNKNOWN));
cachePath = new File(FileUtils.getExternalFilesDir().getPath() + "/pbw-cache");
} catch (IOException e) {
LOG.warn("could not get external dir while reading pbw cache.");
return cachedAppList;
}
File files[] = cachePath.listFiles();
if (files != null) {
for (File file : files) {
if (file.getName().endsWith(".pbw")) {
String baseName = file.getName().substring(0, file.getName().length() - 4);
File jsonFile = new File(cachePath, baseName + ".json");
try {
String jsonstring = FileUtils.getStringFromFile(jsonFile);
JSONObject json = new JSONObject(jsonstring);
cachedAppList.add(new GBDeviceApp(json));
} catch (Exception e) {
LOG.warn("could not read json file for " + baseName, e.getMessage(), e);
cachedAppList.add(new GBDeviceApp(UUID.fromString(baseName), baseName, "N/A", "", GBDeviceApp.Type.UNKNOWN));
}
}
}
} catch (IOException e) {
LOG.error("Error getting cached apps: " + e.getMessage(), e);
}
return cachedAppList;
}

View File

@ -6,9 +6,12 @@ import android.net.Uri;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.activities.InstallActivity;
@ -115,13 +118,31 @@ public class PBWInstallHandler implements InstallHandler {
return;
}
File destDir;
GBDeviceApp app = mPBWReader.getGBDeviceApp();
try {
File destDir = new File(FileUtils.getExternalFilesDir() + "/pbw-cache");
destDir = new File(FileUtils.getExternalFilesDir() + "/pbw-cache");
destDir.mkdirs();
FileUtils.copyURItoFile(mContext, mUri, new File(destDir + "/" + app.getUUID().toString() + ".pbw"));
FileUtils.copyURItoFile(mContext, mUri, new File(destDir, app.getUUID().toString() + ".pbw"));
} catch (IOException e) {
LOG.error("Installation failed: " + e.getMessage(), e);
return;
}
File outputFile = new File(destDir, app.getUUID().toString() + ".json");
Writer writer;
try {
writer = new BufferedWriter(new FileWriter(outputFile));
} catch (IOException e) {
LOG.error("Failed to open output file: " + e.getMessage(), e);
return;
}
try {
LOG.info(app.getJSON().toString());
writer.write(app.getJSON().toString());
writer.close();
} catch (IOException e) {
LOG.error("Failed to write to output file: " + e.getMessage(), e);
}
}

View File

@ -1,5 +1,8 @@
package nodomain.freeyourgadget.gadgetbridge.impl;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.UUID;
public class GBDeviceApp {
@ -17,6 +20,30 @@ public class GBDeviceApp {
this.type = type;
}
public GBDeviceApp(JSONObject json) {
UUID uuid = UUID.fromString("00000000-0000-0000-0000-000000000000");
String name = "";
String creator = "";
String version = "";
Type type = Type.UNKNOWN;
try {
uuid = UUID.fromString(json.getString("uuid"));
name = json.getString("name");
creator = json.getString("creator");
version = json.getString("version");
type = Type.valueOf(json.getString("type"));
} catch (JSONException e) {
e.printStackTrace();
}
this.uuid = uuid;
this.name = name;
this.creator = creator;
this.version = version;
this.type = type;
}
public String getName() {
return name;
}
@ -43,4 +70,18 @@ public class GBDeviceApp {
APP_GENERIC,
APP_ACTIVITYTRACKER,
}
public JSONObject getJSON() {
JSONObject json = new JSONObject();
try {
json.put("uuid", uuid.toString());
json.put("name", name);
json.put("creator", creator);
json.put("version", version);
json.put("type", type.name());
} catch (JSONException e) {
e.printStackTrace();
}
return json;
}
}

View File

@ -8,6 +8,7 @@ import android.support.annotation.NonNull;
import android.util.Log;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
@ -15,6 +16,7 @@ import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.List;
@ -64,6 +66,20 @@ public class FileUtils {
fout.close();
}
public static String getStringFromFile(File file) throws IOException {
FileInputStream fin = new FileInputStream(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(fin));
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
reader.close();
fin.close();
return sb.toString();
}
/**
* Returns the existing external storage dir. The directory is guaranteed to
* exist and to be writable.