Android Open Source - Android-Print-SDK Order Receipt Activity






From Project

Back to project page Android-Print-SDK.

License

The source code is released under:

MIT License

If you think the Android project Android-Print-SDK listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package ly.kite.checkout;
//w  w w  .j  a  v  a 2s .  co  m
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import ly.kite.print.PrintOrder;
import ly.kite.print.PrintOrderSubmissionListener;
import ly.kite.R;

public class OrderReceiptActivity extends Activity {

    public static final String EXTRA_PRINT_ORDER = "ly.kite.EXTRA_PRINT_ORDER";

    private PrintOrder printOrder;

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

        this.printOrder = (PrintOrder) getIntent().getParcelableExtra(EXTRA_PRINT_ORDER);

        if (printOrder == null) {
            throw new IllegalArgumentException("You must specify a PrintOrder object extra in the intent used to start the OrderReceiptActivity");
        }

        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment(printOrder))
                    .commit();
        }

        getActionBar().setDisplayHomeAsUpEnabled(true);

        // Show an error dialog if we're arriving with a recent Payment success but we failed to successfully print the order.
        if (!printOrder.isPrinted() && getParent() instanceof PaymentActivity && printOrder.getLastPrintSubmissionError() != null) {
            showErrorDialog(printOrder.getLastPrintSubmissionError().getMessage());
        }

    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            setResult(Activity.RESULT_OK);
            finish();
            return true;
        }
        return super.onMenuItemSelected(featureId, item);
    }

    @Override
    public void onBackPressed() {
        setResult(Activity.RESULT_OK);
        super.onBackPressed();
    }

    public void onButtonRetryPrintClicked(View view) {
        final ProgressDialog dialog = new ProgressDialog(this);
        dialog.setCancelable(false);
        dialog.setIndeterminate(false);
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dialog.setTitle("Processing");
        dialog.setMessage("One moment...");
        dialog.setMax(100);
        dialog.show();

        printOrder.submitForPrinting(this, new PrintOrderSubmissionListener() {
            @Override
            public void onProgress(PrintOrder printOrder, int totalAssetsUploaded, int totalAssetsToUpload, long totalAssetBytesWritten, long totalAssetBytesExpectedToWrite, long totalBytesWritten, long totalBytesExpectedToWrite) {
                if (Looper.myLooper() != Looper.getMainLooper()) throw new AssertionError("Should be calling back on the main thread");
                final float step = (1.0f / totalAssetsToUpload);
                float progress = totalAssetsUploaded * step + (totalAssetBytesWritten / (float) totalAssetBytesExpectedToWrite) * step;
                dialog.setProgress((int) (totalAssetsUploaded * step * 100));
                dialog.setSecondaryProgress((int) (progress * 100));
                dialog.setMessage("Uploading images");
            }

            @Override
            public void onSubmissionComplete(PrintOrder printOrder, String orderIdReceipt) {
                if (Looper.myLooper() != Looper.getMainLooper()) throw new AssertionError("Should be calling back on the main thread");
                printOrder.saveToHistory(OrderReceiptActivity.this);
                dialog.dismiss();

                Button retryPrintButton = (Button) findViewById(R.id.button_retry_print);
                ImageView headerView = (ImageView) findViewById(R.id.image_view_order_receipt_header);
                TextView orderView = (TextView) findViewById(R.id.text_view_order_id);
                orderView.setText(printOrder.getReceipt());
                headerView.setImageResource(R.drawable.receipt_success);
                retryPrintButton.setVisibility(View.GONE);
            }

            @Override
            public void onError(PrintOrder printOrder, Exception error) {
                if (Looper.myLooper() != Looper.getMainLooper()) throw new AssertionError("Should be calling back on the main thread");
                dialog.dismiss();
                showErrorDialog(error.getMessage());
            }
        });
    }

    private void showErrorDialog(String message) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Oops!").setMessage(message).setPositiveButton("OK", null);
        Dialog d = builder.create();
        d.show();
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        private final PrintOrder printOrder;

        public PlaceholderFragment(PrintOrder printOrder) {
            this.printOrder = printOrder;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_order_receipt, container, false);
            return rootView;
        }

        @Override
        public void onViewCreated(View view, Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);

            ListView l = (ListView) view.findViewById(R.id.list_view_order_summary);
            l.setAdapter(new PrintOrderSummaryListAdapter(printOrder));

            TextView orderView = (TextView) view.findViewById(R.id.text_view_order_id);
            orderView.setText(printOrder.getReceipt());

            Button retryPrintButton = (Button) view.findViewById(R.id.button_retry_print);
            ImageView headerView = (ImageView) view.findViewById(R.id.image_view_order_receipt_header);
            if (this.printOrder.isPrinted()) {
                headerView.setImageResource(R.drawable.receipt_success);
                retryPrintButton.setVisibility(View.GONE);
            } else {
                headerView.setImageResource(R.drawable.receipt_failure);
                retryPrintButton.setVisibility(View.VISIBLE);

                StringBuilder receipt = new StringBuilder();
                if (printOrder.getProofOfPayment() != null) {
                    receipt.append(printOrder.getProofOfPayment());
                }

                if (printOrder.getPromoCode() != null) {
                    if (receipt.length() > 0) {
                        receipt.append(" ");
                    }

                    receipt.append("PROMO-").append(printOrder.getPromoCode());
                }

                orderView.setText(receipt);
            }
        }
    }

}




Java Source Code List

co.oceanlabs.sample.MainActivity.java
ly.kite.address.AddressBookActivity.java
ly.kite.address.AddressEditActivity.java
ly.kite.address.AddressSearchActivity.java
ly.kite.address.AddressSearchRequestListener.java
ly.kite.address.AddressSearchRequest.java
ly.kite.address.Address.java
ly.kite.address.Country.java
ly.kite.checkout.CheckoutActivity.java
ly.kite.checkout.OrderReceiptActivity.java
ly.kite.checkout.PaymentActivity.java
ly.kite.checkout.PrintOrderSummaryListAdapter.java
ly.kite.payment.CheckPromoCodeRequestListener.java
ly.kite.payment.CheckPromoRequest.java
ly.kite.payment.PayPalCardChargeListener.java
ly.kite.payment.PayPalCardVaultStorageListener.java
ly.kite.payment.PayPalCard.java
ly.kite.print.ApplyPromoCodeListener.java
ly.kite.print.AssetGetBytesLengthListener.java
ly.kite.print.AssetGetBytesListener.java
ly.kite.print.AssetUploadRequestListener.java
ly.kite.print.AssetUploadRequest.java
ly.kite.print.Asset.java
ly.kite.print.BaseRequest.java
ly.kite.print.KitePrintSDKException.java
ly.kite.print.KitePrintSDK.java
ly.kite.print.PrintJob.java
ly.kite.print.PrintOrderSubmissionListener.java
ly.kite.print.PrintOrder.java
ly.kite.print.PrintsPrintJob.java
ly.kite.print.ProductType.java
ly.kite.print.SubmitPrintOrderRequestListener.java
ly.kite.print.SubmitPrintOrderRequest.java