mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Use Storage Access Framework when importing files
This commit is contained in:
@@ -20,6 +20,7 @@
|
|||||||
package org.isoron.uhabits.activities.habits.list;
|
package org.isoron.uhabits.activities.habits.list;
|
||||||
|
|
||||||
import android.content.*;
|
import android.content.*;
|
||||||
|
import android.net.*;
|
||||||
import android.support.annotation.*;
|
import android.support.annotation.*;
|
||||||
|
|
||||||
import org.isoron.uhabits.*;
|
import org.isoron.uhabits.*;
|
||||||
@@ -31,24 +32,30 @@ import org.isoron.uhabits.commands.*;
|
|||||||
import org.isoron.uhabits.intents.*;
|
import org.isoron.uhabits.intents.*;
|
||||||
import org.isoron.uhabits.io.*;
|
import org.isoron.uhabits.io.*;
|
||||||
import org.isoron.uhabits.models.*;
|
import org.isoron.uhabits.models.*;
|
||||||
|
import org.isoron.uhabits.utils.*;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
|
||||||
import javax.inject.*;
|
import javax.inject.*;
|
||||||
|
|
||||||
|
import static android.os.Build.VERSION.*;
|
||||||
|
import static android.os.Build.VERSION_CODES.*;
|
||||||
|
|
||||||
@ActivityScope
|
@ActivityScope
|
||||||
public class ListHabitsScreen extends BaseScreen
|
public class ListHabitsScreen extends BaseScreen
|
||||||
implements CommandRunner.Listener
|
implements CommandRunner.Listener
|
||||||
{
|
{
|
||||||
public static final int RESULT_BUG_REPORT = 4;
|
public static final int RESULT_IMPORT_DATA = 1;
|
||||||
|
|
||||||
public static final int RESULT_EXPORT_CSV = 2;
|
public static final int RESULT_EXPORT_CSV = 2;
|
||||||
|
|
||||||
public static final int RESULT_EXPORT_DB = 3;
|
public static final int RESULT_EXPORT_DB = 3;
|
||||||
|
|
||||||
|
public static final int RESULT_BUG_REPORT = 4;
|
||||||
|
|
||||||
public static final int RESULT_REPAIR_DB = 5;
|
public static final int RESULT_REPAIR_DB = 5;
|
||||||
|
|
||||||
public static final int RESULT_IMPORT_DATA = 1;
|
public static final int REQUEST_OPEN_DOCUMENT = 6;
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private ListHabitsController controller;
|
private ListHabitsController controller;
|
||||||
@@ -128,6 +135,31 @@ public class ListHabitsScreen extends BaseScreen
|
|||||||
{
|
{
|
||||||
if (controller == null) return;
|
if (controller == null) return;
|
||||||
|
|
||||||
|
if (requestCode == REQUEST_OPEN_DOCUMENT)
|
||||||
|
{
|
||||||
|
if(resultCode != BaseActivity.RESULT_OK) return;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// TODO: Make it async
|
||||||
|
// TODO: Remove temporary file at the end of operation
|
||||||
|
Uri uri = data.getData();
|
||||||
|
ContentResolver cr = activity.getContentResolver();
|
||||||
|
InputStream is = cr.openInputStream(uri);
|
||||||
|
|
||||||
|
File cacheDir = activity.getCacheDir();
|
||||||
|
File tempFile = File.createTempFile("import", "", cacheDir);
|
||||||
|
|
||||||
|
FileUtils.copy(is, tempFile);
|
||||||
|
controller.onImportData(tempFile);
|
||||||
|
}
|
||||||
|
catch (IOException e)
|
||||||
|
{
|
||||||
|
showMessage(R.string.could_not_import);
|
||||||
|
e.printStackTrace();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
switch (resultCode)
|
switch (resultCode)
|
||||||
{
|
{
|
||||||
case RESULT_IMPORT_DATA:
|
case RESULT_IMPORT_DATA:
|
||||||
@@ -208,6 +240,21 @@ public class ListHabitsScreen extends BaseScreen
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void showImportScreen()
|
public void showImportScreen()
|
||||||
|
{
|
||||||
|
if (SDK_INT < KITKAT)
|
||||||
|
{
|
||||||
|
showImportScreenPreKitKat();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
|
||||||
|
intent.addCategory(Intent.CATEGORY_OPENABLE);
|
||||||
|
intent.setType("*/*");
|
||||||
|
|
||||||
|
activity.startActivityForResult(intent, REQUEST_OPEN_DOCUMENT);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void showImportScreenPreKitKat()
|
||||||
{
|
{
|
||||||
File dir = dirFinder.findStorageDir(null);
|
File dir = dirFinder.findStorageDir(null);
|
||||||
|
|
||||||
@@ -221,6 +268,7 @@ public class ListHabitsScreen extends BaseScreen
|
|||||||
|
|
||||||
if (controller != null)
|
if (controller != null)
|
||||||
picker.setListener(file -> controller.onImportData(file));
|
picker.setListener(file -> controller.onImportData(file));
|
||||||
|
|
||||||
activity.showDialog(picker.getDialog());
|
activity.showDialog(picker.getDialog());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user