import java.io.IOException;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.weibo.net.AsyncWeiboRunner.RequestListener;
import com.weibo.net.WeiboException;
import com.wwj.sina.weibo.interfaces.Const;
import com.wwj.sina.weibo.library.JSONAndObject;
import com.wwj.sina.weibo.library.WeiboManager;
import com.wwj.sina.weibo.object.Status;
import com.wwj.sina.weibo.util.Tools;
import com.wwj.sina.weibo.workqueue.DoneAndProcess;
import com.wwj.sina.weibo.workqueue.task.FavoriteWeiboTask;
import com.wwj.sina.weibo.workqueue.task.ParentTask;
import com.wwj.sina.weibo.workqueue.task.PullFileTask;
/**
-
浏览单条微博视图
-
@author wwj
*/
public class WeiboViewer extends Activity implements Const, RequestListener,
DoneAndProcess, OnClickListener {
// 必须声明为static,使用intent传递status就不是原来的status了
public static Status status;
private ImageView profileImage; // 头像
private ImageView verified; // 认证图片
private TextView userName; // 用户昵称
private TextView statusText; // 微博内容
private View insideContent; // 转发内容
private TextView retweetdetailText; // 转发文本
private ImageView retweetdetailImage; // 转发内容的图片
private ImageView statusImage; // 微博图片
private TextView source; // 微博来源
private Button forwardButton; // 转发按钮
private Button retweetdetailForwardButton;
private Button commentButton; // 评论按钮
private Button retweetdetailCommentButton;
private Button refreshButton; // 刷新按钮
private ProgressBar refreshProgressBar; // 刷新进度条
private View favorite; // 收藏
private View unfavorite; // 取消收藏
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
// 处理文件下载任务
if (msg.obj instanceof PullFileTask) {
String imageUrl = null;
imageUrl = WeiboManager.getImageurl(WeiboViewer.this,
status.user.profile_image_url, WeiboViewer.this);
if (imageUrl != null) {
profileImage.setImageURI(Uri.fromFile(new File(imageUrl)));
}
if (status.retweeted_status == null) {
imageUrl = WeiboManager.getImageurl(WeiboViewer.this,
status.bmiddle_pic, WeiboViewer.this);
if (imageUrl != null) {
statusImage.setImageURI(Uri
.fromFile(new File(imageUrl)));
}
} else {
imageUrl = WeiboManager.getImageurl(WeiboViewer.this,
status.retweeted_status.bmiddle_pic,
WeiboViewer.this);
if (imageUrl != null) {
retweetdetailImage.setImageURI(Uri.fromFile(new File(
imageUrl)));
}
}
}
// 处理收藏微博任务
else if (msg.obj instanceof FavoriteWeiboTask) {
if (favorite.getVisibility() == View.VISIBLE) {
favorite.setVisibility(View.GONE);
unfavorite.setVisibility(View.VISIBLE);
status.favorited = true;
Toast.makeText(WeiboViewer.this, “收藏成功”, Toast.LENGTH_LONG)
.show();
} else {
status.favorited = false;
favorite.setVisibility(View.VISIBLE);
unfavorite.setVisibility(View.GONE);
Toast.makeText(WeiboViewer.this, “取消收藏成功”,
Toast.LENGTH_LONG).show();
}
}
// 处理刷新
else if (msg.obj instanceof Status) // 刷新
{
refreshButton.setVisibility(View.VISIBLE);
refreshProgressBar.setVisibility(View.GONE);
loadContent();
}
super.handleMessage(msg);
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.weibo_viewer);
profileImage = (ImageView) findViewById(R.id.iv_profile_image);
verified = (ImageView) findViewById(R.id.imageview_verified);
userName = (TextView) findViewById(R.id.tv_name);
statusText = (TextView) findViewById(R.id.tv_text);
statusImage = (ImageView) findViewById(R.id.iv_status_image);
insideContent = findViewById(R.id.ll_inside_content);
retweetdetailText = (TextView) findViewById(R.id.tv_retweetdetail_text);
retweetdetailImage = (ImageView) findViewById(R.id.iv_retweetdetail_image);
source = (TextView) findViewById(R.id.tv_source);
forwardButton = (Button) findViewById(R.id.btn_forward);
commentButton = (Button) findViewById(R.id.btn_comment);
retweetdetailForwardButton = (Button) findViewById(R.id.btn_retweetdetail_forward);
retweetdetailCommentButton = (Button) findViewById(R.id.btn_retweetdetail_comment);
favorite = findViewById(R.id.ll_fav);
unfavorite = findViewById(R.id.ll_unfav);
refreshButton = (Button) findViewById(R.id.btn_refresh);
refreshProgressBar = (ProgressBar) findViewById(R.id.pb_refresh);
findViewById(R.id.ll_forward).setOnClickListener(this);
findViewById(R.id.ll_comment).setOnClickListener(this);
findViewById(R.id.btn_back).setOnClickListener(this);
refreshButton.setOnClickListener(this);
retweetdetailImage.setOnClickListener(this);
statusImage.setOnClickListener(this);
forwardButton.setOnClickListener(this);
commentButton.setOnClickListener(this);
retweetdetailForwardButton.setOnClickListener(this);
retweetdetailCommentButton.setOnClickListener(this);
favorite.setOnClickListener(this);
unfavorite.setOnClickListener(this);
loadContent();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
private void loadContent() {
// 异步加载头像
String imageUrl = WeiboManager.getImageurl(this,
status.user.profile_image_url, this);
if (imageUrl != null) {
profileImage.setImageURI(Uri.fromFile(new File(imageUrl)));
}
userName.setText(status.user.name);
Tools.userVerified(verified, status.user.verified_type);
statusText.setText(Tools.changeTextToFace(this,
Html.fromHtml(Tools.atBlue(status.text))));
source.setText("来自 " + status.getTextSource());
if (status.retweeted_status == null) {
insideContent.setVisibility(View.GONE);
} else {
insideContent.setVisibility(View.VISIBLE);
retweetdetailText.setText(Html.fromHtml(Tools.atBlue(“@”
-
status.retweeted_status.user.name + “:”
-
status.retweeted_status.text)));
retweetdetailForwardButton.setText(String
.valueOf(status.retweeted_status.reposts_count));
retweetdetailCommentButton.setText(String
.valueOf(status.retweeted_status.comments_count));
}
if (WeiboManager.hasPicture(status)) {
if (status.retweeted_status == null) {
statusImage.setVisibility(View.VISIBLE);
retweetdetailImage.setVisibility(View.GONE);
// 异步装载图片
imageUrl = WeiboManager.getImageurl(this, status.bmiddle_pic,
this);
if (imageUrl != null) {
statusImage.setImageURI(Uri.fromFile(new File(imageUrl)));
}
} else {
statusImage.setVisibility(View.GONE);
retweetdetailImage.setVisibility(View.VISIBLE);
// 异步装载图像
imageUrl = WeiboManager.getImageurl(this,
status.retweeted_status.bmiddle_pic, this);
if (imageUrl != null) {
retweetdetailImage.setImageURI(Uri.fromFile(new File(
imageUrl)));
}
}
}
forwardButton.setText(String.valueOf(status.reposts_count));
commentButton.setText(String.valueOf(status.comments_count));
if (status.favorited) {
favorite.setVisibility(View.GONE);
unfavorite.setVisibility(View.VISIBLE);
} else {
favorite.setVisibility(View.VISIBLE);
unfavorite.setVisibility(View.GONE);
}
}
@Override
public void onClick(View v) {
Intent intent = null;
switch (v.getId()) {
case R.id.iv_status_image:
case R.id.iv_retweetdetail_image:
String url = status.original_pic;
if (status.retweeted_status != null) {
url = status.retweeted_status.original_pic;
}
intent = new Intent(this, PictureViewer.class);
intent.putExtra(“file_url”, url);
intent.putExtra(“type”, PICTURE_VIEWER_WEIBO_BROWSER);
startActivity(intent);
break;
case R.id.btn_forward:
case R.id.ll_forward:
forward(); // 转发微博
break;
case R.id.btn_retweetdetail_forward:
retweetdetailForword();
break;
case R.id.ll_comment:
comment();
break;
case R.id.ll_fav:
case R.id.ll_unfav:
favorite(!status.favorited);
break;
case R.id.btn_comment:
intent = new Intent(this, CommentListViewer.class);
intent.putExtra(“status_id”, status.id);
intent.putExtra(“text”, “//@” + status.user.name + “:”
- status.text);
startActivity(intent);
break;
case R.id.btn_retweetdetail_comment:
intent = new Intent(this, CommentListViewer.class);
intent.putExtra(“status_id”, status.retweeted_status.id);
intent.putExtra(“text”, “//@” + status.retweeted_status.user.name
- “:” + status.retweeted_status.text);
startActivity(intent);
break;
case R.id.btn_back:
finish();
break;
case R.id.btn_refresh:
refreshButton.setVisibility(View.GONE);
refreshProgressBar.setVisibility(View.VISIBLE);
WeiboManager.getStatusAsync(this, status.id, this);
break;
default:
break;
}
}
// 收藏微博
private void favorite(boolean fav) {
FavoriteWeiboTask favoriteWeiboTask = new FavoriteWeiboTask();
favoriteWeiboTask.id = status.id;
favoriteWeiboTask.fav = fav;
favoriteWeiboTask.doneAndProcess = this;
Tools.getGlobalObject(this).getWorkQueueStorage()
.addTask(favoriteWeiboTask);
}
// 转发微博
private void forward() {
String text = “//@” + status.user.name + “:” + status.text;
Intent intent = new Intent(this, PostWeibo.class);
intent.putExtra(“type”, TYPE_FORWARD);
【附】相关架构及资料
往期Android高级架构资料、源码、笔记、视频。高级UI、性能优化、架构师课程、NDK、混合式开发(ReactNative+Weex)微信小程序、Flutter全方面的Android进阶实践技术,群内还有技术大牛一起讨论交流解决问题。
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门,即可获取!
dTask(favoriteWeiboTask);
}
// 转发微博
private void forward() {
String text = “//@” + status.user.name + “:” + status.text;
Intent intent = new Intent(this, PostWeibo.class);
intent.putExtra(“type”, TYPE_FORWARD);
【附】相关架构及资料
[外链图片转存中…(img-WEoPf8By-1715327896599)]
[外链图片转存中…(img-F8zo6WXk-1715327896600)]
往期Android高级架构资料、源码、笔记、视频。高级UI、性能优化、架构师课程、NDK、混合式开发(ReactNative+Weex)微信小程序、Flutter全方面的Android进阶实践技术,群内还有技术大牛一起讨论交流解决问题。
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门,即可获取!