Example usage for android.database Cursor move

List of usage examples for android.database Cursor move

Introduction

In this page you can find the example usage for android.database Cursor move.

Prototype

boolean move(int offset);

Source Link

Document

Move the cursor by a relative amount, forward or backward, from the current position.

Usage

From source file:uk.ac.horizon.ubihelper.service.PeersOpenHelper.java

public static List<PeerInfo> getPeerInfos(SQLiteDatabase database, String where, String values[]) {
    Cursor c = database.query(PEER_TABLE_NAME, PEER_TABLE_COLUMNS, where, values, null, null,
            KEY_NAME + " ASC");
    LinkedList<PeerInfo> pis = new LinkedList<PeerInfo>();
    Log.d(TAG, "getPeerInfos returned " + c.getCount() + " rows");
    while (c.move(1)) {
        pis.add(getPeerInfo(c));/*w  w w .j  ava  2 s .  c  om*/
    }
    c.close();
    return pis;
}

From source file:org.linphone.compatibility.ApiFivePlus.java

public static Contact getContact(ContentResolver cr, Cursor cursor, int position) {
    try {/*from  w  w  w  . ja va 2  s .  c  o m*/
        if (cursor != null) {
            cursor.moveToFirst();
            boolean success = cursor.move(position);
            if (!success)
                return null;

            String id = cursor.getString(cursor.getColumnIndex(Data.CONTACT_ID));
            String name = getContactDisplayName(cursor);
            Uri thumbnail = getContactPictureUri(id);
            Uri photo = getContactPhotoUri(id);
            InputStream input = getContactPictureInputStream(cr, id);

            Contact contact;
            if (input == null) {
                contact = new Contact(id, name);
            } else {
                Bitmap bm = null;
                try {
                    bm = BitmapFactory.decodeStream(input);
                } catch (OutOfMemoryError oome) {
                }
                contact = new Contact(id, name, photo, thumbnail, bm);
            }

            return contact;
        } else {
            return null;
        }
    } catch (Exception e) {

    }
    return null;
}

From source file:com.anrlabs.locationreminder.GeoFenceReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    this.context = context;

    if (intent.getAction().equals("com.anrlabs.ACTION_RECEIVE_GEOFENCE")) {
        // if (intent  instanceof )
        broadcastIntent.addCategory(GeoFenceConsants.CATEGORY_LOCATION_SERVICES);
        handleEnter(intent);/*from   w  w  w.  j  ava2 s. c  om*/
    } else if (intent.getAction().equals("android.intent.action.RUN")) {
        //call timer
        dataID = intent.getLongExtra("idNumber", -1);

        if (dataID > (-1)) {
            Cursor constantsCursor = DatabaseHelper.getInstance(context).loadReminderDetails(dataID);
            constantsCursor.move(1);

            strinIds = new String[] { Long.toString(dataID) };
            sendNotification(constantsCursor.getString(constantsCursor.getColumnIndex(DatabaseHelper.TITLE)));

            constantsCursor.close();
        }
    }

}

From source file:com.google.zxing.client.android.history.HistoryManager.java

public HistoryItem buildHistoryItem(int number) {
    SQLiteOpenHelper helper = new DBHelper(activity);
    SQLiteDatabase db = null;/*from  www .  j a  v a  2s  .c o  m*/
    Cursor cursor = null;
    try {
        db = helper.getReadableDatabase();
        cursor = db.query(DBHelper.TABLE_NAME, COLUMNS, null, null, null, null,
                DBHelper.TIMESTAMP_COL + " DESC");
        cursor.move(number + 1);
        String text = cursor.getString(0);
        String display = cursor.getString(1);
        String format = cursor.getString(2);
        long timestamp = cursor.getLong(3);
        // String details = cursor.getString(4);
        Result result = new Result(text, null, null, BarcodeFormat.valueOf(format), timestamp);
        // return new HistoryItem(result, display, details);
        return new HistoryItem(result, display);
    } finally {
        close(cursor, db);
    }
}

From source file:com.google.zxing.client.android.history.HistoryManager.java

public void deleteHistoryItem(int number) {
    SQLiteOpenHelper helper = new DBHelper(activity);
    SQLiteDatabase db = null;/*from ww  w  .j  a v a 2s.co m*/
    Cursor cursor = null;
    try {
        db = helper.getWritableDatabase();
        cursor = db.query(DBHelper.TABLE_NAME, ID_COL_PROJECTION, null, null, null, null,
                DBHelper.TIMESTAMP_COL + " DESC");
        cursor.move(number);
        db.delete(DBHelper.TABLE_NAME, DBHelper.ID_COL + '=' + cursor.getString(0), null);
    } finally {
        close(cursor, db);
    }
}

From source file:com.google.zxing.client.android.history.HistoryManager.java

public void trimHistory() {
    SQLiteOpenHelper helper = new DBHelper(activity);
    SQLiteDatabase db = null;//  ww w.  j a  v a 2s . c o  m
    Cursor cursor = null;
    try {
        db = helper.getWritableDatabase();
        cursor = db.query(DBHelper.TABLE_NAME, ID_COL_PROJECTION, null, null, null, null,
                DBHelper.TIMESTAMP_COL + " DESC");
        cursor.move(MAX_ITEMS);
        while (cursor.moveToNext()) {
            String id = cursor.getString(0);
            Log.i(TAG, "Deleting scan history ID " + id);
            db.delete(DBHelper.TABLE_NAME, DBHelper.ID_COL + '=' + id, null);
        }
    } catch (SQLiteException sqle) {
        // We're seeing an error here when called in
        // CaptureActivity.onCreate() in rare cases
        // and don't understand it. First theory is that it's transient so
        // can be safely ignored.
        Log.w(TAG, sqle);
        // continue
    } finally {
        close(cursor, db);
    }
}

From source file:com.clearcenter.mobile_demo.mdSyncAdapter.java

public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider,
        SyncResult syncResult) {/*from   w w w  . j ava 2 s .com*/
    String authtoken = null;
    Log.i(TAG, "Starting sync operation...");

    try {
        mdSSLUtil.DisableSecurity();

        // Use the account manager to request the AuthToken we'll need
        // to talk to our sample server.  If we don't have an AuthToken
        // yet, this could involve a round-trip to the server to request
        // an AuthToken.
        authtoken = account_manager.blockingGetAuthToken(account, mdConstants.AUTHTOKEN_TYPE,
                NOTIFY_AUTH_FAILURE);

        final String hostname = account_manager.getUserData(account, "hostname");

        long _last_sample = -1;
        if (last_sample.containsKey(account.name))
            _last_sample = last_sample.get(account.name);

        // Get sync data from server...
        final String data = mdRest.GetSystemInfo(hostname, authtoken, _last_sample);

        // Something went wrong :^(
        if (TextUtils.isEmpty(data))
            return;

        if (_last_sample < 0) {
            int count = provider.delete(mdDeviceSamples.CONTENT_URI, mdDeviceSamples.NICKNAME + " = ?",
                    new String[] { account.name });

            Log.d(TAG, "Reset database, purged " + count + " samples.");
        }

        try {
            JSONObject json_data = new JSONObject(data);
            if (json_data.has("time"))
                _last_sample = Long.valueOf(json_data.getString("time"));
            Log.d(TAG, account.name + ": last sample time-stamp: " + _last_sample);
        } catch (JSONException e) {
            Log.e(TAG, "JSONException", e);
            return;
        }

        last_sample.put(account.name, _last_sample);

        ContentValues values = new ContentValues();
        values.put(mdDeviceSamples.NICKNAME, account.name);
        values.put(mdDeviceSamples.DATA, data);

        provider.insert(mdDeviceSamples.CONTENT_URI, values);

        String projection[] = new String[] { mdDeviceSamples.SAMPLE_ID };

        Cursor cursor = provider.query(mdDeviceSamples.CONTENT_URI, projection,
                mdDeviceSamples.NICKNAME + " = ?", new String[] { account.name }, mdDeviceSamples.SAMPLE_ID);

        int rows = cursor.getCount();
        Log.d(TAG, "Rows: " + rows);
        if (rows <= mdConstants.MAX_SAMPLES) {
            cursor.close();
            return;
        }

        Log.d(TAG, "Samples to purge: " + (rows - mdConstants.MAX_SAMPLES));
        cursor.move(rows - mdConstants.MAX_SAMPLES);

        int top_id = cursor.getInt(cursor.getColumnIndex(mdDeviceSamples.SAMPLE_ID));
        Log.d(TAG, "Top ID: " + top_id);

        cursor.close();

        int count = provider.delete(mdDeviceSamples.CONTENT_URI,
                mdDeviceSamples.SAMPLE_ID + " <= ? AND " + mdDeviceSamples.NICKNAME + " = ?",
                new String[] { String.valueOf(top_id), account.name });

        Log.d(TAG, "Purged " + count + " samples.");

    } catch (final RemoteException e) {
        Log.e(TAG, "RemoteException", e);
        syncResult.stats.numParseExceptions++;
    } catch (final AuthenticatorException e) {
        Log.e(TAG, "AuthenticatorException", e);
        syncResult.stats.numParseExceptions++;
    } catch (final OperationCanceledException e) {
        Log.e(TAG, "OperationCanceledExcetpion", e);
    } catch (final IOException e) {
        Log.e(TAG, "IOException", e);
        syncResult.stats.numIoExceptions++;
    } catch (final ParseException e) {
        Log.e(TAG, "ParseException", e);
        syncResult.stats.numParseExceptions++;
    } catch (final AuthenticationException e) {
        Log.e(TAG, "AuthenticationException", e);
        syncResult.stats.numAuthExceptions++;
        account_manager.invalidateAuthToken(mdConstants.AUTHTOKEN_TYPE, authtoken);
    } catch (GeneralSecurityException e) {
        Log.e(TAG, "GeneralSecurityException", e);
    } catch (final JSONException e) {
        Log.e(TAG, "JSONException", e);
        syncResult.stats.numParseExceptions++;
    }
}

From source file:org.smap.smapTask.android.tasks.DownloadTasksTask.java

private Outcome submitCompletedForms() {

    String selection = InstanceColumns.SOURCE + "=? and (" + InstanceColumns.STATUS + "=? or "
            + InstanceColumns.STATUS + "=?)";
    String selectionArgs[] = { Utilities.getSource(), InstanceProviderAPI.STATUS_COMPLETE,
            InstanceProviderAPI.STATUS_SUBMISSION_FAILED };

    ArrayList<Long> toUpload = new ArrayList<Long>();
    Cursor c = null;
    try {//from   w  w  w  . jav  a 2s  . c  o m
        c = Collect.getInstance().getContentResolver().query(InstanceColumns.CONTENT_URI, null, selection,
                selectionArgs, null);

        if (c != null && c.getCount() > 0) {
            c.move(-1);
            while (c.moveToNext()) {
                Long l = c.getLong(c.getColumnIndex(InstanceColumns._ID));
                toUpload.add(Long.valueOf(l));
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (c != null) {
            c.close();
        }
    }

    InstanceUploaderTask instanceUploaderTask = new InstanceUploaderTask();
    publishProgress("Submitting " + toUpload.size() + " finalised surveys");
    instanceUploaderTask.setUploaderListener((InstanceUploaderListener) mStateListener);

    Long[] toSendArray = new Long[toUpload.size()];
    toUpload.toArray(toSendArray);
    Log.i(getClass().getSimpleName(), "Submitting " + toUpload.size() + " finalised surveys");
    if (toUpload.size() > 0) {
        return instanceUploaderTask.doInBackground(toSendArray); // Already running a background task so call direct
    } else {
        return null;
    }

}

From source file:com.raspi.chatapp.util.storage.MessageHistory.java

public ArrayList<ImageMessage> getImageMessages(String chatId) {
    ArrayList<ImageMessage> messages = new ArrayList<>();
    SQLiteDatabase db = mDbHelper.getReadableDatabase();
    Cursor c = db.query(chatId, new String[] { MessageHistoryContract.MessageEntry._ID },
            MessageHistoryContract.MessageEntry.COLUMN_NAME_MESSAGE_TYPE + "=?",
            new String[] { MessageHistory.TYPE_IMAGE }, null, null, null);
    c.moveToFirst();/*from ww  w.j  ava 2s.c o m*/
    do {
        long messageId = c.getLong(0);
        MessageArrayContent mac = getMessage(chatId, messageId);
        if (mac instanceof ImageMessage)
            messages.add((ImageMessage) mac);
    } while (c.move(1));
    c.close();
    db.close();
    return messages;
}

From source file:com.clearcenter.mobile_demo.mdStatusActivity.java

public void updateData() {
    JSONObject json_data;//from  w ww  .  j a v  a2 s. c o m

    String projection[] = new String[] { mdDeviceSamples.DATA };

    Cursor cursor = getContentResolver().query(mdDeviceSamples.CONTENT_URI, projection,
            mdDeviceSamples.NICKNAME + " = ?", new String[] { account_nickname }, null);

    int rows = 0;
    try {
        rows = cursor.getCount();
    } catch (NullPointerException e) {
    }

    Log.d(TAG, "Matches: " + rows);

    if (rows == 0) {
        Log.d(TAG, "No rows match for nickname: " + account_nickname);
        cursor.close();
        return;
    }

    cursor.moveToLast();

    String data = cursor.getString(cursor.getColumnIndex(mdDeviceSamples.DATA));

    try {
        json_data = new JSONObject(data);
        if (json_data.has("hostname"))
            hostname_textview.setText("Hostname: " + json_data.getString("hostname"));
        if (json_data.has("name") && json_data.has("release")) {
            final String release = json_data.getString("name") + " " + json_data.getString("release");
            release_textview.setText("Release: " + release);
        }
        if (json_data.has("time_locale")) {
            time_textview.setText("Clock: " + json_data.getString("time_locale"));
        }

        if (rows >= 2) {
            bw_graphview.reset();
            loadavg_graphview.reset();
            mem_graphview.reset();

            if (rows <= samples)
                cursor.moveToFirst();
            else
                cursor.move(-samples);

            long unix_time = 0;
            double bw_up = 0.0;
            double bw_down = 0.0;

            do {
                data = cursor.getString(cursor.getColumnIndex(mdDeviceSamples.DATA));

                final List<JSONObject> samples = sortedSamplesList(data);
                ListIterator<JSONObject> li = samples.listIterator();

                while (li.hasNext()) {
                    json_data = li.next();

                    if (unix_time == 0) {
                        bw_up = json_data.getDouble("bandwidth_up");
                        bw_down = json_data.getDouble("bandwidth_down");
                        unix_time = Long.valueOf(json_data.getString("time"));
                        continue;
                    }

                    long diff = Long.valueOf(json_data.getString("time")) - unix_time;

                    double rate_up = (json_data.getDouble("bandwidth_up") - bw_up) / diff;
                    double rate_down = (json_data.getDouble("bandwidth_down") - bw_down) / diff;

                    if (rate_up < 0.0)
                        rate_up = 0.0;
                    if (rate_down < 0.0)
                        rate_down = 0.0;

                    bw_graphview.addSample(bw_graph_up, unix_time, (float) rate_up);
                    bw_graphview.addSample(bw_graph_down, unix_time, (float) rate_down);

                    //                        Log.d(TAG, "time: " + diff +
                    //                            ", rate_up: " + rate_up + ", rate_down: " + rate_down);
                    bw_up = json_data.getDouble("bandwidth_up");
                    bw_down = json_data.getDouble("bandwidth_down");

                    loadavg_graphview.addSample(loadavg_graph_5min, unix_time,
                            (float) json_data.getDouble("loadavg_5min"));
                    loadavg_graphview.addSample(loadavg_graph_15min, unix_time,
                            (float) json_data.getDouble("loadavg_15min"));

                    mem_graphview.addSample(mem_graph_active, unix_time,
                            (float) json_data.getDouble("mem_active"));
                    mem_graphview.addSample(mem_graph_swap, unix_time,
                            (float) json_data.getDouble("mem_swap_used"));

                    unix_time = Long.valueOf(json_data.getString("time"));
                }
            } while (cursor.moveToNext());

            bw_graphview.update();
            loadavg_graphview.update();
            mem_graphview.update();
        }
    } catch (JSONException e) {
        Log.e(TAG, "JSONException", e);
    }

    cursor.close();
}