package com.bigdo.app;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.bigdo.common.BDSQLiteHelper;
import com.bigdo.common.RConfig;
import com.bigdo.common.RState;
import com.bigdo.controls.IXListViewListener;
import com.bigdo.controls.XListView;
import com.bigdo.util.Base64;
import com.bigdo.util.MD5;
import com.bigdo.util.WSCheck;
import com.bigdo.util.WSHelper;
import com.bigdo.util.WSResult;
import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView.ScaleType;
public class TeacherListActivity extends BaseActivity implements
IXListViewListener {
protected XListView teacher_list;
public TeacherAdapter ad = null;
public ArrayList<HashMap<String, Object>> data = null;
protected TextView no_data;
AsyncImgDown imgDown;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.teacher_list_activity);
View com_title = findViewById(R.id.com_title_host);
Button back = (Button) com_title.findViewById(R.id.sub_com_title_back);
back.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
finish();
}
});
imgDown = new ImgDown(this);
TextView title_msg = (TextView) com_title
.findViewById(R.id.sub_com_title_context);
title_msg.setText("名师讲堂");
teacher_list = (XListView) this.findViewById(R.id.teacher_list);
no_data = (TextView) this.findViewById(R.id.teacher_list_no_data);
teacher_list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
arg2--;
if (arg2 < 0) {
arg2 = 0;
}
goTeacherSingle(arg2);
}
});
teacher_list.setPullLoadEnable(false);
teacher_list.setXListViewListener(this);
data = new ArrayList<HashMap<String, Object>>();
ad = new TeacherAdapter();
teacher_list.setAdapter(ad);
loadData();
}
private void goTeacherSingle(int p) {
if (data != null) {
HashMap<String, Object> mp = data.get(p);
if (mp != null) {
int idteacher = (Integer) mp.get("idteacher");
String teacher = mp.get("teacher") + "";
String taptitudes = mp.get("taptitudes") + "";
String img = mp.get("img") + "";
Intent i = new Intent();
i.putExtra("idteacher", idteacher);
i.putExtra("teacher", teacher);
i.putExtra("taptitudes", taptitudes);
i.putExtra("img", img);
i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
i.setClass(this, TeacherSingleActivity.class);
startActivityForResult(i, 1);
}
}
}
private int getTeacherLocalInfo() {
int idteacher = -1;
BDSQLiteHelper sqlobj = null;
SQLiteDatabase sqlobj_r = null;
Cursor cu = null;
try {
sqlobj = new BDSQLiteHelper(this);
sqlobj_r = sqlobj.getReadableDatabase();
cu = sqlobj_r
.rawQuery(
"select idteacher from teacher order by idteacher desc limit 0,1",
null);
if (cu.moveToFirst()) {
idteacher = cu.getInt(0);
}
Log.w("getTeacherLocalInfo", idteacher + "");
} catch (Exception e) {
idteacher = -1;
}
try {
if (cu != null) {
cu.close();
cu = null;
}
} catch (Exception ex) {
}
try {
if (sqlobj_r != null) {
sqlobj_r.close();
sqlobj_r = null;
}
} catch (Exception ex) {
}
try {
if (sqlobj != null) {
sqlobj.close();
sqlobj = null;
}
} catch (Exception ex) {
}
return idteacher;
}
private void startGetTeacher() {
checkHasData(true);
// int idteacher = getTeacherLocalInfo();
String url = RConfig.teacher(this);
/*
* JSONObject jparam = new JSONObject(); try {
* //jparam.put("_idteacher", idteacher); } catch (Exception e) {
* checkHasData(false); this.showInfo("准备请求参数出错.");
* teacher_list.stopRefresh(); return; } String data =
* jparam.toString();
*/
WSHelper ws = new WSHelper(this, "", false, img_handler, url, "", null,
0, 0, 0, 0, WSHelper.POST);
ws.start();
Log.e("startGetTeacher", url);
// Log.e("startGetTeacher_data", data);
}
public void loadData() {
getTeacher(0, 0);
if (data.size() <= 0) {
teacher_list.startAutoHeightRefresh(1);
}
}
private int getTeacher(int op, int tag) {
int count = 0;
if (data != null && ad != null) {
data.clear();
BDSQLiteHelper sqlobj = null;
SQLiteDatabase sqlobj_r = null;
Cursor cu = null;
try {
sqlobj = new BDSQLiteHelper(this);
sqlobj_r = sqlobj.getReadableDatabase();
String sqlt = "select * from teacher order by teacher asc";
cu = sqlobj_r.rawQuery(sqlt, null);
int idteacher;
String teacher, taptitudes, img;
String fp, img_name;
while (cu.moveToNext()) {
idteacher = cu.getInt(cu.getColumnIndex("idteacher"));
teacher = cu.getString(cu.getColumnIndex("teacher"));
taptitudes = cu.getString(cu.getColumnIndex("taptitudes"));
img = cu.getString(cu.getColumnIndex("img"));
HashMap<String, Object> map = new HashMap<String, Object>();
if (idteacher >= 0 && img != null && !img.equals("")) {
img_name = imgDown.handleImgName(idteacher + img);
map.put("__down__name__", img_name);
final String sd = imgDown.sdDir(99998888);
if (sd == null || sd.equals("")) {
map.put("__error__", "no_exist_for_service");
} else {
fp = sd + img_name + ".r";
File im = new File(fp);
if (im.exists()) {
map.put("__path__", fp);
}
}
}
map.put("idteacher", idteacher);
map.put("teacher", teacher);
map.put("taptitudes", taptitudes);
map.put("img", img);
data.add(map);
count++;
}
} catch (Exception e2) {
messageBox("加载本地缓存老师出错");
}
try {
if (cu != null) {
cu.close();
cu = null;
}
} catch (Exception ex) {
}
try {
if (sqlobj_r != null) {
sqlobj_r.close();
sqlobj_r = null;
}
} catch (Exception ex) {
}
try {
if (sqlobj != null) {
sqlobj.close();
sqlobj = null;
}
} catch (Exception ex) {
}
if (ad != null) {
ad.notifyDataSetChanged();
}
checkHasData(false);
}
return count;
}
private int update(WSResult result) {
if (teacher_list != null && data != null && ad != null) {
boolean addtag = false, deltag = false, uptag = false;
JSONObject jresult = WSCheck.Json(this, result);
if (jresult != null) {
BDSQLiteHelper sqlobj = null;
SQLiteDatabase sqlobj_w = null;
Cursor cu = null;
try {
sqlobj = new BDSQLiteHelper(this);
sqlobj_w = sqlobj.getWritableDatabase();
JSONArray item = jresult.getJSONArray("item");
try {
int clear = jresult.getInt("__clear");
if (clear == 1) {
if (sqlobj_w.delete("teacher", null, null) > 0) {
deltag = true;
data.clear();
// ad.notifyDataSetChanged();
}
}
} catch (Exception ee) {
}
if (item != null) {
int add_count = 0, update_count = 0;
try {
int lg = item.length();
if (lg >= 0) {
String teacher = "", taptitudes = "", img = "";
int idteacher;
long c = 0;
for (int i = 0; i < lg; i++) {
JSONObject vo = (JSONObject) item.opt(i);
if (vo != null) {
idteacher = vo.getInt("idteacher");
teacher = vo.getString("teacher");
taptitudes = vo.getString("taptitudes");
img = vo.getString("img");
ContentValues cv = new ContentValues();
cv.put("idteacher", idteacher);
cv.put("teacher", teacher);
cv.put("taptitudes", taptitudes);
cv.put("img", img);
cu = sqlobj_w
.rawQuery(
"select idteacher from teacher where idteacher = ? ",
new String[] { idteacher
+ "" });
c = cu.getCount();
try {
if (cu != null) {
cu.close();
}
} catch (Exception ex) {
}
cu = null;
if (c <= 0) {
c = sqlobj_w.insert("teacher", "",
cv);
add_count++;
if (c > 0) {
addtag = true;
}
} else {
cv.remove("idteacher");
c = sqlobj_w.update("teacher", cv,
"idteacher = ?",
new String[] { idteacher
+ "" });
update_count += c;
if (c > 0) {
uptag = true;
}
}
cv = null;
}
}
}
} catch (Exception e) {
messageBox("缓存老师数据出错.");
}
String msg = "";
if (add_count > 0) {
msg = "亲!新加载了 " + add_count + " 位老师";
}
if (update_count > 0) {
if (msg.length() > 0) {
msg += ",";
} else {
msg = "亲! ";
}
msg = "更新了 " + update_count + " 位老师.";
}
if (msg.length() > 0) {
this.showInfo(msg);
}
} else {
showInfo("亲!没有新老师了哦.");
}
} catch (Exception e) {
}
try {
if (cu != null) {
cu.close();
cu = null;
}
} catch (Exception ex) {
}
try {
if (sqlobj_w != null) {
sqlobj_w.close();
sqlobj_w = null;
}
} catch (Exception ex) {
}
try {
if (sqlobj != null) {
sqlobj.close();
sqlobj = null;
}
} catch (Exception ex) {
}
}
if (uptag || deltag) {
return 2;
} else if (addtag) {
return 1;
}
}
return 0;
}
public class TeacherAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public TeacherAdapter() {
this.mInflater = LayoutInflater.from(TeacherListActivity.this);
}
@Override
public int getCount() {
if (data != null) {
return data.size();
}
return 0;
}
@Override
public Object getItem(int arg0) {
if (data != null) {
return data.get(arg0);
}
return null;
}
@Override
public long getItemId(int arg0) {
return arg0;
}
String img_path, img, teacher, taptitudes, error, __down__name__;
int idteacher = 0;
boolean isHasImg;
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
Holder holder = null;
if (arg1 == null) {
holder = new Holder();
arg1 = mInflater.inflate(R.layout.teacher_list_item, null);
holder.vline = (View) arg1
.findViewById(R.id.teacher_list_item_vline);
holder.img = (ImageView) arg1
.findViewById(R.id.teacher_list_item_img);
holder.name = (TextView) arg1
.findViewById(R.id.teacher_list_item_name);
holder.desc = (TextView) arg1
.findViewById(R.id.teacher_list_item_desc);
arg1.setTag(holder);
} else {
holder = (Holder) arg1.getTag();
}
if (arg0 == 0 || arg0 == data.size()) {
holder.vline.setVisibility(View.GONE);
} else {
holder.vline.setVisibility(View.VISIBLE);
}
Map<String, Object> mp = data.get(arg0);
idteacher = mp.get("idteacher") != null ? (Integer) mp
.get("idteacher") : 0;
img = mp.get("img") != null ? (mp.get("img") + "") : "";
teacher = mp.get("teacher") != null ? (mp.get("teacher") + "") : "";
taptitudes = mp.get("taptitudes") != null ? (mp.get("taptitudes") + "")
: "";
holder.name.setText(teacher);
holder.desc.setText(taptitudes);
__down__name__ = mp.get("__down__name__") != null ? (mp
.get("__down__name__") + "") : "";
if (!__down__name__.equals("")) {
img_path = mp.get("__path__") != null ? (mp.get("__path__") + "")
: "";
error = mp.get("__error__") != null ? (mp.get("__error__") + "")
: "0";
isHasImg = imgDown.loadImg(holder.img, img_path, error, arg0);
if (!isHasImg) {
AsyncImgDownInfo img_info = new AsyncImgDownInfo();
img_info.cacheName = __down__name__;
img_info.downName = img;
img_info.pos = arg0;
img_info.requestCode = 99998888;
error = imgDown.startGetImg(img_info);
mp.put("__error__", error);
}
}
return arg1;
}
}
private void checkHasData(boolean isoping) {
if (no_data != null) {
if (!isoping && data.size() <= 0) {
no_data.setVisibility(LinearLayout.VISIBLE);
} else {
no_data.setVisibility(LinearLayout.GONE);
}
}
}
private class Holder {
ImageView img;
TextView name, desc;
View vline;
}
@SuppressLint("HandlerLeak")
protected Handler img_handler = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.arg1 == RState.WSCall) {
WSResult result = (WSResult) msg.getData().getParcelable(
WSResult.ResultKey);
if (result.RequestCode == 0) {
if (teacher_list != null) {
int tag = 0;
try {
tag = update(result);
} catch (Exception e) {
}
getTeacher(result.RequestCode2, tag);
teacher_list.stopRefresh();
checkHasData(false);
}
}
}
}
};
@Override
public void onRefresh(XListView xListView, int tag, int requestCode) {
// TODO Auto-generated method stub
startGetTeacher();
}
@Override
public void onLoadMore(XListView xListView, int tag, int requestCode) {
// TODO Auto-generated method stub
}
private class ImgDown extends AsyncImgDown {
public ImgDown(BaseActivity v) {
super(v);
}
@Override
protected void finishGetImg(String path, AsyncImgDownInfo info) {
if (info != null && data != null && data.size() > info.pos
&& ad != null) {
Map<String, Object> mp = data.get(info.pos);
if (path == null || path.equals("")) {
mp.put("__error__", "no_exist_for_service");
ad.notifyDataSetChanged();
return;
}
mp.put("__error__", "");
mp.put("__path__", path);
ad.notifyDataSetChanged();
}
}
@Override
protected String imgDir(int requestCode) {
return RConfig.Teacher_Img_Dir;
}
@Override
protected int imgFailure(int requestCode) {
return R.drawable.menu_me;
}
@Override
protected int imgLoading(int requestCode) {
return R.drawable.menu_me;
}
}
@Override
public void onClearData() {
// TODO Auto-generated method stub
}
@Override
public void onVideoBroadcast(Intent intent) {
// TODO Auto-generated method stub
}
}