Example usage for org.apache.cordova PluginResult PluginResult

List of usage examples for org.apache.cordova PluginResult PluginResult

Introduction

In this page you can find the example usage for org.apache.cordova PluginResult PluginResult.

Prototype

public PluginResult(Status status, List<PluginResult> multipartMessages) 

Source Link

Usage

From source file:com.samsung.spen.SpenTrayBar.java

License:Apache License

/**
 * Set Text Recognition//from   w  w w. ja  va 2  s. co  m
*/
private void setTextRecognition() {
    if (Log.isLoggable(Utils.SPEN, Log.DEBUG)) {
        Log.d(TAG, "Inside setTextRecognition, intializing text recognition");
    }
    Context context = mContextParams.getSpenCustomDrawPlugin().cordova.getActivity().getApplicationContext();
    mSpenTextRecognitionManager = new SpenTextRecognitionManager(context);

    List<SpenRecognitionInfo> textRecognitionList = mSpenTextRecognitionManager
            .getInfoList(SpenObjectBase.TYPE_STROKE, SpenObjectBase.TYPE_CONTAINER);

    try {
        if (textRecognitionList.size() > 0) {
            for (SpenRecognitionInfo info : textRecognitionList) {
                if (info.name.equalsIgnoreCase("SpenText")) {
                    if (mTextRecognition == null) {
                        mTextRecognition = mSpenTextRecognitionManager.createRecognition(info);
                    }
                    break;
                }
            }
        }

        List<String> languageList = mTextRecognition.getSupportedLanguage();
        if (textRecognitionList.size() > 0) {
            for (String language : languageList) {
                if (language.equalsIgnoreCase("eng")) {
                    mTextRecognition.setLanguage(language);
                    break;
                }
            }
        }

        mTextRecognition.setResultListener(new ResultListener() {
            @Override
            public void onResult(List<SpenObjectBase> input, List<SpenObjectBase> output) {

                mIsProcessingRecognition = false;

                if (isDone) {
                    if (mSpenNoteDoc != null) {
                        ArrayList<SpenObjectBase> selectedList = mSpenPageDoc.getObjectList();
                        int j = 0;
                        for (int i = 0; i < selectedList.size(); i++) {
                            if (selectedList.get(i).getType() == SpenObjectBase.TYPE_TEXT_BOX) {
                                if (output == null) {
                                    output = new ArrayList<SpenObjectBase>();
                                }
                                output.add(j, selectedList.get(i));
                                j++;
                            }
                        }
                    }

                    if (output == null) {
                        return;
                    }

                    StringBuilder js_out = new StringBuilder();

                    for (SpenObjectBase obj : output) {
                        if (obj instanceof SpenObjectTextBox) {
                            SpenObjectTextBox mSpenObjectTextBox = (SpenObjectTextBox) obj;
                            js_out.append(" " + mSpenObjectTextBox.getText());
                        }
                    }
                    String js_out2 = js_out.toString();
                    if (js_out2.contains("\n")) {
                        js_out2 = js_out2.replace("\n", " ");
                    }
                    js_out2 = js_out2.trim();
                    if (Log.isLoggable(Utils.SPEN, Log.DEBUG)) {
                        Log.d(TAG, "getting text form output, text:" + js_out);
                    }

                    PluginResult progressResult = new PluginResult(PluginResult.Status.OK, js_out2);
                    progressResult.setKeepCallback(true);
                    mContextParams.getCallbackContext().sendPluginResult(progressResult);
                    if (Log.isLoggable(Utils.SPEN, Log.DEBUG)) {
                        Log.d(TAG, "Returned Text for return type recognitzed text is: " + js_out2);
                    }
                    isDone = false;
                } else {

                    if (output != null) {
                        handleResult((SpenObjectTextBox) output.get(0));
                    }
                    mSpenSurfaceView.closeControl();
                    mSpenSurfaceView.update();
                }
            }
        });
    } catch (ClassNotFoundException e) {
        Log.d(TAG, "SpenTextRecognitionManager class not found: " + e.getMessage());
        e.printStackTrace();
        SpenException.sendPluginResult(SpenExceptionType.FAILED_RECOGNIZE_TEXT,
                mContextParams.getCallbackContext());
    } catch (InstantiationException e) {
        Log.d(TAG, "Failed to access the SpenTextRecognitionManager constructor: " + e.getMessage());
        e.printStackTrace();
        SpenException.sendPluginResult(SpenExceptionType.FAILED_RECOGNIZE_TEXT,
                mContextParams.getCallbackContext());
    } catch (IllegalAccessException e) {
        Log.d(TAG, "Failed to access the SpenTextRecognitionManager field or method: " + e.getMessage());
        e.printStackTrace();
        SpenException.sendPluginResult(SpenExceptionType.FAILED_RECOGNIZE_TEXT,
                mContextParams.getCallbackContext());
    } catch (SpenCreationFailureException e) {
        Log.d(TAG, "exception while creating spen: " + e.getMessage());
        e.printStackTrace();
        SpenException.sendPluginResult(SpenExceptionType.FAILED_RECOGNIZE_TEXT,
                mContextParams.getCallbackContext());
    } catch (IllegalStateException e) {
        Log.d(TAG, "SpenTextRecognition is not loaded: " + e.getMessage());
        e.printStackTrace();
        SpenException.sendPluginResult(SpenExceptionType.FAILED_RECOGNIZE_TEXT,
                mContextParams.getCallbackContext());
    } catch (Exception e) {
        Log.d(TAG, "SpenTextRecognitionManager engine not loaded: " + e.getMessage());
        e.printStackTrace();
        SpenException.sendPluginResult(SpenExceptionType.FAILED_RECOGNIZE_TEXT,
                mContextParams.getCallbackContext());
    }
}

