Skip to content

Commit

Permalink
Refactor to separate app platform code. support dynamic discovery. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
amitnj authored and pull[bot] committed Sep 1, 2023
1 parent d099d51 commit c84a442
Show file tree
Hide file tree
Showing 17 changed files with 313 additions and 191 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
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.service.MatterServant;
import com.matter.tv.server.service.AppPlatformService;
import java.util.LinkedHashMap;

public class MainActivity extends AppCompatActivity {
Expand Down Expand Up @@ -43,7 +43,7 @@ protected void onCreate(Bundle savedInstanceState) {

// MainActivity is needed to launch dialog prompt
// in UserPrompter
MatterServant.get().setActivity(this);
AppPlatformService.get().setActivity(this);

BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnItemSelectedListener(navListener);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,17 @@
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import androidx.annotation.NonNull;
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.MatterServant;
import java.util.ArrayList;

/**
Expand Down Expand Up @@ -99,20 +95,6 @@ public View getView(int position, @Nullable View convertView, @NonNull ViewGroup
ViewHolder viewHolder = new ViewHolder();
viewHolder.appName = convertView.findViewById(R.id.appNameTextView);
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);
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();
Expand All @@ -128,9 +110,11 @@ private void registerReceiver(ArrayAdapter adapter) {
@Override
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")
|| action.equals("com.matter.tv.server.appagent.remove")) {
String packageName =
intent.getStringExtra(
ContentAppDiscoveryService.DISCOVERY_APPAGENT_EXTRA_PACKAGENAME);
if (action.equals(ContentAppDiscoveryService.DISCOVERY_APPAGENT_ACTION_ADD)
|| action.equals(ContentAppDiscoveryService.DISCOVERY_APPAGENT_ACTION_REMOVE)) {
adapter.clear();
adapter.addAll(
ContentAppDiscoveryService.getReceiverInstance()
Expand All @@ -141,14 +125,16 @@ public void onReceive(Context context, Intent intent) {
}
};
getContext()
.registerReceiver(broadcastReceiver, new IntentFilter("com.matter.tv.server.appagent.add"));
.registerReceiver(
broadcastReceiver,
new IntentFilter(ContentAppDiscoveryService.DISCOVERY_APPAGENT_ACTION_ADD));
getContext()
.registerReceiver(
broadcastReceiver, new IntentFilter("com.matter.tv.server.appagent.remove"));
broadcastReceiver,
new IntentFilter(ContentAppDiscoveryService.DISCOVERY_APPAGENT_ACTION_REMOVE));
}

public class ViewHolder {
TextView appName;
Button sendMessageButton;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ public class ContentAppDiscoveryService extends BroadcastReceiver {
private static final String ANDROID_PACKAGE_ADDED_ACTION = "android.intent.action.PACKAGE_ADDED";
private static final String ANDROID_PACKAGE_REPLACED_ACTION =
"android.intent.action.PACKAGE_REPLACED";
public static final String DISCOVERY_APPAGENT_ACTION_ADD = "com.matter.tv.server.appagent.add";
public static final String DISCOVERY_APPAGENT_ACTION_REMOVE =
"com.matter.tv.server.appagent.remove";
public static final String DISCOVERY_APPAGENT_EXTRA_PACKAGENAME =
"com.matter.tv.server.appagent.pkg";
public static final String DISCOVERY_APPAGENT_EXTRA_ENDPOINTID =
"com.matter.tv.server.appagent.endpointId";

private static ResourceUtils resourceUtils = ResourceUtils.getInstance();

Expand Down Expand Up @@ -105,9 +112,9 @@ private void handlePackageAdded(Context context, String pkg) {
ContentApp app = new ContentApp(pkg, vendorName, vendorId, productId, supportedClusters);
applications.put(pkg, app);

Intent in = new Intent("com.matter.tv.server.appagent.add");
Intent in = new Intent(DISCOVERY_APPAGENT_ACTION_ADD);
Bundle extras = new Bundle();
extras.putString("com.matter.tv.server.appagent.add.pkg", pkg);
extras.putString(DISCOVERY_APPAGENT_EXTRA_PACKAGENAME, pkg);
in.putExtras(extras);
context.sendBroadcast(in);
} catch (PackageManager.NameNotFoundException e) {
Expand All @@ -117,15 +124,19 @@ private void handlePackageAdded(Context context, String pkg) {

private void handlePackageRemoved(final Intent intent, final Context context) {
String pkg = intent.getData().getSchemeSpecificPart();
Log.i(TAG, pkg + " Removed.");

applications.remove(pkg);

Intent in = new Intent("com.matter.tv.server.appagent.remove");
Bundle extras = new Bundle();
extras.putString("com.matter.tv.server.appagent.add.pkg", pkg);
in.putExtras(extras);
context.sendBroadcast(in);
ContentApp contentApp = applications.get(pkg);
if (contentApp != null) {
applications.remove(pkg);
Intent in = new Intent(DISCOVERY_APPAGENT_ACTION_REMOVE);
Bundle extras = new Bundle();
extras.putString(DISCOVERY_APPAGENT_EXTRA_PACKAGENAME, pkg);
extras.putInt(DISCOVERY_APPAGENT_EXTRA_ENDPOINTID, contentApp.getEndpointId());
in.putExtras(extras);
context.sendBroadcast(in);
Log.i(TAG, "Removing Matter content app " + pkg);
} else {
Log.i(TAG, "App not found in set of Matter content apps. Doing nothing for app " + pkg);
}
}

public void registerSelf(Context context) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Copyright (c) 2021-2022 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.matter.tv.server.service;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import androidx.annotation.NonNull;
import com.matter.tv.server.MatterCommissioningPrompter;
import com.matter.tv.server.handlers.ContentAppEndpointManagerImpl;
import com.matter.tv.server.model.ContentApp;
import com.matter.tv.server.receivers.ContentAppDiscoveryService;
import com.matter.tv.server.tvapp.AppPlatform;

public class AppPlatformService {

private AppPlatform mAppPlatform;
private BroadcastReceiver mBroadcastReceiver;

private AppPlatformService() {}

private static class SingletonHolder {
static AppPlatformService instance = new AppPlatformService();
}

public static AppPlatformService get() {
return SingletonHolder.instance;
}

private Context context;
private Activity activity;

public void init(@NonNull Context context) {
this.context = context;
mAppPlatform =
new AppPlatform(
new MatterCommissioningPrompter(activity), new ContentAppEndpointManagerImpl(context));
ContentAppDiscoveryService.getReceiverInstance().registerSelf(context.getApplicationContext());
for (ContentApp app :
ContentAppDiscoveryService.getReceiverInstance().getDiscoveredContentApps().values()) {
addContentApp(app);
}
registerReceiver();
}

private void registerReceiver() {
mBroadcastReceiver =
new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
String packageName =
intent.getStringExtra(
ContentAppDiscoveryService.DISCOVERY_APPAGENT_EXTRA_PACKAGENAME);
if (action.equals(ContentAppDiscoveryService.DISCOVERY_APPAGENT_ACTION_ADD)) {
ContentApp app =
ContentAppDiscoveryService.getReceiverInstance()
.getDiscoveredContentApps()
.get(packageName);
addContentApp(app);
} else if (action.equals(ContentAppDiscoveryService.DISCOVERY_APPAGENT_ACTION_REMOVE)) {
int endpointId =
intent.getIntExtra(
ContentAppDiscoveryService.DISCOVERY_APPAGENT_EXTRA_ENDPOINTID, -1);
if (endpointId != -1) {
removeContentApp(endpointId);
}
}
}
};
context.registerReceiver(
mBroadcastReceiver,
new IntentFilter(ContentAppDiscoveryService.DISCOVERY_APPAGENT_ACTION_ADD));
context.registerReceiver(
mBroadcastReceiver,
new IntentFilter(ContentAppDiscoveryService.DISCOVERY_APPAGENT_ACTION_REMOVE));
}

public void setActivity(Activity activity) {
this.activity = activity;
}

public void addContentApp(ContentApp app) {
app.setEndpointId(
mAppPlatform.addContentApp(
app.getVendorName(),
app.getVendorId(),
app.getAppName(),
app.getProductId(),
"1.0",
new ContentAppEndpointManagerImpl(context)));
}

public int removeContentApp(int endpointID) {
return mAppPlatform.removeContentApp(endpointID);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*/
package com.matter.tv.server.service;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import androidx.annotation.NonNull;
Expand All @@ -30,9 +29,6 @@
import chip.platform.NsdManagerServiceResolver;
import chip.platform.PreferencesConfigurationManager;
import chip.platform.PreferencesKeyValueStoreManager;
import com.matter.tv.server.MatterCommissioningPrompter;
import com.matter.tv.server.handlers.ContentAppEndpointManagerImpl;
import com.matter.tv.server.model.ContentApp;
import com.matter.tv.server.tvapp.ChannelManagerStub;
import com.matter.tv.server.tvapp.Clusters;
import com.matter.tv.server.tvapp.ContentLaunchManagerStub;
Expand Down Expand Up @@ -69,7 +65,6 @@ public static MatterServant get() {
}

private Context context;
private Activity activity;

public void init(@NonNull Context context) {

Expand Down Expand Up @@ -117,7 +112,6 @@ public void init(@NonNull Context context) {
}
});
mTvApp.setDACProvider(new DACProviderStub());
mTvApp.setUserPrompter(new MatterCommissioningPrompter(activity));

mTvApp.setChipDeviceEventProvider(
new DeviceEventProvider() {
Expand All @@ -144,8 +138,6 @@ public void onCommissioningComplete() {

chipAppServer = new ChipAppServer();
chipAppServer.startApp();

mTvApp.postServerInit(new ContentAppEndpointManagerImpl(context));
}

public void restart() {
Expand All @@ -158,10 +150,6 @@ public void toggleOnOff() {
mIsOn = !mIsOn;
}

public void setActivity(Activity activity) {
this.activity = activity;
}

public void sendCustomCommand(String customCommand) {
Log.i(MatterServant.class.getName(), customCommand);
// TODO: insert logic ot send custom command here
Expand All @@ -170,18 +158,4 @@ public void sendCustomCommand(String customCommand) {
public void updateLevel(int value) {
mTvApp.setCurrentLevel(mLevelEndpoint, value);
}

public int addContentApp(ContentApp app) {
return mTvApp.addContentApp(
app.getVendorName(),
app.getVendorId(),
app.getAppName(),
app.getProductId(),
"1.0",
new ContentAppEndpointManagerImpl(context));
}

public void sendTestMessage(int endpoint, String message) {
mTvApp.sendTestMessage(endpoint, message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
import androidx.core.app.NotificationCompat;
import com.matter.tv.server.MainActivity;
import com.matter.tv.server.R;
import com.matter.tv.server.model.ContentApp;
import com.matter.tv.server.receivers.ContentAppDiscoveryService;

public class MatterServantService extends Service {
private static final String CHANNEL_ID = "Matter";
Expand All @@ -24,12 +22,7 @@ public void onCreate() {
// Start Matter Server
MatterServant.get().init(this.getApplicationContext());

// Register for packages updates
ContentAppDiscoveryService.getReceiverInstance().registerSelf(this.getApplicationContext());
for (ContentApp app :
ContentAppDiscoveryService.getReceiverInstance().getDiscoveredContentApps().values()) {
app.setEndpointId(MatterServant.get().addContentApp(app));
}
AppPlatformService.get().init(this.getApplicationContext());
}

@Nullable
Expand Down
3 changes: 3 additions & 0 deletions examples/tv-app/android/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ shared_library("jni") {
"include/target-navigator/TargetNavigatorManager.h",
"java/AppImpl.cpp",
"java/AppImpl.h",
"java/AppPlatform-JNI.cpp",
"java/AppPlatform-JNI.h",
"java/AppPlatformShellCommands-JNI.cpp",
"java/AppPlatformShellCommands-JNI.h",
"java/ChannelManager.cpp",
Expand Down Expand Up @@ -105,6 +107,7 @@ android_library("java") {
]

sources = [
"java/src/com/matter/tv/server/tvapp/AppPlatform.java",
"java/src/com/matter/tv/server/tvapp/AppPlatformShellCommands.java",
"java/src/com/matter/tv/server/tvapp/ChannelInfo.java",
"java/src/com/matter/tv/server/tvapp/ChannelLineupInfo.java",
Expand Down
Loading

0 comments on commit c84a442

Please sign in to comment.