Example usage for android.content ContentResolver acquireContentProviderClient

List of usage examples for android.content ContentResolver acquireContentProviderClient

Introduction

In this page you can find the example usage for android.content ContentResolver acquireContentProviderClient.

Prototype

public final @Nullable ContentProviderClient acquireContentProviderClient(@NonNull String name) 

Source Link

Document

Returns a ContentProviderClient that is associated with the ContentProvider with the authority of name, starting the provider if necessary.

Usage

From source file:org.exfio.csyncdroid.resource.LocalCalendar.java

@SuppressLint("InlinedApi")
public static void create(Account account, ContentResolver resolver, ServerInfo.ResourceInfo info)
        throws RemoteException {
    ContentProviderClient client = resolver.acquireContentProviderClient(CalendarContract.AUTHORITY);

    //FIXME - change default colour
    int color = 0xFFC3EA6E; // fallback: "DAVdroid green"
    if (info.getColor() != null) {
        Pattern p = Pattern.compile("#(\\p{XDigit}{6})(\\p{XDigit}{2})?");
        Matcher m = p.matcher(info.getColor());
        if (m.find()) {
            int color_rgb = Integer.parseInt(m.group(1), 16);
            int color_alpha = m.group(2) != null ? (Integer.parseInt(m.group(2), 16) & 0xFF) : 0xFF;
            color = (color_alpha << 24) | color_rgb;
        }/*  www  . j  a v a2 s  . com*/
    }

    ContentValues values = new ContentValues();
    values.put(Calendars.ACCOUNT_NAME, account.name);
    values.put(Calendars.ACCOUNT_TYPE, account.type);
    values.put(Calendars.NAME, info.getCollection());
    values.put(Calendars.CALENDAR_DISPLAY_NAME, info.getTitle());
    values.put(Calendars.CALENDAR_COLOR, color);
    values.put(Calendars.OWNER_ACCOUNT, account.name);
    values.put(Calendars.SYNC_EVENTS, 1);
    values.put(Calendars.VISIBLE, 1);
    values.put(Calendars.ALLOWED_REMINDERS, Reminders.METHOD_ALERT);

    if (info.isReadOnly())
        values.put(Calendars.CALENDAR_ACCESS_LEVEL, Calendars.CAL_ACCESS_READ);
    else {
        values.put(Calendars.CALENDAR_ACCESS_LEVEL, Calendars.CAL_ACCESS_OWNER);
        values.put(Calendars.CAN_ORGANIZER_RESPOND, 1);
        values.put(Calendars.CAN_MODIFY_TIME_ZONE, 1);
    }

    if (android.os.Build.VERSION.SDK_INT >= 15) {
        values.put(Calendars.ALLOWED_AVAILABILITY, Events.AVAILABILITY_BUSY + "," + Events.AVAILABILITY_FREE
                + "," + Events.AVAILABILITY_TENTATIVE);
        values.put(Calendars.ALLOWED_ATTENDEE_TYPES, Attendees.TYPE_NONE + "," + Attendees.TYPE_OPTIONAL + ","
                + Attendees.TYPE_REQUIRED + "," + Attendees.TYPE_RESOURCE);
    }

    if (info.getTimezone() != null)
        values.put(Calendars.CALENDAR_TIME_ZONE, info.getTimezone());

    Log.i(TAG, "Inserting calendar: " + values.toString() + " -> " + calendarsURI(account).toString());
    client.insert(calendarsURI(account), values);
}

From source file:com.marvin.rocklock.PlaylistUtils.java

/**
 * Writes to a playlist given a list of new song IDs and the playlist name
 *
 * @param context the activity calling this method
 * @param playlistName the playlist name
 * @param ids the song IDs to add/*from w w  w . j a  v  a2  s. c  om*/
 */
