Java Code Examples for com.facebook.react.bridge.GuardedAsyncTask

The following are top voted examples for showing how to use com.facebook.react.bridge.GuardedAsyncTask. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: react-native-sockets   File: SocketsModule.java   View source code 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 2
Project: RNLearn_Project1   File: AsyncStorageModule.java   View source code 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()));
      }
    }
  }.execute();
}
 
Example 3
Project: RNLearn_Project1   File: ImageLoaderModule.java   View source code 6 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 4
Project: RNLearn_Project1   File: AsyncStorageModule.java   View source code 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()));
      }
    }
  }.execute();
}
 
Example 5
Project: RNLearn_Project1   File: ImageLoaderModule.java   View source code 6 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 6
Project: ReactNativeSignatureExample   File: AsyncStorageModule.java   View source code 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()));
      }
    }
  }.execute();
}
 
Example 7
Project: react-native-ibeacon-android   File: AsyncStorageModule.java   View source code 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()));
      }
    }
  }.execute();
}
 
Example 8
Project: react-native-box-loaders   File: AsyncStorageModule.java   View source code 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()));
      }
    }
  }.execute();
}
 
Example 9
Project: react-native-speech   File: speechModule.java   View source code 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 10
Project: react-native-speech   File: speechModule.java   View source code 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
Project: react-native-speech   File: speechModule.java   View source code 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 12
Project: Ironman   File: AsyncStorageModule.java   View source code 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()));
      }
    }
  }.execute();
}
 
Example 13
Project: Ironman   File: ImageLoaderModule.java   View source code 6 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 14
Project: react-native-android-sqlite   File: DBManager.java   View source code 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 15
Project: react-native-android-sqlite   File: DBManager.java   View source code 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 16
Project: react-native-android-sqlite   File: DBManager.java   View source code 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 17
Project: rnrecord   File: SaveCommand.java   View source code 5 votes vote down vote up
private void executeDbCallsAsync(final String tableName, final Promise promise, final String createQuery, final ContentValues contentValues) {
    new GuardedAsyncTask(reactContext) {
        @Override
        protected void doInBackgroundGuarded(Object[] params) {
            SQLiteDatabase db = rnRecordSQLiteHelper.getWritableDatabase();

            db.execSQL(createQuery);
            promise.resolve((double)db.insert(tableName, null, contentValues));

        }
    }.execute();
}
 
Example 18
Project: rnrecord   File: FindAllCommand.java   View source code 5 votes vote down vote up
private void executeDbCallsAsync(final String tableName, final Promise promise) {
    new GuardedAsyncTask(reactContext) {
        @Override
        protected void doInBackgroundGuarded(Object[] params) {
            SQLiteDatabase db = rnRecordSQLiteHelper.getWritableDatabase();

            promise.resolve(transformQueryResults(db.rawQuery("select * from " + tableName, null)));

        }
    }.execute();
}
 
Example 19
Project: rnrecord   File: FindCommand.java   View source code 5 votes vote down vote up
private void executeDbCallsAsync(final String queryString, final ArrayList<String> queryArguments, final Promise promise) {
    new GuardedAsyncTask(reactContext) {
        @Override
        protected void doInBackgroundGuarded(Object[] params) {
            SQLiteDatabase db = rnRecordSQLiteHelper.getWritableDatabase();

            promise.resolve(transformQueryResults(db.rawQuery(queryString, queryArguments.toArray(new String[queryArguments.size()]) )));

        }
    }.execute();
}
 
Example 20
Project: rnrecord   File: DeleteCommand.java   View source code 5 votes vote down vote up
private void executeDbCallsAsync(final String tableName, final Promise promise, final int id) {
    new GuardedAsyncTask(reactContext) {
        @Override
        protected void doInBackgroundGuarded(Object[] params) {
            SQLiteDatabase db = rnRecordSQLiteHelper.getWritableDatabase();

            promise.resolve(db.delete(tableName, "id = ?", new String[] {String.valueOf(id)} ));

        }
    }.execute();
}
 
Example 21
Project: rnrecord   File: UpdateCommand.java   View source code 5 votes vote down vote up
private void executeDbCallsAsync(final String tableName, final Promise promise, final ContentValues contentValues) {
    new GuardedAsyncTask(reactContext) {
        @Override
        protected void doInBackgroundGuarded(Object[] params) {
            SQLiteDatabase db = rnRecordSQLiteHelper.getWritableDatabase();

            promise.resolve(db.update(tableName, contentValues, "id = ?", new String[] {contentValues.getAsString("id")} ));

        }
    }.execute();
}
 
Example 22
Project: RNLearn_Project1   File: ForwardingCookieHandler.java   View source code 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
Project: RNLearn_Project1   File: NetworkingModule.java   View source code 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 24
Project: RNLearn_Project1   File: AsyncStorageModule.java   View source code 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);
    }
  }.execute();
}
 
Example 25
Project: RNLearn_Project1   File: ForwardingCookieHandler.java   View source code 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 26
Project: RNLearn_Project1   File: NetworkingModule.java   View source code 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 27
Project: RNLearn_Project1   File: AsyncStorageModule.java   View source code 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);
    }
  }.execute();
}
 
Example 28
Project: ReactNativeSignatureExample   File: ForwardingCookieHandler.java   View source code 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 29
Project: ReactNativeSignatureExample   File: NetworkingModule.java   View source code 5 votes vote down vote up
@ReactMethod
public void abortRequest(ExecutorToken executorToken, 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) {
      mClient.cancel(requestId);
    }
  }.execute();
}
 
Example 30
Project: ReactNativeSignatureExample   File: AsyncStorageModule.java   View source code 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);
    }
  }.execute();
}
 
Example 31
Project: react-native-ibeacon-android   File: ForwardingCookieHandler.java   View source code 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 32
Project: react-native-ibeacon-android   File: NetworkingModule.java   View source code 5 votes vote down vote up
@ReactMethod
public void abortRequest(ExecutorToken executorToken, 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) {
      mClient.cancel(requestId);
    }
  }.execute();
}
 
Example 33
Project: react-native-ibeacon-android   File: AsyncStorageModule.java   View source code 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);
    }
  }.execute();
}
 
Example 34
Project: react-native-lanscan   File: LANScanModule.java   View source code 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 35
Project: react-native-box-loaders   File: ForwardingCookieHandler.java   View source code 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 36
Project: react-native-box-loaders   File: NetworkingModule.java   View source code 5 votes vote down vote up
@ReactMethod
public void abortRequest(ExecutorToken executorToken, 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) {
      mClient.cancel(requestId);
    }
  }.execute();
}
 
Example 37
Project: react-native-box-loaders   File: AsyncStorageModule.java   View source code 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);
    }
  }.execute();
}
 
Example 38
Project: react-native-speech   File: speechModule.java   View source code 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 39
Project: Ironman   File: ForwardingCookieHandler.java   View source code 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 40
Project: Ironman   File: NetworkingModule.java   View source code 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();
}