From a2f301f75e34a3a28cad081d445dfd021fce45c1 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Thu, 1 Aug 2019 13:23:47 +0800 Subject: Implement Deep Link to open reddit links directly in this app. --- app/src/main/AndroidManifest.xml | 34 +++++++++++++++++++--- .../infinityforreddit/MainActivity.java | 10 +++++++ .../infinityforreddit/ViewPostDetailActivity.java | 25 ++++++++++++++-- .../ViewSubredditDetailActivity.java | 25 +++++++++++++++- .../infinityforreddit/ViewUserDetailActivity.java | 19 +++++++++++- 5 files changed, 104 insertions(+), 9 deletions(-) (limited to 'app/src') diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 423600da..8a75d069 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -73,7 +73,6 @@ android:theme="@style/AppTheme.NoActionBarWithTransparentStatusBar"> - @@ -92,15 +91,42 @@ + android:theme="@style/AppTheme.NoActionBar"> + + + + + + + + android:theme="@style/AppTheme.NoActionBarWithTranslucentWindow"> + + + + + + + + android:theme="@style/AppTheme.NoActionBarWithTranslucentWindow"> + + + + + + + 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 fb5664ad..f6e0e9ca 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java @@ -2,6 +2,7 @@ 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; @@ -26,6 +27,8 @@ 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; @@ -63,6 +66,7 @@ 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; @@ -113,7 +117,26 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So statusBarHeight = getResources().getDimensionPixelSize(resourceId); } - subredditName = getIntent().getExtras().getString(EXTRA_SUBREDDIT_NAME_KEY); + 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); + } + 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 cc1edbad..1e1c6d7c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java @@ -2,6 +2,7 @@ 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; @@ -29,6 +30,8 @@ import com.google.android.material.chip.Chip; import com.google.android.material.snackbar.Snackbar; import com.google.android.material.tabs.TabLayout; +import java.util.List; + import javax.inject.Inject; import javax.inject.Named; @@ -71,6 +74,7 @@ public class ViewUserDetailActivity extends AppCompatActivity { private AppBarLayout.LayoutParams params; private String userName; + private Uri userUri; private boolean subscriptionReady = false; private boolean isInLazyMode = false; private int colorPrimary; @@ -106,7 +110,20 @@ public class ViewUserDetailActivity extends AppCompatActivity { statusBarHeight = getResources().getDimensionPixelSize(resourceId); } - userName = getIntent().getExtras().getString(EXTRA_USER_NAME_KEY); + if(getIntent().getData() != null) { + userUri = getIntent().getData(); + List segments = userUri.getPathSegments(); + int userIndex = segments.indexOf("user"); + if(userIndex >= 0 && userIndex < segments.size() - 1) { + userName = segments.get(userIndex + 1); + } else { + //Deep link error handling + finish(); + } + } else { + userName = getIntent().getExtras().getString(EXTRA_USER_NAME_KEY); + } + String title = "u/" + userName; userNameTextView.setText(title); -- cgit v1.2.3