From 040e3cae67b54a43edfdef39e6aa532098ca0aa9 Mon Sep 17 00:00:00 2001 From: EmmanuelAlfaro Date: Fri, 17 May 2019 14:36:23 -0600 Subject: [PATCH] Fix issue #189, adding multiple tags to track --- .../net/osmtracker/activity/TrackLogger.java | 65 ++++++++++++------- 1 file changed, 42 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/net/osmtracker/activity/TrackLogger.java b/app/src/main/java/net/osmtracker/activity/TrackLogger.java index 492d5e642..5838f8f98 100644 --- a/app/src/main/java/net/osmtracker/activity/TrackLogger.java +++ b/app/src/main/java/net/osmtracker/activity/TrackLogger.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.Date; +import java.util.HashSet; import net.osmtracker.OSMTracker; import net.osmtracker.R; @@ -92,6 +93,11 @@ public class TrackLogger extends Activity { * Bundle state key for tracking flag. */ public static final String STATE_IS_TRACKING = "isTracking"; + + /** + * The character to separate the tags of a track + */ + public static final String TAG_SEPARATOR = ","; /** * Bundle state key button state. @@ -164,7 +170,11 @@ public class TrackLogger extends Activity { private ComponentName mediaButtonReceiver; - private ArrayList layoutNameTags = new ArrayList(); + + /* + * Avoid taking care of duplicated elements + */ + private HashSet layoutNameTags = new HashSet(); @Override protected void onCreate(Bundle savedInstanceState) { @@ -214,44 +224,55 @@ protected void onCreate(Bundle savedInstanceState) { * Also, the default layout is excluded and the 'osmtracker' tag is added by default. */ private void saveTagsForTrack(){ - //obtain the current track id and initialize the values variable + // Obtain the current track id and initialize the values variable Uri trackUri = ContentUris.withAppendedId(TrackContentProvider.CONTENT_URI_TRACK, currentTrackId); ContentValues values = new ContentValues(); - //Checking for previously saved tags - Cursor cursor = getContentResolver().query( trackUri, null, null, null, null); - StringBuilder previouslySavedTags = new StringBuilder(); - int index = cursor.getColumnIndex(TrackContentProvider.Schema.COL_TAGS); + // A set with all tags to save + HashSet tagsToSave = new HashSet<>(); + // Get and add previously saved tags to the set + Cursor cursor = getContentResolver().query( trackUri, null, null, null, null); + int tagsIndex = cursor.getColumnIndex(TrackContentProvider.Schema.COL_TAGS); + String previouslySavedTags = null; while (cursor.moveToNext()) { - if(cursor.getString(index) != null) - previouslySavedTags.append(cursor.getString(index) + ","); + if(cursor.getString(tagsIndex) != null) { + previouslySavedTags = cursor.getString(tagsIndex); + } + } + if(previouslySavedTags != null){ + for (String tag : previouslySavedTags.split(TAG_SEPARATOR)){ + tagsToSave.add(tag); + } } - - StringBuilder tags = new StringBuilder(); - tags.append(previouslySavedTags); - ArrayList fixedTags = new ArrayList(); + // Add the names of the layouts that were used in the track to the set - //covert the file name to simple layout name for(String layoutFileName : layoutNameTags){ //OSMTracker.Preferences.VAL_UI_BUTTONS_LAYOUT -> 'default' if(! layoutFileName.equals(OSMTracker.Preferences.VAL_UI_BUTTONS_LAYOUT)){ - fixedTags.add(CustomLayoutsUtils.convertFileName(layoutFileName)); + // Covert the file name to simple layout name + tagsToSave.add(CustomLayoutsUtils.convertFileName(layoutFileName)); } } - fixedTags.add("osmtracker"); + // Check if the osmtracker tag has already been added + String trackerTag = "osmtracker"; + tagsToSave.add(trackerTag); - //create the string with all tags - for(String simpleName : fixedTags){ - tags.append(simpleName).append(","); - } + // Create the string with all tags + StringBuilder tagsString = new StringBuilder(); + + for(String tag : tagsToSave){ + tagsString.append(tag).append(TAG_SEPARATOR); + } + int lastIndex = tagsString.length()-1; + tagsString.deleteCharAt(lastIndex); //set the values tag and update the table - values.put(TrackContentProvider.Schema.COL_TAGS, tags.toString()); + values.put(TrackContentProvider.Schema.COL_TAGS, tagsString.toString()); getContentResolver().update(trackUri, values, null, null); } @@ -331,9 +352,7 @@ protected void onResume() { //save the layout file name if it change, in tags array String layoutName = CustomLayoutsUtils.getCurrentLayoutName(getApplicationContext()); - if(! layoutNameTags.contains(layoutName)){ - layoutNameTags.add(layoutName); - } + layoutNameTags.add(layoutName); super.onResume(); }