Last active
January 22, 2020 09:20
-
-
Save esabook/619566ce6c576daa81e61642dfec57c6 to your computer and use it in GitHub Desktop.
Multi JS Listener for android webview
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
package ***; | |
import android.net.Uri; | |
import android.webkit.JavascriptInterface; | |
import android.webkit.WebView; | |
import android.widget.Toast; | |
import androidx.annotation.NonNull; | |
import java.util.HashMap; | |
import java.util.Map; | |
/** | |
* EXAMPLE USAGE: | |
* | |
* MobileAppInterface.ActionHander actionHander = new MobileAppInterface.ActionHander() { | |
* @Override | |
* public void run(String query) { | |
* WalletPaymentActivity.this.finish(); | |
* startActivity(new Intent(WalletPaymentActivity.this, TransactionActivity.class)); | |
* } | |
* }; | |
* | |
* MobileAppInterface.injectToWebView(webview); | |
* MobileAppInterface.addNewActionMap("/webview/close", actionHander); | |
* MobileAppInterface.addNewActionMap("/transaction/open", actionHander); | |
*/ | |
public class MobileAppInterface { | |
public static final String ROOT_INTERFACE_NAME_ALT = "Android"; | |
public static final String ROOT_INTERFACE_NAME = "MobileApp"; | |
public static final String KNOWN_SCHEME = "myOwnApplicationName"; | |
public static final String KNOWN_HOST = "mobile.app"; | |
private final static Map<String, ActionHander> actionMaps = new HashMap<>(); | |
private WebView mWebView; | |
public interface ActionHander { | |
void run(String query); | |
} | |
public static void addNewActionMap(@NonNull String path, @NonNull ActionHander handler) { | |
actionMaps.put(path, handler); | |
} | |
public static void removeHandler(String path) { | |
actionMaps.remove(path); | |
} | |
/** | |
* Instantiate the interface and set the context | |
*/ | |
public MobileAppInterface(WebView c) { | |
mWebView = c; | |
} | |
public static void injectToWebView(WebView webView) { | |
MobileAppInterface mobileAppInterface = new MobileAppInterface(webView); | |
webView.addJavascriptInterface(mobileAppInterface, ROOT_INTERFACE_NAME_ALT); | |
webView.addJavascriptInterface(mobileAppInterface, ROOT_INTERFACE_NAME); | |
} | |
/** | |
* Show a toast from the web page | |
*/ | |
@JavascriptInterface | |
public void showToast(String toast) { | |
Toast.makeText(mWebView.getContext(), toast, Toast.LENGTH_SHORT).show(); | |
} | |
@JavascriptInterface | |
public void action(String uri) { | |
Uri webUri = null; | |
try { | |
webUri = Uri.parse(uri); | |
} catch (Exception ignore) { | |
} | |
if (webUri == null) return; | |
handlePathAction(webUri); | |
} | |
void handlePathAction(Uri uri) { | |
if (!KNOWN_SCHEME.equals(uri.getScheme())) return; | |
if (!KNOWN_HOST.equals(uri.getHost())) return; | |
if (actionMaps.containsKey(uri.getPath())) | |
actionMaps.get(uri.getPath()).run(uri.getQuery()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment