Android Open Source - kirin-for-android Kirin Helper






From Project

Back to project page kirin-for-android.

License

The source code is released under:

Apache License

If you think the Android project kirin-for-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.futureplatforms.kirin.helpers;
/*ww  w.j a  v  a2 s.  co m*/
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import android.text.TextUtils;

import com.futureplatforms.kirin.internal.JSONUtils;
import com.futureplatforms.kirin.internal.attic.ProxyGenerator;
import com.futureplatforms.kirin.internal.core.IJsContext;
import com.futureplatforms.kirin.internal.core.IKirinState;
import com.futureplatforms.kirin.internal.core.INativeContext;
import com.futureplatforms.kirin.internal.core.JsCommands;
import com.futureplatforms.kirin.internal.core.NativeContext.SettableFuture;
import com.futureplatforms.kirin.state.IKirinDropbox;
import com.futureplatforms.kirin.state.IKirinFileSystem;

public class KirinHelper implements IKirinHelper {

  private final String mModuleName;
  private final Object mNativeObject;
  private final INativeContext mNativeContext;
  private final IJsContext mJsContext;
  private final IKirinState mAppState;
  private final ProxyGenerator mProxyGenerator;
  
  public KirinHelper(Object nativeObject, String moduleName, IJsContext jsContext, INativeContext nativeContext, IKirinState appState) {
    super();
    mNativeObject = nativeObject;
    mModuleName = moduleName;
    
    mNativeContext = nativeContext;
    mJsContext = jsContext;
    
    mAppState = appState;
    
    mProxyGenerator = new ProxyGenerator(this);
  }
  
  @Override
  public void jsMethod(String methodName, Object... args) {
    if (args == null || args.length == 0) {      
      mJsContext.js(MessageFormat.format(JsCommands.EXECUTE_METHOD_JS, getModuleName(), methodName, null));
    } else {
      String argsList = prepareArgs(args);
      mJsContext.js(MessageFormat.format(JsCommands.EXECUTE_METHOD_JS_WITH_ARGS, getModuleName(), 
          methodName, argsList, null));
    }
  }

  public <T> T jsSyncMethod(Class<T> returnType, String methodName, Object... args) {
    Long id = mNativeContext.createNewId();
    SettableFuture<T> future = mNativeContext.getFuture(id);
    
    if (args == null || args.length == 0) {      
      mJsContext.js(MessageFormat.format(JsCommands.EXECUTE_METHOD_JS, getModuleName(), methodName, Long.toString(id)));
    } else {
      String argsList = prepareArgs(args);
      mJsContext.js(MessageFormat.format(JsCommands.EXECUTE_METHOD_JS_WITH_ARGS, getModuleName(), 
          methodName, argsList, Long.toString(id)));
    }
    return future.get();
  }
  
  private String prepareArgs(Object... args) {
    for (int i=0, length=args.length; i<length; i++) {
      Object arg = args[i];
      if (arg instanceof String) {
        arg = "'" + JSONUtils.escapeJavaScript((String)arg) + "'";
      } else if (arg instanceof Integer) {
        arg = Integer.toString((Integer) arg);
      } else if (arg instanceof Long) {
        arg = Long.toString((Long) arg);
      } else if (arg instanceof Map<?,?>) {
        arg = new JSONObject((Map<?,?>) arg);
      } else if (arg instanceof Collection) {
        arg = new JSONArray((Collection<?>) arg);
      }
      args[i] = arg;
    }
    
    return TextUtils.join(",", args);
  }

  
  @SuppressWarnings("deprecation")
  @Override
  public void jsCallback(String callbackId, Object... args) {
    if (args == null || args.length == 0) {      
      mJsContext.js(MessageFormat.format(JsCommands.EXECUTE_CALLBACK_JS, callbackId));
    } else {
      String argsList = prepareArgs(args);
      mJsContext.js(MessageFormat.format(JsCommands.EXECUTE_CALLBACK_WITH_ARGS_JS, callbackId, argsList));
    }
  }

  @Override
  public void jsCallback(JSONObject config, String callbackName,
      Object... args) {
    String callbackId = config.optString(callbackName);
    if (callbackId != null) {
      this.jsCallback(callbackId, (Object[]) args);
    }
  }

  @Override
  public void cleanupCallback(String... callbackIds) {
    if (callbackIds != null && callbackIds.length > 0) {
      cleanupCallbacks(TextUtils.join("','", callbackIds));
    }
  }

  @Override
  public void cleanupCallback(JSONObject config, String... callbackNames) {
    List<String> callbackIds = new ArrayList<String>();
    for (String callbackName : callbackNames) {
      Object callbackId = config.opt(callbackName);
      if (callbackId != null && callbackId instanceof String) {
        callbackIds.add((String) callbackId);
      }
    }
    if (!callbackIds.isEmpty()) {
      cleanupCallbacks(TextUtils.join("','",callbackIds));
    }
  }

