Handle when custom tabs and browser is not installed on the device. (#172)

* Handle when custom tabs and browser is not installed on the device.
This commit is contained in:
Saket Kumar 2018-01-11 02:36:19 +05:30 committed by Guilherme Gazzo
parent 89c178470a
commit 1df96aef91
4 changed files with 53 additions and 7 deletions

View File

@ -177,9 +177,10 @@ jobs:
path: ios/RocketChatRN.ipa
- persist_to_workspace:
root: ios
root: .
paths:
- RocketChatRN.ipa
- ios/*.ipa
- ios/fastlane/report.xml
ios-testflight:
macos:

View File

@ -1,13 +1,18 @@
package com.rocketchatrn;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.support.customtabs.CustomTabsIntent;
import android.widget.Toast;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import java.util.List;
import chat.rocket.reactnative.R;
/**
* Launches custom tabs.
@ -15,12 +20,9 @@ import com.facebook.react.bridge.ReactMethod;
public class CustomTabsAndroid extends ReactContextBaseJavaModule {
public ReactApplicationContext context;
public CustomTabsAndroid(ReactApplicationContext reactContext) {
super(reactContext);
this.context = reactContext;
}
@Override
@ -32,6 +34,23 @@ public class CustomTabsAndroid extends ReactContextBaseJavaModule {
public void openURL(String url) throws NullPointerException {
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(getReactApplicationContext().getCurrentActivity(), Uri.parse(url));
if (CustomTabsHelper.isChromeCustomTabsSupported(getReactApplicationContext())) {
customTabsIntent.launchUrl(getReactApplicationContext().getCurrentActivity(), Uri.parse(url));
} else {
//open in browser
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
//ensure browser is present
final List<ResolveInfo> customTabsApps = getReactApplicationContext()
.getCurrentActivity().getPackageManager().queryIntentActivities(i, 0);
if (customTabsApps.size() > 0) {
getReactApplicationContext().startActivity(i);
} else {
// no browser
Toast.makeText(getReactApplicationContext(), R.string.no_browser_found, Toast.LENGTH_SHORT).show();
}
}
}
}

View File

@ -0,0 +1,24 @@
package com.rocketchatrn;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import java.util.List;
/**
* Contains helper methods for custom tabs.
*/
public class CustomTabsHelper {
private static final String SERVICE_ACTION = "android.support.customtabs.action.CustomTabsService";
private static final String CHROME_PACKAGE = "com.android.chrome";
public static boolean isChromeCustomTabsSupported(final Context context) {
Intent serviceIntent = new Intent(SERVICE_ACTION);
serviceIntent.setPackage(CHROME_PACKAGE);
List<ResolveInfo> resolveInfos = context.getPackageManager().queryIntentServices(serviceIntent, 0);
return !(resolveInfos == null || resolveInfos.isEmpty());
}
}

View File

@ -1,3 +1,5 @@
<resources>
<string name="app_name">RocketChatRN</string>
<string name="no_browser_found">No Browser Found</string>
</resources>