Java tutorial
/* * Calendula - An assistant for personal medication management. * Copyright (C) 2016 CITIUS - USC * * Calendula is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this software. If not, see <http://www.gnu.org/licenses/>. */ package es.usc.citius.servando.calendula.activities; import android.content.Context; import android.content.res.Resources; import android.graphics.Color; import android.text.Spannable; import android.text.SpannableString; import android.text.style.ForegroundColorSpan; import android.text.style.RelativeSizeSpan; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import com.roomorama.caldroid.CaldroidFragment; import com.roomorama.caldroid.CaldroidGridAdapter; import org.joda.time.LocalDate; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.TimeZone; import es.usc.citius.servando.calendula.R; import es.usc.citius.servando.calendula.persistence.PickupInfo; import es.usc.citius.servando.calendula.util.PickupUtils; import hirondelle.date4j.DateTime; public class CaldroidSampleCustomAdapter extends CaldroidGridAdapter { private static final String notTakenSymbol = "?"; private static final String takenSymbol = ""; private static final String lostSymbol = ""; private PickupUtils pkUtils; public CaldroidSampleCustomAdapter(Context context, int month, int year, Map<String, Object> caldroidData, Map<String, Object> extraData, PickupUtils pkUtils) { super(context, month, year, caldroidData, extraData); this.pkUtils = pkUtils; } @Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View cellView = convertView; // For reuse if (convertView == null) { cellView = inflater.inflate(R.layout.custom_calendar_cell, null); } int topPadding = cellView.getPaddingTop(); int leftPadding = cellView.getPaddingLeft(); int bottomPadding = cellView.getPaddingBottom(); int rightPadding = cellView.getPaddingRight(); TextView tv1 = (TextView) cellView.findViewById(R.id.tv1); TextView tv2 = (TextView) cellView.findViewById(R.id.tv2); tv1.setTextColor(Color.BLACK); // Get dateTime of this cell DateTime dateTime = this.datetimeList.get(position); Resources resources = context.getResources(); // Set color of the dates in previous / next month if (dateTime.getMonth() != month) { tv1.setTextColor(resources.getColor(com.caldroid.R.color.caldroid_darker_gray)); } boolean shouldResetDiabledView = false; boolean shouldResetSelectedView = false; // Customize for disabled dates and date outside min/max dates if ((minDateTime != null && dateTime.lt(minDateTime)) || (maxDateTime != null && dateTime.gt(maxDateTime)) || (disableDates != null && disableDates.indexOf(dateTime) != -1)) { tv1.setTextColor(CaldroidFragment.disabledTextColor); if (CaldroidFragment.disabledBackgroundDrawable == -1) { cellView.setBackgroundResource(com.caldroid.R.drawable.disable_cell); } else { cellView.setBackgroundResource(CaldroidFragment.disabledBackgroundDrawable); } if (dateTime.equals(getToday())) { cellView.setBackgroundResource(com.caldroid.R.drawable.red_border_gray_bg); } } else { shouldResetDiabledView = true; } // Customize for selected dates if (selectedDates != null && selectedDates.indexOf(dateTime) != -1) { cellView.setBackgroundColor(resources.getColor(com.caldroid.R.color.caldroid_sky_blue)); tv1.setTextColor(Color.BLACK); } else { shouldResetSelectedView = true; } if (shouldResetDiabledView && shouldResetSelectedView) { // Customize for today if (dateTime.equals(getToday())) { cellView.setBackgroundResource(com.caldroid.R.drawable.red_border); } else { cellView.setBackgroundResource(com.caldroid.R.drawable.cell_bg); } } tv1.setText("" + dateTime.getDay()); List<PickupInfo> pk = pkUtils.pickupsMap() .get(new LocalDate(dateTime.getMilliseconds(TimeZone.getDefault()))); if (pk != null && pk.size() > 0) { LocalDate today = LocalDate.now(); HashMap<Integer, String> colors = new HashMap<>(); for (int i = 0; i < pk.size(); i++) { PickupInfo pki = pk.get(i); Integer color = pkUtils.getPatient(pki).color(); if (!colors.containsKey(color)) { colors.put(color, pki.taken() ? takenSymbol : notTakenSymbol); } else if (!pki.taken()) { colors.put(color, pki.to().isAfter(today) ? notTakenSymbol : lostSymbol); } } String text = ""; for (Integer c : colors.keySet()) { text += colors.get(c); } Spannable span = new SpannableString(text); int i = 0; for (int color : colors.keySet()) { span.setSpan(new ForegroundColorSpan(color), i, i + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); span.setSpan(new RelativeSizeSpan(1.2f), i, i + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); i++; } tv2.setText(span); } else { tv2.setText(""); } // Somehow after setBackgroundResource, the padding collapse. // This is to recover the padding cellView.setPadding(leftPadding, topPadding, rightPadding, bottomPadding); // Set custom color if required setCustomResources(dateTime, cellView, tv1); return cellView; } }