com.smartdevicelink.managers.screen.choiceset.DeleteChoicesOperation Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sdl_java_se Show documentation
Show all versions of sdl_java_se Show documentation
The app library component of SDL is meant to run on the end userâs smart-device from within SDL enabled apps, as an embedded app, or connected to the cloud. App libraries allow the apps to connect to SDL enabled head-units and hardware through bluetooth, USB, and TCP for Android, and cloud and embedded apps can connect through web sockets, Java Beans, and other custom transports. Once the library establishes a connection between the smart device and head-unit through the preferred method of transport, the two components are able to communicate using the SDL defined protocol. The app integrating this library project is then able to expose its functionality to the head-unit through text, media, and other interactive elements.
/*
* Copyright (c) 2019 Livio, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following
* disclaimer in the documentation and/or other materials provided with the
* distribution.
*
* Neither the name of the Livio Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* Created by brettywhite on 6/12/19 1:52 PM
*
*/
package com.smartdevicelink.managers.screen.choiceset;
import com.livio.taskmaster.Task;
import com.smartdevicelink.managers.ISdl;
import com.smartdevicelink.proxy.RPCResponse;
import com.smartdevicelink.proxy.rpc.DeleteInteractionChoiceSet;
import com.smartdevicelink.proxy.rpc.listeners.OnMultipleRequestListener;
import com.smartdevicelink.util.DebugTool;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
class DeleteChoicesOperation extends Task {
private static final String TAG = "DeleteChoicesOperation";
private final WeakReference internalInterface;
private HashSet cellsToDelete;
private final BaseChoiceSetManager.ChoicesOperationCompletionListener completionListener;
private HashSet loadedCells;
private List deleteChoices;
private boolean completionSuccess = false;
DeleteChoicesOperation(ISdl internalInterface, HashSet cellsToDelete, HashSet loadedCells, BaseChoiceSetManager.ChoicesOperationCompletionListener completionListener) {
super("DeleteChoicesOperation");
this.internalInterface = new WeakReference<>(internalInterface);
this.cellsToDelete = cellsToDelete;
this.completionListener = completionListener;
this.loadedCells = loadedCells == null ? new HashSet() : new HashSet<>(loadedCells);
}
@Override
public void onExecute() {
DebugTool.logInfo(TAG, "Choice Operation: Executing delete choices operation");
updateCellsToDelete();
if (this.cellsToDelete == null || this.cellsToDelete.isEmpty()) {
if (completionListener != null) {
completionListener.onComplete(true, loadedCells);
DebugTool.logInfo(TAG, "No cells were provided to delete");
}
DeleteChoicesOperation.super.onFinished();
}
sendDeletions();
}
private void sendDeletions() {
deleteChoices = createDeleteSets();
if (deleteChoices.size() > 0) {
if (internalInterface.get() != null) {
internalInterface.get().sendRPCs(deleteChoices, new OnMultipleRequestListener() {
@Override
public void onUpdate(int remainingRequests) {
}
@Override
public void onFinished() {
if (completionListener != null) {
completionListener.onComplete(true, loadedCells);
}
DebugTool.logInfo(TAG, "Successfully deleted choices");
DeleteChoicesOperation.super.onFinished();
}
@Override
public void onResponse(int correlationId, RPCResponse response) {
if (!response.getSuccess()) {
if (completionListener != null) {
completionListener.onComplete(false, loadedCells);
}
DebugTool.logError(TAG, "Failed to delete choice: " + response.getInfo() + " | Corr ID: " + correlationId);
DeleteChoicesOperation.super.onFinished();
} else {
if (loadedCells != null) {
loadedCells.remove(loadedCellFromCorrelationId(deleteChoices, correlationId));
}
}
}
});
}
} else {
if (completionListener != null) {
completionListener.onComplete(true, this.loadedCells);
}
DebugTool.logInfo(TAG, "No Choices to delete, continue");
DeleteChoicesOperation.super.onFinished();
}
}
public void setLoadedCells(HashSet loadedCells) {
this.loadedCells = new HashSet<>(loadedCells);
}
private void updateCellsToDelete() {
HashSet updatedCellsToDelete = new HashSet<>(this.cellsToDelete);
updatedCellsToDelete.retainAll(loadedCells);
for (ChoiceCell cell : updatedCellsToDelete) {
for (ChoiceCell loadedCell : this.loadedCells) {
if (loadedCell.equals(cell)) {
cell.setChoiceId(loadedCell.getChoiceId());
}
}
}
this.cellsToDelete = updatedCellsToDelete;
}
private ChoiceCell loadedCellFromCorrelationId(List deleteRpcs, int correlationId) {
Integer choiceId = null;
for (DeleteInteractionChoiceSet rpc : deleteRpcs) {
if (rpc.getCorrelationID() == correlationId) {
choiceId = rpc.getInteractionChoiceSetID();
}
}
if (choiceId == null) {
return null;
}
for (ChoiceCell cell : this.loadedCells) {
if (cell.getChoiceId() == choiceId) {
return cell;
}
}
return null;
}
List createDeleteSets() {
List deleteChoices = new ArrayList<>(cellsToDelete.size());
for (ChoiceCell cell : cellsToDelete) {
deleteChoices.add(new DeleteInteractionChoiceSet(cell.getChoiceId()));
}
return deleteChoices;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy