use of net.osmand.plus.settings.backend.OsmandSettings in project Osmand by osmandapp.
the class DiscountHelper method processDiscountResponse.
@SuppressLint("SimpleDateFormat")
private static void processDiscountResponse(String response, MapActivity mapActivity) {
try {
OsmandApplication app = mapActivity.getMyApplication();
JSONObject obj = new JSONObject(response);
ControllerData data = ControllerData.parse(app, obj);
SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm");
Date start = df.parse(obj.getString("start"));
Date end = df.parse(obj.getString("end"));
int showStartFrequency = obj.getInt("show_start_frequency");
double showDayFrequency = obj.getDouble("show_day_frequency");
int maxTotalShow = obj.getInt("max_total_show");
JSONObject application = obj.getJSONObject("application");
boolean showChristmasDialog = obj.optBoolean("show_christmas_dialog", false);
if (!validateUrl(app, data.url)) {
return;
}
if (data.oneOfConditions != null) {
boolean oneOfConditionsMatch = false;
try {
Conditions conditions = new Conditions(app);
JSONArray conditionsArr = data.oneOfConditions;
for (int i = 0; i < conditionsArr.length(); i++) {
JSONObject conditionObj = conditionsArr.getJSONObject(i);
JSONArray conditionArr = conditionObj.getJSONArray("condition");
if (conditionArr.length() > 0) {
boolean conditionMatch = true;
for (int k = 0; k < conditionArr.length(); k++) {
JSONObject o = conditionArr.getJSONObject(k);
conditionMatch = conditions.matchesCondition(o);
if (!conditionMatch) {
break;
}
}
oneOfConditionsMatch |= conditionMatch;
}
}
} catch (JSONException e) {
// ignore
}
if (!oneOfConditionsMatch) {
return;
}
}
String appName = app.getPackageName();
Date date = new Date();
if (application.has(appName) && application.getBoolean(appName) && date.after(start) && date.before(end)) {
OsmandSettings settings = app.getSettings();
int discountId = getDiscountId(data.message, start);
boolean discountChanged = settings.DISCOUNT_ID.get() != discountId;
if (discountChanged) {
settings.DISCOUNT_TOTAL_SHOW.set(0);
}
// show after every N (getNumberOfStarts()) starts or show after every N (double show_day_frequency) frequency
if (discountChanged || (app.getAppInitializer().getNumberOfStarts() - settings.DISCOUNT_SHOW_NUMBER_OF_STARTS.get() >= showStartFrequency || System.currentTimeMillis() - settings.DISCOUNT_SHOW_DATETIME_MS.get() > 1000L * 60 * 60 * 24 * showDayFrequency)) {
if (settings.DISCOUNT_TOTAL_SHOW.get() < maxTotalShow) {
settings.DISCOUNT_ID.set(discountId);
settings.DISCOUNT_TOTAL_SHOW.set(settings.DISCOUNT_TOTAL_SHOW.get() + 1);
settings.DISCOUNT_SHOW_NUMBER_OF_STARTS.set(app.getAppInitializer().getNumberOfStarts());
settings.DISCOUNT_SHOW_DATETIME_MS.set(System.currentTimeMillis());
if (showChristmasDialog) {
mapActivity.showXMasDialog();
} else {
InAppPurchaseHelper purchaseHelper = mapActivity.getPurchaseHelper();
if (purchaseHelper != null) {
purchaseHelper.requestInventory(false);
}
showDiscountBanner(mapActivity, data);
}
}
}
}
} catch (Exception e) {
logError("JSON parsing error: ", e);
}
}
use of net.osmand.plus.settings.backend.OsmandSettings in project Osmand by osmandapp.
the class GpxUiHelper method createGPXSlopeDataSet.
public static OrderedLineDataSet createGPXSlopeDataSet(@NonNull OsmandApplication ctx, @NonNull LineChart mChart, @NonNull GPXTrackAnalysis analysis, @NonNull GPXDataSetAxisType axisType, @Nullable List<Entry> eleValues, boolean useRightAxis, boolean drawFilled, boolean calcWithoutGaps) {
if (axisType == GPXDataSetAxisType.TIME || axisType == GPXDataSetAxisType.TIMEOFDAY) {
return null;
}
OsmandSettings settings = ctx.getSettings();
boolean light = settings.isLightContent();
MetricsConstants mc = settings.METRIC_SYSTEM.get();
boolean useFeet = (mc == MetricsConstants.MILES_AND_FEET) || (mc == MetricsConstants.MILES_AND_YARDS);
final float convEle = useFeet ? 3.28084f : 1.0f;
final float totalDistance = calcWithoutGaps ? analysis.totalDistanceWithoutGaps : analysis.totalDistance;
XAxis xAxis = mChart.getXAxis();
float divX = setupAxisDistance(ctx, xAxis, calcWithoutGaps ? analysis.totalDistanceWithoutGaps : analysis.totalDistance);
final String mainUnitY = "%";
YAxis yAxis;
if (useRightAxis) {
yAxis = mChart.getAxisRight();
yAxis.setEnabled(true);
} else {
yAxis = mChart.getAxisLeft();
}
yAxis.setTextColor(ActivityCompat.getColor(mChart.getContext(), R.color.gpx_chart_green_label));
yAxis.setGranularity(1f);
yAxis.resetAxisMinimum();
yAxis.setValueFormatter((value, axis) -> OsmAndFormatter.formatInteger((int) value, mainUnitY, ctx));
List<Entry> values;
if (eleValues == null) {
values = calculateElevationArray(analysis, GPXDataSetAxisType.DISTANCE, 1f, 1f, false, calcWithoutGaps);
} else {
values = new ArrayList<>(eleValues.size());
for (Entry e : eleValues) {
values.add(new Entry(e.getX() * divX, e.getY() / convEle));
}
}
if (Algorithms.isEmpty(values)) {
if (useRightAxis) {
yAxis.setEnabled(false);
}
return null;
}
int lastIndex = values.size() - 1;
double STEP = 5;
int l = 10;
while (l > 0 && totalDistance / STEP > MAX_CHART_DATA_ITEMS) {
STEP = Math.max(STEP, totalDistance / (values.size() * l--));
}
double[] calculatedDist = new double[(int) (totalDistance / STEP) + 1];
double[] calculatedH = new double[(int) (totalDistance / STEP) + 1];
int nextW = 0;
for (int k = 0; k < calculatedDist.length; k++) {
if (k > 0) {
calculatedDist[k] = calculatedDist[k - 1] + STEP;
}
while (nextW < lastIndex && calculatedDist[k] > values.get(nextW).getX()) {
nextW++;
}
double pd = nextW == 0 ? 0 : values.get(nextW - 1).getX();
double ph = nextW == 0 ? values.get(0).getY() : values.get(nextW - 1).getY();
calculatedH[k] = ph + (values.get(nextW).getY() - ph) / (values.get(nextW).getX() - pd) * (calculatedDist[k] - pd);
}
double SLOPE_PROXIMITY = Math.max(100, STEP * 2);
if (totalDistance - SLOPE_PROXIMITY < 0) {
if (useRightAxis) {
yAxis.setEnabled(false);
}
return null;
}
double[] calculatedSlopeDist = new double[(int) ((totalDistance - SLOPE_PROXIMITY) / STEP) + 1];
double[] calculatedSlope = new double[(int) ((totalDistance - SLOPE_PROXIMITY) / STEP) + 1];
int index = (int) ((SLOPE_PROXIMITY / STEP) / 2);
for (int k = 0; k < calculatedSlopeDist.length; k++) {
calculatedSlopeDist[k] = calculatedDist[index + k];
calculatedSlope[k] = (calculatedH[2 * index + k] - calculatedH[k]) * 100 / SLOPE_PROXIMITY;
if (Double.isNaN(calculatedSlope[k])) {
calculatedSlope[k] = 0;
}
}
List<Entry> slopeValues = new ArrayList<>(calculatedSlopeDist.length);
float prevSlope = -80000;
float slope;
float x;
float lastXSameY = 0;
boolean hasSameY = false;
Entry lastEntry = null;
lastIndex = calculatedSlopeDist.length - 1;
for (int i = 0; i < calculatedSlopeDist.length; i++) {
x = (float) calculatedSlopeDist[i] / divX;
slope = (float) calculatedSlope[i];
if (prevSlope != -80000) {
if (prevSlope == slope && i < lastIndex) {
hasSameY = true;
lastXSameY = x;
continue;
}
if (hasSameY) {
slopeValues.add(new Entry(lastXSameY, lastEntry.getY()));
}
hasSameY = false;
}
prevSlope = slope;
lastEntry = new Entry(x, slope);
slopeValues.add(lastEntry);
}
OrderedLineDataSet dataSet = new OrderedLineDataSet(slopeValues, "", GPXDataSetType.SLOPE, axisType, !useRightAxis);
dataSet.divX = divX;
dataSet.units = mainUnitY;
dataSet.setColor(ContextCompat.getColor(mChart.getContext(), R.color.gpx_chart_green));
dataSet.setLineWidth(1f);
if (drawFilled) {
dataSet.setFillAlpha(128);
dataSet.setFillColor(ContextCompat.getColor(mChart.getContext(), R.color.gpx_chart_green));
dataSet.setDrawFilled(true);
} else {
dataSet.setDrawFilled(false);
}
dataSet.setDrawValues(false);
dataSet.setValueTextSize(9f);
dataSet.setFormLineWidth(1f);
dataSet.setFormSize(15.f);
dataSet.setDrawCircles(false);
dataSet.setDrawCircleHole(false);
dataSet.setHighlightEnabled(true);
dataSet.setDrawVerticalHighlightIndicator(true);
dataSet.setDrawHorizontalHighlightIndicator(false);
dataSet.setHighLightColor(ColorUtilities.getSecondaryTextColor(mChart.getContext(), !light));
/*
dataSet.setFillFormatter(new IFillFormatter() {
@Override
public float getFillLinePosition(ILineDataSet dataSet, LineDataProvider dataProvider) {
return dataProvider.getYChartMin();
}
});
*/
if (useRightAxis) {
dataSet.setAxisDependency(YAxis.AxisDependency.RIGHT);
}
return dataSet;
}
use of net.osmand.plus.settings.backend.OsmandSettings in project Osmand by osmandapp.
the class WaypointHelper method calculateAlarms.
private void calculateAlarms(RouteCalculationResult route, List<LocationPointWrapper> array, ApplicationMode mode) {
OsmandSettings settings = app.getSettings();
if (!settings.SHOW_ROUTING_ALARMS.getModeValue(mode)) {
return;
}
AlarmInfo prevSpeedCam = null;
AlarmInfo prevRailway = null;
for (AlarmInfo alarmInfo : route.getAlarmInfo()) {
AlarmInfoType type = alarmInfo.getType();
if (type == AlarmInfoType.SPEED_CAMERA) {
if (settings.SHOW_CAMERAS.getModeValue(mode) || settings.SPEAK_SPEED_CAMERA.getModeValue(mode)) {
// ignore double speed cams
if (prevSpeedCam == null || MapUtils.getDistance(prevSpeedCam.getLatitude(), prevSpeedCam.getLongitude(), alarmInfo.getLatitude(), alarmInfo.getLongitude()) >= DISTANCE_IGNORE_DOUBLE_SPEEDCAMS) {
addPointWrapper(alarmInfo, array, settings.SPEAK_SPEED_CAMERA.getModeValue(mode));
prevSpeedCam = alarmInfo;
}
}
} else if (type == AlarmInfoType.TUNNEL) {
if (settings.SHOW_TUNNELS.getModeValue(mode) || settings.SPEAK_TUNNELS.getModeValue(mode)) {
addPointWrapper(alarmInfo, array, settings.SPEAK_TUNNELS.getModeValue(mode));
}
} else if (type == AlarmInfoType.PEDESTRIAN) {
if (settings.SHOW_PEDESTRIAN.getModeValue(mode) || settings.SPEAK_PEDESTRIAN.getModeValue(mode)) {
addPointWrapper(alarmInfo, array, settings.SPEAK_PEDESTRIAN.getModeValue(mode));
}
} else if (type == AlarmInfoType.RAILWAY) {
if (prevRailway == null || MapUtils.getDistance(prevRailway.getLatitude(), prevRailway.getLongitude(), alarmInfo.getLatitude(), alarmInfo.getLongitude()) >= DISTANCE_IGNORE_DOUBLE_RAILWAYS) {
addPointWrapper(alarmInfo, array, settings.SPEAK_TRAFFIC_WARNINGS.getModeValue(mode));
prevRailway = alarmInfo;
}
} else if (settings.SHOW_TRAFFIC_WARNINGS.getModeValue(mode) || settings.SPEAK_TRAFFIC_WARNINGS.getModeValue(mode)) {
addPointWrapper(alarmInfo, array, settings.SPEAK_TRAFFIC_WARNINGS.getModeValue(mode));
}
}
}
use of net.osmand.plus.settings.backend.OsmandSettings in project Osmand by osmandapp.
the class RateUsHelper method storeRateResult.
private static void storeRateResult(FragmentActivity activity, RateUsState state) {
if (state != null && activity != null && !activity.isChangingConfigurations()) {
OsmandApplication app = (OsmandApplication) activity.getApplication();
OsmandSettings settings = app.getSettings();
RateUsState newState = RateUsState.getNewState(app, state);
settings.RATE_US_STATE.set(newState);
if (newState != RateUsState.LIKED) {
settings.NUMBER_OF_APP_STARTS_ON_DISLIKE_MOMENT.set(app.getAppInitializer().getNumberOfStarts());
}
settings.LAST_DISPLAY_TIME.set(System.currentTimeMillis());
}
}
use of net.osmand.plus.settings.backend.OsmandSettings in project Osmand by osmandapp.
the class LiveMonitoringHelper method updateLocation.
public void updateLocation(net.osmand.Location location) {
boolean record = false;
long locationTime = System.currentTimeMillis();
if (location != null && isLiveMonitoringEnabled() && OsmAndLocationProvider.isNotSimulatedLocation(location) && OsmandPlugin.isActive(OsmandMonitoringPlugin.class)) {
OsmandSettings settings = app.getSettings();
if (locationTime - lastTimeUpdated > settings.LIVE_MONITORING_INTERVAL.get()) {
record = true;
}
float minDistance = settings.SAVE_TRACK_MIN_DISTANCE.get();
if (minDistance > 0 && lastPoint != null && MapUtils.getDistance(lastPoint, location.getLatitude(), location.getLongitude()) < minDistance) {
record = false;
}
float precision = settings.SAVE_TRACK_PRECISION.get();
if (precision > 0 && (!location.hasAccuracy() || location.getAccuracy() > precision)) {
record = false;
}
float minSpeed = settings.SAVE_TRACK_MIN_SPEED.get();
if (minSpeed > 0 && (!location.hasSpeed() || location.getSpeed() < minSpeed)) {
record = false;
}
}
if (isLiveMonitoringEnabled()) {
if (!started) {
new LiveSender().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, queue);
started = true;
}
} else {
started = false;
}
if (record) {
LiveMonitoringData data = new LiveMonitoringData((float) location.getLatitude(), (float) location.getLongitude(), (float) location.getAltitude(), location.getSpeed(), location.getAccuracy(), location.getBearing(), locationTime);
queue.add(data);
lastPoint = new LatLon(location.getLatitude(), location.getLongitude());
lastTimeUpdated = locationTime;
}
}
Aggregations