Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package eu.elcom.euro2a;
- import android.app.PendingIntent;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.content.IntentFilter;
- import android.hardware.usb.UsbConstants;
- import android.hardware.usb.UsbDevice;
- import android.hardware.usb.UsbDeviceConnection;
- import android.hardware.usb.UsbEndpoint;
- import android.hardware.usb.UsbInterface;
- import android.hardware.usb.UsbManager;
- import android.util.Log;
- import java.io.IOException;
- import java.lang.reflect.Array;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.HashMap;
- import java.util.Iterator;
- import eu.elcom.euro2a.Singletons.ApplicationData;
- public class Usb implements Communication{
- private String typKomunikacie;
- private UsbDevice usbDevice;
- private UsbInterface usbInterface;
- private UsbEndpoint usbEndpointWrite;
- private UsbEndpoint usbEndpointRead;
- private UsbDeviceConnection usbDeviceConnection;
- private UsbManager usbManager;
- private Intent intent;
- private byte[] bytesRead;
- private byte[] bytesWrite;
- private boolean addedEndpoint;
- private int timeout;
- //private boolean access;
- private static final String ACTION_USB_PERMISSION =
- "eu.elcom.euro2a.MainActivity";
- PendingIntent mPermissionIntent;
- private Usb(){}
- public Usb(Context context){
- this.addedEndpoint = false;
- this.usbEndpointRead = null;
- this.usbEndpointWrite = null;
- this.timeout = 1000;
- //this.access = false;
- /*if( string.equals("USB") ){
- this.typKomunikacie = "USB";
- this.setupUSB(context);
- } else if( string.equals("LAN") ){
- this.typKomunikacie = "LAN";
- this.setupLAN();
- } else {
- this.typKomunikacie = "NONE";
- }
- */
- usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
- mPermissionIntent = PendingIntent.getBroadcast(context, 0, new Intent(ACTION_USB_PERMISSION), 0);
- IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
- context.registerReceiver(mUsbReceiver, filter);
- context.unregisterReceiver(mUsbReceiver);
- HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList();
- Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
- if(deviceIterator.hasNext()) {
- usbDevice = deviceIterator.next();
- usbManager.requestPermission(usbDevice, mPermissionIntent);
- }
- }
- /*0public boolean getAccess(){
- return this.access;
- }*/
- private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
- public void onReceive(Context context, Intent intent) {
- String action = intent.getAction();
- if (ACTION_USB_PERMISSION.equals(action)) {
- synchronized (this) {
- usbDevice = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
- if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
- if(usbDevice != null){
- //call method to set up device communication
- //access = true;
- }
- }
- else {
- Log.d("aaa", "permission denied for device " + usbDevice);
- }
- }
- }
- }
- };
- public void setupUSB(Context context){
- //this.usbManager = usbManager;//(UsbManager) getSystemService(Context.USB_SERVICE);
- //this.usbDevice = usbDevice;
- //intent = setupIntent;
- //usbDevice = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
- for(int i = 0; i < usbDevice.getInterfaceCount(); i++){
- if(usbDevice.getInterface(i).getEndpointCount() >= 2 && !this.addedEndpoint){
- for(int j = 0; j<usbDevice.getInterface(i).getEndpointCount();j++){
- if(this.usbEndpointRead == null || this.usbEndpointWrite == null) {
- if (usbDevice.getInterface(i).getEndpoint(j).getDirection() == UsbConstants.USB_DIR_IN /*&& usbDevice.getInterface(i).getEndpoint(j).getDirection() == UsbConstants.USB_ENDPOINT_XFER_BULK*/) {
- this.usbEndpointRead = usbDevice.getInterface(i).getEndpoint(j);
- } else if (usbDevice.getInterface(i).getEndpoint(j).getDirection() == UsbConstants.USB_DIR_OUT /*&& usbDevice.getInterface(i).getEndpoint(j).getDirection() == UsbConstants.USB_ENDPOINT_XFER_BULK*/) {
- this.usbEndpointWrite = usbDevice.getInterface(i).getEndpoint(j);
- }
- this.usbInterface = usbDevice.getInterface(i);
- }
- }
- this.addedEndpoint = true;
- }
- }
- /* usbInterface = usbDevice.getInterface(1);
- usbEndpointRead = usbInterface.getEndpoint(1);
- usbEndpointWrite = usbInterface.getEndpoint(0);*/
- usbDeviceConnection = usbManager.openDevice(usbDevice);
- usbDeviceConnection.claimInterface(usbInterface, true);
- bytesRead = new byte[this.usbEndpointRead.getMaxPacketSize()];
- }
- private void setupLAN(){
- }
- private void writeUSB(String string){
- try {
- bytesWrite = string.getBytes("cp1250");
- //tv.append("\n" + "String v bajtoch: " + bytes);
- /*vysledok = */usbDeviceConnection.bulkTransfer(this.usbEndpointWrite, this.bytesWrite, bytesWrite.length, this.timeout);
- }catch (IOException ioe){
- }
- //return null;
- }
- public String getReadedData(){
- //test
- bytesRead = new byte[this.usbEndpointRead.getMaxPacketSize()+64];
- usbDeviceConnection.bulkTransfer(this.usbEndpointRead, this.bytesRead, bytesRead.length, this.timeout);
- try {
- return (new String(bytesRead, "cp1250"));
- }catch (IOException ioe){
- return "Error";
- }
- }
- public UsbDevice getDevice(){
- return this.usbDevice;
- }
- public String connect(){
- this.writeUSB(Commands.connect);
- return this.getReadedData();
- }
- public ArrayList<String> getDeviceInfo(){
- this.writeUSB(Commands.getDevInfo);
- String[] string = this.getReadedData().split("\t");
- ArrayList<String> list = new ArrayList<>(Arrays.asList(string));
- return list;
- }
- public ArrayList<String> getPlu(int number){
- this.writeUSB(Commands.setHeadPlu);
- this.getReadedData();
- this.writeUSB(Commands.getRecPlu + "\t" + number + "\n");
- String[] string = this.getReadedData().split("\t");
- ArrayList<String> list = new ArrayList<>(Arrays.asList(string));
- return list;
- }
- public void setPlu(ArrayList<String> array){
- if(ApplicationData.getInstance().getTypeCash().indexOf("150") >= 0){
- this.writeUSB(Commands.setHeadStock150SET);
- this.getReadedData();
- this.writeUSB(Commands.setRecPlu + array.get(0) + "\t" + array.get(1) + "\t" + array.get(2) + "\t" + array.get(3) + "\t" + array.get(4) + "\t" + array.get(5) + "\t" + array.get(6) + "\t" + array.get(7) + "\t" + array.get(8) + "\t" + array.get(9) + "\t" + array.get(10) + "\t" + array.get(11) + "\n");
- this.getReadedData();
- }else {
- this.writeUSB(Commands.setHeadStock50SET);
- this.getReadedData();
- this.writeUSB(Commands.setRecPlu + array.get(0) + "\t" + array.get(1) + "\t" + array.get(2) + "\t" + array.get(3) + "\t" + array.get(4) + "\t" + array.get(5) + "\t" + array.get(6) + "\t" + array.get(7) + "\t" + array.get(8) + "\t" + array.get(9) + "\t" + array.get(10) + "\n");
- this.getReadedData();
- }
- }
- public void setOperator(ArrayList<String> array){
- if(ApplicationData.getInstance().getTypeCash().indexOf("150") >= 0){
- this.writeUSB(Commands.setHeadOperator150SET);
- this.getReadedData();
- this.writeUSB(Commands.setRecOperator + array.get(0) + "\t" + array.get(1) + "\t" + array.get(2) + "\t" + array.get(3) + "\t" + array.get(4) + "\t" + array.get(5) + "\t" + array.get(6) + "\t" + array.get(7) + "\t" + array.get(8) + "\t" + array.get(9) + "\n");
- this.getReadedData();
- }else {
- this.writeUSB(Commands.setHeadOperator50SET);
- this.getReadedData();
- this.writeUSB(Commands.setRecOperator + array.get(0) + "\t" + array.get(1) + "\t" + array.get(2) + "\t" + array.get(3) + "\t" + array.get(4) + "\t" + array.get(5) + "\t" + array.get(6) + "\n");
- this.getReadedData();
- }
- }
- public void setArticleGroup(ArrayList<String> array){
- this.writeUSB(Commands.setHeadDptSET);
- this.getReadedData();
- this.writeUSB(Commands.setRecDpt + array.get(0) + "\t" + array.get(1) + "\t" + array.get(2) + "\t" + array.get(3) + "\t" + array.get(4) + "\t" + array.get(5) + "\n");
- this.getReadedData();
- }
- public ArrayList<String> getCountPlu(){
- this.writeUSB(Commands.getCountPlu);
- String[] string = this.getReadedData().split("\t");
- ArrayList<String> list = new ArrayList<>(Arrays.asList(string));
- return list;
- }
- public ArrayList<String> getStock50(int number){
- this.writeUSB(Commands.setHeadStock50);
- this.getReadedData();
- this.writeUSB(Commands.getRecPlu + "\t" + number + "\n");
- String[] string = this.getReadedData().split("\t");
- ArrayList<String> list = new ArrayList<>(Arrays.asList(string));
- return list;
- }
- public ArrayList<String> getStock150(int number){
- this.writeUSB(Commands.setHeadStock150);
- this.getReadedData();
- this.writeUSB(Commands.getRecPlu + "\t" + number + "\n");
- String[] string = this.getReadedData().split("\t");
- ArrayList<String> list = new ArrayList<>(Arrays.asList(string));
- return list;
- }
- public ArrayList<String> getCountDpt(){
- this.writeUSB(Commands.getCountDpt);
- String[] string = this.getReadedData().split("\t");
- ArrayList<String> list = new ArrayList<>(Arrays.asList(string));
- return list;
- }
- public ArrayList<String> getCountOperator(){
- this.writeUSB(Commands.getCountOperator);
- String[] string = this.getReadedData().split("\t");
- ArrayList<String> list = new ArrayList<>(Arrays.asList(string));
- return list;
- }
- public ArrayList<String> getCountStock(){
- this.writeUSB(Commands.getCountPlu);
- String[] string = this.getReadedData().split("\t");
- ArrayList<String> list = new ArrayList<>(Arrays.asList(string));
- return list;
- }
- public ArrayList<String> getDpt(int number){
- this.writeUSB(Commands.setHeadDpt);
- this.getReadedData();
- this.writeUSB(Commands.getRecDpt + "\t" + number + "\n");
- String[] string = this.getReadedData().split("\t");
- /*
- //druha cast
- this.writeUSB(Commands.setHeadDpt2);
- this.getReadedData();
- this.writeUSB(Commands.getRecDpt + "\t" + number + "\n");
- String[] string2 = this.getReadedData().split("\t");
- */
- ArrayList<String> list = new ArrayList<>(Arrays.asList(string));
- /*Log.d("ERROR",string2[0]+string2[1]+string2[2]+string2[3]+string2[4]+string2[5]);
- list.add(string2[5]);
- list.add(string2[6]);*/
- return list;
- }
- public ArrayList<String> getOperator(int number){
- this.writeUSB(Commands.setHeadOperator);
- this.getReadedData();
- this.writeUSB(Commands.getRecOperator + "\t" + number + "\n");
- String[] string = this.getReadedData().split("\t");
- ArrayList<String> list = new ArrayList<>(Arrays.asList(string));
- return list;
- }
- public void DennaUzavierka(){
- this.writeUSB(Commands.setDennaUzavierka);
- this.getReadedData();
- }
- public void deleteAllReport(int print, int max){
- this.writeUSB(Commands.setAllReport + print + "\t1" + "\t" + max + "\n" );
- this.getReadedData();
- }
- public void delReport(int print, int from, int to){
- this.writeUSB(Commands.setAllReport + print + "\t"+from + "\t" + to + "\n" );
- this.getReadedData();
- }
- public void delDpt(int print){
- this.writeUSB(Commands.setDelDpt + print + "\n");
- this.getReadedData();
- }
- public void delOperator(int print){
- this.writeUSB(Commands.setDelOperator + print + "\n");
- this.getReadedData();
- }
- public void delZasuvky(int print){
- this.writeUSB(Commands.setDelZasuvky + print + "\n");
- this.getReadedData();
- }
- public ArrayList<String> getOperator50(int number){
- this.writeUSB(Commands.setHeadOperator50);
- this.getReadedData();
- this.writeUSB(Commands.getRecOperator + "\t" + number + "\n");
- String[] string = this.getReadedData().split("\t");
- ArrayList<String> list = new ArrayList<>(Arrays.asList(string));
- return list;
- }
- public ArrayList<String> getOperator150(int number){
- this.writeUSB(Commands.setHeadOperator150);
- this.getReadedData();
- this.writeUSB(Commands.getRecOperator + "\t" + number + "\n");
- String[] string = this.getReadedData().split("\t");
- ArrayList<String> list = new ArrayList<>(Arrays.asList(string));
- return list;
- }
- public void setTaxes(String string){
- this.writeUSB(Commands.setHeadTaxes);
- this.getReadedData();
- this.writeUSB(Commands.setTaxes + string);
- }
- /*public void setCurrency(ArrayList<String> array){
- this.writeUSB(Commands.setHeadCurrency);
- this.getReadedData();
- this.writeUSB(Commands.setCurrency + array.get(0));
- this.getReadedData();
- this.writeUSB(Commands.setCurrency + array.get(1));
- this.getReadedData();
- }*/
- public ArrayList<String> getCurrency1() {
- this.writeUSB(Commands.setHeadCurrency);
- this.getReadedData();
- this.writeUSB(Commands.getCurrency1);
- String[] string = this.getReadedData().split("\t");
- ArrayList<String> list = new ArrayList<>(Arrays.asList(string));
- return list;
- }
- public ArrayList<String> getCurrency2() {
- this.writeUSB(Commands.setHeadCurrency);
- this.getReadedData();
- this.writeUSB(Commands.getCurrency2);
- String[] string = this.getReadedData().split("\t");
- ArrayList<String> list = new ArrayList<>(Arrays.asList(string));
- return list;
- }
- public ArrayList<String> getTaxSettings() {
- this.writeUSB(Commands.setHeadTaxes);
- this.getReadedData();
- this.writeUSB(Commands.getTaxes);
- String[] string = this.getReadedData().split("\t");
- ArrayList<String> list = new ArrayList<>(Arrays.asList(string));
- return list;
- }
- public ArrayList<String> getGraphicLogo() {
- this.writeUSB(Commands.setHeadGraphicLogo);
- this.getReadedData();
- this.writeUSB(Commands.getGraphicLogo);
- String[] string = this.getReadedData().split("\t");
- ArrayList<String> list = new ArrayList<>(Arrays.asList(string));
- return list;
- }
- public ArrayList<String> getSurdisc() {
- this.writeUSB(Commands.setHeadSurdisc);
- this.getReadedData();
- this.writeUSB(Commands.getSurdisc);
- String[] string = this.getReadedData().split("\t");
- ArrayList<String> list = new ArrayList<>(Arrays.asList(string));
- return list;
- }
- public ArrayList<String> getTextJournal() {
- this.writeUSB(Commands.setHeadTextJournal);
- this.getReadedData();
- this.writeUSB(Commands.getTextJournal);
- String[] string = this.getReadedData().split("\t");
- ArrayList<String> list = new ArrayList<>(Arrays.asList(string));
- return list;
- }
- public ArrayList<String> getDataJournal() {
- this.writeUSB(Commands.setHeadDataJournal);
- this.getReadedData();
- this.writeUSB(Commands.getDataJournal);
- String[] string = this.getReadedData().split("\t");
- ArrayList<String> list = new ArrayList<>(Arrays.asList(string));
- return list;
- }
- public ArrayList<String> getTimeConfig() {
- this.writeUSB(Commands.setHeadTimeConfig);
- this.getReadedData();
- this.writeUSB(Commands.getTimeConfig);
- String[] string = this.getReadedData().split("\t");
- ArrayList<String> list = new ArrayList<>(Arrays.asList(string));
- return list;
- }
- public ArrayList<String> getDateConfig() {
- this.writeUSB(Commands.setHeadDateConfig);
- this.getReadedData();
- this.writeUSB(Commands.getDateConfig);
- String[] string = this.getReadedData().split("\t");
- ArrayList<String> list = new ArrayList<>(Arrays.asList(string));
- return list;
- }
- /*public void setCash(ArrayList<String> array) {
- this.writeUSB(Commands.setHeadCash);
- this.getReadedData();
- this.writeUSB(Commands.setCash + array.get(0));
- this.getReadedData();
- this.writeUSB(Commands.setCash + array.get(1));
- this.getReadedData();
- }*/
- public ArrayList<String> getCash1() {
- this.writeUSB(Commands.setHeadCash);
- this.getReadedData();
- this.writeUSB(Commands.getCash1);
- String[] string = this.getReadedData().split("\t");
- ArrayList<String> list = new ArrayList<>(Arrays.asList(string));
- return list;
- }
- public ArrayList<String> getCash2() {
- this.writeUSB(Commands.setHeadCash);
- this.getReadedData();
- this.writeUSB(Commands.getCash2);
- String[] string = this.getReadedData().split("\t");
- ArrayList<String> list = new ArrayList<>(Arrays.asList(string));
- return list;
- }
- public void setPrice(String string) {
- this.writeUSB(Commands.setHeadPrice);
- this.getReadedData();
- this.writeUSB(Commands.setPrice + string);
- this.getReadedData();
- }
- public ArrayList<String> getPrice() {
- this.writeUSB(Commands.setHeadPrice);
- this.getReadedData();
- this.writeUSB(Commands.getPrice);
- String[] string = this.getReadedData().split("\t");
- ArrayList<String> list = new ArrayList<>(Arrays.asList(string));
- return list;
- }
- public String getSerialNumber(){
- this.writeUSB(Commands.getDevInfo);
- String[] string = this.getReadedData().split("\t");
- return string[8];
- }
- public String disconnect(){
- this.writeUSB(Commands.disConnect);
- return this.getReadedData();
- }
- public void close(){
- }
- public void setDiscount(ArrayList<String> array) {
- this.writeUSB(Commands.setHeadDiscount);
- this.getReadedData();
- this.writeUSB(Commands.setDiscount + array.get(0) + "\t"+ array.get(1) + "\t"+ array.get(2) + "\t"+ array.get(3) + "\t"+ array.get(4) +"\n");
- this.getReadedData();
- }
- /*public void setGrLogo(ArrayList<String> array) {
- this.writeUSB(Commands.setHeadLogo);
- this.getReadedData();
- this.writeUSB(Commands.setLogo + array.get(0) + "\t" + array.get(1) + "\n");
- this.getReadedData();
- }*/
- public void setTextJournal(ArrayList<String> array){
- this.writeUSB(Commands.setHeadTextJournal);
- this.getReadedData();
- this.writeUSB(Commands.setTextJournal + array.get(0)+ "\t" + array.get(1) + "\n");
- this.getReadedData();
- }
- public void setDataJournal(ArrayList<String> array){
- this.writeUSB(Commands.setHeadDataJournal);
- this.getReadedData();
- this.writeUSB(Commands.setDataJournal + array.get(0)+ "\t" + array.get(1) + "\n");
- this.getReadedData();
- }
- public void setJournal(ArrayList<String> array){
- this.writeUSB(Commands.setHeadTextJournal);
- this.getReadedData();
- this.writeUSB(Commands.setTextJournal + array.get(0) + "\n");
- this.getReadedData();
- this.writeUSB(Commands.setHeadDataJournal);
- this.getReadedData();
- this.writeUSB(Commands.setDataJournal + array.get(1) + "\n");
- this.getReadedData();
- }
- public void setSurdisc(ArrayList<String> array){
- this.writeUSB(Commands.setHeadSurdisc);
- this.getReadedData();
- this.writeUSB(Commands.setSurdisc + array.get(0)+ "\t" +array.get(1)+ "\t" +array.get(2)+ "\t" +array.get(3) + "\n");
- this.getReadedData();
- }
- public void setGraphicLogo(ArrayList<String> array){
- this.writeUSB(Commands.setHeadGraphicLogo);
- this.getReadedData();
- this.writeUSB(Commands.setGraphicLogo + array.get(0) + "\n");
- this.getReadedData();
- }
- public void setTaxSettings(ArrayList<String> array){
- this.writeUSB(Commands.setHeadTaxes);
- this.getReadedData();
- this.writeUSB(Commands.setTaxes + array.get(0) + "\t" + array.get(1)+"\t" + array.get(2)+ "\n");
- this.getReadedData();
- }
- public void setCurrency(ArrayList<String> array){
- this.writeUSB(Commands.setHeadCurrency);
- this.getReadedData();
- this.writeUSB(Commands.setCurrency1 + array.get(0) + "\t" + array.get(1)+"\t" + array.get(2)+ "\n");
- this.getReadedData();
- this.writeUSB(Commands.setCurrency2 + array.get(3) + "\t" + array.get(4)+"\t" + array.get(5)+ "\n");
- this.getReadedData();
- }
- public void setPriceConfig(ArrayList<String> array){
- this.writeUSB(Commands.setHeadPrice);
- this.getReadedData();
- this.writeUSB(Commands.setPrice + array.get(0) + "\t" + array.get(1)+"\t" + array.get(2)+ "\n");
- this.getReadedData();
- }
- public void setCashSetting(ArrayList<String> array){
- this.writeUSB(Commands.setHeadCash);
- this.getReadedData();
- this.writeUSB(Commands.setCash1 + array.get(0) + "\t" + array.get(1) + "\t" + "\n");
- this.getReadedData();
- this.writeUSB(Commands.setCash2 + array.get(2) + "\t" + array.get(3)+"\t" + "\n");
- this.getReadedData();
- }
- public void setTime(ArrayList<String> array){
- this.writeUSB(Commands.setHeadTime);
- this.getReadedData();
- this.writeUSB(Commands.setTime + array.get(0)+array.get(1) + "\n");
- this.getReadedData();
- }
- public void setDateConfig(ArrayList<String> array){
- this.writeUSB(Commands.setHeadDateConfig);
- this.getReadedData();
- this.writeUSB(Commands.setDateConfig + array.get(0) + array.get(1) + array.get(2).substring(2, 4) + "\n");
- this.getReadedData();
- //Log.d("USB",this.getReadedData().toString());
- }
- /*
- public void setDate (ArrayList<String> array){
- this.writeUSB(Commands.setHeadDate);
- this.getReadedData();
- this.writeUSB(Commands.setDate + array.get(0) + "\t" + array.get(1) + "\t" + array.get(2) + "\t\n");
- this.getReadedData();
- }*/
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement