package com.s3hengwu.smile;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class ImageActivity extends Activity {
private GridView mGrid;
private TextView title;
private Button back;
// private String url="http://www.laifu.org";
private String pageUrl_1;
private String name;
private int page = 1;
private String flag;
private ImageAdapter mAdapter;
private ProgressDialog mDialog;
private ArrayList<String> mImageUrl = new ArrayList<String>();// 缩略图
// private ArrayList<String> mImageUrl2=new ArrayList<String>();//全图
private ArrayList<String> mImageName = new ArrayList<String>();// 缩略名
private ArrayList<String> mImageName2 = new ArrayList<String>();// 全名
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_iamge);
LinearLayout ll = (LinearLayout) findViewById(R.id.ll_title_bottom);
mGrid = (GridView) findViewById(R.id.gridview);
title = (TextView) findViewById(R.id.title);
back = (Button) findViewById(R.id.btnback);
mGrid.setOnItemClickListener(listener);
mAdapter = new ImageAdapter();
mDialog = new ProgressDialog(this);
mDialog.setMessage("正在加载数据...");
Intent it = getIntent();
name = it.getStringExtra("title");
title.setText(name);
if (name.equals("动物漫画")) {
pageUrl_1 = "http://www.laifu.org/tupian/dongwutupian.htm";
flag = "dongwutupian";
} else {
pageUrl_1 = "http://www.laifu.org/tupian/ertongtupian.htm";
flag = "ertongtupian";
}
mDialog.show();
new Thread() {
public void run() {
try {
Document doc = Jsoup.connect(pageUrl_1).get();
Elements es = doc.select(".picListTable img");
for (int i = 0; i < es.size(); i++) {
String img_url = es.get(i).attr("src");
String name = es.get(i).attr("alt");
mImageName2.add(name);
if (name.length() > 8) {
name = name.subSequence(0, 8).toString();
}
mImageName.add(name);
mImageUrl.add(img_url);
Message m = new Message();
m.what = 1;
myHandler.sendMessage(m);
}
} catch (IOException e) {
Message m = new Message();
m.what = 2;
myHandler.sendMessage(m);
}
};
}.start();
back.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ImageActivity.this.finish();
}
});
}
private OnItemClickListener listener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
int count = mAdapter.getCount();
if (arg2 == count - 1) {
page++;
final String nextPage = "http://www.laifu.org/tupian/" + flag
+ "_" + page + ".htm";
mDialog.show();
new Thread() {
public void run() {
try {
Document doc = Jsoup.connect(nextPage).get();
Elements es = doc.select(".picListTable img");
for (int i = 0; i < es.size(); i++) {
String img_url = es.get(i).attr("src");
String name = es.get(i).attr("alt");
mImageName2.add(name);
if (name.length() > 8) {
name = name.subSequence(0, 8).toString();
}
mImageName.add(name);
mImageUrl.add(img_url);
Message m = new Message();
m.what = 3;
myHandler.sendMessage(m);
}
} catch (IOException e) {
Message m = new Message();
m.what = 2;
myHandler.sendMessage(m);
}
};
}.start();
return;
}
Intent it = new Intent(ImageActivity.this, ImageReadActivity.class);
it.putExtra("name", name);
it.putExtra("aticle_name", mImageName2.get(arg2));
it.putExtra("aticle_url", mImageUrl.get(arg2));
startActivity(it);
}
};
private Handler myHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 1:
mDialog.cancel();
mGrid.setAdapter(mAdapter);
break;
case 2:
mDialog.cancel();
final AlertDialog.Builder builder = new AlertDialog.Builder(
ImageActivity.this);
builder.setTitle("提示")
.setMessage("晚上8:30-10:00数据更新中")
.setPositiveButton("我知道了",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
android.os.Process
.killProcess(android.os.Process
.myPid());
System.exit(0);
}
});
builder.create().show();
break;
case 3:
mDialog.cancel();
mAdapter.notifyDataSetChanged();
break;
}
};
};
private class ImageAdapter extends BaseAdapter {
@Override
public int getCount() {
return mImageName.size() + 1;
}
@Override
public Object getItem(int arg0) {
return arg0;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
// if(convertView==null){
convertView = View.inflate(getApplicationContext(), R.layout.item2,
null);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.text);
holder.image = (ImageView) convertView.findViewById(R.id.image);
convertView.setTag(holder);
// }else{
// holder = (ViewHolder) convertView.getTag();
// }
if (position == mImageName.size()) {
holder.text.setText("");
holder.image.setImageResource(R.drawable.add_section);
return convertView;
}
holder.text.setText(mImageName.get(position));
ImageAsyncTask task = new ImageAsyncTask(ImageActivity.this,
holder.image);
task.execute(mImageUrl.get(position));
// holder.image.setImageResource(R.drawable.main_pic_0);
return convertView;
}
class ViewHolder {
TextView text;
ImageView image;
}
};
private class ImageAsyncTask extends AsyncTask<String, Integer, Bitmap> {
private ImageView iv = null;
public ImageAsyncTask(Context context, ImageView iv) {
this.iv = iv;
}
@Override
protected Bitmap doInBackground(String... params) {
String s = params[0];
URL url = null;
HttpURLConnection conn = null;
InputStream is = null;
try {
url = new URL(s);
conn = (HttpURLConnection) url.openConnection();
is = conn.getInputStream();
return BitmapFactory.decodeStream(is);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
}
if (conn != null) {
conn.disconnect();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return nul