com.facebook.react.bridge.GuardedAsyncTask Java Examples

The following examples show how to use com.facebook.react.bridge.GuardedAsyncTask. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ImageLoaderModule.java    From react-native-GPay with MIT License 7 votes vote down vote up
@ReactMethod
public void queryCache(final ReadableArray uris, final Promise promise) {
  // perform cache interrogation in async task as disk cache checks are expensive
  new GuardedAsyncTask<Void, Void>(getReactApplicationContext()) {
    @Override
    protected void doInBackgroundGuarded(Void... params) {
      WritableMap result = Arguments.createMap();
      ImagePipeline imagePipeline = Fresco.getImagePipeline();
      for (int i = 0; i < uris.size(); i++) {
        String uriString = uris.getString(i);
        final Uri uri = Uri.parse(uriString);
        if (imagePipeline.isInBitmapMemoryCache(uri)) {
          result.putString(uriString, "memory");
        } else if (imagePipeline.isInDiskCacheSync(uri)) {
          result.putString(uriString, "disk");
        }
      }
      promise.resolve(result);
    }
  }.executeOnExecutor(GuardedAsyncTask.THREAD_POOL_EXECUTOR);
}
 
Example #2
Source File: speechModule.java    From react-native-speech with MIT License 6 votes vote down vote up
@ReactMethod
public void shutDown(final Callback callBack) {
    new GuardedAsyncTask<Void, Void>(getReactApplicationContext()) {
        @Override
        protected void doInBackgroundGuarded(Void... params) {
            if (tts == null) {
                callBack.invoke(true);
            }
            try {
                tts.shutdown();
                callBack.invoke(null, true);
            } catch (Exception e) {
                callBack.invoke(e.getMessage());
            }
        }
    }.execute();
}
 
Example #3
Source File: DBManager.java    From react-native-android-sqlite with MIT License 6 votes vote down vote up
@ReactMethod
public void close(final Callback callback) {
	new GuardedAsyncTask<Void, Void>(getReactApplicationContext()) {
		@Override
		protected void doInBackgroundGuarded(Void ...params) {
			try {
				mDb.close();
			} catch(Exception e) {
				FLog.w(ReactConstants.TAG, "Exception in database close: ", e);
				callback.invoke(ErrorUtil.getError(null, e.getMessage()), null);
			}

			callback.invoke();
		}
	}.execute();
}
 
Example #4
Source File: DBManager.java    From react-native-android-sqlite with MIT License 6 votes vote down vote up
@ReactMethod
public void exec(final String sql, final ReadableArray values, final Callback callback) {
	new GuardedAsyncTask<Void, Void>(getReactApplicationContext()) {
		@Override
		protected void doInBackgroundGuarded(Void ...params) {
			try {
				mDb.exec(sql, values);
			} catch(Exception e) {
				FLog.w(ReactConstants.TAG, "Exception in database exec: ", e);
				callback.invoke(ErrorUtil.getError(null, e.getMessage()), null);
			}

			callback.invoke();
		}
	}.execute();
}
 
Example #5
Source File: DBManager.java    From react-native-android-sqlite with MIT License 6 votes vote down vote up
@ReactMethod
public void query(final String sql, final ReadableArray values, final Callback callback) {
	new GuardedAsyncTask<Void, Void>(getReactApplicationContext()) {
		@Override
		protected void doInBackgroundGuarded(Void ...params) {
			WritableArray data = Arguments.createArray();

			// FLog.w(ReactConstants.TAG, "dbmanager.query.sql=%s", sql);
			// FLog.w(ReactConstants.TAG, "dbmanager.query.values.size()=%d", values.size());

			try {
				data = mDb.query(sql, values);
			} catch(Exception e) {
				FLog.w(ReactConstants.TAG, "Exception in database query: ", e);
				callback.invoke(ErrorUtil.getError(null, e.getMessage()), null);
			}

			callback.invoke(null, data);
		}
	}.execute();
}
 
Example #6
Source File: UdpSockets.java    From react-native-udp with MIT License 6 votes vote down vote up
/**
 * Sets the broadcast flag for a given client.
 */
