Skip to content

Commit

Permalink
TV Android Sample App: Dialogs for new commissioning feedback and mes…
Browse files Browse the repository at this point in the history
…sage cluster (project-chip#32281)

* Dialogs for new commissioning feedback and message cluster

* Address comments

* Restyle TV Android Sample App: Dialogs for new commissioning feedback and message cluster (project-chip#32282)

* Restyled by whitespace

* Restyled by google-java-format

---------

Co-authored-by: Restyled.io <[email protected]>

---------

Co-authored-by: restyled-io[bot] <32688539+restyled-io[bot]@users.noreply.github.com>
Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
3 people authored Feb 23, 2024
1 parent b7a781a commit 1d429ea
Show file tree
Hide file tree
Showing 10 changed files with 507 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import androidx.appcompat.app.AlertDialog;
import androidx.core.app.NotificationCompat;
import com.matter.tv.server.service.MatterServant;
import com.matter.tv.server.tvapp.Message;
import com.matter.tv.server.tvapp.UserPrompter;
import com.matter.tv.server.tvapp.UserPrompterResolver;

Expand All @@ -26,6 +27,7 @@ public class MatterCommissioningPrompter extends UserPrompterResolver implements
public MatterCommissioningPrompter(Context context) {
this.context = context;
this.createNotificationChannel();
setUserPrompter(this);
}

private Activity getActivity() {
Expand All @@ -34,7 +36,6 @@ private Activity getActivity() {

public void promptForCommissionOkPermission(
int vendorId, int productId, String commissioneeName) {
// TODO: find app by vendorId and productId
Log.d(
TAG,
"Received prompt for OK permission vendor id:"
Expand All @@ -43,28 +44,33 @@ public void promptForCommissionOkPermission(
+ productId
+ ". Commissionee: "
+ commissioneeName);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

builder
.setMessage(commissioneeName + " is requesting permission to cast to this device, approve?")
.setTitle("Allow access to " + commissioneeName)
.setPositiveButton(
"Ok",
(dialog, which) -> {
OnPromptAccepted();
})
.setNegativeButton(
"Cancel",
(dialog, which) -> {
OnPromptDeclined();
})
.create()
.show();

getActivity()
.runOnUiThread(
() -> {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder
.setMessage(
commissioneeName
+ " is requesting permission to cast to this device, approve?")
.setTitle("Allow access to " + commissioneeName)
.setPositiveButton(
"Ok",
(dialog, which) -> {
OnPromptAccepted();
})
.setNegativeButton(
"Cancel",
(dialog, which) -> {
OnPromptDeclined();
})
.create()
.show();
});
}

@Override
public void promptForCommissionPinCode(int vendorId, int productId, String commissioneeName) {
// TODO: find app by vendorId and productId
Log.d(
TAG,
"Received prompt for PIN code vendor id:"
Expand All @@ -73,26 +79,143 @@ public void promptForCommissionPinCode(int vendorId, int productId, String commi
+ productId
+ ". Commissionee: "
+ commissioneeName);
EditText editText = new EditText(getActivity());
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

builder
.setMessage("Please enter PIN displayed in casting app.")
.setTitle("Allow access to " + commissioneeName)
.setView(editText)
.setPositiveButton(
"Ok",
(dialog, which) -> {
String pinCode = editText.getText().toString();
OnPinCodeEntered(Integer.parseInt(pinCode));
})
.setNegativeButton(
"Cancel",
(dialog, which) -> {
OnPinCodeDeclined();
})
.create()
.show();

getActivity()
.runOnUiThread(
() -> {
EditText editText = new EditText(getActivity());
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

builder
.setMessage("Please enter PIN displayed in casting app.")
.setTitle("Allow access to " + commissioneeName)
.setView(editText)
.setPositiveButton(
"Ok",
(dialog, which) -> {
String pinCode = editText.getText().toString();
OnPinCodeEntered(Integer.parseInt(pinCode));
})
.setNegativeButton(
"Cancel",
(dialog, which) -> {
OnPinCodeDeclined();
})
.create()
.show();
});
}

public void hidePromptsOnCancel(int vendorId, int productId, String commissioneeName) {
Log.d(
TAG,
"Received Cancel from vendor id:"
+ vendorId
+ " productId:"
+ productId
+ ". Commissionee: "
+ commissioneeName);

getActivity()
.runOnUiThread(
() -> {
AlertDialog.Builder abuilder = new AlertDialog.Builder(getActivity());
abuilder
.setMessage("Cancelled connection to " + commissioneeName)
.setTitle("Connection Cancelled")
.create()
.show();

NotificationCompat.Builder builder =
new NotificationCompat.Builder(getActivity(), CHANNEL_ID)
.setSmallIcon(R.drawable.ic_baseline_check_24)
.setContentTitle("Connection Cancelled")
.setContentText("Cancelled connection to " + commissioneeName)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);

notificationManager.notify(SUCCESS_ID, builder.build());
});
}

public void promptWithCommissionerPasscode(
int vendorId,
int productId,
String commissioneeName,
long passcode,
int pairingHint,
String pairingInstruction) {
Log.d(
TAG,
"Received prompt for Commissioner Passcode:"
+ passcode
+ " vendor id:"
+ vendorId
+ " productId:"
+ productId
+ ". Commissionee: "
+ commissioneeName);

getActivity()
.runOnUiThread(
() -> {
EditText editText = new EditText(getActivity());
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

builder
.setMessage(
"Please enter "
+ passcode
+ " in "
+ commissioneeName
+ " app. "
+ pairingInstruction
+ " ["
+ pairingHint
+ "]")
.setTitle("Passcode" + passcode)
.setPositiveButton(
"Ok",
(dialog, which) -> {
OnCommissionerPasscodeOK();
})
.setNegativeButton(
"Cancel",
(dialog, which) -> {
OnCommissionerPasscodeCancel();
})
.create()
.show();
});
}

public void promptCommissioningStarted(int vendorId, int productId, String commissioneeName) {
Log.d(
TAG,
"Received prompt for started vendor id:"
+ vendorId
+ " productId:"
+ productId
+ ". Commissionee: "
+ commissioneeName);
getActivity()
.runOnUiThread(
() -> {
AlertDialog.Builder abuilder = new AlertDialog.Builder(getActivity());
abuilder
.setMessage("Starting connection to " + commissioneeName)
.setTitle("Connection Starting")
.create()
.show();

NotificationCompat.Builder builder =
new NotificationCompat.Builder(getActivity(), CHANNEL_ID)
.setSmallIcon(R.drawable.ic_baseline_check_24)
.setContentTitle("Connection Starting")
.setContentText("Starting connection to " + commissioneeName)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);

notificationManager.notify(SUCCESS_ID, builder.build());
});
}

public void promptCommissioningSucceeded(int vendorId, int productId, String commissioneeName) {
Expand All @@ -104,35 +227,105 @@ public void promptCommissioningSucceeded(int vendorId, int productId, String com
+ productId
+ ". Commissionee: "
+ commissioneeName);
NotificationCompat.Builder builder =
new NotificationCompat.Builder(getActivity(), CHANNEL_ID)
.setSmallIcon(R.drawable.ic_baseline_check_24)
.setContentTitle("Connection Complete")
.setContentText(
"Success. "
+ commissioneeName
+ " can now cast to this device. Visit settings to manage access control for casting.")
.setPriority(NotificationCompat.PRIORITY_DEFAULT);

notificationManager.notify(SUCCESS_ID, builder.build());
getActivity()
.runOnUiThread(
() -> {
AlertDialog.Builder abuilder = new AlertDialog.Builder(getActivity());
abuilder
.setMessage(
"Success. "
+ commissioneeName
+ " can now cast to this device. Visit settings to manage access control for casting.")
.setTitle("Connection Complete")
.create()
.show();

NotificationCompat.Builder builder =
new NotificationCompat.Builder(getActivity(), CHANNEL_ID)
.setSmallIcon(R.drawable.ic_baseline_check_24)
.setContentTitle("Connection Complete")
.setContentText(
"Success. "
+ commissioneeName
+ " can now cast to this device. Visit settings to manage access control for casting.")
.setPriority(NotificationCompat.PRIORITY_DEFAULT);

notificationManager.notify(SUCCESS_ID, builder.build());
});
}

public void promptCommissioningFailed(String commissioneeName, String error) {
Log.d(TAG, "Received prompt for failure Commissionee: " + commissioneeName);
NotificationCompat.Builder builder =
new NotificationCompat.Builder(getActivity(), CHANNEL_ID)
.setSmallIcon(R.drawable.ic_baseline_clear_24)
.setContentTitle("Connection Failed")
.setContentText("Failed. " + commissioneeName + " experienced error: " + error + ".")
.setPriority(NotificationCompat.PRIORITY_DEFAULT);

notificationManager.notify(FAIL_ID, builder.build());
getActivity()
.runOnUiThread(
() -> {
AlertDialog.Builder abuilder = new AlertDialog.Builder(getActivity());
abuilder
.setMessage("Failed. " + commissioneeName + " experienced error: " + error + ".")
.setTitle("Connection Failed")
.create()
.show();

NotificationCompat.Builder builder =
new NotificationCompat.Builder(getActivity(), CHANNEL_ID)
.setSmallIcon(R.drawable.ic_baseline_clear_24)
.setContentTitle("Connection Failed")
.setContentText(
"Failed. " + commissioneeName + " experienced error: " + error + ".")
.setPriority(NotificationCompat.PRIORITY_DEFAULT);

notificationManager.notify(FAIL_ID, builder.build());
});
}

public void promptWithMessage(Message message) {
Log.d(TAG, "Received message prompt for " + message.messageText);
getActivity()
.runOnUiThread(
() -> {
if (message.responseOptions.length != 2) {
AlertDialog.Builder abuilder = new AlertDialog.Builder(getActivity());
abuilder
.setMessage("" + message.messageId + ":" + message.messageText)
.setTitle("New Message from Test")
.setPositiveButton(
"Ok",
(dialog, which) -> {
OnMessageResponse(message.messageId, 0); // ack
})
.setNegativeButton(
"Ignore",
(dialog, which) -> {
OnMessageResponse(message.messageId, -1); // ignore
})
.create()
.show();
} else {
AlertDialog.Builder abuilder = new AlertDialog.Builder(getActivity());
abuilder
.setMessage("" + message.messageId + ":" + message.messageText)
.setTitle("New Message from Test")
.setPositiveButton(
message.responseOptions[0].label,
(dialog, which) -> {
OnMessageResponse(message.messageId, message.responseOptions[0].id);
})
.setNegativeButton(
message.responseOptions[1].label,
(dialog, which) -> {
OnMessageResponse(message.messageId, message.responseOptions[1].id);
})
.create()
.show();
}
});
}

private void createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Log.d(TAG, " ------------- createNotificationChannel");
CharSequence name = "MatterPromptNotificationChannel";
String description = "Matter Channel for sending notifications";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
Expand All @@ -142,6 +335,8 @@ private void createNotificationChannel() {
// or other notification behaviors after this
this.notificationManager = getSystemService(context, NotificationManager.class);
notificationManager.createNotificationChannel(channel);
} else {
Log.d(TAG, " ------------- NOT createNotificationChannel");
}
}
}
6 changes: 3 additions & 3 deletions examples/tv-app/android/java/MessagesManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,11 +382,11 @@ CHIP_ERROR MessagesManager::HandlePresentMessagesRequest(
return CHIP_ERROR_INTERNAL;
}

jobject jlong = env->NewObject(longClass, longCtor, response.messageResponseID.Value());
VerifyOrReturnError(jlong != nullptr, CHIP_ERROR_INCORRECT_STATE, ChipLogError(Zcl, "Could not create Long"));
jobject jlongobj = env->NewObject(longClass, longCtor, static_cast<uint64_t>(response.messageResponseID.Value()));
VerifyOrReturnError(jlongobj != nullptr, CHIP_ERROR_INCORRECT_STATE, ChipLogError(Zcl, "Could not create Long"));

// add to HashMap
env->CallObjectMethod(joptions, hashMapPut, jlong, jlabel);
env->CallObjectMethod(joptions, hashMapPut, jlongobj, jlabel);
if (env->ExceptionCheck())
{
ChipLogError(DeviceLayer, "Java exception in MessagesManager::HandlePresentMessagesRequest");
Expand Down
Loading

0 comments on commit 1d429ea

Please sign in to comment.