From 5f9de8f182fe6ced9c8ce075891b1971c2a5b6bb Mon Sep 17 00:00:00 2001 From: Alex Crown Date: Tue, 18 Dec 2018 13:03:22 -0800 Subject: [PATCH 01/26] Bump SDK version number to 0.7.4 --- EmbeddedSocialClient/sdk/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/EmbeddedSocialClient/sdk/build.gradle b/EmbeddedSocialClient/sdk/build.gradle index c5a1965..a471e05 100644 --- a/EmbeddedSocialClient/sdk/build.gradle +++ b/EmbeddedSocialClient/sdk/build.gradle @@ -126,7 +126,7 @@ clearOutputFolder() def siteUrl = 'https://github.com/Microsoft/EmbeddedSocial-Android-SDK' def gitUrl = 'https://github.com/Microsoft/EmbeddedSocial-Android-SDK.git' -def versionNum = '0.7.3' +def versionNum = '0.7.4' group = "com.acrowntest.test" version = versionNum def description = 'Embedded Social Android SDK version ' + versionNum From b643fc2932917a949331f1575c748c6f11361a2f Mon Sep 17 00:00:00 2001 From: Alex Crown Date: Wed, 26 Dec 2018 11:29:24 -0800 Subject: [PATCH 02/26] Use WorkManager to delete search history --- EmbeddedSocialClient/sdk/build.gradle | 5 ++++ .../service/IntentProcessorFactory.java | 2 -- .../embeddedsocial/service/ServiceAction.java | 2 -- .../handler/DeleteSearchHistoryHandler.java | 27 ------------------- .../worker/DeleteSearchHistoryWorker.java | 26 ++++++++++++++++++ .../ui/fragment/OptionsFragment.java | 8 +++--- 6 files changed, 36 insertions(+), 34 deletions(-) delete mode 100644 EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/DeleteSearchHistoryHandler.java create mode 100644 EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/DeleteSearchHistoryWorker.java diff --git a/EmbeddedSocialClient/sdk/build.gradle b/EmbeddedSocialClient/sdk/build.gradle index a471e05..36e9240 100644 --- a/EmbeddedSocialClient/sdk/build.gradle +++ b/EmbeddedSocialClient/sdk/build.gradle @@ -91,6 +91,11 @@ dependencies { api 'com.squareup:otto:1.3.6' api 'com.squareup.retrofit2:retrofit:2.0.0-beta4' api 'com.squareup.okhttp3:okhttp:3.1.2' + implementation ('android.arch.work:work-runtime:1.0.0-alpha13') { + // workaround for build issue regarding com.google.common.util.concurrent.ListenableFuture + //https://stackoverflow.com/questions/52467819/compile-errors-after-updating-to-workmanager-1-0-0-alpha09 + exclude group: 'com.google.guava', module: 'listenablefuture' + } api 'androidx.appcompat:appcompat:1.0.0' api 'androidx.recyclerview:recyclerview:1.0.0' api 'androidx.cardview:cardview:1.0.0' diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/IntentProcessorFactory.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/IntentProcessorFactory.java index d7c504a..4849a56 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/IntentProcessorFactory.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/IntentProcessorFactory.java @@ -10,7 +10,6 @@ import com.microsoft.embeddedsocial.base.service.IntentProcessor; import com.microsoft.embeddedsocial.service.handler.BackgroundInitializationHandler; import com.microsoft.embeddedsocial.service.handler.CreateAccountHandler; import com.microsoft.embeddedsocial.service.handler.DeleteAccountHandler; -import com.microsoft.embeddedsocial.service.handler.DeleteSearchHistoryHandler; import com.microsoft.embeddedsocial.service.handler.GetCommentHandler; import com.microsoft.embeddedsocial.service.handler.GetFcmIdHandler; import com.microsoft.embeddedsocial.service.handler.GetReplyHandler; @@ -51,7 +50,6 @@ public class IntentProcessorFactory { processor.registerIntentHandler(ServiceAction.UPDATE_NOTIFICATION_COUNT, new UpdateNotificationCountHandler()); processor.registerIntentHandler(ServiceAction.GET_COMMENT, new GetCommentHandler()); processor.registerIntentHandler(ServiceAction.GET_REPLY, new GetReplyHandler()); - processor.registerIntentHandler(ServiceAction.DELETE_SEARCH_HISTORY, new DeleteSearchHistoryHandler()); processor.registerIntentHandler(ServiceAction.DELETE_ACCOUNT, new DeleteAccountHandler()); processor.registerIntentHandler(ServiceAction.LINK_USER_THIRD_PARTY_ACCOUNT, new LinkUserThirdPartyAccountHandler()); processor.registerIntentHandler(ServiceAction.UNLINK_USER_THIRD_PARTY_ACCOUNT, new UnlinkUserThirdPartyAccountHandler()); diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/ServiceAction.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/ServiceAction.java index 461de20..8f89015 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/ServiceAction.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/ServiceAction.java @@ -14,8 +14,6 @@ public enum ServiceAction { DELETE_ACCOUNT, - DELETE_SEARCH_HISTORY, - FCM_REGISTER, GET_COMMENT, diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/DeleteSearchHistoryHandler.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/DeleteSearchHistoryHandler.java deleted file mode 100644 index f1f35ca..0000000 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/DeleteSearchHistoryHandler.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright (c) Microsoft Corporation. All rights reserved. - * Licensed under the MIT License. See LICENSE in the project root for license information. - */ - -package com.microsoft.embeddedsocial.service.handler; - -import com.microsoft.embeddedsocial.base.service.IServiceIntentHandler; -import com.microsoft.embeddedsocial.data.storage.SearchHistory; -import com.microsoft.embeddedsocial.service.ServiceAction; - -import android.content.Intent; - -/** - * Deletes the search history. - */ -public class DeleteSearchHistoryHandler implements IServiceIntentHandler { - @Override - public void handleIntent(ServiceAction action, Intent intent) { - new SearchHistory().clear(); - } - - @Override - public void dispose() { - - } -} diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/DeleteSearchHistoryWorker.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/DeleteSearchHistoryWorker.java new file mode 100644 index 0000000..dbe30f3 --- /dev/null +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/DeleteSearchHistoryWorker.java @@ -0,0 +1,26 @@ +/* + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See LICENSE in the project root for license information. + */ + +package com.microsoft.embeddedsocial.service.worker; + +import com.microsoft.embeddedsocial.data.storage.SearchHistory; + +import android.content.Context; + +import androidx.work.Worker; +import androidx.work.WorkerParameters; + +public class DeleteSearchHistoryWorker extends Worker { + + public DeleteSearchHistoryWorker(Context context, WorkerParameters workerParameters) { + super(context, workerParameters); + } + + @Override + public Result doWork() { + new SearchHistory().clear(); + return Result.success(); + } +} diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/OptionsFragment.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/OptionsFragment.java index 7d7a8ed..7ff0621 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/OptionsFragment.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/OptionsFragment.java @@ -14,8 +14,7 @@ import com.microsoft.embeddedsocial.data.storage.DatabaseHelper; import com.microsoft.embeddedsocial.sdk.Options; import com.microsoft.embeddedsocial.sdk.R; import com.microsoft.embeddedsocial.service.IntentExtras; -import com.microsoft.embeddedsocial.service.ServiceAction; -import com.microsoft.embeddedsocial.service.WorkerService; +import com.microsoft.embeddedsocial.service.worker.DeleteSearchHistoryWorker; import com.microsoft.embeddedsocial.ui.activity.DeleteAccountActivity; import com.microsoft.embeddedsocial.ui.activity.FriendlistActivity; import com.microsoft.embeddedsocial.ui.activity.LinkedAccountsActivity; @@ -34,6 +33,8 @@ import android.widget.Toast; import androidx.annotation.Nullable; import androidx.appcompat.widget.SwitchCompat; +import androidx.work.OneTimeWorkRequest; +import androidx.work.WorkManager; /** * Settings fragment. @@ -123,7 +124,8 @@ public class OptionsFragment extends BaseFragment { } private void deleteSearchHistory() { - WorkerService.getLauncher(getContext()).launchService(ServiceAction.DELETE_SEARCH_HISTORY); + OneTimeWorkRequest deleteSearchHistoryWork = new OneTimeWorkRequest.Builder(DeleteSearchHistoryWorker.class).build(); + WorkManager.getInstance().enqueue(deleteSearchHistoryWork); showToast(R.string.es_msg_general_search_history_deleted); } } From 151ea31d11de7d443c7ab3fefff504b48b3b43c4 Mon Sep 17 00:00:00 2001 From: Alex Crown Date: Wed, 26 Dec 2018 13:12:49 -0800 Subject: [PATCH 03/26] Use WorkManager for synchronization, fcm registration, and background init --- .../NotificationServiceCachingWrapper.java | 8 ++-- .../data/storage/UserActionProxy.java | 9 +++-- .../embeddedsocial/sdk/EmbeddedSocial.java | 8 ++-- .../service/FcmInstanceIdListenerService.java | 7 +++- .../service/IntentProcessorFactory.java | 6 --- .../embeddedsocial/service/ServiceAction.java | 6 --- .../BackgroundInitializationHandler.java | 30 -------------- .../service/handler/CreateAccountHandler.java | 8 +++- .../service/handler/SignInHandler.java | 8 +++- .../BackgroundInitializationWorker.java | 28 +++++++++++++ .../GetFcmIdWorker.java} | 27 ++++++------- .../SynchronizationWorker.java} | 40 ++++++++----------- .../ui/fragment/AddPostFragment.java | 8 ++-- 13 files changed, 96 insertions(+), 97 deletions(-) delete mode 100644 EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/BackgroundInitializationHandler.java create mode 100644 EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/BackgroundInitializationWorker.java rename EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/{handler/GetFcmIdHandler.java => worker/GetFcmIdWorker.java} (69%) rename EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/{handler/SynchronizationHandler.java => worker/SynchronizationWorker.java} (74%) diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/data/storage/NotificationServiceCachingWrapper.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/data/storage/NotificationServiceCachingWrapper.java index ed5b70c..eee134d 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/data/storage/NotificationServiceCachingWrapper.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/data/storage/NotificationServiceCachingWrapper.java @@ -17,14 +17,15 @@ import com.microsoft.embeddedsocial.server.model.notification.RegisterPushNotifi import com.microsoft.embeddedsocial.server.model.notification.UnRegisterPushNotificationRequest; import com.microsoft.embeddedsocial.server.model.notification.UpdateNotificationStatusRequest; import com.microsoft.embeddedsocial.server.model.view.ActivityView; -import com.microsoft.embeddedsocial.service.ServiceAction; -import com.microsoft.embeddedsocial.service.WorkerService; +import com.microsoft.embeddedsocial.service.worker.SynchronizationWorker; import android.content.Context; import android.text.TextUtils; import java.sql.SQLException; +import androidx.work.OneTimeWorkRequest; +import androidx.work.WorkManager; import retrofit2.Response; /** @@ -143,6 +144,7 @@ public class NotificationServiceCachingWrapper implements INotificationService { } private void launchSync() { - WorkerService.getLauncher(context).launchService(ServiceAction.SYNC_DATA); + OneTimeWorkRequest backgroundInit = new OneTimeWorkRequest.Builder(SynchronizationWorker.class).build(); + WorkManager.getInstance().enqueue(backgroundInit); } } diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/data/storage/UserActionProxy.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/data/storage/UserActionProxy.java index cfc55c7..a9fad7c 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/data/storage/UserActionProxy.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/data/storage/UserActionProxy.java @@ -30,13 +30,15 @@ import com.microsoft.embeddedsocial.sdk.BuildConfig; import com.microsoft.embeddedsocial.server.model.view.CommentView; import com.microsoft.embeddedsocial.server.model.view.ReplyView; import com.microsoft.embeddedsocial.server.model.view.TopicView; -import com.microsoft.embeddedsocial.service.ServiceAction; -import com.microsoft.embeddedsocial.service.WorkerService; +import com.microsoft.embeddedsocial.service.worker.SynchronizationWorker; import android.content.Context; import java.sql.SQLException; +import androidx.work.OneTimeWorkRequest; +import androidx.work.WorkManager; + /** * Is used as a facade class allowing to perform user actions such as likes/pins/etc. */ @@ -77,7 +79,8 @@ public class UserActionProxy { } private void launchSync() { - WorkerService.getLauncher(context).launchService(ServiceAction.SYNC_DATA); + OneTimeWorkRequest backgroundInit = new OneTimeWorkRequest.Builder(SynchronizationWorker.class).build(); + WorkManager.getInstance().enqueue(backgroundInit); } /** diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/sdk/EmbeddedSocial.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/sdk/EmbeddedSocial.java index 648315f..6fc5d78 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/sdk/EmbeddedSocial.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/sdk/EmbeddedSocial.java @@ -25,8 +25,7 @@ import com.microsoft.embeddedsocial.server.EmbeddedSocialServiceProvider; import com.microsoft.embeddedsocial.server.NetworkAvailability; import com.microsoft.embeddedsocial.server.RequestInfoProvider; import com.microsoft.embeddedsocial.service.IntentExtras; -import com.microsoft.embeddedsocial.service.ServiceAction; -import com.microsoft.embeddedsocial.service.WorkerService; +import com.microsoft.embeddedsocial.service.worker.BackgroundInitializationWorker; import com.microsoft.embeddedsocial.telemetry.Telemetry; import com.microsoft.embeddedsocial.ui.activity.ActivityFeedActivity; import com.microsoft.embeddedsocial.ui.activity.AddPostActivity; @@ -62,6 +61,8 @@ import androidx.annotation.Nullable; import androidx.annotation.RawRes; import androidx.annotation.StringRes; import androidx.fragment.app.Fragment; +import androidx.work.OneTimeWorkRequest; +import androidx.work.WorkManager; /** * Embedded Social SDK facade. @@ -102,7 +103,8 @@ public final class EmbeddedSocial { options.verify(); GlobalObjectRegistry.addObject(options); initGlobalObjects(application, options); - WorkerService.getLauncher(application).launchService(ServiceAction.BACKGROUND_INIT); + OneTimeWorkRequest backgroundInit = new OneTimeWorkRequest.Builder(BackgroundInitializationWorker.class).build(); + WorkManager.getInstance().enqueue(backgroundInit); if (telemetryToken != null) { options.setTelemetryToken(telemetryToken); Telemetry.setAnalyticsSolution(application, telemetryToken); diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/FcmInstanceIdListenerService.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/FcmInstanceIdListenerService.java index eaea956..49a57b0 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/FcmInstanceIdListenerService.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/FcmInstanceIdListenerService.java @@ -8,6 +8,10 @@ package com.microsoft.embeddedsocial.service; import com.google.firebase.iid.FirebaseInstanceIdService; import com.microsoft.embeddedsocial.fcm.FcmTokenHolder; +import com.microsoft.embeddedsocial.service.worker.GetFcmIdWorker; + +import androidx.work.OneTimeWorkRequest; +import androidx.work.WorkManager; /** * Listens to InstanceID API callbacks. @@ -17,6 +21,7 @@ public class FcmInstanceIdListenerService extends FirebaseInstanceIdService { public void onTokenRefresh() { super.onTokenRefresh(); FcmTokenHolder.create(this).resetToken(); - WorkerService.getLauncher(this).launchService(ServiceAction.FCM_REGISTER); + OneTimeWorkRequest fcmRegister = new OneTimeWorkRequest.Builder(GetFcmIdWorker.class).build(); + WorkManager.getInstance().enqueue(fcmRegister); } } diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/IntentProcessorFactory.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/IntentProcessorFactory.java index 4849a56..edfb1fb 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/IntentProcessorFactory.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/IntentProcessorFactory.java @@ -7,17 +7,14 @@ package com.microsoft.embeddedsocial.service; import com.microsoft.embeddedsocial.base.service.IServiceIntentProcessor; import com.microsoft.embeddedsocial.base.service.IntentProcessor; -import com.microsoft.embeddedsocial.service.handler.BackgroundInitializationHandler; import com.microsoft.embeddedsocial.service.handler.CreateAccountHandler; import com.microsoft.embeddedsocial.service.handler.DeleteAccountHandler; import com.microsoft.embeddedsocial.service.handler.GetCommentHandler; -import com.microsoft.embeddedsocial.service.handler.GetFcmIdHandler; import com.microsoft.embeddedsocial.service.handler.GetReplyHandler; import com.microsoft.embeddedsocial.service.handler.LinkUserThirdPartyAccountHandler; import com.microsoft.embeddedsocial.service.handler.RemoveFollowerHandler; import com.microsoft.embeddedsocial.service.handler.SignInHandler; import com.microsoft.embeddedsocial.service.handler.SignOutHandler; -import com.microsoft.embeddedsocial.service.handler.SynchronizationHandler; import com.microsoft.embeddedsocial.service.handler.UnlinkUserThirdPartyAccountHandler; import com.microsoft.embeddedsocial.service.handler.UpdateAccountHandler; import com.microsoft.embeddedsocial.service.handler.UpdateNotificationCountHandler; @@ -42,11 +39,8 @@ public class IntentProcessorFactory { processor.registerIntentHandler(ServiceAction.SIGN_IN, new SignInHandler(context)); processor.registerIntentHandler(ServiceAction.SIGN_OUT, new SignOutHandler(context)); - processor.registerIntentHandler(ServiceAction.SYNC_DATA, new SynchronizationHandler(context)); processor.registerIntentHandler(ServiceAction.CREATE_ACCOUNT, new CreateAccountHandler(context)); processor.registerIntentHandler(ServiceAction.UPDATE_ACCOUNT, new UpdateAccountHandler(context)); - processor.registerIntentHandler(ServiceAction.FCM_REGISTER, new GetFcmIdHandler(context)); - processor.registerIntentHandler(ServiceAction.BACKGROUND_INIT, new BackgroundInitializationHandler(context)); processor.registerIntentHandler(ServiceAction.UPDATE_NOTIFICATION_COUNT, new UpdateNotificationCountHandler()); processor.registerIntentHandler(ServiceAction.GET_COMMENT, new GetCommentHandler()); processor.registerIntentHandler(ServiceAction.GET_REPLY, new GetReplyHandler()); diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/ServiceAction.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/ServiceAction.java index 8f89015..76049a1 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/ServiceAction.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/ServiceAction.java @@ -10,12 +10,8 @@ package com.microsoft.embeddedsocial.service; */ public enum ServiceAction { - BACKGROUND_INIT, - DELETE_ACCOUNT, - FCM_REGISTER, - GET_COMMENT, GET_REPLY, @@ -24,8 +20,6 @@ public enum ServiceAction { SIGN_OUT, - SYNC_DATA, - CREATE_ACCOUNT, UPDATE_ACCOUNT, diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/BackgroundInitializationHandler.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/BackgroundInitializationHandler.java deleted file mode 100644 index 87bebc2..0000000 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/BackgroundInitializationHandler.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright (c) Microsoft Corporation. All rights reserved. - * Licensed under the MIT License. See LICENSE in the project root for license information. - */ - -package com.microsoft.embeddedsocial.service.handler; - -import com.microsoft.embeddedsocial.base.service.IServiceIntentHandler; -import com.microsoft.embeddedsocial.service.ServiceAction; -import com.microsoft.embeddedsocial.service.WorkerService; - -import android.content.Context; -import android.content.Intent; - -public class BackgroundInitializationHandler implements IServiceIntentHandler { - - private final Context context; - - public BackgroundInitializationHandler(Context context) { - this.context = context; - } - - @Override - public void handleIntent(ServiceAction action, Intent intent) { - WorkerService.getLauncher(context).launchService(ServiceAction.FCM_REGISTER); - } - - @Override - public void dispose() { } -} diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/CreateAccountHandler.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/CreateAccountHandler.java index 72f5498..71eafad 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/CreateAccountHandler.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/CreateAccountHandler.java @@ -25,7 +25,7 @@ import com.microsoft.embeddedsocial.server.model.account.GetUserAccountResponse; import com.microsoft.embeddedsocial.server.model.auth.AuthenticationResponse; import com.microsoft.embeddedsocial.service.IntentExtras; import com.microsoft.embeddedsocial.service.ServiceAction; -import com.microsoft.embeddedsocial.service.WorkerService; +import com.microsoft.embeddedsocial.service.worker.GetFcmIdWorker; import android.content.Context; import android.content.Intent; @@ -34,6 +34,9 @@ import android.os.Bundle; import java.io.IOException; +import androidx.work.OneTimeWorkRequest; +import androidx.work.WorkManager; + /** * Sends a create account request to the server. */ @@ -89,7 +92,8 @@ public class CreateAccountHandler extends ActionHandler { if (!action.isCompleted()) { int messageId = R.string.es_msg_general_create_user_success; UserAccount.getInstance().onSignedIn(userHandle, sessionToken, accountData, messageId); - WorkerService.getLauncher(context).launchService(ServiceAction.FCM_REGISTER); + OneTimeWorkRequest fcmRegister = new OneTimeWorkRequest.Builder(GetFcmIdWorker.class).build(); + WorkManager.getInstance().enqueue(fcmRegister); } } diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/SignInHandler.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/SignInHandler.java index 8b99478..adfd26c 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/SignInHandler.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/SignInHandler.java @@ -25,7 +25,7 @@ import com.microsoft.embeddedsocial.server.model.auth.AuthenticationResponse; import com.microsoft.embeddedsocial.server.model.auth.CreateSessionRequest; import com.microsoft.embeddedsocial.service.IntentExtras; import com.microsoft.embeddedsocial.service.ServiceAction; -import com.microsoft.embeddedsocial.service.WorkerService; +import com.microsoft.embeddedsocial.service.worker.GetFcmIdWorker; import com.microsoft.embeddedsocial.ui.activity.CreateProfileActivity; import com.microsoft.embeddedsocial.ui.util.SocialNetworkAccount; @@ -33,6 +33,9 @@ import android.content.Context; import android.content.Intent; import android.os.Bundle; +import androidx.work.OneTimeWorkRequest; +import androidx.work.WorkManager; + /** * Sends sign-in requests. */ @@ -103,7 +106,8 @@ public class SignInHandler extends ActionHandler { if (!action.isCompleted()) { int messageId = R.string.es_msg_general_signin_success; UserAccount.getInstance().onSignedIn(userHandle, sessionToken, accountData, messageId); - WorkerService.getLauncher(context).launchService(ServiceAction.FCM_REGISTER); + OneTimeWorkRequest fcmRegister = new OneTimeWorkRequest.Builder(GetFcmIdWorker.class).build(); + WorkManager.getInstance().enqueue(fcmRegister); } } diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/BackgroundInitializationWorker.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/BackgroundInitializationWorker.java new file mode 100644 index 0000000..b4290bd --- /dev/null +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/BackgroundInitializationWorker.java @@ -0,0 +1,28 @@ +/* + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See LICENSE in the project root for license information. + */ + +package com.microsoft.embeddedsocial.service.worker; + +import android.content.Context; + +import androidx.work.OneTimeWorkRequest; +import androidx.work.WorkManager; +import androidx.work.Worker; +import androidx.work.WorkerParameters; + +public class BackgroundInitializationWorker extends Worker { + + public BackgroundInitializationWorker(Context context, WorkerParameters workerParameters) { + super(context, workerParameters); + } + + @Override + public Result doWork() { + OneTimeWorkRequest fcmRegister = new OneTimeWorkRequest.Builder(GetFcmIdWorker.class).build(); + WorkManager.getInstance().enqueue(fcmRegister); + + return Result.success(); + } +} diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/GetFcmIdHandler.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/GetFcmIdWorker.java similarity index 69% rename from EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/GetFcmIdHandler.java rename to EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/GetFcmIdWorker.java index fda5c9f..6c9c69a 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/GetFcmIdHandler.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/GetFcmIdWorker.java @@ -3,39 +3,37 @@ * Licensed under the MIT License. See LICENSE in the project root for license information. */ -package com.microsoft.embeddedsocial.service.handler; +package com.microsoft.embeddedsocial.service.worker; import com.google.android.gms.tasks.OnFailureListener; import com.google.android.gms.tasks.OnSuccessListener; import com.google.firebase.iid.FirebaseInstanceId; import com.google.firebase.iid.InstanceIdResult; -import com.microsoft.embeddedsocial.base.service.IServiceIntentHandler; import com.microsoft.embeddedsocial.base.utils.debug.DebugLog; import com.microsoft.embeddedsocial.fcm.FcmTokenHolder; -import com.microsoft.embeddedsocial.service.ServiceAction; -import com.microsoft.embeddedsocial.service.WorkerService; import android.content.Context; -import android.content.Intent; import androidx.annotation.NonNull; +import androidx.work.OneTimeWorkRequest; +import androidx.work.WorkManager; +import androidx.work.Worker; +import androidx.work.WorkerParameters; -/** - * Registers the app with Firebase Cloud Messaging framework. - */ -public class GetFcmIdHandler implements IServiceIntentHandler { +public class GetFcmIdWorker extends Worker { private final Context context; private final FcmTokenHolder tokenHolder; - public GetFcmIdHandler(Context context) { + public GetFcmIdWorker(Context context, WorkerParameters workerParameters) { + super(context, workerParameters); this.context = context; this.tokenHolder = FcmTokenHolder.create(context); } @Override - public void handleIntent(ServiceAction action, Intent intent) { + public Result doWork() { if (!tokenHolder.hasValidToken()) { DebugLog.i("obtaining new FCM token"); FirebaseInstanceId instanceId = FirebaseInstanceId.getInstance(); @@ -45,7 +43,8 @@ public class GetFcmIdHandler implements IServiceIntentHandler { FcmTokenHolder.create(context).storeToken(instanceIdResult.getToken()); DebugLog.i("FCM token obtained successfully"); - WorkerService.getLauncher(context).launchService(ServiceAction.SYNC_DATA); + OneTimeWorkRequest backgroundInit = new OneTimeWorkRequest.Builder(SynchronizationWorker.class).build(); + WorkManager.getInstance().enqueue(backgroundInit); } }).addOnFailureListener(new OnFailureListener() { @Override @@ -54,9 +53,7 @@ public class GetFcmIdHandler implements IServiceIntentHandler { } }); } - } - @Override - public void dispose() { + return Result.success(); } } diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/SynchronizationHandler.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/SynchronizationWorker.java similarity index 74% rename from EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/SynchronizationHandler.java rename to EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/SynchronizationWorker.java index e6f2cae..17c6632 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/SynchronizationHandler.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/SynchronizationWorker.java @@ -3,9 +3,8 @@ * Licensed under the MIT License. See LICENSE in the project root for license information. */ -package com.microsoft.embeddedsocial.service.handler; +package com.microsoft.embeddedsocial.service.worker; -import com.microsoft.embeddedsocial.base.service.IServiceIntentHandler; import com.microsoft.embeddedsocial.base.utils.debug.DebugLog; import com.microsoft.embeddedsocial.data.storage.ActivityCache; import com.microsoft.embeddedsocial.data.storage.PostStorage; @@ -13,24 +12,20 @@ import com.microsoft.embeddedsocial.data.storage.UserActionCache; import com.microsoft.embeddedsocial.data.storage.UserCache; import com.microsoft.embeddedsocial.fcm.FcmTokenHolder; import com.microsoft.embeddedsocial.server.sync.DataSynchronizer; -import com.microsoft.embeddedsocial.service.ServiceAction; import android.content.Context; -import android.content.Intent; -/** - * Uploads all available data to the server. - */ -public class SynchronizationHandler implements IServiceIntentHandler { +import androidx.work.Worker; +import androidx.work.WorkerParameters; + +public class SynchronizationWorker extends Worker { public static final String PENDING_POST_SYNC_NAME = "posts"; - private final DataSynchronizer synchronizer = new DataSynchronizer(); - /** - * Creates an instance. - */ - public SynchronizationHandler(Context context) { + + public SynchronizationWorker(Context context, WorkerParameters workerParameters) { + super(context, workerParameters); UserActionCache userActionCache = new UserActionCache(); PostStorage postStorage = new PostStorage(context); synchronizer.registerSyncProducer(postStorage::getPendingPosts, PENDING_POST_SYNC_NAME); @@ -40,26 +35,25 @@ public class SynchronizationHandler implements IServiceIntentHandler Date: Thu, 27 Dec 2018 12:38:49 -0800 Subject: [PATCH 04/26] Use WorkManager for sign in operations --- .../embeddedsocial/account/UserAccount.java | 29 +++- .../actions/ActionsLauncher.java | 6 - .../service/IntentProcessorFactory.java | 2 - .../embeddedsocial/service/ServiceAction.java | 2 - .../service/handler/SignInHandler.java | 118 ----------------- .../service/worker/SignInWorker.java | 124 ++++++++++++++++++ .../ui/fragment/SignInFragment.java | 12 +- .../ui/util/SocialNetworkAccount.java | 5 +- 8 files changed, 162 insertions(+), 136 deletions(-) delete mode 100644 EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/SignInHandler.java create mode 100644 EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/SignInWorker.java diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/account/UserAccount.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/account/UserAccount.java index b783012..924af78 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/account/UserAccount.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/account/UserAccount.java @@ -13,6 +13,7 @@ import com.microsoft.embeddedsocial.auth.SocialNetworkTokens; import com.microsoft.embeddedsocial.autorest.models.FollowerStatus; import com.microsoft.embeddedsocial.base.GlobalObjectRegistry; import com.microsoft.embeddedsocial.base.event.EventBus; +import com.microsoft.embeddedsocial.base.utils.debug.DebugLog; import com.microsoft.embeddedsocial.data.Preferences; import com.microsoft.embeddedsocial.data.model.AccountData; import com.microsoft.embeddedsocial.data.storage.DatabaseHelper; @@ -28,14 +29,23 @@ import com.microsoft.embeddedsocial.pending.PendingAction; import com.microsoft.embeddedsocial.pending.PendingBlock; import com.microsoft.embeddedsocial.pending.PendingFollow; import com.microsoft.embeddedsocial.server.model.view.UserCompactView; +import com.microsoft.embeddedsocial.service.worker.SignInWorker; import com.microsoft.embeddedsocial.ui.util.NotificationCountChecker; import com.microsoft.embeddedsocial.ui.util.SocialNetworkAccount; import android.content.Context; import android.text.TextUtils; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectOutputStream; + import androidx.core.app.NotificationManagerCompat; import androidx.fragment.app.Fragment; +import androidx.work.Data; +import androidx.work.OneTimeWorkRequest; +import androidx.work.Operation; +import androidx.work.WorkManager; /** * Manages functionality related to user account. @@ -58,8 +68,23 @@ public class UserAccount { /** * Launches sign-in process via third party account. */ - public Action signInUsingThirdParty(SocialNetworkAccount thirdPartyAccount) { - return ActionsLauncher.signInUsingThirdParty(context, thirdPartyAccount); + public Operation signInUsingThirdParty(SocialNetworkAccount thirdPartyAccount) { + String serializedData = null; + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + try { + ObjectOutputStream os = new ObjectOutputStream(bos); + os.writeObject(thirdPartyAccount); + serializedData = android.util.Base64.encodeToString(bos.toByteArray(), android.util.Base64.DEFAULT); + os.close(); + } catch (IOException e) { + DebugLog.logException(e); + } + + Data inputData = new Data.Builder() + .putString(SignInWorker.SOCIAL_NETWORK_ACCOUNT, serializedData).build(); + OneTimeWorkRequest fcmRegister = new OneTimeWorkRequest.Builder(SignInWorker.class) + .setInputData(inputData).addTag(SignInWorker.TAG).build(); + return WorkManager.getInstance().enqueue(fcmRegister); } /** diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/actions/ActionsLauncher.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/actions/ActionsLauncher.java index e9c8618..5f38b93 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/actions/ActionsLauncher.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/actions/ActionsLauncher.java @@ -23,12 +23,6 @@ public final class ActionsLauncher { private ActionsLauncher() { } - public static Action signInUsingThirdParty(Context context, SocialNetworkAccount thirdPartyAccount) { - return ActionIntentBuilder.forActionWithTag(Action.Tags.SIGN_IN) - .setThirdPartyAccount(thirdPartyAccount) - .launch(context, ServiceAction.SIGN_IN); - } - public static Action getComment(Context context, String commentHandle) { return ActionIntentBuilder.forActionWithTag(Action.Tags.GET_COMMENT) .setCommentHandle(commentHandle) diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/IntentProcessorFactory.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/IntentProcessorFactory.java index edfb1fb..e18d029 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/IntentProcessorFactory.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/IntentProcessorFactory.java @@ -13,7 +13,6 @@ import com.microsoft.embeddedsocial.service.handler.GetCommentHandler; import com.microsoft.embeddedsocial.service.handler.GetReplyHandler; import com.microsoft.embeddedsocial.service.handler.LinkUserThirdPartyAccountHandler; import com.microsoft.embeddedsocial.service.handler.RemoveFollowerHandler; -import com.microsoft.embeddedsocial.service.handler.SignInHandler; import com.microsoft.embeddedsocial.service.handler.SignOutHandler; import com.microsoft.embeddedsocial.service.handler.UnlinkUserThirdPartyAccountHandler; import com.microsoft.embeddedsocial.service.handler.UpdateAccountHandler; @@ -37,7 +36,6 @@ public class IntentProcessorFactory { IServiceIntentProcessor createIntentProcessor() { IntentProcessor processor = new IntentProcessor<>(context, ServiceAction.class, WORKER_THREADS); - processor.registerIntentHandler(ServiceAction.SIGN_IN, new SignInHandler(context)); processor.registerIntentHandler(ServiceAction.SIGN_OUT, new SignOutHandler(context)); processor.registerIntentHandler(ServiceAction.CREATE_ACCOUNT, new CreateAccountHandler(context)); processor.registerIntentHandler(ServiceAction.UPDATE_ACCOUNT, new UpdateAccountHandler(context)); diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/ServiceAction.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/ServiceAction.java index 76049a1..206c378 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/ServiceAction.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/ServiceAction.java @@ -16,8 +16,6 @@ public enum ServiceAction { GET_REPLY, - SIGN_IN, - SIGN_OUT, CREATE_ACCOUNT, diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/SignInHandler.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/SignInHandler.java deleted file mode 100644 index adfd26c..0000000 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/SignInHandler.java +++ /dev/null @@ -1,118 +0,0 @@ -/* - * Copyright (c) Microsoft Corporation. All rights reserved. - * Licensed under the MIT License. See LICENSE in the project root for license information. - */ - -package com.microsoft.embeddedsocial.service.handler; - -import com.microsoft.embeddedsocial.account.UserAccount; -import com.microsoft.embeddedsocial.actions.Action; -import com.microsoft.embeddedsocial.base.GlobalObjectRegistry; -import com.microsoft.embeddedsocial.base.utils.debug.DebugLog; -import com.microsoft.embeddedsocial.data.model.AccountData; -import com.microsoft.embeddedsocial.sdk.R; -import com.microsoft.embeddedsocial.server.EmbeddedSocialServiceProvider; -import com.microsoft.embeddedsocial.server.IAccountService; -import com.microsoft.embeddedsocial.server.IAuthenticationService; -import com.microsoft.embeddedsocial.server.exception.NetworkRequestException; -import com.microsoft.embeddedsocial.server.exception.NotFoundException; -import com.microsoft.embeddedsocial.server.model.UserRequest; -import com.microsoft.embeddedsocial.server.model.account.GetMyProfileRequest; -import com.microsoft.embeddedsocial.server.model.account.GetUserAccountRequest; -import com.microsoft.embeddedsocial.server.model.account.GetUserAccountResponse; -import com.microsoft.embeddedsocial.server.model.account.GetUserProfileResponse; -import com.microsoft.embeddedsocial.server.model.auth.AuthenticationResponse; -import com.microsoft.embeddedsocial.server.model.auth.CreateSessionRequest; -import com.microsoft.embeddedsocial.service.IntentExtras; -import com.microsoft.embeddedsocial.service.ServiceAction; -import com.microsoft.embeddedsocial.service.worker.GetFcmIdWorker; -import com.microsoft.embeddedsocial.ui.activity.CreateProfileActivity; -import com.microsoft.embeddedsocial.ui.util.SocialNetworkAccount; - -import android.content.Context; -import android.content.Intent; -import android.os.Bundle; - -import androidx.work.OneTimeWorkRequest; -import androidx.work.WorkManager; - -/** - * Sends sign-in requests. - */ -public class SignInHandler extends ActionHandler { - - private final IAccountService accountService = GlobalObjectRegistry - .getObject(EmbeddedSocialServiceProvider.class) - .getAccountService(); - - private final IAuthenticationService authenticationService = GlobalObjectRegistry - .getObject(EmbeddedSocialServiceProvider.class) - .getAuthenticationService(); - - private final Context context; - - public SignInHandler(Context context) { - this.context = context; - } - - @Override - protected void handleAction(Action action, ServiceAction serviceAction, Intent intent) { - signinWithThirdParty(action, intent.getParcelableExtra(IntentExtras.THIRD_PARTY_ACCOUNT)); - intent.removeExtra(IntentExtras.SOCIAL_NETWORK_ACCOUNT); - } - - private void signinWithThirdParty(Action action, SocialNetworkAccount thirdPartyAccount) { - CreateSessionRequest signInWithThirdPartyRequest = new CreateSessionRequest( - thirdPartyAccount.getAccountType(), - thirdPartyAccount.getThirdPartyAccessToken(), - thirdPartyAccount.getThirdPartyRequestToken()); - - - String authorization = signInWithThirdPartyRequest.getAuthorization(); - GetMyProfileRequest getMyProfileRequest = new GetMyProfileRequest(authorization); - - try { - // Determine the user's user handle - GetUserProfileResponse getUserProfileResponse = getMyProfileRequest.send(); - - // set the user handle and attempt sign in - signInWithThirdPartyRequest.setRequestUserHandle(getUserProfileResponse.getUser().getHandle()); - AuthenticationResponse signInResponse = authenticationService.signInWithThirdParty(signInWithThirdPartyRequest); - handleSuccessfulResult(action, signInResponse); - } catch (NotFoundException e) { - // User handle not found; create an account - Intent i = new Intent(context, CreateProfileActivity.class); - i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - Bundle extras = new Bundle(); - extras.putParcelable(IntentExtras.THIRD_PARTY_ACCOUNT, thirdPartyAccount); - i.putExtras(extras); - context.startActivity(i); - } catch (Exception e) { - DebugLog.logException(e); - UserAccount.getInstance().onSignInWithThirdPartyFailed(); - } finally { - thirdPartyAccount.clearTokens(); - } - } - - private void handleSuccessfulResult(Action action, AuthenticationResponse response) - throws NetworkRequestException { - - String userHandle = response.getUserHandle(); - String sessionToken = UserRequest.createSessionAuthorization(response.getSessionToken()); - GetUserAccountRequest getUserRequest = new GetUserAccountRequest(sessionToken); - GetUserAccountResponse userAccount = accountService.getUserAccount(getUserRequest); - AccountData accountData = AccountData.fromServerResponse(userAccount.getUser()); - if (!action.isCompleted()) { - int messageId = R.string.es_msg_general_signin_success; - UserAccount.getInstance().onSignedIn(userHandle, sessionToken, accountData, messageId); - OneTimeWorkRequest fcmRegister = new OneTimeWorkRequest.Builder(GetFcmIdWorker.class).build(); - WorkManager.getInstance().enqueue(fcmRegister); - } - } - - @Override - public void dispose() { - - } -} diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/SignInWorker.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/SignInWorker.java new file mode 100644 index 0000000..c0da944 --- /dev/null +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/SignInWorker.java @@ -0,0 +1,124 @@ +/* + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See LICENSE in the project root for license information. + */ + +package com.microsoft.embeddedsocial.service.worker; + +import com.microsoft.embeddedsocial.account.UserAccount; +import com.microsoft.embeddedsocial.base.GlobalObjectRegistry; +import com.microsoft.embeddedsocial.base.utils.debug.DebugLog; +import com.microsoft.embeddedsocial.data.model.AccountData; +import com.microsoft.embeddedsocial.sdk.R; +import com.microsoft.embeddedsocial.server.EmbeddedSocialServiceProvider; +import com.microsoft.embeddedsocial.server.IAccountService; +import com.microsoft.embeddedsocial.server.IAuthenticationService; +import com.microsoft.embeddedsocial.server.exception.NetworkRequestException; +import com.microsoft.embeddedsocial.server.exception.NotFoundException; +import com.microsoft.embeddedsocial.server.model.UserRequest; +import com.microsoft.embeddedsocial.server.model.account.GetMyProfileRequest; +import com.microsoft.embeddedsocial.server.model.account.GetUserAccountRequest; +import com.microsoft.embeddedsocial.server.model.account.GetUserAccountResponse; +import com.microsoft.embeddedsocial.server.model.account.GetUserProfileResponse; +import com.microsoft.embeddedsocial.server.model.auth.AuthenticationResponse; +import com.microsoft.embeddedsocial.server.model.auth.CreateSessionRequest; +import com.microsoft.embeddedsocial.service.IntentExtras; +import com.microsoft.embeddedsocial.ui.activity.CreateProfileActivity; +import com.microsoft.embeddedsocial.ui.util.SocialNetworkAccount; + +import android.content.Context; +import android.content.Intent; +import android.os.Bundle; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.io.ObjectInputStream; + +import androidx.work.OneTimeWorkRequest; +import androidx.work.WorkManager; +import androidx.work.Worker; +import androidx.work.WorkerParameters; + +/** + * Sends sign in requests. Launches the create user page if necessary. + */ +public class SignInWorker extends Worker { + public static final String SOCIAL_NETWORK_ACCOUNT = "thirdPartyAccount"; + public static final String TAG = "signInWorker"; + + private final Context context; + + private final IAccountService accountService = GlobalObjectRegistry + .getObject(EmbeddedSocialServiceProvider.class) + .getAccountService(); + + private final IAuthenticationService authenticationService = GlobalObjectRegistry + .getObject(EmbeddedSocialServiceProvider.class) + .getAuthenticationService(); + + public SignInWorker(Context context, WorkerParameters workerParameters) { + super(context, workerParameters); + this.context = context; + } + + @Override + public Result doWork() { + String serializedNetworkAccount = getInputData().getString(SOCIAL_NETWORK_ACCOUNT); + if (serializedNetworkAccount == null) { + return Result.failure(); + } + InputStream inputStream = new ByteArrayInputStream( + android.util.Base64.decode(serializedNetworkAccount, android.util.Base64.DEFAULT)); + try { + ObjectInputStream objectStream = new ObjectInputStream(inputStream); + SocialNetworkAccount socialNetworkAccount = (SocialNetworkAccount)objectStream.readObject(); + CreateSessionRequest signInWithThirdPartyRequest = new CreateSessionRequest( + socialNetworkAccount.getAccountType(), + socialNetworkAccount.getThirdPartyAccessToken(), + socialNetworkAccount.getThirdPartyRequestToken()); + + try { + String authorization = signInWithThirdPartyRequest.getAuthorization(); + GetMyProfileRequest getMyProfileRequest = new GetMyProfileRequest(authorization); + + // Determine the user's user handle + GetUserProfileResponse getUserProfileResponse = getMyProfileRequest.send(); + + // set the user handle and attempt sign in + signInWithThirdPartyRequest.setRequestUserHandle(getUserProfileResponse.getUser().getHandle()); + AuthenticationResponse signInResponse = authenticationService.signInWithThirdParty(signInWithThirdPartyRequest); + handleSuccessfulResult(signInResponse); + } catch (NotFoundException e) { + // User handle not found; create an account + Intent i = new Intent(context, CreateProfileActivity.class); + i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + Bundle extras = new Bundle(); + extras.putParcelable(IntentExtras.THIRD_PARTY_ACCOUNT, socialNetworkAccount); + i.putExtras(extras); + context.startActivity(i); + } finally { + socialNetworkAccount.clearTokens(); + } + } catch (Exception e) { + DebugLog.logException(e); + UserAccount.getInstance().onSignInWithThirdPartyFailed(); + return Result.failure(); + } + + return Result.success(); + } + + private void handleSuccessfulResult(AuthenticationResponse response) + throws NetworkRequestException { + + String userHandle = response.getUserHandle(); + String sessionToken = UserRequest.createSessionAuthorization(response.getSessionToken()); + GetUserAccountRequest getUserRequest = new GetUserAccountRequest(sessionToken); + GetUserAccountResponse userAccount = accountService.getUserAccount(getUserRequest); + AccountData accountData = AccountData.fromServerResponse(userAccount.getUser()); + int messageId = R.string.es_msg_general_signin_success; + UserAccount.getInstance().onSignedIn(userHandle, sessionToken, accountData, messageId); + OneTimeWorkRequest fcmRegister = new OneTimeWorkRequest.Builder(GetFcmIdWorker.class).build(); + WorkManager.getInstance().enqueue(fcmRegister); + } +} diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/SignInFragment.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/SignInFragment.java index 68306ed..07e3e90 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/SignInFragment.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/SignInFragment.java @@ -6,7 +6,6 @@ package com.microsoft.embeddedsocial.ui.fragment; import com.microsoft.embeddedsocial.account.UserAccount; -import com.microsoft.embeddedsocial.actions.Action; import com.microsoft.embeddedsocial.auth.AbstractAuthenticator; import com.microsoft.embeddedsocial.auth.FacebookAuthenticator; import com.microsoft.embeddedsocial.auth.GoogleAppAuthAuthenticator; @@ -20,6 +19,7 @@ import com.microsoft.embeddedsocial.event.signin.SignInWithThirdPartyFailedEvent import com.microsoft.embeddedsocial.event.signin.UserSignedInEvent; import com.microsoft.embeddedsocial.sdk.Options; import com.microsoft.embeddedsocial.sdk.R; +import com.microsoft.embeddedsocial.service.worker.SignInWorker; import com.microsoft.embeddedsocial.ui.fragment.base.BaseFragment; import com.microsoft.embeddedsocial.ui.fragment.module.SlowConnectionMessageModule; import com.microsoft.embeddedsocial.ui.util.SocialNetworkAccount; @@ -40,6 +40,8 @@ import android.widget.Toast; import androidx.annotation.IdRes; import androidx.annotation.Nullable; +import androidx.work.Operation; +import androidx.work.WorkManager; /** * Sign-in fragment. @@ -53,14 +55,14 @@ public class SignInFragment extends BaseFragment implements IAuthenticationCallb private AbstractAuthenticator authenticator; private boolean isGettingThirdPartyCredentials; - private Action signInAction; + private Operation signInOperation; private final SlowConnectionMessageModule slowConnectionMessageModule = new SlowConnectionMessageModule( this, R.string.es_cancel, SIGN_IN_TIMEOUT, () -> { - if (signInAction != null) { - signInAction.fail(); + if (signInOperation != null && !signInOperation.getResult().isDone()) { + WorkManager.getInstance().cancelAllWorkByTag(SignInWorker.TAG); setProgressVisible(false); } } @@ -239,7 +241,7 @@ public class SignInFragment extends BaseFragment implements IAuthenticationCallb clearAuthenticator(); ThreadUtils.runOnMainThread(() -> { onSignInStarted(); - signInAction = UserAccount.getInstance().signInUsingThirdParty(account); + signInOperation = UserAccount.getInstance().signInUsingThirdParty(account); }); } diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/util/SocialNetworkAccount.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/util/SocialNetworkAccount.java index 9834021..b29a711 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/util/SocialNetworkAccount.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/util/SocialNetworkAccount.java @@ -10,11 +10,14 @@ import com.microsoft.embeddedsocial.autorest.models.IdentityProvider; import android.os.Parcel; import android.os.Parcelable; +import java.io.Serializable; + /** * Model for social network data */ -public class SocialNetworkAccount implements Parcelable { +public class SocialNetworkAccount implements Parcelable, Serializable { + private static final long serialVersionUID = -113535768434512586L; private final IdentityProvider identityProvider; private final String thirdPartyAccountHandle; private String thirdPartyAccessToken; From 90d78a621c91beb49121702dfe85cc23eb1c5f7e Mon Sep 17 00:00:00 2001 From: Alex Crown Date: Thu, 27 Dec 2018 12:50:52 -0800 Subject: [PATCH 05/26] Rename work request variables --- .../com/microsoft/embeddedsocial/account/UserAccount.java | 4 ++-- .../java/com/microsoft/embeddedsocial/sdk/EmbeddedSocial.java | 4 ++-- .../embeddedsocial/service/FcmInstanceIdListenerService.java | 4 ++-- .../embeddedsocial/service/handler/CreateAccountHandler.java | 4 ++-- .../service/worker/BackgroundInitializationWorker.java | 4 ++-- .../microsoft/embeddedsocial/service/worker/SignInWorker.java | 4 ++-- .../microsoft/embeddedsocial/ui/fragment/AddPostFragment.java | 4 ++-- .../microsoft/embeddedsocial/ui/fragment/OptionsFragment.java | 4 ++-- 8 files changed, 16 insertions(+), 16 deletions(-) diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/account/UserAccount.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/account/UserAccount.java index 924af78..e3cd976 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/account/UserAccount.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/account/UserAccount.java @@ -82,9 +82,9 @@ public class UserAccount { Data inputData = new Data.Builder() .putString(SignInWorker.SOCIAL_NETWORK_ACCOUNT, serializedData).build(); - OneTimeWorkRequest fcmRegister = new OneTimeWorkRequest.Builder(SignInWorker.class) + OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(SignInWorker.class) .setInputData(inputData).addTag(SignInWorker.TAG).build(); - return WorkManager.getInstance().enqueue(fcmRegister); + return WorkManager.getInstance().enqueue(workRequest); } /** diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/sdk/EmbeddedSocial.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/sdk/EmbeddedSocial.java index 6fc5d78..7a3020b 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/sdk/EmbeddedSocial.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/sdk/EmbeddedSocial.java @@ -103,8 +103,8 @@ public final class EmbeddedSocial { options.verify(); GlobalObjectRegistry.addObject(options); initGlobalObjects(application, options); - OneTimeWorkRequest backgroundInit = new OneTimeWorkRequest.Builder(BackgroundInitializationWorker.class).build(); - WorkManager.getInstance().enqueue(backgroundInit); + OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(BackgroundInitializationWorker.class).build(); + WorkManager.getInstance().enqueue(workRequest); if (telemetryToken != null) { options.setTelemetryToken(telemetryToken); Telemetry.setAnalyticsSolution(application, telemetryToken); diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/FcmInstanceIdListenerService.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/FcmInstanceIdListenerService.java index 49a57b0..34de3f9 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/FcmInstanceIdListenerService.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/FcmInstanceIdListenerService.java @@ -21,7 +21,7 @@ public class FcmInstanceIdListenerService extends FirebaseInstanceIdService { public void onTokenRefresh() { super.onTokenRefresh(); FcmTokenHolder.create(this).resetToken(); - OneTimeWorkRequest fcmRegister = new OneTimeWorkRequest.Builder(GetFcmIdWorker.class).build(); - WorkManager.getInstance().enqueue(fcmRegister); + OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(GetFcmIdWorker.class).build(); + WorkManager.getInstance().enqueue(workRequest); } } diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/CreateAccountHandler.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/CreateAccountHandler.java index 71eafad..c6fb5ce 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/CreateAccountHandler.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/CreateAccountHandler.java @@ -92,8 +92,8 @@ public class CreateAccountHandler extends ActionHandler { if (!action.isCompleted()) { int messageId = R.string.es_msg_general_create_user_success; UserAccount.getInstance().onSignedIn(userHandle, sessionToken, accountData, messageId); - OneTimeWorkRequest fcmRegister = new OneTimeWorkRequest.Builder(GetFcmIdWorker.class).build(); - WorkManager.getInstance().enqueue(fcmRegister); + OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(GetFcmIdWorker.class).build(); + WorkManager.getInstance().enqueue(workRequest); } } diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/BackgroundInitializationWorker.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/BackgroundInitializationWorker.java index b4290bd..95dc288 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/BackgroundInitializationWorker.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/BackgroundInitializationWorker.java @@ -20,8 +20,8 @@ public class BackgroundInitializationWorker extends Worker { @Override public Result doWork() { - OneTimeWorkRequest fcmRegister = new OneTimeWorkRequest.Builder(GetFcmIdWorker.class).build(); - WorkManager.getInstance().enqueue(fcmRegister); + OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(GetFcmIdWorker.class).build(); + WorkManager.getInstance().enqueue(workRequest); return Result.success(); } diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/SignInWorker.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/SignInWorker.java index c0da944..21935dc 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/SignInWorker.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/SignInWorker.java @@ -118,7 +118,7 @@ public class SignInWorker extends Worker { AccountData accountData = AccountData.fromServerResponse(userAccount.getUser()); int messageId = R.string.es_msg_general_signin_success; UserAccount.getInstance().onSignedIn(userHandle, sessionToken, accountData, messageId); - OneTimeWorkRequest fcmRegister = new OneTimeWorkRequest.Builder(GetFcmIdWorker.class).build(); - WorkManager.getInstance().enqueue(fcmRegister); + OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(GetFcmIdWorker.class).build(); + WorkManager.getInstance().enqueue(workRequest); } } diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/AddPostFragment.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/AddPostFragment.java index b178471..271ebe1 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/AddPostFragment.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/AddPostFragment.java @@ -141,8 +141,8 @@ public class AddPostFragment extends BaseEditPostFragment implements OnClickList @Override protected void onFinishedEditing() { postStorage.storePost(getTitle(), getDescription(), imageUri, PublisherType.USER); - OneTimeWorkRequest backgroundInit = new OneTimeWorkRequest.Builder(SynchronizationWorker.class).build(); - WorkManager.getInstance().enqueue(backgroundInit); + OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(SynchronizationWorker.class).build(); + WorkManager.getInstance().enqueue(workRequest); finishActivity(); } diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/OptionsFragment.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/OptionsFragment.java index 7ff0621..144a4cf 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/OptionsFragment.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/OptionsFragment.java @@ -124,8 +124,8 @@ public class OptionsFragment extends BaseFragment { } private void deleteSearchHistory() { - OneTimeWorkRequest deleteSearchHistoryWork = new OneTimeWorkRequest.Builder(DeleteSearchHistoryWorker.class).build(); - WorkManager.getInstance().enqueue(deleteSearchHistoryWork); + OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(DeleteSearchHistoryWorker.class).build(); + WorkManager.getInstance().enqueue(workRequest); showToast(R.string.es_msg_general_search_history_deleted); } } From 325b07ecc7fd5191bbe074cb5c364251f288e761 Mon Sep 17 00:00:00 2001 From: Alex Crown Date: Thu, 27 Dec 2018 13:32:46 -0800 Subject: [PATCH 06/26] Use WorkManager to handle remove follower requests --- .../actions/ActionsLauncher.java | 6 ---- .../service/IntentProcessorFactory.java | 2 -- .../embeddedsocial/service/ServiceAction.java | 4 +-- .../RemoveFollowerWorker.java} | 33 ++++++++--------- .../adapter/renderer/MyFollowersRenderer.java | 36 +++++++++++++------ 5 files changed, 42 insertions(+), 39 deletions(-) rename EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/{handler/RemoveFollowerHandler.java => worker/RemoveFollowerWorker.java} (60%) diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/actions/ActionsLauncher.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/actions/ActionsLauncher.java index 5f38b93..3cf5b31 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/actions/ActionsLauncher.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/actions/ActionsLauncher.java @@ -57,12 +57,6 @@ public final class ActionsLauncher { return ActionIntentBuilder.forActionWithTag(Action.Tags.DELETE_ACCOUNT).launch(context, ServiceAction.DELETE_ACCOUNT); } - public static Action removeFollower(Context context, String userHandle) { - return ActionIntentBuilder.forActionWithTag(Action.Tags.REMOVE_FOLLOWER) - .setUserHandle(userHandle) - .launch(context, ServiceAction.REMOVE_FOLLOWER); - } - private static Action createAndStartAction(String tag) { Action action = new Action(tag); action.start(); diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/IntentProcessorFactory.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/IntentProcessorFactory.java index e18d029..2de72a3 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/IntentProcessorFactory.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/IntentProcessorFactory.java @@ -12,7 +12,6 @@ import com.microsoft.embeddedsocial.service.handler.DeleteAccountHandler; import com.microsoft.embeddedsocial.service.handler.GetCommentHandler; import com.microsoft.embeddedsocial.service.handler.GetReplyHandler; import com.microsoft.embeddedsocial.service.handler.LinkUserThirdPartyAccountHandler; -import com.microsoft.embeddedsocial.service.handler.RemoveFollowerHandler; import com.microsoft.embeddedsocial.service.handler.SignOutHandler; import com.microsoft.embeddedsocial.service.handler.UnlinkUserThirdPartyAccountHandler; import com.microsoft.embeddedsocial.service.handler.UpdateAccountHandler; @@ -45,7 +44,6 @@ public class IntentProcessorFactory { processor.registerIntentHandler(ServiceAction.DELETE_ACCOUNT, new DeleteAccountHandler()); processor.registerIntentHandler(ServiceAction.LINK_USER_THIRD_PARTY_ACCOUNT, new LinkUserThirdPartyAccountHandler()); processor.registerIntentHandler(ServiceAction.UNLINK_USER_THIRD_PARTY_ACCOUNT, new UnlinkUserThirdPartyAccountHandler()); - processor.registerIntentHandler(ServiceAction.REMOVE_FOLLOWER, new RemoveFollowerHandler()); return processor; } diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/ServiceAction.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/ServiceAction.java index 206c378..f14ce7b 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/ServiceAction.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/ServiceAction.java @@ -26,7 +26,5 @@ public enum ServiceAction { LINK_USER_THIRD_PARTY_ACCOUNT, - UNLINK_USER_THIRD_PARTY_ACCOUNT, - - REMOVE_FOLLOWER, + UNLINK_USER_THIRD_PARTY_ACCOUNT } diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/RemoveFollowerHandler.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/RemoveFollowerWorker.java similarity index 60% rename from EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/RemoveFollowerHandler.java rename to EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/RemoveFollowerWorker.java index 7809c53..144c850 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/RemoveFollowerHandler.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/RemoveFollowerWorker.java @@ -3,48 +3,45 @@ * Licensed under the MIT License. See LICENSE in the project root for license information. */ -package com.microsoft.embeddedsocial.service.handler; +package com.microsoft.embeddedsocial.service.worker; -import com.microsoft.embeddedsocial.actions.Action; import com.microsoft.embeddedsocial.base.GlobalObjectRegistry; import com.microsoft.embeddedsocial.base.utils.debug.DebugLog; import com.microsoft.embeddedsocial.server.EmbeddedSocialServiceProvider; import com.microsoft.embeddedsocial.server.IRelationshipService; import com.microsoft.embeddedsocial.server.exception.NetworkRequestException; import com.microsoft.embeddedsocial.server.model.relationship.RemoveFollowerRequest; -import com.microsoft.embeddedsocial.service.IntentExtras; -import com.microsoft.embeddedsocial.service.ServiceAction; -import android.content.Intent; -import android.os.Bundle; +import android.content.Context; + +import androidx.work.Worker; +import androidx.work.WorkerParameters; /** - * Sends a remove follower request to the server. + * Sends remove follower requests to the server */ -public class RemoveFollowerHandler extends ActionHandler { +public class RemoveFollowerWorker extends Worker { + public static final String USER_HANDLE = "userHandle"; private final IRelationshipService relationshipService = GlobalObjectRegistry .getObject(EmbeddedSocialServiceProvider.class) .getRelationshipService(); - public RemoveFollowerHandler() { } + public RemoveFollowerWorker(Context context, WorkerParameters workerParams) { + super(context, workerParams); + } @Override - protected void handleAction(Action action, ServiceAction serviceAction, Intent intent) { - Bundle extras = intent.getExtras(); - String userHandle = extras.getString(IntentExtras.USER_HANDLE); + public Result doWork() { + String userHandle = getInputData().getString(USER_HANDLE); RemoveFollowerRequest removeFollowerRequest = new RemoveFollowerRequest(userHandle); try { relationshipService.removeFollower(removeFollowerRequest); } catch (NetworkRequestException e) { DebugLog.logException(e); - action.fail(); + return Result.failure(); } - } - - @Override - public void dispose() { - + return Result.success(); } } diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/adapter/renderer/MyFollowersRenderer.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/adapter/renderer/MyFollowersRenderer.java index 953756d..426f1bb 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/adapter/renderer/MyFollowersRenderer.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/adapter/renderer/MyFollowersRenderer.java @@ -6,18 +6,25 @@ package com.microsoft.embeddedsocial.ui.adapter.renderer; import com.microsoft.embeddedsocial.account.UserAccount; -import com.microsoft.embeddedsocial.actions.Action; -import com.microsoft.embeddedsocial.actions.ActionsLauncher; import com.microsoft.embeddedsocial.autorest.models.FollowerStatus; import com.microsoft.embeddedsocial.data.model.AccountData; import com.microsoft.embeddedsocial.sdk.R; import com.microsoft.embeddedsocial.server.model.view.UserCompactView; +import com.microsoft.embeddedsocial.service.worker.RemoveFollowerWorker; import com.microsoft.embeddedsocial.ui.adapter.viewholder.UserListItemHolder; import android.view.View; import android.widget.TextView; import androidx.fragment.app.Fragment; +import androidx.lifecycle.Observer; +import androidx.lifecycle.ProcessLifecycleOwner; +import androidx.work.Data; +import androidx.work.OneTimeWorkRequest; +import androidx.work.Operation; +import androidx.work.WorkManager; + +import static androidx.work.Operation.SUCCESS; /** * Renders my followers with context menu. @@ -46,14 +53,23 @@ public class MyFollowersRenderer extends UserRenderer { button.setVisibility(View.VISIBLE); button.setEnabled(true); button.setOnClickListener(v -> { - Action action = ActionsLauncher.removeFollower(context, otherUser.getHandle()); - if (!action.isFailed()) { - // redraw the button - button.setText(R.string.es_removed_follower); - getStyleHelper().applyRedCompletedStyle(button); - // decrement local followers count - currUser.setFollowersCount(Math.max(0, currUser.getFollowersCount() - 1)); - } + Data inputData = new Data.Builder() + .putString(RemoveFollowerWorker.USER_HANDLE, otherUser.getHandle()).build(); + OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(RemoveFollowerWorker.class) + .setInputData(inputData).build(); + Operation operation = WorkManager.getInstance().enqueue(workRequest); + operation.getState().observe(ProcessLifecycleOwner.get(), new Observer() { + @Override + public void onChanged(Operation.State state) { + if (SUCCESS.equals(state)) { + // redraw the button + button.setText(R.string.es_removed_follower); + getStyleHelper().applyRedCompletedStyle(button); + // decrement local followers count + currUser.setFollowersCount(Math.max(0, currUser.getFollowersCount() - 1)); + } + } + }); }); button.setText(R.string.es_remove_follower); getStyleHelper().applyRedStyle(button); From 822fd7b52f1147149a99aaa39cb41ea3ebeb28a2 Mon Sep 17 00:00:00 2001 From: Alex Crown Date: Thu, 27 Dec 2018 13:44:46 -0800 Subject: [PATCH 07/26] Use WorkManager to implement sign out --- .../embeddedsocial/account/UserAccount.java | 10 ++++++-- .../actions/ActionsLauncher.java | 6 ----- .../service/IntentProcessorFactory.java | 2 -- .../embeddedsocial/service/ServiceAction.java | 2 -- .../SignOutWorker.java} | 24 ++++++++++--------- 5 files changed, 21 insertions(+), 23 deletions(-) rename EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/{handler/SignOutHandler.java => worker/SignOutWorker.java} (73%) diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/account/UserAccount.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/account/UserAccount.java index e3cd976..376f491 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/account/UserAccount.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/account/UserAccount.java @@ -7,7 +7,6 @@ package com.microsoft.embeddedsocial.account; import com.facebook.login.LoginManager; import com.microsoft.embeddedsocial.actions.Action; -import com.microsoft.embeddedsocial.actions.ActionsLauncher; import com.microsoft.embeddedsocial.actions.OngoingActions; import com.microsoft.embeddedsocial.auth.SocialNetworkTokens; import com.microsoft.embeddedsocial.autorest.models.FollowerStatus; @@ -30,6 +29,7 @@ import com.microsoft.embeddedsocial.pending.PendingBlock; import com.microsoft.embeddedsocial.pending.PendingFollow; import com.microsoft.embeddedsocial.server.model.view.UserCompactView; import com.microsoft.embeddedsocial.service.worker.SignInWorker; +import com.microsoft.embeddedsocial.service.worker.SignOutWorker; import com.microsoft.embeddedsocial.ui.util.NotificationCountChecker; import com.microsoft.embeddedsocial.ui.util.SocialNetworkAccount; @@ -153,7 +153,13 @@ public class UserAccount { * Clears all the data associated with the current user (except the data in the database) and launch the request to the server to sign-out. */ public void signOut() { - ActionsLauncher.signOut(context, Preferences.getInstance().getAuthorizationToken()); + Data inputData = new Data.Builder() + .putString(SignOutWorker.AUTHORIZATION, Preferences.getInstance().getAuthorizationToken()) + .build(); + OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(SignOutWorker.class) + .setInputData(inputData).build(); + WorkManager.getInstance().enqueue(workRequest); + AccountDataStorage.clear(context); Preferences.getInstance().setUserHandle(null); Preferences.getInstance().setAuthorizationToken(null); diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/actions/ActionsLauncher.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/actions/ActionsLauncher.java index 3cf5b31..257d17b 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/actions/ActionsLauncher.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/actions/ActionsLauncher.java @@ -35,12 +35,6 @@ public final class ActionsLauncher { .launch(context, ServiceAction.GET_REPLY); } - public static Action signOut(Context context, String authorization) { - return ActionIntentBuilder.forActionWithTag(Action.Tags.SIGN_OUT) - .setAuthorization(authorization) - .launch(context, ServiceAction.SIGN_OUT); - } - public static Action createAccount(Context context, CreateAccountData createAccountData) { return ActionIntentBuilder.forActionWithTag(Action.Tags.CREATE_ACCOUNT) .setCreateAccountData(createAccountData) diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/IntentProcessorFactory.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/IntentProcessorFactory.java index 2de72a3..4dd5e58 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/IntentProcessorFactory.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/IntentProcessorFactory.java @@ -12,7 +12,6 @@ import com.microsoft.embeddedsocial.service.handler.DeleteAccountHandler; import com.microsoft.embeddedsocial.service.handler.GetCommentHandler; import com.microsoft.embeddedsocial.service.handler.GetReplyHandler; import com.microsoft.embeddedsocial.service.handler.LinkUserThirdPartyAccountHandler; -import com.microsoft.embeddedsocial.service.handler.SignOutHandler; import com.microsoft.embeddedsocial.service.handler.UnlinkUserThirdPartyAccountHandler; import com.microsoft.embeddedsocial.service.handler.UpdateAccountHandler; import com.microsoft.embeddedsocial.service.handler.UpdateNotificationCountHandler; @@ -35,7 +34,6 @@ public class IntentProcessorFactory { IServiceIntentProcessor createIntentProcessor() { IntentProcessor processor = new IntentProcessor<>(context, ServiceAction.class, WORKER_THREADS); - processor.registerIntentHandler(ServiceAction.SIGN_OUT, new SignOutHandler(context)); processor.registerIntentHandler(ServiceAction.CREATE_ACCOUNT, new CreateAccountHandler(context)); processor.registerIntentHandler(ServiceAction.UPDATE_ACCOUNT, new UpdateAccountHandler(context)); processor.registerIntentHandler(ServiceAction.UPDATE_NOTIFICATION_COUNT, new UpdateNotificationCountHandler()); diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/ServiceAction.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/ServiceAction.java index f14ce7b..7025b45 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/ServiceAction.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/ServiceAction.java @@ -16,8 +16,6 @@ public enum ServiceAction { GET_REPLY, - SIGN_OUT, - CREATE_ACCOUNT, UPDATE_ACCOUNT, diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/SignOutHandler.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/SignOutWorker.java similarity index 73% rename from EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/SignOutHandler.java rename to EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/SignOutWorker.java index dd21011..b3b5ee9 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/SignOutHandler.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/SignOutWorker.java @@ -3,9 +3,8 @@ * Licensed under the MIT License. See LICENSE in the project root for license information. */ -package com.microsoft.embeddedsocial.service.handler; +package com.microsoft.embeddedsocial.service.worker; -import com.microsoft.embeddedsocial.actions.Action; import com.microsoft.embeddedsocial.auth.MicrosoftLiveAuthenticator; import com.microsoft.embeddedsocial.base.GlobalObjectRegistry; import com.microsoft.embeddedsocial.base.utils.debug.DebugLog; @@ -13,28 +12,30 @@ import com.microsoft.embeddedsocial.server.EmbeddedSocialServiceProvider; import com.microsoft.embeddedsocial.server.IAuthenticationService; import com.microsoft.embeddedsocial.server.exception.NetworkRequestException; import com.microsoft.embeddedsocial.server.model.auth.SignOutRequest; -import com.microsoft.embeddedsocial.service.IntentExtras; -import com.microsoft.embeddedsocial.service.ServiceAction; import android.content.Context; -import android.content.Intent; import android.os.Build; import android.webkit.CookieManager; +import androidx.work.Worker; +import androidx.work.WorkerParameters; + /** - * Performs sign-out. + * Performs the sign out operation */ -public class SignOutHandler extends ActionHandler { +public class SignOutWorker extends Worker { + public static final String AUTHORIZATION = "authorization"; - private final Context context; + Context context; - public SignOutHandler(Context context) { + public SignOutWorker(Context context, WorkerParameters workerParams) { + super(context, workerParams); this.context = context; } @Override - protected void handleAction(Action action, ServiceAction serviceAction, Intent intent) { - String authorization = intent.getStringExtra(IntentExtras.AUTHORIZATION); + public Result doWork() { + String authorization = getInputData().getString(AUTHORIZATION); IAuthenticationService server = GlobalObjectRegistry.getObject(EmbeddedSocialServiceProvider.class).getAuthenticationService(); try { SignOutRequest request = new SignOutRequest(authorization); @@ -45,6 +46,7 @@ public class SignOutHandler extends ActionHandler { } MicrosoftLiveAuthenticator.signOut(context); clearCookies(); + return Result.success(); } private void clearCookies() { From 86dd7f646ae7983b776bbb54f2df4d50a2477a1a Mon Sep 17 00:00:00 2001 From: Alex Crown Date: Thu, 27 Dec 2018 13:52:55 -0800 Subject: [PATCH 08/26] Use WorkManager handle get comment requests --- .../actions/ActionsLauncher.java | 6 ---- .../service/IntentProcessorFactory.java | 2 -- .../embeddedsocial/service/ServiceAction.java | 2 -- .../GetCommentWorker.java} | 28 ++++++++++++------- .../ui/fragment/DisplayCommentFragment.java | 14 ++++++++-- 5 files changed, 30 insertions(+), 22 deletions(-) rename EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/{handler/GetCommentHandler.java => worker/GetCommentWorker.java} (69%) diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/actions/ActionsLauncher.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/actions/ActionsLauncher.java index 257d17b..54edcd9 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/actions/ActionsLauncher.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/actions/ActionsLauncher.java @@ -23,12 +23,6 @@ public final class ActionsLauncher { private ActionsLauncher() { } - public static Action getComment(Context context, String commentHandle) { - return ActionIntentBuilder.forActionWithTag(Action.Tags.GET_COMMENT) - .setCommentHandle(commentHandle) - .launch(context, ServiceAction.GET_COMMENT); - } - public static Action getReply(Context context, String replyHandle) { return ActionIntentBuilder.forActionWithTag(Action.Tags.GET_REPLY) .setReplyHandle(replyHandle) diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/IntentProcessorFactory.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/IntentProcessorFactory.java index 4dd5e58..750bacd 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/IntentProcessorFactory.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/IntentProcessorFactory.java @@ -9,7 +9,6 @@ import com.microsoft.embeddedsocial.base.service.IServiceIntentProcessor; import com.microsoft.embeddedsocial.base.service.IntentProcessor; import com.microsoft.embeddedsocial.service.handler.CreateAccountHandler; import com.microsoft.embeddedsocial.service.handler.DeleteAccountHandler; -import com.microsoft.embeddedsocial.service.handler.GetCommentHandler; import com.microsoft.embeddedsocial.service.handler.GetReplyHandler; import com.microsoft.embeddedsocial.service.handler.LinkUserThirdPartyAccountHandler; import com.microsoft.embeddedsocial.service.handler.UnlinkUserThirdPartyAccountHandler; @@ -37,7 +36,6 @@ public class IntentProcessorFactory { processor.registerIntentHandler(ServiceAction.CREATE_ACCOUNT, new CreateAccountHandler(context)); processor.registerIntentHandler(ServiceAction.UPDATE_ACCOUNT, new UpdateAccountHandler(context)); processor.registerIntentHandler(ServiceAction.UPDATE_NOTIFICATION_COUNT, new UpdateNotificationCountHandler()); - processor.registerIntentHandler(ServiceAction.GET_COMMENT, new GetCommentHandler()); processor.registerIntentHandler(ServiceAction.GET_REPLY, new GetReplyHandler()); processor.registerIntentHandler(ServiceAction.DELETE_ACCOUNT, new DeleteAccountHandler()); processor.registerIntentHandler(ServiceAction.LINK_USER_THIRD_PARTY_ACCOUNT, new LinkUserThirdPartyAccountHandler()); diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/ServiceAction.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/ServiceAction.java index 7025b45..bd3f554 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/ServiceAction.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/ServiceAction.java @@ -12,8 +12,6 @@ public enum ServiceAction { DELETE_ACCOUNT, - GET_COMMENT, - GET_REPLY, CREATE_ACCOUNT, diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/GetCommentHandler.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/GetCommentWorker.java similarity index 69% rename from EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/GetCommentHandler.java rename to EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/GetCommentWorker.java index 06d5f69..1d59df8 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/GetCommentHandler.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/GetCommentWorker.java @@ -3,9 +3,8 @@ * Licensed under the MIT License. See LICENSE in the project root for license information. */ -package com.microsoft.embeddedsocial.service.handler; +package com.microsoft.embeddedsocial.service.worker; -import com.microsoft.embeddedsocial.actions.Action; import com.microsoft.embeddedsocial.base.GlobalObjectRegistry; import com.microsoft.embeddedsocial.base.event.EventBus; import com.microsoft.embeddedsocial.base.utils.debug.DebugLog; @@ -15,21 +14,28 @@ import com.microsoft.embeddedsocial.server.IContentService; import com.microsoft.embeddedsocial.server.exception.NetworkRequestException; import com.microsoft.embeddedsocial.server.model.content.comments.GetCommentRequest; import com.microsoft.embeddedsocial.server.model.content.comments.GetCommentResponse; -import com.microsoft.embeddedsocial.service.IntentExtras; -import com.microsoft.embeddedsocial.service.ServiceAction; -import android.content.Intent; +import android.content.Context; + +import androidx.work.Worker; +import androidx.work.WorkerParameters; /** - * Get single comment. + * Fetches a comment from the server */ -public class GetCommentHandler extends ActionHandler { +public class GetCommentWorker extends Worker { + public static final String COMMENT_HANDLE = "commentHandle"; + + public GetCommentWorker(Context context, WorkerParameters workerParameters) { + super(context, workerParameters); + } + @Override - protected void handleAction(Action action, ServiceAction serviceAction, Intent intent) { + public Result doWork() { IContentService contentService = GlobalObjectRegistry.getObject(EmbeddedSocialServiceProvider.class).getContentService(); - final String commentHandle = intent.getExtras().getString(IntentExtras.COMMENT_HANDLE); + final String commentHandle = getInputData().getString(COMMENT_HANDLE); try { final GetCommentRequest request = new GetCommentRequest(commentHandle); @@ -37,8 +43,10 @@ public class GetCommentHandler extends ActionHandler { EventBus.post(new GetCommentEvent(response.getComment(), response.getComment() != null)); } catch (NetworkRequestException e) { DebugLog.logException(e); - action.fail(e.getMessage()); EventBus.post(new GetCommentEvent(null, false)); + return Result.failure(); } + + return Result.success(); } } diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/DisplayCommentFragment.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/DisplayCommentFragment.java index 08d0e0f..56462fe 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/DisplayCommentFragment.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/DisplayCommentFragment.java @@ -5,7 +5,6 @@ package com.microsoft.embeddedsocial.ui.fragment; -import com.microsoft.embeddedsocial.actions.ActionsLauncher; import com.microsoft.embeddedsocial.base.utils.debug.DebugLog; import com.microsoft.embeddedsocial.event.content.CommentRemovedEvent; import com.microsoft.embeddedsocial.event.content.GetCommentEvent; @@ -14,6 +13,7 @@ import com.microsoft.embeddedsocial.fetcher.base.ViewStateListener; import com.microsoft.embeddedsocial.sdk.R; import com.microsoft.embeddedsocial.server.model.view.CommentView; import com.microsoft.embeddedsocial.service.IntentExtras; +import com.microsoft.embeddedsocial.service.worker.GetCommentWorker; import com.microsoft.embeddedsocial.ui.activity.TopicActivity; import com.microsoft.embeddedsocial.ui.adapter.viewholder.CommentButtonListener; import com.microsoft.embeddedsocial.ui.adapter.viewholder.CommentViewHolder; @@ -28,6 +28,9 @@ import android.widget.Button; import android.widget.Toast; import androidx.annotation.Nullable; +import androidx.work.Data; +import androidx.work.OneTimeWorkRequest; +import androidx.work.WorkManager; /** * Fragment to display single comment. @@ -62,7 +65,14 @@ public class DisplayCommentFragment extends BaseFragment implements ViewStateLis initView(view); final String commentHandle = arguments.getString(IntentExtras.COMMENT_HANDLE); - ActionsLauncher.getComment(getActivity(), commentHandle); + + Data inputData = new Data.Builder() + .putString(GetCommentWorker.COMMENT_HANDLE, commentHandle) + .build(); + OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(GetCommentWorker.class) + .setInputData(inputData).build(); + WorkManager.getInstance().enqueue(workRequest); + displayLoadView(); } From 6079727b34e592c7764ed31afc5aee495030d966 Mon Sep 17 00:00:00 2001 From: Alex Crown Date: Thu, 27 Dec 2018 14:00:27 -0800 Subject: [PATCH 09/26] Use WorkManager handle get reply requests --- .../actions/ActionsLauncher.java | 6 ---- .../service/IntentProcessorFactory.java | 2 -- .../embeddedsocial/service/ServiceAction.java | 2 -- .../GetReplyWorker.java} | 28 ++++++++++++------- .../ui/fragment/DisplayReplyFragment.java | 14 ++++++++-- 5 files changed, 30 insertions(+), 22 deletions(-) rename EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/{handler/GetReplyHandler.java => worker/GetReplyWorker.java} (69%) diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/actions/ActionsLauncher.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/actions/ActionsLauncher.java index 54edcd9..32b0769 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/actions/ActionsLauncher.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/actions/ActionsLauncher.java @@ -23,12 +23,6 @@ public final class ActionsLauncher { private ActionsLauncher() { } - public static Action getReply(Context context, String replyHandle) { - return ActionIntentBuilder.forActionWithTag(Action.Tags.GET_REPLY) - .setReplyHandle(replyHandle) - .launch(context, ServiceAction.GET_REPLY); - } - public static Action createAccount(Context context, CreateAccountData createAccountData) { return ActionIntentBuilder.forActionWithTag(Action.Tags.CREATE_ACCOUNT) .setCreateAccountData(createAccountData) diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/IntentProcessorFactory.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/IntentProcessorFactory.java index 750bacd..a3ef8b8 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/IntentProcessorFactory.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/IntentProcessorFactory.java @@ -9,7 +9,6 @@ import com.microsoft.embeddedsocial.base.service.IServiceIntentProcessor; import com.microsoft.embeddedsocial.base.service.IntentProcessor; import com.microsoft.embeddedsocial.service.handler.CreateAccountHandler; import com.microsoft.embeddedsocial.service.handler.DeleteAccountHandler; -import com.microsoft.embeddedsocial.service.handler.GetReplyHandler; import com.microsoft.embeddedsocial.service.handler.LinkUserThirdPartyAccountHandler; import com.microsoft.embeddedsocial.service.handler.UnlinkUserThirdPartyAccountHandler; import com.microsoft.embeddedsocial.service.handler.UpdateAccountHandler; @@ -36,7 +35,6 @@ public class IntentProcessorFactory { processor.registerIntentHandler(ServiceAction.CREATE_ACCOUNT, new CreateAccountHandler(context)); processor.registerIntentHandler(ServiceAction.UPDATE_ACCOUNT, new UpdateAccountHandler(context)); processor.registerIntentHandler(ServiceAction.UPDATE_NOTIFICATION_COUNT, new UpdateNotificationCountHandler()); - processor.registerIntentHandler(ServiceAction.GET_REPLY, new GetReplyHandler()); processor.registerIntentHandler(ServiceAction.DELETE_ACCOUNT, new DeleteAccountHandler()); processor.registerIntentHandler(ServiceAction.LINK_USER_THIRD_PARTY_ACCOUNT, new LinkUserThirdPartyAccountHandler()); processor.registerIntentHandler(ServiceAction.UNLINK_USER_THIRD_PARTY_ACCOUNT, new UnlinkUserThirdPartyAccountHandler()); diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/ServiceAction.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/ServiceAction.java index bd3f554..fbd70be 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/ServiceAction.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/ServiceAction.java @@ -12,8 +12,6 @@ public enum ServiceAction { DELETE_ACCOUNT, - GET_REPLY, - CREATE_ACCOUNT, UPDATE_ACCOUNT, diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/GetReplyHandler.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/GetReplyWorker.java similarity index 69% rename from EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/GetReplyHandler.java rename to EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/GetReplyWorker.java index 0617f68..88427a9 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/GetReplyHandler.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/GetReplyWorker.java @@ -3,9 +3,8 @@ * Licensed under the MIT License. See LICENSE in the project root for license information. */ -package com.microsoft.embeddedsocial.service.handler; +package com.microsoft.embeddedsocial.service.worker; -import com.microsoft.embeddedsocial.actions.Action; import com.microsoft.embeddedsocial.base.GlobalObjectRegistry; import com.microsoft.embeddedsocial.base.event.EventBus; import com.microsoft.embeddedsocial.base.utils.debug.DebugLog; @@ -15,21 +14,28 @@ import com.microsoft.embeddedsocial.server.IContentService; import com.microsoft.embeddedsocial.server.exception.NetworkRequestException; import com.microsoft.embeddedsocial.server.model.content.replies.GetReplyRequest; import com.microsoft.embeddedsocial.server.model.content.replies.GetReplyResponse; -import com.microsoft.embeddedsocial.service.IntentExtras; -import com.microsoft.embeddedsocial.service.ServiceAction; -import android.content.Intent; +import android.content.Context; + +import androidx.work.Worker; +import androidx.work.WorkerParameters; /** - * Get single reply. + * Fetches a reply from the server */ -public class GetReplyHandler extends ActionHandler { +public class GetReplyWorker extends Worker { + public static final String REPLY_HANDLE = "replyHandle"; + + public GetReplyWorker(Context context, WorkerParameters workerParameters) { + super(context, workerParameters); + } + @Override - protected void handleAction(Action action, ServiceAction serviceAction, Intent intent) { + public Result doWork() { IContentService contentService = GlobalObjectRegistry.getObject(EmbeddedSocialServiceProvider.class).getContentService(); - final String replyHandle = intent.getExtras().getString(IntentExtras.REPLY_HANDLE); + final String replyHandle = getInputData().getString(REPLY_HANDLE); try { final GetReplyRequest request = new GetReplyRequest(replyHandle); @@ -37,8 +43,10 @@ public class GetReplyHandler extends ActionHandler { EventBus.post(new GetReplyEvent(response.getReply(), response.getReply() != null)); } catch (NetworkRequestException e) { DebugLog.logException(e); - action.fail(e.getMessage()); EventBus.post(new GetReplyEvent(null, false)); + return Result.failure(); } + + return Result.success(); } } diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/DisplayReplyFragment.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/DisplayReplyFragment.java index 6a46f2f..92946ae 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/DisplayReplyFragment.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/DisplayReplyFragment.java @@ -5,13 +5,13 @@ package com.microsoft.embeddedsocial.ui.fragment; -import com.microsoft.embeddedsocial.actions.ActionsLauncher; import com.microsoft.embeddedsocial.base.utils.debug.DebugLog; import com.microsoft.embeddedsocial.event.content.GetReplyEvent; import com.microsoft.embeddedsocial.event.content.ReplyRemovedEvent; import com.microsoft.embeddedsocial.sdk.R; import com.microsoft.embeddedsocial.server.model.view.ReplyView; import com.microsoft.embeddedsocial.service.IntentExtras; +import com.microsoft.embeddedsocial.service.worker.GetReplyWorker; import com.microsoft.embeddedsocial.ui.activity.DisplayNoteActivity; import com.microsoft.embeddedsocial.ui.adapter.viewholder.ReplyButtonListener; import com.microsoft.embeddedsocial.ui.adapter.viewholder.ReplyViewHolder; @@ -26,6 +26,9 @@ import android.widget.Button; import android.widget.Toast; import androidx.annotation.Nullable; +import androidx.work.Data; +import androidx.work.OneTimeWorkRequest; +import androidx.work.WorkManager; /** * Fragment to display single reply. @@ -60,7 +63,14 @@ public class DisplayReplyFragment extends BaseFragment { initView(view); final String replyHandle = arguments.getString(IntentExtras.REPLY_HANDLE); - ActionsLauncher.getReply(getActivity(), replyHandle); + + Data inputData = new Data.Builder() + .putString(GetReplyWorker.REPLY_HANDLE, replyHandle) + .build(); + OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(GetReplyWorker.class) + .setInputData(inputData).build(); + WorkManager.getInstance().enqueue(workRequest); + displayLoadView(); } From f9fb4adbbeb1d55c044ede2d983dd0d638212e25 Mon Sep 17 00:00:00 2001 From: Alex Crown Date: Wed, 9 Jan 2019 14:13:54 -0800 Subject: [PATCH 10/26] Use WorkManager handle delete account requests. Split apart the logic for signing out of the device and out of the server. --- .../embeddedsocial/account/UserAccount.java | 25 +++++++++ .../actions/ActionsLauncher.java | 4 -- .../service/IntentProcessorFactory.java | 2 - .../embeddedsocial/service/ServiceAction.java | 2 - .../DeleteAccountWorker.java} | 27 ++++++---- .../service/worker/SignOutWorker.java | 16 +----- .../ui/fragment/DeleteAccountFragment.java | 53 +++++++++---------- 7 files changed, 67 insertions(+), 62 deletions(-) rename EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/{handler/DeleteAccountHandler.java => worker/DeleteAccountWorker.java} (63%) diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/account/UserAccount.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/account/UserAccount.java index 376f491..4eadf38 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/account/UserAccount.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/account/UserAccount.java @@ -8,6 +8,7 @@ package com.microsoft.embeddedsocial.account; import com.facebook.login.LoginManager; import com.microsoft.embeddedsocial.actions.Action; import com.microsoft.embeddedsocial.actions.OngoingActions; +import com.microsoft.embeddedsocial.auth.MicrosoftLiveAuthenticator; import com.microsoft.embeddedsocial.auth.SocialNetworkTokens; import com.microsoft.embeddedsocial.autorest.models.FollowerStatus; import com.microsoft.embeddedsocial.base.GlobalObjectRegistry; @@ -34,7 +35,9 @@ import com.microsoft.embeddedsocial.ui.util.NotificationCountChecker; import com.microsoft.embeddedsocial.ui.util.SocialNetworkAccount; import android.content.Context; +import android.os.Build; import android.text.TextUtils; +import android.webkit.CookieManager; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -160,6 +163,15 @@ public class UserAccount { .setInputData(inputData).build(); WorkManager.getInstance().enqueue(workRequest); + signOutOfDevice(); + } + + /** + * Clears all the data associated with the current user (except the data in the database) + */ + public void signOutOfDevice() { + MicrosoftLiveAuthenticator.signOut(context); + clearCookies(); AccountDataStorage.clear(context); Preferences.getInstance().setUserHandle(null); Preferences.getInstance().setAuthorizationToken(null); @@ -172,6 +184,19 @@ public class UserAccount { SocialNetworkTokens.clearAll(); } + /** + * Removes all cookies + */ + private void clearCookies() { + CookieManager cookieManager = CookieManager.getInstance(); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + cookieManager.removeAllCookies(null); + } else { + //noinspection deprecation + cookieManager.removeAllCookie(); + } + } + /** * Replaces the current user's account details. */ diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/actions/ActionsLauncher.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/actions/ActionsLauncher.java index 32b0769..36bfadf 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/actions/ActionsLauncher.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/actions/ActionsLauncher.java @@ -35,10 +35,6 @@ public final class ActionsLauncher { .launch(context, ServiceAction.UPDATE_ACCOUNT); } - public static Action deleteAccount(Context context) { - return ActionIntentBuilder.forActionWithTag(Action.Tags.DELETE_ACCOUNT).launch(context, ServiceAction.DELETE_ACCOUNT); - } - private static Action createAndStartAction(String tag) { Action action = new Action(tag); action.start(); diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/IntentProcessorFactory.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/IntentProcessorFactory.java index a3ef8b8..c3032c5 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/IntentProcessorFactory.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/IntentProcessorFactory.java @@ -8,7 +8,6 @@ package com.microsoft.embeddedsocial.service; import com.microsoft.embeddedsocial.base.service.IServiceIntentProcessor; import com.microsoft.embeddedsocial.base.service.IntentProcessor; import com.microsoft.embeddedsocial.service.handler.CreateAccountHandler; -import com.microsoft.embeddedsocial.service.handler.DeleteAccountHandler; import com.microsoft.embeddedsocial.service.handler.LinkUserThirdPartyAccountHandler; import com.microsoft.embeddedsocial.service.handler.UnlinkUserThirdPartyAccountHandler; import com.microsoft.embeddedsocial.service.handler.UpdateAccountHandler; @@ -35,7 +34,6 @@ public class IntentProcessorFactory { processor.registerIntentHandler(ServiceAction.CREATE_ACCOUNT, new CreateAccountHandler(context)); processor.registerIntentHandler(ServiceAction.UPDATE_ACCOUNT, new UpdateAccountHandler(context)); processor.registerIntentHandler(ServiceAction.UPDATE_NOTIFICATION_COUNT, new UpdateNotificationCountHandler()); - processor.registerIntentHandler(ServiceAction.DELETE_ACCOUNT, new DeleteAccountHandler()); processor.registerIntentHandler(ServiceAction.LINK_USER_THIRD_PARTY_ACCOUNT, new LinkUserThirdPartyAccountHandler()); processor.registerIntentHandler(ServiceAction.UNLINK_USER_THIRD_PARTY_ACCOUNT, new UnlinkUserThirdPartyAccountHandler()); diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/ServiceAction.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/ServiceAction.java index fbd70be..586a477 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/ServiceAction.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/ServiceAction.java @@ -10,8 +10,6 @@ package com.microsoft.embeddedsocial.service; */ public enum ServiceAction { - DELETE_ACCOUNT, - CREATE_ACCOUNT, UPDATE_ACCOUNT, diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/DeleteAccountHandler.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/DeleteAccountWorker.java similarity index 63% rename from EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/DeleteAccountHandler.java rename to EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/DeleteAccountWorker.java index 970d05a..72779bb 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/DeleteAccountHandler.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/DeleteAccountWorker.java @@ -3,34 +3,41 @@ * Licensed under the MIT License. See LICENSE in the project root for license information. */ -package com.microsoft.embeddedsocial.service.handler; +package com.microsoft.embeddedsocial.service.worker; import com.microsoft.embeddedsocial.account.UserAccount; -import com.microsoft.embeddedsocial.actions.Action; import com.microsoft.embeddedsocial.base.GlobalObjectRegistry; import com.microsoft.embeddedsocial.base.utils.debug.DebugLog; import com.microsoft.embeddedsocial.server.EmbeddedSocialServiceProvider; import com.microsoft.embeddedsocial.server.IAccountService; import com.microsoft.embeddedsocial.server.exception.NetworkRequestException; import com.microsoft.embeddedsocial.server.model.account.DeleteUserRequest; -import com.microsoft.embeddedsocial.service.ServiceAction; -import android.content.Intent; +import android.content.Context; + +import androidx.work.Worker; +import androidx.work.WorkerParameters; /** - * Sends a delete account request to the server. + * Sends a delete account request to the server */ -public class DeleteAccountHandler extends ActionHandler { +public class DeleteAccountWorker extends Worker { + + public DeleteAccountWorker(Context context, WorkerParameters workerParameters) { + super(context, workerParameters); + } + @Override - protected void handleAction(Action action, ServiceAction serviceAction, Intent intent) { + public Result doWork() { IAccountService server = GlobalObjectRegistry.getObject(EmbeddedSocialServiceProvider.class).getAccountService(); try { server.deleteUser(new DeleteUserRequest()); - UserAccount.getInstance().signOut(); - action.complete(); + UserAccount.getInstance().signOutOfDevice(); } catch (NetworkRequestException e) { DebugLog.logException(e); - action.fail(); + return Result.failure(); } + + return Result.success(); } } diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/SignOutWorker.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/SignOutWorker.java index b3b5ee9..84c2774 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/SignOutWorker.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/SignOutWorker.java @@ -5,7 +5,6 @@ package com.microsoft.embeddedsocial.service.worker; -import com.microsoft.embeddedsocial.auth.MicrosoftLiveAuthenticator; import com.microsoft.embeddedsocial.base.GlobalObjectRegistry; import com.microsoft.embeddedsocial.base.utils.debug.DebugLog; import com.microsoft.embeddedsocial.server.EmbeddedSocialServiceProvider; @@ -14,8 +13,6 @@ import com.microsoft.embeddedsocial.server.exception.NetworkRequestException; import com.microsoft.embeddedsocial.server.model.auth.SignOutRequest; import android.content.Context; -import android.os.Build; -import android.webkit.CookieManager; import androidx.work.Worker; import androidx.work.WorkerParameters; @@ -44,18 +41,7 @@ public class SignOutWorker extends Worker { // ignore server errors DebugLog.logException(e); } - MicrosoftLiveAuthenticator.signOut(context); - clearCookies(); + return Result.success(); } - - private void clearCookies() { - CookieManager cookieManager = CookieManager.getInstance(); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - cookieManager.removeAllCookies(null); - } else { - //noinspection deprecation - cookieManager.removeAllCookie(); - } - } } diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/DeleteAccountFragment.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/DeleteAccountFragment.java index db74b94..b443119 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/DeleteAccountFragment.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/DeleteAccountFragment.java @@ -6,48 +6,30 @@ package com.microsoft.embeddedsocial.ui.fragment; import com.microsoft.embeddedsocial.actions.Action; -import com.microsoft.embeddedsocial.actions.ActionTagFilter; -import com.microsoft.embeddedsocial.actions.ActionsLauncher; import com.microsoft.embeddedsocial.actions.OngoingActions; import com.microsoft.embeddedsocial.sdk.R; -import com.microsoft.embeddedsocial.ui.fragment.base.ActionListener; +import com.microsoft.embeddedsocial.service.worker.DeleteAccountWorker; import com.microsoft.embeddedsocial.ui.fragment.base.BaseFragmentWithProgress; import android.os.Bundle; import android.view.View; -import java.util.List; - import androidx.annotation.Nullable; +import androidx.lifecycle.LiveData; +import androidx.work.OneTimeWorkRequest; +import androidx.work.WorkInfo; +import androidx.work.WorkInfo.State; +import androidx.work.WorkManager; + +import static androidx.work.WorkInfo.State.CANCELLED; +import static androidx.work.WorkInfo.State.FAILED; +import static androidx.work.WorkInfo.State.SUCCEEDED; /** * Fragment for deleting an account. */ public class DeleteAccountFragment extends BaseFragmentWithProgress { - public DeleteAccountFragment() { - addActionListener(new ActionTagFilter(Action.Tags.DELETE_ACCOUNT), new ActionListener() { - @Override - protected void onActionSucceeded(Action action) { - onAccountDeleted(); - } - - @Override - protected void onActionFailed(Action action, String error) { - onError(); - } - - @Override - protected void onActionsCompletionMissed(List completedActions, List succeededActions, List failedActions) { - if (!succeededActions.isEmpty()) { - onAccountDeleted(); - } else if (!failedActions.isEmpty()) { - onError(); - } - } - }); - } - @Override protected int getContentLayoutId() { return R.layout.es_fragment_delete_account; @@ -59,7 +41,20 @@ public class DeleteAccountFragment extends BaseFragmentWithProgress { setOnClickListener(view, R.id.es_cancelButton, v -> finishActivity()); setOnClickListener(view, R.id.es_deleteButton, v -> { setProgressVisible(true); - ActionsLauncher.deleteAccount(v.getContext()); + OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(DeleteAccountWorker.class) + .build(); + WorkManager.getInstance().enqueue(workRequest); + LiveData liveData = WorkManager.getInstance().getWorkInfoByIdLiveData(workRequest.getId()); + liveData.observe(this, workInfo -> { + State state = workInfo.getState(); + if (state.isFinished()) { + if (state.equals(SUCCEEDED)) { + getActivity().runOnUiThread(() -> onAccountDeleted()); + } else if (state.equals(FAILED) || state.equals(CANCELLED)) { + getActivity().runOnUiThread(() -> onError()); + } + } + }); }); } From 8e8e72391aaff163876af13a01f0aa3c6970160b Mon Sep 17 00:00:00 2001 From: Alex Crown Date: Wed, 9 Jan 2019 14:40:05 -0800 Subject: [PATCH 11/26] Use LiveData to detect the completion of the remove follower request. --- .../adapter/renderer/MyFollowersRenderer.java | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/adapter/renderer/MyFollowersRenderer.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/adapter/renderer/MyFollowersRenderer.java index 426f1bb..3a80a45 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/adapter/renderer/MyFollowersRenderer.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/adapter/renderer/MyFollowersRenderer.java @@ -17,14 +17,15 @@ import android.view.View; import android.widget.TextView; import androidx.fragment.app.Fragment; -import androidx.lifecycle.Observer; +import androidx.lifecycle.LiveData; import androidx.lifecycle.ProcessLifecycleOwner; import androidx.work.Data; import androidx.work.OneTimeWorkRequest; -import androidx.work.Operation; +import androidx.work.WorkInfo; +import androidx.work.WorkInfo.State; import androidx.work.WorkManager; -import static androidx.work.Operation.SUCCESS; +import static androidx.work.WorkInfo.State.SUCCEEDED; /** * Renders my followers with context menu. @@ -57,17 +58,17 @@ public class MyFollowersRenderer extends UserRenderer { .putString(RemoveFollowerWorker.USER_HANDLE, otherUser.getHandle()).build(); OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(RemoveFollowerWorker.class) .setInputData(inputData).build(); - Operation operation = WorkManager.getInstance().enqueue(workRequest); - operation.getState().observe(ProcessLifecycleOwner.get(), new Observer() { - @Override - public void onChanged(Operation.State state) { - if (SUCCESS.equals(state)) { - // redraw the button - button.setText(R.string.es_removed_follower); - getStyleHelper().applyRedCompletedStyle(button); - // decrement local followers count - currUser.setFollowersCount(Math.max(0, currUser.getFollowersCount() - 1)); - } + WorkManager.getInstance().enqueue(workRequest); + + LiveData liveData = WorkManager.getInstance().getWorkInfoByIdLiveData(workRequest.getId()); + liveData.observe(ProcessLifecycleOwner.get(), workInfo -> { + State state = workInfo.getState(); + if (state.equals(SUCCEEDED)) { + // redraw the button + button.setText(R.string.es_removed_follower); + getStyleHelper().applyRedCompletedStyle(button); + // decrement local followers count + currUser.setFollowersCount(Math.max(0, currUser.getFollowersCount() - 1)); } }); }); From e74243ad5b5b3c50bdedb6685e60928abac25459 Mon Sep 17 00:00:00 2001 From: Alex Crown Date: Wed, 9 Jan 2019 16:27:52 -0800 Subject: [PATCH 12/26] Import android.Base64 to SignInWorker --- .../microsoft/embeddedsocial/service/worker/SignInWorker.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/SignInWorker.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/SignInWorker.java index 21935dc..3624054 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/SignInWorker.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/SignInWorker.java @@ -29,6 +29,7 @@ import com.microsoft.embeddedsocial.ui.util.SocialNetworkAccount; import android.content.Context; import android.content.Intent; import android.os.Bundle; +import android.util.Base64; import java.io.ByteArrayInputStream; import java.io.InputStream; @@ -68,7 +69,7 @@ public class SignInWorker extends Worker { return Result.failure(); } InputStream inputStream = new ByteArrayInputStream( - android.util.Base64.decode(serializedNetworkAccount, android.util.Base64.DEFAULT)); + Base64.decode(serializedNetworkAccount, Base64.DEFAULT)); try { ObjectInputStream objectStream = new ObjectInputStream(inputStream); SocialNetworkAccount socialNetworkAccount = (SocialNetworkAccount)objectStream.readObject(); From 6424978a23fac60fdffa0b9346cef9b4940cd2de Mon Sep 17 00:00:00 2001 From: Alex Crown Date: Wed, 9 Jan 2019 16:52:18 -0800 Subject: [PATCH 13/26] Use WorkManager in the create account flow --- .../actions/ActionsLauncher.java | 6 -- .../data/model/CreateAccountData.java | 14 ++- .../service/IntentProcessorFactory.java | 2 - .../embeddedsocial/service/ServiceAction.java | 2 - .../CreateAccountWorker.java} | 93 +++++++++---------- .../ui/fragment/CreateProfileFragment.java | 27 +++++- 6 files changed, 78 insertions(+), 66 deletions(-) rename EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/{handler/CreateAccountHandler.java => worker/CreateAccountWorker.java} (54%) diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/actions/ActionsLauncher.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/actions/ActionsLauncher.java index 36bfadf..de1fee2 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/actions/ActionsLauncher.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/actions/ActionsLauncher.java @@ -23,12 +23,6 @@ public final class ActionsLauncher { private ActionsLauncher() { } - public static Action createAccount(Context context, CreateAccountData createAccountData) { - return ActionIntentBuilder.forActionWithTag(Action.Tags.CREATE_ACCOUNT) - .setCreateAccountData(createAccountData) - .launch(context, ServiceAction.CREATE_ACCOUNT); - } - public static Action updateAccount(Context context, AccountDataDifference difference) { return ActionIntentBuilder.forActionWithTag(Action.Tags.UPDATE_ACCOUNT) .setAccountDataDifference(difference) diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/data/model/CreateAccountData.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/data/model/CreateAccountData.java index 9ee11d7..bd7b59b 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/data/model/CreateAccountData.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/data/model/CreateAccountData.java @@ -11,14 +11,18 @@ import android.net.Uri; import android.os.Parcel; import android.os.Parcelable; +import java.io.Serializable; + /** * Information needed to create an account. */ -public class CreateAccountData implements Parcelable { +public class CreateAccountData implements Parcelable, Serializable { + + private static final long serialVersionUID = 6172725706417779546L; private String firstName; private String lastName; private String bio; - private Uri photoUri; + private String photoUri; private IdentityProvider identityProvider; private String thirdPartyAccessToken; private String thirdPartyRequestToken; @@ -50,11 +54,11 @@ public class CreateAccountData implements Parcelable { } public Uri getPhotoUri() { - return photoUri; + return Uri.parse(photoUri); } public void setPhotoUri(Uri photoUri) { - this.photoUri = photoUri; + this.photoUri = photoUri.toString(); } public IdentityProvider getIdentityProvider() { @@ -101,7 +105,7 @@ public class CreateAccountData implements Parcelable { dest.writeString(this.firstName); dest.writeString(this.lastName); dest.writeString(this.bio); - dest.writeParcelable(this.photoUri, 0); + dest.writeString(this.photoUri); dest.writeString(this.identityProvider.toValue()); dest.writeString(this.thirdPartyAccessToken); dest.writeString(this.thirdPartyRequestToken); diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/IntentProcessorFactory.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/IntentProcessorFactory.java index c3032c5..c1fc444 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/IntentProcessorFactory.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/IntentProcessorFactory.java @@ -7,7 +7,6 @@ package com.microsoft.embeddedsocial.service; import com.microsoft.embeddedsocial.base.service.IServiceIntentProcessor; import com.microsoft.embeddedsocial.base.service.IntentProcessor; -import com.microsoft.embeddedsocial.service.handler.CreateAccountHandler; import com.microsoft.embeddedsocial.service.handler.LinkUserThirdPartyAccountHandler; import com.microsoft.embeddedsocial.service.handler.UnlinkUserThirdPartyAccountHandler; import com.microsoft.embeddedsocial.service.handler.UpdateAccountHandler; @@ -31,7 +30,6 @@ public class IntentProcessorFactory { IServiceIntentProcessor createIntentProcessor() { IntentProcessor processor = new IntentProcessor<>(context, ServiceAction.class, WORKER_THREADS); - processor.registerIntentHandler(ServiceAction.CREATE_ACCOUNT, new CreateAccountHandler(context)); processor.registerIntentHandler(ServiceAction.UPDATE_ACCOUNT, new UpdateAccountHandler(context)); processor.registerIntentHandler(ServiceAction.UPDATE_NOTIFICATION_COUNT, new UpdateNotificationCountHandler()); processor.registerIntentHandler(ServiceAction.LINK_USER_THIRD_PARTY_ACCOUNT, new LinkUserThirdPartyAccountHandler()); diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/ServiceAction.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/ServiceAction.java index 586a477..da06c23 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/ServiceAction.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/ServiceAction.java @@ -10,8 +10,6 @@ package com.microsoft.embeddedsocial.service; */ public enum ServiceAction { - CREATE_ACCOUNT, - UPDATE_ACCOUNT, UPDATE_NOTIFICATION_COUNT, diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/CreateAccountHandler.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/CreateAccountWorker.java similarity index 54% rename from EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/CreateAccountHandler.java rename to EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/CreateAccountWorker.java index c6fb5ce..d1eb2d9 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/CreateAccountHandler.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/CreateAccountWorker.java @@ -3,10 +3,9 @@ * Licensed under the MIT License. See LICENSE in the project root for license information. */ -package com.microsoft.embeddedsocial.service.handler; +package com.microsoft.embeddedsocial.service.worker; import com.microsoft.embeddedsocial.account.UserAccount; -import com.microsoft.embeddedsocial.actions.Action; import com.microsoft.embeddedsocial.actions.ActionsLauncher; import com.microsoft.embeddedsocial.base.GlobalObjectRegistry; import com.microsoft.embeddedsocial.base.utils.debug.DebugLog; @@ -16,91 +15,92 @@ import com.microsoft.embeddedsocial.data.model.CreateAccountData; import com.microsoft.embeddedsocial.sdk.R; import com.microsoft.embeddedsocial.server.EmbeddedSocialServiceProvider; import com.microsoft.embeddedsocial.server.IAccountService; -import com.microsoft.embeddedsocial.server.IAuthenticationService; import com.microsoft.embeddedsocial.server.exception.NetworkRequestException; import com.microsoft.embeddedsocial.server.model.UserRequest; import com.microsoft.embeddedsocial.server.model.account.CreateUserRequest; import com.microsoft.embeddedsocial.server.model.account.GetUserAccountRequest; import com.microsoft.embeddedsocial.server.model.account.GetUserAccountResponse; import com.microsoft.embeddedsocial.server.model.auth.AuthenticationResponse; -import com.microsoft.embeddedsocial.service.IntentExtras; -import com.microsoft.embeddedsocial.service.ServiceAction; -import com.microsoft.embeddedsocial.service.worker.GetFcmIdWorker; import android.content.Context; -import android.content.Intent; import android.net.Uri; -import android.os.Bundle; +import android.util.Base64; +import java.io.ByteArrayInputStream; import java.io.IOException; +import java.io.InputStream; +import java.io.ObjectInputStream; import androidx.work.OneTimeWorkRequest; import androidx.work.WorkManager; +import androidx.work.Worker; +import androidx.work.WorkerParameters; -/** - * Sends a create account request to the server. - */ -public class CreateAccountHandler extends ActionHandler { +public class CreateAccountWorker extends Worker { + public static final String CREATE_ACCOUNT_DATA = "createAccountData"; + + private final Context context; private final IAccountService accountService = GlobalObjectRegistry .getObject(EmbeddedSocialServiceProvider.class) .getAccountService(); - private final IAuthenticationService authenticationService = GlobalObjectRegistry - .getObject(EmbeddedSocialServiceProvider.class) - .getAuthenticationService(); - - private final Context context; - - public CreateAccountHandler(Context context) { + public CreateAccountWorker(Context context, WorkerParameters workerParameters) { + super(context, workerParameters); this.context = context; } @Override - protected void handleAction(Action action, ServiceAction serviceAction, Intent intent) { - Bundle extras = intent.getExtras(); - CreateAccountData createAccountData = extras.getParcelable(IntentExtras.CREATE_ACCOUNT_DATA); + public Result doWork() { + String serializedAccountData = getInputData().getString(CREATE_ACCOUNT_DATA); + if (serializedAccountData == null) { + return Result.failure(); + } + + InputStream inputStream = new ByteArrayInputStream( + Base64.decode(serializedAccountData, Base64.DEFAULT)); - CreateUserRequest createUserRequest = new CreateUserRequest.Builder() - .setFirstName(createAccountData.getFirstName()) - .setLastName(createAccountData.getLastName()) - .setBio(createAccountData.getBio()) - .setIdentityProvider(createAccountData.getIdentityProvider()) - .setAccessToken(createAccountData.getThirdPartyAccessToken()) - .setRequestToken(createAccountData.getThirdPartyRequestToken()) - .build(); try { - AuthenticationResponse createUserResponse = accountService.createUser(createUserRequest); - handleSuccessfulResult(action, createUserResponse); + ObjectInputStream objectInputStream = new ObjectInputStream(inputStream); + CreateAccountData createAccountData = (CreateAccountData) objectInputStream.readObject(); + CreateUserRequest createUserRequest = new CreateUserRequest.Builder() + .setFirstName(createAccountData.getFirstName()) + .setLastName(createAccountData.getLastName()) + .setBio(createAccountData.getBio()) + .setIdentityProvider(createAccountData.getIdentityProvider()) + .setAccessToken(createAccountData.getThirdPartyAccessToken()) + .setRequestToken(createAccountData.getThirdPartyRequestToken()) + .build(); + + AuthenticationResponse createUserResponse = accountService.createUser(createUserRequest); + handleSuccessfulResult(createUserResponse); uploadPhoto(createAccountData.getPhotoUri()); - } catch (IOException | NetworkRequestException e) { + } catch (ClassNotFoundException|IOException|NetworkRequestException e) { DebugLog.logException(e); UserAccount.getInstance().onCreateUserFailed(); - action.fail(); + return Result.failure(); } + + return Result.success(); } - private void handleSuccessfulResult(Action action, AuthenticationResponse response) - throws NetworkRequestException { - + private void handleSuccessfulResult(AuthenticationResponse response) throws NetworkRequestException { String userHandle = response.getUserHandle(); String sessionToken = UserRequest.createSessionAuthorization(response.getSessionToken()); GetUserAccountRequest getUserRequest = new GetUserAccountRequest(sessionToken); GetUserAccountResponse userAccount = accountService.getUserAccount(getUserRequest); AccountData accountData = AccountData.fromServerResponse(userAccount.getUser()); - if (!action.isCompleted()) { - int messageId = R.string.es_msg_general_create_user_success; - UserAccount.getInstance().onSignedIn(userHandle, sessionToken, accountData, messageId); - OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(GetFcmIdWorker.class).build(); - WorkManager.getInstance().enqueue(workRequest); - } + int messageId = R.string.es_msg_general_create_user_success; + UserAccount.getInstance().onSignedIn(userHandle, sessionToken, accountData, messageId); + OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(GetFcmIdWorker.class).build(); + WorkManager.getInstance().enqueue(workRequest); } /** * Uploads the profile photo */ - private void uploadPhoto(Uri photoUri) throws IOException, NetworkRequestException { + private void uploadPhoto(Uri photoUri) { // TODO this is a separate call which could fail and leave the wrong public access if (photoUri != null) { AccountDataDifference difference = new AccountDataDifference(); @@ -108,9 +108,4 @@ public class CreateAccountHandler extends ActionHandler { ActionsLauncher.updateAccount(context, difference); } } - - @Override - public void dispose() { - - } } diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/CreateProfileFragment.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/CreateProfileFragment.java index 2e92a34..8490da6 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/CreateProfileFragment.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/CreateProfileFragment.java @@ -7,10 +7,10 @@ package com.microsoft.embeddedsocial.ui.fragment; import com.microsoft.embeddedsocial.account.UserAccount; import com.microsoft.embeddedsocial.actions.Action; -import com.microsoft.embeddedsocial.actions.ActionsLauncher; import com.microsoft.embeddedsocial.actions.OngoingActions; import com.microsoft.embeddedsocial.base.utils.BitmapUtils; import com.microsoft.embeddedsocial.base.utils.ViewUtils; +import com.microsoft.embeddedsocial.base.utils.debug.DebugLog; import com.microsoft.embeddedsocial.data.model.AccountData; import com.microsoft.embeddedsocial.data.model.CreateAccountData; import com.microsoft.embeddedsocial.event.signin.CreateUserFailedEvent; @@ -22,6 +22,7 @@ import com.microsoft.embeddedsocial.image.ImageViewContentLoader; import com.microsoft.embeddedsocial.image.UserPhotoLoader; import com.microsoft.embeddedsocial.sdk.R; import com.microsoft.embeddedsocial.service.IntentExtras; +import com.microsoft.embeddedsocial.service.worker.CreateAccountWorker; import com.microsoft.embeddedsocial.ui.fragment.base.BaseFragmentWithProgress; import com.microsoft.embeddedsocial.ui.fragment.module.PhotoProviderModule; import com.microsoft.embeddedsocial.ui.theme.ThemeAttributes; @@ -39,10 +40,16 @@ import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectOutputStream; import java.util.LinkedList; import java.util.List; import androidx.annotation.Nullable; +import androidx.work.Data; +import androidx.work.OneTimeWorkRequest; +import androidx.work.WorkManager; /** * Fragment to create profile. @@ -125,7 +132,23 @@ public class CreateProfileFragment extends BaseFragmentWithProgress { .build(); thirdPartyAccount.clearTokens(); - ActionsLauncher.createAccount(getContext(), createAccountData); + + String serializedData = null; + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + try { + ObjectOutputStream os = new ObjectOutputStream(bos); + os.writeObject(createAccountData); + serializedData = android.util.Base64.encodeToString(bos.toByteArray(), android.util.Base64.DEFAULT); + os.close(); + } catch (IOException e) { + DebugLog.logException(e); + } + + Data inputData = new Data.Builder() + .putString(CreateAccountWorker.CREATE_ACCOUNT_DATA, serializedData).build(); + OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(CreateAccountWorker.class) + .setInputData(inputData).build(); + WorkManager.getInstance().enqueue(workRequest); } } From ed7c7818627828296b0c40b03bacfa9f33bf8b96 Mon Sep 17 00:00:00 2001 From: Alex Crown Date: Wed, 9 Jan 2019 17:28:11 -0800 Subject: [PATCH 14/26] Abstract away the serialization/deserialization work to a helper class --- .../embeddedsocial/account/UserAccount.java | 20 +----- .../service/worker/CreateAccountWorker.java | 17 ++--- .../service/worker/SignInWorker.java | 63 ++++++++---------- .../worker/WorkerSerializationHelper.java | 64 +++++++++++++++++++ .../ui/fragment/CreateProfileFragment.java | 19 +----- 5 files changed, 102 insertions(+), 81 deletions(-) create mode 100644 EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/WorkerSerializationHelper.java diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/account/UserAccount.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/account/UserAccount.java index 4eadf38..2a2417c 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/account/UserAccount.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/account/UserAccount.java @@ -13,7 +13,6 @@ import com.microsoft.embeddedsocial.auth.SocialNetworkTokens; import com.microsoft.embeddedsocial.autorest.models.FollowerStatus; import com.microsoft.embeddedsocial.base.GlobalObjectRegistry; import com.microsoft.embeddedsocial.base.event.EventBus; -import com.microsoft.embeddedsocial.base.utils.debug.DebugLog; import com.microsoft.embeddedsocial.data.Preferences; import com.microsoft.embeddedsocial.data.model.AccountData; import com.microsoft.embeddedsocial.data.storage.DatabaseHelper; @@ -31,6 +30,7 @@ import com.microsoft.embeddedsocial.pending.PendingFollow; import com.microsoft.embeddedsocial.server.model.view.UserCompactView; import com.microsoft.embeddedsocial.service.worker.SignInWorker; import com.microsoft.embeddedsocial.service.worker.SignOutWorker; +import com.microsoft.embeddedsocial.service.worker.WorkerSerializationHelper; import com.microsoft.embeddedsocial.ui.util.NotificationCountChecker; import com.microsoft.embeddedsocial.ui.util.SocialNetworkAccount; @@ -39,10 +39,6 @@ import android.os.Build; import android.text.TextUtils; import android.webkit.CookieManager; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.ObjectOutputStream; - import androidx.core.app.NotificationManagerCompat; import androidx.fragment.app.Fragment; import androidx.work.Data; @@ -72,19 +68,9 @@ public class UserAccount { * Launches sign-in process via third party account. */ public Operation signInUsingThirdParty(SocialNetworkAccount thirdPartyAccount) { - String serializedData = null; - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - try { - ObjectOutputStream os = new ObjectOutputStream(bos); - os.writeObject(thirdPartyAccount); - serializedData = android.util.Base64.encodeToString(bos.toByteArray(), android.util.Base64.DEFAULT); - os.close(); - } catch (IOException e) { - DebugLog.logException(e); - } - Data inputData = new Data.Builder() - .putString(SignInWorker.SOCIAL_NETWORK_ACCOUNT, serializedData).build(); + .putString(SignInWorker.SOCIAL_NETWORK_ACCOUNT, + WorkerSerializationHelper.serialize(thirdPartyAccount)).build(); OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(SignInWorker.class) .setInputData(inputData).addTag(SignInWorker.TAG).build(); return WorkManager.getInstance().enqueue(workRequest); diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/CreateAccountWorker.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/CreateAccountWorker.java index d1eb2d9..701ec99 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/CreateAccountWorker.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/CreateAccountWorker.java @@ -24,12 +24,6 @@ import com.microsoft.embeddedsocial.server.model.auth.AuthenticationResponse; import android.content.Context; import android.net.Uri; -import android.util.Base64; - -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.ObjectInputStream; import androidx.work.OneTimeWorkRequest; import androidx.work.WorkManager; @@ -53,17 +47,14 @@ public class CreateAccountWorker extends Worker { @Override public Result doWork() { String serializedAccountData = getInputData().getString(CREATE_ACCOUNT_DATA); + CreateAccountData createAccountData = WorkerSerializationHelper.deserialize(serializedAccountData); + if (serializedAccountData == null) { + UserAccount.getInstance().onCreateUserFailed(); return Result.failure(); } - InputStream inputStream = new ByteArrayInputStream( - Base64.decode(serializedAccountData, Base64.DEFAULT)); - try { - ObjectInputStream objectInputStream = new ObjectInputStream(inputStream); - CreateAccountData createAccountData = (CreateAccountData) objectInputStream.readObject(); - CreateUserRequest createUserRequest = new CreateUserRequest.Builder() .setFirstName(createAccountData.getFirstName()) .setLastName(createAccountData.getLastName()) @@ -76,7 +67,7 @@ public class CreateAccountWorker extends Worker { AuthenticationResponse createUserResponse = accountService.createUser(createUserRequest); handleSuccessfulResult(createUserResponse); uploadPhoto(createAccountData.getPhotoUri()); - } catch (ClassNotFoundException|IOException|NetworkRequestException e) { + } catch (NetworkRequestException e) { DebugLog.logException(e); UserAccount.getInstance().onCreateUserFailed(); return Result.failure(); diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/SignInWorker.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/SignInWorker.java index 3624054..d13fd8b 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/SignInWorker.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/SignInWorker.java @@ -29,11 +29,6 @@ import com.microsoft.embeddedsocial.ui.util.SocialNetworkAccount; import android.content.Context; import android.content.Intent; import android.os.Bundle; -import android.util.Base64; - -import java.io.ByteArrayInputStream; -import java.io.InputStream; -import java.io.ObjectInputStream; import androidx.work.OneTimeWorkRequest; import androidx.work.WorkManager; @@ -65,45 +60,43 @@ public class SignInWorker extends Worker { @Override public Result doWork() { String serializedNetworkAccount = getInputData().getString(SOCIAL_NETWORK_ACCOUNT); + SocialNetworkAccount socialNetworkAccount = WorkerSerializationHelper.deserialize(serializedNetworkAccount); + if (serializedNetworkAccount == null) { + UserAccount.getInstance().onSignInWithThirdPartyFailed(); return Result.failure(); } - InputStream inputStream = new ByteArrayInputStream( - Base64.decode(serializedNetworkAccount, Base64.DEFAULT)); + + CreateSessionRequest signInWithThirdPartyRequest = new CreateSessionRequest( + socialNetworkAccount.getAccountType(), + socialNetworkAccount.getThirdPartyAccessToken(), + socialNetworkAccount.getThirdPartyRequestToken()); + try { - ObjectInputStream objectStream = new ObjectInputStream(inputStream); - SocialNetworkAccount socialNetworkAccount = (SocialNetworkAccount)objectStream.readObject(); - CreateSessionRequest signInWithThirdPartyRequest = new CreateSessionRequest( - socialNetworkAccount.getAccountType(), - socialNetworkAccount.getThirdPartyAccessToken(), - socialNetworkAccount.getThirdPartyRequestToken()); + String authorization = signInWithThirdPartyRequest.getAuthorization(); + GetMyProfileRequest getMyProfileRequest = new GetMyProfileRequest(authorization); - try { - String authorization = signInWithThirdPartyRequest.getAuthorization(); - GetMyProfileRequest getMyProfileRequest = new GetMyProfileRequest(authorization); + // Determine the user's user handle + GetUserProfileResponse getUserProfileResponse = getMyProfileRequest.send(); - // Determine the user's user handle - GetUserProfileResponse getUserProfileResponse = getMyProfileRequest.send(); - - // set the user handle and attempt sign in - signInWithThirdPartyRequest.setRequestUserHandle(getUserProfileResponse.getUser().getHandle()); - AuthenticationResponse signInResponse = authenticationService.signInWithThirdParty(signInWithThirdPartyRequest); - handleSuccessfulResult(signInResponse); - } catch (NotFoundException e) { - // User handle not found; create an account - Intent i = new Intent(context, CreateProfileActivity.class); - i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - Bundle extras = new Bundle(); - extras.putParcelable(IntentExtras.THIRD_PARTY_ACCOUNT, socialNetworkAccount); - i.putExtras(extras); - context.startActivity(i); - } finally { - socialNetworkAccount.clearTokens(); - } - } catch (Exception e) { + // set the user handle and attempt sign in + signInWithThirdPartyRequest.setRequestUserHandle(getUserProfileResponse.getUser().getHandle()); + AuthenticationResponse signInResponse = authenticationService.signInWithThirdParty(signInWithThirdPartyRequest); + handleSuccessfulResult(signInResponse); + } catch (NotFoundException e) { + // User handle not found; create an account + Intent i = new Intent(context, CreateProfileActivity.class); + i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + Bundle extras = new Bundle(); + extras.putParcelable(IntentExtras.THIRD_PARTY_ACCOUNT, socialNetworkAccount); + i.putExtras(extras); + context.startActivity(i); + } catch (NetworkRequestException e) { DebugLog.logException(e); UserAccount.getInstance().onSignInWithThirdPartyFailed(); return Result.failure(); + } finally { + socialNetworkAccount.clearTokens(); } return Result.success(); diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/WorkerSerializationHelper.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/WorkerSerializationHelper.java new file mode 100644 index 0000000..0b95f68 --- /dev/null +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/WorkerSerializationHelper.java @@ -0,0 +1,64 @@ +/* + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See LICENSE in the project root for license information. + */ + +package com.microsoft.embeddedsocial.service.worker; + +import com.microsoft.embeddedsocial.base.utils.debug.DebugLog; + +import android.util.Base64; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.Serializable; + +public class WorkerSerializationHelper { + + /** + * Serialize the given Object to a String + * @param data data to serialize + * @return String containing the serialized data + */ + public static String serialize(Serializable data) { + String serializedData = null; + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + try { + ObjectOutputStream os = new ObjectOutputStream(bos); + os.writeObject(data); + serializedData = Base64.encodeToString(bos.toByteArray(), Base64.DEFAULT); + os.close(); + } catch (IOException e) { + DebugLog.logException(e); + } + + return serializedData; + } + + /** + * Deserialize the given string to an object + * @param serializedData data to deserialize + * @param type to cast the data + * @return deserialized object + */ + public static DataType deserialize(String serializedData) { + if (serializedData == null) { + return null; + } + + DataType data = null; + InputStream inputStream = new ByteArrayInputStream(Base64.decode(serializedData, Base64.DEFAULT)); + try { + ObjectInputStream objectInputStream = new ObjectInputStream(inputStream); + data = (DataType)objectInputStream.readObject(); + } catch (ClassNotFoundException|IOException e) { + DebugLog.logException(e); + } + + return data; + } +} diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/CreateProfileFragment.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/CreateProfileFragment.java index 8490da6..cdf50fc 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/CreateProfileFragment.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/CreateProfileFragment.java @@ -10,7 +10,6 @@ import com.microsoft.embeddedsocial.actions.Action; import com.microsoft.embeddedsocial.actions.OngoingActions; import com.microsoft.embeddedsocial.base.utils.BitmapUtils; import com.microsoft.embeddedsocial.base.utils.ViewUtils; -import com.microsoft.embeddedsocial.base.utils.debug.DebugLog; import com.microsoft.embeddedsocial.data.model.AccountData; import com.microsoft.embeddedsocial.data.model.CreateAccountData; import com.microsoft.embeddedsocial.event.signin.CreateUserFailedEvent; @@ -23,6 +22,7 @@ import com.microsoft.embeddedsocial.image.UserPhotoLoader; import com.microsoft.embeddedsocial.sdk.R; import com.microsoft.embeddedsocial.service.IntentExtras; import com.microsoft.embeddedsocial.service.worker.CreateAccountWorker; +import com.microsoft.embeddedsocial.service.worker.WorkerSerializationHelper; import com.microsoft.embeddedsocial.ui.fragment.base.BaseFragmentWithProgress; import com.microsoft.embeddedsocial.ui.fragment.module.PhotoProviderModule; import com.microsoft.embeddedsocial.ui.theme.ThemeAttributes; @@ -40,9 +40,6 @@ import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.ObjectOutputStream; import java.util.LinkedList; import java.util.List; @@ -133,19 +130,9 @@ public class CreateProfileFragment extends BaseFragmentWithProgress { thirdPartyAccount.clearTokens(); - String serializedData = null; - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - try { - ObjectOutputStream os = new ObjectOutputStream(bos); - os.writeObject(createAccountData); - serializedData = android.util.Base64.encodeToString(bos.toByteArray(), android.util.Base64.DEFAULT); - os.close(); - } catch (IOException e) { - DebugLog.logException(e); - } - Data inputData = new Data.Builder() - .putString(CreateAccountWorker.CREATE_ACCOUNT_DATA, serializedData).build(); + .putString(CreateAccountWorker.CREATE_ACCOUNT_DATA, + WorkerSerializationHelper.serialize(createAccountData)).build(); OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(CreateAccountWorker.class) .setInputData(inputData).build(); WorkManager.getInstance().enqueue(workRequest); From 7572144e9b48f01a7acb915af6dbd65d99e22e88 Mon Sep 17 00:00:00 2001 From: Alex Crown Date: Wed, 9 Jan 2019 18:19:37 -0800 Subject: [PATCH 15/26] Prevent NullPointerExceptions in the CreateAccountData object --- .../embeddedsocial/data/model/CreateAccountData.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/data/model/CreateAccountData.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/data/model/CreateAccountData.java index bd7b59b..8ac0886 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/data/model/CreateAccountData.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/data/model/CreateAccountData.java @@ -54,11 +54,19 @@ public class CreateAccountData implements Parcelable, Serializable { } public Uri getPhotoUri() { + if (photoUri == null) { + return null; + } + return Uri.parse(photoUri); } public void setPhotoUri(Uri photoUri) { - this.photoUri = photoUri.toString(); + if (photoUri == null) { + this.photoUri = null; + } else { + this.photoUri = photoUri.toString(); + } } public IdentityProvider getIdentityProvider() { @@ -94,7 +102,7 @@ public class CreateAccountData implements Parcelable, Serializable { this.firstName = in.readString(); this.lastName = in.readString(); this.bio = in.readString(); - this.photoUri = in.readParcelable(Uri.class.getClassLoader()); + this.photoUri = in.readString(); this.identityProvider = IdentityProvider.fromValue(in.readString()); this.thirdPartyAccessToken = in.readString(); this.thirdPartyRequestToken = in.readString(); From 1640d612125e93cc688a708b4bc930c054a71f0e Mon Sep 17 00:00:00 2001 From: Alex Crown Date: Thu, 10 Jan 2019 14:28:56 -0800 Subject: [PATCH 16/26] Use WorkManager in the update account flow --- .../actions/ActionsLauncher.java | 6 --- .../data/model/AccountDataDifference.java | 33 ++++++++---- .../service/IntentProcessorFactory.java | 2 - .../embeddedsocial/service/ServiceAction.java | 2 - .../service/worker/CreateAccountWorker.java | 10 +++- .../UpdateAccountWorker.java} | 37 +++++++------ .../ui/fragment/EditProfileFragment.java | 54 ++++++++++--------- 7 files changed, 78 insertions(+), 66 deletions(-) rename EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/{handler/UpdateAccountHandler.java => worker/UpdateAccountWorker.java} (80%) diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/actions/ActionsLauncher.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/actions/ActionsLauncher.java index de1fee2..bc3891d 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/actions/ActionsLauncher.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/actions/ActionsLauncher.java @@ -23,12 +23,6 @@ public final class ActionsLauncher { private ActionsLauncher() { } - public static Action updateAccount(Context context, AccountDataDifference difference) { - return ActionIntentBuilder.forActionWithTag(Action.Tags.UPDATE_ACCOUNT) - .setAccountDataDifference(difference) - .launch(context, ServiceAction.UPDATE_ACCOUNT); - } - private static Action createAndStartAction(String tag) { Action action = new Action(tag); action.start(); diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/data/model/AccountDataDifference.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/data/model/AccountDataDifference.java index 7ceee02..376a9e3 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/data/model/AccountDataDifference.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/data/model/AccountDataDifference.java @@ -7,16 +7,19 @@ package com.microsoft.embeddedsocial.data.model; import android.net.Uri; import android.os.Parcel; +import android.os.Parcelable; + +import java.io.Serializable; /** * Encapsulates changes in account data. Used to track changes during account editing. */ -public final class AccountDataDifference implements android.os.Parcelable { +public final class AccountDataDifference implements Parcelable, Serializable { private boolean photoUriChanged = false; private boolean coverUriChanged = false; - private Uri photoUri; - private Uri coverUri; + private String photoUri; + private String coverUri; private boolean publicInfoChanged = false; private String firstName; @@ -32,8 +35,8 @@ public final class AccountDataDifference implements android.os.Parcelable { private AccountDataDifference(Parcel in) { this.photoUriChanged = in.readByte() != 0; this.coverUriChanged = in.readByte() != 0; - this.photoUri = in.readParcelable(Uri.class.getClassLoader()); - this.coverUri = in.readParcelable(Uri.class.getClassLoader()); + this.photoUri = in.readString(); + this.coverUri = in.readString(); this.publicInfoChanged = in.readByte() != 0; this.firstName = in.readString(); this.lastName = in.readString(); @@ -54,12 +57,12 @@ public final class AccountDataDifference implements android.os.Parcelable { * @param newPhotoUri local Uri to the new photo (may be null) */ public void setNewPhoto(Uri newPhotoUri) { - photoUri = newPhotoUri; + photoUri = newPhotoUri == null ? null : newPhotoUri.toString(); photoUriChanged = true; } public void setNewCover(Uri newCoverUri) { - coverUri = newCoverUri; + coverUri = newCoverUri == null ? null : newCoverUri.toString(); coverUriChanged = true; } @@ -98,11 +101,19 @@ public final class AccountDataDifference implements android.os.Parcelable { } public Uri getPhotoUri() { - return photoUri; + if (photoUri == null) { + return null; + } + + return Uri.parse(photoUri); } public Uri getCoverUri() { - return coverUri; + if (coverUri == null) { + return null; + } + + return Uri.parse(coverUri); } /** @@ -142,8 +153,8 @@ public final class AccountDataDifference implements android.os.Parcelable { public void writeToParcel(Parcel dest, int flags) { dest.writeByte(photoUriChanged ? (byte) 1 : (byte) 0); dest.writeByte(coverUriChanged ? (byte) 1 : (byte) 0); - dest.writeParcelable(this.photoUri, 0); - dest.writeParcelable(this.coverUri, 0); + dest.writeString(this.photoUri); + dest.writeString(this.coverUri); dest.writeByte(publicInfoChanged ? (byte) 1 : (byte) 0); dest.writeString(this.firstName); dest.writeString(this.lastName); diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/IntentProcessorFactory.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/IntentProcessorFactory.java index c1fc444..8b3a516 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/IntentProcessorFactory.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/IntentProcessorFactory.java @@ -9,7 +9,6 @@ import com.microsoft.embeddedsocial.base.service.IServiceIntentProcessor; import com.microsoft.embeddedsocial.base.service.IntentProcessor; import com.microsoft.embeddedsocial.service.handler.LinkUserThirdPartyAccountHandler; import com.microsoft.embeddedsocial.service.handler.UnlinkUserThirdPartyAccountHandler; -import com.microsoft.embeddedsocial.service.handler.UpdateAccountHandler; import com.microsoft.embeddedsocial.service.handler.UpdateNotificationCountHandler; import android.content.Context; @@ -30,7 +29,6 @@ public class IntentProcessorFactory { IServiceIntentProcessor createIntentProcessor() { IntentProcessor processor = new IntentProcessor<>(context, ServiceAction.class, WORKER_THREADS); - processor.registerIntentHandler(ServiceAction.UPDATE_ACCOUNT, new UpdateAccountHandler(context)); processor.registerIntentHandler(ServiceAction.UPDATE_NOTIFICATION_COUNT, new UpdateNotificationCountHandler()); processor.registerIntentHandler(ServiceAction.LINK_USER_THIRD_PARTY_ACCOUNT, new LinkUserThirdPartyAccountHandler()); processor.registerIntentHandler(ServiceAction.UNLINK_USER_THIRD_PARTY_ACCOUNT, new UnlinkUserThirdPartyAccountHandler()); diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/ServiceAction.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/ServiceAction.java index da06c23..c57cb55 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/ServiceAction.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/ServiceAction.java @@ -10,8 +10,6 @@ package com.microsoft.embeddedsocial.service; */ public enum ServiceAction { - UPDATE_ACCOUNT, - UPDATE_NOTIFICATION_COUNT, LINK_USER_THIRD_PARTY_ACCOUNT, diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/CreateAccountWorker.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/CreateAccountWorker.java index 701ec99..69dc063 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/CreateAccountWorker.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/CreateAccountWorker.java @@ -6,7 +6,6 @@ package com.microsoft.embeddedsocial.service.worker; import com.microsoft.embeddedsocial.account.UserAccount; -import com.microsoft.embeddedsocial.actions.ActionsLauncher; import com.microsoft.embeddedsocial.base.GlobalObjectRegistry; import com.microsoft.embeddedsocial.base.utils.debug.DebugLog; import com.microsoft.embeddedsocial.data.model.AccountData; @@ -25,6 +24,7 @@ import com.microsoft.embeddedsocial.server.model.auth.AuthenticationResponse; import android.content.Context; import android.net.Uri; +import androidx.work.Data; import androidx.work.OneTimeWorkRequest; import androidx.work.WorkManager; import androidx.work.Worker; @@ -96,7 +96,13 @@ public class CreateAccountWorker extends Worker { if (photoUri != null) { AccountDataDifference difference = new AccountDataDifference(); difference.setNewPhoto(photoUri); - ActionsLauncher.updateAccount(context, difference); + + Data inputData = new Data.Builder() + .putString(UpdateAccountWorker.ACCOUNT_DATA_DIFFERENCE, + WorkerSerializationHelper.serialize(difference)).build(); + OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(UpdateAccountWorker.class) + .setInputData(inputData).build(); + WorkManager.getInstance().enqueue(workRequest); } } } diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/UpdateAccountHandler.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/UpdateAccountWorker.java similarity index 80% rename from EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/UpdateAccountHandler.java rename to EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/UpdateAccountWorker.java index 1a1b8f38..342184a 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/UpdateAccountHandler.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/UpdateAccountWorker.java @@ -3,10 +3,9 @@ * Licensed under the MIT License. See LICENSE in the project root for license information. */ -package com.microsoft.embeddedsocial.service.handler; +package com.microsoft.embeddedsocial.service.worker; import com.microsoft.embeddedsocial.account.UserAccount; -import com.microsoft.embeddedsocial.actions.Action; import com.microsoft.embeddedsocial.autorest.models.ImageType; import com.microsoft.embeddedsocial.autorest.models.Visibility; import com.microsoft.embeddedsocial.base.GlobalObjectRegistry; @@ -20,43 +19,44 @@ import com.microsoft.embeddedsocial.server.exception.NetworkRequestException; import com.microsoft.embeddedsocial.server.model.account.UpdateUserPhotoRequest; import com.microsoft.embeddedsocial.server.model.account.UpdateUserPublicAccountInfoRequest; import com.microsoft.embeddedsocial.server.model.account.UpdateUserVisibilityRequest; -import com.microsoft.embeddedsocial.service.IntentExtras; -import com.microsoft.embeddedsocial.service.ServiceAction; import android.content.Context; -import android.content.Intent; import android.net.Uri; -import android.os.Bundle; import java.io.IOException; -/** - * Updates account. - */ -public class UpdateAccountHandler extends ActionHandler { +import androidx.work.Worker; +import androidx.work.WorkerParameters; + +public class UpdateAccountWorker extends Worker { + public static final String ACCOUNT_DATA_DIFFERENCE = "accountDataDifference"; private final Context context; private final IAccountService server = GlobalObjectRegistry.getObject(EmbeddedSocialServiceProvider.class).getAccountService(); - public UpdateAccountHandler(Context context) { + public UpdateAccountWorker(Context context, WorkerParameters workerParams) { + super(context, workerParams); this.context = context; } @Override - protected void handleAction(Action action, ServiceAction serviceAction, Intent intent) { - Bundle extras = intent.getExtras(); - AccountDataDifference difference = extras.getParcelable(IntentExtras.ACCOUNT_DATA_DIFFERENCE); + public Result doWork() { + String serializedData = getInputData().getString(ACCOUNT_DATA_DIFFERENCE); + AccountDataDifference difference = WorkerSerializationHelper.deserialize(serializedData); AccountData accountData = UserAccount.getInstance().getAccountDetails(); + try { updatePhotoIfNeeded(difference, accountData); updatePublicInfoIfNeeded(difference, accountData); updatePrivacyIfNeeded(difference, accountData); } catch (IOException | NetworkRequestException e) { DebugLog.logException(e); - action.fail(); + return Result.failure(); } finally { UserAccount.getInstance().updateAccountDetails(accountData); } + + return Result.success(); } private void updatePrivacyIfNeeded(AccountDataDifference difference, AccountData accountData) throws NetworkRequestException { @@ -70,9 +70,9 @@ public class UpdateAccountHandler extends ActionHandler { private void updatePublicInfoIfNeeded(AccountDataDifference difference, AccountData accountData) throws NetworkRequestException { if (difference.isPublicInfoChanged()) { server.updateUserPublicAccountInfo(new UpdateUserPublicAccountInfoRequest( - difference.getFirstName(), - difference.getLastName(), - difference.getBio())); + difference.getFirstName(), + difference.getLastName(), + difference.getBio())); accountData.setFirstName(difference.getFirstName()); accountData.setLastName(difference.getLastName()); accountData.setBio(difference.getBio()); @@ -87,5 +87,4 @@ public class UpdateAccountHandler extends ActionHandler { accountData.setUserPhotoUrl(photoUrl); } } - } diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/EditProfileFragment.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/EditProfileFragment.java index 87c1972..ce5426f 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/EditProfileFragment.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/EditProfileFragment.java @@ -7,8 +7,6 @@ package com.microsoft.embeddedsocial.ui.fragment; import com.microsoft.embeddedsocial.account.UserAccount; import com.microsoft.embeddedsocial.actions.Action; -import com.microsoft.embeddedsocial.actions.ActionTagFilter; -import com.microsoft.embeddedsocial.actions.ActionsLauncher; import com.microsoft.embeddedsocial.actions.OngoingActions; import com.microsoft.embeddedsocial.base.GlobalObjectRegistry; import com.microsoft.embeddedsocial.base.utils.BitmapUtils; @@ -23,7 +21,8 @@ import com.microsoft.embeddedsocial.image.ImageViewContentLoader; import com.microsoft.embeddedsocial.image.UserPhotoLoader; import com.microsoft.embeddedsocial.sdk.Options; import com.microsoft.embeddedsocial.sdk.R; -import com.microsoft.embeddedsocial.ui.fragment.base.ActionListener; +import com.microsoft.embeddedsocial.service.worker.UpdateAccountWorker; +import com.microsoft.embeddedsocial.service.worker.WorkerSerializationHelper; import com.microsoft.embeddedsocial.ui.fragment.base.BaseFragmentWithProgress; import com.microsoft.embeddedsocial.ui.fragment.module.PhotoProviderModule; import com.microsoft.embeddedsocial.ui.theme.ThemeAttributes; @@ -46,6 +45,15 @@ import java.util.List; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.widget.SwitchCompat; +import androidx.lifecycle.LiveData; +import androidx.work.Data; +import androidx.work.OneTimeWorkRequest; +import androidx.work.WorkInfo; +import androidx.work.WorkManager; + +import static androidx.work.WorkInfo.State.CANCELLED; +import static androidx.work.WorkInfo.State.FAILED; +import static androidx.work.WorkInfo.State.SUCCEEDED; /** * Fragment to edit profile. @@ -82,26 +90,6 @@ public class EditProfileFragment extends BaseFragmentWithProgress { photoProvider = new PhotoProviderModule(this, new SelectProfilePhotoConsumer()); addModule(photoProvider); - addActionListener(new ActionTagFilter(Action.Tags.UPDATE_ACCOUNT), new ActionListener() { - @Override - protected void onActionFailed(Action action, String error) { - onUpdateFailed(); - } - - @Override - protected void onActionSucceeded(Action action) { - onUpdateSucceeded(); - } - - @Override - protected void onActionsCompletionMissed(List completedActions, List succeededActions, List failedActions) { - if (!succeededActions.isEmpty()) { - onUpdateSucceeded(); - } else if (!failedActions.isEmpty()) { - onUpdateFailed(); - } - } - }); } @Override @@ -146,7 +134,25 @@ public class EditProfileFragment extends BaseFragmentWithProgress { finishActivity(); } else { setProgressVisible(true); - ActionsLauncher.updateAccount(getContext(), difference); + + Data inputData = new Data.Builder() + .putString(UpdateAccountWorker.ACCOUNT_DATA_DIFFERENCE, + WorkerSerializationHelper.serialize(difference)).build(); + OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(UpdateAccountWorker.class) + .setInputData(inputData).build(); + WorkManager.getInstance().enqueue(workRequest); + + LiveData liveData = WorkManager.getInstance().getWorkInfoByIdLiveData(workRequest.getId()); + liveData.observe(this, workInfo -> { + WorkInfo.State state = workInfo.getState(); + if (state.isFinished()) { + if (state.equals(SUCCEEDED)) { + getActivity().runOnUiThread(() -> onUpdateSucceeded()); + } else if (state.equals(FAILED) || state.equals(CANCELLED)) { + getActivity().runOnUiThread(() -> onUpdateFailed()); + } + } + }); } } } From 972f32394ee308f56423ace948f9dbd70f380f6c Mon Sep 17 00:00:00 2001 From: Alex Crown Date: Thu, 10 Jan 2019 16:21:00 -0800 Subject: [PATCH 17/26] Use a helper class to abstract away the logic of waiting for a work request to complete --- .../embeddedsocial/account/UserAccount.java | 4 +- .../service/worker/CreateAccountWorker.java | 4 +- .../service/worker/SignInWorker.java | 2 +- .../service/worker/UpdateAccountWorker.java | 2 +- ...alizationHelper.java => WorkerHelper.java} | 43 ++++++++++++++++++- .../adapter/renderer/MyFollowersRenderer.java | 16 +++---- .../ui/fragment/CreateProfileFragment.java | 4 +- .../ui/fragment/DeleteAccountFragment.java | 26 +++++------ .../ui/fragment/EditProfileFragment.java | 28 +++++------- 9 files changed, 78 insertions(+), 51 deletions(-) rename EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/{WorkerSerializationHelper.java => WorkerHelper.java} (56%) diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/account/UserAccount.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/account/UserAccount.java index 2a2417c..c785a4c 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/account/UserAccount.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/account/UserAccount.java @@ -30,7 +30,7 @@ import com.microsoft.embeddedsocial.pending.PendingFollow; import com.microsoft.embeddedsocial.server.model.view.UserCompactView; import com.microsoft.embeddedsocial.service.worker.SignInWorker; import com.microsoft.embeddedsocial.service.worker.SignOutWorker; -import com.microsoft.embeddedsocial.service.worker.WorkerSerializationHelper; +import com.microsoft.embeddedsocial.service.worker.WorkerHelper; import com.microsoft.embeddedsocial.ui.util.NotificationCountChecker; import com.microsoft.embeddedsocial.ui.util.SocialNetworkAccount; @@ -70,7 +70,7 @@ public class UserAccount { public Operation signInUsingThirdParty(SocialNetworkAccount thirdPartyAccount) { Data inputData = new Data.Builder() .putString(SignInWorker.SOCIAL_NETWORK_ACCOUNT, - WorkerSerializationHelper.serialize(thirdPartyAccount)).build(); + WorkerHelper.serialize(thirdPartyAccount)).build(); OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(SignInWorker.class) .setInputData(inputData).addTag(SignInWorker.TAG).build(); return WorkManager.getInstance().enqueue(workRequest); diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/CreateAccountWorker.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/CreateAccountWorker.java index 69dc063..5531172 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/CreateAccountWorker.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/CreateAccountWorker.java @@ -47,7 +47,7 @@ public class CreateAccountWorker extends Worker { @Override public Result doWork() { String serializedAccountData = getInputData().getString(CREATE_ACCOUNT_DATA); - CreateAccountData createAccountData = WorkerSerializationHelper.deserialize(serializedAccountData); + CreateAccountData createAccountData = WorkerHelper.deserialize(serializedAccountData); if (serializedAccountData == null) { UserAccount.getInstance().onCreateUserFailed(); @@ -99,7 +99,7 @@ public class CreateAccountWorker extends Worker { Data inputData = new Data.Builder() .putString(UpdateAccountWorker.ACCOUNT_DATA_DIFFERENCE, - WorkerSerializationHelper.serialize(difference)).build(); + WorkerHelper.serialize(difference)).build(); OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(UpdateAccountWorker.class) .setInputData(inputData).build(); WorkManager.getInstance().enqueue(workRequest); diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/SignInWorker.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/SignInWorker.java index d13fd8b..cfd6c39 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/SignInWorker.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/SignInWorker.java @@ -60,7 +60,7 @@ public class SignInWorker extends Worker { @Override public Result doWork() { String serializedNetworkAccount = getInputData().getString(SOCIAL_NETWORK_ACCOUNT); - SocialNetworkAccount socialNetworkAccount = WorkerSerializationHelper.deserialize(serializedNetworkAccount); + SocialNetworkAccount socialNetworkAccount = WorkerHelper.deserialize(serializedNetworkAccount); if (serializedNetworkAccount == null) { UserAccount.getInstance().onSignInWithThirdPartyFailed(); diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/UpdateAccountWorker.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/UpdateAccountWorker.java index 342184a..fecccbe 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/UpdateAccountWorker.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/UpdateAccountWorker.java @@ -42,7 +42,7 @@ public class UpdateAccountWorker extends Worker { @Override public Result doWork() { String serializedData = getInputData().getString(ACCOUNT_DATA_DIFFERENCE); - AccountDataDifference difference = WorkerSerializationHelper.deserialize(serializedData); + AccountDataDifference difference = WorkerHelper.deserialize(serializedData); AccountData accountData = UserAccount.getInstance().getAccountDetails(); try { diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/WorkerSerializationHelper.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/WorkerHelper.java similarity index 56% rename from EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/WorkerSerializationHelper.java rename to EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/WorkerHelper.java index 0b95f68..e8283f2 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/WorkerSerializationHelper.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/WorkerHelper.java @@ -16,8 +16,21 @@ import java.io.InputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; +import java.util.UUID; -public class WorkerSerializationHelper { +import androidx.lifecycle.LifecycleOwner; +import androidx.lifecycle.LiveData; +import androidx.work.WorkInfo; +import androidx.work.WorkManager; + +import static androidx.work.WorkInfo.State.CANCELLED; +import static androidx.work.WorkInfo.State.FAILED; +import static androidx.work.WorkInfo.State.SUCCEEDED; + +/** + * Implements utility functions to aid in setting up and handling androidx + */ +public class WorkerHelper { /** * Serialize the given Object to a String @@ -61,4 +74,32 @@ public class WorkerSerializationHelper { return data; } + + /** + * Handle the result of a completed work request + * @param lifecycleOwner the lifecycle owner used to observe the work request + * @param workerId ID of the work request + * @param handler ResultHandler to invoke upon work completion + */ + public static void handleResult(LifecycleOwner lifecycleOwner, UUID workerId, ResultHandler handler) { + LiveData liveData = WorkManager.getInstance().getWorkInfoByIdLiveData(workerId); + liveData.observe(lifecycleOwner, workInfo -> { + WorkInfo.State state = workInfo.getState(); + if (state.isFinished()) { + if (state.equals(SUCCEEDED)) { + handler.onSuccess(); + } else if (state.equals(FAILED) || state.equals(CANCELLED)) { + handler.onFailure(); + } + } + }); + } + + /** + * Defines functions to run upon success or failure of a single work request + */ + public interface ResultHandler { + void onSuccess(); + void onFailure(); + } } diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/adapter/renderer/MyFollowersRenderer.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/adapter/renderer/MyFollowersRenderer.java index 3a80a45..5265021 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/adapter/renderer/MyFollowersRenderer.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/adapter/renderer/MyFollowersRenderer.java @@ -11,22 +11,18 @@ import com.microsoft.embeddedsocial.data.model.AccountData; import com.microsoft.embeddedsocial.sdk.R; import com.microsoft.embeddedsocial.server.model.view.UserCompactView; import com.microsoft.embeddedsocial.service.worker.RemoveFollowerWorker; +import com.microsoft.embeddedsocial.service.worker.WorkerHelper; import com.microsoft.embeddedsocial.ui.adapter.viewholder.UserListItemHolder; import android.view.View; import android.widget.TextView; import androidx.fragment.app.Fragment; -import androidx.lifecycle.LiveData; import androidx.lifecycle.ProcessLifecycleOwner; import androidx.work.Data; import androidx.work.OneTimeWorkRequest; -import androidx.work.WorkInfo; -import androidx.work.WorkInfo.State; import androidx.work.WorkManager; -import static androidx.work.WorkInfo.State.SUCCEEDED; - /** * Renders my followers with context menu. */ @@ -60,16 +56,18 @@ public class MyFollowersRenderer extends UserRenderer { .setInputData(inputData).build(); WorkManager.getInstance().enqueue(workRequest); - LiveData liveData = WorkManager.getInstance().getWorkInfoByIdLiveData(workRequest.getId()); - liveData.observe(ProcessLifecycleOwner.get(), workInfo -> { - State state = workInfo.getState(); - if (state.equals(SUCCEEDED)) { + WorkerHelper.handleResult(ProcessLifecycleOwner.get(), workRequest.getId(), new WorkerHelper.ResultHandler() { + @Override + public void onSuccess() { // redraw the button button.setText(R.string.es_removed_follower); getStyleHelper().applyRedCompletedStyle(button); // decrement local followers count currUser.setFollowersCount(Math.max(0, currUser.getFollowersCount() - 1)); } + + @Override + public void onFailure() { } }); }); button.setText(R.string.es_remove_follower); diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/CreateProfileFragment.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/CreateProfileFragment.java index cdf50fc..5f58337 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/CreateProfileFragment.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/CreateProfileFragment.java @@ -22,7 +22,7 @@ import com.microsoft.embeddedsocial.image.UserPhotoLoader; import com.microsoft.embeddedsocial.sdk.R; import com.microsoft.embeddedsocial.service.IntentExtras; import com.microsoft.embeddedsocial.service.worker.CreateAccountWorker; -import com.microsoft.embeddedsocial.service.worker.WorkerSerializationHelper; +import com.microsoft.embeddedsocial.service.worker.WorkerHelper; import com.microsoft.embeddedsocial.ui.fragment.base.BaseFragmentWithProgress; import com.microsoft.embeddedsocial.ui.fragment.module.PhotoProviderModule; import com.microsoft.embeddedsocial.ui.theme.ThemeAttributes; @@ -132,7 +132,7 @@ public class CreateProfileFragment extends BaseFragmentWithProgress { Data inputData = new Data.Builder() .putString(CreateAccountWorker.CREATE_ACCOUNT_DATA, - WorkerSerializationHelper.serialize(createAccountData)).build(); + WorkerHelper.serialize(createAccountData)).build(); OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(CreateAccountWorker.class) .setInputData(inputData).build(); WorkManager.getInstance().enqueue(workRequest); diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/DeleteAccountFragment.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/DeleteAccountFragment.java index b443119..7967c82 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/DeleteAccountFragment.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/DeleteAccountFragment.java @@ -9,22 +9,16 @@ import com.microsoft.embeddedsocial.actions.Action; import com.microsoft.embeddedsocial.actions.OngoingActions; import com.microsoft.embeddedsocial.sdk.R; import com.microsoft.embeddedsocial.service.worker.DeleteAccountWorker; +import com.microsoft.embeddedsocial.service.worker.WorkerHelper; import com.microsoft.embeddedsocial.ui.fragment.base.BaseFragmentWithProgress; import android.os.Bundle; import android.view.View; import androidx.annotation.Nullable; -import androidx.lifecycle.LiveData; import androidx.work.OneTimeWorkRequest; -import androidx.work.WorkInfo; -import androidx.work.WorkInfo.State; import androidx.work.WorkManager; -import static androidx.work.WorkInfo.State.CANCELLED; -import static androidx.work.WorkInfo.State.FAILED; -import static androidx.work.WorkInfo.State.SUCCEEDED; - /** * Fragment for deleting an account. */ @@ -44,15 +38,15 @@ public class DeleteAccountFragment extends BaseFragmentWithProgress { OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(DeleteAccountWorker.class) .build(); WorkManager.getInstance().enqueue(workRequest); - LiveData liveData = WorkManager.getInstance().getWorkInfoByIdLiveData(workRequest.getId()); - liveData.observe(this, workInfo -> { - State state = workInfo.getState(); - if (state.isFinished()) { - if (state.equals(SUCCEEDED)) { - getActivity().runOnUiThread(() -> onAccountDeleted()); - } else if (state.equals(FAILED) || state.equals(CANCELLED)) { - getActivity().runOnUiThread(() -> onError()); - } + WorkerHelper.handleResult(this, workRequest.getId(), new WorkerHelper.ResultHandler() { + @Override + public void onSuccess() { + getActivity().runOnUiThread(() -> onAccountDeleted()); + } + + @Override + public void onFailure() { + getActivity().runOnUiThread(() -> onError()); } }); }); diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/EditProfileFragment.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/EditProfileFragment.java index ce5426f..73cd175 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/EditProfileFragment.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/EditProfileFragment.java @@ -22,7 +22,7 @@ import com.microsoft.embeddedsocial.image.UserPhotoLoader; import com.microsoft.embeddedsocial.sdk.Options; import com.microsoft.embeddedsocial.sdk.R; import com.microsoft.embeddedsocial.service.worker.UpdateAccountWorker; -import com.microsoft.embeddedsocial.service.worker.WorkerSerializationHelper; +import com.microsoft.embeddedsocial.service.worker.WorkerHelper; import com.microsoft.embeddedsocial.ui.fragment.base.BaseFragmentWithProgress; import com.microsoft.embeddedsocial.ui.fragment.module.PhotoProviderModule; import com.microsoft.embeddedsocial.ui.theme.ThemeAttributes; @@ -45,16 +45,10 @@ import java.util.List; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.widget.SwitchCompat; -import androidx.lifecycle.LiveData; import androidx.work.Data; import androidx.work.OneTimeWorkRequest; -import androidx.work.WorkInfo; import androidx.work.WorkManager; -import static androidx.work.WorkInfo.State.CANCELLED; -import static androidx.work.WorkInfo.State.FAILED; -import static androidx.work.WorkInfo.State.SUCCEEDED; - /** * Fragment to edit profile. */ @@ -137,20 +131,20 @@ public class EditProfileFragment extends BaseFragmentWithProgress { Data inputData = new Data.Builder() .putString(UpdateAccountWorker.ACCOUNT_DATA_DIFFERENCE, - WorkerSerializationHelper.serialize(difference)).build(); + WorkerHelper.serialize(difference)).build(); OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(UpdateAccountWorker.class) .setInputData(inputData).build(); WorkManager.getInstance().enqueue(workRequest); - LiveData liveData = WorkManager.getInstance().getWorkInfoByIdLiveData(workRequest.getId()); - liveData.observe(this, workInfo -> { - WorkInfo.State state = workInfo.getState(); - if (state.isFinished()) { - if (state.equals(SUCCEEDED)) { - getActivity().runOnUiThread(() -> onUpdateSucceeded()); - } else if (state.equals(FAILED) || state.equals(CANCELLED)) { - getActivity().runOnUiThread(() -> onUpdateFailed()); - } + WorkerHelper.handleResult(this, workRequest.getId(), new WorkerHelper.ResultHandler() { + @Override + public void onSuccess() { + getActivity().runOnUiThread(() -> onUpdateSucceeded()); + } + + @Override + public void onFailure() { + getActivity().runOnUiThread(() -> onUpdateFailed()); } }); } From 280db23e0cab0e918d6d6099fca505212ee1e003 Mon Sep 17 00:00:00 2001 From: Alex Crown Date: Thu, 10 Jan 2019 17:28:59 -0800 Subject: [PATCH 18/26] Use WorkManager to update notification counts --- .../worker/UpdateNotificationCountWorker.java | 44 +++++++++++++++++++ .../ui/fragment/NavigationFragment.java | 2 +- .../ui/util/NotificationCountChecker.java | 15 ++++--- 3 files changed, 53 insertions(+), 8 deletions(-) create mode 100644 EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/UpdateNotificationCountWorker.java diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/UpdateNotificationCountWorker.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/UpdateNotificationCountWorker.java new file mode 100644 index 0000000..42bea16 --- /dev/null +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/UpdateNotificationCountWorker.java @@ -0,0 +1,44 @@ +/* + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See LICENSE in the project root for license information. + */ + +package com.microsoft.embeddedsocial.service.worker; + +import com.microsoft.embeddedsocial.autorest.models.CountResponse; +import com.microsoft.embeddedsocial.base.GlobalObjectRegistry; +import com.microsoft.embeddedsocial.base.utils.debug.DebugLog; +import com.microsoft.embeddedsocial.data.Preferences; +import com.microsoft.embeddedsocial.server.EmbeddedSocialServiceProvider; +import com.microsoft.embeddedsocial.server.INotificationService; +import com.microsoft.embeddedsocial.server.exception.NetworkRequestException; +import com.microsoft.embeddedsocial.server.model.notification.GetNotificationCountRequest; + +import android.content.Context; + +import androidx.work.Worker; +import androidx.work.WorkerParameters; + +/** + * Updates notification count from the server. + */ +public class UpdateNotificationCountWorker extends Worker { + public UpdateNotificationCountWorker(Context context, WorkerParameters workerParams) { + super(context, workerParams); + } + + @Override + public Result doWork() { + INotificationService server = GlobalObjectRegistry.getObject(EmbeddedSocialServiceProvider.class).getNotificationService(); + try { + CountResponse response = server.getNotificationCount(new GetNotificationCountRequest()); + long notificationCount = response.getCount(); + Preferences.getInstance().setNotificationCount(notificationCount); + } catch (NetworkRequestException e) { + DebugLog.logException(e); + return Result.failure(); + } + + return Result.success(); + } +} diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/NavigationFragment.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/NavigationFragment.java index 671a3e3..7ab8ea5 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/NavigationFragment.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/NavigationFragment.java @@ -76,7 +76,7 @@ public class NavigationFragment extends BaseFragment { @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); - notificationCountChecker = new NotificationCountChecker(getContext()); + notificationCountChecker = new NotificationCountChecker(); activeItemId = getArguments().getInt(CURRENT_ITEM_EXTRA); isShowProfile = getArguments().getBoolean(SHOW_PROFILE_EXTRA); } diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/util/NotificationCountChecker.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/util/NotificationCountChecker.java index eafea2e..8d57705 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/util/NotificationCountChecker.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/util/NotificationCountChecker.java @@ -6,15 +6,16 @@ package com.microsoft.embeddedsocial.ui.util; import com.microsoft.embeddedsocial.account.UserAccount; -import com.microsoft.embeddedsocial.service.ServiceAction; -import com.microsoft.embeddedsocial.service.WorkerService; +import com.microsoft.embeddedsocial.service.worker.UpdateNotificationCountWorker; -import android.content.Context; import android.os.Handler; import android.os.Looper; import android.os.SystemClock; import android.text.format.DateUtils; +import androidx.work.OneTimeWorkRequest; +import androidx.work.WorkManager; + /** * Periodically updates notification count. */ @@ -24,12 +25,10 @@ public class NotificationCountChecker { private static long lastUpdateTime; - private final Context context; private final Handler handler; private Runnable updateTask = this::updateNotificationCount; - public NotificationCountChecker(Context context) { - this.context = context; + public NotificationCountChecker() { handler = new Handler(Looper.getMainLooper()); } @@ -50,7 +49,9 @@ public class NotificationCountChecker { private void updateNotificationCount() { lastUpdateTime = SystemClock.elapsedRealtime(); - WorkerService.getLauncher(context).launchService(ServiceAction.UPDATE_NOTIFICATION_COUNT); + OneTimeWorkRequest workRequest = + new OneTimeWorkRequest.Builder(UpdateNotificationCountWorker.class).build(); + WorkManager.getInstance().enqueue(workRequest); handler.postDelayed(updateTask, PERIOD); } From 80bd5df6f3cb124576f382f9ed5168291ddaabfb Mon Sep 17 00:00:00 2001 From: Alex Crown Date: Thu, 10 Jan 2019 17:29:16 -0800 Subject: [PATCH 19/26] Add comments to various workers --- .../service/worker/BackgroundInitializationWorker.java | 3 +++ .../embeddedsocial/service/worker/CreateAccountWorker.java | 3 +++ .../service/worker/DeleteSearchHistoryWorker.java | 3 +++ .../embeddedsocial/service/worker/GetFcmIdWorker.java | 3 +++ .../embeddedsocial/service/worker/SynchronizationWorker.java | 3 +++ 5 files changed, 15 insertions(+) diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/BackgroundInitializationWorker.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/BackgroundInitializationWorker.java index 95dc288..def800b 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/BackgroundInitializationWorker.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/BackgroundInitializationWorker.java @@ -12,6 +12,9 @@ import androidx.work.WorkManager; import androidx.work.Worker; import androidx.work.WorkerParameters; +/** + * Initializes the SDK for use + */ public class BackgroundInitializationWorker extends Worker { public BackgroundInitializationWorker(Context context, WorkerParameters workerParameters) { diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/CreateAccountWorker.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/CreateAccountWorker.java index 5531172..8ea91dc 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/CreateAccountWorker.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/CreateAccountWorker.java @@ -30,6 +30,9 @@ import androidx.work.WorkManager; import androidx.work.Worker; import androidx.work.WorkerParameters; +/** + * Sends a create account request to the server and updates the account's profile image if necessary + */ public class CreateAccountWorker extends Worker { public static final String CREATE_ACCOUNT_DATA = "createAccountData"; diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/DeleteSearchHistoryWorker.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/DeleteSearchHistoryWorker.java index dbe30f3..7e861b7 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/DeleteSearchHistoryWorker.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/DeleteSearchHistoryWorker.java @@ -12,6 +12,9 @@ import android.content.Context; import androidx.work.Worker; import androidx.work.WorkerParameters; +/** + * Deletes search history from the device + */ public class DeleteSearchHistoryWorker extends Worker { public DeleteSearchHistoryWorker(Context context, WorkerParameters workerParameters) { diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/GetFcmIdWorker.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/GetFcmIdWorker.java index 6c9c69a..bf9805e 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/GetFcmIdWorker.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/GetFcmIdWorker.java @@ -21,6 +21,9 @@ import androidx.work.WorkManager; import androidx.work.Worker; import androidx.work.WorkerParameters; +/** + * Gets FCM tokens and stores them + */ public class GetFcmIdWorker extends Worker { private final Context context; diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/SynchronizationWorker.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/SynchronizationWorker.java index 17c6632..3ec4f06 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/SynchronizationWorker.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/SynchronizationWorker.java @@ -18,6 +18,9 @@ import android.content.Context; import androidx.work.Worker; import androidx.work.WorkerParameters; +/** + * Synchronizes local data with the server + */ public class SynchronizationWorker extends Worker { public static final String PENDING_POST_SYNC_NAME = "posts"; From 39fe8a9993f42fc5e2f3d259b9f82870a33d512a Mon Sep 17 00:00:00 2001 From: Alex Crown Date: Thu, 10 Jan 2019 17:44:46 -0800 Subject: [PATCH 20/26] Use WorkManager to handle requests to link accounts --- .../service/IntentProcessorFactory.java | 2 -- .../UnlinkUserThirdPartyAccountWorker.java} | 34 +++++++++++-------- .../ui/fragment/LinkedAccountsFragment.java | 14 ++++++-- 3 files changed, 31 insertions(+), 19 deletions(-) rename EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/{handler/UnlinkUserThirdPartyAccountHandler.java => worker/UnlinkUserThirdPartyAccountWorker.java} (69%) diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/IntentProcessorFactory.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/IntentProcessorFactory.java index 8b3a516..3e80022 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/IntentProcessorFactory.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/IntentProcessorFactory.java @@ -8,7 +8,6 @@ package com.microsoft.embeddedsocial.service; import com.microsoft.embeddedsocial.base.service.IServiceIntentProcessor; import com.microsoft.embeddedsocial.base.service.IntentProcessor; import com.microsoft.embeddedsocial.service.handler.LinkUserThirdPartyAccountHandler; -import com.microsoft.embeddedsocial.service.handler.UnlinkUserThirdPartyAccountHandler; import com.microsoft.embeddedsocial.service.handler.UpdateNotificationCountHandler; import android.content.Context; @@ -31,7 +30,6 @@ public class IntentProcessorFactory { processor.registerIntentHandler(ServiceAction.UPDATE_NOTIFICATION_COUNT, new UpdateNotificationCountHandler()); processor.registerIntentHandler(ServiceAction.LINK_USER_THIRD_PARTY_ACCOUNT, new LinkUserThirdPartyAccountHandler()); - processor.registerIntentHandler(ServiceAction.UNLINK_USER_THIRD_PARTY_ACCOUNT, new UnlinkUserThirdPartyAccountHandler()); return processor; } diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/UnlinkUserThirdPartyAccountHandler.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/UnlinkUserThirdPartyAccountWorker.java similarity index 69% rename from EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/UnlinkUserThirdPartyAccountHandler.java rename to EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/UnlinkUserThirdPartyAccountWorker.java index 7f64355..66dde98 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/UnlinkUserThirdPartyAccountHandler.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/UnlinkUserThirdPartyAccountWorker.java @@ -3,30 +3,37 @@ * Licensed under the MIT License. See LICENSE in the project root for license information. */ -package com.microsoft.embeddedsocial.service.handler; +package com.microsoft.embeddedsocial.service.worker; import com.microsoft.embeddedsocial.autorest.models.IdentityProvider; import com.microsoft.embeddedsocial.base.GlobalObjectRegistry; import com.microsoft.embeddedsocial.base.event.EventBus; -import com.microsoft.embeddedsocial.base.service.IServiceIntentHandler; import com.microsoft.embeddedsocial.base.utils.debug.DebugLog; import com.microsoft.embeddedsocial.event.LinkUserThirdPartyAccountEvent; import com.microsoft.embeddedsocial.server.EmbeddedSocialServiceProvider; import com.microsoft.embeddedsocial.server.IAccountService; import com.microsoft.embeddedsocial.server.exception.StatusException; import com.microsoft.embeddedsocial.server.model.account.UnlinkUserThirdPartyAccountRequest; -import com.microsoft.embeddedsocial.service.IntentExtras; -import com.microsoft.embeddedsocial.service.ServiceAction; -import android.content.Intent; +import android.content.Context; + +import androidx.work.Worker; +import androidx.work.WorkerParameters; /** * Sends a unlink user third party account request to the server. */ -public class UnlinkUserThirdPartyAccountHandler implements IServiceIntentHandler { +public class UnlinkUserThirdPartyAccountWorker extends Worker { + public static final String IDENTITY_PROVIDER = "identityProvider"; + + public UnlinkUserThirdPartyAccountWorker(Context context, WorkerParameters workerParams) { + super(context, workerParams); + } + @Override - public void handleIntent(ServiceAction action, Intent intent) { - final IdentityProvider accountType = IdentityProvider.fromValue(intent.getExtras().getString(IntentExtras.IDENTITY_PROVIDER)); + public Result doWork() { + final IdentityProvider accountType = + IdentityProvider.fromValue(getInputData().getString(IDENTITY_PROVIDER)); IAccountService accountService = GlobalObjectRegistry.getObject(EmbeddedSocialServiceProvider.class).getAccountService(); UnlinkUserThirdPartyAccountRequest unlinkUserThirdPartyAccountRequest @@ -35,21 +42,20 @@ public class UnlinkUserThirdPartyAccountHandler implements IServiceIntentHandler try { accountService.unlinkUserThirdPartyAccount(unlinkUserThirdPartyAccountRequest); EventBus.post(LinkUserThirdPartyAccountEvent.createUnlinkEvent(accountType)); + return Result.success(); } catch (Exception e) { DebugLog.logException(e); LinkUserThirdPartyAccountEvent event; + // Notify the handler that the request failed if (e instanceof StatusException) { event = LinkUserThirdPartyAccountEvent.createUnlinkEvent(accountType, e.getMessage(), - ((StatusException)e).getStatusCode()); + ((StatusException) e).getStatusCode()); } else { event = LinkUserThirdPartyAccountEvent.createUnlinkEvent(accountType, e.getMessage()); } + EventBus.post(event); + return Result.failure(); } } - - @Override - public void dispose() { - - } } diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/LinkedAccountsFragment.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/LinkedAccountsFragment.java index 830a695..dacbf54 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/LinkedAccountsFragment.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/LinkedAccountsFragment.java @@ -24,6 +24,7 @@ import com.microsoft.embeddedsocial.server.model.view.ThirdPartyAccountView; import com.microsoft.embeddedsocial.service.IntentExtras; import com.microsoft.embeddedsocial.service.ServiceAction; import com.microsoft.embeddedsocial.service.WorkerService; +import com.microsoft.embeddedsocial.service.worker.UnlinkUserThirdPartyAccountWorker; import com.microsoft.embeddedsocial.ui.adapter.LinkedAccountsAdapter; import com.microsoft.embeddedsocial.ui.dialog.AlertDialogFragment; import com.microsoft.embeddedsocial.ui.fragment.base.BaseListContentFragment; @@ -37,6 +38,10 @@ import android.widget.Toast; import java.util.ArrayList; import java.util.List; +import androidx.work.Data; +import androidx.work.OneTimeWorkRequest; +import androidx.work.WorkManager; + /** * Screen with linked accounts state. */ @@ -103,9 +108,12 @@ public class LinkedAccountsFragment extends BaseListContentFragment Date: Thu, 10 Jan 2019 17:46:13 -0800 Subject: [PATCH 21/26] Remove unused class UpdateNotificationCountHandler --- .../service/IntentProcessorFactory.java | 2 - .../UpdateNotificationCountHandler.java | 41 ------------------- 2 files changed, 43 deletions(-) delete mode 100644 EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/UpdateNotificationCountHandler.java diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/IntentProcessorFactory.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/IntentProcessorFactory.java index 3e80022..88ddc20 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/IntentProcessorFactory.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/IntentProcessorFactory.java @@ -8,7 +8,6 @@ package com.microsoft.embeddedsocial.service; import com.microsoft.embeddedsocial.base.service.IServiceIntentProcessor; import com.microsoft.embeddedsocial.base.service.IntentProcessor; import com.microsoft.embeddedsocial.service.handler.LinkUserThirdPartyAccountHandler; -import com.microsoft.embeddedsocial.service.handler.UpdateNotificationCountHandler; import android.content.Context; @@ -28,7 +27,6 @@ public class IntentProcessorFactory { IServiceIntentProcessor createIntentProcessor() { IntentProcessor processor = new IntentProcessor<>(context, ServiceAction.class, WORKER_THREADS); - processor.registerIntentHandler(ServiceAction.UPDATE_NOTIFICATION_COUNT, new UpdateNotificationCountHandler()); processor.registerIntentHandler(ServiceAction.LINK_USER_THIRD_PARTY_ACCOUNT, new LinkUserThirdPartyAccountHandler()); return processor; diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/UpdateNotificationCountHandler.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/UpdateNotificationCountHandler.java deleted file mode 100644 index 9b1c892..0000000 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/UpdateNotificationCountHandler.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (c) Microsoft Corporation. All rights reserved. - * Licensed under the MIT License. See LICENSE in the project root for license information. - */ - -package com.microsoft.embeddedsocial.service.handler; - -import com.microsoft.embeddedsocial.autorest.models.CountResponse; -import com.microsoft.embeddedsocial.base.GlobalObjectRegistry; -import com.microsoft.embeddedsocial.base.service.IServiceIntentHandler; -import com.microsoft.embeddedsocial.base.utils.debug.DebugLog; -import com.microsoft.embeddedsocial.data.Preferences; -import com.microsoft.embeddedsocial.server.EmbeddedSocialServiceProvider; -import com.microsoft.embeddedsocial.server.INotificationService; -import com.microsoft.embeddedsocial.server.exception.NetworkRequestException; -import com.microsoft.embeddedsocial.server.model.notification.GetNotificationCountRequest; -import com.microsoft.embeddedsocial.service.ServiceAction; - -import android.content.Intent; - -/** - * Updates notification count from the server. - */ -public class UpdateNotificationCountHandler implements IServiceIntentHandler { - @Override - public void handleIntent(ServiceAction action, Intent intent) { - INotificationService server = GlobalObjectRegistry.getObject(EmbeddedSocialServiceProvider.class).getNotificationService(); - try { - CountResponse response = server.getNotificationCount(new GetNotificationCountRequest()); - long notificationCount = response.getCount(); - Preferences.getInstance().setNotificationCount(notificationCount); - } catch (NetworkRequestException e) { - DebugLog.logException(e); - } - } - - @Override - public void dispose() { - - } -} From 388de249cd03d24e9307b113529fbd5c1a66c3cc Mon Sep 17 00:00:00 2001 From: Alex Crown Date: Thu, 10 Jan 2019 17:59:51 -0800 Subject: [PATCH 22/26] Use WorkManager to handle link third party account requests --- .../service/IntentProcessorFactory.java | 3 -- .../embeddedsocial/service/ServiceAction.java | 5 --- .../LinkUserThirdPartyAccountWorker.java} | 35 +++++++++---------- .../UnlinkUserThirdPartyAccountWorker.java | 2 +- .../ui/fragment/LinkedAccountsFragment.java | 16 +++++---- 5 files changed, 26 insertions(+), 35 deletions(-) rename EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/{handler/LinkUserThirdPartyAccountHandler.java => worker/LinkUserThirdPartyAccountWorker.java} (69%) diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/IntentProcessorFactory.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/IntentProcessorFactory.java index 88ddc20..258b072 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/IntentProcessorFactory.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/IntentProcessorFactory.java @@ -7,7 +7,6 @@ package com.microsoft.embeddedsocial.service; import com.microsoft.embeddedsocial.base.service.IServiceIntentProcessor; import com.microsoft.embeddedsocial.base.service.IntentProcessor; -import com.microsoft.embeddedsocial.service.handler.LinkUserThirdPartyAccountHandler; import android.content.Context; @@ -27,8 +26,6 @@ public class IntentProcessorFactory { IServiceIntentProcessor createIntentProcessor() { IntentProcessor processor = new IntentProcessor<>(context, ServiceAction.class, WORKER_THREADS); - processor.registerIntentHandler(ServiceAction.LINK_USER_THIRD_PARTY_ACCOUNT, new LinkUserThirdPartyAccountHandler()); - return processor; } } diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/ServiceAction.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/ServiceAction.java index c57cb55..ea1f200 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/ServiceAction.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/ServiceAction.java @@ -10,9 +10,4 @@ package com.microsoft.embeddedsocial.service; */ public enum ServiceAction { - UPDATE_NOTIFICATION_COUNT, - - LINK_USER_THIRD_PARTY_ACCOUNT, - - UNLINK_USER_THIRD_PARTY_ACCOUNT } diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/LinkUserThirdPartyAccountHandler.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/LinkUserThirdPartyAccountWorker.java similarity index 69% rename from EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/LinkUserThirdPartyAccountHandler.java rename to EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/LinkUserThirdPartyAccountWorker.java index a34cb6e..e33124e 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/LinkUserThirdPartyAccountHandler.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/LinkUserThirdPartyAccountWorker.java @@ -1,13 +1,7 @@ -/* - * Copyright (c) Microsoft Corporation. All rights reserved. - * Licensed under the MIT License. See LICENSE in the project root for license information. - */ - -package com.microsoft.embeddedsocial.service.handler; +package com.microsoft.embeddedsocial.service.worker; import com.microsoft.embeddedsocial.base.GlobalObjectRegistry; import com.microsoft.embeddedsocial.base.event.EventBus; -import com.microsoft.embeddedsocial.base.service.IServiceIntentHandler; import com.microsoft.embeddedsocial.base.utils.debug.DebugLog; import com.microsoft.embeddedsocial.event.LinkUserThirdPartyAccountEvent; import com.microsoft.embeddedsocial.server.EmbeddedSocialServiceProvider; @@ -15,34 +9,41 @@ import com.microsoft.embeddedsocial.server.IAccountService; import com.microsoft.embeddedsocial.server.exception.NetworkRequestException; import com.microsoft.embeddedsocial.server.exception.StatusException; import com.microsoft.embeddedsocial.server.model.account.LinkThirdPartyRequest; -import com.microsoft.embeddedsocial.service.IntentExtras; -import com.microsoft.embeddedsocial.service.ServiceAction; import com.microsoft.embeddedsocial.ui.util.SocialNetworkAccount; -import android.content.Intent; +import android.content.Context; + +import androidx.work.Worker; +import androidx.work.WorkerParameters; /** * Sends a link user third party account request to the server. */ -public class LinkUserThirdPartyAccountHandler implements IServiceIntentHandler { +public class LinkUserThirdPartyAccountWorker extends Worker { + public static final String SOCIAL_NETWORK_ACCOUNT = "socialNetworkAccount"; + + public LinkUserThirdPartyAccountWorker(Context context, WorkerParameters workerParams) { + super(context, workerParams); + } @Override - public void handleIntent(ServiceAction action, Intent intent) { - final SocialNetworkAccount account = intent.getExtras().getParcelable(IntentExtras.SOCIAL_NETWORK_ACCOUNT); + public Result doWork() { + final SocialNetworkAccount account = WorkerHelper.deserialize(getInputData().getString(SOCIAL_NETWORK_ACCOUNT)); IAccountService service = GlobalObjectRegistry.getObject(EmbeddedSocialServiceProvider.class).getAccountService(); LinkThirdPartyRequest linkUserThirdPartyAccountRequest = new LinkThirdPartyRequest( account.getAccountType(), account.getThirdPartyAccessToken()); - intent.removeExtra(IntentExtras.SOCIAL_NETWORK_ACCOUNT); account.clearTokens(); try { service.linkUserThirdPartyAccount(linkUserThirdPartyAccountRequest); EventBus.post(LinkUserThirdPartyAccountEvent.createLinkEvent(account)); + return Result.success(); } catch (NetworkRequestException e) { DebugLog.logException(e); LinkUserThirdPartyAccountEvent event; + // Notify the handler that the request failed if (e instanceof StatusException) { event = LinkUserThirdPartyAccountEvent.createLinkEvent(account, e.getMessage(), ((StatusException)e).getStatusCode()); @@ -50,11 +51,7 @@ public class LinkUserThirdPartyAccountHandler implements IServiceIntentHandler { onAuthenticationCompleted(); - Bundle extras = new Bundle(); - extras.putParcelable(IntentExtras.SOCIAL_NETWORK_ACCOUNT, account); - WorkerService.getLauncher(getContext()).launchService(ServiceAction.LINK_USER_THIRD_PARTY_ACCOUNT, extras); + + Data inputData = new Data.Builder() + .putString(LinkUserThirdPartyAccountWorker.SOCIAL_NETWORK_ACCOUNT, + WorkerHelper.serialize(account)).build(); + OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(LinkUserThirdPartyAccountWorker.class) + .setInputData(inputData).build(); + WorkManager.getInstance().enqueue(workRequest); }); } From a2fb3b351d88c1db7363bd2fbc31682678742863 Mon Sep 17 00:00:00 2001 From: Alex Crown Date: Fri, 11 Jan 2019 11:55:43 -0800 Subject: [PATCH 23/26] Remove no longer used Actions class and handlers --- .../embeddedsocial/account/UserAccount.java | 4 +- .../embeddedsocial/actions/Action.java | 129 ------------------ .../embeddedsocial/actions/ActionFilter.java | 18 --- .../actions/ActionTagFilter.java | 32 ----- .../actions/ActionsLauncher.java | 93 ------------- .../actions/OngoingActions.java | 96 ------------- .../event/action/ActionCompletedEvent.java | 22 --- .../event/action/ActionEvent.java | 26 ---- .../event/action/ActionStartedEvent.java | 23 ---- .../service/IntentProcessorFactory.java | 31 ----- .../embeddedsocial/service/ServiceAction.java | 13 -- .../embeddedsocial/service/WorkerService.java | 29 ---- .../service/handler/ActionHandler.java | 45 ------ .../service/worker/CreateAccountWorker.java | 1 + .../service/worker/DeleteAccountWorker.java | 1 + .../service/worker/UpdateAccountWorker.java | 1 + .../service/worker/WorkerHelper.java | 20 +++ .../ui/fragment/CreateProfileFragment.java | 8 +- .../ui/fragment/DeleteAccountFragment.java | 5 +- .../ui/fragment/EditProfileFragment.java | 8 +- .../ui/fragment/base/ActionListener.java | 76 ----------- .../ui/fragment/base/BaseFragment.java | 43 ------ 22 files changed, 34 insertions(+), 690 deletions(-) delete mode 100644 EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/actions/Action.java delete mode 100644 EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/actions/ActionFilter.java delete mode 100644 EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/actions/ActionTagFilter.java delete mode 100644 EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/actions/ActionsLauncher.java delete mode 100644 EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/actions/OngoingActions.java delete mode 100644 EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/event/action/ActionCompletedEvent.java delete mode 100644 EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/event/action/ActionEvent.java delete mode 100644 EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/event/action/ActionStartedEvent.java delete mode 100644 EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/IntentProcessorFactory.java delete mode 100644 EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/ServiceAction.java delete mode 100644 EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/WorkerService.java delete mode 100644 EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/ActionHandler.java delete mode 100644 EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/base/ActionListener.java diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/account/UserAccount.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/account/UserAccount.java index c785a4c..955ec3b 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/account/UserAccount.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/account/UserAccount.java @@ -6,8 +6,6 @@ package com.microsoft.embeddedsocial.account; import com.facebook.login.LoginManager; -import com.microsoft.embeddedsocial.actions.Action; -import com.microsoft.embeddedsocial.actions.OngoingActions; import com.microsoft.embeddedsocial.auth.MicrosoftLiveAuthenticator; import com.microsoft.embeddedsocial.auth.SocialNetworkTokens; import com.microsoft.embeddedsocial.autorest.models.FollowerStatus; @@ -121,7 +119,7 @@ public class UserAccount { * Whether sign-in is in progress now. */ public boolean isSigningIn() { - return OngoingActions.hasActionsWithTag(Action.Tags.SIGN_IN); + return WorkerHelper.isOngoing(SignInWorker.TAG); } /** diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/actions/Action.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/actions/Action.java deleted file mode 100644 index f613a9c..0000000 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/actions/Action.java +++ /dev/null @@ -1,129 +0,0 @@ -/* - * Copyright (c) Microsoft Corporation. All rights reserved. - * Licensed under the MIT License. See LICENSE in the project root for license information. - */ - -package com.microsoft.embeddedsocial.actions; - -import java.util.concurrent.atomic.AtomicLong; - -/** - * Some program's action (with progress visible in UI). - */ -public class Action { - - /** - * Action tag constants. - */ - public static class Tags { - public static final String SIGN_IN = "signIn"; - public static final String SIGN_OUT = "signOut"; - public static final String CREATE_ACCOUNT = "createAccount"; - public static final String UPDATE_ACCOUNT = "updateAccount"; - public static final String DELETE_ACCOUNT = "deleteAccount"; - public static final String GET_COMMENT = "getComment"; - public static final String GET_REPLY = "getReply"; - public static final String REMOVE_FOLLOWER = "removeFollower"; - } - - private static AtomicLong NEXT_ID = new AtomicLong(); - - private final long id = NEXT_ID.incrementAndGet(); - private final String tag; - - private boolean failed; - private String error; - - /** - * Creates a new instance. - * - * @param tag string id of the action set this action belongs to; it can be used for action filtering - */ - Action(String tag) { - this.tag = tag; - } - - /** - * Marks this action as running. - */ - public void start() { - OngoingActions.add(this); - } - - /** - * Marks this action as completed successfully. - */ - public void complete() { - OngoingActions.notifyCompleted(this); - } - - /** - * Marks this action as completed with an error. - * @param errorMessage error message - */ - public void fail(String errorMessage) { - failed = true; - error = errorMessage; - complete(); - } - - /** - * Marks this action as completed with an error (error message is empty). - */ - public void fail() { - fail(null); - } - - /** - * Returns whether this action is completed (successfully or not). - */ - public boolean isCompleted() { - return OngoingActions.isCompleted(this); - } - - /** - * Returns an error message (passed to {@link #fail(String)} method. - */ - public String getError() { - return error; - } - - /** - * Returns whether this action failed. - */ - public boolean isFailed() { - return failed; - } - - /** - * Returns the id of this action (unique among all actions). The action can be found by id via {@link OngoingActions#findActionById(Long)} - */ - public long getId() { - return id; - } - - /** - * Returns action's tag. - */ - public String getTag() { - return tag; - } - - // XXX: assume Action's are equal if their ids are equal - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - Action action = (Action) o; - return id == action.id; - } - - @Override - public int hashCode() { - return (int) (id); - } -} diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/actions/ActionFilter.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/actions/ActionFilter.java deleted file mode 100644 index 12cbdd0..0000000 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/actions/ActionFilter.java +++ /dev/null @@ -1,18 +0,0 @@ -/* - * Copyright (c) Microsoft Corporation. All rights reserved. - * Licensed under the MIT License. See LICENSE in the project root for license information. - */ - -package com.microsoft.embeddedsocial.actions; - -/** - * Determines which actions are interesting for an application's component. - */ -public interface ActionFilter { - - /** - * Whether the action is interesting for a component. - */ - boolean filter(Action action); - -} diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/actions/ActionTagFilter.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/actions/ActionTagFilter.java deleted file mode 100644 index 72d3ea2..0000000 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/actions/ActionTagFilter.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright (c) Microsoft Corporation. All rights reserved. - * Licensed under the MIT License. See LICENSE in the project root for license information. - */ - -package com.microsoft.embeddedsocial.actions; - -import java.util.Arrays; -import java.util.Collection; - -/** - * Filters actions by tag. - */ -public class ActionTagFilter implements ActionFilter { - - private final Collection tags; - - public ActionTagFilter(String... tags) { - this.tags = Arrays.asList(tags); - } - - @Override - public boolean filter(Action action) { - String actionTag = action.getTag(); - for (String tag : tags) { - if (actionTag.equals(tag)) { - return true; - } - } - return false; - } -} diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/actions/ActionsLauncher.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/actions/ActionsLauncher.java deleted file mode 100644 index bc3891d..0000000 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/actions/ActionsLauncher.java +++ /dev/null @@ -1,93 +0,0 @@ -/* - * Copyright (c) Microsoft Corporation. All rights reserved. - * Licensed under the MIT License. See LICENSE in the project root for license information. - */ - -package com.microsoft.embeddedsocial.actions; - -import com.microsoft.embeddedsocial.data.model.AccountDataDifference; -import com.microsoft.embeddedsocial.data.model.CreateAccountData; -import com.microsoft.embeddedsocial.service.IntentExtras; -import com.microsoft.embeddedsocial.service.ServiceAction; -import com.microsoft.embeddedsocial.service.WorkerService; -import com.microsoft.embeddedsocial.ui.util.SocialNetworkAccount; - -import android.content.Context; -import android.os.Bundle; - -/** - * Launches actions. - */ -public final class ActionsLauncher { - - private ActionsLauncher() { - } - - private static Action createAndStartAction(String tag) { - Action action = new Action(tag); - action.start(); - return action; - } - - /** - * Builds a service intent and sends it. - */ - private static final class ActionIntentBuilder { - - private final Bundle extras = new Bundle(); - private final Action action; - - private ActionIntentBuilder(Action action) { - this.action = action; - extras.putLong(IntentExtras.ACTION_ID, action.getId()); - } - - ActionIntentBuilder setAuthorization(String authorization) { - extras.putString(IntentExtras.AUTHORIZATION, authorization); - return this; - } - - ActionIntentBuilder setThirdPartyAccount(SocialNetworkAccount thirdPartyAccount) { - extras.putParcelable(IntentExtras.THIRD_PARTY_ACCOUNT, thirdPartyAccount); - return this; - } - - ActionIntentBuilder setUserHandle(String userHandle) { - extras.putString(IntentExtras.USER_HANDLE, userHandle); - return this; - } - - ActionIntentBuilder setCreateAccountData(CreateAccountData createAccountData) { - extras.putParcelable(IntentExtras.CREATE_ACCOUNT_DATA, createAccountData); - return this; - } - - ActionIntentBuilder setAccountDataDifference(AccountDataDifference difference) { - extras.putParcelable(IntentExtras.ACCOUNT_DATA_DIFFERENCE, difference); - return this; - } - - ActionIntentBuilder setCommentHandle(String commentHandle) { - extras.putString(IntentExtras.COMMENT_HANDLE, commentHandle); - return this; - } - - ActionIntentBuilder setReplyHandle(String replyHandle) { - extras.putString(IntentExtras.REPLY_HANDLE, replyHandle); - return this; - } - - Action launch(Context context, ServiceAction serviceAction) { - WorkerService.getLauncher(context).launchService(serviceAction, extras); - return action; - } - - static ActionIntentBuilder forActionWithTag(String tag) { - Action action = createAndStartAction(tag); - return new ActionIntentBuilder(action); - } - - } - - -} diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/actions/OngoingActions.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/actions/OngoingActions.java deleted file mode 100644 index bebf385..0000000 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/actions/OngoingActions.java +++ /dev/null @@ -1,96 +0,0 @@ -/* - * Copyright (c) Microsoft Corporation. All rights reserved. - * Licensed under the MIT License. See LICENSE in the project root for license information. - */ - -package com.microsoft.embeddedsocial.actions; - -import com.microsoft.embeddedsocial.base.event.EventBus; -import com.microsoft.embeddedsocial.event.action.ActionCompletedEvent; -import com.microsoft.embeddedsocial.event.action.ActionStartedEvent; - -import java.util.HashMap; -import java.util.Map; -import java.util.WeakHashMap; - -/** - * Holds information about currently running actions and their errors. - */ -public final class OngoingActions { - - private static final Map ACTIONS = new HashMap<>(); - private static final Map COMPLETED_ACTIONS = new WeakHashMap<>(); - - private OngoingActions() { - } - - /** - * Finds a currently running (i.e. started and not completed) action by it's id - * - * @param id action's id - */ - public static Action findActionById(Long id) { - synchronized (OngoingActions.class) { - return ACTIONS.get(id); - } - } - - /** - * Returns whether there is a running action satisfying the filter. - * @param filter action filter - */ - public static boolean hasActions(ActionFilter filter) { - synchronized (OngoingActions.class) { - for (Action action : ACTIONS.values()) { - if (filter.filter(action)) { - return true; - } - } - } - return false; - } - - /** - * Returns whether there is a running action with such tag. - */ - public static boolean hasActionsWithTag(String tag) { - return hasActions(new ActionTagFilter(tag)); - } - - /** - * Register the action as running. - */ - static void add(Action action) { - synchronized (OngoingActions.class) { - if (ACTIONS.containsKey(action.getId())) { - throw new RuntimeException("Action has been already started"); - } - ACTIONS.put(action.getId(), action); - } - EventBus.post(new ActionStartedEvent(action)); - } - - /** - * returns whether the action is already completed. - */ - static boolean isCompleted(Action action) { - synchronized (OngoingActions.class) { - return COMPLETED_ACTIONS.containsKey(action); - } - } - - /** - * Marks the action as completed. - */ - static void notifyCompleted(Action action) { - synchronized (OngoingActions.class) { - if (!ACTIONS.containsKey(action.getId())) { - throw new RuntimeException("Action has been already completed"); - } - ACTIONS.remove(action.getId()); - COMPLETED_ACTIONS.put(action, Boolean.TRUE); - } - EventBus.post(new ActionCompletedEvent(action)); - } - -} diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/event/action/ActionCompletedEvent.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/event/action/ActionCompletedEvent.java deleted file mode 100644 index bb95787..0000000 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/event/action/ActionCompletedEvent.java +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright (c) Microsoft Corporation. All rights reserved. - * Licensed under the MIT License. See LICENSE in the project root for license information. - */ - -package com.microsoft.embeddedsocial.event.action; - -import com.microsoft.embeddedsocial.actions.Action; -import com.microsoft.embeddedsocial.base.event.HandlingThread; -import com.microsoft.embeddedsocial.base.event.ThreadType; - -/** - * Action was completed event. - */ -@HandlingThread(ThreadType.MAIN) -public class ActionCompletedEvent extends ActionEvent { - - public ActionCompletedEvent(Action action) { - super(action); - } - -} diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/event/action/ActionEvent.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/event/action/ActionEvent.java deleted file mode 100644 index a8aae06..0000000 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/event/action/ActionEvent.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright (c) Microsoft Corporation. All rights reserved. - * Licensed under the MIT License. See LICENSE in the project root for license information. - */ - -package com.microsoft.embeddedsocial.event.action; - - -import com.microsoft.embeddedsocial.actions.Action; -import com.microsoft.embeddedsocial.base.event.AbstractEvent; - -/** - * Base event class for actions. - */ -abstract class ActionEvent extends AbstractEvent { - - private final Action action; - - ActionEvent(Action action) { - this.action = action; - } - - public Action getAction() { - return action; - } -} diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/event/action/ActionStartedEvent.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/event/action/ActionStartedEvent.java deleted file mode 100644 index b97fbdc..0000000 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/event/action/ActionStartedEvent.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright (c) Microsoft Corporation. All rights reserved. - * Licensed under the MIT License. See LICENSE in the project root for license information. - */ - -package com.microsoft.embeddedsocial.event.action; - - -import com.microsoft.embeddedsocial.actions.Action; -import com.microsoft.embeddedsocial.base.event.HandlingThread; -import com.microsoft.embeddedsocial.base.event.ThreadType; - -/** - * Action started event. - */ -@HandlingThread(ThreadType.MAIN) -public class ActionStartedEvent extends ActionEvent { - - public ActionStartedEvent(Action action) { - super(action); - } - -} diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/IntentProcessorFactory.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/IntentProcessorFactory.java deleted file mode 100644 index 258b072..0000000 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/IntentProcessorFactory.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright (c) Microsoft Corporation. All rights reserved. - * Licensed under the MIT License. See LICENSE in the project root for license information. - */ - -package com.microsoft.embeddedsocial.service; - -import com.microsoft.embeddedsocial.base.service.IServiceIntentProcessor; -import com.microsoft.embeddedsocial.base.service.IntentProcessor; - -import android.content.Context; - -/** - * Builds intents processor. - */ -public class IntentProcessorFactory { - - private static final int WORKER_THREADS = 2; - - private final Context context; - - IntentProcessorFactory(Context context) { - this.context = context; - } - - IServiceIntentProcessor createIntentProcessor() { - IntentProcessor processor = new IntentProcessor<>(context, ServiceAction.class, WORKER_THREADS); - - return processor; - } -} diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/ServiceAction.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/ServiceAction.java deleted file mode 100644 index ea1f200..0000000 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/ServiceAction.java +++ /dev/null @@ -1,13 +0,0 @@ -/* - * Copyright (c) Microsoft Corporation. All rights reserved. - * Licensed under the MIT License. See LICENSE in the project root for license information. - */ - -package com.microsoft.embeddedsocial.service; - -/** - * Service actions. - */ -public enum ServiceAction { - -} diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/WorkerService.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/WorkerService.java deleted file mode 100644 index 5c96717..0000000 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/WorkerService.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright (c) Microsoft Corporation. All rights reserved. - * Licensed under the MIT License. See LICENSE in the project root for license information. - */ - -package com.microsoft.embeddedsocial.service; - -import com.microsoft.embeddedsocial.base.service.AbstractProcessingService; -import com.microsoft.embeddedsocial.base.service.IServiceIntentProcessor; -import com.microsoft.embeddedsocial.base.service.ServiceLauncher; - -import android.content.Context; - -/** - * Worker service. - */ -public class WorkerService extends AbstractProcessingService { - - IntentProcessorFactory factory = new IntentProcessorFactory(this); - - @Override - protected IServiceIntentProcessor createIntentProcessor() { - return factory.createIntentProcessor(); - } - - public static ServiceLauncher getLauncher(Context context) { - return new ServiceLauncher<>(context, WorkerService.class); - } -} diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/ActionHandler.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/ActionHandler.java deleted file mode 100644 index f5560c9..0000000 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/handler/ActionHandler.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright (c) Microsoft Corporation. All rights reserved. - * Licensed under the MIT License. See LICENSE in the project root for license information. - */ - -package com.microsoft.embeddedsocial.service.handler; - -import com.microsoft.embeddedsocial.actions.Action; -import com.microsoft.embeddedsocial.actions.OngoingActions; -import com.microsoft.embeddedsocial.base.service.IServiceIntentHandler; -import com.microsoft.embeddedsocial.base.utils.debug.DebugLog; -import com.microsoft.embeddedsocial.service.IntentExtras; -import com.microsoft.embeddedsocial.service.ServiceAction; - -import android.content.Intent; - -/** - * Base class for {@link IServiceIntentHandler} implementation dealing with {@link Action}. - */ -public abstract class ActionHandler implements IServiceIntentHandler { - - @Override - public final void handleIntent(ServiceAction serviceAction, Intent intent) { - long actionId = intent.getLongExtra(IntentExtras.ACTION_ID, -1); - Action action = OngoingActions.findActionById(actionId); - if (action == null) { - DebugLog.e("Action is null"); - return; - } - try { - handleAction(action, serviceAction, intent); - } finally { - if (!action.isCompleted()) { - action.complete(); - } - } - } - - protected abstract void handleAction(Action action, ServiceAction serviceAction, Intent intent); - - @Override - public void dispose() { - - } -} diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/CreateAccountWorker.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/CreateAccountWorker.java index 8ea91dc..21962ef 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/CreateAccountWorker.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/CreateAccountWorker.java @@ -35,6 +35,7 @@ import androidx.work.WorkerParameters; */ public class CreateAccountWorker extends Worker { public static final String CREATE_ACCOUNT_DATA = "createAccountData"; + public static final String TAG = "createAccountWorker"; private final Context context; diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/DeleteAccountWorker.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/DeleteAccountWorker.java index 72779bb..387ea47 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/DeleteAccountWorker.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/DeleteAccountWorker.java @@ -22,6 +22,7 @@ import androidx.work.WorkerParameters; * Sends a delete account request to the server */ public class DeleteAccountWorker extends Worker { + public static String TAG = "deleteAccountWorker"; public DeleteAccountWorker(Context context, WorkerParameters workerParameters) { super(context, workerParameters); diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/UpdateAccountWorker.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/UpdateAccountWorker.java index fecccbe..590f1ba 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/UpdateAccountWorker.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/UpdateAccountWorker.java @@ -30,6 +30,7 @@ import androidx.work.WorkerParameters; public class UpdateAccountWorker extends Worker { public static final String ACCOUNT_DATA_DIFFERENCE = "accountDataDifference"; + public static final String TAG = "updateAccountWorker"; private final Context context; private final IAccountService server = GlobalObjectRegistry.getObject(EmbeddedSocialServiceProvider.class).getAccountService(); diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/WorkerHelper.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/WorkerHelper.java index e8283f2..e72f5ba 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/WorkerHelper.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/WorkerHelper.java @@ -5,6 +5,8 @@ package com.microsoft.embeddedsocial.service.worker; +import com.google.common.util.concurrent.ListenableFuture; + import com.microsoft.embeddedsocial.base.utils.debug.DebugLog; import android.util.Base64; @@ -16,7 +18,9 @@ import java.io.InputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; +import java.util.List; import java.util.UUID; +import java.util.concurrent.ExecutionException; import androidx.lifecycle.LifecycleOwner; import androidx.lifecycle.LiveData; @@ -75,6 +79,22 @@ public class WorkerHelper { return data; } + public static boolean isOngoing(String tag) { + ListenableFuture> workInfoListenableFutureList = + WorkManager.getInstance().getWorkInfosByTag(tag); + try { + List workInfoList = workInfoListenableFutureList.get(); + boolean running = false; + for (WorkInfo workInfo : workInfoList) { + running = running || !workInfo.getState().isFinished(); + } + return running; + } catch (InterruptedException|ExecutionException e) { + return false; + } + + } + /** * Handle the result of a completed work request * @param lifecycleOwner the lifecycle owner used to observe the work request diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/CreateProfileFragment.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/CreateProfileFragment.java index 5f58337..4f684f8 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/CreateProfileFragment.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/CreateProfileFragment.java @@ -6,8 +6,6 @@ package com.microsoft.embeddedsocial.ui.fragment; import com.microsoft.embeddedsocial.account.UserAccount; -import com.microsoft.embeddedsocial.actions.Action; -import com.microsoft.embeddedsocial.actions.OngoingActions; import com.microsoft.embeddedsocial.base.utils.BitmapUtils; import com.microsoft.embeddedsocial.base.utils.ViewUtils; import com.microsoft.embeddedsocial.data.model.AccountData; @@ -134,7 +132,9 @@ public class CreateProfileFragment extends BaseFragmentWithProgress { .putString(CreateAccountWorker.CREATE_ACCOUNT_DATA, WorkerHelper.serialize(createAccountData)).build(); OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(CreateAccountWorker.class) - .setInputData(inputData).build(); + .setInputData(inputData) + .addTag(CreateAccountWorker.TAG) + .build(); WorkManager.getInstance().enqueue(workRequest); } } @@ -241,7 +241,7 @@ public class CreateProfileFragment extends BaseFragmentWithProgress { @Override public boolean onBackPressed() { hideKeyboard(); - if (OngoingActions.hasActionsWithTag(Action.Tags.UPDATE_ACCOUNT)) { + if (WorkerHelper.isOngoing(CreateAccountWorker.TAG)) { showToast(R.string.es_message_wait_until_account_created); return false; } diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/DeleteAccountFragment.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/DeleteAccountFragment.java index 7967c82..45193d9 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/DeleteAccountFragment.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/DeleteAccountFragment.java @@ -5,8 +5,6 @@ package com.microsoft.embeddedsocial.ui.fragment; -import com.microsoft.embeddedsocial.actions.Action; -import com.microsoft.embeddedsocial.actions.OngoingActions; import com.microsoft.embeddedsocial.sdk.R; import com.microsoft.embeddedsocial.service.worker.DeleteAccountWorker; import com.microsoft.embeddedsocial.service.worker.WorkerHelper; @@ -36,6 +34,7 @@ public class DeleteAccountFragment extends BaseFragmentWithProgress { setOnClickListener(view, R.id.es_deleteButton, v -> { setProgressVisible(true); OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(DeleteAccountWorker.class) + .addTag(DeleteAccountWorker.TAG) .build(); WorkManager.getInstance().enqueue(workRequest); WorkerHelper.handleResult(this, workRequest.getId(), new WorkerHelper.ResultHandler() { @@ -55,7 +54,7 @@ public class DeleteAccountFragment extends BaseFragmentWithProgress { @Override public void onResume() { super.onResume(); - setProgressVisible(OngoingActions.hasActionsWithTag(Action.Tags.DELETE_ACCOUNT)); + setProgressVisible(WorkerHelper.isOngoing(DeleteAccountWorker.TAG)); } private void onAccountDeleted() { diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/EditProfileFragment.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/EditProfileFragment.java index 73cd175..26ec747 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/EditProfileFragment.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/EditProfileFragment.java @@ -6,8 +6,6 @@ package com.microsoft.embeddedsocial.ui.fragment; import com.microsoft.embeddedsocial.account.UserAccount; -import com.microsoft.embeddedsocial.actions.Action; -import com.microsoft.embeddedsocial.actions.OngoingActions; import com.microsoft.embeddedsocial.base.GlobalObjectRegistry; import com.microsoft.embeddedsocial.base.utils.BitmapUtils; import com.microsoft.embeddedsocial.base.utils.ObjectUtils; @@ -133,7 +131,9 @@ public class EditProfileFragment extends BaseFragmentWithProgress { .putString(UpdateAccountWorker.ACCOUNT_DATA_DIFFERENCE, WorkerHelper.serialize(difference)).build(); OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(UpdateAccountWorker.class) - .setInputData(inputData).build(); + .setInputData(inputData) + .addTag(UpdateAccountWorker.TAG) + .build(); WorkManager.getInstance().enqueue(workRequest); WorkerHelper.handleResult(this, workRequest.getId(), new WorkerHelper.ResultHandler() { @@ -279,7 +279,7 @@ public class EditProfileFragment extends BaseFragmentWithProgress { @Override public boolean onBackPressed() { hideKeyboard(); - if (OngoingActions.hasActionsWithTag(Action.Tags.UPDATE_ACCOUNT)) { + if (WorkerHelper.isOngoing(UpdateAccountWorker.TAG)) { showToast(R.string.es_message_wait_until_account_updated); return false; } diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/base/ActionListener.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/base/ActionListener.java deleted file mode 100644 index 3de6caa..0000000 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/base/ActionListener.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * Copyright (c) Microsoft Corporation. All rights reserved. - * Licensed under the MIT License. See LICENSE in the project root for license information. - */ - -package com.microsoft.embeddedsocial.ui.fragment.base; - -import com.microsoft.embeddedsocial.actions.Action; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; - -/** - * Listens for actions state. - */ -@SuppressWarnings("UnusedParameters") -public abstract class ActionListener { - - private List ongoingActions = new LinkedList<>(); - - protected void onActionStarted(Action action) { - } - - protected void onActionCompleted(Action action) { - } - - protected void onActionSucceeded(Action action) { - } - - protected void onActionFailed(Action action, String error) { - } - - protected void onActionsCompletionMissed(List completedActions, List succeededActions, List failedActions) { - } - - void notifyActionStarted(Action action) { - ongoingActions.add(action); - onActionStarted(action); - } - - void notifyActionCompleted(Action action) { - ongoingActions.remove(action); - if (action.isFailed()) { - onActionFailed(action, action.getError()); - } else { - onActionSucceeded(action); - } - onActionCompleted(action); - } - - void notifyOnResume() { - if (!ongoingActions.isEmpty()) { - List completedActions = new ArrayList<>(); - List succeededActions = new ArrayList<>(); - List failedActions = new ArrayList<>(); - Iterator iterator = ongoingActions.iterator(); - while (iterator.hasNext()) { - Action action = iterator.next(); - if (action.isCompleted()) { - completedActions.add(action); - if (action.isFailed()) { - failedActions.add(action); - } else { - succeededActions.add(action); - } - iterator.remove(); - } - } - if (!completedActions.isEmpty()) { - onActionsCompletionMissed(completedActions, succeededActions, failedActions); - } - } - } -} diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/base/BaseFragment.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/base/BaseFragment.java index 678acdd..79bb12f 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/base/BaseFragment.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/ui/fragment/base/BaseFragment.java @@ -5,12 +5,8 @@ package com.microsoft.embeddedsocial.ui.fragment.base; -import com.microsoft.embeddedsocial.actions.Action; -import com.microsoft.embeddedsocial.actions.ActionFilter; import com.microsoft.embeddedsocial.base.event.EventBus; import com.microsoft.embeddedsocial.base.utils.ViewUtils; -import com.microsoft.embeddedsocial.event.action.ActionCompletedEvent; -import com.microsoft.embeddedsocial.event.action.ActionStartedEvent; import com.microsoft.embeddedsocial.sdk.BuildConfig; import com.microsoft.embeddedsocial.sdk.R; import com.microsoft.embeddedsocial.telemetry.Event; @@ -18,13 +14,11 @@ import com.microsoft.embeddedsocial.telemetry.Telemetry; import com.microsoft.embeddedsocial.ui.activity.PopularActivity; import com.microsoft.embeddedsocial.ui.activity.base.BaseActivity; import com.microsoft.embeddedsocial.ui.util.CommonBehaviorEventListener; -import com.squareup.otto.Subscribe; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.os.Bundle; -import android.util.Pair; import android.view.ContextThemeWrapper; import android.view.LayoutInflater; import android.view.Menu; @@ -55,43 +49,13 @@ public abstract class BaseFragment extends Fragment { private final List modules = new LinkedList<>(); - private final List> actionListeners = new LinkedList<>(); private final List eventListeners = new LinkedList<>(); - @SuppressWarnings("FieldCanBeLocal") - private final Object actionEventListener = new Object() { - - @Subscribe - public void onActionStartedEvent(ActionStartedEvent event) { - Action action = event.getAction(); - for (Pair pair : actionListeners) { - ActionFilter actionFilter = pair.first; - if (actionFilter.filter(action)) { - ActionListener actionListener = pair.second; - actionListener.notifyActionStarted(action); - } - } - } - - @Subscribe - public void onActionCompletedEvent(ActionCompletedEvent event) { - Action action = event.getAction(); - for (Pair pair : actionListeners) { - ActionFilter actionFilter = pair.first; - if (actionFilter.filter(action)) { - ActionListener actionListener = pair.second; - actionListener.notifyActionCompleted(action); - } - } - } - - }; protected BaseFragment() { addThemeToMerge(R.style.EmbeddedSocialSdkThemeOverlayBaseFragment); setRetainInstance(true); eventListeners.add(this); - eventListeners.add(actionEventListener); if (Telemetry.isTelemetryEnabled()) { Event fragmentCreate = Telemetry.newEvent("FragmentCreate"); @@ -114,10 +78,6 @@ public abstract class BaseFragment extends Fragment { modules.add(module); } - protected void addActionListener(ActionFilter filter, ActionListener listener) { - actionListeners.add(new Pair<>(filter, listener)); - } - /** * Adds an event listener. Call it only from constructor (or at least before onResume) */ @@ -249,9 +209,6 @@ public abstract class BaseFragment extends Fragment { for (Module module : modules) { module.onResume(); } - for (Pair actionListener : actionListeners) { - actionListener.second.notifyOnResume(); - } for (Object eventListener : eventListeners) { EventBus.register(eventListener); } From 484d064f97f42a77d947379ef7f778615ef6e62a Mon Sep 17 00:00:00 2001 From: Alex Crown Date: Fri, 11 Jan 2019 11:58:24 -0800 Subject: [PATCH 24/26] Add comments to isOngoing in WorkerHelper --- .../embeddedsocial/service/worker/WorkerHelper.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/WorkerHelper.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/WorkerHelper.java index e72f5ba..70f374a 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/WorkerHelper.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/WorkerHelper.java @@ -79,6 +79,11 @@ public class WorkerHelper { return data; } + /** + * Determines if any workers matching the given tag are enqueued or running + * @param tag Tag to match with active workers + * @return true if any workers matching the tag are enqueued or running; false otherwise + */ public static boolean isOngoing(String tag) { ListenableFuture> workInfoListenableFutureList = WorkManager.getInstance().getWorkInfosByTag(tag); From ca364c882c3b8203c67c8dbdf341e10dc7a9b094 Mon Sep 17 00:00:00 2001 From: Alex Crown Date: Fri, 29 Mar 2019 16:15:21 -0700 Subject: [PATCH 25/26] Clean up worker classes and remove unnecessary context from CreateAccountWorker --- .../service/worker/BackgroundInitializationWorker.java | 1 - .../embeddedsocial/service/worker/CreateAccountWorker.java | 3 --- .../service/worker/DeleteSearchHistoryWorker.java | 1 - .../embeddedsocial/service/worker/GetFcmIdWorker.java | 1 - .../embeddedsocial/service/worker/SynchronizationWorker.java | 2 -- .../microsoft/embeddedsocial/service/worker/WorkerHelper.java | 1 - 6 files changed, 9 deletions(-) diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/BackgroundInitializationWorker.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/BackgroundInitializationWorker.java index def800b..61fa11b 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/BackgroundInitializationWorker.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/BackgroundInitializationWorker.java @@ -16,7 +16,6 @@ import androidx.work.WorkerParameters; * Initializes the SDK for use */ public class BackgroundInitializationWorker extends Worker { - public BackgroundInitializationWorker(Context context, WorkerParameters workerParameters) { super(context, workerParameters); } diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/CreateAccountWorker.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/CreateAccountWorker.java index 21962ef..6438a4d 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/CreateAccountWorker.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/CreateAccountWorker.java @@ -37,15 +37,12 @@ public class CreateAccountWorker extends Worker { public static final String CREATE_ACCOUNT_DATA = "createAccountData"; public static final String TAG = "createAccountWorker"; - private final Context context; - private final IAccountService accountService = GlobalObjectRegistry .getObject(EmbeddedSocialServiceProvider.class) .getAccountService(); public CreateAccountWorker(Context context, WorkerParameters workerParameters) { super(context, workerParameters); - this.context = context; } @Override diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/DeleteSearchHistoryWorker.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/DeleteSearchHistoryWorker.java index 7e861b7..c9c9c39 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/DeleteSearchHistoryWorker.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/DeleteSearchHistoryWorker.java @@ -16,7 +16,6 @@ import androidx.work.WorkerParameters; * Deletes search history from the device */ public class DeleteSearchHistoryWorker extends Worker { - public DeleteSearchHistoryWorker(Context context, WorkerParameters workerParameters) { super(context, workerParameters); } diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/GetFcmIdWorker.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/GetFcmIdWorker.java index bf9805e..0956e94 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/GetFcmIdWorker.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/GetFcmIdWorker.java @@ -25,7 +25,6 @@ import androidx.work.WorkerParameters; * Gets FCM tokens and stores them */ public class GetFcmIdWorker extends Worker { - private final Context context; private final FcmTokenHolder tokenHolder; diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/SynchronizationWorker.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/SynchronizationWorker.java index 3ec4f06..c8e30ae 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/SynchronizationWorker.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/SynchronizationWorker.java @@ -22,11 +22,9 @@ import androidx.work.WorkerParameters; * Synchronizes local data with the server */ public class SynchronizationWorker extends Worker { - public static final String PENDING_POST_SYNC_NAME = "posts"; private final DataSynchronizer synchronizer = new DataSynchronizer(); - public SynchronizationWorker(Context context, WorkerParameters workerParameters) { super(context, workerParameters); UserActionCache userActionCache = new UserActionCache(); diff --git a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/WorkerHelper.java b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/WorkerHelper.java index 70f374a..ce55fb6 100644 --- a/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/WorkerHelper.java +++ b/EmbeddedSocialClient/sdk/src/main/java/com/microsoft/embeddedsocial/service/worker/WorkerHelper.java @@ -35,7 +35,6 @@ import static androidx.work.WorkInfo.State.SUCCEEDED; * Implements utility functions to aid in setting up and handling androidx */ public class WorkerHelper { - /** * Serialize the given Object to a String * @param data data to serialize From 885e5abea802598290ddb7fba38c7f591079092c Mon Sep 17 00:00:00 2001 From: Alex Crown Date: Fri, 5 Apr 2019 10:52:03 -0700 Subject: [PATCH 26/26] Update work-runtime to the most recent stable release --- EmbeddedSocialClient/sdk/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/EmbeddedSocialClient/sdk/build.gradle b/EmbeddedSocialClient/sdk/build.gradle index 36e9240..4fffff3 100644 --- a/EmbeddedSocialClient/sdk/build.gradle +++ b/EmbeddedSocialClient/sdk/build.gradle @@ -91,7 +91,7 @@ dependencies { api 'com.squareup:otto:1.3.6' api 'com.squareup.retrofit2:retrofit:2.0.0-beta4' api 'com.squareup.okhttp3:okhttp:3.1.2' - implementation ('android.arch.work:work-runtime:1.0.0-alpha13') { + implementation ('androidx.work:work-runtime:2.0.0') { // workaround for build issue regarding com.google.common.util.concurrent.ListenableFuture //https://stackoverflow.com/questions/52467819/compile-errors-after-updating-to-workmanager-1-0-0-alpha09 exclude group: 'com.google.guava', module: 'listenablefuture'