2019-02-09 00:33:50 +01:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2019-07-12 20:38:15 +02:00
|
|
|
* 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/. */
|
2019-02-09 00:33:50 +01:00
|
|
|
|
|
|
|
package org.mozilla.fenix.library.history
|
|
|
|
|
2019-05-21 00:33:59 +02:00
|
|
|
import android.content.DialogInterface
|
2019-06-23 19:13:52 +02:00
|
|
|
import android.graphics.PorterDuff.Mode.SRC_IN
|
2019-05-10 18:58:54 +02:00
|
|
|
import android.graphics.PorterDuffColorFilter
|
2019-02-09 00:33:50 +01:00
|
|
|
import android.os.Bundle
|
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.Menu
|
|
|
|
import android.view.MenuInflater
|
|
|
|
import android.view.MenuItem
|
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
2019-05-21 00:33:59 +02:00
|
|
|
import androidx.appcompat.app.AlertDialog
|
2019-02-09 00:33:50 +01:00
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
2019-05-10 18:58:54 +02:00
|
|
|
import androidx.core.content.ContextCompat
|
2019-02-09 00:33:50 +01:00
|
|
|
import androidx.fragment.app.Fragment
|
2019-06-13 02:14:46 +02:00
|
|
|
import androidx.lifecycle.lifecycleScope
|
2019-07-11 23:39:06 +02:00
|
|
|
import androidx.lifecycle.whenStarted
|
2019-02-09 00:33:50 +01:00
|
|
|
import androidx.navigation.Navigation
|
2019-02-09 01:00:33 +01:00
|
|
|
import kotlinx.android.synthetic.main.fragment_history.view.*
|
2019-05-29 19:59:05 +02:00
|
|
|
import kotlinx.coroutines.Dispatchers
|
2019-05-10 18:58:54 +02:00
|
|
|
import kotlinx.coroutines.Dispatchers.Main
|
2019-02-09 01:21:55 +01:00
|
|
|
import kotlinx.coroutines.launch
|
2019-05-13 22:50:42 +02:00
|
|
|
import kotlinx.coroutines.withContext
|
2019-04-05 00:28:27 +02:00
|
|
|
import mozilla.components.concept.storage.VisitType
|
2019-07-11 23:39:06 +02:00
|
|
|
import mozilla.components.lib.state.ext.observe
|
2019-02-15 00:23:41 +01:00
|
|
|
import mozilla.components.support.base.feature.BackHandler
|
2019-04-05 22:11:05 +02:00
|
|
|
import org.mozilla.fenix.BrowserDirection
|
2019-05-10 18:58:54 +02:00
|
|
|
import org.mozilla.fenix.BrowsingModeManager
|
2019-04-05 22:11:05 +02:00
|
|
|
import org.mozilla.fenix.HomeActivity
|
2019-02-09 00:33:50 +01:00
|
|
|
import org.mozilla.fenix.R
|
2019-05-10 18:58:54 +02:00
|
|
|
import org.mozilla.fenix.components.Components
|
2019-07-11 23:39:06 +02:00
|
|
|
import org.mozilla.fenix.components.StoreProvider
|
2019-07-16 21:21:03 +02:00
|
|
|
import org.mozilla.fenix.components.metrics.Event
|
2019-05-10 18:58:54 +02:00
|
|
|
import org.mozilla.fenix.ext.components
|
2019-05-13 22:50:42 +02:00
|
|
|
import org.mozilla.fenix.ext.getHostFromUrl
|
2019-06-06 21:40:10 +02:00
|
|
|
import org.mozilla.fenix.ext.nav
|
2019-02-09 01:21:55 +01:00
|
|
|
import org.mozilla.fenix.ext.requireComponents
|
2019-05-29 00:05:16 +02:00
|
|
|
import org.mozilla.fenix.share.ShareTab
|
2019-05-13 22:50:42 +02:00
|
|
|
import java.util.concurrent.TimeUnit
|
2019-02-09 01:21:55 +01:00
|
|
|
|
2019-03-29 17:46:34 +01:00
|
|
|
@SuppressWarnings("TooManyFunctions")
|
2019-06-13 02:14:46 +02:00
|
|
|
class HistoryFragment : Fragment(), BackHandler {
|
2019-07-11 23:39:06 +02:00
|
|
|
private lateinit var historyStore: HistoryStore
|
|
|
|
private lateinit var historyView: HistoryView
|
|
|
|
private lateinit var historyInteractor: HistoryInteractor
|
2019-02-15 00:23:41 +01:00
|
|
|
|
2019-02-09 00:33:50 +01:00
|
|
|
override fun onCreateView(
|
|
|
|
inflater: LayoutInflater,
|
|
|
|
container: ViewGroup?,
|
|
|
|
savedInstanceState: Bundle?
|
2019-07-11 23:39:06 +02:00
|
|
|
): View? {
|
|
|
|
val view = inflater.inflate(R.layout.fragment_history, container, false)
|
|
|
|
historyStore = StoreProvider.get(
|
|
|
|
this,
|
|
|
|
HistoryStore(
|
|
|
|
HistoryState(
|
|
|
|
items = listOf(), mode = HistoryState.Mode.Normal
|
2019-05-29 13:40:56 +02:00
|
|
|
)
|
2019-05-15 08:16:48 +02:00
|
|
|
)
|
2019-07-11 23:39:06 +02:00
|
|
|
)
|
|
|
|
historyInteractor = HistoryInteractor(
|
|
|
|
historyStore,
|
|
|
|
::openItem,
|
|
|
|
::displayDeleteAllDialog,
|
|
|
|
::invalidateOptionsMenu,
|
|
|
|
::deleteHistoryItems
|
|
|
|
)
|
|
|
|
historyView = HistoryView(view.history_layout, historyInteractor)
|
|
|
|
return view
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun invalidateOptionsMenu() {
|
|
|
|
activity?.invalidateOptionsMenu()
|
|
|
|
}
|
2019-02-09 00:33:50 +01:00
|
|
|
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
2019-02-09 01:21:55 +01:00
|
|
|
|
2019-07-16 21:21:03 +02:00
|
|
|
requireComponents.analytics.metrics.track(Event.HistoryOpened)
|
2019-02-09 00:33:50 +01:00
|
|
|
setHasOptionsMenu(true)
|
2019-02-25 20:37:20 +01:00
|
|
|
}
|
|
|
|
|
2019-07-11 23:39:06 +02:00
|
|
|
fun deleteHistoryItems(items: List<HistoryItem>) {
|
|
|
|
lifecycleScope.launch {
|
|
|
|
val storage = context?.components?.core?.historyStorage
|
|
|
|
for (item in items) {
|
2019-07-18 22:36:52 +02:00
|
|
|
context?.components?.analytics?.metrics?.track(Event.HistoryItemRemoved)
|
2019-07-11 23:39:06 +02:00
|
|
|
storage?.deleteVisit(item.url, item.visitedAt)
|
|
|
|
}
|
|
|
|
reloadData()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-29 13:40:56 +02:00
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
|
super.onViewCreated(view, savedInstanceState)
|
|
|
|
|
2019-07-11 23:39:06 +02:00
|
|
|
historyStore.observe(view) {
|
|
|
|
viewLifecycleOwner.lifecycleScope.launch {
|
|
|
|
whenStarted {
|
|
|
|
historyView.update(it)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-09 00:33:50 +01:00
|
|
|
|
2019-07-11 23:39:06 +02:00
|
|
|
lifecycleScope.launch { reloadData() }
|
2019-05-29 13:40:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onResume() {
|
|
|
|
super.onResume()
|
|
|
|
(activity as AppCompatActivity).apply {
|
|
|
|
title = getString(R.string.library_history)
|
|
|
|
supportActionBar?.show()
|
|
|
|
}
|
2019-02-13 23:36:59 +01:00
|
|
|
}
|
|
|
|
|
2019-02-09 00:33:50 +01:00
|
|
|
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
|
2019-07-11 23:39:06 +02:00
|
|
|
val mode = historyStore.state.mode
|
2019-05-29 13:40:56 +02:00
|
|
|
when (mode) {
|
|
|
|
HistoryState.Mode.Normal ->
|
|
|
|
R.menu.library_menu
|
|
|
|
is HistoryState.Mode.Editing ->
|
|
|
|
R.menu.history_select_multi
|
2019-05-29 19:59:05 +02:00
|
|
|
else -> null
|
|
|
|
}?.let { inflater.inflate(it, menu) }
|
2019-05-13 22:50:42 +02:00
|
|
|
|
2019-05-29 13:40:56 +02:00
|
|
|
if (mode is HistoryState.Mode.Editing) {
|
|
|
|
menu.findItem(R.id.share_history_multi_select)?.run {
|
|
|
|
isVisible = mode.selectedItems.isNotEmpty()
|
|
|
|
icon.colorFilter = PorterDuffColorFilter(
|
|
|
|
ContextCompat.getColor(context!!, R.color.white_color),
|
2019-06-23 19:13:52 +02:00
|
|
|
SRC_IN
|
2019-05-29 13:40:56 +02:00
|
|
|
)
|
2019-02-17 02:04:32 +01:00
|
|
|
}
|
2019-05-29 13:40:56 +02:00
|
|
|
}
|
2019-02-09 01:21:55 +01:00
|
|
|
}
|
|
|
|
|
2019-05-29 13:40:56 +02:00
|
|
|
override fun onOptionsItemSelected(item: MenuItem): Boolean = when (item.itemId) {
|
|
|
|
R.id.share_history_multi_select -> {
|
2019-07-11 23:39:06 +02:00
|
|
|
val selectedHistory =
|
|
|
|
(historyStore.state.mode as? HistoryState.Mode.Editing)?.selectedItems ?: listOf()
|
2019-05-29 13:40:56 +02:00
|
|
|
when {
|
|
|
|
selectedHistory.size == 1 ->
|
|
|
|
share(selectedHistory.first().url)
|
|
|
|
selectedHistory.size > 1 -> {
|
|
|
|
val shareTabs = selectedHistory.map { ShareTab(it.url, it.title) }
|
|
|
|
share(tabs = shareTabs)
|
2019-05-10 18:58:54 +02:00
|
|
|
}
|
2019-02-09 00:33:50 +01:00
|
|
|
}
|
2019-05-29 13:40:56 +02:00
|
|
|
true
|
|
|
|
}
|
|
|
|
R.id.libraryClose -> {
|
|
|
|
Navigation.findNavController(requireActivity(), R.id.container)
|
|
|
|
.popBackStack(R.id.libraryFragment, true)
|
|
|
|
true
|
|
|
|
}
|
|
|
|
R.id.delete_history_multi_select -> {
|
|
|
|
val components = context?.applicationContext?.components!!
|
2019-07-11 23:39:06 +02:00
|
|
|
val selectedHistory =
|
|
|
|
(historyStore.state.mode as? HistoryState.Mode.Editing)?.selectedItems ?: listOf()
|
2019-05-10 18:58:54 +02:00
|
|
|
|
2019-06-13 02:14:46 +02:00
|
|
|
lifecycleScope.launch(Main) {
|
2019-05-29 13:40:56 +02:00
|
|
|
deleteSelectedHistory(selectedHistory, components)
|
|
|
|
reloadData()
|
2019-05-10 18:58:54 +02:00
|
|
|
}
|
2019-05-29 13:40:56 +02:00
|
|
|
true
|
|
|
|
}
|
|
|
|
R.id.open_history_in_new_tabs_multi_select -> {
|
2019-07-11 23:39:06 +02:00
|
|
|
val selectedHistory =
|
|
|
|
(historyStore.state.mode as? HistoryState.Mode.Editing)?.selectedItems ?: listOf()
|
2019-05-29 13:40:56 +02:00
|
|
|
requireComponents.useCases.tabsUseCases.addTab.let { useCase ->
|
|
|
|
for (selectedItem in selectedHistory) {
|
2019-07-18 22:36:52 +02:00
|
|
|
requireComponents.analytics.metrics.track(Event.HistoryItemOpened)
|
2019-05-29 13:40:56 +02:00
|
|
|
useCase.invoke(selectedItem.url)
|
2019-05-10 18:58:54 +02:00
|
|
|
}
|
2019-05-29 13:40:56 +02:00
|
|
|
}
|
2019-05-10 18:58:54 +02:00
|
|
|
|
2019-05-29 13:40:56 +02:00
|
|
|
(activity as HomeActivity).apply {
|
|
|
|
browsingModeManager.mode = BrowsingModeManager.Mode.Normal
|
|
|
|
supportActionBar?.hide()
|
2019-05-10 18:58:54 +02:00
|
|
|
}
|
2019-07-11 23:39:06 +02:00
|
|
|
nav(
|
|
|
|
R.id.historyFragment,
|
|
|
|
HistoryFragmentDirections.actionHistoryFragmentToHomeFragment()
|
|
|
|
)
|
2019-05-29 13:40:56 +02:00
|
|
|
true
|
|
|
|
}
|
|
|
|
R.id.open_history_in_private_tabs_multi_select -> {
|
2019-07-11 23:39:06 +02:00
|
|
|
val selectedHistory =
|
|
|
|
(historyStore.state.mode as? HistoryState.Mode.Editing)?.selectedItems ?: listOf()
|
2019-05-29 13:40:56 +02:00
|
|
|
requireComponents.useCases.tabsUseCases.addPrivateTab.let { useCase ->
|
|
|
|
for (selectedItem in selectedHistory) {
|
2019-07-18 22:36:52 +02:00
|
|
|
requireComponents.analytics.metrics.track(Event.HistoryItemOpened)
|
2019-05-29 13:40:56 +02:00
|
|
|
useCase.invoke(selectedItem.url)
|
2019-05-10 18:58:54 +02:00
|
|
|
}
|
2019-05-29 13:40:56 +02:00
|
|
|
}
|
2019-05-10 18:58:54 +02:00
|
|
|
|
2019-05-29 13:40:56 +02:00
|
|
|
(activity as HomeActivity).apply {
|
|
|
|
browsingModeManager.mode = BrowsingModeManager.Mode.Private
|
|
|
|
supportActionBar?.hide()
|
2019-05-10 18:58:54 +02:00
|
|
|
}
|
2019-07-11 23:39:06 +02:00
|
|
|
nav(
|
|
|
|
R.id.historyFragment,
|
|
|
|
HistoryFragmentDirections.actionHistoryFragmentToHomeFragment()
|
|
|
|
)
|
2019-05-29 13:40:56 +02:00
|
|
|
true
|
2019-02-09 00:33:50 +01:00
|
|
|
}
|
2019-05-29 13:40:56 +02:00
|
|
|
else -> super.onOptionsItemSelected(item)
|
2019-02-09 00:33:50 +01:00
|
|
|
}
|
2019-02-15 00:23:41 +01:00
|
|
|
|
2019-07-11 23:39:06 +02:00
|
|
|
override fun onBackPressed(): Boolean = historyView.onBackPressed()
|
2019-05-29 13:40:56 +02:00
|
|
|
|
2019-07-11 23:39:06 +02:00
|
|
|
fun openItem(item: HistoryItem) {
|
2019-07-16 21:21:03 +02:00
|
|
|
requireComponents.analytics.metrics.track(Event.HistoryItemOpened)
|
2019-05-29 13:40:56 +02:00
|
|
|
(activity as HomeActivity).openToBrowserAndLoad(
|
|
|
|
searchTermOrURL = item.url,
|
2019-07-01 23:31:30 +02:00
|
|
|
newTab = true,
|
2019-05-29 13:40:56 +02:00
|
|
|
from = BrowserDirection.FromHistory
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-07-11 23:39:06 +02:00
|
|
|
fun displayDeleteAllDialog() {
|
2019-05-29 13:40:56 +02:00
|
|
|
activity?.let { activity ->
|
2019-05-31 23:26:34 +02:00
|
|
|
AlertDialog.Builder(activity).apply {
|
2019-05-29 13:40:56 +02:00
|
|
|
setMessage(R.string.history_delete_all_dialog)
|
|
|
|
setNegativeButton(android.R.string.cancel) { dialog: DialogInterface, _ ->
|
|
|
|
dialog.cancel()
|
|
|
|
}
|
|
|
|
setPositiveButton(R.string.history_clear_dialog) { dialog: DialogInterface, _ ->
|
2019-07-11 23:39:06 +02:00
|
|
|
historyStore.dispatch(HistoryAction.EnterDeletionMode)
|
2019-06-13 02:14:46 +02:00
|
|
|
lifecycleScope.launch {
|
2019-07-16 21:21:03 +02:00
|
|
|
requireComponents.analytics.metrics.track(Event.HistoryAllItemsRemoved)
|
2019-05-29 13:40:56 +02:00
|
|
|
requireComponents.core.historyStorage.deleteEverything()
|
|
|
|
reloadData()
|
2019-05-29 19:59:05 +02:00
|
|
|
launch(Dispatchers.Main) {
|
2019-07-11 23:39:06 +02:00
|
|
|
historyStore.dispatch(HistoryAction.ExitDeletionMode)
|
2019-05-29 19:59:05 +02:00
|
|
|
}
|
2019-05-29 13:40:56 +02:00
|
|
|
}
|
2019-05-29 19:59:05 +02:00
|
|
|
|
2019-05-29 13:40:56 +02:00
|
|
|
dialog.dismiss()
|
|
|
|
}
|
|
|
|
create()
|
|
|
|
}.show()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-29 17:46:34 +01:00
|
|
|
private suspend fun reloadData() {
|
2019-05-16 02:23:01 +02:00
|
|
|
val excludeTypes = listOf(
|
|
|
|
VisitType.NOT_A_VISIT,
|
|
|
|
VisitType.DOWNLOAD,
|
|
|
|
VisitType.REDIRECT_TEMPORARY,
|
|
|
|
VisitType.RELOAD,
|
|
|
|
VisitType.EMBED,
|
|
|
|
VisitType.FRAMED_LINK,
|
|
|
|
VisitType.REDIRECT_PERMANENT
|
|
|
|
)
|
2019-05-13 22:50:42 +02:00
|
|
|
|
2019-04-05 00:28:27 +02:00
|
|
|
// Until we have proper pagination, only display a limited set of history to avoid blowing up the UI.
|
|
|
|
// See https://github.com/mozilla-mobile/fenix/issues/1393
|
2019-05-13 22:50:42 +02:00
|
|
|
val sinceTimeMs = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(HISTORY_TIME_DAYS)
|
|
|
|
val items = requireComponents.core.historyStorage
|
|
|
|
.getDetailedVisits(sinceTimeMs, excludeTypes = excludeTypes)
|
2019-04-05 00:28:27 +02:00
|
|
|
// We potentially have a large amount of visits, and multiple processing steps.
|
2019-05-29 13:40:56 +02:00
|
|
|
// Wrapping iterator in a sequence should make this a little memory-more efficient.
|
2019-04-05 00:28:27 +02:00
|
|
|
.asSequence()
|
|
|
|
.sortedByDescending { it.visitTime }
|
2019-04-09 22:53:32 +02:00
|
|
|
.mapIndexed { id, item ->
|
2019-05-13 22:50:42 +02:00
|
|
|
val title = item.title
|
|
|
|
?.takeIf(String::isNotEmpty)
|
|
|
|
?: item.url.getHostFromUrl()
|
|
|
|
?: item.url
|
|
|
|
|
|
|
|
HistoryItem(id, title, item.url, item.visitTime)
|
2019-04-09 22:53:32 +02:00
|
|
|
}
|
2019-04-05 00:28:27 +02:00
|
|
|
.toList()
|
2019-03-29 17:46:34 +01:00
|
|
|
|
2019-05-13 22:50:42 +02:00
|
|
|
withContext(Main) {
|
2019-07-11 23:39:06 +02:00
|
|
|
historyStore.dispatch(HistoryAction.Change(items))
|
2019-03-29 17:46:34 +01:00
|
|
|
}
|
|
|
|
}
|
2019-05-10 18:58:54 +02:00
|
|
|
|
|
|
|
private suspend fun deleteSelectedHistory(
|
|
|
|
selected: List<HistoryItem>,
|
|
|
|
components: Components = requireComponents
|
|
|
|
) {
|
2019-07-16 21:21:03 +02:00
|
|
|
requireComponents.analytics.metrics.track(Event.HistoryItemRemoved)
|
2019-05-29 13:40:56 +02:00
|
|
|
val storage = components.core.historyStorage
|
|
|
|
for (item in selected) {
|
|
|
|
storage.deleteVisit(item.url, item.visitedAt)
|
2019-05-10 18:58:54 +02:00
|
|
|
}
|
|
|
|
}
|
2019-05-23 19:48:22 +02:00
|
|
|
|
2019-05-29 00:05:16 +02:00
|
|
|
private fun share(url: String? = null, tabs: List<ShareTab>? = null) {
|
2019-07-16 21:21:03 +02:00
|
|
|
requireComponents.analytics.metrics.track(Event.HistoryItemShared)
|
2019-05-29 00:05:16 +02:00
|
|
|
val directions =
|
2019-06-15 01:46:40 +02:00
|
|
|
HistoryFragmentDirections.actionHistoryFragmentToShareFragment(
|
|
|
|
url = url,
|
|
|
|
tabs = tabs?.toTypedArray()
|
|
|
|
)
|
2019-06-06 21:40:10 +02:00
|
|
|
nav(R.id.historyFragment, directions)
|
2019-05-23 19:48:22 +02:00
|
|
|
}
|
2019-05-29 13:40:56 +02:00
|
|
|
|
2019-05-29 19:59:05 +02:00
|
|
|
companion object {
|
|
|
|
private const val HISTORY_TIME_DAYS = 3L
|
|
|
|
}
|
|
|
|
}
|