From 6bc78aae903744c09b9550086b01f603b7941c54 Mon Sep 17 00:00:00 2001 From: Mario Danic Date: Fri, 10 Aug 2018 02:09:41 +0200 Subject: [PATCH] Start migration to WorkManager Signed-off-by: Mario Danic --- app/build.gradle | 4 + .../MagicFirebaseMessagingService.java | 28 +-- ...cationJob.java => NotificationWorker.java} | 233 +++++++++--------- .../talk/jobs/creator/MagicJobCreator.java | 8 +- .../drawable/ic_people_group_black_24px.xml | 47 ++++ 5 files changed, 184 insertions(+), 136 deletions(-) rename app/src/main/java/com/nextcloud/talk/jobs/{NotificationJob.java => NotificationWorker.java} (72%) create mode 100644 app/src/main/res/drawable/ic_people_group_black_24px.xml diff --git a/app/build.gradle b/app/build.gradle index 4bda40820..f6fd9ae8f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -66,6 +66,7 @@ android { ext { supportLibraryVersion = '27.1.1' + workVersion = "1.0.0-alpha06" } @@ -84,6 +85,9 @@ dependencies { implementation 'com.android.support.constraint:constraint-layout:1.1.2' implementation "com.android.support:support-emoji-bundled:${supportLibraryVersion}" implementation "com.android.support:palette-v7:${supportLibraryVersion}" + implementation "android.arch.work:work-runtime:${workVersion}" + implementation "android.arch.work:work-firebase:${workVersion}" + androidTestImplementation "android.arch.work:work-testing:${workVersion}" implementation "android.arch.lifecycle:extensions:1.1.1" diff --git a/app/src/gplay/java/com/nextcloud/talk/services/firebase/MagicFirebaseMessagingService.java b/app/src/gplay/java/com/nextcloud/talk/services/firebase/MagicFirebaseMessagingService.java index 554c56192..b5ab09996 100644 --- a/app/src/gplay/java/com/nextcloud/talk/services/firebase/MagicFirebaseMessagingService.java +++ b/app/src/gplay/java/com/nextcloud/talk/services/firebase/MagicFirebaseMessagingService.java @@ -22,13 +22,15 @@ package com.nextcloud.talk.services.firebase; import android.annotation.SuppressLint; -import com.evernote.android.job.JobRequest; -import com.evernote.android.job.util.support.PersistableBundleCompat; import com.google.firebase.messaging.FirebaseMessagingService; import com.google.firebase.messaging.RemoteMessage; -import com.nextcloud.talk.jobs.NotificationJob; +import com.nextcloud.talk.jobs.NotificationWorker; import com.nextcloud.talk.utils.bundle.BundleKeys; +import androidx.work.Data; +import androidx.work.OneTimeWorkRequest; +import androidx.work.WorkManager; + public class MagicFirebaseMessagingService extends FirebaseMessagingService { @SuppressLint("LongLogTag") @@ -39,17 +41,15 @@ public class MagicFirebaseMessagingService extends FirebaseMessagingService { } if (remoteMessage.getData() != null) { - PersistableBundleCompat persistableBundleCompat = new PersistableBundleCompat(); - persistableBundleCompat.putString(BundleKeys.KEY_NOTIFICATION_SUBJECT, remoteMessage.getData().get - ("subject")); - persistableBundleCompat.putString(BundleKeys.KEY_NOTIFICATION_SIGNATURE, remoteMessage.getData().get - ("signature")); - new JobRequest.Builder(NotificationJob.TAG) - .addExtras(persistableBundleCompat) - .setUpdateCurrent(false) - .startNow() - .build() - .schedule(); + Data messageData = new Data.Builder() + .putString(BundleKeys.KEY_NOTIFICATION_SUBJECT, remoteMessage.getData().get("subject")) + .putString(BundleKeys.KEY_NOTIFICATION_SIGNATURE, remoteMessage.getData().get("signature")) + .build(); + + OneTimeWorkRequest mathWork = new OneTimeWorkRequest.Builder(NotificationWorker.class) + .setInputData(messageData) + .build(); + WorkManager.getInstance().enqueue(mathWork); } } } diff --git a/app/src/main/java/com/nextcloud/talk/jobs/NotificationJob.java b/app/src/main/java/com/nextcloud/talk/jobs/NotificationWorker.java similarity index 72% rename from app/src/main/java/com/nextcloud/talk/jobs/NotificationJob.java rename to app/src/main/java/com/nextcloud/talk/jobs/NotificationWorker.java index 1c7cbc219..7096c21fe 100644 --- a/app/src/main/java/com/nextcloud/talk/jobs/NotificationJob.java +++ b/app/src/main/java/com/nextcloud/talk/jobs/NotificationWorker.java @@ -40,8 +40,6 @@ import android.util.Base64; import android.util.Log; import com.bluelinelabs.logansquare.LoganSquare; -import com.evernote.android.job.Job; -import com.evernote.android.job.util.support.PersistableBundleCompat; import com.nextcloud.talk.R; import com.nextcloud.talk.activities.MagicCallActivity; import com.nextcloud.talk.activities.MainActivity; @@ -79,14 +77,16 @@ import javax.crypto.Cipher; import javax.crypto.NoSuchPaddingException; import javax.inject.Inject; +import androidx.work.Data; +import androidx.work.Worker; import autodagger.AutoInjector; import io.reactivex.Observer; import io.reactivex.disposables.Disposable; import io.reactivex.schedulers.Schedulers; @AutoInjector(NextcloudTalkApplication.class) -public class NotificationJob extends Job { - public static final String TAG = "NotificationJob"; +public class NotificationWorker extends Worker { + public static final String TAG = "NotificationWorker"; @Inject UserUtils userUtils; @@ -102,119 +102,6 @@ public class NotificationJob extends Job { private SignatureVerification signatureVerification; private String conversationType = ""; - @NonNull - @Override - protected Result onRunJob(Params params) { - NextcloudTalkApplication.getSharedApplication().getComponentApplication().inject(this); - - context = getContext(); - PersistableBundleCompat persistableBundleCompat = getParams().getExtras(); - String subject = persistableBundleCompat.getString(BundleKeys.KEY_NOTIFICATION_SUBJECT, ""); - String signature = persistableBundleCompat.getString(BundleKeys.KEY_NOTIFICATION_SIGNATURE, ""); - - if (!TextUtils.isEmpty(subject) && !TextUtils.isEmpty(signature)) { - - try { - byte[] base64DecodedSubject = Base64.decode(subject, Base64.DEFAULT); - byte[] base64DecodedSignature = Base64.decode(signature, Base64.DEFAULT); - PushUtils pushUtils = new PushUtils(); - PrivateKey privateKey = (PrivateKey) pushUtils.readKeyFromFile(false); - - try { - signatureVerification = pushUtils.verifySignature(base64DecodedSignature, - base64DecodedSubject); - - if (signatureVerification.isSignatureValid()) { - Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding"); - cipher.init(Cipher.DECRYPT_MODE, privateKey); - byte[] decryptedSubject = cipher.doFinal(base64DecodedSubject); - decryptedPushMessage = LoganSquare.parse(new String(decryptedSubject), - DecryptedPushMessage.class); - - boolean hasChatSupport = signatureVerification.getUserEntity().hasSpreedCapabilityWithName - ("chat-v2"); - - boolean isInTheSameRoomAsNotification = (ApplicationWideCurrentRoomHolder.getInstance(). - getCurrentRoomId().equals(decryptedPushMessage.getId()) || - ApplicationWideCurrentRoomHolder.getInstance() - .getCurrentRoomToken().equals(decryptedPushMessage.getId())) && - signatureVerification.getUserEntity().equals(ApplicationWideCurrentRoomHolder - .getInstance().getUserInRoom()); - - boolean shouldShowNotification = decryptedPushMessage.getApp().equals("spreed") && - !decryptedPushMessage.getType().equals("room") && - (!isInTheSameRoomAsNotification || - !ApplicationWideStateHolder.getInstance().isInForeground() || - decryptedPushMessage.getType().equals("call")); - - if (shouldShowNotification) { - Intent intent; - Bundle bundle = new Bundle(); - - - boolean startACall = decryptedPushMessage.getType().equals("call") || !hasChatSupport; - if (startACall) { - intent = new Intent(context, MagicCallActivity.class); - } else { - intent = new Intent(context, MainActivity.class); - } - - intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - - - if (!signatureVerification.getUserEntity().hasSpreedCapabilityWithName - ("no-ping")) { - bundle.putString(BundleKeys.KEY_ROOM_ID, decryptedPushMessage.getId()); - } else { - bundle.putString(BundleKeys.KEY_ROOM_TOKEN, decryptedPushMessage.getId()); - } - - bundle.putParcelable(BundleKeys.KEY_USER_ENTITY, Parcels.wrap(signatureVerification - .getUserEntity())); - - bundle.putBoolean(BundleKeys.KEY_FROM_NOTIFICATION_START_CALL, - startACall); - - intent.putExtras(bundle); - - switch (decryptedPushMessage.getType()) { - case "call": - if (!bundle.containsKey(BundleKeys.KEY_ROOM_TOKEN)) { - context.startActivity(intent); - } else { - showNotificationForCallWithNoPing(intent); - } - break; - case "room": - // do absolutely nothing, we won't even come to this point - break; - case "chat": - if (decryptedPushMessage.getNotificationId() != Long.MIN_VALUE) { - showMessageNotificationWithObjectData(intent); - } else { - showNotification(intent); - } - break; - default: - break; - } - - } - } - } catch (NoSuchAlgorithmException e1) { - Log.d(TAG, "No proper algorithm to decrypt the message " + e1.getLocalizedMessage()); - } catch (NoSuchPaddingException e1) { - Log.d(TAG, "No proper padding to decrypt the message " + e1.getLocalizedMessage()); - } catch (InvalidKeyException e1) { - Log.d(TAG, "Invalid private key " + e1.getLocalizedMessage()); - } - } catch (Exception exception) { - Log.d(TAG, "Something went very wrong" + exception.getLocalizedMessage()); - } - } - return Result.SUCCESS; - } - private void showNotificationForCallWithNoPing(Intent intent) { UserEntity userEntity = signatureVerification.getUserEntity(); ncApi.getRoom(ApiUtils.getCredentials(userEntity.getUserId(), @@ -348,7 +235,7 @@ public class NotificationJob extends Job { largeIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_call_black_24dp); } } - + PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT); @@ -462,4 +349,114 @@ public class NotificationJob extends Job { } } } + + @NonNull + @Override + public Result doWork() { + NextcloudTalkApplication.getSharedApplication().getComponentApplication().inject(this); + + context = getApplicationContext(); + Data data = getInputData(); + String subject = data.getString(BundleKeys.KEY_NOTIFICATION_SUBJECT); + String signature = data.getString(BundleKeys.KEY_NOTIFICATION_SIGNATURE); + + try { + byte[] base64DecodedSubject = Base64.decode(subject, Base64.DEFAULT); + byte[] base64DecodedSignature = Base64.decode(signature, Base64.DEFAULT); + PushUtils pushUtils = new PushUtils(); + PrivateKey privateKey = (PrivateKey) pushUtils.readKeyFromFile(false); + + try { + signatureVerification = pushUtils.verifySignature(base64DecodedSignature, + base64DecodedSubject); + + if (signatureVerification.isSignatureValid()) { + Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding"); + cipher.init(Cipher.DECRYPT_MODE, privateKey); + byte[] decryptedSubject = cipher.doFinal(base64DecodedSubject); + decryptedPushMessage = LoganSquare.parse(new String(decryptedSubject), + DecryptedPushMessage.class); + + boolean hasChatSupport = signatureVerification.getUserEntity().hasSpreedCapabilityWithName + ("chat-v2"); + + boolean isInTheSameRoomAsNotification = (ApplicationWideCurrentRoomHolder.getInstance(). + getCurrentRoomId().equals(decryptedPushMessage.getId()) || + ApplicationWideCurrentRoomHolder.getInstance() + .getCurrentRoomToken().equals(decryptedPushMessage.getId())) && + signatureVerification.getUserEntity().equals(ApplicationWideCurrentRoomHolder + .getInstance().getUserInRoom()); + + boolean shouldShowNotification = decryptedPushMessage.getApp().equals("spreed") && + !decryptedPushMessage.getType().equals("room") && + (!isInTheSameRoomAsNotification || + !ApplicationWideStateHolder.getInstance().isInForeground() || + decryptedPushMessage.getType().equals("call")); + + if (shouldShowNotification) { + Intent intent; + Bundle bundle = new Bundle(); + + + boolean startACall = decryptedPushMessage.getType().equals("call") || !hasChatSupport; + if (startACall) { + intent = new Intent(context, MagicCallActivity.class); + } else { + intent = new Intent(context, MainActivity.class); + } + + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + + + if (!signatureVerification.getUserEntity().hasSpreedCapabilityWithName + ("no-ping")) { + bundle.putString(BundleKeys.KEY_ROOM_ID, decryptedPushMessage.getId()); + } else { + bundle.putString(BundleKeys.KEY_ROOM_TOKEN, decryptedPushMessage.getId()); + } + + bundle.putParcelable(BundleKeys.KEY_USER_ENTITY, Parcels.wrap(signatureVerification + .getUserEntity())); + + bundle.putBoolean(BundleKeys.KEY_FROM_NOTIFICATION_START_CALL, + startACall); + + intent.putExtras(bundle); + + switch (decryptedPushMessage.getType()) { + case "call": + if (!bundle.containsKey(BundleKeys.KEY_ROOM_TOKEN)) { + context.startActivity(intent); + } else { + showNotificationForCallWithNoPing(intent); + } + break; + case "room": + // do absolutely nothing, we won't even come to this point + break; + case "chat": + if (decryptedPushMessage.getNotificationId() != Long.MIN_VALUE) { + showMessageNotificationWithObjectData(intent); + } else { + showNotification(intent); + } + break; + default: + break; + } + + } + } + } catch (NoSuchAlgorithmException e1) { + Log.d(TAG, "No proper algorithm to decrypt the message " + e1.getLocalizedMessage()); + } catch (NoSuchPaddingException e1) { + Log.d(TAG, "No proper padding to decrypt the message " + e1.getLocalizedMessage()); + } catch (InvalidKeyException e1) { + Log.d(TAG, "Invalid private key " + e1.getLocalizedMessage()); + } + } catch (Exception exception) { + Log.d(TAG, "Something went very wrong" + exception.getLocalizedMessage()); + } + return Result.SUCCESS; + } } diff --git a/app/src/main/java/com/nextcloud/talk/jobs/creator/MagicJobCreator.java b/app/src/main/java/com/nextcloud/talk/jobs/creator/MagicJobCreator.java index 32eb814e4..e408d45e6 100644 --- a/app/src/main/java/com/nextcloud/talk/jobs/creator/MagicJobCreator.java +++ b/app/src/main/java/com/nextcloud/talk/jobs/creator/MagicJobCreator.java @@ -2,7 +2,7 @@ * Nextcloud Talk application * * @author Mario Danic - * Copyright (C) 2017 Mario Danic + * Copyright (C) 2017-2018 Mario Danic * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -27,7 +27,7 @@ import com.evernote.android.job.Job; import com.evernote.android.job.JobCreator; import com.nextcloud.talk.jobs.AccountRemovalJob; import com.nextcloud.talk.jobs.CapabilitiesJob; -import com.nextcloud.talk.jobs.NotificationJob; +import com.nextcloud.talk.jobs.NotificationWorker; import com.nextcloud.talk.jobs.PushRegistrationJob; public class MagicJobCreator implements JobCreator { @@ -40,8 +40,8 @@ public class MagicJobCreator implements JobCreator { return new PushRegistrationJob(); case AccountRemovalJob.TAG: return new AccountRemovalJob(); - case NotificationJob.TAG: - return new NotificationJob(); + case NotificationWorker.TAG: + return new NotificationWorker(); case CapabilitiesJob.TAG: return new CapabilitiesJob(); default: diff --git a/app/src/main/res/drawable/ic_people_group_black_24px.xml b/app/src/main/res/drawable/ic_people_group_black_24px.xml new file mode 100644 index 000000000..f1fd901a0 --- /dev/null +++ b/app/src/main/res/drawable/ic_people_group_black_24px.xml @@ -0,0 +1,47 @@ + + + + + + +