|
|
@ -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();
|
|
|
|
|
|
|
|
File tempFile = File.createTempFile("import", "", cacheDir);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
FileUtils.copy(is, tempFile);
|
|
|
|
if (requestCode == REQUEST_SETTINGS)
|
|
|
|
controller.onImportData(tempFile);
|
|
|
|
onSettingsResult(resultCode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (IOException e)
|
|
|
|
|
|
|
|
|
|
|
|
private void onSettingsResult(int resultCode)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
showMessage(R.string.could_not_import);
|
|
|
|
if (controller == null) return;
|
|
|
|
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()
|
|
|
|