ExternalSiteWebView.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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.app.Activity;
  23. import android.content.Intent;
  24. import android.os.Bundle;
  25. import android.support.v4.widget.DrawerLayout;
  26. import android.view.MenuItem;
  27. import android.view.Window;
  28. import android.webkit.WebChromeClient;
  29. import android.webkit.WebSettings;
  30. import android.webkit.WebView;
  31. import android.webkit.WebViewClient;
  32. import android.widget.ProgressBar;
  33. import com.owncloud.android.MainApp;
  34. import com.owncloud.android.R;
  35. import com.owncloud.android.lib.common.utils.Log_OC;
  36. import com.owncloud.android.utils.DisplayUtils;
  37. /**
  38. * This activity shows an URL as a web view
  39. */
  40. public class ExternalSiteWebView extends FileActivity {
  41. public static final String EXTRA_TITLE = "TITLE";
  42. public static final String EXTRA_URL = "URL";
  43. public static final String EXTRA_SHOW_SIDEBAR = "SHOW_SIDEBAR";
  44. public static final String EXTRA_MENU_ITEM_ID = "MENU_ITEM_ID";
  45. private static final String TAG = ExternalSiteWebView.class.getSimpleName();
  46. private boolean showSidebar = false;
  47. private int menuItemId;
  48. private WebView webview;
  49. @Override
  50. protected void onCreate(Bundle savedInstanceState) {
  51. Log_OC.v(TAG, "onCreate() start");
  52. Bundle extras = getIntent().getExtras();
  53. String title = extras.getString(EXTRA_TITLE);
  54. String url = extras.getString(EXTRA_URL);
  55. menuItemId = extras.getInt(EXTRA_MENU_ITEM_ID);
  56. showSidebar = extras.getBoolean(EXTRA_SHOW_SIDEBAR);
  57. // show progress
  58. getWindow().requestFeature(Window.FEATURE_PROGRESS);
  59. super.onCreate(savedInstanceState);
  60. setContentView(R.layout.externalsite_webview);
  61. webview = (WebView) findViewById(R.id.webView);
  62. final WebSettings webSettings = webview.getSettings();
  63. webview.setFocusable(true);
  64. webview.setFocusableInTouchMode(true);
  65. webview.setClickable(true);
  66. // setup toolbar
  67. setupToolbar();
  68. // setup drawer
  69. setupDrawer(menuItemId);
  70. if (!showSidebar) {
  71. setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
  72. }
  73. getSupportActionBar().setTitle(title);
  74. getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  75. // enable zoom
  76. webSettings.setSupportZoom(true);
  77. webSettings.setBuiltInZoomControls(true);
  78. webSettings.setDisplayZoomControls(false);
  79. // Non-responsive webs are zoomed out when loaded
  80. webSettings.setUseWideViewPort(true);
  81. webSettings.setLoadWithOverviewMode(true);
  82. // user agent
  83. webSettings.setUserAgentString(MainApp.getUserAgent());
  84. // no private data storing
  85. webSettings.setSavePassword(false);
  86. webSettings.setSaveFormData(false);
  87. // disable local file access
  88. webSettings.setAllowFileAccess(false);
  89. // enable javascript
  90. webview.getSettings().setJavaScriptEnabled(true);
  91. final Activity activity = this;
  92. final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
  93. webview.setWebChromeClient(new WebChromeClient() {
  94. public void onProgressChanged(WebView view, int progress) {
  95. progressBar.setProgress(progress * 1000);
  96. }
  97. });
  98. webview.setWebViewClient(new WebViewClient() {
  99. public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
  100. webview.loadData(DisplayUtils.getData(getResources().openRawResource(R.raw.custom_error)),"text/html; charset=UTF-8", null);
  101. }
  102. });
  103. webview.loadUrl(url);
  104. }
  105. @Override
  106. public boolean onOptionsItemSelected(MenuItem item) {
  107. boolean retval;
  108. switch (item.getItemId()) {
  109. case android.R.id.home:
  110. if (showSidebar) {
  111. if (isDrawerOpen()) {
  112. closeDrawer();
  113. } else {
  114. openDrawer();
  115. }
  116. } else {
  117. Intent settingsIntent = new Intent(getApplicationContext(), Preferences.class);
  118. startActivity(settingsIntent);
  119. }
  120. retval = true;
  121. break;
  122. default:
  123. retval = super.onOptionsItemSelected(item);
  124. break;
  125. }
  126. return retval;
  127. }
  128. @Override
  129. public void showFiles(boolean onDeviceOnly) {
  130. super.showFiles(onDeviceOnly);
  131. Intent fileDisplayActivity = new Intent(getApplicationContext(), FileDisplayActivity.class);
  132. fileDisplayActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  133. startActivity(fileDisplayActivity);
  134. }
  135. @Override
  136. protected void onPostCreate(Bundle savedInstanceState) {
  137. super.onPostCreate(savedInstanceState);
  138. setDrawerMenuItemChecked(menuItemId);
  139. }
  140. }