Android Open Source - task-timer-legacy Utilities






From Project

Back to project page task-timer-legacy.

License

The source code is released under:

GNU General Public License

If you think the Android project task-timer-legacy 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.gawdl3y.android.tasktimer.util;
/*from w  w  w.j a  v a  2s.  c  o  m*/
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import com.gawdl3y.android.tasktimer.TaskTimerApplication;
import com.gawdl3y.android.tasktimer.pojos.Group;
import com.gawdl3y.android.tasktimer.pojos.Task;

import java.util.ArrayList;
import java.util.List;

/**
 * General utility methods for Task Timer
 * @author Schuyler Cebulskie
 */
public final class Utilities {
    /**
     * Sets the positions of tasks or groups in a List to the position in the array
     * @param arr The List to reorder
     * @return An {@code ArrayList} of modified elements
     */
    public static final ArrayList<Object> reposition(List<?> arr) {
        ArrayList<Object> modifiedEntries = new ArrayList<Object>();

        for(int i = 0; i < arr.size(); i++) {
            Object thing = arr.get(i);

            if(thing instanceof Task) {
                Task task = (Task) thing;
                if(task.getPosition() != i) {
                    task.setPosition(i);
                    modifiedEntries.add(task);
                }
            } else if(thing instanceof Group) {
                Group group = (Group) thing;
                if(group.getPosition() != i) {
                    group.setPosition(i);
                    modifiedEntries.add(group);
                }
            }
        }

        return modifiedEntries;
    }

    /**
     * Finds and returns a task by an ID in a List of tasks
     * @param id    The ID to search for
     * @param tasks The tasks to search in
     * @return The task with the specified ID
     */
    public static final Task getTaskByID(int id, List<Task> tasks) {
        if(tasks != null) {
            for(Task t : tasks) {
                if(t.getId() == id) return t;
            }
        }

        return null;
    }

    /**
     * Finds and returns the index of a Task in a List by ID
     * @param id    The ID of the Task to search for
     * @param tasks The List of Tasks to search in
     * @return The index of the Task found, or -1 if no match
     */
    public static final int getTaskIndexByID(int id, List<Task> tasks) {
        if(tasks != null) {
            for(int t = 0; t < tasks.size(); t++) {
                if(tasks.get(t).getId() == id) return t;
            }
        }

        return -1;
    }

    /**
     * Finds and returns a group by an ID
     * @param id     The ID to search for
     * @param groups The groups to search in
     * @return The group with the specified ID
     */
    public static final Group getGroupByID(int id, List<Group> groups) {
        if(groups != null) {
            for(Group g : groups) {
                if(g.getId() == id) return g;
            }
        }

        return null;
    }

    /**
     * Finds and returns the index of a Group in a List by ID
     * @param id The ID of the Group to search for
     * @param groups The List of Groups to search in
     * @return The index of the Group found, or -1 if no match
     */
    public static final int getGroupIndexByID(int id, List<Group> groups) {
        if(groups != null) {
            for(int t = 0; t < groups.size(); t++) {
                if(groups.get(t).getId() == id) return t;
            }
        }

        return -1;
    }

    /**
     * Finds and returns a task by an ID in a List of groups
     * @param id     The ID to search for
     * @param groups The groups to search in
     * @return The task with the specified ID
     */
    public static final Task getGroupedTaskByID(int id, List<Group> groups) {
        if(groups != null) {
            for(Group g : groups) {
                if(g.getTasks() != null) {
                    for(Task t : g.getTasks()) {
                        if(t.getId() == id) return t;
                    }
                }
            }
        }

        return null;
    }

    /**
     * Generates a Bitmap from a Drawable
     * @param drawable The Drawable to convert to a Bitmap
     * @return Bitmap
     */
    public static final Bitmap drawableToBitmap(Drawable drawable) {
        if(drawable instanceof BitmapDrawable) return ((BitmapDrawable) drawable).getBitmap();

        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);

        return bitmap;
    }

    /**
     * Opens the Play Store item for the app
     * @param context The context we're coming from
     */
    public static final void openPlayStore(Context context) {
        openPlayStore(context, TaskTimerApplication.PACKAGE);
    }

    /**
     * Opens the Play Store item for a specified app package
     * @param context    The context we're coming from
     * @param appPackage The package of the target app
     */
    public static final void openPlayStore(Context context, String appPackage) {
        try {
            context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackage)));
        } catch(android.content.ActivityNotFoundException e) {
            context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackage)));
        }
    }
}




Java Source Code List

com.gawdl3y.android.actionablelistview.ActionItem.java
com.gawdl3y.android.actionablelistview.ActionableAdapter.java
com.gawdl3y.android.actionablelistview.ActionableListFragment.java
com.gawdl3y.android.actionablelistview.ActionableListView.java
com.gawdl3y.android.actionablelistview.Actionable.java
com.gawdl3y.android.actionablelistview.CheckableAdapter.java
com.gawdl3y.android.actionablelistview.CheckableListView.java
com.gawdl3y.android.actionablelistview.OnListItemCheckStateChangeListener.java
com.gawdl3y.android.tasktimer.TaskTimerApplication.java
com.gawdl3y.android.tasktimer.activities.MainActivity.java
com.gawdl3y.android.tasktimer.activities.SettingsActivity.java
com.gawdl3y.android.tasktimer.adapters.GroupListAdapter.java
com.gawdl3y.android.tasktimer.adapters.NewFragmentStatePagerAdapter.java
com.gawdl3y.android.tasktimer.adapters.TaskListAdapter.java
com.gawdl3y.android.tasktimer.adapters.TaskListFragmentAdapter.java
com.gawdl3y.android.tasktimer.data.TaskTimerDatabaseHelper.java
com.gawdl3y.android.tasktimer.data.TaskTimerProvider.java
com.gawdl3y.android.tasktimer.data.TaskTimerReceiver.java
com.gawdl3y.android.tasktimer.layout.GroupEditDialogFragment.java
com.gawdl3y.android.tasktimer.layout.GroupListItem.java
com.gawdl3y.android.tasktimer.layout.GroupsFragment.java
com.gawdl3y.android.tasktimer.layout.SettingsFragment.java
com.gawdl3y.android.tasktimer.layout.TaskEditDialogFragment.java
com.gawdl3y.android.tasktimer.layout.TaskListFragment.java
com.gawdl3y.android.tasktimer.layout.TaskListItem.java
com.gawdl3y.android.tasktimer.layout.TasksFragment.java
com.gawdl3y.android.tasktimer.pojos.Group.java
com.gawdl3y.android.tasktimer.pojos.TaskTimerEvents.java
com.gawdl3y.android.tasktimer.pojos.TaskTimerThread.java
com.gawdl3y.android.tasktimer.pojos.Task.java
com.gawdl3y.android.tasktimer.pojos.TimeAmount.java
com.gawdl3y.android.tasktimer.util.DialogPreference.java
com.gawdl3y.android.tasktimer.util.Log.java
com.gawdl3y.android.tasktimer.util.Utilities.java