Fix issues with resolving Links

This commit changes how resolving links work. It will not use the resolve object function now for local instance links, as they would fail almost every time.

It also opens toasts in the application context instead of the activity context. This might prevent some crashes
This commit is contained in:
Balazs Toldi 2023-08-25 09:02:39 +02:00
parent 6e51ea6ee7
commit 143bc5e1b6
No known key found for this signature in database
GPG Key ID: 6C7D440036F99D58

View File

@ -7,6 +7,7 @@ import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo; import android.content.pm.ResolveInfo;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.webkit.URLUtil; import android.webkit.URLUtil;
import android.widget.Toast; import android.widget.Toast;
@ -17,8 +18,11 @@ import androidx.browser.customtabs.CustomTabsService;
import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.FilenameUtils;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.Executor;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
@ -29,6 +33,7 @@ import eu.toldi.infinityforlemmy.RetrofitHolder;
import eu.toldi.infinityforlemmy.comment.Comment; import eu.toldi.infinityforlemmy.comment.Comment;
import eu.toldi.infinityforlemmy.comment.FetchComment; import eu.toldi.infinityforlemmy.comment.FetchComment;
import eu.toldi.infinityforlemmy.customtheme.CustomThemeWrapper; import eu.toldi.infinityforlemmy.customtheme.CustomThemeWrapper;
import eu.toldi.infinityforlemmy.post.FetchPost;
import eu.toldi.infinityforlemmy.post.ObjectResolver; import eu.toldi.infinityforlemmy.post.ObjectResolver;
import eu.toldi.infinityforlemmy.post.Post; import eu.toldi.infinityforlemmy.post.Post;
import eu.toldi.infinityforlemmy.utils.LemmyUtils; import eu.toldi.infinityforlemmy.utils.LemmyUtils;
@ -75,6 +80,9 @@ public class LinkResolverActivity extends AppCompatActivity {
@Named("no_oauth") @Named("no_oauth")
RetrofitHolder mRetrofit; RetrofitHolder mRetrofit;
@Inject
Executor mExecutor;
private String mAccessToken; private String mAccessToken;
private Uri getRedditUriByPath(String path) { private Uri getRedditUriByPath(String path) {
@ -100,14 +108,14 @@ public class LinkResolverActivity extends AppCompatActivity {
if (uri == null) { if (uri == null) {
String url = getIntent().getStringExtra(Intent.EXTRA_TEXT); String url = getIntent().getStringExtra(Intent.EXTRA_TEXT);
if (!URLUtil.isValidUrl(url)) { if (!URLUtil.isValidUrl(url)) {
Toast.makeText(this, R.string.invalid_link, Toast.LENGTH_SHORT).show(); Toast.makeText(getApplicationContext(), R.string.invalid_link, Toast.LENGTH_SHORT).show();
finish(); finish();
return; return;
} }
try { try {
uri = Uri.parse(url); uri = Uri.parse(url);
} catch (NullPointerException e) { } catch (NullPointerException e) {
Toast.makeText(this, R.string.invalid_link, Toast.LENGTH_SHORT).show(); Toast.makeText(getApplicationContext(), R.string.invalid_link, Toast.LENGTH_SHORT).show();
finish(); finish();
return; return;
} }
@ -115,7 +123,7 @@ public class LinkResolverActivity extends AppCompatActivity {
if (uri.getScheme() == null && uri.getHost() == null) { if (uri.getScheme() == null && uri.getHost() == null) {
if (uri.toString().isEmpty()) { if (uri.toString().isEmpty()) {
Toast.makeText(this, R.string.invalid_link, Toast.LENGTH_SHORT).show(); Toast.makeText(getApplicationContext(), R.string.invalid_link, Toast.LENGTH_SHORT).show();
finish(); finish();
return; return;
} }
@ -127,7 +135,7 @@ public class LinkResolverActivity extends AppCompatActivity {
private void handleUri(Uri uri) { private void handleUri(Uri uri) {
if (uri == null) { if (uri == null) {
Toast.makeText(this, R.string.no_link_available, Toast.LENGTH_SHORT).show(); Toast.makeText(getApplicationContext(), R.string.no_link_available, Toast.LENGTH_SHORT).show();
} else { } else {
String path = uri.getPath(); String path = uri.getPath();
if (path == null) { if (path == null) {
@ -201,6 +209,34 @@ public class LinkResolverActivity extends AppCompatActivity {
intent.putExtra(ViewPostDetailActivity.EXTRA_NEW_ACCOUNT_NAME, newAccountName); intent.putExtra(ViewPostDetailActivity.EXTRA_NEW_ACCOUNT_NAME, newAccountName);
startActivity(intent); startActivity(intent);
} else { } else {
boolean local = false;
try {
URL baseURL = new URL(mRetrofit.getBaseURL());
if (baseURL.getHost().equalsIgnoreCase(uri.getHost())) {
local = true;
FetchPost.fetchPost(mExecutor, new Handler(), mRetrofit.getRetrofit(), segments.get(segments.size() - 1), mAccessToken, new FetchPost.FetchPostListener() {
@Override
public void fetchPostSuccess(Post post) {
Intent intent = new Intent(LinkResolverActivity.this, ViewPostDetailActivity.class);
intent.putExtra(ViewPostDetailActivity.EXTRA_POST_ID, post.getId());
intent.putExtra(ViewPostDetailActivity.EXTRA_MESSAGE_FULLNAME, messageFullname);
intent.putExtra(ViewPostDetailActivity.EXTRA_NEW_ACCOUNT_NAME, newAccountName);
startActivity(intent);
}
@Override
public void fetchPostFailed() {
Toast.makeText(getApplicationContext(), R.string.could_not_resolve_link, Toast.LENGTH_SHORT).show();
finish();
}
});
}
} catch (MalformedURLException e) {
Toast.makeText(getApplicationContext(), R.string.could_not_resolve_link, Toast.LENGTH_SHORT).show();
finish();
}
if (!local) {
mObjectResolver.resolvePost(uri.toString(), mAccessToken, new ObjectResolver.ObjectResolverListener() { mObjectResolver.resolvePost(uri.toString(), mAccessToken, new ObjectResolver.ObjectResolverListener() {
@Override @Override
public void onResolveObjectSuccess(Object p) { public void onResolveObjectSuccess(Object p) {
@ -214,11 +250,12 @@ public class LinkResolverActivity extends AppCompatActivity {
@Override @Override
public void onResolveObjectFailed() { public void onResolveObjectFailed() {
Toast.makeText(LinkResolverActivity.this, R.string.could_not_resolve_link, Toast.LENGTH_SHORT).show(); Toast.makeText(getApplicationContext(), R.string.could_not_resolve_link, Toast.LENGTH_SHORT).show();
finish(); finish();
} }
}); });
} }
}
} else if (uri.toString().matches(COMMENT_PATTERN)) { } else if (uri.toString().matches(COMMENT_PATTERN)) {
if (mAccessToken == null) { if (mAccessToken == null) {
mRetrofit.setBaseURL(uri.getScheme() + "://" + uri.getHost() + "/"); mRetrofit.setBaseURL(uri.getScheme() + "://" + uri.getHost() + "/");
@ -237,11 +274,41 @@ public class LinkResolverActivity extends AppCompatActivity {
@Override @Override
public void onFetchCommentFailed() { public void onFetchCommentFailed() {
Toast.makeText(LinkResolverActivity.this, R.string.could_not_resolve_link, Toast.LENGTH_SHORT).show(); Toast.makeText(getApplicationContext(), R.string.could_not_resolve_link, Toast.LENGTH_SHORT).show();
finish(); finish();
} }
}); });
} else { } else {
boolean local = false;
try {
URL baseURL = new URL(mRetrofit.getBaseURL());
if (baseURL.getHost().equalsIgnoreCase(uri.getHost())) {
local = true;
FetchComment.fetchSingleComment(mRetrofit.getRetrofit(), mAccessToken, Integer.parseInt(segments.get(segments.size() - 1)), new FetchComment.FetchCommentListener() {
@Override
public void onFetchCommentSuccess(ArrayList<Comment> comments, Integer parentId, ArrayList<Integer> moreChildrenIds) {
Intent intent = new Intent(LinkResolverActivity.this, ViewPostDetailActivity.class);
Comment comment = comments.get(0);
intent.putExtra(ViewPostDetailActivity.EXTRA_POST_ID, comment.getPostId());
intent.putExtra(ViewPostDetailActivity.EXTRA_MESSAGE_FULLNAME, messageFullname);
intent.putExtra(ViewPostDetailActivity.EXTRA_NEW_ACCOUNT_NAME, newAccountName);
intent.putExtra(ViewPostDetailActivity.EXTRA_SINGLE_COMMENT_ID, comment.getId());
intent.putExtra(ViewPostDetailActivity.EXTRA_SINGLE_COMMENT_PARENT_ID, comment.getParentId());
startActivity(intent);
}
@Override
public void onFetchCommentFailed() {
Toast.makeText(getApplicationContext(), R.string.could_not_resolve_link, Toast.LENGTH_SHORT).show();
finish();
}
});
}
} catch (MalformedURLException e) {
Toast.makeText(getApplicationContext(), R.string.could_not_resolve_link, Toast.LENGTH_SHORT).show();
finish();
}
if (!local) {
mObjectResolver.resolveComment(uri.toString(), mAccessToken, new ObjectResolver.ObjectResolverListener() { mObjectResolver.resolveComment(uri.toString(), mAccessToken, new ObjectResolver.ObjectResolverListener() {
@Override @Override
public void onResolveObjectSuccess(Object c) { public void onResolveObjectSuccess(Object c) {
@ -257,11 +324,12 @@ public class LinkResolverActivity extends AppCompatActivity {
@Override @Override
public void onResolveObjectFailed() { public void onResolveObjectFailed() {
Toast.makeText(LinkResolverActivity.this, R.string.could_not_resolve_link, Toast.LENGTH_SHORT).show(); Toast.makeText(getApplicationContext(), R.string.could_not_resolve_link, Toast.LENGTH_SHORT).show();
finish(); finish();
} }
}); });
} }
}
} else if (authority.equals("v.redd.it")) { } else if (authority.equals("v.redd.it")) {
Intent intent = new Intent(this, ViewVideoActivity.class); Intent intent = new Intent(this, ViewVideoActivity.class);
intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_TYPE, ViewVideoActivity.VIDEO_TYPE_V_REDD_IT); intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_TYPE, ViewVideoActivity.VIDEO_TYPE_V_REDD_IT);