mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 01:08:50 -06:00
More detailed logs in case getFilesDir fails
This commit is contained in:
@@ -55,6 +55,7 @@ import org.isoron.uhabits.BaseActivity;
|
|||||||
import org.isoron.uhabits.commands.Command;
|
import org.isoron.uhabits.commands.Command;
|
||||||
import org.isoron.uhabits.commands.ToggleRepetitionCommand;
|
import org.isoron.uhabits.commands.ToggleRepetitionCommand;
|
||||||
import org.isoron.uhabits.dialogs.FilePickerDialog;
|
import org.isoron.uhabits.dialogs.FilePickerDialog;
|
||||||
|
import org.isoron.uhabits.helpers.DatabaseHelper;
|
||||||
import org.isoron.uhabits.helpers.DateHelper;
|
import org.isoron.uhabits.helpers.DateHelper;
|
||||||
import org.isoron.uhabits.helpers.UIHelper.OnSavedListener;
|
import org.isoron.uhabits.helpers.UIHelper.OnSavedListener;
|
||||||
import org.isoron.uhabits.helpers.HintManager;
|
import org.isoron.uhabits.helpers.HintManager;
|
||||||
@@ -428,8 +429,12 @@ public class ListHabitsFragment extends Fragment
|
|||||||
|
|
||||||
public void showImportDialog()
|
public void showImportDialog()
|
||||||
{
|
{
|
||||||
File dir = activity.getExternalFilesDir(null);
|
File dir = DatabaseHelper.getFilesDir(null);
|
||||||
if(dir == null) return;
|
if(dir == null)
|
||||||
|
{
|
||||||
|
activity.showToast(R.string.could_not_import);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
FilePickerDialog picker = new FilePickerDialog(activity, dir);
|
FilePickerDialog picker = new FilePickerDialog(activity, dir);
|
||||||
picker.setListener(new FilePickerDialog.OnFileSelectedListener()
|
picker.setListener(new FilePickerDialog.OnFileSelectedListener()
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ import android.database.Cursor;
|
|||||||
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;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
import com.activeandroid.ActiveAndroid;
|
import com.activeandroid.ActiveAndroid;
|
||||||
import com.activeandroid.Cache;
|
import com.activeandroid.Cache;
|
||||||
@@ -126,14 +127,24 @@ public class DatabaseHelper
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public static File getFilesDir(String prefix)
|
public static File getFilesDir(@Nullable String prefix)
|
||||||
{
|
{
|
||||||
|
if(prefix == null) prefix = "";
|
||||||
|
|
||||||
Context context = HabitsApplication.getContext();
|
Context context = HabitsApplication.getContext();
|
||||||
if(context == null) return null;
|
if(context == null)
|
||||||
|
{
|
||||||
|
Log.e("DatabaseHelper", "getFilesDir: no application context available");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
File chosenDir = null;
|
File chosenDir = null;
|
||||||
File externalFilesDirs[] = ContextCompat.getExternalFilesDirs(context, null);
|
File externalFilesDirs[] = ContextCompat.getExternalFilesDirs(context, null);
|
||||||
if(externalFilesDirs == null) return null;
|
if(externalFilesDirs == null)
|
||||||
|
{
|
||||||
|
Log.e("DatabaseHelper", "getFilesDir: getExternalFilesDirs returned null");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
for(File dir : externalFilesDirs)
|
for(File dir : externalFilesDirs)
|
||||||
{
|
{
|
||||||
@@ -142,10 +153,18 @@ public class DatabaseHelper
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(chosenDir == null) return null;
|
if(chosenDir == null)
|
||||||
|
{
|
||||||
|
Log.e("DatabaseHelper", "getFilesDir: all external dirs 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(), prefix));
|
||||||
dir.mkdirs();
|
if (!dir.exists() && !dir.mkdirs())
|
||||||
|
{
|
||||||
|
Log.e("DatabaseHelper", "getFilesDir: chosen dir does not exist and cannot be created");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
return dir;
|
return dir;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user