package com.douwen.kys_36.bmob_im.Util;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;
import com.douwen.kys_36.bmob_im.JavaBean.ActiveBean;
import com.douwen.kys_36.bmob_im.JavaBean.PersonBean;
import com.douwen.kys_36.bmob_im.JavaBean.SociationBean;
import com.douwen.kys_36.bmob_im.code;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;
import java.io.File;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobObject;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;
import cn.bmob.v3.listener.UploadFileListener;
/**
* Created by kys-36 on 2016/10/4.
*/
public class BmobUtil {
/*需要返回值时调用的Handler*/
public static Handler bmobHandler;
/*登录返回 boolean*/
public static final int BMOB_LOGIN_OK = 1;
/*注册返回 boolean*/
public static final int BMOB_REGIST_OK = 0;
/*查询返回 List<JavaBean>*/
public static final int BMOB_QUERY_OK = 2;
/*上传头像返回 boolean*/
public static final int BMOB_ICON_OK = 3;
/*修改返回 boolean*/
public static final int BMOB_EDIT_OK = 4;
/*增加JAVABEAN返回*/
public static final int BMOB_SAVE_OK = 5;
/*当前环境*/
private Context mContext;
/*Log提示信息头*/
private final String TAG = "Bmob";
/*上传的头像*/
private String icon;
/*登陆成功判断*/
private boolean islogin = false;
/*注册成功判断*/
private boolean isregist = false;
public BmobUtil(Context context) {
mContext = context;
Bmob.initialize(mContext, code.AppKey);
}
/**
* @param username 用户名
* @param password 密码
* @function 登录
*/
public void bmobLogin(final String username, final String password) {
final Message message = new Message();
message.what = BMOB_LOGIN_OK;
if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
Toast.makeText(mContext, "用户名密码不能为空", Toast.LENGTH_SHORT).show();
} else {
BmobUser bmobUser = new BmobUser();
bmobUser.setUsername(username);
bmobUser.setPassword(password);
bmobUser.login(mContext, new SaveListener() {
@Override
public void onSuccess() {
islogin = true;
Log.e(TAG, "denglu");
EMClient.getInstance().login(username, password, new EMCallBack() {//回调
@Override
public void onSuccess() {
EMClient.getInstance().groupManager().loadAllGroups();
EMClient.getInstance().chatManager().loadAllConversations();
message.obj = true;
bmobHandler.sendMessage(message);
}
@Override
public void onProgress(int progress, String status) {
}
@Override
public void onError(int code, String msg) {
Log.e(TAG,"huanxin"+code+" "+msg);
message.obj = false;
bmobHandler.sendMessage(message);
}
});
}
@Override
public void onFailure(int i, String s) {
Log.e(TAG, "Login" + i + "&&" + s);
message.obj = false;
bmobHandler.sendMessage(message);
}
});
Log.e(TAG, "denglucheng");
}
}
/**
* @param username 用户名
* @param password 密码
* @param name 昵称
* @param phoneNumber 手机号码
* @param school 学校
* @function 注册
*/
public void bmobRegist(final String username, final String password, String name, String sex, String phoneNumber, String school) {
final Message message = new Message();
message.what = BMOB_REGIST_OK;
if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password) || TextUtils.isEmpty(school)) {
Toast.makeText(mContext, "信息不能为空", Toast.LENGTH_SHORT).show();
} else if (!StringUtil.isMobileNO(phoneNumber)) {
Toast.makeText(mContext, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
} else {
PersonBean bmobUser = new PersonBean();
bmobUser.setUsername(username);
bmobUser.setPassword(password);
bmobUser.setMobilePhoneNumber(phoneNumber);
bmobUser.setName(name);
bmobUser.setSchool(school);
bmobUser.setSex(sex);
bmobUser.signUp(mContext, new SaveListener() {
@Override
public void onSuccess() {
//注册失败会抛出HyphenateException
try {
EMClient.getInstance().createAccount(username, password);//同步方法
} catch (HyphenateException e) {
e.printStackTrace();
}
message.obj = true;
bmobHandler.sendMessage(message);
}
@Override
public void onFailure(int i, String s) {
Log.e(TAG, " " + i + " " + s);
message.obj = false;
bmobHandler.sendMessage(message);
}
});
}
}
/**
* @param t JAVABEAN
* @param <T> 泛型,这里可以使用JAVABEAN来让BMOB知道查询的具体类别(每个JavaBean的查询类别在JAVABEAN内部)
* @function 保存进数据库
*/
public<T extends BmobObject> void saveMSG(T t){
final Message message = new Message();
message.what = BMOB_SAVE_OK;
t.save(mContext, new SaveListener() {
@Override
public void onSuccess() {
Log.i(TAG,"保存成功");
message.obj = true;
bmobHandler.sendMessage(message);
}
@Override
public void onFailure(int i, String s) {
Log.e(TAG,"保存失败");
message.obj = false;
bmobHandler.sendMessage(message);
}
});
}
/**
* @param file 文件路径(图片)
* @param personBean 用户JAVABEAN
* @function 上传头像
*/
public void upLoadIcon(final PersonBean personBean, File file){
final Message message = new Message();
message.what = BMOB_ICON_OK;
if (!StringUtil.isEmpty(personBean.toString())) {
if (StringUtil.fileIsExists(file)) {
final BmobFile bmobFile = new BmobFile(file);
bmobFile.upload(mContext, new UploadFileListener() {
@Override
public void onSuccess() {
icon = bmobFile.getFileUrl(mContext);
Log.e(TAG, "上传地址" + icon);
personBean.setIcon(icon);
Log.e(TAG, "上传chenggong ");
update(personBean);
message.obj = true;
bmobHandler.sendMessage(message);
}
@Override
public void onFailure(int i, String s) {
Log.e(TAG, "111upLoad:" + i);
message.obj = false;
bmobHandler.sendMessage(message);
}
});
} else {
Toast.makeText(mContext, "文件路径不正确", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(mContext, "登录超时,请重新登录", Toast.LENGTH_SHORT).show();
}
}
/**
* @param condition 登录账号
* @function 查询用户信息
*/
public void queryPersonMSG(String condition) {
final Message message = new Message();
message.what = BMOB_QUERY_OK;
if (StringUtil.isEmpty(condition)) {
Toast.makeText(mContext, "查询条件为空", Toast.LENGTH_SHORT).show();
} else {
BmobQuery<PersonBean> bq = new BmobQuery<>();
bq.addWhereEqualTo("username", condition);
bq.findObjects(mContext, new FindListener<PersonBean>() {
@Override
public void onSuccess(List<PersonBean> list) {
Log.e(TAG, "getUserMSG:" + "获取成功" + list.toString());
message.obj = list;
bmobHandler.sendMessage(message);
}
@Override
public void onError(int i, String s) {
Log.e(TAG, "getUserMSG:" + "获取失败" + s);
message.obj = null;
bmobHandler.sendMessage(message);
}
});
}
}
/**
* @param condition_head 查询头
* @param condition 查询信息
* @function 查询社团信息
*/
public void querySociationMSG(String condition_head, String condition) {
final Message message = new Message();
message.what = BMOB_QUERY_OK;
if (StringUtil.isEmpty(condition) || StringUtil.isEmpty(condition_head)) {
Toast.makeText(mContext, "查询条件为空", Toast.LENGTH_SHORT).show();
} else {
BmobQuery<SociationBean> bq = new BmobQuery<>();
bq.addWhereEqualTo(condition_head, condition);
bq.findObjects(mContext, new FindListener<SociationBean>() {
@Override
public void onSuccess(List<SociationBean> list) {
Log.e(TAG, "getUserMSG:" + "获取成功" + list.toString());
message.obj = list;
bmobHandler.sendMessage(message);
}
@Override
public void onError(int i, String s) {
Log.e(TAG, "getUserMSG:" + "获取失败" + s);
message.obj = null;
bmobHandler.sendMessage(message);
}
});
}
}
/**
* @param condition_head 查询头
* @param condition 查询信息
* @function 查询活动信息
*/
public void queryAvtiveMSG(String condition_head, String condition) {
final Message message = new Message();
message.what = BMOB_QUERY_OK;
if (StringUtil.isEmpty(condition) || StringUtil.isEmpty(condition_head)) {
Toast.makeText(mContext, "查询条件为空", Toast.LENGTH_SHORT).show();
} else {
BmobQuery<ActiveBean> bq = new BmobQuery<>();
bq.addWhereEqualTo(condition_head, condition);
bq.findObjects(mContext, new FindListener<ActiveBean>() {
@Override
public void onSuccess(List<ActiveBean> list) {
Log.e(TAG, "getUserMSG:" + "获取成功" + list.toString());
message.obj = list;
bmobHandler.sendMessage(message);
}
@Override
public void onError(int i, String s) {
Log.e(TAG, "getUserMSG:" + "获取失败" + s);
message.obj = null;
bmobHandler.sendMessage(message);
}
});
}
}
/**
* @param t
* @param <T>
* @function 修改
*/
public <T extends BmobObject> void update(T t) {
final Message message = new Message();
message.what = BMOB_EDIT_OK;
t.update(mContext, new UpdateListener() {
@Override
public void onSuccess() {
Log.e(TAG, "修改成功");
message.obj = true;
bmobHandler.sendMessage(message);
}
@Override
public void onFailure(int i, String s) {
Log.e(TAG, "修改失败:" + s);
message.obj = false;
bmobHandler.sendMessage(message);
}
});
}
/**范类转换*/
public static Class getGenericSuperclass(Class entity) {
ParameterizedType type = (ParameterizedType) entity.getGenericSuperclass();
Class entityClass = (Class) type.getActualTypeArguments()[0];
return entityClass;
}
}
因为和即时通信用的是环信,所以在登录和注册那儿加了环信的方法。
由于不晓得该怎么得到泛型对象所在的类,方法内的 JAVABEAN参数传不进去,所以查询部分只能用笨方法,就是一个一个查。。。。