mirror of
https://github.com/TrianguloY/UrlChecker.git
synced 2024-09-19 20:02:16 +02:00
parent
9ef77acc6a
commit
f311071afe
@ -17,6 +17,7 @@ import com.trianguloy.urlchecker.modules.AModuleData;
|
||||
import com.trianguloy.urlchecker.modules.AModuleDialog;
|
||||
import com.trianguloy.urlchecker.url.UrlData;
|
||||
import com.trianguloy.urlchecker.utilities.GenericPref;
|
||||
import com.trianguloy.urlchecker.utilities.JavaUtils;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@ -29,6 +30,10 @@ public class LogModule extends AModuleData {
|
||||
return new GenericPref.Str("log_data", "", cntx);
|
||||
}
|
||||
|
||||
public static GenericPref.Int LOG_LIMIT(Context cntx) {
|
||||
return new GenericPref.Int("log_limit", 0, cntx); // 0 means unlimited
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getId() {
|
||||
return "log";
|
||||
@ -53,10 +58,12 @@ public class LogModule extends AModuleData {
|
||||
class LogDialog extends AModuleDialog {
|
||||
|
||||
private final GenericPref.Str log;
|
||||
private final GenericPref.Int limit;
|
||||
|
||||
public LogDialog(MainDialog dialog) {
|
||||
super(dialog);
|
||||
log = LogModule.LOG_DATA(dialog);
|
||||
limit = LogModule.LOG_LIMIT(dialog);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -67,15 +74,21 @@ class LogDialog extends AModuleDialog {
|
||||
@Override
|
||||
public void onInitialize(View views) {
|
||||
// new instance, log date
|
||||
log.add((log.get().isEmpty() ? "" : "\n")
|
||||
+ "--- " + new Date().toLocaleString() + " ---\n"
|
||||
);
|
||||
addLine("--- " + new Date().toLocaleString() + " ---");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPrepareUrl(UrlData urlData) {
|
||||
// new url, log it
|
||||
log.add("> " + urlData.url + "\n");
|
||||
addLine("> " + urlData.url);
|
||||
}
|
||||
|
||||
private void addLine(String line) {
|
||||
var text = log.get();
|
||||
if (limit.get() > 0) text = JavaUtils.limitLines(text, '\n', limit.get() - 1);
|
||||
if (!text.isEmpty()) text += "\n";
|
||||
text += line.replace("\n", "%0A");
|
||||
log.set(text);
|
||||
}
|
||||
}
|
||||
|
||||
@ -97,6 +110,8 @@ class LogConfig extends AModuleConfig {
|
||||
public void onInitialize(View views) {
|
||||
views.findViewById(R.id.view).setOnClickListener(v -> showLog(false));
|
||||
views.findViewById(R.id.edit).setOnClickListener(v -> showLog(true));
|
||||
|
||||
LogModule.LOG_LIMIT(getActivity()).attachToEditText(views.findViewById(R.id.limit), 0);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -105,6 +105,32 @@ public abstract class GenericPref<T> {
|
||||
protected void save(Integer value) {
|
||||
prefs.edit().putInt(prefName, value).apply();
|
||||
}
|
||||
|
||||
/**
|
||||
* This editText will be set to the pref value, and when the editText changes the value will too.
|
||||
* The special empty value will be set when the input is empty.
|
||||
*/
|
||||
public void attachToEditText(EditText editText, int empty) {
|
||||
editText.setText(get() == empty ? "" : get().toString());
|
||||
editText.addTextChangedListener(new DefaultTextWatcher() {
|
||||
@Override
|
||||
public void afterTextChanged(Editable s) {
|
||||
try {
|
||||
// empty -> set empty
|
||||
if (s.length() == 0) set(empty);
|
||||
else {
|
||||
var value = Integer.parseInt(s.toString());
|
||||
if (value == empty) s.clear(); // empty input -> clear
|
||||
set(value);
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
// shouldn't be possible, but just in case
|
||||
s.clear();
|
||||
s.append(get() == empty ? "" : get().toString());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -65,6 +65,26 @@ public interface JavaUtils {
|
||||
return match;
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes the [limit] last lines of a [text], separated by [delimiter].
|
||||
* This is equivalent to text.split(delimiter).takeLast(limit).join(delimiter) but using java sdk 14 (and hopefully more efficient)
|
||||
*/
|
||||
static String limitLines(String text, char delimiter, int limit) {
|
||||
// early exit
|
||||
if (limit <= 0 || text.isEmpty()) return "";
|
||||
|
||||
// find the first character of the last (first) line required
|
||||
var i = text.length() - 1; // character pointer
|
||||
var lines = 0; // count full lines (delimiters encountered)
|
||||
while (i >= 0 && lines < limit) {
|
||||
if (text.charAt(i) == delimiter) lines++; // delimiter found
|
||||
i--; // continue
|
||||
}
|
||||
// split if necessary
|
||||
if (lines >= limit) text = text.substring(i + 2); // found the lines, split
|
||||
return text;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the object, or default if null
|
||||
* java.util.Optional requires api 24
|
||||
|
@ -9,6 +9,26 @@
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/mLog_desc" />
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal">
|
||||
|
||||
<TextView
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center_vertical"
|
||||
android:layout_weight="1"
|
||||
android:text="@string/mLog_limit" />
|
||||
|
||||
<EditText
|
||||
android:id="@+id/limit"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:hint="@string/mLog_unlimited"
|
||||
android:inputType="number" />
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
|
@ -272,6 +272,8 @@ This module has been deprecated in favor of the 'Uri parts' module"</string>
|
||||
<string name="mLog_view">View log</string>
|
||||
<string name="mLog_edit">Edit log</string>
|
||||
<string name="mLog_empty">No data</string>
|
||||
<string name="mLog_limit">Maximum number of entries to keep. Recommended value: 100. Set empty for unlimited</string>
|
||||
<string name="mLog_unlimited">Unlimited</string>
|
||||
<!-- -->
|
||||
<string name="mHosts_name">Hosts labeler</string>
|
||||
<string name="mHosts_desc">"This module labels hosts, configured either by specifying them manually or by using a remote hosts-like file. You can use it to warn about dangerous or special sites. The builtin configuration specifies StevenBlack's hosts (adware/malware, fakenews, gambling and adult content) from https://github.com/StevenBlack/hosts"</string>
|
||||
|
Loading…
Reference in New Issue
Block a user