FriendsOrFollowsList.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. package com.ouling.weibo;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import com.ouling.weibo.OAuth.OAuthConstant;
  5. import com.ouling.weibo.listener.OnGetMoreListener;
  6. import com.ouling.weibo.listener.OnRefreshListener;
  7. import com.ouling.weibo.basic.User;
  8. import com.ouling.weibo.basic.UserWapper;
  9. import com.ouling.weibo.basic.Weibo;
  10. import com.ouling.weibo.basic.WeiboException;
  11. import com.ouling.weibo.org.json.JSONException;
  12. import com.ouling.weibo.org.json.JSONObject;
  13. import com.ouling.weibo.util.AsyncImageView;
  14. import com.ouling.weibo.util.AutoGetMoreListView;
  15. import com.ouling.weibo.util.ImageRel;
  16. import com.ouling.weibo.util.InfoHelper;
  17. import com.ouling.weibo.util.WaitingView;
  18. import android.app.Activity;
  19. import android.app.NotificationManager;
  20. import android.content.Intent;
  21. import android.graphics.Color;
  22. import android.os.Bundle;
  23. import android.os.Handler;
  24. import android.os.Message;
  25. import android.text.TextUtils;
  26. import android.view.View;
  27. import android.view.View.OnClickListener;
  28. import android.view.ViewGroup;
  29. import android.view.ViewGroup.LayoutParams;
  30. import android.widget.AdapterView;
  31. import android.widget.AdapterView.OnItemClickListener;
  32. import android.widget.BaseAdapter;
  33. import android.widget.Button;
  34. import android.widget.LinearLayout;
  35. import android.widget.TextView;
  36. import android.widget.Toast;
  37. public class FriendsOrFollowsList extends Activity implements
  38. OnRefreshListener, OnGetMoreListener {
  39. static class ViewHolder {
  40. AsyncImageView asyncImageView;
  41. TextView info;
  42. Button btn;
  43. }
  44. private FriendsOrFollowsListAdapter adapter;
  45. private AutoGetMoreListView autoGetMoreListView;
  46. private List<User> list = new ArrayList<User>();
  47. private List<JSONObject> list_relations = new ArrayList<JSONObject>();
  48. private boolean flag; // true for follower, false for friends
  49. private String cid;
  50. private UserWapper curUserWapper;
  51. private Handler handler = new Handler() {
  52. @Override
  53. public void handleMessage(Message msg) {
  54. super.handleMessage(msg);
  55. switch (msg.what) {
  56. case InfoHelper.LOADING_DATA_FAILED:
  57. Toast.makeText(FriendsOrFollowsList.this, "获取信息失败",
  58. Toast.LENGTH_LONG).show();
  59. FriendsOrFollowsList.this.finish();
  60. break;
  61. case InfoHelper.LOADING_DATA_COMPLETED:
  62. adapter.notifyDataSetChanged();
  63. FriendsOrFollowsList.this
  64. .setContentView(autoGetMoreListView);
  65. Toast.makeText(FriendsOrFollowsList.this, "刷新完成",
  66. Toast.LENGTH_LONG).show();
  67. break;
  68. }
  69. }
  70. };
  71. @Override
  72. public void onCreate(Bundle savedInstanceState) {
  73. super.onCreate(savedInstanceState);
  74. System.setProperty("weibo4j.oauth.consumerKey", Weibo.CONSUMER_KEY);
  75. System.setProperty("weibo4j.oauth.consumerSecret",
  76. Weibo.CONSUMER_SECRET);
  77. setContentView(new WaitingView(this));
  78. cid = null;
  79. Bundle bundle = getIntent().getExtras();
  80. if (bundle != null) {
  81. flag = bundle.getBoolean("flag"); //判断获取关注还是粉丝
  82. cid = bundle.getString("cid");
  83. }
  84. if (TextUtils.isEmpty(cid))
  85. cid = OAuthConstant.getInstance().getAccessToken().getUserId() + "";
  86. autoGetMoreListView = new AutoGetMoreListView(this);
  87. autoGetMoreListView.setLayoutParams(new LayoutParams(
  88. LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
  89. adapter = new FriendsOrFollowsListAdapter();
  90. autoGetMoreListView.setAdapter(adapter);
  91. autoGetMoreListView.setOnItemClickListener(new OnItemClickListener() {
  92. @Override
  93. public void onItemClick(AdapterView<?> parent, View view,
  94. int position, long id) {
  95. Intent intent = new Intent(FriendsOrFollowsList.this,
  96. UserInfo.class);
  97. if (position <= list.size())
  98. intent.putExtra("cid", list.get(position - 1).getId() + "");
  99. startActivity(intent);
  100. }
  101. });
  102. autoGetMoreListView.setOnRefreshListener(this);
  103. autoGetMoreListView.setOnGetMoreListener(this);
  104. // setContentView(autoGetMoreListView);
  105. new Thread(new Runnable() {
  106. @Override
  107. public void run() {
  108. try {
  109. Weibo weibo = OAuthConstant.getInstance().getWeibo();
  110. if (flag) {
  111. curUserWapper = weibo.getFollowersStatuses(cid, -1);
  112. list = curUserWapper.getUsers();
  113. NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  114. notificationManager
  115. .cancel(MainActivity.UNREAD_FOLLOWER);
  116. weibo.resetCount(4);
  117. } else {
  118. curUserWapper = weibo.getFriendsStatuses(cid, -1);
  119. list = curUserWapper.getUsers();
  120. }
  121. getRelation(false, 0);
  122. handler.sendEmptyMessage(InfoHelper.LOADING_DATA_COMPLETED);
  123. } catch (WeiboException e) {
  124. e.printStackTrace();
  125. handler.sendEmptyMessage(InfoHelper.LOADING_DATA_FAILED);
  126. }
  127. }
  128. }).start();
  129. }
  130. protected void getRelation(boolean isGetMore, int beg) {
  131. if (!isGetMore)
  132. list_relations.clear();
  133. Weibo weibo = OAuthConstant.getInstance().getWeibo();
  134. for (int index = beg; index < list.size(); index++) {
  135. User user = list.get(index);
  136. try {
  137. list_relations.add(weibo.showFriendships(OAuthConstant
  138. .getInstance().getAccessToken().getUserId()
  139. + "", user.getId() + ""));
  140. } catch (WeiboException e) {
  141. e.printStackTrace();
  142. }
  143. }
  144. }
  145. class FriendsOrFollowsListAdapter extends BaseAdapter {
  146. @Override
  147. public int getCount() {
  148. return list.size();
  149. }
  150. @Override
  151. public User getItem(int position) {
  152. return list.get(position);
  153. }
  154. @Override
  155. public long getItemId(int position) {
  156. return position;
  157. }
  158. @Override
  159. public View getView(int position, View convertView, ViewGroup parent) {
  160. final User user = list.get(position);
  161. final ViewHolder holder;
  162. if (convertView == null) {
  163. LinearLayout friLayout = new LinearLayout(FriendsOrFollowsList.this);
  164. friLayout.setOrientation(LinearLayout.HORIZONTAL);
  165. LinearLayout reLayout = new LinearLayout(FriendsOrFollowsList.this);
  166. reLayout.setOrientation(LinearLayout.VERTICAL);
  167. reLayout.setLayoutParams(new LayoutParams(130, LayoutParams.WRAP_CONTENT));
  168. AsyncImageView asyncImageView = new AsyncImageView(
  169. FriendsOrFollowsList.this);
  170. TextView info = new TextView(FriendsOrFollowsList.this);
  171. final Button btn = new Button(FriendsOrFollowsList.this);
  172. reLayout.addView(asyncImageView);
  173. reLayout.addView(btn);
  174. friLayout.addView(reLayout);
  175. friLayout.addView(info);
  176. convertView = friLayout;
  177. holder = new ViewHolder();
  178. holder.asyncImageView = asyncImageView;
  179. holder.info = info;
  180. holder.btn = btn;
  181. convertView.setTag(holder);
  182. } else {
  183. holder = (ViewHolder) convertView.getTag();
  184. }
  185. holder.asyncImageView.setPadding(10, 10, 10, 10);
  186. holder.asyncImageView.setProgressBitmaps(ImageRel
  187. .getBitmaps_avatar(FriendsOrFollowsList.this));
  188. holder.asyncImageView.setUrl(user.getProfileImageURL().toString());
  189. String info_text = "昵称: "
  190. + user.getScreenName()
  191. + "\n性别: "
  192. + (user.getGender().equals("m") ? "男" : "女")
  193. + "\n所在地: "
  194. + user.getLocation()
  195. + "\n博客地址:"
  196. + ((user.getURL() != null) ? user.getURL().toString() : "无")
  197. + "\n粉丝数: " + user.getFollowersCount() + "\n关注数: "
  198. + user.getFriendsCount();
  199. // Weibo weibo = OAuthConstant.getInstance().getWeibo();
  200. final String sex = (user.getGender().equals("m") ? "他" : "她");
  201. boolean following = false;
  202. boolean followed = true;
  203. try {
  204. JSONObject object = list_relations.get(position);
  205. JSONObject source = object.getJSONObject("source");
  206. JSONObject target = object.getJSONObject("target");
  207. following = Boolean.valueOf(target.getString("followed_by")); // 我是否关注他
  208. followed = Boolean.valueOf(source.getString("followed_by")); // 他是否关注我
  209. if (following && followed) {
  210. info_text = info_text + "\n" + "互相关注";
  211. } else {
  212. if (following) {
  213. info_text = info_text + "\n" + "我已关注" + sex;
  214. } else {
  215. info_text = info_text + "\n" + "我未关注" + sex;
  216. }
  217. if (followed) {
  218. info_text = info_text + "\n" + sex + "已关注我";
  219. } else {
  220. info_text = info_text + "\n" + sex + "未关注我";
  221. }
  222. }
  223. }
  224. // catch (WeiboException e) {
  225. // e.printStackTrace();
  226. // }
  227. catch (JSONException e) {
  228. e.printStackTrace();
  229. } catch (Exception e) {
  230. }
  231. holder.info.setPadding(10, 5, 10, 10);
  232. holder.info.setText(info_text);
  233. holder.info.setTextColor(Color.BLACK);
  234. if (following)
  235. holder.btn.setText("取消关注");
  236. else
  237. holder.btn.setText("关注" + sex);
  238. holder.btn.setOnClickListener(new OnClickListener() {
  239. @Override
  240. public void onClick(View v) {
  241. Weibo weibo = OAuthConstant.getInstance().getWeibo();
  242. if (holder.btn.getText().equals("关注" + sex)) {
  243. try {
  244. // 官方要求要用post,函数createFriendship(id)是用get
  245. weibo.createFriendshipByUserid(user.getId() + "");
  246. holder.btn.setText("取消关注");
  247. Toast.makeText(FriendsOrFollowsList.this, "关注成功!",
  248. Toast.LENGTH_SHORT).show();
  249. } catch (WeiboException e) {
  250. e.printStackTrace();
  251. Toast.makeText(FriendsOrFollowsList.this, "关注失败!",
  252. Toast.LENGTH_SHORT).show();
  253. }
  254. } else {
  255. try {
  256. weibo.destroyFriendship(user.getId() + "");
  257. holder.btn.setText("关注" + sex);
  258. Toast.makeText(FriendsOrFollowsList.this,
  259. "取消关注成功!", Toast.LENGTH_SHORT).show();
  260. } catch (WeiboException e) {
  261. e.printStackTrace();
  262. Toast.makeText(FriendsOrFollowsList.this,
  263. "取消关注失败!", Toast.LENGTH_SHORT).show();
  264. }
  265. }
  266. }
  267. });
  268. holder.btn.setFocusable(false);
  269. return convertView;
  270. }
  271. }
  272. @Override
  273. public void onGetMore() {
  274. new Thread(new Runnable() {
  275. @Override
  276. public void run() {
  277. if (curUserWapper.getNextCursor() != 0) {
  278. try {
  279. Weibo weibo = OAuthConstant.getInstance().getWeibo();
  280. List<User> tmp;
  281. if (flag) {
  282. curUserWapper = weibo.getFollowersStatuses(cid,
  283. curUserWapper.getNextCursor());
  284. tmp = curUserWapper.getUsers();
  285. } else {
  286. curUserWapper = weibo.getFriendsStatuses(cid,
  287. curUserWapper.getNextCursor());
  288. tmp = curUserWapper.getUsers();
  289. }
  290. list.addAll(tmp);
  291. getRelation(true, list.size() - tmp.size());
  292. } catch (WeiboException e) {
  293. e.printStackTrace();
  294. }
  295. }
  296. autoGetMoreListView.getMoreFinished();
  297. }
  298. }).start();
  299. }
  300. @Override
  301. public void onRefresh() {
  302. new Thread(new Runnable() {
  303. @Override
  304. public void run() {
  305. try {
  306. Weibo weibo = OAuthConstant.getInstance().getWeibo();
  307. List<User> tmp;
  308. if (flag)
  309. tmp = weibo.getFollowersStatuses(cid);
  310. else
  311. tmp = weibo.getFriendsStatuses(cid);
  312. list.clear();
  313. list = tmp;
  314. getRelation(false, 0);
  315. } catch (WeiboException e) {
  316. e.printStackTrace();
  317. }
  318. autoGetMoreListView.refreshFinished();
  319. }
  320. }).start();
  321. }
  322. }