|
|
|
|
@ -1,11 +1,11 @@
|
|
|
|
|
/**
|
|
|
|
|
* DatapointHelper.java
|
|
|
|
|
* Copyright (C) 2009 Char Software Inc., DBA Localytics
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* This code is provided under the Localytics Modified BSD License.
|
|
|
|
|
* A copy of this license has been distributed in a file called LICENSE
|
|
|
|
|
* with this source code.
|
|
|
|
|
*
|
|
|
|
|
* with this source code.
|
|
|
|
|
*
|
|
|
|
|
* Please visit www.localytics.com for more information.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
@ -21,33 +21,34 @@ import java.util.TimeZone;
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.content.pm.PackageManager;
|
|
|
|
|
import android.net.wifi.WifiManager;
|
|
|
|
|
import android.provider.Settings.System;
|
|
|
|
|
import android.telephony.TelephonyManager;
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
import android.provider.Settings.System;
|
|
|
|
|
/**
|
|
|
|
|
* Provides a number of static functions to aid in the collection and formatting
|
|
|
|
|
* of datapoints.
|
|
|
|
|
* @author Localytics
|
|
|
|
|
*/
|
|
|
|
|
public final class DatapointHelper
|
|
|
|
|
@SuppressWarnings("nls")
|
|
|
|
|
public final class DatapointHelper
|
|
|
|
|
{
|
|
|
|
|
// This class should never be instantiated
|
|
|
|
|
private DatapointHelper() { }
|
|
|
|
|
|
|
|
|
|
private DatapointHelper() { /**/ }
|
|
|
|
|
|
|
|
|
|
private static final String LOG_PREFIX = "(DatapointHelper) ";
|
|
|
|
|
private static final String DROID2_ANDROID_ID = "9774d56d682e549c";
|
|
|
|
|
|
|
|
|
|
// Each YAML entry either goes to the session controller, the event controller
|
|
|
|
|
// or the optin controller
|
|
|
|
|
|
|
|
|
|
// Each YAML entry either goes to the session controller, the event controller
|
|
|
|
|
// or the optin controller
|
|
|
|
|
public static final String CONTROLLER_SESSION = "- c: se\n";
|
|
|
|
|
public static final String CONTROLLER_EVENT = "- c: ev\n";
|
|
|
|
|
public static final String CONTROLLER_OPT = "- c: optin\n";
|
|
|
|
|
|
|
|
|
|
// Each entry is either a create action, or an update action
|
|
|
|
|
|
|
|
|
|
// Each entry is either a create action, or an update action
|
|
|
|
|
public static final String ACTION_CREATE = " a: c\n";
|
|
|
|
|
public static final String ACTION_UPDATE = " a: u\n";
|
|
|
|
|
public static final String ACTION_UPDATE = " a: u\n";
|
|
|
|
|
public static final String ACTION_OPTIN = " a: optin\n";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// The target object for the data being set up.
|
|
|
|
|
public static final String OBJECT_SESSION_DP = " se:\n";
|
|
|
|
|
public static final String OBJECT_EVENT_DP = " ev:\n";
|
|
|
|
|
@ -60,91 +61,91 @@ public final class DatapointHelper
|
|
|
|
|
* WEBSERVICE PARAMENTER NAMES *
|
|
|
|
|
* *****************************
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Every object has a UUID
|
|
|
|
|
public static final String PARAM_UUID = "u";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// The identifier for this application, generated by the user on the webservice
|
|
|
|
|
public static final String PARAM_APP_UUID = "au";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// The version of this application, taken from the application's manifest.
|
|
|
|
|
public static final String PARAM_APP_VERSION = "av";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// A session's UUID as previously created.
|
|
|
|
|
public static final String PARAM_SESSION_UUID = "su";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// A hashed identifier unique to this device
|
|
|
|
|
public static final String PARAM_DEVICE_UUID = "du";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// android, iphone, blackberry, windowsmobile
|
|
|
|
|
public static final String PARAM_DEVICE_PLATFORM = "dp";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// maker of this device (currently not supported by Android)
|
|
|
|
|
public static final String PARAM_DEVICE_MAKE = "dma";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// model of the device
|
|
|
|
|
public static final String PARAM_DEVICE_MODEL = "dmo";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// version of the OS on this device
|
|
|
|
|
public static final String PARAM_OS_VERSION = "dov";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// country device is from (obtained by querying the SIM card)
|
|
|
|
|
public static final String PARAM_DEVICE_COUNTRY = "dc";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// country the current locale is set to
|
|
|
|
|
public static final String PARAM_LOCALE_COUNTRY = "dlc";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// country the language is set to
|
|
|
|
|
public static final String PARAM_LOCALE_LANGUAGE = "dll";
|
|
|
|
|
|
|
|
|
|
// Locale as a language_country string. (Not collected because this info
|
|
|
|
|
|
|
|
|
|
// Locale as a language_country string. (Not collected because this info
|
|
|
|
|
// is already provided by LOCALE_LANGUAGE and LOCALE_COUNTRY.
|
|
|
|
|
public static final String PARAM_LOCALE = "dl";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Country the user is currently in (comes from Sim card)
|
|
|
|
|
public static final String PARAM_NETWORK_COUNTRY = "nc";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Current carrier (comes from sim card)
|
|
|
|
|
public static final String PARAM_NETWORK_CARRIER = "nca";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Current mobile network code (comes from sim card)
|
|
|
|
|
public static final String PARAM_NETWORK_MNC = "mnc";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// current mobile country code (comes from sim card)
|
|
|
|
|
public static final String PARAM_NETWORK_MCC = "mcc";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// type of data connection (wifi, umts, gprs, evdo, ...)
|
|
|
|
|
public static final String PARAM_DATA_CONNECTION = "dac";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// the version of this Localytics client library
|
|
|
|
|
public static final String PARAM_LIBRARY_VERSION = "lv";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// The source where the location came from
|
|
|
|
|
public static final String PARAM_LOCATION_SOURCE = "ls";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// the latitude returned by the location provider
|
|
|
|
|
public static final String PARAM_LOCATION_LAT = "lat";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// the longitude from the location provider
|
|
|
|
|
public static final String PARAM_LOCATION_LNG = "lng";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// the current time on the user's device
|
|
|
|
|
public static final String PARAM_CLIENT_TIME = "ct";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// sent at closing time, the current time on the users's device
|
|
|
|
|
public static final String PARAM_CLIENT_CLOSED_TIME = "ctc";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// The name an event that occured
|
|
|
|
|
public static final String PARAM_EVENT_NAME = "n";
|
|
|
|
|
|
|
|
|
|
// the optin value sent in if a user opts in or out.
|
|
|
|
|
public static final String PARAM_EVENT_NAME = "n";
|
|
|
|
|
|
|
|
|
|
// the optin value sent in if a user opts in or out.
|
|
|
|
|
public static final String PARAM_OPT_VALUE = "optin";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the given key/value pair as a YAML string. This string is intended to be
|
|
|
|
|
* used to define values for the first level of data in the YAML file. This is
|
|
|
|
|
* different from the datapoints which belong another level in.
|
|
|
|
|
* @param paramName The name of the parameter
|
|
|
|
|
* different from the datapoints which belong another level in.
|
|
|
|
|
* @param paramName The name of the parameter
|
|
|
|
|
* @param paramValue The value of the parameter
|
|
|
|
|
* @param paramIndent The indent level of the parameter
|
|
|
|
|
* @return a YAML string which can be dumped to the YAML file
|
|
|
|
|
@ -153,13 +154,13 @@ public final class DatapointHelper
|
|
|
|
|
{
|
|
|
|
|
if (paramName.length() > LocalyticsSession.MAX_NAME_LENGTH)
|
|
|
|
|
{
|
|
|
|
|
Log.v(DatapointHelper.LOG_PREFIX, "Parameter name exceeds "
|
|
|
|
|
Log.v(DatapointHelper.LOG_PREFIX, "Parameter name exceeds "
|
|
|
|
|
+ LocalyticsSession.MAX_NAME_LENGTH + " character limit. Truncating.");
|
|
|
|
|
paramName = paramName.substring(0, LocalyticsSession.MAX_NAME_LENGTH);
|
|
|
|
|
}
|
|
|
|
|
if (paramValue.length() > LocalyticsSession.MAX_NAME_LENGTH)
|
|
|
|
|
{
|
|
|
|
|
Log.v(DatapointHelper.LOG_PREFIX, "Parameter value exceeds "
|
|
|
|
|
Log.v(DatapointHelper.LOG_PREFIX, "Parameter value exceeds "
|
|
|
|
|
+ LocalyticsSession.MAX_NAME_LENGTH + " character limit. Truncating.");
|
|
|
|
|
paramValue = paramValue.substring(0, LocalyticsSession.MAX_NAME_LENGTH);
|
|
|
|
|
}
|
|
|
|
|
@ -178,7 +179,7 @@ public final class DatapointHelper
|
|
|
|
|
formattedString.append(escapeString(paramValue));
|
|
|
|
|
|
|
|
|
|
formattedString.append("\n");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return formattedString.toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -187,30 +188,30 @@ public final class DatapointHelper
|
|
|
|
|
* one way hash and cannot be used to determine what device this data came from.
|
|
|
|
|
* @param appContext The context used to access the settings resolver
|
|
|
|
|
* @return An 1-way hashed identifier unique to this device or null if an ID, or the hashing
|
|
|
|
|
* algorithm is not available.
|
|
|
|
|
* algorithm is not available.
|
|
|
|
|
*/
|
|
|
|
|
public static String getGlobalDeviceId(final Context appContext)
|
|
|
|
|
{
|
|
|
|
|
String systemId = System.getString(appContext.getContentResolver(), System.ANDROID_ID);
|
|
|
|
|
if(systemId == null || systemId.toLowerCase().equals(DROID2_ANDROID_ID))
|
|
|
|
|
{
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
MessageDigest md = MessageDigest.getInstance("SHA-256");
|
|
|
|
|
byte[] digest = md.digest(systemId.getBytes());
|
|
|
|
|
BigInteger hashedNumber = new BigInteger(1, digest);
|
|
|
|
|
return new String(hashedNumber.toString(16));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch(NoSuchAlgorithmException e)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Determines the type of network this device is connected to.
|
|
|
|
|
* @param appContext the context used to access the device's WIFI
|
|
|
|
|
@ -219,10 +220,10 @@ public final class DatapointHelper
|
|
|
|
|
*/
|
|
|
|
|
public static String getNetworkType(
|
|
|
|
|
final Context appContext,
|
|
|
|
|
TelephonyManager telephonyManager)
|
|
|
|
|
{
|
|
|
|
|
WifiManager wifiManager = (WifiManager)appContext.getSystemService(Context.WIFI_SERVICE);
|
|
|
|
|
|
|
|
|
|
TelephonyManager telephonyManager)
|
|
|
|
|
{
|
|
|
|
|
WifiManager wifiManager = (WifiManager)appContext.getSystemService(Context.WIFI_SERVICE);
|
|
|
|
|
|
|
|
|
|
// this will only work for apps which already have wifi permissions.
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
@ -230,29 +231,29 @@ public final class DatapointHelper
|
|
|
|
|
{
|
|
|
|
|
return "wifi";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e) { }
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e) { /**/ }
|
|
|
|
|
|
|
|
|
|
switch (telephonyManager.getNetworkType())
|
|
|
|
|
{
|
|
|
|
|
{
|
|
|
|
|
case TelephonyManager.NETWORK_TYPE_EDGE : return "edge";
|
|
|
|
|
case TelephonyManager.NETWORK_TYPE_GPRS : return "GPRS";
|
|
|
|
|
case TelephonyManager.NETWORK_TYPE_UMTS : return "UMTS";
|
|
|
|
|
case TelephonyManager.NETWORK_TYPE_UNKNOWN : return "unknown";
|
|
|
|
|
case TelephonyManager.NETWORK_TYPE_UNKNOWN : return "unknown";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return "none";
|
|
|
|
|
|
|
|
|
|
return "none";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets the pretty string for this application's version.
|
|
|
|
|
* @param appContext The context used to examine packages
|
|
|
|
|
* @return The application's version as a pretty string
|
|
|
|
|
*/
|
|
|
|
|
public static String getAppVersion(final Context appContext)
|
|
|
|
|
public static String getAppVersion(final Context appContext)
|
|
|
|
|
{
|
|
|
|
|
PackageManager pm = appContext.getPackageManager();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return pm.getPackageInfo(appContext.getPackageName(), 0).versionName;
|
|
|
|
|
@ -262,9 +263,9 @@ public final class DatapointHelper
|
|
|
|
|
return "unknown";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets the current time, along with local timezone, formatted as a DateTime for the webservice.
|
|
|
|
|
* Gets the current time, along with local timezone, formatted as a DateTime for the webservice.
|
|
|
|
|
* @return a DateTime of the current local time and timezone.
|
|
|
|
|
*/
|
|
|
|
|
public static String getTimeAsDatetime()
|
|
|
|
|
@ -289,7 +290,7 @@ public final class DatapointHelper
|
|
|
|
|
|
|
|
|
|
int startRead = 0; // Index to start reading at
|
|
|
|
|
int stopRead = 0; // Index characters get read from and where the substring ends
|
|
|
|
|
int bufferLength = rawString.length();
|
|
|
|
|
int bufferLength = rawString == null ? 0 : rawString.length();
|
|
|
|
|
|
|
|
|
|
if (rawString == null)
|
|
|
|
|
{
|
|
|
|
|
|