1
1
package com.analyticsreactnativepluginadvertisingid
2
2
3
- import com.facebook.react.bridge.ReactApplicationContext
4
- import com.facebook.react.bridge.ReactContextBaseJavaModule
5
- import com.facebook.react.bridge.ReactMethod
6
- import com.facebook.react.bridge.Promise
7
- import com.facebook.react.ReactApplication
8
- import com.google.android.gms.ads.identifier.AdvertisingIdClient
9
- import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
3
+ import com.facebook.react.bridge.*
10
4
import com.facebook.react.module.annotations.ReactModule
11
- import android.util.Log
12
- import java.io.IOException;
13
-
14
-
15
- @ReactModule(name= " AnalyticsReactNativePluginAdvertisingId" )
16
- class AnalyticsReactNativePluginAdvertisingIdModule (reactContext : ReactApplicationContext ) : ReactContextBaseJavaModule(reactContext) {
17
- override fun getName (): String {
18
- return " AnalyticsReactNativePluginAdvertisingId"
19
- }
20
-
21
- @ReactMethod
22
- fun getAdvertisingId (promise : Promise ) {
23
- getAdvertisingIdInfo(promise) { advertisingInfo ->
24
- val id = advertisingInfo.id
25
- promise.resolve(id.toString())
26
- }
27
- }
5
+ import com.google.android.gms.ads.identifier.AdvertisingIdClient
6
+ import kotlinx.coroutines.*
28
7
29
- @ReactMethod
30
- fun getIsLimitAdTrackingEnableStatus (promise : Promise ) {
31
- getAdvertisingIdInfo(promise) { advertisingInfo ->
32
- val isLimitAdTrackingEnabled = advertisingInfo.isLimitAdTrackingEnabled
33
- promise.resolve(isLimitAdTrackingEnabled)
34
- }
35
- }
8
+ @ReactModule(name = " AnalyticsReactNativePluginAdvertisingId" )
36
9
37
- private fun getAdvertisingIdInfo (promise : Promise , callback : (AdvertisingIdClient .Info ) -> Unit ) {
38
- if (currentActivity?.application == null ) {
39
- promise.resolve(null )
40
- return
41
- }
10
+ class AnalyticsReactNativePluginAdvertisingIdModule (
11
+ private val reactContext : ReactApplicationContext
12
+ ) : ReactContextBaseJavaModule(reactContext) {
42
13
43
- val reactContext = (currentActivity?.application as ReactApplication )
44
- ?.reactNativeHost
45
- ?.reactInstanceManager
46
- ?.currentReactContext
14
+ override fun getName (): String {
15
+ return " AnalyticsReactNativePluginAdvertisingId"
16
+ }
47
17
48
- if (reactContext == null ) {
49
- promise.resolve(null )
50
- return
51
- }
18
+ /* *
19
+ * Return only the advertising ID string
20
+ */
21
+ @ReactMethod
22
+ fun getAdvertisingId (promise : Promise ) {
23
+ Thread {
24
+ try {
25
+ val info = AdvertisingIdClient .getAdvertisingIdInfo(reactContext)
26
+ promise.resolve(info.id ? : " " )
27
+ } catch (e: Exception ) {
28
+ promise.reject(" ERROR" , e)
29
+ }
30
+ }.start()
31
+ }
32
+ /* *
33
+ * Return only the "is limit ad tracking enabled" status
34
+ */
35
+ @ReactMethod
36
+ fun getIsLimitAdTrackingEnableStatus (promise : Promise ) {
37
+ Thread {
38
+ try {
39
+ val info = AdvertisingIdClient .getAdvertisingIdInfo(reactContext)
40
+ promise.resolve(info.isLimitAdTrackingEnabled ? : false )
41
+ } catch (e: Exception ) {
42
+ promise.reject(" ERROR" , e)
43
+ }
44
+ }.start()
45
+ }
52
46
53
- try {
54
- val advertisingInfo = AdvertisingIdClient .getAdvertisingIdInfo(reactContext)
55
- callback(advertisingInfo)
56
- } catch (e: GooglePlayServicesNotAvailableException ) {
57
- Log .d(name, e.toString())
58
- promise.resolve(null )
59
- } catch (e: IOException ) {
60
- Log .d(name, e.toString())
61
- promise.resolve(null )
62
- }
47
+ /* *
48
+ * Return both values together
49
+ */
50
+ @ReactMethod
51
+ fun getAdvertisingInfo (promise : Promise ) {
52
+ Thread {
53
+ try {
54
+ val info = AdvertisingIdClient .getAdvertisingIdInfo(reactContext)
55
+ val result = Arguments .createMap()
56
+ result.putString(" advertisingId" , info.id ? : " " )
57
+ result.putBoolean(" isLimitAdTrackingEnabled" , info.isLimitAdTrackingEnabled ? : false )
58
+ promise.resolve(result)
59
+ } catch (e: Exception ) {
60
+ promise.reject(" ERROR" , e)
61
+ }
62
+ }.start()
63
63
}
64
64
}
0 commit comments