Fix ringtone, file sharing
This commit is contained in:
parent
c9811ca06b
commit
fce012aaf3
20 changed files with 261 additions and 196 deletions
|
@ -25,11 +25,14 @@ import android.net.Uri;
|
|||
import android.os.Environment;
|
||||
import android.provider.BaseColumns;
|
||||
import android.provider.MediaStore;
|
||||
import android.provider.Settings;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.core.content.FileProvider;
|
||||
|
||||
import org.jaudiotagger.audio.AudioFileIO;
|
||||
import org.jaudiotagger.tag.FieldKey;
|
||||
|
||||
|
@ -40,9 +43,6 @@ import java.util.List;
|
|||
import java.util.Locale;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.core.content.FileProvider;
|
||||
import code.name.monkey.retromusic.R;
|
||||
import code.name.monkey.retromusic.helper.MusicPlayerRemote;
|
||||
import code.name.monkey.retromusic.loaders.PlaylistLoader;
|
||||
|
@ -72,9 +72,9 @@ public class MusicUtil {
|
|||
|
||||
@NonNull
|
||||
public static Intent createShareSongFileIntent(@NonNull final Song song, @NonNull Context context) {
|
||||
Uri file = FileProvider.getUriForFile(context, context.getPackageName() + ".provider", new File(song.getData()));
|
||||
try {
|
||||
return new Intent().setAction(Intent.ACTION_SEND).putExtra(Intent.EXTRA_STREAM,
|
||||
FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName(), new File(song.getData())))
|
||||
return new Intent().setAction(Intent.ACTION_SEND).putExtra(Intent.EXTRA_STREAM, file)
|
||||
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
||||
.setType("audio/*");
|
||||
} catch (IllegalArgumentException e) {
|
||||
|
@ -84,41 +84,6 @@ public class MusicUtil {
|
|||
}
|
||||
}
|
||||
|
||||
public static void setRingtone(@NonNull final Context context, final int id) {
|
||||
final ContentResolver resolver = context.getContentResolver();
|
||||
final Uri uri = getSongFileUri(id);
|
||||
try {
|
||||
final ContentValues values = new ContentValues(2);
|
||||
values.put(MediaStore.Audio.AudioColumns.IS_RINGTONE, "1");
|
||||
values.put(MediaStore.Audio.AudioColumns.IS_ALARM, "1");
|
||||
resolver.update(uri, values, null, null);
|
||||
} catch (@NonNull final UnsupportedOperationException ignored) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
Cursor cursor = resolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
|
||||
new String[]{MediaStore.MediaColumns.TITLE},
|
||||
BaseColumns._ID + "=?",
|
||||
new String[]{String.valueOf(id)},
|
||||
null);
|
||||
try {
|
||||
if (cursor != null && cursor.getCount() == 1) {
|
||||
cursor.moveToFirst();
|
||||
Settings.System.putString(resolver, Settings.System.RINGTONE, uri.toString());
|
||||
final String message = context
|
||||
.getString(R.string.x_has_been_set_as_ringtone, cursor.getString(0));
|
||||
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
} finally {
|
||||
if (cursor != null) {
|
||||
cursor.close();
|
||||
}
|
||||
}
|
||||
} catch (SecurityException ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public static String getSongInfoString(@NonNull Song song) {
|
||||
return MusicUtil.buildInfoString(
|
||||
|
|
|
@ -0,0 +1,89 @@
|
|||
/*
|
||||
* Copyright (c) 2019 Hemanth Savarala.
|
||||
*
|
||||
* Licensed under the GNU General Public License v3
|
||||
*
|
||||
* This 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 3 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This software 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.
|
||||
*/
|
||||
|
||||
package code.name.monkey.retromusic.util
|
||||
|
||||
import android.content.ContentValues
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.provider.BaseColumns
|
||||
import android.provider.MediaStore
|
||||
import android.provider.Settings
|
||||
import android.widget.Toast
|
||||
import code.name.monkey.retromusic.R
|
||||
import code.name.monkey.retromusic.model.Song
|
||||
import code.name.monkey.retromusic.util.MusicUtil.getSongFileUri
|
||||
import com.afollestad.materialdialogs.MaterialDialog
|
||||
|
||||
class RingtoneManager(val context: Context) {
|
||||
fun setRingtone(song: Song) {
|
||||
val resolver = context.contentResolver
|
||||
val uri = getSongFileUri(song.id)
|
||||
try {
|
||||
val values = ContentValues(2)
|
||||
values.put(MediaStore.Audio.AudioColumns.IS_RINGTONE, "1")
|
||||
values.put(MediaStore.Audio.AudioColumns.IS_ALARM, "1")
|
||||
resolver.update(uri, values, null, null)
|
||||
} catch (ignored: UnsupportedOperationException) {
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
val cursor = resolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
|
||||
arrayOf(MediaStore.MediaColumns.TITLE),
|
||||
BaseColumns._ID + "=?",
|
||||
arrayOf(song.id.toString()), null)
|
||||
cursor.use { cursorSong ->
|
||||
if (cursorSong != null && cursorSong.count == 1) {
|
||||
cursorSong.moveToFirst()
|
||||
Settings.System.putString(resolver, Settings.System.RINGTONE, uri.toString())
|
||||
val message = context
|
||||
.getString(R.string.x_has_been_set_as_ringtone, cursorSong.getString(0))
|
||||
Toast.makeText(context, message, Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
} catch (ignored: SecurityException) {
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val TAG = "RingtoneManager"
|
||||
|
||||
fun requiresDialog(context: Context): Boolean {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
if (!Settings.System.canWrite(context)) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
fun getDialog(context: Context): MaterialDialog {
|
||||
return MaterialDialog(context).show {
|
||||
title(R.string.dialog_title_set_ringtone)
|
||||
message(R.string.dialog_message_set_ringtone)
|
||||
positiveButton(android.R.string.ok) {
|
||||
val intent = Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS)
|
||||
intent.data = Uri.parse("package:" + context.applicationContext.packageName)
|
||||
context.startActivity(intent)
|
||||
}
|
||||
negativeButton(android.R.string.cancel)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue