2019-05-23 19:48:22 +02:00
|
|
|
package org.mozilla.fenix.share
|
|
|
|
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
import android.content.Intent
|
|
|
|
import android.content.Intent.ACTION_SEND
|
|
|
|
import android.content.Intent.EXTRA_TEXT
|
|
|
|
import android.content.Intent.FLAG_ACTIVITY_NEW_TASK
|
|
|
|
import android.os.Bundle
|
2019-05-24 01:51:25 +02:00
|
|
|
import android.view.ContextThemeWrapper
|
2019-05-23 19:48:22 +02:00
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
2019-05-24 01:51:25 +02:00
|
|
|
import androidx.appcompat.app.AlertDialog
|
2019-05-24 00:38:03 +02:00
|
|
|
import androidx.appcompat.app.AppCompatDialogFragment
|
|
|
|
import androidx.navigation.fragment.NavHostFragment.findNavController
|
2019-05-23 19:48:22 +02:00
|
|
|
import kotlinx.android.synthetic.main.fragment_share.view.*
|
|
|
|
import kotlinx.coroutines.CoroutineScope
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
import kotlinx.coroutines.Job
|
2019-05-24 00:38:03 +02:00
|
|
|
import mozilla.components.concept.sync.DeviceEventOutgoing
|
2019-05-23 19:48:22 +02:00
|
|
|
import org.mozilla.fenix.FenixViewModelProvider
|
|
|
|
import org.mozilla.fenix.R
|
|
|
|
import org.mozilla.fenix.ext.requireComponents
|
|
|
|
import org.mozilla.fenix.mvi.ActionBusFactory
|
|
|
|
import org.mozilla.fenix.mvi.getAutoDisposeObservable
|
|
|
|
import kotlin.coroutines.CoroutineContext
|
|
|
|
|
2019-05-24 00:38:03 +02:00
|
|
|
class ShareFragment : AppCompatDialogFragment(), CoroutineScope {
|
2019-05-23 19:48:22 +02:00
|
|
|
override val coroutineContext: CoroutineContext
|
|
|
|
get() = Dispatchers.Main + job
|
|
|
|
private lateinit var job: Job
|
|
|
|
private lateinit var component: ShareComponent
|
|
|
|
private lateinit var url: String
|
2019-05-24 00:38:03 +02:00
|
|
|
private lateinit var title: String
|
2019-05-23 19:48:22 +02:00
|
|
|
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
setStyle(STYLE_NO_TITLE, R.style.CreateCollectionDialogStyle)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
|
|
|
|
val view = inflater.inflate(R.layout.fragment_share, container, false)
|
|
|
|
val args = ShareFragmentArgs.fromBundle(arguments!!)
|
|
|
|
|
|
|
|
job = Job()
|
|
|
|
url = args.url
|
2019-05-24 00:38:03 +02:00
|
|
|
title = args.title ?: ""
|
2019-05-23 19:48:22 +02:00
|
|
|
component = ShareComponent(
|
|
|
|
view.share_wrapper,
|
|
|
|
ActionBusFactory.get(this),
|
|
|
|
FenixViewModelProvider.create(
|
|
|
|
this,
|
|
|
|
ShareUIViewModel::class.java
|
|
|
|
) {
|
|
|
|
ShareUIViewModel(ShareState)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
return view
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onResume() {
|
|
|
|
super.onResume()
|
|
|
|
subscribeToActions()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onDestroyView() {
|
|
|
|
super.onDestroyView()
|
|
|
|
job.cancel()
|
|
|
|
}
|
|
|
|
|
2019-05-24 00:38:03 +02:00
|
|
|
@SuppressWarnings("ComplexMethod")
|
2019-05-23 19:48:22 +02:00
|
|
|
private fun subscribeToActions() {
|
|
|
|
getAutoDisposeObservable<ShareAction>().subscribe {
|
|
|
|
when (it) {
|
|
|
|
ShareAction.Close -> {
|
|
|
|
dismiss()
|
|
|
|
}
|
2019-05-24 00:38:03 +02:00
|
|
|
ShareAction.SignInClicked -> {
|
|
|
|
val directions = ShareFragmentDirections.actionShareFragmentToTurnOnSyncFragment()
|
|
|
|
findNavController(this@ShareFragment).navigate(directions)
|
2019-05-24 01:51:25 +02:00
|
|
|
dismiss()
|
2019-05-24 00:38:03 +02:00
|
|
|
}
|
2019-05-23 19:48:22 +02:00
|
|
|
ShareAction.AddNewDeviceClicked -> {
|
2019-05-24 01:51:25 +02:00
|
|
|
AlertDialog.Builder(
|
|
|
|
ContextThemeWrapper(
|
|
|
|
context,
|
|
|
|
R.style.DialogStyle
|
|
|
|
)
|
|
|
|
).apply {
|
|
|
|
setMessage(R.string.sync_connect_device_dialog)
|
|
|
|
setPositiveButton(R.string.sync_confirmation_button) { dialog, _ -> dialog.cancel() }
|
|
|
|
create()
|
|
|
|
}.show()
|
2019-05-24 00:38:03 +02:00
|
|
|
}
|
|
|
|
is ShareAction.ShareDeviceClicked -> {
|
|
|
|
val authAccount = requireComponents.backgroundServices.accountManager.authenticatedAccount()
|
|
|
|
authAccount?.run {
|
|
|
|
deviceConstellation().sendEventToDeviceAsync(
|
|
|
|
it.device.id,
|
|
|
|
DeviceEventOutgoing.SendTab(title, url)
|
|
|
|
)
|
|
|
|
}
|
2019-05-24 01:51:25 +02:00
|
|
|
dismiss()
|
2019-05-24 00:38:03 +02:00
|
|
|
}
|
|
|
|
is ShareAction.SendAllClicked -> {
|
|
|
|
val authAccount = requireComponents.backgroundServices.accountManager.authenticatedAccount()
|
|
|
|
authAccount?.run {
|
|
|
|
it.devices.forEach { device ->
|
|
|
|
deviceConstellation().sendEventToDeviceAsync(
|
|
|
|
device.id,
|
|
|
|
DeviceEventOutgoing.SendTab(title, url)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2019-05-24 01:51:25 +02:00
|
|
|
dismiss()
|
2019-05-24 00:38:03 +02:00
|
|
|
}
|
2019-05-23 19:48:22 +02:00
|
|
|
is ShareAction.ShareAppClicked -> {
|
|
|
|
val intent = Intent(ACTION_SEND).apply {
|
|
|
|
putExtra(EXTRA_TEXT, url)
|
|
|
|
type = "text/plain"
|
|
|
|
flags = FLAG_ACTIVITY_NEW_TASK
|
|
|
|
`package` = it.packageName
|
|
|
|
}
|
|
|
|
startActivity(intent)
|
2019-05-24 01:51:25 +02:00
|
|
|
dismiss()
|
2019-05-23 19:48:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|