From ac69434ee2b2bc6d1e6bd548d1abdab0b8edd194 Mon Sep 17 00:00:00 2001 From: stom79 Date: Sat, 1 Sep 2018 15:59:16 +0200 Subject: [PATCH] Clickable status link --- .../mastodon/activities/BaseMainActivity.java | 32 +++++++++++-------- .../mastodon/client/Entities/Status.java | 17 ++++++++-- .../gouv/etalab/mastodon/helper/Helper.java | 2 ++ app/src/main/res/values/strings.xml | 2 +- 4 files changed, 35 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/activities/BaseMainActivity.java b/app/src/main/java/fr/gouv/etalab/mastodon/activities/BaseMainActivity.java index 8b0ff9517..12ec04f9e 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/activities/BaseMainActivity.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/activities/BaseMainActivity.java @@ -125,7 +125,9 @@ import static fr.gouv.etalab.mastodon.helper.Helper.INTENT_TARGETED_ACCOUNT; import static fr.gouv.etalab.mastodon.helper.Helper.NOTIFICATION_INTENT; import static fr.gouv.etalab.mastodon.helper.Helper.PREF_KEY_ID; import static fr.gouv.etalab.mastodon.helper.Helper.SEARCH_KEYWORD; +import static fr.gouv.etalab.mastodon.helper.Helper.SEARCH_REMOTE; import static fr.gouv.etalab.mastodon.helper.Helper.SEARCH_TAG; +import static fr.gouv.etalab.mastodon.helper.Helper.SEARCH_URL; import static fr.gouv.etalab.mastodon.helper.Helper.THEME_BLACK; import static fr.gouv.etalab.mastodon.helper.Helper.changeDrawableColor; import static fr.gouv.etalab.mastodon.helper.Helper.changeUser; @@ -1134,7 +1136,6 @@ public abstract class BaseMainActivity extends BaseActivity String type = intent.getType(); Bundle extras = intent.getExtras(); String userIdIntent; - if( extras != null && extras.containsKey(INTENT_ACTION) ){ final NavigationView navigationView = findViewById(R.id.nav_view); userIdIntent = extras.getString(PREF_KEY_ID); //Id of the account in the intent @@ -1171,20 +1172,23 @@ public abstract class BaseMainActivity extends BaseActivity }else if( extras.getInt(INTENT_ACTION) == BACKUP_INTENT){ Intent myIntent = new Intent(BaseMainActivity.this, OwnerStatusActivity.class); startActivity(myIntent); - }else if(extras.getInt(INTENT_ACTION) == SEARCH_TAG){ - String keyword = extras.getString(SEARCH_KEYWORD); - if( keyword != null){ - adapter = new PagerAdapter - (getSupportFragmentManager(), tabLayout.getTabCount()); - viewPager.setAdapter(adapter); - for(int i = 0; i < tabLayout.getTabCount() ; i++ ){ - if( tabLayout.getTabAt(i).getText() != null && tabLayout.getTabAt(i).getText().equals(keyword.trim())){ - tabLayout.getTabAt(i).select(); - break; - } - - } + }else if (extras.getInt(INTENT_ACTION) == SEARCH_REMOTE) { + String url = extras.getString(SEARCH_URL); + if( url == null) + return; + Matcher matcher; + if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) + matcher = Patterns.WEB_URL.matcher(url); + else + matcher = Helper.urlPattern.matcher(url); + boolean isUrl = false; + while (matcher.find()){ + isUrl = true; } + if(!isUrl) + return; + //Here we know that the intent contains a valid URL + new RetrieveRemoteDataAsyncTask(BaseMainActivity.this, url, BaseMainActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } }else if( Intent.ACTION_SEND.equals(action) && type != null ) { if ("text/plain".equals(type)) { diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/Status.java b/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/Status.java index d37ace36e..15115a165 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/Status.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/Status.java @@ -55,10 +55,15 @@ import java.util.regex.Pattern; import fr.gouv.etalab.mastodon.R; import fr.gouv.etalab.mastodon.activities.HashTagActivity; +import fr.gouv.etalab.mastodon.activities.MainActivity; import fr.gouv.etalab.mastodon.activities.ShowAccountActivity; import fr.gouv.etalab.mastodon.helper.Helper; import fr.gouv.etalab.mastodon.interfaces.OnRetrieveEmojiInterface; +import static fr.gouv.etalab.mastodon.helper.Helper.INTENT_ACTION; +import static fr.gouv.etalab.mastodon.helper.Helper.SEARCH_REMOTE; +import static fr.gouv.etalab.mastodon.helper.Helper.SEARCH_URL; + /** * Created by Thomas on 23/04/2017. * Manage Status (ie: toots) @@ -695,7 +700,6 @@ public class Status implements Parcelable{ int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK); Matcher matcher; - Pattern aLink = Pattern.compile("(<\\s?a\\s?href=\"(https?:\\/\\/[\\da-z\\.-]+\\.[a-z\\.]{2,6}[\\/]?[^\"@(\\/tags\\/)]*)\"\\s?[^.]*<\\s?\\/\\s?a\\s?>)"); Matcher matcherALink = aLink.matcher(spannableString.toString()); while (matcherALink.find()){ @@ -778,9 +782,16 @@ public class Status implements Parcelable{ @Override public void onClick(View textView) { String finalUrl = url; - if( !url.startsWith("http://") && ! url.startsWith("https://")) + Pattern link = Pattern.compile("https?:\\/\\/([\\da-z\\.-]+\\.[a-z\\.]{2,6})\\/(@[\\/\\w._-]*[0-9]*)"); + if( url.contains("@")){ + Intent intent = new Intent(context, MainActivity.class); + intent.putExtra(INTENT_ACTION, SEARCH_REMOTE); + intent.putExtra(SEARCH_URL, url); + context.startActivity(intent); + }else if( !url.startsWith("http://") && ! url.startsWith("https://")) { finalUrl = "http://" + url; - Helper.openBrowser(context, finalUrl); + Helper.openBrowser(context, finalUrl); + } } @Override public void updateDrawState(TextPaint ds) { diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java b/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java index 08cdffc21..05cd81715 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java @@ -214,6 +214,7 @@ public class Helper { public static final String SHOULD_CONTINUE_STREAMING_FEDERATED = "should_continue_streaming_federated"; public static final String SHOULD_CONTINUE_STREAMING_LOCAL = "should_continue_streaming_local"; public static final String SEARCH_KEYWORD = "search_keyword"; + public static final String SEARCH_URL = "search_url"; public static final String CLIP_BOARD = "clipboard"; public static final String INSTANCE_NAME = "instance_name"; //Notifications @@ -225,6 +226,7 @@ public class Helper { public static final int BACKUP_INTENT = 6; public static final int SEARCH_TAG = 7; public static final int SEARCH_INSTANCE = 8; + public static final int SEARCH_REMOTE = 9; //Settings public static final String SET_TOOTS_PER_PAGE = "set_toots_per_page"; diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index aecf9caa9..970701b50 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -555,7 +555,7 @@ You already follow this instance! The instance is followed! masto.host - Masto.host is our Mastodon hosting partner.\nWith this partnership they provide me free hosting for Mastalab\'s instance and in exchange I promote them in here.\nTo be clear, there is no money involved or tracking of users sent. I needed an instance for testing, we talked and agreed on this.\n\nGo check them out if you want to run your own Mastodon instance. + Masto.host is our Mastodon hosting partner.\n\nWith this partnership they provide me free hosting for Mastalab\'s instance and in exchange I promote them in here.\n\nTo be clear, there is no money involved or tracking of users. I needed an instance for testing, we talked and agreed on this.\n\nGo check them out if you want to run your own Mastodon instance. Partnerships Information