From source file:com.sesamtv.cordova.chromecast.ChromeCast.java

License:MIT License

public void onMediaStatusCallback(JSONObject status) {
    if (mediaStatusCallback != null) {
        PluginResult result = new PluginResult(PluginResult.Status.OK, status);
        result.setKeepCallback(true);//from  w  w w  .j  a va  2  s  .  co  m
        mediaStatusCallback.sendPluginResult(result);
    }
}

From source file:com.sesamtv.cordova.chromecast.ChromeCast.java

License:MIT License

private void onReceiverListChanged() {
    cordova.getActivity().runOnUiThread(new Runnable() {
        public void run() {
            Log.d(TAG, "onReceiverListChanged " + (receiverCallback != null));
            if (receiverCallback != null) {
                JSONArray jsonRoute = getRoutes();
                PluginResult result = new PluginResult(PluginResult.Status.OK, jsonRoute);
                result.setKeepCallback(true);
                Log.d(TAG, "onReceiverListChanged " + jsonRoute.toString());
                receiverCallback.sendPluginResult(result);
            }//from w  ww  .j a  v a2  s .  c o  m
        }
    });

}

From source file:com.sesamtv.cordova.chromecast.ChromeCast.java

License:MIT License

private void onSessionStarted(ApplicationConnectionResult result) {
    if (setReceiverCallback != null) {
        setReceiverCallback.success();/*from  ww  w  .  j a  v  a2s.  c o m*/
        setReceiverCallback = null;
    }
    if (onSessionCreatedCallback != null) {
        try {
            JSONObject info = new JSONObject();
            info.put("appId", APP_ID);
            info.put("sessionId", result.getSessionId());
            info.put("displayName", result.getApplicationMetadata().getName());
            info.put("statusCode", result.getStatus().getStatusCode());
            info.put("wasLaunched", result.getWasLaunched());

            ArrayList<JSONObject> media = new ArrayList<JSONObject>();
            if (mMediaPlayer != null) {
                JSONObject mediaStatus = mMediaPlayer.getMediaStatus();
                if (mediaStatus.has("contentId")) {
                    media.add(mediaStatus);
                }
            }
            info.put("media", media);
            PluginResult res = new PluginResult(PluginResult.Status.OK, info);
            res.setKeepCallback(true);
            onSessionCreatedCallback.sendPluginResult(res);
        } catch (JSONException e) {
            Log.e(TAG, e.getMessage());
        }
    }
}