public static void writePlaylist(RockLockActivity context, String playlistName, ArrayList<String> ids) {

    ContentResolver resolver = context.getContentResolver();
    int playlistId = getPlaylistId(context, playlistName);

    Uri uri;
    int playOrder = 1;
    if (playlistId == -1) {
        // Case: new playlist
        ContentValues values = new ContentValues(1);
        values.put(MediaStore.Audio.Playlists.NAME, playlistName);
        // this might be missing a members extension...
        uri = resolver.insert(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, values);
    } else {
        // Case: exists playlist
        uri = MediaStore.Audio.Playlists.Members.getContentUri("external", playlistId);
        // Get most recent play order ID from playlist, so we can append
        Cursor orderCursor = resolver.query(uri, new String[] { MediaStore.Audio.Playlists.Members.PLAY_ORDER },
                null, null, MediaStore.Audio.Playlists.Members.PLAY_ORDER + " DESC ");

        if (orderCursor != null) {
            if (orderCursor.moveToFirst()) {
                playOrder = orderCursor.getInt(0) + 1;
            }
            orderCursor.close();
        }
    }

    Log.d("PLAYLIST ACTIVITY", String.format("Writing playlist %s", uri));

    // Add all the new tracks to the playlist.
    int size = ids.size();
    ContentValues values[] = new ContentValues[size];

    final ContentProviderClient provider = resolver.acquireContentProviderClient(uri);
    for (int i = 0; i < size; ++i) {
        values[i] = new ContentValues();
        values[i].put(MediaStore.Audio.Playlists.Members.AUDIO_ID, ids.get(i));
        values[i].put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, playOrder++);
        resolver.insert(uri, values[i]);
    }
    provider.release();
}

From source file:com.robotoworks.mechanoid.sqlite.SQuery.java

/**
 * <p>Select records using this query</p>
 * @param uri The ContentProvider Uri to query for
 * @param sortOrder The order by clause//from   w  w w. j  a  va 2s  .c  om
 * @return The results as active records
 */
public <T extends ActiveRecord> List<T> select(Uri uri, String sortOrder) {
    ContentResolver resolver = Mechanoid.getContentResolver();

    ContentProviderClient client = resolver.acquireContentProviderClient(uri);

    MechanoidContentProvider provider = (MechanoidContentProvider) client.getLocalContentProvider();

    List<T> records = provider.selectRecords(uri, this, sortOrder);

    return records;
}

From source file:com.robotoworks.mechanoid.sqlite.SQuery.java

/**
 * <p>Select records using this query</p>
 * @param uri The ContentProvider Uri to query for
 * @return The results as active records
 *//*from  w  w w .  ja v a 2  s.co  m*/
public <T extends ActiveRecord> List<T> select(Uri uri) {
    ContentResolver resolver = Mechanoid.getContentResolver();

    ContentProviderClient client = resolver.acquireContentProviderClient(uri);

    MechanoidContentProvider provider = (MechanoidContentProvider) client.getLocalContentProvider();

    List<T> records = provider.selectRecords(uri, this, null);

    return records;
}

From source file:com.robotoworks.mechanoid.sqlite.SQuery.java

/**
 * <p>Select the first record from the results of using this query</p>
 * @param uri The ContentProvider Uri to query for
 * @param sortOrder The order by clause//from  ww w.  ja va  2s.  c om
 * @return The results as active records
 */
public <T extends ActiveRecord> T selectFirst(Uri uri, String sortOrder) {
    ContentResolver resolver = Mechanoid.getContentResolver();

    ContentProviderClient client = resolver.acquireContentProviderClient(uri);

    MechanoidContentProvider provider = (MechanoidContentProvider) client.getLocalContentProvider();

    List<T> records = provider.selectRecords(uri, this, sortOrder);

    if (records.size() > 0) {
        return records.get(0);
    } else {
        return null;
    }
}

From source file:com.robotoworks.mechanoid.sqlite.SQuery.java

/**
 * <p>Select the first record from the results of using this query</p>
 * @param uri The ContentProvider Uri to query for
 * @return The results as active records
 *///from   www  .ja v  a2s.co  m
