For #225 - Deletes data on button press
parent
3cf91fc303
commit
51451ff002
|
@ -4,20 +4,28 @@
|
||||||
|
|
||||||
package org.mozilla.fenix.settings
|
package org.mozilla.fenix.settings
|
||||||
|
|
||||||
|
import android.content.DialogInterface
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
|
import android.view.ContextThemeWrapper
|
||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
|
import androidx.appcompat.app.AlertDialog
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.fragment.app.Fragment
|
import androidx.fragment.app.Fragment
|
||||||
|
import kotlinx.android.synthetic.main.fragment_delete_browsing_data.*
|
||||||
import kotlinx.android.synthetic.main.fragment_delete_browsing_data.view.*
|
import kotlinx.android.synthetic.main.fragment_delete_browsing_data.view.*
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.Deferred
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.Job
|
import kotlinx.coroutines.Job
|
||||||
|
import kotlinx.coroutines.async
|
||||||
|
import kotlinx.coroutines.awaitAll
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import mozilla.components.browser.session.Session
|
import mozilla.components.browser.session.Session
|
||||||
import mozilla.components.browser.session.SessionManager
|
import mozilla.components.browser.session.SessionManager
|
||||||
|
import mozilla.components.concept.engine.Engine
|
||||||
import org.mozilla.fenix.R
|
import org.mozilla.fenix.R
|
||||||
import org.mozilla.fenix.ext.requireComponents
|
import org.mozilla.fenix.ext.requireComponents
|
||||||
import kotlin.coroutines.CoroutineContext
|
import kotlin.coroutines.CoroutineContext
|
||||||
|
@ -66,6 +74,12 @@ class DeleteBrowsingDataFragment : Fragment(), CoroutineScope {
|
||||||
requireComponents.core.sessionManager.register(sessionObserver, owner = this)
|
requireComponents.core.sessionManager.register(sessionObserver, owner = this)
|
||||||
|
|
||||||
view?.open_tabs_item?.onCheckListener = { _ -> updateDeleteButton() }
|
view?.open_tabs_item?.onCheckListener = { _ -> updateDeleteButton() }
|
||||||
|
view?.browsing_data_item?.onCheckListener = { _ -> updateDeleteButton() }
|
||||||
|
view?.collections_item?.onCheckListener = { _ -> updateDeleteButton() }
|
||||||
|
|
||||||
|
view?.delete_data?.setOnClickListener {
|
||||||
|
askToDelete()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onDestroyView() {
|
override fun onDestroyView() {
|
||||||
|
@ -86,13 +100,76 @@ class DeleteBrowsingDataFragment : Fragment(), CoroutineScope {
|
||||||
updateDeleteButton()
|
updateDeleteButton()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun updateDeleteButton() {
|
private fun askToDelete() {
|
||||||
view?.delete_data?.isEnabled =
|
AlertDialog.Builder(
|
||||||
view!!.open_tabs_item!!.isChecked
|
ContextThemeWrapper(
|
||||||
|| view!!.browsing_data_item!!.isChecked
|
activity,
|
||||||
|| view!!.collections_item!!.isChecked
|
R.style.DialogStyle
|
||||||
|
)
|
||||||
|
).apply {
|
||||||
|
val appName = context.getString(R.string.app_name)
|
||||||
|
val message = context.getString(R.string.preferences_delete_browsing_data_prompt_message, appName)
|
||||||
|
setMessage(message)
|
||||||
|
|
||||||
Log.e("wat", view?.delete_data?.isEnabled.toString())
|
setNegativeButton(R.string.preferences_delete_browsing_data_prompt_cancel) { dialog: DialogInterface, _ ->
|
||||||
|
dialog.cancel()
|
||||||
|
}
|
||||||
|
|
||||||
|
setPositiveButton(R.string.preferences_delete_browsing_data_prompt_allow) { dialog: DialogInterface, _ ->
|
||||||
|
dialog.dismiss()
|
||||||
|
deleteSelected()
|
||||||
|
}
|
||||||
|
create()
|
||||||
|
}.show()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun deleteSelected() {
|
||||||
|
val open_tabs = view!!.open_tabs_item!!.isChecked
|
||||||
|
val browsing_data = view!!.browsing_data_item!!.isChecked
|
||||||
|
val collections = view!!.collections_item!!.isChecked
|
||||||
|
|
||||||
|
startDeletion()
|
||||||
|
launch(Dispatchers.IO) {
|
||||||
|
var jobs = mutableListOf<Deferred<Unit>>()
|
||||||
|
if (open_tabs) jobs.add(deleteTabsAsync())
|
||||||
|
if (browsing_data) jobs.add(deleteBrowsingDataAsync())
|
||||||
|
if (collections) jobs.add(deleteCollectionsAsync())
|
||||||
|
|
||||||
|
jobs.awaitAll()
|
||||||
|
|
||||||
|
launch(Dispatchers.Main) {
|
||||||
|
finishDeletion()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
fun startDeletion() {
|
||||||
|
progress_bar.visibility = View.VISIBLE
|
||||||
|
delete_browsing_data_wrapper.isEnabled = false
|
||||||
|
delete_browsing_data_wrapper.isClickable = false
|
||||||
|
delete_browsing_data_wrapper.alpha = 0.6f
|
||||||
|
}
|
||||||
|
|
||||||
|
fun finishDeletion() {
|
||||||
|
progress_bar.visibility = View.GONE
|
||||||
|
delete_browsing_data_wrapper.isEnabled = true
|
||||||
|
delete_browsing_data_wrapper.isClickable = true
|
||||||
|
delete_browsing_data_wrapper.alpha = 1f
|
||||||
|
|
||||||
|
updateTabCount()
|
||||||
|
updateHistoryCount()
|
||||||
|
updateCollectionsCount()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun updateDeleteButton() {
|
||||||
|
val open_tabs = view!!.open_tabs_item!!.isChecked
|
||||||
|
val browsing_data = view!!.browsing_data_item!!.isChecked
|
||||||
|
val collections = view!!.collections_item!!.isChecked
|
||||||
|
val enabled = open_tabs || browsing_data || collections
|
||||||
|
|
||||||
|
view?.delete_data?.isEnabled = enabled
|
||||||
|
view?.delete_data?.alpha = if (enabled) 1f else 0.6f
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun updateTabCount() {
|
private fun updateTabCount() {
|
||||||
|
@ -135,4 +212,18 @@ class DeleteBrowsingDataFragment : Fragment(), CoroutineScope {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun deleteTabsAsync() = async(Dispatchers.IO) { requireComponents.core.sessionManager.removeSessions() }
|
||||||
|
|
||||||
|
private fun deleteBrowsingDataAsync() = async(Dispatchers.IO) {
|
||||||
|
requireComponents.core.engine.clearData(Engine.BrowsingData.all())
|
||||||
|
requireComponents.core.historyStorage.deleteEverything()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun deleteCollectionsAsync() = async(Dispatchers.IO) {
|
||||||
|
val count = requireComponents.core.tabCollectionStorage.getTabCollectionsCount()
|
||||||
|
val data = requireComponents.core.tabCollectionStorage.getCollections(count).value?.forEach {
|
||||||
|
requireComponents.core.tabCollectionStorage.removeCollection(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,7 +17,8 @@
|
||||||
app:titleMarginEnd="16dp"
|
app:titleMarginEnd="16dp"
|
||||||
app:titleTextAppearance="@style/ToolbarTitleTextStyle"
|
app:titleTextAppearance="@style/ToolbarTitleTextStyle"
|
||||||
android:background="?foundation"
|
android:background="?foundation"
|
||||||
android:elevation="8dp"/>
|
android:elevation="8dp">
|
||||||
|
</androidx.appcompat.widget.Toolbar>
|
||||||
|
|
||||||
<fragment
|
<fragment
|
||||||
android:id="@+id/container"
|
android:id="@+id/container"
|
||||||
|
|
|
@ -2,61 +2,79 @@
|
||||||
<!-- This Source Code Form is subject to the terms of the Mozilla Public
|
<!-- 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
|
- 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/. -->
|
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
|
||||||
<ScrollView
|
<androidx.constraintlayout.widget.ConstraintLayout
|
||||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
android:layout_width="fill_parent"
|
android:layout_height="match_parent"
|
||||||
android:layout_height="fill_parent">
|
android:layout_width="match_parent">
|
||||||
|
<ProgressBar
|
||||||
<LinearLayout
|
android:id="@+id/progress_bar"
|
||||||
|
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
|
||||||
|
android:indeterminate="true"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="8dp"
|
||||||
android:orientation="vertical">
|
android:translationY="-3dp"
|
||||||
<org.mozilla.fenix.settings.DeleteBrowsingDataItem
|
android:visibility="gone"
|
||||||
android:id="@+id/open_tabs_item"
|
app:layout_constraintTop_toTopOf="parent"
|
||||||
|
app:layout_constraintStart_toStartOf="parent" />
|
||||||
|
<ScrollView
|
||||||
|
android:id="@+id/delete_browsing_data_wrapper"
|
||||||
|
android:layout_width="fill_parent"
|
||||||
|
android:layout_height="fill_parent"
|
||||||
|
app:layout_constraintTop_toTopOf="parent"
|
||||||
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
app:layout_constraintBottom_toBottomOf="parent">
|
||||||
|
<LinearLayout
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:background="?android:attr/selectableItemBackground"
|
android:orientation="vertical">
|
||||||
android:clickable="true"
|
<org.mozilla.fenix.settings.DeleteBrowsingDataItem
|
||||||
android:contentDescription="@string/library_bookmarks"
|
android:id="@+id/open_tabs_item"
|
||||||
android:focusable="true"
|
android:layout_width="match_parent"
|
||||||
app:deleteBrowsingDataItemIcon="@drawable/ic_tab_circle_background"
|
android:layout_height="wrap_content"
|
||||||
app:deleteBrowsingDataItemTitle="@string/preferences_delete_browsing_data_tabs_title"
|
android:background="?android:attr/selectableItemBackground"
|
||||||
app:deleteBrowsingDataItemSubtitle="@string/preferences_delete_browsing_data_tabs_subtitle" />
|
android:clickable="true"
|
||||||
<org.mozilla.fenix.settings.DeleteBrowsingDataItem
|
android:contentDescription="@string/library_bookmarks"
|
||||||
android:id="@+id/browsing_data_item"
|
android:focusable="true"
|
||||||
android:layout_width="match_parent"
|
app:deleteBrowsingDataItemIcon="@drawable/ic_tab_circle_background"
|
||||||
android:layout_height="wrap_content"
|
app:deleteBrowsingDataItemTitle="@string/preferences_delete_browsing_data_tabs_title"
|
||||||
android:background="?android:attr/selectableItemBackground"
|
app:deleteBrowsingDataItemSubtitle="@string/preferences_delete_browsing_data_tabs_subtitle" />
|
||||||
android:clickable="true"
|
<org.mozilla.fenix.settings.DeleteBrowsingDataItem
|
||||||
android:contentDescription="@string/library_bookmarks"
|
android:id="@+id/browsing_data_item"
|
||||||
android:focusable="true"
|
android:layout_width="match_parent"
|
||||||
app:deleteBrowsingDataItemIcon="@drawable/library_icon_history_circle_background"
|
android:layout_height="wrap_content"
|
||||||
app:deleteBrowsingDataItemTitle="@string/preferences_delete_browsing_data_browsing_data_title"
|
android:background="?android:attr/selectableItemBackground"
|
||||||
app:deleteBrowsingDataItemSubtitle="@string/preferences_delete_browsing_data_browsing_data_subtitle" />
|
android:clickable="true"
|
||||||
<org.mozilla.fenix.settings.DeleteBrowsingDataItem
|
android:contentDescription="@string/library_bookmarks"
|
||||||
android:id="@+id/collections_item"
|
android:focusable="true"
|
||||||
android:layout_width="match_parent"
|
app:deleteBrowsingDataItemIcon="@drawable/library_icon_history_circle_background"
|
||||||
android:layout_height="wrap_content"
|
app:deleteBrowsingDataItemTitle="@string/preferences_delete_browsing_data_browsing_data_title"
|
||||||
android:background="?android:attr/selectableItemBackground"
|
app:deleteBrowsingDataItemSubtitle="@string/preferences_delete_browsing_data_browsing_data_subtitle" />
|
||||||
android:clickable="true"
|
<org.mozilla.fenix.settings.DeleteBrowsingDataItem
|
||||||
android:contentDescription="@string/library_bookmarks"
|
android:id="@+id/collections_item"
|
||||||
android:focusable="true"
|
android:layout_width="match_parent"
|
||||||
app:deleteBrowsingDataItemIcon="@drawable/ic_collections_circle_background"
|
android:layout_height="wrap_content"
|
||||||
app:deleteBrowsingDataItemTitle="@string/preferences_delete_browsing_data_collections_title"
|
android:background="?android:attr/selectableItemBackground"
|
||||||
app:deleteBrowsingDataItemSubtitle="@string/preferences_delete_browsing_data_collections_subtitle" />
|
android:clickable="true"
|
||||||
<Button android:id="@+id/delete_data"
|
android:contentDescription="@string/library_bookmarks"
|
||||||
android:layout_width="match_parent"
|
android:focusable="true"
|
||||||
android:layout_height="wrap_content"
|
app:deleteBrowsingDataItemIcon="@drawable/ic_collections_circle_background"
|
||||||
android:layout_gravity="start"
|
app:deleteBrowsingDataItemTitle="@string/preferences_delete_browsing_data_collections_title"
|
||||||
android:layout_margin="12dp"
|
app:deleteBrowsingDataItemSubtitle="@string/preferences_delete_browsing_data_collections_subtitle" />
|
||||||
android:backgroundTint="?attr/neutral"
|
<Button android:id="@+id/delete_data"
|
||||||
android:paddingStart="24dp"
|
android:layout_width="match_parent"
|
||||||
android:paddingEnd="24dp"
|
android:layout_height="wrap_content"
|
||||||
android:text="@string/preferences_delete_browsing_data_button"
|
android:layout_gravity="start"
|
||||||
android:textAllCaps="false"
|
android:layout_margin="12dp"
|
||||||
android:textColor="?attr/accentHighContrast"
|
android:backgroundTint="?attr/neutral"
|
||||||
android:textSize="16sp"
|
android:paddingStart="24dp"
|
||||||
android:textStyle="bold" />
|
android:paddingEnd="24dp"
|
||||||
</LinearLayout>
|
android:text="@string/preferences_delete_browsing_data_button"
|
||||||
</ScrollView>
|
android:textAllCaps="false"
|
||||||
|
android:textColor="?attr/accentHighContrast"
|
||||||
|
android:textSize="16sp"
|
||||||
|
android:textStyle="bold" />
|
||||||
|
</LinearLayout>
|
||||||
|
</ScrollView>
|
||||||
|
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
xmlns:tools="http://schemas.android.com/tools"
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
android:id="@+id/nav_graph"
|
android:id="@+id/nav_graph"
|
||||||
app:startDestination="@id/deleteBrowsingDataFragment">
|
app:startDestination="@id/homeFragment">
|
||||||
|
|
||||||
<action
|
<action
|
||||||
android:id="@+id/action_global_browser"
|
android:id="@+id/action_global_browser"
|
||||||
|
|
|
@ -581,4 +581,11 @@
|
||||||
<string name="preferences_delete_browsing_data_collections_subtitle">%d collections</string>
|
<string name="preferences_delete_browsing_data_collections_subtitle">%d collections</string>
|
||||||
<!-- Text for the button to delete browsing data -->
|
<!-- Text for the button to delete browsing data -->
|
||||||
<string name="preferences_delete_browsing_data_button">Delete browsing data</string>
|
<string name="preferences_delete_browsing_data_button">Delete browsing data</string>
|
||||||
|
|
||||||
|
<!-- Dialog message to the user asking to delete browsing data. Parameter is the name of the app (e.g. Fenix) -->
|
||||||
|
<string name="preferences_delete_browsing_data_prompt_message">Allow %s to delete your browsing data?</string>
|
||||||
|
<!-- Text for the cancel button for the data deletion dialog -->
|
||||||
|
<string name="preferences_delete_browsing_data_prompt_cancel">Don’t allow</string>
|
||||||
|
<!-- Text for the allow button for the data deletion dialog -->
|
||||||
|
<string name="preferences_delete_browsing_data_prompt_allow">Allow</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
|
Loading…
Reference in New Issue