Commit 2aca580e authored by Denys M's avatar Denys M Committed by Sebastian Kaspari
Browse files

Fixes #790. Add `isScreenReaderEnabled` ktx-extension.

parent 3d396e21
......@@ -15,6 +15,7 @@ import android.content.Intent.FLAG_ACTIVITY_NEW_TASK
import android.content.pm.PackageManager.PERMISSION_GRANTED
import android.hardware.camera2.CameraManager
import android.os.Process
import android.view.accessibility.AccessibilityManager
import androidx.annotation.VisibleForTesting
import androidx.core.content.ContextCompat.checkSelfPermission
import androidx.core.content.getSystemService
......@@ -96,6 +97,14 @@ fun Context.share(text: String, subject: String = getString(R.string.mozac_suppo
}
}
/**
* Check if TalkBack service is enabled.
*
* (via https://stackoverflow.com/a/12362545/512580)
*/
inline val Context.isScreenReaderEnabled: Boolean
get() = (getSystemService(Context.ACCESSIBILITY_SERVICE) as AccessibilityManager).isTouchExplorationEnabled
@VisibleForTesting
internal var isMainProcess: Boolean? = null
......
/* 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.ktx.android.content
import android.content.Context
import android.view.accessibility.AccessibilityManager
import androidx.test.ext.junit.runners.AndroidJUnit4
import mozilla.components.support.test.robolectric.testContext
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.Shadows.shadowOf
import org.robolectric.shadows.ShadowAccessibilityManager
@RunWith(AndroidJUnit4::class)
class ContextKtTest {
lateinit var accessibilityManager: ShadowAccessibilityManager
@Before
fun setUp() {
accessibilityManager = shadowOf(testContext
.getSystemService(Context.ACCESSIBILITY_SERVICE) as AccessibilityManager)
}
@Test
fun `screen reader enabled`() {
// Given
accessibilityManager.setTouchExplorationEnabled(true)
// When
val isEnabled = testContext.isScreenReaderEnabled
// Then
assertTrue(isEnabled)
}
@Test
fun `screen reader disabled`() {
// Given
accessibilityManager.setTouchExplorationEnabled(false)
// When
val isEnabled = testContext.isScreenReaderEnabled
// Then
assertFalse(isEnabled)
}
}
......@@ -15,6 +15,9 @@ permalink: /changelog/
* **lib-state**
* A new component for maintaining application, screen or component state via a redux-style `Store`. This component provides the architectural foundation for the `browser-state` component (in development).
* **support-ktx**
* Added `Context.isScreenReaderEnabled` extension to check if TalkBack service is enabled.
# 1.0.0
* [Commits](https://github.com/mozilla-mobile/android-components/compare/v0.56.0...v1.0.0)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment