Android Open Source - scrumchatter Meeting Cursor Adapter






From Project

Back to project page scrumchatter.

License

The source code is released under:

GNU General Public License

If you think the Android project scrumchatter 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

/**
 * Copyright 2013 Carmen Alvarez/*from  ww w  . j a  v  a  2  s  .  co  m*/
 *
 * This file is part of Scrum Chatter.
 *
 * Scrum Chatter 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.
 *
 * Scrum Chatter 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 Scrum Chatter. If not, see <http://www.gnu.org/licenses/>.
 */
package ca.rmen.android.scrumchatter.meeting.detail;

import android.content.Context;
import android.database.Cursor;
import android.graphics.drawable.AnimationDrawable;
import android.os.SystemClock;
import android.support.v4.widget.CursorAdapter;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.Chronometer;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import ca.rmen.android.scrumchatter.Constants;
import ca.rmen.android.scrumchatter.R;
import ca.rmen.android.scrumchatter.provider.MeetingColumns.State;
import ca.rmen.android.scrumchatter.provider.MeetingMemberCursorWrapper;
import ca.rmen.android.scrumchatter.util.ViewHolder;

/**
 * Adapter for the list of members in one meeting, and their speaking durations
 * for that meeting.
 */
class MeetingCursorAdapter extends CursorAdapter {
    private static final String TAG = Constants.TAG + "/" + MeetingCursorAdapter.class.getSimpleName();
    private final OnClickListener mOnClickListener;
    private final int mColorChronoActive;
    private final int mColorChronoInactive;
    private final int mColorChronoNotStarted;

    /**
     * @param onClickListener
     *            clicks on widgets on each list item will be forwarded to this
     *            listener.
     */
    MeetingCursorAdapter(Context context, OnClickListener onClickListener) {
        super(context, null, false);
        Log.v(TAG, "Constructor");
        mOnClickListener = onClickListener;
        mColorChronoActive = context.getResources().getColor(R.color.chrono_active);
        mColorChronoInactive = context.getResources().getColor(R.color.chrono_inactive);
        mColorChronoNotStarted = context.getResources().getColor(R.color.chrono_not_started);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup viewGroup) {
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = layoutInflater.inflate(R.layout.meeting_member_list_item, null);
        return view;
    }

    /**
     * Set the view elements (TextView text, etc) for the given member of a
     * meeting.
     * 
     * @param view
     *            a view we just created or recycled
     * @param cursor
     *            a row for one member in one meeting.
     */
    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        // Extract the fields we need from this cursor
        @SuppressWarnings("resource")
        MeetingMemberCursorWrapper cursorWrapper = new MeetingMemberCursorWrapper(cursor);
        Long memberId = cursorWrapper.getMemberId();
        String memberName = cursorWrapper.getMemberName();
        long duration = cursorWrapper.getDuration();
        State meetingState = cursorWrapper.getMeetingState();
        Long talkStartTime = cursorWrapper.getTalkStartTime();

        // Find the Views we need to set up
        TextView tvName = ViewHolder.get(view, R.id.tv_name);
        Chronometer chronometer = ViewHolder.get(view, R.id.tv_duration);
        ImageButton btnStartStop = ViewHolder.get(view, R.id.btn_start_stop_member);
        final ImageView ivChatterFace = ViewHolder.get(view, R.id.iv_chatter_face);
        // Set up the member's name
        tvName.setText(memberName);

        // if the talkStartTime is non-zero, this means the
        // member is talking (and started talking that long ago).
        boolean memberIsTalking = talkStartTime != null && talkStartTime > 0;

        // Set up the start/stop button for this member.
        // If the meeting is finished, we hide the start/stop button.
        if (meetingState == State.FINISHED) {
            btnStartStop.setVisibility(View.INVISIBLE);
        }
        // If the meeting is in progress, set the button to stop
        // or start, depending on whether the member is already talking
        // or not.
        else {
            btnStartStop.setOnClickListener(mOnClickListener);
            btnStartStop.setImageResource(memberIsTalking ? R.drawable.ic_action_stop : R.drawable.ic_action_start);
        }

        // If the member is currently talking, show the chronometer.
        // Otherwise, show the duration that they talked (if any).
        if (memberIsTalking) {
            long hasBeenTalkingFor = duration * 1000 + (System.currentTimeMillis() - talkStartTime);
            chronometer.setBase(SystemClock.elapsedRealtime() - hasBeenTalkingFor);
            chronometer.start();
            chronometer.setTextColor(mColorChronoActive);
            startAnimation(ivChatterFace);
        } else {
            chronometer.stop();
            chronometer.setText(DateUtils.formatElapsedTime(duration));
            chronometer.setTextColor(duration > 0 ? mColorChronoInactive : mColorChronoNotStarted);
            stopAnimation(ivChatterFace);
        }

