Android Open Source - utexas-utilities Schedule Class Adapter






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.adapters;
/*from  w ww  . jav a2  s  .  co  m*/
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.Shape;
import android.preference.PreferenceManager;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.nasageek.utexasutilities.model.Classtime;
import com.nasageek.utexasutilities.model.UTClass;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;

public class ScheduleClassAdapter extends BaseAdapter {

    private ArrayList<Classtime> orderedClasstimes;
    private ArrayList<Boolean> firstlist;
    private Context mContext;
    private int currentTimePos = -1;
    private int currMinutes;
    private String emptyCellBackgroundPref;

    private static final int DARKGRAY = 0xFFCECECE;
    private static final int LIGHTGRAY = 0xFFDCDCDC;

    public ScheduleClassAdapter(Context c, ArrayList<UTClass> classList) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(c);
        emptyCellBackgroundPref = sp.getString("schedule_background_style", "checkhour");
        mContext = c;
        ArrayList<Classtime> allClasstimes = new ArrayList<Classtime>(50);
        updateTime();

        // grab all ClassTimes out of classList
        for (UTClass clz : classList) {
            for (Classtime classtime : clz.getClassTimes()) {
                allClasstimes.add(classtime);
            }
        }

        // element is true if it's the first cell of a class in the schedule (cells are a half hour)
        firstlist = new ArrayList<Boolean>();
        firstlist.ensureCapacity(180);

        // contains the Classtimes that have been ordered/grouped into a fake "grid"
        orderedClasstimes = new ArrayList<Classtime>();
        orderedClasstimes.ensureCapacity(180);

        for (int x = 0; x < 180; x++) {
            orderedClasstimes.add(null);
            firstlist.add(false);
        }

        HashMap<Character, Integer> dayCharMap = new HashMap<Character, Integer>();
        // set up day map for manipulating orderedClasstimes as if it were a grid
        dayCharMap.put('M', 0);
        dayCharMap.put('T', 1);
        dayCharMap.put('W', 2);
        dayCharMap.put('H', 3);
        dayCharMap.put('F', 4);

        for (Classtime classTime : allClasstimes) {
            int startPos = timeToPos(classTime.getStartTime());
            int endPos = timeToPos(classTime.getEndTime());
            int dayOffset = dayCharMap.get(classTime.getDay());

            for (int a = 0; a < (endPos - startPos); a++) {
                int dayIndex = dayOffset + 5 * startPos + a * 5;
                orderedClasstimes.set(dayIndex, classTime);
                if (a == 0) {
                    firstlist.set(dayIndex, true);
                }
            }
        }
    }

    public void updateTime() {
        Calendar cal = Calendar.getInstance();
        int day = cal.get(Calendar.DAY_OF_WEEK) - 2;
        String time = cal.get(Calendar.HOUR) + (cal.get(Calendar.MINUTE) >= 30 ? ":30" : ":00")
                + (cal.get(Calendar.AM_PM) == Calendar.PM ? "pm" : "");

        if (day < 5 && day >= 0 && cal.get(Calendar.HOUR_OF_DAY) <= 22
                && cal.get(Calendar.HOUR_OF_DAY) >= 6) {
            currentTimePos = day + 5 * timeToPos(time);
            currMinutes = cal.get(Calendar.MINUTE) % 30;
        }
    }

    // 6am is at position 0
    private int timeToPos(String time) {
        String[] temp = time.split(":");
        int pos = Integer.parseInt(temp[0]) * 2 - 12;
        if (temp[1].contains("pm") && pos != 12) {
            pos += 24;
        }
        if (temp[1].charAt(0) == '3') {
            pos++;
        }
        return pos;
    }

    public int getEarliestClassPos() {
        for (int i = 0; i < firstlist.size(); i++) {
            if (firstlist.get(i)) {
                return i;
            }
        }
        return 0;
    }

    @Override
    public int getCount() {
        return orderedClasstimes.size();
    }

    @Override
    public Object getItem(int position) {
        return orderedClasstimes.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressWarnings("deprecation")
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        TextView textView;

        if (convertView == null) {
            textView = new TextView(mContext);
        } else {
            textView = (TextView) convertView;
        }

        textView.setTextColor(Color.BLACK);
        textView.setTextSize(13f); // 11.75 to fill cell space

        // is there class at this time?
        if (orderedClasstimes.get(position) == null) {
            if (position == currentTimePos) {
                final int colorInt = getEmptyCellColor(position);
                Drawable currentMinutesLine = new ShapeDrawable(new Shape() {

                    @Override
                    public void draw(Canvas canvas, Paint paint) {
                        drawCurrentMinutesLine(this, canvas, paint, colorInt);
                    }
                });
                textView.setBackgroundDrawable(currentMinutesLine);
            } else {
                textView.setBackgroundColor(getEmptyCellColor(position));
            }
            textView.setText("");
        } else {
            final Classtime cl = orderedClasstimes.get(position);
            final String color = "#" + cl.getColor();

            if (position == currentTimePos) {
                final int colorInt = Color.parseColor(color);
                Drawable currentMinutesLine = new ShapeDrawable(new Shape() {

                    @Override
                    public void draw(Canvas canvas, Paint paint) {
                        drawCurrentMinutesLine(this, canvas, paint, colorInt);
                    }
                });
                textView.setBackgroundDrawable(currentMinutesLine);
            } else {
                textView.setBackgroundColor(Color.parseColor(color));
            }

            // is this the first Classtime in a "block"?
            if (firstlist.get(position)) {
                // if so, label it with the start time of the class
                textView.setText(orderedClasstimes.get(position).getStartTime());
                textView.setGravity(Gravity.CENTER_HORIZONTAL);
            } else {
                textView.setText("");
            }
        }
        return textView;
    }

    private void drawCurrentMinutesLine(Shape shape, Canvas canvas, Paint paint, int bgColor) {
        paint.setStrokeWidth(3f);
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.STROKE);
        canvas.drawColor(bgColor);

        Paint blur = new Paint(paint);
        blur.setStrokeWidth(3f);
        blur.setMaskFilter(new BlurMaskFilter(3, BlurMaskFilter.Blur.SOLID));
        canvas.drawLine(0, (int) ((currMinutes / 30.0) * shape.getHeight() + .5),
                shape.getWidth(), (int) ((currMinutes / 30.0) * shape.getHeight() + .5), paint);
    }

    private int getEmptyCellColor(int position) {
        if (emptyCellBackgroundPref.equals("checkhour")) {
            if ((position / 10) % 2 == 0) {
                if ((position / 5) % 2 == 0) {
                    return position % 2 == 0 ? LIGHTGRAY : DARKGRAY;
                } else {
                    return position % 2 == 0 ? DARKGRAY : LIGHTGRAY;
                }
            } else {
                if ((position / 5) % 2 == 0) {
                    return position % 2 == 0 ? DARKGRAY : LIGHTGRAY;
                } else {
                    return position % 2 == 0 ? LIGHTGRAY : DARKGRAY;
                }
            }
        } else if (emptyCellBackgroundPref.equals("checkhalf")) {
            return position % 2 == 0 && (position % 10) % 2 == 0 ? LIGHTGRAY : DARKGRAY;
        } else if (emptyCellBackgroundPref.equals("stripehour")) {
            return position / 10 % 2 == 0 ? LIGHTGRAY : DARKGRAY;
        } else if (emptyCellBackgroundPref.equals("stripehalf")) {
            return position / 5 % 2 == 0 ? LIGHTGRAY : DARKGRAY;
        } else {
            return Color.BLACK;
        }
    }
}




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