<commit_before>/*
 *  JSCommand.java
 *  Connect SDK
 *
 *  Copyright (c) 2014 LG Electronics.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.connectsdk.cordova;

import java.util.List;

import org.apache.cordova.CallbackContext;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.connectsdk.core.AppInfo;
import com.connectsdk.core.ChannelInfo;
import com.connectsdk.core.JSONSerializable;
import com.connectsdk.service.capability.Launcher;
import com.connectsdk.service.capability.MediaControl.PlayStateListener;
import com.connectsdk.service.capability.MediaControl.PlayStateStatus;
import com.connectsdk.service.capability.MediaPlayer;
import com.connectsdk.service.capability.MediaPlayer.MediaLaunchObject;
import com.connectsdk.service.capability.TVControl;
import com.connectsdk.service.capability.VolumeControl;
import com.connectsdk.service.capability.listeners.ResponseListener;
import com.connectsdk.service.command.ServiceCommandError;
import com.connectsdk.service.command.ServiceSubscription;
import com.connectsdk.service.sessions.LaunchSession;
import com.connectsdk.service.sessions.WebAppSession;

public class JSCommand {
    ConnectableDeviceWrapper deviceWrapper;
    String commandId;
    boolean subscription;
    CallbackContext callbackContext;
    ServiceSubscription<?> serviceSubscription;

    JSCommand(ConnectableDeviceWrapper deviceWrapper, String commandId, boolean subscription, CallbackContext callbackContext) {
        this.deviceWrapper = deviceWrapper;
        this.commandId = commandId;
        this.subscription = subscription;
        this.callbackContext = callbackContext;
    }

    void checkDone() {
        if (subscription) {
            destroy();
        }
    }

    void destroy() {
        deviceWrapper.cancelCommand(commandId);
    }

    public void cancel() {
        callbackContext = null;

        if (serviceSubscription != null) {
            serviceSubscription.unsubscribe();
        }
    }

    void sendSuccessEvent(Object ... objs) {
        if (callbackContext == null) return;

        JSONArray arr = new JSONArray();
        arr.put("success");

        for (Object o : objs) {
            arr.put(o);
        }

        callbackContext.success(arr);
        checkDone();
    }

    public void success() {
        sendSuccessEvent();
    }

    public void success(JSONObject obj) {
        sendSuccessEvent(obj);
    }

    public void success(JSONArray arr) {
        sendSuccessEvent(arr);
    }

    public void success(JSONSerializable obj) {
        JSONObject response = null;<commit_msg>[Android] fix subscriptions in Android implementation<commit_after>/*
 *  JSCommand.java
 *  Connect SDK
 *
 *  Copyright (c) 2014 LG Electronics.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.connectsdk.cordova;

import java.util.List;

import org.apache.cordova.CallbackContext;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.connectsdk.core.AppInfo;
import com.connectsdk.core.ChannelInfo;
import com.connectsdk.core.JSONSerializable;
import com.connectsdk.service.capability.Launcher;
import com.connectsdk.service.capability.MediaControl.PlayStateListener;
import com.connectsdk.service.capability.MediaControl.PlayStateStatus;
import com.connectsdk.service.capability.MediaPlayer;
import com.connectsdk.service.capability.MediaPlayer.MediaLaunchObject;
import com.connectsdk.service.capability.TVControl;
import com.connectsdk.service.capability.VolumeControl;
import com.connectsdk.service.capability.listeners.ResponseListener;
import com.connectsdk.service.command.ServiceCommandError;
import com.connectsdk.service.command.ServiceSubscription;
import com.connectsdk.service.sessions.LaunchSession;
import com.connectsdk.service.sessions.WebAppSession;

public class JSCommand {
    ConnectableDeviceWrapper deviceWrapper;
    String commandId;
    boolean subscription;
    CallbackContext callbackContext;
    ServiceSubscription<?> serviceSubscription;

    JSCommand(ConnectableDeviceWrapper deviceWrapper, String commandId, boolean subscription, CallbackContext callbackContext) {
        this.deviceWrapper = deviceWrapper;
        this.commandId = commandId;
        this.subscription = subscription;
        this.callbackContext = callbackContext;
    }

    void checkDone() {
        if (subscription) {
            destroy();
        }
    }

    void destroy() {
        deviceWrapper.cancelCommand(commandId);
    }

    public void cancel() {
        callbackContext = null;

        if (serviceSubscription != null) {
            serviceSubscription.unsubscribe();
        }
    }

    void sendSuccessEvent(Object ... objs) {
        if (callbackContext == null) return;

        JSONArray arr = new JSONArray();
        arr.put("success");

        for (Object o : objs) {
            arr.put(o);
        }

        if (subscription) {
            callbackContext.successSubscription(arr);
        } else {
            callbackContext.success(arr);
            checkDone();
        }
    }

    public void success() {
        sendSuccessEvent();
    }

    public void success(JSONObject obj) {
        sendSuccessEvent(obj);
    }

    public void success(JSONArray arr) {
        sendSuccessEvent(arr);
    }

    public void success(JSONSerializable obj) {
        JSONObject response = null;
{'fraction_non_alphanumeric': 0.0863109048723898, 'fraction_numerical': 0.0024748646558391337, 'mean_word_length': 3.365968939905469, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6929523', 'n_tokens_mistral': 1720, 'n_tokens_neox': 1695, 'n_words': 438}