Android Open Source - utexas-utilities Transactions Fragment






From Project

Back to project page utexas-utilities.

License

The source code is released under:

Apache License

If you think the Android project utexas-utilities 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 com.nasageek.utexasutilities.fragments;
/*from w  ww .  j av a  2s .c om*/
import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockFragment;
import com.foound.widget.AmazingListView;
import com.nasageek.utexasutilities.AsyncTask;
import com.nasageek.utexasutilities.AuthCookie;
import com.nasageek.utexasutilities.R;
import com.nasageek.utexasutilities.TempLoginException;
import com.nasageek.utexasutilities.UTilitiesApplication;
import com.nasageek.utexasutilities.Utility;
import com.nasageek.utexasutilities.activities.LoginActivity;
import com.nasageek.utexasutilities.adapters.TransactionAdapter;
import com.nasageek.utexasutilities.model.Transaction;

import org.apache.http.HttpResponse;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.nasageek.utexasutilities.UTilitiesApplication.UTD_AUTH_COOKIE_KEY;

//TODO: last transaction doesn't show when loading dialog is present at the bottom, low priority fix

public class TransactionsFragment extends SherlockFragment {
    private DefaultHttpClient httpclient;
    private LinearLayout t_pb_ll;
    private AmazingListView tlv;
    private ArrayList<Transaction> transactionlist;
    private TransactionAdapter ta;
    private TextView balanceLabelView, balanceView;
    private View balanceLabelSeparatorView;
    private TextView etv;
    private LinearLayout ell;

    private List<BasicNameValuePair> postdata;
    // private SherlockFragmentActivity parentAct;

    private View vg;
    private String balance;
    private fetchTransactionDataTask fetch;

    public enum TransactionType {
        Bevo, Dinein
    }

    private TransactionType mType;

    public TransactionsFragment() {
    }

    public static TransactionsFragment newInstance(String title, TransactionType type) {
        TransactionsFragment f = new TransactionsFragment();

        Bundle args = new Bundle();
        args.putSerializable("type", type);
        args.putString("title", title);
        f.setArguments(args);

        return f;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        vg = inflater.inflate(R.layout.transactions_fragment_layout, container, false);

        tlv = (AmazingListView) vg.findViewById(R.id.transactions_listview);
        t_pb_ll = (LinearLayout) vg.findViewById(R.id.trans_progressbar_ll);
        etv = (TextView) vg.findViewById(R.id.tv_failure);
        ell = (LinearLayout) vg.findViewById(R.id.trans_error);
        balanceLabelView = (TextView) vg.findViewById(R.id.balance_label_tv);
        balanceLabelSeparatorView = vg.findViewById(R.id.balance_label_separator);
        balanceView = (TextView) vg.findViewById(R.id.balance_tv);

        /*
         * if(TransactionType.Bevo.equals(mType))
         * balanceLabelView.setText("Bevo Bucks "); else
         * if(TransactionType.Dinein.equals(mType))
         * balanceLabelView.setText("Dine In Dollars ");
         */
        if (!"".equals(balance)) {
            balanceView.setText(balance);
        }

        tlv.setLoadingView(inflater.inflate(R.layout.loading_content_layout, null));
        tlv.setAdapter(ta);

        if (ta.getCount() == 0) {
            parser(false);
        }

        return vg;

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);

        postdata = new ArrayList<BasicNameValuePair>();
        mType = (TransactionType) getArguments().getSerializable("type");
        if (TransactionType.Bevo.equals(mType)) {
            postdata.add(new BasicNameValuePair("sRequestSw", "B"));
        } else if (TransactionType.Dinein.equals(mType)) {
            postdata.add(new BasicNameValuePair("rRequestSw", "B"));
        }

