// TODO: also delete corresponding audio files (only those that are no longer used)
private void processResponseBody(List<AudioGson> audioGsons) {
        Log.i(getClass().getName(), "processResponseBody");
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                Log.i(getClass().getName(), "run");
                RoomDb roomDb = RoomDb.getDatabase(getContext());
                AudioDao audioDao = roomDb.audioDao();
                // Empty the database table before downloading up-to-date content
                audioDao.deleteAll();
                // TODO: also delete corresponding audio files (only those that are no longer used)
                for (AudioGson audioGson : audioGsons) {
                    Log.i(getClass().getName(), "audioGson.getId(): " + audioGson.getId());
                    Audio audio = GsonToRoomConverter.getAudio(audioGson);
                    // Check if the corresponding audio file has already been downloaded
                    File audioFile = FileHelper.getAudioFile(audioGson, getContext());
                    Log.i(getClass().getName(), "audioFile: " + audioFile);
                    Log.i(getClass().getName(), "audioFile.exists(): " + audioFile.exists());
                    if (!audioFile.exists()) {
                        // Download file bytes
                        BaseApplication baseApplication = (BaseApplication) getActivity().getApplication();
                        String downloadUrl = baseApplication.getBaseUrl() + audioGson.getBytesUrl();
                        Log.i(getClass().getName(), "downloadUrl: " + downloadUrl);
                        byte[] bytes = MultimediaDownloader.downloadFileBytes(downloadUrl);
                        Log.i(getClass().getName(), "bytes.length: " + bytes.length);
                        // Store the downloaded file in the external storage directory
                        try {
                            FileOutputStream fileOutputStream = new FileOutputStream(audioFile);
                            fileOutputStream.write(bytes);
                        } catch (FileNotFoundException e) {
                            Log.e(getClass().getName(), null, e);
                        } catch (IOException e) {
                            Log.e(getClass().getName(), null, e);
                        }
                        Log.i(getClass().getName(), "audioFile.exists(): " + audioFile.exists());
                    }
                    // Store the Audio in the database
                    audioDao.insert(audio);
                    Log.i(getClass().getName(), "Stored Audio in database with ID " + audio.getId());
                }
                // Update the UI
                List<Audio> audios = audioDao.loadAll();
                Log.i(getClass().getName(), "audios.size(): " + audios.size());
                getActivity().runOnUiThread(() -> {
                    textView.setText("audios.size(): " + audios.size());
                    Snackbar.make(textView, "audios.size(): " + audios.size(), Snackbar.LENGTH_LONG).show();
                    progressBar.setVisibility(View.GONE);
                });
            }
        });
    }
// Empty the database table before downloading up-to-date content
audioDao.deleteAll();
// TODO: also delete corresponding audio files (only those that are no longer used)
for (AudioGson audioGson : audioGsons) {
Log.i(getClass().getName(), "audioGson.getId(): " + audioGson.getId());
Log.i(getClass().getName(), "processResponseBody");
ExecutorService executorService = Executors.newSingleThreadExecutor();
executorService.execute(new Runnable() {
@Override
public void run() {
Log.i(getClass().getName(), "run");
RoomDb roomDb = RoomDb.getDatabase(getContext());
AudioDao audioDao = roomDb.audioDao();
// Empty the database table before downloading up-to-date content
audioDao.deleteAll();
// TODO: also delete corresponding audio files (only those that are no longer used)
for (AudioGson audioGson : audioGsons) {
Log.i(getClass().getName(), "audioGson.getId(): " + audioGson.getId());
Audio audio = GsonToRoomConverter.getAudio(audioGson);
// Check if the corresponding audio file has already been downloaded
File audioFile = FileHelper.getAudioFile(audioGson, getContext());
Log.i(getClass().getName(), "audioFile: " + audioFile);
Log.i(getClass().getName(), "audioFile.exists(): " + audioFile.exists());
if (!audioFile.exists()) {
// Download file bytes
BaseApplication baseApplication = (BaseApplication) getActivity().getApplication();
private void processResponseBody(List<AudioGson> audioGsons) {
Log.i(getClass().getName(), "processResponseBody");
ExecutorService executorService = Executors.newSingleThreadExecutor();
executorService.execute(new Runnable() {
@Override
public void run() {
Log.i(getClass().getName(), "run");
RoomDb roomDb = RoomDb.getDatabase(getContext());
AudioDao audioDao = roomDb.audioDao();
// Empty the database table before downloading up-to-date content
audioDao.deleteAll();
// TODO: also delete corresponding audio files (only those that are no longer used)
for (AudioGson audioGson : audioGsons) {
Log.i(getClass().getName(), "audioGson.getId(): " + audioGson.getId());
Audio audio = GsonToRoomConverter.getAudio(audioGson);
// Check if the corresponding audio file has already been downloaded
File audioFile = FileHelper.getAudioFile(audioGson, getContext());
Log.i(getClass().getName(), "audioFile: " + audioFile);
Log.i(getClass().getName(), "audioFile.exists(): " + audioFile.exists());
if (!audioFile.exists()) {
// Download file bytes
BaseApplication baseApplication = (BaseApplication) getActivity().getApplication();
String downloadUrl = baseApplication.getBaseUrl() + audioGson.getBytesUrl();
Log.i(getClass().getName(), "downloadUrl: " + downloadUrl);
byte[] bytes = MultimediaDownloader.downloadFileBytes(downloadUrl);
Log.i(getClass().getName(), "bytes.length: " + bytes.length);
// Store the downloaded file in the external storage directory
try {
FileOutputStream fileOutputStream = new FileOutputStream(audioFile);
fileOutputStream.write(bytes);
} catch (FileNotFoundException e) {
Log.e(getClass().getName(), null, e);