From b29c7e695a17f8d870d3b6cb2500cea8a09a6b49 Mon Sep 17 00:00:00 2001 From: Alinson Xavier Date: Tue, 5 Apr 2016 05:47:09 -0400 Subject: [PATCH] Save failed views to SD card root if writable --- .../isoron/uhabits/unit/views/ViewTest.java | 16 ++++++++-- app/src/debug/AndroidManifest.xml | 13 ++++++++- .../uhabits/helpers/DatabaseHelper.java | 29 ++++++++++++++----- 3 files changed, 47 insertions(+), 11 deletions(-) diff --git a/app/src/androidTest/java/org/isoron/uhabits/unit/views/ViewTest.java b/app/src/androidTest/java/org/isoron/uhabits/unit/views/ViewTest.java index d1953ff58..d5e69fa60 100644 --- a/app/src/androidTest/java/org/isoron/uhabits/unit/views/ViewTest.java +++ b/app/src/androidTest/java/org/isoron/uhabits/unit/views/ViewTest.java @@ -27,6 +27,7 @@ import android.view.MotionEvent; import android.view.View; import org.isoron.uhabits.BaseTest; +import org.isoron.uhabits.helpers.DatabaseHelper; import org.isoron.uhabits.helpers.UIHelper; import org.isoron.uhabits.tasks.BaseTask; import org.isoron.uhabits.views.HabitDataView; @@ -123,9 +124,18 @@ public class ViewTest extends BaseTest private String saveBitmap(String filename, String suffix, Bitmap bitmap) throws IOException { - String absolutePath = String.format("%s/Failed/%s", targetContext.getExternalCacheDir(), - filename.replaceAll("\\.png$", suffix + ".png")); - new File(absolutePath).getParentFile().mkdirs(); + File dir = DatabaseHelper.getSDCardDir("test-screenshots"); + if(dir == null) dir = DatabaseHelper.getFilesDir("test-screenshots"); + if(dir == null) throw new RuntimeException("Could not find suitable dir for screenshots"); + + filename = filename.replaceAll("\\.png$", suffix + ".png"); + String absolutePath = String.format("%s/%s", dir.getAbsolutePath(), filename); + + File parent = new File(absolutePath).getParentFile(); + if(!parent.exists() && !parent.mkdirs()) + throw new RuntimeException(String.format("Could not create dir: %s", + parent.getAbsolutePath())); + FileOutputStream out = new FileOutputStream(absolutePath); bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); diff --git a/app/src/debug/AndroidManifest.xml b/app/src/debug/AndroidManifest.xml index 01dd1532a..be52ed256 100644 --- a/app/src/debug/AndroidManifest.xml +++ b/app/src/debug/AndroidManifest.xml @@ -19,10 +19,21 @@ --> + xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:tools="http://schemas.android.com/tools"> + + + + diff --git a/app/src/main/java/org/isoron/uhabits/helpers/DatabaseHelper.java b/app/src/main/java/org/isoron/uhabits/helpers/DatabaseHelper.java index b8bb05bfb..d3c3d21e5 100644 --- a/app/src/main/java/org/isoron/uhabits/helpers/DatabaseHelper.java +++ b/app/src/main/java/org/isoron/uhabits/helpers/DatabaseHelper.java @@ -21,6 +21,7 @@ package org.isoron.uhabits.helpers; import android.content.Context; import android.database.Cursor; +import android.os.Environment; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.content.ContextCompat; @@ -127,10 +128,15 @@ public class DatabaseHelper } @Nullable - public static File getFilesDir(@Nullable String prefix) + public static File getSDCardDir(@Nullable String relativePath) { - if(prefix == null) prefix = ""; + File parents[] = new File[]{ Environment.getExternalStorageDirectory() }; + return getDir(parents, relativePath); + } + @Nullable + public static File getFilesDir(@Nullable String relativePath) + { Context context = HabitsApplication.getContext(); if(context == null) { @@ -138,15 +144,24 @@ public class DatabaseHelper return null; } - File chosenDir = null; File externalFilesDirs[] = ContextCompat.getExternalFilesDirs(context, null); + if(externalFilesDirs == null) { Log.e("DatabaseHelper", "getFilesDir: getExternalFilesDirs returned null"); return null; } - for(File dir : externalFilesDirs) + return getDir(externalFilesDirs, relativePath); + } + + @Nullable + private static File getDir(@NonNull File potentialParentDirs[], @Nullable String relativePath) + { + if(relativePath == null) relativePath = ""; + + File chosenDir = null; + for(File dir : potentialParentDirs) { if (dir == null || !dir.canWrite()) continue; chosenDir = dir; @@ -155,14 +170,14 @@ public class DatabaseHelper if(chosenDir == null) { - Log.e("DatabaseHelper", "getFilesDir: all external dirs are null or non-writable"); + Log.e("DatabaseHelper", "getDir: all potential parents are null or non-writable"); return null; } - File dir = new File(String.format("%s/%s/", chosenDir.getAbsolutePath(), prefix)); + File dir = new File(String.format("%s/%s/", chosenDir.getAbsolutePath(), relativePath)); if (!dir.exists() && !dir.mkdirs()) { - Log.e("DatabaseHelper", "getFilesDir: chosen dir does not exist and cannot be created"); + Log.e("DatabaseHelper", "getDir: chosen dir does not exist and cannot be created"); return null; }