de.azapps.mirakel.helper.TaskHelper.java Source code

Java tutorial

Introduction

Here is the source code for de.azapps.mirakel.helper.TaskHelper.java

Source

/*******************************************************************************
 * Mirakel is an Android App for managing your ToDo-Lists
 * 
 * Copyright (c) 2013 Anatolij Zelenin, Georg Semmler.
 * 
 *     This program 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
 *     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 program.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package de.azapps.mirakel.helper;

import java.util.Calendar;

import org.joda.time.LocalDate;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import de.azapps.mirakel.main_activity.MainActivity;
import de.azapps.mirakel.model.task.Task;
import de.azapps.mirakelandroid.R;

public class TaskHelper {

    public static Task getTaskFromIntent(Intent intent) {
        Task task = null;
        long taskId = intent.getLongExtra(MainActivity.EXTRA_ID, 0);
        if (taskId == 0) {
            // ugly fix for show Task from Widget
            taskId = (long) intent.getIntExtra(MainActivity.EXTRA_ID, 0);
        }
        if (taskId != 0) {
            task = Task.get(taskId);
        }
        return task;
    }

    /**
     * Helper for the share-functions
     * 
     * @param ctx
     * @param t
     * @return
     */
    static String getTaskName(Context ctx, Task t) {
        String subject;
        if (t.getDue() == null)
            subject = ctx.getString(R.string.share_task_title, t.getName());
        else
            subject = ctx.getString(R.string.share_task_title_with_date, t.getName(),
                    DateTimeHelper.formatDate(t.getDue(), ctx.getString(R.string.dateFormat)));
        return subject;
    }

    /**
     * Returns the ID of the ColorResource for a DueDate
     * 
     * @param origDue
     *            The DueDate
     * @param isDone
     *            Is the Task done?
     * @return ID of the ColorResource
     */
    public static int getTaskDueColor(Calendar origDue, boolean isDone) {
        if (origDue == null)
            return R.color.Grey;
        LocalDate today = new LocalDate();
        LocalDate nextWeek = new LocalDate().plusDays(7);
        LocalDate due = new LocalDate(origDue);
        int cmpr = today.compareTo(due);
        int color;
        if (isDone) {
            color = R.color.Grey;
        } else if (cmpr > 0) {
            color = R.color.Red;
        } else if (cmpr == 0) {
            color = R.color.Orange;
        } else if (nextWeek.compareTo(due) >= 0) {
            color = R.color.Yellow;
        } else {
            color = R.color.Green;
        }
        return color;
    }

    public static int getPrioColor(int priority, Context context) {
        final int[] PRIO_COLOR = { Color.parseColor("#669900"), Color.parseColor("#99CC00"),
                Color.parseColor("#33B5E5"), Color.parseColor("#FFBB33"), Color.parseColor("#FF4444") };
        final int[] DARK_PRIO_COLOR = { Color.parseColor("#008000"), Color.parseColor("#00c400"),
                Color.parseColor("#3377FF"), Color.parseColor("#FF7700"), Color.parseColor("#FF3333") };
        if (MirakelPreferences.isDark()) {
            return DARK_PRIO_COLOR[priority + 2];
        } else {
            return PRIO_COLOR[priority + 2];
        }

    }

}