ShareFragment.kt 5.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
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
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
24
import org.mozilla.fenix.FenixViewModelProvider
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
    private lateinit var component: ShareComponent
32
    private var tabs: Array<ShareTab> = emptyArray()
33
34
35

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

    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!!)

43
44
45
46
47
48
        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 ?: ""))

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

        return view
    }

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

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

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

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

    private fun sendSendTab(account: OAuthAccount, deviceId: String, tabs: Array<ShareTab>) {
        account.run {
            tabs.forEach { tab ->
                deviceConstellation().sendEventToDeviceAsync(
                    deviceId,
                    DeviceEventOutgoing.SendTab(tab.title, tab.url)
                )
            }
        }
    }
135
}
136
137
138

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