From 13b7369794ad1ed7288cc9414b01986b34441756 Mon Sep 17 00:00:00 2001 From: Devansh Date: Tue, 2 Feb 2016 10:44:47 +0530 Subject: [PATCH] Add functionality to set Ringtone --- res/values/translatable.xml | 1 + src/org/kreed/vanilla/LibraryActivity.java | 55 +++++++++++++++++++++- 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/res/values/translatable.xml b/res/values/translatable.xml index 9ac927b4..32deb9f3 100644 --- a/res/values/translatable.xml +++ b/res/values/translatable.xml @@ -108,6 +108,7 @@ THE SOFTWARE. All Songs More from artist More from album + Set As Ringtone Name Number of tracks diff --git a/src/org/kreed/vanilla/LibraryActivity.java b/src/org/kreed/vanilla/LibraryActivity.java index e4046777..c8e5b1d1 100644 --- a/src/org/kreed/vanilla/LibraryActivity.java +++ b/src/org/kreed/vanilla/LibraryActivity.java @@ -464,6 +464,49 @@ private void pickSongs(Intent intent, int action) } } + /** + * Make the selected song , ringtone of the mobile + */ + + private void makeRingtone(Intent intent){ + long id = intent.getLongExtra("id", LibraryAdapter.INVALID_ID); + Uri trackUri = ContentUris.withAppendedId( + android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, + id); + String path = getRealPathFromURI(this,trackUri); + Uri uri = MediaStore.Audio.Media.getContentUriForPath(path); + ContentValues values = new ContentValues(); + values.put(MediaStore.MediaColumns.DATA, path); + values.put(MediaStore.MediaColumns.TITLE,intent.getStringExtra(LibraryAdapter.DATA_TITLE)); + values.put(MediaStore.MediaColumns.MIME_TYPE,""); + values.put(MediaStore.Audio.Media.ARTIST,""); + 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); + + this.getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + path + "\"", null); + Uri newUri = this.getContentResolver().insert(uri, values); + + RingtoneManager.setActualDefaultRingtoneUri(this, + RingtoneManager.TYPE_RINGTONE, newUri); + } + + public String getRealPathFromURI(Context context, Uri contentUri) { + Cursor cursor = null; + try { + String[] proj = { MediaStore.Images.Media.DATA }; + cursor = context.getContentResolver().query(contentUri, proj, null, null, null); + int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); + cursor.moveToFirst(); + return cursor.getString(column_index); + } finally { + if (cursor != null) { + cursor.close(); + } + } + } + /** * "Expand" the view represented by the given intent by setting the limiter * from the view and switching to the appropriate tab. @@ -702,6 +745,7 @@ private QueryTask buildQueryFromIntent(Intent intent, boolean empty, boolean all private static final int MENU_ENQUEUE_ALL = 10; private static final int MENU_MORE_FROM_ALBUM = 11; private static final int MENU_MORE_FROM_ARTIST = 12; + private static final int MENU_RINGTONE = 13; /** * Creates a context menu for an adapter row. @@ -735,8 +779,10 @@ public void onCreateContextMenu(ContextMenu menu, Intent rowData) } if (type == MediaUtils.TYPE_ALBUM || type == MediaUtils.TYPE_SONG) menu.add(0, MENU_MORE_FROM_ARTIST, 0, R.string.more_from_artist).setIntent(rowData); - if (type == MediaUtils.TYPE_SONG) + if (type == MediaUtils.TYPE_SONG){ menu.add(0, MENU_MORE_FROM_ALBUM, 0, R.string.more_from_album).setIntent(rowData); + menu.add(0,MENU_RINGTONE,0,R.string.set_as_ringtone).setIntent(rowData); + } menu.addSubMenu(0, MENU_ADD_TO_PLAYLIST, 0, R.string.add_to_playlist).getItem().setIntent(rowData); menu.add(0, MENU_DELETE, 0, R.string.delete).setIntent(rowData); } @@ -880,11 +926,16 @@ public boolean onContextItemSelected(MenuItem item) updateLimiterViews(); break; } - case MENU_MORE_FROM_ALBUM: + case MENU_MORE_FROM_ALBUM: { setLimiter(MediaUtils.TYPE_ALBUM, "_id=" + intent.getLongExtra(LibraryAdapter.DATA_ID, LibraryAdapter.INVALID_ID)); updateLimiterViews(); break; } + case MENU_RINGTONE:{ + makeRingtone(intent); + break; + } + } return true; }