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

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

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

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

    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!!)
47
48
49
50
51
52
        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 ?: ""))

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

        return view
    }

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

72
    @SuppressWarnings("ComplexMethod")
73
74
75
76
77
78
    private fun subscribeToActions() {
        getAutoDisposeObservable<ShareAction>().subscribe {
            when (it) {
                ShareAction.Close -> {
                    dismiss()
                }
79
80
                ShareAction.SignInClicked -> {
                    val directions = ShareFragmentDirections.actionShareFragmentToTurnOnSyncFragment()
81
                    nav(R.id.shareFragment, directions)
82
                    dismiss()
83
                }
84
                ShareAction.AddNewDeviceClicked -> {
85
86
87
88
89
90
91
92
93
94
                    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()
95
96
97
98
                }
                is ShareAction.ShareDeviceClicked -> {
                    val authAccount = requireComponents.backgroundServices.accountManager.authenticatedAccount()
                    authAccount?.run {
99
                        sendSendTab(this, it.device.id, tabs)
100
                    }
101
                    dismiss()
102
103
104
105
106
                }
                is ShareAction.SendAllClicked -> {
                    val authAccount = requireComponents.backgroundServices.accountManager.authenticatedAccount()
                    authAccount?.run {
                        it.devices.forEach { device ->
107
                            sendSendTab(this, device.id, tabs)
108
109
                        }
                    }
110
                    dismiss()
111
                }
112
                is ShareAction.ShareAppClicked -> {
113
114
115

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

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

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

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