Helcim Logo Support & Tutorials

© Copyright 2006-2018 Helcim Inc.

All Rights Reserved.

POS Hybrid Integration » Android »

Android App Sample


public class Mainscreen extends AppCompatActivity {

    // CREATE HCMTERMINAL CLASS
    hcmTerminal terminalTest = new hcmTerminal();

    // SETTINGS
    String ip;
    Integer port;


    // CREATE OBJECTS FOR TEXTFIELDS
    EditText txtAmount;
    EditText txtClerkId;
    EditText txtInvoiceNumber;
    EditText txtLog;
    Timer logTimer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.demoscreen);

        // INIT WIDGETS
        txtAmount = (EditText) findViewById(R.id.txtAmount);
        txtClerkId = (EditText) findViewById(R.id.txtClerkId);
        txtInvoiceNumber = (EditText) findViewById(R.id.txtInvoiceNumber);
        txtLog = (EditText) findViewById(R.id.txtLog);

        // BUTTON TEST CONNECTION CLICK EVENT
        Button btnTestCon = (Button) findViewById(R.id.btnTestCon);
        btnTestCon.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {

                // SET IP AND PORT
                terminalTest.terminalIp = ip;
                terminalTest.terminalPort = port;

                // CREATE THREAD
                Thread thread = new Thread() {

                    @Override
                    public void run() {

                        // TEST CONNECTION
                        terminalTest.hcmTestConnection();

                        // RESET TERMINAL
                        terminalTest = new hcmTerminal();

                    }
                };

                // START THREAD
                thread.start();

                // INIT TIMER
                logTimer = new Timer();

                // START TIMER AT 500MS
                logTimer.scheduleAtFixedRate(new TimerTask() {
                    @Override
                    public void run() {

                        // CREATE UI THREAD SO I CAN CHANGE UI ELEMENTS WHILE THREAD IS RUNNING
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {

                                // MALE SURE NOT NULL
                                if (terminalTest.connectionLog != null) {

                                    // MAKE SURE IT HAS DATA
                                    if (terminalTest.connectionLog.length() > 1) {

                                        // DISPLAY
                                        txtLog.append(terminalTest.connectionLog);

                                        // CHECK IF CONNCTION HAS ENDED
                                        if (terminalTest.connectionLog.contains("Ended Connection")) {

                                            // STOP TIMER
                                            logTimer.cancel();

                                        }

                                        // CLEAR LOG
                                        terminalTest.connectionLog = "";

                                        // CHECK FOR RESPONSE
                                        if (terminalTest.responseMessage != null) {

                                            // ADD RESPONSE
                                            txtLog.append("\n\n" + terminalTest.responseMessage);

                                        }

                                    }
                                }
                            }
                        });

                    }
                }, 0, 500);

            }
        });

        // BUTTON PURCHASE CLICK EVENT
        Button btnProcess = (Button) findViewById(R.id.btnProcess);
        btnProcess.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {

                // SET IP AND PORT
                terminalTest.terminalIp = ip;
                terminalTest.terminalPort = port;

                // ENTER SETTINGS
                terminalTest.amount = Integer.parseInt(txtAmount.getText().toString());
                terminalTest.clerkId = Integer.parseInt(txtClerkId.getText().toString());
                terminalTest.invoiceNumber = Integer.parseInt(txtInvoiceNumber.getText().toString());

                // CREATE THREAD
                Thread thread = new Thread() {

                    @Override
                    public void run() {

                        // PROCESS PURCHASE
                        terminalTest.hcmPurchase();

                    }
                };

                // START THREAD
                thread.start();


                // INIT TIMER
                logTimer = new Timer();

                // START TIMER AT 500MS
                logTimer.scheduleAtFixedRate(new TimerTask() {
                    @Override
                    public void run() {

                        // CREATE UI THREAD SO I CAN CHANGE UI ELEMENTS WHILE THREAD IS RUNNING
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {

                                // MALE SURE NOT NULL
                                if (terminalTest.connectionLog != null) {

                                    // MAKE SURE IT HAS DATA
                                    if (terminalTest.connectionLog.length() > 1) {

                                        // DISPLAY
                                        txtLog.append(terminalTest.connectionLog);

                                        // CHECK IF CONNCTION HAS ENDED
                                        if (terminalTest.connectionLog.contains("Ended Connection")) {

                                            // STOP TIMER
                                            logTimer.cancel();

                                        }

                                        // CLEAR LOG
                                        terminalTest.connectionLog = "";

                                        // CHECK FOR RESPONSE
                                        if (terminalTest.responseMessage != null) {

                                            // ADD RESPONSE
                                            txtLog.append("\n\n" + terminalTest.responseMessage);

                                        }

                                    }
                                }
                            }
                        });

                    }
                }, 0, 500);

            }


        });


        // CREATE BUTTON CLICK
        ImageButton imgBtnSettings = (ImageButton) findViewById(R.id.imgBtnSettings);
        imgBtnSettings.setOnClickListener((new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                // GET VIEW
                View view = (LayoutInflater.from(Mainscreen.this)).inflate(R.layout.settings, null);

                // CREATE ALERT AND ADD SETTINGS
                AlertDialog.Builder alertBuilder = new AlertDialog.Builder(Mainscreen.this);
                alertBuilder.setView(view);
                final EditText txtIp = (EditText) view.findViewById(R.id.txtIp);
                final EditText txtPort = (EditText) view.findViewById(R.id.txtPort);

                alertBuilder.setCancelable(true).setPositiveButton("Okay", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        // SET IP
                        ip = txtIp.getText().toString();
                        port = Integer.parseInt(txtPort.getText().toString());

                    }
                });
                
                // CREATE POPUP AND SHOW
                Dialog ipDialog = alertBuilder.create();
                ipDialog.show();
                
            }
        }));


    }
}