package com.gh.gamecenter.manager; import android.content.Context; import android.preference.PreferenceManager; import com.gh.common.util.Installation; import com.gh.common.util.PackageUtils; import com.gh.common.util.TokenUtils; import com.gh.gamecenter.db.DataCollectionDao; import com.gh.gamecenter.db.info.DataCollectionInfo; import com.gh.gamecenter.retrofit.JSONObjectResponse; import com.gh.gamecenter.retrofit.Response; import com.gh.gamecenter.retrofit.RetrofitManager; import com.halo.assistant.HaloApp; import com.lightgame.utils.Utils; 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 retrofit2.HttpException; import rx.android.schedulers.AndroidSchedulers; import rx.schedulers.Schedulers; public class DataCollectionManager { private static DataCollectionManager mInstance; private Context mContext; private DataCollectionDao dao; private boolean isUploading; private DataCollectionManager(Context context) { mContext = context; dao = new DataCollectionDao(mContext); isUploading = false; } public static void onEvent(Context context, String type, Map 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); } } /* * 实时上传 */ private void realTimeUpload(final String type, Map map) { String version = PackageUtils.getPatchVersionName(); String user = Installation.getUUID(mContext); String channel = HaloApp.getInstance().getChannel(); map.put("version", version); map.put("user", user); map.put("device_id", TokenUtils.getDeviceId(mContext)); map.put("channel", channel); Map 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.getInstance(mContext).getData().postRealData(body) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Response() { @Override public void onResponse(ResponseBody response) { if ("applist".equals(type)) { PreferenceManager.getDefaultSharedPreferences(mContext) .edit().putLong("last_upload_applist_time", Utils.getTime(mContext)).apply(); } } }); } 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, 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, DataCollectionInfo entity, boolean isUpload) { DataCollectionManager.getInstance(context).add(entity, isUpload); } // 把事件加入列表 public void add(DataCollectionInfo entity, boolean isUpload) { // 加入列表 dao.add(entity); // 检查列表数目是否满足条件 if (isUpload) { List list = dao.getAll(); if (list.size() >= 20) { try { upload(list, false); } catch (JSONException e) { e.printStackTrace(); } } } } // 上传数据 private void upload(List list, boolean isForce) throws JSONException { if (isUploading) { return; } isUploading = true; final List ids = new ArrayList<>(); String version = PackageUtils.getPatchVersionName(); String user = Installation.getUUID(mContext); String channel = HaloApp.getInstance().getChannel(); HashMap 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 ("click-item".equals(type)) { 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()); } if (!isForce && ids.size() < 20) { return; } ArrayList params = new ArrayList<>(); for (String key : map.keySet()) { HashMap 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.getInstance(mContext).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(); } } } @Override public void onFailure(HttpException e) { isUploading = false; } }); } public static void upsert(Context context, String type, Map 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 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 ids = new ArrayList<>(); for (int i = 1, size = list.size(); i < size; i++) { ids.add(list.get(i).getId()); } dao.delete(ids); } } } // 上传数据 public void upload() { statClickData(); List list = dao.getAll(); if (list != null && !list.isEmpty()) { try { upload(list, true); } catch (JSONException e) { e.printStackTrace(); } } } /* * 统计点击数据 */ public void statClickData() { List list = dao.getClickData(); if (list != null && !list.isEmpty()) { List ids = new ArrayList<>(); List 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 map = new HashMap<>(); map.put("data", data); onEvent(mContext, "click", map); dao.delete(ids); } } public static void onEvent(Context context, String type, Map map) { map.put("createdOn", Utils.getTime(context)); if ("news".equals(type) || "download".equals(type) || "search".equals(type) || "position".equals(type) || "applist".equals(type)) { DataCollectionManager.getInstance(context).realTimeUpload(type, map); return; } onEvent(context, type, new JSONObject(map).toString(), true); } }