From source file:com.sesamtv.cordova.chromecast.ChromeCast.java

License:MIT License

private void onSessionEnded(JSONObject info) {
    if (info == null) {
        info = new JSONObject();
    }/*from   w ww  . j av a2s. co  m*/
    if (onEndedCallback != null) {
        PluginResult result = new PluginResult(PluginResult.Status.OK, info);
        result.setKeepCallback(true);
        onEndedCallback.sendPluginResult(result);
    }
}

From source file:com.sesamtv.cordova.chromecast.ChromeCast.java

License:MIT License

public PluginResult getMediaStatus() {
    JSONObject status = new JSONObject();
    try {/*from w ww .  j a  v a  2s.co  m*/
        if (mMediaPlayer != null) {
            status = mMediaPlayer.getMediaStatus();
        } else {
            status.put("state", "");
            status.put("position", 0.0);
            status.put("duration", 0.0);
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }
    PluginResult result = new PluginResult(PluginResult.Status.OK, status);
    result.setKeepCallback(true);
    return result;
}

From source file:com.simplec.phonegap.plugins.videoplayer.VideoPlayer.java

License:BSD License

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
protected void openVideoDialog(String path, JSONObject options, final CallbackContext callbackContext) {
    this.callbackContext = callbackContext;

    MediaMetadataRetriever metaRetriever = new MediaMetadataRetriever();
    if (path.startsWith(ASSETS)) {
        String f = path.substring(ASSETS.length());
        AssetFileDescriptor fd = null;/*from  w  ww.j  a  v  a 2  s .  c o  m*/
        try {
            fd = cordova.getActivity().getAssets().openFd(f);
            metaRetriever.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength());
        } catch (Exception e) {
            callbackContext.error(e.getLocalizedMessage());
            Log.v(LOG_TAG, "error: " + e.getLocalizedMessage());
            e.printStackTrace();
        }
    } else {
        try {
            Log.v(LOG_TAG, "setDataSource file");
            metaRetriever.setDataSource(path);
        } catch (Exception e) {
            callbackContext.error(e.getLocalizedMessage());
            Log.v(LOG_TAG, "error: " + e.getLocalizedMessage());
            e.printStackTrace();
        }
    }

    double mVideoHeight = 0;
    double mVideoWidth = 0;
    try {
        String height = metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT);
        String width = metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH);
        mVideoHeight = Float.parseFloat(height);
        mVideoWidth = Float.parseFloat(width);
    } catch (NumberFormatException e) {
        mVideoHeight = 900;
        mVideoWidth = 1600;
        Log.d(LOG_TAG, e.getMessage());
    }

    Display display = this.cordova.getActivity().getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    size.x = size.x * 9 / 10;
    size.y = size.y * 9 / 10;

    double displayAspect = ((double) size.x) / ((double) size.y);
    double videoAspect = ((double) mVideoWidth) / ((double) mVideoHeight);

    if (displayAspect > videoAspect) {
        mVideoHeight = size.y;
        mVideoWidth = mVideoHeight * videoAspect;
    } else {
        mVideoWidth = mVideoHeight * displayAspect;
    }

    // Let's create the main dialog
    dialog = new Dialog(cordova.getActivity(), android.R.style.Theme_NoTitleBar);
    dialog.getWindow().getAttributes().windowAnimations = android.R.style.Animation_Dialog;
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setCancelable(false);

    Log.v(LOG_TAG, "getting dimensions");
    int h = (int) mVideoHeight;
    int w = (int) mVideoWidth;
    Log.v(LOG_TAG, "width: " + w);
    Log.v(LOG_TAG, "height: " + h);

    // Main container layout
    LinearLayout main = new LinearLayout(cordova.getActivity());
    main.setBackgroundColor(color.black);
    main.setLayoutParams(new LinearLayout.LayoutParams(w, h));
    // main.setOrientation(LinearLayout.VERTICAL);
    main.setHorizontalGravity(Gravity.CENTER_HORIZONTAL);
    main.setVerticalGravity(Gravity.CENTER_VERTICAL);
    main.setLayerType(View.LAYER_TYPE_NONE, null);

    videoView = new VideoView(cordova.getActivity());
    videoView.setBackgroundColor(color.black);
    videoView.setLayoutParams(new LinearLayout.LayoutParams(w, h));
    //videoView.setZOrderOnTop(true);
    // videoView.setVideoURI(uri);
    // videoView.setVideoPath(path);
    main.addView(videoView);

    detector = new PlayerGestureDetection(cordova.getActivity(),
            new PlayerGestureDetection.SimpleGestureListener() {

                @Override
                public void onSwipe(int direction) {
                    // TODO Auto-generated method stub
                    Log.v(LOG_TAG, "  onSwipe " + direction);
                    JSONObject event = new JSONObject();
                    try {
                        event.put("type", "swipe");
                        if (direction == PlayerGestureDetection.SWIPE_LEFT) {
                            event.put("direction", "right");
                        }
                        if (direction == PlayerGestureDetection.SWIPE_RIGHT) {
                            event.put("direction", "left");
                        }
                        if (direction == PlayerGestureDetection.SWIPE_UP) {
                            event.put("direction", "up");
                        }
                        if (direction == PlayerGestureDetection.SWIPE_DOWN) {
                            event.put("direction", "down");
                        }
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    PluginResult errorResult = new PluginResult(PluginResult.Status.OK, event);
                    errorResult.setKeepCallback(true);
                    callbackContext.sendPluginResult(errorResult);
                }

                @Override
                public void onSingleTap() {
                    Log.v(LOG_TAG, "  onSingleTapUp ");
                    if (player.isPlaying()) {
                        Log.v(LOG_TAG, "  pausing ");
                        player.pause();
                        videoView.pause();
                    } else {
                        Log.v(LOG_TAG, "  resuming ");
                        videoView.start();
                        player.start();
                    }
                }
            });

    player = new MediaPlayer();
    player.setOnPreparedListener(this);
    player.setOnCompletionListener(this);
    player.setOnErrorListener(this);

    if (path.startsWith(ASSETS)) {
        String f = path.substring(ASSETS.length());
        AssetFileDescriptor fd = null;
        try {
            fd = cordova.getActivity().getAssets().openFd(f);
            player.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength());
        } catch (Exception e) {
            callbackContext.error(e.getLocalizedMessage());
            Log.v(LOG_TAG, "error3: " + e.getLocalizedMessage());
            e.printStackTrace();
            stop();
            return;
        }
    } else {
        try {
            Log.v(LOG_TAG, "setDataSource file");
            player.setDataSource(path);
        } catch (Exception e) {
            callbackContext.error(e.getLocalizedMessage());
            Log.v(LOG_TAG, "error4: " + e.getLocalizedMessage());
            e.printStackTrace();
            stop();
            return;
        }
    }

    videoView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {

        @Override
        public void onSystemUiVisibilityChange(int visibility) {
            // TODO Auto-generated method stub

        }
    });
    videoView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            Log.v(LOG_TAG, "onTouch ");
            if (detector != null) {
                detector.onTouchEvent(event);
            }
            return true;
        }
    });
    videoView.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent keyEvent) {
            Log.v(LOG_TAG, "onKey keyCode=" + keyCode + "  evt.keycode" + keyEvent.getKeyCode() + "  evt.flags"
                    + keyEvent.getFlags());

            JSONObject event = new JSONObject();
            try {
                event.put("type", "key");
                event.put("keyCode", keyCode);
                event.put("flags", keyEvent.getFlags());
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            PluginResult errorResult = new PluginResult(PluginResult.Status.OK, event);
            errorResult.setKeepCallback(true);
            callbackContext.sendPluginResult(errorResult);

            return true;
        }
    });

    try {
        float volume = Float.valueOf(options.getString("volume"));
        player.setVolume(volume, volume);
    } catch (Exception e) {
        Log.v(LOG_TAG, "error: " + e.getLocalizedMessage());
    }

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
        int scalingMode = 0;
        try {
            scalingMode = options.getInt("scalingMode");
        } catch (Exception e) {
            scalingMode = 0;
        }
        Log.v(LOG_TAG, "Scaling: " + scalingMode);

        switch (scalingMode) {
        case MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING:
            player.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);
            break;
        case MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT:
            player.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT);
            break;
        default:
            player.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);
        }
    }

    final SurfaceHolder mHolder = videoView.getHolder();
    mHolder.setKeepScreenOn(true);
    //mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    mHolder.addCallback(new SurfaceHolder.Callback() {
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            Log.v(LOG_TAG, "surfaceCreated");
            if (player != null) {
                player.setDisplay(holder);
                try {
                    Log.v(LOG_TAG, "preparing player");
                    if (!prepared) {
                        player.prepare();
                        prepared = true;
                    }

                } catch (Exception e) {
                    callbackContext.error(e.getLocalizedMessage());
                }
            }
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            Log.v(LOG_TAG, "surfaceDestroyed");
            if (player != null) {
                // don't destroy the player
            }
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        }
    });

    WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
    lp.copyFrom(dialog.getWindow().getAttributes());
    lp.width = WindowManager.LayoutParams.MATCH_PARENT;
    lp.height = WindowManager.LayoutParams.MATCH_PARENT;

    dialog.setContentView(main);
    dialog.show();
    dialog.getWindow().setAttributes(lp);
}

