Skip to content

Commit

Permalink
feat: Provide sounds of letter-sound correspondences
Browse files Browse the repository at this point in the history
  • Loading branch information
jo-elimu committed Dec 1, 2023
1 parent 526e438 commit ea1f4c7
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package ai.elimu.content_provider.provider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

import java.util.List;

import ai.elimu.content_provider.BuildConfig;
import ai.elimu.content_provider.room.dao.SoundDao;
import ai.elimu.content_provider.room.db.RoomDb;

public class SoundContentProvider extends ContentProvider {

private static final String AUTHORITY = BuildConfig.APPLICATION_ID + ".provider.sound_provider";
private static final String TABLE_SOUNDS = "sounds";
private static final int CODE_SOUNDS = 1;
private static final int CODE_SOUND_ID = 2;

private static final int CODE_SOUNDS_BY_LETTER_SOUND_ID = 3;
private static final UriMatcher MATCHER = new UriMatcher(UriMatcher.NO_MATCH);

static {
MATCHER.addURI(AUTHORITY, TABLE_SOUNDS, CODE_SOUNDS);
MATCHER.addURI(AUTHORITY, TABLE_SOUNDS + "/#", CODE_SOUND_ID);
MATCHER.addURI(AUTHORITY, TABLE_SOUNDS + "/by-letter-sound-id/#", CODE_SOUNDS_BY_LETTER_SOUND_ID);
}

@Override
public boolean onCreate() {
Log.i(getClass().getName(), "onCreate");
return true;
}

@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
Log.i(getClass().getName(), "query");

Log.i(getClass().getName(), "uri: " + uri);
Log.i(getClass().getName(), "projection: " + projection);
Log.i(getClass().getName(), "selection: " + selection);
Log.i(getClass().getName(), "selectionArgs: " + selectionArgs);
Log.i(getClass().getName(), "sortOrder: " + sortOrder);

Context context = getContext();
Log.i(getClass().getName(), "context: " + context);
if (context == null) {
return null;
}

RoomDb roomDb = RoomDb.getDatabase(context);
SoundDao soundDao = roomDb.soundDao();

final int code = MATCHER.match(uri);
Log.i(getClass().getName(), "code: " + code);
if (code == CODE_SOUNDS) {
final Cursor cursor;

// Get the Room Cursor
cursor = soundDao.loadAllOrderedByUsageCount_Cursor();
Log.i(getClass().getName(), "cursor: " + cursor);

cursor.setNotificationUri(context.getContentResolver(), uri);

return cursor;
} else if (code == CODE_SOUNDS_BY_LETTER_SOUND_ID) {
// Extract the letter-sound correspondence ID from the URI
List<String> pathSegments = uri.getPathSegments();
Log.i(getClass().getName(), "pathSegments: " + pathSegments);
String letterSoundIdAsString = pathSegments.get(2);
Long letterSoundId = Long.valueOf(letterSoundIdAsString);
Log.i(getClass().getName(), "letterSoundId: " + letterSoundId);

final Cursor cursor;

// Get the Room Cursor
cursor = soundDao.loadAllByLetterSound(letterSoundId);
Log.i(getClass().getName(), "cursor: " + cursor);

cursor.setNotificationUri(context.getContentResolver(), uri);

return cursor;
} else if (code == CODE_SOUND_ID) {
// Extract the Sound ID from the URI
List<String> pathSegments = uri.getPathSegments();
Log.i(getClass().getName(), "pathSegments: " + pathSegments);
String soundIdAsString = pathSegments.get(1);
Long soundId = Long.valueOf(soundIdAsString);
Log.i(getClass().getName(), "soundId: " + soundId);

final Cursor cursor;

// Get the Room Cursor
cursor = soundDao.load_Cursor(soundId);
Log.i(getClass().getName(), "cursor: " + cursor);

cursor.setNotificationUri(context.getContentResolver(), uri);

return cursor;
} else {
throw new IllegalArgumentException("Unknown URI: " + uri);
}
}

@Override
public String getType(Uri uri) {
Log.i(getClass().getName(), "getType");

throw new UnsupportedOperationException("Not yet implemented");
}

@Override
public Uri insert(Uri uri, ContentValues values) {
Log.i(getClass().getName(), "insert");

throw new UnsupportedOperationException("Not yet implemented");
}

@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
Log.i(getClass().getName(), "update");

