1
0
mirror of https://github.com/oliexdev/openScale.git synced 2025-08-30 19:49:59 +02:00

Use intent to open file for import

This commit is contained in:
Erik Johansson
2018-02-13 21:12:10 +01:00
parent 979638bc2f
commit 930630883c
2 changed files with 42 additions and 43 deletions

View File

@@ -21,8 +21,11 @@ import android.arch.persistence.room.Room;
import android.arch.persistence.room.RoomDatabase; import android.arch.persistence.room.RoomDatabase;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler; import android.os.Handler;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.provider.OpenableColumns;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.text.format.DateFormat; import android.text.format.DateFormat;
import android.util.Log; import android.util.Log;
@@ -46,9 +49,10 @@ import com.health.openscale.core.utils.CsvHelper;
import com.health.openscale.gui.fragments.FragmentUpdateListener; import com.health.openscale.gui.fragments.FragmentUpdateListener;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.ParseException; import java.text.ParseException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
@@ -328,10 +332,20 @@ public class OpenScale {
updateScaleData(); updateScaleData();
} }
public void importData(String filename) { private String getFilenameFromUri(Uri uri) {
Cursor cursor = context.getContentResolver().query(
uri, null, null, null, null);
cursor.moveToFirst();
return cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
}
public void importData(Uri uri) {
try { try {
final String filename = getFilenameFromUri(uri);
InputStream input = context.getContentResolver().openInputStream(uri);
List<ScaleMeasurement> csvScaleMeasurementList = List<ScaleMeasurement> csvScaleMeasurementList =
CsvHelper.importFrom(new BufferedReader(new FileReader(filename))); CsvHelper.importFrom(new BufferedReader(new InputStreamReader(input)));
final int userId = getSelectedScaleUser().getId(); final int userId = getSelectedScaleUser().getId();
for (ScaleMeasurement measurement : csvScaleMeasurementList) { for (ScaleMeasurement measurement : csvScaleMeasurementList) {
@@ -340,11 +354,11 @@ public class OpenScale {
measurementDAO.insertAll(csvScaleMeasurementList); measurementDAO.insertAll(csvScaleMeasurementList);
updateScaleData(); updateScaleData();
Toast.makeText(context, context.getString(R.string.info_data_imported) + " /sdcard" + filename, Toast.LENGTH_SHORT).show(); Toast.makeText(context, context.getString(R.string.info_data_imported) + " " + filename, Toast.LENGTH_SHORT).show();
} catch (IOException e) { } catch (IOException e) {
Toast.makeText(context, context.getString(R.string.error_importing) + e.getMessage(), Toast.LENGTH_SHORT).show(); Toast.makeText(context, context.getString(R.string.error_importing) + ": " + e.getMessage(), Toast.LENGTH_SHORT).show();
} catch (ParseException e) { } catch (ParseException e) {
Toast.makeText(context, context.getString(R.string.error_importing) + e.getMessage(), Toast.LENGTH_SHORT).show(); Toast.makeText(context, context.getString(R.string.error_importing) + ": " + e.getMessage(), Toast.LENGTH_SHORT).show();
} }
} }

View File

@@ -79,6 +79,7 @@ import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import static android.app.Activity.RESULT_OK;
import static android.util.TypedValue.COMPLEX_UNIT_DIP; import static android.util.TypedValue.COMPLEX_UNIT_DIP;
public class TableFragment extends Fragment implements FragmentUpdateListener { public class TableFragment extends Fragment implements FragmentUpdateListener {
@@ -94,7 +95,9 @@ public class TableFragment extends Fragment implements FragmentUpdateListener {
private ArrayList <MeasurementView> measurementsList; private ArrayList <MeasurementView> measurementsList;
private int selectedSubpageNr; private int selectedSubpageNr;
private static String SELECTED_SUBPAGE_NR_KEY = "selectedSubpageNr"; private static final String SELECTED_SUBPAGE_NR_KEY = "selectedSubpageNr";
private static final int IMPORT_DATA_REQUEST = 100;
public TableFragment() { public TableFragment() {
@@ -158,12 +161,9 @@ public class TableFragment extends Fragment implements FragmentUpdateListener {
@Override @Override
public boolean onMenuItemClick(MenuItem item) { public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) { switch (item.getItemId()) {
case R.id.importData: case R.id.importData:
if (PermissionHelper.requestReadPermission(getActivity())) { importTable();
importTable();
}
return true; return true;
case R.id.exportData: case R.id.exportData:
if (PermissionHelper.requestWritePermission(getActivity())) { if (PermissionHelper.requestWritePermission(getActivity())) {
@@ -285,11 +285,19 @@ public class TableFragment extends Fragment implements FragmentUpdateListener {
} }
} }
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == IMPORT_DATA_REQUEST && resultCode == RESULT_OK && data != null) {
OpenScale.getInstance(getContext()).importData(data.getData());
}
}
private void importTable() { private void importTable() {
int selectedUserId = OpenScale.getInstance(getContext()).getSelectedScaleUserId(); int selectedUserId = OpenScale.getInstance(getContext()).getSelectedScaleUserId();
if (selectedUserId == -1) if (selectedUserId == -1) {
{
AlertDialog.Builder infoDialog = new AlertDialog.Builder(getContext()); AlertDialog.Builder infoDialog = new AlertDialog.Builder(getContext());
infoDialog.setMessage(getResources().getString(R.string.info_no_selected_user)); infoDialog.setMessage(getResources().getString(R.string.info_no_selected_user));
@@ -298,36 +306,14 @@ public class TableFragment extends Fragment implements FragmentUpdateListener {
infoDialog.show(); infoDialog.show();
} }
else else {
{ Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
AlertDialog.Builder filenameDialog = new AlertDialog.Builder(getActivity()); intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("text/*");
filenameDialog.setTitle(getResources().getString(R.string.info_set_filename) + " /sdcard ..."); startActivityForResult(
Intent.createChooser(intent, getResources().getString(R.string.label_import)),
String exportFilename = prefs.getString("exportFilename", "/openScale_data_" + OpenScale.getInstance(getContext()).getSelectedScaleUser().getUserName() + ".csv"); IMPORT_DATA_REQUEST);
final EditText txtFilename = new EditText(tableView.getContext());
txtFilename.setText(exportFilename);
filenameDialog.setView(txtFilename);
filenameDialog.setPositiveButton(getResources().getString(R.string.label_ok), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
OpenScale.getInstance(getContext()).importData(Environment.getExternalStorageDirectory().getPath() + txtFilename.getText().toString());
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(tableView.getContext());
prefs.edit().putString("exportFilename", txtFilename.getText().toString()).commit();
updateOnView(OpenScale.getInstance(getContext()).getScaleMeasurementList());
}
});
filenameDialog.setNegativeButton(getResources().getString(R.string.label_cancel), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
filenameDialog.show();
} }
} }
@@ -365,7 +351,6 @@ public class TableFragment extends Fragment implements FragmentUpdateListener {
filenameDialog.show(); filenameDialog.show();
} }
private void shareTable() { private void shareTable() {
final ScaleUser selectedScaleUser = OpenScale.getInstance(getContext()).getSelectedScaleUser(); final ScaleUser selectedScaleUser = OpenScale.getInstance(getContext()).getSelectedScaleUser();