crolloz

VideoEnabledWebView

Aug 5th, 2020
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. package tagalog.moviex;
  2.  
  3. import android.annotation.*;
  4. import android.content.*;
  5. import android.os.*;
  6. import android.util.*;
  7. import android.webkit.*;
  8. import android.widget.*;
  9. import java.util.*;
  10.  
  11. /**
  12. * This class serves as a WebView to be used in conjunction with a VideoEnabledWebChromeClient.
  13. * It makes possible:
  14. * - To detect the HTML5 video ended event so that the VideoEnabledWebChromeClient can exit full-screen.
  15. *
  16. * Important notes:
  17. * - Javascript is enabled by default and must not be disabled with getSettings().setJavaScriptEnabled(false).
  18. * - setWebChromeClient() must be called before any loadData(), loadDataWithBaseURL() or loadUrl() method.
  19. *
  20. * @author Cristian Perez (http://cpr.name)
  21. *
  22. */
  23. public class VideoEnabledWebView extends WebView
  24. {
  25.  
  26. public class JavascriptInterface
  27. {
  28. @android.webkit.JavascriptInterface
  29. public void notifyVideoEnd() // Must match Javascript interface method of VideoEnabledWebChromeClient
  30. {
  31. // This code is not executed in the UI thread, so we must force that to happen
  32. new Handler(Looper.getMainLooper()).post(new Runnable()
  33. {
  34. @Override
  35. public void run()
  36. {
  37. if (videoEnabledWebChromeClient != null)
  38. {
  39. videoEnabledWebChromeClient.onHideCustomView();
  40. }
  41. }
  42. });
  43. }
  44. }
  45.  
  46. private VideoEnabledWebChromeClient videoEnabledWebChromeClient;
  47. private boolean addedJavascriptInterface;
  48.  
  49. public VideoEnabledWebView(Context context)
  50. {
  51. super(context);
  52. addedJavascriptInterface = false;
  53. }
  54.  
  55. @SuppressWarnings("unused")
  56. public VideoEnabledWebView(Context context, AttributeSet attrs)
  57. {
  58. super(context, attrs);
  59. addedJavascriptInterface = false;
  60. }
  61.  
  62. @SuppressWarnings("unused")
  63. public VideoEnabledWebView(Context context, AttributeSet attrs, int defStyle)
  64. {
  65. super(context, attrs, defStyle);
  66. addedJavascriptInterface = false;
  67. }
  68.  
  69. /**
  70. * Indicates if the video is being displayed using a custom view (typically full-screen)
  71. * @return true it the video is being displayed using a custom view (typically full-screen)
  72. */
  73. public boolean isVideoFullscreen()
  74. {
  75. return videoEnabledWebChromeClient != null && videoEnabledWebChromeClient.isVideoFullscreen();
  76. }
  77.  
  78. /**
  79. * Pass only a VideoEnabledWebChromeClient instance.
  80. */
  81. @Override @SuppressLint("SetJavaScriptEnabled")
  82. public void setWebChromeClient(WebChromeClient client)
  83. {
  84. getSettings().setJavaScriptEnabled(true);
  85.  
  86. if (client instanceof VideoEnabledWebChromeClient)
  87. {
  88. this.videoEnabledWebChromeClient = (VideoEnabledWebChromeClient) client;
  89. }
  90.  
  91. super.setWebChromeClient(client);
  92. }
  93.  
  94. @Override
  95. public void loadData(String data, String mimeType, String encoding)
  96. {
  97. addJavascriptInterface();
  98. super.loadData(data, mimeType, encoding);
  99. }
  100.  
  101. @Override
  102. public void loadDataWithBaseURL(String baseUrl, String data, String mimeType, String encoding, String historyUrl)
  103. {
  104. addJavascriptInterface();
  105. super.loadDataWithBaseURL(baseUrl, data, mimeType, encoding, historyUrl);
  106. }
  107.  
  108. @Override
  109. public void loadUrl(String url)
  110. {
  111. addJavascriptInterface();
  112. super.loadUrl(url);
  113.  
  114. }
  115.  
  116.  
  117. @Override
  118. public void loadUrl(String url, Map<String, String> additionalHttpHeaders)
  119. {
  120. addJavascriptInterface();
  121. super.loadUrl(url, additionalHttpHeaders);
  122. }
  123.  
  124. private void addJavascriptInterface()
  125. {
  126. if (!addedJavascriptInterface)
  127. {
  128. // Add javascript interface to be called when the video ends (must be done before page load)
  129. addJavascriptInterface(new JavascriptInterface(), "_VideoEnabledWebView"); // Must match Javascript interface name of VideoEnabledWebChromeClient
  130.  
  131. addedJavascriptInterface = true;
  132. }
  133. }
  134.  
  135. }
Add Comment
Please, Sign In to add comment