Android Open Source - frc-notebook Get Notes For Match






From Project

Back to project page frc-notebook.

License

The source code is released under:

GNU General Public License

If you think the Android project frc-notebook 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.plnyyanks.frcnotebook.background;
/*from w ww .j  a v a 2 s. c  o  m*/
import android.app.Activity;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.AsyncTask;
import android.util.Log;
import android.util.SparseArray;
import android.view.ActionMode;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.plnyyanks.frcnotebook.Constants;
import com.plnyyanks.frcnotebook.R;
import com.plnyyanks.frcnotebook.activities.StartActivity;
import com.plnyyanks.frcnotebook.adapters.ActionBarCallback;
import com.plnyyanks.frcnotebook.adapters.AllianceExpandableListAdapter;
import com.plnyyanks.frcnotebook.adapters.ListViewArrayAdapter;
import com.plnyyanks.frcnotebook.database.PreferenceHandler;
import com.plnyyanks.frcnotebook.datatypes.Event;
import com.plnyyanks.frcnotebook.datatypes.ListElement;
import com.plnyyanks.frcnotebook.datatypes.ListGroup;
import com.plnyyanks.frcnotebook.datatypes.ListItem;
import com.plnyyanks.frcnotebook.datatypes.Match;
import com.plnyyanks.frcnotebook.datatypes.Note;
import com.plnyyanks.frcnotebook.dialogs.DeleteDialog;
import com.plnyyanks.frcnotebook.dialogs.EditNoteDialog;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;

/**
 * File created by phil on 3/1/14.
 * Copyright 2014, Phil Lopreiato
 * This file is part of FRC Notebook.
 * FRC Notebook 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.
 * FRC Notebook 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 FRC Notebook. If not, see http://www.gnu.org/licenses/.
 */
public class GetNotesForMatch extends AsyncTask<String, String, String> {

    private static final String TIME_FORMAT = "hh:mm a";

    private static Activity activity;
    private static String previousMatchKey,
                          nextMatchKey;
    private static SparseArray<ListGroup> redGroups = new SparseArray<ListGroup>(),
                                          blueGroups = new SparseArray<ListGroup>();
    public static Object mActionMode;
    private static ListView genericList;
    public static String selectedNote="";
    private static AllianceExpandableListAdapter redAdaper, blueAdapter;
    private static ExpandableListView redAlliance,blueAlliance;
    private static  ListViewArrayAdapter genericAdapter;

    public GetNotesForMatch(Activity activity) {
        super();
        GetNotesForMatch.activity = activity;
    }

