[TASK] Initial commit with basic product setup

This commit is contained in:
2019-08-18 13:50:14 +02:00
commit 01a66a8e1f
2548 changed files with 167528 additions and 0 deletions

View File

@@ -0,0 +1,9 @@
namespace Mapbox.Unity.Telemetry
{
public interface ITelemetryLibrary
{
void Initialize(string accessToken);
void SendTurnstile();
void SetLocationCollectionState(bool enable);
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 60254f905810e483288c61f416978e74
timeCreated: 1495739868
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,86 @@
#if UNITY_ANDROID
namespace Mapbox.Unity.Telemetry
{
using UnityEngine;
public class TelemetryAndroid : ITelemetryLibrary
{
AndroidJavaObject _activityContext = null;
AndroidJavaObject _telemInstance = null;
static ITelemetryLibrary _instance = new TelemetryAndroid();
public static ITelemetryLibrary Instance
{
get
{
return _instance;
}
}
public void Initialize(string accessToken)
{
if (string.IsNullOrEmpty(accessToken))
{
throw new System.ArgumentNullException("accessToken");
}
using (AndroidJavaClass activityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
_activityContext = activityClass.GetStatic<AndroidJavaObject>("currentActivity");
}
if (null == _activityContext)
{
Debug.LogError("Could not get current activity");
return;
}
using (AndroidJavaClass MapboxAndroidTelem = new AndroidJavaClass("com.mapbox.services.android.telemetry.MapboxTelemetry"))
{
if (null == MapboxAndroidTelem)
{
Debug.LogError("Could not get class 'MapboxTelemetry'");
return;
}
_telemInstance = MapboxAndroidTelem.CallStatic<AndroidJavaObject>("getInstance");
if (null == _telemInstance)
{
Debug.LogError("Could not get MapboxTelemetry instance");
return;
}
_telemInstance.Call(
"initialize"
, _activityContext
, accessToken
, "MapboxEventsUnityAndroid/" + Constants.SDK_VERSION
);
}
}
public void SendTurnstile()
{
using (AndroidJavaClass MapboxAndroidEvent = new AndroidJavaClass("com.mapbox.services.android.telemetry.MapboxEvent"))
{
if (null == MapboxAndroidEvent)
{
Debug.LogError("Could not get class 'MapboxEvent'");
return;
}
AndroidJavaObject mapLoadEvent = MapboxAndroidEvent.CallStatic<AndroidJavaObject>("buildMapLoadEvent");
_telemInstance.Call("pushEvent", mapLoadEvent);
}
}
public void SetLocationCollectionState(bool enable)
{
_telemInstance.Call(
"setTelemetryEnabled"
, enable
);
}
}
}
#endif

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 482b4af485a1148558ccb578af5ed81f
timeCreated: 1495731343
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,29 @@
namespace Mapbox.Unity.Telemetry
{
public class TelemetryDummy : ITelemetryLibrary
{
static ITelemetryLibrary _instance = new TelemetryDummy();
public static ITelemetryLibrary Instance
{
get
{
return _instance;
}
}
public void Initialize(string accessToken)
{
// empty.
}
public void SendTurnstile()
{
// empty.
}
public void SetLocationCollectionState(bool enable)
{
// empty.
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 5cf9f00b4c350437ea7ef15fa73733de
timeCreated: 1495739868
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,118 @@
#if UNITY_EDITOR
namespace Mapbox.Unity.Telemetry
{
using System.Collections.Generic;
using System.Collections;
using Mapbox.Json;
using System;
using Mapbox.Unity.Utilities;
using UnityEngine;
using System.Text;
using UnityEditor;
public class TelemetryEditor : ITelemetryLibrary
{
string _url;
static ITelemetryLibrary _instance = new TelemetryEditor();
public static ITelemetryLibrary Instance
{
get
{
return _instance;
}
}
public void Initialize(string accessToken)
{
_url = string.Format("{0}events/v2?access_token={1}", Mapbox.Utils.Constants.EventsAPI, accessToken);
}
public void SendTurnstile()
{
// This is only needed for maps at design-time.
//Runnable.EnableRunnableInEditor();
var ticks = DateTime.Now.Ticks;
if (ShouldPostTurnstile(ticks))
{
Runnable.Run(PostWWW(_url, GetPostBody()));
}
}
string GetPostBody()
{
List<Dictionary<string, object>> eventList = new List<Dictionary<string, object>>();
Dictionary<string, object> jsonDict = new Dictionary<string, object>();
long unixTimestamp = (long)Mapbox.Utils.UnixTimestampUtils.To(DateTime.UtcNow);
jsonDict.Add("event", "appUserTurnstile");
jsonDict.Add("created", unixTimestamp);
jsonDict.Add("userId", SystemInfo.deviceUniqueIdentifier);
jsonDict.Add("enabled.telemetry", false);
eventList.Add(jsonDict);
var jsonString = JsonConvert.SerializeObject(eventList);
return jsonString;
}
bool ShouldPostTurnstile(long ticks)
{
var date = new DateTime(ticks);
var longAgo = DateTime.Now.AddDays(-100).Ticks.ToString();
var lastDateString = PlayerPrefs.GetString(Constants.Path.TELEMETRY_TURNSTILE_LAST_TICKS_EDITOR_KEY, longAgo);
long lastTicks = 0;
long.TryParse(lastDateString, out lastTicks);
var lastDate = new DateTime(lastTicks);
var timeSpan = date - lastDate;
return timeSpan.Days >= 1;
}
IEnumerator PostWWW(string url, string bodyJsonString)
{
byte[] bodyRaw = Encoding.UTF8.GetBytes(bodyJsonString);
var headers = new Dictionary<string, string>();
headers.Add("Content-Type", "application/json");
headers.Add("user-agent", GetUserAgent());
var www = new WWW(url, bodyRaw, headers);
yield return www;
while (!www.isDone) { yield return null; }
// www doesn't expose HTTP status code, relay on 'error' property
if (!string.IsNullOrEmpty(www.error))
{
PlayerPrefs.SetString(Constants.Path.TELEMETRY_TURNSTILE_LAST_TICKS_EDITOR_KEY, "0");
}
else
{
PlayerPrefs.SetString(Constants.Path.TELEMETRY_TURNSTILE_LAST_TICKS_EDITOR_KEY, DateTime.Now.Ticks.ToString());
}
}
static string GetUserAgent()
{
var userAgent = string.Format(
"{0}/{1}/{2} MapboxEventsUnityEditor/{3}",
PlayerSettings.applicationIdentifier,
PlayerSettings.bundleVersion,
#if UNITY_IOS
PlayerSettings.iOS.buildNumber,
#elif UNITY_ANDROID
PlayerSettings.Android.bundleVersionCode,
#else
"0",
#endif
Constants.SDK_VERSION
);
return userAgent;
}
public void SetLocationCollectionState(bool enable)
{
// Empty.
}
}
}
#endif

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 27d433fd51a8f43dfb24c4797efe3304
timeCreated: 1499712270
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,26 @@
namespace Mapbox.Unity.Telemetry
{
public static class TelemetryFactory
{
#if UNITY_EDITOR || UNITY_IOS || UNITY_ANDROID
public static readonly string EventQuery = "events=true";
#else
public static readonly string EventQuery = "events=false";
#endif
public static ITelemetryLibrary GetTelemetryInstance()
{
#if UNITY_EDITOR
return TelemetryEditor.Instance;
#elif UNITY_IOS
return TelemetryIos.Instance;
#elif UNITY_ANDROID
return TelemetryAndroid.Instance;
#elif UNITY_WEBGL
return TelemetryWebgl.Instance;
#else
return TelemetryFallback.Instance;
#endif
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 22b1b1677ad464ff499ec366ca41ad2b
timeCreated: 1511971959
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,96 @@
namespace Mapbox.Unity.Telemetry
{
using System.Collections.Generic;
using System.Collections;
using Mapbox.Json;
using System;
using Mapbox.Unity.Utilities;
using UnityEngine;
using System.Text;
public class TelemetryFallback : ITelemetryLibrary
{
string _url;
static ITelemetryLibrary _instance = new TelemetryFallback();
public static ITelemetryLibrary Instance
{
get
{
return _instance;
}
}
public void Initialize(string accessToken)
{
_url = string.Format("{0}events/v2?access_token={1}", Mapbox.Utils.Constants.EventsAPI, accessToken);
}
public void SendTurnstile()
{
var ticks = DateTime.Now.Ticks;
if (ShouldPostTurnstile(ticks))
{
Runnable.Run(PostWWW(_url, GetPostBody()));
PlayerPrefs.SetString(Constants.Path.TELEMETRY_TURNSTILE_LAST_TICKS_FALLBACK_KEY, ticks.ToString());
}
}
string GetPostBody()
{
List<Dictionary<string, object>> eventList = new List<Dictionary<string, object>>();
Dictionary<string, object> jsonDict = new Dictionary<string, object>();
long unixTimestamp = (long)Mapbox.Utils.UnixTimestampUtils.To(DateTime.UtcNow);
jsonDict.Add("event", "appUserTurnstile");
jsonDict.Add("created", unixTimestamp);
jsonDict.Add("userId", SystemInfo.deviceUniqueIdentifier);
jsonDict.Add("enabled.telemetry", false);
eventList.Add(jsonDict);
var jsonString = JsonConvert.SerializeObject(eventList);
return jsonString;
}
bool ShouldPostTurnstile(long ticks)
{
var date = new DateTime(ticks);
var longAgo = DateTime.Now.AddDays(-100).Ticks.ToString();
var lastDateString = PlayerPrefs.GetString(Constants.Path.TELEMETRY_TURNSTILE_LAST_TICKS_FALLBACK_KEY, longAgo);
long lastTicks = 0;
long.TryParse(lastDateString, out lastTicks);
var lastDate = new DateTime(lastTicks);
var timeSpan = date - lastDate;
return timeSpan.Days >= 1;
}
IEnumerator PostWWW(string url, string bodyJsonString)
{
byte[] bodyRaw = Encoding.UTF8.GetBytes(bodyJsonString);
var headers = new Dictionary<string, string>();
headers.Add("Content-Type", "application/json");
headers.Add("user-agent", GetUserAgent());
var www = new WWW(url, bodyRaw, headers);
yield return www;
}
static string GetUserAgent()
{
var userAgent = string.Format("{0}/{1}/{2} MapboxEventsUnity{3}/{4}",
Application.identifier,
Application.version,
"0",
Application.platform,
Constants.SDK_VERSION
);
return userAgent;
}
public void SetLocationCollectionState(bool enable)
{
// empty.
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 30faf59456fb74b29a054e8684302e5e
timeCreated: 1499713323
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,42 @@
#if UNITY_IOS
namespace Mapbox.Unity.Telemetry
{
using System.Runtime.InteropServices;
public class TelemetryIos : ITelemetryLibrary
{
[DllImport("__Internal")]
private static extern void initialize(string accessToken, string userAgentBase, string hostSDKVersion);
[DllImport("__Internal")]
static extern void sendTurnstileEvent();
[DllImport("__Internal")]
private static extern void setLocationCollectionState(bool enable);
static ITelemetryLibrary _instance = new TelemetryIos();
public static ITelemetryLibrary Instance
{
get
{
return _instance;
}
}
public void Initialize(string accessToken)
{
initialize(accessToken, "MapboxEventsUnityiOS", Constants.SDK_VERSION);
}
public void SendTurnstile()
{
sendTurnstileEvent();
}
public void SetLocationCollectionState(bool enable)
{
setLocationCollectionState(enable);
}
}
}
#endif

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 1089e784167a94312a00eacedff66aa7
timeCreated: 1495731331
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,100 @@
namespace Mapbox.Unity.Telemetry
{
using System.Collections.Generic;
using System.Collections;
using Mapbox.Json;
using System;
using Mapbox.Unity.Utilities;
using UnityEngine;
using System.Text;
public class TelemetryWebgl : ITelemetryLibrary
{
string _url;
static ITelemetryLibrary _instance = new TelemetryFallback();
public static ITelemetryLibrary Instance
{
get
{
return _instance;
}
}
public void Initialize(string accessToken)
{
_url = string.Format("{0}events/v2?access_token={1}", Mapbox.Utils.Constants.EventsAPI, accessToken);
}
public void SendTurnstile()
{
var ticks = DateTime.Now.Ticks;
if (ShouldPostTurnstile(ticks))
{
Runnable.Run(PostWWW(_url, GetPostBody()));
PlayerPrefs.SetString(Constants.Path.TELEMETRY_TURNSTILE_LAST_TICKS_FALLBACK_KEY, ticks.ToString());
}
}
string GetPostBody()
{
List<Dictionary<string, object>> eventList = new List<Dictionary<string, object>>();
Dictionary<string, object> jsonDict = new Dictionary<string, object>();
long unixTimestamp = (long)Mapbox.Utils.UnixTimestampUtils.To(DateTime.UtcNow);
jsonDict.Add("event", "appUserTurnstile");
jsonDict.Add("created", unixTimestamp);
jsonDict.Add("userId", SystemInfo.deviceUniqueIdentifier);
jsonDict.Add("enabled.telemetry", false);
// user-agent cannot be set from web broswer, so we send in payload, instead!
jsonDict.Add("userAgent", GetUserAgent());
eventList.Add(jsonDict);
var jsonString = JsonConvert.SerializeObject(eventList);
return jsonString;
}
bool ShouldPostTurnstile(long ticks)
{
var date = new DateTime(ticks);
var longAgo = DateTime.Now.AddDays(-100).Ticks.ToString();
var lastDateString = PlayerPrefs.GetString(Constants.Path.TELEMETRY_TURNSTILE_LAST_TICKS_FALLBACK_KEY, longAgo);
long lastTicks = 0;
long.TryParse(lastDateString, out lastTicks);
var lastDate = new DateTime(lastTicks);
var timeSpan = date - lastDate;
return timeSpan.Days >= 1;
}
IEnumerator PostWWW(string url, string bodyJsonString)
{
byte[] bodyRaw = Encoding.UTF8.GetBytes(bodyJsonString);
var headers = new Dictionary<string, string>();
headers.Add("Content-Type", "application/json");
var www = new WWW(url, bodyRaw, headers);
yield return www;
}
static string GetUserAgent()
{
var userAgent = string.Format("{0}/{1}/{2} MapboxEventsUnity{3}/{4}",
Application.identifier,
Application.version,
"0",
Application.platform,
Constants.SDK_VERSION
);
return userAgent;
}
public void SetLocationCollectionState(bool enable)
{
// empty.
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: cd7351c4625584230b9b6fbe75e801ca
timeCreated: 1511971959
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: