SimpleActivity

PHOTO EMBED

Wed Aug 10 2022 01:54:02 GMT+0000 (Coordinated Universal Time)

Saved by @malibaig #java

package com.sunmi.printerhelper.activity;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.sunmi.peripheral.printer.SunmiPrinterService;
import com.sunmi.printerhelper.R;
import com.sunmi.printerhelper.utils.BluetoothUtil;
import com.sunmi.printerhelper.utils.SunmiPrinter;

public class SimpleActivity extends BaseActivity implements View.OnClickListener {
    SunmiPrinterService sunmiPrinter;
    SunmiPrinter printer;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        printer = SunmiPrinter.instance();
        printer.connectWithSunmiPrinter(this);

        setContentView(R.layout.activity_simple);
        setMyTitle("Simple Activity");


        TextView printerInfoTV = findViewById(R.id.tv_yes);
        TextView paperTV = findViewById(R.id.tv_paper);

        boolean res = printer.doesPrinterExist(this);
        String paperSize =  printer.getPaperSize();

        if(res) printerInfoTV.setText("YES"); else printerInfoTV.setText("NO");
        paperTV.setText(paperSize);

        checkAfter2000m();

        Button simpleBut = findViewById(R.id.btn_simple);
        Button exampleBut = findViewById(R.id.btn_example);
        Button reportBut = findViewById(R.id.btn_report);