    @Override
    protected String doInBackground(String... strings) {
        String thisMatchKey,eventKey;
        previousMatchKey = strings[0];
        thisMatchKey = strings[1];
        nextMatchKey = strings[2];
        eventKey = strings[3];
        selectedNote = "";

        Match match = StartActivity.db.getMatch(thisMatchKey);
        Event parentEvent = match.getParentEvent();
        TextView matchTitle = (TextView) activity.findViewById(R.id.match_title);
        String titleString = Match.LONG_TYPES.get(match.getMatchType()) + (match.getMatchType()== Match.MATCH_TYPES.QUAL ? " " : (" " + match.getSetNumber() + " Match ")) + match.getMatchNumber();
        matchTitle.setText(titleString);
        if(PreferenceHandler.getTimesEnabled() && parentEvent.isHappeningNow()){
            String matchTime = match.getMatchTime();
            if(matchTime!=null && !matchTime.equals("")){
                TextView timeOffset = (TextView)activity.findViewById(R.id.match_time);
                SimpleDateFormat df = new SimpleDateFormat(TIME_FORMAT);
                Log.d(Constants.LOG_TAG,"Showing match time");
                try {
                    Date matchStart = df.parse(matchTime);
                    Date currentTime = new Date();
                    int hourdif = currentTime.getHours() - matchStart.getHours();
                    int mindif = currentTime.getMinutes() - matchStart.getMinutes();
                    String timeString =  Math.abs(hourdif)+":"+String.format("%02d", Math.abs(mindif));
                    boolean ahead;
                    if(hourdif>0 || (hourdif==0 && mindif>0)){
                        timeString += " behind";
                        ahead = false;
                    }else{
                        timeString += " ahead";
                        ahead = true;
                    }
                    timeOffset.setText(timeString);
                    timeOffset.setTextColor(ahead? Color.GREEN:Color.RED);
                    timeOffset.setVisibility(View.VISIBLE);
                } catch (ParseException e) {
                    Log.w(Constants.LOG_TAG,"Error parsing match time");
                }
            }
        }

        TextView redHeader = (TextView) activity.findViewById(R.id.red_score);
        if (match.getRedScore() >= 0 && PreferenceHandler.showMatchScores()) {
            redHeader.setText(Integer.toString(match.getRedScore()) + " Points");
        } else {
            redHeader.setVisibility(View.GONE);
        }

        TextView blueHeader = (TextView) activity.findViewById(R.id.blue_score);
        if (match.getBlueScore() >= 0 && PreferenceHandler.showMatchScores()) {
            blueHeader.setText(Integer.toString(match.getBlueScore()) + " Points");
        } else {
            blueHeader.setVisibility(View.GONE);
        }

        JsonArray redTeams = match.getRedAllianceTeams(),
                blueTeams = match.getBlueAllianceTeams();

        if (redTeams.size() > 0) {
            Iterator<JsonElement> iterator = redTeams.iterator();
            String teamKey;
            for (int i = 0; iterator.hasNext(); i++) {
                teamKey = iterator.next().getAsString();
                ArrayList<Note> notes = new ArrayList<Note>();
                if(PreferenceHandler.showGeneralNotes()){
                    notes.addAll(StartActivity.db.getAllNotes(StartActivity.db.KEY_EVENTKEY+"=? AND "+ StartActivity.db.KEY_MATCHKEY + "=?",new String[]{"all","all"}));
                }
                notes.addAll(StartActivity.db.getAllNotes(teamKey, eventKey, thisMatchKey));
                int size = notes.size();
                ListGroup teamHeader = new ListGroup(teamKey.substring(3)+(size>0?(" ("+ notes.size()+")"):""));

                for (Note n : notes) {
                    teamHeader.children.add(Note.buildMatchNoteTitle(n, false, false,false));
                    teamHeader.children_keys.add(Short.toString(n.getId()));
                }

                redGroups.append(i, teamHeader);
            }
        }
        if (blueTeams.size() > 0) {
            Iterator<JsonElement> iterator = blueTeams.iterator();
            String teamKey;
            for (int i = 0; iterator.hasNext(); i++) {
                teamKey = iterator.next().getAsString();
                ArrayList<Note> notes = new ArrayList<Note>();
                if(PreferenceHandler.showGeneralNotes()){
                    notes.addAll(StartActivity.db.getAllNotes(StartActivity.db.KEY_EVENTKEY+"=? AND "+ StartActivity.db.KEY_MATCHKEY + "=?",new String[]{"all","all"}));
                }
                notes.addAll(StartActivity.db.getAllNotes(teamKey, eventKey, thisMatchKey));
                int size = notes.size();
                ListGroup teamHeader = new ListGroup(teamKey.substring(3)+(size>0?(" ("+ notes.size()+")"):""));


                for (Note n : notes) {
                    teamHeader.children.add(Note.buildMatchNoteTitle(n, false, false,false));
                    teamHeader.children_keys.add(Short.toString(n.getId()));
                }

                blueGroups.append(i, teamHeader);
            }
        }

        //generic notes go here
        final ArrayList<Note> genericNotes = StartActivity.db.getAllNotes(StartActivity.db.KEY_TEAMKEY+"=? AND "+StartActivity.db.KEY_EVENTKEY+"=? AND "+ StartActivity.db.KEY_MATCHKEY + "=?",new String[]{"all",eventKey,match.getMatchKey()});
        Log.d(Constants.LOG_TAG,"Found "+genericNotes.size()+" generic notes");
        ArrayList<ListItem> genericVals = new ArrayList<ListItem>();
        ArrayList<String> genericKeys = new ArrayList<String>();
        for(Note n:genericNotes){
            genericVals.add(new ListElement(n.getNote(),Short.toString(n.getId())));
            genericKeys.add(Short.toString(n.getId()));
        }
        genericAdapter = new ListViewArrayAdapter(activity,genericVals,genericKeys);

        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (!StartActivity.db.matchExists(nextMatchKey)) {
                    ImageView nextButton = (ImageView) activity.findViewById(R.id.next_match);
                    nextButton.setVisibility(View.GONE);
                }
                if (!StartActivity.db.matchExists(previousMatchKey)) {
                    ImageView prevButton = (ImageView) activity.findViewById(R.id.prev_match);
                    prevButton.setVisibility(View.GONE);
                }

                redAlliance = (ExpandableListView) activity.findViewById(R.id.red_teams);
                if (redAlliance == null)
                    return;
                redAdaper = new AllianceExpandableListAdapter(activity, redGroups);
                redAlliance.setAdapter(redAdaper);

                blueAlliance = (ExpandableListView) activity.findViewById(R.id.blue_teams);
                if (redAlliance == null)
                    return;
                blueAdapter = new AllianceExpandableListAdapter(activity, blueGroups);
                blueAlliance.setAdapter(blueAdapter);

                genericList = (ListView)activity.findViewById(R.id.generic_notes);
                genericList.setAdapter(genericAdapter);
                GenericNoteClick clickListener = new GenericNoteClick();
                genericList.setOnItemClickListener(clickListener);
                genericList.setOnItemLongClickListener(clickListener);
                if(genericNotes.size()>0){
                    genericList.setVisibility(View.VISIBLE);
                }

                //hide the progress bar
                ProgressBar prog = (ProgressBar) activity.findViewById(R.id.match_loading_progress);
                prog.setVisibility(View.GONE);
            }
        });

        return null;
    }

    public static void updateListData(){
        redAdaper.notifyDataSetChanged();
        blueAdapter.notifyDataSetChanged();
        genericAdapter.notifyDataSetChanged();
    }

    public static AllianceExpandableListAdapter getRedAdaper(){
        return redAdaper;
    }

    public static AllianceExpandableListAdapter getBlueAdapter(){
        return blueAdapter;
    }

    public static ListViewArrayAdapter getGenericAdapter(){
        return genericAdapter;
    }

    public static ActionMode.Callback mActionModeCallback = new ActionBarCallback() {
        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            switch (item.getItemId()) {
                case R.id.action_delete:
                    confirmAndDelete(selectedNote);
                    // the Action was executed, close the CAB
                    selectedNote = "";
                    mode.finish();
                    return true;
                default:
                    return false;
            }
        }

        @Override
        public void onDestroyActionMode(ActionMode actionMode) {
            Log.d(Constants.LOG_TAG, "Destroy CAB");
            mActionMode = null;
            redAlliance.requestFocusFromTouch();
            redAlliance.clearChoices();
            blueAlliance.clearChoices();
            updateListData();
        }

        private void confirmAndDelete(final String noteId){
            DialogInterface.OnClickListener deleter =
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            //delete the event now
                            StartActivity.db.deleteNote(noteId);
                            redAdaper.removeNote(Short.parseShort(noteId));
                            blueAdapter.removeNote(Short.parseShort(noteId));
                            genericAdapter.removeKey(noteId);
                            activity.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    if(genericAdapter.keys.size()==0){
                                        genericList.setVisibility(View.GONE);
                                    }
                                }
                            });
                            updateListData();
                            Toast.makeText(activity, "Deleted note from database", Toast.LENGTH_SHORT).show();
                            dialog.cancel();
                        }
                    };
            new DeleteDialog(activity.getString(R.string.note_deletion_message),deleter)
                    .show(activity.getFragmentManager(),"delete_note");
        }
    };

    class GenericNoteClick implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            if(!selectedNote.equals("")) return;
            final short noteId = Short.parseShort(genericAdapter.getKey(i));
            final Note oldNote = StartActivity.db.getNote(noteId);

            new EditNoteDialog(activity.getString(R.string.edit_note_generic_title),oldNote,noteId,genericAdapter).show(activity.getFragmentManager(),"edit_note");
        }

        @Override
        public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
            view.setSelected(true);
            GetNotesForMatch.selectedNote = genericAdapter.getKey(i);
            Log.d(Constants.LOG_TAG, "Note selected, id:" + GetNotesForMatch.selectedNote);
            GetNotesForMatch.mActionMode = activity.startActionMode(GetNotesForMatch.mActionModeCallback);
            GetNotesForMatch.updateListData();
            return false;
        }
    }

}




