Skip to content

Commit

Permalink
Improve performance of file access SDK > 25
Browse files Browse the repository at this point in the history
  • Loading branch information
AndInTheClouds committed Jan 18, 2023
1 parent 8d5aa5d commit c82ab2c
Show file tree
Hide file tree
Showing 17 changed files with 848 additions and 565 deletions.
1 change: 0 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
<application
android:icon="@mipmap/chord_reader_icon"
android:label="@string/app_name"
android:requestLegacyExternalStorage="true"
android:allowBackup="true">

<activity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,9 @@ public void onClick(DialogInterface dialogInterface, int i) {
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI,
PreferenceHelper.getStorageLocation(getApplicationContext()));

intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
directoryPickerResultLauncher.launch(intent);
} else {
ActivityCompat.requestPermissions(MainActivity.this,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,165 +29,144 @@
import java.util.Map;

public class ChordDictionary {
private static final String LOG_TAG = "ChordDictionary";

// maps chords to finger positions on guitar frets, e.g. 133211
private static Map<Chord, List<String>> chordsToGuitarChords = null;
private static String customChordVars;

public static void initialize(Context context) {
Map<Chord, List<String>> dictionary = new HashMap<>();

// load custom chord variations
try {
customChordVars = SaveFileHelper.openFile(context,"customChordVariations_DO_NOT_EDIT.crd");

if (!customChordVars.isEmpty()) {
loadIntoChordDictionary(context, -1, NoteNaming.English, dictionary);
} else
throw new IOException();

} catch (IOException e) {
//no customChordVariations_DO_NOT_EDIT.crd - ignore
Log.e(LOG_TAG, e + " - No customChordVariations file, load default");

try {
loadIntoChordDictionary(context, R.raw.chords1, NoteNaming.English, dictionary);
loadIntoChordDictionary(context, R.raw.chords2, NoteNaming.NorthernEuropean, dictionary);

} catch (IOException f) {
Log.e(LOG_TAG, f + " - unexpected exception, couldn't initialize ChordDictionary");
} catch (Exception f) {
Log.e(LOG_TAG, f + " - unknown exception, couldn't initialize ChordDictionary");
}
}
if (!dictionary.isEmpty())
Log.i(LOG_TAG, "Chord Dictionary initialized");
chordsToGuitarChords = dictionary;
}

private static void loadIntoChordDictionary(Context context, int resId, NoteNaming noteNaming, Map<Chord, List<String>> dictionary) throws IOException {
InputStream inputStream;

if (resId == -1) {
inputStream = new ByteArrayInputStream(customChordVars.getBytes());
} else {
inputStream = context.getResources().openRawResource(resId);
}

BufferedReader bufferedReader = null;

try {
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
while (bufferedReader.ready()) {
String line = bufferedReader.readLine();
line = line.trim();
String[] tokens = StringUtil.split(line, ":");

String chordText = tokens[0].trim();
String guitarChord = tokens[1].trim();

Chord chord = ChordParser.parseChord(chordText, noteNaming);

if (chord == null) {
Log.w(LOG_TAG, "Unable to parse chord text - skipping:" + chordText);
continue;
}

// map chords to their string guitar chord representations
// note that there may be multiples - e.g. there are several ways
// to play a G chord
List<String> existingValue = dictionary.get(chord);
if (existingValue == null) {
dictionary.put(chord, new ArrayList<>(Collections.singleton(guitarChord)));
} else if (!existingValue.contains(guitarChord)) {
existingValue.add(guitarChord);
}

}
} finally {
if (bufferedReader != null) {
bufferedReader.close();
}
}
}

public static boolean isInitialized() {
return chordsToGuitarChords != null;
}

public static List<String> getGuitarChordsForChord(Chord chord) {
List<String> result = chordsToGuitarChords.get(chord);
return result != null ? result : Collections.emptyList();
}

public static void setGuitarChordsForChord(Context context, Chord chord, List<String> newGuitarChords) {
List<String> existingValue = chordsToGuitarChords.get(chord);
if (existingValue != null) {
chordsToGuitarChords.remove(chord);
}
chordsToGuitarChords.put(chord, newGuitarChords);

saveChordDictionaryToFile(context);
}

private static void saveChordDictionaryToFile(Context context) {
StringBuilder stringBuilder = new StringBuilder();
for (Object key : chordsToGuitarChords.keySet()) {
String chord = ((Chord) key).toPrintableString(NoteNaming.English);
List<String> chordVarsList = chordsToGuitarChords.get(key);

for (String chordVar : chordVarsList) {
stringBuilder
.append(chord)
.append(": ")
.append(chordVar)
.append('\n');
}
}

final String result = stringBuilder.substring(0, stringBuilder.length() - 1); // cut off final newline

// do in background to avoid jankiness
HandlerThread handlerThread = new HandlerThread("SaveChordsHandlerThread");
handlerThread.start();

Handler asyncHandler = new Handler(handlerThread.getLooper()) {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
Boolean successfullySavedLog = (Boolean) msg.obj;

String toastMessage;

if(successfullySavedLog) {
toastMessage = context.getString(R.string.file_saved);
} else {
toastMessage = context.getString(R.string.unable_to_save_file);
}

// must be called on the main thread
Handler mainThread = new Handler(Looper.getMainLooper());
mainThread.post(new Runnable() {
@Override
public void run() {
Toast.makeText(context, toastMessage, Toast.LENGTH_SHORT).show();
}
});


handlerThread.quit();
}
};

Runnable runnable = () -> {
// your async code goes here.
Message message = new Message();
message.obj = SaveFileHelper.saveFile(context, result, "customChordVariations_DO_NOT_EDIT.crd");

asyncHandler.sendMessage(message);
};

asyncHandler.post(runnable);
}
private static final String LOG_TAG = "ChordDictionary";

// maps chords to finger positions on guitar frets, e.g. 133211
private static Map<Chord, List<String>> chordsToGuitarChords = null;
private static String customChordVars;

public static void initialize(Context context) {
Map<Chord, List<String>> dictionary = new HashMap<>();

// load custom chord variations
try {
customChordVars = SaveFileHelper.openFile(context, "customChordVariations_DO_NOT_EDIT.crd");

if (!customChordVars.isEmpty()) {
loadIntoChordDictionary(context, -1, NoteNaming.English, dictionary);
} else
throw new IOException();

} catch (IOException e) {
//no customChordVariations_DO_NOT_EDIT.crd - ignore
Log.e(LOG_TAG, e + " - No customChordVariations file, load default");

try {
loadIntoChordDictionary(context, R.raw.chords1, NoteNaming.English, dictionary);
loadIntoChordDictionary(context, R.raw.chords2, NoteNaming.NorthernEuropean, dictionary);

} catch (IOException f) {
Log.e(LOG_TAG, f + " - unexpected exception, couldn't initialize ChordDictionary");
} catch (Exception f) {
Log.e(LOG_TAG, f + " - unknown exception, couldn't initialize ChordDictionary");
}
}
if (!dictionary.isEmpty())
Log.i(LOG_TAG, "Chord Dictionary initialized");
chordsToGuitarChords = dictionary;
}

private static void loadIntoChordDictionary(Context context, int resId, NoteNaming noteNaming, Map<Chord, List<String>> dictionary) throws IOException {
InputStream inputStream;

if (resId == -1) {
inputStream = new ByteArrayInputStream(customChordVars.getBytes());
} else {
inputStream = context.getResources().openRawResource(resId);
}

BufferedReader bufferedReader = null;

try {
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
while (bufferedReader.ready()) {
String line = bufferedReader.readLine();
line = line.trim();
String[] tokens = StringUtil.split(line, ":");

String chordText = tokens[0].trim();
String guitarChord = tokens[1].trim();

Chord chord = ChordParser.parseChord(chordText, noteNaming);

if (chord == null) {
Log.w(LOG_TAG, "Unable to parse chord text - skipping:" + chordText);
continue;
}

// map chords to their string guitar chord representations
// note that there may be multiples - e.g. there are several ways
// to play a G chord
List<String> existingValue = dictionary.get(chord);
if (existingValue == null) {
dictionary.put(chord, new ArrayList<>(Collections.singleton(guitarChord)));
} else if (!existingValue.contains(guitarChord)) {
existingValue.add(guitarChord);
}

}
} finally {
if (bufferedReader != null) {
bufferedReader.close();
}
}
}

public static boolean isInitialized() {
return chordsToGuitarChords != null;
}

public static List<String> getGuitarChordsForChord(Chord chord) {
List<String> result = chordsToGuitarChords.get(chord);
return result != null ? result : Collections.emptyList();
}

public static void setGuitarChordsForChord(Context context, Chord chord, List<String> newGuitarChords) {
List<String> existingValue = chordsToGuitarChords.get(chord);
if (existingValue != null) {
chordsToGuitarChords.remove(chord);
}
chordsToGuitarChords.put(chord, newGuitarChords);

saveChordDictionaryToFile(context);
}

private static void saveChordDictionaryToFile(Context context) {
StringBuilder stringBuilder = new StringBuilder();
for (Object key : chordsToGuitarChords.keySet()) {
String chord = ((Chord) key).toPrintableString(NoteNaming.English);
List<String> chordVarsList = chordsToGuitarChords.get(key);

for (String chordVar : chordVarsList) {
stringBuilder
.append(chord)
.append(": ")
.append(chordVar)
.append('\n');
}
}

final String result = stringBuilder.substring(0, stringBuilder.length() - 1); // cut off final newline

Boolean successfullySavedLog = SaveFileHelper.saveFile(
context, result, "customChordVariations_DO_NOT_EDIT.crd");

String toastMessage;

if (successfullySavedLog) {
toastMessage = context.getString(R.string.file_saved);
} else {
toastMessage = context.getString(R.string.unable_to_save_file);
}

// must be called on the main thread
Handler mainThread = new Handler(Looper.getMainLooper());
mainThread.post(new Runnable() {
@Override
public void run() {
Toast.makeText(context, toastMessage, Toast.LENGTH_SHORT).show();
}
});

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public static void clearCache() {
colorScheme = null;
noteNaming = null;
searchEngineURL = null;
storageLocation = null;
}

private static void cacheTextsize(Context context, int dimenId) {
Expand Down
Loading

0 comments on commit c82ab2c

Please sign in to comment.