Browse Source

Use Uri.parse for IntentUtils.parseUrl

Fixes #1178
renovate/org.robolectric-robolectric-4.x
tcely 4 years ago
parent
commit
53ce38f5cd
No known key found for this signature in database GPG Key ID: E361829E1B3040DB
  1. 8
      app/src/main/java/awais/instagrabber/activities/MainActivity.java
  2. 51
      app/src/main/java/awais/instagrabber/utils/IntentUtils.java

8
app/src/main/java/awais/instagrabber/activities/MainActivity.java

@ -613,6 +613,14 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage
if (url == null) return; if (url == null) return;
// Log.d(TAG, url); // Log.d(TAG, url);
final IntentModel intentModel = IntentUtils.parseUrl(url); final IntentModel intentModel = IntentUtils.parseUrl(url);
// Temporarily log URLs that parse in a new way, then use the old parser result
final IntentModel intentModelOld = IntentUtils.parseUrlOld(url);
if (intentModelOld != null && (intentModel == null || (intentModelOld.getType() != intentModel.getType()) || (intentModelOld.getText() != intentModel.getText()))) {
Log.w(TAG, url);
intentModel = intentModelOld;
}
if (intentModel == null) return; if (intentModel == null) return;
showView(intentModel); showView(intentModel);
} }

51
app/src/main/java/awais/instagrabber/utils/IntentUtils.java

@ -1,17 +1,66 @@
package awais.instagrabber.utils; package awais.instagrabber.utils;
import android.net.Uri;
import android.text.TextUtils; import android.text.TextUtils;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import java.util.List;
import awais.instagrabber.models.IntentModel; import awais.instagrabber.models.IntentModel;
import awais.instagrabber.models.enums.IntentModelType; import awais.instagrabber.models.enums.IntentModelType;
public final class IntentUtils { public final class IntentUtils {
@Nullable @Nullable
public static IntentModel parseUrl(@NonNull String url) {
public static IntentModel parseUrl(@NonNull final String url) {
try {
final Uri parsedUrl = Uri.parse(url).normalizeScheme();
} catch (NullPointerException e) {
return null;
}
// final String domain = parsedUrl.getHost().replaceFirst("^www\\.", "");
// final boolean isHttpsUri = "https".equals(parsedUrl.getScheme());
final List<String> paths = parsedUrl.getPathSegments();
String path = paths.get(0);
String text = null;
IntentModelType type = IntentModelType.UNKNOWN;
if (1 == paths.size()) {
text = path;
type = IntentModelType.USERNAME;
} else if ("_u".equals(path) || "u".equals(path)) {
text = paths.get(1);
type = IntentModelType.USERNAME;
} else if ("p".equals(path) || "reel".equals(path) || "tv".equals(path)) {
text = paths.get(1);
type = IntentModelType.POST;
} else if (2 >= paths.size() && "explore".equals(path)) {
path = paths.get(1);
if ("locations".equals(path)) {
text = paths.get(2);
type = IntentModelType.LOCATION;
}
if ("tags".equals(path)) {
text = paths.get(2);
type = IntentModelType.HASHTAG;
}
}
if (TextUtils.isEmpty(text)) {
return null;
}
return new IntentModel(type, text);
}
@Nullable
public static IntentModel parseUrlOld(@NonNull String url) {
if (url.contains("instagr.am/")) { if (url.contains("instagr.am/")) {
url = url.replaceFirst("s?://(?:www\\.)?instagr\\.am/", "s://www.instagram.com/"); url = url.replaceFirst("s?://(?:www\\.)?instagr\\.am/", "s://www.instagram.com/");
} }

Loading…
Cancel
Save