Last active
September 15, 2022 19:54
-
-
Save jldubz/36f376f6c513cca39260 to your computer and use it in GitHub Desktop.
Web Chrome Client for Android. Wraps HTML pages for interaction with Native Android functions. Offers locked down wrapper so external URLs don't load.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by Jon-Luke.West on 4/6/2015. | |
*/ | |
public class FullscreenWebChromeClient extends WebChromeClient { | |
private View mVideoProgressView; | |
private View mCustomView; | |
private WebView webView; | |
private FrameLayout customViewContainer; | |
private WebChromeClient.CustomViewCallback customViewCallback; | |
public FullscreenWebChromeClient(WebView webView, FrameLayout viewContainer) { | |
super(); | |
this.webView = webView; | |
customViewContainer = viewContainer; | |
} | |
@Override | |
public void onShowCustomView(View view, CustomViewCallback callback) { | |
// if a view already exists then immediately terminate the new one | |
if (mCustomView != null) { | |
callback.onCustomViewHidden(); | |
return; | |
} | |
mCustomView = view; | |
webView.setVisibility(View.GONE); | |
customViewContainer.setVisibility(View.VISIBLE); | |
customViewContainer.addView(view); | |
customViewCallback = callback; | |
} | |
@Override | |
public void onHideCustomView() { | |
super.onHideCustomView(); //To change body of overridden methods use File | Settings | File Templates. | |
if (mCustomView == null) | |
return; | |
webView.setVisibility(View.VISIBLE); | |
customViewContainer.setVisibility(View.GONE); | |
// Hide the custom view. | |
mCustomView.setVisibility(View.GONE); | |
// Remove the custom view from its container. | |
customViewContainer.removeView(mCustomView); | |
customViewCallback.onCustomViewHidden(); | |
mCustomView = null; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by Jon-Luke.West on 4/6/2015. | |
*/ | |
public class LockedWebViewClient extends WebViewClient { | |
@Override | |
public boolean shouldOverrideUrlLoading(WebView view, String url) { | |
return true; //This object will handle the request and do nothing with it | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by Jon-Luke.West on 3/4/2016. | |
*/ | |
public class WebAppInterface { | |
Context mContext; | |
/** Instantiate the interface and set the context */ | |
WebAppInterface(Context c) { | |
mContext = c; | |
} | |
/** Show a toast from the web page */ | |
@JavascriptInterface | |
public void testToast() { | |
Toast.makeText(mContext, "Connection Successful", Toast.LENGTH_SHORT).show(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by Jon-Luke.West on 4/6/2015. | |
*/ | |
public class WebFragment extends Fragment { | |
private WebView webContent; | |
private String htmlCode = null; | |
public WebFragment() { | |
} | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | |
//Inflate the layout | |
View rootView = inflater.inflate(R.layout.webwrapper_main, container, false); | |
//Get handles to the fullscreenFrame and WebView container | |
FrameLayout viewContainer = (FrameLayout) rootView.findViewById(R.id.fullscreenFrame); | |
webContent = (WebView) rootView.findViewById(R.id.web_content); | |
//Makes Javascript work | |
WebSettings settings = webContent.getSettings(); | |
settings.setJavaScriptEnabled(true); | |
settings.setDomStorageEnabled(true); | |
//Set JavaScriptInterface | |
webContent.addJavascriptInterface(new WebAppInterface(getActivity().getApplicationContext()), "Android"); | |
//Handles fullscreen video | |
webContent.setWebChromeClient(new FullscreenWebChromeClient(webContent,viewContainer)); | |
//Handles overriding links to external web content | |
webContent.setWebViewClient(new LockedWebViewClient()); | |
//Hides the scroll bars | |
webContent.setVerticalScrollBarEnabled(false); | |
webContent.setHorizontalScrollBarEnabled(false); | |
Bundle bundle = this.getArguments(); | |
if (bundle != null) { | |
htmlCode = bundle.getString("com.jldubz.webwrapper.html"); | |
if (htmlCode != null) { | |
webContent.loadUrl(htmlCode); | |
} else { | |
webContent.loadUrl("about:blank"); | |
} | |
} else { | |
webContent.loadUrl("about:blank"); | |
} | |
return rootView; | |
} | |
@Override | |
public void onPause() { | |
//Kills all WebView threads to avoid resource consumption while the activity is not running | |
webContent.loadUrl("about:blank"); | |
super.onPause(); | |
} | |
@Override | |
public void onResume() { | |
if (htmlCode != null) { | |
webContent.loadUrl(htmlCode); | |
} | |
super.onResume(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
test