package com.bigdo.app;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;
import android.annotation.SuppressLint;
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.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.TextView;
import com.bigdo.common.BDSQLiteHelper;
import com.bigdo.common.RConfig;
import com.bigdo.controls.IXExpandableListView;
import com.bigdo.controls.XExpandableListView;
import com.bigdo.controls.XListView;
import com.bigdo.controls.XSlidingMenu;
import com.bigdo.util.DensityUtil;
import com.bigdo.util.WSCheck;
import com.bigdo.util.WSHelper;
import com.bigdo.util.WSResult;
public class MainBoxBll extends MainBaseBll implements IXExpandableListView {
public static final int Ws_R_Code = 20000;
VdieoBll vbll;
int idtype = 0, idptype = 0;
String typeName = "";
ArrayList<HashMap<String, Object>> data;
XSlidingMenu MainViewBox;
View conterView;
VideoTypeAdapter ad;
XExpandableListView videoType;
public MainBoxBll(BaseActivity activity, View v, Handler handler) {
super(activity, v, handler);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
init();
vbll = new VdieoBll(this.a);
vbll.onCreate(savedInstanceState);
}
@SuppressLint("InflateParams")
private void init() {
MainViewBox = (XSlidingMenu) this.host.findViewById(R.id.MainViewBox);
conterView = this.a.getLayoutInflater().inflate(
R.layout.main_box_conter, null);
View rightView = this.a.getLayoutInflater().inflate(
R.layout.main_box_right, null);
MainViewBox.setrightview(rightView, DensityUtil.dip2px(this.a, 60));
MainViewBox.setCenterView(conterView);
View conter_title = conterView.findViewById(R.id.com_title_host);
Button video_type_more = (Button) conter_title
.findViewById(R.id.sub_com_title_right);
video_type_more.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
MainViewBox.showRight();
}
});
View right_title = rightView.findViewById(R.id.com_title_host);
TextView r_title = (TextView) right_title
.findViewById(R.id.ra_title_name);
r_title.setText("视频分类");
data = new ArrayList<HashMap<String, Object>>();
ad = new VideoTypeAdapter();
videoType = (XExpandableListView) rightView
.findViewById(R.id.video_type_list);
videoType.setPullLoadEnable(false);
videoType.setXListViewListener(this);
videoType.setAdapter(ad);
videoType.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView arg0, View arg1,
int arg2, long arg3) {
groupClick(arg2);
return false;
}
});
videoType.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView arg0, View arg1,
int arg2, int arg3, long arg4) {
childClick(arg2, arg3);
return false;
}
});
setTitle();
}
private void groupClick(int p) {
if (!vbll.video_list.isRefreshing() && !vbll.video_list.isPullLoading()) {
if (data != null && data.size() > 0) {
HashMap<String, Object> mp = data.get(p);
if (mp != null) {
int _idtype = (Integer) mp.get("idtype");
if (_idtype != this.idtype) {
MainViewBox.resetCenter();
this.idtype = (Integer) mp.get("idtype");
this.idptype = (Integer) mp.get("idptype");
this.typeName = mp.get("typename") + "";
setTitle();
setTypeDefault(this.idtype);
vbll.data.clear();
vbll.ad.notifyDataSetChanged();
vbll.loadData();
ad.notifyDataSetChanged();
}
}
}
} else {
this.a.showInfo("正在获取数据中,请稍候重试");
}
}
private void childClick(int p, int c) {
if (!vbll.video_list.isRefreshing() && !vbll.video_list.isPullLoading()) {
if (data != null && data.size() > 0) {
HashMap<String, Object> mp = data.get(p);
if (mp != null) {
@SuppressWarnings("unchecked")
ArrayList<HashMap<String, Object>> tc = (ArrayList<HashMap<String, Object>>) mp
.get("type");
if (tc != null) {
HashMap<String, Object> mc = tc.get(c);
if (mc != null) {
int _idtype = (Integer) mc.get("idtype");
if (_idtype != this.idtype) {
MainViewBox.resetCenter();
this.idtype = _idtype;
this.idptype = (Integer) mc.get("idptype");
this.typeName = mc.get("typename") + "";
setTitle();
setTypeDefault(this.idtype);
vbll.data.clear();
vbll.ad.notifyDataSetChanged();
vbll.loadData();
ad.notifyDataSetChanged();
}
}
}
}
}
} else {
this.a.showInfo("正在获取数据中,请稍候重试");
}
}
private void setTypeDefault(int idtype) {
BDSQLiteHelper sqlobj = null;
SQLiteDatabase sqlobj_w = null;
try {
sqlobj = new BDSQLiteHelper(this.a);
sqlobj_w = sqlobj.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("isdefault", 0);
sqlobj_w.update("videotype", cv, "", null);
cv.put("isdefault", 1);
sqlobj_w.update("videotype", cv, "idtype = ? ",
new String[] { idtype + "" });
} 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) {
}
}
private void setTitle() {
View conter_title = conterView.findViewById(R.id.com_title_host);
TextView c_title = (TextView) conter_title
.findViewById(R.id.sub_com_title_context);
TextView c_title_1 = (TextView) conter_title
.findViewById(R.id.sub_com_title_context_1);
String t1 = typeName, t2 = "";
if (idptype != 0) {
if (data != null && data.size() > 0) {
for (int i = 0; i < data.size(); i++) {
HashMap<String, Object> mp = data.get(i);
if ((Integer) mp.get("idtype") == idptype) {
t2 = mp.get("typename") + "";
break;
}
}
}
t1 = t2;
t2 = typeName;
}
if (t1 == null || t1.equals("")) {
c_title.setText("全部视频...");
} else {
c_title.setText(t1);
}
if (t2 == null || t2.equals("")) {
c_title_1.setText("");
c_title_1.setVisibility(TextView.GONE);
} else {
c_title_1.setText(t2);
c_title_1.setVisibility(TextView.VISIBLE);
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
}
@Override
protected boolean onHandler(WSResult result) {
try {
JSONObject jresult = WSCheck.Json(this.a, result);
doUpdateVideoType(jresult, false);
} catch (Exception e) {
}
videoType.stopRefresh();
return false;
}
public class VdieoBll extends SingleImgVideo {
public VdieoBll(BaseActivity v) {
super(v);
// TODO Auto-generated constructor stub
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
protected String orderBy(int op) {
// TODO Auto-generated method stub
return "idvideo desc";
}
@Override
protected String table() {
// TODO Auto-generated method stub
return "box";
}
@Override
protected String service() {
// TODO Auto-generated method stub
return RConfig.getBoxVideo(MainBoxBll.this.a);
}
@Override
protected boolean free() {
// TODO Auto-generated method stub
return false;
}
@Override
protected JSONObject param(int op) {
// TODO Auto-generated method stub
JSONObject jresult = new JSONObject();
try {
jresult.put("idtype", idtype);
} catch (Exception e) {
}
return jresult;
}
@Override
protected void wsCall(WSResult result) {
// TODO Auto-generated method stub
updateVideoType(result);
}
@Override
protected int count() {
// TODO Auto-generated method stub
return 20;
}
@Override
protected boolean loadMore() {
// TODO Auto-generated method stub
return true;
}
@Override
protected String updateWhere(int op) {
// TODO Auto-generated method stub
return null;
}
@Override
protected String selectWhere(int op) {
// TODO Auto-generated method stub
String w = "";
if (idtype > 0) {
w = "(idtype = ? or idptype = ?)";
if (data.size() > 0) {
if (op == op_refresh) {
w += " and idvideo > ?";
} else if (op == op_more) {
w += " and idvideo < ?";
}
}
} else {
if (data.size() > 0) {
if (op == op_refresh) {
w += " idvideo > ?";
} else if (op == op_more) {
w += " idvideo < ?";
}
}
}
return w;
}
@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
if (idtype > 0) {
if (data.size() > 0 && (op == op_refresh || op == op_more)) {
return new String[] { "idtype", "idptype", "idvideo" };
}
return new String[] { "idtype", "idptype" };
} else {
if (data.size() > 0 && (op == op_refresh || op == op_more)) {
return new String[] { "idvideo" };
}
}
return null;
}
@Override
protected String[] selectVal(int op) {
// TODO Auto-generated method stub
if (idtype > 0) {
if (data.size() > 0) {
if (op == op_refresh) {
return new String[] { idtype + "", idtype + "",
data.get(0).get("idvideo") + "" };
} else if (op == op_more) {
return new String[] { idtype + "", idtype + "",
data.get(data.size() - 1).get("idvideo") + "" };
}
}
return new String[] { idtype + "", idtype + "" };
} else {
if (data.size() > 0) {
if (op == op_refresh) {
return new String[] { data.get(0).get("idvideo") + "" };
} else if (op == op_more) {
return new String[] { data.get(data.size() - 1).get(
"idvideo")
+ "" };
}
}
}
return null;
}
@Override
protected boolean batch(int op) {
// TODO Auto-generated method stub
return true;
}
@Override
protected XListView videoList() {
// TODO Auto-generated method stub
return (XListView) conterView.findViewById(R.id.video_list);
}
@Override
protected TextView noData() {
// TODO Auto-generated method stub
return (TextView) conterView.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;
}
}
private void startGetVideoType() {
String u = RConfig.getBoxVideoType(this.a);
new WSHelper(this.a, "", false, this.handler, u, null, null, Ws_R_Code,
10000, 0, 0, WSHelper.POST).start();
Log.e("startGetVideoType", u);
}
@SuppressWarnings("unchecked")
private void getVideoType() {
if (data != null && ad != null) {
if (this.idtype > 0 && vbll != null && vbll.data != null
&& vbll.data.size() > 0) {
HashMap<String, Object> mp = vbll.data.get(0);
if (mp != null) {
int idtype = (Integer) mp.get("idtype");
if (idtype != this.idtype) {
vbll.data.clear();
vbll.loadData();
}
}
}
data.clear();
BDSQLiteHelper sqlobj = null;
SQLiteDatabase sqlobj_r = null;
Cursor cu = null;
int isdefault = 0, c = 0;
this.idtype = 0;
this.idptype = 0;
this.typeName = "";
try {
sqlobj = new BDSQLiteHelper(this.a);
sqlobj_r = sqlobj.getReadableDatabase();
String sqlt = "select * from videotype order by idptype asc, idtype asc";
cu = sqlobj_r.rawQuery(sqlt, null);
int idtype, idptype, _idtype;
String typename;
HashMap<String, Object> mpp = null;
ArrayList<HashMap<String, Object>> mapp = null;
while (cu.moveToNext()) {
idtype = cu.getInt(cu.getColumnIndex("idtype"));
idptype = cu.getInt(cu.getColumnIndex("idptype"));
isdefault = cu.getInt(cu.getColumnIndex("isdefault"));
typename = cu.getString(cu.getColumnIndex("typename"));
if (isdefault == 1) {
this.idtype = idtype;
this.idptype = idptype;
this.typeName = typename;
}
HashMap<String, Object> mp = new HashMap<String, Object>();
mp.put("idtype", idtype);
mp.put("isdefault", isdefault);
mp.put("idptype", idptype);
mp.put("typename", typename);
if (idptype == 0) {
mp.put("_size", 0);
mp.put("type", new ArrayList<HashMap<String, Object>>());
data.add(mp);
c++;
} else {
if (mpp != null) {
_idtype = (Integer) mpp.get("idtype");
if (_idtype == this.idptype) {
mapp = (ArrayList<HashMap<String, Object>>) mpp
.get("type");
if (mapp == null) {
mapp = new ArrayList<HashMap<String, Object>>();
mpp.put("type", mapp);
}
mapp.add(mp);
mpp.put("_size",
((Integer) mpp.get("_size")) + 1);
continue;
}
}
for (int i = 0; i < data.size(); i++) {
mpp = data.get(i);
_idtype = (Integer) mpp.get("idtype");
if (_idtype == idptype) {
mapp = (ArrayList<HashMap<String, Object>>) mpp
.get("type");
if (mapp == null) {
mapp = new ArrayList<HashMap<String, Object>>();
mpp.put("type", mapp);
}
mapp.add(mp);
mpp.put("_size",
((Integer) mpp.get("_size")) + 1);
break;
}
mpp = null;
_idtype = 0;
}
}
}
} catch (Exception e2) {
this.a.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 (c > 0) {
HashMap<String, Object> mp = new HashMap<String, Object>();
mp.put("idtype", 0);
if (this.idtype == 0) {
mp.put("isdefault", 1);
} else {
mp.put("isdefault", 0);
}
mp.put("idptype", 0);
mp.put("typename", "全部视频");
mp.put("_size", 0);
mp.put("type", null);
data.add(0, mp);
}
if (ad != null) {
ad.notifyDataSetChanged();
}
int lg = videoType.getCount();
for (int i = 0; i < lg; i++) {
videoType.expandGroup(i);
}
setTitle();
}
}
private void doUpdateVideoType(final JSONObject json, boolean fromGetVideo) {
int c = 0;
JSONObject jresult = null;
if (fromGetVideo) {
jresult = WSCheck.Json(this.a, json, null);
} else {
jresult = json;
}
if (jresult != null) {
BDSQLiteHelper sqlobj = null;
SQLiteDatabase sqlobj_w = null;
try {
sqlobj = new BDSQLiteHelper(this.a);
sqlobj_w = sqlobj.getWritableDatabase();
JSONArray item = jresult.getJSONArray("item");
if (item != null && item.length() > 0) {
int d_idtype = 0;
try {
Cursor cu = sqlobj_w
.rawQuery(
"select idtype from videotype where isdefault = 1 limit 0,1",
null);
if (cu.moveToFirst()) {
d_idtype = cu.getInt(cu.getColumnIndex("idtype"));
}
try {
cu.close();
cu = null;
} catch (Exception e) {
}
try {
sqlobj_w.delete("videotype", null, null);
} catch (Exception ee) {
}
int lg = item.length();
String typename = "";
int idtype, idptype, isdefault = 0;
for (int i = 0; i < lg; i++) {
JSONObject vo = (JSONObject) item.opt(i);
idtype = vo.getInt("idtype");
idptype = vo.getInt("idptype");
typename = vo.getString("typename");
isdefault = vo.getInt("isdefault");
if (isdefault == 1 || d_idtype == idtype) {
isdefault = 1;
this.idtype = idtype;
this.idptype = idptype;
this.typeName = typename;
}
ContentValues cv = new ContentValues();
cv.put("idtype", idtype);
cv.put("idptype", idptype);
cv.put("typename", typename);
cv.put("isdefault", isdefault);
sqlobj_w.insert("videotype", "", cv);
c++;
}
} 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) {
}
if (!fromGetVideo) {
if (c > 0) {
this.a.showInfo("刷新了 " + c + " 条视频分类.");
} else {
this.a.showInfo("没有新视频类别了哦.");
}
}
if (c > 0) {
getVideoType();
}
}
}
private void updateVideoType(WSResult result) {
JSONObject json = null;
if (result.Result != null && !result.Result.equals("")) {
try {
JSONTokener JSONTokener = new JSONTokener(result.Result);
JSONObject jresult = new JSONObject(JSONTokener);
json = jresult.getJSONObject("titem");
} catch (Exception e) {
}
}
if (json != null) {
doUpdateVideoType(json, true);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
}
private class VideoTypeAdapter extends BaseExpandableListAdapter {
private int _size = 0;
@Override
public Object getChild(int arg0, int arg1) {
HashMap<String, Object> tobj = data.get(arg0);
if (tobj != null) {
@SuppressWarnings("unchecked")
ArrayList<HashMap<String, Object>> tc = (ArrayList<HashMap<String, Object>>) tobj
.get("type");
if (tc != null) {
return tc.get(arg1);
}
}
return null;
}
@Override
public long getChildId(int arg0, int arg1) {
// TODO Auto-generated method stub
return arg1;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
View itemView;
if (convertView == null) {
itemView = LayoutInflater.from(MainBoxBll.this.a).inflate(
R.layout.video_type_child, null);
convertView = itemView;
} else {
itemView = convertView;
}
View vline = (View) itemView
.findViewById(R.id.video_type_child_vline);
vline.setVisibility(View.VISIBLE);
TextView typeName = (TextView) itemView.findViewById(R.id.typeName);
typeName.setText("");
typeName.setTextColor(0xff222222);
HashMap<String, Object> tobj = data.get(groupPosition);
if (tobj != null) {
@SuppressWarnings("unchecked")
ArrayList<HashMap<String, Object>> tc = (ArrayList<HashMap<String, Object>>) tobj
.get("type");
if (childPosition <= 0 || tc == null
|| childPosition == (tc.size())) {
vline.setVisibility(View.GONE);
}
if (tc != null) {
Map<String, Object> cmp = tc.get(childPosition);
int _idtype = (Integer) cmp.get("idtype");
if (idtype == _idtype) {
typeName.setTextColor(0xffee0000);
}
typeName.setText(cmp.get("typename") + "");
}
}
return itemView;
}
@Override
public int getChildrenCount(int groupPosition) {
if (data.size() > groupPosition) {
HashMap<String, Object> tobj = data.get(groupPosition);
if (tobj != null) {
return (Integer) tobj.get("_size");
}
}
return 0;
}
@Override
public Object getGroup(int groupPosition) {
HashMap<String, Object> tobj = data.get(groupPosition);
return tobj;
}
@Override
public int getGroupCount() {
return data.size();
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
View itemView;
if (convertView == null) {
itemView = LayoutInflater.from(MainBoxBll.this.a).inflate(
R.layout.video_type_parent, null);
convertView = itemView;
} else {
itemView = convertView;
}
View vline = (View) itemView
.findViewById(R.id.video_type_parent_vline);
if (groupPosition <= 0 || data == null
|| groupPosition == (data.size())) {
vline.setVisibility(View.GONE);
} else {
vline.setVisibility(View.VISIBLE);
}
TextView typeName = (TextView) itemView.findViewById(R.id.typeName);
TextView childCont = (TextView) itemView
.findViewById(R.id.childCont);
typeName.setText("");
typeName.setTextColor(0xff222222);
childCont.setVisibility(TextView.GONE);
HashMap<String, Object> tdobj = data.get(groupPosition);
if (tdobj != null) {
_size = (Integer) tdobj.get("_size");
if (_size > 0) {
childCont.setVisibility(TextView.VISIBLE);
}
int _idtype = (Integer) tdobj.get("idtype");
if (idtype == _idtype) {
typeName.setTextColor(0xffee0000);
}
childCont.setText(tdobj.get("_size") + "");
typeName.setText(tdobj.get("typename") + "");
}
return itemView;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
}
@Override
public void onRefresh(XExpandableListView xExpandableListView, int tag,
int requestCode) {
// TODO Auto-generated method stub
startGetVideoType();
}
@Override
public void onLoadMore(XExpandableListView xExpandableListView, int tag,
int requestCode) {
// TODO Auto-generated method stub
}
@Override
public void onShow() {
getVideoType();
vbll.loadData();
}
@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 (data != null && ad != null) {
data.clear();
ad.notifyDataSetChanged();
}
idtype = 0;
idptype = 0;
typeName = "";
setTitle();
}
}