        if (savedInstanceState == null) {
            transactionlist = new ArrayList<Transaction>();
            balance = "";
        } else {
            transactionlist = savedInstanceState.getParcelableArrayList("transactions");
            // someone was crashing with a null transactionlist, shouldn't be
            // happening
            // but this should fix it regardless
            if (transactionlist == null) {
                transactionlist = new ArrayList<Transaction>();
            }
            balance = savedInstanceState.getString("balance");
        }

        ta = new TransactionAdapter(getActivity(), this, transactionlist);

    }

    @Override
    public void onSaveInstanceState(Bundle out) {
        super.onSaveInstanceState(out);
        out.putParcelableArrayList("transactions", transactionlist);
        out.putString("balance", balance);
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public void parser(boolean refresh) {
        httpclient = new DefaultHttpClient();
        httpclient.getCookieStore().clear();

        BasicClientCookie screen = new BasicClientCookie("webBrowserSize", "B");
        screen.setDomain(".utexas.edu");
        httpclient.getCookieStore().addCookie(screen);

        fetch = new fetchTransactionDataTask(httpclient, refresh);
        Utility.parallelExecute(fetch, false);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (fetch != null) {
            fetch.cancel(true);
        }
    }

    public void refresh() {
        // tlv.setVisibility(View.GONE);
        // etv.setVisibility(View.GONE);
        // t_pb_ll.setVisibility(View.VISIBLE);
        if (fetch != null) {
            fetch.cancel(true);
            fetch = null;
        }
        transactionlist.clear();
        balance = "";
        postdata.clear();

        if (TransactionType.Bevo.equals(mType)) {
            postdata.add(new BasicNameValuePair("sRequestSw", "B"));
        } else if (TransactionType.Dinein.equals(mType)) {
            postdata.add(new BasicNameValuePair("rRequestSw", "B"));
        }

        parser(true);
        ta.resetPage();
        tlv.setSelectionFromTop(0, 0);
    }

    private class fetchTransactionDataTask extends AsyncTask<Boolean, Void, Character> {
        private DefaultHttpClient client;
        private boolean refresh;
        private String errorMsg;
        private ArrayList<Transaction> tempTransactionList;

        public fetchTransactionDataTask(DefaultHttpClient client, boolean refresh) {
            this.client = client;
            this.refresh = refresh;
        }

        @Override
        protected void onPreExecute() {
            // only show the loading view if we're loading the first page of
            // transactions or refreshing
            if (ta.page == 1 || refresh) {
                t_pb_ll.setVisibility(View.VISIBLE);
                tlv.setVisibility(View.GONE);
                ell.setVisibility(View.GONE);
                balanceLabelView.setVisibility(View.GONE);
                balanceLabelSeparatorView.setVisibility(View.GONE);
                balanceView.setVisibility(View.GONE);
            }
        }

        @Override
        protected Character doInBackground(Boolean... params) {
            Boolean recursing = params[0];
            AuthCookie utdAuthCookie = ((UTilitiesApplication) getActivity().getApplication())
                    .getAuthCookie(UTD_AUTH_COOKIE_KEY);
            BasicClientCookie cookie = new BasicClientCookie(utdAuthCookie.getAuthCookieKey(),
                    utdAuthCookie.getAuthCookieVal());
            cookie.setDomain(".utexas.edu");
            httpclient.getCookieStore().addCookie(cookie);

            HttpPost hpost = new HttpPost("https://utdirect.utexas.edu/hfis/transactions.WBX");
            String pagedata = "";
            tempTransactionList = new ArrayList<Transaction>();
            try {
                hpost.setEntity(new UrlEncodedFormEntity(postdata));
                HttpResponse response = client.execute(hpost);
                pagedata = EntityUtils.toString(response.getEntity());
            } catch (Exception e) {
                e.printStackTrace();
                errorMsg = "UTilities could not fetch transaction data.  Try refreshing.";
                cancel(true);
                return null;
            }

            if (pagedata.contains("<title>UT EID Login</title>")) {
                errorMsg = "You've been logged out of UTDirect, back out and log in again.";
                if (getActivity() != null) {
                    UTilitiesApplication mApp = (UTilitiesApplication) getActivity().getApplication();
                    if (!recursing) {
                        try {
                            mApp.getAuthCookie(UTD_AUTH_COOKIE_KEY).logout();
                            mApp.getAuthCookie(UTD_AUTH_COOKIE_KEY).login();
                        } catch (IOException e) {
                            errorMsg
                                    = "UTilities could not fetch your transaction data.  Try refreshing.";
                            cancel(true);
                            e.printStackTrace();
                            return null;
                        } catch (TempLoginException tle) {
                            /*
                            ooooh boy is this lazy. I'd rather not init SharedPreferences here
                            to check if persistent login is on, so we'll just catch the exception
                             */
                            Intent login = new Intent(getActivity(), LoginActivity.class);
                            login.putExtra("activity", getActivity().getIntent().getComponent()
                                    .getClassName());
                            login.putExtra("service", 'u');
                            getActivity().startActivity(login);
                            getActivity().finish();
                            errorMsg = "Session expired, please log in again";
                            cancel(true);
                            return null;
                        }
                        return doInBackground(true);
                    } else {
                      mApp.logoutAll();
                    }
                }
                cancel(true);
                return null;
            }

            Pattern reasonPattern = Pattern.compile("\"center\">\\s+(.*?)\\s*<");
            Matcher reasonMatcher = reasonPattern.matcher(pagedata);

            Pattern costPattern = Pattern.compile("\"right\">\\s*(.*)</td>\\s*<td");
            Matcher costMatcher = costPattern.matcher(pagedata);

            Pattern datePattern = Pattern.compile("\"left\">\\s*?(\\S+)");
            Matcher dateMatcher = datePattern.matcher(pagedata);

            Pattern balancePattern = Pattern.compile("\"right\">\\s*(.*)</td>\\s*</tr");
            Matcher balanceMatcher = balancePattern.matcher(pagedata);

            if (balanceMatcher.find() && ta.page == 1) {
                balance = balanceMatcher.group(1);
            }
            while (reasonMatcher.find() && costMatcher.find() && dateMatcher.find()
                    && !this.isCancelled()) {
                Transaction tran = new Transaction(reasonMatcher.group(1).trim(), costMatcher
                        .group(1).replaceAll("\\s", ""), dateMatcher.group(1));
                tempTransactionList.add(tran);
            }
            // check for additional pages
            if (pagedata.contains("<form name=\"next\"") && !this.isCancelled()) {
                Pattern namePattern = Pattern.compile("sNameFL\".*?value=\"(.*?)\"");
                Matcher nameMatcher = namePattern.matcher(pagedata);
                Pattern nextTransPattern = Pattern.compile("nexttransid\".*?value=\"(.*?)\"");
                Matcher nextTransMatcher = nextTransPattern.matcher(pagedata);
                Pattern dateTimePattern = Pattern.compile("sStartDateTime\".*?value=\"(.*?)\"");
                Matcher dateTimeMatcher = dateTimePattern.matcher(pagedata);
                if (nameMatcher.find() && nextTransMatcher.find() && dateTimeMatcher.find()
                        && !this.isCancelled()) {
                    postdata.clear();
                    postdata.add(new BasicNameValuePair("sNameFL", nameMatcher.group(1)));
                    postdata.add(new BasicNameValuePair("nexttransid", nextTransMatcher.group(1)));
                    if (TransactionType.Bevo.equals(mType)) {
                        postdata.add(new BasicNameValuePair("sRequestSw", "B"));
                    } else if (TransactionType.Dinein.equals(mType)) {
                        postdata.add(new BasicNameValuePair("rRequestSw", "B"));
                    }
                    postdata.add(new BasicNameValuePair("sStartDateTime", dateTimeMatcher.group(1)));
                }
                return 'm';
            } else {
                return 'n';
            }
        }

        @Override
        protected void onPostExecute(Character result) {
            if (!this.isCancelled()) {
                transactionlist.addAll(tempTransactionList);
                ta.notifyDataSetChanged();
                ta.updateHeaders();
                ta.notifyDataSetChanged();
                int index = tlv.getFirstVisiblePosition();
                View v = tlv.getChildAt(0);
                int top = (v == null) ? 0 : v.getTop();
                if (result == 'm') {
                    ta.notifyMayHaveMorePages();
                }
                if (result == 'n') {
                    ta.notifyNoMorePages();
                }
                if (!refresh) {
                    tlv.setSelectionFromTop(index, top);
                } else {
                    tlv.setSelection(0);
                }

                if (transactionlist.isEmpty()) {
                    balanceLabelView.setText(getResources().getText(R.string.balance_tabs_no_balance));
                } else {
                    balanceView.setText(balance);
                }

                t_pb_ll.setVisibility(View.GONE);
                ell.setVisibility(View.GONE);
                tlv.setVisibility(View.VISIBLE);
                balanceLabelView.setVisibility(View.VISIBLE);
                balanceLabelSeparatorView.setVisibility(transactionlist.isEmpty() ? View.GONE : View.VISIBLE);
                balanceView.setVisibility(View.VISIBLE);
            }
        }

        @Override
        protected void onCancelled(Character nullIfError) {
            if (nullIfError == null) {
                if (ta.page == 1) { // if the first page fails just hide
                                    // everything
                    // etv off center, not sure if worth hiding the balance
                    // stuff to get it centered
                    etv.setText(errorMsg);
                    t_pb_ll.setVisibility(View.GONE);
                    tlv.setVisibility(View.GONE);
                    balanceLabelView.setVisibility(View.GONE);
                    balanceLabelSeparatorView.setVisibility(View.GONE);
                    balanceView.setVisibility(View.GONE);
                    ell.setVisibility(View.VISIBLE);
                } else { // on later pages we should let them see what's already
                         // loaded
                         // got an NPE here, seems like a race condition where
                         // cancel is called externally
                    if (getActivity() != null) {
                        Toast.makeText(getActivity(), errorMsg, Toast.LENGTH_SHORT).show();
                    }
                    ta.notifyNoMorePages();
                    ta.notifyDataSetChanged();
                }
            }
        }
    }
}




