Merge pull request #9974 from AlexSyt/fix_online_tracking_link_9621

#9621 Add validation for the online tracking link
This commit is contained in:
Vitaliy 2020-10-08 12:25:34 +03:00 committed by GitHub
commit 57e971a1bb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 61 additions and 9 deletions

View file

@ -942,4 +942,20 @@ public class Algorithms {
}
return res;
}
public static boolean isValidMessageFormat(CharSequence sequence) {
if (!isEmpty(sequence)) {
int counter = 0;
for (int i = 0; i < sequence.length(); i++) {
char ch = sequence.charAt(i);
if (ch == '{') {
counter++;
} else if (ch == '}') {
counter--;
}
}
return counter == 0;
}
return false;
}
}

View file

@ -131,7 +131,14 @@ public class LiveMonitoringHelper {
}
public void sendData(LiveMonitoringData data) {
String urlStr = getLiveUrl(data);
String baseUrl = app.getSettings().LIVE_MONITORING_URL.get();
String urlStr;
try {
urlStr = getLiveUrl(baseUrl, data);
} catch (IllegalArgumentException e) {
log.error("Could not construct live url from base url: " + baseUrl, e);
return;
}
try {
// Parse the URL and let the URI constructor handle proper encoding of special characters such as spaces
URL url = new URL(urlStr);
@ -172,12 +179,11 @@ public class LiveMonitoringHelper {
}
}
private String getLiveUrl(LiveMonitoringData data) {
String st = app.getSettings().LIVE_MONITORING_URL.get();
private String getLiveUrl(String baseUrl, LiveMonitoringData data) {
List<String> prm = new ArrayList<String>();
int maxLen = 0;
for (int i = 0; i < 7; i++) {
boolean b = st.contains("{"+i+"}");
boolean b = baseUrl.contains("{"+i+"}");
if(b) {
maxLen = i;
}
@ -210,6 +216,6 @@ public class LiveMonitoringHelper {
break;
}
}
return MessageFormat.format(st, prm.toArray());
return MessageFormat.format(baseUrl, prm.toArray());
}
}

View file

@ -8,6 +8,7 @@ import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
@ -23,11 +24,11 @@ import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import net.osmand.plus.settings.backend.ApplicationMode;
import net.osmand.plus.OsmAndFormatter;
import net.osmand.plus.OsmAndTaskManager.OsmAndTaskRunnable;
import net.osmand.plus.OsmandApplication;
@ -35,6 +36,8 @@ import net.osmand.plus.R;
import net.osmand.plus.UiUtilities;
import net.osmand.plus.activities.SavingTrackHelper;
import net.osmand.plus.activities.SettingsBaseActivity;
import net.osmand.plus.settings.backend.ApplicationMode;
import net.osmand.util.Algorithms;
import java.util.Map;
@ -172,8 +175,20 @@ public class SettingsMonitoringActivity extends SettingsBaseActivity {
cat.setTitle(R.string.live_monitoring_m);
grp.addPreference(cat);
cat.addPreference(createEditTextPreference(settings.LIVE_MONITORING_URL, R.string.live_monitoring_url,
R.string.live_monitoring_url_descr));
EditTextPreference urlPreference = createEditTextPreference(settings.LIVE_MONITORING_URL, R.string.live_monitoring_url,
R.string.live_monitoring_url_descr);
urlPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (Algorithms.isValidMessageFormat((String) newValue)) {
return SettingsMonitoringActivity.super.onPreferenceChange(preference, newValue);
} else {
Toast.makeText(SettingsMonitoringActivity.this, R.string.wrong_format, Toast.LENGTH_SHORT).show();
return false;
}
}
});
cat.addPreference(urlPreference);
final CheckBoxPreference liveMonitoring = createCheckBoxPreference(settings.LIVE_MONITORING, R.string.live_monitoring_m,
R.string.live_monitoring_m_descr);
cat.addPreference(liveMonitoring);

View file

@ -5,6 +5,7 @@ import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.widget.SwitchCompat;
import androidx.core.content.ContextCompat;
@ -12,11 +13,12 @@ import androidx.preference.Preference;
import net.osmand.AndroidUtils;
import net.osmand.plus.OsmAndFormatter;
import net.osmand.plus.settings.backend.ApplicationMode;
import net.osmand.plus.R;
import net.osmand.plus.UiUtilities;
import net.osmand.plus.settings.backend.ApplicationMode;
import net.osmand.plus.settings.preferences.EditTextPreferenceEx;
import net.osmand.plus.settings.preferences.ListPreferenceEx;
import net.osmand.util.Algorithms;
import static net.osmand.plus.UiUtilities.CompoundButtonType.TOOLBAR;
import static net.osmand.plus.monitoring.OsmandMonitoringPlugin.MAX_INTERVAL_TO_SEND_MINUTES;
@ -60,6 +62,19 @@ public class LiveMonitoringFragment extends BaseSettingsFragment {
updateToolbarSwitch();
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (preference.getKey().equals(settings.LIVE_MONITORING_URL.getId())) {
if (Algorithms.isValidMessageFormat((String) newValue)) {
return super.onPreferenceChange(preference, newValue);
} else {
Toast.makeText(app, R.string.wrong_format, Toast.LENGTH_SHORT).show();
return false;
}
}
return super.onPreferenceChange(preference, newValue);
}
private void updateToolbarSwitch() {
View view = getView();
if (view == null) {