Browse Source
Handle intents (urls/share) from outside the app
renovate/org.robolectric-robolectric-4.x
Handle intents (urls/share) from outside the app
renovate/org.robolectric-robolectric-4.x
Ammar Githam
4 years ago
9 changed files with 263 additions and 196 deletions
-
91app/src/main/AndroidManifest.xml
-
2app/src/main/java/awais/instagrabber/MainHelper.java
-
154app/src/main/java/awais/instagrabber/activities/MainActivity.java
-
48app/src/main/java/awais/instagrabber/asyncs/PostFetcher.java
-
3app/src/main/java/awais/instagrabber/directdownload/DirectDownload.java
-
7app/src/main/java/awais/instagrabber/fragments/HashTagFragment.java
-
6app/src/main/java/awais/instagrabber/fragments/PostViewFragment.java
-
75app/src/main/java/awais/instagrabber/utils/IntentUtils.java
-
53app/src/main/java/awais/instagrabber/utils/Utils.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; |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue