All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.smartdevicelink.managers.screen.SoftButtonTransitionOperation Maven / Gradle / Ivy

package com.smartdevicelink.managers.screen;

import com.livio.taskmaster.Task;
import com.smartdevicelink.managers.ISdl;
import com.smartdevicelink.proxy.RPCResponse;
import com.smartdevicelink.proxy.rpc.Show;
import com.smartdevicelink.proxy.rpc.SoftButton;
import com.smartdevicelink.proxy.rpc.listeners.OnRPCResponseListener;
import com.smartdevicelink.util.DebugTool;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * Created by Bilal Alsharifi on 6/15/20.
 */
class SoftButtonTransitionOperation extends Task {
    private static final String TAG = "SoftButtonTransitionOperation";
    private final WeakReference internalInterface;
    private final CopyOnWriteArrayList softButtonObjects;
    private String currentMainField1;

    SoftButtonTransitionOperation(ISdl internalInterface, CopyOnWriteArrayList softButtonObjects, String currentMainField1) {
        super("SoftButtonReplaceOperation");
        this.internalInterface = new WeakReference<>(internalInterface);
        this.softButtonObjects = softButtonObjects;
        this.currentMainField1 = currentMainField1;
    }

    @Override
    public void onExecute() {
        start();
    }

    private void start() {
        if (getState() == Task.CANCELED) {
            return;
        }

        sendNewSoftButtons();
    }

    private void sendNewSoftButtons() {
        Show show = new Show();
        show.setOnRPCResponseListener(new OnRPCResponseListener() {
            @Override
            public void onResponse(int correlationId, RPCResponse response) {
                if (!response.getSuccess()) {
                    DebugTool.logWarning(TAG, "Failed to transition soft button to new state");
                }
                onFinished();
            }
        });
        show.setMainField1(currentMainField1);
        show.setSoftButtons(currentStateSoftButtonsForObjects(softButtonObjects));
        if (internalInterface.get() != null) {
            internalInterface.get().sendRPC(show);
        }
    }

    private List currentStateSoftButtonsForObjects(List softButtonObjects) {
        List softButtons = new ArrayList<>();
        for (SoftButtonObject softButtonObject : softButtonObjects) {
            softButtons.add(softButtonObject.getCurrentStateSoftButton());
        }
        return softButtons;
    }

    void setCurrentMainField1(String currentMainField1) {
        this.currentMainField1 = currentMainField1;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy