Search in sources :

Example 16 with Cursor

use of android.database.Cursor in project AndroidSyncProviderDemo by c99koder.

the class ContactsSyncAdapterService method performSync.

private static void performSync(Context context, Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) throws OperationCanceledException {
    HashMap<String, SyncEntry> localContacts = new HashMap<String, SyncEntry>();
    mContentResolver = context.getContentResolver();
    Log.i(TAG, "performSync: " + account.toString());
    // Load the local contacts
    Uri rawContactUri = RawContacts.CONTENT_URI.buildUpon().appendQueryParameter(RawContacts.ACCOUNT_NAME, account.name).appendQueryParameter(RawContacts.ACCOUNT_TYPE, account.type).build();
    Cursor c1 = mContentResolver.query(rawContactUri, new String[] { BaseColumns._ID, UsernameColumn, PhotoTimestampColumn }, null, null, null);
    while (c1.moveToNext()) {
        SyncEntry entry = new SyncEntry();
        entry.raw_id = c1.getLong(c1.getColumnIndex(BaseColumns._ID));
        entry.photo_timestamp = c1.getLong(c1.getColumnIndex(PhotoTimestampColumn));
        localContacts.put(c1.getString(1), entry);
    }
    ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
    try {
        // status message
        if (localContacts.get("efudd") == null) {
            addContact(account, "Elmer Fudd", "efudd");
        } else {
            if (localContacts.get("efudd").photo_timestamp == null || System.currentTimeMillis() > (localContacts.get("efudd").photo_timestamp + 604800000L)) {
                //You would probably download an image file and just pass the bytes, but this sample doesn't use network so we'll decode and re-compress the icon resource to get the bytes
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                Bitmap icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon);
                icon.compress(CompressFormat.PNG, 0, stream);
                updateContactPhoto(operationList, localContacts.get("efudd").raw_id, stream.toByteArray());
            }
            updateContactStatus(operationList, localContacts.get("efudd").raw_id, "hunting wabbits");
        }
        if (operationList.size() > 0)
            mContentResolver.applyBatch(ContactsContract.AUTHORITY, operationList);
    } catch (Exception e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
}
Also used : ContentProviderOperation(android.content.ContentProviderOperation) Bitmap(android.graphics.Bitmap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Cursor(android.database.Cursor) Uri(android.net.Uri) OperationCanceledException(android.accounts.OperationCanceledException)

Example 17 with Cursor

use of android.database.Cursor in project AndroidSyncProviderDemo by c99koder.

the class ProfileActivity method onCreate.

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.profile);
    if (getIntent().getData() != null) {
        Cursor cursor = managedQuery(getIntent().getData(), null, null, null, null);
        if (cursor.moveToNext()) {
            String username = cursor.getString(cursor.getColumnIndex("DATA1"));
            TextView tv = (TextView) findViewById(R.id.profiletext);
            tv.setText("This is the profile for " + username);
        }
    } else {
        // How did we get here without data?
        finish();
    }
}
Also used : TextView(android.widget.TextView) Cursor(android.database.Cursor)

Example 18 with Cursor

use of android.database.Cursor in project photo-picker-plus-android by chute.

the class AppUtil method getPath.

public static String getPath(Context context, Uri uri) throws NullPointerException {
    final String[] projection = { MediaColumns.DATA };
    final Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null);
    final int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}
Also used : Cursor(android.database.Cursor) SuppressLint(android.annotation.SuppressLint)

Example 19 with Cursor

use of android.database.Cursor in project photo-picker-plus-android by chute.

the class CursorRecyclerViewAdapter method swapCursor.

/**
     * Swap in a new Cursor, returning the old Cursor.  Unlike
     * {@link #changeCursor(Cursor)}, the returned old Cursor is <em>not</em>
     * closed.
     */
public Cursor swapCursor(Cursor newCursor) {
    if (newCursor == mCursor) {
        return null;
    }
    final Cursor oldCursor = mCursor;
    if (oldCursor != null && mDataSetObserver != null) {
        oldCursor.unregisterDataSetObserver(mDataSetObserver);
    }
    mCursor = newCursor;
    if (mCursor != null) {
        if (mDataSetObserver != null) {
            mCursor.registerDataSetObserver(mDataSetObserver);
        }
        mRowIdColumn = newCursor.getColumnIndexOrThrow("_id");
        mDataValid = true;
        notifyDataSetChanged();
    } else {
        mRowIdColumn = -1;
        mDataValid = false;
        notifyDataSetChanged();
    //There is no notifyDataSetInvalidated() method in RecyclerView.Adapter
    }
    return oldCursor;
}
Also used : Cursor(android.database.Cursor)

Example 20 with Cursor

use of android.database.Cursor in project photo-picker-plus-android by chute.

the class GridAdapter method resolveImageOrientation.

public int resolveImageOrientation(AssetModel assetModel) {
    int rotation = -1;
    if (TextUtils.isEmpty(assetModel.getId())) {
        return rotation;
    }
    if (MediaType.VIDEO.name().equalsIgnoreCase(assetModel.getType())) {
        return rotation;
    }
    if (assetModel.getUrl().startsWith("file:") == false) {
        return rotation;
    }
    Cursor mediaCursor = context.getContentResolver().query(Uri.parse(assetModel.getId()), new String[] { MediaStore.Images.ImageColumns.ORIENTATION, MediaStore.MediaColumns.SIZE }, null, null, null);
    if (mediaCursor != null && mediaCursor.getCount() != 0) {
        while (mediaCursor.moveToNext()) {
            long size = mediaCursor.getLong(1);
            Log.d(TAG, "Media Size " + size);
            //Extra check to make sure that we are getting the orientation from the proper file
            rotation = mediaCursor.getInt(0);
        }
    }
    Log.d(TAG, "Media Rotation " + rotation);
    return rotation;
}
Also used : Cursor(android.database.Cursor)

Aggregations

Cursor (android.database.Cursor)3795 ArrayList (java.util.ArrayList)522 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)499 Uri (android.net.Uri)455 ContentValues (android.content.ContentValues)300 ContentResolver (android.content.ContentResolver)188 RemoteException (android.os.RemoteException)182 Test (org.junit.Test)172 File (java.io.File)164 IOException (java.io.IOException)156 MatrixCursor (android.database.MatrixCursor)154 Intent (android.content.Intent)119 MediumTest (android.test.suitebuilder.annotation.MediumTest)116 SQLException (android.database.SQLException)115 HashMap (java.util.HashMap)108 SQLiteCursor (android.database.sqlite.SQLiteCursor)88 SQLiteException (android.database.sqlite.SQLiteException)85 Query (android.app.DownloadManager.Query)76 MergeCursor (android.database.MergeCursor)75 LargeTest (android.test.suitebuilder.annotation.LargeTest)69