From source file:com.simplec.phonegap.plugins.videoplayer.VideoPlayer.java

License:BSD License

@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
    Log.e(LOG_TAG, "VideoPlayer.onError(" + what + ", " + extra + ")");

    JSONObject event = new JSONObject();
    try {//  w ww. ja va2s  .  c o  m
        event.put("type", "error");
        event.put("what", what);
        event.put("extra", extra);
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    PluginResult errorResult = new PluginResult(PluginResult.Status.OK, event);
    errorResult.setKeepCallback(false);
    callbackContext.sendPluginResult(errorResult);

    mp.stop();
    mp.release();
    dialog.dismiss();

    prepared = false;
    dialog = null;
    player = null;
    videoView = null;
    detector = null;
    callbackContext = null;

    return false;
}

From source file:com.simplec.phonegap.plugins.videoplayer.VideoPlayer.java

License:BSD License

@Override
public void onPrepared(MediaPlayer mp) {
    Log.v(LOG_TAG, "onPrepared");
    JSONObject event = new JSONObject();
    try {/*from w w  w.  j a va  2  s . co m*/
        event.put("type", "prepared");
        event.put("duration", player.getDuration() / 1000);
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    Log.v(LOG_TAG, "starting video");
    mp.start();

    PluginResult errorResult = new PluginResult(PluginResult.Status.OK, event);
    errorResult.setKeepCallback(true);
    callbackContext.sendPluginResult(errorResult);

    /*MediaController mc = new MediaController(cordova.getActivity());
    mc.setAnchorView(videoView);
    mc.setMediaPlayer(videoView);
    videoView.setMediaController(mc);
    mc.setVisibility(View.VISIBLE);
    //((ViewGroup)videoView.getParent()).addView(mc);*/

}

From source file:com.simplec.phonegap.plugins.videoplayer.VideoPlayer.java

License:BSD License

@Override
public void onCompletion(MediaPlayer mp) {
    Log.v(LOG_TAG, "onCompletion");
    JSONObject event = new JSONObject();
    try {//from  w  w w .  j  a v a2s. c  o  m
        event.put("type", "completed");
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    PluginResult errorResult = new PluginResult(PluginResult.Status.OK, event);
    errorResult.setKeepCallback(false);
    callbackContext.sendPluginResult(errorResult);

    mp.stop();
    mp.release();
    dialog.dismiss();

    prepared = false;
    dialog = null;
    player = null;
    videoView = null;
    detector = null;
    callbackContext = null;
}