diff --git a/sdk/android_sdk/SDK/project.properties b/sdk/android_sdk/SDK/project.properties index 337e8f37..e61077f7 100644 --- a/sdk/android_sdk/SDK/project.properties +++ b/sdk/android_sdk/SDK/project.properties @@ -8,5 +8,5 @@ # project structure. # Project target. -target=android-7 +target=android-17 android.library=true diff --git a/sdk/android_sdk/SDK/src/com/wbtech/ums/UmsAgent.java b/sdk/android_sdk/SDK/src/com/wbtech/ums/UmsAgent.java index 83923ee2..f6dc12e9 100644 --- a/sdk/android_sdk/SDK/src/com/wbtech/ums/UmsAgent.java +++ b/sdk/android_sdk/SDK/src/com/wbtech/ums/UmsAgent.java @@ -20,7 +20,6 @@ import java.util.Iterator; import java.util.Locale; -import org.apache.http.util.LangUtils; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -654,7 +653,7 @@ private static void uploadAllLog(Context context) { sb.append(new String(s, 0, i)); } if (CommonUtil.isNetworkAvailable(context)) { - MyMessage message = NetworkUitlity.post(UmsConstants.preUrl + MyMessage message = NetworkUitlity.postCompressed(UmsConstants.preUrl + UmsConstants.uploadUrl, sb + ""); if (message.isFlag()) { File file = new File(Environment.getExternalStorageDirectory() diff --git a/sdk/android_sdk/SDK/src/com/wbtech/ums/common/CommonUtil.java b/sdk/android_sdk/SDK/src/com/wbtech/ums/common/CommonUtil.java index 91510d05..660e2003 100644 --- a/sdk/android_sdk/SDK/src/com/wbtech/ums/common/CommonUtil.java +++ b/sdk/android_sdk/SDK/src/com/wbtech/ums/common/CommonUtil.java @@ -17,6 +17,10 @@ import java.util.Date; import java.util.List; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + import android.app.ActivityManager; import android.content.ComponentName; import android.content.Context; @@ -38,10 +42,6 @@ import com.wbtech.ums.objects.LatitudeAndLongitude; import com.wbtech.ums.objects.SCell; -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; - public class CommonUtil { public static void saveInfoToFile(Handler handler,String type, JSONObject info, Context context) { @@ -471,42 +471,75 @@ public static boolean isHaveGravity(Context context){ * @return WIFI or MOBILE */ public static String getNetworkType(Context context){ - TelephonyManager manager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); - int type= manager.getNetworkType(); - String typeString="UNKNOWN"; - if(type==TelephonyManager.NETWORK_TYPE_CDMA){ - typeString ="CDMA"; - } - if(type==TelephonyManager.NETWORK_TYPE_EDGE){ - typeString ="EDGE"; - } - if(type==TelephonyManager.NETWORK_TYPE_EVDO_0){ - typeString ="EVDO_0"; - } - if(type==TelephonyManager.NETWORK_TYPE_EVDO_A){ - typeString ="EVDO_A"; - } - if(type==TelephonyManager.NETWORK_TYPE_GPRS){ - typeString ="GPRS"; - } - if(type==TelephonyManager.NETWORK_TYPE_HSDPA){ - typeString ="HSDPA"; - } - if(type==TelephonyManager.NETWORK_TYPE_HSPA){ - typeString ="HSPA"; - } - if(type==TelephonyManager.NETWORK_TYPE_HSUPA){ - typeString ="HSUPA"; - } - if(type==TelephonyManager.NETWORK_TYPE_UMTS){ - typeString ="UMTS"; - } - if(type==TelephonyManager.NETWORK_TYPE_UNKNOWN){ - typeString ="UNKNOWN"; - } - - return typeString; - } + String typeString = "UNKNOWN"; + // check if wifi or mobile first + final ConnectivityManager connectivityManager = (ConnectivityManager) context + .getSystemService(Context.CONNECTIVITY_SERVICE); + final NetworkInfo mobNetInfoActivity = connectivityManager.getActiveNetworkInfo(); + if (mobNetInfoActivity == null || !mobNetInfoActivity.isAvailable()) { + return typeString; + } + + int netType = mobNetInfoActivity.getType(); + if (netType == ConnectivityManager.TYPE_WIFI) { + return "wifi"; + } else if (netType == ConnectivityManager.TYPE_MOBILE) { + // for mobile net + TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); + int type = manager.getNetworkType(); + + switch (type) { + case TelephonyManager.NETWORK_TYPE_GPRS: + typeString = "GPRS"; + break; + case TelephonyManager.NETWORK_TYPE_EDGE: + typeString = "EDGE"; + break; + case TelephonyManager.NETWORK_TYPE_UMTS: + typeString = "UMTS"; + break; + + case TelephonyManager.NETWORK_TYPE_CDMA: + typeString = "CDMA"; + break; + case TelephonyManager.NETWORK_TYPE_EVDO_0: + typeString = "EVDO rev. 0"; + break; + case TelephonyManager.NETWORK_TYPE_EVDO_A: + typeString = "EVDO rev. A"; + break; + case TelephonyManager.NETWORK_TYPE_1xRTT: + typeString = "1xRTT"; + break; + case TelephonyManager.NETWORK_TYPE_HSDPA: + typeString = "HSDPA"; + break; + case TelephonyManager.NETWORK_TYPE_HSUPA: + typeString = "HSUPA"; + break; + case TelephonyManager.NETWORK_TYPE_HSPA: + typeString = "HSPA"; + break; + case TelephonyManager.NETWORK_TYPE_IDEN: + typeString = "iDen"; + break; + case TelephonyManager.NETWORK_TYPE_EVDO_B: + typeString = "EVDO rev. B"; + break; + case TelephonyManager.NETWORK_TYPE_LTE: + typeString = "LTE"; + break; + case TelephonyManager.NETWORK_TYPE_EHRPD: + typeString = "eHRPD"; + break; + case TelephonyManager.NETWORK_TYPE_HSPAP: + typeString = "HSPA+"; + break; + } + + } + return typeString; + } /** * Determine the current network type * @param context diff --git a/sdk/android_sdk/SDK/src/com/wbtech/ums/common/NetworkUitlity.java b/sdk/android_sdk/SDK/src/com/wbtech/ums/common/NetworkUitlity.java index e48401dc..2912fbc6 100644 --- a/sdk/android_sdk/SDK/src/com/wbtech/ums/common/NetworkUitlity.java +++ b/sdk/android_sdk/SDK/src/com/wbtech/ums/common/NetworkUitlity.java @@ -1,109 +1,138 @@ -/** - * Cobub Razor - * - * An open source analytics android sdk for mobile applications - * - * @package Cobub Razor - * @author WBTECH Dev Team - * @copyright Copyright (c) 2011 - 2012, NanJing Western Bridge Co.,Ltd. - * @license http://www.cobub.com/products/cobub-razor/license - * @link http://www.cobub.com/products/cobub-razor/ - * @since Version 0.1 - * @filesource - */ -package com.wbtech.ums.common; - -import java.net.URLDecoder; - -import org.apache.http.HttpResponse; -import org.apache.http.client.HttpClient; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.entity.StringEntity; -import org.apache.http.impl.client.DefaultHttpClient; -import org.apache.http.protocol.HTTP; -import org.apache.http.util.EntityUtils; -import org.json.JSONException; -import org.json.JSONObject; - -import android.util.Log; - -import com.wbtech.ums.objects.MyMessage; -public class NetworkUitlity { - public static long paramleng = 256L; - public static String DEFAULT_CHARSET=" HTTP.UTF_8"; - public static MyMessage post(String url, String data) { - // TODO Auto-generated method stub - CommonUtil.printLog("ums",url); - String returnContent = ""; - MyMessage message=new MyMessage(); - HttpClient httpclient = new DefaultHttpClient(); - HttpPost httppost = new HttpPost(url); - try { - StringEntity se = new StringEntity("content="+data, HTTP.UTF_8); - CommonUtil.printLog("postdata", "content="+data); - se.setContentType("application/x-www-form-urlencoded"); - httppost.setEntity(se); - HttpResponse response = httpclient.execute(httppost); - int status = response.getStatusLine().getStatusCode(); - CommonUtil.printLog("ums",status+""); - String returnXML = EntityUtils.toString(response.getEntity()); - returnContent = URLDecoder.decode(returnXML); - switch (status) { - case 200: - message.setFlag(true); - message.setMsg(returnContent); - break; - - default: - Log.e("error", status+returnContent); - message.setFlag(false); - message.setMsg(returnContent); - break; - } - } catch (Exception e) { - JSONObject jsonObject = new JSONObject(); - - try { - jsonObject.put("err", e.toString()); - returnContent = jsonObject.toString(); - message.setFlag(false); - message.setMsg(returnContent); - } catch (JSONException e1) { - e1.printStackTrace(); - } - - - } - CommonUtil.printLog("UMSAGENT", message.getMsg()); - return message; - } - - public static String Post(String url, String data) { - - CommonUtil.printLog("ums",url); - - - HttpClient httpclient = new DefaultHttpClient(); - HttpPost httppost = new HttpPost(url); - try { - StringEntity se = new StringEntity("content="+data, HTTP.UTF_8); - CommonUtil.printLog("postdata", "content="+data); - se.setContentType("application/x-www-form-urlencoded"); - httppost.setEntity(se); - HttpResponse response = httpclient.execute(httppost); - int status = response.getStatusLine().getStatusCode(); - CommonUtil.printLog("ums",status+""); - String returnXML = EntityUtils.toString(response.getEntity()); - Log.d("returnString", URLDecoder.decode(returnXML)); - return URLDecoder.decode(returnXML); - - } catch (Exception e) { - CommonUtil.printLog("ums",e.toString()); - } - return null; - } - - - - -} +/** + * Cobub Razor + * + * An open source analytics android sdk for mobile applications + * + * @package Cobub Razor + * @author WBTECH Dev Team + * @copyright Copyright (c) 2011 - 2012, NanJing Western Bridge Co.,Ltd. + * @license http://www.cobub.com/products/cobub-razor/license + * @link http://www.cobub.com/products/cobub-razor/ + * @since Version 0.1 + * @filesource + */ +package com.wbtech.ums.common; + +import java.net.HttpURLConnection; +import java.net.URL; +import java.net.URLDecoder; +import java.util.zip.GZIPOutputStream; + +import org.apache.http.HttpResponse; +import org.apache.http.HttpStatus; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.protocol.HTTP; +import org.apache.http.util.EntityUtils; +import org.json.JSONException; +import org.json.JSONObject; + +import android.util.Log; + +import com.wbtech.ums.objects.MyMessage; + +public class NetworkUitlity { + public static long paramleng = 256L; + public static String DEFAULT_CHARSET = " HTTP.UTF_8"; + + public static MyMessage post(String url, String data) { + // TODO Auto-generated method stub + CommonUtil.printLog("ums", url); + String returnContent = ""; + MyMessage message = new MyMessage(); + HttpClient httpclient = new DefaultHttpClient(); + HttpPost httppost = new HttpPost(url); + try { + StringEntity se = new StringEntity("content=" + data, HTTP.UTF_8); + CommonUtil.printLog("postdata", "content=" + data); + se.setContentType("application/x-www-form-urlencoded"); + httppost.setEntity(se); + HttpResponse response = httpclient.execute(httppost); + int status = response.getStatusLine().getStatusCode(); + CommonUtil.printLog("ums", status + ""); + String returnXML = EntityUtils.toString(response.getEntity()); + returnContent = URLDecoder.decode(returnXML); + switch (status) { + case 200: + message.setFlag(true); + message.setMsg(returnContent); + break; + + default: + Log.e("error", status + returnContent); + message.setFlag(false); + message.setMsg(returnContent); + break; + } + } catch (Exception e) { + JSONObject jsonObject = new JSONObject(); + + try { + jsonObject.put("err", e.toString()); + returnContent = jsonObject.toString(); + message.setFlag(false); + message.setMsg(returnContent); + } catch (JSONException e1) { + e1.printStackTrace(); + } + + } + CommonUtil.printLog("UMSAGENT", message.getMsg()); + return message; + } + + public static String Post(String url, String data) { + + CommonUtil.printLog("ums", url); + + HttpClient httpclient = new DefaultHttpClient(); + HttpPost httppost = new HttpPost(url); + try { + StringEntity se = new StringEntity("content=" + data, HTTP.UTF_8); + CommonUtil.printLog("postdata", "content=" + data); + se.setContentType("application/x-www-form-urlencoded"); + httppost.setEntity(se); + HttpResponse response = httpclient.execute(httppost); + int status = response.getStatusLine().getStatusCode(); + CommonUtil.printLog("ums", status + ""); + String returnXML = EntityUtils.toString(response.getEntity()); + Log.d("returnString", URLDecoder.decode(returnXML)); + return URLDecoder.decode(returnXML); + + } catch (Exception e) { + CommonUtil.printLog("ums", e.toString()); + } + return null; + } + + /** + * compress string use gzip to reduce the size + * */ + public static MyMessage postCompressed(String requestUrl, String data) { + MyMessage message = new MyMessage(); + try { + URL url = new URL(requestUrl); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setDoOutput(true); + conn.setRequestProperty("Content-encoding", "gzip"); + conn.setRequestProperty("Content-type", "application/octet-stream"); + GZIPOutputStream dos1 = new GZIPOutputStream(conn.getOutputStream()); + dos1.write(data.getBytes("utf-8")); + dos1.flush(); + dos1.close(); + if (conn.getResponseCode() == HttpStatus.SC_OK) { + message.setFlag(true); + } else { + message.setFlag(false); + message.setMsg(conn.getResponseMessage()); + } + } catch (Exception e) { + Log.e("dakele", e.getMessage(), e); + } + message.setFlag(false); + return message; + } + +} diff --git a/sdk/android_sdk/SDK/src/com/wbtech/ums/controller/EventController.java b/sdk/android_sdk/SDK/src/com/wbtech/ums/controller/EventController.java index 13753652..d07da863 100644 --- a/sdk/android_sdk/SDK/src/com/wbtech/ums/controller/EventController.java +++ b/sdk/android_sdk/SDK/src/com/wbtech/ums/controller/EventController.java @@ -1,6 +1,7 @@ - package com.wbtech.ums.controller; +import org.json.JSONObject; + import android.content.Context; import android.os.Handler; @@ -12,42 +13,39 @@ 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 (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; + } }