ExternalSiteWebView.java 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Tobias Kaminsky
  5. * Copyright (C) 2017 Tobias Kaminsky
  6. * Copyright (C) 2017 Nextcloud GmbH.
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. package com.owncloud.android.ui.activity;
  22. import android.annotation.SuppressLint;
  23. import android.content.pm.ApplicationInfo;
  24. import android.os.Bundle;
  25. import android.text.TextUtils;
  26. import android.view.MenuItem;
  27. import android.view.View;
  28. import android.view.Window;
  29. import android.webkit.WebChromeClient;
  30. import android.webkit.WebSettings;
  31. import android.webkit.WebView;
  32. import android.webkit.WebViewClient;
  33. import android.widget.ProgressBar;
  34. import com.owncloud.android.MainApp;
  35. import com.owncloud.android.R;
  36. import com.owncloud.android.lib.common.utils.Log_OC;
  37. import com.owncloud.android.utils.DisplayUtils;
  38. import com.owncloud.android.utils.ThemeUtils;
  39. import java.io.InputStream;
  40. import androidx.appcompat.app.ActionBar;
  41. import androidx.drawerlayout.widget.DrawerLayout;
  42. /**
  43. * This activity shows an URL as a web view
  44. */
  45. public class ExternalSiteWebView extends FileActivity {
  46. public static final String EXTRA_TITLE = "TITLE";
  47. public static final String EXTRA_URL = "URL";
  48. public static final String EXTRA_SHOW_SIDEBAR = "SHOW_SIDEBAR";
  49. public static final String EXTRA_SHOW_TOOLBAR = "SHOW_TOOLBAR";
  50. public static final String EXTRA_MENU_ITEM_ID = "MENU_ITEM_ID";
  51. public static final String EXTRA_TEMPLATE = "TEMPLATE";
  52. private static final String TAG = ExternalSiteWebView.class.getSimpleName();
  53. protected boolean showToolbar = true;
  54. protected int webViewLayout = R.layout.externalsite_webview;
  55. private int menuItemId;
  56. protected WebView webview;
  57. private boolean showSidebar;
  58. String url;
  59. @Override
  60. protected void onCreate(Bundle savedInstanceState) {
  61. Log_OC.v(TAG, "onCreate() start");
  62. Bundle extras = getIntent().getExtras();
  63. String title = extras.getString(EXTRA_TITLE);
  64. url = extras.getString(EXTRA_URL);
  65. if (extras.containsKey(EXTRA_SHOW_TOOLBAR)) {
  66. showToolbar = extras.getBoolean(EXTRA_SHOW_TOOLBAR);
  67. }
  68. menuItemId = extras.getInt(EXTRA_MENU_ITEM_ID);
  69. showSidebar = extras.getBoolean(EXTRA_SHOW_SIDEBAR);
  70. // show progress
  71. Window window = getWindow();
  72. if (window != null) {
  73. window.requestFeature(Window.FEATURE_PROGRESS);
  74. }
  75. super.onCreate(savedInstanceState);
  76. setContentView(webViewLayout);
  77. webview = findViewById(R.id.webView);
  78. final WebSettings webSettings = webview.getSettings();
  79. webview.setFocusable(true);
  80. webview.setFocusableInTouchMode(true);
  81. webview.setClickable(true);
  82. // allow debugging (when building the debug version); see details in
  83. // https://developers.google.com/web/tools/chrome-devtools/remote-debugging/webviews
  84. if ((getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0 ||
  85. getResources().getBoolean(R.bool.is_beta)) {
  86. Log_OC.d(this, "Enable debug for webView");
  87. WebView.setWebContentsDebuggingEnabled(true);
  88. }
  89. // setup toolbar
  90. if (showToolbar) {
  91. setupToolbar();
  92. } else {
  93. if (findViewById(R.id.appbar) != null) {
  94. findViewById(R.id.appbar).setVisibility(View.GONE);
  95. }
  96. }
  97. // setup drawer
  98. setupDrawer(menuItemId);
  99. if (!showSidebar) {
  100. setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
  101. }
  102. if (!TextUtils.isEmpty(title)) {
  103. setupActionBar(title);
  104. }
  105. setupWebSettings(webSettings);
  106. final ProgressBar progressBar = findViewById(R.id.progressBar);
  107. if (progressBar != null) {
  108. webview.setWebChromeClient(new WebChromeClient() {
  109. public void onProgressChanged(WebView view, int progress) {
  110. progressBar.setProgress(progress * 1000);
  111. }
  112. });
  113. }
  114. webview.setWebViewClient(new WebViewClient() {
  115. public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
  116. InputStream resources = getResources().openRawResource(R.raw.custom_error);
  117. String customError = DisplayUtils.getData(resources);
  118. if (!customError.isEmpty()) {
  119. webview.loadData(customError, "text/html; charset=UTF-8", null);
  120. }
  121. }
  122. });
  123. webview.loadUrl(url);
  124. }
  125. @SuppressLint("SetJavaScriptEnabled")
  126. private void setupWebSettings(WebSettings webSettings) {
  127. // enable zoom
  128. webSettings.setSupportZoom(true);
  129. webSettings.setBuiltInZoomControls(true);
  130. webSettings.setDisplayZoomControls(false);
  131. // Non-responsive webs are zoomed out when loaded
  132. webSettings.setUseWideViewPort(true);
  133. webSettings.setLoadWithOverviewMode(true);
  134. // user agent
  135. webSettings.setUserAgentString(MainApp.getUserAgent());
  136. // no private data storing
  137. webSettings.setSavePassword(false);
  138. webSettings.setSaveFormData(false);
  139. // disable local file access
  140. webSettings.setAllowFileAccess(false);
  141. // enable javascript
  142. webSettings.setJavaScriptEnabled(true);
  143. webSettings.setDomStorageEnabled(true);
  144. // caching disabled in debug mode
  145. if ((getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) == 0) {
  146. webSettings.setAppCacheEnabled(true);
  147. webSettings.setAppCachePath(getCacheDir().getPath());
  148. }
  149. }
  150. private void setupActionBar(String title) {
  151. ActionBar actionBar = getSupportActionBar();
  152. if (actionBar != null) {
  153. ThemeUtils.setColoredTitle(actionBar, title, this);
  154. if (showSidebar) {
  155. actionBar.setDisplayHomeAsUpEnabled(true);
  156. } else {
  157. setDrawerIndicatorEnabled(false);
  158. }
  159. }
  160. }
  161. @Override
  162. public boolean onOptionsItemSelected(MenuItem item) {
  163. boolean retval;
  164. switch (item.getItemId()) {
  165. case android.R.id.home:
  166. if (showSidebar) {
  167. if (isDrawerOpen()) {
  168. closeDrawer();
  169. } else {
  170. openDrawer();
  171. }
  172. } else {
  173. finish();
  174. }
  175. retval = true;
  176. break;
  177. default:
  178. retval = super.onOptionsItemSelected(item);
  179. break;
  180. }
  181. return retval;
  182. }
  183. @Override
  184. protected void onPostCreate(Bundle savedInstanceState) {
  185. super.onPostCreate(savedInstanceState);
  186. setDrawerMenuItemChecked(menuItemId);
  187. }
  188. public WebView getWebview() {
  189. return this.webview;
  190. }
  191. }