Android Open Source - LyricHere Music Broadcast Receiver






From Project

Back to project page LyricHere.

License

The source code is released under:

Apache License

If you think the Android project LyricHere listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package cn.zhaiyifan.lyrichere;
//from  w w  w.  java2 s .  co  m
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.text.TextUtils;

import cn.zhaiyifan.lyrichere.ui.LyricExplorerActivity;
import cn.zhaiyifan.lyrichere.utils.Util;

/**
 * Created by yifan on 6/21/14.
 */
public class MusicBroadcastReceiver extends BroadcastReceiver {
    private static final String TAG = MusicBroadcastReceiver.class.getSimpleName();
    private static final int NOTIFICATION_ID = 5657;

    private String mTitle = null;
    private String mArtist = null;
    private String mAlbum = null;

    @Override
    public void onReceive(Context context, Intent intent) {
        Util.log(TAG, "Action : " + intent.getAction());
        String artist = intent.getStringExtra("artist");
        String title = intent.getStringExtra("track");
        String album = intent.getStringExtra("album");

        // at least we should have a track title
        if (title == null) {
            return;
        }

        boolean isPlaying = intent.getBooleanExtra("isplaying", true);
        isPlaying = intent.getBooleanExtra("playing", isPlaying);
        isPlaying = intent.getBooleanExtra("spotifyPlaying", isPlaying);

        Util.log(TAG, String.valueOf("title: " + title));
        Util.log(TAG, String.valueOf("isPlaying: " + isPlaying));

        if (isPlaying && !isSameTrack(title, artist, album)) {
            addNotification(context);
        } else {
            removeNotification(context);
        }
    }

    public void addNotification(Context context) {
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder mNotifyBuilder;
        Intent intent = new Intent(context, LyricExplorerActivity.class);
        intent.putExtra(Constants.Column.TITLE, mTitle);
        intent.putExtra(Constants.Column.ARTIST, mArtist);
        intent.putExtra(Constants.Column.ALBUM, mAlbum);
        mNotifyBuilder = new NotificationCompat.Builder(context)
                .setContentTitle(context.getString(R.string.notification_title))
                .setContentText(makeNotificationText())
                .setTicker(context.getString(R.string.notification_ticker))
                .setContentIntent(PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT))
                .setSmallIcon(R.drawable.play);
        notificationManager.notify(NOTIFICATION_ID, mNotifyBuilder.build());
    }

    public void removeNotification(Context paramContext) {
        ((NotificationManager) paramContext.getSystemService(Context.NOTIFICATION_SERVICE)).cancel(NOTIFICATION_ID);
    }

    private String makeNotificationText() {
        String text = null;
        if (!TextUtils.isEmpty(mTitle)) {
            if (TextUtils.isEmpty(mArtist)) {
                text = mTitle;
            } else {
                text = String.format("%s - %s", mArtist, mTitle);
            }
        }
        return text;
    }

    /**
     * Check if new received meta is the same track as before
     */
    private synchronized boolean isSameTrack(String title, String artist, String album) {
        boolean isSame = true;
        if (title != null) {
            // mTitle is null or different from title, then different track
            if (!TextUtils.equals(title, mTitle) || !TextUtils.equals(artist, mArtist) || !TextUtils.equals(album, mAlbum)) {
                isSame = false;
            } else {
                isSame = true;
            }
        }
        if (!isSame) {
            mTitle = title;
            mArtist = artist;
            mAlbum = album;
        }
        Util.log(TAG, "isSameTrack: " + String.valueOf(isSame));
        return isSame;
    }
}




Java Source Code List

cn.zhaiyifan.lyrichere.ApplicationTest.java
cn.zhaiyifan.lyrichere.Constants.java
cn.zhaiyifan.lyrichere.MusicBroadcastReceiver.java
cn.zhaiyifan.lyrichere.adapters.LyricCursorAdapter.java
cn.zhaiyifan.lyrichere.db.DbHelper.java
cn.zhaiyifan.lyrichere.db.LyricContentProvider.java
cn.zhaiyifan.lyrichere.model.Lyric.java
cn.zhaiyifan.lyrichere.prefs.SettingsActivity.java
cn.zhaiyifan.lyrichere.prefs.SettingsFragment.java
cn.zhaiyifan.lyrichere.prefs.colorpicker.AlphaPatternDrawable.java
cn.zhaiyifan.lyrichere.prefs.colorpicker.ColorPickerDialog.java
cn.zhaiyifan.lyrichere.prefs.colorpicker.ColorPickerPanelView.java
cn.zhaiyifan.lyrichere.prefs.colorpicker.ColorPickerPreference.java
cn.zhaiyifan.lyrichere.prefs.colorpicker.ColorPickerView.java
cn.zhaiyifan.lyrichere.prefs.colorpicker.Test.java
cn.zhaiyifan.lyrichere.ui.AboutActivity.java
cn.zhaiyifan.lyrichere.ui.DownloadFragment.java
cn.zhaiyifan.lyrichere.ui.ListScrollTextView.java
cn.zhaiyifan.lyrichere.ui.LyricExplorerActivity.java
cn.zhaiyifan.lyrichere.ui.LyricExplorerFragment.java
cn.zhaiyifan.lyrichere.ui.LyricPlayerActivity.java
cn.zhaiyifan.lyrichere.ui.LyricPlayerFragment.java
cn.zhaiyifan.lyrichere.ui.LyricSearchView.java
cn.zhaiyifan.lyrichere.ui.LyricView.java
cn.zhaiyifan.lyrichere.utils.DbUtils.java
cn.zhaiyifan.lyrichere.utils.FileUtils.java
cn.zhaiyifan.lyrichere.utils.LyricCache.java
cn.zhaiyifan.lyrichere.utils.LyricProvider.java
cn.zhaiyifan.lyrichere.utils.LyricUtils.java
cn.zhaiyifan.lyrichere.utils.Util.java
cn.zhaiyifan.lyrichere.workers.Finder.java
cn.zhaiyifan.lyrichere.workers.LyricEncodingUpdater.java
cn.zhaiyifan.lyrichere.workers.LyricLastVisitUpdater.java
cn.zhaiyifan.lyrichere.workers.LyricOpener.java