diff options
author | Alex Ning <chineseperson5@gmail.com> | 2019-08-02 05:51:32 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2019-08-02 05:51:32 +0000 |
commit | f86f06ffe513e250df51dbdc7045966b220a357f (patch) | |
tree | a475453733f59484cc6b0eaa531dd0f1f2486f9d /app | |
parent | a2f301f75e34a3a28cad081d445dfd021fce45c1 (diff) | |
download | infinity-for-reddit-f86f06ffe513e250df51dbdc7045966b220a357f.tar infinity-for-reddit-f86f06ffe513e250df51dbdc7045966b220a357f.tar.gz infinity-for-reddit-f86f06ffe513e250df51dbdc7045966b220a357f.tar.bz2 infinity-for-reddit-f86f06ffe513e250df51dbdc7045966b220a357f.tar.lz infinity-for-reddit-f86f06ffe513e250df51dbdc7045966b220a357f.tar.xz infinity-for-reddit-f86f06ffe513e250df51dbdc7045966b220a357f.tar.zst infinity-for-reddit-f86f06ffe513e250df51dbdc7045966b220a357f.zip |
Create LinkResolverActivity to resolve the reddit links instead of resolving them in different activities. Deep link error handling is still broken.
Diffstat (limited to '')
5 files changed, 97 insertions, 78 deletions
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 8a75d069..362a128d 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -19,6 +19,18 @@ android:supportsRtl="true" android:theme="@style/AppTheme" android:usesCleartextTraffic="true"> + <activity android:name=".LinkResolverActivity"> + <intent-filter> + <action android:name="android.intent.action.VIEW" /> + + <category android:name="android.intent.category.DEFAULT" /> + <category android:name="android.intent.category.BROWSABLE" /> + + <data + android:host="www.reddit.com" + android:scheme="https" /> + </intent-filter> + </activity> <activity android:name=".SearchActivity" android:label="@string/search_activity_label" @@ -73,6 +85,7 @@ android:theme="@style/AppTheme.NoActionBarWithTransparentStatusBar"> <intent-filter> <action android:name="android.intent.action.MAIN" /> + <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> @@ -91,42 +104,15 @@ <activity android:name=".ViewPostDetailActivity" android:parentActivityName=".MainActivity" - android:theme="@style/AppTheme.NoActionBar"> - <intent-filter> - <action android:name="android.intent.action.VIEW" /> - <category android:name="android.intent.category.DEFAULT" /> - <category android:name="android.intent.category.BROWSABLE" /> - <data android:scheme="https" - android:host="www.reddit.com" - android:pathPattern="/r/..*/comments/..*"/> - </intent-filter> - </activity> + android:theme="@style/AppTheme.NoActionBar" /> <activity android:name=".ViewSubredditDetailActivity" android:parentActivityName=".MainActivity" - android:theme="@style/AppTheme.NoActionBarWithTranslucentWindow"> - <intent-filter> - <action android:name="android.intent.action.VIEW" /> - <category android:name="android.intent.category.DEFAULT" /> - <category android:name="android.intent.category.BROWSABLE" /> - <data android:scheme="https" - android:host="www.reddit.com" - android:pathPattern="/r/..*"/> - </intent-filter> - </activity> + android:theme="@style/AppTheme.NoActionBarWithTranslucentWindow" /> <activity android:name=".ViewUserDetailActivity" android:parentActivityName=".MainActivity" - android:theme="@style/AppTheme.NoActionBarWithTranslucentWindow"> - <intent-filter> - <action android:name="android.intent.action.VIEW" /> - <category android:name="android.intent.category.DEFAULT" /> - <category android:name="android.intent.category.BROWSABLE" /> - <data android:scheme="https" - android:host="www.reddit.com" - android:pathPattern="/user/..*"/> - </intent-filter> - </activity> + android:theme="@style/AppTheme.NoActionBarWithTranslucentWindow" /> <provider android:name="androidx.core.content.FileProvider" @@ -135,8 +121,7 @@ android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" - android:resource="@xml/file_paths"> - </meta-data> + android:resource="@xml/file_paths" /> </provider> </application> 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<String> 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<String> 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<String> 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); |