reformat code & optimize import & reorder entries
This commit is contained in:
@ -1,289 +1,273 @@
|
||||
package com.gh.gamecenter.manager;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.gh.common.constant.Config;
|
||||
import com.gh.common.util.Installation;
|
||||
import com.gh.common.util.PackageUtils;
|
||||
import com.gh.common.util.TokenUtils;
|
||||
import com.gh.common.util.Utils;
|
||||
import com.gh.common.util.*;
|
||||
import com.gh.gamecenter.db.DataCollectionDao;
|
||||
import com.gh.gamecenter.db.info.DataCollectionInfo;
|
||||
import com.gh.gamecenter.retrofit.JSONObjectResponse;
|
||||
import com.gh.gamecenter.retrofit.*;
|
||||
import com.gh.gamecenter.retrofit.Response;
|
||||
import com.gh.gamecenter.retrofit.RetrofitManager;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.ResponseBody;
|
||||
import okhttp3.*;
|
||||
import org.json.*;
|
||||
import retrofit2.adapter.rxjava.HttpException;
|
||||
import rx.android.schedulers.AndroidSchedulers;
|
||||
import rx.schedulers.Schedulers;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class DataCollectionManager {
|
||||
|
||||
private static DataCollectionManager mInstance;
|
||||
private static DataCollectionManager mInstance;
|
||||
|
||||
private Context mContext;
|
||||
private DataCollectionDao dao;
|
||||
private Context mContext;
|
||||
private DataCollectionDao dao;
|
||||
private boolean isUploading;
|
||||
|
||||
private DataCollectionManager(Context context) {
|
||||
mContext = context;
|
||||
dao = new DataCollectionDao(mContext);
|
||||
isUploading = false;
|
||||
}
|
||||
private DataCollectionManager(Context context) {
|
||||
mContext = context;
|
||||
dao = new DataCollectionDao(mContext);
|
||||
isUploading = false;
|
||||
}
|
||||
|
||||
public static DataCollectionManager getInstance(Context context) {
|
||||
if (mInstance == null) {
|
||||
synchronized (DataCollectionManager.class) {
|
||||
if (mInstance == null) {
|
||||
mInstance = new DataCollectionManager(
|
||||
context.getApplicationContext());
|
||||
}
|
||||
}
|
||||
}
|
||||
return mInstance;
|
||||
}
|
||||
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, 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, 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) {
|
||||
map.put("createdOn", Utils.getTime(context));
|
||||
if ("news".equals(type) || "download".equals(type) || "search".equals(type) || "position".equals(type)) {
|
||||
DataCollectionManager.getInstance(context).realTimeUpload(type, map);
|
||||
return;
|
||||
}
|
||||
onEvent(context, type, new JSONObject(map).toString(), true);
|
||||
}
|
||||
public static void onEvent(Context context, String type, Map<String, Object> map) {
|
||||
map.put("createdOn", Utils.getTime(context));
|
||||
if ("news".equals(type) || "download".equals(type) || "search".equals(type) || "position".equals(type)) {
|
||||
DataCollectionManager.getInstance(context).realTimeUpload(type, map);
|
||||
return;
|
||||
}
|
||||
onEvent(context, type, new JSONObject(map).toString(), true);
|
||||
}
|
||||
|
||||
public static void onEvent(Context context, String type, Map<String, Object> map, boolean isUpload) {
|
||||
map.put("createdOn", Utils.getTime(context));
|
||||
if (isUpload) {
|
||||
DataCollectionManager.getInstance(context).realTimeUpload(type, map);
|
||||
} else {
|
||||
onEvent(context, type, new JSONObject(map).toString(), false);
|
||||
}
|
||||
}
|
||||
public static void onEvent(Context context, String type, Map<String, Object> map, boolean isUpload) {
|
||||
map.put("createdOn", Utils.getTime(context));
|
||||
if (isUpload) {
|
||||
DataCollectionManager.getInstance(context).realTimeUpload(type, map);
|
||||
} else {
|
||||
onEvent(context, type, new JSONObject(map).toString(), false);
|
||||
}
|
||||
}
|
||||
|
||||
public static void upsert(Context context, String type, Map<String, Object> map) {
|
||||
map.put("createdOn", Utils.getTime(context));
|
||||
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 static void upsert(Context context, String type, Map<String, Object> map) {
|
||||
map.put("createdOn", Utils.getTime(context));
|
||||
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(type);
|
||||
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<>();
|
||||
for (int i = 1, size = list.size(); i < size; i++) {
|
||||
ids.add(list.get(i).getId());
|
||||
}
|
||||
dao.delete(ids);
|
||||
}
|
||||
}
|
||||
}
|
||||
public void upsert(DataCollectionInfo entity, String type) {
|
||||
List<DataCollectionInfo> list = dao.findByType(type);
|
||||
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<>();
|
||||
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);
|
||||
// 把事件加入列表
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// 检查列表数目是否满足条件
|
||||
if (isUpload) {
|
||||
List<DataCollectionInfo> list = dao.getAll();
|
||||
if (list.size() >= 20) {
|
||||
try {
|
||||
upload(list, false);
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* 实时上传
|
||||
*/
|
||||
private void realTimeUpload(final String type, Map<String, Object> map) {
|
||||
String version = PackageUtils.getVersionName(mContext);
|
||||
String user = Installation.getUUID(mContext);
|
||||
String channel = (String) PackageUtils.getMetaData(mContext,
|
||||
mContext.getPackageName(), "TD_CHANNEL_ID");
|
||||
map.put("version", version);
|
||||
map.put("user", user);
|
||||
map.put("device_id", TokenUtils.getDeviceId(mContext));
|
||||
map.put("channel", channel);
|
||||
Map<String, String> params = new HashMap<>();
|
||||
params.put("type", type);
|
||||
params.put("data", new JSONObject(map).toString());
|
||||
/*
|
||||
* 实时上传
|
||||
*/
|
||||
private void realTimeUpload(final String type, Map<String, Object> map) {
|
||||
String version = PackageUtils.getVersionName(mContext);
|
||||
String user = Installation.getUUID(mContext);
|
||||
String channel = (String) PackageUtils.getMetaData(mContext,
|
||||
mContext.getPackageName(), "TD_CHANNEL_ID");
|
||||
map.put("version", version);
|
||||
map.put("user", user);
|
||||
map.put("device_id", TokenUtils.getDeviceId(mContext));
|
||||
map.put("channel", channel);
|
||||
Map<String, String> params = new HashMap<>();
|
||||
params.put("type", type);
|
||||
params.put("data", new JSONObject(map).toString());
|
||||
|
||||
RequestBody body = RequestBody.create(MediaType.parse("application/json"),
|
||||
new JSONObject(params).toString());
|
||||
RetrofitManager.getData().postRealData(body)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(new Response<ResponseBody>() {
|
||||
@Override
|
||||
public void onResponse(ResponseBody response) {
|
||||
if ("applist".equals(type)) {
|
||||
mContext.getSharedPreferences(Config.PREFERENCE, Context.MODE_PRIVATE)
|
||||
.edit().putLong("last_upload_applist_time", Utils.getTime(mContext)).apply();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
RequestBody body = RequestBody.create(MediaType.parse("application/json"),
|
||||
new JSONObject(params).toString());
|
||||
RetrofitManager.getData().postRealData(body)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(new Response<ResponseBody>() {
|
||||
@Override
|
||||
public void onResponse(ResponseBody response) {
|
||||
if ("applist".equals(type)) {
|
||||
mContext.getSharedPreferences(Config.PREFERENCE, Context.MODE_PRIVATE)
|
||||
.edit().putLong("last_upload_applist_time", Utils.getTime(mContext)).apply();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/*
|
||||
* 统计点击数据
|
||||
*/
|
||||
public void statClickData() {
|
||||
List<DataCollectionInfo> list = dao.getClickData();
|
||||
if (list != null && !list.isEmpty()) {
|
||||
List<String> ids = new ArrayList<>();
|
||||
List<Object> data = new ArrayList<>();
|
||||
try {
|
||||
DataCollectionInfo dataCollectionEntity;
|
||||
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<>();
|
||||
map.put("data", data);
|
||||
onEvent(mContext, "click", map);
|
||||
dao.delete(ids);
|
||||
}
|
||||
}
|
||||
/*
|
||||
* 统计点击数据
|
||||
*/
|
||||
public void statClickData() {
|
||||
List<DataCollectionInfo> list = dao.getClickData();
|
||||
if (list != null && !list.isEmpty()) {
|
||||
List<String> ids = new ArrayList<>();
|
||||
List<Object> data = new ArrayList<>();
|
||||
try {
|
||||
DataCollectionInfo dataCollectionEntity;
|
||||
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<>();
|
||||
map.put("data", data);
|
||||
onEvent(mContext, "click", map);
|
||||
dao.delete(ids);
|
||||
}
|
||||
}
|
||||
|
||||
// 上传数据
|
||||
public void upload() {
|
||||
statClickData();
|
||||
List<DataCollectionInfo> list = dao.getAll();
|
||||
if (list != null && !list.isEmpty()) {
|
||||
try {
|
||||
upload(list, true);
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
// 上传数据
|
||||
public void upload() {
|
||||
statClickData();
|
||||
List<DataCollectionInfo> list = dao.getAll();
|
||||
if (list != null && !list.isEmpty()) {
|
||||
try {
|
||||
upload(list, true);
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isUploading;
|
||||
// 上传数据
|
||||
private void upload(List<DataCollectionInfo> list, boolean isForce) throws JSONException {
|
||||
if (isUploading) {
|
||||
return;
|
||||
}
|
||||
isUploading = true;
|
||||
|
||||
// 上传数据
|
||||
private void upload(List<DataCollectionInfo> list, boolean isForce) throws JSONException {
|
||||
if (isUploading) {
|
||||
return;
|
||||
}
|
||||
isUploading = true;
|
||||
final List<String> ids = new ArrayList<>();
|
||||
|
||||
final List<String> ids = new ArrayList<>();
|
||||
String version = PackageUtils.getVersionName(mContext);
|
||||
String user = Installation.getUUID(mContext);
|
||||
String channel = (String) PackageUtils.getMetaData(mContext,
|
||||
mContext.getPackageName(), "TD_CHANNEL_ID");
|
||||
|
||||
String version = PackageUtils.getVersionName(mContext);
|
||||
String user = Installation.getUUID(mContext);
|
||||
String channel = (String) PackageUtils.getMetaData(mContext,
|
||||
mContext.getPackageName(), "TD_CHANNEL_ID");
|
||||
HashMap<String, JSONArray> map = new HashMap<>();
|
||||
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();
|
||||
map.put(type, jsonArray);
|
||||
}
|
||||
JSONObject jsonObject = new JSONObject(
|
||||
dataCollectionEntity.getData());
|
||||
jsonObject.put("version", version);
|
||||
jsonObject.put("user", user);
|
||||
jsonObject.put("device_id", TokenUtils.getDeviceId(mContext));
|
||||
jsonObject.put("channel", channel);
|
||||
jsonArray.put(jsonObject.toString());
|
||||
|
||||
HashMap<String, JSONArray> map = new HashMap<>();
|
||||
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();
|
||||
map.put(type, jsonArray);
|
||||
}
|
||||
JSONObject jsonObject = new JSONObject(
|
||||
dataCollectionEntity.getData());
|
||||
jsonObject.put("version", version);
|
||||
jsonObject.put("user", user);
|
||||
jsonObject.put("device_id", TokenUtils.getDeviceId(mContext));
|
||||
jsonObject.put("channel", channel);
|
||||
jsonArray.put(jsonObject.toString());
|
||||
ids.add(dataCollectionEntity.getId());
|
||||
}
|
||||
|
||||
ids.add(dataCollectionEntity.getId());
|
||||
}
|
||||
if (!isForce && ids.size() < 20) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isForce && ids.size() < 20) {
|
||||
return;
|
||||
}
|
||||
ArrayList<Object> params = new ArrayList<>();
|
||||
for (String key : map.keySet()) {
|
||||
HashMap<String, Object> hashMap = new HashMap<>();
|
||||
hashMap.put("type", key);
|
||||
hashMap.put("data", map.get(key));
|
||||
JSONObject jsonObject = new JSONObject(hashMap);
|
||||
params.add(jsonObject);
|
||||
}
|
||||
|
||||
ArrayList<Object> params = new ArrayList<>();
|
||||
for (String key : map.keySet()) {
|
||||
HashMap<String, Object> hashMap = new HashMap<>();
|
||||
hashMap.put("type", key);
|
||||
hashMap.put("data", map.get(key));
|
||||
JSONObject jsonObject = new JSONObject(hashMap);
|
||||
params.add(jsonObject);
|
||||
}
|
||||
RequestBody body = RequestBody.create(MediaType.parse("application/json"),
|
||||
new JSONArray(params).toString());
|
||||
RetrofitManager.getData().postData(body)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(new JSONObjectResponse() {
|
||||
@Override
|
||||
public void onResponse(JSONObject response) {
|
||||
isUploading = false;
|
||||
|
||||
RequestBody body = RequestBody.create(MediaType.parse("application/json"),
|
||||
new JSONArray(params).toString());
|
||||
RetrofitManager.getData().postData(body)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(new JSONObjectResponse() {
|
||||
@Override
|
||||
public void onResponse(JSONObject response) {
|
||||
isUploading = false;
|
||||
if (response.length() != 0) {
|
||||
try {
|
||||
if ("success".equals(response.getString("status"))) {
|
||||
// 上传成功,删除本地数据
|
||||
dao.delete(ids);
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (response.length() != 0) {
|
||||
try {
|
||||
if ("success".equals(response.getString("status"))) {
|
||||
// 上传成功,删除本地数据
|
||||
dao.delete(ids);
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(HttpException e) {
|
||||
isUploading = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
@Override
|
||||
public void onFailure(HttpException e) {
|
||||
isUploading = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user