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 62a02ba64..a47244748 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AppManagerActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AppManagerActivity.java @@ -92,7 +92,7 @@ public class AppManagerActivity extends Activity { } } } catch (IOException e) { - e.printStackTrace(); + 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 ababb0797..7311a5ad3 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 @@ -3,6 +3,9 @@ package nodomain.freeyourgadget.gadgetbridge.devices.pebble; import android.content.Context; import android.net.Uri; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.io.File; import java.io.IOException; @@ -16,6 +19,7 @@ import nodomain.freeyourgadget.gadgetbridge.model.GenericItem; import nodomain.freeyourgadget.gadgetbridge.util.FileUtils; public class PBWInstallHandler implements InstallHandler { + private static final Logger LOG = LoggerFactory.getLogger(PBWInstallHandler.class); private final Context mContext; private PBWReader mPBWReader; @@ -110,7 +114,7 @@ public class PBWInstallHandler implements InstallHandler { destDir.mkdirs(); FileUtils.copyFile(pbwFile, new File(destDir + "/" + app.getUUID().toString() + ".pbw")); } catch (IOException e) { - e.printStackTrace(); + LOG.error("Installation failed: " + e.getMessage(), e); } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleIoThread.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleIoThread.java index df388c394..24c48c098 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleIoThread.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleIoThread.java @@ -491,7 +491,7 @@ public class PebbleIoThread extends GBDeviceIoThread { try { installApp(Uri.fromFile(new File(FileUtils.getExternalFilesDir() + "/pbw-cache/" + appMgmt.uuid.toString() + ".pbw")), appMgmt.token); } catch (IOException e) { - e.printStackTrace(); + LOG.error("Error installing app: " + e.getMessage(), e); } break; default: 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 45a4f8919..258c8c6ef 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/FileUtils.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/FileUtils.java @@ -2,6 +2,7 @@ package nodomain.freeyourgadget.gadgetbridge.util; import android.content.Context; import android.os.Environment; +import android.support.annotation.NonNull; import android.util.Log; import java.io.ByteArrayOutputStream; @@ -42,7 +43,8 @@ public class FileUtils { } /** - * Returns the existing external storage dir. + * Returns the existing external storage dir. The directory is guaranteed to + * exist and to be writable. * * @throws IOException when the directory is not available */ @@ -72,6 +74,17 @@ 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 + */ + @NonNull private static List getWritableExternalFilesDirs() throws IOException { Context context = GBApplication.getContext(); File[] dirs = context.getExternalFilesDirs(null); @@ -87,17 +100,13 @@ public class FileUtils { if (!dir.exists() && !dir.mkdirs()) { continue; } -// if (!dir.canWrite() || !Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState(dir))) { + // the first directory is also the primary external storage, i.e. the same as Environment.getExternalFilesDir() + // TODO: check the mount state of *all* dirs when switching to later API level if (!dir.canWrite() || (i == 0 && !Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))) { Log.i(TAG, "ignoring non-writable external storage dir: " + dir); continue; } -// if (Environment.isExternalStorageEmulated(dir)) { - if (i == 0 && Environment.isExternalStorageEmulated()) { - result.add(dir); // add last - } else { - result.add(0, dir); // add first - } + result.add(dir); // add last } return result; }