2019-02-15 18:31:03 +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-15 18:31:03 +01:00
|
|
|
|
2019-08-21 17:33:59 +02:00
|
|
|
package org.mozilla.fenix.browser.browsingmode
|
2019-02-15 18:31:03 +01:00
|
|
|
|
2020-01-09 20:28:21 +01:00
|
|
|
import org.mozilla.fenix.utils.Settings
|
|
|
|
|
2019-08-07 22:02:08 +02:00
|
|
|
/**
|
|
|
|
* Enum that represents whether or not private browsing is active.
|
|
|
|
*/
|
|
|
|
enum class BrowsingMode {
|
|
|
|
Normal, Private;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the [BrowsingMode] is [Private]
|
|
|
|
*/
|
|
|
|
val isPrivate get() = this == Private
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a boolean into a [BrowsingMode].
|
|
|
|
* True corresponds to [Private] and false corresponds to [Normal].
|
|
|
|
*/
|
|
|
|
fun fromBoolean(isPrivate: Boolean) = if (isPrivate) Private else Normal
|
2019-02-15 18:31:03 +01:00
|
|
|
}
|
2019-05-31 00:49:58 +02:00
|
|
|
}
|
|
|
|
|
2019-08-07 22:02:08 +02:00
|
|
|
interface BrowsingModeManager {
|
|
|
|
var mode: BrowsingMode
|
2019-02-15 18:31:03 +01:00
|
|
|
}
|
2019-05-31 00:49:58 +02:00
|
|
|
|
2020-02-10 22:57:29 +01:00
|
|
|
interface BrowsingModeListener {
|
|
|
|
fun onBrowsingModeChange(newMode: BrowsingMode)
|
|
|
|
}
|
|
|
|
|
2019-10-07 19:51:09 +02:00
|
|
|
/**
|
|
|
|
* Wraps a [BrowsingMode] and executes a callback whenever [mode] is updated.
|
|
|
|
*/
|
2019-05-31 01:13:06 +02:00
|
|
|
class DefaultBrowsingModeManager(
|
2020-02-10 22:57:29 +01:00
|
|
|
private var _mode: BrowsingMode = BrowsingMode.Normal
|
2019-05-31 01:13:06 +02:00
|
|
|
) : BrowsingModeManager {
|
2019-10-07 19:51:09 +02:00
|
|
|
|
2020-02-10 22:57:29 +01:00
|
|
|
private var _browsingModeListener: BrowsingModeListener? = null
|
|
|
|
|
|
|
|
fun registerBrowsingModeListener(browsingModeListener: BrowsingModeListener) {
|
|
|
|
_browsingModeListener = browsingModeListener
|
|
|
|
}
|
|
|
|
|
|
|
|
fun unregisterBrowsingModeListener() {
|
|
|
|
_browsingModeListener = null
|
|
|
|
}
|
|
|
|
|
2019-08-07 22:02:08 +02:00
|
|
|
override var mode: BrowsingMode
|
2019-10-07 19:51:09 +02:00
|
|
|
get() = _mode
|
2019-05-31 01:13:06 +02:00
|
|
|
set(value) {
|
2019-10-07 19:51:09 +02:00
|
|
|
_mode = value
|
2020-02-10 22:57:29 +01:00
|
|
|
_browsingModeListener?.onBrowsingModeChange(value)
|
2020-01-09 20:28:21 +01:00
|
|
|
Settings.instance?.lastKnownMode = value
|
2019-05-31 01:13:06 +02:00
|
|
|
}
|
2019-05-31 00:49:58 +02:00
|
|
|
}
|