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.
  • Loading branch information
amitnj committed May 20, 2022
1 parent 55ef609 commit 22ba274
Show file tree
Hide file tree
Showing 25 changed files with 574 additions and 113 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 @@ -22,6 +22,8 @@
import chip.setuppayload.DiscoveryCapability;
import chip.setuppayload.SetupPayload;
import chip.setuppayload.SetupPayloadParser;

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;
Expand All @@ -40,14 +42,9 @@ public class MainActivity extends AppCompatActivity {
private BroadcastReceiver broadcastReceiver;
private ListView pkgUpdatesView;

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

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

@Override
Expand Down Expand Up @@ -113,9 +110,8 @@ public void onStopTrackingTouch(SeekBar seekBar) {}
e.printStackTrace();
}

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

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

pkgUpdatesView = findViewById(R.id.pkgUpdates);
Expand All @@ -130,37 +126,25 @@ 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();
}
}
}
};
registerReceiver(broadcastReceiver, new IntentFilter("com.matter.tv.server.appagent.add"));
registerReceiver(broadcastReceiver, new IntentFilter("com.matter.tv.server.appagent.remove"));

ContentAppDiscoveryService.getReceiverInstance().registerSelf(this.getApplicationContext());
ContentAppDiscoveryService.getReceiverInstance()
.initializeMatterApps(this.getApplicationContext());
}

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 ArrayList<String> packages) {
super(context, resource, packages);
layout = resource;
}
Expand All @@ -174,33 +158,32 @@ public View getView(int position, @Nullable View convertView, @NonNull ViewGroup
convertView = inflator.inflate(layout, parent, false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.appName = (TextView) convertView.findViewById(R.id.appNameTextView);
viewHolder.appDetails = (TextView) convertView.findViewById(R.id.appDetailsTextView);
viewHolder.appName.setText(getItem(position).getKey());
viewHolder.appDetails.setText(getItem(position).getValue());
viewHolder.appName.setText(getItem(position));
viewHolder.sendMessageButton = (Button) convertView.findViewById(R.id.sendMessageButton);
viewHolder.sendMessageButton.setText(R.string.send_command);
viewHolder.sendMessageButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.i(TAG, "Button was clicked for " + position);
ContentAppAgentService.sendCommand(
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;
}
}

public class ViewHolder {
TextView appName;
TextView appDetails;
Button sendMessageButton;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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.matter.tv.server.service.MatterServant;
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,53 @@
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 22ba274

Please sign in to comment.