InstalledAddonDetailsFragment.kt 13 KB
Newer Older
Gabriel Luong's avatar
Gabriel Luong committed
1
2
3
4
5
6
7
8
9
10
/* 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/. */

package org.mozilla.fenix.addons

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
11
import androidx.core.view.isVisible
Gabriel Luong's avatar
Gabriel Luong committed
12
import androidx.fragment.app.Fragment
13
import androidx.lifecycle.lifecycleScope
Gabriel Luong's avatar
Gabriel Luong committed
14
15
import androidx.navigation.Navigation
import androidx.navigation.findNavController
16
import androidx.navigation.fragment.findNavController
17
import com.google.android.material.switchmaterial.SwitchMaterial
Gabriel Luong's avatar
Gabriel Luong committed
18
import kotlinx.android.synthetic.main.fragment_installed_add_on_details.view.*
19
20
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
Gabriel Luong's avatar
Gabriel Luong committed
21
import mozilla.components.feature.addons.Addon
22
import mozilla.components.feature.addons.AddonManagerException
23
import mozilla.components.feature.addons.ui.translateName
Alex Catarineu's avatar
Alex Catarineu committed
24
import mozilla.components.support.webextensions.WebExtensionSupport.installedExtensions
25
import org.mozilla.fenix.HomeActivity
Gabriel Luong's avatar
Gabriel Luong committed
26
27
28
29
30
31
32
import org.mozilla.fenix.R
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.showToolbar

/**
 * An activity to show the details of a installed add-on.
 */
33
@Suppress("LargeClass", "TooManyFunctions")
Gabriel Luong's avatar
Gabriel Luong committed
34
35
36
37
38
39
40
41
42
43
44
45
class InstalledAddonDetailsFragment : Fragment() {
    private lateinit var addon: Addon

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        if (!::addon.isInitialized) {
            addon = AddonDetailsFragmentArgs.fromBundle(requireNotNull(arguments)).addon
        }

46
47
48
        return inflater.inflate(R.layout.fragment_installed_add_on_details, container, false).also {
            bindUI(it)
        }
49
50
51
52
53
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        bindAddon(view)
Gabriel Luong's avatar
Gabriel Luong committed
54
55
    }

56
57
58
59
60
61
62
63
64
65
66
67
68
69
    private fun bindAddon(view: View) {
        lifecycleScope.launch(Dispatchers.IO) {
            try {
                val addons = requireContext().components.addonManager.getAddons()
                lifecycleScope.launch(Dispatchers.Main) {
                    runIfFragmentIsAttached {
                        addons.find { addon.id == it.id }.let {
                            if (it == null) {
                                throw AddonManagerException(Exception("Addon ${addon.id} not found"))
                            } else {
                                addon = it
                                bindUI(view)
                            }
                            view.add_on_progress_bar.isVisible = false
70
                            view.addon_container.isVisible = true
71
72
73
                        }
                    }
                }
74
75
76
77
78
79
80
            } catch (e: AddonManagerException) {
                lifecycleScope.launch(Dispatchers.Main) {
                    runIfFragmentIsAttached {
                        showSnackBar(
                            view,
                            getString(R.string.mozac_feature_addons_failed_to_query_add_ons)
                        )
81
                        findNavController().popBackStack()
82
83
84
                    }
                }
            }
85
86
87
        }
    }

88
    private fun bindUI(view: View) {
89
        val title = addon.translateName(view.context)
Gabriel Luong's avatar
Gabriel Luong committed
90
91
92
93
94
95
        showToolbar(title)

        bindEnableSwitch(view)
        bindSettings(view)
        bindDetails(view)
        bindPermissions(view)
96
        bindAllowInPrivateBrowsingSwitch(view)
Gabriel Luong's avatar
Gabriel Luong committed
97
98
99
        bindRemoveButton(view)
    }

100
    @SuppressWarnings("LongMethod")
