@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); } }
/** * 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(); }
@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); }
/*** * 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(); }
@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(); }
@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(); }
@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(); }
@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(); }
@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(); }
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(); }
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(); }
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(); }
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(); }
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(); }
private void runInBackground(final Runnable runnable) { new GuardedAsyncTask<Void, Void>(mContext) { @Override protected void doInBackgroundGuarded(Void... params) { runnable.run(); } }.execute(); }
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(); }
/** * 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(); }
@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(); }
@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); }
@ReactMethod public void stop() { new GuardedAsyncTask<Void, Void>(getReactApplicationContext()) { @Override protected void doInBackgroundGuarded(Void... params) { tts.stop(); } }.execute(); }