diff --git a/android/build.gradle b/android/build.gradle index ce982ac75e..7658b954d1 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -39,16 +39,16 @@ apply plugin: 'io.fabric' dependencies { // android support libs - implementation ('com.android.support:support-v4:25.1.0') { + implementation ('com.android.support:support-v4:26.1.0') { force = true; } - implementation 'com.android.support:appcompat-v7:25.0.0' - implementation 'com.android.support:recyclerview-v7:25.0.0' - implementation 'com.android.support:design:25.0.0' - implementation 'com.android.support:cardview-v7:25.0.0' - implementation 'com.android.support:preference-v7:25.0.0' - implementation 'com.android.support:preference-v14:25.0.0' - implementation 'com.android.support:customtabs:25.0.0' + implementation 'com.android.support:appcompat-v7:26.1.0' + implementation 'com.android.support:recyclerview-v7:26.1.0' + implementation 'com.android.support:design:26.1.0' + implementation 'com.android.support:cardview-v7:26.1.0' + implementation 'com.android.support:preference-v7:26.1.0' + implementation 'com.android.support:preference-v14:26.1.0' + implementation 'com.android.support:customtabs:26.1.0' // google play services implementation 'com.google.android.gms:play-services-location:11.8.0' @@ -72,7 +72,8 @@ dependencies { exclude group: 'com.android.support' } implementation 'com.google.code.gson:gson:2.6.1' - implementation 'com.pushwoosh:pushwoosh:4.12.2' + implementation 'com.pushwoosh:pushwoosh:5.8.6' + implementation 'com.pushwoosh:pushwoosh-gcm:5.8.6' implementation 'com.my.tracker:mytracker-sdk:1.5.3' implementation ('com.my.target:mytarget-sdk:4.6.14') { exclude group: 'com.android.support' @@ -94,7 +95,7 @@ dependencies { implementation 'net.jcip:jcip-annotations:1.0' implementation 'com.android.support:multidex:1.0.3' implementation 'com.appsflyer:af-android-sdk:4.8.7' - implementation ("ru.mail:libnotify:0.1.163-notify-support-v100@aar") { + implementation ("ru.mail:libnotify:0.1.177-notify-support-v105-sdk-26@aar") { transitive = true; changing = true; exclude group: 'com.android.support' diff --git a/android/gradle.properties b/android/gradle.properties index 6c94df7a9c..db11b83cab 100644 --- a/android/gradle.properties +++ b/android/gradle.properties @@ -1,7 +1,7 @@ propMinSdkVersion=15 # TODO use 23 target and build tools version, when ProGuard problem will be fixed # https://code.google.com/p/android/issues/detail?id=184567 -propTargetSdkVersion=23 +propTargetSdkVersion=26 propCompileSdkVersion=26 propBuildToolsVersion=27.0.3 propVersionCode=836 diff --git a/android/src/com/mapswithme/maps/MwmApplication.java b/android/src/com/mapswithme/maps/MwmApplication.java index 555ecd9219..7aa5b16e32 100644 --- a/android/src/com/mapswithme/maps/MwmApplication.java +++ b/android/src/com/mapswithme/maps/MwmApplication.java @@ -44,8 +44,9 @@ import com.mopub.common.MoPub; import com.mopub.common.SdkConfiguration; import com.my.tracker.MyTracker; import com.my.tracker.MyTrackerParams; -import com.pushwoosh.PushManager; +import com.pushwoosh.Pushwoosh; import io.fabric.sdk.android.Fabric; +import ru.mail.libnotify.api.NotificationApi; import ru.mail.libnotify.api.NotificationFactory; import ru.mail.notify.core.api.BackgroundAwakeMode; import ru.mail.notify.core.api.NetworkSyncMode; @@ -66,6 +67,9 @@ public class MwmApplication extends Application @SuppressWarnings("NullableProblems") @NonNull private SubwayManager mSubwayManager; + @SuppressWarnings("NullableProblems") + @NonNull + private PushwooshHelper mPushwooshHelper; private boolean mFrameworkInitialized; private boolean mPlatformInitialized; @@ -269,8 +273,8 @@ public class MwmApplication extends Application mBackgroundTracker.addListener(mBackgroundListener); TrackRecorder.init(); - Editor.init(); - UGC.init(); + Editor.init(this); + UGC.init(this); mPlatformInitialized = true; } @@ -370,13 +374,12 @@ public class MwmApplication extends Application if (BuildConfig.PW_APPID.equals(PW_EMPTY_APP_ID)) return; - PushManager pushManager = PushManager.getInstance(this); - pushManager.onStartup(this); + Pushwoosh pushManager = Pushwoosh.getInstance(); pushManager.registerForPushNotifications(); - PushwooshHelper.get().setContext(this); - PushwooshHelper.get().synchronize(); + NotificationApi api = NotificationFactory.get(this); + mPushwooshHelper = new PushwooshHelper(api); } catch(Exception e) { @@ -425,10 +428,7 @@ public class MwmApplication extends Application { try { - if (values.length == 1) - PushwooshHelper.get().sendTag(tag, values[0]); - else - PushwooshHelper.get().sendTag(tag, values); + mPushwooshHelper.sendTags(tag, values); } catch(Exception e) { diff --git a/android/src/com/mapswithme/maps/background/AppBackgroundTracker.java b/android/src/com/mapswithme/maps/background/AppBackgroundTracker.java index 76a8463aa7..800300f9be 100644 --- a/android/src/com/mapswithme/maps/background/AppBackgroundTracker.java +++ b/android/src/com/mapswithme/maps/background/AppBackgroundTracker.java @@ -26,7 +26,7 @@ public final class AppBackgroundTracker private final Listeners mTransitionListeners = new Listeners<>(); private final Listeners mVisibleAppLaunchListeners = new Listeners<>(); private SparseArray> mActivities = new SparseArray<>(); - private boolean mForeground; + private volatile boolean mForeground; private final Runnable mTransitionProc = new Runnable() { diff --git a/android/src/com/mapswithme/maps/background/NotificationService.java b/android/src/com/mapswithme/maps/background/NotificationService.java index 8bc637e421..8f38da99ae 100644 --- a/android/src/com/mapswithme/maps/background/NotificationService.java +++ b/android/src/com/mapswithme/maps/background/NotificationService.java @@ -1,9 +1,9 @@ package com.mapswithme.maps.background; -import android.app.IntentService; import android.content.Context; import android.content.Intent; -import android.support.annotation.Nullable; +import android.support.annotation.NonNull; +import android.support.v4.app.JobIntentService; import android.text.TextUtils; import com.mapswithme.maps.LightFramework; @@ -14,16 +14,18 @@ import com.mapswithme.util.PermissionsUtils; import com.mapswithme.util.log.Logger; import com.mapswithme.util.log.LoggerFactory; +import java.util.concurrent.TimeUnit; + import static android.net.ConnectivityManager.CONNECTIVITY_ACTION; import static com.mapswithme.maps.MwmApplication.prefs; -public class NotificationService extends IntentService +public class NotificationService extends JobIntentService { private static final Logger LOGGER = LoggerFactory.INSTANCE.getLogger(LoggerFactory.Type.MISC); private static final String TAG = NotificationService.class.getSimpleName(); private static final String LAST_AUTH_NOTIFICATION_TIMESTAMP = "DownloadOrUpdateTimestamp"; private static final int MIN_COUNT_UNSENT_UGC = 2; - private static final long MIN_AUTH_EVENT_DELTA_MILLIS = 5 * 24 * 60 * 60 * 1000; // 5 days + private static final long MIN_AUTH_EVENT_DELTA_MILLIS = TimeUnit.DAYS.toMillis(5); private interface NotificationExecutor { @@ -32,9 +34,11 @@ public class NotificationService extends IntentService static void startOnConnectivityChanged(Context context) { - final Intent intent = new Intent(context, NotificationService.class); - intent.setAction(CONNECTIVITY_ACTION); - context.startService(intent); + final Intent intent = new Intent(context, NotificationService.class) + .setAction(CONNECTIVITY_ACTION); + + final int jobId = NotificationService.class.hashCode(); + JobIntentService.enqueueWork(context, NotificationService.class, jobId, intent); } private static boolean notifyIsNotAuthenticated() @@ -79,17 +83,9 @@ public class NotificationService extends IntentService return false; } - public NotificationService() - { - super(NotificationService.class.getSimpleName()); - } - @Override - protected void onHandleIntent(@Nullable Intent intent) + protected void onHandleWork(@NonNull Intent intent) { - if (intent == null) - return; - final String action = intent.getAction(); if (TextUtils.isEmpty(action)) diff --git a/android/src/com/mapswithme/maps/background/WorkerService.java b/android/src/com/mapswithme/maps/background/WorkerService.java index 7b999de8da..edbbd7678d 100644 --- a/android/src/com/mapswithme/maps/background/WorkerService.java +++ b/android/src/com/mapswithme/maps/background/WorkerService.java @@ -1,8 +1,9 @@ package com.mapswithme.maps.background; -import android.app.IntentService; +import android.content.Context; import android.content.Intent; -import android.content.SharedPreferences; +import android.support.annotation.NonNull; +import android.support.v4.app.JobIntentService; import android.text.TextUtils; import android.util.Log; @@ -13,7 +14,7 @@ import com.mapswithme.util.CrashlyticsUtils; import com.mapswithme.util.log.Logger; import com.mapswithme.util.log.LoggerFactory; -public class WorkerService extends IntentService +public class WorkerService extends JobIntentService { private static final Logger LOGGER = LoggerFactory.INSTANCE.getLogger(LoggerFactory.Type.MISC); private static final String TAG = WorkerService.class.getSimpleName(); @@ -26,34 +27,28 @@ public class WorkerService extends IntentService /** * Starts this service to upload map edits to osm servers. */ - public static void startActionUploadOsmChanges() + public static void startActionUploadOsmChanges(@NonNull Context context) { - final Intent intent = new Intent(MwmApplication.get(), WorkerService.class); + final Intent intent = new Intent(context, WorkerService.class); intent.setAction(WorkerService.ACTION_UPLOAD_OSM_CHANGES); - MwmApplication.get().startService(intent); + JobIntentService.enqueueWork(context.getApplicationContext(), WorkerService.class, + WorkerService.class.hashCode(), intent); } /** * Starts this service to upload UGC to our servers. */ - public static void startActionUploadUGC() + public static void startActionUploadUGC(@NonNull Context context) { - final Intent intent = new Intent(MwmApplication.get(), WorkerService.class); + final Intent intent = new Intent(context, WorkerService.class); intent.setAction(WorkerService.ACTION_UPLOAD_UGC); - MwmApplication.get().startService(intent); - } - - public WorkerService() - { - super("WorkerService"); + final int jobId = WorkerService.class.hashCode(); + JobIntentService.enqueueWork(context, WorkerService.class, jobId, intent); } @Override - protected void onHandleIntent(Intent intent) + protected void onHandleWork(@NonNull Intent intent) { - if (intent == null) - return; - String msg = "onHandleIntent: " + intent + " app in background = " + !MwmApplication.backgroundTracker().isForeground(); LOGGER.i(TAG, msg); @@ -68,13 +63,13 @@ public class WorkerService extends IntentService switch (action) { - case ACTION_UPLOAD_OSM_CHANGES: - handleActionUploadOsmChanges(); - break; + case ACTION_UPLOAD_OSM_CHANGES: + handleActionUploadOsmChanges(); + break; - case ACTION_UPLOAD_UGC: - handleUploadUGC(); - break; + case ACTION_UPLOAD_UGC: + handleUploadUGC(); + break; } } diff --git a/android/src/com/mapswithme/maps/bookmarks/SystemDownloadCompletedReceiver.java b/android/src/com/mapswithme/maps/bookmarks/SystemDownloadCompletedReceiver.java index 41ff1b838d..9903c5d42a 100644 --- a/android/src/com/mapswithme/maps/bookmarks/SystemDownloadCompletedReceiver.java +++ b/android/src/com/mapswithme/maps/bookmarks/SystemDownloadCompletedReceiver.java @@ -1,11 +1,10 @@ package com.mapswithme.maps.bookmarks; import android.app.DownloadManager; -import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.support.annotation.NonNull; -import android.support.annotation.Nullable; +import android.support.v4.app.JobIntentService; import com.mapswithme.maps.background.AbstractLogBroadcastReceiver; @@ -25,6 +24,7 @@ public class SystemDownloadCompletedReceiver extends AbstractLogBroadcastReceive if (manager == null) return; intent.setClass(context, SystemDownloadCompletedService.class); - context.startService(intent); + int jobId = SystemDownloadCompletedService.class.hashCode(); + JobIntentService.enqueueWork(context, SystemDownloadCompletedService.class, jobId, intent); } } diff --git a/android/src/com/mapswithme/maps/bookmarks/SystemDownloadCompletedService.java b/android/src/com/mapswithme/maps/bookmarks/SystemDownloadCompletedService.java index 4ab329ac1a..1376703632 100644 --- a/android/src/com/mapswithme/maps/bookmarks/SystemDownloadCompletedService.java +++ b/android/src/com/mapswithme/maps/bookmarks/SystemDownloadCompletedService.java @@ -1,13 +1,13 @@ package com.mapswithme.maps.bookmarks; import android.app.DownloadManager; -import android.app.IntentService; import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.support.v4.app.JobIntentService; import android.text.TextUtils; import android.widget.Toast; @@ -21,13 +21,8 @@ import com.mapswithme.util.concurrency.UiThread; import java.io.IOException; -public class SystemDownloadCompletedService extends IntentService +public class SystemDownloadCompletedService extends JobIntentService { - public SystemDownloadCompletedService() - { - super("GetFileMetaDataService"); - } - @Override public void onCreate() { @@ -39,7 +34,7 @@ public class SystemDownloadCompletedService extends IntentService } @Override - protected void onHandleIntent(@Nullable Intent intent) + protected void onHandleWork(@NonNull Intent intent) { DownloadManager manager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); if (manager == null) diff --git a/android/src/com/mapswithme/maps/editor/Editor.java b/android/src/com/mapswithme/maps/editor/Editor.java index cfccf28b75..bc7daf9fc1 100644 --- a/android/src/com/mapswithme/maps/editor/Editor.java +++ b/android/src/com/mapswithme/maps/editor/Editor.java @@ -1,5 +1,6 @@ package com.mapswithme.maps.editor; +import android.content.Context; import android.support.annotation.IntDef; import android.support.annotation.NonNull; import android.support.annotation.Size; @@ -36,16 +37,6 @@ public final class Editor public static final int MODIFIED = 3; public static final int CREATED = 4; - private static final AppBackgroundTracker.OnTransitionListener sOsmUploader = new AppBackgroundTracker.OnTransitionListener() - { - @Override - public void onTransit(boolean foreground) - { - if (!foreground) - WorkerService.startActionUploadOsmChanges(); - } - }; - private Editor() {} static @@ -55,9 +46,9 @@ public final class Editor private static native void nativeInit(); - public static void init() + public static void init(@NonNull Context context) { - MwmApplication.backgroundTracker().addListener(sOsmUploader); + MwmApplication.backgroundTracker().addListener(new OsmUploadListener(context)); } @WorkerThread @@ -189,4 +180,24 @@ public final class Editor @FeatureStatus public static native int nativeGetMapObjectStatus(); public static native boolean nativeIsMapObjectUploaded(); + + private static class OsmUploadListener implements AppBackgroundTracker.OnTransitionListener + { + @NonNull + private final Context mContext; + + OsmUploadListener(@NonNull Context context) + { + mContext = context.getApplicationContext(); + } + + @Override + public void onTransit(boolean foreground) + { + if (foreground) + return; + + WorkerService.startActionUploadOsmChanges(mContext); + } + } } diff --git a/android/src/com/mapswithme/maps/location/TrackRecorder.java b/android/src/com/mapswithme/maps/location/TrackRecorder.java index a0e9bb6495..fdfac4b6f3 100644 --- a/android/src/com/mapswithme/maps/location/TrackRecorder.java +++ b/android/src/com/mapswithme/maps/location/TrackRecorder.java @@ -6,7 +6,7 @@ import android.content.Context; import android.content.Intent; import android.location.Location; import android.os.SystemClock; -import android.support.annotation.Nullable; +import android.support.annotation.NonNull; import com.mapswithme.maps.MwmApplication; import com.mapswithme.maps.background.AppBackgroundTracker; @@ -35,8 +35,7 @@ public final class TrackRecorder } }; - private static Boolean sEnableLogging; - @Nullable + @NonNull private static final Logger LOGGER = LoggerFactory.INSTANCE.getLogger(LoggerFactory.Type.TRACK_RECORDER); private static final LocationListener sLocationListener = new LocationListener.Simple() @@ -135,14 +134,14 @@ public final class TrackRecorder nativeSetDuration(hours); } - static void onWakeAlarm() + static void onWakeAlarm(@NonNull Context context) { LOGGER.d(TAG, "onWakeAlarm(). Enabled: " + nativeIsEnabled()); UiThread.cancelDelayedTasks(sStartupAwaitProc); if (nativeIsEnabled() && !MwmApplication.backgroundTracker().isForeground()) - TrackRecorderWakeService.start(); + TrackRecorderWakeService.start(context); else stop(); } diff --git a/android/src/com/mapswithme/maps/location/TrackRecorderWakeReceiver.java b/android/src/com/mapswithme/maps/location/TrackRecorderWakeReceiver.java index d30fff9fdf..82653c9c9c 100644 --- a/android/src/com/mapswithme/maps/location/TrackRecorderWakeReceiver.java +++ b/android/src/com/mapswithme/maps/location/TrackRecorderWakeReceiver.java @@ -22,6 +22,6 @@ public class TrackRecorderWakeReceiver extends BroadcastReceiver + !backgroundTracker().isForeground(); LOGGER.i(TAG, msg); CrashlyticsUtils.log(Log.INFO, TAG, msg); - TrackRecorder.onWakeAlarm(); + TrackRecorder.onWakeAlarm(context); } } diff --git a/android/src/com/mapswithme/maps/location/TrackRecorderWakeService.java b/android/src/com/mapswithme/maps/location/TrackRecorderWakeService.java index 57261eb956..9f98bc5243 100644 --- a/android/src/com/mapswithme/maps/location/TrackRecorderWakeService.java +++ b/android/src/com/mapswithme/maps/location/TrackRecorderWakeService.java @@ -1,8 +1,9 @@ package com.mapswithme.maps.location; -import android.app.IntentService; +import android.content.Context; import android.content.Intent; -import android.support.v4.content.WakefulBroadcastReceiver; +import android.support.annotation.NonNull; +import android.support.v4.app.JobIntentService; import android.util.Log; import com.mapswithme.maps.MwmApplication; @@ -13,7 +14,7 @@ import com.mapswithme.util.log.LoggerFactory; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; -public class TrackRecorderWakeService extends IntentService +public class TrackRecorderWakeService extends JobIntentService { private static final String TAG = TrackRecorderWakeService.class.getSimpleName(); private static final Logger LOGGER = LoggerFactory.INSTANCE.getLogger(LoggerFactory.Type.TRACK_RECORDER); @@ -21,13 +22,8 @@ public class TrackRecorderWakeService extends IntentService private static TrackRecorderWakeService sService; private final CountDownLatch mWaitMonitor = new CountDownLatch(1); - public TrackRecorderWakeService() - { - super("TrackRecorderWakeService"); - } - @Override - protected final void onHandleIntent(Intent intent) + protected void onHandleWork(@NonNull Intent intent) { String msg = "onHandleIntent: " + intent + " app in background = " + !MwmApplication.backgroundTracker().isForeground(); @@ -58,20 +54,14 @@ public class TrackRecorderWakeService extends IntentService } TrackRecorder.onServiceStopped(); - WakefulBroadcastReceiver.completeWakefulIntent(intent); } - public static void start() + public static void start(@NonNull Context context) { - LOGGER.d(TAG, "SVC.start()"); - - synchronized (sLock) - { - if (sService == null) - WakefulBroadcastReceiver.startWakefulService(MwmApplication.get(), new Intent(MwmApplication.get(), TrackRecorderWakeService.class)); - else - LOGGER.d(TAG, "SVC.start() SKIPPED because (sService != null)"); - } + Context app = context.getApplicationContext(); + Intent intent = new Intent(app, TrackRecorderWakeService.class); + final int jobId = TrackRecorderWakeService.class.hashCode(); + JobIntentService.enqueueWork(app, TrackRecorderWakeService.class, jobId, intent); } public static void stop() diff --git a/android/src/com/mapswithme/maps/ugc/UGC.java b/android/src/com/mapswithme/maps/ugc/UGC.java index 16acd96426..6ca44019f8 100644 --- a/android/src/com/mapswithme/maps/ugc/UGC.java +++ b/android/src/com/mapswithme/maps/ugc/UGC.java @@ -1,5 +1,6 @@ package com.mapswithme.maps.ugc; +import android.content.Context; import android.os.Parcel; import android.os.Parcelable; import android.support.annotation.IntDef; @@ -33,17 +34,6 @@ public class UGC static final int RATING_EXCELLENT = 5; static final int RATING_COMING_SOON = 6; - private static final AppBackgroundTracker.OnTransitionListener UPLOADER = - new AppBackgroundTracker.OnTransitionListener() - { - @Override - public void onTransit(boolean foreground) - { - if (!foreground) - WorkerService.startActionUploadUGC(); - } - }; - @NonNull private final Rating[] mRatings; @Nullable @@ -53,9 +43,10 @@ public class UGC @Nullable private static UGCListener mListener; - public static void init() + public static void init(final @NonNull Context context) { - MwmApplication.backgroundTracker().addListener(UPLOADER); + final AppBackgroundTracker.OnTransitionListener listener = new UploadUgcTransitionListener(context); + MwmApplication.backgroundTracker().addListener(listener); } private UGC(@NonNull Rating[] ratings, float averageRating, @Nullable Review[] reviews, @@ -272,4 +263,23 @@ public class UGC void onUGCReceived(@Nullable UGC ugc, @Nullable UGCUpdate ugcUpdate, @Impress int impress, @NonNull String rating); } + + private static class UploadUgcTransitionListener implements AppBackgroundTracker.OnTransitionListener + { + @NonNull + private final Context mContext; + + UploadUgcTransitionListener(@NonNull Context context) + { + mContext = context; + } + + @Override + public void onTransit(boolean foreground) + { + if (foreground) + return; + WorkerService.startActionUploadUGC(mContext); + } + } } diff --git a/android/src/com/mapswithme/util/push/GCMListenerRouterService.java b/android/src/com/mapswithme/util/push/GCMListenerRouterService.java index 4bd025a03e..1bc3226b7c 100644 --- a/android/src/com/mapswithme/util/push/GCMListenerRouterService.java +++ b/android/src/com/mapswithme/util/push/GCMListenerRouterService.java @@ -14,7 +14,7 @@ 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 com.pushwoosh.PushGcmIntentService; import ru.mail.libnotify.api.NotificationFactory; // It's temporary class, it may be deleted along with Pushwoosh sdk. @@ -23,6 +23,7 @@ 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: " @@ -37,7 +38,7 @@ public class GCMListenerRouterService extends GcmListenerService String pwProjectId = getPWProjectId(getApplicationContext()); if (!TextUtils.isEmpty(pwProjectId) && pwProjectId.contains(from)) { - dispatchMessage(GCMListenerService.class.getName(), data); + dispatchMessage(PushGcmIntentService.class.getName(), data); return; } diff --git a/android/src/com/mapswithme/util/push/GcmInstanceIDRouterListenerService.java b/android/src/com/mapswithme/util/push/GcmInstanceIDRouterListenerService.java index 06658a2c5e..f12643ac63 100644 --- a/android/src/com/mapswithme/util/push/GcmInstanceIDRouterListenerService.java +++ b/android/src/com/mapswithme/util/push/GcmInstanceIDRouterListenerService.java @@ -1,24 +1,19 @@ 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 com.pushwoosh.GcmRegistrationService; import ru.mail.libnotify.api.NotificationFactory; -public class GcmInstanceIDRouterListenerService extends InstanceIDListenerService +public class GcmInstanceIDRouterListenerService extends GcmRegistrationService { + private static final Logger LOGGER = LoggerFactory.INSTANCE.getLogger(LoggerFactory.Type.THIRD_PARTY); + @Override public void onTokenRefresh() { + LOGGER.i(GcmInstanceIDRouterListenerService.class.getSimpleName(), "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 f59031d1a6..d2b9eae92a 100644 --- a/android/src/com/mapswithme/util/statistics/PushwooshHelper.java +++ b/android/src/com/mapswithme/util/statistics/PushwooshHelper.java @@ -1,144 +1,69 @@ package com.mapswithme.util.statistics; -import android.content.Context; -import android.os.AsyncTask; -import android.os.Handler; -import android.os.Looper; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; -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 com.pushwoosh.Pushwoosh; +import com.pushwoosh.exception.PushwooshException; +import com.pushwoosh.function.Result; +import com.pushwoosh.tags.TagsBundle; +import ru.mail.libnotify.api.NotificationApi; -import java.lang.ref.WeakReference; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; +import java.util.Arrays; +import java.util.Collections; import java.util.Map; -public final class PushwooshHelper implements SendPushTagsCallBack +public final class PushwooshHelper { private static final Logger LOGGER = LoggerFactory.INSTANCE.getLogger(LoggerFactory.Type.MISC); - private static final PushwooshHelper sInstance = new PushwooshHelper(); + @NonNull + private final NotificationApi mNotificationApi; - private WeakReference 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) + public PushwooshHelper(@NonNull NotificationApi api) { - synchronized (mSyncObject) - { - mContext = new WeakReference<>(context); - } + mNotificationApi = api; } - public void synchronize() - { - sendTags(null); - } - - public void sendTag(String tag, Object value) - { - Map tags = new HashMap<>(); - tags.put(tag, value); - sendTags(tags); - } - - private void sendTags(Map tags) + public void sendTags(@NonNull String tag, @Nullable String[] params) { //TODO: move notifylib code to another place when Pushwoosh is deleted. - NotificationFactory.get(MwmApplication.get()).collectEventBatch(tags); - synchronized (mSyncObject) - { - if (!canSendTags()) - { - mTagsQueue.add(tags); - return; - } + if (params == null) + 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) - { - final Context context = mContext.get(); - if (context == null) - return null; - - PushManager.sendTags(context, tagsToSend, PushwooshHelper.this); - return null; - } - }; - mTask.execute((Void) null); - } + TagsBundle.Builder builder = new TagsBundle.Builder(); + boolean isSingleParam = params.length == 1; + TagsBundle tagsBundle = isSingleParam ? builder.putString(tag, params[0]).build() + : builder.putList(tag, Arrays.asList(params)).build(); + Pushwoosh.getInstance().sendTags(tagsBundle, this::onPostExecute); + sendLibNotifyParams(tag, isSingleParam ? params[0] : params); } - @Override - public void taskStarted() {} - - @Override - public void onSentTagsSuccess(Map map) + private void onPostExecute(@NonNull Result result) { - new Handler(Looper.getMainLooper()).post(new Runnable() - { - @Override - public void run() - { - synchronized (mSyncObject) - { - mTask = null; - } - } - }); + if (result.isSuccess()) + onSuccess(result); + else + onError(result); } - @Override - public void onSentTagsError(final Exception e) + private void onError(@NonNull Result result) { - new Handler(Looper.getMainLooper()).post(new Runnable() - { - @Override - public void run() - { - synchronized (mSyncObject) - { - if (e != null) - { - String msg = e.getLocalizedMessage(); - LOGGER.e("Pushwoosh", msg != null ? msg : "onSentTagsError"); - } - mTask = null; - } - } - }); + PushwooshException exception = result.getException(); + String msg = exception == null ? null : exception.getLocalizedMessage(); + LOGGER.e("Pushwoosh", msg != null ? msg : "onSentTagsError"); } - private boolean canSendTags() + private void onSuccess(@NonNull Result result) { - return mContext != null && mTask == null; + /* Do nothing by default */ + } + + private void sendLibNotifyParams(@NonNull String tag, @NonNull Object value) + { + Map map = Collections.singletonMap(tag, value); + mNotificationApi.collectEventBatch(map); } public static native void nativeProcessFirstLaunch();