Skip to content

Commit

Permalink
NowPlayingFragment: Add QuickLyric support.
Browse files Browse the repository at this point in the history
It shows on the NowPlayingFragment menu if the package is installed. This fixes
issue #20
  • Loading branch information
avuton committed Apr 12, 2015
1 parent c9ef307 commit 94fec96
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 22 deletions.
24 changes: 2 additions & 22 deletions MPDroid/src/main/java/com/namelessdev/mpdroid/ErrorHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -222,26 +222,6 @@ private void dismissAlertDialog() {
}
}

/**
* This method checks the {@link PackageManager} for a MPD package installation on localhost.
*
* @return True if MPD is installed, false otherwise.
*/
private boolean isMPDInstalled() {
final PackageManager packageManager = mActivity.getPackageManager();
boolean isInstalled;

try {
packageManager.getPackageInfo(MPD_PACKAGE_NAME, PackageManager.GET_SERVICES);
isInstalled = true;
} catch (final PackageManager.NameNotFoundException ignored) {
isInstalled = false;
debug("MPD is not installed, cannot launch.");
}

return isInstalled;
}

/**
* This method checks if MPD on localhost is running.
*
Expand Down Expand Up @@ -271,8 +251,8 @@ private boolean isMPDRunning() {
* This method launches MPD if a MPD server is setup for the localhost.
*/
private void launchMPD() {
if ("127.0.0.1".equals(mApp.getConnectionSettings().server) && isMPDInstalled()
&& !isMPDRunning()) {
if ("127.0.0.1".equals(mApp.getConnectionSettings().server) && !isMPDRunning() &&
Tools.isPackageInstalled(MPD_PACKAGE_NAME)) {
/**
* No delay; no matter the time given, this takes a bit.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,14 @@ public class NowPlayingFragment extends Fragment implements

private static final int POPUP_FOLDER = 3;

private static final int POPUP_LYRICS = 9;

private static final int POPUP_SHARE = 5;

private static final int POPUP_STREAM = 4;

private static final String QUICK_LYRIC_PACKAGE_NAME = "com.geecko.QuickLyric";

private static final String TAG = "NowPlayingFragment";

private final MPDApplication mApp = MPDApplication.getInstance();
Expand Down Expand Up @@ -408,6 +412,20 @@ private CoverAsyncHelper getCoverAsyncHelper(final View view) {
return coverAsyncHelper;
}

/**
* This produces a intent to open QuickLyric.
*
* @return An Intent to open QuickLyric.
*/
private Intent getLyricIntent() {
final Intent intent = new Intent(QUICK_LYRIC_PACKAGE_NAME + ".getLyrics");
final String[] tags = {mCurrentSong.getArtistName(), mCurrentSong.getTitle()};

intent.putExtra("TAGS", tags);

return intent;
}

/**
* This method generates selected track information to send to another application.
*
Expand Down Expand Up @@ -464,6 +482,9 @@ private View getSongInfo(final View view) {
R.string.goToAlbumArtist);
menu.add(Menu.NONE, POPUP_FOLDER, Menu.NONE, R.string.goToFolder);
menu.add(Menu.NONE, POPUP_CURRENT, Menu.NONE, R.string.goToCurrent);
if (com.namelessdev.mpdroid.tools.Tools.isPackageInstalled(QUICK_LYRIC_PACKAGE_NAME)) {
menu.add(Menu.NONE, POPUP_LYRICS, Menu.NONE, R.string.lyrics);
}
menu.add(Menu.NONE, POPUP_SHARE, Menu.NONE, R.string.share);
popupMenu.setOnMenuItemClickListener(this);
mPopupMenuTouchListener = PopupMenuCompat.getDragToOpenListener(popupMenu);
Expand Down Expand Up @@ -813,6 +834,11 @@ public boolean onMenuItemClick(final MenuItem item) {
}
}
break;
case POPUP_LYRICS:
if (mCurrentSong != null) {
startActivity(getLyricIntent());
}
break;
default:
result = isSimpleLibraryItem(itemId);
break;
Expand Down
26 changes: 26 additions & 0 deletions MPDroid/src/main/java/com/namelessdev/mpdroid/tools/Tools.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.StringRes;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.widget.Toast;
Expand All @@ -47,6 +49,8 @@ public void onClick(final DialogInterface dialog, final int which) {

private static final MPDApplication APP = MPDApplication.getInstance();

private static final String TAG = "Tools";

private Tools() {
super();
}
Expand Down Expand Up @@ -180,6 +184,28 @@ public static Bitmap decodeSampledBitmapFromPath(
}
}

/**
* This method checks the {@link PackageManager} for the package represented by the
* {@code packageName} argument.
*
* @param packageName The packageName to find.
* @return True if the package appears to be on the localhost, false otherwise.
*/
public static boolean isPackageInstalled(final String packageName) {
final PackageManager packageManager = APP.getPackageManager();
boolean isInstalled;

try {
packageManager.getPackageInfo(packageName, PackageManager.GET_SERVICES);
isInstalled = true;
} catch (final PackageManager.NameNotFoundException ignored) {
isInstalled = false;
Log.d(TAG, packageName + " is not installed, cannot launch.");
}

return isInstalled;
}

public static boolean isServerLocalhost() {
return "127.0.0.1".equals(APP.getConnectionSettings().server);
}
Expand Down
1 change: 1 addition & 0 deletions MPDroid/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@
<string name="enableExitConfirmationDescription">Require to double tap Back to exit</string>
<string name="enableLightTheme">Use light theme</string>
<string name="share">Share</string>
<string name="lyrics">Lyrics</string>
<string name="sharePrefix">Now Playing :</string>
<string name="enableTabletUI">Use tablet UI</string>
<string name="enableTabletUIDescription">Restart the application to see the changes</string>
Expand Down

0 comments on commit 94fec96

Please sign in to comment.