提交项目
This commit is contained in:
@ -0,0 +1,240 @@
|
||||
package com.gh.gamecenter.manager;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.Log;
|
||||
|
||||
import com.android.volley.Request.Method;
|
||||
import com.android.volley.Response;
|
||||
import com.android.volley.VolleyError;
|
||||
import com.gh.common.util.DeviceUtils;
|
||||
import com.gh.common.util.PackageUtils;
|
||||
import com.gh.common.util.Utils;
|
||||
import com.gh.gamecenter.db.DataCollectionDao;
|
||||
import com.gh.gamecenter.db.info.DataCollectionInfo;
|
||||
import com.gh.gamecenter.volley.extended.JsonObjectExtendedRequest;
|
||||
|
||||
public class DataCollectionManager {
|
||||
|
||||
private static DataCollectionManager mInstance;
|
||||
|
||||
private Context mContext;
|
||||
private DataCollectionDao dao;
|
||||
|
||||
private DataCollectionManager(Context context) {
|
||||
mContext = context;
|
||||
dao = new DataCollectionDao(mContext);
|
||||
}
|
||||
|
||||
public static DataCollectionManager getInstance(Context context) {
|
||||
if (mInstance == null) {
|
||||
synchronized (DataCollectionManager.class) {
|
||||
if (mInstance == null) {
|
||||
mInstance = new DataCollectionManager(
|
||||
context.getApplicationContext());
|
||||
}
|
||||
}
|
||||
}
|
||||
return mInstance;
|
||||
}
|
||||
|
||||
// 添加事件
|
||||
public static void onEvent(Context context, DataCollectionInfo entity,
|
||||
boolean isUpload) {
|
||||
DataCollectionManager.getInstance(context).add(entity, isUpload);
|
||||
}
|
||||
|
||||
public static void onEvent(Context context, String type, String data,
|
||||
boolean isUpload) {
|
||||
String id = UUID.randomUUID().toString().replaceAll("-", "");
|
||||
DataCollectionInfo entity = new DataCollectionInfo();
|
||||
entity.setId(id);
|
||||
entity.setType(type);
|
||||
entity.setData(data);
|
||||
onEvent(context, entity, isUpload);
|
||||
}
|
||||
|
||||
public static void onEvent(Context context, String type,
|
||||
Map<String, Object> map) {
|
||||
onEvent(context, type, new JSONObject(map).toString(), true);
|
||||
}
|
||||
|
||||
public static void onEvent(Context context, String type,
|
||||
Map<String, Object> map, boolean isUpload) {
|
||||
onEvent(context, type, new JSONObject(map).toString(), isUpload);
|
||||
}
|
||||
|
||||
public static void upsert(Context context, String type,
|
||||
Map<String, Object> map) {
|
||||
String id = UUID.randomUUID().toString().replaceAll("-", "");
|
||||
DataCollectionInfo entity = new DataCollectionInfo();
|
||||
entity.setId(id);
|
||||
entity.setType(type);
|
||||
entity.setData(new JSONObject(map).toString());
|
||||
DataCollectionManager.getInstance(context).upsert(entity, type);
|
||||
}
|
||||
|
||||
public void upsert(DataCollectionInfo entity, String type) {
|
||||
List<DataCollectionInfo> list = dao.findByType("user");
|
||||
if (list == null || list.isEmpty()) {
|
||||
dao.add(entity);
|
||||
} else {
|
||||
entity.setId(list.get(0).getId());
|
||||
dao.update(entity);
|
||||
if (list.size() > 1) {
|
||||
List<String> ids = new ArrayList<String>();
|
||||
for (int i = 1, size = list.size(); i < size; i++) {
|
||||
ids.add(list.get(i).getId());
|
||||
}
|
||||
dao.delete(ids);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 把事件加入列表
|
||||
public void add(DataCollectionInfo entity, boolean isUpload) {
|
||||
// 加入列表
|
||||
dao.add(entity);
|
||||
|
||||
// 检查列表数目是否满足条件
|
||||
if (isUpload) {
|
||||
// List<DataCollectionInfo> list = dao.getAll();
|
||||
// if (list.size() >= 20) {
|
||||
// try {
|
||||
// upload(list, false);
|
||||
// } catch (JSONException e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* 统计点击数据
|
||||
*/
|
||||
public void statClickData() {
|
||||
List<DataCollectionInfo> list = dao.getClickData();
|
||||
if (!list.isEmpty()) {
|
||||
List<String> ids = new ArrayList<String>();
|
||||
DataCollectionInfo dataCollectionEntity;
|
||||
List<Object> data = new ArrayList<Object>();
|
||||
try {
|
||||
for (int i = 0, size = list.size(); i < size; i++) {
|
||||
dataCollectionEntity = list.get(i);
|
||||
ids.add(dataCollectionEntity.getId());
|
||||
data.add(new JSONObject(dataCollectionEntity.getData()));
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
map.put("data", data);
|
||||
map.put("createOn", System.currentTimeMillis() / 100);
|
||||
onEvent(mContext, "click", map);
|
||||
dao.delete(ids);
|
||||
}
|
||||
}
|
||||
|
||||
// 上传数据
|
||||
public void upload() {
|
||||
statClickData();
|
||||
List<DataCollectionInfo> list = dao.getAll();
|
||||
if (!list.isEmpty()) {
|
||||
try {
|
||||
upload(list);
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 上传数据
|
||||
private void upload(List<DataCollectionInfo> list)
|
||||
throws JSONException {
|
||||
String url = "http://114.215.139.210/data/collection/upload";
|
||||
|
||||
final List<String> ids = new ArrayList<String>();
|
||||
|
||||
String version = PackageUtils.getVersion(mContext);
|
||||
String user = DeviceUtils.getDeviceID(mContext);
|
||||
String channel = (String) PackageUtils.getMetaData(mContext,
|
||||
mContext.getPackageName(), "TD_CHANNEL_ID");
|
||||
|
||||
HashMap<String, JSONArray> map = new HashMap<String, JSONArray>();
|
||||
DataCollectionInfo dataCollectionEntity;
|
||||
String type;
|
||||
JSONArray jsonArray;
|
||||
for (int i = 0, size = list.size(); i < size; i++) {
|
||||
dataCollectionEntity = list.get(i);
|
||||
type = dataCollectionEntity.getType();
|
||||
if (type.equals("click-item")) {
|
||||
continue;
|
||||
}
|
||||
jsonArray = map.get(type);
|
||||
if (jsonArray == null) {
|
||||
jsonArray = new JSONArray();
|
||||
}
|
||||
JSONObject jsonObject = new JSONObject(
|
||||
dataCollectionEntity.getData());
|
||||
jsonObject.put("version", version);
|
||||
jsonObject.put("user", user);
|
||||
jsonObject.put("channel", channel);
|
||||
jsonArray.put(jsonObject.toString());
|
||||
map.put(type, jsonArray);
|
||||
|
||||
ids.add(dataCollectionEntity.getId());
|
||||
}
|
||||
|
||||
ArrayList<Object> params = new ArrayList<Object>();
|
||||
for (String key : map.keySet()) {
|
||||
HashMap<String, Object> hashMap = new HashMap<String, Object>();
|
||||
hashMap.put("type", key);
|
||||
hashMap.put("data", map.get(key));
|
||||
JSONObject jsonObject = new JSONObject(hashMap);
|
||||
params.add(jsonObject);
|
||||
}
|
||||
JSONArray body = new JSONArray(params);
|
||||
|
||||
JsonObjectExtendedRequest request = new JsonObjectExtendedRequest(
|
||||
Method.POST, url, body.toString(),
|
||||
new Response.Listener<JSONObject>() {
|
||||
@Override
|
||||
public void onResponse(JSONObject response) {
|
||||
|
||||
Utils.log(response);
|
||||
try {
|
||||
if ("success".equals(response.getString("status"))) {
|
||||
// 上传成功,删除本地数据
|
||||
dao.delete(ids);
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}, new Response.ErrorListener() {
|
||||
@Override
|
||||
public void onErrorResponse(VolleyError error) {
|
||||
|
||||
// 上传失败,检查网络状态,继续上传
|
||||
Utils.log(error);
|
||||
if (error.networkResponse != null) {
|
||||
Utils.log(new String(error.networkResponse.data));
|
||||
}
|
||||
}
|
||||
});
|
||||
Log.e("result", new String(request.getBody()));
|
||||
// AppController.addToRequestQueue(request,
|
||||
// DataCollectionManager.class);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user