Java Source Code List

.BasicTest.java
.TestStartActivity.java
com.plnyyanks.frcnotebook.Constants.java
com.plnyyanks.frcnotebook.activities.AddEvent.java
com.plnyyanks.frcnotebook.activities.EventDownloadActivity.java
com.plnyyanks.frcnotebook.activities.FieldMonitorActivity.java
com.plnyyanks.frcnotebook.activities.PredefinedNoteManager.java
com.plnyyanks.frcnotebook.activities.SettingsActivity.java
com.plnyyanks.frcnotebook.activities.StartActivity.java
com.plnyyanks.frcnotebook.activities.ViewEvent.java
com.plnyyanks.frcnotebook.activities.ViewMatch.java
com.plnyyanks.frcnotebook.activities.ViewTeam.java
com.plnyyanks.frcnotebook.adapters.ActionBarCallback.java
com.plnyyanks.frcnotebook.adapters.AdapterInterface.java
com.plnyyanks.frcnotebook.adapters.AllianceExpandableListAdapter.java
com.plnyyanks.frcnotebook.adapters.CustomExapandableListAdapter.java
com.plnyyanks.frcnotebook.adapters.ListViewArrayAdapter.java
com.plnyyanks.frcnotebook.adapters.MatchListExpandableListAdapter.java
com.plnyyanks.frcnotebook.adapters.NotesExpandableListAdapter.java
com.plnyyanks.frcnotebook.background.AddMatchesFromURL.java
com.plnyyanks.frcnotebook.background.DeleteEvent.java
com.plnyyanks.frcnotebook.background.GetEventMatches.java
com.plnyyanks.frcnotebook.background.GetNotesForMatch.java
com.plnyyanks.frcnotebook.background.GetNotesForTeam.java
com.plnyyanks.frcnotebook.background.GetTeamsAttending.java
com.plnyyanks.frcnotebook.background.ShowLocalEvents.java
com.plnyyanks.frcnotebook.background.ValidateNewEventData.java
com.plnyyanks.frcnotebook.database.BackupDatabase.java
com.plnyyanks.frcnotebook.database.DatabaseHandler.java
com.plnyyanks.frcnotebook.database.ImportDatabase.java
com.plnyyanks.frcnotebook.database.PreferenceHandler.java
com.plnyyanks.frcnotebook.datafeed.EventDetailFetcher.java
com.plnyyanks.frcnotebook.datafeed.EventListFetcher.java
com.plnyyanks.frcnotebook.datafeed.GET_Request.java
com.plnyyanks.frcnotebook.datafeed.MATCH_PROPS.java
com.plnyyanks.frcnotebook.datafeed.MatchDetailFetcher.java
com.plnyyanks.frcnotebook.datafeed.TBADatafeed.java
com.plnyyanks.frcnotebook.datafeed.USFIRSTParser.java
com.plnyyanks.frcnotebook.datatypes.Event.java
com.plnyyanks.frcnotebook.datatypes.ListElement.java
com.plnyyanks.frcnotebook.datatypes.ListGroup.java
com.plnyyanks.frcnotebook.datatypes.ListHeader.java
com.plnyyanks.frcnotebook.datatypes.ListItem.java
com.plnyyanks.frcnotebook.datatypes.Match.java
com.plnyyanks.frcnotebook.datatypes.Note.java
com.plnyyanks.frcnotebook.datatypes.Team.java
com.plnyyanks.frcnotebook.dialogs.AddNoteDialog.java
com.plnyyanks.frcnotebook.dialogs.AddPredefNoteDialog.java
com.plnyyanks.frcnotebook.dialogs.DatePickerFragment.java
com.plnyyanks.frcnotebook.dialogs.DeleteDialog.java
com.plnyyanks.frcnotebook.dialogs.EditNoteDialog.java
com.plnyyanks.frcnotebook.dialogs.EditPredefNoteDialog.java
com.plnyyanks.frcnotebook.dialogs.InputURLForMatchesDialog.java
com.plnyyanks.frcnotebook.dialogs.ProgressDialog.java
com.plnyyanks.frcnotebook.json.JSONManager.java