mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 01:08:50 -06:00
Remove temp file after importing
This commit is contained in:
@@ -128,7 +128,8 @@ public class ListHabitsController
|
|||||||
taskRunner.execute(() -> habitList.reorder(from, to));
|
taskRunner.execute(() -> habitList.reorder(from, to));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onImportData(@NonNull File file)
|
public void onImportData(@NonNull File file,
|
||||||
|
@NonNull OnFinishedListener finishedListener)
|
||||||
{
|
{
|
||||||
taskRunner.execute(importTaskFactory.create(file, result -> {
|
taskRunner.execute(importTaskFactory.create(file, result -> {
|
||||||
switch (result)
|
switch (result)
|
||||||
@@ -146,6 +147,8 @@ public class ListHabitsController
|
|||||||
screen.showMessage(R.string.could_not_import);
|
screen.showMessage(R.string.could_not_import);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
finishedListener.onFinish();
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -208,4 +211,9 @@ public class ListHabitsController
|
|||||||
prefs.updateLastHint(-1, DateUtils.getStartOfToday());
|
prefs.updateLastHint(-1, DateUtils.getStartOfToday());
|
||||||
screen.showIntroScreen();
|
screen.showIntroScreen();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public interface OnFinishedListener
|
||||||
|
{
|
||||||
|
void onFinish();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,6 +19,7 @@
|
|||||||
|
|
||||||
package org.isoron.uhabits.activities.habits.list;
|
package org.isoron.uhabits.activities.habits.list;
|
||||||
|
|
||||||
|
import android.app.*;
|
||||||
import android.content.*;
|
import android.content.*;
|
||||||
import android.net.*;
|
import android.net.*;
|
||||||
import android.support.annotation.*;
|
import android.support.annotation.*;
|
||||||
@@ -57,6 +58,8 @@ public class ListHabitsScreen extends BaseScreen
|
|||||||
|
|
||||||
public static final int REQUEST_OPEN_DOCUMENT = 6;
|
public static final int REQUEST_OPEN_DOCUMENT = 6;
|
||||||
|
|
||||||
|
public static final int REQUEST_SETTINGS = 7;
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private ListHabitsController controller;
|
private ListHabitsController controller;
|
||||||
|
|
||||||
@@ -133,32 +136,16 @@ public class ListHabitsScreen extends BaseScreen
|
|||||||
@Override
|
@Override
|
||||||
public void onResult(int requestCode, int resultCode, Intent data)
|
public void onResult(int requestCode, int resultCode, Intent data)
|
||||||
{
|
{
|
||||||
if (controller == null) return;
|
|
||||||
|
|
||||||
if (requestCode == REQUEST_OPEN_DOCUMENT)
|
if (requestCode == REQUEST_OPEN_DOCUMENT)
|
||||||
{
|
onOpenDocumentResult(resultCode, data);
|
||||||
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();
|
if (requestCode == REQUEST_SETTINGS)
|
||||||
File tempFile = File.createTempFile("import", "", cacheDir);
|
onSettingsResult(resultCode);
|
||||||
|
}
|
||||||
|
|
||||||
FileUtils.copy(is, tempFile);
|
private void onSettingsResult(int resultCode)
|
||||||
controller.onImportData(tempFile);
|
{
|
||||||
}
|
if (controller == null) return;
|
||||||
catch (IOException e)
|
|
||||||
{
|
|
||||||
showMessage(R.string.could_not_import);
|
|
||||||
e.printStackTrace();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (resultCode)
|
switch (resultCode)
|
||||||
{
|
{
|
||||||
@@ -184,6 +171,30 @@ public class ListHabitsScreen extends BaseScreen
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void onOpenDocumentResult(int resultCode, Intent data)
|
||||||
|
{
|
||||||
|
if (controller == null) return;
|
||||||
|
if (resultCode != Activity.RESULT_OK) return;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Uri uri = data.getData();
|
||||||
|
ContentResolver cr = activity.getContentResolver();
|
||||||
|
InputStream is = cr.openInputStream(uri);
|
||||||
|
|
||||||
|
File cacheDir = activity.getExternalCacheDir();
|
||||||
|
File tempFile = File.createTempFile("import", "", cacheDir);
|
||||||
|
|
||||||
|
FileUtils.copy(is, tempFile);
|
||||||
|
controller.onImportData(tempFile, () -> tempFile.delete());
|
||||||
|
}
|
||||||
|
catch (IOException e)
|
||||||
|
{
|
||||||
|
showMessage(R.string.could_not_import);
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void setController(@Nullable ListHabitsController controller)
|
public void setController(@Nullable ListHabitsController controller)
|
||||||
{
|
{
|
||||||
this.controller = controller;
|
this.controller = controller;
|
||||||
@@ -267,7 +278,7 @@ public class ListHabitsScreen extends BaseScreen
|
|||||||
FilePickerDialog picker = filePickerDialogFactory.create(dir);
|
FilePickerDialog picker = filePickerDialogFactory.create(dir);
|
||||||
|
|
||||||
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());
|
||||||
}
|
}
|
||||||
@@ -281,7 +292,7 @@ public class ListHabitsScreen extends BaseScreen
|
|||||||
public void showSettingsScreen()
|
public void showSettingsScreen()
|
||||||
{
|
{
|
||||||
Intent intent = intentFactory.startSettingsActivity(activity);
|
Intent intent = intentFactory.startSettingsActivity(activity);
|
||||||
activity.startActivityForResult(intent, 0);
|
activity.startActivityForResult(intent, REQUEST_SETTINGS);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void toggleNightMode()
|
public void toggleNightMode()
|
||||||
|
|||||||
@@ -38,10 +38,11 @@ import org.junit.runners.*;
|
|||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
|
||||||
|
import static org.isoron.uhabits.activities.habits.list.ListHabitsScreen.*;
|
||||||
import static org.mockito.Matchers.any;
|
import static org.mockito.Matchers.any;
|
||||||
import static org.mockito.Mockito.anyInt;
|
import static org.mockito.Mockito.anyInt;
|
||||||
import static org.mockito.Mockito.eq;
|
|
||||||
import static org.mockito.Mockito.*;
|
import static org.mockito.Mockito.*;
|
||||||
|
import static org.mockito.Mockito.eq;
|
||||||
|
|
||||||
@RunWith(JUnit4.class)
|
@RunWith(JUnit4.class)
|
||||||
public class ListHabitsScreenTest extends BaseUnitTest
|
public class ListHabitsScreenTest extends BaseUnitTest
|
||||||
@@ -124,28 +125,28 @@ public class ListHabitsScreenTest extends BaseUnitTest
|
|||||||
@Test
|
@Test
|
||||||
public void testOnResult_bugReport()
|
public void testOnResult_bugReport()
|
||||||
{
|
{
|
||||||
screen.onResult(0, ListHabitsScreen.RESULT_BUG_REPORT, null);
|
screen.onResult(REQUEST_SETTINGS, RESULT_BUG_REPORT, null);
|
||||||
verify(controller).onSendBugReport();
|
verify(controller).onSendBugReport();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testOnResult_exportCSV()
|
public void testOnResult_exportCSV()
|
||||||
{
|
{
|
||||||
screen.onResult(0, ListHabitsScreen.RESULT_EXPORT_CSV, null);
|
screen.onResult(REQUEST_SETTINGS, RESULT_EXPORT_CSV, null);
|
||||||
verify(controller).onExportCSV();
|
verify(controller).onExportCSV();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testOnResult_exportDB()
|
public void testOnResult_exportDB()
|
||||||
{
|
{
|
||||||
screen.onResult(0, ListHabitsScreen.RESULT_EXPORT_DB, null);
|
screen.onResult(REQUEST_SETTINGS, RESULT_EXPORT_DB, null);
|
||||||
verify(controller).onExportDB();
|
verify(controller).onExportDB();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testOnResult_importData()
|
public void testOnResult_importData()
|
||||||
{
|
{
|
||||||
screen.onResult(0, ListHabitsScreen.RESULT_IMPORT_DATA, null);
|
screen.onResult(REQUEST_SETTINGS, RESULT_IMPORT_DATA, null);
|
||||||
testShowImportScreen();
|
testShowImportScreen();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user