From 3629c4cba0627752ca06a99c79ea0efa290c4177 Mon Sep 17 00:00:00 2001 From: Mariotaku Lee Date: Sun, 17 Apr 2016 11:54:43 +0800 Subject: [PATCH] fixed #509 --- .../activity/WebLinkHandlerActivity.java | 1 + .../twidere/fragment/StatusFragment.java | 10 +- .../twidere/fragment/UserFragment.java | 13 +-- .../util/DirectMessageOnLinkClickHandler.java | 4 +- .../twidere/util/OnLinkClickHandler.java | 92 ++++++++++++------- .../twidere/util/TwidereLinkify.java | 2 +- 6 files changed, 77 insertions(+), 45 deletions(-) diff --git a/twidere/src/main/java/org/mariotaku/twidere/activity/WebLinkHandlerActivity.java b/twidere/src/main/java/org/mariotaku/twidere/activity/WebLinkHandlerActivity.java index 5e2930fad..3d2790a2f 100644 --- a/twidere/src/main/java/org/mariotaku/twidere/activity/WebLinkHandlerActivity.java +++ b/twidere/src/main/java/org/mariotaku/twidere/activity/WebLinkHandlerActivity.java @@ -85,6 +85,7 @@ public class WebLinkHandlerActivity extends Activity implements Constants { } } if (handled.first != null) { + handled.first.putExtras(intent); startActivity(handled.first); } else { if (!handled.second) { diff --git a/twidere/src/main/java/org/mariotaku/twidere/fragment/StatusFragment.java b/twidere/src/main/java/org/mariotaku/twidere/fragment/StatusFragment.java index f9d7ec89f..a54dcecbc 100644 --- a/twidere/src/main/java/org/mariotaku/twidere/fragment/StatusFragment.java +++ b/twidere/src/main/java/org/mariotaku/twidere/fragment/StatusFragment.java @@ -1600,21 +1600,21 @@ public class StatusFragment extends BaseSupportFragment implements LoaderCallbac } @Override - public void onLinkClick(final String link, final String orig, final UserKey accountKey, - long extraId, int type, boolean sensitive, int start, int end) { + public boolean onLinkClick(final String link, final String orig, final UserKey accountKey, + long extraId, int type, boolean sensitive, int start, int end) { final ParcelableStatus status = adapter.getStatus(); ParcelableMedia current; if ((current = ParcelableMediaUtils.findByUrl(status.media, link)) != null && !current.open_browser) { expandOrOpenMedia(current); - return; + return true; } if ((current = ParcelableMediaUtils.findByUrl(status.quoted_media, link)) != null && !current.open_browser) { expandOrOpenMedia(current); - return; + return true; } - super.onLinkClick(link, orig, accountKey, extraId, type, sensitive, start, end); + return super.onLinkClick(link, orig, accountKey, extraId, type, sensitive, start, end); } private void expandOrOpenMedia(ParcelableMedia current) { diff --git a/twidere/src/main/java/org/mariotaku/twidere/fragment/UserFragment.java b/twidere/src/main/java/org/mariotaku/twidere/fragment/UserFragment.java index 71723cc2c..71d06f7e0 100644 --- a/twidere/src/main/java/org/mariotaku/twidere/fragment/UserFragment.java +++ b/twidere/src/main/java/org/mariotaku/twidere/fragment/UserFragment.java @@ -1354,20 +1354,20 @@ public class UserFragment extends BaseSupportFragment implements OnClickListener } @Override - public void onLinkClick(final String link, final String orig, final UserKey accountKey, + public boolean onLinkClick(final String link, final String orig, final UserKey accountKey, final long extraId, final int type, final boolean sensitive, int start, int end) { final ParcelableUser user = getUser(); - if (user == null) return; + if (user == null) return false; switch (type) { case TwidereLinkify.LINK_TYPE_MENTION: { IntentUtils.openUserProfile(getActivity(), user.account_key, null, link, null, mPreferences.getBoolean(KEY_NEW_DOCUMENT_API), Referral.USER_MENTION); - break; + return true; } case TwidereLinkify.LINK_TYPE_HASHTAG: { IntentUtils.openTweetSearch(getActivity(), user.account_key, "#" + link); - break; + return true; } case TwidereLinkify.LINK_TYPE_LINK_IN_TEXT: case TwidereLinkify.LINK_TYPE_ENTITY_URL: { @@ -1379,7 +1379,7 @@ public class UserFragment extends BaseSupportFragment implements OnClickListener intent = new Intent(Intent.ACTION_VIEW, uri.buildUpon().scheme("http").build()); } startActivity(intent); - break; + return true; } case TwidereLinkify.LINK_TYPE_LIST: { if (link == null) break; @@ -1387,9 +1387,10 @@ public class UserFragment extends BaseSupportFragment implements OnClickListener if (mentionList.length != 2) { break; } - break; + return true; } } + return false; } @Override diff --git a/twidere/src/main/java/org/mariotaku/twidere/util/DirectMessageOnLinkClickHandler.java b/twidere/src/main/java/org/mariotaku/twidere/util/DirectMessageOnLinkClickHandler.java index 5f23f16ef..419d05c87 100644 --- a/twidere/src/main/java/org/mariotaku/twidere/util/DirectMessageOnLinkClickHandler.java +++ b/twidere/src/main/java/org/mariotaku/twidere/util/DirectMessageOnLinkClickHandler.java @@ -66,8 +66,8 @@ public class DirectMessageOnLinkClickHandler extends OnLinkClickHandler { } private boolean hasShortenedLinks(final String link) { - for (final String short_link_service : SHORT_LINK_SERVICES) { - if (link.contains(short_link_service)) return true; + for (final String shortLinkService : SHORT_LINK_SERVICES) { + if (link.contains(shortLinkService)) return true; } return false; } diff --git a/twidere/src/main/java/org/mariotaku/twidere/util/OnLinkClickHandler.java b/twidere/src/main/java/org/mariotaku/twidere/util/OnLinkClickHandler.java index 8df8144a6..73c2b07aa 100644 --- a/twidere/src/main/java/org/mariotaku/twidere/util/OnLinkClickHandler.java +++ b/twidere/src/main/java/org/mariotaku/twidere/util/OnLinkClickHandler.java @@ -28,6 +28,7 @@ import android.support.annotation.Nullable; import org.apache.commons.lang3.StringUtils; import org.mariotaku.twidere.Constants; +import org.mariotaku.twidere.activity.WebLinkHandlerActivity; import org.mariotaku.twidere.fragment.UserFragment; import org.mariotaku.twidere.model.ParcelableMedia; import org.mariotaku.twidere.model.UserKey; @@ -55,10 +56,10 @@ public class OnLinkClickHandler implements OnLinkClickListener, Constants { } @Override - public void onLinkClick(final String link, final String orig, final UserKey accountKey, - final long extraId, final int type, final boolean sensitive, - final int start, final int end) { - if (manager != null && manager.isActive()) return; + public boolean onLinkClick(final String link, final String orig, final UserKey accountKey, + final long extraId, final int type, final boolean sensitive, + final int start, final int end) { + if (manager != null && manager.isActive()) return false; if (!isPrivateData()) { // BEGIN HotMobi final LinkEvent event = LinkEvent.create(context, link, type); @@ -71,11 +72,11 @@ public class OnLinkClickHandler implements OnLinkClickListener, Constants { IntentUtils.openUserProfile(context, accountKey, null, link, null, preferences.getBoolean(KEY_NEW_DOCUMENT_API), UserFragment.Referral.USER_MENTION); - break; + return true; } case TwidereLinkify.LINK_TYPE_HASHTAG: { IntentUtils.openTweetSearch(context, accountKey, "#" + link); - break; + return true; } case TwidereLinkify.LINK_TYPE_LINK_IN_TEXT: { if (isMedia(link, extraId)) { @@ -83,60 +84,75 @@ public class OnLinkClickHandler implements OnLinkClickListener, Constants { } else { openLink(link); } - break; + return true; } case TwidereLinkify.LINK_TYPE_ENTITY_URL: { if (isMedia(link, extraId)) { openMedia(accountKey, extraId, sensitive, link, start, end); } else { - if (orig != null && "fanfou.com".equals(UriUtils.getAuthority(link))) { - // Process special case for fanfou - final char ch = orig.charAt(0); - // Extend selection - final int length = orig.length(); - if (TwidereLinkify.isAtSymbol(ch)) { - String id = UriUtils.getPath(link); - if (id != null) { - int idxOfSlash = id.indexOf('/'); - if (idxOfSlash == 0) { - id = id.substring(1); + final String authority = UriUtils.getAuthority(link); + if (authority == null) { + openLink(link); + return true; + } + switch (authority) { + case "fanfou.com": { + if (orig != null) { + // Process special case for fanfou + final char ch = orig.charAt(0); + // Extend selection + final int length = orig.length(); + if (TwidereLinkify.isAtSymbol(ch)) { + String id = UriUtils.getPath(link); + if (id != null) { + int idxOfSlash = id.indexOf('/'); + if (idxOfSlash == 0) { + id = id.substring(1); + } + final String screenName = orig.substring(1, length); + IntentUtils.openUserProfile(context, accountKey, UserKey.valueOf(id), + screenName, null, preferences.getBoolean(KEY_NEW_DOCUMENT_API), + UserFragment.Referral.USER_MENTION); + return true; + } + } else if (TwidereLinkify.isHashSymbol(ch) && + TwidereLinkify.isHashSymbol(orig.charAt(length - 1))) { + IntentUtils.openSearch(context, accountKey, orig.substring(1, length - 1)); + return true; } - final String screenName = orig.substring(1, length); - IntentUtils.openUserProfile(context, accountKey, UserKey.valueOf(id), - screenName, null, preferences.getBoolean(KEY_NEW_DOCUMENT_API), - UserFragment.Referral.USER_MENTION); - break; } - } else if (TwidereLinkify.isHashSymbol(ch) && - TwidereLinkify.isHashSymbol(orig.charAt(length - 1))) { - IntentUtils.openSearch(context, accountKey, orig.substring(1, length - 1)); break; } + case "twitter.com": { + openTwitterLink(link, accountKey); + return true; + } } openLink(link); } - break; + return true; } case TwidereLinkify.LINK_TYPE_LIST: { final String[] mentionList = StringUtils.split(link, "/"); if (mentionList.length != 2) { - break; + return false; } IntentUtils.openUserListDetails(context, accountKey, null, null, mentionList[0], mentionList[1]); - break; + return true; } case TwidereLinkify.LINK_TYPE_CASHTAG: { IntentUtils.openTweetSearch(context, accountKey, link); - break; + return true; } case TwidereLinkify.LINK_TYPE_USER_ID: { IntentUtils.openUserProfile(context, accountKey, UserKey.valueOf(link), null, null, preferences.getBoolean(KEY_NEW_DOCUMENT_API), UserFragment.Referral.USER_MENTION); - break; + return true; } } + return false; } protected boolean isPrivateData() { @@ -157,6 +173,20 @@ public class OnLinkClickHandler implements OnLinkClickListener, Constants { if (manager != null && manager.isActive()) return; final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(link)); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + intent.setPackage(IntentUtils.getDefaultBrowserPackage(context)); + try { + context.startActivity(intent); + } catch (final ActivityNotFoundException e) { + // TODO + } + } + + protected void openTwitterLink(final String link, final UserKey accountKey) { + if (manager != null && manager.isActive()) return; + final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(link)); + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + intent.setClass(context, WebLinkHandlerActivity.class); + intent.putExtra(EXTRA_ACCOUNT_KEY, accountKey); try { context.startActivity(intent); } catch (final ActivityNotFoundException e) { diff --git a/twidere/src/main/java/org/mariotaku/twidere/util/TwidereLinkify.java b/twidere/src/main/java/org/mariotaku/twidere/util/TwidereLinkify.java index f56afae4c..f4dc86816 100644 --- a/twidere/src/main/java/org/mariotaku/twidere/util/TwidereLinkify.java +++ b/twidere/src/main/java/org/mariotaku/twidere/util/TwidereLinkify.java @@ -323,7 +323,7 @@ public final class TwidereLinkify implements Constants { } public interface OnLinkClickListener { - void onLinkClick(String link, String orig, UserKey accountKey, long extraId, int type, + boolean onLinkClick(String link, String orig, UserKey accountKey, long extraId, int type, boolean sensitive, int start, int end); } }