Skip to content

Commit b8e0062

Browse files
mXalnjsarabia
authored andcommitted
Profiles are saved in TranslationRecorder/Profiles directory (#1159)
* Profiles are saved in TranslationRecorder/Profiles directory * Comments fixed * App version increased * App version increased
1 parent 4e46353 commit b8e0062

3 files changed

Lines changed: 72 additions & 7 deletions

File tree

translationRecorder/app/src/main/java/org/wycliffeassociates/translationrecorder/SplashScreen.java

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
package org.wycliffeassociates.translationrecorder;
22

3-
import android.app.Activity;
43
import android.content.Intent;
54
import android.content.res.AssetManager;
5+
import android.media.MediaMetadataRetriever;
66
import android.os.Bundle;
7+
import android.os.Environment;
78
import android.preference.PreferenceManager;
9+
import android.util.Log;
810
import android.widget.ProgressBar;
911

1012
import com.door43.tools.reporting.Logger;
1113

14+
import org.apache.commons.codec.binary.Hex;
15+
import org.apache.commons.codec.digest.DigestUtils;
1216
import org.json.JSONException;
1317
import org.wycliffeassociates.translationrecorder.SettingsPage.Settings;
1418
import org.wycliffeassociates.translationrecorder.database.ProjectDatabaseHelper;
@@ -17,11 +21,10 @@
1721
import org.wycliffeassociates.translationrecorder.project.ParseJSON;
1822
import org.wycliffeassociates.translationrecorder.project.ProjectPlugin;
1923
import org.wycliffeassociates.translationrecorder.project.components.Language;
24+
import org.wycliffeassociates.translationrecorder.project.components.User;
2025

21-
import java.io.File;
22-
import java.io.FileOutputStream;
23-
import java.io.IOException;
24-
import java.io.InputStream;
26+
import java.io.*;
27+
import java.util.List;
2528

2629
/**
2730
* Created by sarabiaj on 5/5/2016.
@@ -79,6 +82,9 @@ private void initDatabase(){
7982
} catch (JSONException e) {
8083
e.printStackTrace();
8184
}
85+
86+
importProfiles(db);
87+
deleteDanglingProfiles(db);
8288
}
8389

8490
private void initializePlugins() throws IOException {
@@ -148,4 +154,48 @@ private void copyPluginContentFromAssets(AssetManager am, File outputRoot, Strin
148154
Logger.e(this.toString(), "Exception copying " + pluginName + " from assets", e);
149155
}
150156
}
157+
158+
private void importProfiles(ProjectDatabaseHelper db) {
159+
File profilesDir = new File(Environment.getExternalStorageDirectory(), "TranslationRecorder/Profiles/");
160+
if (!profilesDir.exists()) {
161+
profilesDir.mkdirs();
162+
}
163+
164+
for (File profile: profilesDir.listFiles()) {
165+
String hash = getHash(profile);
166+
String mimeType = null;
167+
168+
try {
169+
MediaMetadataRetriever mmr = new MediaMetadataRetriever();
170+
mmr.setDataSource(profile.getAbsolutePath());
171+
mimeType = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_MIMETYPE);
172+
} catch (Exception e) {
173+
Log.i("PROFILE", "File is not a media file");
174+
}
175+
176+
if(hash != null && mimeType != null && mimeType.equals("audio/mp4")) {
177+
db.addUser(new User(profile, hash));
178+
}
179+
}
180+
}
181+
182+
private void deleteDanglingProfiles(ProjectDatabaseHelper db) {
183+
List<User> profiles = db.getAllUsers();
184+
for (User profile: profiles) {
185+
File file = profile.getAudio();
186+
if(!file.exists()) {
187+
db.deleteUser(profile.getHash());
188+
}
189+
}
190+
}
191+
192+
private String getHash(File file) {
193+
try {
194+
return new String(Hex.encodeHex(DigestUtils.md5(new FileInputStream(file))));
195+
} catch (FileNotFoundException e) {
196+
return null;
197+
} catch (IOException e) {
198+
return null;
199+
}
200+
}
151201
}

translationRecorder/app/src/main/java/org/wycliffeassociates/translationrecorder/database/ProjectDatabaseHelper.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ public String getLanguageName(String languageSlug) throws IllegalArgumentExcepti
384384
name = DatabaseUtils.stringForQuery(db, languageNameQuery, new String[]{languageSlug});
385385
} catch (SQLiteDoneException e) {
386386
//db.close();
387-
throw new IllegalArgumentException("Language: " + languageSlug + " not ");
387+
throw new IllegalArgumentException("Language: " + languageSlug + " not found.");
388388
}
389389
//db.close();
390390
return name;
@@ -431,7 +431,7 @@ public User getUser(int id) throws IllegalArgumentException {
431431
String hash = cursor.getString(cursor.getColumnIndex(ProjectContract.UserEntry.USER_HASH));
432432
user = new User(userId, audio, hash);
433433
} else {
434-
throw new IllegalArgumentException("Language id not found in database.");
434+
throw new IllegalArgumentException("User id not found in database.");
435435
}
436436
return user;
437437
}
@@ -462,6 +462,13 @@ public List<User> getAllUsers() {
462462
return userList;
463463
}
464464

465+
public int deleteUser(String hash) {
466+
SQLiteDatabase db = getWritableDatabase();
467+
final String deleteWhere = String.format("%s=?", ProjectContract.UserEntry.USER_HASH);
468+
int result = db.delete(ProjectContract.UserEntry.TABLE_USER, deleteWhere, new String[]{ hash });
469+
return result;
470+
}
471+
465472
public String getBookName(String bookSlug) throws IllegalArgumentException {
466473
SQLiteDatabase db = getReadableDatabase();
467474
final String bookNameQuery = String.format("SELECT %s FROM %s WHERE %s=?",

translationRecorder/app/src/main/java/org/wycliffeassociates/translationrecorder/login/fragments/FragmentReviewProfile.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import android.graphics.Canvas
77
import android.graphics.Paint
88
import android.media.AudioTrack
99
import android.os.Bundle
10+
import android.os.Environment
1011
import android.preference.PreferenceManager
1112
import android.view.LayoutInflater
1213
import android.view.View
@@ -86,6 +87,13 @@ class FragmentReviewProfile : Fragment(), WaveformLayer.WaveformDrawDelegator {
8687
}
8788
btnYes as Button
8889
btnYes.setOnClickListener {
90+
val profilesDir = File(Environment.getExternalStorageDirectory(), "TranslationRecorder/Profiles/")
91+
if (profilesDir.exists().not()) {
92+
profilesDir.mkdirs()
93+
}
94+
val newAudio = File(profilesDir.absolutePath + "/" + audio.name)
95+
audio.renameTo(newAudio)
96+
audio = newAudio
8997
val user = User(audio, hash)
9098
val db = ProjectDatabaseHelper(activity)
9199
db.addUser(user)

0 commit comments

Comments
 (0)