/** * Executes the request and returns PluginResult. * * @param action The action to execute. * @param args JSONArry of arguments for the plugin. * @param callbackId The callback id used when calling back into JavaScript. * @return A PluginResult object with a status and message. */ public PluginResult execute(String action, JSONArray args, String callbackId) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; this.callbackId = callbackId; try { if (action.equals("takePicture")) { int destType = DATA_URL; if (args.length() > 1) { destType = args.getInt(1); } int srcType = CAMERA; if (args.length() > 2) { srcType = args.getInt(2); } if (srcType == CAMERA) { this.takePicture(args.getInt(0), destType); } else if ((srcType == PHOTOLIBRARY) || (srcType == SAVEDPHOTOALBUM)) { this.getImage(srcType, destType); } PluginResult r = new PluginResult(PluginResult.Status.NO_RESULT); r.setKeepCallback(true); return r; } return new PluginResult(status, result); } catch (JSONException e) { e.printStackTrace(); return new PluginResult(PluginResult.Status.JSON_EXCEPTION); } }
/** * Native implementation for "authenticate" action * * @param args The arguments used for authentication. * @param callbackId The callback ID used when calling back into Javascript. * @return NO_RESULT since authentication is asynchronous. * @throws JSONException */ protected PluginResult authenticate(JSONArray args, final String callbackId) throws JSONException { Log.i("SalesforceOAuthPlugin.authenticate", "authenticate called"); JSONObject oauthProperties = new JSONObject((String) args.get(0)); LoginOptions loginOptions = parseLoginOptions(oauthProperties); clientManager = new ClientManager(ctx, ForceApp.APP.getAccountType(), loginOptions); autoRefresh = oauthProperties.getBoolean("autoRefreshOnForeground"); clientManager.getRestClient( ctx, new RestClientCallback() { @Override public void authenticatedRestClient(RestClient c) { if (c == null) { Log.w("SalesforceOAuthPlugin.authenticate", "authenticate failed - logging out"); logout(ctx); } else { Log.i("SalesforceOAuthPlugin.authenticate", "authenticate successful"); // Only updating time if we went through login if (SalesforceOAuthPlugin.client == null) { updateRefreshTime(); } SalesforceOAuthPlugin.client = c; callAuthenticateSuccess(callbackId); } } }); // Done PluginResult noop = new PluginResult(PluginResult.Status.NO_RESULT); noop.setKeepCallback(true); return noop; }
@Override public PluginResult execute(String action, JSONArray args, String callbackId) { this.callbackId = callbackId; this.limit = 1; this.duration = 0.0f; this.results = new JSONArray(); JSONObject options = args.optJSONObject(0); if (options != null) { limit = options.optLong("limit", 1); duration = options.optDouble("duration", 0.0f); } if (action.equals("getFormatData")) { try { JSONObject obj = getFormatData(args.getString(0), args.getString(1)); return new PluginResult(PluginResult.Status.OK, obj); } catch (JSONException e) { return new PluginResult(PluginResult.Status.ERROR); } } else if (action.equals("captureAudio")) { this.captureAudio(); } else if (action.equals("captureImage")) { this.captureImage(); } else if (action.equals("captureVideo")) { this.captureVideo(duration); } PluginResult r = new PluginResult(PluginResult.Status.NO_RESULT); r.setKeepCallback(true); return r; }
@Override public PluginResult execute(String action, JSONArray args, String callbackId) { this.callback = callbackId; if (action.equals("load")) { load(args); } else if (action.equals("fetch")) { fetch(args); } else if (action.equals("remove")) { remove(args); } else { return new PluginResult(PluginResult.Status.INVALID_ACTION); } PluginResult r = new PluginResult(PluginResult.Status.NO_RESULT); r.setKeepCallback(true); return r; }
@Override public PluginResult execute(String action, JSONArray args, final String callbackId) { PluginResult pr = new PluginResult(PluginResult.Status.NO_RESULT); pr.setKeepCallback(true); if (action.equals("init")) { try { facebook = new Facebook(args.getString(0)); Log.d("PhoneGapLog", "reauthorize"); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.ctx); String access_token = prefs.getString("access_token", null); Long expires = prefs.getLong("access_expires", -1); if (access_token != null && expires != -1) { this.facebook.setAccessToken(access_token); this.facebook.setAccessExpires(expires); try { JSONObject o = new JSONObject(this.facebook.request("/me")); this.userId = o.getString("id"); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (facebook.isSessionValid() && this.userId != null) { return new PluginResult(PluginResult.Status.OK, this.getResponse()); } else { return new PluginResult(PluginResult.Status.NO_RESULT); } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); return new PluginResult( PluginResult.Status.ERROR, "Invalid JSON args used. expected a string as the first arg."); } } else if (action.equals("login")) { if (facebook != null) { if (facebook.isSessionValid()) { Log.d("FB", "Session already valid"); pr = new PluginResult(PluginResult.Status.OK, getResponse()); } else { final ConnectPlugin me = this; String[] permissions = new String[args.length()]; try { for (int i = 0; i < args.length(); i++) { permissions[i] = args.getString(i); } } catch (JSONException e1) { // TODO Auto-generated catch block e1.printStackTrace(); return new PluginResult( PluginResult.Status.ERROR, "Invalid JSON args used. Expected a string array of permissions."); } this.ctx.setActivityResultCallback(this); this.permissions = permissions; this.callbackId = callbackId; Runnable runnable = new Runnable() { public void run() { me.facebook.authorize( (Activity) me.ctx, me.permissions, new AuthorizeListener(me)); }; }; this.ctx.runOnUiThread(runnable); } } else { pr = new PluginResult(PluginResult.Status.ERROR, "Must call FB.init before FB.login"); } } else if (action.equals("logout")) { if (facebook != null) { try { facebook.logout(this.ctx); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.ctx); prefs.edit().putLong("access_expires", -1).commit(); prefs.edit().putString("access_token", null).commit(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); pr = new PluginResult(PluginResult.Status.MALFORMED_URL_EXCEPTION, "Error logging out."); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); pr = new PluginResult(PluginResult.Status.IO_EXCEPTION, "Error logging out."); } pr = new PluginResult(PluginResult.Status.OK, getResponse()); } } else if (action.equals("getLoginStatus")) { if (facebook != null) { pr = new PluginResult(PluginResult.Status.OK, getResponse()); } } return pr; }