Browse Source

Handle intents (urls/share) from outside the app

renovate/org.robolectric-robolectric-4.x
Ammar Githam 4 years ago
parent
commit
466ac22d23
  1. 91
      app/src/main/AndroidManifest.xml
  2. 2
      app/src/main/java/awais/instagrabber/MainHelper.java
  3. 156
      app/src/main/java/awais/instagrabber/activities/MainActivity.java
  4. 62
      app/src/main/java/awais/instagrabber/asyncs/PostFetcher.java
  5. 3
      app/src/main/java/awais/instagrabber/directdownload/DirectDownload.java
  6. 9
      app/src/main/java/awais/instagrabber/fragments/HashTagFragment.java
  7. 8
      app/src/main/java/awais/instagrabber/fragments/PostViewFragment.java
  8. 75
      app/src/main/java/awais/instagrabber/utils/IntentUtils.java
  9. 53
      app/src/main/java/awais/instagrabber/utils/Utils.java

91
app/src/main/AndroidManifest.xml

@ -17,7 +17,6 @@
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="UnusedAttribute">
<activity
android:name=".activities.MainActivity"
android:launchMode="singleTop"
@ -29,18 +28,15 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<action android:name="android.intent.action.SEARCH" />
<action android:name="android.intent.action.WEB_SEARCH" />
<!--<action android:name="android.intent.action.SEARCH" />-->
<!--<action android:name="android.intent.action.WEB_SEARCH" />-->
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:mimeType="text/plain" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
@ -49,18 +45,15 @@
<data android:scheme="http" />
<data android:scheme="https" />
<data android:host="ig.me" />
<data android:host="www.ig.me" />
<data android:host="instagram.com" />
<data android:host="www.instagram.com" />
<data android:pathPrefix="/" />
<data android:pathPrefix="/p" />
<data android:pathPrefix="/explore/tags" />
</intent-filter>
</activity>
<activity
android:name="awaisomereport.ErrorReporterActivity"
android:allowEmbedded="false"
@ -77,7 +70,6 @@
android:screenOrientation="portrait"
android:taskAffinity="awais.instagrabber.errorreport"
android:theme="@android:style/Theme.DeviceDefault.Dialog" />
<activity
android:name=".directdownload.MultiDirectDialog"
android:allowEmbedded="false"
@ -93,7 +85,6 @@
android:noHistory="false"
android:taskAffinity="awais.instagrabber.multidialog"
android:theme="@style/FlyingGayDialog" />
<activity
android:name=".directdownload.DirectDownload"
android:allowEmbedded="false"
@ -110,7 +101,6 @@
android:lockTaskMode="never"
android:noHistory="false"
android:theme="@style/CompletelyTransparent">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<action android:name="android.intent.action.SEARCH" />
@ -121,7 +111,6 @@
<data android:mimeType="text/plain" />
</intent-filter>
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
@ -131,98 +120,22 @@
<data android:scheme="https" />
</intent-filter>
</activity>
<!--<activity-->
<!-- android:name=".activities.PostViewer"-->
<!-- android:parentActivityName=".activities.MainActivity">-->
<!-- <meta-data-->
<!-- android:name="android.support.PARENT_ACTIVITY"-->
<!-- android:value=".activities.MainActivity" />-->
<!--</activity>-->
<!--<activity-->
<!-- android:name=".activities.CommentsViewerFragment"-->
<!-- android:parentActivityName=".activities.PostViewer">-->
<!-- <meta-data-->
<!-- android:name="android.support.PARENT_ACTIVITY"-->
<!-- android:value=".activities.PostViewer" />-->
<!--</activity>-->
<!--<activity-->
<!-- android:name=".activities.StoryViewer"-->
<!-- android:parentActivityName=".activities.MainActivity">-->
<!-- <meta-data-->
<!-- android:name="android.support.PARENT_ACTIVITY"-->
<!-- android:value=".activities.MainActivity" />-->
<!--</activity>-->
<!--<activity-->
<!-- android:name=".activities.FollowViewerFragment"-->
<!-- android:parentActivityName=".activities.MainActivity">-->
<!-- <meta-data-->
<!-- android:name="android.support.PARENT_ACTIVITY"-->
<!-- android:value=".activities.MainActivity" />-->
<!--</activity>-->
<!--<activity-->
<!-- android:name=".activities.ProfilePicViewer"-->
<!-- android:parentActivityName=".activities.MainActivity">-->
<!-- <meta-data-->
<!-- android:name="android.support.PARENT_ACTIVITY"-->
<!-- android:value=".activities.MainActivity" />-->
<!--</activity>-->
<!--<activity-->
<!-- android:name=".activities.ProfileViewer"-->
<!-- android:parentActivityName=".activities.PostViewer">-->
<!-- <meta-data-->
<!-- android:name="android.support.PARENT_ACTIVITY"-->
<!-- android:value=".activities.PostViewer" />-->
<!--</activity>-->
<!--<activity-->
<!-- android:name=".activities.SavedViewerFragment"-->
<!-- android:parentActivityName=".activities.MainActivity">-->
<!-- <meta-data-->
<!-- android:name="android.support.PARENT_ACTIVITY"-->
<!-- android:value=".activities.MainActivity" />-->
<!--</activity>-->
<activity
android:name=".activities.NotificationsViewer"
android:parentActivityName=".activities.MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activities.MainActivity" />
</activity>
<activity
android:name=".activities.Login"
android:label="@string/login"
android:parentActivityName=".activities.MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activities.MainActivity" />
</activity>
<!--<activity-->
<!-- android:name=".activities.DirectMessagesActivity"-->
<!-- android:parentActivityName=".activities.MainActivity">-->
<!-- <meta-data-->
<!-- android:name="android.support.PARENT_ACTIVITY"-->
<!-- android:value=".activities.MainActivity" />-->
<!--</activity>-->
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"

2
app/src/main/java/awais/instagrabber/MainHelper.java

@ -811,7 +811,7 @@
// if (data != null && !Utils.isEmpty(data)) {
// if (data.indexOf('\n') > 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();

156
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);
}
}

62
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<Void, Void, ViewerPostModel[]> {
private static final String TAG = "PostFetcher";
private final String shortCode;
private final FetchListener<ViewerPostModel[]> fetchListener;
@ -46,17 +47,41 @@ public final class PostFetcher extends AsyncTask<Void, Void, ViewerPostModel[]>
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<Void, Void, ViewerPostModel[]>
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<Void, Void, ViewerPostModel[]>
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<Void, Void, ViewerPostModel[]>
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<Void, Void, ViewerPostModel[]>
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<Void, Void, ViewerPostModel[]>
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;
}

3
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();

9
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);
}
}

8
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);

75
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;
}
}

53
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();

Loading…
Cancel
Save