[Android] Thread and fix ringtone setting for Oreo

Geoffrey Métais git at videolan.org
Fri Dec 21 11:15:36 CET 2018


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Fri Dec 21 10:53:46 2018 +0100| [935ac622ab74fc653f64193f76ce141ac41333d0] | committer: Geoffrey Métais

Thread and fix ringtone setting for Oreo

> https://code.videolan.org/videolan/vlc-android/commit/935ac622ab74fc653f64193f76ce141ac41333d0
---

 .../org/videolan/vlc/gui/helpers/AudioUtil.java    | 108 +++++++++++++--------
 .../src/org/videolan/vlc/util/Permissions.java     |  13 +--
 2 files changed, 73 insertions(+), 48 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/helpers/AudioUtil.java b/vlc-android/src/org/videolan/vlc/gui/helpers/AudioUtil.java
index b461df0bf..994fbf85f 100644
--- a/vlc-android/src/org/videolan/vlc/gui/helpers/AudioUtil.java
+++ b/vlc-android/src/org/videolan/vlc/gui/helpers/AudioUtil.java
@@ -30,8 +30,6 @@ import android.graphics.BitmapFactory;
 import android.media.RingtoneManager;
 import android.net.Uri;
 import android.provider.MediaStore;
-import androidx.annotation.WorkerThread;
-import androidx.fragment.app.FragmentActivity;
 import android.text.TextUtils;
 import android.util.Log;
 import android.widget.Toast;
@@ -47,6 +45,7 @@ import org.videolan.vlc.util.HttpImageLoader;
 import org.videolan.vlc.util.MurmurHash;
 import org.videolan.vlc.util.Permissions;
 import org.videolan.vlc.util.Util;
+import org.videolan.vlc.util.WorkersKt;
 
 import java.io.BufferedOutputStream;
 import java.io.File;
@@ -63,6 +62,9 @@ import java.util.LinkedList;
 import java.util.List;
 import java.util.concurrent.atomic.AtomicReference;
 
