From 466ac22d23e0e4cf482c253bd8fc771a11d568ac Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Mon, 7 Sep 2020 21:17:06 +0900 Subject: [PATCH] Handle intents (urls/share) from outside the app --- app/src/main/AndroidManifest.xml | 91 +--------- .../java/awais/instagrabber/MainHelper.java | 2 +- .../instagrabber/activities/MainActivity.java | 156 ++++++++++++++---- .../instagrabber/asyncs/PostFetcher.java | 62 +++++-- .../directdownload/DirectDownload.java | 3 +- .../fragments/HashTagFragment.java | 9 +- .../fragments/PostViewFragment.java | 8 +- .../awais/instagrabber/utils/IntentUtils.java | 75 +++++++++ .../java/awais/instagrabber/utils/Utils.java | 53 ------ 9 files changed, 263 insertions(+), 196 deletions(-) create mode 100644 app/src/main/java/awais/instagrabber/utils/IntentUtils.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 8ab7c6db..b0576c00 100755 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -17,7 +17,6 @@ android:supportsRtl="true" android:theme="@style/AppTheme" tools:ignore="UnusedAttribute"> - - - - - + + - @@ -49,18 +45,15 @@ - - - - - - @@ -121,7 +111,6 @@ - @@ -131,98 +120,22 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 0) data = data.substring(data.lastIndexOf('\n') + 1); // -// final IntentModel model = Utils.stripString(data); +// final IntentModel model = Utils.parseUrl(data); // if (model != null) { // final String modelText = model.getText(); // final IntentModelType modelType = model.getType(); diff --git a/app/src/main/java/awais/instagrabber/activities/MainActivity.java b/app/src/main/java/awais/instagrabber/activities/MainActivity.java index 3ad1e974..0e1e1593 100644 --- a/app/src/main/java/awais/instagrabber/activities/MainActivity.java +++ b/app/src/main/java/awais/instagrabber/activities/MainActivity.java @@ -2,8 +2,10 @@ package awais.instagrabber.activities; import android.annotation.SuppressLint; +import android.content.Intent; import android.content.res.TypedArray; import android.database.MatrixCursor; +import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.os.Handler; @@ -41,10 +43,12 @@ import awais.instagrabber.asyncs.SuggestionsFetcher; import awais.instagrabber.customviews.helpers.CustomHideBottomViewOnScrollBehavior; import awais.instagrabber.databinding.ActivityMainBinding; import awais.instagrabber.interfaces.FetchListener; +import awais.instagrabber.models.IntentModel; import awais.instagrabber.models.SuggestionModel; import awais.instagrabber.models.enums.SuggestionType; import awais.instagrabber.utils.Constants; import awais.instagrabber.utils.FlavorTown; +import awais.instagrabber.utils.IntentUtils; import awais.instagrabber.utils.Utils; import static awais.instagrabber.utils.NavigationExtensions.setupWithNavController; @@ -110,6 +114,46 @@ public class MainActivity extends BaseLanguageActivity { setupSuggestions(); FlavorTown.updateCheck(this); FlavorTown.changelogCheck(this); + + final Intent intent = getIntent(); + handleIntent(intent); + } + + @Override + public boolean onCreateOptionsMenu(final Menu menu) { + getMenuInflater().inflate(R.menu.main_menu, menu); + searchMenuItem = menu.findItem(R.id.search); + if (!showSearch) { + searchMenuItem.setVisible(false); + return true; + } + return setupSearchView(); + } + + @Override + protected void onSaveInstanceState(@NonNull final Bundle outState) { + outState.putString(FIRST_FRAGMENT_GRAPH_INDEX_KEY, String.valueOf(firstFragmentGraphIndex)); + super.onSaveInstanceState(outState); + } + + @Override + protected void onRestoreInstanceState(@NonNull final Bundle savedInstanceState) { + super.onRestoreInstanceState(savedInstanceState); + final String key = (String) savedInstanceState.get(FIRST_FRAGMENT_GRAPH_INDEX_KEY); + if (key != null) { + try { + firstFragmentGraphIndex = Integer.parseInt(key); + } catch (NumberFormatException ignored) { } + } + setupBottomNavigationBar(false); + } + + @Override + public boolean onSupportNavigateUp() { + if (currentNavControllerLiveData != null && currentNavControllerLiveData.getValue() != null) { + return currentNavControllerLiveData.getValue().navigateUp(); + } + return false; } private void setupSuggestions() { @@ -144,17 +188,6 @@ public class MainActivity extends BaseLanguageActivity { binding.bottomNavView.requestLayout(); } - @Override - public boolean onCreateOptionsMenu(final Menu menu) { - getMenuInflater().inflate(R.menu.main_menu, menu); - searchMenuItem = menu.findItem(R.id.search); - if (!showSearch) { - searchMenuItem.setVisible(false); - return true; - } - return setupSearchView(); - } - private boolean setupSearchView() { final View actionView = searchMenuItem.getActionView(); if (!(actionView instanceof SearchView)) return false; @@ -371,29 +404,92 @@ public class MainActivity extends BaseLanguageActivity { binding.collapsingToolbarLayout.requestLayout(); } - @Override - protected void onSaveInstanceState(@NonNull final Bundle outState) { - outState.putString(FIRST_FRAGMENT_GRAPH_INDEX_KEY, String.valueOf(firstFragmentGraphIndex)); - super.onSaveInstanceState(outState); + private void handleIntent(final Intent intent) { + if (intent == null) return; + final String action = intent.getAction(); + final String type = intent.getType(); + // Log.d(TAG, action + " " + type); + if (Intent.ACTION_MAIN.equals(action)) return; + if (Intent.ACTION_SEND.equals(action) && type != null) { + if (type.equals("text/plain")) { + handleUrl(intent.getStringExtra(Intent.EXTRA_TEXT)); + } + return; + } + if (Intent.ACTION_VIEW.equals(action)) { + final Uri data = intent.getData(); + if (data == null) return; + handleUrl(data.toString()); + } } - @Override - protected void onRestoreInstanceState(@NonNull final Bundle savedInstanceState) { - super.onRestoreInstanceState(savedInstanceState); - final String key = (String) savedInstanceState.get(FIRST_FRAGMENT_GRAPH_INDEX_KEY); - if (key != null) { - try { - firstFragmentGraphIndex = Integer.parseInt(key); - } catch (NumberFormatException ignored) { } - } - setupBottomNavigationBar(false); + private void handleUrl(final String url) { + if (url == null) return; + // Log.d(TAG, url); + final IntentModel intentModel = IntentUtils.parseUrl(url); + if (intentModel == null) return; + showView(intentModel); } - @Override - public boolean onSupportNavigateUp() { - if (currentNavControllerLiveData != null && currentNavControllerLiveData.getValue() != null) { - return currentNavControllerLiveData.getValue().navigateUp(); + private void showView(final IntentModel intentModel) { + switch (intentModel.getType()) { + case USERNAME: + showProfileView(intentModel); + break; + case POST: + showPostView(intentModel); + break; + case LOCATION: + showLocationView(intentModel); + break; + case HASHTAG: + showHashtagView(intentModel); + break; + case UNKNOWN: + default: + Log.w(TAG, "Unknown model type received!"); } - return false; + } + + private void showProfileView(@NonNull final IntentModel intentModel) { + final String username = intentModel.getText(); + // Log.d(TAG, "username: " + username); + final NavController navController = currentNavControllerLiveData.getValue(); + if (currentNavControllerLiveData == null || navController == null) return; + final Bundle bundle = new Bundle(); + bundle.putString("username", "@" + username); + navController.navigate(R.id.action_global_profileFragment, bundle); + } + + private void showPostView(@NonNull final IntentModel intentModel) { + final String shortCode = intentModel.getText(); + // Log.d(TAG, "shortCode: " + shortCode); + final NavController navController = currentNavControllerLiveData.getValue(); + if (currentNavControllerLiveData == null || navController == null) return; + final Bundle bundle = new Bundle(); + bundle.putStringArray("idOrCodeArray", new String[]{shortCode}); + bundle.putInt("index", 0); + bundle.putBoolean("isId", false); + navController.navigate(R.id.action_global_postViewFragment, bundle); + } + + private void showLocationView(@NonNull final IntentModel intentModel) { + final String locationId = intentModel.getText(); + // Log.d(TAG, "locationId: " + locationId); + final NavController navController = currentNavControllerLiveData.getValue(); + if (currentNavControllerLiveData == null || navController == null) return; + final Bundle bundle = new Bundle(); + bundle.putString("locationId", locationId); + navController.navigate(R.id.action_global_locationFragment, bundle); + } + + private void showHashtagView(@NonNull final IntentModel intentModel) { + final String hashtag = intentModel.getText(); + // Log.d(TAG, "hashtag: " + hashtag); + final NavController navController = currentNavControllerLiveData.getValue(); + if (currentNavControllerLiveData == null || navController == null) return; + final Bundle bundle = new Bundle(); + bundle.putString("hashtag", "#" + hashtag); + navController.navigate(R.id.action_global_hashTagFragment, bundle); } } \ No newline at end of file diff --git a/app/src/main/java/awais/instagrabber/asyncs/PostFetcher.java b/app/src/main/java/awais/instagrabber/asyncs/PostFetcher.java index d032dcf4..c58fc0e8 100755 --- a/app/src/main/java/awais/instagrabber/asyncs/PostFetcher.java +++ b/app/src/main/java/awais/instagrabber/asyncs/PostFetcher.java @@ -11,7 +11,6 @@ import java.io.File; import java.net.HttpURLConnection; import java.net.URL; -import awais.instagrabber.BuildConfig; import awais.instagrabber.interfaces.FetchListener; import awais.instagrabber.models.ProfileModel; import awais.instagrabber.models.ViewerPostModel; @@ -26,6 +25,8 @@ import static awais.instagrabber.utils.Constants.FOLDER_SAVE_TO; import static awais.instagrabber.utils.Utils.logCollector; public final class PostFetcher extends AsyncTask { + private static final String TAG = "PostFetcher"; + private final String shortCode; private final FetchListener fetchListener; @@ -46,17 +47,41 @@ public final class PostFetcher extends AsyncTask if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) { final JSONObject media = new JSONObject(Utils.readFromConnection(conn)).getJSONObject("graphql") - .getJSONObject("shortcode_media"); - - final String username = media.has("owner") ? media.getJSONObject("owner").getString(Constants.EXTRAS_USERNAME) : null; - + .getJSONObject("shortcode_media"); + + ProfileModel profileModel = null; + if (media.has("owner")) { + final JSONObject owner = media.getJSONObject("owner"); + profileModel = new ProfileModel( + owner.optBoolean("is_private"), + owner.optBoolean("is_private"), + owner.optBoolean("is_verified"), + owner.optString("id"), + owner.optString("username"), + owner.optString("full_name"), + null, + null, + owner.optString("profile_pic_url"), + owner.optString("profile_pic_url"), + owner.optInt("edge_owner_to_timeline_media"), + owner.optInt("edge_followed_by"), + -1, + owner.optBoolean("followed_by_viewer"), + owner.optBoolean("restricted_by_viewer"), + owner.optBoolean("blocked_by_viewer"), + owner.optBoolean("requested_by_viewer") + ); + } + final String username = profileModel == null ? "" : profileModel.getUsername(); // to check if file exists final File downloadDir = new File(Environment.getExternalStorageDirectory(), "Download" + - (Utils.settingsHelper.getBoolean(DOWNLOAD_USER_FOLDER) ? ("/"+username) : "")); + (Utils.settingsHelper.getBoolean(DOWNLOAD_USER_FOLDER) ? ("/" + username) : "")); File customDir = null; if (Utils.settingsHelper.getBoolean(FOLDER_SAVE_TO)) { final String customPath = Utils.settingsHelper.getString(FOLDER_PATH + - (Utils.settingsHelper.getBoolean(DOWNLOAD_USER_FOLDER) ? ("/"+username) : "")); + (Utils.settingsHelper.getBoolean(DOWNLOAD_USER_FOLDER) + ? ("/" + username) + : "")); if (!Utils.isEmpty(customPath)) customDir = new File(customPath); } @@ -76,19 +101,20 @@ public final class PostFetcher extends AsyncTask else { final JSONArray captions = mediaToCaption.optJSONArray("edges"); postCaption = captions != null && captions.length() > 0 ? - captions.getJSONObject(0).getJSONObject("node").optString("text") : null; + captions.getJSONObject(0).getJSONObject("node").optString("text") : null; } JSONObject commentObject = media.optJSONObject("edge_media_to_parent_comment"); final long commentsCount = commentObject != null ? commentObject.optLong("count") : 0; String endCursor = null; - if (commentObject != null && (commentObject = commentObject.optJSONObject("page_info")) != null) + if (commentObject != null && (commentObject = commentObject.optJSONObject("page_info")) != null) { endCursor = commentObject.optString("end_cursor"); + } - final ProfileModel profileModel = ProfileModel.getDefaultProfileModel(null, username); if (mediaItemType != MediaItemType.MEDIA_TYPE_SLIDER) { - final ViewerPostModel postModel = new ViewerPostModel(mediaItemType, + final ViewerPostModel postModel = new ViewerPostModel( + mediaItemType, media.getString(Constants.EXTRAS_ID), isVideo ? media.getString("video_url") : Utils.getHighQualityImage(media), shortCode, @@ -99,7 +125,7 @@ public final class PostFetcher extends AsyncTask media.getJSONObject("edge_media_preview_like").getLong("count"), media.isNull("location") ? null : media.getJSONObject("location").optString("name"), media.isNull("location") ? null : - (media.getJSONObject("location").optString("id") + "/" + + (media.getJSONObject("location").optString("id") + "/" + media.getJSONObject("location").optString("slug"))); postModel.setCommentsCount(commentsCount); @@ -116,7 +142,8 @@ public final class PostFetcher extends AsyncTask final JSONObject node = children.getJSONObject(i).getJSONObject("node"); final boolean isChildVideo = node.getBoolean("is_video"); - postModels[i] = new ViewerPostModel(isChildVideo ? MediaItemType.MEDIA_TYPE_VIDEO : MediaItemType.MEDIA_TYPE_IMAGE, + postModels[i] = new ViewerPostModel( + isChildVideo ? MediaItemType.MEDIA_TYPE_VIDEO : MediaItemType.MEDIA_TYPE_IMAGE, media.getString(Constants.EXTRAS_ID), isChildVideo ? node.getString("video_url") : Utils.getHighQualityImage(node), node.getString(Constants.EXTRAS_SHORTCODE), @@ -127,8 +154,8 @@ public final class PostFetcher extends AsyncTask media.getJSONObject("edge_media_preview_like").getLong("count"), media.isNull("location") ? null : media.getJSONObject("location").optString("name"), media.isNull("location") ? null : - (media.getJSONObject("location").optString("id") + "/" + - media.getJSONObject("location").optString("slug"))); + (media.getJSONObject("location").optString("id") + "/" + + media.getJSONObject("location").optString("slug"))); postModels[i].setSliderDisplayUrl(node.getString("display_url")); Utils.checkExistence(downloadDir, customDir, true, postModels[i]); @@ -142,9 +169,10 @@ public final class PostFetcher extends AsyncTask conn.disconnect(); } catch (Exception e) { - if (logCollector != null) + if (logCollector != null) { logCollector.appendException(e, LogCollector.LogFile.ASYNC_POST_FETCHER, "doInBackground"); - if (BuildConfig.DEBUG) Log.e("AWAISKING_APP", "", e); + } + Log.e(TAG, "Error fetching post", e); } return result; } diff --git a/app/src/main/java/awais/instagrabber/directdownload/DirectDownload.java b/app/src/main/java/awais/instagrabber/directdownload/DirectDownload.java index 4cf51f65..31794555 100755 --- a/app/src/main/java/awais/instagrabber/directdownload/DirectDownload.java +++ b/app/src/main/java/awais/instagrabber/directdownload/DirectDownload.java @@ -33,6 +33,7 @@ import awais.instagrabber.models.ViewerPostModel; import awais.instagrabber.models.enums.DownloadMethod; import awais.instagrabber.models.enums.IntentModelType; import awais.instagrabber.utils.Constants; +import awais.instagrabber.utils.IntentUtils; import awais.instagrabber.utils.Utils; import static awais.instagrabber.utils.Utils.CHANNEL_ID; @@ -109,7 +110,7 @@ public final class DirectDownload extends Activity { } if (data != null && !Utils.isEmpty(data)) { - final IntentModel model = Utils.stripString(data); + final IntentModel model = IntentUtils.parseUrl(data); if (model != null && model.getType() == IntentModelType.POST) { final String text = model.getText(); diff --git a/app/src/main/java/awais/instagrabber/fragments/HashTagFragment.java b/app/src/main/java/awais/instagrabber/fragments/HashTagFragment.java index 132c3570..bfe157d2 100644 --- a/app/src/main/java/awais/instagrabber/fragments/HashTagFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/HashTagFragment.java @@ -4,6 +4,7 @@ import android.content.res.ColorStateList; import android.graphics.Typeface; import android.os.AsyncTask; import android.os.Bundle; +import android.os.Handler; import android.text.SpannableStringBuilder; import android.text.style.RelativeSizeSpan; import android.text.style.StyleSpan; @@ -44,7 +45,6 @@ import awais.instagrabber.customviews.helpers.GridSpacingItemDecoration; import awais.instagrabber.customviews.helpers.NestedCoordinatorLayout; import awais.instagrabber.customviews.helpers.RecyclerLazyLoader; import awais.instagrabber.databinding.FragmentHashtagBinding; -import awais.instagrabber.viewmodels.PostsViewModel; import awais.instagrabber.interfaces.FetchListener; import awais.instagrabber.models.HashtagModel; import awais.instagrabber.models.PostModel; @@ -53,6 +53,7 @@ import awais.instagrabber.models.enums.DownloadMethod; import awais.instagrabber.models.enums.PostItemType; import awais.instagrabber.utils.Constants; import awais.instagrabber.utils.Utils; +import awais.instagrabber.viewmodels.PostsViewModel; import awaisomereport.LogCollector; import static awais.instagrabber.utils.Utils.logCollector; @@ -297,7 +298,11 @@ public class HashTagFragment extends Fragment { private void setTitle() { final ActionBar actionBar = fragmentActivity.getSupportActionBar(); if (actionBar != null) { - actionBar.setTitle(hashtag); + Log.d(TAG, "setting title: " + hashtag); + final Handler handler = new Handler(); + handler.postDelayed(() -> { + actionBar.setTitle(hashtag); + }, 200); } } diff --git a/app/src/main/java/awais/instagrabber/fragments/PostViewFragment.java b/app/src/main/java/awais/instagrabber/fragments/PostViewFragment.java index 98ae40a2..73abbd80 100644 --- a/app/src/main/java/awais/instagrabber/fragments/PostViewFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/PostViewFragment.java @@ -30,7 +30,6 @@ import awais.instagrabber.adapters.PostViewAdapter.OnPostViewChildViewClickListe import awais.instagrabber.asyncs.PostFetcher; import awais.instagrabber.asyncs.i.iPostFetcher; import awais.instagrabber.databinding.FragmentPostViewBinding; -import awais.instagrabber.viewmodels.ViewerPostViewModel; import awais.instagrabber.interfaces.FetchListener; import awais.instagrabber.interfaces.MentionClickListener; import awais.instagrabber.models.ViewerPostModel; @@ -40,6 +39,7 @@ import awais.instagrabber.services.MediaService; import awais.instagrabber.services.ServiceCallback; import awais.instagrabber.utils.Constants; import awais.instagrabber.utils.Utils; +import awais.instagrabber.viewmodels.ViewerPostViewModel; import static androidx.core.content.ContextCompat.checkSelfPermission; import static awais.instagrabber.utils.Utils.settingsHelper; @@ -71,8 +71,10 @@ public class PostViewFragment extends Fragment { if (firstPost == null) return; String idOrCode = isId ? firstPost.getPostId() : firstPost.getShortCode(); if (idOrCode == null) return; - // some values are appended to the post/short code with `_` - idOrCode = idOrCode.substring(0, idOrCode.indexOf('_')); + if (isId) { + // the post id is appended with `_` in the result + idOrCode = idOrCode.substring(0, idOrCode.indexOf('_')); + } final int index = idOrCodeList.indexOf(idOrCode); if (index < 0) return; final ViewerPostModelWrapper viewerPostModelWrapper = temp.get(index); diff --git a/app/src/main/java/awais/instagrabber/utils/IntentUtils.java b/app/src/main/java/awais/instagrabber/utils/IntentUtils.java new file mode 100644 index 00000000..80271c64 --- /dev/null +++ b/app/src/main/java/awais/instagrabber/utils/IntentUtils.java @@ -0,0 +1,75 @@ +package awais.instagrabber.utils; + +import android.text.TextUtils; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + +import awais.instagrabber.models.IntentModel; +import awais.instagrabber.models.enums.IntentModelType; + +public final class IntentUtils { + + @Nullable + public static IntentModel parseUrl(@NonNull String url) { + final int wwwDel = url.contains("www.") ? 4 : 0; + final boolean isHttps = url.startsWith("https"); + + IntentModelType type = IntentModelType.UNKNOWN; + if (url.contains("instagram.com/")) { + url = url.substring((isHttps ? 22 : 21) + wwwDel); + + // final char firstChar = url.charAt(0); + if (url.startsWith("p/") || url.startsWith("reel/") || url.startsWith("tv/")) { + url = url.substring(url.startsWith("p/") ? 2 : (url.startsWith("tv/") ? 3 : 5)); + type = IntentModelType.POST; + } else if (url.startsWith("explore/tags/")) { + url = url.substring(13); + type = IntentModelType.HASHTAG; + } else if (url.startsWith("explore/locations/")) { + url = url.substring(18); + type = IntentModelType.LOCATION; + } else if (url.startsWith("_u/")) { // usually exists in embeds + url = url.substring(3); + type = IntentModelType.USERNAME; + } + url = cleanString(url); + if (TextUtils.isEmpty(url)) return null; + else if (type == IntentModelType.UNKNOWN){ + type = IntentModelType.USERNAME; + } + } else if (url.contains("ig.me/u/")) { + url = url.substring((isHttps ? 16 : 15) + wwwDel); + url = cleanString(url); + type = IntentModelType.USERNAME; + + } else return null; + + final int clipLen = url.length() - 1; + if (url.charAt(clipLen) == '/') + url = url.substring(0, clipLen); + + if (type == IntentModelType.LOCATION && url.contains("/")) { + url = url.substring(0, url.indexOf("/")); + } + + if (!url.contains("/")) return new IntentModel(type, url); + return null; + } + + @NonNull + public static String cleanString(@NonNull final String clipString) { + final int queryIndex = clipString.indexOf('?'); + final int paramIndex = clipString.indexOf('#'); + int startIndex = -1; + if (queryIndex > 0 && paramIndex > 0) { + if (queryIndex < paramIndex) startIndex = queryIndex; + else if (paramIndex < queryIndex) startIndex = paramIndex; + } else if (queryIndex == -1 && paramIndex >= 0) { + startIndex = paramIndex; + } else if (paramIndex == -1 && queryIndex >= 0) { + startIndex = queryIndex; + } + return startIndex != -1 ? clipString.substring(0, startIndex) : clipString; + } +} diff --git a/app/src/main/java/awais/instagrabber/utils/Utils.java b/app/src/main/java/awais/instagrabber/utils/Utils.java index 1f4f3185..047f97da 100755 --- a/app/src/main/java/awais/instagrabber/utils/Utils.java +++ b/app/src/main/java/awais/instagrabber/utils/Utils.java @@ -170,59 +170,6 @@ public final class Utils { return null; } - @Nullable - public static IntentModel stripString(@NonNull String clipString) { - final int wwwDel = clipString.contains("www.") ? 4 : 0; - final boolean isHttps = clipString.startsWith("https"); - - IntentModelType type = IntentModelType.UNKNOWN; - if (clipString.contains("instagram.com/")) { - clipString = clipString.substring((isHttps ? 22 : 21) + wwwDel); - - final char firstChar = clipString.charAt(0); - if (clipString.startsWith("p/") || clipString.startsWith("reel/") || clipString.startsWith("tv/")) { - clipString = clipString.substring(clipString.startsWith("p/") ? 2 : (clipString.startsWith("tv/") ? 3 : 5)); - type = IntentModelType.POST; - } else if (clipString.startsWith("explore/tags/")) { - clipString = clipString.substring(13); - type = IntentModelType.HASHTAG; - } else if (clipString.startsWith("explore/locations/")) { - clipString = clipString.substring(18); - type = IntentModelType.LOCATION; - } else if (clipString.startsWith("_u/")) { // usually exists in embeds - clipString = clipString.substring(3); - type = IntentModelType.USERNAME; - } - - clipString = cleanString(clipString); - } else if (clipString.contains("ig.me/u/")) { - clipString = clipString.substring((isHttps ? 16 : 15) + wwwDel); - clipString = cleanString(clipString); - type = IntentModelType.USERNAME; - - } else return null; - - final int clipLen = clipString.length() - 1; - if (clipString.charAt(clipLen) == '/') - clipString = clipString.substring(0, clipLen); - - if (!clipString.contains("/")) return new IntentModel(type, clipString); - else return null; - } - - @NonNull - public static String cleanString(@NonNull final String clipString) { - final int queryIndex = clipString.indexOf('?'); - final int paramIndex = clipString.indexOf('#'); - int startIndex = -1; - if (queryIndex > 0 && paramIndex > 0) { - if (queryIndex < paramIndex) startIndex = queryIndex; - else if (paramIndex < queryIndex) startIndex = paramIndex; - } else if (queryIndex == -1 && paramIndex > 0) startIndex = paramIndex; - else if (paramIndex == -1 && queryIndex > 0) startIndex = queryIndex; - return startIndex != -1 ? clipString.substring(0, startIndex) : clipString; - } - @NonNull public static CharSequence getMentionText(@NonNull final CharSequence text) { final int commentLength = text.length();