Remove unnecessary method

This commit is contained in:
Vitaliy 2020-08-01 15:53:41 +03:00
parent df0dc024f8
commit c334cfa62e
4 changed files with 40 additions and 83 deletions

View file

@ -320,10 +320,8 @@ public class Algorithms {
* exception. Supported formats are: * exception. Supported formats are:
* #RRGGBB * #RRGGBB
* #AARRGGBB * #AARRGGBB
* 'red', 'blue', 'green', 'black', 'white', 'gray', 'cyan', 'magenta',
* 'yellow', 'lightgray', 'darkgray'
*/ */
public static int parseColor(String colorString) { public static int parseColor(String colorString) throws IllegalArgumentException {
if (colorString.charAt(0) == '#') { if (colorString.charAt(0) == '#') {
// Use a long to avoid rollovers on #ffXXXXXX // Use a long to avoid rollovers on #ffXXXXXX
if (colorString.length() == 4) { if (colorString.length() == 4) {

View file

@ -520,12 +520,12 @@ public class GpxSelectionHelper {
} }
GPXFile gpx = GPXUtilities.loadGPXFile(fl); GPXFile gpx = GPXUtilities.loadGPXFile(fl);
if (obj.has(COLOR)) { if (obj.has(COLOR)) {
int clr = Algorithms.parseColor(obj.getString(COLOR)); int clr = parseColor(obj.getString(COLOR));
gpx.setColor(clr); gpx.setColor(clr);
} }
for (GradientScaleType scaleType : GradientScaleType.values()) { for (GradientScaleType scaleType : GradientScaleType.values()) {
if (obj.has(scaleType.getColorTypeName())) { if (obj.has(scaleType.getColorTypeName())) {
int clr = Algorithms.parseColor(obj.getString(scaleType.getColorTypeName())); int clr = parseColor(obj.getString(scaleType.getColorTypeName()));
gpx.setGradientScaleColor(scaleType.getColorTypeName(), clr); gpx.setGradientScaleColor(scaleType.getColorTypeName(), clr);
} }
} }
@ -567,6 +567,14 @@ public class GpxSelectionHelper {
} }
} }
private int parseColor(String color) {
try {
return Algorithms.isEmpty(color) ? 0 : Algorithms.parseColor(color);
} catch (IllegalArgumentException e) {
return 0;
}
}
private void saveCurrentSelections() { private void saveCurrentSelections() {
JSONArray ar = new JSONArray(); JSONArray ar = new JSONArray();
for (SelectedGpxFile s : selectedGPXFiles) { for (SelectedGpxFile s : selectedGPXFiles) {

View file

@ -26,6 +26,7 @@ import net.osmand.plus.base.MenuBottomSheetDialogFragment;
import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem; import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem;
import net.osmand.plus.base.bottomsheetmenu.SimpleBottomSheetItem; import net.osmand.plus.base.bottomsheetmenu.SimpleBottomSheetItem;
import net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleItem; import net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleItem;
import net.osmand.util.Algorithms;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
@ -103,7 +104,13 @@ public class CustomColorBottomSheet extends MenuBottomSheetDialogFragment implem
@Override @Override
public void afterTextChanged(Editable s) { public void afterTextChanged(Editable s) {
if (hexEditText.isFocused()) { if (hexEditText.isFocused()) {
int color = parseColorString(s.toString()); int color = colorPicker.getColor();
try {
color = Algorithms.parseColor("#" + s.toString());
} catch (IllegalArgumentException e) {
hexEditText.setError(getString(R.string.wrong_input));
log.error(e);
}
if (color != colorPicker.getColor()) { if (color != colorPicker.getColor()) {
fromEditText = true; fromEditText = true;
colorPicker.setColor(color, true); colorPicker.setColor(color, true);
@ -166,60 +173,6 @@ public class CustomColorBottomSheet extends MenuBottomSheetDialogFragment implem
} }
} }
private static int parseColorString(String colorString) throws NumberFormatException {
int a, r, g, b = 0;
if (colorString.startsWith("#")) {
colorString = colorString.substring(1);
}
if (colorString.length() == 0) {
r = 0;
a = 255;
g = 0;
} else if (colorString.length() <= 2) {
a = 255;
r = 0;
b = Integer.parseInt(colorString, 16);
g = 0;
} else if (colorString.length() == 3) {
a = 255;
r = Integer.parseInt(colorString.substring(0, 1), 16);
g = Integer.parseInt(colorString.substring(1, 2), 16);
b = Integer.parseInt(colorString.substring(2, 3), 16);
} else if (colorString.length() == 4) {
a = 255;
r = Integer.parseInt(colorString.substring(0, 2), 16);
g = r;
r = 0;
b = Integer.parseInt(colorString.substring(2, 4), 16);
} else if (colorString.length() == 5) {
a = 255;
r = Integer.parseInt(colorString.substring(0, 1), 16);
g = Integer.parseInt(colorString.substring(1, 3), 16);
b = Integer.parseInt(colorString.substring(3, 5), 16);
} else if (colorString.length() == 6) {
a = 255;
r = Integer.parseInt(colorString.substring(0, 2), 16);
g = Integer.parseInt(colorString.substring(2, 4), 16);
b = Integer.parseInt(colorString.substring(4, 6), 16);
} else if (colorString.length() == 7) {
a = Integer.parseInt(colorString.substring(0, 1), 16);
r = Integer.parseInt(colorString.substring(1, 3), 16);
g = Integer.parseInt(colorString.substring(3, 5), 16);
b = Integer.parseInt(colorString.substring(5, 7), 16);
} else if (colorString.length() == 8) {
a = Integer.parseInt(colorString.substring(0, 2), 16);
r = Integer.parseInt(colorString.substring(2, 4), 16);
g = Integer.parseInt(colorString.substring(4, 6), 16);
b = Integer.parseInt(colorString.substring(6, 8), 16);
} else {
b = -1;
g = -1;
r = -1;
a = -1;
}
return Color.argb(a, r, g, b);
}
public interface ColorPickerListener { public interface ColorPickerListener {
void onColorSelected(@ColorInt int prevColor, @ColorInt int newColor); void onColorSelected(@ColorInt int prevColor, @ColorInt int newColor);

View file

@ -379,7 +379,8 @@ public class GPXLayer extends OsmandMapLayer implements IContextMenuProvider, IM
for (int k = 0; k < items.size(); k++) { for (int k = 0; k < items.size(); k++) {
GpxDisplayItem i = items.get(k); GpxDisplayItem i = items.get(k);
WptPt point = i.locationEnd; WptPt point = i.locationEnd;
if (point != null) { if (point != null && point.lat >= latLonBounds.bottom && point.lat <= latLonBounds.top
&& point.lon >= latLonBounds.left && point.lon <= latLonBounds.right) {
float x = tileBox.getPixXFromLatLon(point.lat, point.lon); float x = tileBox.getPixXFromLatLon(point.lat, point.lon);
float y = tileBox.getPixYFromLatLon(point.lat, point.lon); float y = tileBox.getPixYFromLatLon(point.lat, point.lon);
if (px != -1 || py != -1) { if (px != -1 || py != -1) {
@ -389,29 +390,26 @@ public class GPXLayer extends OsmandMapLayer implements IContextMenuProvider, IM
} }
px = x; px = x;
py = y; py = y;
if (point.lat >= latLonBounds.bottom && point.lat <= latLonBounds.top String name = i.splitName;
&& point.lon >= latLonBounds.left && point.lon <= latLonBounds.right) { if (name != null) {
String name = i.splitName; int ind = name.indexOf(' ');
if (name != null) { if (ind > 0) {
int ind = name.indexOf(' '); name = name.substring(0, ind);
if (ind > 0) {
name = name.substring(0, ind);
}
Rect bounds = new Rect();
paintTextIcon.getTextBounds(name, 0, name.length(), bounds);
float nameHalfWidth = bounds.width() / 2f;
float nameHalfHeight = bounds.height() / 2f;
float density = (float) Math.ceil(tileBox.getDensity());
RectF rect = new RectF(x - nameHalfWidth - 2 * density,
y + nameHalfHeight + 3 * density,
x + nameHalfWidth + 3 * density,
y - nameHalfHeight - 2 * density);
canvas.drawRoundRect(rect, 0, 0, paintInnerRect);
canvas.drawRoundRect(rect, 0, 0, paintOuterRect);
canvas.drawText(name, x, y + nameHalfHeight, paintTextIcon);
} }
Rect bounds = new Rect();
paintTextIcon.getTextBounds(name, 0, name.length(), bounds);
float nameHalfWidth = bounds.width() / 2f;
float nameHalfHeight = bounds.height() / 2f;
float density = (float) Math.ceil(tileBox.getDensity());
RectF rect = new RectF(x - nameHalfWidth - 2 * density,
y + nameHalfHeight + 3 * density,
x + nameHalfWidth + 3 * density,
y - nameHalfHeight - 2 * density);
canvas.drawRoundRect(rect, 0, 0, paintInnerRect);
canvas.drawRoundRect(rect, 0, 0, paintOuterRect);
canvas.drawText(name, x, y + nameHalfHeight, paintTextIcon);
} }
} }
} }