Gabriel Luong's avatar
Gabriel Luong committed
101
102
    private fun bindEnableSwitch(view: View) {
        val switch = view.enable_switch
103
        val privateBrowsingSwitch = view.allow_in_private_browsing_switch
Gabriel Luong's avatar
Gabriel Luong committed
104
        switch.setState(addon.isEnabled())
Tiger Oakes's avatar
Tiger Oakes committed
105
106
        switch.setOnCheckedChangeListener { v, isChecked ->
            val addonManager = v.context.components.addonManager
107
            switch.isClickable = false
108
            view.remove_add_on.isEnabled = false
Gabriel Luong's avatar
Gabriel Luong committed
109
            if (isChecked) {
Tiger Oakes's avatar
Tiger Oakes committed
110
                addonManager.enableAddon(
Gabriel Luong's avatar
Gabriel Luong committed
111
112
                    addon,
                    onSuccess = {
113
                        runIfFragmentIsAttached {
114
                            this.addon = it
115
                            switch.isClickable = true
Alex Catarineu's avatar
Alex Catarineu committed
116
                            privateBrowsingSwitch.isVisible = false
117
                            privateBrowsingSwitch.isChecked = it.isAllowedInPrivateBrowsing()
118
                            switch.setText(R.string.mozac_feature_addons_enabled)
119
                            view.settings.isVisible = shouldSettingsBeVisible()
120
                            view.remove_add_on.isEnabled = true
121
122
123
124
125
126
127
                            context?.let {
                                showSnackBar(
                                    view,
                                    getString(
                                        R.string.mozac_feature_addons_successfully_enabled,
                                        addon.translateName(it)
                                    )
128
                                )
129
                            }
130
                        }
Gabriel Luong's avatar
Gabriel Luong committed
131
132
                    },
                    onError = {
133
                        runIfFragmentIsAttached {
134
                            switch.isClickable = true
135
                            view.remove_add_on.isEnabled = true
136
                            switch.setState(addon.isEnabled())
137
138
139
140
141
142
143
                            context?.let {
                                showSnackBar(
                                    view,
                                    getString(
                                        R.string.mozac_feature_addons_failed_to_enable,
                                        addon.translateName(it)
                                    )
144
                                )
145
                            }
146
                        }
Gabriel Luong's avatar
Gabriel Luong committed
147
148
149
                    }
                )
            } else {
150
                view.settings.isVisible = false
Tiger Oakes's avatar
Tiger Oakes committed
151
                addonManager.disableAddon(
Gabriel Luong's avatar
Gabriel Luong committed
152
153
                    addon,
                    onSuccess = {
154
                        runIfFragmentIsAttached {
155
                            this.addon = it
156
                            switch.isClickable = true
157
                            privateBrowsingSwitch.isVisible = it.isEnabled()
158
                            switch.setText(R.string.mozac_feature_addons_disabled)
159
                            view.remove_add_on.isEnabled = true
160
161
162
163
164
165
166
                            context?.let {
                                showSnackBar(
                                    view,
                                    getString(
                                        R.string.mozac_feature_addons_successfully_disabled,
                                        addon.translateName(it)
                                    )
167
                                )
168
                            }
169
                        }
Gabriel Luong's avatar
Gabriel Luong committed
170
171
                    },
                    onError = {
172
                        runIfFragmentIsAttached {
173
                            switch.isClickable = true
174
                            privateBrowsingSwitch.isClickable = true
175
                            view.remove_add_on.isEnabled = true
176
                            switch.setState(addon.isEnabled())
177
178
179
180
181
182
183
                            context?.let {
                                showSnackBar(
                                    view,
                                    getString(
                                        R.string.mozac_feature_addons_failed_to_disable,
                                        addon.translateName(it)
                                    )
184
                                )
185
                            }
186
                        }
Gabriel Luong's avatar
Gabriel Luong committed
187
188
189
190
191
192
193
194
                    }
                )
            }
        }
    }

    private fun bindSettings(view: View) {
        view.settings.apply {
195
            isVisible = shouldSettingsBeVisible()
Gabriel Luong's avatar
Gabriel Luong committed
196
            setOnClickListener {
197
                val settingUrl = addon.installedState?.optionsPageUrl ?: return@setOnClickListener
198
199
200
                val directions = if (addon.installedState?.openOptionsPageInTab == true) {
                    val components = it.context.components
                    val shouldCreatePrivateSession =
201
                        (activity as HomeActivity).browsingModeManager.mode.isPrivate
202
203

                    if (shouldCreatePrivateSession) {
204
                        components.useCases.tabsUseCases.addPrivateTab(settingUrl)
205
                    } else {
206
                        components.useCases.tabsUseCases.addTab(settingUrl)
207
208
                    }

209
                    InstalledAddonDetailsFragmentDirections.actionGlobalBrowser(null)
210
211
212
213
                } else {
                    InstalledAddonDetailsFragmentDirections
                        .actionInstalledAddonFragmentToAddonInternalSettingsFragment(addon)
                }
Gabriel Luong's avatar
Gabriel Luong committed
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
                Navigation.findNavController(this).navigate(directions)
            }
        }
    }

    private fun bindDetails(view: View) {
        view.details.setOnClickListener {
            val directions =
                InstalledAddonDetailsFragmentDirections.actionInstalledAddonFragmentToAddonDetailsFragment(
                    addon
                )
            Navigation.findNavController(view).navigate(directions)
        }
    }

    private fun bindPermissions(view: View) {
        view.permissions.setOnClickListener {
            val directions =
                InstalledAddonDetailsFragmentDirections.actionInstalledAddonFragmentToAddonPermissionsDetailsFragment(
                    addon
                )
            Navigation.findNavController(view).navigate(directions)
        }
    }