  @Override
  public void jsCallbackObjectMethod(String objectId, String methodName, Object... args) {
    // TODO test this.
    if (args == null || args.length == 0) {      
      mJsContext.js(MessageFormat.format(JsCommands.EXECUTE_CALLBACK_METHOD_JS, objectId, methodName, null));
    } else {
      String argsList = prepareArgs(args);
      mJsContext.js(MessageFormat.format(JsCommands.EXECUTE_CALLBACK_METHOD_WITH_ARGS_JS, objectId, methodName, argsList, null));
    }
  }
  
  @Override
  public <T> T jsSyncCallbackObjectMethod(String objectId, Class<T> returnType, String methodName, Object... args) {
    // TODO test this.
    Long id = mNativeContext.createNewId();
    SettableFuture<T> future = mNativeContext.getFuture(id);
    
    if (args == null || args.length == 0) {      
      mJsContext.js(MessageFormat.format(JsCommands.EXECUTE_CALLBACK_METHOD_JS, objectId, methodName, Long.toString(id)));
    } else {
      String argsList = prepareArgs(args);
      mJsContext.js(MessageFormat.format(JsCommands.EXECUTE_CALLBACK_METHOD_WITH_ARGS_JS, objectId, methodName, argsList, Long.toString(id)));
    }
    
    return future.get();
  }
  
  @SuppressWarnings("deprecation")
  private void cleanupCallbacks(String argsList) {
    mJsContext.js(MessageFormat.format(JsCommands.DELETE_CALLBACK_JS, argsList));
  }

  @Override
  public IKirinDropbox getDropbox() {
    return getAppState().getDropbox();
  }

  @Override
  public void onLoad() {
    // let the object be available for calling from Javascript.
    mNativeContext.registerNativeObject(getModuleName(), getNativeObject(), mProxyGenerator);
    
    // and now tell Javascript the method names so it can construct a proxy for us.
    Collection<String> methods = mNativeContext.getMethodNamesForObject(getModuleName());
    mJsContext.js(MessageFormat.format(JsCommands.REGISTER_MODULE_WITH_METHODS, getModuleName(), TextUtils.join("','", methods)));
  }

  @Override
  public void onUnload() {
    // this will call the onUnload method in the module.
    mJsContext.js(MessageFormat.format(JsCommands.UNREGISTER_MODULE, getModuleName()));
    mNativeContext.unregisterNativeObject(getModuleName());
  }

  protected IKirinState getAppState() {
    return mAppState;
  }

  public String getModuleName() {
    return mModuleName;
  }

  protected Object getNativeObject() {
    return mNativeObject;
  }

  @Override
  public IKirinFileSystem getFileSystem() {
    return mAppState.getPaths();
  }

  @Override
  public <T> T javascriptProxyForModule(Class<T> interfaceClass) {
    return mProxyGenerator.javascriptProxyForModule(interfaceClass);
  }
  
  @Override
  public <T> T javascriptProxyForValueObject(JSONObject obj, Class<T> interfaceClass) {
    return mProxyGenerator.javascriptProxyForResponse(obj, interfaceClass);
  }
}




Java Source Code List

