Fixed deep link error handling.

This commit is contained in:
Alex Ning 2019-08-02 15:25:44 +08:00
parent f86f06ffe5
commit 8940d4da68
3 changed files with 61 additions and 32 deletions

View File

@ -1,14 +1,20 @@
package ml.docilealligator.infinityforreddit;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.browser.customtabs.CustomTabsIntent;
import java.util.ArrayList;
import java.util.List;
import static androidx.browser.customtabs.CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION;
public class LinkResolverActivity extends AppCompatActivity {
private static final String POST_PATTERN = "/r/\\w+/comments/\\w+/*[\\w+]*/*";
@ -58,12 +64,56 @@ public class LinkResolverActivity extends AppCompatActivity {
}
private void deepLinkError(Uri uri) {
//Deep link error handling
PackageManager pm = getPackageManager();
ArrayList<ResolveInfo> resolveInfos = getCustomTabsPackages(pm);
if(!resolveInfos.isEmpty()) {
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.intent.setPackage(resolveInfos.get(0).activityInfo.packageName);
customTabsIntent.launchUrl(this, uri);
} else {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(uri);
List<ResolveInfo> activities = pm.queryIntentActivities(intent, 0);
ArrayList<String> packageNames = new ArrayList<>();
String currentPackageName = getApplicationContext().getPackageName();
for(ResolveInfo info : activities) {
if(!info.activityInfo.packageName.equals(currentPackageName)) {
packageNames.add(info.activityInfo.packageName);
}
}
if(!packageNames.isEmpty()) {
intent.setPackage(packageNames.get(0));
startActivity(intent);
} else {
Toast.makeText(this, R.string.no_browser_found, Toast.LENGTH_SHORT).show();
}
}
}
private ArrayList<ResolveInfo> getCustomTabsPackages(PackageManager pm) {
// Get default VIEW intent handler.
Intent activityIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.example.com"));
// Get all apps that can handle VIEW intents.
List<ResolveInfo> resolvedActivityList = pm.queryIntentActivities(activityIntent, 0);
ArrayList<ResolveInfo> packagesSupportingCustomTabs = new ArrayList<>();
for (ResolveInfo info : resolvedActivityList) {
Intent serviceIntent = new Intent();
serviceIntent.setAction(ACTION_CUSTOM_TABS_CONNECTION);
serviceIntent.setPackage(info.activityInfo.packageName);
// Check if this package also resolves the Custom Tabs service.
if (pm.resolveService(serviceIntent, 0) != null) {
packagesSupportingCustomTabs.add(info);
}
}
return packagesSupportingCustomTabs;
}
}

View File

@ -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;
@ -14,7 +13,6 @@ 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;
@ -31,8 +29,6 @@ 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;
@ -75,7 +71,6 @@ 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;
@ -111,26 +106,7 @@ public class ViewUserDetailActivity extends AppCompatActivity {
statusBarHeight = getResources().getDimensionPixelSize(resourceId);
}
if(getIntent().getData() != null) {
userUri = getIntent().getData();
List<String> 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
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);
}
String title = "u/" + userName;
userNameTextView.setText(title);

View File

@ -146,4 +146,7 @@
<string name="sort_relevance">Relevance</string>
<string name="sort_comments">Comments</string>
<string name="sort_activity">Activity</string>
<string name="open_link_with">Open link with</string>
<string name="no_browser_found">No browser found</string>
</resources>