Android Open Source - utexas-utilities Blackboard Grades 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 www  . j a  v a2  s. co  m
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.mapsaurus.paneslayout.FragmentLauncher;
import com.nasageek.utexasutilities.AsyncTask;
import com.nasageek.utexasutilities.R;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

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

public class BlackboardGradesFragment extends BlackboardFragment {

    private LinearLayout g_pb_ll;
    private ListView glv;

    private LinearLayout gell;
    private TextView getv;

    private String courseID, courseName, viewUri;
    private String selection;
    private OkHttpClient httpclient;
    private fetchGradesTask fetch;

    private ArrayList<bbGrade> grades;
    private GradesAdapter gradeAdapter;

    public BlackboardGradesFragment() {
    }

    public static BlackboardGradesFragment newInstance(String courseID, String courseName,
            String viewUri, boolean fromDashboard, String selection) {
        BlackboardGradesFragment bgf = new BlackboardGradesFragment();

        Bundle args = new Bundle();
        args.putString("courseID", courseID);
        args.putString("courseName", courseName);
        args.putString("viewUri", viewUri);
        args.putBoolean("fromDashboard", fromDashboard);
        args.putString("selection", selection);
        bgf.setArguments(args);

        return bgf;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
        courseID = getArguments().getString("courseID");
        courseName = getArguments().getString("courseName");
        viewUri = getArguments().getString("viewUri");
        selection = getArguments().getString("selection");
        setHasOptionsMenu(true);

        grades = new ArrayList<bbGrade>();
        gradeAdapter = new GradesAdapter(getActivity(), grades);
        httpclient = new OkHttpClient();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: check type of container, should we get the current index from
        // container or parent activity?

        final View vg = inflater.inflate(R.layout.blackboard_grades_layout, container, false);

        setupActionBar();
        g_pb_ll = (LinearLayout) vg.findViewById(R.id.grades_progressbar_ll);
        glv = (ListView) vg.findViewById(R.id.gradesListView);

        gell = (LinearLayout) vg.findViewById(R.id.grades_error);
        getv = (TextView) vg.findViewById(R.id.tv_failure);

        glv.setAdapter(gradeAdapter);
        glv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                bbGrade grade = (bbGrade) arg0.getAdapter().getItem(arg2);

                Dialog dlg = new Dialog(getActivity());// ,R.style.Theme_Sherlock_Light_Dialog);
                dlg.requestWindowFeature(Window.FEATURE_NO_TITLE);
                dlg.setContentView(R.layout.grade_info_dialog);
                dlg.setTitle("Grade Info");

                TextView name = (TextView) dlg.findViewById(R.id.grade_info_name);
                TextView value = (TextView) dlg.findViewById(R.id.grade_info_value);
                TextView comment = (TextView) dlg.findViewById(R.id.grade_info_comment);

                name.setText(grade.getName());

                String valueString = null;
                if (grade.getNumGrade().equals(-1)) {
                    valueString = "-";
                } else if (grade.getNumGrade().equals(-2)) {
                    valueString = grade.getGrade();
                } else {
                    valueString = grade.getNumGrade() + "/" + grade.getNumPointsPossible();
                }
                value.setText(valueString);
                comment.setText(grade.getComment());

                dlg.setCanceledOnTouchOutside(true);
                dlg.show();
                // TODO: DialogFragment or showDialog
            }

        });

        // there should always be at least 2 grades, so checking for 0 is a
        // valid way to see if it couldn't load last time
        if (grades.size() == 0) {
            fetch = new fetchGradesTask(httpclient);
            fetch.execute();
        }

        return vg;

    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

        menu.clear();
        // if(!getIntent().getBooleanExtra("showViewInWeb", false))
        if (viewUri != null && !viewUri.equals("")) {
            inflater.inflate(R.menu.blackboard_grades_menu, menu);
            // menu.removeItem(R.id.viewInWeb);
        }

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id) {
            case R.id.grades_view_in_web:
                showAreYouSureDlg(getActivity());
                break;
        }
        return false;
    }

    private void showAreYouSureDlg(final Context con) {
        AlertDialog.Builder alertBuilder = new AlertDialog.Builder(con);
        alertBuilder
                .setMessage("Would you like to view this item on the Blackboard website? (you might need to log in again if"
                        + " you have disabled the embedded browser)");
        alertBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                dialog.dismiss();

            }
        });

        alertBuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface arg0, int arg1) {

                SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(con);
                if (sp.getBoolean("embedded_browser", true)) {
                    ((FragmentLauncher) getActivity()).addFragment(
                            BlackboardGradesFragment.this, BlackboardExternalItemFragment
                                    .newInstance(viewUri, courseID, courseName, "Grades", false));
                } else {
                    Intent i = new Intent(Intent.ACTION_VIEW);
                    i.setData(Uri.parse(viewUri));
                    startActivity(i);
                }

                /*
                 * Intent web = new
                 * Intent(null,Uri.parse(getIntent().getStringExtra
                 * ("viewUri")),BlackboardGradesActivity
                 * .this,BlackboardExternalItemActivity.class);
                 * web.putExtra("itemName", "Grades");
                 * web.putExtra("coursename",
                 * getIntent().getStringExtra("coursename"));
                 * startActivity(web);
                 */
            }
        });
        alertBuilder.setTitle("View on Blackboard");
        alertBuilder.show();
    }

    private void setupActionBar() {
        final ActionBar actionbar = getSherlockActivity().getSupportActionBar();
        actionbar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE, ActionBar.DISPLAY_SHOW_TITLE
                | ActionBar.DISPLAY_SHOW_CUSTOM);
        actionbar.setTitle(courseName);
        actionbar.setSubtitle("Grades");
    }

    @Override
    public String getBbid() {
        return getArguments().getString("courseID");
    }

    @Override
    public String getCourseName() {
        return getArguments().getString("courseName");
    }

    @Override
    public boolean isFromDashboard() {
        return getArguments().getBoolean("fromDashboard");
    }

    private class fetchGradesTask extends AsyncTask<Object, Void, ArrayList<bbGrade>> {
        private OkHttpClient client;
        private String errorMsg;
        private int selectIndex = 0;

        public fetchGradesTask(OkHttpClient client) {
            this.client = client;
        }

        @Override
        protected void onPreExecute() {
            g_pb_ll.setVisibility(View.VISIBLE);
            glv.setVisibility(View.GONE);
            gell.setVisibility(View.GONE);
        }

        @Override
        protected ArrayList<bbGrade> doInBackground(Object... params) {
            String reqUrl = BLACKBOARD_DOMAIN
                    + "/webapps/Bb-mobile-BBLEARN/courseData?course_section=GRADES&course_id="
                    + courseID;
            Request request = new Request.Builder()
                    .url(reqUrl)
                    .build();
            String pagedata = "";

            try {
                Response response = client.newCall(request).execute();
                pagedata = response.body().string();
            } catch (IOException e) {
                errorMsg = "UTilities could not fetch this course's grades";
                e.printStackTrace();
                cancel(true);
                return null;
            }

            ArrayList<bbGrade> data = new ArrayList<bbGrade>();

            Pattern gradeItemPattern = Pattern.compile("<grade-item.*?/>", Pattern.DOTALL);
            Matcher gradeItemMatcher = gradeItemPattern.matcher(pagedata);

            while (gradeItemMatcher.find()) {
                String gradeData = gradeItemMatcher.group();
                Pattern namePattern = Pattern.compile("name=\"(.*?)\"");
                Matcher nameMatcher = namePattern.matcher(gradeData);
                Pattern pointsPattern = Pattern.compile("pointspossible=\"(.*?)\"");
                Matcher pointsMatcher = pointsPattern.matcher(gradeData);
                Pattern gradePattern = Pattern.compile("grade=\"(.*?)\"");
                Matcher gradeMatcher = gradePattern.matcher(gradeData);
                Pattern commentPattern = Pattern.compile("comments=\"(.*?)\"", Pattern.DOTALL);
                Matcher commentMatcher = commentPattern.matcher(gradeData);

                if (nameMatcher.find() && pointsMatcher.find() && gradeMatcher.find()) {
                    data.add(new bbGrade(nameMatcher.group(1).replace("&amp;", "&"), gradeMatcher
                            .group(1), pointsMatcher.group(1), commentMatcher.find() ? Html
                            .fromHtml(Html.fromHtml(commentMatcher.group(1)).toString()).toString()
                            : "No comments"));
                }
            }
            if (!selection.equals("")) {
                for (int i = 0; i < data.size(); i++) {
                    if (data.get(i).getName().equals(selection)) {
                        selectIndex = i;
                    }
                }
            }
            return data;
        }

        @Override
        protected void onPostExecute(ArrayList<bbGrade> result) {
            if (!this.isCancelled()) {
                grades.addAll(result);
                gradeAdapter.notifyDataSetChanged();
                glv.setSelection(selectIndex);

                g_pb_ll.setVisibility(View.GONE);
                gell.setVisibility(View.GONE);
                glv.setVisibility(View.VISIBLE);
            }
        }

        @Override
        protected void onCancelled() {
            getv.setText(errorMsg);

            g_pb_ll.setVisibility(View.GONE);
            glv.setVisibility(View.GONE);
            gell.setVisibility(View.VISIBLE);
        }
    }

    class GradesAdapter extends ArrayAdapter<bbGrade> {

        private Context con;
        private ArrayList<bbGrade> items;
        private LayoutInflater li;

        public GradesAdapter(Context c, ArrayList<bbGrade> items) {
            super(c, 0, items);
            con = c;
            this.items = items;
            li = (LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        }

        @Override
        public int getCount() {

            return items.size();
        }

        @Override
        public bbGrade getItem(int position) {

            return items.get(position);
        }

        @Override
        public long getItemId(int position) {

            return 0;
        }

        @Override
        public boolean areAllItemsEnabled() {
            return true;
        }

        @Override
        public boolean isEnabled(int i) {
            return true;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            bbGrade grade = items.get(position);

            String title = grade.getName();
            String value = null;
            if (grade.getNumGrade().equals(-1)) {
                value = "-";
            } else if (grade.getNumGrade().equals(-2)) {
                value = grade.getGrade();
            } else {
                value = grade.getNumGrade() + "/" + grade.getNumPointsPossible();
            }
            ViewGroup lin = (ViewGroup) convertView;

            if (lin == null) {
                lin = (RelativeLayout) li.inflate(R.layout.grade_item_view, null, false);
            }

            TextView gradeName = (TextView) lin.findViewById(R.id.grade_name);
            TextView gradeValue = (TextView) lin.findViewById(R.id.grade_value);
            ImageView commentImg = (ImageView) lin.findViewById(R.id.comment_available_img);

            if (grade.getComment().equals("No comments")) {
                commentImg.setVisibility(View.INVISIBLE);
            } else {
                commentImg.setVisibility(View.VISIBLE);
            }
            gradeName.setText(title);
            gradeValue.setText(value);

            return lin;
        }
    }

    class bbGrade {
        String name, grade, pointsPossible, comment;

        public bbGrade(String name, String grade, String pointsPossible, String comment) {
            this.name = name;
            this.grade = grade;
            this.pointsPossible = pointsPossible;
            this.comment = comment;
        }

        public String getName() {
            return name;
        }

        public String getComment() {
            return comment;
        }

        public Number getNumGrade() {
            if (!grade.equals("-")) {
                String temp = grade.replaceAll("[^\\d\\.]*", "");
                if (temp.equals("")) {
                    return -2;
                }
                double d = Double.parseDouble(temp);
                if (d == Math.floor(d)) {
                    return (int) d;
                } else {
                    return d;
                }
            } else {
                return -1;
            }
        }

        public String getGrade() {
            return grade;
        }

        public String getPointsPossible() {
            return pointsPossible;
        }

        public Number getNumPointsPossible() {
            String temp = pointsPossible.replaceAll("[^\\d\\.]*", "");
            double d = Double.parseDouble(temp);
            if (d == Math.floor(d)) {
                return (int) d;
            } else {
                return d;
            }
        }

    }

    @Override
    public void onPanesScrolled() {
        setupActionBar();
    }

}




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