use of org.jaudiotagger.tag.images.AndroidArtwork in project saga-android by AnandChowdhary.
the class DownloadReceiver method onReceive.
@Override
public void onReceive(final Context context, final Intent intent) {
DownloadManager dMgr = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
Long downloadId = intent.getExtras().getLong(DownloadManager.EXTRA_DOWNLOAD_ID);
Cursor c = dMgr.query(new DownloadManager.Query().setFilterById(downloadId));
if (c.moveToFirst()) {
int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
if (status == DownloadManager.STATUS_SUCCESSFUL) {
final String title = c.getString(c.getColumnIndex(DownloadManager.COLUMN_TITLE));
Log.d("Receiver", "Title:" + title);
if (title.equalsIgnoreCase(context.getString(R.string.app_name) + " " + context.getString(R.string.update))) {
Intent install = new Intent(Intent.ACTION_VIEW);
install.setDataAndType(Uri.parse(c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI))), "application/vnd.android.package-archive");
install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(install);
} else {
try {
TagOptionSingleton.getInstance().setAndroid(true);
final File file = new File(Utils.getStoragePath(context) + "/" + title);
final AudioFile f = AudioFileIO.read(file);
final Tag tag = f.getTag();
String json = readFromFile(context, title);
String url = null;
if (json != null) {
JSONObject jsonObject = new JSONObject(json);
if (jsonObject.getString("track") != null) {
if (jsonObject.getString("artist") != null) {
url = Utils.getAlbumArt(jsonObject.getString("track"), jsonObject.getString("artist"));
} else {
url = Utils.getAlbumArt(jsonObject.getString("track"), null);
}
}
if (jsonObject.getString("artist") != null)
tag.setField(FieldKey.ARTIST, jsonObject.getString("artist"));
if (jsonObject.getString("artist") != null)
tag.setField(FieldKey.ALBUM_ARTIST, jsonObject.getString("artist"));
if (jsonObject.getString("release") != null)
tag.setField(FieldKey.YEAR, jsonObject.getString("release"));
if (jsonObject.getString("trackno") != null)
tag.setField(FieldKey.TRACK, jsonObject.getString("trackno"));
if (jsonObject.getString("album") != null)
tag.setField(FieldKey.ALBUM, jsonObject.getString("album"));
if (jsonObject.getString("genre") != null)
tag.setField(FieldKey.GENRE, jsonObject.getString("genre"));
tag.setField(FieldKey.COMMENT, "Downloaded from Saga");
} else {
url = Utils.getAlbumArt(title.substring(0, title.length() - 4), null);
}
if (url != null) {
ImageRequest request = new ImageRequest(url, new Response.Listener<Bitmap>() {
@Override
public void onResponse(Bitmap bitmap) {
FileOutputStream out = null;
try {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = context.getCacheDir();
File cover = File.createTempFile(imageFileName, /* prefix */
".jpg", /* suffix */
storageDir);
out = new FileOutputStream(cover);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
AndroidArtwork artwork = AndroidArtwork.createArtworkFromFile(cover);
tag.setField(artwork);
Log.d(TAG, "AlbumArt deleted " + cover.delete());
} catch (Exception e) {
e.printStackTrace();
} finally {
commitAudio(context, f, file);
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}, 0, 0, null, new Response.ErrorListener() {
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
commitAudio(context, f, file);
}
});
request.setShouldCache(false);
VolleySingleton.getInstance(context).addToRequestQueue(request);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
Aggregations