package com.bigdo.app;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONObject;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.bigdo.common.BDSQLiteHelper;
import com.bigdo.common.RConfig;
import com.bigdo.controls.XListView;
import com.bigdo.util.WSCheck;
import com.bigdo.util.WSHelper;
import com.bigdo.util.WSResult;
public class MainHomeBll extends MainBaseBll {
public static final int Ws_R_Code = 10000;
RelativeLayout top;
VdieoBll vbll;
SearchVdieoBll sbll;
HashMap<String, Object> topVideo = null;
AsyncImgDown imgDown;
ImageView top_img;
ArrayList<HashMap<String, Object>> goodata = null;
public MainHomeBll(BaseActivity activity, View v, Handler handler) {
super(activity, v, handler);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
vbll = new VdieoBll(this.a);
vbll.onCreate(savedInstanceState);
imgDown = new TopImgDown(this.a);
sbll = new SearchVdieoBll(this.a);
sbll.onCreate(savedInstanceState);
setControl(savedInstanceState);
}
@Override
protected void onDestroy() {
}
public boolean onKeyBack() {
RelativeLayout search_host = (RelativeLayout) host
.findViewById(R.id.search_host);
if (search_host.getVisibility() == RelativeLayout.VISIBLE) {
search_host.setVisibility(RelativeLayout.GONE);
return true;
} else {
return false;
}
}
private void setControl(Bundle savedInstanceState) {
top = (RelativeLayout) LayoutInflater.from(this.a).inflate(
R.layout.main_home_top, null);
vbll.video_list.setHeaderExtensionView(top);
top_img = (ImageView) top.findViewById(R.id.video_first_img);
Button latest = (Button) top.findViewById(R.id.video_type_latest);
latest.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
setLatestEvent();
}
});
Button teacher = (Button) top.findViewById(R.id.video_type_tearcher);
teacher.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
setTeacherEvent();
}
});
Button free = (Button) top.findViewById(R.id.video_type_free);
free.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
setFreeEvent();
}
});
Button hot = (Button) top.findViewById(R.id.video_type_hot);
hot.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
setHotEvent();
}
});
RelativeLayout video_host = (RelativeLayout) top
.findViewById(R.id.video_host);
video_host.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
goVideoDetail();
}
});
View com_title = host.findViewById(R.id.com_title_host);
TextView search_submit = (TextView) com_title
.findViewById(R.id.search_submit);
search_submit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
showSearchHost();
}
});
final Button search_submit_do = (Button) this.host
.findViewById(R.id.search_submit_do);
final EditText search_key = (EditText) this.host
.findViewById(R.id.search_key);
final XListView video_list_s = (XListView) this.host
.findViewById(R.id.video_list_s);
search_submit_do.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if (search_key.getText().toString().length() > 0) {
video_list_s.setVisibility(XListView.VISIBLE);
startSearch();
} else {
showSearchHost();
if (search_key.getText().toString().length() > 0) {
video_list_s.setVisibility(XListView.VISIBLE);
}
}
}
});
search_key.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if (search_key.getText().toString().length() > 0) {
search_submit_do.setText("搜索");
} else {
search_submit_do.setText("取消");
video_list_s.setVisibility(XListView.GONE);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
private void showSearchHost() {
RelativeLayout search_host = (RelativeLayout) host
.findViewById(R.id.search_host);
if (search_host.getVisibility() == RelativeLayout.GONE) {
search_host.setVisibility(RelativeLayout.VISIBLE);
if (goodata == null || goodata.size() <= 0) {
getSearchGood();
startSearchGood();
}
} else {
search_host.setVisibility(RelativeLayout.GONE);
this.a.hiddenKeyboard();
}
}
public void setLatestEvent() {
Intent i = new Intent();
i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
i.setClass(this.a, LatestVideoActivity.class);
this.a.startActivityForResult(i, 12);
}
public void setTeacherEvent() {
Intent i = new Intent();
i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
i.setClass(this.a, TeacherListActivity.class);
this.a.startActivityForResult(i, 13);
}
public void setFreeEvent() {
Intent i = new Intent();
i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
i.setClass(this.a, FreeVideoActivity.class);
this.a.startActivityForResult(i, 14);
}
public void setHotEvent() {
Intent i = new Intent();
i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
i.setClass(this.a, HotVideoActivity.class);
this.a.startActivityForResult(i, 15);
}
@Override
protected boolean onHandler(WSResult result) {
if (result != null) {
if (result.RequestCode2 == 11111) {
updateSearchGood(result);
getSearchGood();
ProgressBar p = (ProgressBar) host
.findViewById(R.id.search_good_progress);
p.setVisibility(ProgressBar.GONE);
}
}
return false;
}
private void getSearchGood() {
if (goodata == null) {
goodata = new ArrayList<HashMap<String, Object>>();
} else {
goodata.clear();
}
BDSQLiteHelper sqlobj = null;
SQLiteDatabase sqlobj_r = null;
Cursor cu = null;
try {
sqlobj = new BDSQLiteHelper(this.a);
sqlobj_r = sqlobj.getReadableDatabase();
String sqlt = "select * from searchgood";
cu = sqlobj_r.rawQuery(sqlt, null);
int idvideo, idtype;
String title;
while (cu.moveToNext()) {
idtype = cu.getInt(cu.getColumnIndex("idtype"));
idvideo = cu.getInt(cu.getColumnIndex("idvideo"));
title = cu.getString(cu.getColumnIndex("title"));
HashMap<String, Object> mp = new HashMap<String, Object>();
mp.put("idtype", idtype);
mp.put("idvideo", idvideo);
mp.put("title", title);
goodata.add(mp);
}
} catch (Exception e2) {
// this.a.ShowInfo("加载本地缓存推荐视频出错");
}
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) {
}
setGood();
}
private void updateSearchGood(WSResult result) {
try {
JSONObject jresult = WSCheck.Json(this.a, result.Result, null);
if (jresult != null) {
BDSQLiteHelper sqlobj = null;
SQLiteDatabase sqlobj_w = null;
try {
sqlobj = new BDSQLiteHelper(this.a);
sqlobj_w = sqlobj.getWritableDatabase();
try {
sqlobj_w.delete("searchgood", null, null);
} catch (Exception e) {
}
JSONArray item = jresult.getJSONArray("item");
if (item != null) {
try {
int lg = item.length();
String title = "", teacher = "", taptitudes = "", timg = "";
String desc = "", img = "", video = "", createtime = "", updatetime = "", paytime = "";
int idvideo, idtype, idptype, score, paycount, idteacher;
for (int i = 0; i < lg; i++) {
JSONObject vo = (JSONObject) item.opt(i);
if (vo != null) {
idvideo = vo.getInt("idvideo");
idtype = vo.getInt("idtype");
idptype = vo.getInt("idptype");
score = vo.getInt("score");
title = vo.getString("title");
idteacher = vo.getInt("idteacher");
teacher = vo.getString("teacher");
//desc = vo.getString("desc");
paycount = vo.getInt("paycount");
paytime = vo.getString("paytime");
img = vo.getString("img");
video = vo.getString("video");
createtime = vo.getString("createtime");
updatetime = vo.getString("updatetime");
taptitudes = vo.getString("taptitudes");
timg = vo.getString("timg");
if (idvideo >= 0 && idtype >= 0) {
ContentValues cv = new ContentValues();
cv.put("idbatch", 0);
cv.put("idvideo", idvideo);
cv.put("idtype", idtype);
cv.put("idptype", idptype);
cv.put("score", score);
cv.put("title", title);
cv.put("idteacher", idteacher);
cv.put("teacher", teacher);
cv.put("taptitudes", taptitudes);
cv.put("timg", timg);
//cv.put("desc", desc);
cv.put("paycount", paycount);
cv.put("paytime", paytime);
cv.put("img", img);
cv.put("video", video);
cv.put("createtime", createtime);
cv.put("updatetime", updatetime);
cv.put("idvideo", idvideo);
cv.put("idtype", idtype);
sqlobj_w.insert("searchgood", "", cv);
}
}
}
} catch (Exception e) {
this.a.messageBox("缓存搜索推荐视频数据出错.");
}
}
} catch (Exception e) {
}
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) {
}
}
} catch (Exception ex) {
}
}
private void setGood() {
LinearLayout good_list = (LinearLayout) host
.findViewById(R.id.search_good_list);
good_list.removeAllViews();
if (goodata != null && goodata.size() > 0) {
int lg = goodata.size();
LayoutInflater mInflater = LayoutInflater.from(this.a);
for (int i = 0; i < lg; i++) {
HashMap<String, Object> mp = goodata.get(i);
String title = mp.get("title") + "";
if (title != null && !title.equals("")) {
View th = mInflater
.inflate(R.layout.search_good_item, null);
TextView t = (TextView) th.findViewById(R.id.good_title);
t.setText(title);
final int p = i;
t.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
goVideoDetailByGood(p);
}
});
good_list.addView(th);
}
}
}
}
private void goVideoDetailByGood(int p) {
try {
if (goodata != null && goodata.size() > 0 && p >= 0) {
HashMap<String, Object> mp = goodata.get(p);
if (mp != null) {
int idvideo = mp.get("idvideo") != null ? (Integer) mp
.get("idvideo") : -1;
int idtype = mp.get("idtype") != null ? (Integer) mp
.get("idtype") : -1;
if (idvideo >= 0 && idtype >= 0) {
Intent i = new Intent();
i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
i.setClass(this.a, Video_Detail_Activity.class);
i.putExtra("idvideo", idvideo);
i.putExtra("idtype", idtype);
i.putExtra("free", false);
i.putExtra("table", "searchgood");
this.a.startActivityForResult(i, 1999);
}
}
}
} catch (Exception e) {
}
}
private void startSearchGood() {
ProgressBar p = (ProgressBar) host
.findViewById(R.id.search_good_progress);
p.setVisibility(ProgressBar.VISIBLE);
String url = RConfig.searchGood(this.a);
WSHelper ws = new WSHelper(this.a, "", false, handler, url, "", null,
Ws_R_Code, 11111, 0, 0, WSHelper.GET);
Log.e("startSearchGood", url);
ws.start();
}
private void startSearch() {
final EditText search_key = (EditText) this.host
.findViewById(R.id.search_key);
String key = search_key.getText().toString();
if (key != null && !"".equals(key.trim())) {
if (goodata != null) {
goodata.clear();
if (sbll.ad != null) {
sbll.ad.notifyDataSetChanged();
}
}
a.showProgress("搜索中...");
sbll.startRefreshVideo();
} else {
search_key.setError("请输入有效的搜索字");
search_key.requestFocus();
}
}
private void goVideoDetail() {
try {
if (topVideo != null && topVideo.size() > 0) {
int idvideo = topVideo.get("idvideo") != null ? (Integer) topVideo
.get("idvideo") : -1;
int idtype = topVideo.get("idtype") != null ? (Integer) topVideo
.get("idtype") : -1;
if (idvideo >= 0 && idtype >= 0) {
Intent i = new Intent();
i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
i.setClass(this.a, Video_Detail_Activity.class);
i.putExtra("idvideo", idvideo);
i.putExtra("idtype", idtype);
i.putExtra("free", false);
i.putExtra("table", "hometop");
this.a.startActivityForResult(i, 1999);
}
}
} catch (Exception e) {
}
}
private void getTop() {
if (topVideo == null) {
topVideo = new HashMap<String, Object>();
} else {
topVideo.clear();
}
boolean loadImg = false;
BDSQLiteHelper sqlobj = null;
SQLiteDatabase sqlobj_r = null;
Cursor cu = null;
try {
sqlobj = new BDSQLiteHelper(this.a);
sqlobj_r = sqlobj.getReadableDatabase();
String sqlt = "select * from hometop limit 0,1";
cu = sqlobj_r.rawQuery(sqlt, null);
int idvideo, idtype;
String img, img_name;
float score;
int paycount;
if (cu.moveToFirst()) {
idtype = cu.getInt(cu.getColumnIndex("idtype"));
idvideo = cu.getInt(cu.getColumnIndex("idvideo"));
score = cu.getFloat(cu.getColumnIndex("score"));
paycount = cu.getInt(cu.getColumnIndex("paycount"));
img = cu.getString(cu.getColumnIndex("img"));
if (idvideo >= 0 && idtype >= 0 && img != null
&& !img.equals("")) {
img_name = imgDown.handleImgName(idtype + idvideo + img);
String sd = imgDown.sdDir(0);
String img_path = sd + img_name + ".r";
String error = "";
loadImg = true;
boolean isHasImg = imgDown.loadImg(top_img, img_path,
error, 0);
if (!isHasImg) {
AsyncImgDownInfo img_info = new AsyncImgDownInfo();
img_info.cacheName = img_name;
img_info.downName = img;
img_info.pos = 0;
img_info.param = "l";
img_info.requestCode = 0;
error = imgDown.startGetImg(img_info);
topVideo.put("__error__", error);
}
}
topVideo.put("idvideo", idvideo);
topVideo.put("idtype", idtype);
topVideo.put("score", score);
topVideo.put("paycount", paycount);
topVideo.put("img", img);
}
} catch (Exception e2) {
this.a.showInfo("加载本地缓存推荐视频出错");
}
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 (!loadImg) {
imgDown.loadImg(top_img, "", "", 0);
}
}
private void updateTop(WSResult result) {
try {
StringBuilder sb = new StringBuilder();
JSONObject jresult = WSCheck.Json(this.a, result.Result, sb);
if (jresult != null) {
BDSQLiteHelper sqlobj = null;
SQLiteDatabase sqlobj_w = null;
try {
sqlobj = new BDSQLiteHelper(this.a);
sqlobj_w = sqlobj.getWritableDatabase();
try {
sqlobj_w.delete("hometop", null, null);
} catch (Exception e) {
}
JSONObject top = jresult.getJSONObject("top");
if (top != null) {
try {
String title = "", idteacher = "", teacher = "", taptitudes = "", timg = "";
String desc = "", img = "", video = "", createtime = "", updatetime = "", paytime = "";
int idvideo, idtype, idptype, score, paycount;
if (top.has("idvideo") && top.has("idptype")) {
idvideo = top.getInt("idvideo");
idtype = top.getInt("idtype");
idptype = top.getInt("idptype");
score = top.getInt("score");
title = top.getString("title");
idteacher = top.getString("idteacher");
teacher = top.getString("teacher");
// desc = top.getString("desc");
paycount = top.getInt("paycount");
paytime = top.getString("paytime");
img = top.getString("img");
video = top.getString("video");
createtime = top.getString("createtime");
updatetime = top.getString("updatetime");
taptitudes = top.getString("taptitudes");
timg = top.getString("timg");
if (idvideo != 0 && idtype != 0) {
ContentValues cv = new ContentValues();
cv.put("idbatch", 0);
cv.put("idvideo", idvideo);
cv.put("idtype", idtype);
cv.put("idptype", idptype);
cv.put("score", score);
cv.put("title", title);
cv.put("idteacher", idteacher);
cv.put("teacher", teacher);
cv.put("taptitudes", taptitudes);
cv.put("timg", timg);
// cv.put("desc", desc);
cv.put("paycount", paycount);
cv.put("paytime", paytime);
cv.put("img", img);
cv.put("video", video);
cv.put("createtime", createtime);
cv.put("updatetime", updatetime);
sqlobj_w.insert("hometop", "", cv);
}
}
} catch (Exception e) {
this.a.messageBox("缓存推荐视频数据出错.");
}
}
} catch (Exception e) {
}
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) {
}
}
} catch (Exception ex) {
}
}
public class VdieoBll extends SingleImgVideo {
public VdieoBll(BaseActivity v) {
super(v);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setControl(savedInstanceState);
}
@Override
protected String orderBy(int op) {
return "paycount desc,idvideo desc";
}
@Override
protected String table() {
return "hometoplist";
}
@Override
protected String service() {
return RConfig.getHomeVideo(MainHomeBll.this.a);
}
@Override
protected boolean free() {
// TODO Auto-generated method stub
return false;
}
@Override
protected JSONObject param(int op) {
return null;
}
@Override
protected void wsCall(WSResult result) {
updateTop(result);
getTop();
}
@Override
protected int count() {
return 50;
}
@Override
protected boolean loadMore() {
// TODO Auto-generated method stub
return false;
}
@Override
protected String updateWhere(int op) {
// TODO Auto-generated method stub
return null;
}
@Override
protected String selectWhere(int op) {
// TODO Auto-generated method stub
return null;
}
@Override
protected String[] updateField(int op) {
// TODO Auto-generated method stub
return null;
}
@Override
protected String[] updateVal(int op) {
// TODO Auto-generated method stub
return null;
}
@Override
protected String[] selectField(int op) {
// TODO Auto-generated method stub
return null;
}
@Override
protected String[] selectVal(int op) {
// TODO Auto-generated method stub
return null;
}
@Override
protected boolean batch(int op) {
// TODO Auto-generated method stub
return false;
}
@Override
protected XListView videoList() {
return (XListView) host.findViewById(R.id.video_list);
}
@Override
protected TextView noData() {
// TODO Auto-generated method stub
return (TextView) host.findViewById(R.id.video_list_no_data);
}
@Override
protected boolean isCache() {
// TODO Auto-generated method stub
return true;
}
@Override
protected boolean refresh() {
// TODO Auto-generated method stub
return true;
}
@Override
protected boolean onGoVideoDetail(int pos) {
// TODO Auto-generated method stub
return false;
}
@Override
protected ExtensionColumn[] extensionColumn() {
// TODO Auto-generated method stub
return null;
}
}
public class SearchVdieoBll extends SingleImgVideo {
public SearchVdieoBll(BaseActivity v) {
super(v);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setControl(savedInstanceState);
}
@Override
protected String orderBy(int op) {
return "idvideo desc";
}
@Override
protected String table() {
return "search";
}
@Override
protected String service() {
return RConfig.searchVideo(MainHomeBll.this.a);
}
@Override
protected boolean free() {
// TODO Auto-generated method stub
return false;
}
@Override
protected JSONObject param(int op) {
final EditText search_key = (EditText) host
.findViewById(R.id.search_key);
String key = search_key.getText().toString();
JSONObject jresult = new JSONObject();
try {
jresult.put("key", key);
} catch (Exception e) {
}
return jresult;
}
@Override
protected void wsCall(WSResult result) {
}
@Override
protected int count() {
return 0;
}
@Override
protected boolean loadMore() {
// TODO Auto-generated method stub
return false;
}
@Override
protected String updateWhere(int op) {
// TODO Auto-generated method stub
return null;
}
@Override
protected String selectWhere(int op) {
// TODO Auto-generated method stub
return null;
}
@Override
protected String[] updateField(int op) {
// TODO Auto-generated method stub
return null;
}
@Override
protected String[] updateVal(int op) {
// TODO Auto-generated method stub
return null;
}
@Override
protected String[] selectField(int op) {
// TODO Auto-generated method stub
return null;
}
@Override
protected String[] selectVal(int op) {
// TODO Auto-generated method stub
return null;
}
@Override
protected boolean batch(int op) {
// TODO Auto-generated method stub
return false;
}
@Override
protected XListView videoList() {
return (XListView) host.findViewById(R.id.video_list_s);
}
@Override
protected TextView noData() {
// TODO Auto-generated method stub
return (TextView) host.findViewById(R.id.video_list_s_no_data);
}
@Override
protected boolean isCache() {
// TODO Auto-generated method stub
return false;
}
@Override
protected boolean refresh() {
// TODO Auto-generated method stub
return false;
}
@Override
protected boolean onGoVideoDetail(int pos) {
// TODO Auto-generated method stub
return false;
}
@Override
protected ExtensionColumn[] extensionColumn() {
// TODO Auto-generated method stub
return null;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
}
@Override
public void onShow() {
getTop();
vbll.loadData();
}
private class TopImgDown extends AsyncImgDown {
public TopImgDown(BaseActivity v) {
super(v);
// TODO Auto-generated constructor stub
}
@Override
protected void finishGetImg(String path, AsyncImgDownInfo info) {
// TODO Auto-generated method stub
imgDown.loadImg(top_img, path, "", info.requestCode);
}
@Override
protected String imgDir(int requestCode) {
return RConfig.Big_Img_Dir;
}
@Override
protected int imgFailure(int requestCode) {
return 0;
}
@Override
protected int imgLoading(int requestCode) {
return 0;
}
}
@Override
public void onClearData() {
// TODO Auto-generated method stub
if (vbll != null) {
if (vbll.data != null && vbll.ad != null) {
vbll.data.clear();
vbll.ad.notifyDataSetChanged();
}
}
if (sbll != null) {
if (sbll.data != null && sbll.ad != null) {
sbll.data.clear();
sbll.ad.notifyDataSetChanged();
}
}
if (goodata != null) {
goodata.clear();
}
setGood();
if (topVideo != null) {
topVideo.clear();
}
if (imgDown != null) {
imgDown.loadImg(top_img, "", "", 0);
}
}
}