From 64a3ad3b16ba52c7bbfa503ff4b0fee62e6bf7ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B0=D0=BD=D0=B4=D1=80=20?= =?UTF-8?q?=D0=97=D0=B0=D1=86=D0=B5=D0=BF=D0=B8=D0=BD?= Date: Mon, 9 Apr 2018 11:28:41 +0300 Subject: [PATCH] [android] Integrated libnotify library --- android/AndroidManifest.xml | 19 +++-- android/build.gradle | 15 ++++ android/flavors/beta/res/values/libnotify.xml | 27 +++++++ .../flavors/debug/res/values/libnotify.xml | 27 +++++++ android/res/values/libnotify.xml | 27 +++++++ .../com/mapswithme/maps/MwmApplication.java | 20 +++++ .../maps/search/SearchFragment.java | 7 ++ .../util/log/LibnotifyLogReceiver.java | 45 +++++++++++ .../mapswithme/util/log/LoggerFactory.java | 8 +- .../util/push/GCMListenerRouterService.java | 75 +++++++++++++++++++ .../GcmInstanceIDRouterListenerService.java | 24 ++++++ .../util/statistics/PushwooshHelper.java | 4 + 12 files changed, 290 insertions(+), 8 deletions(-) create mode 100644 android/flavors/beta/res/values/libnotify.xml create mode 100644 android/flavors/debug/res/values/libnotify.xml create mode 100644 android/res/values/libnotify.xml create mode 100644 android/src/com/mapswithme/util/log/LibnotifyLogReceiver.java create mode 100644 android/src/com/mapswithme/util/push/GCMListenerRouterService.java create mode 100644 android/src/com/mapswithme/util/push/GcmInstanceIDRouterListenerService.java diff --git a/android/AndroidManifest.xml b/android/AndroidManifest.xml index 795a683e4b..2f89c9d509 100644 --- a/android/AndroidManifest.xml +++ b/android/AndroidManifest.xml @@ -474,17 +474,21 @@ + - - + android:name="com.mapswithme.util.push.GCMListenerRouterService" + android:enabled="true" + android:exported="false" > + + + android:name="com.mapswithme.util.push.GcmInstanceIDRouterListenerService" + android:enabled="true" + android:exported="false"> @@ -496,7 +500,8 @@ - + + diff --git a/android/build.gradle b/android/build.gradle index d7d3f08dd7..12f3ec82a0 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -28,6 +28,9 @@ repositories { maven { url 'https://s3.amazonaws.com/moat-sdk-builds' } + maven { + url 'https://dl.bintray.com/libverify/android' + } } apply plugin: 'com.android.application' @@ -89,6 +92,18 @@ dependencies { implementation 'net.jcip:jcip-annotations:1.0' implementation 'com.android.support:multidex:1.0.3' implementation 'com.appsflyer:af-android-sdk:4.8.3' + implementation ("ru.mail:libnotify:0.1.163-notify-support-v96@aar") { + transitive = true; + changing = true; + exclude group: 'com.android.support' + exclude group: 'com.google.android.gms' + } + implementation ("ru.mail:libnotify-debug:0.1.163-notify-support-v96@aar") { + transitive = true; + changing = true; + exclude group: 'com.android.support' + exclude group: 'com.google.android.gms' + } } def getDate() { diff --git a/android/flavors/beta/res/values/libnotify.xml b/android/flavors/beta/res/values/libnotify.xml new file mode 100644 index 0000000000..0fe07311e4 --- /dev/null +++ b/android/flavors/beta/res/values/libnotify.xml @@ -0,0 +1,27 @@ + + + + MapsMeBeta + + Notify secured properties will be used here + + drawable/ic_notification + + color/base_accent + + color/text_dark + diff --git a/android/flavors/debug/res/values/libnotify.xml b/android/flavors/debug/res/values/libnotify.xml new file mode 100644 index 0000000000..7c85764e37 --- /dev/null +++ b/android/flavors/debug/res/values/libnotify.xml @@ -0,0 +1,27 @@ + + + + MapsMeDebug + + Notify secured properties will be used here + + drawable/ic_notification + + color/base_accent + + color/text_dark + diff --git a/android/res/values/libnotify.xml b/android/res/values/libnotify.xml new file mode 100644 index 0000000000..b3e6922031 --- /dev/null +++ b/android/res/values/libnotify.xml @@ -0,0 +1,27 @@ + + + + MapsMe + + Notify secured properties will be used here + + drawable/ic_notification + + color/base_accent + + color/text_dark + diff --git a/android/src/com/mapswithme/maps/MwmApplication.java b/android/src/com/mapswithme/maps/MwmApplication.java index b24dc0a22e..bb005118c5 100644 --- a/android/src/com/mapswithme/maps/MwmApplication.java +++ b/android/src/com/mapswithme/maps/MwmApplication.java @@ -41,6 +41,9 @@ import com.my.tracker.MyTracker; import com.my.tracker.MyTrackerParams; import com.pushwoosh.PushManager; import io.fabric.sdk.android.Fabric; +import ru.mail.libnotify.api.NotificationFactory; +import ru.mail.notify.core.api.BackgroundAwakeMode; +import ru.mail.notify.core.api.NetworkSyncMode; import java.util.List; @@ -165,6 +168,7 @@ public class MwmApplication extends Application private void initCoreIndependentSdks() { initCrashlytics(); + initLibnotify(); initPushWoosh(); initAppsFlyer(); initTracker(); @@ -333,6 +337,22 @@ public class MwmApplication extends Application } } + private void initLibnotify() + { + if (BuildConfig.DEBUG || BuildConfig.BUILD_TYPE.equals("beta")) + { + NotificationFactory.enableDebugMode(); + NotificationFactory.setLogReceiver(LoggerFactory.INSTANCE.createLibnotifyLogger()); + NotificationFactory.setUncaughtExceptionListener((thread, throwable) -> { + Logger l = LoggerFactory.INSTANCE.getLogger(LoggerFactory.Type.THIRD_PARTY); + l.e("LIBNOTIFY", "Thread: " + thread, throwable); + }); + } + NotificationFactory.setNetworkSyncMode(NetworkSyncMode.WIFI_ONLY); + NotificationFactory.setBackgroundAwakeMode(BackgroundAwakeMode.DISABLED); + NotificationFactory.initialize(this); + } + private void initAppsFlyer() { // There is no necessary to use a conversion data listener for a while. diff --git a/android/src/com/mapswithme/maps/search/SearchFragment.java b/android/src/com/mapswithme/maps/search/SearchFragment.java index f52b8a3a4d..d8ce890d48 100644 --- a/android/src/com/mapswithme/maps/search/SearchFragment.java +++ b/android/src/com/mapswithme/maps/search/SearchFragment.java @@ -41,6 +41,7 @@ import com.mapswithme.util.UiUtils; import com.mapswithme.util.Utils; import com.mapswithme.util.concurrency.UiThread; import com.mapswithme.util.statistics.Statistics; +import ru.mail.libnotify.debug.NotifyDebugActivity; import java.util.ArrayList; import java.util.LinkedList; @@ -518,6 +519,12 @@ public class SearchFragment extends BaseMwmFragment return true; } + if (str.equals("?libnotifyId")) + { + startActivity(new Intent(getContext(), NotifyDebugActivity.class)); + return true; + } + return false; } diff --git a/android/src/com/mapswithme/util/log/LibnotifyLogReceiver.java b/android/src/com/mapswithme/util/log/LibnotifyLogReceiver.java new file mode 100644 index 0000000000..a5c3ad2a63 --- /dev/null +++ b/android/src/com/mapswithme/util/log/LibnotifyLogReceiver.java @@ -0,0 +1,45 @@ +package com.mapswithme.util.log; + +import ru.mail.notify.core.utils.LogReceiver; + +class LibnotifyLogReceiver implements LogReceiver +{ + private static final Logger LOGGER + = LoggerFactory.INSTANCE.getLogger(LoggerFactory.Type.THIRD_PARTY); + private static final String TAG = "LIBNOTIFY_"; + @Override + public void v(String tag, String msg) + { + LOGGER.v(TAG + tag, msg); + } + + @Override + public void v(String tag, String msg, Throwable throwable) + { + LOGGER.v(TAG + tag, msg, throwable); + } + + @Override + public void e(String tag, String msg) + { + LOGGER.e(TAG + tag, msg); + } + + @Override + public void e(String tag, String msg, Throwable throwable) + { + LOGGER.e(tag, msg, throwable); + } + + @Override + public void d(String tag, String msg) + { + LOGGER.d(TAG + tag, msg); + } + + @Override + public void d(String tag, String msg, Throwable throwable) + { + LOGGER.d(TAG + tag, msg, throwable); + } +} diff --git a/android/src/com/mapswithme/util/log/LoggerFactory.java b/android/src/com/mapswithme/util/log/LoggerFactory.java index de130074f5..89782bf4b2 100644 --- a/android/src/com/mapswithme/util/log/LoggerFactory.java +++ b/android/src/com/mapswithme/util/log/LoggerFactory.java @@ -12,6 +12,7 @@ import com.mapswithme.maps.R; import com.mapswithme.util.StorageUtils; import net.jcip.annotations.GuardedBy; import net.jcip.annotations.ThreadSafe; +import ru.mail.notify.core.utils.LogReceiver; import java.io.File; import java.util.EnumMap; @@ -24,7 +25,7 @@ public class LoggerFactory public enum Type { MISC, LOCATION, TRAFFIC, GPS_TRACKING, TRACK_RECORDER, ROUTING, NETWORK, STORAGE, DOWNLOADER, - CORE + CORE, THIRD_PARTY } public interface OnZipCompletedListener @@ -135,6 +136,11 @@ public class LoggerFactory return mFileLoggerExecutor; } + @NonNull + public LogReceiver createLibnotifyLogger() + { + return new LibnotifyLogReceiver(); + } // Called from JNI. @SuppressWarnings("unused") private static void logCoreMessage(int level, String msg) diff --git a/android/src/com/mapswithme/util/push/GCMListenerRouterService.java b/android/src/com/mapswithme/util/push/GCMListenerRouterService.java new file mode 100644 index 0000000000..4bd025a03e --- /dev/null +++ b/android/src/com/mapswithme/util/push/GCMListenerRouterService.java @@ -0,0 +1,75 @@ +package com.mapswithme.util.push; + +import android.content.ComponentName; +import android.content.Context; +import android.content.Intent; +import android.content.pm.ApplicationInfo; +import android.content.pm.PackageManager; +import android.os.Bundle; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.text.TextUtils; + +import com.google.android.gms.gcm.GcmListenerService; +import com.google.android.gms.gcm.GcmReceiver; +import com.mapswithme.util.log.Logger; +import com.mapswithme.util.log.LoggerFactory; +import com.pushwoosh.GCMListenerService; +import ru.mail.libnotify.api.NotificationFactory; + +// It's temporary class, it may be deleted along with Pushwoosh sdk. +// The base of this code is taken from https://www.pushwoosh.com/docs/gcm-integration-legacy. +public class GCMListenerRouterService extends GcmListenerService +{ + private static final Logger LOGGER = LoggerFactory.INSTANCE.getLogger(LoggerFactory.Type.THIRD_PARTY); + private static final String TAG = GCMListenerRouterService.class.getSimpleName(); + @Override + public void onMessageReceived(@Nullable String from, @Nullable Bundle data) { + LOGGER.i(TAG, "Gcm router service received message: " + + (data != null ? data.toString() : "") + " from: " + from); + + if (data == null || TextUtils.isEmpty(from)) + return; + + // Base GCM listener service removes this extra before calling onMessageReceived. + // Need to set it again to pass intent to another service. + data.putString("from", from); + + String pwProjectId = getPWProjectId(getApplicationContext()); + if (!TextUtils.isEmpty(pwProjectId) && pwProjectId.contains(from)) { + dispatchMessage(GCMListenerService.class.getName(), data); + return; + } + + NotificationFactory.deliverGcmMessageIntent(this, from, data); + } + + @Nullable + private static String getPWProjectId(@NonNull Context context) + { + PackageManager pMngr = context.getPackageManager(); + try + { + ApplicationInfo ai = pMngr.getApplicationInfo(context.getPackageName(), PackageManager + .GET_META_DATA); + Bundle metaData = ai.metaData; + if (metaData == null) + return null; + return metaData.getString("PW_PROJECT_ID"); + } + catch (PackageManager.NameNotFoundException e) + { + LOGGER.e(TAG, "Failed to get push woosh projectId: ", e); + } + return null; + } + + private void dispatchMessage(@NonNull String component, @NonNull Bundle data) { + Intent intent = new Intent(); + intent.putExtras(data); + intent.setAction("com.google.android.c2dm.intent.RECEIVE"); + intent.setComponent(new ComponentName(getPackageName(), component)); + + GcmReceiver.startWakefulService(getApplicationContext(), intent); + } +} diff --git a/android/src/com/mapswithme/util/push/GcmInstanceIDRouterListenerService.java b/android/src/com/mapswithme/util/push/GcmInstanceIDRouterListenerService.java new file mode 100644 index 0000000000..06658a2c5e --- /dev/null +++ b/android/src/com/mapswithme/util/push/GcmInstanceIDRouterListenerService.java @@ -0,0 +1,24 @@ +package com.mapswithme.util.push; + +import android.content.Intent; + +import com.google.android.gms.iid.InstanceIDListenerService; +import com.mapswithme.util.log.Logger; +import com.mapswithme.util.log.LoggerFactory; +import com.pushwoosh.GCMInstanceIDListenerService; +import ru.mail.libnotify.api.NotificationFactory; + +public class GcmInstanceIDRouterListenerService extends InstanceIDListenerService +{ + @Override + public void onTokenRefresh() + { + super.onTokenRefresh(); + Logger l = LoggerFactory.INSTANCE.getLogger(LoggerFactory.Type.THIRD_PARTY); + l.i(GcmInstanceIDRouterListenerService.class.getSimpleName(), "onTokenRefresh()"); + Intent pwIntent = new Intent(this, GCMInstanceIDListenerService.class); + pwIntent.setAction("com.google.android.gms.iid.InstanceID"); + startService(pwIntent); + NotificationFactory.refreshGcmToken(this); + } +} diff --git a/android/src/com/mapswithme/util/statistics/PushwooshHelper.java b/android/src/com/mapswithme/util/statistics/PushwooshHelper.java index f72f033ee0..f59031d1a6 100644 --- a/android/src/com/mapswithme/util/statistics/PushwooshHelper.java +++ b/android/src/com/mapswithme/util/statistics/PushwooshHelper.java @@ -5,10 +5,12 @@ import android.os.AsyncTask; import android.os.Handler; import android.os.Looper; +import com.mapswithme.maps.MwmApplication; import com.mapswithme.util.log.Logger; import com.mapswithme.util.log.LoggerFactory; import com.pushwoosh.PushManager; import com.pushwoosh.SendPushTagsCallBack; +import ru.mail.libnotify.api.NotificationFactory; import java.lang.ref.WeakReference; import java.util.HashMap; @@ -53,6 +55,8 @@ public final class PushwooshHelper implements SendPushTagsCallBack private void sendTags(Map tags) { + //TODO: move notifylib code to another place when Pushwoosh is deleted. + NotificationFactory.get(MwmApplication.get()).collectEventBatch(tags); synchronized (mSyncObject) { if (!canSendTags())