ExternalSiteWebView.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /**
  2. * Nextcloud Android client application
  3. *
  4. * @author Tobias Kaminsky
  5. * Copyright (C) 2017 Tobias Kaminsky
  6. * Copyright (C) 2017 Nextcloud GmbH.
  7. * <p>
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero 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. * <p>
  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 Affero General Public License for more details.
  17. * <p>
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. package com.owncloud.android.ui.activity;
  22. import android.annotation.SuppressLint;
  23. import android.content.Intent;
  24. import android.os.Bundle;
  25. import android.support.v4.widget.DrawerLayout;
  26. import android.support.v7.app.ActionBar;
  27. import android.view.MenuItem;
  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. /**
  41. * This activity shows an URL as a web view
  42. */
  43. public class ExternalSiteWebView extends FileActivity {
  44. public static final String EXTRA_TITLE = "TITLE";
  45. public static final String EXTRA_URL = "URL";
  46. public static final String EXTRA_SHOW_SIDEBAR = "SHOW_SIDEBAR";
  47. public static final String EXTRA_MENU_ITEM_ID = "MENU_ITEM_ID";
  48. private static final String TAG = ExternalSiteWebView.class.getSimpleName();
  49. private boolean showSidebar;
  50. private int menuItemId;
  51. private WebView webview;
  52. @SuppressLint("SetJavaScriptEnabled")
  53. @Override
  54. protected void onCreate(Bundle savedInstanceState) {
  55. Log_OC.v(TAG, "onCreate() start");
  56. Bundle extras = getIntent().getExtras();
  57. String title = extras.getString(EXTRA_TITLE);
  58. String url = extras.getString(EXTRA_URL);
  59. menuItemId = extras.getInt(EXTRA_MENU_ITEM_ID);
  60. showSidebar = extras.getBoolean(EXTRA_SHOW_SIDEBAR);
  61. // show progress
  62. Window window = getWindow();
  63. if (window != null) {
  64. window.requestFeature(Window.FEATURE_PROGRESS);
  65. }
  66. super.onCreate(savedInstanceState);
  67. setContentView(R.layout.externalsite_webview);
  68. webview = findViewById(R.id.webView);
  69. final WebSettings webSettings = webview.getSettings();
  70. webview.setFocusable(true);
  71. webview.setFocusableInTouchMode(true);
  72. webview.setClickable(true);
  73. // setup toolbar
  74. setupToolbar();
  75. // setup drawer
  76. setupDrawer(menuItemId);
  77. if (!showSidebar) {
  78. setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
  79. }
  80. ActionBar actionBar = getSupportActionBar();
  81. if (actionBar != null) {
  82. ThemeUtils.setColoredTitle(actionBar, title, this);
  83. if (showSidebar) {
  84. actionBar.setDisplayHomeAsUpEnabled(true);
  85. } else {
  86. setDrawerIndicatorEnabled(false);
  87. }
  88. }
  89. // enable zoom
  90. webSettings.setSupportZoom(true);
  91. webSettings.setBuiltInZoomControls(true);
  92. webSettings.setDisplayZoomControls(false);
  93. // Non-responsive webs are zoomed out when loaded
  94. webSettings.setUseWideViewPort(true);
  95. webSettings.setLoadWithOverviewMode(true);
  96. // user agent
  97. webSettings.setUserAgentString(MainApp.getUserAgent());
  98. // no private data storing
  99. webSettings.setSavePassword(false);
  100. webSettings.setSaveFormData(false);
  101. // disable local file access
  102. webSettings.setAllowFileAccess(false);
  103. // enable javascript
  104. webSettings.setJavaScriptEnabled(true);
  105. webSettings.setDomStorageEnabled(true);
  106. final ProgressBar progressBar = findViewById(R.id.progressBar);
  107. webview.setWebChromeClient(new WebChromeClient() {
  108. public void onProgressChanged(WebView view, int progress) {
  109. progressBar.setProgress(progress * 1000);
  110. }
  111. });
  112. webview.setWebViewClient(new WebViewClient() {
  113. public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
  114. InputStream resources = getResources().openRawResource(R.raw.custom_error);
  115. String customError = DisplayUtils.getData(resources);
  116. if (!customError.isEmpty()) {
  117. webview.loadData(customError, "text/html; charset=UTF-8", null);
  118. }
  119. }
  120. });
  121. webview.loadUrl(url);
  122. }
  123. @Override
  124. public boolean onOptionsItemSelected(MenuItem item) {
  125. boolean retval;
  126. switch (item.getItemId()) {
  127. case android.R.id.home:
  128. if (showSidebar) {
  129. if (isDrawerOpen()) {
  130. closeDrawer();
  131. } else {
  132. openDrawer();
  133. }
  134. } else {
  135. finish();
  136. }
  137. retval = true;
  138. break;
  139. default:
  140. retval = super.onOptionsItemSelected(item);
  141. break;
  142. }
  143. return retval;
  144. }
  145. @Override
  146. public void showFiles(boolean onDeviceOnly) {
  147. super.showFiles(onDeviceOnly);
  148. Intent fileDisplayActivity = new Intent(getApplicationContext(), FileDisplayActivity.class);
  149. fileDisplayActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  150. startActivity(fileDisplayActivity);
  151. }
  152. @Override
  153. protected void onPostCreate(Bundle savedInstanceState) {
  154. super.onPostCreate(savedInstanceState);
  155. setDrawerMenuItemChecked(menuItemId);
  156. }
  157. }