mirror of
https://github.com/oliexdev/openScale.git
synced 2025-08-29 19:20:36 +02:00
Move import/export/share to toolbar menu
This commit is contained in:
@@ -26,6 +26,7 @@ import android.content.pm.PackageManager;
|
||||
import android.content.res.Configuration;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.os.Environment;
|
||||
import android.os.Handler;
|
||||
import android.os.Message;
|
||||
import android.preference.PreferenceManager;
|
||||
@@ -36,6 +37,7 @@ import android.support.design.widget.BottomNavigationView;
|
||||
import android.support.design.widget.NavigationView;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.FragmentManager;
|
||||
import android.support.v4.content.FileProvider;
|
||||
import android.support.v4.view.GravityCompat;
|
||||
import android.support.v4.widget.DrawerLayout;
|
||||
import android.support.v7.app.ActionBarDrawerToggle;
|
||||
@@ -45,12 +47,15 @@ import android.util.Log;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.widget.EditText;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.health.openscale.BuildConfig;
|
||||
import com.health.openscale.R;
|
||||
import com.health.openscale.core.OpenScale;
|
||||
import com.health.openscale.core.bluetooth.BluetoothCommunication;
|
||||
import com.health.openscale.core.datatypes.ScaleMeasurement;
|
||||
import com.health.openscale.core.datatypes.ScaleUser;
|
||||
import com.health.openscale.gui.activities.DataEntryActivity;
|
||||
import com.health.openscale.gui.activities.SettingsActivity;
|
||||
import com.health.openscale.gui.activities.UserSettingsActivity;
|
||||
@@ -60,6 +65,7 @@ import com.health.openscale.gui.fragments.StatisticsFragment;
|
||||
import com.health.openscale.gui.fragments.TableFragment;
|
||||
import com.health.openscale.gui.utils.PermissionHelper;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import cat.ereza.customactivityoncrash.config.CaocConfig;
|
||||
@@ -71,6 +77,8 @@ public class MainActivity extends AppCompatActivity
|
||||
private static int bluetoothStatusIcon = R.drawable.ic_bluetooth_disabled;
|
||||
private static MenuItem bluetoothStatus;
|
||||
|
||||
private static final int IMPORT_DATA_REQUEST = 100;
|
||||
|
||||
private Fragment currentFragment;
|
||||
private DrawerLayout drawerLayout;
|
||||
private Toolbar toolbar;
|
||||
@@ -383,6 +391,17 @@ public class MainActivity extends AppCompatActivity
|
||||
case R.id.action_bluetooth_status:
|
||||
invokeSearchBluetoothDevice();
|
||||
return true;
|
||||
case R.id.importData:
|
||||
importCsvFile();
|
||||
return true;
|
||||
case R.id.exportData:
|
||||
if (PermissionHelper.requestWritePermission(this)) {
|
||||
exportCsvFile();
|
||||
}
|
||||
return true;
|
||||
case R.id.shareData:
|
||||
shareCsvFile();
|
||||
return true;
|
||||
}
|
||||
|
||||
return super.onOptionsItemSelected(item);
|
||||
@@ -507,24 +526,128 @@ public class MainActivity extends AppCompatActivity
|
||||
bluetoothStatus.setIcon(getResources().getDrawable(bluetoothStatusIcon));
|
||||
}
|
||||
|
||||
private void importCsvFile() {
|
||||
int selectedUserId = OpenScale.getInstance(getApplicationContext()).getSelectedScaleUserId();
|
||||
|
||||
if (selectedUserId == -1) {
|
||||
AlertDialog.Builder infoDialog = new AlertDialog.Builder(this);
|
||||
|
||||
infoDialog.setMessage(getResources().getString(R.string.info_no_selected_user));
|
||||
infoDialog.setPositiveButton(getResources().getString(R.string.label_ok), null);
|
||||
|
||||
infoDialog.show();
|
||||
}
|
||||
else {
|
||||
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
|
||||
intent.addCategory(Intent.CATEGORY_OPENABLE);
|
||||
intent.setType("text/*");
|
||||
|
||||
startActivityForResult(
|
||||
Intent.createChooser(intent, getResources().getString(R.string.label_import)),
|
||||
IMPORT_DATA_REQUEST);
|
||||
}
|
||||
}
|
||||
|
||||
private void exportCsvFile() {
|
||||
AlertDialog.Builder filenameDialog = new AlertDialog.Builder(this);
|
||||
|
||||
filenameDialog.setTitle(getResources().getString(R.string.info_set_filename) + " "
|
||||
+ Environment.getExternalStorageDirectory().getPath());
|
||||
|
||||
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
|
||||
|
||||
final ScaleUser selectedScaleUser = OpenScale.getInstance(getApplicationContext()).getSelectedScaleUser();
|
||||
String exportFilename = prefs.getString("exportFilename" + selectedScaleUser.getId(),
|
||||
"openScale_data_" + selectedScaleUser.getUserName() + ".csv");
|
||||
|
||||
final EditText txtFilename = new EditText(this);
|
||||
txtFilename.setText(exportFilename);
|
||||
|
||||
filenameDialog.setView(txtFilename);
|
||||
|
||||
filenameDialog.setPositiveButton(getResources().getString(R.string.label_export), new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int id) {
|
||||
String fullPath = Environment.getExternalStorageDirectory().getPath() + "/" + txtFilename.getText().toString();
|
||||
|
||||
if (OpenScale.getInstance(getApplicationContext()).exportData(fullPath)) {
|
||||
prefs.edit().putString("exportFilename" + selectedScaleUser.getId(), txtFilename.getText().toString()).commit();
|
||||
Toast.makeText(getApplicationContext(), getResources().getString(
|
||||
R.string.info_data_exported) + " " + fullPath, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
filenameDialog.setNegativeButton(getResources().getString(R.string.label_cancel), new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int id) {
|
||||
dialog.dismiss();
|
||||
}
|
||||
});
|
||||
|
||||
filenameDialog.show();
|
||||
}
|
||||
|
||||
private void shareCsvFile() {
|
||||
final ScaleUser selectedScaleUser = OpenScale.getInstance(getApplicationContext()).getSelectedScaleUser();
|
||||
|
||||
File shareFile = new File(getApplicationContext().getCacheDir(),
|
||||
String.format("openScale %s.csv", selectedScaleUser.getUserName()));
|
||||
if (!OpenScale.getInstance(getApplicationContext()).exportData(shareFile.getPath())) {
|
||||
return;
|
||||
}
|
||||
|
||||
Intent intent = new Intent(Intent.ACTION_SEND);
|
||||
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
||||
intent.setType("text/csv");
|
||||
|
||||
final Uri uri = FileProvider.getUriForFile(
|
||||
getApplicationContext(), BuildConfig.APPLICATION_ID + ".fileprovider", shareFile);
|
||||
intent.putExtra(Intent.EXTRA_STREAM, uri);
|
||||
|
||||
intent.putExtra(Intent.EXTRA_SUBJECT,
|
||||
getResources().getString(R.string.label_share_subject, selectedScaleUser.getUserName()));
|
||||
|
||||
startActivity(Intent.createChooser(intent, getResources().getString(R.string.label_share)));
|
||||
}
|
||||
|
||||
@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(getApplicationContext()).importData(data.getData());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
|
||||
boolean permissionGranted = true;
|
||||
switch (requestCode) {
|
||||
case PermissionHelper.PERMISSIONS_REQUEST_ACCESS_COARSE_LOCATION:
|
||||
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||
invokeSearchBluetoothDevice();
|
||||
} else {
|
||||
setBluetoothStatusIcon(R.drawable.ic_bluetooth_disabled);
|
||||
Toast.makeText(getApplicationContext(), getResources().getString(R.string.permission_not_granted), Toast.LENGTH_SHORT).show();
|
||||
permissionGranted = false;
|
||||
}
|
||||
break;
|
||||
break;
|
||||
case PermissionHelper.PERMISSIONS_REQUEST_ACCESS_WRITE_STORAGE:
|
||||
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||
exportCsvFile();
|
||||
} else {
|
||||
permissionGranted = false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (!permissionGranted) {
|
||||
Toast.makeText(getApplicationContext(), getResources().getString(
|
||||
R.string.permission_not_granted), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
currentFragment.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
||||
}
|
||||
|
||||
|
||||
@SuppressLint("RestrictedApi")
|
||||
public static void disableShiftMode(BottomNavigationView view) {
|
||||
BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt(0);
|
||||
|
@@ -15,75 +15,52 @@
|
||||
*/
|
||||
package com.health.openscale.gui.fragments;
|
||||
|
||||
import android.app.AlertDialog;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.res.Configuration;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.Typeface;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.os.Environment;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.content.ContextCompat;
|
||||
import android.support.v4.content.FileProvider;
|
||||
import android.text.SpannableStringBuilder;
|
||||
import android.text.Spanned;
|
||||
import android.view.Gravity;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.BaseAdapter;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.ListView;
|
||||
import android.widget.PopupMenu;
|
||||
import android.widget.TableLayout;
|
||||
import android.widget.TableRow;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.health.openscale.BuildConfig;
|
||||
import com.health.openscale.R;
|
||||
import com.health.openscale.core.OpenScale;
|
||||
import com.health.openscale.core.datatypes.ScaleMeasurement;
|
||||
import com.health.openscale.core.datatypes.ScaleUser;
|
||||
import com.health.openscale.gui.activities.DataEntryActivity;
|
||||
import com.health.openscale.gui.utils.PermissionHelper;
|
||||
import com.health.openscale.gui.views.MeasurementView;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import static android.app.Activity.RESULT_OK;
|
||||
import static android.util.TypedValue.COMPLEX_UNIT_DIP;
|
||||
|
||||
public class TableFragment extends Fragment implements FragmentUpdateListener {
|
||||
private View tableView;
|
||||
private ListView tableDataView;
|
||||
private LinearLayout tableHeaderView;
|
||||
private SharedPreferences prefs;
|
||||
private LinearLayout subpageView;
|
||||
|
||||
private ImageView optionMenu;
|
||||
private PopupMenu popup;
|
||||
|
||||
private List<MeasurementView> measurementViews;
|
||||
|
||||
private int selectedSubpageNr;
|
||||
private static final String SELECTED_SUBPAGE_NR_KEY = "selectedSubpageNr";
|
||||
|
||||
private static final int IMPORT_DATA_REQUEST = 100;
|
||||
|
||||
public TableFragment() {
|
||||
|
||||
}
|
||||
@@ -101,10 +78,6 @@ public class TableFragment extends Fragment implements FragmentUpdateListener {
|
||||
tableDataView.setAdapter(new ListViewAdapter());
|
||||
tableDataView.setOnItemClickListener(new onClickListenerRow());
|
||||
|
||||
optionMenu = (ImageView) tableView.findViewById(R.id.optionMenu);
|
||||
|
||||
prefs = PreferenceManager.getDefaultSharedPreferences(tableView.getContext());
|
||||
|
||||
if (savedInstanceState == null) {
|
||||
selectedSubpageNr = 0;
|
||||
}
|
||||
@@ -119,37 +92,6 @@ public class TableFragment extends Fragment implements FragmentUpdateListener {
|
||||
measurement.setUpdateViews(false);
|
||||
}
|
||||
|
||||
optionMenu.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
popup.show();
|
||||
}
|
||||
});
|
||||
|
||||
popup = new PopupMenu(getContext(), optionMenu);
|
||||
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
|
||||
@Override
|
||||
public boolean onMenuItemClick(MenuItem item) {
|
||||
|
||||
switch (item.getItemId()) {
|
||||
case R.id.importData:
|
||||
importTable();
|
||||
return true;
|
||||
case R.id.exportData:
|
||||
if (PermissionHelper.requestWritePermission(getActivity())) {
|
||||
exportTable();
|
||||
}
|
||||
return true;
|
||||
case R.id.shareData:
|
||||
shareTable();
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
});
|
||||
popup.getMenuInflater().inflate(R.menu.table_menu, popup.getMenu());
|
||||
|
||||
OpenScale.getInstance(getContext()).registerFragment(this);
|
||||
|
||||
return tableView;
|
||||
@@ -254,117 +196,6 @@ 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() {
|
||||
int selectedUserId = OpenScale.getInstance(getContext()).getSelectedScaleUserId();
|
||||
|
||||
if (selectedUserId == -1) {
|
||||
AlertDialog.Builder infoDialog = new AlertDialog.Builder(getContext());
|
||||
|
||||
infoDialog.setMessage(getResources().getString(R.string.info_no_selected_user));
|
||||
|
||||
infoDialog.setPositiveButton(getResources().getString(R.string.label_ok), null);
|
||||
|
||||
infoDialog.show();
|
||||
}
|
||||
else {
|
||||
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
|
||||
intent.addCategory(Intent.CATEGORY_OPENABLE);
|
||||
intent.setType("text/*");
|
||||
|
||||
startActivityForResult(
|
||||
Intent.createChooser(intent, getResources().getString(R.string.label_import)),
|
||||
IMPORT_DATA_REQUEST);
|
||||
}
|
||||
}
|
||||
|
||||
private void exportTable() {
|
||||
AlertDialog.Builder filenameDialog = new AlertDialog.Builder(getActivity());
|
||||
|
||||
filenameDialog.setTitle(getResources().getString(R.string.info_set_filename) + " " + Environment.getExternalStorageDirectory().getPath());
|
||||
|
||||
final ScaleUser selectedScaleUser = OpenScale.getInstance(getContext()).getSelectedScaleUser();
|
||||
String exportFilename = prefs.getString("exportFilename" + selectedScaleUser.getId(), "openScale_data_" + selectedScaleUser.getUserName() + ".csv");
|
||||
|
||||
final EditText txtFilename = new EditText(tableView.getContext());
|
||||
txtFilename.setText(exportFilename);
|
||||
|
||||
filenameDialog.setView(txtFilename);
|
||||
|
||||
filenameDialog.setPositiveButton(getResources().getString(R.string.label_export), new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int id) {
|
||||
String fullPath = Environment.getExternalStorageDirectory().getPath() + "/" + txtFilename.getText().toString();
|
||||
|
||||
if (OpenScale.getInstance(getContext()).exportData(fullPath)) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(tableView.getContext());
|
||||
prefs.edit().putString("exportFilename" + selectedScaleUser.getId(), txtFilename.getText().toString()).commit();
|
||||
Toast.makeText(getContext(), getResources().getString(R.string.info_data_exported) + " " + fullPath, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
filenameDialog.setNegativeButton(getResources().getString(R.string.label_cancel), new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int id) {
|
||||
dialog.dismiss();
|
||||
}
|
||||
});
|
||||
|
||||
filenameDialog.show();
|
||||
}
|
||||
|
||||
private void shareTable() {
|
||||
final ScaleUser selectedScaleUser = OpenScale.getInstance(getContext()).getSelectedScaleUser();
|
||||
|
||||
File shareFile = new File(getContext().getCacheDir(),
|
||||
String.format("openScale %s.csv", selectedScaleUser.getUserName()));
|
||||
if (!OpenScale.getInstance(getContext()).exportData(shareFile.getPath())) {
|
||||
return;
|
||||
}
|
||||
|
||||
Intent intent = new Intent(Intent.ACTION_SEND);
|
||||
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
||||
intent.setType("text/csv");
|
||||
|
||||
final Uri uri = FileProvider.getUriForFile(
|
||||
getContext(), BuildConfig.APPLICATION_ID + ".fileprovider", shareFile);
|
||||
intent.putExtra(Intent.EXTRA_STREAM, uri);
|
||||
|
||||
intent.putExtra(Intent.EXTRA_SUBJECT,
|
||||
getResources().getString(R.string.label_share_subject, selectedScaleUser.getUserName()));
|
||||
|
||||
startActivity(Intent.createChooser(intent, getResources().getString(R.string.label_share)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
|
||||
switch (requestCode) {
|
||||
case PermissionHelper.PERMISSIONS_REQUEST_ACCESS_READ_STORAGE:
|
||||
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||
importTable();
|
||||
} else {
|
||||
Toast.makeText(getContext(), getResources().getString(R.string.permission_not_granted), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
break;
|
||||
case PermissionHelper.PERMISSIONS_REQUEST_ACCESS_WRITE_STORAGE:
|
||||
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||
exportTable();
|
||||
} else {
|
||||
Toast.makeText(getContext(), getResources().getString(R.string.permission_not_granted), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
||||
}
|
||||
|
||||
private class onClickListenerMoveSubpageLeft implements View.OnClickListener {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
|
Binary file not shown.
Before Width: | Height: | Size: 261 B |
Binary file not shown.
Before Width: | Height: | Size: 215 B |
Binary file not shown.
Before Width: | Height: | Size: 189 B |
Binary file not shown.
Before Width: | Height: | Size: 301 B |
Binary file not shown.
Before Width: | Height: | Size: 393 B |
Binary file not shown.
Before Width: | Height: | Size: 478 B |
@@ -38,13 +38,6 @@
|
||||
android:gravity="right"
|
||||
android:layout_weight="0.9"
|
||||
android:orientation="horizontal">
|
||||
<ImageView
|
||||
android:id="@+id/optionMenu"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="match_parent"
|
||||
android:scaleType="centerInside"
|
||||
android:tint="@android:color/white"
|
||||
app:srcCompat="@drawable/ic_share" />
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
||||
|
@@ -15,4 +15,19 @@
|
||||
android:title="+"
|
||||
app:showAsAction="always" />
|
||||
|
||||
<item
|
||||
android:id="@+id/importData"
|
||||
android:title="@string/label_import"
|
||||
app:showAsAction="never"/>
|
||||
|
||||
<item
|
||||
android:id="@+id/exportData"
|
||||
android:title="@string/label_export"
|
||||
app:showAsAction="never"/>
|
||||
|
||||
<item
|
||||
android:id="@+id/shareData"
|
||||
android:title="@string/label_share"
|
||||
app:showAsAction="never"/>
|
||||
|
||||
</menu>
|
||||
|
@@ -1,16 +0,0 @@
|
||||
<menu xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
tools:context="com.example.openscale.MainActivity">
|
||||
|
||||
<item
|
||||
android:id="@+id/importData"
|
||||
android:title="@string/label_import"/>
|
||||
|
||||
<item
|
||||
android:id="@+id/exportData"
|
||||
android:title="@string/label_export"/>
|
||||
|
||||
<item
|
||||
android:id="@+id/shareData"
|
||||
android:title="@string/label_share"/>
|
||||
</menu>
|
Reference in New Issue
Block a user