From 2740af53d860ec062df06e58f598103a6977a54a Mon Sep 17 00:00:00 2001 From: "r.kuznetsov" Date: Thu, 21 Jul 2016 16:42:09 +0300 Subject: [PATCH] Added tags to PushWoosh --- .../jni/com/mapswithme/platform/Platform.cpp | 31 +++- .../jni/com/mapswithme/platform/Platform.hpp | 5 +- .../com/mapswithme/maps/MwmApplication.java | 20 +++ .../maps/routing/RoutingController.java | 3 +- .../util/statistics/PushwooshHelper.java | 156 ++++++++++++++++++ .../util/statistics/Statistics.java | 11 +- platform/platform.hpp | 4 + platform/platform_android.cpp | 6 + platform/platform_ios.mm | 15 ++ platform/platform_mac.mm | 12 ++ platform/platform_qt.cpp | 12 ++ platform/platform_tizen.cpp | 16 ++ platform/platform_win.cpp | 12 ++ storage/downloading_policy.cpp | 2 +- 14 files changed, 297 insertions(+), 8 deletions(-) create mode 100644 android/src/com/mapswithme/util/statistics/PushwooshHelper.java diff --git a/android/jni/com/mapswithme/platform/Platform.cpp b/android/jni/com/mapswithme/platform/Platform.cpp index 6b0b1d76ce..56c61df873 100644 --- a/android/jni/com/mapswithme/platform/Platform.cpp +++ b/android/jni/com/mapswithme/platform/Platform.cpp @@ -69,6 +69,21 @@ void Platform::RunOnGuiThread(TFunctor const & fn) android::Platform::Instance().RunOnGuiThread(fn); } +void Platform::SendPushWooshTag(string const & tag) +{ + SendPushWooshTag(tag, vector{ "1" }); +} + +void Platform::SendPushWooshTag(string const & tag, string const & value) +{ + SendPushWooshTag(tag, vector{ value }); +} + +void Platform::SendPushWooshTag(string const & tag, vector const & values) +{ + android::Platform::Instance().SendPushWooshTag(tag, values); +} + Platform::EConnectionType Platform::ConnectionStatus() { JNIEnv * env = jni::GetEnv(); @@ -94,6 +109,7 @@ namespace android m_functorProcessObject = env->NewGlobalRef(functorProcessObject); jclass const functorProcessClass = env->GetObjectClass(functorProcessObject); m_functorProcessMethod = env->GetMethodID(functorProcessClass, "forwardToMainThread", "(J)V"); + m_sendPushWooshTagsMethod = env->GetMethodID(functorProcessClass, "sendPushWooshTags", "(Ljava/lang/String;[Ljava/lang/String;)V"); string const flavor = jni::ToNativeString(env, flavorName); string const build = jni::ToNativeString(env, buildType); @@ -183,7 +199,20 @@ namespace android TFunctor * functor = new TFunctor(fn); jni::GetEnv()->CallVoidMethod(m_functorProcessObject, m_functorProcessMethod, reinterpret_cast(functor)); } -} + + void Platform::SendPushWooshTag(string const & tag, vector const & values) + { + if (values.empty()) + return; + + JNIEnv * env = jni::GetEnv(); + if (env == nullptr) + return; + + env->CallVoidMethod(m_functorProcessObject, m_sendPushWooshTagsMethod, jni::ToJavaString(env, tag), + jni::TScopedLocalObjectArrayRef(env, jni::ToJavaStringArray(env, values)).get()); + } +} // namespace android Platform & GetPlatform() { diff --git a/android/jni/com/mapswithme/platform/Platform.hpp b/android/jni/com/mapswithme/platform/Platform.hpp index 2b11f6c9a5..f47bf1645f 100644 --- a/android/jni/com/mapswithme/platform/Platform.hpp +++ b/android/jni/com/mapswithme/platform/Platform.hpp @@ -29,10 +29,13 @@ namespace android bool HasAvailableSpaceForWriting(uint64_t size) const; void RunOnGuiThread(TFunctor const & fn); + void SendPushWooshTag(string const & tag, vector const & values); + static Platform & Instance(); private: jobject m_functorProcessObject; jmethodID m_functorProcessMethod; + jmethodID m_sendPushWooshTagsMethod; }; -} +} // namespace android diff --git a/android/src/com/mapswithme/maps/MwmApplication.java b/android/src/com/mapswithme/maps/MwmApplication.java index 24eff6cbd8..3440484701 100644 --- a/android/src/com/mapswithme/maps/MwmApplication.java +++ b/android/src/com/mapswithme/maps/MwmApplication.java @@ -30,6 +30,7 @@ import com.mapswithme.util.Config; import com.mapswithme.util.Constants; import com.mapswithme.util.ThemeSwitcher; import com.mapswithme.util.UiUtils; +import com.mapswithme.util.statistics.PushwooshHelper; import com.mapswithme.util.statistics.Statistics; import com.pushwoosh.PushManager; import io.fabric.sdk.android.Fabric; @@ -253,6 +254,25 @@ public class MwmApplication extends Application pushManager.onStartup(this); pushManager.registerForPushNotifications(); pushManager.startTrackingGeoPushes(); + + PushwooshHelper.get().setContext(this); + PushwooshHelper.get().synchronize(); + } + catch(Exception e) + { + Log.e("Pushwoosh", e.getLocalizedMessage()); + } + } + + @SuppressWarnings("unused") + void sendPushWooshTags(String tag, String[] values) + { + try + { + if (values.length == 1) + PushwooshHelper.get().sendTag(tag, values[0]); + else + PushwooshHelper.get().sendTag(tag, values); } catch(Exception e) { diff --git a/android/src/com/mapswithme/maps/routing/RoutingController.java b/android/src/com/mapswithme/maps/routing/RoutingController.java index e0b0cdeccb..63933e1b26 100644 --- a/android/src/com/mapswithme/maps/routing/RoutingController.java +++ b/android/src/com/mapswithme/maps/routing/RoutingController.java @@ -247,10 +247,9 @@ public class RoutingController setBuildState(BuildState.BUILDING); updatePlan(); - Statistics.INSTANCE.trackRouteBuild(Statistics.getPointType(mStartPoint), Statistics.getPointType(mEndPoint)); + Statistics.INSTANCE.trackRouteBuild(mLastRouterType, mStartPoint, mEndPoint); org.alohalytics.Statistics.logEvent(AlohaHelper.ROUTING_BUILD, new String[] {Statistics.EventParam.FROM, Statistics.getPointType(mStartPoint), Statistics.EventParam.TO, Statistics.getPointType(mEndPoint)}); - Framework.nativeBuildRoute(mStartPoint.getLat(), mStartPoint.getLon(), mEndPoint.getLat(), mEndPoint.getLon()); } diff --git a/android/src/com/mapswithme/util/statistics/PushwooshHelper.java b/android/src/com/mapswithme/util/statistics/PushwooshHelper.java new file mode 100644 index 0000000000..55cbb9a980 --- /dev/null +++ b/android/src/com/mapswithme/util/statistics/PushwooshHelper.java @@ -0,0 +1,156 @@ +package com.mapswithme.util.statistics; + +import android.content.Context; +import android.os.AsyncTask; +import android.os.Handler; +import android.os.Looper; +import android.util.Log; + +import com.mapswithme.maps.Framework; +import com.pushwoosh.PushManager; +import com.pushwoosh.SendPushTagsCallBack; + +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +public final class PushwooshHelper implements SendPushTagsCallBack +{ + public final static String ADD_MAP_OBJECT = "editor_add_discovered"; + public final static String EDIT_MAP_OBJECT = "editor_edit_discovered"; + + private static final PushwooshHelper sInstance = new PushwooshHelper(); + + private Context mContext; + private final Object mSyncObject = new Object(); + private AsyncTask mTask; + private List> mTagsQueue = new LinkedList<>(); + + private PushwooshHelper() {} + + public static PushwooshHelper get() { return sInstance; } + + public void setContext(Context context) + { + synchronized (mSyncObject) + { + mContext = context; + } + } + + public void synchronize() + { + sendTags(null); + } + + public void sendTag(String tag) + { + sendTag(tag, "1"); + } + + public void sendTag(String tag, Object value) + { + Map tags = new HashMap<>(); + tags.put(tag, value); + sendTags(tags); + } + + public void sendTags(Map tags) + { + synchronized (mSyncObject) + { + if (!canSendTags()) + { + mTagsQueue.add(tags); + return; + } + + final Map tagsToSend = new HashMap<>(); + for (Map t: mTagsQueue) + { + if (t != null) + tagsToSend.putAll(t); + } + if (tags != null) + tagsToSend.putAll(tags); + + mTagsQueue.clear(); + + if (tagsToSend.isEmpty()) + return; + + mTask = new AsyncTask() + { + @Override + protected Void doInBackground(Void... params) + { + PushManager.sendTags(mContext, tagsToSend, PushwooshHelper.this); + return null; + } + }; + mTask.execute((Void) null); + } + } + + @Override + public void taskStarted() {} + + @Override + public void onSentTagsSuccess(Map map) + { + new Handler(Looper.getMainLooper()).post(new Runnable() + { + @Override + public void run() + { + synchronized (mSyncObject) + { + mTask = null; + } + } + }); + } + + @Override + public void onSentTagsError(final Exception e) + { + new Handler(Looper.getMainLooper()).post(new Runnable() + { + @Override + public void run() + { + synchronized (mSyncObject) + { + if (e != null) + Log.e("Pushwoosh", e.getLocalizedMessage()); + mTask = null; + } + } + }); + } + + private boolean canSendTags() + { + return mContext != null && mTask == null; + } + + public static String getRoutingTag(int routerType, boolean isP2P) + { + String result = "routing_"; + if (isP2P) + result += "p2p_"; + + if (routerType == Framework.ROUTER_TYPE_VEHICLE) + result += "vehicle"; + else if (routerType == Framework.ROUTER_TYPE_PEDESTRIAN) + result += "pedestrian"; + else if (routerType == Framework.ROUTER_TYPE_BICYCLE) + result += "bicycle"; + else + result += "unknown"; + + result += "_discovered"; + return result; + } +} diff --git a/android/src/com/mapswithme/util/statistics/Statistics.java b/android/src/com/mapswithme/util/statistics/Statistics.java index 4f0d736d73..0bfca87bc0 100644 --- a/android/src/com/mapswithme/util/statistics/Statistics.java +++ b/android/src/com/mapswithme/util/statistics/Statistics.java @@ -380,10 +380,13 @@ public enum Statistics } } - public void trackRouteBuild(String from, String to) + public void trackRouteBuild(int routerType, MapObject from, MapObject to) { - trackEvent(EventName.ROUTING_BUILD, params().add(EventParam.FROM, from) - .add(EventParam.TO, to)); + trackEvent(EventName.ROUTING_BUILD, params().add(EventParam.FROM, Statistics.getPointType(from)) + .add(EventParam.TO, Statistics.getPointType(to))); + + boolean isP2P = !MapObject.isOfType(MapObject.MY_POSITION, from) && !MapObject.isOfType(MapObject.MY_POSITION, to); + PushwooshHelper.get().sendTag(PushwooshHelper.getRoutingTag(routerType, isP2P)); } public void trackEditorLaunch(boolean newObject) @@ -391,6 +394,8 @@ public enum Statistics trackEvent(newObject ? EventName.EDITOR_START_CREATE : EventName.EDITOR_START_EDIT, editorMwmParams().add(EventParam.IS_AUTHENTICATED, String.valueOf(OsmOAuth.isAuthorized())) .add(EventParam.IS_ONLINE, String.valueOf(ConnectionState.isConnected()))); + + PushwooshHelper.get().sendTag(newObject ? PushwooshHelper.ADD_MAP_OBJECT : PushwooshHelper.EDIT_MAP_OBJECT); } public void trackEditorSuccess(boolean newObject) diff --git a/platform/platform.hpp b/platform/platform.hpp index a8247f799b..b654c3e75c 100644 --- a/platform/platform.hpp +++ b/platform/platform.hpp @@ -224,6 +224,10 @@ public: void SetupMeasurementSystem() const; + void SendPushWooshTag(string const & tag); + void SendPushWooshTag(string const & tag, string const & value); + void SendPushWooshTag(string const & tag, vector const & values); + private: void GetSystemFontNames(FilesList & res) const; }; diff --git a/platform/platform_android.cpp b/platform/platform_android.cpp index 758cfc1d7f..3613a8fc5b 100644 --- a/platform/platform_android.cpp +++ b/platform/platform_android.cpp @@ -260,6 +260,12 @@ void Platform::SetupMeasurementSystem() const settings::Set(settings::kMeasurementUnits, units); } +/// @see implementation of methods below in android/jni/com/.../Platform.cpp +//void Platform::RunOnGuiThread(TFunctor const & fn){} +//void Platform::SendPushWooshTag(string const & tag){} +//void Platform::SendPushWooshTag(string const & tag, string const & value){} +//void Platform::SendPushWooshTag(string const & tag, vector const & values){} + namespace { class FunctorWrapper : public threads::IRoutine diff --git a/platform/platform_ios.mm b/platform/platform_ios.mm index 24505604b1..babe36072a 100644 --- a/platform/platform_ios.mm +++ b/platform/platform_ios.mm @@ -271,6 +271,21 @@ void Platform::SetupMeasurementSystem() const settings::Set(settings::kMeasurementUnits, units); } +void Platform::SendPushWooshTag(string const & tag) +{ + SendPushWooshTag(tag, vector{ "1" }); +} + +void Platform::SendPushWooshTag(string const & tag, string const & value) +{ + SendPushWooshTag(tag, vector{ value }); +} + +void Platform::SendPushWooshTag(string const & tag, vector const & values) +{ + //TODO: implement +} + //////////////////////////////////////////////////////////////////////// extern Platform & GetPlatform() { diff --git a/platform/platform_mac.mm b/platform/platform_mac.mm index 6267c9aa34..48ca2925ae 100644 --- a/platform/platform_mac.mm +++ b/platform/platform_mac.mm @@ -139,3 +139,15 @@ Platform::EConnectionType Platform::ConnectionStatus() return EConnectionType::CONNECTION_NONE; return EConnectionType::CONNECTION_WIFI; } + +void Platform::SendPushWooshTag(string const & tag) +{ +} + +void Platform::SendPushWooshTag(string const & tag, string const & value) +{ +} + +void Platform::SendPushWooshTag(string const & tag, vector const & values) +{ +} diff --git a/platform/platform_qt.cpp b/platform/platform_qt.cpp index dd78b7c4d4..c951310ae9 100644 --- a/platform/platform_qt.cpp +++ b/platform/platform_qt.cpp @@ -95,6 +95,18 @@ void Platform::RunAsync(TFunctor const & fn, Priority p) { async(fn); } + +void Platform::SendPushWooshTag(string const & tag) +{ +} + +void Platform::SendPushWooshTag(string const & tag, string const & value) +{ +} + +void Platform::SendPushWooshTag(string const & tag, vector const & values) +{ +} #endif // defined(OMIM_OS_LINUX) extern Platform & GetPlatform() diff --git a/platform/platform_tizen.cpp b/platform/platform_tizen.cpp index 58ea92257d..43313a8ebd 100644 --- a/platform/platform_tizen.cpp +++ b/platform/platform_tizen.cpp @@ -105,6 +105,22 @@ Platform::EConnectionType Platform::ConnectionStatus() return EConnectionType::CONNECTION_NONE; } +void Platform::GetSystemFontNames(FilesList & res) const +{ +} + +void Platform::SendPushWooshTag(string const & tag) +{ +} + +void Platform::SendPushWooshTag(string const & tag, string const & value) +{ +} + +void Platform::SendPushWooshTag(string const & tag, vector const & values) +{ +} + extern Platform & GetPlatform() { static Platform platform; diff --git a/platform/platform_win.cpp b/platform/platform_win.cpp index 1bbe9d5df0..b4571f46cf 100644 --- a/platform/platform_win.cpp +++ b/platform/platform_win.cpp @@ -180,3 +180,15 @@ bool Platform::GetFileSizeByFullPath(string const & filePath, uint64_t & size) void Platform::GetSystemFontNames(FilesList & res) const { } + +void Platform::SendPushWooshTag(string const & tag) +{ +} + +void Platform::SendPushWooshTag(string const & tag, string const & value) +{ +} + +void Platform::SendPushWooshTag(string const & tag, vector const & values) +{ +} diff --git a/storage/downloading_policy.cpp b/storage/downloading_policy.cpp index 0d9cc2098d..c5175ed82f 100644 --- a/storage/downloading_policy.cpp +++ b/storage/downloading_policy.cpp @@ -19,7 +19,7 @@ void StorageDownloadingPolicy::ScheduleRetry(storage::TCountriesSet const & fail --m_autoRetryCounter; func(failedCountries); }; - m_autoRetryWorker.RestartWith([action]{ Platform().RunOnGuiThread(action); }); + m_autoRetryWorker.RestartWith([action]{ GetPlatform().RunOnGuiThread(action); }); } else {