Search in sources :

Example 1 with BaseColumns

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));
}
Also used : AlertDialog(android.app.AlertDialog) ContentValues(android.content.ContentValues) R(com.simplecity.amp_library.R) Context(android.content.Context) Stream(com.annimon.stream.Stream) PackageManager(android.content.pm.PackageManager) Uri(android.net.Uri) Intent(android.content.Intent) NonNull(android.support.annotation.NonNull) PlayCountTable(com.simplecity.amp_library.sql.providers.PlayCountTable) Single(io.reactivex.Single) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) ArrayList(java.util.ArrayList) Song(com.simplecity.amp_library.model.Song) SuppressLint(android.annotation.SuppressLint) ContentResolver(android.content.ContentResolver) MediaStore(android.provider.MediaStore) Toast(android.widget.Toast) BaseFileObject(com.simplecity.amp_library.model.BaseFileObject) Settings(android.provider.Settings) Observable(io.reactivex.Observable) Schedulers(io.reactivex.schedulers.Schedulers) Build(android.os.Build) PreferenceManager(android.preference.PreferenceManager) Log(android.util.Log) Cursor(android.database.Cursor) ConnectivityManager(android.net.ConnectivityManager) BaseColumns(android.provider.BaseColumns) NetworkInfo(android.net.NetworkInfo) BuildConfig(com.simplecity.amp_library.BuildConfig) File(java.io.File) AlertDialog(android.app.AlertDialog) Query(com.simplecity.amp_library.model.Query) WifiManager(android.net.wifi.WifiManager) ShuttleApplication(com.simplecity.amp_library.ShuttleApplication) List(java.util.List) Config(com.simplecity.amp_library.constants.Config) SharedPreferences(android.content.SharedPreferences) Configuration(android.content.res.Configuration) ContentValues(android.content.ContentValues) SqlUtils(com.simplecity.amp_library.sql.SqlUtils) Activity(android.app.Activity) ContentUris(android.content.ContentUris) Query(com.simplecity.amp_library.model.Query) Intent(android.content.Intent) Cursor(android.database.Cursor) Uri(android.net.Uri) ContentResolver(android.content.ContentResolver)

Aggregations

SuppressLint (android.annotation.SuppressLint)1 Activity (android.app.Activity)1 AlertDialog (android.app.AlertDialog)1 ContentResolver (android.content.ContentResolver)1 ContentUris (android.content.ContentUris)1 ContentValues (android.content.ContentValues)1 Context (android.content.Context)1 Intent (android.content.Intent)1 SharedPreferences (android.content.SharedPreferences)1 PackageManager (android.content.pm.PackageManager)1 Configuration (android.content.res.Configuration)1 Cursor (android.database.Cursor)1 ConnectivityManager (android.net.ConnectivityManager)1 NetworkInfo (android.net.NetworkInfo)1 Uri (android.net.Uri)1 WifiManager (android.net.wifi.WifiManager)1 Build (android.os.Build)1 PreferenceManager (android.preference.PreferenceManager)1 BaseColumns (android.provider.BaseColumns)1 MediaStore (android.provider.MediaStore)1