Search in sources :

Example 11 with JavascriptInterface

use of android.webkit.JavascriptInterface in project AgentWebX5 by Justson.

the class JsBaseInterfaceHolder method checkObject.

@Override
public boolean checkObject(Object v) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1)
        return true;
    if (AgentWebX5Config.WEBVIEW_TYPE == AgentWebX5Config.WEBVIEW_AGENTWEB_SAFE_TYPE)
        return true;
    boolean tag = false;
    Class clazz = v.getClass();
    Method[] mMethods = clazz.getMethods();
    for (Method mMethod : mMethods) {
        Annotation[] mAnnotations = mMethod.getAnnotations();
        for (Annotation mAnnotation : mAnnotations) {
            if (mAnnotation instanceof JavascriptInterface) {
                tag = true;
                break;
            }
        }
        if (tag)
            break;
    }
    return tag;
}
Also used : Method(java.lang.reflect.Method) JavascriptInterface(android.webkit.JavascriptInterface) Annotation(java.lang.annotation.Annotation)

Example 12 with JavascriptInterface

use of android.webkit.JavascriptInterface in project AgentWeb by Justson.

the class SonicJavaScriptInterface method getPerformance.

@JavascriptInterface
public String getPerformance() {
    long clickTime = intent.getLongExtra(PARAM_CLICK_TIME, -1);
    long loadUrlTime = intent.getLongExtra(PARAM_LOAD_URL_TIME, -1);
    try {
        JSONObject result = new JSONObject();
        result.put(PARAM_CLICK_TIME, clickTime);
        result.put(PARAM_LOAD_URL_TIME, loadUrlTime);
        Log.i("Info", "getPerformance");
        return result.toString();
    } catch (Exception e) {
    }
    return "";
}
Also used : JSONObject(org.json.JSONObject) JavascriptInterface(android.webkit.JavascriptInterface)

Example 13 with JavascriptInterface

use of android.webkit.JavascriptInterface in project Gadgetbridge by Freeyourgadget.

the class JSInterface method getAppLocalstoragePrefix.

@JavascriptInterface
public String getAppLocalstoragePrefix() {
    String prefix = device.getAddress() + this.mUuid.toString();
    try {
        MessageDigest digest = MessageDigest.getInstance("SHA-1");
        byte[] bytes = prefix.getBytes("UTF-8");
        digest.update(bytes, 0, bytes.length);
        bytes = digest.digest();
        final StringBuilder sb = new StringBuilder();
        for (byte aByte : bytes) {
            sb.append(String.format("%02X", aByte));
        }
        return sb.toString().toLowerCase();
    } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
        LOG.warn("Error definining local storage prefix", e);
        return prefix;
    }
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest) JavascriptInterface(android.webkit.JavascriptInterface)

Example 14 with JavascriptInterface

use of android.webkit.JavascriptInterface in project Gadgetbridge by Freeyourgadget.

the class JSInterface method sendAppMessage.

@JavascriptInterface
public String sendAppMessage(String msg, String needsTransactionMsg) {
    boolean needsTransaction = "true".equals(needsTransactionMsg);
    LOG.debug("from WEBVIEW: " + msg + " needs a transaction: " + needsTransaction);
    JSONObject knownKeys = PebbleUtils.getAppConfigurationKeys(this.mUuid);
    if (knownKeys == null) {
        LOG.warn("No app configuration keys for: " + mUuid);
        return null;
    }
    try {
        JSONObject in = new JSONObject(msg);
        JSONObject out = new JSONObject();
        String inKey, outKey;
        boolean passKey;
        for (Iterator<String> key = in.keys(); key.hasNext(); ) {
            passKey = false;
            inKey = key.next();
            outKey = null;
            int pebbleAppIndex = knownKeys.optInt(inKey, -1);
            if (pebbleAppIndex != -1) {
                passKey = true;
                outKey = String.valueOf(pebbleAppIndex);
            } else {
                // do not discard integer keys (see https://developer.pebble.com/guides/communication/using-pebblekit-js/ )
                Scanner scanner = new Scanner(inKey);
                if (scanner.hasNextInt() && inKey.equals("" + scanner.nextInt())) {
                    passKey = true;
                    outKey = inKey;
                }
            }
            if (passKey) {
                Object obj = in.get(inKey);
                out.put(outKey, obj);
            } else {
                GB.toast("Discarded key " + inKey + ", not found in the local configuration and is not an integer key.", Toast.LENGTH_SHORT, GB.WARN);
            }
        }
        LOG.info("WEBVIEW message to pebble: " + out.toString());
        if (needsTransaction) {
            this.lastTransaction++;
            GBApplication.deviceService().onAppConfiguration(this.mUuid, out.toString(), this.lastTransaction);
            return this.lastTransaction.toString();
        } else {
            GBApplication.deviceService().onAppConfiguration(this.mUuid, out.toString(), null);
        }
    } catch (JSONException e) {
        LOG.warn("Error building the appmessage JSON object", e);
    }
    return null;
}
Also used : Scanner(java.util.Scanner) JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) JavascriptInterface(android.webkit.JavascriptInterface)

Example 15 with JavascriptInterface

use of android.webkit.JavascriptInterface in project Gadgetbridge by Freeyourgadget.

the class JSInterface method getActiveWatchInfo.

@JavascriptInterface
public String getActiveWatchInfo() {
    JSONObject wi = new JSONObject();
    try {
        wi.put("firmware", device.getFirmwareVersion());
        wi.put("platform", PebbleUtils.getPlatformName(device.getModel()));
        wi.put("model", PebbleUtils.getModel(device.getModel()));
        // TODO: use real info
        wi.put("language", "en");
    } catch (JSONException e) {
        LOG.warn("Error building the ActiveWathcInfo JSON object", e);
    }
    // Json not supported apparently, we need to cast back and forth
    return wi.toString();
}
Also used : JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) JavascriptInterface(android.webkit.JavascriptInterface)

Aggregations

JavascriptInterface (android.webkit.JavascriptInterface)32 JSONObject (org.json.JSONObject)9 JSONException (org.json.JSONException)7 Intent (android.content.Intent)4 WebView (android.webkit.WebView)4 WebViewClient (android.webkit.WebViewClient)4 IOException (java.io.IOException)4 Annotation (java.lang.annotation.Annotation)4 Method (java.lang.reflect.Method)4 File (java.io.File)3 SharedPreferences (android.content.SharedPreferences)2 IBaseForumPost (forpdateam.ru.forpda.api.IBaseForumPost)2 HashMap (java.util.HashMap)2 SuppressLint (android.annotation.SuppressLint)1 Bitmap (android.graphics.Bitmap)1 Uri (android.net.Uri)1 SslError (android.net.http.SslError)1 Bundle (android.os.Bundle)1 AlertDialog (android.support.v7.app.AlertDialog)1 JsPromptResult (android.webkit.JsPromptResult)1