Java Source Code List

com.google.maps.android.ui.MyIconGenerator.java
com.mapsaurus.paneslayout.ActivityDelegate.java
com.mapsaurus.paneslayout.FragmentLauncher.java
com.mapsaurus.paneslayout.MenuItemWrapper.java
com.mapsaurus.paneslayout.MySlidingPaneLayout.java
com.mapsaurus.paneslayout.PanesActivity.java
com.mapsaurus.paneslayout.PanesLayout.java
com.mapsaurus.paneslayout.PanesSizer.java
com.mapsaurus.paneslayout.PhoneDelegate.java
com.mapsaurus.paneslayout.SimpleScrollView.java
com.mapsaurus.paneslayout.TabletDelegate.java
com.nasageek.utexasutilities.AsyncTask.java
com.nasageek.utexasutilities.AttachmentDownloadService.java
com.nasageek.utexasutilities.AuthCookie.java
com.nasageek.utexasutilities.BlackboardDashboardXmlParser.java
com.nasageek.utexasutilities.BuildingDatabase.java
com.nasageek.utexasutilities.BuildingProvider.java
com.nasageek.utexasutilities.BuildingSaxHandler.java
com.nasageek.utexasutilities.ChangeLog.java
com.nasageek.utexasutilities.ChangeableContextTask.java
com.nasageek.utexasutilities.CourseMapSaxHandler.java
com.nasageek.utexasutilities.FixedListPreference.java
com.nasageek.utexasutilities.LoginWebViewClient.java
com.nasageek.utexasutilities.MarkerManager.java
com.nasageek.utexasutilities.MyPair.java
com.nasageek.utexasutilities.MyScrollView.java
com.nasageek.utexasutilities.PlacemarkSaxHandler.java
com.nasageek.utexasutilities.PnaAuthCookie.java
com.nasageek.utexasutilities.RouteSaxHandler.java
com.nasageek.utexasutilities.SecurePreferences.java
com.nasageek.utexasutilities.TempLoginException.java
com.nasageek.utexasutilities.UTilitiesApplication.java
com.nasageek.utexasutilities.UtdAuthCookie.java
com.nasageek.utexasutilities.Utility.java
com.nasageek.utexasutilities.WrappedViewPager.java
com.nasageek.utexasutilities.WrappingSlidingDrawer.java
com.nasageek.utexasutilities.activities.AboutMeActivity.java
com.nasageek.utexasutilities.activities.BalanceActivity.java
com.nasageek.utexasutilities.activities.BlackboardPanesActivity.java
com.nasageek.utexasutilities.activities.CampusMapActivity.java
com.nasageek.utexasutilities.activities.DataUsageActivity.java
com.nasageek.utexasutilities.activities.LoginActivity.java
com.nasageek.utexasutilities.activities.MenuActivity.java
com.nasageek.utexasutilities.activities.NutritionInfoActivity.java
com.nasageek.utexasutilities.activities.Preferences.java
com.nasageek.utexasutilities.activities.ScheduleActivity.java
com.nasageek.utexasutilities.activities.UTilitiesActivity.java
com.nasageek.utexasutilities.adapters.BBClassAdapter.java
com.nasageek.utexasutilities.adapters.CourseMapAdapter.java
com.nasageek.utexasutilities.adapters.MultiPanePagerAdapter.java
com.nasageek.utexasutilities.adapters.MyFragmentPagerAdapter.java
com.nasageek.utexasutilities.adapters.ScheduleClassAdapter.java
com.nasageek.utexasutilities.adapters.TransactionAdapter.java
com.nasageek.utexasutilities.fragments.ActionModeFragment.java
com.nasageek.utexasutilities.fragments.BlackboardAnnouncementsFragment.java
com.nasageek.utexasutilities.fragments.BlackboardCourseListFragment.java
com.nasageek.utexasutilities.fragments.BlackboardCourseMapFragment.java
com.nasageek.utexasutilities.fragments.BlackboardDashboardFragment.java
com.nasageek.utexasutilities.fragments.BlackboardDownloadableItemFragment.java
com.nasageek.utexasutilities.fragments.BlackboardExternalItemFragment.java
com.nasageek.utexasutilities.fragments.BlackboardFragment.java
com.nasageek.utexasutilities.fragments.BlackboardGradesFragment.java
com.nasageek.utexasutilities.fragments.BlackboardPagerFragment.java
com.nasageek.utexasutilities.fragments.CourseScheduleFragment.java
com.nasageek.utexasutilities.fragments.DatePickerFragment.java
com.nasageek.utexasutilities.fragments.DoubleDatePickerDialogFragment.java
com.nasageek.utexasutilities.fragments.ExamScheduleFragment.java
com.nasageek.utexasutilities.fragments.MenuFragment.java
com.nasageek.utexasutilities.fragments.NutritionInfoFragment.java
com.nasageek.utexasutilities.fragments.PickCalendarDialogFragment.java
com.nasageek.utexasutilities.fragments.TransactionsFragment.java
com.nasageek.utexasutilities.model.BBClass.java
com.nasageek.utexasutilities.model.Building.java
com.nasageek.utexasutilities.model.Classtime.java
com.nasageek.utexasutilities.model.CourseMapItem.java
com.nasageek.utexasutilities.model.FeedItem.java
com.nasageek.utexasutilities.model.Placemark.java
com.nasageek.utexasutilities.model.Transaction.java
com.nasageek.utexasutilities.model.UTClass.java
com.viewpagerindicator.MyTabPageIndicator.java