From 62a87233446574a6bce93505b18b6c9fd6e128e1 Mon Sep 17 00:00:00 2001 From: jens Date: Fri, 13 Jan 2023 18:48:00 +0100 Subject: [PATCH] Setting ringtones from profiles works again --- app/build.gradle | 2 +- .../automation2/ActivityManageProfile.java | 58 ++++++----- .../java/com/jens/automation2/Profile.java | 99 ++++++++++--------- .../jens/automation2/XmlFileInterface.java | 12 +-- .../metadata/android/en-US/changelogs/123.txt | 3 +- 5 files changed, 94 insertions(+), 80 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index f9631c07..1a29e00b 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -12,7 +12,7 @@ android { buildToolsVersion '29.0.2' useLibrary 'org.apache.http.legacy' versionCode 123 - versionName "1.7.7" + versionName "1.7.8" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } diff --git a/app/src/main/java/com/jens/automation2/ActivityManageProfile.java b/app/src/main/java/com/jens/automation2/ActivityManageProfile.java index 273ab22a..affd7384 100644 --- a/app/src/main/java/com/jens/automation2/ActivityManageProfile.java +++ b/app/src/main/java/com/jens/automation2/ActivityManageProfile.java @@ -47,37 +47,37 @@ public class ActivityManageProfile extends Activity boolean guiUpdate = false; - File incomingCallsRingtone = null, notificationsRingtone = null; + String incomingCallsRingtone = null, notificationsRingtone = null; ArrayAdapter soundModeAdapter; ArrayAdapter dndModeAdapter; - public void setIncomingCallsRingtone(File incomingCallsRingtone) + public void setIncomingCallsRingtone(String incomingCallsRingtone) { this.incomingCallsRingtone = incomingCallsRingtone; if(incomingCallsRingtone != null) - tvIncomingCallsRingtone.setText(this.incomingCallsRingtone.getAbsolutePath()); + tvIncomingCallsRingtone.setText(this.incomingCallsRingtone); else tvIncomingCallsRingtone.setText(getResources().getString(R.string.none)); } - public File getIncomingCallsRingtone() + public String getIncomingCallsRingtone() { return incomingCallsRingtone; } - public void setNotificationsRingtone(File notificationsRingtone) + public void setNotificationsRingtone(String notificationsRingtone) { this.notificationsRingtone = notificationsRingtone; if(this.notificationsRingtone != null) - tvNotificationsRingtone.setText(this.notificationsRingtone.getAbsolutePath()); + tvNotificationsRingtone.setText(this.notificationsRingtone); else tvNotificationsRingtone.setText(getResources().getString(R.string.none)); } - public File getNotificationsRingtone() + public String getNotificationsRingtone() { return notificationsRingtone; } @@ -284,26 +284,26 @@ public class ActivityManageProfile extends Activity @Override public void onClick(View v) { - try - { - Intent fileIntent = new Intent(Intent.ACTION_GET_CONTENT); - fileIntent.setType("audio/*"); - startActivityForResult(Intent.createChooser(fileIntent, "Select a ringtone"), intentCodeRingtonePickerCallsFile); - } - catch(ActivityNotFoundException e) - { +// try +// { +// Intent fileIntent = new Intent(Intent.ACTION_GET_CONTENT); +// fileIntent.setType("audio/*"); +// startActivityForResult(Intent.createChooser(fileIntent, "Select a ringtone"), intentCodeRingtonePickerCallsFile); +// } +// catch(ActivityNotFoundException e) +// { // Use media browser instead Intent fileSelectionIntent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER); if(ActivityMainProfiles.profileToEdit != null) { - Uri currenturi = Uri.parse(ActivityMainProfiles.profileToEdit.incomingCallsRingtone.getAbsolutePath()); + Uri currenturi = Uri.parse(ActivityMainProfiles.profileToEdit.incomingCallsRingtone); if(ActivityMainProfiles.profileToEdit.changeIncomingCallsRingtone) fileSelectionIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, currenturi); } startActivityForResult(fileSelectionIntent, intentCodeRingtonePickerCallsRingtone); - } +// } } }); bChangeSoundNotifications.setOnClickListener(new OnClickListener() @@ -324,7 +324,7 @@ public class ActivityManageProfile extends Activity if(ActivityMainProfiles.profileToEdit != null) { - Uri currenturi = Uri.parse(ActivityMainProfiles.profileToEdit.notificationRingtone.getAbsolutePath()); + Uri currenturi = Uri.parse(ActivityMainProfiles.profileToEdit.notificationRingtone); if(ActivityMainProfiles.profileToEdit.changeNotificationRingtone) fileSelectionIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, currenturi); } @@ -495,15 +495,20 @@ public class ActivityManageProfile extends Activity Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI); if (uri != null) { - String ringTonePath = CompensateCrappyAndroidPaths.getPath(ActivityManageProfile.this, uri); - setIncomingCallsRingtone(new File(ringTonePath)); +// if(Build.VERSION.SDK_INT < 26) +// { +// String ringTonePath = CompensateCrappyAndroidPaths.getPath(ActivityManageProfile.this, uri); +// setIncomingCallsRingtone(ringTonePath); +// } +// else + setIncomingCallsRingtone(uri.toString()); } break; } case intentCodeRingtonePickerCallsFile: { String ringTonePath = CompensateCrappyAndroidPaths.getPath(ActivityManageProfile.this, data.getData()); - setIncomingCallsRingtone(new File(ringTonePath)); + setIncomingCallsRingtone(ringTonePath); break; } case intentCodeRingtonePickerNotificationsRingtone: // notifications @@ -511,15 +516,20 @@ public class ActivityManageProfile extends Activity Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI); if (uri != null) { - String ringTonePath = CompensateCrappyAndroidPaths.getPath(ActivityManageProfile.this, data.getData()); - setNotificationsRingtone(new File(ringTonePath)); +// if(Build.VERSION.SDK_INT < 26) +// { +// String ringTonePath = CompensateCrappyAndroidPaths.getPath(ActivityManageProfile.this, uri); +// setNotificationsRingtone(ringTonePath); +// } +// else + setNotificationsRingtone(uri.toString()); } break; } case intentCodeRingtonePickerNotificationsFile: { String ringTonePath = CompensateCrappyAndroidPaths.getPath(ActivityManageProfile.this, data.getData()); - setNotificationsRingtone(new File(ringTonePath)); + setNotificationsRingtone(ringTonePath); break; } default: diff --git a/app/src/main/java/com/jens/automation2/Profile.java b/app/src/main/java/com/jens/automation2/Profile.java index c70b847d..3fb924a7 100644 --- a/app/src/main/java/com/jens/automation2/Profile.java +++ b/app/src/main/java/com/jens/automation2/Profile.java @@ -3,6 +3,7 @@ package com.jens.automation2; import android.app.NotificationManager; import android.content.ContentValues; import android.content.Context; +import android.database.Cursor; import android.media.AudioManager; import android.media.RingtoneManager; import android.net.Uri; @@ -41,13 +42,13 @@ public class Profile implements Comparable protected int volumeAlarms; protected boolean changeIncomingCallsRingtone; - protected File incomingCallsRingtone; + protected String incomingCallsRingtone; protected boolean changeVibrateWhenRinging; protected boolean vibrateWhenRinging; protected boolean changeNotificationRingtone; - protected File notificationRingtone; + protected String notificationRingtone; protected boolean changeAudibleSelection; protected boolean audibleSelection; @@ -171,11 +172,11 @@ public class Profile implements Comparable return changeIncomingCallsRingtone; } - public void setIncomingCallsRingtone(File incomingCallsRingtone) + public void setIncomingCallsRingtone(String incomingCallsRingtone) { this.incomingCallsRingtone = incomingCallsRingtone; } - public File getIncomingCallsRingtone() + public String getIncomingCallsRingtone() { return incomingCallsRingtone; } @@ -207,11 +208,11 @@ public class Profile implements Comparable return changeNotificationRingtone; } - public void setNotificationRingtone(File notificationsRingtone) + public void setNotificationRingtone(String notificationsRingtone) { this.notificationRingtone = notificationsRingtone; } - public File getNotificationRingtone() + public String getNotificationRingtone() { return notificationRingtone; } @@ -292,9 +293,9 @@ public class Profile implements Comparable return null; } - private boolean applyRingTone(File ringtoneFile, int ringtoneType, Context context) + private boolean applyRingTone(String ringtoneFile, int ringtoneType, Context context) { - Miscellaneous.logEvent("i", "Profile", "Request to set ringtone to " + ringtoneFile.getAbsolutePath(), 3); + Miscellaneous.logEvent("i", "Profile", "Request to set ringtone to " + ringtoneFile, 3); // if(!ringtoneFile.exists() || !ringtoneFile.canRead()) // { @@ -304,55 +305,57 @@ public class Profile implements Comparable // return false; // } - ContentValues values = new ContentValues(); - values.put(MediaStore.MediaColumns.DATA, ringtoneFile.getAbsolutePath()); - values.put(MediaStore.MediaColumns.TITLE, ringtoneFile.getName()); - values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3"); - values.put(MediaStore.MediaColumns.SIZE, ringtoneFile.length()); - values.put(MediaStore.Audio.Media.IS_RINGTONE, ringtoneType == RingtoneManager.TYPE_RINGTONE); - values.put(MediaStore.Audio.Media.IS_NOTIFICATION, ringtoneType == RingtoneManager.TYPE_NOTIFICATION); - values.put(MediaStore.Audio.Media.IS_ALARM, false); - values.put(MediaStore.Audio.Media.IS_MUSIC, false); - - try + // Set by URI + if(ringtoneFile.contains("//")) { - Uri newRingTone = null; + Uri target = Uri.parse(ringtoneFile); + RingtoneManager.setActualDefaultRingtoneUri(context, ringtoneType, target); + Miscellaneous.logEvent("i", "Profile", "Ringtone set to: " + ringtoneFile, 1); + return true; + } // Set by filepath + else + { + ContentValues values = new ContentValues(); + values.put(MediaStore.MediaColumns.DATA, ringtoneFile); + values.put(MediaStore.MediaColumns.TITLE, ringtoneFile); + //values.put(MediaStore.MediaColumns.TITLE, ringtoneFile.getName()); + values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3"); + values.put(MediaStore.MediaColumns.SIZE, ringtoneFile.length()); + values.put(MediaStore.Audio.Media.IS_RINGTONE, ringtoneType == RingtoneManager.TYPE_RINGTONE); + values.put(MediaStore.Audio.Media.IS_NOTIFICATION, ringtoneType == RingtoneManager.TYPE_NOTIFICATION); + values.put(MediaStore.Audio.Media.IS_ALARM, false); + values.put(MediaStore.Audio.Media.IS_MUSIC, false); - //TODO: This part needs to be made compatible with Android 11 and above. - if(Build.VERSION.SDK_INT < 30) + try { - Uri existingRingTone = MediaStore.Audio.Media.getContentUriForPath(ringtoneFile.getAbsolutePath()); + Uri newRingTone = null; - if (existingRingTone != null) - context.getContentResolver().delete(existingRingTone, MediaStore.MediaColumns.DATA + "=\"" + ringtoneFile.getAbsolutePath() + "\"", null); + Uri existingRingTone = MediaStore.Audio.Media.getContentUriForPath(ringtoneFile); + + try + { + if (existingRingTone != null) + context.getContentResolver().delete(existingRingTone, MediaStore.MediaColumns.DATA + "=\"" + ringtoneFile + "\"", null); + } + catch(Exception e) + { + Miscellaneous.logEvent("w","Delete file from ringtones", "Deleting ringtone from library failed: " + Log.getStackTraceString(e), 2); + } newRingTone = context.getContentResolver().insert(existingRingTone, values); - } - /* - Uri uri = MediaStore.Audio.Media.getContentUriForPath(newSoundFile.getAbsolutePath()); - Uri newUri = mCr.insert(uri, values); - try { - Uri rUri = RingtoneManager.getValidRingtoneUri(this); - if (rUri != null) - ringtoneManager.setStopPreviousRingtone(true); - RingtoneManager.setActualDefaultRingtoneUri(getApplicationContext(), RingtoneManager.TYPE_RINGTONE, newUri); - Toast.makeText(this, "New Rigntone set", Toast.LENGTH_SHORT).show(); - } catch (Throwable t) { - Log.e("sanjay in catch", "catch exception"+e.getMessage()); - } - */ + RingtoneManager.setActualDefaultRingtoneUri(context, ringtoneType, newRingTone); - RingtoneManager.setActualDefaultRingtoneUri(context, ringtoneType, newRingTone); - Miscellaneous.logEvent("i", "Profile", "Ringtone set to: " + newRingTone.toString(), 1); - return true; + Miscellaneous.logEvent("i", "Profile", "Ringtone set to: " + newRingTone.toString(), 1); + return true; + } + catch (Throwable t) + { + String message = "Error setting ringtone: " + Log.getStackTraceString(t); + Miscellaneous.logEvent("e", "Profile", message, 1); + } } - catch (Throwable t) - { - String message = "Error setting ringtone: " + Log.getStackTraceString(t); - Miscellaneous.logEvent("e", "Profile", message, 1); - } - + return false; } diff --git a/app/src/main/java/com/jens/automation2/XmlFileInterface.java b/app/src/main/java/com/jens/automation2/XmlFileInterface.java index 7193b0bc..ca192c93 100644 --- a/app/src/main/java/com/jens/automation2/XmlFileInterface.java +++ b/app/src/main/java/com/jens/automation2/XmlFileInterface.java @@ -160,9 +160,9 @@ public class XmlFileInterface serializer.text(String.valueOf(Profile.getProfileCollection().get(i).getChangeIncomingCallsRingtone())); serializer.endTag(null, "changeIncomingCallsRingtone");// serializer.startTag(null, "incomingCallsRingtone"); - File incomingFile = Profile.getProfileCollection().get(i).getIncomingCallsRingtone(); + String incomingFile = Profile.getProfileCollection().get(i).getIncomingCallsRingtone(); if(incomingFile != null) - serializer.text(String.valueOf(Profile.getProfileCollection().get(i).getIncomingCallsRingtone().getPath())); + serializer.text(String.valueOf(Profile.getProfileCollection().get(i).getIncomingCallsRingtone())); else serializer.text("null"); serializer.endTag(null, "incomingCallsRingtone"); @@ -178,9 +178,9 @@ public class XmlFileInterface serializer.text(String.valueOf(Profile.getProfileCollection().get(i).getChangeNotificationRingtone())); serializer.endTag(null, "changeNotificationRingtone");// serializer.startTag(null, "notificationRingtone"); - File notificationFile = Profile.getProfileCollection().get(i).getNotificationRingtone(); + String notificationFile = Profile.getProfileCollection().get(i).getNotificationRingtone(); if(notificationFile != null) - serializer.text(String.valueOf(Profile.getProfileCollection().get(i).getNotificationRingtone().getPath())); + serializer.text(String.valueOf(Profile.getProfileCollection().get(i).getNotificationRingtone())); else serializer.text("null"); serializer.endTag(null, "notificationRingtone"); @@ -631,7 +631,7 @@ public class XmlFileInterface { String path = readTag(parser, "incomingCallsRingtone"); if(!path.equals("null")) - newProfile.setIncomingCallsRingtone(new File(path)); + newProfile.setIncomingCallsRingtone(path); else newProfile.setIncomingCallsRingtone(null); } @@ -643,7 +643,7 @@ public class XmlFileInterface { String path = readTag(parser, "notificationRingtone"); if(!path.equals("null")) - newProfile.setNotificationRingtone(new File(path)); + newProfile.setNotificationRingtone(path); else newProfile.setNotificationRingtone(null); } diff --git a/fastlane/metadata/android/en-US/changelogs/123.txt b/fastlane/metadata/android/en-US/changelogs/123.txt index 0d893a7b..b59c4d2f 100644 --- a/fastlane/metadata/android/en-US/changelogs/123.txt +++ b/fastlane/metadata/android/en-US/changelogs/123.txt @@ -2,4 +2,5 @@ * New action: Make phone call without further prompt * New trigger/action: Set custom variables as actions and check for them in triggers * Fixed: Battery receiver could trigger above 100% or below 0% -* Fixed: Date format when variables are used in texts \ No newline at end of file +* Fixed: Date format when variables are used in texts +* Fixed: Changing ringtone now works after Android 7.1, but you need to edit the profile for it to apply \ No newline at end of file