Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 42 additions & 23 deletions app/src/main/java/net/osmtracker/activity/TrackLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;

import net.osmtracker.OSMTracker;
import net.osmtracker.R;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -164,7 +170,11 @@ public class TrackLogger extends Activity {

private ComponentName mediaButtonReceiver;

private ArrayList<String> layoutNameTags = new ArrayList<String>();

/*
* Avoid taking care of duplicated elements
*/
private HashSet<String> layoutNameTags = new HashSet<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -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<String> 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<String> fixedTags = new ArrayList<String>();
// 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);
}

Expand Down Expand Up @@ -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();
}
Expand Down