        simpleBut.setOnClickListener(this);
        exampleBut.setOnClickListener(this);
        reportBut.setOnClickListener(this);

    }

    private void setPrinterInfo(){
        TextView printerInfoTV = findViewById(R.id.tv_yes_2);
        TextView paperTV = findViewById(R.id.tv_paper_2);

        boolean res = printer.doesPrinterExist(this);
        String paperSize =  printer.getPaperSize();

        if(res) printerInfoTV.setText("YES"); else printerInfoTV.setText("NO");
        paperTV.setText(paperSize);
    }

    private void checkAfter2000m() {
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                setPrinterInfo();
            }
        }, 2000);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_simple:
                Toast.makeText(this, "Print Simple Example Clicked ", Toast.LENGTH_SHORT).show();
                onSimpleButtonClicked();
                break;
            case R.id.btn_example:
                Toast.makeText(this, "Print Complex Example Clicked", Toast.LENGTH_LONG).show();
                // onComplexButtonClicked();
                break;
            case R.id.btn_report:
                Toast.makeText(this, "Print Simply Report Clicked ", Toast.LENGTH_SHORT).show();
                // onSimplyReportButtonClicked();
                break;
        }

    }


    public void onSimpleButtonClicked() {
        if (sunmiPrinter == null) {
            Toast.makeText(this, "SunmiPrinter is Null SIMPLE BUTTON!!!!", Toast.LENGTH_SHORT).show();
            return;
        }

        Toast.makeText(this, "SunmiPrinter is NOT null!!!", Toast.LENGTH_SHORT).show();

        printer.printText("Start of Sample");
        printer.printTextLeftRight("LEFT", "RIGHT");
        printer.printText("Alignment should be normal");

        printer.printTextInCenter("CENTER\n");
        printer.printText("Alignment should be normal\n");

        // sunmiPrinter.sendRAWData(ESCUtil.boldOn(), null);
        // sunmiPrinter.printText("Bold Feature is ON\n", null);
        // sunmiPrinter.sendRAWData(ESCUtil.boldOff(), null);
        printer.printTextBold("This Text Should be bold!!!\n");
        printer.printText("Text should not be bold\n");


        printer.dashesPlusNextLine();
        printer.printText("This is default Font Size\n");
        printer.setFontSize(16f);
        printer.printText("This is 16 Font Size\n");
        printer.setFontSize(32f);
        printer.printText("This is 32 Font Size\n");
        printer.setFontSize(16f);
        printer.printText("This is again 16 Font Size\n");

        printer.dashesPlusNextLine();
        printer.printTextFontSize("Font Size is increased to 30\n", 30f);
        printer.printText("Does Font Size return to normal\n");
        printer.setFontSize(16f);
        printer.printText("Font Size must be normal\n");
        printer.dashesPlusNextLine();

        //sunmiPrinter.printText("FONT size is increased to 40\n", null);
        //sunmiPrinter.printTextWithFont("ITEM:           MEET", null, 40, null);
        //sunmiPrinter.printTextWithFont("PRICE:          $59\b", null, 40, null);
        //sunmiPrinter.setFontSize(36, null);

        printer.setAlignment(SunmiPrinter.ALIGNMENT.CENTER);
        printer.printTextFontSize("END OF PRINT", 40f);
        printer.setAlignment(SunmiPrinter.ALIGNMENT.LEFT);
        printer.print3Line();

    }

    private void onComplexButtonClicked() {
        if (!BluetoothUtil.isBlueToothPrinter) {
            // SunmiPrinter.instance().printExample(this);
        } else {
            Toast.makeText(this, "Is Bluetooth is on ???", Toast.LENGTH_SHORT).show();
        }
    }

    /*private void onSimplyReportButtonClicked() {
        if (sunmiPrinter == null) {
            Toast.makeText(this, "SunmiPrinter is Null REPORT!!!!", Toast.LENGTH_SHORT).show();
            return;
        }

        String transactionsLabel = "Transaction";
        String turnoverLabel = "TurnOver";
        String netSaleLabel = "NetSale";
        String salesVatLabel = "Sales VAT";
        String returnsLabel = "Returns";
        String returnsVatLabel = "Returns VAT";

        String str_breakdown_by_payment_type = "Breakdown by payment type";
        String str_sales_totals_by_department = "Sales totals by department";
        String str_total_sales = "Total sales:";
        String str_net_sales = "Net sales:";
        String str_vat_ = "VAT:";
        String str_breakdown_by_vat_rate = "Breakdown by VAT Rate";
        String str_breakdown_by_invoice_type = "Break down by Invoice type";

        try {
            sunmiPrinter.printerInit(null);

            Toast.makeText(this, "TRY BLOCK REPORT!!!!", Toast.LENGTH_SHORT).show();

            // itemsString.append("[C]<font size='tall'>Report</font>").append("\n");
            // itemsString.append(getLineSeparator()).append("\n");
            sunmiPrinter.setAlignment(ALIGN_CENTER, null);
            sunmiPrinter.printTextWithFont("Report\n", null, 40, null);

            sunmiPrinter.setFontSize(30, null);
            sunmiPrinter.printText(getLineSeparator() + "\n", null);

            // itemsString.append("[L]Date From").append("[R]" + invoiceReport.getDateFrom()).append("\n");
            sunmiPrinter.setAlignment(ALIGN_LEFT, null);
            sunmiPrinter.printText("Date From", null);
            sunmiPrinter.setAlignment(ALIGN_RIGHT, null);
            sunmiPrinter.printText("Monday, August 8, 2022\n", null);


            // itemsString.append("[L]Date To").append("[R]" + invoiceReport.getDateTo()).append("\n");
            sunmiPrinter.setAlignment(ALIGN_LEFT, null);
            sunmiPrinter.printText("Date To", null);
            sunmiPrinter.setAlignment(ALIGN_RIGHT, null);
            sunmiPrinter.printText("Monday, August 8, 2022\n", null);

            // itemsString.append(getLineSeparator()).append("\n");
            // itemsString.append("\n");
            sunmiPrinter.printText(getLineSeparator() + "\n\n", null);

            // itemsString.append("[L]<b>" + "Total" + "</b>").append("\n");
            sunmiPrinter.setAlignment(ALIGN_LEFT, null);
            sunmiPrinter.sendRAWData(ESCUtil.boldOn(), null);
            sunmiPrinter.printText("Total", null);
            sunmiPrinter.sendRAWData(ESCUtil.boldOff(), null);

            // itemsString.append("[L]" + transactionsLabel + "").append("[R]" + invoiceReport.getTransactions()).append("\n");
            sunmiPrinter.setAlignment(ALIGN_LEFT, null);
            sunmiPrinter.printText(" " + transactionsLabel + " ", null);
            sunmiPrinter.setAlignment(ALIGN_RIGHT, null);
            sunmiPrinter.printText(" " + transactionsLabel + " \n", null);

            // itemsString.append("[L]" + turnoverLabel + "").append("[R]" + invoiceReport.getTurnOver()).append("\n");
            sunmiPrinter.setAlignment(ALIGN_LEFT, null);
            sunmiPrinter.printText(" " + turnoverLabel + " ", null);
            sunmiPrinter.setAlignment(ALIGN_RIGHT, null);
            sunmiPrinter.printText(" " + turnoverLabel + " \n", null);

            // itemsString.append("[L]" + netSaleLabel + "").append("[R]" + invoiceReport.getNetSales()).append("\n");
            sunmiPrinter.setAlignment(ALIGN_LEFT, null);
            sunmiPrinter.printText(" " + netSaleLabel + " ", null);
            sunmiPrinter.setAlignment(ALIGN_RIGHT, null);
            sunmiPrinter.printText(" " + netSaleLabel + " \n", null);


            String txts[] = new String[]{salesVatLabel, salesVatLabel + "\n"};
            int width[] = new int[]{1, 1};
            int align[] = new int[]{0, 2};
            sunmiPrinter.printColumnsString(txts, width, align, null);
            // itemsString.append("[L]" + salesVatLabel + "").append("[R]" + invoiceReport.getSalesVAT()).append("\n");


            // itemsString.append("[L]" + returnsLabel + "").append("[R]" + invoiceReport.getReturns()).append("\n");
            String txts2[] = new String[]{returnsLabel, returnsLabel + "\n"};
            sunmiPrinter.printColumnsString(txts2, width, align, null);

            // itemsString.append("[L]" + returnsVatLabel + "").append("[R]" + invoiceReport.getReturnVAT()).append("\n");
            // itemsString.append("\n");
            String txts3[] = new String[]{returnsVatLabel, returnsVatLabel + "\n"};
            sunmiPrinter.printColumnsString(txts3, width, align, null);

            // itemsString.append(getLineSeparator()).append("\n");
            // itemsString.append("[C]").append("End of Report").append("\n");
            // itemsString.append(getLineSeparator()).append("\n");

            sunmiPrinter.printText(getLineSeparator() + "\n", null);
            sunmiPrinter.setAlignment(ALIGN_CENTER, null);
            sunmiPrinter.printText("End of Report\n", null);

            SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yy HH:mm");
            String currentDateandTime = sdf.format(new Date());

            //itemsString.append("[L]" + "Printed on:" + "").append("[R]" + currentDateandTime).append("\n");
            String txts4[] = new String[]{"Printed on:", currentDateandTime + "\n"};
            sunmiPrinter.printColumnsString(txts4, width, align, null);


            // itemsString.append("\n");
            // itemsString.append("\n");
            // itemsString.append("\n");

            sunmiPrinter.lineWrap(3, null);

        } catch (RemoteException e) {
            Toast.makeText(this, "CATCH BLOCK REPORT!!!!", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }

    }*/

    public static String getLineSeparator() {
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < 32; i++) {
            str.append("-");
        }
        return str.toString();
    }

    /*public void otherWayOfGettingSunmiPrinterService(){
        try {
            boolean result = InnerPrinterManager.getInstance().bindService(this, new InnerPrinterCallback() {
                @Override
                protected void onConnected(SunmiPrinterService service) {
                    Toast.makeText(SimpleActivity.this, "Printer Connected", Toast.LENGTH_SHORT).show();
                    sunmiPrinter = service;
                }

                @Override
                protected void onDisconnected() {

                }
            });
            Log.e("SUNMIDEMO", "Result" + result);
        } catch (InnerPrinterException e) {
            e.printStackTrace();
        }
    }*/

}

content_copyCOPY