Android Open Source - MyTwitter-Android Timeline Adapter






From Project

Back to project page MyTwitter-Android.

License

The source code is released under:

Apache License

If you think the Android project MyTwitter-Android 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 org.fukata.android.mytw;
//  w ww  . j  a  v  a 2  s  . co  m
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.fukata.android.exandroid.util.StringUtil;
import org.fukata.android.mytw.util.PrettyDateUtil;
import org.fukata.android.mytw.util.SettingUtil;

import android.content.Context;
import android.graphics.Color;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class TimelineAdapter extends ArrayAdapter<TimelineItem> {
  LayoutInflater inflater; 
  List<TimelineItem> items;
  static final Pattern USERNAME_PATTERN = Pattern.compile("(@[a-zA-Z0-9_]+)");
  static final Pattern URL_PATTERN = Pattern.compile("(https?://)[\\w\\.\\-/:\\#\\?\\=\\&\\;\\%\\~\\+\\@\\,\\_\\!\\*\\(\\)]+");
  static final Pattern HASHTAG_PATTERN = Pattern.compile("(#[\\w]+)");
  
  public TimelineAdapter(Context context,  List<TimelineItem> items) {
    super(context, android.R.layout.simple_list_item_1, items);
    this.items = items;
    this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  }
  
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    if (view==null) {
      view = inflater.inflate(R.layout.timeline_item, null);
    }
    
    
    TimelineItem item = getItem(position);
    decorateView(view, item);
    
    // username
    TextView username = (TextView) view.findViewById(R.id.timeline_username);
    username.setText(item.getUsername());
    // source
    TextView source = (TextView) view.findViewById(R.id.timeline_source);
    source.setText(item.getSource());
    // created_at
    TextView createdAt = (TextView) view.findViewById(R.id.timeline_created_at);
    createdAt.setText( PrettyDateUtil.toString(item.getCreatedAt()) );
    // status
    TextView status = (TextView) view.findViewById(R.id.timeline_status);
    status.setText(decorateStatus(item.getStatus()));

    updateTextSize(view);
    
    return view;
  }

  void updateTextSize(View view) {
    float size = SettingUtil.getFontSize();
    ((TextView) view.findViewById(R.id.timeline_username)).setTextSize(size);
    ((TextView) view.findViewById(R.id.timeline_from)).setTextSize(size);
    ((TextView) view.findViewById(R.id.timeline_source)).setTextSize(size);
    ((TextView) view.findViewById(R.id.timeline_created_at)).setTextSize(size);
    ((TextView) view.findViewById(R.id.timeline_status)).setTextSize(size);
  }
  
  void decorateView(View view, TimelineItem item) {
    if (isMention(item, SettingUtil.getAccountName())) {
      view.setBackgroundColor(Color.DKGRAY);
    } else {
      view.setBackgroundColor(Color.TRANSPARENT);
    }
  }

  boolean isMention(TimelineItem item, String accountName) {
    String status = item.getStatus();
    Matcher usernameMatcher = USERNAME_PATTERN.matcher(status);
    usernameMatcher.reset();
    while (usernameMatcher.find()) {
      String username = usernameMatcher.group();
      if (StringUtil.equalsIgnoreCase(username, "@"+accountName)) {
        return true;
      }
    }
    
    return false;
  }

  CharSequence decorateStatus(String status) {
    Map<String, Boolean> replaced = new HashMap<String, Boolean>();

    // username
    Matcher usernameMatcher = USERNAME_PATTERN.matcher(status);
    usernameMatcher.reset();
    while (usernameMatcher.find()) {
      String username = usernameMatcher.group();
      if (replaced.containsKey(username)) {
        continue;
      }
      replaced.put(username, true);
      status = status.replace(username, "<font color=\"green\">"+username+"</font>");
    }
    
    // url
    Matcher urlMatcher = URL_PATTERN.matcher(status);
    urlMatcher.reset();
    while (urlMatcher.find()) {
      String url = urlMatcher.group();
      if (replaced.containsKey(url)) {
        continue;
      }
      replaced.put(url, true);
      status = status.replace(url, "<font color=\"aqua\">"+url+"</font>");
    }
    
    // hashtag
    Matcher hashtagMatcher = HASHTAG_PATTERN.matcher(status);
    hashtagMatcher.reset();
    while (hashtagMatcher.find()) {
      String hashtag = hashtagMatcher.group();
      if (replaced.containsKey(hashtag)) {
        continue;
      }
      replaced.put(hashtag, true);
      status = status.replace(hashtag, "<font color=\"fuchsia\">"+hashtag+"</font>");
    }
    
    return Html.fromHtml(status);
  }
}




Java Source Code List

org.fukata.android.mytw.DirectMessageTimelineAdapter.java
org.fukata.android.mytw.DirectMessageTimelineView.java
org.fukata.android.mytw.HomeTimelineView.java
org.fukata.android.mytw.MentionTimelineView.java
org.fukata.android.mytw.MyTwitterActivity.java
org.fukata.android.mytw.MyTwitterApp.java
org.fukata.android.mytw.SettingsActivity.java
org.fukata.android.mytw.TimelineActivity.java
org.fukata.android.mytw.TimelineAdapter.java
org.fukata.android.mytw.TimelineItem.java
org.fukata.android.mytw.TimelineView.java
org.fukata.android.mytw.UpdateStatusActivity.java
org.fukata.android.mytw.database.MyTwitterDB.java
org.fukata.android.mytw.database.dao.BaseDao.java
org.fukata.android.mytw.database.dao.TweetDao.java
org.fukata.android.mytw.database.dto.TweetDto.java
org.fukata.android.mytw.database.schema.TweetSchema.java
org.fukata.android.mytw.receiver.InstallAnalyticsReceiver.java
org.fukata.android.mytw.twitter.Twitter.java
org.fukata.android.mytw.twitter.rs.Status.java
org.fukata.android.mytw.twitter.rs.User.java
org.fukata.android.mytw.util.PrettyDateUtil.java
org.fukata.android.mytw.util.SettingUtil.java
org.fukata.android.mytw.util.StringMatchUtils.java
org.fukata.android.mytw.util.StringUtils.java