Open browser first instead of Chrome Custom Tabs when opening links.

This commit is contained in:
Alex Ning 2019-10-06 09:11:13 +08:00
parent b56594aeba
commit 2a2270155d

View File

@ -149,25 +149,30 @@ public class LinkResolverActivity extends AppCompatActivity {
} }
private void deepLinkError(Uri uri) { private void deepLinkError(Uri uri) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(uri);
PackageManager pm = getPackageManager(); PackageManager pm = getPackageManager();
ArrayList<ResolveInfo> resolveInfos = getCustomTabsPackages(pm); List<ResolveInfo> activities = pm.queryIntentActivities(intent, 0);
if (!resolveInfos.isEmpty()) { ArrayList<String> packageNames = new ArrayList<>();
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
// add share action to menu list String currentPackageName = getApplicationContext().getPackageName();
builder.addDefaultShareMenuItem();
builder.setToolbarColor(getResources().getColor(R.color.colorPrimary)); for (ResolveInfo info : activities) {
CustomTabsIntent customTabsIntent = builder.build(); if (!info.activityInfo.packageName.equals(currentPackageName)) {
customTabsIntent.intent.setPackage(resolveInfos.get(0).activityInfo.packageName); packageNames.add(info.activityInfo.packageName);
if (uri.getScheme() == null) {
uri = Uri.parse("http://" + uri.toString());
} }
}
if (!packageNames.isEmpty()) {
intent.setPackage(packageNames.get(0));
try { try {
customTabsIntent.launchUrl(this, uri); startActivity(intent);
} catch (ActivityNotFoundException e) { } catch (ActivityNotFoundException e) {
openInBrowser(uri, pm); openInCustomTabs(uri, pm);
} }
} else { } else {
openInBrowser(uri, pm); openInCustomTabs(uri, pm);
} }
} }
@ -190,25 +195,20 @@ public class LinkResolverActivity extends AppCompatActivity {
return packagesSupportingCustomTabs; return packagesSupportingCustomTabs;
} }
private void openInBrowser(Uri uri, PackageManager pm) { private void openInCustomTabs(Uri uri, PackageManager pm) {
Intent intent = new Intent(Intent.ACTION_VIEW); ArrayList<ResolveInfo> resolveInfos = getCustomTabsPackages(pm);
intent.setData(uri); if (!resolveInfos.isEmpty()) {
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
List<ResolveInfo> activities = pm.queryIntentActivities(intent, 0); // add share action to menu list
ArrayList<String> packageNames = new ArrayList<>(); builder.addDefaultShareMenuItem();
builder.setToolbarColor(getResources().getColor(R.color.colorPrimary));
String currentPackageName = getApplicationContext().getPackageName(); CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.intent.setPackage(resolveInfos.get(0).activityInfo.packageName);
for (ResolveInfo info : activities) { if (uri.getScheme() == null) {
if (!info.activityInfo.packageName.equals(currentPackageName)) { uri = Uri.parse("http://" + uri.toString());
packageNames.add(info.activityInfo.packageName);
} }
}
if (!packageNames.isEmpty()) {
intent.setPackage(packageNames.get(0));
try { try {
startActivity(intent); customTabsIntent.launchUrl(this, uri);
} catch (ActivityNotFoundException e) { } catch (ActivityNotFoundException e) {
Toast.makeText(this, R.string.no_browser_found, Toast.LENGTH_SHORT).show(); Toast.makeText(this, R.string.no_browser_found, Toast.LENGTH_SHORT).show();
} }