List of usage examples for org.json JSONArray getLong
public long getLong(int index) throws JSONException
From source file:org.sufficientlysecure.privacybox.VaultProvider.java
/** * Recursively delete the given document and any children under it. */// w ww . ja va 2 s . co m private void deleteDocumentTree(long docId) throws IOException, GeneralSecurityException { final EncryptedDocument doc = getDocument(docId); final JSONObject meta = doc.readMetadata(); try { if (Document.MIME_TYPE_DIR.equals(meta.getString(Document.COLUMN_MIME_TYPE))) { final JSONArray children = meta.getJSONArray(KEY_CHILDREN); for (int i = 0; i < children.length(); i++) { final long childDocId = children.getLong(i); deleteDocumentTree(childDocId); } } } catch (JSONException e) { throw new IOException(e); } if (!doc.getFile().delete()) { throw new IOException("Failed to delete " + docId); } }
From source file:org.sufficientlysecure.privacybox.VaultProvider.java
@Override public Cursor queryChildDocuments(String parentDocumentId, String[] projection, String sortOrder) throws FileNotFoundException { final ExtrasMatrixCursor result = new ExtrasMatrixCursor(resolveDocumentProjection(projection)); result.setNotificationUri(getContext().getContentResolver(), DocumentsContract.buildChildDocumentsUri(AUTHORITY, parentDocumentId)); // TODO: Extra inf below? not for special root folder! // result.putString(DocumentsContract.EXTRA_INFO, "bla"); try {/*from w ww. ja v a 2 s . c o m*/ final EncryptedDocument doc = getDocument(Long.parseLong(parentDocumentId)); final JSONObject meta = doc.readMetadata(); final JSONArray children = meta.getJSONArray(KEY_CHILDREN); for (int i = 0; i < children.length(); i++) { final long docId = children.getLong(i); includeDocument(result, docId); } } catch (IOException e) { throw new IllegalStateException(e); } catch (GeneralSecurityException e) { throw new IllegalStateException(e); } catch (JSONException e) { throw new IllegalStateException(e); } return result; }
From source file:org.sufficientlysecure.privacybox.VaultProvider.java
/** * Maybe remove the given value from a {@link JSONArray}. * * @return if the array was mutated./*from w ww .ja v a 2 s .co m*/ */ private static boolean maybeRemove(JSONArray array, long value) throws JSONException { boolean mutated = false; int i = 0; while (i < array.length()) { if (value == array.getLong(i)) { array.remove(i); mutated = true; } else { i++; } } return mutated; }
From source file:com.suarez.cordova.mapsforge.MapsforgePlugin.java
@Override public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { if ("global-status".equals(action)) { this.status(); callbackContext.success();/* ww w. j a va 2 s . com*/ return true; } else if (action.contains("native-")) { if ("native-set-center".equals(action)) { try { MapsforgeNative.INSTANCE.setCenter(args.getDouble(0), args.getDouble(1)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-set-zoom".equals(action)) { try { MapsforgeNative.INSTANCE.setZoom(Byte.parseByte(args.getString(0))); callbackContext.success(); } catch (NumberFormatException nfe) { callbackContext.error("Incorrect argument format. Should be: (byte zoom)"); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-show".equals(action)) { try { MapsforgeNative.INSTANCE.show(); callbackContext.success(); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-hide".equals(action)) { MapsforgeNative.INSTANCE.hide(); return true; } else if ("native-marker".equals(action)) { try { Activity context = this.cordova.getActivity(); int markerId = context.getResources().getIdentifier(args.getString(0), "drawable", context.getPackageName()); if (markerId == 0) { Log.i(MapsforgePlugin.TAG, "Marker not found...using default marker: marker_green"); markerId = context.getResources().getIdentifier("marker_green", "drawable", context.getPackageName()); } int markerKey = MapsforgeNative.INSTANCE.addMarker(markerId, args.getDouble(1), args.getDouble(2)); callbackContext.success(markerKey); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-polyline".equals(action)) { try { JSONArray points = args.getJSONArray(2); if (points.length() % 2 != 0) throw new JSONException("Invalid array of coordinates. Length should be multiple of 2"); int polylineKey = MapsforgeNative.INSTANCE.addPolyline(args.getInt(0), args.getInt(1), points); callbackContext.success(polylineKey); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-delete-layer".equals(action)) { try { MapsforgeNative.INSTANCE.deleteLayer(args.getInt(0)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-initialize".equals(action)) { try { MapsforgeNative.createInstance(this.cordova.getActivity(), args.getString(0), args.getInt(1), args.getInt(2)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (IllegalArgumentException e) { callbackContext.error(e.getMessage()); } catch (IOException e) { callbackContext.error(e.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-set-max-zoom".equals(action)) { try { MapsforgeNative.INSTANCE.setMaxZoom(Byte.parseByte(args.getString(0))); callbackContext.success(); } catch (NumberFormatException nfe) { callbackContext.error("Incorrect argument format. Should be: (byte zoom)"); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-set-min-zoom".equals(action)) { try { MapsforgeNative.INSTANCE.setMinZoom(Byte.parseByte(args.getString(0))); callbackContext.success(); } catch (NumberFormatException nfe) { callbackContext.error("Incorrect argument format. Should be: (byte zoom)"); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-show-controls".equals(action)) { try { MapsforgeNative.INSTANCE.setBuiltInZoomControls(args.getBoolean(0)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-clickable".equals(action)) { try { MapsforgeNative.INSTANCE.setClickable(args.getBoolean(0)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-show-scale".equals(action)) { try { MapsforgeNative.INSTANCE.showScaleBar(args.getBoolean(0)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-destroy-cache".equals(action)) { try { MapsforgeNative.INSTANCE.destroyCache(args.getBoolean(0)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } return true; } else if ("native-map-path".equals(action)) { try { MapsforgeNative.INSTANCE.setMapFilePath(args.getString(0)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (IllegalArgumentException iae) { callbackContext.error(iae.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-cache-name".equals(action)) { try { MapsforgeNative.INSTANCE.setCacheName(args.getString(0)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-theme-path".equals(action)) { try { MapsforgeNative.INSTANCE.setRenderThemePath(args.getString(0)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (IllegalArgumentException e) { callbackContext.error(e.getMessage()); } catch (IOException e) { callbackContext.error(e.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-stop".equals(action)) { try { MapsforgeNative.INSTANCE.onStop(); callbackContext.success(); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-start".equals(action)) { try { MapsforgeNative.INSTANCE.onStart(); callbackContext.success(); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-destroy".equals(action)) { try { MapsforgeNative.INSTANCE.onDestroy(); callbackContext.success(); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-online".equals(action)) { try { MapsforgeNative.INSTANCE.setOnline(args.getString(0), args.getString(1), args.getString(2), args.getString(3), args.getInt(4)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-offline".equals(action)) { try { MapsforgeNative.INSTANCE.setOffline(args.getString(0), args.getString(1)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (IllegalArgumentException e) { callbackContext.error(e.getMessage()); } catch (IOException e) { callbackContext.error(e.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } } else if (action.contains("cache-")) { if ("cache-get-tile".equals(action)) { try { final long x = args.getLong(0); final long y = args.getLong(1); final byte z = Byte.parseByte(args.getString(2)); final CallbackContext callbacks = callbackContext; cordova.getThreadPool().execute(new Runnable() { public void run() { try { String path = MapsforgeCache.INSTANCE.getTilePath(x, y, z); callbacks.success(path); } catch (IOException e) { callbacks.error(e.getMessage()); } catch (Exception e) { callbacks.error(e.getMessage()); } } }); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (NumberFormatException nfe) { callbackContext.error(nfe.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("cache-initialize".equals(action)) { try { MapsforgeCache.createInstance(cordova.getActivity(), args.getString(0)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (IllegalArgumentException e) { callbackContext.error(e.getMessage()); } catch (IOException e) { callbackContext.error(e.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("cache-map-path".equals(action)) { try { final String mapFile = args.getString(0); final CallbackContext callbacks = callbackContext; cordova.getThreadPool().execute(new Runnable() { @Override public void run() { try { MapsforgeCache.INSTANCE.setMapFilePath(mapFile); callbacks.success(); } catch (IllegalArgumentException e) { callbacks.error(e.getMessage()); } catch (FileNotFoundException e) { callbacks.error(e.getMessage()); } catch (Exception e) { callbacks.error(e.getMessage()); } } }); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("cache-max-size".equals(action)) { try { MapsforgeCache.INSTANCE.setMaxCacheSize(args.getInt(0)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("cache-max-age".equals(action)) { try { MapsforgeCache.INSTANCE.setMaxCacheAge(args.getLong(0)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("cache-cleaning-trigger".equals(action)) { try { MapsforgeCache.INSTANCE.setCleanCacheTrigger(args.getInt(0)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("cache-enabled".equals(action)) { try { MapsforgeCache.INSTANCE.setCacheEnabled(args.getBoolean(0)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("cache-external".equals(action)) { try { MapsforgeCache.INSTANCE.setExternalCache(args.getBoolean(0)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("cache-name".equals(action)) { try { MapsforgeCache.INSTANCE.setCacheName(args.getString(0)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("cache-tile-size".equals(action)) { try { MapsforgeCache.INSTANCE.setTileSize(args.getInt(0)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("cache-clean-destroy".equals(action)) { try { MapsforgeCache.INSTANCE.setCleanOnDestroy(args.getBoolean(0)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("cache-theme-path".equals(action)) { try { final CallbackContext callbacks = callbackContext; final String themePath = args.getString(0); cordova.getThreadPool().execute(new Runnable() { @Override public void run() { try { MapsforgeCache.INSTANCE.setRenderTheme(themePath); callbacks.success(); } catch (IllegalArgumentException e) { callbacks.error(e.getMessage()); } catch (FileNotFoundException e) { callbacks.error(e.getMessage()); } catch (Exception e) { callbacks.error(e.getMessage()); } } }); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("cache-screen-ratio".equals(action)) { try { MapsforgeCache.INSTANCE.setScreenRatio(Float.parseFloat(args.getString(0))); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (NumberFormatException nfe) { callbackContext.error(nfe.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("cache-overdraw".equals(action)) { try { MapsforgeCache.INSTANCE.setOverdrawFactor(Float.parseFloat(args.getString(0))); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (NumberFormatException nfe) { callbackContext.error(nfe.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("cache-destroy".equals(action)) { try { MapsforgeCache.INSTANCE.onDestroy(); callbackContext.success(); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } } return false; // Returning false results in a "MethodNotFound" error. }
From source file:com.yoncabt.ebr.util.ResultSetDeserializer.java
public List<Object[]> getData() { List<Object[]> ret = new ArrayList<>(); JSONArray arr = jo.getJSONArray("values"); for (int i = 0; i < arr.length(); i++) { List<Object> column = new ArrayList<>(); JSONArray row = arr.getJSONArray(i); for (int j = 0; j < types.size(); j++) { if (row.isNull(j)) { column.add(null);/*from w ww. j a va 2 s . c om*/ } else { switch (types.get(j)) { case DATE: column.add(new Date(row.getLong(j))); break; case STRING: column.add(row.getString(j)); break; case INTEGER: column.add(row.getInt(j)); break; case LONG: column.add(row.getLong(j)); break; case DOUBLE: column.add(row.getDouble(j)); break; default: throw new AssertionError(); } } } ret.add(column.toArray()); } return ret; }
From source file:com.rasrin.locale.formatter.plugin.LocaleFormatter.java
@Override public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { try {//from www .j a va 2 s .com if (action.equals("formatCurrency")) { if (args.length() < 3) { callbackContext.error("Expected 3 arguments."); return false; } String ilocale = args.getString(0); long amount = args.getLong(1); boolean debug = args.getBoolean(2); Locale locale = new Locale("en", "US"); if (ilocale != null && ilocale.length() <= 5) { if (ilocale.indexOf('-') > 0) { String[] parts = ilocale.split("-"); locale = new Locale(parts[0], parts[1]); } } NumberFormat formatter = NumberFormat.getCurrencyInstance(locale); String moneyString = formatter.format(amount); if (debug) { System.out.println("Called with locale: " + ilocale + " & amount: " + amount + ". Formatted string is: " + moneyString); } callbackContext.success(moneyString); return true; } else { callbackContext.error("Unsupported operation"); return false; } } catch (Exception ex) { callbackContext.error("Something went wrong: " + ex.getMessage()); return false; } }
From source file:com.liferay.mobile.android.v7.staging.StagingService.java
public Long createStagingRequest(long groupId, String checksum) throws Exception { JSONObject _command = new JSONObject(); try {//w w w . j av a 2s . c om JSONObject _params = new JSONObject(); _params.put("groupId", groupId); _params.put("checksum", checkNull(checksum)); _command.put("/staging/create-staging-request", _params); } catch (JSONException _je) { throw new Exception(_je); } JSONArray _result = session.invoke(_command); if (_result == null) { return null; } return _result.getLong(0); }
From source file:com.dubsar_dictionary.Dubsar.model.DailyWord.java
@Override public void parseData(Object jsonResponse) throws JSONException { JSONArray response = (JSONArray) jsonResponse; int id = response.getInt(0); String name = response.getString(1); String pos = response.getString(2); int freqCnt = response.getInt(3); String inflections = response.getString(4); mWord = new Word(id, name, pos); mWord.setFreqCnt(freqCnt);/*from w w w . j ava2 s. c o m*/ mWord.setInflections(inflections); mExpirationMillis = response.getLong(5) * 1000; }
From source file:io.rapidpro.androidchannel.json.JSON.java
public Long[] getLongArray(String key) { try {/* www .j a v a 2 s. c om*/ JSONArray array = m_o.getJSONArray(key); Long[] longArray = new Long[array.length()]; for (int i = 0; i < longArray.length; i++) { longArray[i] = array.getLong(i); } return longArray; } catch (Throwable t) { throw new JSONException(t); } }
From source file:com.hichinaschool.flashcards.libanki.Decks.java
/** * Deck selection *********************************************************** ************************************ *//*from w w w. ja v a2 s . c om*/ /* The currrently active dids. MAke sure to copy before modifying */ public LinkedList<Long> active() { try { String actv = mCol.getConf().getString("activeDecks"); JSONArray ja = new JSONArray(actv); LinkedList<Long> result = new LinkedList<Long>(); for (int i = 0; i < ja.length(); i++) { result.add(ja.getLong(i)); } return result; } catch (JSONException e) { throw new RuntimeException(e); } }