Skip to content
This repository has been archived by the owner on Apr 17, 2020. It is now read-only.

Commit

Permalink
now the remote control only needs the exact ip address where elpis is…
Browse files Browse the repository at this point in the history
… running (be sure to press "set ip" when done).

Detection is now very fast.
  • Loading branch information
seliver committed Feb 6, 2015
1 parent b4ecaf4 commit 1e016dd
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 89 deletions.
14 changes: 11 additions & 3 deletions res/layout/activity_remote_control.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView"
android:layout_alignBottom="@+id/textView"
android:layout_alignParentRight="true"
android:layout_toRightOf="@+id/textView"
android:ems="10"
android:inputType="text"
android:text="@string/ip_port" />
android:inputType="phone"
android:text="@string/ip_port"
android:imeActionLabel="Set IP"
android:imeActionId="123" />

<TextView
android:id="@+id/songAndArtist"
Expand Down Expand Up @@ -91,4 +92,11 @@
android:layout_alignRight="@+id/coverImage"
android:text="@string/next_button_string" />

<ProgressBar
android:id="@+id/ProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/ipandportvalue" />

</RelativeLayout>
2 changes: 1 addition & 1 deletion res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="ip_port_label">Your IP/Port</string>
<string name="ip_port">http://192.168.1.64:35747</string>
<string name="ip_port">192.168.1.64</string>
<string name="play_button_string">Playing</string>
<string name="pause_button_string">Paused</string>
<string name="next_button_string">Next</string>
Expand Down
7 changes: 7 additions & 0 deletions src/com/alexey_sel/elpisremote/Constants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.alexey_sel.elpisremote;

public class Constants {
public final static String IP_PORT_SEPARATOR = ":";
public final static String PORT = "35747" ;
public final static String PREFIX = "http://" ;
}
245 changes: 160 additions & 85 deletions src/com/alexey_sel/elpisremote/RemoteControl.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;

import org.apache.http.HttpResponse;
Expand All @@ -24,6 +27,9 @@
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
Expand All @@ -46,6 +52,9 @@ public class RemoteControl extends Activity implements AsyncResponse {
Button dislike;
public AsyncResponse handler;
boolean forceUpdate = false;
boolean connected = false;
ProgressBar progressBar;
String ip;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -58,7 +67,9 @@ protected void onCreate(Bundle savedInstanceState) {
currentSongAmazonID = "";
ipport = (EditText) findViewById(R.id.ipandportvalue);
songAndArtist = (TextView) findViewById(R.id.songAndArtist);
progressBar = (ProgressBar) findViewById(R.id.ProgressBar);
handler = this;
ip = ipport.getText().toString();
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Expand All @@ -74,6 +85,7 @@ public void onClick(View v) {
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showProgressBar();
new RequestTask().execute("next", new String());
}
});
Expand All @@ -89,7 +101,17 @@ public void onClick(View v) {
new RequestTask().execute("dislike", new String());
}
});
getCurrentSong();
ipport.setOnEditorActionListener(new OnEditorActionListener() {

@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId==123){
ip = ipport.getText().toString();
Log.d("Setting ip", ip);
}
return false;
}
});
ScheduledExecutorService scheduler = Executors
.newSingleThreadScheduledExecutor();

Expand All @@ -99,6 +121,31 @@ public void run() {
}
}, 0, 10, TimeUnit.SECONDS);

scheduler.scheduleAtFixedRate(new Runnable() {
public void run() {
if (!connected){
showProgressBar();
connect();
}else{
hideProgressBar();
}
}
}, 0, 2, TimeUnit.SECONDS);

}

public void showProgressBar() {
if (progressBar.getVisibility() != View.VISIBLE)
progressBar.setVisibility(View.VISIBLE);
}

public void hideProgressBar() {
if (progressBar.getVisibility() == View.VISIBLE)
progressBar.setVisibility(View.GONE);
}

public void connect() {
new RequestTask().execute("connect", new String());
}

public void getCurrentSong() {
Expand Down Expand Up @@ -130,40 +177,58 @@ protected void onPostExecute(Bitmap result) {
}
}

public String getIPPort() {
String address = Constants.PREFIX + ip
+ Constants.IP_PORT_SEPARATOR + Constants.PORT;
//Log.d("address", address);
return address;
}

