Search in sources :

Example 1 with AndroidArtwork

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();
                }
            }
        }
    }
}
Also used : VolleyError(com.android.volley.VolleyError) Intent(android.content.Intent) IOException(java.io.IOException) Cursor(android.database.Cursor) DownloadManager(android.app.DownloadManager) Date(java.util.Date) CannotWriteException(org.jaudiotagger.audio.exceptions.CannotWriteException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Response(com.android.volley.Response) AudioFile(org.jaudiotagger.audio.AudioFile) Bitmap(android.graphics.Bitmap) JSONObject(org.json.JSONObject) AndroidArtwork(org.jaudiotagger.tag.images.AndroidArtwork) ImageRequest(com.android.volley.toolbox.ImageRequest) FileOutputStream(java.io.FileOutputStream) Tag(org.jaudiotagger.tag.Tag) AudioFile(org.jaudiotagger.audio.AudioFile) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

DownloadManager (android.app.DownloadManager)1 Intent (android.content.Intent)1 Cursor (android.database.Cursor)1 Bitmap (android.graphics.Bitmap)1 Response (com.android.volley.Response)1 VolleyError (com.android.volley.VolleyError)1 ImageRequest (com.android.volley.toolbox.ImageRequest)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 AudioFile (org.jaudiotagger.audio.AudioFile)1 CannotWriteException (org.jaudiotagger.audio.exceptions.CannotWriteException)1 Tag (org.jaudiotagger.tag.Tag)1 AndroidArtwork (org.jaudiotagger.tag.images.AndroidArtwork)1 JSONObject (org.json.JSONObject)1