@@ -54,7 +54,9 @@ public class ConnectionExampleFragment extends Fragment {
54
54
private final CastingPlayer targetCastingPlayer ;
55
55
private final boolean useCommissionerGeneratedPasscode ;
56
56
private TextView connectionFragmentStatusTextView ;
57
+ private TextView commissionerDeclarationErrorTextView ;
57
58
private Button connectionFragmentNextButton ;
59
+ private AlertDialog passcodeDialog ;
58
60
59
61
public ConnectionExampleFragment (
60
62
CastingPlayer targetCastingPlayer , boolean useCommissionerGeneratedPasscode ) {
@@ -100,6 +102,8 @@ public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
100
102
Log .i (TAG , "onViewCreated()" );
101
103
102
104
connectionFragmentStatusTextView = getView ().findViewById (R .id .connectionFragmentStatusText );
105
+ commissionerDeclarationErrorTextView =
106
+ getView ().findViewById (R .id .commissionerDeclarationErrorTextView );
103
107
if (useCommissionerGeneratedPasscode ) {
104
108
connectionFragmentStatusTextView .setText (
105
109
"Verifying or establishing connection with Casting Player with device name: "
@@ -190,32 +194,52 @@ public void handle(MatterError err) {
190
194
},
191
195
null );
192
196
193
- // CommissionerDeclaration is only needed for the CastingPlayer/Commissioner-Generated
194
- // passcode commissioning flow.
195
- if (useCommissionerGeneratedPasscode ) {
196
- connectionCallbacks .onCommissionerDeclaration =
197
- new MatterCallback <CommissionerDeclaration >() {
198
- @ Override
199
- public void handle (CommissionerDeclaration cd ) {
200
- Log .i (TAG , "CastingPlayer CommissionerDeclaration message received: " );
201
- cd .logDetail ();
202
-
203
- getActivity ()
204
- .runOnUiThread (
205
- () -> {
206
- connectionFragmentStatusTextView .setText (
207
- "CommissionerDeclaration message received from Casting Player: \n \n " );
208
- if (cd .getCommissionerPasscode ()) {
209
-
210
- displayPasscodeInputDialog (getActivity ());
197
+ // The CommissionerDeclaration callback is optional and only needed for the
198
+ // CastingPlayer/Commissioner-Generated
199
+ // passcode commissioning flow. However, if we want to know when the
200
+ // CastingPlayer/Commissioner user
201
+ // has cancelled the connection attempt when using the Client/Commissionee generated
202
+ // passcode flow, then we
203
+ // need to implement this callback.
204
+ connectionCallbacks .onCommissionerDeclaration =
205
+ new MatterCallback <CommissionerDeclaration >() {
206
+ @ Override
207
+ public void handle (CommissionerDeclaration cd ) {
208
+ Log .i (TAG , "CastingPlayer CommissionerDeclaration message received: " );
209
+ cd .logDetail ();
210
+
211
+ getActivity ()
212
+ .runOnUiThread (
213
+ () -> {
214
+ connectionFragmentStatusTextView .setText (
215
+ "CommissionerDeclaration message received from Casting Player: \n \n " );
216
+ if (cd .getCommissionerPasscode ()) {
217
+
218
+ displayPasscodeInputDialog (getActivity ());
211
219
220
+ connectionFragmentStatusTextView .setText (
221
+ "CommissionerDeclaration message received from Casting Player: A passcode is now displayed for the user by the Casting Player. \n \n " );
222
+ }
223
+ if (cd .getCancelPasscode ()) {
224
+ if (useCommissionerGeneratedPasscode ) {
212
225
connectionFragmentStatusTextView .setText (
213
- "CommissionerDeclaration message received from Casting Player: A passcode is now displayed for the user by the Casting Player. \n \n " );
226
+ "CastingPlayer/Commissioner-Generated passcode connection attempt cancelled by the CastingPlayer/Commissioner user. \n \n Route back to exit. \n \n " );
227
+ } else {
228
+ connectionFragmentStatusTextView .setText (
229
+ "Connection attempt cancelled by the CastingPlayer/Commissioner user. \n \n Route back to exit. \n \n " );
214
230
}
215
- });
216
- }
217
- };
218
- }
231
+ if (passcodeDialog != null && passcodeDialog .isShowing ()) {
232
+ passcodeDialog .dismiss ();
233
+ }
234
+ }
235
+ if (cd .getErrorCode () != CommissionerDeclaration .CdError .noError ) {
236
+ commissionerDeclarationErrorTextView .setText (
237
+ "CastingPlayer/Commissioner Error: "
238
+ + cd .getErrorCode ().toString ());
239
+ }
240
+ });
241
+ }
242
+ };
219
243
220
244
MatterError err =
221
245
targetCastingPlayer .verifyOrEstablishConnection (
@@ -331,7 +355,7 @@ public void onClick(DialogInterface dialog, int which) {
331
355
TAG ,
332
356
"displayPasscodeInputDialog() user cancelled the CastingPlayer/Commissioner-Generated Passcode input dialog. Calling stopConnecting()" );
333
357
connectionFragmentStatusTextView .setText (
334
- "Connection attempt with Casting Player cancelled by the user, route back to exit. \n \n " );
358
+ "Connection attempt with Casting Player cancelled by the Casting Client/Commissionee user. \n \n Route back to exit. \n \n " );
335
359
MatterError err = targetCastingPlayer .stopConnecting ();
336
360
if (err .hasError ()) {
337
361
MatterError finalErr = err ;
@@ -348,9 +372,9 @@ public void onClick(DialogInterface dialog, int which) {
348
372
});
349
373
350
374
builder .setView (dialogView );
351
- AlertDialog alertDialog = builder .create ();
352
- alertDialog .show ();
353
- alertDialog
375
+ passcodeDialog = builder .create (); // Store the dialog instance in the passcodeDialog variable
376
+ passcodeDialog .show ();
377
+ passcodeDialog
354
378
.getWindow ()
355
379
.setLayout (ViewGroup .LayoutParams .MATCH_PARENT , ViewGroup .LayoutParams .WRAP_CONTENT );
356
380
}
0 commit comments