Skip to content

Commit

Permalink
Merge branch 'code_smell_fixes'
Browse files Browse the repository at this point in the history
  • Loading branch information
simonpoole committed Aug 25, 2024
2 parents 6b26106 + fe9eb5b commit 8e0dca3
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 33 deletions.
12 changes: 7 additions & 5 deletions src/main/java/de/blau/android/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Deque;
Expand Down Expand Up @@ -2818,7 +2819,7 @@ public void gotoTrackPoint(@NonNull final Logic logic, @NonNull TrackPoint track
private File getImageFile() throws IOException {
File outDir = FileUtil.getPublicDirectory(FileUtil.getPublicDirectory(), Paths.DIRECTORY_PATH_PICTURES);
String imageFileName = DateFormatter.getFormattedString(DATE_PATTERN_IMAGE_FILE_NAME_PART);
//FIXME this forces the extension to jpg, but it could be a HEIC image
// FIXME this forces the extension to jpg, but it could be a HEIC image
File newImageFile = File.createTempFile(imageFileName, "." + FileExtensions.JPG, outDir);
Log.d(DEBUG_TAG, "getImageFile " + newImageFile.getAbsolutePath());
return newImageFile;
Expand Down Expand Up @@ -3332,8 +3333,8 @@ public void performTagEdit(@NonNull final OsmElement selectedElement, @Nullable
* @param tags any existing tags to apply
* @param showPresets show the preset tab on start up.
*/
public void performTagEdit(@NonNull final OsmElement selectedElement, @Nullable PresetElementPath presetPath, @Nullable HashMap<String, String> tags,
boolean showPresets) {
public <M extends java.util.Map<String, String> & Serializable> void performTagEdit(@NonNull final OsmElement selectedElement, @Nullable PresetElementPath presetPath,
@Nullable M tags, boolean showPresets) {
ArrayList<PresetElementPath> presetPathList = new ArrayList<>();
if (presetPath != null) {
presetPathList.add(presetPath);
Expand All @@ -3351,8 +3352,9 @@ public void performTagEdit(@NonNull final OsmElement selectedElement, @Nullable
* @param tags any existing tags to apply
* @param showPresets show the preset tab on start up.
*/
private void performTagEdit(@NonNull final OsmElement selectedElement, @Nullable String focusOn, boolean applyLastAddressTags,
@Nullable ArrayList<PresetElementPath> presetPathList, @Nullable HashMap<String, String> tags, boolean showPresets) {
private <M extends java.util.Map<String, String> & Serializable, L extends List<PresetElementPath> & Serializable> void performTagEdit(
@NonNull final OsmElement selectedElement, @Nullable String focusOn, boolean applyLastAddressTags, @Nullable L presetPathList, @Nullable M tags,
boolean showPresets) {
descheduleAutoLock();
unlock();
final Logic logic = App.getLogic();
Expand Down
47 changes: 29 additions & 18 deletions src/main/java/de/blau/android/Mode.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package de.blau.android;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import android.content.Context;
Expand Down Expand Up @@ -61,14 +64,16 @@ public void teardown(Main main, Logic logic) {
// empty
}

@SuppressWarnings("unchecked")
@Override
public HashMap<String, String> getExtraTags(@NonNull Logic logic, @NonNull OsmElement e) {
return null;
public <M extends Map<String, String> & Serializable> M getExtraTags(@NonNull Logic logic, @NonNull OsmElement e) {
return (M) new HashMap<String, String>();
}

@SuppressWarnings("unchecked")
@Override
public ArrayList<PresetElementPath> getPresetItems(@NonNull Context ctx, @NonNull OsmElement e) {
return null;
public <L extends List<PresetElementPath> & Serializable> L getPresetItems(@NonNull Context ctx, @NonNull OsmElement e) {
return (L) new ArrayList<PresetElementPath>();
}
}),
/**
Expand Down Expand Up @@ -119,8 +124,9 @@ public void teardown(final Main main, final Logic logic) {
}

@Override
public HashMap<String, String> getExtraTags(@NonNull Logic logic, @NonNull OsmElement e) {
HashMap<String, String> result = new HashMap<>();
public <M extends Map<String, String> & Serializable> M getExtraTags(@NonNull Logic logic, @NonNull OsmElement e) {
@SuppressWarnings("unchecked")
M result = (M) new HashMap<String, String>();
// we only want to apply a level tag automatically to newly created objects if they don't already have the
// tag and not when the filter is inverted
Filter filter = logic.getFilter();
Expand All @@ -131,9 +137,10 @@ public HashMap<String, String> getExtraTags(@NonNull Logic logic, @NonNull OsmEl
return result;
}

@SuppressWarnings("unchecked")
@Override
public ArrayList<PresetElementPath> getPresetItems(@NonNull Context ctx, @NonNull OsmElement e) {
return null;
public <L extends List<PresetElementPath> & Serializable> L getPresetItems(@NonNull Context ctx, @NonNull OsmElement e) {
return (L) new ArrayList<PresetElementPath>();
}
}), MODE_CORRECT(R.string.mode_correct, "CORRECT", true, true, true, false, null, R.drawable.unlocked_correct_white, new ModeConfig() {

Expand Down Expand Up @@ -181,8 +188,9 @@ public void teardown(final Main main, final Logic logic) {
}

@Override
public ArrayList<PresetElementPath> getPresetItems(@NonNull Context ctx, @NonNull OsmElement e) {
ArrayList<PresetElementPath> result = new ArrayList<>();
public <L extends List<PresetElementPath> & Serializable> L getPresetItems(@NonNull Context ctx, @NonNull OsmElement e) {
@SuppressWarnings("unchecked")
L result = (L) new ArrayList<PresetElementPath>();
Preset[] presets = App.getCurrentPresets(ctx);
if (presets.length > 0 && presets[0] != null) {
PresetItem pi = Preset.findBestMatch(presets, e.getTags(), null, null);
Expand All @@ -193,9 +201,10 @@ public ArrayList<PresetElementPath> getPresetItems(@NonNull Context ctx, @NonNul
return result;
}

@SuppressWarnings("unchecked")
@Override
public HashMap<String, String> getExtraTags(@NonNull Logic logic, @NonNull OsmElement e) {
return null;
public <M extends Map<String, String> & Serializable> M getExtraTags(@NonNull Logic logic, @NonNull OsmElement e) {
return (M) new HashMap<String, String>();
}
}), MODE_VOICE(R.string.mode_voice, "VOICE", true, true, true, true, null, R.drawable.unlocked_voice_white, new FilterModeConfig() {

Expand Down Expand Up @@ -409,12 +418,13 @@ static Mode modeForTag(String tag) {
* @param e the selected element
* @return map containing the additional tags or null
*/
@Nullable
public HashMap<String, String> getExtraTags(@NonNull Logic logic, @NonNull OsmElement e) {
@SuppressWarnings("unchecked")
@NonNull
public <M extends Map<String, String> & Serializable> M getExtraTags(@NonNull Logic logic, @NonNull OsmElement e) {
if (config != null) {
return config.getExtraTags(logic, e);
}
return null;
return (M) new HashMap<String, String>();
}

/**
Expand All @@ -424,12 +434,13 @@ public HashMap<String, String> getExtraTags(@NonNull Logic logic, @NonNull OsmEl
* @param e the OsmElement
* @return a List of PresetElementPath or null if none available
*/
@Nullable
public ArrayList<PresetElementPath> getPresetItems(@NonNull Context ctx, @NonNull OsmElement e) {
@SuppressWarnings("unchecked")
@NonNull
public <L extends List<PresetElementPath> & Serializable> L getPresetItems(@NonNull Context ctx, @NonNull OsmElement e) {
if (config != null) {
return config.getPresetItems(ctx, e);
}
return null;
return (L) new ArrayList<PresetElementPath>();
}

/**
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/de/blau/android/ModeConfig.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package de.blau.android;

import java.util.ArrayList;
import java.io.Serializable;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import android.content.Context;
Expand Down Expand Up @@ -41,7 +42,7 @@ public interface ModeConfig {
* @return HashMap with tags to apply
*/
@Nullable
HashMap<String, String> getExtraTags(@NonNull Logic logic, @NonNull OsmElement e);
<M extends Map<String, String> & Serializable> M getExtraTags(@NonNull Logic logic, @NonNull OsmElement e);

/**
* Called before PropertyEditor startup to provide any mode specific PresetItems
Expand All @@ -53,7 +54,7 @@ public interface ModeConfig {
* @return list of PrestItems to apply
*/
@Nullable
ArrayList<PresetElementPath> getPresetItems(@NonNull Context ctx, @NonNull OsmElement e);
<L extends List<PresetElementPath> & Serializable> L getPresetItems(@NonNull Context ctx, @NonNull OsmElement e);

/**
* Return the simple actions that are enabled in the menu
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static de.blau.android.contract.Constants.LOG_TAG_LEN;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -48,7 +49,7 @@ public class PresetSearchResultsFragment extends DialogFragment implements Updat

private static final int TAG_LEN = Math.min(LOG_TAG_LEN, PresetSearchResultsFragment.class.getSimpleName().length());
private static final String DEBUG_TAG = PresetSearchResultsFragment.class.getSimpleName().substring(0, TAG_LEN);

private static final String SEARCH_RESULTS_KEY = "searchResults";
private static final String SEARCH_TERM_KEY = "searchTerm";

Expand All @@ -68,7 +69,7 @@ public class PresetSearchResultsFragment extends DialogFragment implements Updat
* @param searchResults a List of PresetEements to display
* @return an instance of PresetSearchResultsFragment
*/
public static PresetSearchResultsFragment newInstance(@NonNull String searchTerm, @NonNull ArrayList<PresetElement> searchResults) {
public static <L extends List<PresetElement> & Serializable> PresetSearchResultsFragment newInstance(@NonNull String searchTerm, @NonNull L searchResults) {
PresetSearchResultsFragment f = new PresetSearchResultsFragment();

Bundle args = new Bundle();
Expand All @@ -89,7 +90,6 @@ public void onAttach(Context context) {
propertyEditorListener = (PropertyEditorListener) parent;
}

@SuppressWarnings("unchecked")
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static de.blau.android.contract.Constants.LOG_TAG_LEN;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -144,7 +145,7 @@ public Node getUnusedEnd() {
* @return a new RelationMembersFragment instance
*/
@NonNull
public static RelationMembersFragment newInstance(long id, ArrayList<RelationMemberDescription> members) {
public static <L extends List<RelationMemberDescription> & Serializable> RelationMembersFragment newInstance(long id, L members) {
RelationMembersFragment f = new RelationMembersFragment();

Bundle args = new Bundle();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
// Add any extra tags that were supplied
@SuppressWarnings("unchecked")
Map<String, String> extraTags = Util.getSerializeable(getArguments(), EXTRA_TAGS_KEY, HashMap.class);
if (extraTags != null) {
if (extraTags != null && !extraTags.isEmpty()) {
for (Entry<String, String> e : extraTags.entrySet()) {
addTag(editRowLayout, e.getKey(), e.getValue(), true, false);
}
Expand All @@ -357,7 +357,7 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c

if (savedInstanceState == null) { // the following should only happen once on initial creation
List<PresetElementPath> presetsToApply = Util.getSerializeableArrayList(getArguments(), PRESETSTOAPPLY_KEY, PresetElementPath.class);
if (presetsToApply != null && !presetsToApply.isEmpty()) {
if (Util.notEmpty(presetsToApply)) {
FragmentActivity activity = getActivity();
Preset preset = App.getCurrentRootPreset(activity);
PresetGroup rootGroup = preset.getRootGroup();
Expand Down

0 comments on commit 8e0dca3

Please sign in to comment.