forked from mozilla-mobile/android-components
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Close issue mozilla-mobile#9823 Make users aware that download was no…
…t performed because of a denied permission
- Loading branch information
Showing
18 changed files
with
176 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
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
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
74 changes: 74 additions & 0 deletions
74
...se/src/main/java/mozilla/components/support/base/dialog/DeniedPermissionDialogFragment.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,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 mozilla.components.support.base.dialog | ||
|
||
import android.app.Dialog | ||
import android.content.Intent | ||
import android.net.Uri | ||
import android.os.Bundle | ||
import android.provider.Settings | ||
import androidx.annotation.StringRes | ||
import androidx.annotation.VisibleForTesting | ||
import androidx.appcompat.app.AlertDialog | ||
import androidx.fragment.app.DialogFragment | ||
import mozilla.components.support.base.R | ||
|
||
internal const val KEY_MESSAGE = "KEY_MESSAGE" | ||
|
||
/** | ||
* An dialog to display when Android permission ise denied and | ||
* you want give users a way activate it on the app settings. | ||
* The dialog will have two buttons one "Go to settings" and another for "Dismissing". | ||
*/ | ||
class DeniedPermissionDialogFragment : DialogFragment() { | ||
internal val message: Int by lazy { safeArguments.getInt(KEY_MESSAGE) } | ||
val safeArguments get() = requireNotNull(arguments) | ||
|
||
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { | ||
val builder = AlertDialog.Builder(requireContext()) | ||
.setMessage(message) | ||
.setCancelable(true) | ||
.setNegativeButton(R.string.mozac_support_base_permissions_needed_negative_button) { _, _ -> | ||
dismiss() | ||
} | ||
.setPositiveButton(R.string.mozac_support_base_permissions_needed_positive_button) { _, _ -> | ||
openSettingsPage() | ||
} | ||
return builder.create() | ||
} | ||
|
||
@VisibleForTesting | ||
internal fun openSettingsPage() { | ||
dismiss() | ||
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS) | ||
val uri = Uri.fromParts("package", requireContext().packageName, null) | ||
intent.data = uri | ||
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK | ||
requireContext().startActivity(intent) | ||
} | ||
|
||
companion object { | ||
/** | ||
* A builder method for creating a [DeniedPermissionDialogFragment] | ||
* @param message the message of the dialog. | ||
**/ | ||
fun newInstance( | ||
@StringRes message: Int | ||
): DeniedPermissionDialogFragment { | ||
|
||
val fragment = DeniedPermissionDialogFragment() | ||
val arguments = fragment.arguments ?: Bundle() | ||
|
||
with(arguments) { | ||
putInt(KEY_MESSAGE, message) | ||
} | ||
|
||
fragment.arguments = arguments | ||
return fragment | ||
} | ||
|
||
const val FRAGMENT_TAG = "DENIED_DOWNLOAD_PERMISSION_PROMPT_DIALOG" | ||
} | ||
} |
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,10 @@ | ||
<?xml version="1.0" encoding="utf-8" standalone="yes"?> | ||
<!-- 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/. --> | ||
<resources> | ||
<!-- Text for the positive action button, that will take the user to the settings page --> | ||
<string name="mozac_support_base_permissions_needed_positive_button">Go to settings</string> | ||
<!-- Text for the negative action button to dismiss the dialog. --> | ||
<string name="mozac_support_base_permissions_needed_negative_button">Dismiss</string> | ||
</resources> |
63 changes: 63 additions & 0 deletions
63
...rc/test/java/mozilla/components/support/base/dialog/DeniedPermissionDialogFragmentTest.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,63 @@ | ||
/* 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 mozilla.components.support.base.dialog | ||
|
||
import android.content.DialogInterface.BUTTON_POSITIVE | ||
import android.widget.TextView | ||
import androidx.appcompat.app.AlertDialog | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import mozilla.components.support.base.R | ||
import mozilla.components.support.test.ext.appCompatContext | ||
import mozilla.components.support.test.robolectric.testContext | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.mockito.Mockito.doNothing | ||
import org.mockito.Mockito.doReturn | ||
import org.mockito.Mockito.spy | ||
import org.mockito.Mockito.verify | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class DeniedPermissionDialogFragmentTest { | ||
|
||
@Test | ||
fun `WHEN showing the dialog THEN it has the provided message`() { | ||
val messageId = R.string.mozac_support_base_permissions_needed_negative_button | ||
val fragment = spy( | ||
DeniedPermissionDialogFragment.newInstance(messageId) | ||
) | ||
|
||
doReturn(appCompatContext).`when`(fragment).requireContext() | ||
|
||
val dialog = fragment.onCreateDialog(null) | ||
|
||
dialog.show() | ||
|
||
val messageTextView = dialog.findViewById<TextView>(android.R.id.message) | ||
|
||
assertEquals(fragment.message, messageId) | ||
assertEquals(messageTextView.text.toString(), testContext.getString(messageId)) | ||
} | ||
|
||
@Test | ||
fun `WHEN clicking the positive button THEN the settings page will show`() { | ||
val messageId = R.string.mozac_support_base_permissions_needed_negative_button | ||
|
||
val fragment = spy( | ||
DeniedPermissionDialogFragment.newInstance(messageId) | ||
) | ||
|
||
doNothing().`when`(fragment).dismiss() | ||
doReturn(appCompatContext).`when`(fragment).requireContext() | ||
|
||
val dialog = fragment.onCreateDialog(null) | ||
dialog.show() | ||
|
||
val positiveButton = (dialog as AlertDialog).getButton(BUTTON_POSITIVE) | ||
positiveButton.performClick() | ||
|
||
verify(fragment).openSettingsPage() | ||
} | ||
} |
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