1
0
mirror of https://github.com/etesync/android synced 2024-12-23 07:08:16 +00:00

Add webview activity to handle etesync link.

This commit is contained in:
Tal Hacohen 2017-03-05 19:16:54 +01:00
parent 57810cd5f5
commit c041ddb690
4 changed files with 214 additions and 0 deletions

View File

@ -183,6 +183,13 @@
android:exported="true"
android:label="@string/debug_info_title">
</activity>
<activity
android:name=".ui.WebViewActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar"
android:parentActivityName=".ui.AccountsActivity"/>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="@string/authority_log_provider"

View File

@ -0,0 +1,169 @@
package com.etesync.syncadapter.ui;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import com.etesync.syncadapter.Constants;
import com.etesync.syncadapter.R;
public class WebViewActivity extends AppCompatActivity {
private static final String KEY_URL = "url";
private static final String QUERY_KEY_EMBEDDED = "embedded";
private WebView mWebView;
private ProgressBar mProgressBar;
private Toolbar mToolbar;
public static void openUrl(Context context, Uri uri) {
Intent intent = new Intent(context, WebViewActivity.class);
intent.putExtra(WebViewActivity.KEY_URL, uri);
context.startActivity(intent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Uri uri = getIntent().getParcelableExtra(KEY_URL);
uri = addQueryParams(uri);
mWebView = (WebView) findViewById(R.id.webView);
mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl(uri.toString());
mWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
setTitle(view.getTitle());
}
@SuppressWarnings("deprecation")
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return shouldOverrideUrl(Uri.parse(url));
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
return shouldOverrideUrl(request.getUrl());
}
@SuppressWarnings("deprecation")
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
loadErrorPage(failingUrl);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
loadErrorPage(request.getUrl().toString());
}
});
mWebView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
mToolbar = (Toolbar) findViewById(R.id.toolbar);
if (progress == 100) {
mToolbar.setTitle(view.getTitle());
mProgressBar.setVisibility(View.INVISIBLE);
} else {
mToolbar.setTitle(R.string.loading);
mProgressBar.setVisibility(View.VISIBLE);
mProgressBar.setProgress(progress);
}
}
});
}
private Uri addQueryParams(Uri uri) {
return uri.buildUpon().appendQueryParameter(QUERY_KEY_EMBEDDED, "1").build();
}
private void loadErrorPage(String failingUrl) {
String htmlData = "<html><title>" +
getString(R.string.loading_error_title) +
"</title>" +
"<style>" +
".btn {" +
" display: inline-block;" +
" padding: 6px 12px;" +
" font-size: 20px;" +
" font-weight: 400;" +
" line-height: 1.42857143;" +
" text-align: center;" +
" white-space: nowrap;" +
" vertical-align: middle;" +
" touch-action: manipulation;" +
" cursor: pointer;" +
" user-select: none;" +
" border: 1px solid #ccc;" +
" border-radius: 4px;" +
" color: #333;" +
" text-decoration: none;" +
" margin-top: 50px;" +
"}" +
"</style>" +
"<body>" +
"<div align=\"center\">" +
"<a class=\"btn\" href=\"" + failingUrl + "\">" + getString(R.string.loading_error_content) +
"</a>" +
"</form></body></html>";
mWebView.loadDataWithBaseURL("about:blank", htmlData, "text/html", "UTF-8", null);
mWebView.invalidate();
}
private boolean shouldOverrideUrl(Uri uri) {
if (Constants.webUri.getHost().equals(uri.getHost())) {
if (uri.getQueryParameter(QUERY_KEY_EMBEDDED) != null) {
return false;
} else {
uri = addQueryParams(uri);
mWebView.loadUrl(uri.toString());
return true;
}
} else {
startActivity(new Intent(Intent.ACTION_VIEW, uri));
return true;
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
mWebView.saveState(outState);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
}
return super.onKeyDown(keyCode, event);
}
}

View File

@ -0,0 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:theme="@style/ThemeOverlay.AppCompat.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"/>
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="-8dp"
android:layout_marginTop="-8dp"
android:max="100"
android:maxHeight="1dp"/>
</android.support.design.widget.AppBarLayout>
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>

View File

@ -225,4 +225,8 @@
<string name="certificate_notification_connection_security">EteSync: Connection security</string>
<string name="trust_certificate_unknown_certificate_found">EteSync has encountered an unknown certificate. Do you want to trust it?</string>
<!-- WebView activity-->
<string name="loading">Loading...</string>
<string name="loading_error_title">Loading Error</string>
<string name="loading_error_content">Refresh</string>
</resources>