38
38
import java .util .ArrayList ;
39
39
import java .util .List ;
40
40
import java .util .Optional ;
41
- import java .util .concurrent .Executors ;
42
- import java .util .concurrent .ScheduledExecutorService ;
43
- import java .util .concurrent .ScheduledFuture ;
44
41
45
42
public class DiscoveryExampleFragment extends Fragment {
46
43
private static final String TAG = DiscoveryExampleFragment .class .getSimpleName ();
47
44
// 35 represents device type of Matter Casting Player
48
45
private static final Long DISCOVERY_TARGET_DEVICE_TYPE = 35L ;
49
46
private static final int DISCOVERY_RUNTIME_SEC = 15 ;
50
47
private TextView matterDiscoveryMessageTextView ;
51
- private static final ScheduledExecutorService executorService =
52
- Executors .newSingleThreadScheduledExecutor ();
53
- private ScheduledFuture scheduledFutureTask ;
48
+ private TextView matterDiscoveryErrorMessageTextView ;
54
49
private static final List <CastingPlayer > castingPlayerList = new ArrayList <>();
55
50
private static ArrayAdapter <CastingPlayer > arrayAdapter ;
56
51
@@ -164,15 +159,17 @@ public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
164
159
matterDiscoveryMessageTextView .setText (
165
160
getString (R .string .matter_discovery_message_initializing_text ));
166
161
162
+ matterDiscoveryErrorMessageTextView =
163
+ getActivity ().findViewById (R .id .matterDiscoveryErrorTextView );
164
+ matterDiscoveryErrorMessageTextView .setText (
165
+ getString (R .string .matter_discovery_error_message_initial ));
166
+
167
167
arrayAdapter = new CastingPlayerArrayAdapter (getActivity (), castingPlayerList );
168
168
final ListView list = getActivity ().findViewById (R .id .castingPlayerList );
169
169
list .setAdapter (arrayAdapter );
170
170
171
171
Log .d (TAG , "onViewCreated() creating callbacks" );
172
172
173
- // TODO: In following PRs. Enable startDiscoveryButton and stopDiscoveryButton when
174
- // stopDiscovery is implemented in the core Matter SDK DNS-SD API. Enable in
175
- // fragment_matter_discovery_example.xml
176
173
Button startDiscoveryButton = getView ().findViewById (R .id .startDiscoveryButton );
177
174
startDiscoveryButton .setOnClickListener (
178
175
v -> {
@@ -188,8 +185,6 @@ public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
188
185
v -> {
189
186
Log .i (TAG , "onViewCreated() stopDiscoveryButton button clicked. Calling stopDiscovery()" );
190
187
stopDiscovery ();
191
- Log .i (TAG , "onViewCreated() stopDiscoveryButton button clicked. Canceling future task" );
192
- scheduledFutureTask .cancel (true );
193
188
});
194
189
195
190
Button clearDiscoveryResultsButton = getView ().findViewById (R .id .clearDiscoveryResultsButton );
@@ -198,6 +193,8 @@ public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
198
193
Log .i (
199
194
TAG , "onViewCreated() clearDiscoveryResultsButton button clicked. Clearing results" );
200
195
arrayAdapter .clear ();
196
+ matterDiscoveryErrorMessageTextView .setText (
197
+ getString (R .string .matter_discovery_error_message_initial ));
201
198
});
202
199
}
203
200
@@ -219,11 +216,7 @@ public void onResume() {
219
216
public void onPause () {
220
217
super .onPause ();
221
218
Log .i (TAG , "onPause() called" );
222
- // stopDiscovery();
223
- // Don't crash the app
224
- if (scheduledFutureTask != null ) {
225
- scheduledFutureTask .cancel (true );
226
- }
219
+ stopDiscovery ();
227
220
}
228
221
229
222
/** Interface for notifying the host. */
@@ -235,6 +228,8 @@ public interface Callback {
235
228
236
229
private boolean startDiscovery () {
237
230
Log .i (TAG , "startDiscovery() called" );
231
+ matterDiscoveryErrorMessageTextView .setText (
232
+ getString (R .string .matter_discovery_error_message_initial ));
238
233
239
234
arrayAdapter .clear ();
240
235
@@ -244,13 +239,17 @@ private boolean startDiscovery() {
244
239
matterCastingPlayerDiscovery .addCastingPlayerChangeListener (castingPlayerChangeListener );
245
240
if (err .hasError ()) {
246
241
Log .e (TAG , "startDiscovery() addCastingPlayerChangeListener() called, err Add: " + err );
242
+ matterDiscoveryErrorMessageTextView .setText (
243
+ getString (R .string .matter_discovery_error_message_stop_before_starting ) + err );
247
244
return false ;
248
245
}
249
246
// Start discovery
250
247
Log .i (TAG , "startDiscovery() calling CastingPlayerDiscovery.startDiscovery()" );
251
248
err = matterCastingPlayerDiscovery .startDiscovery (DISCOVERY_TARGET_DEVICE_TYPE );
252
249
if (err .hasError ()) {
253
250
Log .e (TAG , "startDiscovery() startDiscovery() called, err Start: " + err );
251
+ matterDiscoveryErrorMessageTextView .setText (
252
+ getString (R .string .matter_discovery_error_message_start_error ) + err );
254
253
return false ;
255
254
}
256
255
@@ -259,37 +258,23 @@ private boolean startDiscovery() {
259
258
matterDiscoveryMessageTextView .setText (
260
259
getString (R .string .matter_discovery_message_discovering_text ));
261
260
262
- // TODO: In following PRs. Enable this to auto-stop discovery after stopDiscovery is
263
- // implemented in the core Matter SDK DNS-SD API.
264
- // Schedule a service to stop discovery and remove the CastingPlayerChangeListener
265
- // Safe to call if discovery is not running
266
- // scheduledFutureTask =
267
- // executorService.schedule(
268
- // () -> {
269
- // Log.i(
270
- // TAG,
271
- // "startDiscovery() executorService "
272
- // + DISCOVERY_RUNTIME_SEC
273
- // + " seconds timer expired. Auto-calling stopDiscovery()");
274
- // stopDiscovery();
275
- // },
276
- // DISCOVERY_RUNTIME_SEC,
277
- // TimeUnit.SECONDS);
278
-
279
261
return true ;
280
262
}
281
263
282
264
private void stopDiscovery () {
283
265
Log .i (TAG , "stopDiscovery() called" );
266
+ matterDiscoveryErrorMessageTextView .setText (
267
+ getString (R .string .matter_discovery_error_message_initial ));
284
268
285
269
// Stop discovery
286
270
MatterError err = matterCastingPlayerDiscovery .stopDiscovery ();
287
271
if (err .hasError ()) {
288
272
Log .e (
289
273
TAG ,
290
274
"stopDiscovery() MatterCastingPlayerDiscovery.stopDiscovery() called, err Stop: " + err );
275
+ matterDiscoveryErrorMessageTextView .setText (
276
+ getString (R .string .matter_discovery_error_message_stop_error ) + err );
291
277
} else {
292
- // TODO: In following PRs. Implement stop discovery in the Android core API.
293
278
Log .d (TAG , "stopDiscovery() MatterCastingPlayerDiscovery.stopDiscovery() success" );
294
279
}
295
280
@@ -309,6 +294,8 @@ private void stopDiscovery() {
309
294
TAG ,
310
295
"stopDiscovery() matterCastingPlayerDiscovery.removeCastingPlayerChangeListener() called, err Remove: "
311
296
+ err );
297
+ matterDiscoveryErrorMessageTextView .setText (
298
+ getString (R .string .matter_discovery_error_message_stop_error ) + err );
312
299
}
313
300
}
314
301
}
0 commit comments