[fenix] For https://github.com/mozilla-mobile/fenix/issues/15079: handle QR permissions when changed in Android settings (https://github.com/mozilla-mobile/fenix/pull/15097)
* Define intent data for activity * Search dialog shows permissions for allow and deny camera * Check camera permissions for fxa pairing * Check camera permissions for old search * Tests for pairing sync interactor and controller. * Cleanup * Use bool pref for setting. Use interfaces and default implementations for the sync interactor and controller. * Lintpull/600/head
parent
c21bfef9e8
commit
a6f71a3581
@ -0,0 +1,74 @@
|
||||
/* 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.account
|
||||
|
||||
import android.content.DialogInterface
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.provider.Settings
|
||||
import android.text.SpannableString
|
||||
import androidx.annotation.VisibleForTesting
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import org.mozilla.fenix.HomeActivity
|
||||
import org.mozilla.fenix.R
|
||||
import org.mozilla.fenix.settings.SupportUtils
|
||||
|
||||
interface SyncController {
|
||||
fun handleCameraPermissionsNeeded()
|
||||
}
|
||||
|
||||
/**
|
||||
* Controller for handling [DefaultSyncInteractor] requests.
|
||||
*/
|
||||
class DefaultSyncController(
|
||||
private val activity: HomeActivity
|
||||
) : SyncController {
|
||||
|
||||
/**
|
||||
* Creates and shows an [AlertDialog] when camera permissions are needed.
|
||||
*
|
||||
* In versions above M, [AlertDialog.BUTTON_POSITIVE] takes the user to the app settings. This
|
||||
* intent only exists in M and above. Below M, [AlertDialog.BUTTON_POSITIVE] routes to a SUMO
|
||||
* help page to find the app settings.
|
||||
*
|
||||
* [AlertDialog.BUTTON_NEGATIVE] dismisses the dialog.
|
||||
*/
|
||||
override fun handleCameraPermissionsNeeded() {
|
||||
val dialog = buildDialog()
|
||||
dialog.show()
|
||||
}
|
||||
|
||||
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
|
||||
fun buildDialog(): AlertDialog.Builder {
|
||||
return AlertDialog.Builder(activity).apply {
|
||||
val spannableText = SpannableString(
|
||||
activity.resources.getString(R.string.camera_permissions_needed_message)
|
||||
)
|
||||
setMessage(spannableText)
|
||||
setNegativeButton(R.string.camera_permissions_needed_negative_button_text) { dialog: DialogInterface, _ ->
|
||||
dialog.cancel()
|
||||
}
|
||||
setPositiveButton(R.string.camera_permissions_needed_positive_button_text) { dialog: DialogInterface, _ ->
|
||||
val intent: Intent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
|
||||
} else {
|
||||
SupportUtils.createCustomTabIntent(
|
||||
activity,
|
||||
SupportUtils.getSumoURLForTopic(
|
||||
activity,
|
||||
SupportUtils.SumoTopic.QR_CAMERA_ACCESS
|
||||
)
|
||||
)
|
||||
}
|
||||
val uri = Uri.fromParts("package", activity.packageName, null)
|
||||
intent.data = uri
|
||||
dialog.cancel()
|
||||
activity.startActivity(intent)
|
||||
}
|
||||
create()
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
/* 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.account
|
||||
|
||||
interface SyncInteractor {
|
||||
fun onCameraPermissionsNeeded()
|
||||
}
|
||||
|
||||
/**
|
||||
* Interactor for [TurnOnSyncFragment].
|
||||
*
|
||||
* @param syncController Handles the interactions
|
||||
*/
|
||||
class DefaultSyncInteractor(private val syncController: DefaultSyncController) : SyncInteractor {
|
||||
override fun onCameraPermissionsNeeded() {
|
||||
syncController.handleCameraPermissionsNeeded()
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
/* 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.account
|
||||
|
||||
import io.mockk.MockKAnnotations
|
||||
import io.mockk.every
|
||||
import io.mockk.impl.annotations.MockK
|
||||
import io.mockk.mockk
|
||||
import io.mockk.spyk
|
||||
import io.mockk.verify
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.mozilla.fenix.HomeActivity
|
||||
import org.mozilla.fenix.search.AlertDialogBuilder
|
||||
|
||||
class DefaultSyncControllerTest {
|
||||
|
||||
private lateinit var syncController: DefaultSyncController
|
||||
@MockK(relaxed = true) private lateinit var activity: HomeActivity
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
MockKAnnotations.init(this)
|
||||
syncController = DefaultSyncController(activity)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `show camera permissions needed dialog`() {
|
||||
val dialogBuilder: AlertDialogBuilder = mockk(relaxed = true)
|
||||
|
||||
val spyController = spyk(syncController)
|
||||
every { spyController.buildDialog() } returns dialogBuilder
|
||||
|
||||
spyController.handleCameraPermissionsNeeded()
|
||||
|
||||
verify { dialogBuilder.show() }
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
/* 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.account
|
||||
|
||||
import io.mockk.mockk
|
||||
import io.mockk.verify
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
|
||||
class DefaultSyncInteractorTest {
|
||||
|
||||
private lateinit var syncInteractor: DefaultSyncInteractor
|
||||
private lateinit var syncController: DefaultSyncController
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
syncController = mockk(relaxed = true)
|
||||
syncInteractor = DefaultSyncInteractor(syncController)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun onCameraPermissionsNeeded() {
|
||||
syncInteractor.onCameraPermissionsNeeded()
|
||||
|
||||
verify {
|
||||
syncController.handleCameraPermissionsNeeded()
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue