From 79f92b8495556e2a5fc6b5455f733eaf1cad2f4d Mon Sep 17 00:00:00 2001 From: Andreas Shimokawa Date: Sun, 6 Dec 2015 17:17:46 +0100 Subject: [PATCH] Fix installing pbw files from different URIs than local files on Firmware 3.x. Fixes #183 --- CHANGELOG.md | 2 ++ .../devices/pebble/PBWInstallHandler.java | 3 +-- .../gadgetbridge/util/FileUtils.java | 25 ++++++++++++++++++- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 067e50ae..e791a222 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ ###Changelog ####Next Version +* Pebble: fix regression in 0.6.7 when installing pbw/pbz files from content providers (eg. download manager) +* Pebble: fix installation of pbw files on firmware 3.x when using content providers (eg. download manager) + Treat Signal notifications as chat notifications * Fix crash when contacts cannot be read on Android 6.0 (non-granted pemissions) 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 7311a5ad..820e0004 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 @@ -108,11 +108,10 @@ public class PBWInstallHandler implements InstallHandler { } GBDeviceApp app = mPBWReader.getGBDeviceApp(); - File pbwFile = new File(mUri.getPath()); try { File destDir = new File(FileUtils.getExternalFilesDir() + "/pbw-cache"); destDir.mkdirs(); - FileUtils.copyFile(pbwFile, 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); } 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 ed7214e9..bc782b75 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/FileUtils.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/FileUtils.java @@ -1,10 +1,13 @@ package nodomain.freeyourgadget.gadgetbridge.util; +import android.content.ContentResolver; import android.content.Context; +import android.net.Uri; import android.os.Environment; import android.support.annotation.NonNull; import android.util.Log; +import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; @@ -42,6 +45,25 @@ public class FileUtils { } } + public static void copyURItoFile(Context ctx, Uri uri, File destFile) throws IOException { + ContentResolver cr = ctx.getContentResolver(); + InputStream fin; + try { + fin = new BufferedInputStream(cr.openInputStream(uri)); + } catch (FileNotFoundException e) { + e.printStackTrace(); + return; + } + FileOutputStream fout = new FileOutputStream(destFile); + byte[] buf = new byte[4096]; + while (fin.available() > 0) { + int bytes = fin.read(buf); + fout.write(buf, 0, bytes); + } + fin.close(); + fout.close(); + } + /** * Returns the existing external storage dir. The directory is guaranteed to * exist and to be writable. @@ -78,9 +100,10 @@ public class FileUtils { * Returns a list of directories to write to. The list is sorted by priority, * i.e. the first directory should be preferred, the last one is the least * preferred one. - * + *

* Note that the directories may not exist, so it is not guaranteed that you * can actually write to them. But when created, they *should* be writable. + * * @return the list of writable directories * @throws IOException */