|
1 | 1 | package org.wycliffeassociates.translationrecorder; |
2 | 2 |
|
3 | | -import android.app.Activity; |
4 | 3 | import android.content.Intent; |
5 | 4 | import android.content.res.AssetManager; |
| 5 | +import android.media.MediaMetadataRetriever; |
6 | 6 | import android.os.Bundle; |
| 7 | +import android.os.Environment; |
7 | 8 | import android.preference.PreferenceManager; |
| 9 | +import android.util.Log; |
8 | 10 | import android.widget.ProgressBar; |
9 | 11 |
|
10 | 12 | import com.door43.tools.reporting.Logger; |
11 | 13 |
|
| 14 | +import org.apache.commons.codec.binary.Hex; |
| 15 | +import org.apache.commons.codec.digest.DigestUtils; |
12 | 16 | import org.json.JSONException; |
13 | 17 | import org.wycliffeassociates.translationrecorder.SettingsPage.Settings; |
14 | 18 | import org.wycliffeassociates.translationrecorder.database.ProjectDatabaseHelper; |
|
17 | 21 | import org.wycliffeassociates.translationrecorder.project.ParseJSON; |
18 | 22 | import org.wycliffeassociates.translationrecorder.project.ProjectPlugin; |
19 | 23 | import org.wycliffeassociates.translationrecorder.project.components.Language; |
| 24 | +import org.wycliffeassociates.translationrecorder.project.components.User; |
20 | 25 |
|
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; |
25 | 28 |
|
26 | 29 | /** |
27 | 30 | * Created by sarabiaj on 5/5/2016. |
@@ -79,6 +82,9 @@ private void initDatabase(){ |
79 | 82 | } catch (JSONException e) { |
80 | 83 | e.printStackTrace(); |
81 | 84 | } |
| 85 | + |
| 86 | + importProfiles(db); |
| 87 | + deleteDanglingProfiles(db); |
82 | 88 | } |
83 | 89 |
|
84 | 90 | private void initializePlugins() throws IOException { |
@@ -148,4 +154,48 @@ private void copyPluginContentFromAssets(AssetManager am, File outputRoot, Strin |
148 | 154 | Logger.e(this.toString(), "Exception copying " + pluginName + " from assets", e); |
149 | 155 | } |
150 | 156 | } |
| 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 | + } |
151 | 201 | } |
0 commit comments