1
0
Fork 0

For #225 - Deletes data on button press

master
Jeff Boek 2019-05-24 15:31:21 -07:00
parent 3cf91fc303
commit 51451ff002
5 changed files with 178 additions and 61 deletions

View File

@ -4,20 +4,28 @@
package org.mozilla.fenix.settings
import android.content.DialogInterface
import android.os.Bundle
import android.util.Log
import android.view.ContextThemeWrapper
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
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.coroutines.CoroutineScope
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.launch
import mozilla.components.browser.session.Session
import mozilla.components.browser.session.SessionManager
import mozilla.components.concept.engine.Engine
import org.mozilla.fenix.R
import org.mozilla.fenix.ext.requireComponents
import kotlin.coroutines.CoroutineContext
@ -66,6 +74,12 @@ class DeleteBrowsingDataFragment : Fragment(), CoroutineScope {
requireComponents.core.sessionManager.register(sessionObserver, owner = this)
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() {
@ -86,13 +100,76 @@ class DeleteBrowsingDataFragment : Fragment(), CoroutineScope {
updateDeleteButton()
}
private fun updateDeleteButton() {
view?.delete_data?.isEnabled =
view!!.open_tabs_item!!.isChecked
|| view!!.browsing_data_item!!.isChecked
|| view!!.collections_item!!.isChecked
private fun askToDelete() {
AlertDialog.Builder(
ContextThemeWrapper(
activity,
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() {
@ -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)
}
}
}

View File

@ -17,7 +17,8 @@
app:titleMarginEnd="16dp"
app:titleTextAppearance="@style/ToolbarTitleTextStyle"
android:background="?foundation"
android:elevation="8dp"/>
android:elevation="8dp">
</androidx.appcompat.widget.Toolbar>
<fragment
android:id="@+id/container"

View File

@ -2,61 +2,79 @@
<!-- 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/. -->
<ScrollView
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent">
<ProgressBar
android:id="@+id/progress_bar"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:indeterminate="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<org.mozilla.fenix.settings.DeleteBrowsingDataItem
android:id="@+id/open_tabs_item"
android:layout_height="8dp"
android:translationY="-3dp"
android:visibility="gone"
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_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:contentDescription="@string/library_bookmarks"
android:focusable="true"
app:deleteBrowsingDataItemIcon="@drawable/ic_tab_circle_background"
app:deleteBrowsingDataItemTitle="@string/preferences_delete_browsing_data_tabs_title"
app:deleteBrowsingDataItemSubtitle="@string/preferences_delete_browsing_data_tabs_subtitle" />
<org.mozilla.fenix.settings.DeleteBrowsingDataItem
android:id="@+id/browsing_data_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:contentDescription="@string/library_bookmarks"
android:focusable="true"
app:deleteBrowsingDataItemIcon="@drawable/library_icon_history_circle_background"
app:deleteBrowsingDataItemTitle="@string/preferences_delete_browsing_data_browsing_data_title"
app:deleteBrowsingDataItemSubtitle="@string/preferences_delete_browsing_data_browsing_data_subtitle" />
<org.mozilla.fenix.settings.DeleteBrowsingDataItem
android:id="@+id/collections_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:contentDescription="@string/library_bookmarks"
android:focusable="true"
app:deleteBrowsingDataItemIcon="@drawable/ic_collections_circle_background"
app:deleteBrowsingDataItemTitle="@string/preferences_delete_browsing_data_collections_title"
app:deleteBrowsingDataItemSubtitle="@string/preferences_delete_browsing_data_collections_subtitle" />
<Button android:id="@+id/delete_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:layout_margin="12dp"
android:backgroundTint="?attr/neutral"
android:paddingStart="24dp"
android:paddingEnd="24dp"
android:text="@string/preferences_delete_browsing_data_button"
android:textAllCaps="false"
android:textColor="?attr/accentHighContrast"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
</ScrollView>
android:orientation="vertical">
<org.mozilla.fenix.settings.DeleteBrowsingDataItem
android:id="@+id/open_tabs_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:contentDescription="@string/library_bookmarks"
android:focusable="true"
app:deleteBrowsingDataItemIcon="@drawable/ic_tab_circle_background"
app:deleteBrowsingDataItemTitle="@string/preferences_delete_browsing_data_tabs_title"
app:deleteBrowsingDataItemSubtitle="@string/preferences_delete_browsing_data_tabs_subtitle" />
<org.mozilla.fenix.settings.DeleteBrowsingDataItem
android:id="@+id/browsing_data_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:contentDescription="@string/library_bookmarks"
android:focusable="true"
app:deleteBrowsingDataItemIcon="@drawable/library_icon_history_circle_background"
app:deleteBrowsingDataItemTitle="@string/preferences_delete_browsing_data_browsing_data_title"
app:deleteBrowsingDataItemSubtitle="@string/preferences_delete_browsing_data_browsing_data_subtitle" />
<org.mozilla.fenix.settings.DeleteBrowsingDataItem
android:id="@+id/collections_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:contentDescription="@string/library_bookmarks"
android:focusable="true"
app:deleteBrowsingDataItemIcon="@drawable/ic_collections_circle_background"
app:deleteBrowsingDataItemTitle="@string/preferences_delete_browsing_data_collections_title"
app:deleteBrowsingDataItemSubtitle="@string/preferences_delete_browsing_data_collections_subtitle" />
<Button android:id="@+id/delete_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:layout_margin="12dp"
android:backgroundTint="?attr/neutral"
android:paddingStart="24dp"
android:paddingEnd="24dp"
android:text="@string/preferences_delete_browsing_data_button"
android:textAllCaps="false"
android:textColor="?attr/accentHighContrast"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -3,7 +3,7 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/nav_graph"
app:startDestination="@id/deleteBrowsingDataFragment">
app:startDestination="@id/homeFragment">
<action
android:id="@+id/action_global_browser"

View File

@ -581,4 +581,11 @@
<string name="preferences_delete_browsing_data_collections_subtitle">%d collections</string>
<!-- Text for the button to delete browsing data -->
<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">Dont allow</string>
<!-- Text for the allow button for the data deletion dialog -->
<string name="preferences_delete_browsing_data_prompt_allow">Allow</string>
</resources>