android - fix compiler warnings/recomendations

This commit is contained in:
Evan Moore 2019-04-05 10:48:12 -04:00
parent 3c78fd2fe3
commit 31cdc1b281

View File

@ -12,81 +12,67 @@ import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set; import java.util.Set;
import com.zebra.sdk.comm.BluetoothConnection; import com.zebra.sdk.comm.BluetoothConnection;
import com.zebra.sdk.comm.Connection; import com.zebra.sdk.comm.Connection;
import com.zebra.sdk.comm.ConnectionException; import com.zebra.sdk.comm.ConnectionException;
import com.zebra.sdk.printer.PrinterLanguage;
import com.zebra.sdk.printer.PrinterStatus; import com.zebra.sdk.printer.PrinterStatus;
import com.zebra.sdk.printer.ZebraPrinterFactory; import com.zebra.sdk.printer.ZebraPrinterFactory;
import com.zebra.sdk.printer.ZebraPrinterLanguageUnknownException; import com.zebra.sdk.printer.ZebraPrinterLanguageUnknownException;
import com.zebra.sdk.printer.discovery.BluetoothDiscoverer;
import com.zebra.sdk.printer.discovery.DiscoveredPrinter;
import com.zebra.sdk.printer.discovery.DiscoveredPrinterBluetooth;
import com.zebra.sdk.printer.discovery.DiscoveryHandler;
public class ZebraPrinter extends CordovaPlugin { public class ZebraPrinter extends CordovaPlugin {
private Connection printerConnection; private Connection printerConnection;
private com.zebra.sdk.printer.ZebraPrinter printer; private com.zebra.sdk.printer.ZebraPrinter printer;
private String macAddress; private static final String lock = "ZebraPluginLock";
static final String lock = "ZebraPluginLock";
@Override @Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
Log.v("EMO", "Execute on ZebraPrinter Plugin called"); Log.v("EMO", "Execute on ZebraPrinter Plugin called");
if (action.equals("discover")) { switch (action) {
this.discover(args, callbackContext); case "discover":
return true; this.discover(callbackContext);
} else if (action.equals("connect")) { return true;
this.connect(args, callbackContext); case "connect":
return true; this.connect(args, callbackContext);
} else if (action.equals("print")) { return true;
this.print(args, callbackContext); case "print":
return true; this.print(args, callbackContext);
} else if (action.equals("isConnected")) { return true;
this.isConnected(args, callbackContext); case "isConnected":
return true; this.isConnected(callbackContext);
} else if (action.equals("disconnect")) { return true;
this.disconnect(args, callbackContext); case "disconnect":
return true; this.disconnect(callbackContext);
} else if (action.equals("printerStatus")) { return true;
this.printerStatus(args, callbackContext); case "printerStatus":
return true; this.printerStatus(callbackContext);
return true;
} }
return false; return false;
} }
private void printerStatus(JSONArray args, final CallbackContext callbackContext) { private void printerStatus(final CallbackContext callbackContext) {
final ZebraPrinter instance = this; final ZebraPrinter instance = this;
cordova.getThreadPool().execute(new Runnable() { cordova.getThreadPool().execute(() -> {
public void run() { JSONObject status = instance.GetPrinterStatus();
JSONObject status = instance.GetPrinterStatus(); if (status != null) {
if (status != null) { callbackContext.success(status);
callbackContext.success(status); } else {
} else { callbackContext.error("Failed to get status.");
callbackContext.error("Failed to get status.");
}
} }
}); });
} }
private void discover(JSONArray args, final CallbackContext callbackContext) { private void discover(final CallbackContext callbackContext) {
final ZebraPrinter instance = this; final ZebraPrinter instance = this;
cordova.getThreadPool().execute(new Runnable() { cordova.getThreadPool().execute(() -> {
public void run() { JSONArray printers = instance.NonZebraDiscovery();
JSONArray printers = instance.NonZebraDiscovery(); if (printers != null) {
if (printers != null) { callbackContext.success(printers);
callbackContext.success(printers); } else {
} else { callbackContext.error("Discovery Failed");
callbackContext.error("Discovery Failed");
}
} }
}); });
} }
@ -101,14 +87,12 @@ public class ZebraPrinter extends CordovaPlugin {
callbackContext.error("Connect Failed: " + e.getMessage()); callbackContext.error("Connect Failed: " + e.getMessage());
return; return;
} }
cordova.getThreadPool().execute(new Runnable() { cordova.getThreadPool().execute(() -> {
public void run() { printer = instance.connect(address);
printer = instance.connect(address); if (printer != null) {
if (printer != null) { callbackContext.success();
callbackContext.success(); } else {
} else { callbackContext.error("Connect Failed");
callbackContext.error("Connect Failed");
}
} }
}); });
} }
@ -123,35 +107,29 @@ public class ZebraPrinter extends CordovaPlugin {
callbackContext.error("Print Failed: " + e.getMessage()); callbackContext.error("Print Failed: " + e.getMessage());
return; return;
} }
cordova.getThreadPool().execute(new Runnable() { cordova.getThreadPool().execute(() -> {
public void run() { if (instance.printCPCL(cpcl)) {
if (instance.printCPCL(cpcl)) { callbackContext.success();
callbackContext.success(); } else {
} else { callbackContext.error("Print Failed. Printer Likely Disconnected.");
callbackContext.error("Print Failed. Printer Likely Disconnected.");
}
} }
}); });
} }
private void isConnected(JSONArray args, final CallbackContext callbackContext) { private void isConnected(final CallbackContext callbackContext) {
final ZebraPrinter instance = this; final ZebraPrinter instance = this;
cordova.getThreadPool().execute(new Runnable() { cordova.getThreadPool().execute(() -> {
public void run() { boolean result = instance.isConnected();
boolean result = instance.isConnected(); callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, result));
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, result)); callbackContext.success();
callbackContext.success();
}
}); });
} }
private void disconnect(JSONArray args, final CallbackContext callbackContext) { private void disconnect(final CallbackContext callbackContext) {
final ZebraPrinter instance = this; final ZebraPrinter instance = this;
cordova.getThreadPool().execute(new Runnable() { cordova.getThreadPool().execute(() -> {
public void run() { instance.disconnect();
instance.disconnect(); callbackContext.success();
callbackContext.success();
}
}); });
} }
@ -181,10 +159,10 @@ public class ZebraPrinter extends CordovaPlugin {
} }
private com.zebra.sdk.printer.ZebraPrinter connect(String macAddress) { private com.zebra.sdk.printer.ZebraPrinter connect(String macAddress) {
if (isConnected()) if (isConnected()) {
disconnect(); disconnect();
}
printerConnection = null; printerConnection = null;
this.macAddress = macAddress;
printerConnection = new BluetoothConnection(macAddress); printerConnection = new BluetoothConnection(macAddress);
synchronized (ZebraPrinter.lock) { synchronized (ZebraPrinter.lock) {
try { try {
@ -199,7 +177,6 @@ public class ZebraPrinter extends CordovaPlugin {
if (printerConnection.isConnected()) { if (printerConnection.isConnected()) {
try { try {
printer = ZebraPrinterFactory.getInstance(printerConnection); printer = ZebraPrinterFactory.getInstance(printerConnection);
PrinterLanguage pl = printer.getPrinterControlLanguage();
} catch (ConnectionException e) { } catch (ConnectionException e) {
Log.v("EMO", "Printer - Error...", e); Log.v("EMO", "Printer - Error...", e);
printer = null; printer = null;
@ -219,6 +196,8 @@ public class ZebraPrinter extends CordovaPlugin {
try { try {
if (printerConnection != null) { if (printerConnection != null) {
printerConnection.close(); printerConnection.close();
printerConnection = null;
printer = null;
} }
} catch (ConnectionException e) { } catch (ConnectionException e) {
e.printStackTrace(); e.printStackTrace();
@ -262,8 +241,6 @@ public class ZebraPrinter extends CordovaPlugin {
System.err.println(e.getMessage()); System.err.println(e.getMessage());
return errorStatus; return errorStatus;
} }
}else{
} }
return errorStatus; return errorStatus;
@ -276,8 +253,7 @@ public class ZebraPrinter extends CordovaPlugin {
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> devices = adapter.getBondedDevices(); Set<BluetoothDevice> devices = adapter.getBondedDevices();
for (Iterator<BluetoothDevice> it = devices.iterator(); it.hasNext();) { for (BluetoothDevice device : devices) {
BluetoothDevice device = it.next();
String name = device.getName(); String name = device.getName();
String mac = device.getAddress(); String mac = device.getAddress();
@ -292,4 +268,5 @@ public class ZebraPrinter extends CordovaPlugin {
} }
return printers; return printers;
} }
} }