Skip to content

Commit 286e542

Browse files
author
Chris Bellew
committed
Fixed Scan All once and for all!
1 parent 7b6a82a commit 286e542

File tree

1 file changed

+59
-43
lines changed

1 file changed

+59
-43
lines changed

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

Lines changed: 59 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import android.app.Service;
44
import android.content.BroadcastReceiver;
55
import android.content.Intent;
6+
import android.content.IntentFilter;
67
import android.content.SharedPreferences;
78
import android.os.IBinder;
89
import android.speech.RecognizerIntent;
10+
import android.support.v4.content.LocalBroadcastManager;
911

1012
import com.atomjack.vcfp.model.MediaContainer;
1113
import com.atomjack.vcfp.model.PlexClient;
@@ -52,11 +54,13 @@ public class PlexSearch extends Service {
5254
private List<PlexTrack> tracks = new ArrayList<PlexTrack>();
5355
private List<PlexDirectory> albums = new ArrayList<PlexDirectory>();
5456

57+
// Callbacks for when we figure out what action the user wishes to take.
5558
private myRunnable actionToDo;
5659
private interface myRunnable {
57-
boolean stop = false;
5860
void run();
5961
}
62+
// An instance of this interface will be returned by handleVoiceSearch when no server discovery is needed (e.g. pause/resume/stop playback or offset)
63+
private interface stopRunnable extends myRunnable {}
6064

6165
@Override
6266
public int onStartCommand(Intent intent, int flags, int startId) {
@@ -138,6 +142,13 @@ public void onCreate() {
138142
queryText = null;
139143
mPrefs = getSharedPreferences(PREFS, MODE_PRIVATE);
140144
feedback = new Feedback(mPrefs, this);
145+
if(gdmReceiver != null) {
146+
IntentFilter filters = new IntentFilter();
147+
filters.addAction(GDMService.MSG_RECEIVED);
148+
filters.addAction(GDMService.SOCKET_CLOSED);
149+
LocalBroadcastManager.getInstance(this).registerReceiver(gdmReceiver,
150+
filters);
151+
}
141152
}
142153

143154
@Override
@@ -152,14 +163,6 @@ private void startup() {
152163
videos = new ArrayList<PlexVideo>();
153164
shows = new ArrayList<PlexDirectory>();
154165

155-
/*
156-
actionToDo = handleVoiceSearch();
157-
if(actionToDo.stop) {
158-
actionToDo.run();
159-
return;
160-
}
161-
*/
162-
163166
Gson gson = new Gson();
164167
PlexServer defaultServer = gson.fromJson(mPrefs.getString("Server", ""), PlexServer.class);
165168
if(specifiedServer != null && client != null && !specifiedServer.name.equals(getResources().getString(R.string.scan_all))) {
@@ -168,10 +171,6 @@ private void startup() {
168171
plexmediaServers = new ConcurrentHashMap<String, PlexServer>();
169172
plexmediaServers.put(specifiedServer.name, specifiedServer);
170173
setClient();
171-
// actionToDo = handleVoiceSearch();
172-
// actionToDo.run();
173-
// if(actionToDo.stop)
174-
// return;
175174
} else if(specifiedServer == null && defaultServer != null && !defaultServer.name.equals(getResources().getString(R.string.scan_all))) {
176175
// Use the server specified in the main settings
177176
Logger.d("Using server and client specified in main settings");
@@ -181,6 +180,18 @@ private void startup() {
181180
} else {
182181
// Scan All was chosen
183182
Logger.d("Scan all was chosen");
183+
184+
// First, see if what needs to be done actually needs to know about the server (i.e. pause/stop/resume playback of offset).
185+
// If it does, execute the action and return as we don't need to do anything else. However, also check to see if the user
186+
// has specified a client (using " on <client name>") - if this is the case, we will need to find that client via server
187+
// discovery
188+
myRunnable actionToDo = handleVoiceSearch(true);
189+
if(actionToDo instanceof stopRunnable && !queryText.matches(getString(R.string.pattern_on_client))) {
190+
actionToDo.run();
191+
return;
192+
}
193+
194+
184195
if(mServiceIntent == null) {
185196
mServiceIntent = new Intent(this, GDMService.class);
186197
}
@@ -197,7 +208,7 @@ private void setClient() {
197208
Matcher matcher = p.matcher(queryText);
198209
if(!matcher.find()) {
199210
// Client not specified, so use default
200-
Logger.d("Using default client since none specified in query");
211+
Logger.d("Using default client since none specified in query: %s", client.name);
201212
actionToDo = handleVoiceSearch();
202213
actionToDo.run();
203214
} else {
@@ -232,35 +243,44 @@ public void onFailure(Throwable error) {
232243
}
233244

234245
private myRunnable handleVoiceSearch() {
246+
return handleVoiceSearch(false);
247+
}
248+
249+
private myRunnable handleVoiceSearch(boolean noChange) {
235250
Logger.d("GOT QUERY: %s", queryText);
236251

237252
resumePlayback = false;
238253

239-
Pattern p = Pattern.compile(getString(R.string.pattern_on_client), Pattern.DOTALL);
240-
Matcher matcher = p.matcher(queryText);
254+
Pattern p;
255+
Matcher matcher;
241256

242-
if(matcher.find()) {
243-
String specifiedClient = matcher.group(2).toLowerCase();
244-
245-
Logger.d("Clients: %d", clients.size());
246-
Logger.d("Specified client: %s", specifiedClient);
247-
for(int i=0;i<clients.size();i++) {
248-
if(clients.get(i).name.toLowerCase().equals(specifiedClient)) {
249-
client = clients.get(i);
250-
queryText = queryText.replaceAll(getString(R.string.pattern_on_client), "$1");
251-
Logger.d("query text now %s", queryText);
252-
break;
257+
if(!noChange) {
258+
p = Pattern.compile(getString(R.string.pattern_on_client), Pattern.DOTALL);
259+
matcher = p.matcher(queryText);
260+
261+
if (matcher.find()) {
262+
String specifiedClient = matcher.group(2).toLowerCase();
263+
264+
Logger.d("Clients: %d", clients.size());
265+
Logger.d("Specified client: %s", specifiedClient);
266+
for (int i = 0; i < clients.size(); i++) {
267+
if (clients.get(i).name.toLowerCase().equals(specifiedClient)) {
268+
client = clients.get(i);
269+
queryText = queryText.replaceAll(getString(R.string.pattern_on_client), "$1");
270+
Logger.d("query text now %s", queryText);
271+
break;
272+
}
253273
}
254274
}
255-
}
256275

257-
// Check for a sentence starting with "resume watching"
258-
p = Pattern.compile(getString(R.string.pattern_resume_watching));
259-
matcher = p.matcher(queryText);
260-
if(matcher.find()) {
261-
resumePlayback = true;
262-
// Replace "resume watching" with just "watch" so the pattern matching below works
263-
queryText = matcher.replaceAll(getString(R.string.pattern_watch));
276+
// Check for a sentence starting with "resume watching"
277+
p = Pattern.compile(getString(R.string.pattern_resume_watching));
278+
matcher = p.matcher(queryText);
279+
if(matcher.find()) {
280+
resumePlayback = true;
281+
// Replace "resume watching" with just "watch" so the pattern matching below works
282+
queryText = matcher.replaceAll(getString(R.string.pattern_watch));
283+
}
264284
}
265285

266286
p = Pattern.compile( getString(R.string.pattern_watch_movie), Pattern.DOTALL);
@@ -413,8 +433,7 @@ public void run() {
413433
p = Pattern.compile(getString(R.string.pattern_pause_playback), Pattern.DOTALL);
414434
matcher = p.matcher(queryText);
415435
if (matcher.find()) {
416-
return new myRunnable() {
417-
boolean stop = true;
436+
return new stopRunnable() {
418437
@Override
419438
public void run() {
420439
pausePlayback();
@@ -426,8 +445,7 @@ public void run() {
426445
matcher = p.matcher(queryText);
427446
if (matcher.find()) {
428447
Logger.d("resuming playback");
429-
return new myRunnable() {
430-
boolean stop = true;
448+
return new stopRunnable() {
431449
@Override
432450
public void run() {
433451
resumePlayback();
@@ -439,8 +457,7 @@ public void run() {
439457
matcher = p.matcher(queryText);
440458
if (matcher.find()) {
441459
Logger.d("stopping playback");
442-
return new myRunnable() {
443-
boolean stop = true;
460+
return new stopRunnable() {
444461
@Override
445462
public void run() {
446463
stopPlayback();
@@ -472,8 +489,7 @@ else if(matcher.group(3).matches(getString(R.string.pattern_seconds)))
472489
final int h = hours;
473490
final int m = minutes;
474491
final int s = seconds;
475-
return new myRunnable() {
476-
boolean stop = true;
492+
return new stopRunnable() {
477493
@Override
478494
public void run() {
479495
seekTo(h, m, s);

0 commit comments

Comments
 (0)