Skip to content

Commit 9f59aee

Browse files
committed
Replace function call with property access
1 parent cfe63ac commit 9f59aee

File tree

20 files changed

+91
-93
lines changed

20 files changed

+91
-93
lines changed

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/accessibilityinfo/AccessibilityInfoModule.kt

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ import com.facebook.react.module.annotations.ReactModule
2828
*/
2929
@ReactModule(name = NativeAccessibilityInfoSpec.NAME)
3030
internal class AccessibilityInfoModule(context: ReactApplicationContext) :
31-
NativeAccessibilityInfoSpec(context), LifecycleEventListener {
31+
NativeAccessibilityInfoSpec(context), LifecycleEventListener {
3232
private inner class ReactTouchExplorationStateChangeListener :
33-
AccessibilityManager.TouchExplorationStateChangeListener {
33+
AccessibilityManager.TouchExplorationStateChangeListener {
3434
override fun onTouchExplorationStateChanged(enabled: Boolean) {
3535
updateAndSendTouchExplorationChangeEvent(enabled)
3636
}
@@ -54,11 +54,12 @@ internal class AccessibilityInfoModule(context: ReactApplicationContext) :
5454
}
5555

5656
override fun onChange(selfChange: Boolean, uri: Uri?) {
57-
if (getReactApplicationContext().hasActiveReactInstance()) {
57+
if (reactApplicationContext.hasActiveReactInstance()) {
5858
updateAndSendReduceMotionChangeEvent()
5959
}
6060
}
61-
}
61+
}
62+
6263
// Listener that is notified when the ACCESSIBILITY_HIGH_TEXT_CONTRAST_ENABLED changes.
6364
private val highTextContrastObserver: ContentObserver =
6465
object : ContentObserver(UiThreadUtil.getUiThreadHandler()) {
@@ -67,7 +68,7 @@ internal class AccessibilityInfoModule(context: ReactApplicationContext) :
6768
}
6869

6970
override fun onChange(selfChange: Boolean, uri: Uri?) {
70-
if (getReactApplicationContext().hasActiveReactInstance()) {
71+
if (reactApplicationContext.hasActiveReactInstance()) {
7172
updateAndSendHighTextContrastChangeEvent()
7273
}
7374
}
@@ -90,7 +91,7 @@ internal class AccessibilityInfoModule(context: ReactApplicationContext) :
9091
val appContext = context.applicationContext
9192
accessibilityManager =
9293
appContext.getSystemService(Context.ACCESSIBILITY_SERVICE) as AccessibilityManager
93-
contentResolver = getReactApplicationContext().getContentResolver()
94+
contentResolver = reactApplicationContext.getContentResolver()
9495
touchExplorationEnabled = accessibilityManager.isTouchExplorationEnabled
9596
accessibilityServiceEnabled = accessibilityManager.isEnabled
9697
reduceMotionEnabled = isReduceMotionEnabledValue
@@ -181,74 +182,71 @@ internal class AccessibilityInfoModule(context: ReactApplicationContext) :
181182
val isReduceMotionEnabled = isReduceMotionEnabledValue
182183
if (reduceMotionEnabled != isReduceMotionEnabled) {
183184
reduceMotionEnabled = isReduceMotionEnabled
184-
val reactApplicationContext = getReactApplicationContextIfActiveOrWarn()
185-
if (reactApplicationContext != null) {
186-
reactApplicationContext.emitDeviceEvent(REDUCE_MOTION_EVENT_NAME, reduceMotionEnabled)
187-
}
185+
reactApplicationContextIfActiveOrWarn?.emitDeviceEvent(
186+
REDUCE_MOTION_EVENT_NAME,
187+
reduceMotionEnabled
188+
)
188189
}
189190
}
190191

191192
private fun updateAndSendInvertColorsChangeEvent() {
192193
val isInvertColorsEnabled = isInvertColorsEnabledValue
193194
if (invertColorsEnabled != isInvertColorsEnabled) {
194195
invertColorsEnabled = isInvertColorsEnabled
195-
val reactApplicationContext = getReactApplicationContextIfActiveOrWarn()
196-
if (reactApplicationContext != null) {
197-
reactApplicationContext.emitDeviceEvent(INVERT_COLOR_EVENT_NAME, invertColorsEnabled)
198-
}
196+
reactApplicationContextIfActiveOrWarn?.emitDeviceEvent(
197+
INVERT_COLOR_EVENT_NAME,
198+
invertColorsEnabled
199+
)
199200
}
200201
}
201202

202203
private fun updateAndSendHighTextContrastChangeEvent() {
203204
val isHighTextContrastEnabled = isHighTextContrastEnabledValue
204205
if (highTextContrastEnabled != isHighTextContrastEnabled) {
205206
highTextContrastEnabled = isHighTextContrastEnabled
206-
val reactApplicationContext = getReactApplicationContextIfActiveOrWarn()
207-
if (reactApplicationContext != null) {
208-
reactApplicationContext.emitDeviceEvent(
209-
HIGH_TEXT_CONTRAST_EVENT_NAME,
210-
highTextContrastEnabled,
211-
)
212-
}
207+
reactApplicationContextIfActiveOrWarn?.emitDeviceEvent(
208+
HIGH_TEXT_CONTRAST_EVENT_NAME,
209+
highTextContrastEnabled,
210+
)
213211
}
214212
}
215213

216214
private fun updateAndSendTouchExplorationChangeEvent(enabled: Boolean) {
217215
if (touchExplorationEnabled != enabled) {
218216
touchExplorationEnabled = enabled
219-
val reactApplicationContext = getReactApplicationContextIfActiveOrWarn()
220-
if (reactApplicationContext != null) {
221-
getReactApplicationContext()
222-
.emitDeviceEvent(TOUCH_EXPLORATION_EVENT_NAME, touchExplorationEnabled)
223-
}
217+
reactApplicationContextIfActiveOrWarn?.emitDeviceEvent(
218+
TOUCH_EXPLORATION_EVENT_NAME,
219+
touchExplorationEnabled
220+
)
224221
}
225222
}
226223

227224
private fun updateAndSendAccessibilityServiceChangeEvent(enabled: Boolean) {
228225
if (accessibilityServiceEnabled != enabled) {
229226
accessibilityServiceEnabled = enabled
230-
val reactApplicationContext = getReactApplicationContextIfActiveOrWarn()
231-
if (reactApplicationContext != null) {
232-
getReactApplicationContext()
233-
.emitDeviceEvent(ACCESSIBILITY_SERVICE_EVENT_NAME, accessibilityServiceEnabled)
234-
}
227+
reactApplicationContextIfActiveOrWarn?.emitDeviceEvent(
228+
ACCESSIBILITY_SERVICE_EVENT_NAME,
229+
accessibilityServiceEnabled
230+
)
235231
}
236232
}
237233

238234
private fun updateAndSendGrayscaleModeChangeEvent() {
239235
val isGrayscaleModeEnabled = isGrayscaleEnabledValue
240236
if (grayscaleModeEnabled != isGrayscaleModeEnabled) {
241237
grayscaleModeEnabled = isGrayscaleModeEnabled
242-
val reactApplicationContext = getReactApplicationContextIfActiveOrWarn()
243-
if (reactApplicationContext != null) {
244-
reactApplicationContext.emitDeviceEvent(GRAYSCALE_MODE_EVENT_NAME, grayscaleModeEnabled)
245-
}
238+
reactApplicationContextIfActiveOrWarn?.emitDeviceEvent(
239+
GRAYSCALE_MODE_EVENT_NAME,
240+
grayscaleModeEnabled
241+
)
242+
246243
}
247244
}
248245

249246
override fun onHostResume() {
250247
accessibilityManager?.addTouchExplorationStateChangeListener(
251-
touchExplorationStateChangeListener)
248+
touchExplorationStateChangeListener
249+
)
252250
accessibilityManager?.addAccessibilityStateChangeListener(accessibilityServiceChangeListener)
253251
val transitionUri = Settings.Global.getUriFor(Settings.Global.TRANSITION_ANIMATION_SCALE)
254252
contentResolver.registerContentObserver(transitionUri, false, animationScaleObserver)
@@ -273,7 +271,7 @@ internal class AccessibilityInfoModule(context: ReactApplicationContext) :
273271
}
274272

275273
override fun initialize() {
276-
getReactApplicationContext().addLifecycleEventListener(this)
274+
reactApplicationContext.addLifecycleEventListener(this)
277275
updateAndSendTouchExplorationChangeEvent(
278276
accessibilityManager?.isTouchExplorationEnabled == true)
279277
updateAndSendAccessibilityServiceChangeEvent(accessibilityManager?.isEnabled == true)
@@ -282,7 +280,7 @@ internal class AccessibilityInfoModule(context: ReactApplicationContext) :
282280
}
283281

284282
override fun invalidate() {
285-
getReactApplicationContext().removeLifecycleEventListener(this)
283+
reactApplicationContext.removeLifecycleEventListener(this)
286284
super.invalidate()
287285
}
288286

@@ -296,7 +294,7 @@ internal class AccessibilityInfoModule(context: ReactApplicationContext) :
296294
val event = AccessibilityEvent.obtain(AccessibilityEvent.TYPE_ANNOUNCEMENT)
297295
event.text.add(message)
298296
event.className = AccessibilityInfoModule::class.java.name
299-
event.packageName = getReactApplicationContext().getPackageName()
297+
event.packageName = reactApplicationContext.getPackageName()
300298
accessibilityManager.sendAccessibilityEvent(event)
301299
}
302300

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/appearance/AppearanceModule.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ constructor(
9191
public fun emitAppearanceChanged(colorScheme: String) {
9292
val appearancePreferences = Arguments.createMap()
9393
appearancePreferences.putString("colorScheme", colorScheme)
94-
val reactApplicationContext = getReactApplicationContextIfActiveOrWarn()
94+
val reactApplicationContext = reactApplicationContextIfActiveOrWarn
9595
reactApplicationContext?.emitDeviceEvent(APPEARANCE_CHANGED_EVENT_NAME, appearancePreferences)
9696
}
9797

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/appstate/AppStateModule.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ internal class AppStateModule(reactContext: ReactApplicationContext) :
6161
Arguments.createMap().apply { putString("app_state", appState) }
6262

6363
private fun sendEvent(eventName: String, data: Any?) {
64-
val reactApplicationContext = getReactApplicationContext() ?: return
6564
// We don't gain anything interesting from logging here, and it's an extremely common
6665
// race condition for an AppState event to be triggered as the Catalyst instance is being
6766
// set up or torn down. So, just fail silently here.
@@ -85,7 +84,7 @@ internal class AppStateModule(reactContext: ReactApplicationContext) :
8584

8685
override fun invalidate() {
8786
super.invalidate()
88-
getReactApplicationContext().removeLifecycleEventListener(this)
87+
reactApplicationContext.removeLifecycleEventListener(this)
8988
}
9089

9190
companion object {

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/blob/BlobModule.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ public class BlobModule(reactContext: ReactApplicationContext) :
132132
}
133133

134134
public override fun getTypedExportedConstants(): Map<String, Any> {
135-
val resources = getReactApplicationContext().resources
136-
val packageName = getReactApplicationContext().packageName
135+
val resources = reactApplicationContext.resources
136+
val packageName = reactApplicationContext.packageName
137137
val resourceId = resources.getIdentifier("blob_provider_authority", "string", packageName)
138138
if (resourceId == 0) {
139139
return mapOf()

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/blob/FileReaderModule.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class FileReaderModule(reactContext: ReactApplicationContext) :
1919
NativeFileReaderModuleSpec(reactContext) {
2020

2121
private fun getBlobModule(reason: String): BlobModule? {
22-
val reactApplicationContext = getReactApplicationContextIfActiveOrWarn()
22+
val reactApplicationContext = reactApplicationContextIfActiveOrWarn
2323

2424
return reactApplicationContext?.getNativeModule(BlobModule::class.java)
2525
}

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/camera/ImageStoreManager.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ internal class ImageStoreManager(reactContext: ReactApplicationContext) :
3636
val executor = Executors.newSingleThreadExecutor()
3737
executor.execute {
3838
try {
39-
val contentResolver = getReactApplicationContext().getContentResolver()
39+
val contentResolver = reactApplicationContext.getContentResolver()
4040
val parsedUri = Uri.parse(uri)
4141
val inputStream = contentResolver.openInputStream(parsedUri) as InputStream
4242
try {

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/clipboard/ClipboardModule.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ internal class ClipboardModule(context: ReactApplicationContext) : NativeClipboa
2020

2121
private val clipboardService: ClipboardManager
2222
get() =
23-
getReactApplicationContext().getSystemService(ReactApplicationContext.CLIPBOARD_SERVICE)
23+
reactApplicationContext.getSystemService(ReactApplicationContext.CLIPBOARD_SERVICE)
2424
as ClipboardManager
2525

2626
override fun getString(promise: Promise) {

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/DeviceEventManagerModule.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ public open class DeviceEventManagerModule(
3535
/** Sends an event to the JS instance that the hardware back has been pressed. */
3636
public open fun emitHardwareBackPressed() {
3737
val reactApplicationContext: ReactApplicationContext? =
38-
getReactApplicationContextIfActiveOrWarn()
38+
reactApplicationContextIfActiveOrWarn
3939
reactApplicationContext?.emitDeviceEvent("hardwareBackPress", null)
4040
}
4141

4242
/** Sends an event to the JS instance that a new intent was received. */
4343
public open fun emitNewIntentReceived(uri: Uri) {
4444
val reactApplicationContext: ReactApplicationContext? =
45-
getReactApplicationContextIfActiveOrWarn()
45+
reactApplicationContextIfActiveOrWarn
4646
val map = Arguments.createMap()
4747
map.putString("url", uri.toString())
4848
reactApplicationContext?.emitDeviceEvent("url", map)
@@ -56,7 +56,7 @@ public open class DeviceEventManagerModule(
5656
// There should be no need to check if the catalyst instance is alive. After initialization
5757
// the thread instances cannot be null, and scheduling on a thread after ReactApplicationContext
5858
// teardown is a noop.
59-
getReactApplicationContext().runOnUiQueueThread(invokeDefaultBackPressRunnable)
59+
reactApplicationContext.runOnUiQueueThread(invokeDefaultBackPressRunnable)
6060
}
6161

6262
public companion object {

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/HeadlessJsTaskSupportModule.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ internal open class HeadlessJsTaskSupportModule(reactContext: ReactApplicationCo
2323
NativeHeadlessJsTaskSupportSpec(reactContext) {
2424
override fun notifyTaskRetry(taskIdDouble: Double, promise: Promise) {
2525
val taskId = taskIdDouble.toInt()
26-
val headlessJsTaskContext = HeadlessJsTaskContext.getInstance(getReactApplicationContext())
26+
val headlessJsTaskContext = HeadlessJsTaskContext.getInstance(reactApplicationContext)
2727
if (headlessJsTaskContext.isTaskRunning(taskId)) {
2828
val retryPosted = headlessJsTaskContext.retryTask(taskId)
2929
promise.resolve(retryPosted)
@@ -38,7 +38,7 @@ internal open class HeadlessJsTaskSupportModule(reactContext: ReactApplicationCo
3838

3939
override fun notifyTaskFinished(taskIdDouble: Double) {
4040
val taskId = taskIdDouble.toInt()
41-
val headlessJsTaskContext = HeadlessJsTaskContext.getInstance(getReactApplicationContext())
41+
val headlessJsTaskContext = HeadlessJsTaskContext.getInstance(reactApplicationContext)
4242
if (headlessJsTaskContext.isTaskRunning(taskId)) {
4343
headlessJsTaskContext.finishTask(taskId)
4444
} else {

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/TimingModule.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,19 @@ public class TimingModule(
4444
}
4545

4646
override fun callTimers(timerIDs: WritableArray) {
47-
getReactApplicationContextIfActiveOrWarn()
47+
reactApplicationContextIfActiveOrWarn
4848
?.getJSModule(JSTimers::class.java)
4949
?.callTimers(timerIDs)
5050
}
5151

5252
override fun callIdleCallbacks(frameTime: Double) {
53-
getReactApplicationContextIfActiveOrWarn()
53+
reactApplicationContextIfActiveOrWarn
5454
?.getJSModule(JSTimers::class.java)
5555
?.callIdleCallbacks(frameTime)
5656
}
5757

5858
override fun emitTimeDriftWarning(warningMessage: String) {
59-
getReactApplicationContextIfActiveOrWarn()
59+
reactApplicationContextIfActiveOrWarn
6060
?.getJSModule(JSTimers::class.java)
6161
?.emitTimeDriftWarning(warningMessage)
6262
}

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/debug/SourceCodeModule.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class SourceCodeModule(reactContext: ReactApplicationContext) :
2222
mapOf(
2323
"scriptURL" to
2424
Assertions.assertNotNull<String>(
25-
getReactApplicationContext().getSourceURL(),
25+
reactApplicationContext.getSourceURL(),
2626
"No source URL loaded, have you initialised the instance?"))
2727

2828
public companion object {

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/i18nmanager/I18nManagerModule.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import com.facebook.react.module.annotations.ReactModule
1616
internal class I18nManagerModule(context: ReactApplicationContext?) :
1717
NativeI18nManagerSpec(context) {
1818
override fun getTypedExportedConstants(): Map<String, Any> {
19-
val context = getReactApplicationContext()
19+
val context = reactApplicationContext
2020
val locale = context.resources.configuration.locales[0]
2121

2222
return mapOf(
@@ -26,15 +26,15 @@ internal class I18nManagerModule(context: ReactApplicationContext?) :
2626
}
2727

2828
override fun allowRTL(value: Boolean) {
29-
I18nUtil.instance.allowRTL(getReactApplicationContext(), value)
29+
I18nUtil.instance.allowRTL(reactApplicationContext, value)
3030
}
3131

3232
override fun forceRTL(value: Boolean) {
33-
I18nUtil.instance.forceRTL(getReactApplicationContext(), value)
33+
I18nUtil.instance.forceRTL(reactApplicationContext, value)
3434
}
3535

3636
override fun swapLeftAndRightInRTL(value: Boolean) {
37-
I18nUtil.instance.swapLeftAndRightInRTL(getReactApplicationContext(), value)
37+
I18nUtil.instance.swapLeftAndRightInRTL(reactApplicationContext, value)
3838
}
3939

4040
companion object {

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/image/ImageLoaderModule.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ internal class ImageLoaderModule : NativeImageLoaderAndroidSpec, LifecycleEventL
8282
promise.reject(ERROR_INVALID_URI, "Cannot get the size of an image for an empty URI")
8383
return
8484
}
85-
val source = ImageSource(getReactApplicationContext(), uriString)
85+
val source = ImageSource(reactApplicationContext, uriString)
8686
val request: ImageRequest = ImageRequestBuilder.newBuilderWithSource(source.uri).build()
8787
val dataSource: DataSource<CloseableReference<CloseableImage>> =
8888
this.imagePipeline.fetchDecodedImage(request, this.callerContext)
@@ -131,7 +131,7 @@ internal class ImageLoaderModule : NativeImageLoaderAndroidSpec, LifecycleEventL
131131
promise.reject(ERROR_INVALID_URI, "Cannot get the size of an image for an empty URI")
132132
return
133133
}
134-
val source = ImageSource(getReactApplicationContext(), uriString)
134+
val source = ImageSource(reactApplicationContext, uriString)
135135
val imageRequestBuilder: ImageRequestBuilder =
136136
ImageRequestBuilder.newBuilderWithSource(source.uri)
137137
val request: ImageRequest =
@@ -225,7 +225,7 @@ internal class ImageLoaderModule : NativeImageLoaderAndroidSpec, LifecycleEventL
225225
override fun queryCache(uris: ReadableArray, promise: Promise) {
226226
// perform cache interrogation in async task as disk cache checks are expensive
227227
@Suppress("DEPRECATION", "StaticFieldLeak")
228-
object : GuardedAsyncTask<Void, Void>(getReactApplicationContext()) {
228+
object : GuardedAsyncTask<Void, Void>(reactApplicationContext) {
229229
override fun doInBackgroundGuarded(vararg params: Void) {
230230
val result: WritableMap = Arguments.createMap()
231231
val imagePipeline: ImagePipeline = this@ImageLoaderModule.imagePipeline

0 commit comments

Comments
 (0)