List of usage examples for com.facebook.react.bridge Promise reject
void reject(String code, @NonNull WritableMap userInfo);
From source file:by.iba.gomel.avkhonia.MFReactNativeBind.WLResourceRequestRN.java
License:Apache License
@ReactMethod public void asyncRequestWithURL(String url, String method, final Promise promise) { try {// w w w .java2 s . c o m Log.d("IBATimesheet", "REQUEST TO MFP - " + url); WLResourceRequest request = new WLResourceRequest(URI.create(url), method, DEFAULT_TIMEOUT); request.send(new WLResponseListener() { public void onSuccess(WLResponse response) { try { promise.resolve(response.getResponseText()); Log.d("Success", response.getResponseText()); } catch (Exception e) { Log.d("IBATimesheet", "ERROR PROMISE RESOLVE: " + e); } } public void onFailure(WLFailResponse response) { promise.reject(response.getErrorStatusCode(), response.getErrorMsg() + " - " + response.getErrorStatusCode()); Log.d("Failure", response.getErrorMsg()); } }); } catch (IllegalViewOperationException e) { promise.reject("failure", e.getMessage(), e); } }
From source file:by.iba.gomel.avkhonia.MFReactNativeBind.WLResourceRequestRN.java
License:Apache License
@ReactMethod public void asyncRequestWithURLBody(String url, String method, String params, final Promise promise) { try {/*w ww . java 2 s. c o m*/ Log.d("IBATimesheet", "REQUEST TO MFP - " + url); String logbody = params; while (logbody.length() > 400) { Log.d("IBATimesheet", "REQUEST BODY - " + logbody.substring(0, 400)); logbody = logbody.substring(400); } Log.d("IBATimesheet", "REQUEST BODY - " + logbody); WLResourceRequest request = new WLResourceRequest(URI.create(url), method, DEFAULT_TIMEOUT); request.send(new JSONObject(params), new WLResponseListener() { public void onSuccess(WLResponse response) { promise.resolve(response.getResponseText()); Log.d("Success", response.getResponseText()); } public void onFailure(WLFailResponse response) { promise.reject(response.getErrorStatusCode(), response.getErrorMsg() + " - " + response.getErrorStatusCode()); Log.d("Failure", response.getErrorMsg()); } }); } catch (Exception e) { promise.reject("failure", e.getMessage(), e); } }
From source file:com.hijridatepicker.HijriDatePickerAndroidModule.java
License:Open Source License
/** * Copyright (c) 2015-present, Facebook, Inc. * All rights reserved.//from www . j a v a 2 s .c o m * <p> * This source code is licensed under the BSD-style license found in the * LICENSE file in the root directory of this source tree. An additional grant * of patent rights can be found in the PATENTS file in the same directory. * Show a date picker dialog. * * @param options a map containing options. Available keys are: * <p> * <ul> * <li>{@code date} (timestamp in milliseconds) the date to show by default</li> * <li> * {@code minDate} (timestamp in milliseconds) the minimum date the user should be allowed * to select * </li> * <li> * {@code maxDate} (timestamp in milliseconds) the maximum date the user should be allowed * to select * </li> * <li> * {@code mode} To set the date picker mode to ' no_arrows/default' , * currently there's only one mode for HijriDatePicker, so this field works only with mode no_arrows/default * </li> * <li> * {@code weekDayLabels} (array of strings) the day labels that appears on the calendar * </li> * </ul> * @param promise This will be invoked with parameters action, year, * month (0-11), day, where action is {@code dateSetAction} or * {@code dismissedAction}, depending on what the user did. If the action is * dismiss, year, month and date are undefined. */ @ReactMethod public void open(@Nullable final ReadableMap options, Promise promise) { try { Activity activity = getCurrentActivity(); if (activity == null) { promise.reject(ERROR_NO_ACTIVITY, "Tried to open a DatePicker dialog while not attached to an Activity"); return; } // We want to support both android.app.Activity and the pre-Honeycomb FragmentActivity // (for apps that use it for legacy reasons). This unfortunately leads to some code duplication. if (activity instanceof android.support.v4.app.FragmentActivity) { android.support.v4.app.FragmentManager fragmentManager = ((android.support.v4.app.FragmentActivity) activity) .getSupportFragmentManager(); android.support.v4.app.DialogFragment oldFragment = (android.support.v4.app.DialogFragment) fragmentManager .findFragmentByTag(FRAGMENT_TAG); if (oldFragment != null) { oldFragment.dismiss(); } SupportHijriDatePickerDialogFragment fragment = new SupportHijriDatePickerDialogFragment(); if (options != null) { final Bundle args = createFragmentArguments(options, promise); if (args == null) return; fragment.setArguments(args); } final DatePickerDialogListener listener = new DatePickerDialogListener(promise); fragment.setOnDismissListener(listener); fragment.setOnDateSetListener(listener); fragment.setOnExceptionListener(listener); fragment.show(fragmentManager, FRAGMENT_TAG); } else { FragmentManager fragmentManager = activity.getFragmentManager(); DialogFragment oldFragment = (DialogFragment) fragmentManager.findFragmentByTag(FRAGMENT_TAG); if (oldFragment != null) { oldFragment.dismiss(); } HijriDatePickerDialogFragment fragment = new HijriDatePickerDialogFragment(); if (options != null) { final Bundle args = createFragmentArguments(options, promise); if (args == null) return; fragment.setArguments(args); } final DatePickerDialogListener listener = new DatePickerDialogListener(promise); fragment.setOnDismissListener(listener); fragment.setOnDateSetListener(listener); fragment.setOnExceptionListener(listener); fragment.show(fragmentManager, FRAGMENT_TAG); } } catch (Exception e) { promise.reject(ERROR_OPEN, "Exception happened while executing open method, details: " + e.getMessage()); } }
From source file:com.hijridatepicker.HijriDatePickerAndroidModule.java
License:Open Source License
private Bundle createFragmentArguments(ReadableMap options, Promise promise) { final Bundle args = new Bundle(); try {// w w w . j a v a 2 s . c o m if (options.hasKey(ARG_DATE) && !options.isNull(ARG_DATE)) { if (!parseOptionsWithKey(ARG_DATE, options, args, promise)) return null; } if (options.hasKey(ARG_MINDATE) && !options.isNull(ARG_MINDATE)) { if (!parseOptionsWithKey(ARG_MINDATE, options, args, promise)) return null; } if (options.hasKey(ARG_MAXDATE) && !options.isNull(ARG_MAXDATE)) { if (!parseOptionsWithKey(ARG_MAXDATE, options, args, promise)) return null; } if (options.hasKey(ARG_MODE) && !options.isNull(ARG_MODE)) { args.putString(ARG_MODE, options.getString(ARG_MODE)); } if (options.hasKey(ARG_WEEK_DAY_LABELS) && !options.isNull(ARG_WEEK_DAY_LABELS)) { args.putStringArrayList(ARG_WEEK_DAY_LABELS, toStringArrayList(options.getArray(ARG_WEEK_DAY_LABELS))); } } catch (Exception e) { promise.reject(ERROR_PARSING_OPTIONS, "Exception happened while parsing options, details: " + e.getMessage()); return null; } return args; }
From source file:com.hijridatepicker.HijriDatePickerAndroidModule.java
License:Open Source License
private boolean parseOptionsWithKey(String ARG_KEY, ReadableMap options, Bundle args, Promise promise) { ReadableType argDateType = options.getType(ARG_KEY); if (ReadableType.String.equals(argDateType)) { try {/*from ww w .j a v a2 s .c o m*/ long milliSeconds = 0; milliSeconds = (long) convertHijriDateToGregorianMilliseconds(options.getString(ARG_KEY)); args.putLong(ARG_KEY, milliSeconds); return true; } catch (PatternSyntaxException | IndexOutOfBoundsException | NumberFormatException e) { promise.reject(ERROR_PARSING_OPTIONS, "Exception happened while parsing " + ARG_KEY + " we only accept object of Date or String with the format \"dd-MM-yyyy\" in Hijri"); return false; } } else if (ReadableType.Number.equals(argDateType)) { args.putLong(ARG_KEY, (long) options.getDouble(ARG_KEY)); return true; } else { promise.reject(ERROR_PARSING_OPTIONS, "Exception happened while parsing " + ARG_KEY + " we only accept object of Date or String with the format \"dd-MM-yyyy\" in Hijri"); return false; } }
From source file:com.hijridatepicker.HijriDatePickerAndroidModule.java
License:Open Source License
@ReactMethod public void convertHijriDateStrToGregorianMilliseconds(String hijriDate, Promise promise) { try {/*from w ww . j a v a2s.c o m*/ double milliseconds = convertHijriDateToGregorianMilliseconds(hijriDate); promise.resolve(milliseconds); } catch (Exception e) { promise.reject(ERROR_CONVERT, "Exception while executing convertHijriDateStrToGregorianMilliseconds, Details: " + e.getMessage()); } }
From source file:com.hijridatepicker.HijriDatePickerAndroidModule.java
License:Open Source License
/** * @param milliseconds your gregorian date in milliseconds * @return hijri date at the format of "dd-MM-yyyy" *//*from ww w . j av a2s . com*/ @ReactMethod public void convertMillisecondsToHijriDate(double milliseconds, Promise promise) { try { UmmalquraCalendar ummalquraCalendar = new UmmalquraCalendar(); ummalquraCalendar.setTimeInMillis((long) milliseconds); WritableMap result = new WritableNativeMap(); result.putInt("year", ummalquraCalendar.get(Calendar.YEAR)); result.putInt("month", ummalquraCalendar.get(Calendar.MONTH)); result.putInt("day", ummalquraCalendar.get(Calendar.DAY_OF_MONTH)); promise.resolve(result); } catch (Exception e) { promise.reject(ERROR_CONVERT, "Exception while executing convertMillisecondsToHijriDate, Details: " + e.getMessage()); } }
From source file:com.jetbridge.reactobd2.ReactNativeOBD2Module.java
License:Open Source License
@ReactMethod public void getBluetoothDeviceName(Promise aPromise) { if (mOBD2Handler == null) { mOBD2Handler = new OBD2Handler(mReactContext); }//from w ww . j a va2s . c o m try { Set<BluetoothDevice> pairedDevices = mOBD2Handler.getBondedDevices(); WritableArray deviceList = mArguments.createArray(); if (pairedDevices.size() > 0) { for (BluetoothDevice device : pairedDevices) { WritableMap map = mArguments.createMap(); map.putString("name", device.getName()); map.putString("address", device.getAddress()); deviceList.pushMap(map); } } aPromise.resolve(deviceList); } catch (IOException e) { e.printStackTrace(); aPromise.reject(TAG, e); } }
From source file:com.microsoft.appcenter.reactnative.auth.AppCenterReactNativeAuthModule.java
License:Open Source License
@ReactMethod public void signIn(final Promise promise) { Auth.signIn().thenAccept(new AppCenterConsumer<SignInResult>() { @Override/*w w w .ja v a 2s .c o m*/ public void accept(SignInResult signInResult) { WritableMap writableMap = new WritableNativeMap(); if (signInResult.getException() == null) { /* Sign-in succeeded, convert Java result to a JavaScript result. */ UserInformation userInformation = signInResult.getUserInformation(); writableMap.putString(ACCESS_TOKEN_KEY, userInformation.getAccessToken()); writableMap.putString(ACCOUNT_ID_KEY, userInformation.getAccountId()); writableMap.putString(ID_TOKEN_KEY, userInformation.getIdToken()); promise.resolve(writableMap); } else { Exception signInFailureException = signInResult.getException(); promise.reject("Sign-in failed", signInFailureException); } } }); }
From source file:com.shahenlibrary.Trimmer.Trimmer.java
License:Open Source License
static File createTempFile(String extension, final Promise promise, Context ctx) { UUID uuid = UUID.randomUUID(); String imageName = uuid.toString() + "-screenshot"; File cacheDir = ctx.getCacheDir(); File tempFile = null;/* w w w. j a v a 2 s.c o m*/ try { tempFile = File.createTempFile(imageName, "." + extension, cacheDir); } catch (IOException e) { promise.reject("Failed to create temp file", e.toString()); return null; } if (tempFile.exists()) { tempFile.delete(); } return tempFile; }