MainActivity.java 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. package com.ouling.weibo;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import android.app.Notification;
  5. import android.app.NotificationManager;
  6. import android.app.PendingIntent;
  7. import android.app.TabActivity;
  8. import android.content.Context;
  9. import android.content.Intent;
  10. import android.os.Bundle;
  11. import android.os.Handler;
  12. import android.os.Message;
  13. import android.view.Menu;
  14. import android.view.MenuItem;
  15. import android.view.MenuItem.OnMenuItemClickListener;
  16. import android.view.View;
  17. import android.view.View.OnClickListener;
  18. import android.widget.AdapterView;
  19. import android.widget.AdapterView.OnItemClickListener;
  20. import android.widget.FrameLayout;
  21. import android.widget.TabHost;
  22. import android.widget.TabHost.TabContentFactory;
  23. import android.widget.TabHost.TabSpec;
  24. import android.widget.TabWidget;
  25. import android.widget.Toast;
  26. import com.ouling.weibo.OAuth.OAuthConstant;
  27. import com.ouling.weibo.OAuth.OAuthUserList;
  28. import com.ouling.weibo.basic.Count;
  29. import com.ouling.weibo.basic.Status;
  30. import com.ouling.weibo.basic.Weibo;
  31. import com.ouling.weibo.basic.WeiboException;
  32. import com.ouling.weibo.basic.WeiboResponse;
  33. import com.ouling.weibo.db.DBAdapter;
  34. import com.ouling.weibo.pic.WeiboEmotion;
  35. import com.ouling.weibo.util.InfoHelper;
  36. import com.ouling.weibo.util.AutoGetMoreListView;
  37. import com.ouling.weibo.util.WaitingView;
  38. /**
  39. * 显示用户自己以及其关注的人的微博列表/用户的个人资料
  40. *
  41. *
  42. */
  43. public class MainActivity extends TabActivity implements TabContentFactory
  44. {
  45. public static int UNREAD_COMMENT = 1000;
  46. public static int UNREAD_FOLLOWER = 2000;
  47. public static int UNREAD_MENTION = 3000;
  48. private TabHost tabHost;
  49. private WeiboAdapter msgAdapter;
  50. private List<WeiboResponse> friendsTimeline = new ArrayList<WeiboResponse>();
  51. private AutoGetMoreListView autoGetMoreListView;
  52. private int preIndex = 0;
  53. private FrameLayout ff;
  54. private WaitingView waitingView;
  55. private Handler handler = new Handler() {
  56. @Override
  57. public void handleMessage(Message msg) {
  58. super.handleMessage(msg);
  59. switch (msg.what) {
  60. case InfoHelper.LOADING_DATA_FAILED:
  61. Toast.makeText(MainActivity.this, "获取信息失败", Toast.LENGTH_LONG)
  62. .show();
  63. MainActivity.this.finish();
  64. break;
  65. case InfoHelper.LOADING_DATA_COMPLETED:
  66. msgAdapter.notifyDataSetChanged();
  67. waitingView.setVisibility(View.GONE);
  68. autoGetMoreListView.setVisibility(View.VISIBLE);
  69. DBAdapter.getInstance(MainActivity.this).saveFriendTimeline(
  70. friendsTimeline);
  71. autoGetMoreListView.setSelection(1);
  72. Toast.makeText(MainActivity.this, "刷新首页微博完成", Toast.LENGTH_LONG)
  73. .show();
  74. break;
  75. default:
  76. break;
  77. }
  78. }
  79. };
  80. @Override
  81. public void onCreate(Bundle savedInstanceState) {
  82. super.onCreate(savedInstanceState);
  83. System.setProperty("weibo4j.oauth.consumerKey", Weibo.CONSUMER_KEY);
  84. System.setProperty("weibo4j.oauth.consumerSecret",
  85. Weibo.CONSUMER_SECRET);
  86. waitingView = new WaitingView(this);
  87. autoGetMoreListView = new AutoGetMoreListView(this);
  88. msgAdapter = new WeiboAdapter(friendsTimeline, this, 3,
  89. autoGetMoreListView);
  90. autoGetMoreListView.setAdapter(msgAdapter);
  91. autoGetMoreListView.setOnGetMoreListener(msgAdapter);
  92. autoGetMoreListView.setOnRefreshListener(msgAdapter);
  93. autoGetMoreListView.setOnItemClickListener(new OnItemClickListener() {
  94. @Override
  95. public void onItemClick(AdapterView<?> parent, View view,
  96. int position, long id) {
  97. Intent intent = new Intent(MainActivity.this,
  98. ViewActivity.class);
  99. intent.putExtra("cid",
  100. ((Status) friendsTimeline.get(position - 1)).getId());
  101. startActivityForResult(intent, 1);
  102. }
  103. });
  104. initTab();
  105. }
  106. //初始化切换布局
  107. private void initTab() {
  108. setContentView(R.layout.tabactivity);
  109. tabHost = this.getTabHost();
  110. tabHost.setBackgroundResource(R.drawable.bg);
  111. ff = new FrameLayout(this);
  112. TabSpec ts1 = tabHost.newTabSpec("HOME").setIndicator("首页");
  113. ts1.setContent(this);
  114. tabHost.addTab(ts1);
  115. TabSpec ts2 = tabHost.newTabSpec("MSG").setIndicator("消息")
  116. .setContent(new Intent(this, InfoActivity.class));
  117. tabHost.addTab(ts2);
  118. TabSpec ts3 = tabHost.newTabSpec("INFO").setIndicator("资料")
  119. .setContent(new Intent(this, UserInfo.class));
  120. tabHost.addTab(ts3);
  121. //切换改变时
  122. TabWidget widget = tabHost.getTabWidget();
  123. for (int i = 0; i < 3; i++) {
  124. View view = widget.getChildAt(i);
  125. view.setBackgroundResource(R.drawable.widget_btn);
  126. final int index = i;
  127. view.setOnClickListener(new OnClickListener() {
  128. @Override
  129. public void onClick(View v) {
  130. preIndex = tabHost.getCurrentTab();
  131. if (tabHost.getCurrentTab() == index) {
  132. autoGetMoreListView.setSelection(1);
  133. } else {
  134. tabHost.setCurrentTab(index);
  135. preIndex = tabHost.getCurrentTab();
  136. }
  137. }
  138. });
  139. }
  140. }
  141. //发微博
  142. @Override
  143. public boolean onCreateOptionsMenu(Menu menu) {
  144. menu.add(1, 1, 1, "发微博").setIntent(
  145. new Intent(this, ShareActivity.class));
  146. // Intent intent1 = new Intent(this, FriendsOrFollowsList.class);
  147. // intent1.putExtra("flag", false);
  148. return true;
  149. }
  150. public static void unReadNotify(Context context) {
  151. Weibo weibo = OAuthConstant.getInstance().getWeibo();
  152. Count unread_count;
  153. try {
  154. unread_count = weibo.getUnread();
  155. NotificationManager notificationManager = (NotificationManager) context
  156. .getSystemService(NOTIFICATION_SERVICE);
  157. if (unread_count.getComments() != 0) {
  158. Notification notification = new Notification(R.drawable.image,
  159. "你有" + unread_count.getComments() + "条未读评论.",
  160. System.currentTimeMillis());
  161. Intent intent = new Intent(context, InfoActivity.class);
  162. intent.putExtra("type", 0);
  163. PendingIntent contentIntent = PendingIntent.getActivity(
  164. context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
  165. notification.setLatestEventInfo(context,
  166. "你有" + unread_count.getComments() + "条未读评论.", "",
  167. contentIntent);
  168. notification.defaults = Notification.DEFAULT_ALL;
  169. notificationManager.notify(UNREAD_COMMENT, notification);
  170. }
  171. if (unread_count.getMentions() != 0) {
  172. Notification notification = new Notification(R.drawable.image,
  173. "你有" + unread_count.getMentions() + "条未读@你的微薄.",
  174. System.currentTimeMillis());
  175. Intent intent = new Intent(context, InfoActivity.class);
  176. intent.putExtra("type", 2);
  177. PendingIntent contentIntent = PendingIntent.getActivity(
  178. context, UNREAD_MENTION, intent,
  179. PendingIntent.FLAG_UPDATE_CURRENT);
  180. notification.setLatestEventInfo(context,
  181. "你有" + unread_count.getMentions() + "条未读@你的微薄.", "",
  182. contentIntent);
  183. notification.defaults = Notification.DEFAULT_ALL;
  184. notificationManager.notify(UNREAD_MENTION, notification);
  185. }
  186. if (unread_count.getFollowers() != 0) {
  187. Notification notification = new Notification(R.drawable.image,
  188. "你有" + unread_count.getFollowers() + "新的关注者.",
  189. System.currentTimeMillis());
  190. Intent intent = new Intent(context, FriendsOrFollowsList.class);
  191. intent.putExtra("flag", true);
  192. PendingIntent contentIntent = PendingIntent.getActivity(
  193. context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
  194. notification.setLatestEventInfo(context,
  195. "你有" + unread_count.getFollowers() + "新的关注者.", "",
  196. contentIntent);
  197. notification.defaults = Notification.DEFAULT_ALL;
  198. notificationManager.notify(UNREAD_FOLLOWER, notification);
  199. }
  200. if (unread_count.getDm() != 0) {
  201. Notification notification = new Notification(R.drawable.image,
  202. "你有" + unread_count.getDm() + "条未读的私信.",
  203. System.currentTimeMillis());
  204. Intent intent = new Intent(context, OAuthUserList.class);
  205. PendingIntent contentIntent = PendingIntent.getActivity(
  206. context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
  207. notification.setLatestEventInfo(context,
  208. "你有" + unread_count.getDm() + "条未读的私信.", "",
  209. contentIntent);
  210. notification.defaults = Notification.DEFAULT_ALL;
  211. notificationManager.notify(UNREAD_FOLLOWER, notification);
  212. }
  213. } catch (WeiboException e) {
  214. e.printStackTrace();
  215. }
  216. }
  217. @Override
  218. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  219. tabHost.setCurrentTab(preIndex);
  220. }
  221. //首页显示微博
  222. @Override
  223. public View createTabContent(String tag) {
  224. if (tag.equals("HOME")) {
  225. ff.addView(waitingView);
  226. ff.addView(autoGetMoreListView);
  227. autoGetMoreListView.setVisibility(View.GONE);
  228. new Thread(new Runnable() {
  229. @Override
  230. public void run() {
  231. Weibo weibo = OAuthConstant.getInstance().getWeibo();
  232. try {
  233. friendsTimeline.addAll(weibo.getFriendsTimeline());
  234. handler.sendEmptyMessage(InfoHelper.LOADING_DATA_COMPLETED);
  235. } catch (WeiboException e) {
  236. e.printStackTrace();
  237. handler.sendEmptyMessage(InfoHelper.LOADING_DATA_FAILED);
  238. }
  239. unReadNotify(MainActivity.this);
  240. }
  241. }).start();
  242. }
  243. return ff;
  244. }
  245. // @Override
  246. // public boolean onMenuItemClick(MenuItem item) {
  247. // switch (item.getItemId()) {
  248. // case 4:
  249. // Toast.makeText(MainActivity.this, "开始下载表情包,请稍候...",
  250. // Toast.LENGTH_LONG).show();
  251. // new Thread(new Runnable() {
  252. //
  253. // @Override
  254. // public void run() {
  255. // WeiboEmotion.downloadEmotions(handler);
  256. // }
  257. // }).start();
  258. // return true;
  259. // case 5:
  260. // Toast.makeText(MainActivity.this, "开始更新表情包,请稍候...",
  261. // Toast.LENGTH_LONG).show();
  262. // new Thread(new Runnable() {
  263. //
  264. // @Override
  265. // public void run() {
  266. // WeiboEmotion.updateEmotions(handler);
  267. // }
  268. // }).start();
  269. // return true;
  270. // }
  271. // return false;
  272. // }
  273. }