public <T extends ActiveRecord> T selectFirst(Uri uri) {
    ContentResolver resolver = Mechanoid.getContentResolver();

    ContentProviderClient client = resolver.acquireContentProviderClient(uri);

    MechanoidContentProvider provider = (MechanoidContentProvider) client.getLocalContentProvider();

    List<T> records = provider.selectRecords(uri, this, null);

    if (records.size() > 0) {
        return records.get(0);
    } else {
        return null;
    }
}

From source file:com.microsoft.onedrive.apiexplorer.ItemFragment.java

@Override
public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
    final BaseApplication application = (BaseApplication) getActivity().getApplication();
    final IOneDriveClient oneDriveClient = application.getOneDriveClient();

    if (requestCode == REQUEST_CODE_SIMPLE_UPLOAD && data != null && data.getData() != null
            && data.getData().getScheme().equalsIgnoreCase(SCHEME_CONTENT)) {

        final ProgressDialog dialog = new ProgressDialog(getActivity());
        dialog.setTitle(R.string.upload_in_progress_title);
        dialog.setMessage(getString(R.string.upload_in_progress_message));
        dialog.setIndeterminate(false);/*from w w  w . jav a2s.c  o  m*/
        dialog.setCancelable(false);
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dialog.setProgressNumberFormat(getString(R.string.upload_in_progress_number_format));
        dialog.show();
        final AsyncTask<Void, Void, Void> uploadFile = new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(final Void... params) {
                try {
                    final ContentResolver contentResolver = getActivity().getContentResolver();
                    final ContentProviderClient contentProvider = contentResolver
                            .acquireContentProviderClient(data.getData());
                    final byte[] fileInMemory = FileContent.getFileBytes(contentProvider, data.getData());
                    contentProvider.release();

                    // Fix up the file name (needed for camera roll photos, etc)
                    final String filename = FileContent.getValidFileName(contentResolver, data.getData());
                    final Option option = new QueryOption("@name.conflictBehavior", "fail");
                    oneDriveClient.getDrive().getItems(mItemId).getChildren().byId(filename).getContent()
                            .buildRequest(Collections.singletonList(option))
                            .put(fileInMemory, new IProgressCallback<Item>() {
                                @Override
                                public void success(final Item item) {
                                    dialog.dismiss();
                                    Toast.makeText(getActivity(),
                                            application.getString(R.string.upload_complete, item.name),
                                            Toast.LENGTH_LONG).show();
                                    refresh();
                                }

                                @Override
                                public void failure(final ClientException error) {
                                    dialog.dismiss();
                                    if (error.isError(OneDriveErrorCodes.NameAlreadyExists)) {
                                        Toast.makeText(getActivity(), R.string.upload_failed_name_conflict,
                                                Toast.LENGTH_LONG).show();
                                    } else {
                                        Toast.makeText(getActivity(),
                                                application.getString(R.string.upload_failed, filename),
                                                Toast.LENGTH_LONG).show();
                                    }
                                }

                                @Override
                                public void progress(final long current, final long max) {
                                    dialog.setProgress((int) current);
                                    dialog.setMax((int) max);
                                }
                            });
                } catch (final Exception e) {
                    Log.e(getClass().getSimpleName(), e.getMessage());
                    Log.e(getClass().getSimpleName(), e.toString());
                }
                return null;
            }
        };
        uploadFile.execute();
    }
}

From source file:com.robotoworks.mechanoid.db.SQuery.java

private MechanoidContentProvider getContentProvider(Uri uri) {

    if (mProvider != null) {
        return mProvider;
    }/*w w w  .ja  v a  2s  .  c om*/

    ContentResolver resolver = Mechanoid.getContentResolver();

    ContentProviderClient client = resolver.acquireContentProviderClient(uri);

    MechanoidContentProvider provider = (MechanoidContentProvider) client.getLocalContentProvider();
    return provider;
}