Setting ringtones from profiles works again

This commit is contained in:
jens 2023-01-13 18:48:00 +01:00
parent 2b69938ad5
commit 62a8723344
5 changed files with 94 additions and 80 deletions

View File

@ -12,7 +12,7 @@ android {
buildToolsVersion '29.0.2' buildToolsVersion '29.0.2'
useLibrary 'org.apache.http.legacy' useLibrary 'org.apache.http.legacy'
versionCode 123 versionCode 123
versionName "1.7.7" versionName "1.7.8"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
} }

View File

@ -47,37 +47,37 @@ public class ActivityManageProfile extends Activity
boolean guiUpdate = false; boolean guiUpdate = false;
File incomingCallsRingtone = null, notificationsRingtone = null; String incomingCallsRingtone = null, notificationsRingtone = null;
ArrayAdapter<String> soundModeAdapter; ArrayAdapter<String> soundModeAdapter;
ArrayAdapter<String> dndModeAdapter; ArrayAdapter<String> dndModeAdapter;
public void setIncomingCallsRingtone(File incomingCallsRingtone) public void setIncomingCallsRingtone(String incomingCallsRingtone)
{ {
this.incomingCallsRingtone = incomingCallsRingtone; this.incomingCallsRingtone = incomingCallsRingtone;
if(incomingCallsRingtone != null) if(incomingCallsRingtone != null)
tvIncomingCallsRingtone.setText(this.incomingCallsRingtone.getAbsolutePath()); tvIncomingCallsRingtone.setText(this.incomingCallsRingtone);
else else
tvIncomingCallsRingtone.setText(getResources().getString(R.string.none)); tvIncomingCallsRingtone.setText(getResources().getString(R.string.none));
} }
public File getIncomingCallsRingtone() public String getIncomingCallsRingtone()
{ {
return incomingCallsRingtone; return incomingCallsRingtone;
} }
public void setNotificationsRingtone(File notificationsRingtone) public void setNotificationsRingtone(String notificationsRingtone)
{ {
this.notificationsRingtone = notificationsRingtone; this.notificationsRingtone = notificationsRingtone;
if(this.notificationsRingtone != null) if(this.notificationsRingtone != null)
tvNotificationsRingtone.setText(this.notificationsRingtone.getAbsolutePath()); tvNotificationsRingtone.setText(this.notificationsRingtone);
else else
tvNotificationsRingtone.setText(getResources().getString(R.string.none)); tvNotificationsRingtone.setText(getResources().getString(R.string.none));
} }
public File getNotificationsRingtone() public String getNotificationsRingtone()
{ {
return notificationsRingtone; return notificationsRingtone;
} }
@ -284,26 +284,26 @@ public class ActivityManageProfile extends Activity
@Override @Override
public void onClick(View v) public void onClick(View v)
{ {
try // try
{ // {
Intent fileIntent = new Intent(Intent.ACTION_GET_CONTENT); // Intent fileIntent = new Intent(Intent.ACTION_GET_CONTENT);
fileIntent.setType("audio/*"); // fileIntent.setType("audio/*");
startActivityForResult(Intent.createChooser(fileIntent, "Select a ringtone"), intentCodeRingtonePickerCallsFile); // startActivityForResult(Intent.createChooser(fileIntent, "Select a ringtone"), intentCodeRingtonePickerCallsFile);
} // }
catch(ActivityNotFoundException e) // catch(ActivityNotFoundException e)
{ // {
// Use media browser instead // Use media browser instead
Intent fileSelectionIntent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER); Intent fileSelectionIntent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
if(ActivityMainProfiles.profileToEdit != null) if(ActivityMainProfiles.profileToEdit != null)
{ {
Uri currenturi = Uri.parse(ActivityMainProfiles.profileToEdit.incomingCallsRingtone.getAbsolutePath()); Uri currenturi = Uri.parse(ActivityMainProfiles.profileToEdit.incomingCallsRingtone);
if(ActivityMainProfiles.profileToEdit.changeIncomingCallsRingtone) if(ActivityMainProfiles.profileToEdit.changeIncomingCallsRingtone)
fileSelectionIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, currenturi); fileSelectionIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, currenturi);
} }
startActivityForResult(fileSelectionIntent, intentCodeRingtonePickerCallsRingtone); startActivityForResult(fileSelectionIntent, intentCodeRingtonePickerCallsRingtone);
} // }
} }
}); });
bChangeSoundNotifications.setOnClickListener(new OnClickListener() bChangeSoundNotifications.setOnClickListener(new OnClickListener()
@ -324,7 +324,7 @@ public class ActivityManageProfile extends Activity
if(ActivityMainProfiles.profileToEdit != null) if(ActivityMainProfiles.profileToEdit != null)
{ {
Uri currenturi = Uri.parse(ActivityMainProfiles.profileToEdit.notificationRingtone.getAbsolutePath()); Uri currenturi = Uri.parse(ActivityMainProfiles.profileToEdit.notificationRingtone);
if(ActivityMainProfiles.profileToEdit.changeNotificationRingtone) if(ActivityMainProfiles.profileToEdit.changeNotificationRingtone)
fileSelectionIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, currenturi); 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); Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
if (uri != null) if (uri != null)
{ {
String ringTonePath = CompensateCrappyAndroidPaths.getPath(ActivityManageProfile.this, uri); // if(Build.VERSION.SDK_INT < 26)
setIncomingCallsRingtone(new File(ringTonePath)); // {
// String ringTonePath = CompensateCrappyAndroidPaths.getPath(ActivityManageProfile.this, uri);
// setIncomingCallsRingtone(ringTonePath);
// }
// else
setIncomingCallsRingtone(uri.toString());
} }
break; break;
} }
case intentCodeRingtonePickerCallsFile: case intentCodeRingtonePickerCallsFile:
{ {
String ringTonePath = CompensateCrappyAndroidPaths.getPath(ActivityManageProfile.this, data.getData()); String ringTonePath = CompensateCrappyAndroidPaths.getPath(ActivityManageProfile.this, data.getData());
setIncomingCallsRingtone(new File(ringTonePath)); setIncomingCallsRingtone(ringTonePath);
break; break;
} }
case intentCodeRingtonePickerNotificationsRingtone: // notifications case intentCodeRingtonePickerNotificationsRingtone: // notifications
@ -511,15 +516,20 @@ public class ActivityManageProfile extends Activity
Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI); Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
if (uri != null) if (uri != null)
{ {
String ringTonePath = CompensateCrappyAndroidPaths.getPath(ActivityManageProfile.this, data.getData()); // if(Build.VERSION.SDK_INT < 26)
setNotificationsRingtone(new File(ringTonePath)); // {
// String ringTonePath = CompensateCrappyAndroidPaths.getPath(ActivityManageProfile.this, uri);
// setNotificationsRingtone(ringTonePath);
// }
// else
setNotificationsRingtone(uri.toString());
} }
break; break;
} }
case intentCodeRingtonePickerNotificationsFile: case intentCodeRingtonePickerNotificationsFile:
{ {
String ringTonePath = CompensateCrappyAndroidPaths.getPath(ActivityManageProfile.this, data.getData()); String ringTonePath = CompensateCrappyAndroidPaths.getPath(ActivityManageProfile.this, data.getData());
setNotificationsRingtone(new File(ringTonePath)); setNotificationsRingtone(ringTonePath);
break; break;
} }
default: default:

