Skip to content

Commit

Permalink
Merge branch 'trunk' into Jetpack-Focus-Disable-notifications-from-Je…
Browse files Browse the repository at this point in the history
…tpack-app

# Conflicts:
#	WordPress/src/test/java/org/wordpress/android/ui/main/jetpack/migration/JetpackMigrationViewModelTest.kt
  • Loading branch information
ovitrif committed Nov 25, 2022
2 parents 457871a + 296b8fe commit 36a6fd2
Show file tree
Hide file tree
Showing 28 changed files with 319 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.wordpress.android.localcontentmigration

import org.wordpress.android.analytics.AnalyticsTracker.Stat
import org.wordpress.android.localcontentmigration.ContentMigrationAnalyticsTracker.ErrorType.Companion.ERROR_TYPE
import org.wordpress.android.localcontentmigration.ContentMigrationAnalyticsTracker.ErrorType.EmailError
import org.wordpress.android.util.analytics.AnalyticsTrackerWrapper
import javax.inject.Inject

Expand All @@ -11,8 +12,15 @@ class ContentMigrationAnalyticsTracker @Inject constructor(
fun trackContentMigrationFailed(errorType: ErrorType) =
analyticsTracker.track(Stat.SHARED_LOGIN_FAILED, mapOf(ERROR_TYPE to errorType.value))

fun trackMigrationEmailSuccess() =
analyticsTracker.track(Stat.MIGRATION_EMAIL_TRIGGERED)

fun trackMigrationEmailFailed(errorType: EmailError) =
analyticsTracker.track(Stat.MIGRATION_EMAIL_FAILED, mapOf(ERROR_TYPE to errorType.value))

sealed class ErrorType(val value: String) {
object LocalDraftContent : ErrorType("local_draft_content_is_present")
class EmailError(val error: String?) : ErrorType(error ?: "unknown_email_error")

companion object {
const val ERROR_TYPE = "error_type"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.wordpress.android.localcontentmigration

import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import org.wordpress.android.fluxc.store.mobile.JetpackMigrationStore
import org.wordpress.android.fluxc.store.mobile.MigrationCompleteFetchedPayload.Error
import org.wordpress.android.fluxc.store.mobile.MigrationCompleteFetchedPayload.Success
import org.wordpress.android.localcontentmigration.ContentMigrationAnalyticsTracker.ErrorType.EmailError
import org.wordpress.android.modules.BG_THREAD
import javax.inject.Inject
import javax.inject.Named
import kotlin.coroutines.CoroutineContext

class MigrationEmailHelper @Inject constructor(
private val jetpackMigrationStore: JetpackMigrationStore,
private val migrationAnalyticsTracker: ContentMigrationAnalyticsTracker,
@Named(BG_THREAD) private val bgDispatcher: CoroutineDispatcher,
) : CoroutineScope {
private val job = Job()
override val coroutineContext: CoroutineContext
get() = bgDispatcher + job

fun notifyMigrationComplete() = launch(bgDispatcher) {
when (val result = jetpackMigrationStore.migrationComplete()) {
is Success -> migrationAnalyticsTracker.trackMigrationEmailSuccess()
is Error -> migrationAnalyticsTracker.trackMigrationEmailFailed(EmailError(result.error?.message))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import org.wordpress.android.ui.comments.unified.UnifiedCommentsDetailsActivity;
import org.wordpress.android.ui.comments.unified.UnifiedCommentsEditFragment;
import org.wordpress.android.ui.debug.cookies.DebugCookiesFragment;
import org.wordpress.android.ui.deeplinks.DeepLinkingIntentReceiverActivity;
import org.wordpress.android.ui.domains.DomainRegistrationActivity;
import org.wordpress.android.ui.domains.DomainRegistrationDetailsFragment;
import org.wordpress.android.ui.domains.DomainRegistrationResultFragment;
Expand Down Expand Up @@ -244,8 +243,6 @@ public interface AppComponent {

void inject(GCMRegistrationIntentService object);

void inject(DeepLinkingIntentReceiverActivity object);

void inject(ShareIntentReceiverActivity object);

void inject(ShareIntentReceiverFragment object);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.wordpress.android.ui.comments.unified.UnifiedCommentsEditViewModel;
import org.wordpress.android.ui.debug.DebugSettingsViewModel;
import org.wordpress.android.ui.debug.cookies.DebugCookiesViewModel;
import org.wordpress.android.ui.deeplinks.DeepLinkingIntentReceiverViewModel;
import org.wordpress.android.ui.domains.DomainRegistrationDetailsViewModel;
import org.wordpress.android.ui.domains.DomainRegistrationMainViewModel;
import org.wordpress.android.ui.domains.DomainSuggestionsViewModel;
Expand Down Expand Up @@ -492,11 +491,6 @@ abstract class ViewModelModule {
@ViewModelKey(LoginViewModel.class)
abstract ViewModel loginViewModel(LoginViewModel viewModel);

@Binds
@IntoMap
@ViewModelKey(DeepLinkingIntentReceiverViewModel.class)
abstract ViewModel deepLinkingIntentReceiverViewModel(DeepLinkingIntentReceiverViewModel viewModel);

@Binds
@IntoMap
@ViewModelKey(StorageUtilsViewModel.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
import org.wordpress.android.util.AppLog;
import org.wordpress.android.util.AppLog.T;
import org.wordpress.android.util.ToastUtils;
import org.wordpress.android.util.UriWrapper;
import org.wordpress.android.util.UrlUtils;
import org.wordpress.android.util.WPActivityUtils;
import org.wordpress.android.util.analytics.AnalyticsUtils;
Expand Down Expand Up @@ -1768,4 +1769,12 @@ public static void startJetpackMigrationFlow(@NonNull Context context) {
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}

public static void openJetpackForDeeplink(@NonNull Context context, String action, UriWrapper uri) {
Intent intent = new Intent();
intent.setAction(action);
intent.setData(uri.getUri());
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import android.net.Uri
import dagger.Reusable
import org.wordpress.android.fluxc.model.PostImmutableModel
import org.wordpress.android.fluxc.model.SiteModel
import org.wordpress.android.ui.deeplinks.DeepLinkingIntentReceiverActivity
import org.wordpress.android.ui.posts.RemotePreviewLogicHelper.RemotePreviewType
import javax.inject.Inject

Expand Down Expand Up @@ -46,15 +45,6 @@ class ActivityLauncherWrapper @Inject constructor() {
context.startActivity(intent)
}

fun forwardDeepLinkIntent(context: Context, deepLinkIntent: Intent) {
val intent = Intent(context, DeepLinkingIntentReceiverActivity::class.java).apply {
action = deepLinkIntent.action
data = deepLinkIntent.data
flags = Intent.FLAG_ACTIVITY_NEW_TASK
}
context.startActivity(intent)
}

companion object {
const val JETPACK_PACKAGE_NAME = "com.jetpack.android"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import org.wordpress.android.ui.deeplinks.DeepLinkNavigator.NavigateAction.OpenE
import org.wordpress.android.ui.deeplinks.DeepLinkNavigator.NavigateAction.OpenMySite
import org.wordpress.android.ui.deeplinks.DeepLinkNavigator.NavigateAction.OpenInBrowser
import org.wordpress.android.ui.deeplinks.DeepLinkNavigator.NavigateAction.OpenInReader
import org.wordpress.android.ui.deeplinks.DeepLinkNavigator.NavigateAction.OpenJetpackForDeepLink
import org.wordpress.android.ui.deeplinks.DeepLinkNavigator.NavigateAction.OpenLoginPrologue
import org.wordpress.android.ui.deeplinks.DeepLinkNavigator.NavigateAction.OpenNotifications
import org.wordpress.android.ui.deeplinks.DeepLinkNavigator.NavigateAction.OpenPages
Expand Down Expand Up @@ -78,6 +79,8 @@ class DeepLinkNavigator
is OpenQRCodeAuthFlow -> ActivityLauncher.startQRCodeAuthFlowInNewStack(activity, navigateAction.uri)
OpenMySite -> ActivityLauncher.viewMySiteInNewStack(activity)
OpenLoginPrologue -> ActivityLauncher.showLoginPrologue(activity)
is OpenJetpackForDeepLink ->
ActivityLauncher.openJetpackForDeeplink(activity, navigateAction.action, navigateAction.uri)
}
if (navigateAction != LoginForResult) {
activity.finish()
Expand Down Expand Up @@ -107,5 +110,6 @@ class DeepLinkNavigator
data class OpenQRCodeAuthFlow(val uri: String) : NavigateAction()
object OpenMySite : NavigateAction()
object OpenLoginPrologue : NavigateAction()
data class OpenJetpackForDeepLink(val action: String?, val uri: UriWrapper) : NavigateAction()
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.wordpress.android.ui.deeplinks

import android.content.pm.PackageManager
import org.wordpress.android.ui.prefs.AppPrefsWrapper
import org.wordpress.android.util.AppLog
import org.wordpress.android.util.AppLog.T
import org.wordpress.android.util.BuildConfigWrapper
import org.wordpress.android.util.DateTimeUtilsWrapper
import org.wordpress.android.util.FirebaseRemoteConfigWrapper
Expand All @@ -9,6 +12,7 @@ import org.wordpress.android.util.config.OpenWebLinksWithJetpackFlowFeatureConfi
import java.util.Date
import javax.inject.Inject

@Suppress("TooManyFunctions")
class DeepLinkOpenWebLinksWithJetpackHelper @Inject constructor(
private val openWebLinksWithJetpackFlowFeatureConfig: OpenWebLinksWithJetpackFlowFeatureConfig,
private val appPrefsWrapper: AppPrefsWrapper,
Expand Down Expand Up @@ -42,6 +46,20 @@ class DeepLinkOpenWebLinksWithJetpackHelper @Inject constructor(
appPrefsWrapper.setIsOpenWebLinksWithJetpack(false)
}

@Suppress("SwallowedException")
fun handleOpenWebLinksWithJetpack() : Boolean {
try {
enableDisableOpenWithJetpackComponents(true)
packageManagerWrapper.disableReaderDeepLinks()
appPrefsWrapper.setIsOpenWebLinksWithJetpack(true)
appPrefsWrapper.setOpenWebLinksWithJetpackOverlayLastShownTimestamp(System.currentTimeMillis())
return true
} catch (ex: PackageManager.NameNotFoundException) {
AppLog.e(T.UTILS, "Unable to set open web links with Jetpack ${ex.message}")
}
return false
}

private fun showOverlay() : Boolean {
return openWebLinksWithJetpackFlowFeatureConfig.isEnabled()
&& isJetpackInstalled()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
import androidx.annotation.NonNull;
import androidx.lifecycle.ViewModelProvider;

import org.wordpress.android.WordPress;
import org.wordpress.android.ui.ActivityLauncherWrapper;
import org.wordpress.android.ui.LocaleAwareActivity;
import org.wordpress.android.ui.RequestCodes;
import org.wordpress.android.ui.jetpackoverlay.JetpackFeatureFullScreenOverlayFragment;
import org.wordpress.android.ui.jetpackoverlay.JetpackFeatureFullScreenOverlayViewModel;
import org.wordpress.android.ui.jetpackoverlay.JetpackFeatureOverlayActions.ForwardToJetpack;
import org.wordpress.android.util.PackageManagerWrapper;
import org.wordpress.android.util.ToastUtils;
import org.wordpress.android.util.UriWrapper;
Expand All @@ -17,26 +20,30 @@

import static org.wordpress.android.WordPress.getContext;

import dagger.hilt.android.AndroidEntryPoint;

/**
* An activity to handle deep linking and intercepting links like:
* <p>
* wordpress://viewpost?blogId={blogId}&postId={postId}
* <p>
* Redirects users to the reader activity along with IDs passed in the intent
*/
@AndroidEntryPoint
public class DeepLinkingIntentReceiverActivity extends LocaleAwareActivity {
@Inject DeepLinkNavigator mDeeplinkNavigator;
@Inject DeepLinkUriUtils mDeepLinkUriUtils;
@Inject ViewModelProvider.Factory mViewModelFactory;
@Inject PackageManagerWrapper mPackageManagerWrapper;
@Inject ActivityLauncherWrapper mActivityLauncherWrapper;
private DeepLinkingIntentReceiverViewModel mViewModel;
private JetpackFeatureFullScreenOverlayViewModel mJetpackFullScreenViewModel;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
((WordPress) getApplication()).component().inject(this);
mViewModel = new ViewModelProvider(this, mViewModelFactory).get(DeepLinkingIntentReceiverViewModel.class);

mViewModel = new ViewModelProvider(this).get(DeepLinkingIntentReceiverViewModel.class);
mJetpackFullScreenViewModel = new ViewModelProvider(this).get(JetpackFeatureFullScreenOverlayViewModel.class);
setupObservers();

mViewModel.start(
Expand Down Expand Up @@ -67,6 +74,30 @@ private void setupObservers() {
ToastUtils.showToast(getContext(), toastMessage);
return null;
}));
mViewModel.getShowOpenWebLinksWithJetpackOverlay().observe(this,
showOverlay -> showOverlay.applyIfNotHandled(unit -> {
showOverlay();
return null;
}));

observeOverlayEvents();
}

private void observeOverlayEvents() {
mJetpackFullScreenViewModel.getAction().observe(this,
action -> {
if (action instanceof ForwardToJetpack) {
mViewModel.forwardDeepLinkToJetpack();
} else {
mViewModel.handleRequest();
}
});
}

private void showOverlay() {
JetpackFeatureFullScreenOverlayFragment
.newInstance(null, false, true)
.show(getSupportFragmentManager(), JetpackFeatureFullScreenOverlayFragment.TAG);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import android.net.Uri
import android.os.Bundle
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.CoroutineDispatcher
import org.wordpress.android.analytics.AnalyticsTracker.Stat.DEEP_LINKED
import org.wordpress.android.fluxc.store.AccountStore
import org.wordpress.android.modules.UI_THREAD
import org.wordpress.android.ui.deeplinks.DeepLinkEntryPoint.WEB_LINKS
import org.wordpress.android.ui.deeplinks.DeepLinkNavigator.NavigateAction
import org.wordpress.android.ui.deeplinks.DeepLinkNavigator.NavigateAction.LoginForResult
import org.wordpress.android.ui.deeplinks.DeepLinkNavigator.NavigateAction.OpenInBrowser
import org.wordpress.android.ui.deeplinks.DeepLinkNavigator.NavigateAction.OpenJetpackForDeepLink
import org.wordpress.android.ui.deeplinks.DeepLinkNavigator.NavigateAction.OpenLoginPrologue
import org.wordpress.android.ui.deeplinks.DeepLinkNavigator.NavigateAction.ShowSignInFlow
import org.wordpress.android.ui.deeplinks.handlers.DeepLinkHandlers
Expand All @@ -22,27 +25,29 @@ import org.wordpress.android.viewmodel.ScopedViewModel
import javax.inject.Inject
import javax.inject.Named

@HiltViewModel
@Suppress("LongParameterList", "TooManyFunctions")
class DeepLinkingIntentReceiverViewModel
@Suppress("LongParameterList")
@Inject constructor(
@Named(UI_THREAD) private val uiDispatcher: CoroutineDispatcher,
private val deepLinkHandlers: DeepLinkHandlers,
private val deepLinkUriUtils: DeepLinkUriUtils,
private val accountStore: AccountStore,
private val serverTrackingHandler: ServerTrackingHandler,
private val deepLinkTrackingUtils: DeepLinkTrackingUtils,
private val analyticsUtilsWrapper: AnalyticsUtilsWrapper
private val analyticsUtilsWrapper: AnalyticsUtilsWrapper,
private val openWebLinksWithJetpackHelper: DeepLinkOpenWebLinksWithJetpackHelper
) : ScopedViewModel(uiDispatcher) {
private val _navigateAction = MutableLiveData<Event<NavigateAction>>()
val navigateAction = _navigateAction as LiveData<Event<NavigateAction>>
private val _finish = MutableLiveData<Event<Unit>>()
val finish = _finish as LiveData<Event<Unit>>
private val _showOpenWebLinksWithJetpackOverlay = MutableLiveData<Event<Unit>>()
val showOpenWebLinksWithJetpackOverlay = _showOpenWebLinksWithJetpackOverlay as LiveData<Event<Unit>>
val toast = deepLinkHandlers.toast
private var action: String? = null
private var uriWrapper: UriWrapper? = null
private var uri: Uri? = null
private var deepLinkEntryPoint = DeepLinkEntryPoint.DEFAULT
private var showOverlay = false

fun start(
action: String?,
Expand All @@ -57,7 +62,7 @@ class DeepLinkingIntentReceiverViewModel
handleRequest()
}

private fun handleRequest() {
fun handleRequest() {
uriWrapper?.let { uri ->
if (!handleUrl(uri, action)) {
trackWithDeepLinkDataAndFinish()
Expand Down Expand Up @@ -85,6 +90,16 @@ class DeepLinkingIntentReceiverViewModel
outState.putString(DEEP_LINK_ENTRY_POINT_KEY, deepLinkEntryPoint.name)
}

fun forwardDeepLinkToJetpack() {
uriWrapper?.let {
if (openWebLinksWithJetpackHelper.handleOpenWebLinksWithJetpack()) {
_navigateAction.value = Event(OpenJetpackForDeepLink(action = action, uri = it))
} else {
handleRequest()
}
}?: handleRequest()
}

/**
* Handles the following URLs
* `wordpress.com/post...`
Expand All @@ -98,7 +113,7 @@ class DeepLinkingIntentReceiverViewModel
deepLinkTrackingUtils.track(action, it, uriWrapper)
}
if (loginIsUnnecessary(it)) {
_navigateAction.value = Event(it)
_navigateAction.value = Event(it)
} else {
_navigateAction.value = Event(LoginForResult)
}
Expand Down Expand Up @@ -131,7 +146,7 @@ class DeepLinkingIntentReceiverViewModel

private fun extractSavedInstanceStateIfNeeded(savedInstanceState: Bundle?) {
savedInstanceState?.let {
uri = savedInstanceState.getParcelable(URI_KEY)
val uri: Uri? = savedInstanceState.getParcelable(URI_KEY)
uriWrapper = uri?.let { UriWrapper(it) }
deepLinkEntryPoint =
DeepLinkEntryPoint.valueOf(
Expand All @@ -153,7 +168,14 @@ class DeepLinkingIntentReceiverViewModel
}

private fun checkAndShowOpenWebLinksWithJetpackOverlayIfNeeded() : Boolean {
return showOverlay
return if (deepLinkEntryPoint == WEB_LINKS &&
accountStore.hasAccessToken() && // Already logged in
openWebLinksWithJetpackHelper.shouldShowDeepLinkOpenWebLinksWithJetpackOverlay()) {
_showOpenWebLinksWithJetpackOverlay.value = Event(Unit)
true
} else {
false
}
}

override fun onCleared() {
Expand Down
Loading

0 comments on commit 36a6fd2

Please sign in to comment.