0
0
mirror of https://github.com/TrianguloY/UrlChecker.git synced 2024-09-19 20:02:16 +02:00

avoid crash on UrlDecoder.decode

fixes #253
This commit is contained in:
TrianguloY 2024-07-15 10:42:39 +02:00
parent e21f758f26
commit 4a43fc2025
4 changed files with 30 additions and 31 deletions

View File

@ -1,5 +1,7 @@
package com.trianguloy.urlchecker.modules.list;
import static com.trianguloy.urlchecker.utilities.methods.UrlUtils.decode;
import android.content.Context;
import android.util.Pair;
import android.view.View;
@ -364,11 +366,11 @@ class ClearUrlDialog extends AModuleDialog {
* Idea from https://stackoverflow.com/a/6926987, but using own implementation
*/
private static String decodeURIComponent(String text) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
String[] parts = text.split("\\+");
for (String part : parts) {
var result = new StringBuilder();
var parts = text.split("\\+");
for (var part : parts) {
if (result.length() != 0) result.append('+');
result.append(URLDecoder.decode(part, "UTF-8"));
result.append(decode(part));
}
return result.toString();
}

View File

@ -1,5 +1,7 @@
package com.trianguloy.urlchecker.modules.list;
import static com.trianguloy.urlchecker.utilities.methods.UrlUtils.decode;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
@ -168,7 +170,7 @@ class PatternDialog extends AModuleDialog {
// decode if required
if (data.optBoolean("decode")) {
message.newUrl = URLDecoder.decode(message.newUrl);
message.newUrl = decode(message.newUrl);
}
// automatic? apply

View File

@ -1,5 +1,7 @@
package com.trianguloy.urlchecker.modules.list;
import static com.trianguloy.urlchecker.utilities.methods.UrlUtils.decode;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
@ -197,12 +199,7 @@ class RemoveQueriesDialog extends AModuleDialog {
public String getQueryValue(int index) {
String[] split = queries.get(index).split("=");
if (split.length == 1) return "";
try {
return URLDecoder.decode(split[1]);
} catch (Exception e) {
// can't decode, return it directly
return split[1];
}
return decode(split[1]);
}
/**

View File

@ -9,37 +9,24 @@ import android.widget.Toast;
import com.trianguloy.urlchecker.R;
import com.trianguloy.urlchecker.utilities.wrappers.IntentApp;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
/**
* Static utilities related to urls
*/
/** Static utilities related to urls */
public interface UrlUtils {
/**
* Returns an intent that will open the given url, with an optional package
*
* @param url the url that will be opened
* @param intentApp the intentApp that will be opened, null to let android choose
* @return the converted intent
*/
/** Returns an intent that will open the given [url], with an optional [intentApp] */
static Intent getViewIntent(String url, IntentApp intentApp) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
var intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
if (intentApp != null) intent.setComponent(intentApp.getComponent());
return intent;
}
/**
* Opens an url removing this app from the chooser
*
* @param url url to open
* @param cntx base context
*/
/** Opens an [url] removing this app from the chooser */
static void openUrlRemoveThis(String url, Context cntx) {
// get intents that can open the url
List<Intent> intents = new ArrayList<>();
var intents = new ArrayList<Intent>();
for (var pack : IntentApp.getOtherPackages(getViewIntent(url, null), cntx)) {
intents.add(getViewIntent(url, pack));
}
@ -51,10 +38,21 @@ public interface UrlUtils {
}
// create chooser
Intent chooserIntent = Intent.createChooser(intents.remove(0), cntx.getString(R.string.title_choose));
var chooserIntent = Intent.createChooser(intents.remove(0), cntx.getString(R.string.title_choose));
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intents.toArray(new Parcelable[0]));
// open
PackageUtils.startActivity(chooserIntent, R.string.toast_noBrowser, cntx);
}
/** Calls URLDecoder.decode but returns the input string if the decoding failed */
static String decode(String string) {
try {
return URLDecoder.decode(string, "UTF-8");
} catch (Exception e) {
// can't decode, just leave it
e.printStackTrace();
return string;
}
}
}