Example usage for org.apache.cordova PluginResult setKeepCallback

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

Introduction

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

Prototype

public void setKeepCallback(boolean b) 

Source Link

Usage

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);
        mediaStatusCallback.sendPluginResult(result);
    }/*from ww  w. jav  a2  s. c o  m*/
}

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);
            }//w w w.j  a v  a2s  .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 w w w  . ja v  a  2  s  .  com*/
        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();
    }//ww  w . j a va 2 s . c  o 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  va2s .  c  om
        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;/*  ww w .j  av  a 2s .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 {/*from  www. jav  a2s  .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 {//  ww w  .j av a  2s  . c om
        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  . ja  v  a  2  s .  c  om
        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;
}

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

License:BSD License

public boolean pause() {
    if (this.player != null) {
        this.player.pause();

        JSONObject event = new JSONObject();
        try {//w  w w . ja  va2  s  .c om
            event.put("type", "paused");
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

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

        return true;
    }
    return false;
}