View File

@ -3,6 +3,7 @@ package com.jens.automation2;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.content.ContentValues; import android.content.ContentValues;
import android.content.Context; import android.content.Context;
import android.database.Cursor;
import android.media.AudioManager; import android.media.AudioManager;
import android.media.RingtoneManager; import android.media.RingtoneManager;
import android.net.Uri; import android.net.Uri;
@ -41,13 +42,13 @@ public class Profile implements Comparable<Profile>
protected int volumeAlarms; protected int volumeAlarms;
protected boolean changeIncomingCallsRingtone; protected boolean changeIncomingCallsRingtone;
protected File incomingCallsRingtone; protected String incomingCallsRingtone;
protected boolean changeVibrateWhenRinging; protected boolean changeVibrateWhenRinging;
protected boolean vibrateWhenRinging; protected boolean vibrateWhenRinging;
protected boolean changeNotificationRingtone; protected boolean changeNotificationRingtone;
protected File notificationRingtone; protected String notificationRingtone;
protected boolean changeAudibleSelection; protected boolean changeAudibleSelection;
protected boolean audibleSelection; protected boolean audibleSelection;
@ -171,11 +172,11 @@ public class Profile implements Comparable<Profile>
return changeIncomingCallsRingtone; return changeIncomingCallsRingtone;
} }
public void setIncomingCallsRingtone(File incomingCallsRingtone) public void setIncomingCallsRingtone(String incomingCallsRingtone)
{ {
this.incomingCallsRingtone = incomingCallsRingtone; this.incomingCallsRingtone = incomingCallsRingtone;
} }
public File getIncomingCallsRingtone() public String getIncomingCallsRingtone()
{ {
return incomingCallsRingtone; return incomingCallsRingtone;
} }
@ -207,11 +208,11 @@ public class Profile implements Comparable<Profile>
return changeNotificationRingtone; return changeNotificationRingtone;
} }
public void setNotificationRingtone(File notificationsRingtone) public void setNotificationRingtone(String notificationsRingtone)
{ {
this.notificationRingtone = notificationsRingtone; this.notificationRingtone = notificationsRingtone;
} }
public File getNotificationRingtone() public String getNotificationRingtone()
{ {
return notificationRingtone; return notificationRingtone;
} }
@ -292,9 +293,9 @@ public class Profile implements Comparable<Profile>
return null; 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()) // if(!ringtoneFile.exists() || !ringtoneFile.canRead())
// { // {
@ -304,9 +305,20 @@ public class Profile implements Comparable<Profile>
// return false; // return false;
// } // }
// Set by URI
if(ringtoneFile.contains("//"))
{
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(); ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, ringtoneFile.getAbsolutePath()); values.put(MediaStore.MediaColumns.DATA, ringtoneFile);
values.put(MediaStore.MediaColumns.TITLE, ringtoneFile.getName()); 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.MIME_TYPE, "audio/mp3");
values.put(MediaStore.MediaColumns.SIZE, ringtoneFile.length()); values.put(MediaStore.MediaColumns.SIZE, ringtoneFile.length());
values.put(MediaStore.Audio.Media.IS_RINGTONE, ringtoneType == RingtoneManager.TYPE_RINGTONE); values.put(MediaStore.Audio.Media.IS_RINGTONE, ringtoneType == RingtoneManager.TYPE_RINGTONE);
@ -318,32 +330,22 @@ public class Profile implements Comparable<Profile>
{ {
Uri newRingTone = null; Uri newRingTone = null;
//TODO: This part needs to be made compatible with Android 11 and above. Uri existingRingTone = MediaStore.Audio.Media.getContentUriForPath(ringtoneFile);
if(Build.VERSION.SDK_INT < 30)
{
Uri existingRingTone = MediaStore.Audio.Media.getContentUriForPath(ringtoneFile.getAbsolutePath());
try
{
if (existingRingTone != null) if (existingRingTone != null)
context.getContentResolver().delete(existingRingTone, MediaStore.MediaColumns.DATA + "=\"" + ringtoneFile.getAbsolutePath() + "\"", 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); 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); Miscellaneous.logEvent("i", "Profile", "Ringtone set to: " + newRingTone.toString(), 1);
return true; return true;
} }
@ -352,6 +354,7 @@ public class Profile implements Comparable<Profile>
String message = "Error setting ringtone: " + Log.getStackTraceString(t); String message = "Error setting ringtone: " + Log.getStackTraceString(t);
Miscellaneous.logEvent("e", "Profile", message, 1); Miscellaneous.logEvent("e", "Profile", message, 1);
} }
}
return false; return false;
} }

