[Android] Set ringtone from VLC

Alexandre Perraud git at videolan.org
Fri Aug 24 17:31:04 CEST 2012


vlc-ports/android | branch: master | Alexandre Perraud <4leyx4ndre at gmail.com> | Fri Aug 24 16:44:19 2012 +0200| [ee2269b49594a8c71c7c7ef491fb7b6f9ef18533] | committer: Jean-Baptiste Kempf

Set ringtone from VLC

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

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

 vlc-android/AndroidManifest.xml                    |    1 +
 vlc-android/res/menu/audio_list_browser.xml        |    8 ++-
 vlc-android/res/values-fr/strings.xml              |    1 +
 vlc-android/res/values/strings.xml                 |    1 +
 vlc-android/src/org/videolan/vlc/Util.java         |    9 ++++
 .../vlc/gui/audio/AudioBrowserFragment.java        |   12 ++++-
 .../videolan/vlc/gui/audio/AudioListFragment.java  |    8 +++
 .../src/org/videolan/vlc/gui/audio/AudioUtil.java  |   55 ++++++++++++++++++++
 8 files changed, 93 insertions(+), 2 deletions(-)

diff --git a/vlc-android/AndroidManifest.xml b/vlc-android/AndroidManifest.xml
index 12a271f..48d592b 100644
--- a/vlc-android/AndroidManifest.xml
+++ b/vlc-android/AndroidManifest.xml
@@ -14,6 +14,7 @@
     <uses-permission android:name="android.permission.READ_PHONE_STATE" />
     <uses-permission android:name="android.permission.READ_LOGS" />
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
+    <uses-permission android:name="android.permission.WRITE_SETTINGS"/>
 
     <application
         android:name="org.videolan.vlc.VLCApplication"
diff --git a/vlc-android/res/menu/audio_list_browser.xml b/vlc-android/res/menu/audio_list_browser.xml
index 77dc7a8..7bdfa5b 100644
--- a/vlc-android/res/menu/audio_list_browser.xml
+++ b/vlc-android/res/menu/audio_list_browser.xml
@@ -15,6 +15,12 @@
 	        android:title="@string/append_all" />
 	    <item
 	        android:id="@+id/audio_list_browser_delete"
-	        android:title="@string/delete" />
+	        android:title="@string/delete"/>
     </group>
+    <group android:id="@+id/phone_only" >
+	    <item
+	        android:id="@+id/audio_list_browser_set_song"
+	        android:title="@string/set_song"/>
+    </group>
+
 </menu>
\ No newline at end of file
diff --git a/vlc-android/res/values-fr/strings.xml b/vlc-android/res/values-fr/strings.xml
index 50e35b2..0cf9593 100644
--- a/vlc-android/res/values-fr/strings.xml
+++ b/vlc-android/res/values-fr/strings.xml
@@ -22,6 +22,7 @@
     <string name="play_all">Lire tout</string>
     <string name="append_all">Lire tout à la suite</string>
     <string name="delete">Supprimer</string>
+    <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="pause">Pause</string>
diff --git a/vlc-android/res/values/strings.xml b/vlc-android/res/values/strings.xml
index 74db932..7c9d0d3 100644
--- a/vlc-android/res/values/strings.xml
+++ b/vlc-android/res/values/strings.xml
@@ -24,6 +24,7 @@
     <string name="play_all">Play all</string>
     <string name="append_all">Append all</string>
     <string name="delete">Delete</string>
+    <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="pause">Pause</string>
diff --git a/vlc-android/src/org/videolan/vlc/Util.java b/vlc-android/src/org/videolan/vlc/Util.java
index d02d45f..185a11e 100644
--- a/vlc-android/src/org/videolan/vlc/Util.java
+++ b/vlc-android/src/org/videolan/vlc/Util.java
@@ -36,6 +36,7 @@ import java.util.Properties;
 import android.content.Context;
 import android.graphics.Bitmap;
 import android.net.Uri;
+import android.telephony.TelephonyManager;
 import android.util.DisplayMetrics;
 import android.util.TypedValue;
 import android.view.Display;
@@ -330,4 +331,12 @@ public class Util {
         isCompatible = true;
         return true;
     }
+    public static boolean isPhone(){
+        TelephonyManager manager = (TelephonyManager)VLCApplication.getAppContext().getSystemService(Context.TELEPHONY_SERVICE);
+        if(manager.getPhoneType() == TelephonyManager.PHONE_TYPE_NONE){
+            return false;
+        }else{
+            return true;
+        }
+    }
 }
diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.java b/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.java
index fb425db..3eb80af 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.java
@@ -28,6 +28,7 @@ import org.videolan.vlc.AudioServiceController;
 import org.videolan.vlc.Media;
 import org.videolan.vlc.MediaLibrary;
 import org.videolan.vlc.R;
+import org.videolan.vlc.Util;
 import org.videolan.vlc.VlcRunnable;
 import org.videolan.vlc.WeakHandler;
 import org.videolan.vlc.gui.CommonDialogs;
@@ -234,8 +235,12 @@ public class AudioBrowserFragment extends SherlockFragment implements ISortable
         MenuInflater inflater = getActivity().getMenuInflater();
         inflater.inflate(R.menu.audio_list_browser, menu);
 
-        if (v.getId() != R.id.songs_list)
+        if (v.getId() != R.id.songs_list) {
             menu.setGroupEnabled(R.id.songs_view_only, false);
+            menu.setGroupEnabled(R.id.phone_only, false);
+        }
+        if (!Util.isPhone())
+            menu.setGroupVisible(R.id.phone_only, false);
     }
 
     @Override
@@ -283,6 +288,11 @@ public class AudioBrowserFragment extends SherlockFragment implements ISortable
             return true;
         }
 
+        if (id == R.id.audio_list_browser_set_song) {
+            AudioUtil.setRingtone(mSongsAdapter.getItem(groupPosition),getActivity());
+            return true;
+        }
+
         if (useAllItems) {
             startPosition = groupPosition;
             medias = mSongsAdapter.getLocations();
diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/AudioListFragment.java b/vlc-android/src/org/videolan/vlc/gui/audio/AudioListFragment.java
index 651c85c..82c7b44 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/AudioListFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AudioListFragment.java
@@ -28,6 +28,7 @@ import org.videolan.vlc.AudioServiceController;
 import org.videolan.vlc.Media;
 import org.videolan.vlc.MediaLibrary;
 import org.videolan.vlc.R;
+import org.videolan.vlc.Util;
 import org.videolan.vlc.VlcRunnable;
 import org.videolan.vlc.WeakHandler;
 import org.videolan.vlc.gui.CommonDialogs;
@@ -128,6 +129,8 @@ public class AudioListFragment extends SherlockListFragment {
     public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
         MenuInflater inflater = getActivity().getMenuInflater();
         inflater.inflate(R.menu.audio_list_browser, menu);
+        if (!Util.isPhone())
+            menu.setGroupVisible(R.id.phone_only, false);
     }
 
     @Override
@@ -161,6 +164,11 @@ public class AudioListFragment extends SherlockListFragment {
             return true;
         }
 
+        if (id == R.id.audio_list_browser_set_song) {
+            AudioUtil.setRingtone(mSongsAdapter.getItem(menuInfo.position),getActivity());
+            return true;
+        }
+
         if (useAllItems) {
             startPosition = menuInfo.position;
             medias = mSongsAdapter.getLocations();
diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/AudioUtil.java b/vlc-android/src/org/videolan/vlc/gui/audio/AudioUtil.java
new file mode 100644
index 0000000..bafacfa
--- /dev/null
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AudioUtil.java
@@ -0,0 +1,55 @@
+/*****************************************************************************
+ * AudioUtil.java
+ *****************************************************************************
+ * Copyright © 2011-2012 VLC authors and VideoLAN
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+package org.videolan.vlc.gui.audio;
+
+import java.io.File;
+
+import org.videolan.vlc.Media;
+import org.videolan.vlc.Util;
+
+import android.app.Activity;
+import android.content.ContentValues;
+import android.media.RingtoneManager;
+import android.net.Uri;
+import android.provider.MediaStore;
+
+public class AudioUtil {
+
+    public static void setRingtone( Media song, Activity activity){
+        File newringtone = Util.URItoFile(song.getLocation());
+        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());
+        activity.getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + newringtone.getAbsolutePath() + "\"", null);
+        Uri newUri = activity.getContentResolver().insert(uri, values);
+        RingtoneManager.setActualDefaultRingtoneUri(
+                activity.getApplicationContext(),
+                RingtoneManager.TYPE_RINGTONE,
+                newUri
+                );
+    }
+}



More information about the Android mailing list