12
12
import androidx .appcompat .app .AlertDialog ;
13
13
import androidx .core .app .NotificationCompat ;
14
14
import com .matter .tv .server .service .MatterServant ;
15
+ import com .matter .tv .server .tvapp .Message ;
15
16
import com .matter .tv .server .tvapp .UserPrompter ;
16
17
import com .matter .tv .server .tvapp .UserPrompterResolver ;
17
18
@@ -26,6 +27,7 @@ public class MatterCommissioningPrompter extends UserPrompterResolver implements
26
27
public MatterCommissioningPrompter (Context context ) {
27
28
this .context = context ;
28
29
this .createNotificationChannel ();
30
+ setUserPrompter (this );
29
31
}
30
32
31
33
private Activity getActivity () {
@@ -34,7 +36,6 @@ private Activity getActivity() {
34
36
35
37
public void promptForCommissionOkPermission (
36
38
int vendorId , int productId , String commissioneeName ) {
37
- // TODO: find app by vendorId and productId
38
39
Log .d (
39
40
TAG ,
40
41
"Received prompt for OK permission vendor id:"
@@ -43,28 +44,33 @@ public void promptForCommissionOkPermission(
43
44
+ productId
44
45
+ ". Commissionee: "
45
46
+ commissioneeName );
46
- AlertDialog .Builder builder = new AlertDialog .Builder (getActivity ());
47
-
48
- builder
49
- .setMessage (commissioneeName + " is requesting permission to cast to this device, approve?" )
50
- .setTitle ("Allow access to " + commissioneeName )
51
- .setPositiveButton (
52
- "Ok" ,
53
- (dialog , which ) -> {
54
- OnPromptAccepted ();
55
- })
56
- .setNegativeButton (
57
- "Cancel" ,
58
- (dialog , which ) -> {
59
- OnPromptDeclined ();
60
- })
61
- .create ()
62
- .show ();
47
+
48
+ getActivity ()
49
+ .runOnUiThread (
50
+ () -> {
51
+ AlertDialog .Builder builder = new AlertDialog .Builder (getActivity ());
52
+ builder
53
+ .setMessage (
54
+ commissioneeName
55
+ + " is requesting permission to cast to this device, approve?" )
56
+ .setTitle ("Allow access to " + commissioneeName )
57
+ .setPositiveButton (
58
+ "Ok" ,
59
+ (dialog , which ) -> {
60
+ OnPromptAccepted ();
61
+ })
62
+ .setNegativeButton (
63
+ "Cancel" ,
64
+ (dialog , which ) -> {
65
+ OnPromptDeclined ();
66
+ })
67
+ .create ()
68
+ .show ();
69
+ });
63
70
}
64
71
65
72
@ Override
66
73
public void promptForCommissionPinCode (int vendorId , int productId , String commissioneeName ) {
67
- // TODO: find app by vendorId and productId
68
74
Log .d (
69
75
TAG ,
70
76
"Received prompt for PIN code vendor id:"
@@ -73,26 +79,143 @@ public void promptForCommissionPinCode(int vendorId, int productId, String commi
73
79
+ productId
74
80
+ ". Commissionee: "
75
81
+ commissioneeName );
76
- EditText editText = new EditText (getActivity ());
77
- AlertDialog .Builder builder = new AlertDialog .Builder (getActivity ());
78
-
79
- builder
80
- .setMessage ("Please enter PIN displayed in casting app." )
81
- .setTitle ("Allow access to " + commissioneeName )
82
- .setView (editText )
83
- .setPositiveButton (
84
- "Ok" ,
85
- (dialog , which ) -> {
86
- String pinCode = editText .getText ().toString ();
87
- OnPinCodeEntered (Integer .parseInt (pinCode ));
88
- })
89
- .setNegativeButton (
90
- "Cancel" ,
91
- (dialog , which ) -> {
92
- OnPinCodeDeclined ();
93
- })
94
- .create ()
95
- .show ();
82
+
83
+ getActivity ()
84
+ .runOnUiThread (
85
+ () -> {
86
+ EditText editText = new EditText (getActivity ());
87
+ AlertDialog .Builder builder = new AlertDialog .Builder (getActivity ());
88
+
89
+ builder
90
+ .setMessage ("Please enter PIN displayed in casting app." )
91
+ .setTitle ("Allow access to " + commissioneeName )
92
+ .setView (editText )
93
+ .setPositiveButton (
94
+ "Ok" ,
95
+ (dialog , which ) -> {
96
+ String pinCode = editText .getText ().toString ();
97
+ OnPinCodeEntered (Integer .parseInt (pinCode ));
98
+ })
99
+ .setNegativeButton (
100
+ "Cancel" ,
101
+ (dialog , which ) -> {
102
+ OnPinCodeDeclined ();
103
+ })
104
+ .create ()
105
+ .show ();
106
+ });
107
+ }
108
+
109
+ public void hidePromptsOnCancel (int vendorId , int productId , String commissioneeName ) {
110
+ Log .d (
111
+ TAG ,
112
+ "Received Cancel from vendor id:"
113
+ + vendorId
114
+ + " productId:"
115
+ + productId
116
+ + ". Commissionee: "
117
+ + commissioneeName );
118
+
119
+ getActivity ()
120
+ .runOnUiThread (
121
+ () -> {
122
+ AlertDialog .Builder abuilder = new AlertDialog .Builder (getActivity ());
123
+ abuilder
124
+ .setMessage ("Cancelled connection to " + commissioneeName )
125
+ .setTitle ("Connection Cancelled" )
126
+ .create ()
127
+ .show ();
128
+
129
+ NotificationCompat .Builder builder =
130
+ new NotificationCompat .Builder (getActivity (), CHANNEL_ID )
131
+ .setSmallIcon (R .drawable .ic_baseline_check_24 )
132
+ .setContentTitle ("Connection Cancelled" )
133
+ .setContentText ("Cancelled connection to " + commissioneeName )
134
+ .setPriority (NotificationCompat .PRIORITY_DEFAULT );
135
+
136
+ notificationManager .notify (SUCCESS_ID , builder .build ());
137
+ });
138
+ }
139
+
140
+ public void promptWithCommissionerPasscode (
141
+ int vendorId ,
142
+ int productId ,
143
+ String commissioneeName ,
144
+ long passcode ,
145
+ int pairingHint ,
146
+ String pairingInstruction ) {
147
+ Log .d (
148
+ TAG ,
149
+ "Received prompt for Commissioner Passcode:"
150
+ + passcode
151
+ + " vendor id:"
152
+ + vendorId
153
+ + " productId:"
154
+ + productId
155
+ + ". Commissionee: "
156
+ + commissioneeName );
157
+
158
+ getActivity ()
159
+ .runOnUiThread (
160
+ () -> {
161
+ EditText editText = new EditText (getActivity ());
162
+ AlertDialog .Builder builder = new AlertDialog .Builder (getActivity ());
163
+
164
+ builder
165
+ .setMessage (
166
+ "Please enter "
167
+ + passcode
168
+ + " in "
169
+ + commissioneeName
170
+ + " app. "
171
+ + pairingInstruction
172
+ + " ["
173
+ + pairingHint
174
+ + "]" )
175
+ .setTitle ("Passcode" + passcode )
176
+ .setPositiveButton (
177
+ "Ok" ,
178
+ (dialog , which ) -> {
179
+ OnCommissionerPasscodeOK ();
180
+ })
181
+ .setNegativeButton (
182
+ "Cancel" ,
183
+ (dialog , which ) -> {
184
+ OnCommissionerPasscodeCancel ();
185
+ })
186
+ .create ()
187
+ .show ();
188
+ });
189
+ }
190
+
191
+ public void promptCommissioningStarted (int vendorId , int productId , String commissioneeName ) {
192
+ Log .d (
193
+ TAG ,
194
+ "Received prompt for started vendor id:"
195
+ + vendorId
196
+ + " productId:"
197
+ + productId
198
+ + ". Commissionee: "
199
+ + commissioneeName );
200
+ getActivity ()
201
+ .runOnUiThread (
202
+ () -> {
203
+ AlertDialog .Builder abuilder = new AlertDialog .Builder (getActivity ());
204
+ abuilder
205
+ .setMessage ("Starting connection to " + commissioneeName )
206
+ .setTitle ("Connection Starting" )
207
+ .create ()
208
+ .show ();
209
+
210
+ NotificationCompat .Builder builder =
211
+ new NotificationCompat .Builder (getActivity (), CHANNEL_ID )
212
+ .setSmallIcon (R .drawable .ic_baseline_check_24 )
213
+ .setContentTitle ("Connection Starting" )
214
+ .setContentText ("Starting connection to " + commissioneeName )
215
+ .setPriority (NotificationCompat .PRIORITY_DEFAULT );
216
+
217
+ notificationManager .notify (SUCCESS_ID , builder .build ());
218
+ });
96
219
}
97
220
98
221
public void promptCommissioningSucceeded (int vendorId , int productId , String commissioneeName ) {
@@ -104,35 +227,105 @@ public void promptCommissioningSucceeded(int vendorId, int productId, String com
104
227
+ productId
105
228
+ ". Commissionee: "
106
229
+ commissioneeName );
107
- NotificationCompat .Builder builder =
108
- new NotificationCompat .Builder (getActivity (), CHANNEL_ID )
109
- .setSmallIcon (R .drawable .ic_baseline_check_24 )
110
- .setContentTitle ("Connection Complete" )
111
- .setContentText (
112
- "Success. "
113
- + commissioneeName
114
- + " can now cast to this device. Visit settings to manage access control for casting." )
115
- .setPriority (NotificationCompat .PRIORITY_DEFAULT );
116
-
117
- notificationManager .notify (SUCCESS_ID , builder .build ());
230
+ getActivity ()
231
+ .runOnUiThread (
232
+ () -> {
233
+ AlertDialog .Builder abuilder = new AlertDialog .Builder (getActivity ());
234
+ abuilder
235
+ .setMessage (
236
+ "Success. "
237
+ + commissioneeName
238
+ + " can now cast to this device. Visit settings to manage access control for casting." )
239
+ .setTitle ("Connection Complete" )
240
+ .create ()
241
+ .show ();
242
+
243
+ NotificationCompat .Builder builder =
244
+ new NotificationCompat .Builder (getActivity (), CHANNEL_ID )
245
+ .setSmallIcon (R .drawable .ic_baseline_check_24 )
246
+ .setContentTitle ("Connection Complete" )
247
+ .setContentText (
248
+ "Success. "
249
+ + commissioneeName
250
+ + " can now cast to this device. Visit settings to manage access control for casting." )
251
+ .setPriority (NotificationCompat .PRIORITY_DEFAULT );
252
+
253
+ notificationManager .notify (SUCCESS_ID , builder .build ());
254
+ });
118
255
}
119
256
120
257
public void promptCommissioningFailed (String commissioneeName , String error ) {
121
258
Log .d (TAG , "Received prompt for failure Commissionee: " + commissioneeName );
122
- NotificationCompat .Builder builder =
123
- new NotificationCompat .Builder (getActivity (), CHANNEL_ID )
124
- .setSmallIcon (R .drawable .ic_baseline_clear_24 )
125
- .setContentTitle ("Connection Failed" )
126
- .setContentText ("Failed. " + commissioneeName + " experienced error: " + error + "." )
127
- .setPriority (NotificationCompat .PRIORITY_DEFAULT );
128
-
129
- notificationManager .notify (FAIL_ID , builder .build ());
259
+ getActivity ()
260
+ .runOnUiThread (
261
+ () -> {
262
+ AlertDialog .Builder abuilder = new AlertDialog .Builder (getActivity ());
263
+ abuilder
264
+ .setMessage ("Failed. " + commissioneeName + " experienced error: " + error + "." )
265
+ .setTitle ("Connection Failed" )
266
+ .create ()
267
+ .show ();
268
+
269
+ NotificationCompat .Builder builder =
270
+ new NotificationCompat .Builder (getActivity (), CHANNEL_ID )
271
+ .setSmallIcon (R .drawable .ic_baseline_clear_24 )
272
+ .setContentTitle ("Connection Failed" )
273
+ .setContentText (
274
+ "Failed. " + commissioneeName + " experienced error: " + error + "." )
275
+ .setPriority (NotificationCompat .PRIORITY_DEFAULT );
276
+
277
+ notificationManager .notify (FAIL_ID , builder .build ());
278
+ });
279
+ }
280
+
281
+ public void promptWithMessage (Message message ) {
282
+ Log .d (TAG , "Received message prompt for " + message .messageText );
283
+ getActivity ()
284
+ .runOnUiThread (
285
+ () -> {
286
+ if (message .responseOptions .length != 2 ) {
287
+ AlertDialog .Builder abuilder = new AlertDialog .Builder (getActivity ());
288
+ abuilder
289
+ .setMessage ("" + message .messageId + ":" + message .messageText )
290
+ .setTitle ("New Message from Test" )
291
+ .setPositiveButton (
292
+ "Ok" ,
293
+ (dialog , which ) -> {
294
+ OnMessageResponse (message .messageId , 0 ); // ack
295
+ })
296
+ .setNegativeButton (
297
+ "Ignore" ,
298
+ (dialog , which ) -> {
299
+ OnMessageResponse (message .messageId , -1 ); // ignore
300
+ })
301
+ .create ()
302
+ .show ();
303
+ } else {
304
+ AlertDialog .Builder abuilder = new AlertDialog .Builder (getActivity ());
305
+ abuilder
306
+ .setMessage ("" + message .messageId + ":" + message .messageText )
307
+ .setTitle ("New Message from Test" )
308
+ .setPositiveButton (
309
+ message .responseOptions [0 ].label ,
310
+ (dialog , which ) -> {
311
+ OnMessageResponse (message .messageId , message .responseOptions [0 ].id );
312
+ })
313
+ .setNegativeButton (
314
+ message .responseOptions [1 ].label ,
315
+ (dialog , which ) -> {
316
+ OnMessageResponse (message .messageId , message .responseOptions [1 ].id );
317
+ })
318
+ .create ()
319
+ .show ();
320
+ }
321
+ });
130
322
}
131
323
132
324
private void createNotificationChannel () {
133
325
// Create the NotificationChannel, but only on API 26+ because
134
326
// the NotificationChannel class is new and not in the support library
135
327
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
328
+ Log .d (TAG , " ------------- createNotificationChannel" );
136
329
CharSequence name = "MatterPromptNotificationChannel" ;
137
330
String description = "Matter Channel for sending notifications" ;
138
331
int importance = NotificationManager .IMPORTANCE_DEFAULT ;
@@ -142,6 +335,8 @@ private void createNotificationChannel() {
142
335
// or other notification behaviors after this
143
336
this .notificationManager = getSystemService (context , NotificationManager .class );
144
337
notificationManager .createNotificationChannel (channel );
338
+ } else {
339
+ Log .d (TAG , " ------------- NOT createNotificationChannel" );
145
340
}
146
341
}
147
342
}
0 commit comments