Skip to content

Commit

Permalink
Create IgnorePointsManage, rename WorkoutListOperations to WorkoutsLi…
Browse files Browse the repository at this point in the history
…stManager, move creating gpx file name to GPXWriter from WorkoutListManager
  • Loading branch information
sylwke3100 committed Nov 21, 2015
1 parent 911bbf4 commit b42585b
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 54 deletions.
29 changes: 27 additions & 2 deletions src/com/sylwke3100/freetrackgps/GPXWriter.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
package com.sylwke3100.freetrackgps;

import android.os.Environment;
import android.util.Xml;
import org.xmlpull.v1.XmlSerializer;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.StringTokenizer;


public class GPXWriter {
private static final SimpleDateFormat dateFormat =
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
private SimpleDateFormat fileGpxFormat = new SimpleDateFormat("yyyy-MM-dd-HH_mm_ss");
private Boolean isOpen = false;
private FileOutputStream gpxOutputStream;
private XmlSerializer gpxSerializer;
private StringBuffer fileNameBuffer;

public GPXWriter(String fileName, long startTime, String nameWorkout) {
public GPXWriter(long startTime, String nameWorkout) {
String preparedFilename = prepareFilename(startTime);
try {
gpxOutputStream = new FileOutputStream(fileName);
gpxOutputStream = new FileOutputStream(preparedFilename);
isOpen = true;
} catch (FileNotFoundException fnfe) {
isOpen = false;
Expand All @@ -38,6 +45,24 @@ public GPXWriter(String fileName, long startTime, String nameWorkout) {
createMetadata(startTime, nameWorkout);
}

private String prepareFilename(long startTime){
fileNameBuffer = new StringBuffer();
File dir = new File(
Environment.getExternalStorageDirectory() + DefaultValues.defaultFolderWithWorkout);
if (!(dir.exists() && dir.isDirectory()))
dir.mkdir();
fileNameBuffer.append(
Environment.getExternalStorageDirectory() + DefaultValues.defaultFolderWithWorkout);
fileNameBuffer.append(fileGpxFormat.format(new Date(startTime)) + "."
+ DefaultValues.defaultFileFormat);
return fileNameBuffer.toString();
}

public String getFilename(){
return fileNameBuffer.toString();
}


private void createHeader() {
if (isOpen)
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class IgnorePointsAddFromMapActivity extends Activity {
private MapView mMapView;
private LocationSharing sharedLocation;
private GeoPoint ignorePoint;
private DatabaseManager localInstanceDatabase;
private IgnorePointsManager ignorePointsManager;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -55,7 +55,7 @@ public boolean longPressHelper(GeoPoint geoPoint) {
}
});
mMapView.getOverlays().add(0, eventsOverlay);
localInstanceDatabase = new DatabaseManager(getApplicationContext());
ignorePointsManager = new IgnorePointsManager(getApplicationContext());
}

public boolean onCreateOptionsMenu(Menu menu) {
Expand Down Expand Up @@ -115,7 +115,7 @@ public boolean onItemLongPress(int i, OverlayItem overlayItem) {

public void onAddIgnorePointsFromLocation(String ignorePointName) {
if (ignorePoint != null) {
if (!localInstanceDatabase
if (!ignorePointsManager
.addIgnorePoint(ignorePoint.getLatitude(), ignorePoint.getLongitude(),
ignorePointName))
Toast.makeText(getBaseContext(), R.string.ignorePointsExists, Toast.LENGTH_LONG)
Expand Down
25 changes: 9 additions & 16 deletions src/com/sylwke3100/freetrackgps/IgnorePointsListActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,26 @@
import android.view.*;
import android.widget.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class IgnorePointsListActivity extends Activity {
List<IgnorePointsListElement> localListIgnore;
private ListView ignorePonitsList;
private SimpleAdapter simpleAdapter;
private DatabaseManager localInstanceDatabase;
private IgnorePointsManager ignorePointsManager;

@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ignorepoints_list);
ignorePonitsList = (ListView) findViewById(R.id.listIgnorePointsView);
registerForContextMenu(ignorePonitsList);
localInstanceDatabase = new DatabaseManager(getBaseContext());
ignorePointsManager = new IgnorePointsManager(this);
onUpdateIgnoreList();
}

public void onUpdateIgnoreList() {
ArrayList<HashMap<String, String>> baseList = new ArrayList<HashMap<String, String>>();
localListIgnore = localInstanceDatabase.getIgnorePointsList();
for (IgnorePointsListElement element : localListIgnore) {
baseList.add(element.getPreparedHashMapToView());
}
simpleAdapter = new SimpleAdapter(this, baseList, R.layout.textview_ignore_points,
new String[] {"name", "points"}, new int[] {R.id.NameTextView, R.id.PointsTextView});
simpleAdapter = new SimpleAdapter(this, ignorePointsManager.getIgnorePointsPreparedList(),
R.layout.textview_ignore_points, new String[] {"name", "points"},
new int[] {R.id.NameTextView, R.id.PointsTextView});
ignorePonitsList.setAdapter(simpleAdapter);
}

Expand Down Expand Up @@ -75,7 +68,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
return true;
}

public void onResume(){
public void onResume() {
super.onResume();
onUpdateIgnoreList();
}
Expand All @@ -86,8 +79,8 @@ public void onEmptyErrorAlertShow() {
}

public void onDeleteIgnorePoints(long position) {
IgnorePointsListElement element = localListIgnore.get((int) position);
this.localInstanceDatabase.deleteIgnorePoint(element.latitude, element.longitude);
IgnorePointsListElement element = ignorePointsManager.getIgnorePoint((int) position);
ignorePointsManager.deleteIgnorePoint(element);
}

public void onAddIgnorePointsAlertDialogShow() {
Expand Down Expand Up @@ -119,7 +112,7 @@ public void addIgnorePointsFromAlertDialog(EditText inputLat, EditText inputLon,
Double longitude = Double.parseDouble(inputLon.getText().toString());
String name = inputName.getText().toString();
if (latitude != 0 && longitude != 0 && !name.isEmpty())
if (localInstanceDatabase.addIgnorePoint(latitude, longitude, name) == false)
if (ignorePointsManager.addIgnorePoint(latitude, longitude, name) == false)
Toast.makeText(getBaseContext(), R.string.ignorePointsExists, Toast.LENGTH_LONG)
.show();
onUpdateIgnoreList();
Expand Down
42 changes: 42 additions & 0 deletions src/com/sylwke3100/freetrackgps/IgnorePointsManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.sylwke3100.freetrackgps;


import android.content.Context;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class IgnorePointsManager {
private Context localContext;
private DatabaseManager databaseHandler;
private List<IgnorePointsListElement> localListIgnore;

public IgnorePointsManager(Context globalContext) {
localContext = globalContext;
databaseHandler = new DatabaseManager(globalContext);

}

public ArrayList<HashMap<String, String>> getIgnorePointsPreparedList() {
ArrayList<HashMap<String, String>> baseList = new ArrayList<HashMap<String, String>>();
localListIgnore = databaseHandler.getIgnorePointsList();
for (IgnorePointsListElement element : localListIgnore) {
baseList.add(element.getPreparedHashMapToView());
}
return baseList;
}

public IgnorePointsListElement getIgnorePoint(int id){
return localListIgnore.get(id);
}

public void deleteIgnorePoint(IgnorePointsListElement point){
databaseHandler.deleteIgnorePoint(point.latitude, point.longitude);
}

public boolean addIgnorePoint(double latitude,double longitude,String name){
return databaseHandler.addIgnorePoint(latitude, longitude, name);
}

}
3 changes: 1 addition & 2 deletions src/com/sylwke3100/freetrackgps/WorkoutInfoMapFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public class WorkoutInfoMapFragment extends Fragment {
private static final SimpleDateFormat formatDate = new SimpleDateFormat("HH:mm dd.MM.yyyy");
private MapView mMapView;
private DatabaseManager workoutDatabase;
private GeoPoint centerRoutePoint = new GeoPoint(0, 0);
private Context globalContext;
private List<RouteElement> pointsList;

Expand Down Expand Up @@ -115,7 +114,7 @@ public boolean onItemSingleTapUp(int index, OverlayItem item) {
return true;
}

@Override public boolean onItemLongPress(int index, OverlayItem item) {
public boolean onItemLongPress(int index, OverlayItem item) {
return true;
}
});
Expand Down
24 changes: 12 additions & 12 deletions src/com/sylwke3100/freetrackgps/WorkoutsListActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

public class WorkoutsListActivity extends Activity {
private SimpleAdapter simpleAdapter;
private WorkoutsListOperations workoutsListOperations;
private WorkoutsListManager workoutsListManager;
private ListView workoutList;
private Menu optionsMenu;
private ArrayList<HashMap<String, String>> routesList;
Expand All @@ -28,11 +28,11 @@ public void onCreate(Bundle savedInstanceState) {
routesList = new ArrayList<HashMap<String, String>>();
workoutList = (ListView) this.findViewById(R.id.listWorkout);
registerForContextMenu(workoutList);
workoutsListOperations = new WorkoutsListOperations(getBaseContext());
workoutsListManager = new WorkoutsListManager(getBaseContext());
onUpdateWorkoutsList();

workoutList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
List<RouteListElement> objects = workoutsListOperations.getUpdatedWorkoutsRawList();
List<RouteListElement> objects = workoutsListManager.getUpdatedWorkoutsRawList();

public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(WorkoutsListActivity.this, WorkoutInfoActivity.class);
Expand All @@ -46,7 +46,7 @@ public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
}

private void onUpdateWorkoutsList() {
routesList = workoutsListOperations.getUpdatedWorkoutsList();
routesList = workoutsListManager.getUpdatedWorkoutsList();
simpleAdapter = new SimpleAdapter(this, routesList, R.layout.textview_row_lines,
new String[] {"time", "distance"}, new int[] {R.id.line_time, R.id.line_distance});
workoutList.setAdapter(simpleAdapter);
Expand All @@ -67,7 +67,7 @@ public boolean onContextItemSelected(MenuItem item) {
onDeleteWorkoutAlert(info.position);
return true;
case R.id.action_workout_export:
workoutsListOperations.exportWorkout(info.position);
workoutsListManager.exportWorkout(info.position);
return true;
case R.id.action_workout_change:
onUpdateNameWorkout(info.position);
Expand All @@ -83,7 +83,7 @@ public void onDeleteWorkoutAlert(final int id){
.setPositiveButton(this.getString(R.string.yesLabel),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
workoutsListOperations.deleteWorkout(id);
workoutsListManager.deleteWorkout(id);
onUpdateWorkoutsList();
}
}).setNegativeButton(this.getString(R.string.noLabel), null).show();
Expand All @@ -92,11 +92,11 @@ public void onClick(DialogInterface dialog, int which) {

public void updateIconOptionMenu() {
Integer dateFilterIcon, nameFilterIcon;
if (workoutsListOperations.getStatusTimeFilter())
if (workoutsListManager.getStatusTimeFilter())
dateFilterIcon = R.drawable.tick;
else
dateFilterIcon = R.drawable.emptytick;
if (workoutsListOperations.getStatusNameFilter())
if (workoutsListManager.getStatusNameFilter())
nameFilterIcon = R.drawable.tick;
else
nameFilterIcon = R.drawable.emptytick;
Expand Down Expand Up @@ -136,11 +136,11 @@ public void onUpdateNameFilter() {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setView(promptView);
final EditText input = (EditText) promptView.findViewById(R.id.nameFilter);
input.setText(workoutsListOperations.getFilterName());
input.setText(workoutsListManager.getFilterName());
alertDialogBuilder.setCancelable(false)
.setPositiveButton(R.string.okLabel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
workoutsListOperations.setNameFilter(input.getText().toString());
workoutsListManager.setNameFilter(input.getText().toString());
onUpdateWorkoutsList();
}
}).setNegativeButton(R.string.cancelLabel, new DialogInterface.OnClickListener() {
Expand All @@ -158,11 +158,11 @@ public void onUpdateNameWorkout(final int idWorkout) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setView(promptView);
final EditText input = (EditText) promptView.findViewById(R.id.nameWorkout);
input.setText(workoutsListOperations.getWorkoutName(idWorkout));
input.setText(workoutsListManager.getWorkoutName(idWorkout));
alertDialogBuilder.setCancelable(false)
.setPositiveButton(R.string.okLabel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
workoutsListOperations.updateWorkoutName(idWorkout, input.getText().toString());
workoutsListManager.updateWorkoutName(idWorkout, input.getText().toString());
onUpdateWorkoutsList();
}
}).setNegativeButton(R.string.cancelLabel, new DialogInterface.OnClickListener() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,22 @@
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;
import android.widget.Toast;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

public class WorkoutsListOperations {
public class WorkoutsListManager {
private SharedPreferences sharePrefs;
private DatabaseManager currentDataBase;
private List<RouteListElement> rawWorkoutsList;
private Context localContext;
private SimpleDateFormat fileGpxFormat = new SimpleDateFormat("yyyy-MM-dd-HH_mm_ss");
private DatabaseTimeFilter timeFilter;
private DatabaseNameFilter nameFilter;

WorkoutsListOperations(Context context) {
WorkoutsListManager(Context context) {
currentDataBase = new DatabaseManager(context);
localContext = context;
sharePrefs = context.getSharedPreferences("Pref", Activity.MODE_PRIVATE);
Expand Down Expand Up @@ -50,25 +49,17 @@ public void deleteWorkout(int id) {
public void exportWorkout(int id) {
RouteListElement object = rawWorkoutsList.get(id);
List<RouteElement> pointsWorkout = currentDataBase.getPointsInRoute(object.id);
StringBuffer fileNameBuffer = new StringBuffer();
File dir = new File(
Environment.getExternalStorageDirectory() + DefaultValues.defaultFolderWithWorkout);
if (!(dir.exists() && dir.isDirectory()))
dir.mkdir();
fileNameBuffer.append(
Environment.getExternalStorageDirectory() + DefaultValues.defaultFolderWithWorkout);
fileNameBuffer.append(fileGpxFormat.format(new Date(object.startTime)) + "."
+ DefaultValues.defaultFileFormat);
GPXWriter gpx = new GPXWriter(fileNameBuffer.toString(), object.startTime, object.name);
GPXWriter gpx = new GPXWriter(object.startTime, object.name);
for (RouteElement point : pointsWorkout) {
gpx.addPoint(point);
}
if (gpx.save())
Toast.makeText(localContext,
localContext.getString(R.string.SaveTrueInfo) + " " + fileNameBuffer.toString(),
localContext.getString(R.string.SaveTrueInfo) + " " + gpx.getFilename(),
Toast.LENGTH_LONG).show();
else
Toast.makeText(localContext, localContext.getString(R.string.SaveFalseInfo),
Toast.makeText(localContext,
localContext.getString(R.string.SaveFalseInfo) + " " + gpx.getFilename(),
Toast.LENGTH_LONG).show();
}

Expand Down

0 comments on commit b42585b

Please sign in to comment.