From 9bf6ffac7ae5dcd0283ab209be2da4ef6cedf702 Mon Sep 17 00:00:00 2001 From: amitnj <74272437+amitnj@users.noreply.github.com> Date: Thu, 26 Jan 2023 09:03:55 -0800 Subject: [PATCH] Add support for account login cluster for content apps. (#24628) * Add support for account login cluster for content apps. * Use JniUtfString. Co-authored-by: chrisdecenzo <61757564+chrisdecenzo@users.noreply.github.com> --- .../java/com/matter/tv/app/api/Clusters.java | 31 +++ .../contentapp/CommandResponseHolder.java | 60 +++++ .../com/example/contentapp/MainActivity.java | 27 +- .../receiver/MatterCommandReceiver.java | 4 +- .../src/main/res/layout/activity_main.xml | 115 +++++---- .../account-login/AccountLoginManager.cpp | 43 +++- .../account-login/AccountLoginManager.h | 21 +- examples/tv-app/android/java/AppImpl.cpp | 18 +- examples/tv-app/android/java/AppImpl.h | 29 ++- .../java/ContentAppCommandDelegate.cpp | 233 ++++++++++++++---- .../android/java/ContentAppCommandDelegate.h | 17 +- 11 files changed, 473 insertions(+), 125 deletions(-) create mode 100644 examples/tv-app/android/App/content-app/src/main/java/com/example/contentapp/CommandResponseHolder.java diff --git a/examples/tv-app/android/App/common-api/src/main/java/com/matter/tv/app/api/Clusters.java b/examples/tv-app/android/App/common-api/src/main/java/com/matter/tv/app/api/Clusters.java index 93cd816eb95984..d3fffe4e684873 100644 --- a/examples/tv-app/android/App/common-api/src/main/java/com/matter/tv/app/api/Clusters.java +++ b/examples/tv-app/android/App/common-api/src/main/java/com/matter/tv/app/api/Clusters.java @@ -5,7 +5,38 @@ * the rest of the media clusters TODO : Maybe generate using ZAP tool */ public class Clusters { + // Clusters + public static class AccountLogin { + public static final int Id = 0x050E; + + public static class Commands { + public static class GetSetupPIN { + public static final int ID = 0x00; + + public static class Fields { + public static final int TempAccountIdentifier = 0x00; + } + } + + public static class GetSetupPINResponse { + public static final int ID = 0x01; + + public static class Fields { + public static final int SetupPIN = 0x00; + } + } + + public static class Login { + public static final int ID = 0x02; + } + + public static class Logout { + public static final int ID = 0x03; + } + } + } + public static class MediaPlayback { public static final int Id = 0x0506; diff --git a/examples/tv-app/android/App/content-app/src/main/java/com/example/contentapp/CommandResponseHolder.java b/examples/tv-app/android/App/content-app/src/main/java/com/example/contentapp/CommandResponseHolder.java new file mode 100644 index 00000000000000..aaaeaa0cba7005 --- /dev/null +++ b/examples/tv-app/android/App/content-app/src/main/java/com/example/contentapp/CommandResponseHolder.java @@ -0,0 +1,60 @@ +package com.example.contentapp; + +import android.util.Log; +import com.matter.tv.app.api.Clusters; +import java.util.HashMap; +import java.util.Map; + +/** Class to hold attribute values to help test attribute read and subscribe use cases. */ +public class CommandResponseHolder { + private Map> responseValues = new HashMap<>(); + private static final String TAG = "CommandResponseHolder"; + private static final Long DEFAULT_COMMAND = -1L; + + private static CommandResponseHolder instance = new CommandResponseHolder(); + + private CommandResponseHolder() { + // Setting up responses + setResponseValue( + Clusters.ContentLauncher.Id, + DEFAULT_COMMAND, + "{\"0\":0, \"1\":\"custom response from content app for content launcher\"}"); + setResponseValue( + Clusters.TargetNavigator.Id, + DEFAULT_COMMAND, + "{\"0\":0, \"1\":\"custom response from content app for target navigator\"}"); + setResponseValue( + Clusters.MediaPlayback.Id, + DEFAULT_COMMAND, + "{\"0\":0, \"1\":\"custom response from content app for media playback\"}"); + setResponseValue( + Clusters.AccountLogin.Id, + Clusters.AccountLogin.Commands.GetSetupPIN.ID, + "{\"0\":\"12345678\"}"); + }; + + public static CommandResponseHolder getInstance() { + return instance; + } + + public void setResponseValue(long clusterId, long commandId, String value) { + if (value == null) { + Log.d(TAG, "Setting null for cluster " + clusterId + " command " + commandId); + } + Map responses = responseValues.get(clusterId); + if (responses == null) { + responses = new HashMap<>(); + responseValues.put(clusterId, responses); + } + responses.put(commandId, value); + } + + public String getCommandResponse(long clusterId, long commandId) { + Map responses = responseValues.get(clusterId); + String response = responses.get(commandId); + if (response == null) { + response = responses.get(DEFAULT_COMMAND); + } + return response; + } +} diff --git a/examples/tv-app/android/App/content-app/src/main/java/com/example/contentapp/MainActivity.java b/examples/tv-app/android/App/content-app/src/main/java/com/example/contentapp/MainActivity.java index 70011e726bda38..09731c67139a5f 100644 --- a/examples/tv-app/android/App/content-app/src/main/java/com/example/contentapp/MainActivity.java +++ b/examples/tv-app/android/App/content-app/src/main/java/com/example/contentapp/MainActivity.java @@ -4,6 +4,7 @@ import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.Button; +import android.widget.EditText; import android.widget.Spinner; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; @@ -27,6 +28,7 @@ public class MainActivity extends AppCompatActivity { private static final String ATTR_TL_LONG = "Target List : LONG"; private static final String ATTR_TL_SHORT = "Target List : SHORT"; private final ExecutorService executorService = Executors.newSingleThreadExecutor(); + private String setupPIN = ""; @Override protected void onCreate(Bundle savedInstanceState) { @@ -43,9 +45,30 @@ protected void onCreate(Bundle savedInstanceState) { TextView textView = (TextView) findViewById(R.id.commandTextView); textView.setText("Command Payload : " + command); - Button sendMessageButton = findViewById(R.id.sendMessageButton); + Button setupPINButton = findViewById(R.id.setupPINButton); + if (!setupPIN.isEmpty()) { + EditText pinText = findViewById(R.id.setupPINText); + pinText.setText(setupPIN); + } + setupPINButton.setOnClickListener( + view -> { + EditText pinText = findViewById(R.id.setupPINText); + String pinStr = pinText.getText().toString(); + setupPIN = pinStr; + CommandResponseHolder.getInstance() + .setResponseValue( + Clusters.AccountLogin.Id, + Clusters.AccountLogin.Commands.GetSetupPIN.ID, + "{\"" + + Clusters.AccountLogin.Commands.GetSetupPINResponse.Fields.SetupPIN + + "\":\"" + + pinStr + + "\"}"); + }); + + Button attributeUpdateButton = findViewById(R.id.updateAttributeButton); - sendMessageButton.setOnClickListener( + attributeUpdateButton.setOnClickListener( view -> { Spinner dropdown = findViewById(R.id.spinnerAttribute); String attribute = (String) dropdown.getSelectedItem(); diff --git a/examples/tv-app/android/App/content-app/src/main/java/com/example/contentapp/receiver/MatterCommandReceiver.java b/examples/tv-app/android/App/content-app/src/main/java/com/example/contentapp/receiver/MatterCommandReceiver.java index 2fda386654a5c1..1ff92853ec8648 100644 --- a/examples/tv-app/android/App/content-app/src/main/java/com/example/contentapp/receiver/MatterCommandReceiver.java +++ b/examples/tv-app/android/App/content-app/src/main/java/com/example/contentapp/receiver/MatterCommandReceiver.java @@ -6,6 +6,7 @@ import android.content.Intent; import android.util.Log; import com.example.contentapp.AttributeHolder; +import com.example.contentapp.CommandResponseHolder; import com.example.contentapp.MainActivity; import com.example.contentapp.matter.MatterAgentClient; import com.matter.tv.app.api.MatterIntentConstants; @@ -43,7 +44,8 @@ public void onReceive(Context context, Intent intent) { .append(command) .toString(); Log.d(TAG, message); - String response = "{\"0\":1, \"1\":\"custom response from content app\"}"; + String response = + CommandResponseHolder.getInstance().getCommandResponse(clusterId, commandId); Intent in = new Intent(context, MainActivity.class); in.putExtra(MatterIntentConstants.EXTRA_COMMAND_PAYLOAD, command); diff --git a/examples/tv-app/android/App/content-app/src/main/res/layout/activity_main.xml b/examples/tv-app/android/App/content-app/src/main/res/layout/activity_main.xml index dadcd97935481a..fe8b3003c94ca0 100644 --- a/examples/tv-app/android/App/content-app/src/main/res/layout/activity_main.xml +++ b/examples/tv-app/android/App/content-app/src/main/res/layout/activity_main.xml @@ -1,55 +1,82 @@ - - + android:gravity="left" + android:orientation="vertical" + android:padding="10sp"> - - - - - -