Remove obsolete custom URL Filter code

This commit is contained in:
Arjan Schrijver
2025-12-25 14:45:08 +01:00
committed by Arjan Schrijver
parent fa5e52013b
commit d431d73dc3
9 changed files with 0 additions and 311 deletions
@@ -16,41 +16,16 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.activities
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.Menu
import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import android.widget.TextView
import androidx.appcompat.widget.PopupMenu
import androidx.core.net.toUri
import androidx.preference.Preference
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.textfield.TextInputEditText
import com.google.android.material.textfield.TextInputLayout
import nodomain.freeyourgadget.gadgetbridge.BuildConfig
import nodomain.freeyourgadget.gadgetbridge.R
import nodomain.freeyourgadget.gadgetbridge.database.DBHelper
import nodomain.freeyourgadget.gadgetbridge.entities.URLFilterEntry
import nodomain.freeyourgadget.gadgetbridge.util.AndroidUtils
import nodomain.freeyourgadget.gadgetbridge.util.PermissionsUtils.PACKAGE_INTERNET_HELPER
class InternetHelperPreferencesActivity : AbstractGBActivity() {
val urlItems = ArrayList<UrlListAdapter.UrlEntry>()
private fun retrieveList() {
val urlFilterEntries = DBHelper.getURLFilterEntries()
urlItems.clear()
for (entry in urlFilterEntries) {
urlItems.add(UrlListAdapter.UrlEntry(entry))
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_internet_helper_preferences)
@@ -59,56 +34,6 @@ class InternetHelperPreferencesActivity : AbstractGBActivity() {
.beginTransaction()
.replace(R.id.settings_container, InternetHelperPreferencesFragment())
.commit()
retrieveList()
val recyclerView = findViewById<RecyclerView>(R.id.internet_helper_url_list)
recyclerView.isNestedScrollingEnabled = false
recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.adapter = UrlListAdapter(this, urlItems) { entry, action ->
when (action) {
UrlListAdapter.UrlAction.ALLOW -> {
entry.urlFilterEntry.allowed = true
DBHelper.store(entry.urlFilterEntry)
recyclerView.adapter?.notifyDataSetChanged()
}
UrlListAdapter.UrlAction.DENY -> {
entry.urlFilterEntry.allowed = false
DBHelper.store(entry.urlFilterEntry)
recyclerView.adapter?.notifyDataSetChanged()
}
UrlListAdapter.UrlAction.EDIT -> {
val inputLayout = TextInputLayout(this)
val editText = TextInputEditText(this).apply {
setText(entry.urlFilterEntry.url)
}
inputLayout.addView(editText)
inputLayout.hint = getString(R.string.internet_helper_url_filter_hint)
MaterialAlertDialogBuilder(this)
.setTitle(getString(R.string.internet_helper_url_filter_title))
.setView(inputLayout)
.setPositiveButton(R.string.save) { dialog, _ ->
entry.urlFilterEntry.url = editText.text.toString()
DBHelper.store(entry.urlFilterEntry)
recyclerView.adapter?.notifyDataSetChanged()
}
.setNegativeButton(getString(R.string.Cancel), null)
.show()
}
UrlListAdapter.UrlAction.DELETE -> {
MaterialAlertDialogBuilder(this)
.setPositiveButton(R.string.yes) {_,_ ->
DBHelper.delete(entry.urlFilterEntry)
retrieveList()
recyclerView.adapter?.notifyDataSetChanged()
}
.setNegativeButton(R.string.no, null)
.setMessage(getString(R.string.internet_helper_url_filter_delete))
.show()
}
}
}
}
class InternetHelperPreferencesFragment : AbstractPreferenceFragment() {
@@ -132,73 +57,4 @@ class InternetHelperPreferencesActivity : AbstractGBActivity() {
}
}
}
class UrlListAdapter(
private val context: Context,
private val urls: List<UrlEntry>,
private val onAction: (UrlEntry, UrlAction) -> Unit
) : RecyclerView.Adapter<UrlListAdapter.UrlViewHolder>() {
data class UrlEntry(
val urlFilterEntry: URLFilterEntry,
)
enum class UrlAction {
ALLOW, DENY, EDIT, DELETE
}
inner class UrlViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val title: TextView = itemView.findViewById(R.id.url_title)
val status: TextView = itemView.findViewById(R.id.url_status)
val menuButton: ImageButton = itemView.findViewById(R.id.url_menu_button)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): UrlViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_url_list_entry, parent, false)
return UrlViewHolder(view)
}
override fun onBindViewHolder(holder: UrlViewHolder, position: Int) {
val entry = urls[position]
holder.title.text = entry.urlFilterEntry.url
holder.status.text = if (entry.urlFilterEntry.allowed)
context.getString(R.string.internet_helper_url_allowed)
else
context.getString(R.string.internet_helper_url_denied)
holder.menuButton.setOnClickListener {
showPopupMenu(holder.menuButton, entry)
}
}
private fun showPopupMenu(anchor: View, entry: UrlEntry) {
val popupMenu = PopupMenu(anchor.context, anchor)
if (entry.urlFilterEntry.allowed) {
popupMenu.menu.add(Menu.NONE, 1, Menu.NONE,
context.getString(R.string.internet_helper_url_action_deny))
} else {
popupMenu.menu.add(Menu.NONE, 2, Menu.NONE,
context.getString(R.string.internet_helper_url_action_allow))
}
popupMenu.menu.add(Menu.NONE, 3, Menu.NONE,
context.getString(R.string.internet_helper_url_action_edit))
popupMenu.menu.add(Menu.NONE, 4, Menu.NONE,
context.getString(R.string.internet_helper_url_action_delete))
popupMenu.setOnMenuItemClickListener { item ->
when (item.itemId) {
1 -> onAction(entry, UrlAction.DENY)
2 -> onAction(entry, UrlAction.ALLOW)
3 -> onAction(entry, UrlAction.EDIT)
4 -> onAction(entry, UrlAction.DELETE)
}
true
}
popupMenu.show()
}
override fun getItemCount(): Int = urls.size
}
}
@@ -64,8 +64,6 @@ import nodomain.freeyourgadget.gadgetbridge.entities.Reminder;
import nodomain.freeyourgadget.gadgetbridge.entities.ReminderDao;
import nodomain.freeyourgadget.gadgetbridge.entities.Tag;
import nodomain.freeyourgadget.gadgetbridge.entities.TagDao;
import nodomain.freeyourgadget.gadgetbridge.entities.URLFilterEntry;
import nodomain.freeyourgadget.gadgetbridge.entities.URLFilterEntryDao;
import nodomain.freeyourgadget.gadgetbridge.entities.User;
import nodomain.freeyourgadget.gadgetbridge.entities.UserAttributes;
import nodomain.freeyourgadget.gadgetbridge.entities.UserDao;
@@ -77,7 +75,6 @@ import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
import nodomain.freeyourgadget.gadgetbridge.model.ValidByDate;
import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils;
import nodomain.freeyourgadget.gadgetbridge.util.FileUtils;
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
import nodomain.freeyourgadget.gadgetbridge.util.GBPrefs;
@@ -726,20 +723,6 @@ public class DBHelper {
return Collections.emptyList();
}
@NonNull
public static List<URLFilterEntry> getURLFilterEntries() {
try (DBHandler db = GBApplication.acquireDB()) {
final DaoSession daoSession = db.getDaoSession();
final URLFilterEntryDao urlFilterEntryDao = daoSession.getURLFilterEntryDao();
final QueryBuilder<URLFilterEntry> qb = urlFilterEntryDao.queryBuilder();
return qb.build().list();
} catch (final Exception e) {
LOG.error("Error reading contacts from db", e);
}
return Collections.emptyList();
}
public static void store(final Reminder reminder) {
try (DBHandler db = GBApplication.acquireDB()) {
final DaoSession daoSession = db.getDaoSession();
@@ -767,15 +750,6 @@ public class DBHelper {
}
}
public static void store(final URLFilterEntry entry) {
try (DBHandler db = GBApplication.acquireDB()) {
final DaoSession daoSession = db.getDaoSession();
daoSession.insertOrReplace(entry);
} catch (final Exception e) {
LOG.error("Error acquiring database", e);
}
}
public static void delete(final Reminder reminder) {
try (DBHandler db = GBApplication.acquireDB()) {
final DaoSession daoSession = db.getDaoSession();
@@ -803,15 +777,6 @@ public class DBHelper {
}
}
public static void delete(final URLFilterEntry entry) {
try (DBHandler db = GBApplication.acquireDB()) {
final DaoSession daoSession = db.getDaoSession();
daoSession.delete(entry);
} catch (final Exception e) {
LOG.error("Error acquiring database", e);
}
}
public static void clearSession() {
try (DBHandler dbHandler = GBApplication.acquireDB()) {
DaoSession session = dbHandler.getDaoSession();
@@ -40,12 +40,9 @@ import java.io.ByteArrayInputStream;
import java.time.LocalDate;
import java.time.ZonedDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.database.DBHelper;
import nodomain.freeyourgadget.gadgetbridge.entities.URLFilterEntry;
import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec;
import nodomain.freeyourgadget.gadgetbridge.model.weather.Weather;
import nodomain.freeyourgadget.gadgetbridge.model.weather.WeatherMapper;
@@ -97,7 +94,6 @@ public class GBWebClient extends WebViewClient {
boolean locallySupported = StringUtils.indexOfAny(requestedUri.getHost(), LocallySupportedDomains) != -1;
boolean urlIsAllowed = locallySupported;
boolean matchFound = false;
List<URLFilterEntry> urlFilterEntries = DBHelper.getURLFilterEntries();
// Allow full access to internet when available
boolean directInternetAccess = GBApplication.hasDirectInternetAccess();
@@ -125,17 +121,6 @@ public class GBWebClient extends WebViewClient {
urlIsAllowed = prefs.getBoolean("pref_key_internethelper_allow_bangle_app_loader", false);
}
// Search for matches
if (!matchFound) {
for (URLFilterEntry entry : urlFilterEntries) {
if (requestedUri.toString().contains(entry.getUrl())) {
matchFound = true;
urlIsAllowed = entry.getAllowed();
LOG.info("URL matched with URLFilterEntry: {}, allowed={}", entry.getUrl(), entry.getAllowed());
}
}
}
// Handle OpenWeatherMap locally
boolean forceLocal = false;
if (locallySupported && prefs.getBoolean("pref_key_internethelper_force_local", true)) {
@@ -144,17 +129,6 @@ public class GBWebClient extends WebViewClient {
forceLocal = true;
}
// Add to database if missing
if (requestedUri.getHost() != null && !matchFound) {
LOG.info("URL not matched with URLFilterEntry, storing new entry");
String defaultAction = prefs.getString("pref_key_internethelper_new_url_action", "deny");
urlIsAllowed = defaultAction.equals("allow");
URLFilterEntry filterEntry = new URLFilterEntry();
filterEntry.setUrl(requestedUri.toString().replaceAll("\\?.*", ""));
filterEntry.setAllowed(urlIsAllowed);
DBHelper.store(filterEntry);
}
// Handle request
if (requestedUri.getHost() != null && urlIsAllowed) {
if (!forceLocal && !directInternetAccess && InternetHelperSingleton.INSTANCE.ensureInternetHelperBound()) {
@@ -11,18 +11,9 @@
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- Preference screen -->
<FrameLayout
android:id="@+id/settings_container"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- Dynamic URL List -->
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/internet_helper_url_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:paddingTop="8dp"/>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
@@ -1,44 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dp">
<TextView
android:id="@+id/url_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="example.com"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toStartOf="@+id/url_menu_button"
app:layout_constraintBottom_toTopOf="@+id/url_status"
android:maxLines="1"
android:ellipsize="end" />
<TextView
android:id="@+id/url_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/internet_helper_url_allowed"
android:textSize="14sp"
android:textColor="@android:color/darker_gray"
app:layout_constraintStart_toStartOf="@id/url_title"
app:layout_constraintTop_toBottomOf="@id/url_title"
app:layout_constraintBottom_toBottomOf="parent" />
<ImageButton
android:id="@+id/url_menu_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_menu"
android:contentDescription="@string/internet_helper_url_options"
android:background="?attr/selectableItemBackgroundBorderless"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
-9
View File
@@ -4650,15 +4650,6 @@
<item>net.osmand.dev</item>
</string-array>
<string-array name="internethelper_new_url_action">
<item>@string/internet_helper_url_action_allow</item>
<item>@string/internet_helper_url_action_deny</item>
</string-array>
<string-array name="internethelper_new_url_action_values">
<item>allow</item>
<item>deny</item>
</string-array>
<string-array name="workmode">
<item>@string/automatic</item>
<item>@string/manual</item>
-14
View File
@@ -4452,20 +4452,8 @@
<string name="activity_title_rebble_app_store">Rebble app store</string>
<string name="prefs_internet_helper_title">Internet helper</string>
<string name="prefs_internet_helper_summary">Configure the URLs that are allowed to be requested through the internet helper add-on app</string>
<string name="internet_helper_url_filter_hint">Please enter (a part of) a URL to match against.</string>
<string name="internet_helper_url_filter_title">URL filter</string>
<string name="internet_helper_url_filter_delete">Are you sure you want to delete this URL?</string>
<string name="internet_helper_url_allowed">Allowed</string>
<string name="internet_helper_url_denied">Denied</string>
<string name="internet_helper_url_action_deny">Deny</string>
<string name="internet_helper_url_action_allow">Allow</string>
<string name="internet_helper_url_action_edit">Edit</string>
<string name="internet_helper_url_action_delete">Delete</string>
<string name="internet_helper_url_options">Options</string>
<string name="internet_helper_pref_app_req_title">Add-on app required</string>
<string name="internet_helper_pref_app_req_summary">This feature requires the installation of an add-on app. Tap here to go to the download page.</string>
<string name="internet_helper_pref_new_url_action_title">New URL action</string>
<string name="internet_helper_pref_new_url_action_summary">Set the default action to take when a URL is requested that isn\'t already in the list below.</string>
<string name="internet_helper_pref_intercept_supported_title">Intercept supported requests</string>
<string name="internet_helper_pref_intercept_supported_summary">Some internet requests can be intercepted and responded to locally without sending anything to the internet.</string>
<string name="internet_helper_pref_allowed_connections">Allowed connections</string>
@@ -4477,8 +4465,6 @@
<string name="internet_helper_pref_pebble_app_config_summary">Allow access to webpages that are needed for configuring apps and watchfaces</string>
<string name="internet_helper_pref_bangle_app_loader_title">Bangle app loader</string>
<string name="internet_helper_pref_bangle_app_loader_summary">Access the Bangle app loader website</string>
<string name="internet_helper_pref_other_urls_title">Other URLs</string>
<string name="internet_helper_url_list_instruction_summary">Use the list below to configure which (parts of) URLs are allowed or denied to be forwarded to the internethelper add-on app. The validation is using a simple text comparison, so a part of a URL can be entered too.</string>
<string name="rebble_appstore_download_failed_wrong_content_type">Download failed, wrong content-type: %1$s</string>
<string name="rebble_appstore_download_failed">Download failed: %1$s</string>
<string name="rebble_appstore_fetch_app_info_failed_content_type">Fetching app info failed, wrong content-type: %1$s</string>
@@ -15,14 +15,6 @@
<PreferenceCategory
android:title="@string/pref_header_general"
app:iconSpaceReserved="false">
<ListPreference
android:defaultValue="deny"
android:entries="@array/internethelper_new_url_action"
android:entryValues="@array/internethelper_new_url_action_values"
android:key="pref_key_internethelper_new_url_action"
android:title="@string/internet_helper_pref_new_url_action_title"
android:summary="@string/internet_helper_pref_new_url_action_summary"
app:iconSpaceReserved="false" />
<SwitchPreferenceCompat
android:defaultValue="true"
android:key="pref_key_internethelper_force_local"
@@ -64,13 +56,4 @@
android:summary="@string/internet_helper_pref_bangle_app_loader_summary"
app:iconSpaceReserved="false" />
</PreferenceCategory>
<PreferenceCategory
android:key="pref_key_internethelper_url_list"
android:title="@string/internet_helper_pref_other_urls_title"
app:iconSpaceReserved="false">
<Preference
android:key="pref_key_internethelper_info"
android:summary="@string/internet_helper_url_list_instruction_summary"
app:iconSpaceReserved="false" />
</PreferenceCategory>
</PreferenceScreen>