Create LinkResolverActivity to resolve the reddit links instead of resolving them in different activities. Deep link error handling is still broken.

This commit is contained in:
Alex Ning 2019-08-02 13:51:32 +08:00
parent a2f301f75e
commit f86f06ffe5
7 changed files with 97 additions and 78 deletions

Binary file not shown.

Binary file not shown.

View File

@ -19,6 +19,18 @@
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/AppTheme" android:theme="@style/AppTheme"
android:usesCleartextTraffic="true"> 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 <activity
android:name=".SearchActivity" android:name=".SearchActivity"
android:label="@string/search_activity_label" android:label="@string/search_activity_label"
@ -73,6 +85,7 @@
android:theme="@style/AppTheme.NoActionBarWithTransparentStatusBar"> android:theme="@style/AppTheme.NoActionBarWithTransparentStatusBar">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter> </intent-filter>
</activity> </activity>
@ -91,42 +104,15 @@
<activity <activity
android:name=".ViewPostDetailActivity" android:name=".ViewPostDetailActivity"
android:parentActivityName=".MainActivity" android:parentActivityName=".MainActivity"
android:theme="@style/AppTheme.NoActionBar"> 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>
<activity <activity
android:name=".ViewSubredditDetailActivity" android:name=".ViewSubredditDetailActivity"
android:parentActivityName=".MainActivity" android:parentActivityName=".MainActivity"
android:theme="@style/AppTheme.NoActionBarWithTranslucentWindow"> 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>
<activity <activity
android:name=".ViewUserDetailActivity" android:name=".ViewUserDetailActivity"
android:parentActivityName=".MainActivity" android:parentActivityName=".MainActivity"
android:theme="@style/AppTheme.NoActionBarWithTranslucentWindow"> 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>
<provider <provider
android:name="androidx.core.content.FileProvider" android:name="androidx.core.content.FileProvider"
@ -135,8 +121,7 @@
android:grantUriPermissions="true"> android:grantUriPermissions="true">
<meta-data <meta-data
android:name="android.support.FILE_PROVIDER_PATHS" android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"> android:resource="@xml/file_paths" />
</meta-data>
</provider> </provider>
</application> </application>

View File

@ -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);
}
}

View File

@ -3,7 +3,6 @@ package ml.docilealligator.infinityforreddit;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log; import android.util.Log;
import android.view.Menu; import android.view.Menu;
@ -33,7 +32,6 @@ import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.Subscribe;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
import java.util.Locale; import java.util.Locale;
import javax.inject.Inject; import javax.inject.Inject;
@ -64,8 +62,6 @@ public class ViewPostDetailActivity extends AppCompatActivity {
@State @State
Post mPost; Post mPost;
@State @State
Uri postUri;
@State
boolean isLoadingMoreChildren = false; boolean isLoadingMoreChildren = false;
@State @State
boolean isRefreshing = false; boolean isRefreshing = false;
@ -126,17 +122,13 @@ public class ViewPostDetailActivity extends AppCompatActivity {
orientation = getResources().getConfiguration().orientation; orientation = getResources().getConfiguration().orientation;
if(mPost == null) { 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); mProgressBar.setVisibility(View.VISIBLE);
fetchPostAndCommentsById(getIntent().getExtras().getString(EXTRA_POST_ID)); fetchPostAndCommentsById(getIntent().getExtras().getString(EXTRA_POST_ID));
} else if(mPost != null) { } else {
mAdapter = new CommentAndPostRecyclerViewAdapter(ViewPostDetailActivity.this, mRetrofit, mAdapter = new CommentAndPostRecyclerViewAdapter(ViewPostDetailActivity.this, mRetrofit,
mOauthRetrofit, mGlide, mSharedPreferences, mPost, mOauthRetrofit, mGlide, mSharedPreferences, mPost,
mPost.getSubredditNamePrefixed(), mLocale, mLoadSubredditIconAsyncTask, 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)) { if(getIntent().hasExtra(EXTRA_POST_LIST_POSITION)) {

View File

@ -2,7 +2,6 @@ package ml.docilealligator.infinityforreddit;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.net.Uri;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.view.Menu; 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.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar; import com.google.android.material.snackbar.Snackbar;
import java.util.List;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; 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; @BindView(R.id.fab_view_subreddit_detail_activity) FloatingActionButton fab;
private String subredditName; private String subredditName;
private Uri subredditUri;
private boolean subscriptionReady = false; private boolean subscriptionReady = false;
private boolean isInLazyMode = false; private boolean isInLazyMode = false;
@ -117,26 +113,7 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So
statusBarHeight = getResources().getDimensionPixelSize(resourceId); 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; String title = "r/" + subredditName;
subredditNameTextView.setText(title); subredditNameTextView.setText(title);

View File

@ -14,6 +14,7 @@ import android.widget.TextView;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar; import androidx.appcompat.widget.Toolbar;
import androidx.browser.customtabs.CustomTabsIntent;
import androidx.coordinatorlayout.widget.CoordinatorLayout; import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentManager;
@ -118,7 +119,13 @@ public class ViewUserDetailActivity extends AppCompatActivity {
userName = segments.get(userIndex + 1); userName = segments.get(userIndex + 1);
} else { } else {
//Deep link error handling //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(); finish();
return;
} }
} else { } else {
userName = getIntent().getExtras().getString(EXTRA_USER_NAME_KEY); userName = getIntent().getExtras().getString(EXTRA_USER_NAME_KEY);