Skip to content

Commit

Permalink
feat(Android): Make app Scheme configurable with a preference (#274)
Browse files Browse the repository at this point in the history
Fix #269, #255
  • Loading branch information
jcesarmobile authored and mlynch committed Jan 16, 2019
1 parent 3d1bcdd commit 18d9f2c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,23 @@ Default value is `localhost`.
Example `ionic://app` on iOS, `http://app` on Android.

If you change it, you'll need to add a new `allow-navigation` entry in the `config.xml` for the configured url (i.e `<allow-navigation href="http://app/*"/>` if `Hostname` is set to `app`).
This is only needed for the Android url as it uses `http://`, all `ionic://` urls are whitelisted by the plugin.
This is only needed for the Android url when using `http://`, `https://` or a custom scheme. All `ionic://` urls are whitelisted by the plugin.

### Android Preferences

Preferences only available Android platform

#### Scheme

```xml
<preference name="Scheme" value="https" />
```

Default value is `http`

Configures the Scheme the app uses to load the content.


#### MixedContentMode

```xml
Expand Down Expand Up @@ -112,9 +123,9 @@ Whether to use a dark styled keyboard on iOS
cordova plugin add cordova-plugin-ionic-webview@latest
```

1. Apps are now served from HTTP on Android.
1. Apps are now served from HTTP on Android by default.

* The default origin for requests from the Android WebView is `http://localhost`. If `Hostname` preference is set, then origin will be `http://HostnameValue`.
* The default origin for requests from the Android WebView is `http://localhost`. If `Hostname` and `Scheme` preferences are set, then origin will be `schemeValue://HostnameValue`.

1. Apps are now served from `ionic://` scheme on iOS.

Expand Down
2 changes: 2 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
<platform name="android">
<config-file target="config.xml" parent="/*">
<allow-navigation href="http://localhost/*"/>
<allow-navigation href="https://localhost/*"/>
<allow-navigation href="ionic://*"/>
<preference name="webView" value="com.ionicframework.cordova.webview.IonicWebViewEngine"/>
<feature name="IonicWebView">
<param name="android-package" value="com.ionicframework.cordova.webview.IonicWebView"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,10 @@ public void init(CordovaWebView parentWebView, CordovaInterface cordova, final C
parser.parse(cordova.getActivity());

String hostname = preferences.getString("Hostname", "localhost");
CDV_LOCAL_SERVER = "http://" + hostname;
String scheme = preferences.getString("Scheme", "http");
CDV_LOCAL_SERVER = scheme + "://" + hostname;

localServer = new WebViewLocalServer(cordova.getActivity(), hostname, true, parser);
localServer = new WebViewLocalServer(cordova.getActivity(), hostname, true, parser, scheme);
localServer.hostAssets("www");

webView.setWebViewClient(new ServerClient(this, parser));
Expand Down Expand Up @@ -133,9 +134,14 @@ public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
String launchUrl = parser.getLaunchUrl();
if (!launchUrl.contains("http") && url.equals(launchUrl)) {
if (!launchUrl.contains(WebViewLocalServer.httpsScheme) && !launchUrl.contains(WebViewLocalServer.httpScheme) && url.equals(launchUrl)) {
view.stopLoading();
view.loadUrl(CDV_LOCAL_SERVER);
// When using a custom scheme the app won't load if server start url doesn't end in /
String startUrl = CDV_LOCAL_SERVER;
if (!CDV_LOCAL_SERVER.startsWith(WebViewLocalServer.httpsScheme) && !CDV_LOCAL_SERVER.startsWith(WebViewLocalServer.httpScheme)) {
startUrl += "/";
}
view.loadUrl(startUrl);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,15 @@
public class WebViewLocalServer {
private static String TAG = "WebViewAssetServer";
private String basePath;
private final static String httpScheme = "http";
private final static String httpsScheme = "https";
public final static String httpScheme = "http";
public final static String httpsScheme = "https";
public final static String fileStart = "/_app_file_";
public final static String contentStart = "/_app_content_";

private final UriMatcher uriMatcher;
private final AndroidProtocolHandler protocolHandler;
private final String authority;
private final String customScheme;
// Whether we're serving local files or proxying (for example, when doing livereload on a
// non-local endpoint (will be false in that case)
private boolean isAsset;
Expand Down Expand Up @@ -161,12 +162,13 @@ public Uri getHttpsPrefix() {
}
}

WebViewLocalServer(Context context, String authority, boolean html5mode, ConfigXmlParser parser) {
WebViewLocalServer(Context context, String authority, boolean html5mode, ConfigXmlParser parser, String customScheme) {
uriMatcher = new UriMatcher(null);
this.html5mode = html5mode;
this.parser = parser;
this.protocolHandler = new AndroidProtocolHandler(context.getApplicationContext());
this.authority = authority;
this.customScheme = customScheme;
}

private static Uri parseAndVerifyUrl(String url) {
Expand Down Expand Up @@ -245,7 +247,7 @@ private WebResourceResponse handleLocalRequest(Uri uri, PathHandler handler) {
handler.getStatusCode(), handler.getReasonPhrase(), handler.getResponseHeaders(), responseStream);
}

if (path.equals("/") || (!uri.getLastPathSegment().contains(".") && html5mode)) {
if (path.equals("") || path.equals("/") || (!uri.getLastPathSegment().contains(".") && html5mode)) {
InputStream stream;
String launchURL = parser.getLaunchUrl();
String launchFile = launchURL.substring(launchURL.lastIndexOf("/") + 1, launchURL.length());
Expand Down Expand Up @@ -429,6 +431,9 @@ public InputStream handle(Uri url) {

registerUriForScheme(httpScheme, handler, authority);
registerUriForScheme(httpsScheme, handler, authority);
if (!customScheme.equals(httpScheme) && !customScheme.equals(httpsScheme)) {
registerUriForScheme(customScheme, handler, authority);
}

}

Expand Down

0 comments on commit 18d9f2c

Please sign in to comment.