Skip to content

Commit 0c6ab24

Browse files
author
Chris Bellew
committed
Upgraded local server and client scanning to support Rokus. A few other small bug fixes.
1 parent 0fbf38a commit 0c6ab24

File tree

10 files changed

+84
-80
lines changed

10 files changed

+84
-80
lines changed

Voice Control For Plex/src/main/java/com/atomjack/vcfp/GDMService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ protected void onHandleIntent(Intent intent) {
3131
int port = intent.getIntExtra("port", 32414); // Default port, for Plex Media Servers (Clients use 32412)
3232
DatagramSocket socket = new DatagramSocket(32420);
3333
socket.setBroadcast(true);
34-
String data = "M-SEARCH * HTTP/1.0";
34+
String data = "M-SEARCH * HTTP/1.1";
3535
DatagramPacket packet = new DatagramPacket(data.getBytes(), data.length(), getBroadcastAddress(), port);
3636
socket.send(packet);
3737
Logger.i("Search Packet Broadcasted");

Voice Control For Plex/src/main/java/com/atomjack/vcfp/LocalScan.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ public void searchForPlexClients() {
121121
context.startService(mServiceIntent);
122122
}
123123

124+
public void hideSearchDialog() {
125+
if(searchDialog != null)
126+
searchDialog.dismiss();
127+
}
128+
124129
public void showPlexClients(Map<String, PlexClient> clients) {
125130
showPlexClients(clients, false);
126131
}

Voice Control For Plex/src/main/java/com/atomjack/vcfp/PlexSearchService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ private void resumePlayback() {
562562

563563
private void stopPlayback() {
564564
adjustPlayback("stop", getResources().getString(R.string.playback_stopped));
565-
if(VoiceControlForPlexApplication.isNowPlayingVisible()) {
565+
if(VoiceControlForPlexApplication.isApplicationVisible()) {
566566
Intent stopIntent = new Intent(this, NowPlayingActivity.class);
567567
stopIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
568568
stopIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

Voice Control For Plex/src/main/java/com/atomjack/vcfp/VoiceControlForPlexApplication.java

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class VoiceControlForPlexApplication extends Application
2828
{
2929
public final static String MINIMUM_PHT_VERSION = "1.0.7";
3030

31-
private static boolean nowPlayingVisible;
31+
private static boolean isApplicationVisible;
3232

3333
public final static class Intent {
3434
public final static String GDMRECEIVE = "com.atomjack.vcfp.intent.gdmreceive";
@@ -82,20 +82,25 @@ public static void addPlexServer(final PlexServer server) {
8282
}
8383
if (!servers.containsKey(server.name)) {
8484
try {
85-
String url = String.format("http://%s:%s/library/sections/", server.address, server.port);
86-
if(server.accessToken != null)
87-
url += String.format("?%s=%s", PlexHeaders.XPlexToken, server.accessToken);
88-
AsyncHttpClient httpClient = new AsyncHttpClient();
89-
httpClient.get(url, new AsyncHttpResponseHandler() {
90-
@Override
91-
public void onSuccess(int statusCode, org.apache.http.Header[] headers, byte[] responseBody) {
85+
server.findServerConnection(new ServerFindHandler() {
86+
@Override
87+
public void onSuccess() {
88+
Logger.d("active connection: %s", server.activeConnection);
89+
String url = String.format("http://%s:%s/library/sections/", server.activeConnection.address, server.activeConnection.port);
90+
if(server.accessToken != null)
91+
url += String.format("?%s=%s", PlexHeaders.XPlexToken, server.accessToken);
92+
AsyncHttpClient httpClient = new AsyncHttpClient();
93+
Logger.d("Fetching %s", url);
94+
httpClient.get(url, new AsyncHttpResponseHandler() {
95+
@Override
96+
public void onSuccess(int statusCode, org.apache.http.Header[] headers, byte[] responseBody) {
9297
MediaContainer mc = new MediaContainer();
9398
try {
9499
mc = serial.read(MediaContainer.class, new String(responseBody, "UTF-8"));
95100
} catch (NotFoundException e) {
96-
e.printStackTrace();
101+
e.printStackTrace();
97102
} catch (Exception e) {
98-
e.printStackTrace();
103+
e.printStackTrace();
99104
}
100105
for(int i=0;i<mc.directories.size();i++) {
101106
if(mc.directories.get(i).type.equals("movie")) {
@@ -113,9 +118,17 @@ public void onSuccess(int statusCode, org.apache.http.Header[] headers, byte[] r
113118
servers.put(server.name, server);
114119
Logger.d("Added %s.", server.name);
115120
}
116-
}
121+
}
122+
});
123+
}
124+
125+
@Override
126+
public void onFailure(int statusCode) {
127+
// TODO: Handle failure here
128+
}
117129
});
118130

131+
119132
} catch (Exception e) {
120133
Logger.e("Exception getting clients: %s", e.toString());
121134
}
@@ -153,15 +166,15 @@ public void onClick(DialogInterface dialog, int id) {
153166
usageDialog.show();
154167
}
155168

156-
public static boolean isNowPlayingVisible() {
157-
return nowPlayingVisible;
169+
public static boolean isApplicationVisible() {
170+
return isApplicationVisible;
158171
}
159172

160-
public static void nowPlayingResumed() {
161-
nowPlayingVisible = true;
173+
public static void applicationResumed() {
174+
isApplicationVisible = true;
162175
}
163176

164-
public static void nowPlayingPaused() {
165-
nowPlayingVisible = false;
177+
public static void applicationPaused() {
178+
isApplicationVisible = false;
166179
}
167180
}

Voice Control For Plex/src/main/java/com/atomjack/vcfp/activities/MainActivity.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,7 @@ public void onClick(DialogInterface dialog, int id) {
798798
mPrefsEditor.commit();
799799
localScan.showPlexClients(VoiceControlForPlexApplication.clients);
800800
} else {
801+
localScan.hideSearchDialog();
801802
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
802803
builder.setTitle(R.string.no_clients_found);
803804
builder.setCancelable(false)
@@ -883,6 +884,7 @@ protected void onDestroy() {
883884
@Override
884885
protected void onPause() {
885886
super.onPause();
887+
VoiceControlForPlexApplication.applicationPaused();
886888
if(gdmReceiver != null) {
887889
LocalBroadcastManager.getInstance(this).unregisterReceiver(gdmReceiver);
888890
}
@@ -891,6 +893,7 @@ protected void onPause() {
891893
@Override
892894
protected void onResume() {
893895
super.onResume();
896+
VoiceControlForPlexApplication.applicationResumed();
894897
if(gdmReceiver != null) {
895898
IntentFilter filters = new IntentFilter();
896899
filters.addAction(GDMService.MSG_RECEIVED);

Voice Control For Plex/src/main/java/com/atomjack/vcfp/activities/NowPlayingActivity.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public class NowPlayingActivity extends Activity {
4949
private int commandId = 0;
5050
private int subscriptionPort = 7777;
5151
private boolean subscribed = false;
52+
private boolean subscriptionHasStarted = false;
5253
private ServerSocket serverSocket;
5354
Thread serverThread = null;
5455
Handler updateConversationHandler;
@@ -252,14 +253,17 @@ else if(playingTrack != null)
252253
if(type != null) {
253254
Timeline timeline = mc.getTimeline(type);
254255
// If the playing media has stopped, unsubscribe then exit from this activity.
255-
if(timeline.state.equals("stopped")) {
256+
if(timeline.state.equals("stopped") && subscriptionHasStarted) {
256257
unsubscribe(new Runnable() {
257258
@Override
258259
public void run() {
259-
if(VoiceControlForPlexApplication.isNowPlayingVisible())
260+
subscriptionHasStarted = false;
261+
if(VoiceControlForPlexApplication.isApplicationVisible())
260262
finish();
261263
}
262264
});
265+
} else if(timeline.state.equals("playing")) {
266+
subscriptionHasStarted = true;
263267
}
264268
}
265269
}
@@ -268,14 +272,15 @@ public void run() {
268272
@Override
269273
protected void onPause() {
270274
super.onPause();
271-
VoiceControlForPlexApplication.nowPlayingPaused();
275+
VoiceControlForPlexApplication.applicationPaused();
272276
Logger.d("now playing paused");
273277
}
274278

275279
@Override
276280
protected void onResume() {
277281
super.onResume();
278-
VoiceControlForPlexApplication.nowPlayingResumed();
282+
Logger.d("now playing resumed");
283+
VoiceControlForPlexApplication.applicationResumed();
279284
}
280285

281286
private void subscribe() {
@@ -325,7 +330,8 @@ public void onSuccess(PlexResponse response) {
325330

326331
@Override
327332
public void onFailure(Throwable error) {
328-
333+
// TODO: Handle failure here?
334+
Logger.d("failure unsubscribing");
329335
}
330336
});
331337
}

Voice Control For Plex/src/main/java/com/atomjack/vcfp/model/PlexClient.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public void writeToParcel(Parcel parcel, int i) {
3434
parcel.writeString(version);
3535
parcel.writeString(product);
3636
parcel.writeString(address);
37+
parcel.writeString(machineIdentifier);
3738
}
3839

3940
public PlexClient(Parcel in) {
@@ -42,6 +43,7 @@ public PlexClient(Parcel in) {
4243
version = in.readString();
4344
product = in.readString();
4445
address = in.readString();
46+
machineIdentifier = in.readString();
4547
}
4648

4749
public static final Parcelable.Creator<PlexClient> CREATOR = new Parcelable.Creator<PlexClient>() {

Voice Control For Plex/src/main/java/com/atomjack/vcfp/model/Timeline.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
public class Timeline {
88
@Attribute
99
public String state;
10-
@Attribute
10+
@Attribute(required=false)
1111
public String time;
1212
@Attribute
1313
public String type;
Lines changed: 29 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package us.nineworlds.serenity;
22

33
import java.util.ArrayList;
4+
import java.util.HashMap;
45
import java.util.regex.Matcher;
56
import java.util.regex.Pattern;
67

@@ -28,72 +29,30 @@ public void onReceive(Context context, Intent intent) {
2829
String ipAddress = intent.getStringExtra("ipaddress").substring(1);
2930

3031
Logger.d("message: %s", message);
31-
32-
33-
34-
Pattern p;
35-
Matcher matcher;
36-
37-
p = Pattern.compile( "Content-Type: ([^\r]+)", Pattern.DOTALL);
38-
matcher = p.matcher(message);
39-
matcher.find();
40-
String contentType = matcher.group(1);
41-
42-
p = Pattern.compile( "Name: ([^\r]+)", Pattern.DOTALL);
43-
matcher = p.matcher(message);
44-
matcher.find();
45-
String name = matcher.group(1);
46-
47-
p = Pattern.compile( "Port: ([^\r]+)", Pattern.DOTALL);
48-
matcher = p.matcher(message);
49-
matcher.find();
50-
String port = matcher.group(1);
51-
52-
p = Pattern.compile( "Protocol: ([^\r]+)", Pattern.DOTALL);
53-
matcher = p.matcher(message);
54-
String protocol = "";
55-
if(matcher.find())
56-
protocol = matcher.group(1);
57-
58-
p = Pattern.compile( "Version: ([0-9a-f-]+)", Pattern.DOTALL);
59-
matcher = p.matcher(message);
60-
String version = "";
61-
if(matcher.find())
62-
version = matcher.group(1);
63-
64-
65-
p = Pattern.compile( "Resource-Identifier: ([0-9a-f-]+)", Pattern.DOTALL);
66-
matcher = p.matcher(message);
67-
68-
69-
if(matcher.find()) {
70-
String machineIdentifier = matcher.group(1);
71-
72-
if(contentType.equals("plex/media-server")) {
32+
HashMap<String, String> responseMap = processResponse(message);
7333

34+
if(responseMap.get("resource-identifier") != null) {
35+
if(responseMap.get("content-type").equals("plex/media-server")) {
7436
PlexServer server = new PlexServer();
75-
server.port = port;
76-
server.name = name;
37+
server.port = responseMap.get("port");
38+
server.name = responseMap.get("name");
7739
server.address = ipAddress;
78-
server.machineIdentifier = machineIdentifier;
79-
server.version = version;
40+
server.machineIdentifier = responseMap.get("resource-identifier");
41+
server.version = responseMap.get("version");
8042
server.local = true;
8143
Connection connection = new Connection("http", server.address, server.port);
8244
server.connections = new ArrayList<Connection>();
8345
server.connections.add(connection);
8446
VoiceControlForPlexApplication.addPlexServer(server);
85-
} else if(contentType.equals("plex/media-player") && protocol.equals("plex")) {
47+
} else if(responseMap.get("content-type").equals("plex/media-player") && responseMap.get("protocol") != null && responseMap.get("protocol").equals("plex")) {
8648
PlexClient client = new PlexClient();
87-
client.port = port;
88-
client.name = name;
49+
client.port = responseMap.get("port");
50+
client.name = responseMap.get("name");
8951
client.address = ipAddress;
90-
client.machineIdentifier = machineIdentifier;
91-
client.version = version;
52+
client.machineIdentifier = responseMap.get("resource-identifier");
53+
client.version = responseMap.get("version");
9254

93-
p = Pattern.compile( "Product: ([^\r]+)", Pattern.DOTALL);
94-
matcher = p.matcher(message);
95-
if(matcher.find())
96-
client.product = matcher.group(1);
55+
client.product = responseMap.get("product");
9756
clients.add(client);
9857
}
9958
}
@@ -126,4 +85,19 @@ public void onReceive(Context context, Intent intent) {
12685
}
12786
}
12887
}
88+
89+
private HashMap<String, String> processResponse(String response) {
90+
HashMap<String, String> responseMap = new HashMap<String, String>();
91+
String[] lines = response.split("[\n\r]");
92+
93+
Pattern p = Pattern.compile("([^:]+): ([^\r^\n]+)");
94+
Matcher matcher;
95+
for(String line : lines) {
96+
matcher = p.matcher(line);
97+
if(matcher.find()) {
98+
responseMap.put(matcher.group(1).toLowerCase(), matcher.group(2));
99+
}
100+
}
101+
return responseMap;
102+
}
129103
}

Voice Control For Plex/src/main/res/values-fr/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,5 @@
9696
<string name="searching_for_plex_clients">Searching for Plex Clients</string>
9797
<string name="select_plex_client">Select a Plex Client</string>
9898
<string name="no_servers_found">No Plex Servers Found</string>
99+
<string name="no_clients_found">No Plex Clients Found</string>
99100
</resources>

0 commit comments

Comments
 (0)