3
3
import android .app .Service ;
4
4
import android .content .BroadcastReceiver ;
5
5
import android .content .Intent ;
6
+ import android .content .IntentFilter ;
6
7
import android .content .SharedPreferences ;
7
8
import android .os .IBinder ;
8
9
import android .speech .RecognizerIntent ;
10
+ import android .support .v4 .content .LocalBroadcastManager ;
9
11
10
12
import com .atomjack .vcfp .model .MediaContainer ;
11
13
import com .atomjack .vcfp .model .PlexClient ;
@@ -52,11 +54,13 @@ public class PlexSearch extends Service {
52
54
private List <PlexTrack > tracks = new ArrayList <PlexTrack >();
53
55
private List <PlexDirectory > albums = new ArrayList <PlexDirectory >();
54
56
57
+ // Callbacks for when we figure out what action the user wishes to take.
55
58
private myRunnable actionToDo ;
56
59
private interface myRunnable {
57
- boolean stop = false ;
58
60
void run ();
59
61
}
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 {}
60
64
61
65
@ Override
62
66
public int onStartCommand (Intent intent , int flags , int startId ) {
@@ -138,6 +142,13 @@ public void onCreate() {
138
142
queryText = null ;
139
143
mPrefs = getSharedPreferences (PREFS , MODE_PRIVATE );
140
144
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
+ }
141
152
}
142
153
143
154
@ Override
@@ -152,14 +163,6 @@ private void startup() {
152
163
videos = new ArrayList <PlexVideo >();
153
164
shows = new ArrayList <PlexDirectory >();
154
165
155
- /*
156
- actionToDo = handleVoiceSearch();
157
- if(actionToDo.stop) {
158
- actionToDo.run();
159
- return;
160
- }
161
- */
162
-
163
166
Gson gson = new Gson ();
164
167
PlexServer defaultServer = gson .fromJson (mPrefs .getString ("Server" , "" ), PlexServer .class );
165
168
if (specifiedServer != null && client != null && !specifiedServer .name .equals (getResources ().getString (R .string .scan_all ))) {
@@ -168,10 +171,6 @@ private void startup() {
168
171
plexmediaServers = new ConcurrentHashMap <String , PlexServer >();
169
172
plexmediaServers .put (specifiedServer .name , specifiedServer );
170
173
setClient ();
171
- // actionToDo = handleVoiceSearch();
172
- // actionToDo.run();
173
- // if(actionToDo.stop)
174
- // return;
175
174
} else if (specifiedServer == null && defaultServer != null && !defaultServer .name .equals (getResources ().getString (R .string .scan_all ))) {
176
175
// Use the server specified in the main settings
177
176
Logger .d ("Using server and client specified in main settings" );
@@ -181,6 +180,18 @@ private void startup() {
181
180
} else {
182
181
// Scan All was chosen
183
182
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
+
184
195
if (mServiceIntent == null ) {
185
196
mServiceIntent = new Intent (this , GDMService .class );
186
197
}
@@ -197,7 +208,7 @@ private void setClient() {
197
208
Matcher matcher = p .matcher (queryText );
198
209
if (!matcher .find ()) {
199
210
// 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 );
201
212
actionToDo = handleVoiceSearch ();
202
213
actionToDo .run ();
203
214
} else {
@@ -232,35 +243,44 @@ public void onFailure(Throwable error) {
232
243
}
233
244
234
245
private myRunnable handleVoiceSearch () {
246
+ return handleVoiceSearch (false );
247
+ }
248
+
249
+ private myRunnable handleVoiceSearch (boolean noChange ) {
235
250
Logger .d ("GOT QUERY: %s" , queryText );
236
251
237
252
resumePlayback = false ;
238
253
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 ;
241
256
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
+ }
253
273
}
254
274
}
255
- }
256
275
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
+ }
264
284
}
265
285
266
286
p = Pattern .compile ( getString (R .string .pattern_watch_movie ), Pattern .DOTALL );
@@ -413,8 +433,7 @@ public void run() {
413
433
p = Pattern .compile (getString (R .string .pattern_pause_playback ), Pattern .DOTALL );
414
434
matcher = p .matcher (queryText );
415
435
if (matcher .find ()) {
416
- return new myRunnable () {
417
- boolean stop = true ;
436
+ return new stopRunnable () {
418
437
@ Override
419
438
public void run () {
420
439
pausePlayback ();
@@ -426,8 +445,7 @@ public void run() {
426
445
matcher = p .matcher (queryText );
427
446
if (matcher .find ()) {
428
447
Logger .d ("resuming playback" );
429
- return new myRunnable () {
430
- boolean stop = true ;
448
+ return new stopRunnable () {
431
449
@ Override
432
450
public void run () {
433
451
resumePlayback ();
@@ -439,8 +457,7 @@ public void run() {
439
457
matcher = p .matcher (queryText );
440
458
if (matcher .find ()) {
441
459
Logger .d ("stopping playback" );
442
- return new myRunnable () {
443
- boolean stop = true ;
460
+ return new stopRunnable () {
444
461
@ Override
445
462
public void run () {
446
463
stopPlayback ();
@@ -472,8 +489,7 @@ else if(matcher.group(3).matches(getString(R.string.pattern_seconds)))
472
489
final int h = hours ;
473
490
final int m = minutes ;
474
491
final int s = seconds ;
475
- return new myRunnable () {
476
- boolean stop = true ;
492
+ return new stopRunnable () {
477
493
@ Override
478
494
public void run () {
479
495
seekTo (h , m , s );
0 commit comments