List of usage examples for android.database Cursor isClosed
boolean isClosed();
From source file:com.tonyodev.fetch.FetchService.java
private synchronized void startDownload() { if (shuttingDown || runningTask) { return;//from www .j a va2 s.c o m } boolean networkAvailable = Utils.isNetworkAvailable(context); boolean onWiFi = Utils.isOnWiFi(context); if ((!networkAvailable || (preferredNetwork == NETWORK_WIFI && !onWiFi)) && activeDownloads.size() > 0) { runningTask = true; interruptActiveDownloads(); runningTask = false; } else if (networkAvailable && !runningTask && activeDownloads.size() < downloadsLimit && databaseHelper.hasPendingRequests()) { runningTask = true; try { Cursor cursor = databaseHelper.getNextPendingRequest(); if (cursor != null && !cursor.isClosed() && cursor.getCount() > 0) { RequestInfo requestInfo = Utils.cursorToRequestInfo(cursor, true, loggingEnabled); FetchRunnable fetchRunnable = new FetchRunnable(context, requestInfo.getId(), requestInfo.getUrl(), requestInfo.getFilePath(), requestInfo.getHeaders(), requestInfo.getFileSize(), loggingEnabled); databaseHelper.updateStatus(requestInfo.getId(), FetchService.STATUS_DOWNLOADING, DEFAULT_EMPTY_VALUE); activeDownloads.put(fetchRunnable.getId(), fetchRunnable); new Thread(fetchRunnable).start(); } } catch (Exception e) { if (loggingEnabled) { e.printStackTrace(); } } runningTask = false; if (activeDownloads.size() < downloadsLimit && databaseHelper.hasPendingRequests()) { startDownload(); } } }
From source file:info.guardianproject.otr.app.im.app.MessageView.java
protected String convertMediaUriToPath(Uri uri) { String path = null;//from w w w . java2 s .c o m String[] proj = { MediaStore.Images.Media.DATA }; Cursor cursor = getContext().getContentResolver().query(uri, proj, null, null, null); if (cursor != null && (!cursor.isClosed())) { if (cursor.isBeforeFirst()) { int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); path = cursor.getString(column_index); } cursor.close(); } return path; }
From source file:com.chatwing.whitelabel.managers.CommunicationModeManager.java
public boolean processMessageNotInCurrentCommunicationBox(Message message) { Uri uri;//from w ww .j av a2 s .c o m String unreadColumn; if (message.isPrivate()) { uri = ChatWingContentProvider.getConversationWithIdUri(message.getConversationID()); unreadColumn = ConversationTable.UNREAD_COUNT; } else { uri = ChatWingContentProvider.getChatBoxWithIdUri(message.getChatBoxId()); unreadColumn = ChatBoxTable.UNREAD_COUNT; } AppCompatActivity activity = mActivityDelegate.getActivity(); ContentResolver contentResolver = activity.getContentResolver(); // Get unread count of the correct chat box. Cursor cursor = null; int unreadCount = -1; try { cursor = contentResolver.query(uri, new String[] { unreadColumn }, null, null, null); if (cursor.getCount() > 0 && cursor.moveToFirst()) { unreadCount = cursor.getInt(cursor.getColumnIndex(unreadColumn)); } // else { // The chat box is not available in the DB. // TODO: may create the chat box or trigger a sync operation. // } } finally { if (cursor != null && !cursor.isClosed()) { cursor.close(); } } if (unreadCount == -1) { // 1. Something was wrong (the chat box id is invalid // or can't query the chat box from DB). // Let's stop. // 2. Conversation not found. There might be a case client received // a msg but there is not conversation created beforehand. We can // just ignore that and wait for "remote_unread" if (message.isPrivate()) { Intent intent = new Intent(activity, SyncCommunicationBoxesIntentService.class); intent.putExtra(SyncCommunicationBoxesIntentService.UPDATE_CATEGORIES_FLAG, false); intent.putExtra(SyncCommunicationBoxesIntentService.UPDATE_CONVERSATION_FLAG, true); activity.startService(intent); } return false; } // Increase unread count in DB if (message.isPrivate()) { return updateConversationUnreadCount(message.getConversationID(), ++unreadCount); } else { return updateChatBoxUnreadCountInDB(message.getChatBoxId(), ++unreadCount); } }
From source file:com.chatwingsdk.managers.CommunicationModeManager.java
public boolean processMessageNotInCurrentCommunicationBox(Message message) { Uri uri;/*from ww w .j a v a 2 s . c o m*/ String unreadColumn; if (message.isPrivate()) { uri = ChatWingContentProvider.getConversationWithIdUri(message.getConversationID()); unreadColumn = ConversationTable.UNREAD_COUNT; } else { uri = ChatWingContentProvider.getChatBoxWithIdUri(message.getChatBoxId()); unreadColumn = ChatBoxTable.UNREAD_COUNT; } ActionBarActivity activity = mActivityDelegate.getActivity(); ContentResolver contentResolver = activity.getContentResolver(); // Get unread count of the correct chat box. Cursor cursor = null; int unreadCount = -1; try { cursor = contentResolver.query(uri, new String[] { unreadColumn }, null, null, null); if (cursor.getCount() > 0 && cursor.moveToFirst()) { unreadCount = cursor.getInt(cursor.getColumnIndex(unreadColumn)); } // else { // The chat box is not available in the DB. // TODO: may create the chat box or trigger a sync operation. // } } finally { if (cursor != null && !cursor.isClosed()) { cursor.close(); } } if (unreadCount == -1) { // 1. Something was wrong (the chat box id is invalid // or can't query the chat box from DB). // Let's stop. // 2. Conversation not found. There might be a case client received // a msg but there is not conversation created beforehand. We can // just ignore that and wait for "remote_unread" if (message.isPrivate()) { Intent intent = new Intent(activity, SyncCommunicationBoxesIntentService.class); intent.putExtra(SyncCommunicationBoxesIntentService.UPDATE_CATEGORIES_FLAG, false); intent.putExtra(SyncCommunicationBoxesIntentService.UPDATE_CONVERSATION_FLAG, true); activity.startService(intent); } return false; } // Increase unread count in DB if (message.isPrivate()) { return updateConversationUnreadCount(message.getConversationID(), ++unreadCount); } else { return updateChatBoxUnreadCountInDB(message.getChatBoxId(), ++unreadCount); } }
From source file:de.ub0r.android.callmeter.data.RuleMatcher.java
/** * Load {@link Rule}s and {@link Plan}s. * * @param context {@link Context}//from w w w . j av a 2 s . c om */ private static void load(final Context context) { Log.d(TAG, "load()"); if (rules != null && plans != null) { return; } SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); stripLeadingZeros = prefs.getBoolean(Preferences.PREFS_STRIP_LEADING_ZEROS, false); intPrefix = prefs.getString(Preferences.PREFS_INT_PREFIX, ""); zeroPrefix = !intPrefix.equals("+44") && !intPrefix.equals("+49"); final ContentResolver cr = context.getContentResolver(); // load rules rules = new ArrayList<Rule>(); Cursor cursor = cr.query(DataProvider.Rules.CONTENT_URI, DataProvider.Rules.PROJECTION, DataProvider.Rules.ACTIVE + ">0", null, DataProvider.Rules.ORDER); if (cursor != null && cursor.moveToFirst()) { do { rules.add(new Rule(cr, cursor, -1)); } while (cursor.moveToNext()); } if (cursor != null && !cursor.isClosed()) { cursor.close(); } // load plans plans = new SparseArray<Plan>(); cursor = cr.query(DataProvider.Plans.CONTENT_URI, DataProvider.Plans.PROJECTION, DataProvider.Plans.WHERE_REALPLANS, null, null); if (cursor != null && cursor.moveToFirst()) { do { final int i = cursor.getInt(DataProvider.Plans.INDEX_ID); plans.put(i, new Plan(cr, cursor)); } while (cursor.moveToNext()); } if (cursor != null && !cursor.isClosed()) { cursor.close(); } // update parent references int l = plans.size(); for (int i = 0; i < l; i++) { Plan p = plans.valueAt(i); p.parent = plans.get(p.ppid); } }
From source file:com.wildplot.android.ankistats.CardsTypes.java
public boolean calculateCardsTypes(int type) { mTitle = R.string.stats_cards_types; mAxisTitles = new int[] { R.string.stats_answer_type, R.string.stats_answers, R.string.stats_cumulative_correct_percentage }; mValueLabels = new int[] { R.string.statistics_mature, R.string.statistics_young_and_learn, R.string.statistics_unlearned, R.string.statistics_suspended }; mColors = new int[] { R.color.stats_mature, R.color.stats_young, R.color.stats_unseen, R.color.stats_suspended };// w w w .ja v a 2 s .c o m mType = type; ArrayList<double[]> list = new ArrayList<double[]>(); double[] pieData; Cursor cur = null; String query = "select " + "sum(case when queue=2 and ivl >= 21 then 1 else 0 end), -- mtr\n" + "sum(case when queue in (1,3) or (queue=2 and ivl < 21) then 1 else 0 end), -- yng/lrn\n" + "sum(case when queue=0 then 1 else 0 end), -- new\n" + "sum(case when queue<0 then 1 else 0 end) -- susp\n" + "from cards where did in " + _limitWholeOnly(); Log.d(AnkiStatsApplication.TAG, "CardsTypes query: " + query); try { cur = mAnkiDb.getDatabase().rawQuery(query, null); cur.moveToFirst(); pieData = new double[] { cur.getDouble(0), cur.getDouble(1), cur.getDouble(2), cur.getDouble(3) }; } finally { if (cur != null && !cur.isClosed()) { cur.close(); } } //TODO adjust for CardsTypes, for now only copied from intervals // small adjustment for a proper chartbuilding with achartengine // if (list.size() == 0 || list.get(0)[0] > 0) { // list.add(0, new double[] { 0, 0, 0 }); // } // if (num == -1 && list.size() < 2) { // num = 31; // } // if (type != Utils.TYPE_LIFE && list.get(list.size() - 1)[0] < num) { // list.add(new double[] { num, 0, 0 }); // } else if (type == Utils.TYPE_LIFE && list.size() < 2) { // list.add(new double[] { Math.max(12, list.get(list.size() - 1)[0] + 1), 0, 0 }); // } mSeriesList = new double[1][4]; mSeriesList[0] = pieData; return list.size() > 0; }
From source file:com.ichi2.anki.stats.OverviewStatsBuilder.java
private List<int[]> _due(Integer start, Integer end, int chunk) { String lim = ""; if (start != null) { lim += String.format(Locale.US, " and due-%d >= %d", mCol.getSched().getToday(), start); }/* w ww . ja va 2s . c om*/ if (end != null) { lim += String.format(Locale.US, " and day < %d", end); } List<int[]> d = new ArrayList<>(); Cursor cur = null; try { String query; query = String.format(Locale.US, "select (due-%d)/%d as day,\n" + "sum(case when ivl < 21 then 1 else 0 end), -- yng\n" + "sum(case when ivl >= 21 then 1 else 0 end) -- mtr\n" + "from cards\n" + "where did in %s and queue in (2,3)\n" + "%s\n" + "group by day order by day", mCol.getSched().getToday(), chunk, _limit(), lim); cur = mCol.getDb().getDatabase().rawQuery(query, null); while (cur.moveToNext()) { d.add(new int[] { cur.getInt(0), cur.getInt(1), cur.getInt(2) }); } } finally { if (cur != null && !cur.isClosed()) { cur.close(); } } return d; }
From source file:org.sufficientlysecure.localcalendar.ui.EditActivity.java
@SuppressLint("NewApi") @Override/*w w w. jav a 2 s. c o m*/ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.edit); displayNameEditText = (EditText) findViewById(R.id.edit_activity_text_cal_name); colorPicker = (ColorPicker) findViewById(R.id.edit_activity_color_picker); svBar = (SVBar) findViewById(R.id.edit_activity_svbar); colorPicker.addSVBar(svBar); // check if add new or edit existing Intent intent = getIntent(); Uri calendarUri = intent.getData(); if (calendarUri != null) { edit = true; } if (edit) { // edit calendar setTitle(R.string.edit_activity_name_edit); Cursor cur = getContentResolver().query(calendarUri, CalendarController.PROJECTION, null, null, null); mCalendarId = ContentUris.parseId(calendarUri); try { if (cur.moveToFirst()) { String displayName = cur.getString(CalendarController.PROJECTION_DISPLAY_NAME_INDEX); int color = cur.getInt(CalendarController.PROJECTION_COLOR_INDEX); // display for editing displayNameEditText.setText(displayName); setColor(color); } } finally { if (cur != null && !cur.isClosed()) { cur.close(); } } } else { // new calendar setTitle(R.string.edit_activity_name_new); setColor(getResources().getColor(R.color.emphasis)); // on calendar creation, set both center colors to new color colorPicker.setOnColorChangedListener(new ColorPicker.OnColorChangedListener() { @Override public void onColorChanged(int color) { colorPicker.setOldCenterColor(color); } }); } // Based on Android version use ButtonBar on bottom or use custom Actionbar layout if (android.os.Build.VERSION.SDK_INT >= 11) { if (edit) { ActionBarHelper.setDoneView(getActionBar(), new OnClickListener() { @Override public void onClick(View view) { save(); } }); } else { ActionBarHelper.setDoneCancelView(getActionBar(), new OnClickListener() { @Override public void onClick(View view) { save(); } }, new OnClickListener() { @Override public void onClick(View view) { finish(); } }); } } else { // Android < 3.0 editButtons = (LinearLayout) findViewById(R.id.edit_activity_edit_buttons); deleteButton = (Button) findViewById(R.id.edit_activity_delete); importExportButton = (Button) findViewById(R.id.edit_activity_import_export); cancelButton = (Button) findViewById(R.id.edit_activity_cancel); saveButton = (Button) findViewById(R.id.edit_activity_save); if (edit) { editButtons.setVisibility(View.VISIBLE); } deleteButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { delete(); } }); importExportButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { importExport(); } }); cancelButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { finish(); } }); saveButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { save(); } }); } // remove error when characters are entered displayNameEditText.addTextChangedListener(new TextWatcher() { public void afterTextChanged(Editable s) { displayNameEditText.setError(null); } public void beforeTextChanged(CharSequence s, int start, int count, int after) { } public void onTextChanged(CharSequence s, int start, int before, int count) { } }); }
From source file:com.chatwing.whitelabel.fragments.ConversationMessagesFragment.java
private void onCreateConversationResult(Conversation conversation) { if (conversation == null) { return;// ww w .j av a 2 s . c o m } //Create conversation if not existed ContentValues conversationContentValues = ConversationTable.getContentValues(conversation, mUserManager.getCurrentUser()); Cursor cursor = null; try { cursor = getActivity().getContentResolver().query( ChatWingContentProvider.getConversationWithIdUri(conversation.getId()), new String[] { ConversationTable.CONVERSATION_ID }, null, null, null); if (cursor.getCount() == 0) { Uri insert = getActivity().getContentResolver() .insert(ChatWingContentProvider.getConversationsUri(), conversationContentValues); if ("-1".equals(insert.getLastPathSegment())) { LogUtils.v("insert conversation failed "); return; } } mCurrentConversationManager.loadConversation(conversation.getId()); } finally { if (cursor != null && !cursor.isClosed()) { cursor.close(); } } }
From source file:com.chatwing.whitelabel.services.NotificationIntentService.java
private List<Message> getMessagesByGroup(Integer chatboxId) { Cursor cursor = null; try {/*from w w w . j av a 2s . co m*/ cursor = getContentResolver().query(ChatWingContentProvider.getNotificationMessagesUri(), NotificationMessagesTable.getMinimumProjection(), NotificationMessagesTable.CHAT_BOX_ID + " == " + chatboxId, null, NotificationMessagesTable.CREATED_DATE + " DESC"); boolean hasNext = cursor.moveToFirst(); List<Message> messages = new ArrayList<Message>(); while (hasNext) { Message message = NotificationMessagesTable.getMessage(cursor); if (message != null) { messages.add(message); } hasNext = cursor.moveToNext(); } return messages; } finally { if (cursor != null && !cursor.isClosed()) { cursor.close(); } } }