class RequestTask extends AsyncTask<String, String, String> {
public String command = null;

@Override
protected String doInBackground(String... uri) {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
this.command = uri[0];
String responseString = uri[1];
try {
response = httpclient.execute(new HttpGet(ipport.getText()
.toString() + "/" + uri[0]));
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
responseString = out.toString();
out.close();
} else {
// Closes the connection.
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
if (connected || command.equals("connect")) {
Log.d("Command:ip", command+":"+ip);
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 100);
HttpClient httpclient = new DefaultHttpClient(httpParams);
HttpResponse response;

String responseString = uri[1];
try {
response = httpclient.execute(new HttpGet(getIPPort() + "/"
+ uri[0]));
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
responseString = out.toString();
out.close();
} else {
// Closes the connection.
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
} catch (ClientProtocolException e) {
return null;
} catch (IOException e) {
return null;
}
} catch (ClientProtocolException e) {
// TODO Handle problems..
} catch (IOException e) {
// TODO Handle problems..
return responseString;
} else {
showProgressBar();
return null;
}
return responseString;
}

@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (result == null)
result = "";
handler.processFinish(result, this.command);
}
}
Expand Down Expand Up @@ -192,74 +257,84 @@ public boolean onOptionsItemSelected(MenuItem item) {

@Override
public void processFinish(String output, String command) {
switch (command) {
case "currentsong": {
try {
JSONObject jsonObj = new JSONObject(output);
JSONObject station = jsonObj.getJSONObject("Station");
if (station.getBoolean("SkipLimitReached")) {
new AlertDialog.Builder(this)
.setTitle("The Box is Open")
.setMessage(
"Ups, the Skip Limit Was Reached...You're screwed.")
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int which) {
dialog.cancel();
}
})
.setIcon(android.R.drawable.ic_dialog_alert).show();
} else {
if (!jsonObj.getString("AmazonTrackID").equals(
currentSongAmazonID)
|| forceUpdate) {
forceUpdate = false;
new DownloadImageTask(
(ImageView) findViewById(R.id.coverImage))
.execute(jsonObj.getString("AlbumArtUrl"));
songAndArtist.setText(jsonObj.getString("SongTitle")
+ " by " + jsonObj.getString("Artist"));
currentSongAmazonID = jsonObj
.getString("AmazonTrackID");
boolean liked = jsonObj.getBoolean("Loved");
if (liked) {
like.setText(R.string.liked_button_string);
} else {
like.setText(R.string.like_button_string);
if (connected || command.equals("connect")) {
switch (command) {
case "currentsong": {
try {
JSONObject jsonObj = new JSONObject(output);
JSONObject station = jsonObj.getJSONObject("Station");
if (station.getBoolean("SkipLimitReached")) {
new AlertDialog.Builder(this)
.setTitle("The Box is Open")
.setMessage(
"Ups, the Skip Limit Was Reached...You're screwed.")
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int which) {
dialog.cancel();
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
} else {
if (!jsonObj.getString("AmazonTrackID").equals(
currentSongAmazonID)
|| forceUpdate) {
forceUpdate = false;
new DownloadImageTask(
(ImageView) findViewById(R.id.coverImage))
.execute(jsonObj.getString("AlbumArtUrl"));
songAndArtist.setText(jsonObj
.getString("SongTitle")
+ " by "
+ jsonObj.getString("Artist"));
currentSongAmazonID = jsonObj
.getString("AmazonTrackID");
boolean liked = jsonObj.getBoolean("Loved");
if (liked) {
like.setText(R.string.liked_button_string);
} else {
like.setText(R.string.like_button_string);
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
} catch (JSONException e) {
e.printStackTrace();
break;
}
case "next": {
Toast.makeText(getApplicationContext(), output,
Toast.LENGTH_LONG).show();
getCurrentSong();
break;
}
case "dislike": {
getCurrentSong();
break;
}
case "like": {
if (output.equals("Like")) {
like.setText(R.string.liked_button_string);
} else {
like.setText(R.string.like_button_string);
}
break;
}
case "connect": {
if (output.equals("true")) {
connected = true;
}
break;
}
default: {
Toast.makeText(getApplicationContext(), output,
Toast.LENGTH_LONG).show();
break;
}
break;
}
case "next":{
Toast.makeText(getApplicationContext(), output, Toast.LENGTH_LONG)
.show();
getCurrentSong();
break;
}
case "dislike": {
getCurrentSong();
break;
}
case "like": {
if (output.equals("Like")) {
like.setText(R.string.liked_button_string);
} else {
like.setText(R.string.like_button_string);
}
break;
}

default: {
Toast.makeText(getApplicationContext(), output, Toast.LENGTH_LONG)
.show();
break;
}
}
}
}

0 comments on commit 1e016dd

Please sign in to comment.