+import androidx.annotation.WorkerThread;
+import androidx.fragment.app.FragmentActivity;
+
 public class AudioUtil {
     public final static String TAG = "VLC/AudioUtil";
 
@@ -83,51 +85,73 @@ public class AudioUtil {
 //     */
 //    public static AtomicReference<String> PLAYLIST_DIR = new AtomicReference<>();
 
-    public static void setRingtone(MediaWrapper song, FragmentActivity context){
-        if (!Permissions.canWriteSettings(context)) {
-            Permissions.checkWriteSettingsPermission(context, Permissions.PERMISSION_SYSTEM_RINGTONE);
-            return;
-        }
-        File newringtone = AndroidUtil.UriToFile(song.getUri());
-        if(newringtone == null || !newringtone.exists()) {
-            Toast.makeText(context.getApplicationContext(),context.getString(R.string.ringtone_error), Toast.LENGTH_SHORT).show();
+    public static void setRingtone(final MediaWrapper song, final FragmentActivity context){
+        if (AndroidUtil.isOOrLater && !Permissions.canWriteStorage(context)) {
+            Permissions.askWriteStoragePermission(context, false, new Runnable() {
+                @Override
+                public void run() {
+                    setRingtone(song, context);
+                }
+            });
             return;
         }
-
-        ContentValues values = new ContentValues();
-        values.put(MediaStore.MediaColumns.DATA, newringtone.getAbsolutePath());
-        values.put(MediaStore.MediaColumns.TITLE, song.getTitle());
-        values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
-        values.put(MediaStore.Audio.Media.ARTIST, song.getArtist());
-        values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
-        values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
-        values.put(MediaStore.Audio.Media.IS_ALARM, false);
-        values.put(MediaStore.Audio.Media.IS_MUSIC, false);
-
-        Uri uri = MediaStore.Audio.Media.getContentUriForPath(newringtone.getAbsolutePath());
-        Uri newUri;
-        try {
-            context.getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + newringtone.getAbsolutePath() + "\"", null);
-            newUri = context.getContentResolver().insert(uri, values);
-            RingtoneManager.setActualDefaultRingtoneUri(
-                    context.getApplicationContext(),
-                    RingtoneManager.TYPE_RINGTONE,
-                    newUri
-            );
-        } catch(Exception e) {
-            Log.e(TAG, "error setting ringtone", e);
-            Toast.makeText(context.getApplicationContext(),
-                    context.getString(R.string.ringtone_error),
-                    Toast.LENGTH_SHORT).show();
+        if (!Permissions.canWriteSettings(context)) {
+            Permissions.checkWriteSettingsPermission(context, Permissions.PERMISSION_SYSTEM_RINGTONE);
             return;
         }
+        WorkersKt.runIO(new Runnable() {
+            @Override
+            public void run() {
+                final File newRingtone = AndroidUtil.UriToFile(song.getUri());
+                if (!newRingtone.exists()) {
+                    Toast.makeText(context.getApplicationContext(),context.getString(R.string.ringtone_error), Toast.LENGTH_SHORT).show();
+                    return;
+                }
 
-        Toast.makeText(
-                context.getApplicationContext(),
-                context.getString(R.string.ringtone_set, song.getTitle()),
-                Toast.LENGTH_SHORT)
-                .show();
-
+                final ContentValues values = new ContentValues();
+                values.put(MediaStore.MediaColumns.DATA, newRingtone.getAbsolutePath());
+                values.put(MediaStore.MediaColumns.TITLE, song.getTitle());
+                values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
+                values.put(MediaStore.Audio.Media.ARTIST, song.getArtist());
+                values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
+                values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
+                values.put(MediaStore.Audio.Media.IS_ALARM, false);
+                values.put(MediaStore.Audio.Media.IS_MUSIC, false);
+
+                final Uri uri = MediaStore.Audio.Media.getContentUriForPath(newRingtone.getAbsolutePath());
+                final Uri newUri;
+                try {
+                    context.getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + newRingtone.getAbsolutePath() + "\"", null);
+                    newUri = context.getContentResolver().insert(uri, values);
+                    RingtoneManager.setActualDefaultRingtoneUri(
+                            context.getApplicationContext(),
+                            RingtoneManager.TYPE_RINGTONE,
+                            newUri
+                    );
+                } catch(Exception e) {
+                    Log.e(TAG, "error setting ringtone", e);
+                    WorkersKt.runOnMainThread(new Runnable() {
+                        @Override
+                        public void run() {
+                            Toast.makeText(context.getApplicationContext(),
+                                    context.getString(R.string.ringtone_error),
+                                    Toast.LENGTH_SHORT).show();
+                        }
+                    });
+                    return;
+                }
+                WorkersKt.runOnMainThread(new Runnable() {
+                    @Override
+                    public void run() {
+                        Toast.makeText(
+                                context.getApplicationContext(),
+                                context.getString(R.string.ringtone_set, song.getTitle()),
+                                Toast.LENGTH_SHORT)
+                                .show();
+                    }
+                });
+            }
+        });
     }
 
     @SuppressLint("NewApi")
diff --git a/vlc-android/src/org/videolan/vlc/util/Permissions.java b/vlc-android/src/org/videolan/vlc/util/Permissions.java
index 161deef62..3b99c7886 100644
--- a/vlc-android/src/org/videolan/vlc/util/Permissions.java
+++ b/vlc-android/src/org/videolan/vlc/util/Permissions.java
@@ -34,17 +34,18 @@ import android.content.SharedPreferences;
 import android.content.pm.PackageManager;
 import android.net.Uri;
 import android.os.Build;
-import androidx.core.app.ActivityCompat;
-import androidx.fragment.app.FragmentActivity;
-import androidx.core.content.ContextCompat;
-import androidx.appcompat.app.AlertDialog;
-import androidx.appcompat.app.AppCompatActivity;
 
 import org.videolan.libvlc.util.AndroidUtil;
 import org.videolan.vlc.R;
 import org.videolan.vlc.VLCApplication;
 import org.videolan.vlc.gui.helpers.hf.StoragePermissionsDelegate;
 
+import androidx.appcompat.app.AlertDialog;
+import androidx.appcompat.app.AppCompatActivity;
+import androidx.core.app.ActivityCompat;
+import androidx.core.content.ContextCompat;
+import androidx.fragment.app.FragmentActivity;
+
 public class Permissions {
 
     public static final int PERMISSION_STORAGE_TAG = 255;
@@ -80,7 +81,7 @@ public class Permissions {
         return canWriteStorage(VLCApplication.getAppContext());
     }
 
-    private static boolean canWriteStorage(Context context) {
+    public static boolean canWriteStorage(Context context) {
         return ContextCompat.checkSelfPermission(context,
                 Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
     }



More information about the Android mailing list