TwitList.java :  » UnTagged » kyoto-gtug » com » ts0604 » twitterapi » Android Open Source

Android Open Source » UnTagged » kyoto gtug 
kyoto gtug » com » ts0604 » twitterapi » TwitList.java
package com.ts0604.twitterapi;

import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

import twitter4j.PagableResponseList;
import twitter4j.Paging;
import twitter4j.ResponseList;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.User;
import twitter4j.UserList;


class TwitList {

  private String targetID = "";  //TwitterID
  private String targetListFullName = "";  //
  private int targetListID = 0;  //ID
  private int memberCount = 0;  //
  private URI uri = null;  //URI

  private String[] userNames = null;  //
  private int[] userIDs = null;  //ID
  private String[] userScreenNames = null;  //

  private static final int USER_COUNT_IN_CURSOR = 20;  //1

  private boolean result = false;  //
  private String message = "";  //
  private static final String NOT_FOUND_LIST = "";
  private static final String FAILURE_GET_LISTS = "";
  private static final String FAILURE_GET_MEMBERS = "";
  private static final String FAILURE_GET_LIST_TIMELINE = "";

  private Twitter twitter = null;  //API


  public TwitList(String targetID, String targetListName)
  {
    this.targetID = targetID;
    if(targetListName.indexOf("@") != -1)
      this.targetListFullName = targetListName;
    else
      this.targetListFullName = "@" + this.targetID + "/" + targetListName;

    twitter = TngtAccounts.getInstance().getTwitter();  //

    PagableResponseList<UserList> lists;
    UserList list;
    try {
      lists = twitter.getUserLists(this.targetID, -1);


      for(int i = 0; i < lists.size(); i++)
      {
        if((list = this.findTargetList(this.targetListFullName, lists)) != null)  //;
        {
          this.targetListID = list.getId();
          this.uri = list.getURI();
          this.getUsers(list);//
          result = true;
          break;
        }
        else
        {
          this.message = NOT_FOUND_LIST;
          result = false;
        }

      }

    } catch (TwitterException e1) {
      this.message = FAILURE_GET_LISTS;
      result = false;
      e1.printStackTrace();
    }
  }

  //
  private UserList findTargetList(String targetListName, PagableResponseList<UserList> lists)
  {
    UserList list;
    //
    for(int i = 0; i < lists.size(); i++)
    {
      list = lists.get(i);
      if(list.getFullName().equals(targetListName))
      {
        return list;
      }
    }

    return null;
  }

  //
  private void getUsers(UserList list)
  {
    this.memberCount = list.getMemberCount();  //
    long cursor = 0L;
    long cursorMax = 0L;
    cursorMax = memberCount / USER_COUNT_IN_CURSOR + 1;  //1 + 1

    this.userNames = new String[memberCount];
    this.userIDs = new int[memberCount];
    this.userScreenNames = new String[memberCount];
    int index = 0;
    for(int i = 0; i < cursorMax; i++)
    {
      cursor = i;
      if(cursor == 0) cursor = -1;

      PagableResponseList<User> listUsers;
      try {
        listUsers = twitter.getUserListMembers(targetID, targetListID, cursor);//
        for(int j = 0; j < listUsers.size(); j++)
        {
          index = i * USER_COUNT_IN_CURSOR + j;
          this.userNames[index] = listUsers.get(j).getName();  //
          this.userIDs[index] = listUsers.get(j).getId();  //ID
          this.userScreenNames[index] = listUsers.get(j).getScreenName();//
        }
        result = true;
      } catch (TwitterException e) {
        this.message = FAILURE_GET_MEMBERS;
        result = false;
        e.printStackTrace();
      }
    }
  }

  ListTimeLine listTimeLine = null;

  //
  public ListTimeLine getTimeLine(int page)
  {
    try {
      ResponseList<Status> statuses = twitter.getUserListStatuses(this.targetID,
                                      this.targetListID,
                                      new Paging(page));
      this.listTimeLine = new ListTimeLine();
      Tubuyaki tubuyaki;
      Status status;
      SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");
      dateFormat.setTimeZone(TimeZone.getTimeZone("JST"));
      for(int i = 0; i < statuses.size(); i++)
      {
        status = statuses.get(i);
        tubuyaki = new Tubuyaki((int)status.getId(),
                    (int)status.getUser().getId(),
                    status.getUser().getScreenName(),
                    status.getText(),
                    dateFormat.format(status.getCreatedAt()));
        listTimeLine.addTubuyaki(tubuyaki);
      }
      result = true;

    } catch (TwitterException e) {
      e.printStackTrace();
      result = false;
      this.message = FAILURE_GET_LIST_TIMELINE;
      return null;
    }
    return listTimeLine;
  }

  //
  public int getMemberCount() { return this.memberCount; }

  //
  public String getName(int index) { return this.userNames[index]; }

  //
  public String getListName() { return this.targetListFullName; }

  //
  public String getName() { return this.targetID; }

  //ID
  public int getID(int index) { return this.userIDs[index]; }

  //URI
  public URI getURI() { return this.uri; }

  //
  public String getScreenName(int index) { return this.userScreenNames[index]; }

  //
  public String getErrMessage() { return this.message; }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.