├── CODEOWNERS ├── settings.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── sample ├── src │ └── main │ │ ├── res │ │ ├── mipmap-hdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ ├── values │ │ │ ├── colors.xml │ │ │ ├── strings.xml │ │ │ └── styles.xml │ │ ├── mipmap-anydpi-v26 │ │ │ ├── ic_launcher.xml │ │ │ └── ic_launcher_round.xml │ │ ├── drawable-v24 │ │ │ └── ic_launcher_foreground.xml │ │ ├── layout │ │ │ └── activity_main.xml │ │ └── drawable │ │ │ └── ic_launcher_background.xml │ │ ├── AndroidManifest.xml │ │ └── java │ │ └── org │ │ └── buffer │ │ └── sociallinkify │ │ └── sample │ │ └── MainActivity.kt ├── .gitignore ├── proguard-rules.pro └── build.gradle ├── lib ├── src │ └── main │ │ ├── AndroidManifest.xml │ │ └── java │ │ └── org │ │ └── buffer │ │ └── sociallinkify │ │ ├── model │ │ ├── PatternType.kt │ │ └── SocialNetwork.kt │ │ ├── span │ │ ├── CustomTabUrlSpan.kt │ │ ├── HashtagMentionUrlSpan.kt │ │ └── ClickableUrlSpan.kt │ │ ├── util │ │ └── CustomTabUtil.kt │ │ └── SocialLinkify.kt ├── .gitignore ├── proguard-rules.pro └── build.gradle ├── .gitignore ├── gradle.properties ├── licence.txt ├── gradlew.bat ├── README.md └── gradlew /CODEOWNERS: -------------------------------------------------------------------------------- 1 | * @bufferapp/apps 2 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':lib', ':sample' 2 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bufferapp/SocialLinkify/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bufferapp/SocialLinkify/HEAD/sample/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bufferapp/SocialLinkify/HEAD/sample/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bufferapp/SocialLinkify/HEAD/sample/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bufferapp/SocialLinkify/HEAD/sample/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bufferapp/SocialLinkify/HEAD/sample/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bufferapp/SocialLinkify/HEAD/sample/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bufferapp/SocialLinkify/HEAD/sample/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bufferapp/SocialLinkify/HEAD/sample/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /lib/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bufferapp/SocialLinkify/HEAD/sample/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bufferapp/SocialLinkify/HEAD/sample/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /lib/src/main/java/org/buffer/sociallinkify/model/PatternType.kt: -------------------------------------------------------------------------------- 1 | package org.buffer.sociallinkify.model 2 | 3 | enum class PatternType { 4 | HASHTAG, MENTION, URL, EMAIL 5 | } -------------------------------------------------------------------------------- /sample/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /lib/.gitignore: -------------------------------------------------------------------------------- 1 | *.apk 2 | *.ap_ 3 | *.dex 4 | *.class 5 | bin/ 6 | gen/ 7 | out/ 8 | build/ 9 | workspace.xml 10 | .idea 11 | local.properties 12 | ks.properties 13 | .classpath 14 | .project 15 | .DS_Store 16 | lint.xml 17 | protected_strings.xml 18 | .gradle 19 | /dist -------------------------------------------------------------------------------- /sample/.gitignore: -------------------------------------------------------------------------------- 1 | *.apk 2 | *.ap_ 3 | *.dex 4 | *.class 5 | bin/ 6 | gen/ 7 | out/ 8 | build/ 9 | workspace.xml 10 | .idea 11 | local.properties 12 | ks.properties 13 | .classpath 14 | .project 15 | .DS_Store 16 | lint.xml 17 | protected_strings.xml 18 | .gradle 19 | /dist -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.apk 2 | *.ap_ 3 | *.dex 4 | *.class 5 | bin/ 6 | gen/ 7 | out/ 8 | build/ 9 | workspace.xml 10 | .idea 11 | local.properties 12 | ks.properties 13 | .classpath 14 | .project 15 | .DS_Store 16 | lint.xml 17 | protected_strings.xml 18 | .gradle 19 | /dist 20 | *.iml -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Thu Mar 26 11:18:17 GMT 2020 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip 7 | -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /lib/src/main/java/org/buffer/sociallinkify/span/CustomTabUrlSpan.kt: -------------------------------------------------------------------------------- 1 | package org.buffer.sociallinkify.span 2 | 3 | import android.text.TextPaint 4 | 5 | class CustomTabUrlSpan( 6 | private val color: Int, 7 | url: String 8 | ) : ClickableUrlSpan(url) { 9 | 10 | override fun updateDrawState(ds: TextPaint) { 11 | ds.color = color 12 | ds.isUnderlineText = true 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /lib/src/main/java/org/buffer/sociallinkify/span/HashtagMentionUrlSpan.kt: -------------------------------------------------------------------------------- 1 | package org.buffer.sociallinkify.span 2 | 3 | import android.text.TextPaint 4 | 5 | class HashtagMentionUrlSpan( 6 | private val color: Int, 7 | url: String 8 | ) : ClickableUrlSpan(url) { 9 | 10 | override fun updateDrawState(ds: TextPaint) { 11 | ds.color = color 12 | ds.isUnderlineText = false 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /sample/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /lib/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /sample/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | org.gradle.jvmargs=-Xmx1536m 13 | 14 | # When configured, Gradle will run in incubating parallel mode. 15 | # This option should only be used with decoupled projects. More details, visit 16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 17 | # org.gradle.parallel=true 18 | android.useAndroidX=true 19 | android.enableJetifier=true -------------------------------------------------------------------------------- /sample/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'kotlin-android' 3 | apply plugin: 'kotlin-android-extensions' 4 | 5 | android { 6 | compileSdkVersion 29 7 | 8 | defaultConfig { 9 | applicationId "org.buffer.sociallinkify.sample" 10 | minSdkVersion 21 11 | targetSdkVersion 29 12 | versionCode 1 13 | versionName "1.0" 14 | } 15 | 16 | buildTypes { 17 | release { 18 | minifyEnabled false 19 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 20 | } 21 | } 22 | compileOptions { 23 | sourceCompatibility 1.8 24 | targetCompatibility 1.8 25 | } 26 | } 27 | 28 | dependencies { 29 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 30 | implementation project(':lib') 31 | } 32 | -------------------------------------------------------------------------------- /lib/src/main/java/org/buffer/sociallinkify/span/ClickableUrlSpan.kt: -------------------------------------------------------------------------------- 1 | package org.buffer.sociallinkify.span 2 | 3 | import android.content.ActivityNotFoundException 4 | import android.net.Uri 5 | import android.text.style.URLSpan 6 | import android.util.Log 7 | import android.view.View 8 | import org.buffer.sociallinkify.util.CustomTabUtil 9 | 10 | abstract class ClickableUrlSpan(url: String) : URLSpan(url) { 11 | 12 | override fun onClick(widget: View) { 13 | try { 14 | if (url.contains(":")) { 15 | CustomTabUtil.open(widget.context, Uri.parse(url)) 16 | } else { 17 | CustomTabUtil.open(widget.context, Uri.parse("https://$url")) 18 | } 19 | } catch (exception: ActivityNotFoundException) { 20 | Log.e(ClickableUrlSpan::class.java.simpleName, 21 | "Whoops, that doesn't look like a valid URL.") 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /licence.txt: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) [2018] [Buffer inc] 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /sample/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | SocialLinkify Sample 3 | 4 | Twitter 5 | Instagram 6 | Facebook 7 | Mastodon 8 | YouTube 9 | Email 10 | 11 | Visit @Buffer on Twitter 12 | View tweets with #buffer or #android 13 | Visit @Buffer on Instagram 14 | Visit @hackertronix on Mastodon 15 | View photos with #buffer or #android 16 | View posts with #buffer or #android 17 | View toots with #buffer or #android 18 | View posts with #buffer or #android 19 | Hi.there 20 | 21 | -------------------------------------------------------------------------------- /lib/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'kotlin-android' 3 | apply plugin: 'kotlin-android-extensions' 4 | apply plugin: 'com.novoda.bintray-release' 5 | 6 | android { 7 | compileSdkVersion 29 8 | 9 | defaultConfig { 10 | minSdkVersion 21 11 | targetSdkVersion 29 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | 20 | compileOptions { 21 | sourceCompatibility 1.8 22 | targetCompatibility 1.8 23 | } 24 | } 25 | 26 | tasks.withType(Javadoc).all { 27 | enabled = false 28 | } 29 | 30 | dependencies { 31 | implementation 'androidx.browser:browser:1.2.0' 32 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" 33 | } 34 | 35 | publish { 36 | repoName = 'Maven' 37 | userOrg = 'buffer' 38 | groupId = 'org.buffer.android' 39 | artifactId = 'social-linkify' 40 | publishVersion = '1.4' 41 | desc = 'An Android library for linking @ mentions & Hashtags to their corresponding social network' 42 | website = 'https://github.com/bufferapp/SocialLinkify' 43 | } -------------------------------------------------------------------------------- /sample/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 17 | 18 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /lib/src/main/java/org/buffer/sociallinkify/model/SocialNetwork.kt: -------------------------------------------------------------------------------- 1 | package org.buffer.sociallinkify.model 2 | 3 | enum class SocialNetwork(val id: String) { 4 | TWITTER("twitter"), 5 | FACEBOOK("facebook"), 6 | INSTAGRAM("instagram"), 7 | LINKEDIN("linkedin"), 8 | PINTEREST("pinterest"), 9 | GOOGLE_PLUS("google"), 10 | GOOGLEBUSINESS("googlebusiness"), 11 | STARTPAGE("startPage"), 12 | MASTODON("mastodon"), 13 | TIKTOK("tiktok"), 14 | YOUTUBE("youtube"), 15 | THREADS("threads"), 16 | BLUESKY("bluesky"),; 17 | 18 | companion object { 19 | fun fromString(name: String) = when (name) { 20 | TWITTER.id -> TWITTER 21 | FACEBOOK.id -> FACEBOOK 22 | INSTAGRAM.id -> INSTAGRAM 23 | LINKEDIN.id -> LINKEDIN 24 | PINTEREST.id -> PINTEREST 25 | GOOGLE_PLUS.id -> GOOGLE_PLUS 26 | GOOGLEBUSINESS.id -> GOOGLEBUSINESS 27 | STARTPAGE.id -> STARTPAGE 28 | MASTODON.id -> MASTODON 29 | TIKTOK.id -> TIKTOK 30 | YOUTUBE.id -> YOUTUBE 31 | THREADS.id -> THREADS 32 | BLUESKY.id -> BLUESKY 33 | else -> throw IllegalArgumentException("Whoops, the social network $name isn't known!") 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /lib/src/main/java/org/buffer/sociallinkify/util/CustomTabUtil.kt: -------------------------------------------------------------------------------- 1 | package org.buffer.sociallinkify.util 2 | 3 | import android.content.ActivityNotFoundException 4 | import android.content.Context 5 | import android.content.Intent 6 | import android.net.Uri 7 | import android.os.Build 8 | import android.util.TypedValue 9 | import androidx.browser.customtabs.CustomTabsIntent 10 | 11 | object CustomTabUtil { 12 | 13 | fun open(context: Context, uri: Uri) { 14 | try { 15 | context.startActivity(buildIntent(context, uri)) 16 | } catch (error: ActivityNotFoundException) { 17 | context.startActivity(Intent(Intent.ACTION_VIEW, uri)) 18 | } 19 | } 20 | 21 | private fun buildIntent(context: Context, uri: Uri): Intent { 22 | val customTabsIntent = CustomTabsIntent.Builder() 23 | .setShowTitle(true) 24 | .addDefaultShareMenuItem() 25 | 26 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 27 | val typedValue = TypedValue() 28 | context.theme.resolveAttribute(android.R.attr.colorPrimary, typedValue, true) 29 | val color = typedValue.data 30 | customTabsIntent.setToolbarColor(color) 31 | } 32 | 33 | val intent = customTabsIntent.build().intent 34 | intent.data = uri 35 | return intent 36 | } 37 | } -------------------------------------------------------------------------------- /sample/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /sample/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 12 | 13 | 16 | 17 | 20 | 21 | 24 | 25 | 28 | 29 | 32 | 33 | 36 | 37 | 40 | 41 | 42 | 45 | 46 | 49 | 50 | 53 | 54 | 57 | 58 | 61 | 62 | 65 | 66 | 69 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /sample/src/main/java/org/buffer/sociallinkify/sample/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package org.buffer.sociallinkify.sample 2 | 3 | import android.app.Activity 4 | import android.os.Bundle 5 | import android.text.method.LinkMovementMethod 6 | import android.widget.TextView 7 | import kotlinx.android.synthetic.main.activity_main.* 8 | import org.buffer.sociallinkify.SocialLinkify 9 | import org.buffer.sociallinkify.model.PatternType 10 | import org.buffer.sociallinkify.model.PatternType.HASHTAG 11 | import org.buffer.sociallinkify.model.PatternType.MENTION 12 | import org.buffer.sociallinkify.model.PatternType.URL 13 | import org.buffer.sociallinkify.model.SocialNetwork 14 | 15 | class MainActivity : Activity() { 16 | 17 | companion object { 18 | const val server = "androiddev.social" 19 | } 20 | 21 | override fun onCreate(savedInstanceState: Bundle?) { 22 | super.onCreate(savedInstanceState) 23 | setContentView(R.layout.activity_main) 24 | 25 | initTextViews() 26 | } 27 | 28 | private fun initTextViews() { 29 | tvTwitterMention.mentionify(R.string.twitter_mention, SocialNetwork.TWITTER) 30 | tvTwitterHashtag.hashtagify(R.string.twitter_hashtag, SocialNetwork.TWITTER) 31 | tvInstagramMention.mentionify(R.string.instagram_mention, SocialNetwork.INSTAGRAM) 32 | tvInstagramHashtag.hashtagify(R.string.instagram_hashtag, SocialNetwork.INSTAGRAM) 33 | tvFacebookHashtag.hashtagify(R.string.facebook_hashtag, SocialNetwork.FACEBOOK) 34 | tvYouTubeHashtag.hashtagify(R.string.youtube_hashtag, SocialNetwork.YOUTUBE) 35 | tvMastodonHashtag.hashtagify(R.string.mastodon_hashtag, SocialNetwork.MASTODON) 36 | tvMastodonMention.mentionify(R.string.mastodon_mention, SocialNetwork.MASTODON) 37 | tvEmail.linkify(R.string.email_text, SocialNetwork.FACEBOOK, URL) 38 | } 39 | 40 | private fun TextView.hashtagify(stringResId: Int, 41 | socialNetwork: SocialNetwork) { 42 | linkify(stringResId, socialNetwork, HASHTAG) 43 | } 44 | 45 | private fun TextView.mentionify(stringResId: Int, 46 | socialNetwork: SocialNetwork) { 47 | linkify(stringResId, socialNetwork, MENTION) 48 | } 49 | 50 | private fun TextView.linkify(stringResId: Int, 51 | socialNetwork: SocialNetwork, 52 | patternType: PatternType = URL) { 53 | movementMethod = LinkMovementMethod.getInstance() 54 | text = SocialLinkify.socialLinkifyText( 55 | resources.getColor(R.color.colorAccent), 56 | getString(stringResId), 57 | socialNetwork, 58 | patternType, 59 | server = server 60 | ) 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SocialLinkify 2 | An Android library for linking @ mentions & Hashtags to their corresponding social network 3 | 4 | # Motivation 5 | 6 | For most of our projects we handle multiple social network types, which results in us needing to 7 | handle multiple link destinations for @ mentions and Hashtags. For example, if the @Buffer mention 8 | is used then this library will allow you to make that a clickable span pointing to either the Buffer Twitter profile, 9 | Facebook Page or Instagram profile. The same can be done for Hashtags. Rather than duplicate code across 10 | our projects we decided to create a small helper library to separate this from our projects. 11 | 12 | # How to use 13 | 14 | There is only one function to call from SocialLinkify, that is [socialLinkifyText()](https://github.com/bufferapp/SocialLinkify/blob/task/open-source-project/lib/src/main/java/org/buffer/sociallinkify/SocialLinkify.kt#L32). This will return 15 | you an instance of a Spannable which you can then use to set your textview content to. This function 16 | takes four parameters: 17 | 18 | - `color` - An Int representing the color you wish to use for linked elements 19 | 20 | - `text` - The String that you wish to apply linking to 21 | 22 | - `socialNetwork` - The [SocialNetwork](https://github.com/bufferapp/SocialLinkify/blob/task/open-source-project/lib/src/main/java/org/buffer/sociallinkify/model/SocialNetwork.kt) which you want the elements to be linked to. This can be either 23 | `TWITTER`, `FACEBOOK` or `INSTAGRAM`. 24 | 25 | - `patternTypes` - A varargs parameter for the [PatternType](https://github.com/bufferapp/SocialLinkify/blob/task/open-source-project/lib/src/main/java/org/buffer/sociallinkify/model/PatternType.kt) patterns that you want to be recognised, this can be 26 | any collection of `HASHTAG`, `MENTION` and `URL`. 27 | 28 | For example, if I want to create linked spans for Twitter @ mentions, Hashtags & URLs: 29 | 30 | ``` java 31 | text_message.text = SocialLinkify.socialLinkifyText( 32 | ContextCompat.getColor(context, R.color.some_color), message.text, 33 | SocialNetwork.TWITTER, PatternType.HASHTAG, PatternType.MENTION, PatternType.URL) 34 | ``` 35 | 36 | or if I want to create linked spans for Instagram @ mentions only: 37 | 38 | ``` java 39 | text_message.text = SocialLinkify.socialLinkifyText( 40 | ContextCompat.getColor(context, R.color.some_color), message.text, 41 | SocialNetwork.INSTAGRAM, PatternType.MENTION) 42 | ``` 43 | 44 | or if I just want to create linked spans for Facebook hashtags: 45 | 46 | ``` java 47 | text_message.text = SocialLinkify.socialLinkifyText( 48 | ContextCompat.getColor(context, R.color.some_color), message.text, 49 | SocialNetwork.FACEBOOK, PatternType.HASHTAG) 50 | ``` 51 | 52 | The SocialNetwork class also has a handy [fromString()](https://github.com/bufferapp/SocialLinkify/blob/task/open-source-project/lib/src/main/java/org/buffer/sociallinkify/model/SocialNetwork.kt#L7) function to allow you to retrieve a SocialNetwork 53 | instance from some string that may be used in your app such as "twitter". 54 | -------------------------------------------------------------------------------- /lib/src/main/java/org/buffer/sociallinkify/SocialLinkify.kt: -------------------------------------------------------------------------------- 1 | package org.buffer.sociallinkify 2 | 3 | import android.text.SpannableString 4 | import android.text.Spanned 5 | import android.text.SpannedString 6 | import android.text.style.URLSpan 7 | import android.util.Patterns 8 | import org.buffer.sociallinkify.model.PatternType 9 | import org.buffer.sociallinkify.model.SocialNetwork 10 | import org.buffer.sociallinkify.span.CustomTabUrlSpan 11 | import org.buffer.sociallinkify.span.HashtagMentionUrlSpan 12 | import java.util.regex.Pattern 13 | 14 | 15 | object SocialLinkify { 16 | 17 | val URL_BASE_MENTION_TWITTER = "https://twitter.com/" 18 | val URL_BASE_HASHTAG_TWITTER = "https://twitter.com/search?q=" 19 | 20 | val URL_BASE_MENTION_BLUESKY = "https://bsky.app/profile/" 21 | val URL_BASE_HASHTAG_BLUESKY = "https://bsky.app/search?q=" 22 | 23 | val URL_BASE_MENTION_INSTAGRAM = "https://www.instagram.com/" 24 | val URL_BASE_HASHTAG_INSTAGRAM = "https://www.instagram.com/explore/tags/" 25 | 26 | val URL_BASE_MENTION_FACEBOOK = "https://www.facebook.com/" 27 | val URL_BASE_HASHTAG_FACEBOOK = "https://www.facebook.com/hashtag/" 28 | 29 | val URL_YOUTUBE_HASHTAG = "https://www.youtube.com/results?search_query=%23" 30 | private val hashtagPattern = Pattern.compile("#(\\w+)") 31 | private val mentionPattern = Pattern.compile("@([a-zA-Z0-9._]+)") 32 | private val urlPattern = Patterns.WEB_URL 33 | private val emailPattern = Patterns.EMAIL_ADDRESS 34 | 35 | private val patterns = mapOf(PatternType.EMAIL to emailPattern, 36 | PatternType.URL to urlPattern, PatternType.HASHTAG to hashtagPattern, 37 | PatternType.MENTION to mentionPattern) 38 | 39 | fun socialLinkifyText( 40 | color: Int, text: String?, socialNetwork: SocialNetwork, 41 | vararg patternTypes: PatternType, server: String? = null 42 | ): Spanned { 43 | text ?: return SpannedString("") 44 | 45 | val spannable = SpannableString(text) 46 | 47 | patternTypes 48 | .mapNotNull { patterns[it]?.matcher(spannable) } 49 | .forEachIndexed { index, matcher -> 50 | with(matcher) { 51 | while (this.find()) { 52 | val start = this.start() 53 | val end = this.end() 54 | val spanText = text.substring(start, end) 55 | val spans = spannable.getSpans(start, end, URLSpan::class.java) 56 | if (spans.isEmpty()) { 57 | val url = buildSpan(color, spanText, socialNetwork, 58 | patternTypes[index], server) 59 | spannable.setSpan(url, start, end, 0) 60 | } 61 | } 62 | } 63 | } 64 | 65 | return spannable 66 | } 67 | 68 | private fun buildSpan(color: Int, text: String, socialNetwork: SocialNetwork, 69 | patternType: PatternType, server: String? = null) = when (patternType) { 70 | PatternType.URL -> CustomTabUrlSpan(color, text) 71 | PatternType.EMAIL -> HashtagMentionUrlSpan(color, 72 | buildUrl(text, socialNetwork, patternType)) 73 | else -> HashtagMentionUrlSpan(color, buildUrl(text, socialNetwork, patternType, server)) 74 | } 75 | 76 | private fun buildUrl( 77 | entity: String, socialNetwork: SocialNetwork, 78 | patternType: PatternType, server: String? = null 79 | ) = when (patternType) { 80 | PatternType.HASHTAG -> getHashtagUrlBase(socialNetwork, server) + entity.substring(1, entity.length) 81 | PatternType.EMAIL -> "mailto:$entity" 82 | else -> getMentionUrlBase(socialNetwork, server) + entity.substring(1, entity.length) 83 | } 84 | 85 | private fun getMentionUrlBase(socialNetwork: SocialNetwork, server: String? = null) = when (socialNetwork) { 86 | SocialNetwork.FACEBOOK -> URL_BASE_MENTION_FACEBOOK 87 | SocialNetwork.INSTAGRAM -> URL_BASE_MENTION_INSTAGRAM 88 | SocialNetwork.MASTODON -> "https://$server/@" 89 | SocialNetwork.BLUESKY -> URL_BASE_MENTION_BLUESKY 90 | else -> URL_BASE_MENTION_TWITTER 91 | } 92 | 93 | private fun getHashtagUrlBase(socialNetwork: SocialNetwork, server: String? = null) = when (socialNetwork) { 94 | SocialNetwork.FACEBOOK -> URL_BASE_HASHTAG_FACEBOOK 95 | SocialNetwork.INSTAGRAM -> URL_BASE_HASHTAG_INSTAGRAM 96 | SocialNetwork.YOUTUBE -> URL_YOUTUBE_HASHTAG 97 | SocialNetwork.MASTODON -> "https://$server/tags/" 98 | SocialNetwork.BLUESKY -> URL_BASE_HASHTAG_BLUESKY 99 | else -> URL_BASE_HASHTAG_TWITTER 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /sample/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | --------------------------------------------------------------------------------