mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 01:08:50 -06:00
Save failed views to SD card root if writable
This commit is contained in:
@@ -27,6 +27,7 @@ import android.view.MotionEvent;
|
|||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
|
||||||
import org.isoron.uhabits.BaseTest;
|
import org.isoron.uhabits.BaseTest;
|
||||||
|
import org.isoron.uhabits.helpers.DatabaseHelper;
|
||||||
import org.isoron.uhabits.helpers.UIHelper;
|
import org.isoron.uhabits.helpers.UIHelper;
|
||||||
import org.isoron.uhabits.tasks.BaseTask;
|
import org.isoron.uhabits.tasks.BaseTask;
|
||||||
import org.isoron.uhabits.views.HabitDataView;
|
import org.isoron.uhabits.views.HabitDataView;
|
||||||
@@ -123,9 +124,18 @@ public class ViewTest extends BaseTest
|
|||||||
private String saveBitmap(String filename, String suffix, Bitmap bitmap)
|
private String saveBitmap(String filename, String suffix, Bitmap bitmap)
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
String absolutePath = String.format("%s/Failed/%s", targetContext.getExternalCacheDir(),
|
File dir = DatabaseHelper.getSDCardDir("test-screenshots");
|
||||||
filename.replaceAll("\\.png$", suffix + ".png"));
|
if(dir == null) dir = DatabaseHelper.getFilesDir("test-screenshots");
|
||||||
new File(absolutePath).getParentFile().mkdirs();
|
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);
|
FileOutputStream out = new FileOutputStream(absolutePath);
|
||||||
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
|
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
|
||||||
|
|
||||||
|
|||||||
@@ -19,10 +19,21 @@
|
|||||||
-->
|
-->
|
||||||
<manifest
|
<manifest
|
||||||
package="org.isoron.uhabits"
|
package="org.isoron.uhabits"
|
||||||
xmlns:android="http://schemas.android.com/apk/res/android">
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:tools="http://schemas.android.com/tools">
|
||||||
|
|
||||||
<uses-permission android:name="android.permission.SET_ANIMATION_SCALE"/>
|
<uses-permission android:name="android.permission.SET_ANIMATION_SCALE"/>
|
||||||
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
|
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
|
||||||
<uses-permission android:name="android.permission.WAKE_LOCK"/>
|
<uses-permission android:name="android.permission.WAKE_LOCK"/>
|
||||||
|
|
||||||
|
<uses-permission
|
||||||
|
android:name="android.permission.READ_EXTERNAL_STORAGE"
|
||||||
|
tools:replace="maxSdkVersion"
|
||||||
|
android:maxSdkVersion="99" />
|
||||||
|
|
||||||
|
<uses-permission
|
||||||
|
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
|
||||||
|
tools:replace="maxSdkVersion"
|
||||||
|
android:maxSdkVersion="99" />
|
||||||
|
|
||||||
</manifest>
|
</manifest>
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ package org.isoron.uhabits.helpers;
|
|||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.database.Cursor;
|
import android.database.Cursor;
|
||||||
|
import android.os.Environment;
|
||||||
import android.support.annotation.NonNull;
|
import android.support.annotation.NonNull;
|
||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
import android.support.v4.content.ContextCompat;
|
import android.support.v4.content.ContextCompat;
|
||||||
@@ -127,10 +128,15 @@ public class DatabaseHelper
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@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();
|
Context context = HabitsApplication.getContext();
|
||||||
if(context == null)
|
if(context == null)
|
||||||
{
|
{
|
||||||
@@ -138,15 +144,24 @@ public class DatabaseHelper
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
File chosenDir = null;
|
|
||||||
File externalFilesDirs[] = ContextCompat.getExternalFilesDirs(context, null);
|
File externalFilesDirs[] = ContextCompat.getExternalFilesDirs(context, null);
|
||||||
|
|
||||||
if(externalFilesDirs == null)
|
if(externalFilesDirs == null)
|
||||||
{
|
{
|
||||||
Log.e("DatabaseHelper", "getFilesDir: getExternalFilesDirs returned null");
|
Log.e("DatabaseHelper", "getFilesDir: getExternalFilesDirs returned null");
|
||||||
return 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;
|
if (dir == null || !dir.canWrite()) continue;
|
||||||
chosenDir = dir;
|
chosenDir = dir;
|
||||||
@@ -155,14 +170,14 @@ public class DatabaseHelper
|
|||||||
|
|
||||||
if(chosenDir == null)
|
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;
|
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())
|
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;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user