From f86f06ffe513e250df51dbdc7045966b220a357f Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Fri, 2 Aug 2019 13:51:32 +0800 Subject: Create LinkResolverActivity to resolve the reddit links instead of resolving them in different activities. Deep link error handling is still broken. --- .../infinityforreddit/LinkResolverActivity.java | 69 ++++++++++++++++++++++ .../infinityforreddit/ViewPostDetailActivity.java | 25 +------- .../ViewSubredditDetailActivity.java | 25 +------- .../infinityforreddit/ViewUserDetailActivity.java | 7 +++ 4 files changed, 80 insertions(+), 46 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/LinkResolverActivity.java (limited to 'app/src/main/java') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/LinkResolverActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/LinkResolverActivity.java new file mode 100644 index 00000000..93ddc7dd --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/LinkResolverActivity.java @@ -0,0 +1,69 @@ +package ml.docilealligator.infinityforreddit; + +import android.content.Intent; +import android.net.Uri; +import android.os.Bundle; + +import androidx.appcompat.app.AppCompatActivity; +import androidx.browser.customtabs.CustomTabsIntent; + +import java.util.List; + +public class LinkResolverActivity extends AppCompatActivity { + + private static final String POST_PATTERN = "/r/\\w+/comments/\\w+/*[\\w+]*/*"; + private static final String SUBREDDIT_PATTERN = "/r/\\w+/*"; + private static final String USER_PATTERN = "/user/\\w+/*"; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + Uri uri = getIntent().getData(); + String path = uri.getPath(); + if(path.endsWith("/")) { + path = path.substring(0, path.length() - 1); + } + + if(path.matches(POST_PATTERN)) { + List segments = uri.getPathSegments(); + int commentsIndex = segments.lastIndexOf("comments"); + if(commentsIndex >=0 && commentsIndex < segments.size() - 1) { + Intent intent = new Intent(this, ViewPostDetailActivity.class); + intent.putExtra(ViewPostDetailActivity.EXTRA_POST_ID, segments.get(commentsIndex + 1)); + startActivity(intent); + } else { + deepLinkError(uri); + } + } else if(path.matches(SUBREDDIT_PATTERN)) { + String subredditName = path.substring(3); + if(subredditName.equals("popular") || subredditName.equals("all")) { + Intent intent = new Intent(this, MainActivity.class); + intent.putExtra(MainActivity.EXTRA_POST_TYPE, subredditName); + startActivity(intent); + } else { + Intent intent = new Intent(this, ViewSubredditDetailActivity.class); + intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, path.substring(3)); + startActivity(intent); + } + } else if(path.matches(USER_PATTERN)) { + Intent intent = new Intent(this, ViewUserDetailActivity.class); + intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, path.substring(6)); + startActivity(intent); + } else { + deepLinkError(uri); + } + + finish(); + } + + private void deepLinkError(Uri uri) { + //Deep link error handling + CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder(); + // add share action to menu list + builder.addDefaultShareMenuItem(); + builder.setToolbarColor(getResources().getColor(R.color.colorPrimary)); + CustomTabsIntent customTabsIntent = builder.build(); + customTabsIntent.launchUrl(this, uri); + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java index 27b59c33..87c4a380 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java @@ -3,7 +3,6 @@ package ml.docilealligator.infinityforreddit; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; -import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.view.Menu; @@ -33,7 +32,6 @@ import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; import java.util.ArrayList; -import java.util.List; import java.util.Locale; import javax.inject.Inject; @@ -64,8 +62,6 @@ public class ViewPostDetailActivity extends AppCompatActivity { @State Post mPost; @State - Uri postUri; - @State boolean isLoadingMoreChildren = false; @State boolean isRefreshing = false; @@ -126,17 +122,13 @@ public class ViewPostDetailActivity extends AppCompatActivity { orientation = getResources().getConfiguration().orientation; if(mPost == null) { - if(getIntent().getData() != null) { - postUri = getIntent().getData(); - } else { - mPost = getIntent().getExtras().getParcelable(EXTRA_POST_DATA); - } + mPost = getIntent().getExtras().getParcelable(EXTRA_POST_DATA); } - if(postUri == null && mPost == null) { + if(mPost == null) { mProgressBar.setVisibility(View.VISIBLE); fetchPostAndCommentsById(getIntent().getExtras().getString(EXTRA_POST_ID)); - } else if(mPost != null) { + } else { mAdapter = new CommentAndPostRecyclerViewAdapter(ViewPostDetailActivity.this, mRetrofit, mOauthRetrofit, mGlide, mSharedPreferences, mPost, mPost.getSubredditNamePrefixed(), mLocale, mLoadSubredditIconAsyncTask, @@ -170,17 +162,6 @@ public class ViewPostDetailActivity extends AppCompatActivity { } } } - } else { - mProgressBar.setVisibility(View.VISIBLE); - List segments = postUri.getPathSegments(); - int commentIndex = segments.indexOf("comments"); - if(commentIndex >= 0 || commentIndex != segments.size() - 1) { - mProgressBar.setVisibility(View.VISIBLE); - fetchPostAndCommentsById(segments.get(commentIndex + 1)); - } else { - //Deep link error handling - finish(); - } } if(getIntent().hasExtra(EXTRA_POST_LIST_POSITION)) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java index f6e0e9ca..fb5664ad 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java @@ -2,7 +2,6 @@ package ml.docilealligator.infinityforreddit; import android.content.Intent; import android.content.SharedPreferences; -import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.view.Menu; @@ -27,8 +26,6 @@ import com.google.android.material.chip.Chip; import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.google.android.material.snackbar.Snackbar; -import java.util.List; - import javax.inject.Inject; import javax.inject.Named; @@ -66,7 +63,6 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So @BindView(R.id.fab_view_subreddit_detail_activity) FloatingActionButton fab; private String subredditName; - private Uri subredditUri; private boolean subscriptionReady = false; private boolean isInLazyMode = false; @@ -117,26 +113,7 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So statusBarHeight = getResources().getDimensionPixelSize(resourceId); } - if(getIntent().getData() != null) { - subredditUri = getIntent().getData(); - List segments = subredditUri.getPathSegments(); - int rIndex = segments.indexOf("r"); - if(rIndex >= 0 && rIndex < segments.size() - 1) { - subredditName = segments.get(rIndex + 1); - if(subredditName.equals("popular") || subredditName.equals("all")) { - Intent intent = new Intent(this, MainActivity.class); - intent.putExtra(MainActivity.EXTRA_POST_TYPE, subredditName); - startActivity(intent); - finish(); - } - } else { - //Deep link error handling - finish(); - } - } else { - subredditName = getIntent().getExtras().getString(EXTRA_SUBREDDIT_NAME_KEY); - } - + subredditName = getIntent().getExtras().getString(EXTRA_SUBREDDIT_NAME_KEY); String title = "r/" + subredditName; subredditNameTextView.setText(title); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java index 1e1c6d7c..36fecc05 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java @@ -14,6 +14,7 @@ import android.widget.TextView; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; +import androidx.browser.customtabs.CustomTabsIntent; import androidx.coordinatorlayout.widget.CoordinatorLayout; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; @@ -118,7 +119,13 @@ public class ViewUserDetailActivity extends AppCompatActivity { userName = segments.get(userIndex + 1); } else { //Deep link error handling + CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder(); + builder.addDefaultShareMenuItem(); + builder.setToolbarColor(getResources().getColor(R.color.colorPrimary)); + CustomTabsIntent customTabsIntent = builder.build(); + customTabsIntent.launchUrl(this, userUri); finish(); + return; } } else { userName = getIntent().getExtras().getString(EXTRA_USER_NAME_KEY); -- cgit v1.2.3