下面列出了org.apache.cordova.CallbackContext#error ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private boolean setColorEffect(String effect, CallbackContext callbackContext) {
if(this.hasCamera(callbackContext) == false){
return true;
}
Camera camera = fragment.getCamera();
Camera.Parameters params = camera.getParameters();
List<String> supportedColors;
supportedColors = params.getSupportedColorEffects();
if(supportedColors.contains(effect)){
params.setColorEffect(effect);
fragment.setCameraParameters(params);
callbackContext.success(effect);
}else{
callbackContext.error("Color effect not supported" + effect);
return true;
}
return true;
}
boolean disconnectBT(CallbackContext callbackContext) throws IOException {
try {
stopWorker = true;
mmOutputStream.close();
mmInputStream.close();
mmSocket.close();
callbackContext.success("BLUETOOTH DISCONNECT");
return true;
} catch (Exception e) {
String errMsg = e.getMessage();
Log.e(LOG_TAG, errMsg);
e.printStackTrace();
callbackContext.error(errMsg);
}
return false;
}
private boolean getExposureCompensation(CallbackContext callbackContext) {
if(this.hasCamera(callbackContext) == false){
return true;
}
Camera camera = fragment.getCamera();
Camera.Parameters params = camera.getParameters();
if (camera.getParameters().getMinExposureCompensation() == 0 && camera.getParameters().getMaxExposureCompensation() == 0) {
callbackContext.error("Exposure corection not supported");
} else {
int exposureCompensation = camera.getParameters().getExposureCompensation();
callbackContext.success(exposureCompensation);
}
return true;
}
private boolean removeField(int fieldIndex, CallbackContext callbackContext) {
if (ReaderActivity.pdfViewCtrl == null || ReaderActivity.pdfViewCtrl.getDoc() == null) {
callbackContext.error("Please open document first.");
return false;
}
PDFDoc pdfDoc = ReaderActivity.pdfViewCtrl.getDoc();
try {
if (!pdfDoc.hasForm()) {
callbackContext.error("The current document does not have interactive form.");
return false;
}
Form form = new Form(pdfDoc);
Field field = form.getField(fieldIndex, null);
form.removeField(field);
((UIExtensionsManager) ReaderActivity.pdfViewCtrl.getUIExtensionsManager()).getDocumentManager().setDocModified(true);
callbackContext.success("Succeed to remove field.");
return true;
} catch (PDFException e) {
callbackContext.error(e.getMessage() + ", Error code = " + e.getLastError());
}
return false;
}
/**
* Executes the request and returns PluginResult
*
* @param action
* @param args
* @param callbackContext
* @return boolean
* @throws JSONException
*/
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
/**
* appVersion
*/
if (action.equals("getAppVersion")) {
try {
PackageManager packageManager = this.cordova.getActivity().getPackageManager();
JSONObject r = new JSONObject();
r.put("version", packageManager.getPackageInfo(this.cordova.getActivity().getPackageName(), 0).versionName);
r.put("build", packageManager.getPackageInfo(this.cordova.getActivity().getPackageName(), 0).versionCode);
callbackContext.success(r);
} catch (NameNotFoundException e) {
callbackContext.error("Exception thrown");
}
return true;
}
// Default response to say the action hasn't been handled
return false;
}
private boolean hasView(CallbackContext callbackContext) {
if(fragment == null) {
callbackContext.error("No preview");
return false;
}
return true;
}
private void onDataReceived(final CordovaArgs args,
final CallbackContext callbackContext) throws JSONException {
Log.d(TAG, "onDataReceived");
String connectionId = args.getString(0);
WearConnection connection = connections.get(connectionId);
if (connection != null) {
connection.addDataListener(callbackContext);
} else {
callbackContext.error("Invalid connection handle");
}
}
@Override
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
try {
if (ACTION_KEEP_AWAKE.equals(action)) {
cordova.getActivity().runOnUiThread(
new Runnable() {
public void run() {
cordova.getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK));
}
});
return true;
} else if (ACTION_ALLOW_SLEEP_AGAIN.equals(action)) {
cordova.getActivity().runOnUiThread(
new Runnable() {
public void run() {
cordova.getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK));
}
});
return true;
} else {
callbackContext.error("insomnia." + action + " is not a supported function. Did you mean '" + ACTION_KEEP_AWAKE + "'?");
return false;
}
} catch (Exception e) {
callbackContext.error(e.getMessage());
return false;
}
}
/**
* Convenient method to send plugin errors.
*
* @param callbackContext The callback context used when calling back into JavaScript.
* @param error The error code to return
* @param message The error message to return
*/
private void sendPluginError(CallbackContext callbackContext, PluginError error, String message) {
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("code", error.ordinal());
jsonObject.put("message", message);
} catch (JSONException e) {
e.printStackTrace();
}
callbackContext.error(jsonObject);
}
private boolean importFromFDF(String fdfPath, int type, com.foxit.sdk.common.Range range, CallbackContext callbackContext) {
if (fdfPath == null || fdfPath.trim().length() < 1) {
callbackContext.error("Please input validate path.");
return false;
}
if (ReaderActivity.pdfViewCtrl == null) {
callbackContext.error("Please open document first.");
return false;
}
try {
PDFViewCtrl.lock();
boolean success = false;
if (ReaderActivity.pdfViewCtrl.getDoc() != null) {
FDFDoc fdfDoc = new FDFDoc(fdfPath);
success = ReaderActivity.pdfViewCtrl.getDoc().importFromFDF(fdfDoc, type, range);
if (success) {
((UIExtensionsManager) ReaderActivity.pdfViewCtrl.getUIExtensionsManager()).getDocumentManager().setDocModified(true);
int pageIndex = ReaderActivity.pdfViewCtrl.getCurrentPage();
Rect rect = new Rect(0, 0, ReaderActivity.pdfViewCtrl.getPageViewWidth(pageIndex), ReaderActivity.pdfViewCtrl.getPageViewHeight(pageIndex));
ReaderActivity.pdfViewCtrl.refresh(pageIndex, rect);
callbackContext.success();
return true;
}
}
callbackContext.error("Unknown error");
} catch (PDFException e) {
callbackContext.error(e.getMessage());
} finally {
PDFViewCtrl.unlock();
}
return false;
}
private boolean resetField(int fieldIndex, CallbackContext callbackContext) {
if (ReaderActivity.pdfViewCtrl == null || ReaderActivity.pdfViewCtrl.getDoc() == null) {
callbackContext.error("Please open document first.");
return false;
}
PDFDoc pdfDoc = ReaderActivity.pdfViewCtrl.getDoc();
try {
if (!pdfDoc.hasForm()) {
callbackContext.error("The current document does not have interactive form.");
return false;
}
Form form = new Form(pdfDoc);
Field field = form.getField(fieldIndex, null);
boolean ret = field.reset();
((UIExtensionsManager) ReaderActivity.pdfViewCtrl.getUIExtensionsManager()).getDocumentManager().setDocModified(ret);
if (ret) {
callbackContext.success("Succeed to reset the specified form field.");
} else {
callbackContext.error("Unknown error.");
}
return ret;
} catch (PDFException e) {
callbackContext.error(e.getMessage() + ", Error code = " + e.getLastError());
}
return false;
}
private boolean routeScreenOrientation(JSONArray args, CallbackContext callbackContext) {
String action = args.optString(0);
if (action.equals("set")) {
String orientation = args.optString(1);
Log.d(TAG, "Requested ScreenOrientation: " + orientation);
Activity activity = cordova.getActivity();
if (orientation.equals(UNLOCKED)) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
} else if (orientation.equals(LANDSCAPE_PRIMARY)) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
} else if (orientation.equals(PORTRAIT_PRIMARY)) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else if (orientation.equals(LANDSCAPE)) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
} else if (orientation.equals(PORTRAIT)) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
} else if (orientation.equals(LANDSCAPE_SECONDARY)) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
} else if (orientation.equals(PORTRAIT_SECONDARY)) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
}
callbackContext.success();
return true;
} else {
callbackContext.error("ScreenOrientation not recognised");
return false;
}
}
private boolean execIsPendingUpdate(CordovaArgs args, CallbackContext callbackContext) {
try {
InstallOptions pendingInstall = this.codePushPackageManager.getPendingInstall();
callbackContext.success((pendingInstall != null) ? 1 : 0);
} catch (Exception e) {
callbackContext.error("An error occurred. " + e.getMessage());
}
return true;
}
@CordovaMethod
private void setBadge(int value, CallbackContext callbackContext) {
if (value >= 0) {
Context context = cordova.getActivity().getApplicationContext();
ShortcutBadger.applyCount(context, value);
callbackContext.success();
} else {
callbackContext.error("Badge value can't be negative");
}
}
private void checkAvailability(String uri, CallbackContext callbackContext) {
if(appInstalled(uri)) {
callbackContext.success();
}
else {
callbackContext.error("");
}
}
/**
* Executes the request and returns PluginResult
*
* @param action
* @param args
* @param callbackContext
* @return boolean
* @throws JSONException
*/
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
/**
* appVersion
*/
if (action.equals("getAppVersion")) {
try {
PackageManager packageManager = this.cordova.getActivity().getPackageManager();
JSONObject r = new JSONObject();
r.put("version", packageManager.getPackageInfo(this.cordova.getActivity().getPackageName(), 0).versionName);
r.put("build", packageManager.getPackageInfo(this.cordova.getActivity().getPackageName(), 0).versionCode);
callbackContext.success(r);
} catch (NameNotFoundException e) {
callbackContext.error("Exception thrown");
}
return true;
}
// Default response to say the action hasn't been handled
return false;
}
private boolean removeControl(int pageIndex, int controlIndex, CallbackContext callbackContext) {
if (ReaderActivity.pdfViewCtrl == null || ReaderActivity.pdfViewCtrl.getDoc() == null) {
callbackContext.error("Please open document first.");
return false;
}
PDFDoc pdfDoc = ReaderActivity.pdfViewCtrl.getDoc();
try {
if (!pdfDoc.hasForm()) {
callbackContext.error("The current document does not have interactive form.");
return false;
}
Form form = new Form(pdfDoc);
PDFPage page = pdfDoc.getPage(pageIndex);
if (!page.isParsed()) {
page.startParse(PDFPage.e_ParsePageNormal, null, false);
}
form.removeControl(form.getControl(page, controlIndex));
((UIExtensionsManager) ReaderActivity.pdfViewCtrl.getUIExtensionsManager()).getDocumentManager().setDocModified(true);
callbackContext.success("Succeed to remove the specified control.");
return true;
} catch (PDFException e) {
callbackContext.error(e.getMessage() + ", Error code = " + e.getLastError());
}
return false;
}
@Override
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
if (ACTION_SHOW_EVENT.equals(action)) {
if (this.isPaused) {
return true;
}
final String message = args.getString(0);
final String duration = args.getString(1);
final String position = args.getString(2);
cordova.getActivity().runOnUiThread(new Runnable() {
public void run() {
android.widget.Toast toast = android.widget.Toast.makeText(webView.getContext(), message, 0);
if ("top".equals(position)) {
toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL, 0, 20);
} else if ("bottom".equals(position)) {
toast.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 20);
} else if ("center".equals(position)) {
toast.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL, 0, 0);
} else {
callbackContext.error("invalid position. valid options are 'top', 'center' and 'bottom'");
return;
}
if ("short".equals(duration)) {
toast.setDuration(android.widget.Toast.LENGTH_SHORT);
} else if ("long".equals(duration)) {
toast.setDuration(android.widget.Toast.LENGTH_LONG);
} else {
callbackContext.error("invalid duration. valid options are 'short' and 'long'");
return;
}
toast.show();
mostRecentToast = toast;
callbackContext.success();
}
});
return true;
} else {
callbackContext.error("toast." + action + " is not a supported function. Did you mean '" + ACTION_SHOW_EVENT + "'?");
return false;
}
}
public boolean execute(String action, JSONArray data, CallbackContext callbackContext) {
Activity activity = this.cordova.getActivity();
Boolean result = false;
updateServiceIntent = new Intent(activity, BackgroundLocationUpdateService.class);
if (ACTION_START.equalsIgnoreCase(action) && !isEnabled) {
result = true;
updateServiceIntent.putExtra("desiredAccuracy", desiredAccuracy);
updateServiceIntent.putExtra("distanceFilter", distanceFilter);
updateServiceIntent.putExtra("desiredAccuracy", desiredAccuracy);
updateServiceIntent.putExtra("isDebugging", isDebugging);
updateServiceIntent.putExtra("notificationTitle", notificationTitle);
updateServiceIntent.putExtra("notificationText", notificationText);
updateServiceIntent.putExtra("interval", interval);
updateServiceIntent.putExtra("fastestInterval", fastestInterval);
updateServiceIntent.putExtra("aggressiveInterval", aggressiveInterval);
updateServiceIntent.putExtra("activitiesInterval", activitiesInterval);
updateServiceIntent.putExtra("useActivityDetection", useActivityDetection);
if (hasPermisssion()) {
isServiceBound = bindServiceToWebview(activity, updateServiceIntent);
isEnabled = true;
callbackContext.success();
} else {
startCallback = callbackContext;
PermissionHelper.requestPermissions(this, START_REQ_CODE, permissions);
}
} else if (ACTION_STOP.equalsIgnoreCase(action)) {
isEnabled = false;
result = true;
activity.stopService(updateServiceIntent);
callbackContext.success();
result = unbindServiceFromWebview(activity, updateServiceIntent);
if(result) {
callbackContext.success();
} else {
callbackContext.error("Failed To Stop The Service");
}
} else if (ACTION_CONFIGURE.equalsIgnoreCase(action)) {
result = true;
try {
// [distanceFilter, desiredAccuracy, interval, fastestInterval, aggressiveInterval, debug, notificationTitle, notificationText, activityType, fences, url, params, headers]
// 0 1 2 3 4 5 6 7 8 9
this.distanceFilter = data.getString(0);
this.desiredAccuracy = data.getString(1);
this.interval = data.getString(2);
this.fastestInterval = data.getString(3);
this.aggressiveInterval = data.getString(4);
this.isDebugging = data.getString(5);
this.notificationTitle = data.getString(6);
this.notificationText = data.getString(7);
//this.activityType = data.getString(8);
this.useActivityDetection = data.getString(9);
this.activitiesInterval = data.getString(10);
} catch (JSONException e) {
Log.d(TAG, "Json Exception" + e);
callbackContext.error("JSON Exception" + e.getMessage());
}
} else if (ACTION_SET_CONFIG.equalsIgnoreCase(action)) {
result = true;
// TODO reconfigure Service
callbackContext.success();
} else if(ACTION_GET_VERSION.equalsIgnoreCase(action)) {
result = true;
callbackContext.success(PLUGIN_VERSION);
} else if(ACTION_REGISTER_FOR_LOCATION_UPDATES.equalsIgnoreCase(action)) {
result = true;
//Register the function for repeated location update
locationUpdateCallback = callbackContext;
} else if(ACTION_REGISTER_FOR_ACTIVITY_UPDATES.equalsIgnoreCase(action)) {
result = true;
detectedActivitiesCallback = callbackContext;
} else if(ACTION_AGGRESSIVE_TRACKING.equalsIgnoreCase(action)) {
result = true;
if(isEnabled) {
this.cordova.getActivity().sendBroadcast(new Intent(Constants.CHANGE_AGGRESSIVE));
callbackContext.success();
} else {
callbackContext.error("Tracking not enabled, need to start tracking before starting aggressive tracking");
}
}
return result;
}
private boolean executeAndPossiblyThrow(Action action, JSONArray args, CallbackContext cbc)
throws JSONException {
boolean status = true;
JSONObject o;
String echo_value;
String dbname;
switch (action) {
case echoStringValue:
o = args.getJSONObject(0);
echo_value = o.getString("value");
cbc.success(echo_value);
break;
case open:
o = args.getJSONObject(0);
dbname = o.getString("name");
// open database and start reading its queue
this.startDatabase(dbname, o, cbc);
break;
case close:
o = args.getJSONObject(0);
dbname = o.getString("path");
// put request in the q to close the db
this.closeDatabase(dbname, cbc);
break;
case delete:
o = args.getJSONObject(0);
dbname = o.getString("path");
deleteDatabase(dbname, cbc);
break;
case executeSqlBatch:
case backgroundExecuteSqlBatch:
JSONObject allargs = args.getJSONObject(0);
JSONObject dbargs = allargs.getJSONObject("dbargs");
dbname = dbargs.getString("dbname");
JSONArray txargs = allargs.getJSONArray("executes");
if (txargs.isNull(0)) {
cbc.error("missing executes list");
} else {
int len = txargs.length();
String[] queries = new String[len];
JSONArray[] jsonparams = new JSONArray[len];
for (int i = 0; i < len; i++) {
JSONObject a = txargs.getJSONObject(i);
queries[i] = a.getString("sql");
jsonparams[i] = a.getJSONArray("params");
}
// put db query in the queue to be executed in the db thread:
DBQuery q = new DBQuery(queries, jsonparams, cbc);
DBRunner r = dbrmap.get(dbname);
if (r != null) {
try {
r.q.put(q);
} catch(Exception e) {
Log.e(SQLitePlugin.class.getSimpleName(), "couldn't add to queue", e);
cbc.error("couldn't add to queue");
}
} else {
cbc.error("database not open");
}
}
break;
}
return status;
}