@ReactMethod
public void setBroadcast(final Integer cId, final Boolean flag, final Callback callback) {
    new GuardedAsyncTask<Void, Void>(getReactApplicationContext()) {
        @Override
        protected void doInBackgroundGuarded(Void... params) {
            UdpSocketClient client = findClient(cId, callback);
            if (client == null) {
                return;
            }

            try {
                client.setBroadcast(flag);
                callback.invoke();
            } catch (SocketException e) {
                callback.invoke(UdpErrorUtil.getError(null, e.getMessage()));
            }
        }
    }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
 
Example #7
Source File: UdpSockets.java    From react-native-udp with MIT License 6 votes vote down vote up
/**
 * Sends udp data via the {@link UdpSocketClient}
 */
@ReactMethod
public void send(final Integer cId, final String base64String,
                 final Integer port, final String address, final Callback callback) {
    new GuardedAsyncTask<Void, Void>(getReactApplicationContext()) {
        @Override
        protected void doInBackgroundGuarded(Void... params) {
            UdpSocketClient client = findClient(cId, callback);
            if (client == null) {
                return;
            }

            try {
                client.send(base64String, port, address, callback);
            } catch (IllegalStateException ise) {
                callback.invoke(UdpErrorUtil.getError(null, ise.getMessage()));
            }catch (UnknownHostException uhe) {
                callback.invoke(UdpErrorUtil.getError(null, uhe.getMessage()));
            } catch (IOException ioe) {
                // an exception occurred
                callback.invoke(UdpErrorUtil.getError(null, ioe.getMessage()));
            }
        }
    }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
 
Example #8
Source File: UdpSockets.java    From react-native-udp with MIT License 6 votes vote down vote up
/**
 * Leaves a multi-cast group
 */
@ReactMethod
public void dropMembership(final Integer cId, final String multicastAddress) {
    new GuardedAsyncTask<Void, Void>(getReactApplicationContext()) {
        @Override
        protected void doInBackgroundGuarded(Void... params) {
            UdpSocketClient client = findClient(cId, null);
            if (client == null) {
                return;
            }

            try {
                client.dropMembership(multicastAddress);
            } catch (IOException ioe) {
                // an exception occurred
                FLog.e(TAG, "dropMembership", ioe);
            } finally {
                if (mMulticastLock != null && mMulticastLock.isHeld()) {
                    mMulticastLock.release();
                }
            }
        }
    }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
 
Example #9
Source File: UdpSockets.java    From react-native-udp with MIT License 6 votes vote down vote up
@Override
public void onCatalystInstanceDestroy() {
    mShuttingDown = true;

    // serialize on the AsyncTask thread, and block
    try {
        new GuardedAsyncTask<Void, Void>(getReactApplicationContext()) {
            @Override
            protected void doInBackgroundGuarded(Void... params) {
                for (int i = 0; i < mClients.size(); i++) {
                    try {
                        mClients.valueAt(i).close();
                    } catch (IOException e) {
                        FLog.e(TAG, "exception when shutting down", e);
                    }
                }
                mClients.clear();
            }
        }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR).get();
    } catch (InterruptedException ioe) {
        FLog.e(TAG, "onCatalystInstanceDestroy", ioe);
    } catch (ExecutionException ee) {
        FLog.e(TAG, "onCatalystInstanceDestroy", ee);
    }
}
 
Example #10
Source File: speechModule.java    From react-native-speech with MIT License 6 votes vote down vote up
@ReactMethod
public void isSpeaking(final Callback callback) {
    new GuardedAsyncTask<Void, Void>(getReactApplicationContext()) {
        @Override
        protected void doInBackgroundGuarded(Void... params) {
            try {
                if (tts.isSpeaking()) {
                    callback.invoke(null, true);
                } else {
                    callback.invoke(null, false);
                }
            } catch (Exception e) {
                callback.invoke(e.getMessage());
            }
        }
    }.execute();
}
 
Example #11
Source File: speechModule.java    From react-native-speech with MIT License 6 votes vote down vote up
/***
 * This method will expose all the available languages in TTS engine
 *
 * @param callback
 */
@ReactMethod
public void getLocale(final Callback callback) {
    new GuardedAsyncTask<Void, Void>(getReactApplicationContext()) {
        @Override
        protected void doInBackgroundGuarded(Void... params) {
            try {
                if (tts == null) {
                    init();
                }
                Locale[] locales = Locale.getAvailableLocales();
                WritableArray data = Arguments.createArray();
                for (Locale locale : locales) {
                    int res = tts.isLanguageAvailable(locale);
                    if (res == TextToSpeech.LANG_COUNTRY_AVAILABLE) {
                        data.pushString(locale.getLanguage());
                    }
                }
                callback.invoke(null, data);
            } catch (Exception e) {
                callback.invoke(e.getMessage());
            }
        }
    }.execute();
}
 
Example #12
Source File: AsyncStorageModule.java    From react-native-GPay with MIT License 6 votes vote down vote up
/**
 * Clears the database.
 */
@ReactMethod
public void clear(final Callback callback) {
  new GuardedAsyncTask<Void, Void>(getReactApplicationContext()) {
    @Override
    protected void doInBackgroundGuarded(Void... params) {
      if (!mReactDatabaseSupplier.ensureDatabase()) {
        callback.invoke(AsyncStorageErrorUtil.getDBError(null));
        return;
      }
      try {
        mReactDatabaseSupplier.clear();
        callback.invoke();
      } catch (Exception e) {
        FLog.w(ReactConstants.TAG, e.getMessage(), e);
        callback.invoke(AsyncStorageErrorUtil.getError(null, e.getMessage()));
      }
    }
  }.executeOnExecutor(executor);
}
 
Example #13
Source File: TcpSocketModule.java    From react-native-tcp-socket with MIT License 6 votes vote down vote up
@SuppressLint("StaticFieldLeak")
@SuppressWarnings("unused")
@ReactMethod
public void write(@NonNull final Integer cId, @NonNull final String base64String, @Nullable final Callback callback) {
    new GuardedAsyncTask<Void, Void>(mReactContext.getExceptionHandler()) {
        @Override
        protected void doInBackgroundGuarded(Void... params) {
            TcpSocketClient socketClient = socketClients.get(cId);
            if (socketClient == null) {
                return;
            }
            try {
                socketClient.write(Base64.decode(base64String, Base64.NO_WRAP));
                if (callback != null) {
                    callback.invoke();
                }
            } catch (IOException e) {
                if (callback != null) {
                    callback.invoke(e.toString());
                }
                onError(cId, e.toString());
            }
        }
    }.executeOnExecutor(executorService);
}
 
Example #14
Source File: TcpSocketModule.java    From react-native-tcp-socket with MIT License 6 votes vote down vote up
@SuppressLint("StaticFieldLeak")
@SuppressWarnings("unused")
@ReactMethod
public void end(final Integer cId) {
    new GuardedAsyncTask<Void, Void>(mReactContext.getExceptionHandler()) {
        @Override
        protected void doInBackgroundGuarded(Void... params) {
            TcpSocketClient socketClient = socketClients.get(cId);
            if (socketClient == null) {
                return;
            }
            socketClient.close();
            socketClients.remove(cId);
        }
    }.executeOnExecutor(executorService);
}
 
Example #15
Source File: TcpSocketModule.java    From react-native-tcp-socket with MIT License 6 votes vote down vote up
@SuppressLint("StaticFieldLeak")
@SuppressWarnings("unused")
@ReactMethod
public void listen(final Integer cId, final ReadableMap options) {
    new GuardedAsyncTask<Void, Void>(mReactContext.getExceptionHandler()) {
        @Override
        protected void doInBackgroundGuarded(Void... params) {
            try {
                TcpSocketServer server = new TcpSocketServer(socketClients, TcpSocketModule.this, cId, options);
                socketClients.put(cId, server);
                int port = options.getInt("port");
                String host = options.getString("host");
                onConnect(cId, host, port);
            } catch (Exception uhe) {
                onError(cId, uhe.getMessage());
            }
        }
    }.executeOnExecutor(executorService);
}
 
Example #16
Source File: SocketsModule.java    From react-native-sockets with MIT License 6 votes vote down vote up
@Override
public void onCatalystInstanceDestroy() {
    try {
        new GuardedAsyncTask<Void, Void>(getReactApplicationContext()) {
            @Override
            protected void doInBackgroundGuarded(Void... params) {
                if (client != null) {
                    client.disconnect(false);
                }
                if (server != null) {
                    server.close();
                }
            }
        }.execute().get();
    } catch (InterruptedException ioe) {
        Log.e(eTag, "onCatalystInstanceDestroy", ioe);
    } catch (ExecutionException ee) {
        Log.e(eTag, "onCatalystInstanceDestroy", ee);
    }
}
 
Example #17
Source File: AsyncStorageModule.java    From react-native-GPay with MIT License 5 votes vote down vote up
/**
 * Returns an array with all keys from the database.
 */
@ReactMethod
public void getAllKeys(final Callback callback) {
  new GuardedAsyncTask<Void, Void>(getReactApplicationContext()) {
    @Override
    protected void doInBackgroundGuarded(Void... params) {
      if (!ensureDatabase()) {
        callback.invoke(AsyncStorageErrorUtil.getDBError(null), null);
        return;
      }
      WritableArray data = Arguments.createArray();
      String[] columns = {KEY_COLUMN};
      Cursor cursor = mReactDatabaseSupplier.get()
          .query(TABLE_CATALYST, columns, null, null, null, null, null);
      try {
        if (cursor.moveToFirst()) {
          do {
            data.pushString(cursor.getString(0));
          } while (cursor.moveToNext());
        }
      } catch (Exception e) {
        FLog.w(ReactConstants.TAG, e.getMessage(), e);
        callback.invoke(AsyncStorageErrorUtil.getError(null, e.getMessage()), null);
        return;
      } finally {
        cursor.close();
      }
      callback.invoke(null, data);
    }
  }.executeOnExecutor(executor);
}
 
Example #18
Source File: UdpSockets.java    From react-native-udp with MIT License 5 votes vote down vote up
/**
 * Binds to a given port and address, and begins listening for data.
 */
@ReactMethod
public void bind(final Integer cId, final Integer port, final @Nullable String address,
                 final Callback callback) {
    new GuardedAsyncTask<Void, Void>(getReactApplicationContext()) {
        @Override
        protected void doInBackgroundGuarded(Void... params) {
            UdpSocketClient client = findClient(cId, callback);
            if (client == null) {
                    return;
            }

            try {
                client.bind(port, address);

                WritableMap result = Arguments.createMap();
                result.putString("address", address);
                result.putInt("port", port);

                callback.invoke(null, result);
            } catch (SocketException se) {
                // Socket is already bound or a problem occurred during binding
                callback.invoke(UdpErrorUtil.getError(null, se.getMessage()));
            } catch (IllegalArgumentException iae) {
                // SocketAddress is not supported
                callback.invoke(UdpErrorUtil.getError(null, iae.getMessage()));
            } catch (IOException ioe) {
                // an exception occurred
                callback.invoke(UdpErrorUtil.getError(null, ioe.getMessage()));
            }
        }
    }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
 
Example #19
Source File: LANScanModule.java    From react-native-lanscan with MIT License 5 votes vote down vote up
@ReactMethod
public void stop() {
    new GuardedAsyncTask<Void, Void>(getReactApplicationContext()) {
        @Override
        protected void doInBackgroundGuarded(Void... params) {
            ((ThreadPoolExecutor)ManagedThreadPoolExecutor.THREAD_POOL_EXECUTOR_PINGS).shutdownNow();
            ((ThreadPoolExecutor)ManagedThreadPoolExecutor.THREAD_POOL_EXECUTOR_BROADCAST).shutdownNow();

            long startTime = System.currentTimeMillis();
            long endTime = 0L;
            long timeout = 1000;
            boolean isTerminated_broadcast = false;
            boolean isTerminated_pings = false;

            // wait until all the threads are terminated
            // or grace timeout finishes
            while(!isTerminated_broadcast || !isTerminated_pings || endTime < timeout) {
                isTerminated_broadcast = ((ThreadPoolExecutor)ManagedThreadPoolExecutor.THREAD_POOL_EXECUTOR_BROADCAST).isTerminated();
                isTerminated_pings = ((ThreadPoolExecutor)ManagedThreadPoolExecutor.THREAD_POOL_EXECUTOR_BROADCAST).isTerminated();
                endTime = (new Date()).getTime() - startTime;
            }

            // successfully stopped the tasks... send top event
            sendEvent(getReactApplicationContext(), EVENT_STOP, null);
        }
    }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
 
Example #20
Source File: NetworkingModule.java    From react-native-GPay with MIT License 5 votes vote down vote up
private void cancelRequest(final int requestId) {
  // We have to use AsyncTask since this might trigger a NetworkOnMainThreadException, this is an
  // open issue on OkHttp: https://github.com/square/okhttp/issues/869
  new GuardedAsyncTask<Void, Void>(getReactApplicationContext()) {
    @Override
    protected void doInBackgroundGuarded(Void... params) {
      OkHttpCallUtil.cancelTag(mClient, Integer.valueOf(requestId));
    }
  }.execute();
}
 
Example #21
Source File: UdpSockets.java    From react-native-udp with MIT License 5 votes vote down vote up
/**
 * Closes a specific client's socket, and removes it from the list of known clients.
 */
@ReactMethod
public void close(final Integer cId, final Callback callback) {
    new GuardedAsyncTask<Void, Void>(getReactApplicationContext()) {
        @Override
        protected void doInBackgroundGuarded(Void... params) {
            UdpSocketClient client = findClient(cId, callback);
            if (client == null) {
                return;
            }

            if (mMulticastLock != null && mMulticastLock.isHeld() && client.isMulticast()) {
                // drop the multi-cast lock if this is a multi-cast client
                mMulticastLock.release();
            }

            try {
                client.close();
                callback.invoke();
            } catch (IOException ioe) {
                callback.invoke(UdpErrorUtil.getError(null, ioe.getMessage()));
            }

            mClients.remove(cId);
        }
    }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
 
Example #22
Source File: ForwardingCookieHandler.java    From react-native-GPay with MIT License 5 votes vote down vote up
private void runInBackground(final Runnable runnable) {
  new GuardedAsyncTask<Void, Void>(mContext) {
    @Override
    protected void doInBackgroundGuarded(Void... params) {
      runnable.run();
    }
  }.execute();
}
 
Example #23
Source File: UdpSockets.java    From react-native-udp with MIT License 5 votes vote down vote up
/**
 * Notifies the javascript layer upon data receipt.
 */
@Override
public void didReceiveData(final UdpSocketClient socket, final String data, final String host, final int port) {
    new GuardedAsyncTask<Void, Void>(getReactApplicationContext()) {
        @Override
        protected void doInBackgroundGuarded(Void... params) {
            int clientID = -1;
            for(int i = 0; i < mClients.size(); i++) {
                clientID = mClients.keyAt(i);
                // get the object by the key.
                if (socket.equals(mClients.get(clientID))) {
                    break;
                }
            }

            if (clientID == -1) {
                return;
            }

            WritableMap eventParams = Arguments.createMap();
            eventParams.putString("data", data);
            eventParams.putString("address", host);
            eventParams.putInt("port", port);

            ReactContext reactContext = UdpSockets.this.getReactApplicationContext();
            reactContext
                    .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
                    .emit("udp-" + clientID + "-data", eventParams);
        }
    }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
 
Example #24
Source File: DBManager.java    From react-native-android-sqlite with MIT License 5 votes vote down vote up
@ReactMethod
public void init(final String name, final Callback callback) {
	new GuardedAsyncTask<Void, Void>(getReactApplicationContext()) {
		@Override
		protected void doInBackgroundGuarded(Void ...params) {
			// FLog.w(ReactConstants.TAG, "dbmanager.init.name=%s", name);
			mDb = new SQLiteManager(getReactApplicationContext(), name);
			mDb.init();
			callback.invoke();
		}
	}.execute();
}
 
Example #25
Source File: TcpSocketModule.java    From react-native-tcp-socket with MIT License 5 votes vote down vote up
/**
 * Creates a TCP Socket and establish a connection with the given host
 *
 * @param cId     socket ID
 * @param host    socket IP address
 * @param port    socket port to be bound
 * @param options extra options
 */
@SuppressLint("StaticFieldLeak")
@SuppressWarnings("unused")
@ReactMethod
public void connect(@NonNull final Integer cId, @NonNull final String host, @NonNull final Integer port, @NonNull final ReadableMap options) {
    new GuardedAsyncTask<Void, Void>(mReactContext.getExceptionHandler()) {
        @Override
        protected void doInBackgroundGuarded(Void... params) {
            TcpSocketClient client = socketClients.get(cId);
            if (client != null) {
                onError(cId, TAG + "createSocket called twice with the same id.");
                return;
            }
            try {
                // Get the network interface
                final String localAddress = options.hasKey("localAddress") ? options.getString("localAddress") : null;
                final String iface = options.hasKey("interface") ? options.getString("interface") : null;
                selectNetwork(iface, localAddress);
                client = new TcpSocketClient(TcpSocketModule.this, cId, null);
                socketClients.put(cId, client);
                client.connect(mReactContext, host, port, options, currentNetwork.getNetwork());
                onConnect(cId, host, port);
            } catch (Exception e) {
                onError(cId, e.getMessage());
            }
        }
    }.executeOnExecutor(executorService);
}
 
Example #26
Source File: speechModule.java    From react-native-speech with MIT License 5 votes vote down vote up
@ReactMethod
public void stop() {
    new GuardedAsyncTask<Void, Void>(getReactApplicationContext()) {
        @Override
        protected void doInBackgroundGuarded(Void... params) {
            tts.stop();
        }
    }.execute();
}
 
Example #27
Source File: speechModule.java    From react-native-speech with MIT License 4 votes vote down vote up
@ReactMethod
public void speak(final ReadableMap args, final Callback callback) {
    new GuardedAsyncTask<Void, Void>(getReactApplicationContext()) {
        @Override
        protected void doInBackgroundGuarded(Void... params) {
            if (tts == null) {
                init();
            }
            String text = args.hasKey("text") ? args.getString("text") : null;
            String language = args.hasKey("language") ? args.getString("language") : null;
            Boolean forceStop = args.hasKey("forceStop") ? args.getBoolean("forceStop") : null;
            Float pitch = args.hasKey("pitch") ? (float) args.getDouble("pitch") : null;
            if (tts.isSpeaking()) {
                //Force to stop and start new speech
                if (forceStop != null && forceStop) {
                    tts.stop();
                } else {
                    callback.invoke("TTS is already speaking something , Please shutdown or stop  TTS and try again");
                    return;
                }
            }
            if (args.getString("text") == null || text == "") {
                callback.invoke("t can not be blank");
                return;
            }
            try {
                if (language != null && language != "") {
                    tts.setLanguage(new Locale(language));
                } else {
                    //Setting up default language
                    tts.setLanguage(new Locale("en"));
                }
                //Set the pitch if provided by the user
                if (pitch != null) {
                    tts.setPitch(pitch);
                }
                //TODO:: Need to implement the UTTERANCE Id and give the callback
                int speakResult = 0;
                if (Build.VERSION.SDK_INT >= 21)
                    speakResult = tts.speak(text, TextToSpeech.QUEUE_FLUSH, null, null);
                else
                    speakResult = tts.speak(text, TextToSpeech.QUEUE_FLUSH, null, null);

                if (speakResult < 0)
                    throw new Exception("Speak failed, make sure that TTS service is installed on you device");

                callback.invoke(null, true);
            } catch (Exception e) {
                callback.invoke(e.getMessage());
            }
        }
    }.execute();
}
 
Example #28
Source File: UdpSockets.java    From react-native-udp with MIT License 4 votes vote down vote up
/**
 * Joins a multi-cast group
 */
@ReactMethod
public void addMembership(final Integer cId, final String multicastAddress) {
    new GuardedAsyncTask<Void, Void>(getReactApplicationContext()) {
        @Override
        protected void doInBackgroundGuarded(Void... params) {
            UdpSocketClient client = findClient(cId, null);
            if (client == null) {
                return;
            }

            if (mMulticastLock == null) {
                WifiManager wifiMgr = (WifiManager) getReactApplicationContext()
                    .getApplicationContext()
                    .getSystemService(Context.WIFI_SERVICE);
                mMulticastLock = wifiMgr.createMulticastLock("react-native-udp");
                mMulticastLock.setReferenceCounted(true);
            }

           try {
                mMulticastLock.acquire();
                client.addMembership(multicastAddress);
            } catch (IllegalStateException ise) {
                // an exception occurred
                if (mMulticastLock != null && mMulticastLock.isHeld()) {
                    mMulticastLock.release();
                }
                FLog.e(TAG, "addMembership", ise);
            } catch (UnknownHostException uhe) {
                // an exception occurred
                if (mMulticastLock != null && mMulticastLock.isHeld()) {
                    mMulticastLock.release();
                }
                FLog.e(TAG, "addMembership", uhe);
            } catch (IOException ioe) {
                // an exception occurred
                if (mMulticastLock != null && mMulticastLock.isHeld()) {
                    mMulticastLock.release();
                }
                FLog.e(TAG, "addMembership", ioe);
            }
        }
    }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}