ShareFragment.kt 5.24 KB
Newer Older
1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
3
4
5
 * 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/. */

package org.mozilla.fenix.share
6
7
8
9
10
11

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
12
import android.os.Parcelable
13
14
15
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
16
import androidx.appcompat.app.AlertDialog
17
import androidx.appcompat.app.AppCompatDialogFragment
18
import kotlinx.android.parcel.Parcelize
19
import kotlinx.android.synthetic.main.fragment_share.view.*
20
import mozilla.components.concept.sync.DeviceEventOutgoing
21
import mozilla.components.concept.sync.OAuthAccount
22
import org.mozilla.fenix.FenixViewModelProvider
23
import org.mozilla.fenix.HomeActivity
24
import org.mozilla.fenix.R
25
import org.mozilla.fenix.ext.nav
26
27
28
29
import org.mozilla.fenix.ext.requireComponents
import org.mozilla.fenix.mvi.ActionBusFactory
import org.mozilla.fenix.mvi.getAutoDisposeObservable

30
class ShareFragment : AppCompatDialogFragment() {
31
32
33
34
    interface TabsSharedCallback {
        fun onTabsShared(tabsSize: Int)
    }

35
    private lateinit var component: ShareComponent
36
    private var tabs: Array<ShareTab> = emptyArray()
37
38
39

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
40
        setStyle(STYLE_NO_TITLE, R.style.ShareDialogStyle)
41
42
    }

43
44
45
46
47
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
48
49
        val view = inflater.inflate(R.layout.fragment_share, container, false)
        val args = ShareFragmentArgs.fromBundle(arguments!!)
50
51
52
53
54
55
        if (args.url == null && args.tabs.isNullOrEmpty()) {
            throw IllegalStateException("URL and tabs cannot both be null.")
        }

        tabs = args.tabs ?: arrayOf(ShareTab(args.url!!, args.title ?: ""))

56
        component = ShareComponent(
57
            view.shareWrapper,
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
            ActionBusFactory.get(this),
            FenixViewModelProvider.create(
                this,
                ShareUIViewModel::class.java
            ) {
                ShareUIViewModel(ShareState)
            }
        )

        return view
    }

    override fun onResume() {
        super.onResume()
        subscribeToActions()
    }

75
    @SuppressWarnings("ComplexMethod")
76
77
78
79
80
81
    private fun subscribeToActions() {
        getAutoDisposeObservable<ShareAction>().subscribe {
            when (it) {
                ShareAction.Close -> {
                    dismiss()
                }
82
                ShareAction.SignInClicked -> {
83
84
                    val directions =
                        ShareFragmentDirections.actionShareFragmentToTurnOnSyncFragment()
85
                    nav(R.id.shareFragment, directions)
86
                    dismiss()
87
                }
88
                ShareAction.AddNewDeviceClicked -> {
89
90
91
92
93
94
95
                    context?.let {
                        AlertDialog.Builder(it).apply {
                            setMessage(R.string.sync_connect_device_dialog)
                            setPositiveButton(R.string.sync_confirmation_button) { dialog, _ -> dialog.cancel() }
                            create()
                        }.show()
                    }
96
97
                }
                is ShareAction.ShareDeviceClicked -> {
98
99
                    val authAccount =
                        requireComponents.backgroundServices.accountManager.authenticatedAccount()
100
                    authAccount?.run {
101
                        sendSendTab(this, it.device.id, tabs)
102
                    }
103
                    dismiss()
104
105
                }
                is ShareAction.SendAllClicked -> {
106
107
                    val authAccount =
                        requireComponents.backgroundServices.accountManager.authenticatedAccount()
108
109
                    authAccount?.run {
                        it.devices.forEach { device ->
110
                            sendSendTab(this, device.id, tabs)
111
112
                        }
                    }
113
                    dismiss()
114
                }
115
                is ShareAction.ShareAppClicked -> {
116
117
118

                    val shareText = tabs.joinToString("\n") { tab -> tab.url }

119
                    val intent = Intent(ACTION_SEND).apply {
120
                        putExtra(EXTRA_TEXT, shareText)
121
122
                        type = "text/plain"
                        flags = FLAG_ACTIVITY_NEW_TASK
123
                        setClassName(it.item.packageName, it.item.activityName)
124
125
                    }
                    startActivity(intent)
126
                    dismiss()
127
128
129
130
                }
            }
        }
    }
131
132
133
134
135
136
137
138
139
140

    private fun sendSendTab(account: OAuthAccount, deviceId: String, tabs: Array<ShareTab>) {
        account.run {
            tabs.forEach { tab ->
                deviceConstellation().sendEventToDeviceAsync(
                    deviceId,
                    DeviceEventOutgoing.SendTab(tab.title, tab.url)
                )
            }
        }
141
        (activity as? HomeActivity)?.onTabsShared(tabs.size)
142
    }
143
}
144
145
146

@Parcelize
data class ShareTab(val url: String, val title: String, val sessionId: String? = null) : Parcelable