com.futureplatforms.kirin.C.java
com.futureplatforms.kirin.Kirin.java
com.futureplatforms.kirin.TestingApplication.java
com.futureplatforms.kirin.activities.KirinActivity.java
com.futureplatforms.kirin.activities.KirinListActivity.java
com.futureplatforms.kirin.application.IKirinApplication.java
com.futureplatforms.kirin.application.KirinApplication.java
com.futureplatforms.kirin.extensions.IKirinExtensionOnNonDefaultThread.java
com.futureplatforms.kirin.extensions.IKirinExtensionOnUiThread.java
com.futureplatforms.kirin.extensions.IKirinExtension.java
com.futureplatforms.kirin.extensions.IProguardImmunity.java
com.futureplatforms.kirin.extensions.KirinExtensionAdapter.java
com.futureplatforms.kirin.extensions.KirinExtensions.java
com.futureplatforms.kirin.extensions.databases.DBStatement.java
com.futureplatforms.kirin.extensions.databases.DBTransaction.java
com.futureplatforms.kirin.extensions.databases.DatabasesBackend.java
com.futureplatforms.kirin.extensions.databases.IDatabasesBackend.java
com.futureplatforms.kirin.extensions.fs.KirinFilesystemExtensionImpl.java
com.futureplatforms.kirin.extensions.localnotifications.ILocalNotificationsBackend.java
com.futureplatforms.kirin.extensions.localnotifications.LocalNotificationAlarmReceiver.java
com.futureplatforms.kirin.extensions.localnotifications.LocalNotificationsBackend.java
com.futureplatforms.kirin.extensions.networking.INetworkingBackend.java
com.futureplatforms.kirin.extensions.networking.KirinXHRExtensionImpl.java
com.futureplatforms.kirin.extensions.networking.NetworkingBackend.java
com.futureplatforms.kirin.extensions.settings.ISettingsBackend.java
com.futureplatforms.kirin.extensions.settings.PreferencesBackendImpl.java
com.futureplatforms.kirin.extensions.settings.SettingsBackend.java
com.futureplatforms.kirin.generated.fs.KirinCallback.java
com.futureplatforms.kirin.generated.fs.KirinFile.java
com.futureplatforms.kirin.generated.fs.KirinFilesystemExtension.java
com.futureplatforms.kirin.generated.fs.KirinFilesystem.java
com.futureplatforms.kirin.generated.fs.KirinFoundFile.java
com.futureplatforms.kirin.generated.fs.KirinOptionalCallback.java
com.futureplatforms.kirin.generated.location.KirinLocationBackend.java
com.futureplatforms.kirin.generated.location.KirinLocationData.java
com.futureplatforms.kirin.generated.location.KirinLocationListener.java
com.futureplatforms.kirin.generated.location.KirinLocationPermissions.java
com.futureplatforms.kirin.generated.location.KirinLocation.java
com.futureplatforms.kirin.generated.preferences.KirinPreferenceListener.java
com.futureplatforms.kirin.generated.preferences.KirinPreferencesBackend.java
com.futureplatforms.kirin.generated.preferences.KirinPreferences.java
com.futureplatforms.kirin.generated.xhr.KirinXHRExtension.java
com.futureplatforms.kirin.generated.xhr.KirinXHRProgressEvent.java
com.futureplatforms.kirin.generated.xhr.KirinXHRResponse.java
com.futureplatforms.kirin.generated.xhr.KirinXHRequest.java
com.futureplatforms.kirin.generated.xhr.KirinXMLHTTPRequest.java
com.futureplatforms.kirin.helpers.IKirinExtensionHelper.java
com.futureplatforms.kirin.helpers.IKirinHelper.java
com.futureplatforms.kirin.helpers.KirinApplicationHelper.java
com.futureplatforms.kirin.helpers.KirinExtensionHelper.java
com.futureplatforms.kirin.helpers.KirinHelper.java
com.futureplatforms.kirin.helpers.KirinScreenHelper.java
com.futureplatforms.kirin.helpers.KirinUiFragmentHelper.java
com.futureplatforms.kirin.internal.JSONUtils.java
com.futureplatforms.kirin.internal.KirinPaths.java
com.futureplatforms.kirin.internal.attic.IOUtils.java
com.futureplatforms.kirin.internal.attic.KirinDropbox.java
com.futureplatforms.kirin.internal.attic.ProxyGenerator.java
com.futureplatforms.kirin.internal.attic.SDCardFileUtils.java
com.futureplatforms.kirin.internal.core.AbstractObjectHolder.java
com.futureplatforms.kirin.internal.core.DefaultObjectHandler.java
com.futureplatforms.kirin.internal.core.IJsContext.java
com.futureplatforms.kirin.internal.core.IKirinState.java
com.futureplatforms.kirin.internal.core.INativeContext.java
com.futureplatforms.kirin.internal.core.IObjectHolder.java
com.futureplatforms.kirin.internal.core.JsCommands.java
com.futureplatforms.kirin.internal.core.KirinAppState.java
com.futureplatforms.kirin.internal.core.KirinWebViewHolder.java
com.futureplatforms.kirin.internal.core.NativeContext.java
com.futureplatforms.kirin.internal.core.UiObjectHolder.java
com.futureplatforms.kirin.internal.fragmentation.CursorCoercer4.java
com.futureplatforms.kirin.internal.fragmentation.CursorCoercer5.java
com.futureplatforms.kirin.internal.fragmentation.CursorCoercer.java
com.futureplatforms.kirin.internal.fragmentation.WebChromeClient7.java
com.futureplatforms.kirin.internal.fragmentation.WebChromeClient8.java
com.futureplatforms.kirin.state.IKirinDropbox.java
com.futureplatforms.kirin.state.IKirinFileSystem.java
com.futureplatforms.kirin.ui.DebugMenuActions.java
com.futureplatforms.kirin.ui.JSListAdapter.java
com.futureplatforms.kirin.ui.JSOnClickListener.java
com.futureplatforms.kirin.ui.KirinRowRenderer.java