View File

@ -160,9 +160,9 @@ public class XmlFileInterface
serializer.text(String.valueOf(Profile.getProfileCollection().get(i).getChangeIncomingCallsRingtone())); serializer.text(String.valueOf(Profile.getProfileCollection().get(i).getChangeIncomingCallsRingtone()));
serializer.endTag(null, "changeIncomingCallsRingtone");// serializer.endTag(null, "changeIncomingCallsRingtone");//
serializer.startTag(null, "incomingCallsRingtone"); serializer.startTag(null, "incomingCallsRingtone");
File incomingFile = Profile.getProfileCollection().get(i).getIncomingCallsRingtone(); String incomingFile = Profile.getProfileCollection().get(i).getIncomingCallsRingtone();
if(incomingFile != null) if(incomingFile != null)
serializer.text(String.valueOf(Profile.getProfileCollection().get(i).getIncomingCallsRingtone().getPath())); serializer.text(String.valueOf(Profile.getProfileCollection().get(i).getIncomingCallsRingtone()));
else else
serializer.text("null"); serializer.text("null");
serializer.endTag(null, "incomingCallsRingtone"); serializer.endTag(null, "incomingCallsRingtone");
@ -178,9 +178,9 @@ public class XmlFileInterface
serializer.text(String.valueOf(Profile.getProfileCollection().get(i).getChangeNotificationRingtone())); serializer.text(String.valueOf(Profile.getProfileCollection().get(i).getChangeNotificationRingtone()));
serializer.endTag(null, "changeNotificationRingtone");// serializer.endTag(null, "changeNotificationRingtone");//
serializer.startTag(null, "notificationRingtone"); serializer.startTag(null, "notificationRingtone");
File notificationFile = Profile.getProfileCollection().get(i).getNotificationRingtone(); String notificationFile = Profile.getProfileCollection().get(i).getNotificationRingtone();
if(notificationFile != null) if(notificationFile != null)
serializer.text(String.valueOf(Profile.getProfileCollection().get(i).getNotificationRingtone().getPath())); serializer.text(String.valueOf(Profile.getProfileCollection().get(i).getNotificationRingtone()));
else else
serializer.text("null"); serializer.text("null");
serializer.endTag(null, "notificationRingtone"); serializer.endTag(null, "notificationRingtone");
@ -631,7 +631,7 @@ public class XmlFileInterface
{ {
String path = readTag(parser, "incomingCallsRingtone"); String path = readTag(parser, "incomingCallsRingtone");
if(!path.equals("null")) if(!path.equals("null"))
newProfile.setIncomingCallsRingtone(new File(path)); newProfile.setIncomingCallsRingtone(path);
else else
newProfile.setIncomingCallsRingtone(null); newProfile.setIncomingCallsRingtone(null);
} }
@ -643,7 +643,7 @@ public class XmlFileInterface
{ {
String path = readTag(parser, "notificationRingtone"); String path = readTag(parser, "notificationRingtone");
if(!path.equals("null")) if(!path.equals("null"))
newProfile.setNotificationRingtone(new File(path)); newProfile.setNotificationRingtone(path);
else else
newProfile.setNotificationRingtone(null); newProfile.setNotificationRingtone(null);
} }

View File

@ -3,3 +3,4 @@
* New trigger/action: Set custom variables as actions and check for them in triggers * 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: Battery receiver could trigger above 100% or below 0%
* Fixed: Date format when variables are used in texts * 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