summaryrefslogtreecommitdiff
path: root/base/src/main/java/com/smartdevicelink/managers/screen/SoftButtonTransitionOperation.java
blob: 4ef5e715acb34bb2c41974eade7b2c256822cf45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.smartdevicelink.managers.screen;

import com.livio.taskmaster.Task;
import com.smartdevicelink.proxy.RPCResponse;
import com.smartdevicelink.proxy.interfaces.ISdl;
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<ISdl> internalInterface;
    private final CopyOnWriteArrayList<SoftButtonObject> softButtonObjects;
    private String currentMainField1;

    SoftButtonTransitionOperation(ISdl internalInterface, CopyOnWriteArrayList<SoftButtonObject> 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<SoftButton> currentStateSoftButtonsForObjects(List<SoftButtonObject> softButtonObjects) {
        List<SoftButton> softButtons = new ArrayList<>();
        for (SoftButtonObject softButtonObject : softButtonObjects) {
            softButtons.add(softButtonObject.getCurrentStateSoftButton());
        }
        return softButtons;
    }

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