From a5d2c9cdf72203eeb26c7535be37b254a20b386f Mon Sep 17 00:00:00 2001 From: "Jo G." <1451036+jo-elimu@users.noreply.github.com> Date: Wed, 5 Mar 2025 20:13:34 +0700 Subject: [PATCH 1/6] refactor: remove audio - content provider #161 --- app/src/main/AndroidManifest.xml | 5 - .../provider/AudioContentProvider.java | 170 ------------------ 2 files changed, 175 deletions(-) delete mode 100644 app/src/main/java/ai/elimu/content_provider/provider/AudioContentProvider.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 21d1c270..7f8b1068 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -59,11 +59,6 @@ android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/image_file_path"/> - pathSegments = uri.getPathSegments(); - Log.i(getClass().getName(), "pathSegments: " + pathSegments); - String audioIdAsString = pathSegments.get(1); - Long audioId = Long.valueOf(audioIdAsString); - Log.i(getClass().getName(), "audioId: " + audioId); - - final Cursor cursor; - - // Get the Room Cursor - cursor = audioDao.loadAsCursor(audioId); - Log.i(getClass().getName(), "cursor: " + cursor); - - cursor.setNotificationUri(context.getContentResolver(), uri); - - return cursor; - } else if (code == CODE_AUDIO_TRANSCRIPTION) { - // Extract the transcription from the URI - List pathSegments = uri.getPathSegments(); - Log.i(getClass().getName(), "pathSegments: " + pathSegments); - String transcription = pathSegments.get(2); - Log.i(getClass().getName(), "transcription: \"" + transcription + "\""); - - final Cursor cursor; - - // Get the Room Cursor - cursor = audioDao.loadByTranscriptionAsCursor(transcription); - Log.i(getClass().getName(), "cursor: " + cursor); - - cursor.setNotificationUri(context.getContentResolver(), uri); - - return cursor; - } else if (code == CODE_AUDIO_TITLE) { - // Extract the transcription from the URI - List pathSegments = uri.getPathSegments(); - Log.i(getClass().getName(), "pathSegments: " + pathSegments); - String title = pathSegments.get(2); - Log.i(getClass().getName(), "title: \"" + title + "\""); - - final Cursor cursor; - - // Get the Room Cursor - cursor = audioDao.loadByTitleAsCursor(title); - Log.i(getClass().getName(), "cursor: " + cursor); - - cursor.setNotificationUri(context.getContentResolver(), uri); - - return cursor; - } else { - throw new IllegalArgumentException("Unknown URI: " + uri); - } - } - - /** - * Handles requests for the MIME type of the data at the given URI. - */ - @Override - public String getType(Uri uri) { - Log.i(getClass().getName(), "getType"); - - throw new UnsupportedOperationException("Not yet implemented"); - } - - /** - * Handles requests to insert a new row. - */ - @Override - public Uri insert(Uri uri, ContentValues values) { - Log.i(getClass().getName(), "insert"); - - throw new UnsupportedOperationException("Not yet implemented"); - } - - /** - * Handles requests to update one or more rows. - */ - @Override - public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { - Log.i(getClass().getName(), "update"); - - throw new UnsupportedOperationException("Not yet implemented"); - } - - /** - * Handle requests to delete one or more rows. - */ - @Override - public int delete(Uri uri, String selection, String[] selectionArgs) { - Log.i(getClass().getName(), "delete"); - - throw new UnsupportedOperationException("Not yet implemented"); - } -} From e26746b7c717144e1756fb41ced1ebea53ca7bb9 Mon Sep 17 00:00:00 2001 From: "Jo G." <1451036+jo-elimu@users.noreply.github.com> Date: Wed, 5 Mar 2025 20:20:20 +0700 Subject: [PATCH 2/6] refactor: remove audio - ui fragment #161 --- .../elimu/content_provider/MainActivity.java | 1 - .../ui/audio/AudiosFragment.java | 177 ------------------ .../ui/audio/AudiosViewModel.java | 19 -- app/src/main/res/layout/fragment_audios.xml | 29 --- .../main/res/menu/activity_main_drawer.xml | 4 - .../main/res/navigation/mobile_navigation.xml | 6 - app/src/main/res/values/strings.xml | 1 - 7 files changed, 237 deletions(-) delete mode 100644 app/src/main/java/ai/elimu/content_provider/ui/audio/AudiosFragment.java delete mode 100644 app/src/main/java/ai/elimu/content_provider/ui/audio/AudiosViewModel.java delete mode 100644 app/src/main/res/layout/fragment_audios.xml diff --git a/app/src/main/java/ai/elimu/content_provider/MainActivity.java b/app/src/main/java/ai/elimu/content_provider/MainActivity.java index f5c6cd08..eedbff7c 100644 --- a/app/src/main/java/ai/elimu/content_provider/MainActivity.java +++ b/app/src/main/java/ai/elimu/content_provider/MainActivity.java @@ -58,7 +58,6 @@ protected void onCreate(Bundle savedInstanceState) { R.id.nav_numbers, R.id.nav_emojis, R.id.nav_images, - R.id.nav_audios, R.id.nav_storybooks, R.id.nav_videos ) diff --git a/app/src/main/java/ai/elimu/content_provider/ui/audio/AudiosFragment.java b/app/src/main/java/ai/elimu/content_provider/ui/audio/AudiosFragment.java deleted file mode 100644 index 2b3fc29e..00000000 --- a/app/src/main/java/ai/elimu/content_provider/ui/audio/AudiosFragment.java +++ /dev/null @@ -1,177 +0,0 @@ -package ai.elimu.content_provider.ui.audio; - -import android.os.Bundle; -import android.util.Log; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.ProgressBar; -import android.widget.TextView; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.fragment.app.Fragment; -import androidx.lifecycle.Observer; -import androidx.lifecycle.ViewModelProvider; - -import com.google.android.material.snackbar.Snackbar; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.util.List; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; - -import ai.elimu.content_provider.BaseApplication; -import ai.elimu.content_provider.R; -import ai.elimu.content_provider.rest.AudiosService; -import ai.elimu.content_provider.room.GsonToRoomConverter; -import ai.elimu.content_provider.room.dao.AudioDao; -import ai.elimu.content_provider.room.db.RoomDb; -import ai.elimu.content_provider.room.entity.Audio; -import ai.elimu.content_provider.util.FileHelper; -import ai.elimu.content_provider.util.MultimediaDownloader; -import ai.elimu.model.v2.gson.content.AudioGson; -import retrofit2.Call; -import retrofit2.Callback; -import retrofit2.Response; -import retrofit2.Retrofit; - -public class AudiosFragment extends Fragment { - - private AudiosViewModel audiosViewModel; - - private ProgressBar progressBar; - - private TextView textView; - - public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - Log.i(getClass().getName(), "onCreateView"); - - audiosViewModel = new ViewModelProvider(this).get(AudiosViewModel.class); - View root = inflater.inflate(R.layout.fragment_audios, container, false); - progressBar = root.findViewById(R.id.progress_bar_audios); - textView = root.findViewById(R.id.text_audios); - audiosViewModel.getText().observe(getViewLifecycleOwner(), new Observer() { - @Override - public void onChanged(@Nullable String s) { - Log.i(getClass().getName(), "onChanged"); - textView.setText(s); - } - }); - return root; - } - - @Override - public void onStart() { - Log.i(getClass().getName(), "onStart"); - super.onStart(); - - // Download Audios from REST API, and store them in the database - BaseApplication baseApplication = (BaseApplication) getActivity().getApplication(); - Retrofit retrofit = baseApplication.getRetrofit(); - AudiosService audiosService = retrofit.create(AudiosService.class); - Call> audioGsonsCall = audiosService.listAudios(); - Log.i(getClass().getName(), "audioGsonsCall.request(): " + audioGsonsCall.request()); - audioGsonsCall.enqueue(new Callback>() { - - @Override - public void onResponse(Call> call, Response> response) { - Log.i(getClass().getName(), "onResponse"); - - Log.i(getClass().getName(), "response: " + response); - if (response.isSuccessful()) { - List audioGsons = response.body(); - Log.i(getClass().getName(), "audioGsons.size(): " + audioGsons.size()); - - if (audioGsons.size() > 0) { - processResponseBody(audioGsons); - } - } else { - // Handle error - Snackbar.make(textView, response.toString(), Snackbar.LENGTH_LONG) - .setBackgroundTint(getResources().getColor(R.color.deep_orange_darken_4)) - .show(); - progressBar.setVisibility(View.GONE); - } - } - - @Override - public void onFailure(Call> call, Throwable t) { - Log.e(getClass().getName(), "onFailure", t); - - Log.e(getClass().getName(), "t.getCause():", t.getCause()); - - // Handle error - Snackbar.make(textView, t.getCause().toString(), Snackbar.LENGTH_LONG) - .setBackgroundTint(getResources().getColor(R.color.deep_orange_darken_4)) - .show(); - progressBar.setVisibility(View.GONE); - } - }); - } - - private void processResponseBody(List 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