Skip to content

Commit

Permalink
Implement sending message from the linux layer of the android tv-app:…
Browse files Browse the repository at this point in the history
…:platform-app to the tv-app::content-app (#18696)

* Implement sending message from the linux layer of the android tv-app::platform-app to the tv-app::content-app.
Resolving conflicts

* Review fixes. Used more appropriate datatype wrappers. introduced null checks for hardcoded sample apps.

* Squashed commit of the following:

commit bde9a1b
Author: Restyled.io <[email protected]>
Date:   Fri May 20 22:25:44 2022 +0000

    Restyled by clang-format

commit 27c0f3d
Author: Restyled.io <[email protected]>
Date:   Fri May 20 22:25:33 2022 +0000

    Restyled by google-java-format

commit 56bb8b4
Author: Restyled.io <[email protected]>
Date:   Fri May 20 22:25:15 2022 +0000

    Restyled by whitespace

commit 22ba274
Author: Amit Jain <[email protected]>
Date:   Fri May 20 15:15:45 2022 -0700

    Implement sending message from the linux layer of the android tv-app::platform-app to the tv-app::content-app.

* More restyling fixes.

* One more restyle commit.
  • Loading branch information
amitnj authored and pull[bot] committed Nov 14, 2023
1 parent 72d4000 commit 1017480
Show file tree
Hide file tree
Showing 26 changed files with 608 additions and 122 deletions.
17 changes: 17 additions & 0 deletions examples/tv-app/android/App/.idea/deploymentTargetDropDown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
android:supportsRtl="true"
android:enabled="true"
android:theme="@style/Theme.ContentApp">
<meta-data android:name="com.matter.app_agent_api.clusters" android:resource="@raw/static_matter_clusters" />
<meta-data android:name="com.matter.tv.app.api.clusters" android:resource="@raw/static_matter_clusters" />
<meta-data android:name="com.matter.tv.app.api.vendor_name" android:value="CSA" />
<meta-data android:name="com.matter.tv.app.api.vendor_id" android:value="1234" />
<meta-data android:name="com.matter.tv.app.api.product_id" android:value="5678" />
<activity
android:name="com.example.contentapp.MainActivity"
android:exported="true">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public void onReceive(Context context, Intent intent) {
new StringBuilder()
.append("Received matter command: ")
.append(intent.getAction())
.append(". Payload : ")
.append(new String(commandPayload))
.toString());

PendingIntent pendingIntent =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
{
"clusters": [
{
"identifier": "0x050a",
"features": "CS"
"identifier": 1289,
"features": ["NV", "LK", "NK"]
},
{
"identifier": "0x0506",
"features": "AS",
"optionalCommands" : [4, 5]
"identifier": 1290,
"features": ["CS"],
},
{
"identifier": 1286,
"features": ["AS"],
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,12 @@
import com.matter.tv.server.fragments.ContentAppFragment;
import com.matter.tv.server.fragments.QrCodeFragment;
import com.matter.tv.server.fragments.TerminalFragment;
import com.matter.tv.server.receivers.ContentAppDiscoveryService;
import java.util.LinkedHashMap;

public class MainActivity extends AppCompatActivity {

private LinkedHashMap<String, String> packages = new LinkedHashMap<>();

@Override
protected void onRestart() {
super.onRestart();
packages.clear();
ContentAppDiscoveryService.getReceiverInstance()
.initializeMatterApps(this.getApplicationContext());
}

private BottomNavigationView.OnNavigationItemSelectedListener navListener =
item -> {
Fragment selectedFragment = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.matter.tv.server.R;
import com.matter.tv.server.model.ContentApp;
import com.matter.tv.server.receivers.ContentAppDiscoveryService;
import com.matter.tv.server.service.ContentAppAgentService;
import com.matter.tv.server.service.MatterServant;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map.Entry;

/**
* A simple {@link Fragment} subclass. Use the {@link ContentAppFragment#newInstance} factory method
Expand All @@ -33,8 +32,6 @@ public class ContentAppFragment extends Fragment {
private BroadcastReceiver broadcastReceiver;
private ListView pkgUpdatesView;

private LinkedHashMap<String, String> packages = new LinkedHashMap<>();

public ContentAppFragment() {
// Required empty public constructor
}
Expand Down Expand Up @@ -65,8 +62,10 @@ public void onCreate(Bundle savedInstanceState) {
public void onResume() {
super.onResume();

ArrayList<Entry<String, String>> lst = new ArrayList<>(packages.entrySet());

ContentAppDiscoveryService.getReceiverInstance().registerSelf(getContext());
ArrayList<String> lst =
new ArrayList<String>(
ContentAppDiscoveryService.getReceiverInstance().getDiscoveredContentApps().keySet());
ContentAppListAdapter adapter =
new ContentAppListAdapter(getContext(), R.layout.applist_item, lst);

Expand All @@ -82,14 +81,12 @@ public View onCreateView(
return inflater.inflate(R.layout.fragment_content_app, container, false);
}

private class ContentAppListAdapter extends ArrayAdapter<Entry<String, String>> {
private class ContentAppListAdapter extends ArrayAdapter<String> {

private int layout;

public ContentAppListAdapter(
@NonNull Context context,
int resource,
@NonNull ArrayList<Entry<String, String>> packages) {
@NonNull Context context, int resource, @NonNull ArrayList<String> packages) {
super(context, resource, packages);
layout = resource;
}
Expand All @@ -104,22 +101,25 @@ public View getView(int position, @Nullable View convertView, @NonNull ViewGroup

ViewHolder viewHolder = new ViewHolder();
viewHolder.appName = convertView.findViewById(R.id.appNameTextView);
viewHolder.appDetails = convertView.findViewById(R.id.appDetailsTextView);
viewHolder.appName.setText(getItem(position).getKey());
viewHolder.appDetails.setText(getItem(position).getValue());
viewHolder.appName.setText(getItem(position));
viewHolder.sendMessageButton = convertView.findViewById(R.id.sendMessageButton);
viewHolder.sendMessageButton.setText(R.string.send_command);
viewHolder.sendMessageButton.setOnClickListener(
view -> {
Log.i(TAG, "Button was clicked for " + position);
ContentAppAgentService.sendCommand(
getActivity().getApplicationContext(), getItem(position).getKey());
for (ContentApp app :
ContentAppDiscoveryService.getReceiverInstance()
.getDiscoveredContentApps()
.values()) {
if (app.getAppName().equals(getItem(position))) {
MatterServant.get().sendTestMessage(app.getEndpointId(), "My Native Message");
}
}
});
convertView.setTag(viewHolder);
} else {
mainViewHolder = (ViewHolder) convertView.getTag();
mainViewHolder.appName.setText(getItem(position).getKey());
mainViewHolder.appDetails.setText(getItem(position).getValue());
mainViewHolder.appName.setText(getItem(position));
}
return convertView;
}
Expand All @@ -132,18 +132,14 @@ private void registerReceiver(ArrayAdapter adapter) {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
String packageName = intent.getStringExtra("com.matter.tv.server.appagent.add.pkg");
if (action.equals("com.matter.tv.server.appagent.add")) {
packages.put(
packageName, intent.getStringExtra("com.matter.tv.server.appagent.add.clusters"));
if (action.equals("com.matter.tv.server.appagent.add")
|| action.equals("com.matter.tv.server.appagent.remove")) {
adapter.clear();
adapter.addAll(packages.entrySet().toArray());
adapter.addAll(
ContentAppDiscoveryService.getReceiverInstance()
.getDiscoveredContentApps()
.entrySet());
adapter.notifyDataSetChanged();
} else if (action.equals("com.matter.tv.server.appagent.remove")) {
if (packages.remove(packageName) != null) {
adapter.clear();
adapter.addAll(packages.entrySet().toArray());
adapter.notifyDataSetChanged();
}
}
}
};
Expand All @@ -152,14 +148,10 @@ public void onReceive(Context context, Intent intent) {
getContext()
.registerReceiver(
broadcastReceiver, new IntentFilter("com.matter.tv.server.appagent.remove"));

ContentAppDiscoveryService.getReceiverInstance().registerSelf(getContext());
ContentAppDiscoveryService.getReceiverInstance().initializeMatterApps(getContext());
}

public class ViewHolder {
TextView appName;
TextView appDetails;
Button sendMessageButton;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.matter.tv.server.handlers;

import android.content.Context;
import android.util.Log;
import com.matter.tv.server.model.ContentApp;
import com.matter.tv.server.receivers.ContentAppDiscoveryService;
import com.matter.tv.server.service.ContentAppAgentService;
import com.tcl.chip.tvapp.ContentAppEndpointManager;

public class ContentAppEndpointManagerImpl implements ContentAppEndpointManager {

private static final String TAG = "MatterMainActivity";
private final Context context;

public ContentAppEndpointManagerImpl(Context context) {
this.context = context;
}

public String sendCommand(int endpointId, String commandPayload) {
Log.d(TAG, "Received a command for endpointId " + endpointId + ". Message " + commandPayload);
for (ContentApp app :
ContentAppDiscoveryService.getReceiverInstance().getDiscoveredContentApps().values()) {
if (app.getEndpointId() == endpointId) {
Log.d(
TAG, "Sending a command for endpointId " + endpointId + ". Message " + commandPayload);
ContentAppAgentService.sendCommand(context, app.getAppName(), commandPayload);
}
}
return "Success";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.matter.tv.server.model;

import com.matter.tv.app.api.SupportedCluster;
import java.util.Collections;
import java.util.Set;

public class ContentApp {

private String appName;
private String vendorName;
private int vendorId;
private int productId;
private Set<SupportedCluster> supportedClusters;
private int endpoint;

public ContentApp(
String appName,
String vendorName,
int vendorId,
int productId,
Set<SupportedCluster> supportedClusters) {
this.vendorName = vendorName;
this.appName = appName;
this.vendorId = vendorId;
this.productId = productId;
this.supportedClusters = supportedClusters;
}

public String getAppName() {
return appName;
}

public String getVendorName() {
return vendorName;
}

public int getVendorId() {
return vendorId;
}

public int getProductId() {
return productId;
}

public int getEndpointId() {
return endpoint;
}

public void setEndpointId(int endpoint) {
this.endpoint = endpoint;
}

public Set<SupportedCluster> getSupportedClusters() {
return Collections.unmodifiableSet(supportedClusters);
}
}
Loading

0 comments on commit 1017480

Please sign in to comment.