239
240
241
    private fun bindAllowInPrivateBrowsingSwitch(view: View) {
        val switch = view.allow_in_private_browsing_switch
        switch.isChecked = addon.isAllowedInPrivateBrowsing()
Alex Catarineu's avatar
Alex Catarineu committed
242
        switch.isVisible = false
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
        switch.setOnCheckedChangeListener { v, isChecked ->
            val addonManager = v.context.components.addonManager
            switch.isClickable = false
            view.remove_add_on.isEnabled = false
            addonManager.setAddonAllowedInPrivateBrowsing(
                addon,
                isChecked,
                onSuccess = {
                    runIfFragmentIsAttached {
                        this.addon = it
                        switch.isClickable = true
                        view.remove_add_on.isEnabled = true
                    }
                },
                onError = {
                    runIfFragmentIsAttached {
259
                        switch.isChecked = addon.isAllowedInPrivateBrowsing()
260
261
262
263
264
265
266
                        switch.isClickable = true
                        view.remove_add_on.isEnabled = true
                    }
                }
            )
        }
    }
Gabriel Luong's avatar
Gabriel Luong committed
267
    private fun bindRemoveButton(view: View) {
Alex Catarineu's avatar
Alex Catarineu committed
268
269
        val isBuiltin = installedExtensions[addon.id]?.isBuiltIn() ?: false
        view.remove_add_on.isVisible = !isBuiltin
Gabriel Luong's avatar
Gabriel Luong committed
270
        view.remove_add_on.setOnClickListener {
271
            setAllInteractiveViewsClickable(view, false)
Gabriel Luong's avatar
Gabriel Luong committed
272
273
274
            requireContext().components.addonManager.uninstallAddon(
                addon,
                onSuccess = {
275
                    runIfFragmentIsAttached {
276
                        setAllInteractiveViewsClickable(view, true)
277
278
279
280
281
282
283
                        context?.let {
                            showSnackBar(
                                view,
                                getString(
                                    R.string.mozac_feature_addons_successfully_uninstalled,
                                    addon.translateName(it)
                                )
284
                            )
285
                        }
286
287
                        view.findNavController().popBackStack()
                    }
Gabriel Luong's avatar
Gabriel Luong committed
288
289
                },
                onError = { _, _ ->
290
                    runIfFragmentIsAttached {
291
                        setAllInteractiveViewsClickable(view, true)
292
293
294
295
296
297
298
                        context?.let {
                            showSnackBar(
                                view,
                                getString(
                                    R.string.mozac_feature_addons_failed_to_uninstall,
                                    addon.translateName(it)
                                )
299
                            )
300
                        }
301
                    }
Gabriel Luong's avatar
Gabriel Luong committed
302
303
304
305
306
                }
            )
        }
    }

307
308
    private fun setAllInteractiveViewsClickable(view: View, clickable: Boolean) {
        view.enable_switch.isClickable = clickable
309
        view.settings.isClickable = clickable
310
311
312
313
314
        view.details.isClickable = clickable
        view.permissions.isClickable = clickable
        view.remove_add_on.isClickable = clickable
    }

315
    private fun SwitchMaterial.setState(checked: Boolean) {
Gabriel Luong's avatar
Gabriel Luong committed
316
        val text = if (checked) {
317
            R.string.mozac_feature_addons_enabled
Gabriel Luong's avatar
Gabriel Luong committed
318
        } else {
319
            R.string.mozac_feature_addons_disabled
Gabriel Luong's avatar
Gabriel Luong committed
320
321
322
323
        }
        setText(text)
        isChecked = checked
    }
324
325

    private fun shouldSettingsBeVisible() = !addon.installedState?.optionsPageUrl.isNullOrEmpty()
Gabriel Luong's avatar
Gabriel Luong committed
326
}