Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

SystemWebChromeClient.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the License is distributed on an
  12. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, either express or implied. See the License for the
  14. specific language governing permissions and limitations
  15. under the License.
  16. */
  17. package org.apache.cordova.engine;
  18. import java.util.Arrays;
  19. import android.annotation.TargetApi;
  20. import android.app.Activity;
  21. import android.content.Context;
  22. import android.content.ActivityNotFoundException;
  23. import android.content.Intent;
  24. import android.net.Uri;
  25. import android.os.Build;
  26. import android.view.Gravity;
  27. import android.view.View;
  28. import android.view.ViewGroup.LayoutParams;
  29. import android.webkit.ConsoleMessage;
  30. import android.webkit.GeolocationPermissions.Callback;
  31. import android.webkit.JsPromptResult;
  32. import android.webkit.JsResult;
  33. import android.webkit.ValueCallback;
  34. import android.webkit.WebChromeClient;
  35. import android.webkit.WebStorage;
  36. import android.webkit.WebView;
  37. import android.webkit.PermissionRequest;
  38. import android.widget.LinearLayout;
  39. import android.widget.ProgressBar;
  40. import android.widget.RelativeLayout;
  41. import org.apache.cordova.CordovaDialogsHelper;
  42. import org.apache.cordova.CordovaPlugin;
  43. import org.apache.cordova.LOG;
  44. /**
  45. * This class is the WebChromeClient that implements callbacks for our web view.
  46. * The kind of callbacks that happen here are on the chrome outside the document,
  47. * such as onCreateWindow(), onConsoleMessage(), onProgressChanged(), etc. Related
  48. * to but different than CordovaWebViewClient.
  49. */
  50. public class SystemWebChromeClient extends WebChromeClient {
  51. private static final int FILECHOOSER_RESULTCODE = 5173;
  52. private static final String LOG_TAG = "SystemWebChromeClient";
  53. private long MAX_QUOTA = 100 * 1024 * 1024;
  54. protected final SystemWebViewEngine parentEngine;
  55. // the video progress view
  56. private View mVideoProgressView;
  57. private CordovaDialogsHelper dialogsHelper;
  58. private Context appContext;
  59. private WebChromeClient.CustomViewCallback mCustomViewCallback;
  60. private View mCustomView;
  61. public SystemWebChromeClient(SystemWebViewEngine parentEngine) {
  62. this.parentEngine = parentEngine;
  63. appContext = parentEngine.webView.getContext();
  64. dialogsHelper = new CordovaDialogsHelper(appContext);
  65. }
  66. /**
  67. * Tell the client to display a javascript alert dialog.
  68. */
  69. @Override
  70. public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
  71. dialogsHelper.showAlert(message, new CordovaDialogsHelper.Result() {
  72. @Override public void gotResult(boolean success, String value) {
  73. if (success) {
  74. result.confirm();
  75. } else {
  76. result.cancel();
  77. }
  78. }
  79. });
  80. return true;
  81. }
  82. /**
  83. * Tell the client to display a confirm dialog to the user.
  84. */
  85. @Override
  86. public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {
  87. dialogsHelper.showConfirm(message, new CordovaDialogsHelper.Result() {
  88. @Override
  89. public void gotResult(boolean success, String value) {
  90. if (success) {
  91. result.confirm();
  92. } else {
  93. result.cancel();
  94. }
  95. }
  96. });
  97. return true;
  98. }
  99. /**
  100. * Tell the client to display a prompt dialog to the user.
  101. * If the client returns true, WebView will assume that the client will
  102. * handle the prompt dialog and call the appropriate JsPromptResult method.
  103. *
  104. * Since we are hacking prompts for our own purposes, we should not be using them for
  105. * this purpose, perhaps we should hack console.log to do this instead!
  106. */
  107. @Override
  108. public boolean onJsPrompt(WebView view, String origin, String message, String defaultValue, final JsPromptResult result) {
  109. // Unlike the @JavascriptInterface bridge, this method is always called on the UI thread.
  110. String handledRet = parentEngine.bridge.promptOnJsPrompt(origin, message, defaultValue);
  111. if (handledRet != null) {
  112. result.confirm(handledRet);
  113. } else {
  114. dialogsHelper.showPrompt(message, defaultValue, new CordovaDialogsHelper.Result() {
  115. @Override
  116. public void gotResult(boolean success, String value) {
  117. if (success) {
  118. result.confirm(value);
  119. } else {
  120. result.cancel();
  121. }
  122. }
  123. });
  124. }
  125. return true;
  126. }
  127. /**
  128. * Handle database quota exceeded notification.
  129. */
  130. @Override
  131. @SuppressWarnings("deprecation")
  132. public void onExceededDatabaseQuota(String url, String databaseIdentifier, long currentQuota, long estimatedSize,
  133. long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater)
  134. {
  135. LOG.d(LOG_TAG, "onExceededDatabaseQuota estimatedSize: %d currentQuota: %d totalUsedQuota: %d", estimatedSize, currentQuota, totalUsedQuota);
  136. quotaUpdater.updateQuota(MAX_QUOTA);
  137. }
  138. @Override
  139. public boolean onConsoleMessage(ConsoleMessage consoleMessage)
  140. {
  141. if (consoleMessage.message() != null)
  142. LOG.d(LOG_TAG, "%s: Line %d : %s" , consoleMessage.sourceId() , consoleMessage.lineNumber(), consoleMessage.message());
  143. return super.onConsoleMessage(consoleMessage);
  144. }
  145. @Override
  146. /**
  147. * Instructs the client to show a prompt to ask the user to set the Geolocation permission state for the specified origin.
  148. *
  149. * This also checks for the Geolocation Plugin and requests permission from the application to use Geolocation.
  150. *
  151. * @param origin
  152. * @param callback
  153. */
  154. public void onGeolocationPermissionsShowPrompt(String origin, Callback callback) {
  155. super.onGeolocationPermissionsShowPrompt(origin, callback);
  156. callback.invoke(origin, true, false);
  157. //Get the plugin, it should be loaded
  158. CordovaPlugin geolocation = parentEngine.pluginManager.getPlugin("Geolocation");
  159. if(geolocation != null && !geolocation.hasPermisssion())
  160. {
  161. geolocation.requestPermissions(0);
  162. }
  163. }
  164. // API level 7 is required for this, see if we could lower this using something else
  165. @Override
  166. @SuppressWarnings("deprecation")
  167. public void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback) {
  168. parentEngine.getCordovaWebView().showCustomView(view, callback);
  169. }
  170. @Override
  171. @SuppressWarnings("deprecation")
  172. public void onHideCustomView() {
  173. parentEngine.getCordovaWebView().hideCustomView();
  174. }
  175. @Override
  176. /**
  177. * Ask the host application for a custom progress view to show while
  178. * a <video> is loading.
  179. * @return View The progress view.
  180. */
  181. public View getVideoLoadingProgressView() {
  182. if (mVideoProgressView == null) {
  183. // Create a new Loading view programmatically.
  184. // create the linear layout
  185. LinearLayout layout = new LinearLayout(parentEngine.getView().getContext());
  186. layout.setOrientation(LinearLayout.VERTICAL);
  187. RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
  188. layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
  189. layout.setLayoutParams(layoutParams);
  190. // the proress bar
  191. ProgressBar bar = new ProgressBar(parentEngine.getView().getContext());
  192. LinearLayout.LayoutParams barLayoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
  193. barLayoutParams.gravity = Gravity.CENTER;
  194. bar.setLayoutParams(barLayoutParams);
  195. layout.addView(bar);
  196. mVideoProgressView = layout;
  197. }
  198. return mVideoProgressView;
  199. }
  200. // <input type=file> support:
  201. // openFileChooser() is for pre KitKat and in KitKat mr1 (it's known broken in KitKat).
  202. // For Lollipop, we use onShowFileChooser().
  203. public void openFileChooser(ValueCallback<Uri> uploadMsg) {
  204. this.openFileChooser(uploadMsg, "*/*");
  205. }
  206. public void openFileChooser( ValueCallback<Uri> uploadMsg, String acceptType ) {
  207. this.openFileChooser(uploadMsg, acceptType, null);
  208. }
  209. public void openFileChooser(final ValueCallback<Uri> uploadMsg, String acceptType, String capture)
  210. {
  211. Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
  212. intent.addCategory(Intent.CATEGORY_OPENABLE);
  213. intent.setType("*/*");
  214. parentEngine.cordova.startActivityForResult(new CordovaPlugin() {
  215. @Override
  216. public void onActivityResult(int requestCode, int resultCode, Intent intent) {
  217. Uri result = intent == null || resultCode != Activity.RESULT_OK ? null : intent.getData();
  218. LOG.d(LOG_TAG, "Receive file chooser URL: " + result);
  219. uploadMsg.onReceiveValue(result);
  220. }
  221. }, intent, FILECHOOSER_RESULTCODE);
  222. }
  223. @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  224. @Override
  225. public boolean onShowFileChooser(WebView webView, final ValueCallback<Uri[]> filePathsCallback, final WebChromeClient.FileChooserParams fileChooserParams) {
  226. // Check if multiple-select is specified
  227. Boolean selectMultiple = false;
  228. if (fileChooserParams.getMode() == WebChromeClient.FileChooserParams.MODE_OPEN_MULTIPLE) {
  229. selectMultiple = true;
  230. }
  231. Intent intent = fileChooserParams.createIntent();
  232. intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, selectMultiple);
  233. try {
  234. parentEngine.cordova.startActivityForResult(new CordovaPlugin() {
  235. @Override
  236. public void onActivityResult(int requestCode, int resultCode, Intent intent) {
  237. Uri[] result = null;
  238. if (resultCode == Activity.RESULT_OK && intent != null) {
  239. if (intent.getClipData() != null) {
  240. // handle multiple-selected files
  241. final int numSelectedFiles = intent.getClipData().getItemCount();
  242. result = new Uri[numSelectedFiles];
  243. for (int i = 0; i < numSelectedFiles; i++) {
  244. result[i] = intent.getClipData().getItemAt(i).getUri();
  245. LOG.d(LOG_TAG, "Receive file chooser URL: " + result[i]);
  246. }
  247. }
  248. else if (intent.getData() != null) {
  249. // handle single-selected file
  250. result = WebChromeClient.FileChooserParams.parseResult(resultCode, intent);
  251. LOG.d(LOG_TAG, "Receive file chooser URL: " + result);
  252. }
  253. }
  254. filePathsCallback.onReceiveValue(result);
  255. }
  256. }, intent, FILECHOOSER_RESULTCODE);
  257. } catch (ActivityNotFoundException e) {
  258. LOG.w("No activity found to handle file chooser intent.", e);
  259. filePathsCallback.onReceiveValue(null);
  260. }
  261. return true;
  262. }
  263. @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  264. @Override
  265. public void onPermissionRequest(final PermissionRequest request) {
  266. LOG.d(LOG_TAG, "onPermissionRequest: " + Arrays.toString(request.getResources()));
  267. request.grant(request.getResources());
  268. }
  269. public void destroyLastDialog(){
  270. dialogsHelper.destroyLastDialog();
  271. }
  272. }