[Android] Ringtone : add some controls to avoid crash

Alexandre Perraud git at videolan.org
Tue Jan 22 23:44:40 CET 2013


vlc-ports/android | branch: master | Alexandre Perraud <4leyx4ndre at gmail.com> | Tue Jan 22 17:22:20 2013 +0100| [8c62356915acd535850597ecd3360f5051303994] | committer: Jean-Baptiste Kempf

Ringtone : add some controls to avoid crash

Signed-off-by: Jean-Baptiste Kempf <jb at videolan.org>

> http://git.videolan.org/gitweb.cgi/vlc-ports/android.git/?a=commit;h=8c62356915acd535850597ecd3360f5051303994
---

 vlc-android/res/values-fr/strings.xml              |    4 ++++
 vlc-android/res/values/strings.xml                 |    3 +++
 .../src/org/videolan/vlc/gui/audio/AudioUtil.java  |   25 ++++++++++++++++++--
 3 files changed, 30 insertions(+), 2 deletions(-)

diff --git a/vlc-android/res/values-fr/strings.xml b/vlc-android/res/values-fr/strings.xml
index fe3d93e..8b28a9c 100644
--- a/vlc-android/res/values-fr/strings.xml
+++ b/vlc-android/res/values-fr/strings.xml
@@ -26,6 +26,7 @@
     <string name="set_song">Définir comme sonnerie</string>
     <string name="info">Information</string>
     <string name="confirm_delete">Supprimer le média \'%1$s\' ?</string>
+    <string name="ringtone_set"> \'%1$s\' est maintenant votre sonnerie.</string>
     <string name="pause">Pause</string>
     <string name="not_show_again">Ne plus afficher ce message.</string>
     <string name="beta_warning">Ceci est une version bêta.\n\nCette version est INSTABLE !\n\nN\'utiliser que si vous êtes sûr de vous.\n\nCela pourrait tuer votre chat ou détruire votre maison !\n\n Amusez-vous bien ;)</string>
@@ -34,6 +35,9 @@
     <string name="unknown_album">Album inconnu</string>
     <string name="unknown_genre">Genre inconnu</string>
     <string name="songs">Chansons</string>
+
+    <string name="error_generic">Désolé, une erreur est survenue…</string>
+
     <plurals name="songs_quantity">
         <item quantity="one">1 chanson</item>
         <item quantity="other">%d chansons</item>
diff --git a/vlc-android/res/values/strings.xml b/vlc-android/res/values/strings.xml
index a5603d5..e21d667 100644
--- a/vlc-android/res/values/strings.xml
+++ b/vlc-android/res/values/strings.xml
@@ -30,6 +30,7 @@
     <string name="set_song">Set as ringtone</string>
     <string name="info">Information</string>
     <string name="confirm_delete">Delete the file \'%1$s\' ?</string>
+    <string name="ringtone_set"> \'%1$s\' is correctly set as your ringtone.</string>
     <string name="pause">Pause</string>
     <string name="not_show_again">"Don't show this message again."</string>
     <string name="beta_warning">This is a beta version.\n\nThis version is NOT STABLE!\n\nDo NOT use, unless you know what you are doing.\n\nIt might kill your kitten and destroy your house.\nUse it at your own risk.\n\nHave Fun! :)</string>
@@ -39,6 +40,8 @@
     <string name="unknown_genre">Unknown Genre</string>
     <string name="songs">Songs</string>
 
+    <string name="error_generic">Sorry, a error has occurred…</string>
+
     <plurals name="songs_quantity">
         <item quantity="one">1 song</item>
         <item quantity="other">%d songs</item>
diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/AudioUtil.java b/vlc-android/src/org/videolan/vlc/gui/audio/AudioUtil.java
index e36441d..7274a34 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/AudioUtil.java
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AudioUtil.java
@@ -50,6 +50,7 @@ import android.net.Uri;
 import android.os.Environment;
 import android.provider.MediaStore;
 import android.util.Log;
+import android.widget.Toast;
 
 public class AudioUtil {
 
@@ -60,6 +61,11 @@ public class AudioUtil {
 
     public static void setRingtone( Media song, Activity activity){
         File newringtone = Util.URItoFile(song.getLocation());
+        if (!newringtone.exists()) {
+            Toast.makeText(activity.getApplicationContext(),activity.getString(R.string.error_generic), Toast.LENGTH_SHORT).show();
+            return;
+        }
+
         ContentValues values = new ContentValues();
         values.put(MediaStore.MediaColumns.DATA, newringtone.getAbsolutePath());
         values.put(MediaStore.MediaColumns.TITLE, song.getTitle());
@@ -69,14 +75,29 @@ public class AudioUtil {
         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());
-        activity.getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + newringtone.getAbsolutePath() + "\"", null);
-        Uri newUri = activity.getContentResolver().insert(uri, values);
+        Uri newUri;
+        try {
+            activity.getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + newringtone.getAbsolutePath() + "\"", null);
+            newUri = activity.getContentResolver().insert(uri, values);
+        } catch (Exception e) {
+            e.printStackTrace();
+            Toast.makeText(activity.getApplicationContext(),activity.getString(R.string.error_generic), Toast.LENGTH_SHORT).show();
+            return;
+        }
+
         RingtoneManager.setActualDefaultRingtoneUri(
                 activity.getApplicationContext(),
                 RingtoneManager.TYPE_RINGTONE,
                 newUri
                 );
+        Toast.makeText(
+                activity.getApplicationContext(),
+                activity.getString(R.string.ringtone_set, song.getTitle()),
+                Toast.LENGTH_SHORT)
+                .show();
+
     }
 
     @SuppressLint("NewApi")



More information about the Android mailing list