We are no longer offering accounts on this server. Consider https://gitlab.freedesktop.org/ as a place to host projects.

Commit b0f743a5 authored by Mike Sheldon's avatar Mike Sheldon

Allow for banning and loving tracks from Libre Droid

parent 866e7cf6
......@@ -13,14 +13,16 @@ public final class R {
public static final class drawable {
public static final int album=0x7f020000;
public static final int back=0x7f020001;
public static final int icon=0x7f020002;
public static final int logo=0x7f020003;
public static final int next=0x7f020004;
public static final int pause=0x7f020005;
public static final int play=0x7f020006;
public static final int prev=0x7f020007;
public static final int quit=0x7f020008;
public static final int save=0x7f020009;
public static final int ban=0x7f020002;
public static final int icon=0x7f020003;
public static final int logo=0x7f020004;
public static final int love=0x7f020005;
public static final int next=0x7f020006;
public static final int pause=0x7f020007;
public static final int play=0x7f020008;
public static final int prev=0x7f020009;
public static final int quit=0x7f02000a;
public static final int save=0x7f02000b;
}
public static final class id {
public static final int ImageView01=0x7f050004;
......@@ -41,12 +43,14 @@ public final class R {
public static final int TextView07=0x7f050006;
public static final int albumImage=0x7f050014;
public static final int artistText=0x7f050016;
public static final int banButton=0x7f050018;
public static final int loginButton=0x7f05000b;
public static final int nextButton=0x7f05001a;
public static final int loveButton=0x7f05001d;
public static final int nextButton=0x7f05001b;
public static final int passwordEntry=0x7f05000a;
public static final int playPauseButton=0x7f050019;
public static final int prevButton=0x7f050018;
public static final int saveButton=0x7f05001b;
public static final int playPauseButton=0x7f05001a;
public static final int prevButton=0x7f050019;
public static final int saveButton=0x7f05001c;
public static final int stationNameText=0x7f050013;
public static final int titleText=0x7f050015;
public static final int usernameEntry=0x7f050008;
......
......@@ -34,11 +34,14 @@
<TableRow android:id="@+id/TableRow03" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_marginTop="10px" android:layout_gravity="center_horizontal" android:gravity="center_horizontal"><ImageButton android:layout_height="wrap_content" android:id="@+id/prevButton" android:src="@drawable/prev" android:layout_width="fill_parent"></ImageButton>
<TableRow android:id="@+id/TableRow03" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_marginTop="10px" android:layout_gravity="center_horizontal" android:gravity="center_horizontal">
<ImageButton android:layout_height="wrap_content" android:id="@+id/banButton" android:src="@drawable/ban" android:layout_width="fill_parent" android:layout_marginRight="1px"></ImageButton>
<ImageButton android:layout_height="wrap_content" android:id="@+id/prevButton" android:src="@drawable/prev" android:layout_width="fill_parent"></ImageButton>
<ImageButton android:layout_height="wrap_content" android:src="@drawable/pause" android:id="@+id/playPauseButton" android:layout_width="fill_parent" android:layout_marginLeft="20px" android:layout_marginRight="20px"></ImageButton><ImageButton android:layout_height="wrap_content" android:id="@+id/nextButton" android:src="@drawable/next" android:layout_width="fill_parent" android:layout_marginRight="20px"></ImageButton>
<ImageButton android:layout_height="wrap_content" android:src="@drawable/pause" android:id="@+id/playPauseButton" android:layout_width="fill_parent"></ImageButton><ImageButton android:layout_height="wrap_content" android:id="@+id/nextButton" android:src="@drawable/next" android:layout_width="fill_parent"></ImageButton>
<ImageButton android:layout_height="wrap_content" android:id="@+id/saveButton" android:src="@drawable/save" android:layout_width="fill_parent"></ImageButton>
<ImageButton android:layout_height="wrap_content" android:id="@+id/loveButton" android:src="@drawable/love" android:layout_width="fill_parent"></ImageButton>
</TableRow>
</TableLayout>
......
......@@ -11,6 +11,8 @@ public interface ILibreService {
public void play();
public void next();
public void prev();
public void love();
public void ban();
public Song getSong();
public Song getSong(int songNumber);
public void tuneStation(String type, String station);
......
......@@ -159,6 +159,18 @@ public class LibreDroid extends Activity {
LibreDroid.this.save();
}
});
final ImageButton loveButton = (ImageButton) findViewById(R.id.loveButton);
loveButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
LibreDroid.this.libreServiceConn.service.love();
}
});
final ImageButton banButton = (ImageButton) findViewById(R.id.banButton);
banButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
LibreDroid.this.libreServiceConn.service.ban();
}
});
}
@Override
......
......@@ -37,6 +37,7 @@ public class LibreService extends Service implements OnBufferingUpdateListener,
private Playlist playlist;
private String sessionKey;
private String scrobbleKey;
private String serviceToken;
private String stationName;
private int currentSong;
private int currentPage = 0;
......@@ -94,6 +95,7 @@ public class LibreService extends Service implements OnBufferingUpdateListener,
loggedIn = false;
String passMD5 = "";
String token = "";
String wsToken = "";
long timestamp = new Date().getTime() / 1000;
try {
MessageDigest md = MessageDigest.getInstance("MD5");
......@@ -108,6 +110,12 @@ public class LibreService extends Service implements OnBufferingUpdateListener,
if (token.length() == 31) {
token = "0" + token;
}
wsToken = username + passMD5;
md.update(wsToken.getBytes(), 0, wsToken.length());
wsToken = new BigInteger(1, md.digest()).toString(16);
if (wsToken.length() == 31) {
wsToken = "0" + wsToken;
}
} catch (NoSuchAlgorithmException ex) {
Toast.makeText(this, "MD5 hashing unavailable, unable to login.", Toast.LENGTH_LONG);
}
......@@ -130,7 +138,12 @@ public class LibreService extends Service implements OnBufferingUpdateListener,
output = this.httpGet("http://turtle.libre.fm/?hs=true&p=1.2&u=" + username + "&t=" + Long.toString(timestamp) + "&a=" + token + "&c=ldr" );
if (output.split("\n")[0].equals("OK")) {
this.scrobbleKey = output.split("\n")[1].trim();
}
}
// Login for web services
output = this.httpGet("http://alpha.libre.fm/2.0/?method=auth.getmobilesession&username=" + username + "&authToken=" + wsToken);
WSParser wsp = new WSParser();
wsp.parse(output);
this.serviceToken = wsp.getKey();
} catch (Exception ex) {
Toast.makeText(this, "Unable to connect to libre.fm server: " + ex.getMessage(), Toast.LENGTH_LONG).show();
}
......@@ -208,6 +221,27 @@ public class LibreService extends Service implements OnBufferingUpdateListener,
mp.stop();
}
public void love() {
Song song = this.getSong();
try {
this.httpPost("http://alpha.libre.fm/2.0/", "method", "track.love", "sk", this.serviceToken, "artist", song.artist, "track", song.title);
Toast.makeText(this, "Loved", Toast.LENGTH_SHORT).show();
} catch (Exception ex) {
Log.d("libredroid", "Couldn't love " + song.title + ": " + ex.getMessage());
}
}
public void ban() {
Song song = this.getSong();
try {
this.httpPost("http://alpha.libre.fm/2.0/", "method", "track.ban", "sk", this.serviceToken, "artist", song.artist, "track", song.title);
Toast.makeText(this, "Banned", Toast.LENGTH_SHORT).show();
} catch (Exception ex) {
Log.d("libredroid", "Couldn't ban " + song.title + ": " + ex.getMessage());
}
this.next();
}
public Song getSong() {
return this.getSong(this.currentSong);
}
......@@ -316,6 +350,14 @@ public class LibreService extends Service implements OnBufferingUpdateListener,
LibreService.this.prev();
}
public void love() {
LibreService.this.love();
}
public void ban() {
LibreService.this.ban();
}
public Song getSong() {
return LibreService.this.getSong();
}
......
package fm.libre.droid;
import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import android.util.Log;
public class WSParser extends DefaultHandler {
private String currentElement;
private String key = "";
public void startElement(String uri, String name, String qName, Attributes atts) {
this.currentElement = name;
}
public void endElement(String uri, String name, String qName) throws SAXException {
}
public void characters(char ch[], int start, int length) {
String chars = new String(ch).substring(start, start + length).trim();
Log.d("libredroid", chars);
if (this.currentElement.equals("key")) {
this.key += chars;
}
}
public void parse(String xspf) throws SAXException, ParserConfigurationException, IOException {
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
xr.setContentHandler(this);
xr.parse(new InputSource(new StringReader(xspf)));
}
public String getKey() {
return key;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment