From 1c3e0b628b7987a5dc38e0d382b92a218d18a78c Mon Sep 17 00:00:00 2001 From: Andreas Shimokawa Date: Mon, 7 Dec 2015 18:15:26 +0100 Subject: [PATCH] Pebble: store app details in pbw-cache and display them in app manager on firmware 3.x Improves #93 --- CHANGELOG.md | 1 + .../activities/AppManagerActivity.java | 30 ++++++++++---- .../devices/pebble/PBWInstallHandler.java | 25 ++++++++++- .../gadgetbridge/impl/GBDeviceApp.java | 41 +++++++++++++++++++ .../gadgetbridge/util/FileUtils.java | 16 ++++++++ 5 files changed, 102 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f2c654f42..769deb372 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AppManagerActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AppManagerActivity.java index b219b0654..69bf7c3ac 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AppManagerActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AppManagerActivity.java @@ -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 getCachedApps() { List 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; } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/pebble/PBWInstallHandler.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/pebble/PBWInstallHandler.java index 0c0252aee..dda943393 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/pebble/PBWInstallHandler.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/pebble/PBWInstallHandler.java @@ -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); } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/impl/GBDeviceApp.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/impl/GBDeviceApp.java index a860fe6c7..fd4d151a0 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/impl/GBDeviceApp.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/impl/GBDeviceApp.java @@ -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; + } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/FileUtils.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/FileUtils.java index bc782b75b..491646377 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/FileUtils.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/FileUtils.java @@ -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.