Advertisement
farble1670

Untitled

Aug 12th, 2016
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.87 KB | None | 0 0
  1. OrderPaidActivity.java:
  2.  
  3. /**
  4.  * Copyright (C) 2016 Clover Network, Inc.
  5.  *
  6.  * Licensed under the Apache License, Version 2.0 (the "License");
  7.  * you may not use this file except in compliance with the License.
  8.  * You may obtain a copy of the License at
  9.  *
  10.  *    http://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing, software
  13.  * distributed under the License is distributed on an "AS IS" BASIS,
  14.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15.  * See the License for the specific language governing permissions and
  16.  * limitations under the License.
  17.  */
  18. package com.clover.android.sdk.examples;
  19.  
  20. import android.accounts.Account;
  21. import android.app.Activity;
  22. import android.app.LoaderManager;
  23. import android.content.CursorLoader;
  24. import android.content.Loader;
  25. import android.database.Cursor;
  26. import android.os.AsyncTask;
  27. import android.os.Bundle;
  28. import android.os.RemoteException;
  29. import android.util.Log;
  30. import android.view.LayoutInflater;
  31. import android.view.View;
  32. import android.view.ViewGroup;
  33. import android.widget.BaseAdapter;
  34. import android.widget.ListView;
  35. import android.widget.TextView;
  36. import com.clover.sdk.util.CloverAccount;
  37. import com.clover.sdk.v1.BindingException;
  38. import com.clover.sdk.v1.ClientException;
  39. import com.clover.sdk.v1.ServiceException;
  40. import com.clover.sdk.v3.order.Order;
  41. import com.clover.sdk.v3.order.OrderCalc;
  42. import com.clover.sdk.v3.order.OrderConnector;
  43. import com.clover.sdk.v3.order.OrderContract;
  44. import com.clover.sdk.v3.payments.Payment;
  45. import com.clover.sdk.v3.payments.Refund;
  46.  
  47. import java.text.DecimalFormat;
  48. import java.text.SimpleDateFormat;
  49. import java.util.ArrayList;
  50. import java.util.Collections;
  51. import java.util.Comparator;
  52. import java.util.Currency;
  53. import java.util.List;
  54. import java.util.Locale;
  55.  
  56. public class OrderPaidActivity extends Activity implements LoaderManager.LoaderCallbacks<Cursor> {
  57.   private static final String TAG = OrderPaidActivity.class.getSimpleName();
  58.  
  59.   private static final int ORDERS_LOADER_ID = 13;
  60.  
  61.   private class OrdersAdapter extends BaseAdapter {
  62.     private final List<Order> orders;
  63.  
  64.     private OrdersAdapter(List<Order> orders) {
  65.       this.orders = orders;
  66.     }
  67.  
  68.     @Override
  69.     public int getCount() {
  70.       return orders.size();
  71.     }
  72.  
  73.     @Override
  74.     public Object getItem(int position) {
  75.       return orders.get(position);
  76.     }
  77.  
  78.     @Override
  79.     public long getItemId(int position) {
  80.       return position;
  81.     }
  82.  
  83.     @Override
  84.     public View getView(int position, View convertView, ViewGroup parent) {
  85.       View view = convertView;
  86.       if (view == null) {
  87.         view = LayoutInflater.from(OrderPaidActivity.this).inflate(R.layout.item_order_paid, null);
  88.       }
  89.  
  90.       Order order = (Order) getItem(position);
  91.  
  92.       TextView orderIdText = (TextView) view.findViewById(R.id.text_order_id);
  93.       orderIdText.setText(order.getId());
  94.  
  95.       TextView orderTotalText = (TextView) view.findViewById(R.id.text_order_total);
  96.       orderTotalText.setText(longToAmountString(Currency.getInstance("USD"), new OrderCalc(order).getTotal(order.getLineItems())));
  97.  
  98.       TextView orderDateText = (TextView) view.findViewById(R.id.text_order_date);
  99.       orderDateText.setText(SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.SHORT, SimpleDateFormat.SHORT).format(order.getClientCreatedTime()));
  100.  
  101.       TextView orderPaidText = (TextView) view.findViewById(R.id.text_order_paid);
  102.       orderPaidText.setText("Paid? " + isPaid(order));
  103.  
  104.       return view;
  105.     }
  106.   }
  107.  
  108.   private OrderConnector orderConnector;
  109.   private ListView ordersList;
  110.  
  111.   private Account account;
  112.  
  113.   @Override
  114.   protected void onCreate(Bundle savedInstanceState) {
  115.     super.onCreate(savedInstanceState);
  116.     setContentView(R.layout.activity_order_paid);
  117.  
  118.     account = CloverAccount.getAccount(this);
  119.     ordersList = (ListView) findViewById(R.id.list_orders);
  120.   }
  121.  
  122.   @Override
  123.   protected void onResume() {
  124.     super.onResume();
  125.  
  126.     orderConnector = new OrderConnector(this, account, null);
  127.     getLoaderManager().restartLoader(ORDERS_LOADER_ID, null, this);
  128.   }
  129.  
  130.   @Override
  131.   protected void onPause() {
  132.     if (orderConnector != null) {
  133.       orderConnector.disconnect();
  134.       orderConnector = null;
  135.     }
  136.     super.onPause();
  137.   }
  138.  
  139.   @Override
  140.   public Loader<Cursor> onCreateLoader(int id, Bundle args) {
  141.     return new CursorLoader(this, OrderContract.Summaries.contentUriWithAccount(account), null, null, null, OrderContract.Summaries.CREATED + " DESC LIMIT 100");
  142.   }
  143.  
  144.   @Override
  145.   public void onLoadFinished(Loader<Cursor> loader, final Cursor data) {
  146.     final List<String> orderIds = new ArrayList<String>();
  147.     if (data != null && data.moveToFirst()) {
  148.       while (!data.isAfterLast()) {
  149.         orderIds.add(data.getString(data.getColumnIndex(OrderContract.Summaries.ID)));
  150.         data.moveToNext();
  151.       }
  152.     }
  153.  
  154.     new AsyncTask<Void,Void,List<Order>>() {
  155.       @Override
  156.       protected List<Order> doInBackground(Void... params) {
  157.  
  158.         List<Order> orders = new ArrayList<Order>();
  159.         for (String orderId: orderIds) {
  160.           try {
  161.             Order o = orderConnector.getOrder(orderId);
  162.             if (o != null) {
  163.               orders.add(o);
  164.               Log.i(TAG, "Loaded order ID: " + orderId);
  165.             }
  166.           } catch (RemoteException | ClientException | BindingException | ServiceException e) {
  167.             e.printStackTrace();
  168.           }
  169.         }
  170.  
  171.         Collections.sort(orders, new Comparator<Order>() {
  172.           @Override
  173.           public int compare(Order lhs, Order rhs) {
  174.             if (lhs.getClientCreatedTime() < rhs.getClientCreatedTime()) {
  175.               return 1;
  176.             }
  177.             if (lhs.getClientCreatedTime() > rhs.getClientCreatedTime()) {
  178.               return -1;
  179.             }
  180.             return 0;
  181.           }
  182.         });
  183.  
  184.         return orders;
  185.       }
  186.  
  187.       @Override
  188.       protected void onPostExecute(List<Order> orders) {
  189.         ordersList.setAdapter(new OrdersAdapter(orders));
  190.       }
  191.     }.execute();
  192.   }
  193.  
  194.   @Override
  195.   public void onLoaderReset(Loader<Cursor> loader) {
  196.   }
  197.  
  198.   private static boolean isPaid(Order order) {
  199.     return (order.isNotNullLineItems() && order.getLineItems().size() > 0 && (order.isNotEmptyPayments() || order.isNotEmptyCredits()) && getAmountLeftToPay(order) <= 0);
  200.   }
  201.  
  202.   public static long getAmountLeftToPay(Order order) {
  203.     long paymentTotal = 0;
  204.     if (order.isNotNullPayments()) {
  205.       for (Payment p : order.getPayments()) {
  206.         paymentTotal += p.getAmount();
  207.       }
  208.     }
  209.     return new OrderCalc(order).getTotal(order.getLineItems()) - paymentTotal + amountRefundedWithoutTip(order);
  210.   }
  211.  
  212.   public static long amountRefundedWithoutTip(Order order) {
  213.     long amountRefundedWithoutTip = 0l;
  214.     if (order.isNotNullRefunds()) {
  215.       for (Refund r : order.getRefunds()) {
  216.         if (r.getAmount() != null) {
  217.           amountRefundedWithoutTip += r.getAmount();
  218.  
  219.           for (Payment p : order.getPayments()) {
  220.             if (p.getId().equals(r.getPayment().getId())) {
  221.               amountRefundedWithoutTip -= calcTipAmount(p);
  222.             }
  223.           }
  224.         }
  225.       }
  226.     }
  227.     return amountRefundedWithoutTip;
  228.   }
  229.  
  230.   public static long calcTipAmount(Payment p) {
  231.     if (p.isNotNullTipAmount()) {
  232.       return p.getTipAmount();
  233.     } else {
  234.       return 0;
  235.     }
  236.   }
  237.  
  238.   public static double longToDecimal(double num, Currency currency) {
  239.     return num / Math.pow(10, currency.getDefaultFractionDigits());
  240.   }
  241.  
  242.  
  243.   // Take a long and convert it to an amount string (i.e 150 -> $1.50)
  244.   public static String longToAmountString(Currency currency, long amt) {
  245.     DecimalFormat decimalFormat = getCurrencyFormatInstance(currency);
  246.     return longToAmountString(currency, amt, decimalFormat);
  247.   }
  248.  
  249.   // Take a long and convert it to an amount string (i.e 150 -> $1.50)
  250.   public static String longToAmountString(Currency currency, long amt, DecimalFormat decimalFormat) {
  251.     return decimalFormat.format(longToDecimal(amt, currency));
  252.   }
  253.  
  254.   private static ThreadLocal<DecimalFormat> DECIMAL_FORMAT = new ThreadLocal<DecimalFormat>() {
  255.     @Override
  256.     protected DecimalFormat initialValue() {
  257.       return (DecimalFormat) DecimalFormat.getCurrencyInstance(Locale.getDefault());
  258.     }
  259.   };
  260.  
  261.   private static DecimalFormat getCurrencyFormatInstance(Currency currency) {
  262.     DecimalFormat format = DECIMAL_FORMAT.get();
  263.     if (format.getCurrency() != currency) {
  264.       format.setCurrency(currency);
  265.     }
  266.     return format;
  267.   }
  268.  
  269. }
  270.  
  271. activity_order_paid.xml:
  272.  
  273. <!--
  274.   ~ Copyright (C) 2013 Clover Network, Inc.
  275.   ~
  276.   ~ Licensed under the Apache License, Version 2.0 (the "License");
  277.   ~ you may not use this file except in compliance with the License.
  278.   ~ You may obtain a copy of the License at
  279.   ~
  280.   ~    http://www.apache.org/licenses/LICENSE-2.0
  281.   ~
  282.   ~ Unless required by applicable law or agreed to in writing, software
  283.   ~ distributed under the License is distributed on an "AS IS" BASIS,
  284.   ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  285.   ~ See the License for the specific language governing permissions and
  286.   ~ limitations under the License.
  287.   -->
  288.  
  289. <RelativeLayout
  290.     xmlns:android="http://schemas.android.com/apk/res/android"
  291.     xmlns:tools="http://schemas.android.com/tools"
  292.     android:layout_width="match_parent"
  293.     android:layout_height="match_parent"
  294.     android:paddingLeft="@dimen/activity_horizontal_margin"
  295.     android:paddingRight="@dimen/activity_horizontal_margin"
  296.     android:paddingTop="@dimen/activity_vertical_margin"
  297.     android:paddingBottom="@dimen/activity_vertical_margin"
  298.     tools:context=".CreateCustomTenderTestActivity">
  299.  
  300.   <ListView
  301.       android:layout_width="match_parent"
  302.       android:layout_height="wrap_content"
  303.       android:id="@+id/list_orders"/>
  304.  
  305. </RelativeLayout>
  306.  
  307. item_order_paid.xml:
  308.  
  309. <?xml version="1.0" encoding="utf-8"?>
  310. <!--
  311.   ~ Copyright (C) 2013 Clover Network, Inc.
  312.   ~
  313.   ~ Licensed under the Apache License, Version 2.0 (the "License");
  314.   ~ you may not use this file except in compliance with the License.
  315.   ~ You may obtain a copy of the License at
  316.   ~
  317.   ~    http://www.apache.org/licenses/LICENSE-2.0
  318.   ~
  319.   ~ Unless required by applicable law or agreed to in writing, software
  320.   ~ distributed under the License is distributed on an "AS IS" BASIS,
  321.   ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  322.   ~ See the License for the specific language governing permissions and
  323.   ~ limitations under the License.
  324.   -->
  325.  
  326. <LinearLayout
  327.     xmlns:android="http://schemas.android.com/apk/res/android"
  328.     android:orientation="horizontal"
  329.     android:layout_width="match_parent"
  330.     android:layout_height="wrap_content">
  331.  
  332.   <TextView
  333.       android:layout_width="0dp"
  334.       android:layout_weight="1"
  335.       android:layout_height="wrap_content"
  336.       android:textAppearance="?android:attr/textAppearanceLarge"
  337.       android:fontFamily="monospace"
  338.       android:id="@+id/text_order_id"/>
  339.  
  340.   <TextView
  341.       android:layout_width="0dp"
  342.       android:layout_weight="1"
  343.       android:layout_height="wrap_content"
  344.       android:textAppearance="?android:attr/textAppearanceLarge"
  345.       android:fontFamily="monospace"
  346.       android:id="@+id/text_order_total"/>
  347.  
  348.   <TextView
  349.       android:layout_width="0dp"
  350.       android:layout_weight="1"
  351.       android:layout_height="wrap_content"
  352.       android:textAppearance="?android:attr/textAppearanceLarge"
  353.       android:fontFamily="monospace"
  354.       android:id="@+id/text_order_date"/>
  355.  
  356.   <TextView
  357.       android:layout_width="0dp"
  358.       android:layout_weight="1"
  359.       android:layout_height="wrap_content"
  360.       android:textAppearance="?android:attr/textAppearanceLarge"
  361.       android:fontFamily="monospace"
  362.       android:id="@+id/text_order_paid"/>
  363. </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement