List of usage examples for android.database Cursor isClosed
boolean isClosed();
From source file:com.hichinaschool.flashcards.libanki.sync.Syncer.java
private ArrayList<Object[]> newerRows(JSONArray data, String table, int modIdx) { long[] ids = new long[data.length()]; try {// w ww .j av a2 s.com for (int i = 0; i < data.length(); i++) { ids[i] = data.getJSONArray(i).getLong(0); } HashMap<Long, Long> lmods = new HashMap<Long, Long>(); Cursor cur = null; try { cur = mCol.getDb().getDatabase().rawQuery( "SELECT id, mod FROM " + table + " WHERE id IN " + Utils.ids2str(ids) + " AND " + usnLim(), null); while (cur.moveToNext()) { lmods.put(cur.getLong(0), cur.getLong(1)); } } finally { if (cur != null && !cur.isClosed()) { cur.close(); } } ArrayList<Object[]> update = new ArrayList<Object[]>(); for (int i = 0; i < data.length(); i++) { JSONArray r = data.getJSONArray(i); if (!lmods.containsKey(r.getLong(0)) || lmods.get(r.getLong(0)) < r.getLong(modIdx)) { update.add(ConvUtils.jsonArray2Objects(r)); } } return update; } catch (JSONException e) { throw new RuntimeException(e); } }
From source file:com.ichi2.libanki.sync.Syncer.java
private ArrayList<Object[]> newerRows(JSONArray data, String table, int modIdx) { long[] ids = new long[data.length()]; try {// www. j a v a2s . co m for (int i = 0; i < data.length(); i++) { ids[i] = data.getJSONArray(i).getLong(0); } HashMap<Long, Long> lmods = new HashMap<Long, Long>(); Cursor cur = null; try { cur = mCol.getDb().getDatabase().rawQuery( "SELECT id, mod FROM " + table + " WHERE id IN " + Utils.ids2str(ids) + " AND " + usnLim(), null); while (cur.moveToNext()) { lmods.put(cur.getLong(0), cur.getLong(1)); } } finally { if (cur != null && !cur.isClosed()) { cur.close(); } } ArrayList<Object[]> update = new ArrayList<Object[]>(); for (int i = 0; i < data.length(); i++) { JSONArray r = data.getJSONArray(i); if (!lmods.containsKey(r.getLong(0)) || lmods.get(r.getLong(0)) < r.getLong(modIdx)) { update.add(ConvUtils.jsonArray2Objects(r)); } } mCol.log(table, data); return update; } catch (JSONException e) { throw new RuntimeException(e); } }
From source file:de.ub0r.android.callmeter.ui.PlansFragment.java
@Override public void onLoadFinished(final Loader<Cursor> loader, final Cursor data) { Log.d(TAG, "onLoadFinished()"); if (getActivity() == null) { Log.w(TAG, "ignore loaded data, activity finished"); return;/*from w w w . j a v a 2 s . c o m*/ } ignoreQuery = false; PlansAdapter adapter = (PlansAdapter) getListAdapter(); adapter.save(); if (data != null && data.getCount() > 0) { if (now < 0L && data.getColumnIndex(DataProvider.Plans.SUM_COST) > 0) { StringBuilder sb = new StringBuilder(DataProvider.Plans.ID + " in (-1"); try { if (!data.isClosed() && data.moveToFirst()) { do { sb.append(",").append(data.getLong(DataProvider.Plans.INDEX_ID)); } while (data.moveToNext()); } sb.append(")"); PreferenceManager.getDefaultSharedPreferences(getActivity()).edit() .putString("dummy_where", sb.toString()).commit(); } catch (IllegalStateException ex) { Log.e(TAG, "could not walk through cursor to save shown plans", ex); } } vImport.setVisibility(View.GONE); } else { vImport.setVisibility(View.VISIBLE); } vLoading.setVisibility(View.GONE); try { adapter.swapCursor(data); } catch (IllegalStateException ex) { Log.e(TAG, "could not set cursor to adapter", ex); adapter.swapCursor(null); } setInProgress(-1); }
From source file:org.andstatus.app.TimelineActivity.java
private int calcRowsLimit(boolean loadOneMorePage) { int nMessages = 0; if (getListAdapter() != null) { Cursor cursor = ((CursorAdapter) getListAdapter()).getCursor(); if (cursor != null && !cursor.isClosed()) { nMessages = cursor.getCount(); }// w w w .j a v a 2s.co m } if (loadOneMorePage) { nMessages += TimelineListParameters.PAGE_SIZE; } else if (nMessages < TimelineListParameters.PAGE_SIZE) { nMessages = TimelineListParameters.PAGE_SIZE; } return nMessages; }
From source file:org.andstatus.app.msg.TimelineActivity.java
/** * @param position Of current item in the underlying Cursor * @return id of the User linked to this message. This link reflects the User's timeline * or an Account which was used to retrieved the message *//*from w w w .j a v a 2s . c om*/ @Override public long getLinkedUserIdFromCursor(int position) { long userId = 0; try { Cursor cursor = null; if (getListAdapter() != null) { cursor = ((CursorAdapter) getListAdapter()).getCursor(); } if (cursor != null && !cursor.isClosed()) { cursor.moveToPosition(position); int columnIndex = cursor.getColumnIndex(User.LINKED_USER_ID); if (columnIndex > -1) { userId = cursor.getLong(columnIndex); } } } catch (Exception e) { MyLog.v(this, e); } return userId; }
From source file:org.zywx.wbpalmstar.plugin.uexcontacts.EUExContact.java
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); JSONObject jobj = new JSONObject(); switch (requestCode) { case F_ACT_REQ_CODE_UEX_CONTACT: if (resultCode == Activity.RESULT_OK) { try { int sdkVersion = Build.VERSION.SDK_INT; if (EUExContact.customLinkMan && sdkVersion < 8) { jobj.put(EUExCallback.F_JK_NAME, data.getStringExtra(EUExCallback.F_JK_NAME)); jobj.put(EUExCallback.F_JK_NUM, data.getStringExtra(EUExCallback.F_JK_NUM)); jobj.put(EUExCallback.F_JK_EMAIL, data.getStringExtra(EUExCallback.F_JK_EMAIL)); } else { Uri contactData = data.getData(); Cursor c = ((Activity) mContext).managedQuery(contactData, null, null, null, null); c.moveToFirst();//from w w w. ja va 2s .c o m // ??? String name = c.getString( c.getColumnIndex(android.provider.ContactsContract.Contacts.DISPLAY_NAME)); jobj.put(EUExCallback.F_JK_NAME, name); String contactId = c .getString(c.getColumnIndex(android.provider.ContactsContract.Contacts._ID)); // ?? Cursor phones = ((Activity) mContext).getContentResolver().query( android.provider.ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, android.provider.ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null); if (phones != null && phones.getCount() > 0) { phones.moveToFirst(); String phoneNumber = phones.getString(phones.getColumnIndex( android.provider.ContactsContract.CommonDataKinds.Phone.NUMBER)); jobj.put(EUExCallback.F_JK_NUM, phoneNumber); if (phones.isClosed()) { Log.i("tag", "phones.close()"); phones.close(); } } // ? Cursor emails = ((Activity) mContext).getContentResolver().query( android.provider.ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, android.provider.ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId, null, null); if (emails != null && emails.getCount() > 0) { emails.moveToFirst(); String emailAddress = emails.getString(emails .getColumnIndex(android.provider.ContactsContract.CommonDataKinds.Email.DATA)); jobj.put(EUExCallback.F_JK_EMAIL, emailAddress); if (emails.isClosed()) { emails.close(); } } if (c.isClosed()) { c.close(); } } jsCallback(EUExContact.KEY_CONTACT_OPEN, 0, EUExCallback.F_C_JSON, jobj.toString()); } catch (Exception e) { Toast.makeText(mContext, finder.getString("plugin_contact_open_fail"), Toast.LENGTH_SHORT) .show(); return; } } else { return; } break; case F_ACT_REQ_CODE_UEX_MULTI_CONTACT: if (resultCode == Activity.RESULT_OK) { String result = data.getStringExtra(ContactActivity.F_INTENT_KEY_RETURN_SELECT_LIST); jsCallback(EUExContact.KEY_CONTACT_MULTIOPEN, 0, EUExCallback.F_C_JSON, /* * jobj . toString ( ) */result); } break; } }
From source file:org.rapidandroid.activity.chart.form.FormDataBroker.java
private JSONGraphData loadNumericLine() { Date startDateToUse = getStartDate(); SQLiteDatabase db = rawDB.getReadableDatabase(); String fieldcol = RapidSmsDBConstants.FormData.COLUMN_PREFIX + fieldToPlot.getName(); StringBuilder rawQuery = new StringBuilder(); rawQuery.append("select rapidandroid_message.time, " + fieldcol); rawQuery.append(" from "); rawQuery.append(RapidSmsDBConstants.FormData.TABLE_PREFIX + mForm.getPrefix()); rawQuery.append(" join rapidandroid_message on ("); rawQuery.append(RapidSmsDBConstants.FormData.TABLE_PREFIX + mForm.getPrefix()); rawQuery.append(".message_id = rapidandroid_message._id"); rawQuery.append(") "); if (startDateToUse.compareTo(Constants.NULLDATE) != 0 && mEndDate.compareTo(Constants.NULLDATE) != 0) { rawQuery.append(" WHERE rapidandroid_message.time > '" + Message.SQLDateFormatter.format(startDateToUse) + "' AND rapidandroid_message.time < '" + Message.SQLDateFormatter.format(mEndDate) + "' "); }/*from w w w.jav a 2 s . co m*/ rawQuery.append(" order by rapidandroid_message.time ASC"); // the string value is column 0 // the magnitude is column 1 Cursor cr = db.rawQuery(rawQuery.toString(), null); int barCount = cr.getCount(); if (barCount == 0) { cr.close(); } else { Date[] xVals = new Date[barCount]; int[] yVals = new int[barCount]; cr.moveToFirst(); int i = 0; do { try { xVals[i] = Message.SQLDateFormatter.parse(cr.getString(0)); yVals[i] = cr.getInt(1); } catch (Exception ex) { } i++; } while (cr.moveToNext()); // xaxis: { ticks: [0, [Math.PI/2, "\u03c0/2"], [Math.PI, "\u03c0"], // [Math.PI * 3/2, "3\u03c0/2"], [Math.PI * 2, "2\u03c0"]]}, try { return new JSONGraphData(prepareDateData(xVals, yVals), loadOptionsForDateGraph(xVals, false, DateDisplayTypes.Daily)); } catch (Exception ex) { } finally { if (!cr.isClosed()) { cr.close(); } } } // either there was no data or something bad happened return new JSONGraphData(getEmptyData(), new JSONObject()); }
From source file:com.aware.Aware.java
/** * Retrieve setting value given key./*from ww w . j a v a2 s. c o m*/ * @param key * @return value */ public static String getSetting(Context context, String key) { boolean is_restricted_package = true; ArrayList<String> global_settings = new ArrayList<String>(); global_settings.add("debug_flag"); global_settings.add("debug_tag"); global_settings.add("device_id"); global_settings.add("study_id"); global_settings.add("study_start"); if (global_settings.contains(key)) { is_restricted_package = false; } String value = ""; Cursor qry = context.getContentResolver().query(Aware_Settings.CONTENT_URI, null, Aware_Settings.SETTING_KEY + " LIKE '" + key + "'" + (is_restricted_package ? " AND " + Aware_Settings.SETTING_PACKAGE_NAME + " LIKE '" + context.getPackageName() + "'" : ""), null, null); if (qry != null && qry.moveToFirst()) { value = qry.getString(qry.getColumnIndex(Aware_Settings.SETTING_VALUE)); } if (qry != null && !qry.isClosed()) qry.close(); return value; }
From source file:org.rapidandroid.activity.chart.form.FormDataBroker.java
/** * Should return a two element array - the first element is the data, the * second are the options/* w w w . j av a 2 s . c o m*/ * * @return */ private JSONGraphData loadHistogramFromField() { // JSONObject result = new JSONObject(); SQLiteDatabase db = rawDB.getReadableDatabase(); String fieldcol = RapidSmsDBConstants.FormData.COLUMN_PREFIX + fieldToPlot.getName(); StringBuilder rawQuery = new StringBuilder(); rawQuery.append("select " + fieldcol); rawQuery.append(", count(*) from "); rawQuery.append(RapidSmsDBConstants.FormData.TABLE_PREFIX + mForm.getPrefix()); rawQuery.append(" join rapidandroid_message on ("); rawQuery.append(RapidSmsDBConstants.FormData.TABLE_PREFIX + mForm.getPrefix()); rawQuery.append(".message_id = rapidandroid_message._id"); rawQuery.append(") "); if (mStartDate.compareTo(Constants.NULLDATE) != 0 && mEndDate.compareTo(Constants.NULLDATE) != 0) { rawQuery.append(" WHERE rapidandroid_message.time > '" + Message.SQLDateFormatter.format(mStartDate) + "' AND rapidandroid_message.time < '" + Message.SQLDateFormatter.format(mEndDate) + "' "); } rawQuery.append(" group by " + fieldcol); rawQuery.append(" order by " + fieldcol); // the string value is column 0 // the magnitude is column 1 Cursor cr = db.rawQuery(rawQuery.toString(), null); int barCount = cr.getCount(); if (barCount != 0) { String[] xVals = new String[barCount]; int[] yVals = new int[barCount]; cr.moveToFirst(); int i = 0; do { xVals[i] = cr.getString(0); yVals[i] = cr.getInt(1); i++; } while (cr.moveToNext()); // xaxis: { ticks: [0, [Math.PI/2, "\u03c0/2"], [Math.PI, "\u03c0"], // [Math.PI * 3/2, "3\u03c0/2"], [Math.PI * 2, "2\u03c0"]]}, try { // result.put("label", fieldToPlot.getName()); // result.put("data", prepareData(xVals, yVals)); // result.put("bars", getShowTrue()); // result.put("xaxis", getXaxisOptions(xVals)); return new JSONGraphData(prepareHistogramData(xVals, yVals), loadOptionsForHistogram(xVals)); } catch (Exception ex) { } finally { if (!cr.isClosed()) { cr.close(); } if (db.isOpen()) { db.close(); } } } // either there was no data or something bad happened return new JSONGraphData(getEmptyData(), new JSONObject()); }
From source file:org.dbhatt.d_deleted_contact.All_contact_fragment.java
public void add_contact(Contact contact) { try {/* www .j a v a 2 s .c o m*/ if (all_contact != null) if (all_contact.size() <= 0) { all_contact = new ArrayList<>(); Cursor cursor = getContext().getContentResolver().query( ContactsContract.RawContacts.CONTENT_URI, new String[] { ContactsContract.RawContacts._ID, ContactsContract.RawContacts.CONTACT_ID, ContactsContract.RawContacts.ACCOUNT_TYPE, ContactsContract.RawContacts.DISPLAY_NAME_PRIMARY }, ContactsContract.RawContacts._ID + "=?", new String[] { String.valueOf(contact.getId()) }, null); if (cursor != null) { if (cursor.moveToFirst()) all_contact.add(0, new Contact( cursor.getInt(cursor.getColumnIndex(ContactsContract.RawContacts._ID)), cursor.getInt(cursor.getColumnIndex(ContactsContract.RawContacts.CONTACT_ID)), cursor.getString(cursor .getColumnIndex(ContactsContract.RawContacts.DISPLAY_NAME_PRIMARY)), cursor.getString( cursor.getColumnIndex(ContactsContract.RawContacts.ACCOUNT_TYPE)))); if (!cursor.isClosed()) cursor.close(); } else { all_contact.add(0, contact); } if (all_contact.size() > 0) { data_not_found.setVisibility(View.GONE); recyclerView.setVisibility(View.VISIBLE); recyclerView.setHasFixedSize(true); recyclerView.setLayoutManager( new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false)); adapter = new All_contact(all_contact, getContext(), ((MainActivity) getActivity())); recyclerView.setAdapter(adapter); recyclerView.setNestedScrollingEnabled(false); } } else { Cursor cursor = getContext().getContentResolver().query( ContactsContract.RawContacts.CONTENT_URI, new String[] { ContactsContract.RawContacts._ID, ContactsContract.RawContacts.CONTACT_ID, ContactsContract.RawContacts.ACCOUNT_TYPE, ContactsContract.RawContacts.DISPLAY_NAME_PRIMARY }, ContactsContract.RawContacts._ID + "=?", new String[] { String.valueOf(contact.getId()) }, null); if (cursor != null) { if (cursor.moveToFirst()) { all_contact.add(0, new Contact( cursor.getInt(cursor.getColumnIndex(ContactsContract.RawContacts._ID)), cursor.getInt(cursor.getColumnIndex(ContactsContract.RawContacts.CONTACT_ID)), cursor.getString(cursor .getColumnIndex(ContactsContract.RawContacts.DISPLAY_NAME_PRIMARY)), cursor.getString( cursor.getColumnIndex(ContactsContract.RawContacts.ACCOUNT_TYPE)))); } if (!cursor.isClosed()) cursor.close(); } } adapter.notifyItemInserted(0); } catch (Exception e) { e.printStackTrace(); } }