use of android.provider.BaseColumns in project Shuttle by timusus.
the class ShuttleUtils method setRingtone.
/**
* Method setRingtone.
*
* @param context context
* @param song Song
*/
public static void setRingtone(final Context context, final Song song) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.System.canWrite(context)) {
new AlertDialog.Builder(context).setTitle(R.string.dialog_title_set_ringtone).setMessage(R.string.dialog_message_set_ringtone).setPositiveButton(R.string.button_ok, (dialog, which) -> {
Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
intent.setData(Uri.parse("package:" + ShuttleApplication.getInstance().getPackageName()));
context.startActivity(intent);
}).setNegativeButton(R.string.cancel, null).show();
return;
}
}
Observable.fromCallable(() -> {
boolean success = false;
final ContentResolver resolver = context.getContentResolver();
// Set the flag in the database to mark this as a ringtone
final Uri ringUri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, song.id);
try {
final ContentValues values = new ContentValues(2);
values.put(MediaStore.Audio.AudioColumns.IS_RINGTONE, "1");
values.put(MediaStore.Audio.AudioColumns.IS_ALARM, "1");
if (ringUri != null) {
resolver.update(ringUri, values, null, null);
}
} catch (final UnsupportedOperationException ex) {
// most likely the card just got unmounted
Log.e(TAG, "couldn't set ringtone flag for song " + song);
return false;
}
Query query = new Query.Builder().uri(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI).projection(new String[] { BaseColumns._ID, MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.TITLE }).selection(BaseColumns._ID + "=" + song.id).build();
final Cursor cursor = SqlUtils.createQuery(context, query);
if (cursor != null) {
try {
if (cursor.getCount() == 1) {
// Set the system setting to make this the current ringtone
cursor.moveToFirst();
if (ringUri != null) {
Settings.System.putString(resolver, Settings.System.RINGTONE, ringUri.toString());
}
success = true;
}
} finally {
cursor.close();
}
}
return success;
}).map(success -> success ? context.getString(R.string.ringtone_set, song.name) : context.getString(R.string.ringtone_set_failed)).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(message -> Toast.makeText(context, message, Toast.LENGTH_SHORT).show(), error -> LogUtils.logException(TAG, "Error setting ringtone", error));
}
Aggregations