diff options
author | Christian Schneppe <christian@pix-art.de> | 2019-09-20 17:31:27 +0200 |
---|---|---|
committer | Christian Schneppe <christian@pix-art.de> | 2019-09-20 17:34:00 +0200 |
commit | e0989e7a82f65c32d655db266f67fcdc90dba77b (patch) | |
tree | b1b3ca8875f3c00eb1cd1af0b2921349c8166189 | |
parent | 639f3c485bac93721db994339e4624b4e12e4f5f (diff) |
replace YouTube links with Invidious links
increases privacy and is configurable via preferences
-rw-r--r-- | src/main/java/de/pixart/messenger/Config.java | 2 | ||||
-rw-r--r-- | src/main/java/de/pixart/messenger/ui/SettingsActivity.java | 1 | ||||
-rw-r--r-- | src/main/java/de/pixart/messenger/ui/adapter/ConversationAdapter.java | 6 | ||||
-rw-r--r-- | src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java | 10 | ||||
-rw-r--r-- | src/main/java/de/pixart/messenger/ui/util/MyLinkify.java | 28 | ||||
-rw-r--r-- | src/main/res/values/defaults.xml | 29 | ||||
-rw-r--r-- | src/main/res/values/strings.xml | 2 | ||||
-rw-r--r-- | src/main/res/xml/preferences.xml | 7 |
8 files changed, 63 insertions, 22 deletions
diff --git a/src/main/java/de/pixart/messenger/Config.java b/src/main/java/de/pixart/messenger/Config.java index 13e1cc28b..b4b161cb5 100644 --- a/src/main/java/de/pixart/messenger/Config.java +++ b/src/main/java/de/pixart/messenger/Config.java @@ -39,7 +39,6 @@ public final class Config { return (ENCRYPTION_MASK & (ENCRYPTION_MASK - 1)) != 0; } - public static final String LOGTAG = "Pix-Art_Messenger"; public static final Jid BUG_REPORTS = Jid.of("bugs@pix-art.de"); @@ -55,6 +54,7 @@ public final class Config { public static final String MAGIC_CREATE_DOMAIN = "blabber.im"; public static final String QUICKSY_DOMAIN = "quicksy.im"; public static final String CHANNEL_DISCOVERY = "https://search.jabbercat.org"; + public static final String DEFAULT_INVIDIOUS_HOST = "invidio.us"; public static final boolean DISALLOW_REGISTRATION_IN_UI = false; //hide the register checkbox public static final boolean USE_RANDOM_RESOURCE_ON_EVERY_BIND = false; diff --git a/src/main/java/de/pixart/messenger/ui/SettingsActivity.java b/src/main/java/de/pixart/messenger/ui/SettingsActivity.java index fae741587..3c70ee089 100644 --- a/src/main/java/de/pixart/messenger/ui/SettingsActivity.java +++ b/src/main/java/de/pixart/messenger/ui/SettingsActivity.java @@ -69,6 +69,7 @@ public class SettingsActivity extends XmppActivity implements public static final String FORBID_SCREENSHOTS = "screen_security"; public static final String CONFIRM_MESSAGES = "confirm_messages"; public static final String INDICATE_RECEIVED = "indicate_received"; + public static final String USE_INVIDIOUS = "use_invidious"; public static final int REQUEST_CREATE_BACKUP = 0xbf8701; Preference multiAccountPreference; diff --git a/src/main/java/de/pixart/messenger/ui/adapter/ConversationAdapter.java b/src/main/java/de/pixart/messenger/ui/adapter/ConversationAdapter.java index f7be6a365..fe4218dd4 100644 --- a/src/main/java/de/pixart/messenger/ui/adapter/ConversationAdapter.java +++ b/src/main/java/de/pixart/messenger/ui/adapter/ConversationAdapter.java @@ -29,6 +29,8 @@ import de.pixart.messenger.utils.UIHelper; import de.pixart.messenger.xmpp.chatstate.ChatState; import rocks.xmpp.addr.Jid; +import static de.pixart.messenger.ui.util.MyLinkify.replaceYoutube; + public class ConversationAdapter extends RecyclerView.Adapter<ConversationAdapter.ConversationViewHolder> { private XmppActivity activity; @@ -104,7 +106,7 @@ public class ConversationAdapter extends RecyclerView.Adapter<ConversationAdapte if (draft != null) { viewHolder.binding.conversationLastmsgImg.setVisibility(View.GONE); - viewHolder.binding.conversationLastmsg.setText(EmojiWrapper.transform(draft.getMessage())); + viewHolder.binding.conversationLastmsg.setText(EmojiWrapper.transform(replaceYoutube(activity.getApplicationContext(), draft.getMessage()))); viewHolder.binding.senderName.setText(R.string.draft); viewHolder.binding.senderName.setVisibility(View.VISIBLE); viewHolder.binding.conversationLastmsg.setTypeface(null, Typeface.NORMAL); @@ -147,7 +149,7 @@ public class ConversationAdapter extends RecyclerView.Adapter<ConversationAdapte } final Pair<CharSequence, Boolean> preview = UIHelper.getMessagePreview(activity, message, viewHolder.binding.conversationLastmsg.getCurrentTextColor()); if (showPreviewText) { - viewHolder.binding.conversationLastmsg.setText(EmojiWrapper.transform(UIHelper.shorten(preview.first))); + viewHolder.binding.conversationLastmsg.setText(EmojiWrapper.transform(UIHelper.shorten(replaceYoutube(activity.getApplicationContext(), preview.first.toString())))); } else { viewHolder.binding.conversationLastmsgImg.setContentDescription(preview.first); } diff --git a/src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java b/src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java index 1dd98b5bf..e3719d205 100644 --- a/src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java +++ b/src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java @@ -90,6 +90,7 @@ import static de.pixart.messenger.ui.SettingsActivity.PLAY_GIF_INSIDE; import static de.pixart.messenger.ui.SettingsActivity.SHOW_LINKS_INSIDE; import static de.pixart.messenger.ui.SettingsActivity.SHOW_MAPS_INSIDE; import static de.pixart.messenger.ui.util.MyLinkify.removeTrailingBracket; +import static de.pixart.messenger.ui.util.MyLinkify.replaceYoutube; public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextView.CopyHandler { @@ -536,7 +537,7 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie viewHolder.messageBody.setTypeface(null, Typeface.NORMAL); if (message.getBody() != null) { final String nick = UIHelper.getMessageDisplayName(message); - SpannableStringBuilder body = message.getMergedBody(); + SpannableStringBuilder body = new SpannableStringBuilder(replaceYoutube(activity.getApplicationContext(), message.getMergedBody().toString())); boolean hasMeCommand = message.hasMeCommand(); if (hasMeCommand) { body = body.replace(0, Message.ME_COMMAND.length(), nick); @@ -695,7 +696,7 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie viewHolder.image.setVisibility(View.GONE); viewHolder.gifImage.setVisibility(View.GONE); viewHolder.download_button.setVisibility(View.GONE); - Editable body = new SpannableStringBuilder(message.getBody()); + SpannableStringBuilder body = new SpannableStringBuilder(replaceYoutube(activity.getApplicationContext(), message.getMergedBody().toString())); final boolean dataSaverDisabled = activity.xmppConnectionService.isDataSaverDisabled(); viewHolder.richlinkview.setVisibility(View.VISIBLE); if (mShowLinksInside) { @@ -857,6 +858,7 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie } private void toggleWhisperInfo(ViewHolder viewHolder, final Message message, final boolean includeBody, final boolean darkBackground) { + SpannableStringBuilder messageBody = new SpannableStringBuilder(replaceYoutube(activity.getApplicationContext(), message.getBody())); Editable body; if (message.isPrivateMessage()) { final String privateMarker; @@ -870,7 +872,7 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie viewHolder.messageBody.setVisibility(View.VISIBLE); if (includeBody) { body.append("\n"); - body.append(message.getBody()); + body.append(messageBody); } body.setSpan(new ForegroundColorSpan(getMessageTextColor(darkBackground, false)), 0, privateMarker.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); body.setSpan(new StyleSpan(Typeface.BOLD), 0, privateMarker.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); @@ -883,7 +885,7 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie } else { if (includeBody) { viewHolder.messageBody.setVisibility(View.VISIBLE); - body = new SpannableStringBuilder(message.getBody()); + body = new SpannableStringBuilder(messageBody); MyLinkify.addLinks(body, false); viewHolder.messageBody.setAutoLinkMask(0); viewHolder.messageBody.setText(body); diff --git a/src/main/java/de/pixart/messenger/ui/util/MyLinkify.java b/src/main/java/de/pixart/messenger/ui/util/MyLinkify.java index fb271cd7c..401c2cd40 100644 --- a/src/main/java/de/pixart/messenger/ui/util/MyLinkify.java +++ b/src/main/java/de/pixart/messenger/ui/util/MyLinkify.java @@ -29,13 +29,21 @@ package de.pixart.messenger.ui.util; +import android.content.Context; +import android.content.SharedPreferences; import android.os.Build; +import android.preference.PreferenceManager; import android.text.Editable; +import android.text.SpannableString; import android.text.util.Linkify; import java.util.Locale; +import java.util.regex.Matcher; import java.util.regex.Pattern; +import de.pixart.messenger.Config; +import de.pixart.messenger.R; +import de.pixart.messenger.ui.SettingsActivity; import de.pixart.messenger.ui.text.FixedURLSpan; import de.pixart.messenger.utils.GeoHelper; import de.pixart.messenger.utils.Patterns; @@ -43,6 +51,26 @@ import de.pixart.messenger.utils.XmppUri; public class MyLinkify { + private static final Pattern youtubePattern = Pattern.compile("(www\\.|m\\.)?(youtube\\.com|youtu\\.be|youtube-nocookie\\.com)\\/(((?!(\"|'|<)).)*)"); + + public static SpannableString replaceYoutube(Context context, String url) { + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); + boolean invidious = sharedPreferences.getBoolean(SettingsActivity.USE_INVIDIOUS, context.getResources().getBoolean(R.bool.use_invidious)); + if (invidious) { + Matcher matcher = youtubePattern.matcher(url); + while (matcher.find()) { + final String youtubeId = matcher.group(3); + String invidiousHost = Config.DEFAULT_INVIDIOUS_HOST.toLowerCase(); + if (matcher.group(2) != null && matcher.group(2).equals("youtu.be")) { + return new SpannableString(url.replaceAll("https://" + Pattern.quote(matcher.group()), Matcher.quoteReplacement("https://" + invidiousHost + "/watch?v=" + youtubeId + "&local=true"))); + } else { + return new SpannableString(url.replaceAll("https://" + Pattern.quote(matcher.group()), Matcher.quoteReplacement("https://" + invidiousHost + "/" + youtubeId + "&local=true"))); + } + } + } + return new SpannableString(url); + } + private static final Linkify.TransformFilter WEBURL_TRANSFORM_FILTER = (matcher, url) -> { if (url == null) { return null; diff --git a/src/main/res/values/defaults.xml b/src/main/res/values/defaults.xml index 0892b0c63..137a3541c 100644 --- a/src/main/res/values/defaults.xml +++ b/src/main/res/values/defaults.xml @@ -58,26 +58,17 @@ \n\nhttps://github.com/PonnamKarthik/RichLinkPreview\n(Apache License, Version 2.0) </string> - <string name="default_resource" translatable="false">Phone</string> <bool name="portrait_only">true</bool> <bool name="enter_is_send">false</bool> <bool name="dnd_on_silent_mode">false</bool> <bool name="treat_vibrate_as_silent">false</bool> - <integer name="auto_accept_filesize_wifi">10485760</integer> - <integer name="auto_accept_filesize_mobile">524288</integer> - <integer name="auto_accept_filesize_roaming">0</integer> <bool name="vibrate_on_notification">true</bool> <bool name="led">true</bool> - <string name="notification_ringtone">content://settings/system/notification_sound</string> <bool name="notifications_from_strangers">true</bool> <bool name="headsup_notifications">true</bool> <bool name="enable_quiet_hours">false</bool> <bool name="manually_change_presence">true</bool> <bool name="away_when_screen_off">false</bool> - <string name="picture_compression">auto</string> - <string name="image_compression">mid</string> - <string name="video_compression">mid</string> - <integer name="automatic_message_deletion">0</integer> <bool name="confirm_messages">true</bool> <bool name="allow_message_correction">true</bool> <bool name="chat_states">true</bool> @@ -89,14 +80,13 @@ <bool name="dont_trust_system_cas">false</bool> <bool name="btbv">true</bool> <bool name="send_button_status">true</bool> - <string name="quick_action">recent</string> <bool name="display_enter_key">false</bool> <bool name="show_dynamic_tags">false</bool> - <integer name="grace_period">144</integer> <bool name="presence_colored_names">false</bool> <bool name="use_max_brightness">true</bool> <bool name="auto_rotate">true</bool> <bool name="send_crashreport">true</bool> + <bool name="use_invidious">true</bool> <bool name="plain_text_logs">false</bool> <bool name="validate_hostname">false</bool> <bool name="show_foreground_service">true</bool> @@ -105,9 +95,6 @@ <bool name="enable_multi_accounts">false</bool> <bool name="show_qr_code_scan">true</bool> <bool name="scroll_to_bottom">true</bool> - <string name="omemo_setting_default">default_off</string> - <string name="theme">auto</string> - <string name="default_font_size">small</string> <bool name="start_searching">false</bool> <bool name="show_record_voice_btn">true</bool> <bool name="quick_share_attachment_choice">true</bool> @@ -119,6 +106,20 @@ <bool name="use_internal_updater">true</bool> <bool name="show_own_accounts">true</bool> <bool name="vibrate_in_chat">true</bool> + <integer name="auto_accept_filesize_wifi">10485760</integer> + <integer name="auto_accept_filesize_mobile">524288</integer> + <integer name="auto_accept_filesize_roaming">0</integer> + <integer name="automatic_message_deletion">0</integer> + <integer name="grace_period">144</integer> + <string name="default_resource" translatable="false">Phone</string> + <string name="notification_ringtone">content://settings/system/notification_sound</string> + <string name="picture_compression">auto</string> + <string name="image_compression">mid</string> + <string name="video_compression">mid</string> + <string name="quick_action">recent</string> + <string name="omemo_setting_default">default_off</string> + <string name="theme">auto</string> + <string name="default_font_size">small</string> <string-array name="domains"> <item>pix-art.de</item> diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index fa3ebe495..15556843c 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -938,4 +938,6 @@ <string name="x_has_written">%s has written:</string> <string name="no_status_set_instructions">You have not set a presence status message</string> <string name="your_status">Your status</string> + <string name="pref_use_invidious">Replace YouTube links with Invidious</string> + <string name="pref_use_invidious_summary">Invidious is an alternative and privacy friendly alternative to YouTube</string> </resources> diff --git a/src/main/res/xml/preferences.xml b/src/main/res/xml/preferences.xml index d5ce35819..6273d601b 100644 --- a/src/main/res/xml/preferences.xml +++ b/src/main/res/xml/preferences.xml @@ -262,7 +262,7 @@ android:summary="@string/pref_treat_vibrate_as_dnd_summary" android:title="@string/pref_treat_vibrate_as_silent" /> </PreferenceScreen> - <!--Security--> + <!--Security and Privacy--> <PreferenceScreen android:icon="?attr/ic_settings_security" android:key="security" @@ -337,6 +337,11 @@ android:key="crashreport" android:summary="@string/pref_send_crash_summary" android:title="@string/pref_send_crash" /> + <CheckBoxPreference + android:defaultValue="@bool/use_invidious" + android:key="use_invidious" + android:summary="@string/pref_use_invidious_summary" + android:title="@string/pref_use_invidious" /> <PreferenceCategory android:title="@string/pref_create_backup"> <Preference android:key="create_backup" |