throw new UnsupportedOperationException("Not yet implemented");
}

@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
Log.i(getClass().getName(), "delete");

throw new UnsupportedOperationException("Not yet implemented");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ public interface SoundDao {
@Query("SELECT * FROM Sound ORDER BY usageCount DESC")
Cursor loadAllOrderedByUsageCount_Cursor();

@Query("SELECT * FROM Sound s WHERE s.id IN (SELECT sounds_id FROM LetterSound_Sound WHERE LetterSound_id = :letterSoundId)")
Cursor loadAllByLetterSound(Long letterSoundId);

@Update
void update(Sound sound);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import ai.elimu.model.v2.gson.content.LetterGson;
import ai.elimu.model.v2.gson.content.LetterSoundGson;
import ai.elimu.model.v2.gson.content.SoundGson;

public class CursorToLetterSoundGsonConverter {

Expand Down Expand Up @@ -59,12 +60,39 @@ public static LetterSoundGson getLetterSoundGson(Cursor cursor, Context context,
Log.i(CursorToLetterSoundGsonConverter.class.getName(), "lettersCursor.isClosed(): " + lettersCursor.isClosed());
}

List<SoundGson> soundGsons = null;
Uri soundsUri = Uri.parse("content://" + contentProviderApplicationId + ".provider.sound_provider/sounds/by-letter-sound-id/" + id);
Log.i(CursorToLetterSoundGsonConverter.class.getName(), "soundsUri: " + soundsUri);
Cursor soundsCursor = context.getContentResolver().query(soundsUri, null, null, null, null);
if (soundsCursor == null) {
Log.e(CursorToLetterSoundGsonConverter.class.getName(), "soundsCursor == null");
Toast.makeText(context, "soundsCursor == null", Toast.LENGTH_LONG).show();
} else {
Log.i(CursorToLetterSoundGsonConverter.class.getName(), "soundsCursor.getCount(): " + soundsCursor.getCount());

soundGsons = new ArrayList<>();

boolean isLast = false;
while (!isLast) {
soundsCursor.moveToNext();

// Convert from Room to Gson
SoundGson soundGson = CursorToSoundGsonConverter.getSoundGson(soundsCursor);
soundGsons.add(soundGson);

isLast = soundsCursor.isLast();
}

soundsCursor.close();
Log.i(CursorToLetterSoundGsonConverter.class.getName(), "soundsCursor.isClosed(): " + soundsCursor.isClosed());
}

LetterSoundGson letterSound = new LetterSoundGson();
letterSound.setId(id);
letterSound.setRevisionNumber(revisionNumber);
letterSound.setUsageCount(usageCount);
letterSound.setLetters(letterGsons);
// TODO: letterSound.setSounds(sounds);
letterSound.setSounds(soundGsons);

return letterSound;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package ai.elimu.content_provider.utils.converter;

import android.database.Cursor;
import android.util.Log;

import java.util.Arrays;

import ai.elimu.model.v2.gson.content.SoundGson;

public class CursorToSoundGsonConverter {

public static SoundGson getSoundGson(Cursor cursor) {
Log.i(CursorToSoundGsonConverter.class.getName(), "getSoundGson");

Log.i(CursorToSoundGsonConverter.class.getName(), "Arrays.toString(cursor.getColumnNames()): " + Arrays.toString(cursor.getColumnNames()));

int columnIndexId = cursor.getColumnIndex("id");
Long id = cursor.getLong(columnIndexId);
Log.i(CursorToSoundGsonConverter.class.getName(), "id: " + id);

int columnIndexRevisionNumber = cursor.getColumnIndex("revisionNumber");
Integer revisionNumber = cursor.getInt(columnIndexRevisionNumber);
Log.i(CursorToSoundGsonConverter.class.getName(), "revisionNumber: " + revisionNumber);

int columnIndexValueIpa = cursor.getColumnIndex("valueIpa");
String valueIpa = cursor.getString(columnIndexValueIpa);
Log.i(CursorToSoundGsonConverter.class.getName(), "valueIpa: \"" + valueIpa + "\"");

SoundGson soundGson = new SoundGson();
soundGson.setId(id);
soundGson.setRevisionNumber(revisionNumber);
soundGson.setValueIpa(valueIpa);

return soundGson;
}
}

0 comments on commit ea1f4c7

Please sign in to comment.