Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check event online status before send #77

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions sdk/android_sdk/SDK/project.properties

This file was deleted.

31 changes: 19 additions & 12 deletions sdk/android_sdk/SDK/src/com/wbtech/ums/common/UmsConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,24 @@
* @filesource
*/
package com.wbtech.ums.common;

public class UmsConstants {
public static boolean DebugMode=true;
public static long kContinueSessionMillis = 30000L;
public static final Object saveOnlineConfigMutex = new Object();
public static final String eventUrl="/ums/postEvent";
public static final String errorUrl = "/ums/postErrorLog";
public static final String clientDataUrl = "/ums/postClientData";
public static final String updataUrl = "/ums/getApplicationUpdate";
public static final String activityUrl = "/ums/postActivityLog";
public static final String onlineConfigUrl ="/ums/getOnlineConfiguration";
public static final String uploadUrl = "/ums/uploadLog";
public static String preUrl="";
public static boolean DebugMode = true;
public static long kContinueSessionMillis = 30000L;
public static final Object saveOnlineConfigMutex = new Object();
public static final String eventUrl = "/ums/postEvent";
public static final String errorUrl = "/ums/postErrorLog";
public static final String clientDataUrl = "/ums/postClientData";
public static final String updataUrl = "/ums/getApplicationUpdate";
public static final String activityUrl = "/ums/postActivityLog";
public static final String onlineConfigUrl = "/ums/getOnlineConfiguration";
public static final String uploadUrl = "/ums/uploadLog";
public static String preUrl = "";

public static final int FLAG_EVENT_NOT_REGISTER = -5;

public static final String PREFERENCE_CONFIG = "ums_config";
public static final String EVENT_ONLINE_PREFIX = "event_online_";
public static final String EVENT_ONLINE_UPDATEDATE = "event_online_updatedate";
public static final String TAG = "UmsAgent";
}

110 changes: 72 additions & 38 deletions sdk/android_sdk/SDK/src/com/wbtech/ums/controller/EventController.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@

package com.wbtech.ums.controller;

import java.util.Date;

import org.json.JSONObject;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Handler;

import com.wbtech.ums.common.AssembJSONObj;
Expand All @@ -12,42 +17,71 @@
import com.wbtech.ums.objects.MyMessage;
import com.wbtech.ums.objects.PostObjEvent;

import org.json.JSONObject;

public class EventController {
static final String EVENTURL = UmsConstants.preUrl + UmsConstants.eventUrl;
public static boolean postEventInfo(Handler handler, Context context, PostObjEvent event) {
try {
if (!event.verification())
{
CommonUtil.printLog("UMSAgent", "Illegal value of acc in postEventInfo");
return false;
}

JSONObject localJSONObject = AssembJSONObj.getEventJOSNobj( event);

if (1 == CommonUtil.getReportPolicyMode(context)
&& CommonUtil.isNetworkAvailable(context)) {
try {
String reslut = NetworkUitlity.Post(EVENTURL, localJSONObject.toString());
MyMessage info = JSONParser.parse(reslut);
if (!info.isFlag()) {
CommonUtil.saveInfoToFile(handler, "eventInfo", localJSONObject, context);
return false;
}
} catch (Exception e) {
CommonUtil.printLog("UmsAgent", "fail to post eventContent");
}
} else {

CommonUtil.saveInfoToFile(handler, "eventInfo", localJSONObject, context);
return false;
}
} catch (Exception e) {
CommonUtil.printLog("UMSAgent", "Exception occurred in postEventInfo()");
e.printStackTrace();
return false;
}
return true;
}
static final String EVENTURL = UmsConstants.preUrl + UmsConstants.eventUrl;

public static boolean postEventInfo(Handler handler, Context context, PostObjEvent event) {
try {
if (!event.verification()) {
CommonUtil.printLog("UMSAgent", "Illegal value of acc in postEventInfo");
return false;
}

JSONObject localJSONObject = AssembJSONObj.getEventJOSNobj(event);
if (!checkEventOnline(context, event.getEvent_id())) {
return false;
}

if (1 == CommonUtil.getReportPolicyMode(context) && CommonUtil.isNetworkAvailable(context)) {
try {
String reslut = NetworkUitlity.Post(EVENTURL, localJSONObject.toString());
MyMessage info = JSONParser.parse(reslut);
if (!info.isFlag()) {
if (info.getFlagCode() == UmsConstants.FLAG_EVENT_NOT_REGISTER) {
// event not register, cache status for check
SharedPreferences umsConfig = context.getSharedPreferences(UmsConstants.PREFERENCE_CONFIG,
Context.MODE_PRIVATE);
Editor editor = umsConfig.edit();
editor.putBoolean(UmsConstants.EVENT_ONLINE_PREFIX + event.getEvent_id(), false);
editor.putLong(UmsConstants.EVENT_ONLINE_UPDATEDATE, new Date().getTime());
editor.commit();
} else {
CommonUtil.saveInfoToFile(handler, "eventInfo", localJSONObject, context);
}
return false;
}
} catch (Exception e) {
CommonUtil.printLog("UmsAgent", "fail to post eventContent");
}
} else {

CommonUtil.saveInfoToFile(handler, "eventInfo", localJSONObject, context);
return false;
}
} catch (Exception e) {
CommonUtil.printLog("UMSAgent", "Exception occurred in postEventInfo()");
e.printStackTrace();
return false;
}
return true;
}

/**
* check the event online status
* */
private static boolean checkEventOnline(Context context, String event_id) {
SharedPreferences config = context.getSharedPreferences(UmsConstants.PREFERENCE_CONFIG, Context.MODE_PRIVATE);
boolean onlineStatus = config.getBoolean(UmsConstants.EVENT_ONLINE_PREFIX + event_id, true);
if (onlineStatus) {
return true;
}
// check the last update date, if more than 1 day, send event again and
// the status will be saved after event send
long lastUpdateDate = config.getLong(UmsConstants.EVENT_ONLINE_UPDATEDATE, 0);
long now = new Date().getTime();
if (now - lastUpdateDate > 86400000) {
return true;
}
return false;
}
}
7 changes: 4 additions & 3 deletions sdk/android_sdk/SDK/src/com/wbtech/ums/dao/JSONParser.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.wbtech.ums.dao;

import com.wbtech.ums.common.CommonUtil;
import com.wbtech.ums.objects.MyMessage;

import org.json.JSONException;
import org.json.JSONObject;

import com.wbtech.ums.common.CommonUtil;
import com.wbtech.ums.objects.MyMessage;

public class JSONParser {
public static MyMessage parse(String str) {

Expand All @@ -23,6 +23,7 @@ public static MyMessage parse(String str) {
message.setFlag(false);
}
message.setMsg(jsonObject.getString("msg"));
message.setFlagCode(Integer.parseInt(flag));
} catch (JSONException e1) {
CommonUtil.printLog("JSONParser", e1.toString());
} catch (NumberFormatException e2) {
Expand Down
8 changes: 8 additions & 0 deletions sdk/android_sdk/SDK/src/com/wbtech/ums/objects/MyMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
public class MyMessage {
private boolean flag;
private String msg;
private int flagCode;

public int getFlagCode() {
return flagCode;
}
public void setFlagCode(int flagCode) {
this.flagCode = flagCode;
}
public boolean isFlag() {
return flag;
}
Expand Down