2019-11-21 23:23:35 +01:00
|
|
|
/* 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.trackingprotection
|
|
|
|
|
2020-04-22 17:32:26 +02:00
|
|
|
import android.app.Dialog
|
2019-11-21 23:23:35 +01:00
|
|
|
import android.content.Context
|
|
|
|
import android.graphics.Color
|
|
|
|
import android.graphics.drawable.ColorDrawable
|
2020-04-22 17:32:26 +02:00
|
|
|
import android.view.Gravity
|
2019-11-21 23:23:35 +01:00
|
|
|
import android.view.LayoutInflater
|
2020-06-25 20:24:55 +02:00
|
|
|
import android.view.MotionEvent
|
2019-11-21 23:23:35 +01:00
|
|
|
import android.view.View
|
|
|
|
import android.widget.ImageView
|
2019-12-31 21:02:33 +01:00
|
|
|
import androidx.core.view.isGone
|
|
|
|
import androidx.core.view.isVisible
|
2020-04-22 17:32:26 +02:00
|
|
|
import androidx.core.view.marginTop
|
2020-06-05 10:34:06 +02:00
|
|
|
import kotlinx.android.synthetic.main.tracking_protection_onboarding_popup.*
|
2019-11-21 23:23:35 +01:00
|
|
|
import kotlinx.android.synthetic.main.tracking_protection_onboarding_popup.view.*
|
|
|
|
import mozilla.components.browser.session.Session
|
|
|
|
import org.mozilla.fenix.R
|
2020-06-25 20:24:55 +02:00
|
|
|
import org.mozilla.fenix.components.metrics.Event
|
2020-07-21 04:06:22 +02:00
|
|
|
import org.mozilla.fenix.components.metrics.MetricController
|
2020-06-25 20:24:55 +02:00
|
|
|
import org.mozilla.fenix.ext.components
|
2019-11-21 23:23:35 +01:00
|
|
|
import org.mozilla.fenix.ext.increaseTapArea
|
|
|
|
import org.mozilla.fenix.utils.Settings
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Displays an overlay above the tracking protection button in the browser toolbar
|
|
|
|
* to onboard the user about tracking protection.
|
|
|
|
*/
|
|
|
|
class TrackingProtectionOverlay(
|
|
|
|
private val context: Context,
|
|
|
|
private val settings: Settings,
|
2020-07-21 04:06:22 +02:00
|
|
|
private val metrics: MetricController,
|
2019-11-23 07:27:33 +01:00
|
|
|
private val getToolbar: () -> View
|
2019-11-21 23:23:35 +01:00
|
|
|
) : Session.Observer {
|
|
|
|
|
|
|
|
override fun onLoadingStateChanged(session: Session, loading: Boolean) {
|
|
|
|
if (!loading && shouldShowTrackingProtectionOnboarding(session)) {
|
|
|
|
showTrackingProtectionOnboarding()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun shouldShowTrackingProtectionOnboarding(session: Session) =
|
|
|
|
settings.shouldShowTrackingProtectionOnboarding &&
|
2020-06-25 20:24:55 +02:00
|
|
|
session.trackerBlockingEnabled &&
|
|
|
|
session.trackersBlocked.isNotEmpty()
|
2019-11-21 23:23:35 +01:00
|
|
|
|
|
|
|
@Suppress("MagicNumber", "InflateParams")
|
|
|
|
private fun showTrackingProtectionOnboarding() {
|
2020-05-29 23:49:44 +02:00
|
|
|
if (!getToolbar().hasWindowFocus()) return
|
2020-06-25 20:24:55 +02:00
|
|
|
|
|
|
|
val trackingOnboardingDialog = object : Dialog(context) {
|
|
|
|
override fun onTouchEvent(event: MotionEvent): Boolean {
|
|
|
|
|
|
|
|
if (event.action == MotionEvent.ACTION_DOWN) {
|
|
|
|
context.components.analytics.metrics.track(Event.ContextualHintETPOutsideTap)
|
|
|
|
}
|
|
|
|
return super.onTouchEvent(event)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 23:23:35 +01:00
|
|
|
val layout = LayoutInflater.from(context)
|
|
|
|
.inflate(R.layout.tracking_protection_onboarding_popup, null)
|
2020-07-21 04:06:22 +02:00
|
|
|
val isBottomToolbar = settings.shouldUseBottomToolbar
|
2020-04-22 17:32:26 +02:00
|
|
|
|
2019-12-31 21:02:33 +01:00
|
|
|
layout.drop_down_triangle.isGone = isBottomToolbar
|
|
|
|
layout.pop_up_triangle.isVisible = isBottomToolbar
|
2020-04-22 17:32:26 +02:00
|
|
|
|
2019-11-21 23:23:35 +01:00
|
|
|
layout.onboarding_message.text =
|
2019-12-31 21:02:33 +01:00
|
|
|
context.getString(
|
2020-06-05 10:34:06 +02:00
|
|
|
R.string.etp_onboarding_cfr_message,
|
2019-12-31 21:02:33 +01:00
|
|
|
context.getString(R.string.app_name)
|
|
|
|
)
|
2019-11-21 23:23:35 +01:00
|
|
|
|
|
|
|
val closeButton = layout.findViewById<ImageView>(R.id.close_onboarding)
|
|
|
|
closeButton.increaseTapArea(BUTTON_INCREASE_DPS)
|
|
|
|
closeButton.setOnClickListener {
|
2020-07-21 04:06:22 +02:00
|
|
|
metrics.track(Event.ContextualHintETPDismissed)
|
2020-04-22 17:32:26 +02:00
|
|
|
trackingOnboardingDialog.dismiss()
|
2019-11-21 23:23:35 +01:00
|
|
|
}
|
|
|
|
|
2020-04-22 17:32:26 +02:00
|
|
|
val res = context.resources
|
2020-07-21 01:00:57 +02:00
|
|
|
val triangleWidthPx = res.getDimension(R.dimen.cfr_triangle_height)
|
|
|
|
val triangleMarginStartPx = res.getDimension(R.dimen.cfr_triangle_margin_edge)
|
2019-11-21 23:23:35 +01:00
|
|
|
|
2019-11-23 07:27:33 +01:00
|
|
|
val toolbar = getToolbar()
|
|
|
|
val trackingProtectionIcon: View =
|
|
|
|
toolbar.findViewById(R.id.mozac_browser_toolbar_tracking_protection_indicator)
|
|
|
|
|
2020-04-22 17:32:26 +02:00
|
|
|
val xOffset = triangleMarginStartPx + triangleWidthPx / 2
|
2019-11-21 23:23:35 +01:00
|
|
|
|
2020-04-22 17:32:26 +02:00
|
|
|
val gravity = if (isBottomToolbar) {
|
|
|
|
Gravity.START or Gravity.BOTTOM
|
2019-12-31 21:02:33 +01:00
|
|
|
} else {
|
2020-04-22 17:32:26 +02:00
|
|
|
Gravity.START or Gravity.TOP
|
|
|
|
}
|
|
|
|
|
|
|
|
trackingOnboardingDialog.apply {
|
|
|
|
setContentView(layout)
|
|
|
|
setCancelable(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
trackingOnboardingDialog.window?.let {
|
|
|
|
it.setGravity(gravity)
|
|
|
|
val attr = it.attributes
|
|
|
|
attr.x =
|
|
|
|
(trackingProtectionIcon.x + trackingProtectionIcon.width / 2 - xOffset).toInt()
|
|
|
|
attr.y =
|
|
|
|
(trackingProtectionIcon.y + trackingProtectionIcon.height - trackingProtectionIcon.marginTop).toInt()
|
|
|
|
it.attributes = attr
|
|
|
|
it.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
|
2019-12-31 21:02:33 +01:00
|
|
|
}
|
2019-11-21 23:23:35 +01:00
|
|
|
|
2020-06-05 10:34:06 +02:00
|
|
|
val etpShield =
|
|
|
|
getToolbar().findViewById<View>(R.id.mozac_browser_toolbar_tracking_protection_indicator)
|
|
|
|
trackingOnboardingDialog.message.setOnClickListener {
|
2020-07-21 04:06:22 +02:00
|
|
|
metrics.track(Event.ContextualHintETPInsideTap)
|
2020-06-05 10:34:06 +02:00
|
|
|
trackingOnboardingDialog.dismiss()
|
|
|
|
etpShield.performClick()
|
|
|
|
}
|
|
|
|
|
2020-07-21 04:06:22 +02:00
|
|
|
metrics.track(Event.ContextualHintETPDisplayed)
|
2020-04-22 17:32:26 +02:00
|
|
|
trackingOnboardingDialog.show()
|
2019-11-21 23:23:35 +01:00
|
|
|
settings.incrementTrackingProtectionOnboardingCount()
|
|
|
|
}
|
|
|
|
|
|
|
|
private companion object {
|
|
|
|
private const val BUTTON_INCREASE_DPS = 12
|
|
|
|
}
|
|
|
|
}
|