Fix storage permission issues on newer Android devices

Resolves #85.
This commit is contained in:
Trevor Slocum 2023-03-27 21:30:11 -07:00
parent 0cb0b18a8e
commit ed5afd4b83
3 changed files with 91 additions and 23 deletions

View File

@ -1,3 +1,7 @@
1.6.6:
- Fix and re-enable daily reminder feature
- Fix storage permission issues on newer Android devices
1.6.5:
- Add Black theme

View File

@ -59,10 +59,13 @@ import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.CookieHandler;
@ -609,7 +612,7 @@ public class MeditationAssistant extends Application {
String soundPath = prefs.getString("pref_meditation_sound_" + label, "");
if (!soundPath.equals("none")) {
if (soundPath.equals("custom")) {
cacheSound(0, prefs.getString("pref_meditation_sound_" + label + "_custom", ""));
cacheSound(0, getFilesDir().getAbsolutePath() + "/" + prefs.getString("pref_meditation_sound_" + label + "_custom", ""));
} else {
cacheSound(MeditationSounds.getMeditationSound(soundPath), "");
}
@ -715,7 +718,7 @@ public class MeditationAssistant extends Application {
String soundPath = prefs.getString("pref_meditation_sound_" + label, "");
if (!soundPath.equals("none")) {
if (soundPath.equals("custom")) {
playSound(0, prefs.getString("pref_meditation_sound_" + label + "_custom", ""), restoreVolume);
playSound(0, getFilesDir().getAbsolutePath() + "/" + prefs.getString("pref_meditation_sound_" + label + "_custom", ""), restoreVolume);
} else {
playSound(MeditationSounds.getMeditationSound(soundPath), "", restoreVolume);
}
@ -2610,10 +2613,29 @@ public class MeditationAssistant extends Application {
wakeLockerLock.unlock();
}
public enum TrackerName {
APP_TRACKER, // Tracker used only in this app.
GLOBAL_TRACKER, // Tracker used by all the apps from a company. eg: roll-up tracking.
ECOMMERCE_TRACKER, // Tracker used by all ecommerce transactions from a company.
public static boolean writeInputStreamToFile(InputStream in, File file) {
OutputStream out = null;
try {
out = new FileOutputStream(file);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
try {
if (out != null) {
out.close();
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
@Override

View File

@ -11,6 +11,7 @@ import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.database.Cursor;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
@ -25,6 +26,7 @@ import android.preference.PreferenceActivity;
import android.preference.PreferenceCategory;
import android.preference.PreferenceFragment;
import android.preference.RingtonePreference;
import android.provider.OpenableColumns;
import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
@ -41,6 +43,10 @@ import androidx.core.content.ContextCompat;
import net.margaritov.preference.colorpicker.ColorPickerPreference;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URLDecoder;
import java.util.Arrays;
import java.util.HashSet;
@ -550,23 +556,61 @@ public class SettingsActivity extends PreferenceActivity {
String pref;
String pref_key;
String file_prefix;
if (requestCode == FILEPICKER_SELECT_SOUND_START) {
pref_key = "pref_meditation_sound_start";
pref = "pref_meditation_sound_start_custom";
file_prefix = "ma_start_";
} else if (requestCode == FILEPICKER_SELECT_SOUND_INTERVAL) {
pref_key = "pref_meditation_sound_interval";
pref = "pref_meditation_sound_interval_custom";
file_prefix = "ma_interval_";
} else if (requestCode == FILEPICKER_SELECT_SOUND_FINISH) {
pref_key = "pref_meditation_sound_finish";
pref = "pref_meditation_sound_finish_custom";
} else if (requestCode == FILEPICKER_SELECT_SOUND_BELL) {
pref_key = "pref_meditation_sound_bell";
pref = "pref_meditation_sound_bell_custom";
file_prefix = "ma_finish_";
} else if (requestCode == FILEPICKER_SELECT_SOUND_BELL) {
pref_key = "pref_meditation_sound_bell";
pref = "pref_meditation_sound_bell_custom";
file_prefix = "ma_bell_";
} else {
return;
}
getMeditationAssistant().getPrefs().edit().putString(pref, getMeditationAssistant().filePickerResult(intent).toString()).apply();
Cursor cursor = getContentResolver().query(getMeditationAssistant().filePickerResult(intent), null, null, null, null);
int nameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
cursor.moveToFirst();
String filename = cursor.getString(nameIndex);
if (filename == null || filename.equals("")) {
return;
}
filename = file_prefix + filename;
InputStream inputStream;
try {
inputStream = getContentResolver().openInputStream(getMeditationAssistant().filePickerResult(intent));
} catch (FileNotFoundException e) {
e.printStackTrace();
return;
}
File file = new File(getFilesDir(), filename);
boolean success = MeditationAssistant.writeInputStreamToFile(inputStream, file);
if (!success) {
return;
}
String oldFilename = getMeditationAssistant().getPrefs().getString(pref, "");
if (!oldFilename.equals(filename)) {
try {
File oldFile = new File(getFilesDir(), oldFilename);
oldFile.delete();
} catch (Exception e) {
// Do nothing.
}
}
getMeditationAssistant().getPrefs().edit().putString(pref, filename).apply();
if (requestCode == FILEPICKER_SELECT_SOUND_START) {
initialSoundChangeStart = true;
@ -575,8 +619,8 @@ public class SettingsActivity extends PreferenceActivity {
} else if (requestCode == FILEPICKER_SELECT_SOUND_FINISH) {
initialSoundChangeFinish = true;
} else if (requestCode == FILEPICKER_SELECT_SOUND_BELL) {
initialSoundChangeBell = true;
}
initialSoundChangeBell = true;
}
ListPreferenceSound prefMeditationSound = (ListPreferenceSound) (sessionPreferenceFragment == null ? findPreference(pref_key) : sessionPreferenceFragment.findPreference(pref_key));
prefMeditationSound.getOnPreferenceChangeListener().onPreferenceChange(prefMeditationSound, getMeditationAssistant().getPrefs().getString(pref_key, "gong"));
@ -879,18 +923,16 @@ public class SettingsActivity extends PreferenceActivity {
return getString(R.string.noSound);
}
String decodedSound;
try {
decodedSound = URLDecoder.decode(meditation_sound);
} catch (Exception e) {
return meditation_sound;
if (meditation_sound.startsWith("ma_start_")) {
return meditation_sound.substring(9);
} else if (meditation_sound.startsWith("ma_interval_")) {
return meditation_sound.substring(12);
} else if (meditation_sound.startsWith("ma_finish_")) {
return meditation_sound.substring(10);
} else if (meditation_sound.startsWith("ma_bell_")) {
return meditation_sound.substring(8);
}
int index = decodedSound.lastIndexOf("/");
if (index == -1) {
return decodedSound;
}
return decodedSound.substring(index+1);
return meditation_sound;
}
private void bindPreferenceSummaryToValue(Preference preference) {