This repository has been archived by the owner on Feb 20, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
For #3700 - Add Setting to Delete Data on "Quit" menu action
- Loading branch information
Showing
20 changed files
with
574 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
app/src/main/java/org/mozilla/fenix/settings/DeleteBrowsingDataOnQuitFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* 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.settings | ||
|
||
import android.os.Bundle | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.preference.CheckBoxPreference | ||
import androidx.preference.Preference | ||
import androidx.preference.PreferenceFragmentCompat | ||
import androidx.preference.SwitchPreference | ||
import org.mozilla.fenix.R | ||
import org.mozilla.fenix.ext.getPreferenceKey | ||
import org.mozilla.fenix.utils.Settings | ||
|
||
class DeleteBrowsingDataOnQuitFragment : PreferenceFragmentCompat() { | ||
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { | ||
setPreferencesFromResource(R.xml.delete_browsing_data_quit_preferences, rootKey) | ||
} | ||
|
||
@Suppress("ComplexMethod") | ||
override fun onResume() { | ||
super.onResume() | ||
activity?.title = getString(R.string.preferences_delete_browsing_data_on_quit) | ||
(activity as AppCompatActivity).supportActionBar?.show() | ||
|
||
class CheckboxUpdater : SharedPreferenceUpdater() { | ||
override fun onPreferenceChange(preference: Preference, newValue: Any?): Boolean { | ||
super.onPreferenceChange(preference, newValue) | ||
if (!Settings.getInstance(preference.context).shouldDeleteAnyDataOnQuit()) { | ||
findPreference<SwitchPreference>( | ||
getPreferenceKey(R.string.pref_key_delete_browsing_data_on_quit) | ||
)?.apply { | ||
isChecked = false | ||
} | ||
Settings.getInstance(preference.context).preferences.edit().putBoolean( | ||
getString(R.string.pref_key_delete_browsing_data_on_quit), | ||
false | ||
).apply() | ||
} | ||
return true | ||
} | ||
} | ||
|
||
class SwitchUpdater : SharedPreferenceUpdater() { | ||
override fun onPreferenceChange(preference: Preference, newValue: Any?): Boolean { | ||
setAllCheckboxes(newValue as Boolean) | ||
return super.onPreferenceChange(preference, newValue) | ||
} | ||
} | ||
|
||
findPreference<CheckBoxPreference>(getPreferenceKey(R.string.pref_key_delete_open_tabs_on_quit))?.apply { | ||
onPreferenceChangeListener = CheckboxUpdater() | ||
} | ||
findPreference<CheckBoxPreference>(getPreferenceKey(R.string.pref_key_delete_browsing_history_on_quit))?.apply { | ||
onPreferenceChangeListener = CheckboxUpdater() | ||
} | ||
findPreference<CheckBoxPreference>(getPreferenceKey(R.string.pref_key_delete_caches_on_quit))?.apply { | ||
onPreferenceChangeListener = CheckboxUpdater() | ||
} | ||
findPreference<CheckBoxPreference>(getPreferenceKey(R.string.pref_key_delete_permissions_on_quit))?.apply { | ||
onPreferenceChangeListener = CheckboxUpdater() | ||
} | ||
findPreference<CheckBoxPreference>(getPreferenceKey(R.string.pref_key_delete_cookies_on_quit))?.apply { | ||
onPreferenceChangeListener = CheckboxUpdater() | ||
} | ||
|
||
// Delete Browsing Data on Quit Switch | ||
val deleteOnQuitKey = getPreferenceKey(R.string.pref_key_delete_browsing_data_on_quit) | ||
findPreference<SwitchPreference>(deleteOnQuitKey)?.apply { | ||
onPreferenceChangeListener = SwitchUpdater() | ||
isChecked = Settings.getInstance(context!!).shouldDeleteBrowsingDataOnQuit | ||
} | ||
} | ||
|
||
private fun setAllCheckboxes(newValue: Boolean) { | ||
val openTabs = | ||
findPreference<CheckBoxPreference>(getPreferenceKey(R.string.pref_key_delete_open_tabs_on_quit)) | ||
val history = | ||
findPreference<CheckBoxPreference>(getPreferenceKey(R.string.pref_key_delete_browsing_history_on_quit)) | ||
val cache = | ||
findPreference<CheckBoxPreference>(getPreferenceKey(R.string.pref_key_delete_caches_on_quit)) | ||
val permissions = | ||
findPreference<CheckBoxPreference>(getPreferenceKey(R.string.pref_key_delete_permissions_on_quit)) | ||
val cookies = | ||
findPreference<CheckBoxPreference>(getPreferenceKey(R.string.pref_key_delete_cookies_on_quit)) | ||
|
||
openTabs?.isChecked = newValue | ||
history?.isChecked = newValue | ||
cache?.isChecked = newValue | ||
permissions?.isChecked = newValue | ||
cookies?.isChecked = newValue | ||
|
||
Settings.getInstance(context!!).preferences.edit().putBoolean(openTabs?.key, newValue) | ||
.apply() | ||
Settings.getInstance(context!!).preferences.edit().putBoolean(history?.key, newValue) | ||
.apply() | ||
Settings.getInstance(context!!).preferences.edit().putBoolean(cache?.key, newValue).apply() | ||
Settings.getInstance(context!!).preferences.edit().putBoolean(permissions?.key, newValue) | ||
.apply() | ||
Settings.getInstance(context!!).preferences.edit().putBoolean(cookies?.key, newValue) | ||
.apply() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
app/src/main/java/org/mozilla/fenix/utils/DeleteAndQuit.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* 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.utils | ||
|
||
import android.content.Context | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.runBlocking | ||
import kotlinx.coroutines.withContext | ||
import mozilla.components.concept.engine.Engine | ||
import org.mozilla.fenix.ext.asActivity | ||
import org.mozilla.fenix.ext.components | ||
|
||
/** | ||
* Deletes selected browsing data and finishes the activity | ||
*/ | ||
fun Context.deleteAndQuit(coroutineScope: CoroutineScope) { | ||
coroutineScope.launch { | ||
runBlocking { | ||
if (Settings.getInstance(this@deleteAndQuit).deleteCacheOnQuit) { | ||
withContext(coroutineContext) { | ||
this@deleteAndQuit.components.core.engine.clearData(Engine.BrowsingData.allCaches()) | ||
} | ||
} | ||
if (Settings.getInstance(this@deleteAndQuit).deleteTabsOnQuit) { | ||
withContext(coroutineContext) { | ||
this@deleteAndQuit.components.useCases.tabsUseCases.removeAllTabs.invoke() | ||
} | ||
} | ||
if (Settings.getInstance(this@deleteAndQuit).deletePermissionsOnQuit) { | ||
launch(Dispatchers.IO) { | ||
this@deleteAndQuit.components.core.permissionStorage.deleteAllSitePermissions() | ||
} | ||
} | ||
if (Settings.getInstance(this@deleteAndQuit).deleteCookiesOnQuit) { | ||
withContext(coroutineContext) { | ||
this@deleteAndQuit.components.core.engine.clearData( | ||
Engine.BrowsingData.select( | ||
Engine.BrowsingData.COOKIES | ||
) | ||
) | ||
} | ||
} | ||
if (Settings.getInstance(this@deleteAndQuit).deleteHistoryOnQuit) { | ||
withContext(coroutineContext) { | ||
this@deleteAndQuit.components.core.historyStorage.deleteEverything() | ||
} | ||
} | ||
} | ||
this@deleteAndQuit.asActivity()?.finish() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.