        // Set the member id as a tag, so when the OnClickListener receives the
        // click action, it knows for which member the user clicked.
        btnStartStop.setTag(memberId);
    }

    /**
     * Show the imageView and start its animation drawable.
     */
    private void startAnimation(final ImageView imageView) {
        if (imageView.getVisibility() != View.VISIBLE) {
            Log.v(TAG, "startAnimation");
            imageView.setVisibility(View.VISIBLE);
            final AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getDrawable();
            // On some devices, directly calling start() on the animation does not work.
            // We have to wait until the ImageView is visible before starting the animation.
            imageView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
                @SuppressWarnings("deprecation")
                @Override
                public void onGlobalLayout() {
                    if (!animationDrawable.isRunning()) {
                        imageView.post(new Runnable() {

                            @Override
                            public void run() {
                                animationDrawable.setVisible(true, true);
                            }
                        });
                    }
                    imageView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                }
            });
        }
    }

    /**
     * Stop the animation drawable on this imageView and hide the imageView.
     */
    private void stopAnimation(final ImageView imageView) {
        if (imageView.getVisibility() == View.VISIBLE) {
            Log.v(TAG, "stopAnimation");
            imageView.setVisibility(View.INVISIBLE);
            final AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getDrawable();
            animationDrawable.setVisible(false, false);
        }
    }
}




Java Source Code List

ca.rmen.android.scrumchatter.Constants.java
ca.rmen.android.scrumchatter.about.AboutActivity.java
ca.rmen.android.scrumchatter.dialog.ChoiceDialogFragment.java
ca.rmen.android.scrumchatter.dialog.ConfirmDialogFragment.java
ca.rmen.android.scrumchatter.dialog.DialogFragmentFactory.java
ca.rmen.android.scrumchatter.dialog.DialogStyleHacks.java
ca.rmen.android.scrumchatter.dialog.InfoDialogFragment.java
ca.rmen.android.scrumchatter.dialog.InputDialogFragment.java
ca.rmen.android.scrumchatter.dialog.ProgressDialogFragment.java
ca.rmen.android.scrumchatter.export.DBExport.java
ca.rmen.android.scrumchatter.export.FileExport.java
ca.rmen.android.scrumchatter.export.MeetingExport.java
ca.rmen.android.scrumchatter.export.MeetingsExport.java
ca.rmen.android.scrumchatter.main.MainActivity.java
ca.rmen.android.scrumchatter.meeting.Meetings.java
ca.rmen.android.scrumchatter.meeting.detail.MeetingActivity.java
ca.rmen.android.scrumchatter.meeting.detail.MeetingCursorAdapter.java
ca.rmen.android.scrumchatter.meeting.detail.MeetingFragment.java
ca.rmen.android.scrumchatter.meeting.detail.MeetingPagerAdapter.java
ca.rmen.android.scrumchatter.meeting.detail.Meeting.java
ca.rmen.android.scrumchatter.meeting.list.MeetingsCursorAdapter.java
ca.rmen.android.scrumchatter.meeting.list.MeetingsListFragment.java
ca.rmen.android.scrumchatter.member.list.MembersCursorAdapter.java
ca.rmen.android.scrumchatter.member.list.MembersListFragment.java
ca.rmen.android.scrumchatter.member.list.Members.java
ca.rmen.android.scrumchatter.provider.DBImport.java
ca.rmen.android.scrumchatter.provider.MeetingColumns.java
ca.rmen.android.scrumchatter.provider.MeetingCursorWrapper.java
ca.rmen.android.scrumchatter.provider.MeetingMemberColumns.java
ca.rmen.android.scrumchatter.provider.MeetingMemberCursorWrapper.java
ca.rmen.android.scrumchatter.provider.MemberColumns.java
ca.rmen.android.scrumchatter.provider.MemberCursorWrapper.java
ca.rmen.android.scrumchatter.provider.MemberStatsColumns.java
ca.rmen.android.scrumchatter.provider.ScrumChatterDatabase.java
ca.rmen.android.scrumchatter.provider.ScrumChatterProvider.java
ca.rmen.android.scrumchatter.provider.TeamColumns.java
ca.rmen.android.scrumchatter.team.TeamArrayAdapter.java
ca.rmen.android.scrumchatter.team.Teams.java
ca.rmen.android.scrumchatter.util.IOUtils.java
ca.rmen.android.scrumchatter.util.TextUtils.java
ca.rmen.android.scrumchatter.util.ViewHolder.java