ExternalSiteWebView.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 = false;
  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. getWindow().requestFeature(Window.FEATURE_PROGRESS);
  63. super.onCreate(savedInstanceState);
  64. setContentView(R.layout.externalsite_webview);
  65. webview = findViewById(R.id.webView);
  66. final WebSettings webSettings = webview.getSettings();
  67. webview.setFocusable(true);
  68. webview.setFocusableInTouchMode(true);
  69. webview.setClickable(true);
  70. // setup toolbar
  71. setupToolbar();
  72. // setup drawer
  73. setupDrawer(menuItemId);
  74. if (!showSidebar) {
  75. setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
  76. }
  77. ActionBar actionBar = getSupportActionBar();
  78. if (actionBar != null) {
  79. ThemeUtils.setColoredTitle(actionBar, title, this);
  80. actionBar.setDisplayHomeAsUpEnabled(true);
  81. }
  82. // enable zoom
  83. webSettings.setSupportZoom(true);
  84. webSettings.setBuiltInZoomControls(true);
  85. webSettings.setDisplayZoomControls(false);
  86. // Non-responsive webs are zoomed out when loaded
  87. webSettings.setUseWideViewPort(true);
  88. webSettings.setLoadWithOverviewMode(true);
  89. // user agent
  90. webSettings.setUserAgentString(MainApp.getUserAgent());
  91. // no private data storing
  92. webSettings.setSavePassword(false);
  93. webSettings.setSaveFormData(false);
  94. // disable local file access
  95. webSettings.setAllowFileAccess(false);
  96. // enable javascript
  97. webSettings.setJavaScriptEnabled(true);
  98. webSettings.setDomStorageEnabled(true);
  99. final ProgressBar progressBar = findViewById(R.id.progressBar);
  100. webview.setWebChromeClient(new WebChromeClient() {
  101. public void onProgressChanged(WebView view, int progress) {
  102. progressBar.setProgress(progress * 1000);
  103. }
  104. });
  105. webview.setWebViewClient(new WebViewClient() {
  106. public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
  107. InputStream resources = getResources().openRawResource(R.raw.custom_error);
  108. String customError = DisplayUtils.getData(resources);
  109. if (!customError.isEmpty()) {
  110. webview.loadData(customError, "text/html; charset=UTF-8", null);
  111. }
  112. }
  113. });
  114. webview.loadUrl(url);
  115. }
  116. @Override
  117. public boolean onOptionsItemSelected(MenuItem item) {
  118. boolean retval;
  119. switch (item.getItemId()) {
  120. case android.R.id.home:
  121. if (showSidebar) {
  122. if (isDrawerOpen()) {
  123. closeDrawer();
  124. } else {
  125. openDrawer();
  126. }
  127. } else {
  128. Intent settingsIntent = new Intent(getApplicationContext(), Preferences.class);
  129. startActivity(settingsIntent);
  130. }
  131. retval = true;
  132. break;
  133. default:
  134. retval = super.onOptionsItemSelected(item);
  135. break;
  136. }
  137. return retval;
  138. }
  139. @Override
  140. public void showFiles(boolean onDeviceOnly) {
  141. super.showFiles(onDeviceOnly);
  142. Intent fileDisplayActivity = new Intent(getApplicationContext(), FileDisplayActivity.class);
  143. fileDisplayActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  144. startActivity(fileDisplayActivity);
  145. }
  146. @Override
  147. protected void onPostCreate(Bundle savedInstanceState) {
  148. super.onPostCreate(savedInstanceState);
  149. setDrawerMenuItemChecked(menuItemId);
  150. }
  151. }