package org.briarproject.android.groups; import static android.view.Gravity.CENTER_HORIZONTAL; import static android.view.Gravity.CENTER_VERTICAL; import static android.widget.LinearLayout.HORIZONTAL; import static android.widget.LinearLayout.VERTICAL; import static java.text.DateFormat.SHORT; import static org.briarproject.android.util.CommonLayoutParams.WRAP_WRAP_1; import java.util.ArrayList; import org.briarproject.R; import org.briarproject.android.util.AuthorView; import org.briarproject.android.util.LayoutUtils; import org.briarproject.api.Author; import org.briarproject.api.db.MessageHeader; import org.briarproject.util.StringUtils; import android.content.Context; import android.content.res.Resources; import android.text.format.DateUtils; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ImageButton; import android.widget.LinearLayout; import android.widget.TextView; class GroupAdapter extends ArrayAdapter { private final int pad; GroupAdapter(Context ctx) { super(ctx, android.R.layout.simple_expandable_list_item_1, new ArrayList()); pad = LayoutUtils.getPadding(ctx); } @Override public View getView(int position, View convertView, ViewGroup parent) { GroupItem item = getItem(position); MessageHeader header = item.getHeader(); Context ctx = getContext(); Resources res = ctx.getResources(); LinearLayout headerLayout = new LinearLayout(ctx); headerLayout.setOrientation(HORIZONTAL); headerLayout.setGravity(CENTER_VERTICAL); int background; if(header.isRead()) background = res.getColor(R.color.read_background); else background = res.getColor(R.color.unread_background); headerLayout.setBackgroundColor(background); AuthorView authorView = new AuthorView(ctx); authorView.setLayoutParams(WRAP_WRAP_1); Author author = header.getAuthor(); if(author == null) authorView.init(null, header.getAuthorStatus()); else authorView.init(author.getName(), header.getAuthorStatus()); headerLayout.addView(authorView); // FIXME: Factor this out into a TimestampView TextView date = new TextView(ctx); date.setTextSize(14); date.setPadding(0, pad, pad, pad); long then = header.getTimestamp(), now = System.currentTimeMillis(); date.setText(DateUtils.formatSameDayTime(then, now, SHORT, SHORT)); headerLayout.addView(date); if(!item.isExpanded() || item.getBody() == null) return headerLayout; LinearLayout expanded = new LinearLayout(ctx); expanded.setOrientation(VERTICAL); expanded.setGravity(CENTER_HORIZONTAL); expanded.setBackgroundColor(background); expanded.addView(headerLayout); if(header.getContentType().equals("text/plain")) { TextView text = new TextView(ctx); text.setPadding(pad, 0, pad, pad); text.setBackgroundColor(background); text.setText(StringUtils.fromUtf8(item.getBody())); expanded.addView(text); } else { ImageButton attachment = new ImageButton(ctx); attachment.setPadding(pad, 0, pad, pad); attachment.setImageResource(R.drawable.content_attachment); expanded.addView(attachment); } return expanded; } }